diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..40fcdd5 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +pipeline { + agent { + label 'Hadoop' + } + + options { + disableConcurrentBuilds() + buildDiscarder(logRotator(daysToKeepStr: '14')) + timeout(time: 2, unit: 'HOURS') + timestamps() + } + + triggers { + cron('@daily') + } + + stages { + stage('Prepare') { + matrix { + agent any + axes { + axis { + name 'JAVA_VERSION' + values 'jdk_1.8_latest', 'jdk_11_latest' + } + } + + tools { + maven "maven_latest" + jdk "${JAVA_VERSION}" + } + + stages { + stage('BuildAndTest') { + steps { + sh "git clean -fxd" + sh "mvn verify spotbugs:check checkstyle:check -Pfull-build -Dsurefire-forkcount=4" + } + post { + always { + junit '**/target/surefire-reports/TEST-*.xml' + archiveArtifacts '**/target/*.jar' + } + // Jenkins pipeline jobs fill slaves on PRs without this :( + cleanup() { + script { + sh label: 'Cleanup workspace', script: ''' + # See HADOOP-13951 + chmod -R u+rxw "${WORKSPACE}" + ''' + deleteDir() + } + } + } + } + } + } + } + } +} diff --git a/Jenkinsfile-PreCommit b/Jenkinsfile-PreCommit new file mode 100644 index 0000000..600ad1d --- /dev/null +++ b/Jenkinsfile-PreCommit @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +pipeline { + agent { + label 'Hadoop' + } + + options { + disableConcurrentBuilds() + buildDiscarder(logRotator(daysToKeepStr: '14')) + timeout(time: 2, unit: 'HOURS') + timestamps() + } + + tools { + maven "maven_latest" + jdk "jdk_1.8_latest" + } + + stages { + stage('BuildAndTest') { + steps { + git 'https://github.com/apache/zookeeper' + sh "git clean -fxd" + sh "mvn verify spotbugs:check checkstyle:check -Pfull-build -Dsurefire-forkcount=4" + } + post { + always { + junit '**/target/surefire-reports/TEST-*.xml' + } + } + } + } + + post { + // Jenkins pipeline jobs fill slaves on PRs without this :( + cleanup() { + script { + sh label: 'Cleanup workspace', script: ''' + # See HADOOP-13951 + chmod -R u+rxw "${WORKSPACE}" + ''' + deleteDir() + } + } + } +} diff --git a/Jenkinsfile-owasp b/Jenkinsfile-owasp new file mode 100644 index 0000000..03ba497 --- /dev/null +++ b/Jenkinsfile-owasp @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +pipeline { + agent { + label 'Hadoop' + } + + options { + buildDiscarder(logRotator(daysToKeepStr: '14')) + timeout(time: 2, unit: 'HOURS') + timestamps() + } + + tools { + maven "maven_latest" + jdk "jdk_1.8_latest" + } + + stages { + stage('BuildAndTest') { + steps { + sh "git clean -fxd" + sh "mvn clean package -DskipTests dependency-check:check" + } + post { + always { + archiveArtifacts '**/target/dependency-check-*' + } + } + } + } + + post { + // Jenkins pipeline jobs fill slaves on PRs without this :( + cleanup() { + script { + sh label: 'Cleanup workspace', script: ''' + # See HADOOP-13951 + chmod -R u+rxw "${WORKSPACE}" + ''' + deleteDir() + } + } + } +} diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,202 @@ + + 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/NOTICE.txt b/NOTICE.txt new file mode 100644 index 0000000..afff440 --- /dev/null +++ b/NOTICE.txt @@ -0,0 +1,11 @@ +Apache ZooKeeper +Copyright 2009-2021 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +This product includes software components originally +developed for Airlift (https://github.com/airlift/airlift), +licensed under the Apache 2.0 license. The licensing terms +for Airlift code can be found at: +https://github.com/airlift/airlift/blob/master/LICENSE diff --git a/README_packaging.md b/README_packaging.md new file mode 100644 index 0000000..9c87e4c --- /dev/null +++ b/README_packaging.md @@ -0,0 +1,76 @@ +# README file for Packaging Notes + +The ZooKeeper project publishes releases as tarballs. For ZooKeeper packages +specific to your OS (such as rpm and deb), consider using Apache Bigtop: + +http://bigtop.apache.org/ + + +## Requirements + +- you need maven to build the java code +- gcc, cppunit, openssl and python-setuptools are required to build C and python bindings. (only needed when using `-Pfull-build`) + +On RHEL machine: + +``` +yum install cppunit +yum install python-setuptools +yum install openssl openssl-devel +``` + +On Ubuntu (in case of 16.4+): + +``` +apt-get install libcppunit-dev +apt-get install python-setuptools python2.7-dev +apt-get install openssl libssl-dev +``` + + +## Package build command (using maven) + +Commands to clean everything and build the tarball package without executing the tests: `mvn clean install -DskipTests` + + +`zookeeper-assembly/target/apache-zookeeper--bin.tar.gz` tarball file structure layout: + +- `/bin` - User executables +- `/conf` - Configuration files +- `/lib` - ZooKeeper JAR files and all the required java library dependencies +- `/docs` - Documents + +Beside the binary tarball, you can find the whole original source project packaged into: +`zookeeper-assembly/target/apache-zookeeper-.tar.gz` + + +### Building the C client (using maven) + +To also build the C client, you need to activate the `full-build` profile: + +``` +mvn clean -Pfull-build +mvn install -Pfull-build -DskipTests +``` + +Optional parameters you might consider when using maven: +- `-Pfull-build` - activates the full-build profile, causing the C client to be built +- `-DskipTests` - this parameter will skip both java and C++ unit test execution during the build +- `-Pc-test-coverage` - activates the test coverage calculation during the execution of C client tests +- `-Dc-client-openssl` - specify ssl support and openssl library location. Default value: `yes`, resulting in + the autodetection of the openssl library. If the openssl library will not be detected, + then a warning will be shown and the C client will be compiled without SSL support. + Use `-Dc-client-openssl=no` to explicitly disable SSL feature in C client. Or use + `-Dc-client-openssl=/path/to/openssl/` if you want to use a non-default / specific + openssl library location. + +Please note: if you don't provide the `-Pfull-build` parameter, then the C client will not be built, the C client tests +will not be executed and the previous C client builds will no be cleaned up (e.g. with simply using `mvn clean`). + +The compiled C client can be found here: +- `zookeeper-client/zookeeper-client-c/target/c/bin` - User executable +- `zookeeper-client/zookeeper-client-c/target/c/lib` - Native libraries +- `zookeeper-client/zookeeper-client-c/target/c/include/zookeeper` - Native library headers + +The same folders gets archived to the `zookeeper-assembly/target/apache-zookeeper--lib.tar.gz` file, assuming +you activated the `full-build` maven profile. diff --git a/bin/README.txt b/bin/README.txt new file mode 100644 index 0000000..e70506d --- /dev/null +++ b/bin/README.txt @@ -0,0 +1,6 @@ +This directory contain scripts that allow easy access (classpath in particular) +to the ZooKeeper server and command line client. + +Files ending in .sh are unix and cygwin compatible + +Files ending in .cmd are msdos/windows compatible diff --git a/bin/zkCleanup.sh b/bin/zkCleanup.sh new file mode 100644 index 0000000..4553241 --- /dev/null +++ b/bin/zkCleanup.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# This script cleans up old transaction logs and snapshots +# + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# use POSIX interface, symlink is followed automatically +ZOOBIN="${BASH_SOURCE-$0}" +ZOOBIN="$(dirname "${ZOOBIN}")" +ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" + +if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then + . "$ZOOBINDIR"/../libexec/zkEnv.sh +else + . "$ZOOBINDIR"/zkEnv.sh +fi + +ZOODATADIR="$(grep "^[[:space:]]*dataDir=" "$ZOOCFG" | sed -e 's/.*=//')" +ZOODATALOGDIR="$(grep "^[[:space:]]*dataLogDir=" "$ZOOCFG" | sed -e 's/.*=//')" + +ZOO_LOG_FILE=zookeeper-$USER-cleanup-$HOSTNAME.log + +if [ "x$ZOODATALOGDIR" = "x" ] +then +"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \ + -cp "$CLASSPATH" $JVMFLAGS \ + org.apache.zookeeper.server.PurgeTxnLog "$ZOODATADIR" $* +else +"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \ + -cp "$CLASSPATH" $JVMFLAGS \ + org.apache.zookeeper.server.PurgeTxnLog "$ZOODATALOGDIR" "$ZOODATADIR" $* +fi diff --git a/bin/zkCli.cmd b/bin/zkCli.cmd new file mode 100644 index 0000000..c3b0a5d --- /dev/null +++ b/bin/zkCli.cmd @@ -0,0 +1,26 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0zkEnv.cmd" + +set ZOO_LOG_FILE=zookeeper-%USERNAME%-cli-%COMPUTERNAME%.log + +set ZOOMAIN=org.apache.zookeeper.ZooKeeperMain +call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" -cp "%CLASSPATH%" %ZOOMAIN% %* + +endlocal + diff --git a/bin/zkCli.sh b/bin/zkCli.sh new file mode 100644 index 0000000..ecf9a45 --- /dev/null +++ b/bin/zkCli.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# This script cleans up old transaction logs and snapshots +# + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# use POSIX interface, symlink is followed automatically +ZOOBIN="${BASH_SOURCE-$0}" +ZOOBIN="$(dirname "${ZOOBIN}")" +ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" + +if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then + . "$ZOOBINDIR"/../libexec/zkEnv.sh +else + . "$ZOOBINDIR"/zkEnv.sh +fi + +ZOO_LOG_FILE=zookeeper-$USER-cli-$HOSTNAME.log + +"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \ + -cp "$CLASSPATH" $CLIENT_JVMFLAGS $JVMFLAGS \ + org.apache.zookeeper.ZooKeeperMain "$@" diff --git a/bin/zkEnv.cmd b/bin/zkEnv.cmd new file mode 100644 index 0000000..f761ec5 --- /dev/null +++ b/bin/zkEnv.cmd @@ -0,0 +1,54 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +set ZOOCFGDIR=%~dp0%..\conf +set ZOO_LOG_DIR=%~dp0%..\logs +set ZOO_LOG4J_PROP=INFO,CONSOLE + +REM for sanity sake assume Java 1.6 +REM see: http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html + +REM add the zoocfg dir to classpath +set CLASSPATH=%ZOOCFGDIR% + +REM make it work in the release +SET CLASSPATH=%~dp0..\*;%~dp0..\lib\*;%CLASSPATH% + +REM make it work for developers +SET CLASSPATH=%~dp0..\build\classes;%~dp0..\build\lib\*;%CLASSPATH% + +set ZOOCFG=%ZOOCFGDIR%\zoo.cfg + +@REM setup java environment variables + +if not defined JAVA_HOME ( + echo Error: JAVA_HOME is not set. + goto :eof +) + +set JAVA_HOME=%JAVA_HOME:"=% + +if not exist "%JAVA_HOME%"\bin\java.exe ( + echo Error: JAVA_HOME is incorrectly set: %JAVA_HOME% + echo Expected to find java.exe here: %JAVA_HOME%\bin\java.exe + goto :eof +) + +REM strip off trailing \ from JAVA_HOME or java does not start +if "%JAVA_HOME:~-1%" EQU "\" set "JAVA_HOME=%JAVA_HOME:~0,-1%" + +set JAVA="%JAVA_HOME%"\bin\java + diff --git a/bin/zkEnv.sh b/bin/zkEnv.sh new file mode 100644 index 0000000..8d93a50 --- /dev/null +++ b/bin/zkEnv.sh @@ -0,0 +1,148 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# This script should be sourced into other zookeeper +# scripts to setup the env variables + +# We use ZOOCFGDIR if defined, +# otherwise we use /etc/zookeeper +# or the conf directory that is +# a sibling of this script's directory. +# Or you can specify the ZOOCFGDIR using the +# '--config' option in the command line. + +ZOOBINDIR="${ZOOBINDIR:-/usr/bin}" +ZOOKEEPER_PREFIX="${ZOOBINDIR}/.." + +#check to see if the conf dir is given as an optional argument +if [ $# -gt 1 ] +then + if [ "--config" = "$1" ] + then + shift + confdir=$1 + shift + ZOOCFGDIR=$confdir + fi +fi + +if [ "x$ZOOCFGDIR" = "x" ] +then + if [ -e "${ZOOKEEPER_PREFIX}/conf" ]; then + ZOOCFGDIR="$ZOOBINDIR/../conf" + else + ZOOCFGDIR="$ZOOBINDIR/../etc/zookeeper" + fi +fi + +if [ -f "${ZOOCFGDIR}/zookeeper-env.sh" ]; then + . "${ZOOCFGDIR}/zookeeper-env.sh" +fi + +if [ "x$ZOOCFG" = "x" ] +then + ZOOCFG="zoo.cfg" +fi + +ZOOCFG="$ZOOCFGDIR/$ZOOCFG" + +if [ -f "$ZOOCFGDIR/java.env" ] +then + . "$ZOOCFGDIR/java.env" +fi + +if [ "x${ZOO_LOG_DIR}" = "x" ] +then + ZOO_LOG_DIR="$ZOOKEEPER_PREFIX/logs" +fi + +if [ "x${ZOO_LOG4J_PROP}" = "x" ] +then + ZOO_LOG4J_PROP="INFO,CONSOLE" +fi + +if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then + JAVA="$JAVA_HOME/bin/java" +elif type -p java; then + JAVA=java +else + echo "Error: JAVA_HOME is not set and java could not be found in PATH." 1>&2 + exit 1 +fi + +#add the zoocfg dir to classpath +CLASSPATH="$ZOOCFGDIR:$CLASSPATH" + +for i in "$ZOOBINDIR"/../zookeeper-server/src/main/resources/lib/*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work in the binary package +#(use array for LIBPATH to account for spaces within wildcard expansion) +if ls "${ZOOKEEPER_PREFIX}"/share/zookeeper/zookeeper-*.jar > /dev/null 2>&1; then + LIBPATH=("${ZOOKEEPER_PREFIX}"/share/zookeeper/*.jar) +else + #release tarball format + for i in "$ZOOBINDIR"/../zookeeper-*.jar + do + CLASSPATH="$i:$CLASSPATH" + done + LIBPATH=("${ZOOBINDIR}"/../lib/*.jar) +fi + +for i in "${LIBPATH[@]}" +do + CLASSPATH="$i:$CLASSPATH" +done + +#make it work for developers +for d in "$ZOOBINDIR"/../build/lib/*.jar +do + CLASSPATH="$d:$CLASSPATH" +done + +for d in "$ZOOBINDIR"/../zookeeper-server/target/lib/*.jar +do + CLASSPATH="$d:$CLASSPATH" +done + +#make it work for developers +CLASSPATH="$ZOOBINDIR/../build/classes:$CLASSPATH" + +#make it work for developers +CLASSPATH="$ZOOBINDIR/../zookeeper-server/target/classes:$CLASSPATH" + +case "`uname`" in + CYGWIN*|MINGW*) cygwin=true ;; + *) cygwin=false ;; +esac + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + +#echo "CLASSPATH=$CLASSPATH" + +# default heap for zookeeper server +ZK_SERVER_HEAP="${ZK_SERVER_HEAP:-1000}" +export SERVER_JVMFLAGS="-Xmx${ZK_SERVER_HEAP}m $SERVER_JVMFLAGS" + +# default heap for zookeeper client +ZK_CLIENT_HEAP="${ZK_CLIENT_HEAP:-256}" +export CLIENT_JVMFLAGS="-Xmx${ZK_CLIENT_HEAP}m $CLIENT_JVMFLAGS" diff --git a/bin/zkServer-initialize.sh b/bin/zkServer-initialize.sh new file mode 100644 index 0000000..062e265 --- /dev/null +++ b/bin/zkServer-initialize.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# use POSIX interface, symlink is followed automatically +ZOOBIN="${BASH_SOURCE-$0}" +ZOOBIN="$(dirname "${ZOOBIN}")" +ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" + +if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then + . "$ZOOBINDIR"/../libexec/zkEnv.sh +else + . "$ZOOBINDIR"/zkEnv.sh +fi + +usage() { + # the configfile will be properly formatted as long as the + # configfile path is less then 40 chars, otw the line will look a + # bit weird, but otherwise it's fine + printf "usage: $0 + Optional parameters: + -h Display this message + --help Display this message + --configfile=%-40s ZooKeeper config file + --myid=# Set the myid to be used, if any (1-255) + --force Force creation of the data/txnlog dirs +" "$ZOOCFG" + exit 1 +} + +if [ $? != 0 ] ; then + usage + exit 1 +fi + +initialize() { + if [ ! -e "$ZOOCFG" ]; then + echo "Unable to find config file at $ZOOCFG" + exit 1 + fi + + ZOO_DATADIR="$(grep "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')" + ZOO_DATALOGDIR="$(grep "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')" + + if [ -z "$ZOO_DATADIR" ]; then + echo "Unable to determine dataDir from $ZOOCFG" + exit 1 + fi + + if [ $FORCE ]; then + echo "Force enabled, data/txnlog directories will be re-initialized" + else + # we create if version-2 exists (ie real data), not the + # parent. See comments in following section for more insight + if [ -d "$ZOO_DATADIR/version-2" ]; then + echo "ZooKeeper data directory already exists at $ZOO_DATADIR (or use --force to force re-initialization)" + exit 1 + fi + + if [ -n "$ZOO_DATALOGDIR" ] && [ -d "$ZOO_DATALOGDIR/version-2" ]; then + echo "ZooKeeper txnlog directory already exists at $ZOO_DATALOGDIR (or use --force to force re-initialization)" + exit 1 + fi + fi + + # remove the child files that we're (not) interested in, not the + # parent. this allows for parent to be installed separately, and + # permissions to be set based on overarching requirements. by + # default we'll use the permissions of the user running this + # script for the files contained by the parent. note also by using + # -p the parent(s) will be created if it doesn't already exist + rm -rf "$ZOO_DATADIR/myid" 2>/dev/null >/dev/null + rm -rf "$ZOO_DATADIR/version-2" 2>/dev/null >/dev/null + mkdir -p "$ZOO_DATADIR/version-2" + + if [ -n "$ZOO_DATALOGDIR" ]; then + rm -rf "$ZOO_DATALOGDIR/myid" 2>/dev/null >/dev/null + rm -rf "$ZOO_DATALOGDIR/version-2" 2>/dev/null >/dev/null + mkdir -p "$ZOO_DATALOGDIR/version-2" + fi + + if [ $MYID ]; then + echo "Using myid of $MYID" + echo $MYID > "$ZOO_DATADIR/myid" + else + echo "No myid provided, be sure to specify it in $ZOO_DATADIR/myid if using non-standalone" + fi + + touch "$ZOO_DATADIR/initialize" +} + +while [ ! -z "$1" ]; do + case "$1" in + --configfile) + ZOOCFG=$2; shift 2 + ;; + --configfile=?*) + ZOOCFG=${1#*=}; shift 1 + ;; + --myid) + MYID=$2; shift 2 + ;; + --myid=?*) + MYID=${1#*=}; shift 1 + ;; + --force) + FORCE=1; shift 1 + ;; + -h) + usage + ;; + --help) + usage + ;; + *) + echo "Unknown option: $1" + usage + exit 1 + ;; + esac +done +initialize diff --git a/bin/zkServer.cmd b/bin/zkServer.cmd new file mode 100644 index 0000000..1de2213 --- /dev/null +++ b/bin/zkServer.cmd @@ -0,0 +1,26 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0zkEnv.cmd" + +set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain +set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log + +echo on +call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %* + +endlocal diff --git a/bin/zkServer.sh b/bin/zkServer.sh new file mode 100644 index 0000000..ac645bf --- /dev/null +++ b/bin/zkServer.sh @@ -0,0 +1,325 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# TODO_MA 马中华 注释: 如果一个大数据技术组件的底层源码实现是 java +# TODO_MA 马中华 注释: 那么 shell 命令的规范,就一定是先准备各种参数,然后通过 java 命令启动一个 JVM 执行某个类的 main() + + +# use POSIX interface, symlink is followed automatically +ZOOBIN="${BASH_SOURCE-$0}" +ZOOBIN="$(dirname "${ZOOBIN}")" +ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" + +if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then + . "$ZOOBINDIR"/../libexec/zkEnv.sh +else + . "$ZOOBINDIR"/zkEnv.sh +fi + +# See the following page for extensive details on setting +# up the JVM to accept JMX remote management: +# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html +# by default we allow local JMX connections +if [ "x$JMXLOCALONLY" = "x" ] +then + JMXLOCALONLY=false +fi + +if [ "x$JMXDISABLE" = "x" ] || [ "$JMXDISABLE" = 'false' ] +then + echo "ZooKeeper JMX enabled by default" >&2 + if [ "x$JMXPORT" = "x" ] + then + # for some reason these two options are necessary on jdk6 on Ubuntu + # accord to the docs they are not necessary, but otw jconsole cannot + # do a local attach + ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain" + else + if [ "x$JMXAUTH" = "x" ] + then + JMXAUTH=false + fi + if [ "x$JMXSSL" = "x" ] + then + JMXSSL=false + fi + if [ "x$JMXLOG4J" = "x" ] + then + JMXLOG4J=true + fi + echo "ZooKeeper remote JMX Port set to $JMXPORT" >&2 + echo "ZooKeeper remote JMX authenticate set to $JMXAUTH" >&2 + echo "ZooKeeper remote JMX ssl set to $JMXSSL" >&2 + echo "ZooKeeper remote JMX log4j set to $JMXLOG4J" >&2 + ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$JMXPORT -Dcom.sun.management.jmxremote.authenticate=$JMXAUTH -Dcom.sun.management.jmxremote.ssl=$JMXSSL -Dzookeeper.jmx.log4j.disable=$JMXLOG4J org.apache.zookeeper.server.quorum.QuorumPeerMain" + fi +else + echo "JMX disabled by user request" >&2 + # TODO_MA 马中华 注释: 运行主类 + # TODO_MA 马中华 注释:ZK JVM 进程的名称: QuorumPeerMain + # TODO_MA 马中华 注释:QuorumPeer + Main ==> 这个类的作用,就是启动 QuorumPeer + # TODO_MA 马中华 注释:QuorumPeer 代表了一台服务器的全部功能实现! + ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain" +fi + +if [ "x$SERVER_JVMFLAGS" != "x" ] +then + JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS" +fi + +if [ "x$2" != "x" ] +then + ZOOCFG="$ZOOCFGDIR/$2" +fi + +# if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR +if [ "x$(dirname "$ZOOCFG")" != "x$ZOOCFGDIR" ] +then + ZOOCFG="$2" +fi + +if $cygwin +then + ZOOCFG=`cygpath -wp "$ZOOCFG"` + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +echo "Using config: $ZOOCFG" >&2 + +case "$OSTYPE" in +*solaris*) + GREP=/usr/xpg4/bin/grep + ;; +*) + GREP=grep + ;; +esac +ZOO_DATADIR="$($GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//')" +ZOO_DATADIR="$(echo -e "${ZOO_DATADIR}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" +ZOO_DATALOGDIR="$($GREP "^[[:space:]]*dataLogDir" "$ZOOCFG" | sed -e 's/.*=//')" + +# iff autocreate is turned off and the datadirs don't exist fail +# immediately as we can't create the PID file, etc..., anyway. +if [ -n "$ZOO_DATADIR_AUTOCREATE_DISABLE" ]; then + if [ ! -d "$ZOO_DATADIR/version-2" ]; then + echo "ZooKeeper data directory is missing at $ZOO_DATADIR fix the path or run initialize" + exit 1 + fi + + if [ -n "$ZOO_DATALOGDIR" ] && [ ! -d "$ZOO_DATALOGDIR/version-2" ]; then + echo "ZooKeeper txnlog directory is missing at $ZOO_DATALOGDIR fix the path or run initialize" + exit 1 + fi + ZOO_DATADIR_AUTOCREATE="-Dzookeeper.datadir.autocreate=false" +fi + +if [ -z "$ZOOPIDFILE" ]; then + if [ ! -d "$ZOO_DATADIR" ]; then + mkdir -p "$ZOO_DATADIR" + fi + ZOOPIDFILE="$ZOO_DATADIR/zookeeper_server.pid" +else + # ensure it exists, otw stop will fail + mkdir -p "$(dirname "$ZOOPIDFILE")" +fi + +if [ ! -w "$ZOO_LOG_DIR" ] ; then +mkdir -p "$ZOO_LOG_DIR" +fi + +ZOO_LOG_FILE=zookeeper-$USER-server-$HOSTNAME.log +_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper-$USER-server-$HOSTNAME.out" + +# TODO_MA 马中华 注释: zkServer.sh start +# TODO_MA 马中华 注释: $1 = start +case $1 in +start) + echo -n "Starting zookeeper ... " + if [ -f "$ZOOPIDFILE" ]; then + if kill -0 `cat "$ZOOPIDFILE"` > /dev/null 2>&1; then + echo $command already running as process `cat "$ZOOPIDFILE"`. + exit 1 + fi + fi + + # TODO_MA 马中华 注释: 核心的启动命令4 + # TODO_MA 马中华 注释: $JAVA = $JAVA_HOME/bin/java + # TODO_MA 马中华 注释: $ZOOMAIN = 核心java类 = QuorumPeerMain + # TODO_MA 马中华 注释: "$ZOOCFG" = zoo.cfg 的路径 = $ZOOKEEPER_HOME/conf/zoo.cfg + nohup "$JAVA" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \ + "-Dzookeeper.log.file=${ZOO_LOG_FILE}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ + -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null & + if [ $? -eq 0 ] + then + case "$OSTYPE" in + *solaris*) + /bin/echo "${!}\\c" > "$ZOOPIDFILE" + ;; + *) + /bin/echo -n $! > "$ZOOPIDFILE" + ;; + esac + if [ $? -eq 0 ]; + then + sleep 1 + pid=$(cat "${ZOOPIDFILE}") + if ps -p "${pid}" > /dev/null 2>&1; then + echo STARTED + else + echo FAILED TO START + exit 1 + fi + else + echo FAILED TO WRITE PID + exit 1 + fi + else + echo SERVER DID NOT START + exit 1 + fi + ;; +start-foreground) + ZOO_CMD=(exec "$JAVA") + if [ "${ZOO_NOEXEC}" != "" ]; then + ZOO_CMD=("$JAVA") + fi + "${ZOO_CMD[@]}" $ZOO_DATADIR_AUTOCREATE "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" \ + "-Dzookeeper.log.file=${ZOO_LOG_FILE}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \ + -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ + -cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" + ;; +print-cmd) + echo "\"$JAVA\" $ZOO_DATADIR_AUTOCREATE -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" \ + -Dzookeeper.log.file=\"${ZOO_LOG_FILE}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" \ + -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' \ + -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null" + ;; +stop) + echo -n "Stopping zookeeper ... " + if [ ! -f "$ZOOPIDFILE" ] + then + echo "no zookeeper to stop (could not find file $ZOOPIDFILE)" + else + $KILL $(cat "$ZOOPIDFILE") + rm "$ZOOPIDFILE" + sleep 1 + echo STOPPED + fi + exit 0 + ;; +version) + ZOOMAIN=org.apache.zookeeper.version.VersionInfoMain + $JAVA -cp "$CLASSPATH" $ZOOMAIN 2> /dev/null + ;; +restart) + shift + "$0" stop ${@} + sleep 3 + "$0" start ${@} + ;; +status) + # -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output + isSSL="false" + clientPortAddress=`$GREP "^[[:space:]]*clientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'` + if ! [ $clientPortAddress ] + then + clientPortAddress="localhost" + fi + clientPort=`$GREP "^[[:space:]]*clientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'` + if ! [[ "$clientPort" =~ ^[0-9]+$ ]] + then + dataDir=`$GREP "^[[:space:]]*dataDir" "$ZOOCFG" | sed -e 's/.*=//'` + myid=`cat "$dataDir/myid" 2> /dev/null` + if ! [[ "$myid" =~ ^[0-9]+$ ]] ; then + echo "myid could not be determined, will not able to locate clientPort in the server configs." + else + clientPortAndAddress=`$GREP "^[[:space:]]*server.$myid=.*;.*" "$ZOOCFG" | sed -e 's/.*=//' | sed -e 's/.*;//'` + if [ ! "$clientPortAndAddress" ] ; then + echo "Client port not found in static config file. Looking in dynamic config file." + dynamicConfigFile=`$GREP "^[[:space:]]*dynamicConfigFile" "$ZOOCFG" | sed -e 's/.*=//'` + clientPortAndAddress=`$GREP "^[[:space:]]*server.$myid=.*;.*" "$dynamicConfigFile" | sed -e 's/.*=//' | sed -e 's/.*;//'` + fi + if [ ! "$clientPortAndAddress" ] ; then + echo "Client port not found in the server configs" + else + if [[ "$clientPortAndAddress" =~ ^.*:[0-9]+ ]] ; then + if [[ "$clientPortAndAddress" =~ \[.*\]:[0-9]+ ]] ; then + # Extracts address from address:port for example extracts 127::1 from "[127::1]:2181" + clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's|\[||' | sed -e 's|\]:.*||'` + else + clientPortAddress=`echo "$clientPortAndAddress" | sed -e 's/:.*//'` + fi + fi + clientPort=`echo "$clientPortAndAddress" | sed -e 's/.*://'` + fi + fi + fi + if [ ! "$clientPort" ] ; then + echo "Client port not found. Looking for secureClientPort in the static config." + secureClientPort=`$GREP "^[[:space:]]*secureClientPort[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'` + if [ "$secureClientPort" ] ; then + isSSL="true" + clientPort=$secureClientPort + clientPortAddress=`$GREP "^[[:space:]]*secureClientPortAddress[^[:alpha:]]" "$ZOOCFG" | sed -e 's/.*=//'` + if ! [ $clientPortAddress ] + then + clientPortAddress="localhost" + fi + else + echo "Unable to find either secure or unsecure client port in any configs. Terminating." + exit 1 + fi + fi + echo "Client port found: $clientPort. Client address: $clientPortAddress. Client SSL: $isSSL." + STAT=`"$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" "-Dzookeeper.log.file=${ZOO_LOG_FILE}" \ + -cp "$CLASSPATH" $CLIENT_JVMFLAGS $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain \ + $clientPortAddress $clientPort srvr $isSSL 2> /dev/null \ + | $GREP Mode` + if [ "x$STAT" = "x" ] + then + if [ "$isSSL" = "true" ] ; then + echo " " + echo "Note: We used secureClientPort ($secureClientPort) to establish connection, but we failed. The 'status'" + echo " command establishes a client connection to the server to execute diagnostic commands. Please make sure you" + echo " provided all the Client SSL connection related parameters in the CLIENT_JVMFLAGS environment variable! E.g.:" + echo " CLIENT_JVMFLAGS=\"-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty" + echo " -Dzookeeper.ssl.trustStore.location=/tmp/clienttrust.jks -Dzookeeper.ssl.trustStore.password=password" + echo " -Dzookeeper.ssl.keyStore.location=/tmp/client.jks -Dzookeeper.ssl.keyStore.password=password" + echo " -Dzookeeper.client.secure=true\" ./zkServer.sh status" + echo " " + fi + echo "Error contacting service. It is probably not running." + exit 1 + else + echo $STAT + exit 0 + fi + ;; +*) + echo "Usage: $0 [--config ] {start|start-foreground|stop|version|restart|status|print-cmd}" >&2 + +esac diff --git a/bin/zkSnapShotToolkit.cmd b/bin/zkSnapShotToolkit.cmd new file mode 100644 index 0000000..66c2db7 --- /dev/null +++ b/bin/zkSnapShotToolkit.cmd @@ -0,0 +1,24 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0zkEnv.cmd" + +set ZOOMAIN=org.apache.zookeeper.server.SnapshotFormatter +call %JAVA% -cp "%CLASSPATH%" %ZOOMAIN% %* + +endlocal + diff --git a/bin/zkSnapShotToolkit.sh b/bin/zkSnapShotToolkit.sh new file mode 100644 index 0000000..1664d59 --- /dev/null +++ b/bin/zkSnapShotToolkit.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# use POSIX interface, symlink is followed automatically +ZOOBIN="${BASH_SOURCE-$0}" +ZOOBIN="$(dirname "${ZOOBIN}")" +ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" + +if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then + . "$ZOOBINDIR"/../libexec/zkEnv.sh +else + . "$ZOOBINDIR"/zkEnv.sh +fi + +"$JAVA" -cp "$CLASSPATH" $JVMFLAGS \ + org.apache.zookeeper.server.SnapshotFormatter "$@" + + diff --git a/bin/zkTxnLogToolkit.cmd b/bin/zkTxnLogToolkit.cmd new file mode 100644 index 0000000..8fd9d53 --- /dev/null +++ b/bin/zkTxnLogToolkit.cmd @@ -0,0 +1,24 @@ +@echo off +REM Licensed to the Apache Software Foundation (ASF) under one or more +REM contributor license agreements. See the NOTICE file distributed with +REM this work for additional information regarding copyright ownership. +REM The ASF licenses this file to You under the Apache License, Version 2.0 +REM (the "License"); you may not use this file except in compliance with +REM the License. You may obtain a copy of the License at +REM +REM http://www.apache.org/licenses/LICENSE-2.0 +REM +REM Unless required by applicable law or agreed to in writing, software +REM distributed under the License is distributed on an "AS IS" BASIS, +REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +REM See the License for the specific language governing permissions and +REM limitations under the License. + +setlocal +call "%~dp0zkEnv.cmd" + +set ZOOMAIN=org.apache.zookeeper.server.persistence.TxnLogToolkit +call %JAVA% -cp "%CLASSPATH%" %ZOOMAIN% %* + +endlocal + diff --git a/bin/zkTxnLogToolkit.sh b/bin/zkTxnLogToolkit.sh new file mode 100644 index 0000000..8beed20 --- /dev/null +++ b/bin/zkTxnLogToolkit.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# use POSIX interface, symlink is followed automatically +ZOOBIN="${BASH_SOURCE-$0}" +ZOOBIN="$(dirname "${ZOOBIN}")" +ZOOBINDIR="$(cd "${ZOOBIN}"; pwd)" + +if [ -e "$ZOOBIN/../libexec/zkEnv.sh" ]; then + . "$ZOOBINDIR"/../libexec/zkEnv.sh +else + . "$ZOOBINDIR"/zkEnv.sh +fi + +"$JAVA" -cp "$CLASSPATH" $JVMFLAGS \ + org.apache.zookeeper.server.persistence.TxnLogToolkit "$@" + + diff --git a/checkstyle-simple.xml b/checkstyle-simple.xml new file mode 100644 index 0000000..2049370 --- /dev/null +++ b/checkstyle-simple.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/checkstyle-strict.xml b/checkstyle-strict.xml new file mode 100644 index 0000000..f4fa57e --- /dev/null +++ b/checkstyle-strict.xml @@ -0,0 +1,436 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/checkstyleSuppressions.xml b/checkstyleSuppressions.xml new file mode 100644 index 0000000..53d92eb --- /dev/null +++ b/checkstyleSuppressions.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/conf/configuration.xsl b/conf/configuration.xsl new file mode 100644 index 0000000..377cdbe --- /dev/null +++ b/conf/configuration.xsl @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + +
namevaluedescription
+ + +
+
diff --git a/conf/log4j.properties b/conf/log4j.properties new file mode 100644 index 0000000..9e12a38 --- /dev/null +++ b/conf/log4j.properties @@ -0,0 +1,82 @@ +# Copyright 2012 The Apache Software Foundation +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Define some default values that can be overridden by system properties +zookeeper.root.logger=INFO, CONSOLE + +zookeeper.console.threshold=INFO + +zookeeper.log.dir=. +zookeeper.log.file=zookeeper.log +zookeeper.log.threshold=INFO +zookeeper.log.maxfilesize=256MB +zookeeper.log.maxbackupindex=20 + +zookeeper.tracelog.dir=${zookeeper.log.dir} +zookeeper.tracelog.file=zookeeper_trace.log + +log4j.rootLogger=${zookeeper.root.logger} + +# +# console +# Add "console" to rootlogger above if you want to use this +# +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold} +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n + +# +# Add ROLLINGFILE to rootLogger to get log file output +# +log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender +log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold} +log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file} +log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize} +log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex} +log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout +log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n + +# +# Add TRACEFILE to rootLogger to get log file output +# Log TRACE level and above messages to a log file +# +log4j.appender.TRACEFILE=org.apache.log4j.FileAppender +log4j.appender.TRACEFILE.Threshold=TRACE +log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file} + +log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout +### Notice we are including log4j's NDC here (%x) +log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n +# +# zk audit logging +# +zookeeper.auditlog.file=zookeeper_audit.log +zookeeper.auditlog.threshold=INFO +audit.logger=INFO, RFAAUDIT +log4j.logger.org.apache.zookeeper.audit.Log4jAuditLogger=${audit.logger} +log4j.additivity.org.apache.zookeeper.audit.Log4jAuditLogger=false +log4j.appender.RFAAUDIT=org.apache.log4j.RollingFileAppender +log4j.appender.RFAAUDIT.File=${zookeeper.log.dir}/${zookeeper.auditlog.file} +log4j.appender.RFAAUDIT.layout=org.apache.log4j.PatternLayout +log4j.appender.RFAAUDIT.layout.ConversionPattern=%d{ISO8601} %p %c{2}: %m%n +log4j.appender.RFAAUDIT.Threshold=${zookeeper.auditlog.threshold} + +# Max log file size of 10MB +log4j.appender.RFAAUDIT.MaxFileSize=10MB +log4j.appender.RFAAUDIT.MaxBackupIndex=10 diff --git a/conf/zoo.cfg b/conf/zoo.cfg new file mode 100644 index 0000000..6e854b2 --- /dev/null +++ b/conf/zoo.cfg @@ -0,0 +1,36 @@ +# The number of milliseconds of each tick +tickTime=2000 +# The number of ticks that the initial synchronization phase can take +initLimit=10 +# The number of ticks that can pass between sending a request and getting an acknowledgement +syncLimit=5 +# the directory where the snapshot is stored. +# do not use /tmp for storage, /tmp here is just +# example sakes. +dataDir=/home/bigdata/data/zkdata +dataLogDir=/home/bigdata/data/zklog/ +# the port at which the clients will connect +clientPort=2181 +# the maximum number of client connections. +# increase this if you need to handle more clients +#maxClientCnxns=60 +# electionAlg=3 = FastLeaderElection +electionAlg=3 +maxClientCnxns=60 +# 服务器的类型: observer/participant(具有选举权的节点:leader follower) +peerType=observer/participant +# Be sure to read the maintenance section of the +# administrator guide before turning on autopurge. +# +# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance +# +# The number of snapshots to retain in dataDir +autopurge.snapRetainCount=3 +# Purge task interval in hours +# Set to "0" to disable auto purge feature +autopurge.purgeInterval=1 + +server.2=bigdata02:2888 +server.3=bigdata03:2888:3888 +server.4=bigdata04:2888:3888:participant +server.5=bigdata05:2888:3888:observer \ No newline at end of file diff --git a/conf/zoo_sample.cfg b/conf/zoo_sample.cfg new file mode 100644 index 0000000..58effbf --- /dev/null +++ b/conf/zoo_sample.cfg @@ -0,0 +1,36 @@ +# The number of milliseconds of each tick +tickTime=2000 +# The number of ticks that the initial +# synchronization phase can take +initLimit=10 +# The number of ticks that can pass between +# sending a request and getting an acknowledgement +syncLimit=5 +# the directory where the snapshot is stored. +# do not use /tmp for storage, /tmp here is just +# example sakes. +dataDir=/tmp/zookeeper +# the port at which the clients will connect +clientPort=2181 +# the maximum number of client connections. +# increase this if you need to handle more clients +#maxClientCnxns=60 +# +# Be sure to read the maintenance section of the +# administrator guide before turning on autopurge. +# +# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance +# +# The number of snapshots to retain in dataDir +#autopurge.snapRetainCount=3 +# Purge task interval in hours +# Set to "0" to disable auto purge feature +#autopurge.purgeInterval=1 + +## Metrics Providers +# +# https://prometheus.io Metrics Exporter +#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider +#metricsProvider.httpPort=7000 +#metricsProvider.exportJvmInfo=true + diff --git a/dev/docker/Dockerfile b/dev/docker/Dockerfile new file mode 100644 index 0000000..e5c3d45 --- /dev/null +++ b/dev/docker/Dockerfile @@ -0,0 +1,31 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +FROM maven:3.6.3-jdk-8 + +RUN apt-get update +RUN apt-get install -y \ + g++ \ + cmake \ + autoconf \ + pkg-config \ + libcppunit-dev \ + libtool \ + openssl \ + libssl-dev \ No newline at end of file diff --git a/dev/docker/run.sh b/dev/docker/run.sh new file mode 100644 index 0000000..9011310 --- /dev/null +++ b/dev/docker/run.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +set -e -x -u + +SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) + +export IMAGE_NAME="zookeeper/dev" + +pushd ${SCRIPT_DIR} + +docker build --rm=true -t ${IMAGE_NAME} . + +popd + +if [ "$(uname -s)" == "Linux" ]; then + USER_NAME=${SUDO_USER:=$USER} + USER_ID=$(id -u "${USER_NAME}") + GROUP_ID=$(id -g "${USER_NAME}") + LOCAL_HOME=$(realpath ~) +else # boot2docker uid and gid + USER_NAME=$USER + USER_ID=1000 + GROUP_ID=50 + LOCAL_HOME="/Users/${USER_NAME}" +fi + +docker build -t "${IMAGE_NAME}-${USER_NAME}" - < + + + + + + + + + + diff --git a/owaspSuppressions.xml b/owaspSuppressions.xml new file mode 100644 index 0000000..2565f0d --- /dev/null +++ b/owaspSuppressions.xml @@ -0,0 +1,54 @@ + + + + + + + + CVE-2018-8088 + + + + CVE-2018-8012 + + + + CVE-2016-5017 + + + + CVE-2018-12056 + + + + CVE-2019-3826 + + + + CVE-2019-17571 + + + + CVE-2020-9488 + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..3596bc8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,997 @@ + + + + 4.0.0 + + org.apache + apache + 23 + + + + org.apache.zookeeper + parent + pom + + 3.6.3 + Apache ZooKeeper + + ZooKeeper is a centralized service for maintaining configuration information, naming, + providing distributed synchronization, and providing group services. All of these kinds + of services are used in some form or another by distributed applications. Each time they + are implemented there is a lot of work that goes into fixing the bugs and race conditions + that are inevitable. Because of the difficulty of implementing these kinds of services, + applications initially usually skimp on them ,which make them brittle in the presence of + change and difficult to manage. Even when done correctly, different implementations of + these services lead to management complexity when the applications are deployed. + + http://zookeeper.apache.org + 2008 + + + + Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + zookeeper-docs + zookeeper-jute + zookeeper-server + zookeeper-metrics-providers + zookeeper-client + zookeeper-recipes + zookeeper-assembly + zookeeper-compatibility-tests + + + + scm:git:https://gitbox.apache.org/repos/asf/zookeeper.git + scm:git:https://gitbox.apache.org/repos/asf/zookeeper.git + https://gitbox.apache.org/repos/asf/zookeeper.git + release-3.6.3-2 + + + JIRA + http://issues.apache.org/jira/browse/ZOOKEEPER + + + jenkins + https://ci-hadoop.apache.org/view/ZooKeeper/ + + + + User List + user-subscribe@zookeeper.apache.org + user-unsubscribe@zookeeper.apache.org + user@zookeeper.apache.org + http://mail-archives.apache.org/mod_mbox/zookeeper-user/ + + + Developer List + dev-subscribe@zookeeper.apache.org + dev-unsubscribe@zookeeper.apache.org + dev@zookeeper.apache.org + http://mail-archives.apache.org/mod_mbox/zookeeper-dev/ + + + Commits List + commits-subscribe@zookeeper.apache.org + commits-unsubscribe@zookeeper.apache.org + http://mail-archives.apache.org/mod_mbox/zookeeper-commits/ + + + Issues List + issues-subscribe@zookeeper.apache.org + issues-unsubscribe@zookeeper.apache.org + https://lists.apache.org/list.html?issues@zookeeper.apache.org + + + Notifications List + notifications-subscribe@zookeeper.apache.org + notifications-unsubscribe@zookeeper.apache.org + https://lists.apache.org/list.html?notifications@zookeeper.apache.org + + + + + tdunning + Ted Dunning + tdunning@apache.org + -8 + + + camille + Camille Fournier + camille@apache.org + -5 + + + phunt + Patrick Hunt + phunt@apache.org + -8 + + + fpj + Flavio Junqueira + fpj@apache.org + +0 + + + ivank + Ivan Kelly + ivank@apache.org + +2 + + + mahadev + Mahadev Konar + mahadev@apache.org + -8 + + + michim + Michi Mutsuzaki + michim@apache.org + -8 + + + cnauroth + Chris Nauroth + cnauroth@apache.org + -8 + + + breed + Benjamin Reed + breed@apache.org + -8 + + + henry + Henry Robinson + henry@apache.org + -8 + + + rgs + Raul Gutierrez Segales + rgs@apache.org + -8 + + + rakeshr + Rakesh Radhakrishnan + rakeshr@apache.org + +5:30 + + + hanm + Michael Han + hanm@apache.org + -8 + + + gkesavan + Giridharan Kesavan + gkesavan@apache.org + -8 + + + akornev + Andrew Kornev + akornev@apache.org + + + shralex + Alex Shraer + shralex@apache.org + -8 + + + thawan + Thawan Kooburat + thawan@apache.org + -8 + + + hdeng + Hongchao Deng + hdeng@apache.org + -8 + + + arshad + Mohammad Arshad + arshad@apache.org + +5:30 + + + afine + Abraham Fine + afine@apache.org + -8 + + + andor + Andor Molnar + andor@apache.org + +1 + + + lvfangmin + Allan Lyu + fangmin@apache.org + -8 + + + eolivelli + Enrico Olivelli + eolivelli@apache.org + +1 + + + + + + full-build + + zookeeper-it + zookeeper-contrib + + + + fatjar + + zookeeper-it + zookeeper-contrib + + + + java-build + + true + + + + apache-release + + + + org.apache.maven.plugins + maven-assembly-plugin + + + org.apache.apache.resources + apache-source-release-assembly-descriptor + 1.0.6 + + + + + source-release-assembly-tar-gz + initialize + + single + + + true + + + ${sourceReleaseAssemblyDescriptor} + + apache-zookeeper-${project.version} + false + + tar.gz + + posix + + + + + + + + + m2e + + + m2e.version + + + + 8 + + + + jdk-release-flag + + [9,) + + + 8 + + + + + + + 1.8 + 1.8 + false + + 8 + + + 1.7.25 + 0.5.0 + 1.48 + 4.12 + 1.2.17 + 2.27.0 + 1.3 + 1.2 + 4.1.63.Final + 9.4.39.v20210325 + 2.10.5.1 + 1.1.1 + 2.14.6 + 1.1.7 + 2.0.0 + 1.60 + 3.2.2 + 3.2.5 + 4.0.2 + 8.17 + 3.0.0-M3 + + + yes + + + + + + + org.hamcrest + hamcrest-all + ${hamcrest.version} + + + commons-collections + commons-collections + ${commons-collections.version} + + + org.apache.yetus + audience-annotations + ${audience-annotations.version} + + + commons-cli + commons-cli + ${commons-cli.version} + + + org.apache.kerby + kerb-core + ${kerby.version} + + + org.slf4j + slf4j-api + + + + + org.apache.kerby + kerb-simplekdc + ${kerby.version} + + + org.slf4j + slf4j-api + + + + + org.apache.kerby + kerby-config + ${kerby.version} + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + + + org.bouncycastle + bcpkix-jdk15on + ${bouncycastle.version} + + + org.slf4j + slf4j-api + ${slf4j.version} + + + org.slf4j + slf4j-log4j12 + ${slf4j.version} + + + * + * + + + + + log4j + log4j + ${log4j.version} + + + org.jmockit + jmockit + ${jmockit.version} + + + junit + junit + ${junit.version} + + + org.mockito + mockito-core + ${mockito.version} + + + io.netty + netty-handler + ${netty.version} + + + io.netty + netty-transport-native-epoll + ${netty.version} + + + org.eclipse.jetty + jetty-server + ${jetty.version} + + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + + + io.dropwizard.metrics + metrics-core + ${dropwizard.version} + + + org.slf4j + slf4j-api + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.googlecode.json-simple + json-simple + ${json.version} + + + junit + junit + + + + + jline + jline + ${jline.version} + + + com.github.spotbugs + spotbugs-annotations + ${spotbugsannotations.version} + provided + true + + + org.xerial.snappy + snappy-java + ${snappy.version} + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + true + + -Werror + -Xlint:deprecation + -Xlint:unchecked + -Xlint:-options + -Xdoclint:-missing + + -Xpkginfo:always + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + ${mvngit.commit.id} + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.0.1 + + none + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-release-plugin + 2.5.3 + + + org.apache.maven.plugins + maven-scm-plugin + 1.11.2 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + org.apache.maven.plugins + maven-antrun-plugin + 1.8 + + + org.apache.maven.plugins + maven-dependency-plugin + 3.1.1 + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + com.github.koraktor + mavanagaiata + 0.9.4 + + true + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + net.nicoulaj.maven.plugins + checksum-maven-plugin + 1.8 + + + org.openclover + clover-maven-plugin + 4.3.1 + + + com.github.spotbugs + spotbugs-maven-plugin + 4.0.0 + + excludeFindBugsFilter.xml + + + + org.owasp + dependency-check-maven + 5.3.0 + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.0 + + + com.puppycrawl.tools + checkstyle + ${checkstyle.version} + + + + checkstyle-strict.xml + checkstyleSuppressions.xml + UTF-8 + true + true + false + false + true + + + + checkstyle + validate + + check + + + + + + + maven-remote-resources-plugin + + + process-resource-bundles + none + + + + + + + + + com.github.koraktor + mavanagaiata + + + find-current-git-revision + + commit + + validate + + + + + org.openclover + clover-maven-plugin + + true + true + + org/apache/zookeeper/**/* + + + org/apache/zookeeper/version/**/* + + + + + pre-site + + instrument + aggregate + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + validate + + run + + + true + + + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + Jar Tests Package + package + + test-jar + + + + org/** + META_INF/** + + true + + + + + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + attach-javadocs + + jar + + + + aggregate + site + + aggregate + + + + + zookeeper-server/src/main/resources/overview.html + *.recipes.* + + + + + + net.nicoulaj.maven.plugins + checksum-maven-plugin + + + + artifacts + + + + + + SHA-512 + + true + + + + + com.github.spotbugs + spotbugs-maven-plugin + + + org.owasp + dependency-check-maven + + ALL + 0 + + owaspSuppressions.xml + + + + + org.apache.rat + apache-rat-plugin + + + **/log4j.properties + **/README.md + **/findbugsExcludeFile.xml + **/checkstyle-noframes-sorted.xsl + **/configure.ac + **/Makefile.am + conf/zoo_sample.cfg + conf/configuration.xsl + .travis.yml + excludeFindBugsFilter.xml + README_packaging.md + src/main/resources/markdown/skin/* + src/main/resources/markdown/html/* + src/main/resources/markdown/images/* + + **/JMX-RESOURCES + **/src/main/resources/mainClasses + **/Changes + **/MANIFEST + **/src/test/zoo.cfg + **/src/main/resources/webapp/org/apache/zookeeper/graph/resources/* + **/src/main/java/com/nitido/utils/toaster/Toaster.java + **/TODO + + **/acinclude.m4 + **/aminclude.am + **/src/hashtable/* + **/include/winconfig.h + **/tests/wrappers.opt + **/tests/zoo.cfg + **/tests/wrappers-mt.opt + **/c-doc.Doxyfile + + true + + + + org.apache.maven.plugins + maven-release-plugin + false + + + clean install -DskipTests antrun:run@replace-cclient-files-during-release scm:add@add-cclient-files-during-release scm:checkin@commit-cclient-files-during-release + clean install -DskipTests antrun:run@replace-cclient-files-during-release scm:add@add-cclient-files-during-release scm:checkin@commit-cclient-files-during-release + + + + org.apache.maven.plugins + maven-scm-plugin + false + + + add-cclient-files-during-release + none + + add + + + false + zookeeper-client/zookeeper-client-c/CMakeLists.txt,zookeeper-client/zookeeper-client-c/configure.ac,zookeeper-client/zookeeper-client-c/include/zookeeper_version.h + + + + commit-cclient-files-during-release + none + + + checkin + + + false + Prepared ${project.version} + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + ${enforcer.version} + + + banned-commons-lang + + enforce + + + + + + commons-lang:commons-lang + + false + We don't use commons-lang any more, so do not depend on it directly. + + + + + + banned-commons-lang3 + + enforce + + + + + + org.apache.commons:commons-lang3 + + false + We don't use commons-lang3, so do not depend on it directly. + + + + + + + + + + ${project.basedir}src/main/java/resources + + **/*.* + + + + + + + + + org.openclover + clover-maven-plugin + + + + + diff --git a/zk-merge-pr.py b/zk-merge-pr.py new file mode 100644 index 0000000..dc5549d --- /dev/null +++ b/zk-merge-pr.py @@ -0,0 +1,533 @@ +#!/usr/bin/env python + +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# + +# Utility for creating well-formed pull request merges and pushing them to Apache. This script is a modified version +# of the one created by the Spark project (https://github.com/apache/spark/blob/master/dev/merge_spark_pr.py). +# +# Usage: ./zk-merge-pr.py (see config env vars below) +# +# This utility assumes you already have a local ZooKeeper git folder and that you +# have added remotes corresponding to both: +# (i) the github apache ZooKeeper mirror and +# (ii) the apache ZooKeeper git repo. + +import json +import os +import re +import subprocess +import sys +import urllib.request, urllib.error, urllib.parse +import getpass + +try: + import jira.client + JIRA_IMPORTED = True +except ImportError: + JIRA_IMPORTED = False + +PROJECT_NAME = "zookeeper" + +CAPITALIZED_PROJECT_NAME = PROJECT_NAME.upper() + +# Remote name which points to the GitHub site +PR_REMOTE_NAME = os.environ.get("PR_REMOTE_NAME", "apache-github") +# Remote name which points to Apache git +PUSH_REMOTE_NAME = os.environ.get("PUSH_REMOTE_NAME", "apache") +# ASF JIRA username +JIRA_USERNAME = os.environ.get("JIRA_USERNAME", "") +# ASF JIRA password +JIRA_PASSWORD = os.environ.get("JIRA_PASSWORD", "") +# OAuth key used for issuing requests against the GitHub API. If this is not defined, then requests +# will be unauthenticated. You should only need to configure this if you find yourself regularly +# exceeding your IP's unauthenticated request rate limit. You can create an OAuth key at +# https://github.com/settings/tokens. This script only requires the "public_repo" scope. +GITHUB_OAUTH_KEY = os.environ.get("GITHUB_OAUTH_KEY") + +GITHUB_USER = os.environ.get("GITHUB_USER", "apache") +GITHUB_BASE = "https://github.com/%s/%s/pull" % (GITHUB_USER, PROJECT_NAME) +GITHUB_API_BASE = "https://api.github.com/repos/%s/%s" % (GITHUB_USER, PROJECT_NAME) +JIRA_BASE = "https://issues.apache.org/jira/browse" +JIRA_API_BASE = "https://issues.apache.org/jira" +# Prefix added to temporary branches +TEMP_BRANCH_PREFIX = "PR_TOOL" +# TODO Introduce a convention as this is too brittle +RELEASE_BRANCH_PREFIX = "branch-" + +DEV_BRANCH_NAME = "master" + +DEFAULT_FIX_VERSION = os.environ.get("DEFAULT_FIX_VERSION", "branch-3.5") + +def get_json(url): + try: + request = urllib.request.Request(url) + if GITHUB_OAUTH_KEY: + request.add_header('Authorization', 'token %s' % GITHUB_OAUTH_KEY) + return json.load(urllib.request.urlopen(request)) + except urllib.error.HTTPError as e: + if "X-RateLimit-Remaining" in e.headers and e.headers["X-RateLimit-Remaining"] == '0': + print("Exceeded the GitHub API rate limit; see the instructions in " + \ + "zk-merge-pr.py to configure an OAuth token for making authenticated " + \ + "GitHub requests.") + else: + print("Unable to fetch URL, exiting: %s" % url) + sys.exit(-1) + + +def fail(msg): + print(msg) + clean_up() + sys.exit(-1) + + +def run_cmd(cmd): + print(cmd) + if isinstance(cmd, list): + return subprocess.check_output(cmd, encoding='utf8') + else: + return subprocess.check_output(cmd.split(" "), encoding='utf8') + + +def continue_maybe(prompt): + result = input("\n%s (y/n): " % prompt) + if result.lower().strip() != "y": + fail("Okay, exiting") + +def clean_up(): + if original_head != get_current_branch(): + print("Restoring head pointer to %s" % original_head) + run_cmd("git checkout %s" % original_head) + + branches = run_cmd("git branch").replace(" ", "").split("\n") + + for branch in [x for x in branches if x.startswith(TEMP_BRANCH_PREFIX)]: + print("Deleting local branch %s" % branch) + run_cmd("git branch -D %s" % branch) + +def get_current_branch(): + return run_cmd("git rev-parse --abbrev-ref HEAD").replace("\n", "") + +# merge the requested PR and return the merge hash +def merge_pr(pr_num, target_ref, title, body, pr_repo_desc): + pr_branch_name = "%s_MERGE_PR_%s" % (TEMP_BRANCH_PREFIX, pr_num) + target_branch_name = "%s_MERGE_PR_%s_%s" % (TEMP_BRANCH_PREFIX, pr_num, target_ref.upper()) + run_cmd("git fetch %s pull/%s/head:%s" % (PR_REMOTE_NAME, pr_num, pr_branch_name)) + run_cmd("git fetch %s %s:%s" % (PUSH_REMOTE_NAME, target_ref, target_branch_name)) + run_cmd("git checkout %s" % target_branch_name) + + had_conflicts = False + try: + run_cmd(['git', 'merge', pr_branch_name, '--squash']) + except Exception as e: + msg = "Error merging: %s\nWould you like to manually fix-up this merge?" % e + continue_maybe(msg) + msg = "Okay, please fix any conflicts and 'git add' conflicting files... Finished?" + continue_maybe(msg) + had_conflicts = True + + commit_authors = run_cmd(['git', 'log', 'HEAD..%s' % pr_branch_name, + '--pretty=format:%an <%ae>']).split("\n") + distinct_authors = sorted(set(commit_authors), + key=lambda x: commit_authors.count(x), reverse=True) + primary_author = input( + "Enter primary author in the format of \"name \" [%s]: " % + distinct_authors[0]) + if primary_author == "": + primary_author = distinct_authors[0] + + reviewers = input( + "Enter reviewers in the format of \"name1 , name2 \": ").strip() + + commits = run_cmd(['git', 'log', 'HEAD..%s' % pr_branch_name, + '--pretty=format:%h [%an] %s']).split("\n") + + if len(commits) > 1: + result = input("List pull request commits in squashed commit message? (y/n): ") + if result.lower().strip() == "y": + should_list_commits = True + else: + should_list_commits = False + else: + should_list_commits = False + + merge_message_flags = [] + + merge_message_flags += ["-m", title] + if body is not None: + # We remove @ symbols from the body to avoid triggering e-mails + # to people every time someone creates a public fork of the project. + merge_message_flags += ["-m", body.replace("@", "")] + + authors = "\n".join(["Author: %s" % a for a in distinct_authors]) + + merge_message_flags += ["-m", authors] + + if (reviewers != ""): + merge_message_flags += ["-m", "Reviewers: %s" % reviewers] + + if had_conflicts: + committer_name = run_cmd("git config --get user.name").strip() + committer_email = run_cmd("git config --get user.email").strip() + message = "This patch had conflicts when merged, resolved by\nCommitter: %s <%s>" % ( + committer_name, committer_email) + merge_message_flags += ["-m", message] + + # The string "Closes #%s" string is required for GitHub to correctly close the PR + close_line = "Closes #%s from %s" % (pr_num, pr_repo_desc) + if should_list_commits: + close_line += " and squashes the following commits:" + merge_message_flags += ["-m", close_line] + + if should_list_commits: + merge_message_flags += ["-m", "\n".join(commits)] + + run_cmd(['git', 'commit', '--author="%s"' % primary_author] + merge_message_flags) + + continue_maybe("Merge complete (local ref %s). Push to %s?" % ( + target_branch_name, PUSH_REMOTE_NAME)) + + try: + run_cmd('git push %s %s:%s' % (PUSH_REMOTE_NAME, target_branch_name, target_ref)) + except Exception as e: + clean_up() + fail("Exception while pushing: %s" % e) + + merge_hash = run_cmd("git rev-parse %s" % target_branch_name)[:8] + clean_up() + print(("Pull request #%s merged!" % pr_num)) + print(("Merge hash: %s" % merge_hash)) + return merge_hash + + +def cherry_pick(pr_num, merge_hash, default_branch): + pick_ref = input("Enter a branch name [%s]: " % default_branch) + if pick_ref == "": + pick_ref = default_branch + + pick_branch_name = "%s_PICK_PR_%s_%s" % (TEMP_BRANCH_PREFIX, pr_num, pick_ref.upper()) + + run_cmd("git fetch %s %s:%s" % (PUSH_REMOTE_NAME, pick_ref, pick_branch_name)) + run_cmd("git checkout %s" % pick_branch_name) + + try: + run_cmd("git cherry-pick -sx %s" % merge_hash) + except Exception as e: + msg = "Error cherry-picking: %s\nWould you like to manually fix-up this merge?" % e + continue_maybe(msg) + msg = "Okay, please fix any conflicts and finish the cherry-pick. Finished?" + continue_maybe(msg) + + continue_maybe("Pick complete (local ref %s). Push to %s?" % ( + pick_branch_name, PUSH_REMOTE_NAME)) + + try: + run_cmd('git push %s %s:%s' % (PUSH_REMOTE_NAME, pick_branch_name, pick_ref)) + except Exception as e: + clean_up() + fail("Exception while pushing: %s" % e) + + pick_hash = run_cmd("git rev-parse %s" % pick_branch_name)[:8] + clean_up() + + print(("Pull request #%s picked into %s!" % (pr_num, pick_ref))) + print(("Pick hash: %s" % pick_hash)) + return pick_ref + + +def fix_version_from_branch(branch, versions): + # Note: Assumes this is a sorted (newest->oldest) list of un-released versions + if branch == DEV_BRANCH_NAME: + versions = [x for x in versions if x == DEFAULT_FIX_VERSION] + if len(versions) > 0: + return versions[0] + else: + return None + else: + versions = [x for x in versions if x.startswith(branch)] + if len(versions) > 0: + return versions[-1] + else: + return None + + +def resolve_jira_issue(merge_branches, comment, default_jira_id=""): + asf_jira = jira.client.JIRA({'server': JIRA_API_BASE}, + basic_auth=(JIRA_USERNAME, JIRA_PASSWORD)) + + jira_id = input("Enter a JIRA id [%s]: " % default_jira_id) + if jira_id == "": + jira_id = default_jira_id + + try: + issue = asf_jira.issue(jira_id) + except Exception as e: + fail("ASF JIRA could not find %s\n%s" % (jira_id, e)) + + cur_status = issue.fields.status.name + cur_summary = issue.fields.summary + cur_assignee = issue.fields.assignee + if cur_assignee is None: + cur_assignee = "NOT ASSIGNED!!!" + else: + cur_assignee = cur_assignee.displayName + + if cur_status == "Resolved" or cur_status == "Closed": + fail("JIRA issue %s already has status '%s'" % (jira_id, cur_status)) + print(("=== JIRA %s ===" % jira_id)) + print(("summary\t\t%s\nassignee\t%s\nstatus\t\t%s\nurl\t\t%s/%s\n" % ( + cur_summary, cur_assignee, cur_status, JIRA_BASE, jira_id))) + + versions = asf_jira.project_versions(CAPITALIZED_PROJECT_NAME) + versions = sorted(versions, key=lambda x: x.name, reverse=True) + versions = [x for x in versions if x.raw['released'] is False] + + version_names = [x.name for x in versions] + default_fix_versions = [fix_version_from_branch(x, version_names) for x in merge_branches] + default_fix_versions = [x for x in default_fix_versions if x != None] + default_fix_versions = ",".join(default_fix_versions) + + fix_versions = input("Enter comma-separated fix version(s) [%s]: " % default_fix_versions) + if fix_versions == "": + fix_versions = default_fix_versions + fix_versions = fix_versions.replace(" ", "").split(",") + + def get_version_json(version_str): + return filter(lambda v: v.name == version_str, versions)[0].raw + + jira_fix_versions = [get_version_json(v) for v in fix_versions] + + resolve = filter(lambda a: a['name'] == "Resolve Issue", asf_jira.transitions(jira_id))[0] + resolution = filter(lambda r: r.raw['name'] == "Fixed", asf_jira.resolutions())[0] + asf_jira.transition_issue( + jira_id, resolve["id"], fixVersions = jira_fix_versions, + comment = comment, resolution = {'id': resolution.raw['id']}) + + print("Successfully resolved %s with fixVersions=%s!" % (jira_id, fix_versions)) + + +def resolve_jira_issues(title, merge_branches, comment): + jira_ids = re.findall("%s-[0-9]{4,5}" % CAPITALIZED_PROJECT_NAME, title) + + if len(jira_ids) == 0: + resolve_jira_issue(merge_branches, comment) + for jira_id in jira_ids: + resolve_jira_issue(merge_branches, comment, jira_id) + + +def standardize_jira_ref(text): + """ + Standardize the jira reference commit message prefix to "PROJECT_NAME-XXX: Issue" + + >>> standardize_jira_ref("%s-5954: Top by key" % CAPITALIZED_PROJECT_NAME) + 'ZOOKEEPER-5954: Top by key' + >>> standardize_jira_ref("%s-5821: ParquetRelation2 CTAS should check if delete is successful" % PROJECT_NAME) + 'ZOOKEEPER-5821: ParquetRelation2 CTAS should check if delete is successful' + >>> standardize_jira_ref("%s-4123: [WIP] Show new dependencies added in pull requests" % PROJECT_NAME) + 'ZOOKEEPER-4123: [WIP] Show new dependencies added in pull requests' + >>> standardize_jira_ref("%s 5954: Top by key" % PROJECT_NAME) + 'ZOOKEEPER-5954: Top by key' + >>> standardize_jira_ref("%s-979: a LRU scheduler for load balancing in TaskSchedulerImpl" % PROJECT_NAME) + 'ZOOKEEPER-979: a LRU scheduler for load balancing in TaskSchedulerImpl' + >>> standardize_jira_ref("%s-1094: Support MiMa for reporting binary compatibility across versions." % CAPITALIZED_PROJECT_NAME) + 'ZOOKEEPER-1094: Support MiMa for reporting binary compatibility across versions.' + >>> standardize_jira_ref("%s-1146: [WIP] Vagrant support" % CAPITALIZED_PROJECT_NAME) + 'ZOOKEEPER-1146: [WIP] Vagrant support' + >>> standardize_jira_ref("%s-1032: If Yarn app fails before registering, app master stays aroun..." % PROJECT_NAME) + 'ZOOKEEPER-1032: If Yarn app fails before registering, app master stays aroun...' + >>> standardize_jira_ref("%s-6250 %s-6146 %s-5911: Types are now reserved words in DDL parser." % (PROJECT_NAME, PROJECT_NAME, CAPITALIZED_PROJECT_NAME)) + 'ZOOKEEPER-6250 ZOOKEEPER-6146 ZOOKEEPER-5911: Types are now reserved words in DDL parser.' + >>> standardize_jira_ref("Additional information for users building from source code") + 'Additional information for users building from source code' + """ + jira_refs = [] + components = [] + + # Extract JIRA ref(s): + pattern = re.compile(r'(%s[-\s]*[0-9]{3,6})+' % CAPITALIZED_PROJECT_NAME, re.IGNORECASE) + for ref in pattern.findall(text): + # Add brackets, replace spaces with a dash, & convert to uppercase + jira_refs.append(re.sub(r'\s+', '-', ref.upper())) + text = text.replace(ref, '') + + # Extract project name component(s): + # Look for alphanumeric chars, spaces, dashes, periods, and/or commas + pattern = re.compile(r'(\[[\w\s,-\.]+\])', re.IGNORECASE) + for component in pattern.findall(text): + components.append(component.upper()) + text = text.replace(component, '') + + # Cleanup any remaining symbols: + pattern = re.compile(r'^\W+(.*)', re.IGNORECASE) + if (pattern.search(text) is not None): + text = pattern.search(text).groups()[0] + + # Assemble full text (JIRA ref(s), module(s), remaining text) + jira_prefix = ' '.join(jira_refs).strip() + if jira_prefix: + jira_prefix = jira_prefix + ": " + clean_text = jira_prefix + ' '.join(components).strip() + " " + text.strip() + + # Replace multiple spaces with a single space, e.g. if no jira refs and/or components were included + clean_text = re.sub(r'\s+', ' ', clean_text.strip()) + + return clean_text + +def get_remote_repos(): + repos = run_cmd("git remote -v").split() + + dict = {} + for i in range(0, len(repos), 3): + dict[repos[i]] = repos[i+1] + return dict + + +def check_git_remote(): + repos = get_remote_repos() + + # check if all remote endpoints' URLs point to project git repo + name = PROJECT_NAME + ".git" + for url in list(repos.values()): + if not url.endswith(name): + fail("Error: not a %s git repo or at least one remote is invalid" % PROJECT_NAME) + + if not PR_REMOTE_NAME in repos: + fail("Error: PR_REMOTE_NAME (%s) environment variable has not been set!" % PR_REMOTE_NAME) + + if not PUSH_REMOTE_NAME in repos: + fail("Error: PUSH_REMOTE_NAME (%s) environment variable has not been set!" % PUSH_REMOTE_NAME) + + +def check_jira_env(): + + global JIRA_PASSWORD + + if JIRA_IMPORTED: + + if JIRA_USERNAME.strip() != "" and JIRA_PASSWORD.strip() == "": + inform_pwd = input("JIRA_USERNAME set but JIRA_PASSWORD is not. Want to inform it? ") + if inform_pwd.strip() == "y": + JIRA_PASSWORD = getpass.getpass('JIRA PASSWORD: ') + + if JIRA_USERNAME.strip() == "" or JIRA_PASSWORD.strip() == "": + msg ="JIRA_USERNAME and/or JIRA_PASSWORD are not set. Want to continue? " + continue_maybe(msg) + else: + msg = "JIRA lib not installed. Want to continue? " + continue_maybe(msg) + +def main(): + global original_head + + original_head = get_current_branch() + + check_jira_env() + + check_git_remote() + + branches = get_json("%s/branches" % GITHUB_API_BASE) + branch_names = [x for x in [x['name'] for x in branches] if x.startswith(RELEASE_BRANCH_PREFIX)] + # Assumes branch names can be sorted lexicographically + latest_branch = sorted(branch_names, reverse=True)[0] + + pr_num = input("Which pull request would you like to merge? (e.g. 34): ") + pr = get_json("%s/pulls/%s" % (GITHUB_API_BASE, pr_num)) + pr_events = get_json("%s/issues/%s/events" % (GITHUB_API_BASE, pr_num)) + + url = pr["url"] + + pr_title = pr["title"] + commit_title = input("Commit title [%s]: " % pr_title) + if commit_title == "": + commit_title = pr_title + + # Decide whether to use the modified title or not + modified_title = standardize_jira_ref(commit_title) + if modified_title != commit_title: + print("I've re-written the title as follows to match the standard format:") + print("Original: %s" % commit_title) + print("Modified: %s" % modified_title) + result = input("Would you like to use the modified title? (y/n): ") + if result.lower().strip() == "y": + commit_title = modified_title + print("Using modified title:") + else: + print("Using original title:") + print(commit_title) + + body = pr["body"] + target_ref = pr["base"]["ref"] + user_login = pr["user"]["login"] + base_ref = pr["head"]["ref"] + pr_repo_desc = "%s/%s" % (user_login, base_ref) + + # Merged pull requests don't appear as merged in the GitHub API; + # Instead, they're closed by asfgit. + merge_commits = \ + [e for e in pr_events if e["actor"]["login"] == "asfgit" and e["event"] == "closed"] + + if merge_commits: + merge_hash = merge_commits[0]["commit_id"] + message = get_json("%s/commits/%s" % (GITHUB_API_BASE, merge_hash))["commit"]["message"] + + print("Pull request %s has already been merged, assuming you want to backport" % pr_num) + commit_is_downloaded = run_cmd(['git', 'rev-parse', '--quiet', '--verify', + "%s^{commit}" % merge_hash]).strip() != "" + if not commit_is_downloaded: + fail("Couldn't find any merge commit for #%s, you may need to update HEAD." % pr_num) + + print("Found commit %s:\n%s" % (merge_hash, message)) + cherry_pick(pr_num, merge_hash, latest_branch) + sys.exit(0) + + if not bool(pr["mergeable"]): + msg = "Pull request %s is not mergeable in its current form.\n" % pr_num + \ + "Continue? (experts only!)" + continue_maybe(msg) + + print(("\n=== Pull Request #%s ===" % pr_num)) + print(("PR title\t%s\nCommit title\t%s\nSource\t\t%s\nTarget\t\t%s\nURL\t\t%s" % ( + pr_title, commit_title, pr_repo_desc, target_ref, url))) + continue_maybe("Proceed with merging pull request #%s?" % pr_num) + + merged_refs = [target_ref] + + merge_hash = merge_pr(pr_num, target_ref, commit_title, body, pr_repo_desc) + + pick_prompt = "Would you like to pick %s into another branch?" % merge_hash + while input("\n%s (y/n): " % pick_prompt).lower().strip() == "y": + merged_refs = merged_refs + [cherry_pick(pr_num, merge_hash, latest_branch)] + + if JIRA_IMPORTED: + if JIRA_USERNAME and JIRA_PASSWORD: + continue_maybe("Would you like to update an associated JIRA?") + jira_comment = "Issue resolved by pull request %s\n[%s/%s]" % (pr_num, GITHUB_BASE, pr_num) + resolve_jira_issues(commit_title, merged_refs, jira_comment) + else: + print("JIRA_USERNAME and JIRA_PASSWORD not set") + print("Exiting without trying to close the associated JIRA.") + else: + print("Could not find jira-python library. Run 'sudo pip install jira' to install.") + print("Exiting without trying to close the associated JIRA.") + +if __name__ == "__main__": + import doctest + (failure_count, test_count) = doctest.testmod() + if (failure_count): + exit(-1) + + main() diff --git a/zookeeper-assembly/pom.xml b/zookeeper-assembly/pom.xml new file mode 100644 index 0000000..d8443f6 --- /dev/null +++ b/zookeeper-assembly/pom.xml @@ -0,0 +1,178 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + .. + + + zookeeper-assembly + pom + Apache ZooKeeper - Assembly + ZooKeeper Assembly + + + + full-build + + false + + + + + + + 0644 + 0755 + true + + + + + org.apache.zookeeper + zookeeper-docs + ${project.version} + + + org.apache.zookeeper + zookeeper-jute + ${project.version} + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.apache.zookeeper + zookeeper-client + ${project.version} + pom + + + org.apache.zookeeper + zookeeper-prometheus-metrics + ${project.version} + + + org.apache.zookeeper + zookeeper-recipes + ${project.version} + pom + + + + commons-cli + commons-cli + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-servlet + + + com.fasterxml.jackson.core + jackson-databind + + + com.googlecode.json-simple + json-simple + + + jline + jline + + + io.dropwizard.metrics + metrics-core + + + org.xerial.snappy + snappy-java + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + bin-assembly + package + + single + + + + ${project.basedir}/src/main/assembly/bin-package.xml + + apache-zookeeper-${project.version}-bin + false + posix + + + + lib-assembly + package + + single + + + + ${project.basedir}/src/main/assembly/lib-package.xml + + apache-zookeeper-${project.version}-lib + false + posix + ${skip.lib.artifact} + + + + + + + com.github.spotbugs + spotbugs-maven-plugin + + true + + + + + maven-deploy-plugin + + + true + + + + + + diff --git a/zookeeper-assembly/src/main/assembly/bin-package.xml b/zookeeper-assembly/src/main/assembly/bin-package.xml new file mode 100644 index 0000000..1fa81d0 --- /dev/null +++ b/zookeeper-assembly/src/main/assembly/bin-package.xml @@ -0,0 +1,84 @@ + + + bin-package + + tar.gz + + true + + src/main/assembly/components.xml + + + + + + *:* + + + org.apache.zookeeper:zookeeper-recipes + org.apache.zookeeper:zookeeper-client + org.apache.zookeeper:zookeeper-docs + + false + true + lib + ${rw.file.permission} + ${rwx.file.permission} + true + + + + + + + ${project.basedir}/../zookeeper-docs/target/html + docs + ${rw.file.permission} + ${rwx.file.permission} + + + + ${project.basedir}/../zookeeper-jute/target/apidocs + docs/apidocs/zookeeper-jute + ${rw.file.permission} + ${rwx.file.permission} + + + + ${project.basedir}/../zookeeper-server/target/apidocs + docs/apidocs/zookeeper-server + ${rw.file.permission} + ${rwx.file.permission} + + + + ${project.basedir}/../zookeeper-server/src/main/resources/lib + + *.txt + + lib + ${rw.file.permission} + + + + diff --git a/zookeeper-assembly/src/main/assembly/components.xml b/zookeeper-assembly/src/main/assembly/components.xml new file mode 100644 index 0000000..e0df12b --- /dev/null +++ b/zookeeper-assembly/src/main/assembly/components.xml @@ -0,0 +1,49 @@ + + + + + + + + ${project.basedir}/.. + . + + NOTICE.txt + LICENSE.txt + README.md + README_packaging.md + + ${rw.file.permission} + + + ${project.basedir}/../conf + conf + ${rw.file.permission} + ${rwx.file.permission} + + + ${project.basedir}/../bin + bin + ${rwx.file.permission} + ${rwx.file.permission} + + + diff --git a/zookeeper-assembly/src/main/assembly/lib-package.xml b/zookeeper-assembly/src/main/assembly/lib-package.xml new file mode 100644 index 0000000..81194f9 --- /dev/null +++ b/zookeeper-assembly/src/main/assembly/lib-package.xml @@ -0,0 +1,64 @@ + + + lib-package + + tar.gz + + true + + + + + ${project.basedir}/../zookeeper-client/zookeeper-client-c/target/c + usr + + include/**/* + lib/* + + ${rw.file.permission} + ${rwx.file.permission} + + + + ${project.basedir}/../zookeeper-client/zookeeper-client-c/target/c + usr + + bin/* + + ${rwx.file.permission} + ${rwx.file.permission} + + + + ${project.basedir}/../zookeeper-client/zookeeper-client-c + + LICENSE + + / + ${rw.file.permission} + ${rwx.file.permission} + + + + + diff --git a/zookeeper-client/pom.xml b/zookeeper-client/pom.xml new file mode 100644 index 0000000..ba0338c --- /dev/null +++ b/zookeeper-client/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + .. + + + zookeeper-client + pom + Apache ZooKeeper - Client + ZooKeeper client + + + + full-build + + zookeeper-client-c + + + + java-build + + + + + + + maven-deploy-plugin + + + true + + + + + \ No newline at end of file diff --git a/zookeeper-client/zookeeper-client-c/CMakeLists.txt b/zookeeper-client/zookeeper-client-c/CMakeLists.txt new file mode 100644 index 0000000..7b49308 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/CMakeLists.txt @@ -0,0 +1,267 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +cmake_minimum_required(VERSION 3.5) + +project(zookeeper VERSION 3.6.3) +set(email user@zookeeper.apache.org) +set(description "zookeeper C client") + +# general options +if(UNIX) + add_compile_options(-Wall -fPIC) +elseif(WIN32) + add_compile_options(/W3) +endif() +add_definitions(-DUSE_STATIC_LIB) + +# TODO: Enable /WX and /W4 on Windows. Currently there are ~1000 warnings. +# TODO: Add Solaris support. +# TODO: Add a shared library option. +# TODO: Specify symbols to export. +# TODO: Generate doxygen documentation. + +# Sync API option +option(WANT_SYNCAPI "Enables Sync API support" ON) +if(WANT_SYNCAPI) + add_definitions(-DTHREADED) +endif() + +# CppUnit option +if(WIN32 OR APPLE) + # The tests do not yet compile on Windows or macOS, + # so we set this to off by default. + # + # Note that CMake does not have expressions except in conditionals, + # so we're left with this if/else/endif pattern. + set(DEFAULT_WANT_CPPUNIT OFF) +else() + set(DEFAULT_WANT_CPPUNIT ON) +endif() +option(WANT_CPPUNIT "Enables CppUnit and tests" ${DEFAULT_WANT_CPPUNIT}) + +# SOCK_CLOEXEC +option(WANT_SOCK_CLOEXEC "Enables SOCK_CLOEXEC on sockets" OFF) +include(CheckSymbolExists) +check_symbol_exists(SOCK_CLOEXEC sys/socket.h HAVE_SOCK_CLOEXEC) +if(WANT_SOCK_CLOEXEC AND HAVE_SOCK_CLOEXEC) + set(SOCK_CLOEXEC_ENABLED 1) +endif() + + +# The function `to_have(in out)` converts a header name like `arpa/inet.h` +# into an Autotools style preprocessor definition `HAVE_ARPA_INET_H`. +# This is then set or unset in `configure_file()` step. +# +# Note that CMake functions do not have return values; instead an "out" +# variable must be passed, and explicitly set with parent scope. +function(to_have in out) + string(TOUPPER ${in} str) + string(REGEX REPLACE "/|\\." "_" str ${str}) + set(${out} "HAVE_${str}" PARENT_SCOPE) +endfunction() + +# include file checks +foreach(f generated/zookeeper.jute.h generated/zookeeper.jute.c) + if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${f}") + to_have(${f} name) + set(${name} 1) + else() + message(FATAL_ERROR + "jute files are missing!\n" + "Please run 'ant compile_jute' while in the ZooKeeper top level directory.") + endif() +endforeach() + +# header checks +include(CheckIncludeFile) +set(check_headers + arpa/inet.h + dlfcn.h + fcntl.h + inttypes.h + memory.h + netdb.h + netinet/in.h + stdint.h + stdlib.h + string.h + strings.h + sys/socket.h + sys/stat.h + sys/time.h + sys/types.h + unistd.h + sys/utsname.h) + +foreach(f ${check_headers}) + to_have(${f} name) + check_include_file(${f} ${name}) +endforeach() + +# function checks +include(CheckFunctionExists) +set(check_functions + getcwd + gethostbyname + gethostname + getlogin + getpwuid_r + gettimeofday + getuid + memmove + memset + poll + socket + strchr + strdup + strerror + strtol) + +foreach(fn ${check_functions}) + to_have(${fn} name) + check_function_exists(${fn} ${name}) +endforeach() + +# library checks +set(check_libraries rt m pthread) +foreach(lib ${check_libraries}) + to_have("lib${lib}" name) + find_library(${name} ${lib}) +endforeach() + +# IPv6 check +include(CheckStructHasMember) +check_struct_has_member("struct sockaddr_in6" sin6_addr "netinet/in.h" ZOO_IPV6_ENABLED) + +# configure +configure_file(cmake_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/config.h) + +# hashtable library +set(hashtable_sources src/hashtable/hashtable_itr.c src/hashtable/hashtable.c) +add_library(hashtable STATIC ${hashtable_sources}) +target_include_directories(hashtable PUBLIC include) +target_link_libraries(hashtable PUBLIC $<$,$>:m>) + +# zookeeper library +set(zookeeper_sources + src/zookeeper.c + src/recordio.c + generated/zookeeper.jute.c + src/zk_log.c + src/zk_hashtable.c + src/addrvec.c) + +if(WANT_SYNCAPI) + list(APPEND zookeeper_sources src/mt_adaptor.c) +else() + list(APPEND zookeeper_sources src/st_adaptor.c) +endif() + +if(WIN32) + list(APPEND zookeeper_sources src/winport.c) +endif() + +add_library(zookeeper STATIC ${zookeeper_sources}) +target_include_directories(zookeeper PUBLIC include ${CMAKE_CURRENT_BINARY_DIR}/include generated) +target_link_libraries(zookeeper PUBLIC + hashtable + $<$:rt> # clock_gettime + $<$:ws2_32>) # Winsock 2.0 + +option(WITH_OPENSSL "turn ON/OFF SSL support, or define openssl library location (default: ON)" ON) +message("-- using WITH_OPENSSL=${WITH_OPENSSL}") +if(NOT WITH_OPENSSL STREQUAL "OFF") + if(NOT WITH_OPENSSL STREQUAL "ON") + set(OPENSSL_ROOT_DIR,${WITH_OPENSSL}) + endif() + find_package(OpenSSL) + if(OPENSSL_FOUND) + target_compile_definitions(zookeeper PUBLIC HAVE_OPENSSL_H) + target_link_libraries(zookeeper PUBLIC OpenSSL::SSL OpenSSL::Crypto) + message("-- OpenSSL libraries found! will build with SSL support.") + else() + message("-- WARNING: unable to find OpenSSL libraries! will build without SSL support.") + endif() +endif() + +if(WANT_SYNCAPI AND NOT WIN32) + find_package(Threads REQUIRED) + target_link_libraries(zookeeper PUBLIC Threads::Threads) +endif() + +# cli executable +add_executable(cli src/cli.c) +target_link_libraries(cli zookeeper) + +# load_gen executable +if(WANT_SYNCAPI AND NOT WIN32) + add_executable(load_gen src/load_gen.c) + target_link_libraries(load_gen zookeeper) +endif() + +# tests +set(test_sources + tests/TestDriver.cc + tests/LibCMocks.cc + tests/LibCSymTable.cc + tests/MocksBase.cc + tests/ZKMocks.cc + tests/Util.cc + tests/ThreadingUtil.cc + tests/TestZookeeperInit.cc + tests/TestZookeeperClose.cc + tests/TestReconfig.cc + tests/TestReconfigServer.cc + tests/TestClientRetry.cc + tests/TestOperations.cc + tests/TestMulti.cc + tests/TestWatchers.cc + tests/TestClient.cc + tests/ZooKeeperQuorumServer.cc + tests/TestReadOnlyClient.cc + tests/TestLogClientEnv.cc) + +if(WANT_SYNCAPI) + list(APPEND test_sources tests/PthreadMocks.cc) +endif() + +if(WANT_CPPUNIT) + set (CMAKE_CXX_STANDARD 11) + add_executable(zktest ${test_sources}) + target_include_directories(zktest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + + target_compile_definitions(zktest + PRIVATE -DZKSERVER_CMD="${CMAKE_CURRENT_SOURCE_DIR}/tests/zkServer.sh") + # TODO: Use `find_library()` for `cppunit`. + target_link_libraries(zktest zookeeper cppunit dl) + + # This reads the link flags from the file `tests/wrappers.opt` into + # the variable `symbol_wrappers` for use in `target_link_libraries`. + # It is a holdover from the original build system. + file(STRINGS tests/wrappers.opt symbol_wrappers) + if(WANT_SYNCAPI) + file(STRINGS tests/wrappers-mt.opt symbol_wrappers_mt) + endif() + + target_link_libraries(zktest ${symbol_wrappers} ${symbol_wrappers_mt}) + + enable_testing() + add_test(NAME zktest_runner COMMAND zktest) + set_property(TEST zktest_runner PROPERTY ENVIRONMENT + "ZKROOT=${CMAKE_CURRENT_SOURCE_DIR}/../.." + "CLASSPATH=$CLASSPATH:$CLOVER_HOME/lib/clover*.jar") +endif() diff --git a/zookeeper-client/zookeeper-client-c/ChangeLog b/zookeeper-client/zookeeper-client-c/ChangeLog new file mode 100644 index 0000000..c85a6c0 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/ChangeLog @@ -0,0 +1,116 @@ +Release 2.1.1 +2008-04-30 Andrew Kornev + + * changed the distributino package name to "c-client-src" + +Release 2.1.0 +2008-04-30 Andrew Kornev + + * added the client latency diagnostics; the client prints a warning when the + reponse latency exceeds 20ms + + * modified logging format to report the znode path for which the zookeeper + operation is called + + * fixed a minor bug where error messages were missing for some of the newer + zookeeper error codes (ZCLOSING and ZNOTHING). + + * improved logging by adding the XID to the message to make it easy to match + requests to responses + + * fixed the bug causing sporadic session termination and timeouts + + * added a new return code to zookeeper_process() -- ZNOTHING -- + that indicates that the socket has no more data to read + + * more unit tests added + +Release 1.1.3 +2008-02-07 Andrew Kornev + + * get_xid() is not thread-safe (xid initialization race condition + in the multi-threaded mode). + + * the I/O thread doesnt automatically terminate on AUTH_FAILURE and + SESSION_EXPIRED events. + + * all session events should be processed on the completion thread. + + * PING operation doesnt atomically enqueue the completion and + send buffers like other operations do. + + * corrected zookeeper_init() doxygen docs. + + * new unit tests added. + + +Release 1.1.2 +2008-01-24 Andrew Kornev + + * fixed a race condition caused by the code in zookeeper_process() + and free_completions() setting sc->complete to 1 without proper + synchronization; + + * fixed zoo_get() not updating buffer_len value with the actual + buffer length on return; added missing enter_critical/leave_critical + calls to the async ZK operations. + + * Replaced select() with poll() to fix the problem with the FD_SET + macro causing stack corruption for FDs higher than 1024 + + * Added zoo_set_log_stream() to the public API. The function allows + applications to specify a different log file. + + * Removed unused declarations from zookeeper.h (ACL related) + + * changed zoo_get() signature to take a pointer to buffer length. + The function sets the parameter to the actual data length upon return. + + * the watcher callback now takes the zhandle as its first parameter. This + is to avoid a race condition in the multi-threaded client when a watcher + is called before zookeeper_init() has returned. + + * fixed zookeeper_close() resource leaks and race conditions, + fixed the race condition causing xid mismatch. + + * added support for cppunit, added new targets: "check" and "run-check" + to build and run unit tests. + + * Changed the signature of zookeeper_init(): it now takes a context pointer + as a parameter. This is to avoid a race condition in the multi-threaded client. + + * Using a self-pipe rather than SIGUSR1 to wake up select() in the I/O thread + + * Added the doxygen target to the autoconf scripts + + * Pulled out the logging functionality from zookeeper.c to zk_log.c/.h. + Fixed a minor issue with PING responses being unnecessarily put on + the completion queue rather than simply dropped. Make use of DLL_EXPORT + symbol for building shared lib on cygwin. + + * Implemented new Zookeeper operation sync() to flush the leader channel + to ensure that all updates have reached the followers. + + * Synchronous methods not being handled properly on disconnect + + * breed: fixed an incorrect parameter passed to zookeeper API by + the Sync API wrapper functions + + * breed: the set and delete commands now support both Sync and Async API. + Prefix the command name with an 'a' to call the Async API: aset, adelete + + * Make sure mutexes and condition variables are properly initialized + and destroyed + + * Fixed zookeeper_close() causing core dumps with mt_adaptor + + +Release 1.0.0 +2007-11-27 Andrew Kornev + + * configure.ac and Makefile.am added support for GNU autotools + + * recordio.c/.h updated jute IO routines to use bit-explicit integer types + (int32_t vs. int, and int64_t vs. long long) + + * README rough draft \ No newline at end of file diff --git a/zookeeper-client/zookeeper-client-c/INSTALL b/zookeeper-client/zookeeper-client-c/INSTALL new file mode 100644 index 0000000..5458714 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/INSTALL @@ -0,0 +1,234 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, +2006 Free Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf bug. Until the bug is fixed you can use this workaround: + + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/zookeeper-client/zookeeper-client-c/LICENSE b/zookeeper-client/zookeeper-client-c/LICENSE new file mode 100644 index 0000000..863a221 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/LICENSE @@ -0,0 +1,335 @@ + + 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. + + + +=========================================================================================== +=== The following part contains the dual OpenSSL and SSLeay license === +=== for OpenSSL versions 1.1.1, 1.1.0, 1.0.2 and all prior releases === +=== (see https://www.openssl.org/source/license.html) === +=========================================================================================== + + + LICENSE ISSUES + ============== + + The OpenSSL toolkit stays under a double license, i.e. both the conditions of + the OpenSSL License and the original SSLeay license apply to the toolkit. + See below for the actual license texts. + + OpenSSL License + --------------- + +/* ==================================================================== + * Copyright (c) 1998-2019 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * openssl-core@openssl.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + + Original SSLeay License + ----------------------- + +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ diff --git a/zookeeper-client/zookeeper-client-c/Makefile.am b/zookeeper-client/zookeeper-client-c/Makefile.am new file mode 100644 index 0000000..34ef012 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/Makefile.am @@ -0,0 +1,154 @@ +# need this for Doxygen integration +include $(top_srcdir)/aminclude.am + +AUTOMAKE_OPTIONS = serial-tests + +if SOLARIS + SOLARIS_CPPFLAGS = -D_POSIX_PTHREAD_SEMANTICS + SOLARIS_LIB_LDFLAGS = -lnsl -lsocket +endif + +if WANT_OPENSSL + OPENSSL_CPPFLAGS = -DHAVE_OPENSSL_H + OPENSSL_LIB_LDFLAGS = -lssl -lcrypto +endif + +AM_CPPFLAGS = -I${srcdir}/include -I${srcdir}/tests -I${srcdir}/generated $(SOLARIS_CPPFLAGS) $(OPENSSL_CPPFLAGS) +AM_CFLAGS = -Wall -Werror -Wdeclaration-after-statement +AM_CXXFLAGS = -Wall $(USEIPV6) +LIB_LDFLAGS = -no-undefined -version-info 2 $(SOLARIS_LIB_LDFLAGS) $(OPENSSL_LIB_LDFLAGS) + +# Additional flags for coverage testing (if enabled) +if ENABLEGCOV + AM_CFLAGS += -fprofile-arcs -ftest-coverage + AM_LDFLAGS = -lgcov +endif + +pkginclude_HEADERS = include/zookeeper.h include/zookeeper_version.h include/zookeeper_log.h include/proto.h include/recordio.h generated/zookeeper.jute.h +EXTRA_DIST=LICENSE + +HASHTABLE_SRC = src/hashtable/hashtable_itr.h src/hashtable/hashtable_itr.c \ + src/hashtable/hashtable_private.h src/hashtable/hashtable.h src/hashtable/hashtable.c + +noinst_LTLIBRARIES = libhashtable.la +libhashtable_la_SOURCES = $(HASHTABLE_SRC) + +COMMON_SRC = src/zookeeper.c include/zookeeper.h include/zookeeper_version.h include/zookeeper_log.h\ + src/recordio.c include/recordio.h include/proto.h \ + src/zk_adaptor.h generated/zookeeper.jute.c \ + src/zk_log.c src/zk_hashtable.h src/zk_hashtable.c \ + src/addrvec.h src/addrvec.c + +# These are the symbols (classes, mostly) we want to export from our library. +EXPORT_SYMBOLS = '(zoo_|zookeeper_|zhandle|Z|format_log_message|log_message|logLevel|deallocate_|allocate_|zerror|is_unrecoverable)' +noinst_LTLIBRARIES += libzkst.la +libzkst_la_SOURCES =$(COMMON_SRC) src/st_adaptor.c +libzkst_la_LIBADD = -lm $(CLOCK_GETTIME_LIBS) + +lib_LTLIBRARIES = libzookeeper_st.la +libzookeeper_st_la_SOURCES = +libzookeeper_st_la_LIBADD=libzkst.la libhashtable.la +libzookeeper_st_la_DEPENDENCIES=libzkst.la libhashtable.la +libzookeeper_st_la_LDFLAGS = $(LIB_LDFLAGS) -export-symbols-regex $(EXPORT_SYMBOLS) + +if WANT_SYNCAPI +noinst_LTLIBRARIES += libzkmt.la +libzkmt_la_SOURCES =$(COMMON_SRC) src/mt_adaptor.c +libzkmt_la_CFLAGS = -DTHREADED +libzkmt_la_LIBADD = -lm $(CLOCK_GETTIME_LIBS) + +lib_LTLIBRARIES += libzookeeper_mt.la +libzookeeper_mt_la_SOURCES = +libzookeeper_mt_la_LIBADD=libzkmt.la libhashtable.la -lpthread +libzookeeper_mt_la_DEPENDENCIES=libzkmt.la libhashtable.la +libzookeeper_mt_la_LDFLAGS = $(LIB_LDFLAGS) -export-symbols-regex $(EXPORT_SYMBOLS) +endif + +bin_PROGRAMS = cli_st + +cli_st_SOURCES = src/cli.c +cli_st_LDADD = libzookeeper_st.la + +if WANT_SYNCAPI +bin_PROGRAMS += cli_mt load_gen + +cli_mt_SOURCES = src/cli.c +cli_mt_LDADD = libzookeeper_mt.la +cli_mt_CFLAGS = -DTHREADED + +load_gen_SOURCES = src/load_gen.c +load_gen_LDADD = libzookeeper_mt.la +load_gen_CFLAGS = -DTHREADED + +endif + +######################################################################### +# build and run unit tests + +EXTRA_DIST+=$(wildcard ${srcdir}/tests/*.cc) $(wildcard ${srcdir}/tests/*.h) \ + ${srcdir}/tests/wrappers.opt ${srcdir}/tests/wrappers-mt.opt + +# These tests are ordered in a logical manner such that each builds upon basic +# functionality tested in prior tests. e.g. the most basic functionality is +# tested in TestZookeeperInit and TestZookeeperClose and as such should be tested +# first as a foundation with more complex test suites to follow. +TEST_SOURCES = \ + tests/TestDriver.cc \ + tests/LibCMocks.cc \ + tests/LibCSymTable.cc \ + tests/MocksBase.cc \ + tests/ZKMocks.cc \ + tests/Util.cc \ + tests/ThreadingUtil.cc \ + tests/TestZookeeperInit.cc \ + tests/TestZookeeperClose.cc \ + tests/TestReconfig.cc \ + tests/TestReconfigServer.cc \ + tests/TestClientRetry.cc \ + tests/TestOperations.cc \ + tests/TestMulti.cc \ + tests/TestWatchers.cc \ + tests/TestClient.cc \ + tests/ZooKeeperQuorumServer.cc \ + tests/ZooKeeperQuorumServer.h \ + tests/TestReadOnlyClient.cc \ + tests/TestLogClientEnv.cc \ + tests/TestServerRequireClientSASLAuth.cc \ + $(NULL) + +if SOLARIS + SHELL_SYMBOL_WRAPPERS = cat ${srcdir}/tests/wrappers.opt + SYMBOL_WRAPPERS=$(SHELL_SYMBOL_WRAPPERS:sh) +else + SYMBOL_WRAPPERS=$(shell cat ${srcdir}/tests/wrappers.opt) +endif + +check_PROGRAMS = zktest-st +TESTS_ENVIRONMENT = ZKROOT=${srcdir}/../.. \ + CLASSPATH=$$CLASSPATH:$$CLOVER_HOME/lib/clover*.jar +nodist_zktest_st_SOURCES = $(TEST_SOURCES) +zktest_st_LDADD = libzkst.la libhashtable.la $(CPPUNIT_LIBS) $(OPENSSL_LIB_LDFLAGS) -ldl +zktest_st_CXXFLAGS = -DUSE_STATIC_LIB $(CPPUNIT_CFLAGS) $(USEIPV6) $(SOLARIS_CPPFLAGS) +zktest_st_LDFLAGS = -shared $(SYMBOL_WRAPPERS) $(SOLARIS_LIB_LDFLAGS) + +if WANT_SYNCAPI + check_PROGRAMS += zktest-mt + nodist_zktest_mt_SOURCES = $(TEST_SOURCES) tests/PthreadMocks.cc + zktest_mt_LDADD = libzkmt.la libhashtable.la -lpthread $(CPPUNIT_LIBS) $(OPENSSL_LIB_LDFLAGS) -ldl + zktest_mt_CXXFLAGS = -DUSE_STATIC_LIB -DTHREADED $(CPPUNIT_CFLAGS) $(USEIPV6) +if SOLARIS + SHELL_SYMBOL_WRAPPERS_MT = cat ${srcdir}/tests/wrappers-mt.opt + SYMBOL_WRAPPERS_MT=$(SYMBOL_WRAPPERS) $(SHELL_SYMBOL_WRAPPERS_MT:sh) +else + SYMBOL_WRAPPERS_MT=$(SYMBOL_WRAPPERS) $(shell cat ${srcdir}/tests/wrappers-mt.opt) +endif + zktest_mt_LDFLAGS = -shared $(SYMBOL_WRAPPERS_MT) $(SOLARIS_LIB_LDFLAGS) +endif + +TESTS = $(check_PROGRAMS) + +clean-local: clean-check + $(RM) $(DX_CLEANFILES) + +clean-check: + $(RM) $(nodist_zktest_st_OBJECTS) $(nodist_zktest_mt_OBJECTS) diff --git a/zookeeper-client/zookeeper-client-c/NOTICE.txt b/zookeeper-client/zookeeper-client-c/NOTICE.txt new file mode 100644 index 0000000..07ef1ad --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/NOTICE.txt @@ -0,0 +1,41 @@ +Apache ZooKeeper +Copyright 2009-2011 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +---------- +include/winstdint.h is included only for Windows Client support, as follows: + +// ISO C9x compliant stdint.h for Microsoft Visual Studio +// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 +// +// Copyright (c) 2006-2008 Alexander Chemeris +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// 3. The name of the author may be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +/////////////////////////////////////////////////////////////////////////////// + +---------- diff --git a/zookeeper-client/zookeeper-client-c/README b/zookeeper-client/zookeeper-client-c/README new file mode 100644 index 0000000..0816f62 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/README @@ -0,0 +1,163 @@ + Zookeeper C client library + + +This package provides a C client interface to Zookeeper server. + +For the latest information about ZooKeeper, please visit our website at: + http://zookeeper.apache.org/ +and our wiki, at: + https://cwiki.apache.org/confluence/display/ZOOKEEPER + +Full documentation for this release can also be found in ../../docs/index.html + + +OVERVIEW + +The client supports two types of APIs -- synchronous and asynchronous. + +Asynchronous API provides non-blocking operations with completion callbacks and +relies on the application to implement event multiplexing on its behalf. + +On the other hand, Synchronous API provides a blocking flavor of +zookeeper operations and runs its own event loop in a separate thread. + +Sync and Async APIs can be mixed and matched within the same application. + +The package includes two shared libraries: zookeeper_st and +zookeeper_mt. The former only provides the Async API and is not +thread-safe. The only reason this library exists is to support the +platforms were pthread library is not available or unstable +(i.e. FreeBSD 4.x). In all other cases the application developers are +advised to link against zookeeper_mt as it includes support for both +Sync and Async API. + + +INSTALLATION + +If you're building the client from a source checkout you need to +follow the steps outlined below. If you're building from a release +tar downloaded from Apache please skip to step 2. + +1) do a "ant compile_jute" from the zookeeper top level directory (.../trunk). + This will create a directory named "generated" under zookeeper-client/zookeeper-client-c. + Skip to step 3. +2) unzip/untar the source tarball and cd to the zookeeper-x.x.x/zookeeper-client/zookeeper-client-c directory +3) change directory to zookeeper-client/zookeeper-client-c and do a "autoreconf -if" to bootstrap + autoconf, automake and libtool. Please make sure you have autoconf + version 2.59 or greater installed. If cppunit is installed in a non-standard + directory, you need to specify where to find cppunit.m4. For example, if + cppunit is installed under /usr/local, run: + + ACLOCAL="aclocal -I /usr/local/share/aclocal" autoreconf -if + +4) do a "./configure [OPTIONS]" to generate the makefile. See INSTALL + for general information about running configure. Additionally, the + configure supports the following options: + --enable-debug enables optimization and enables debug info compiler + options, disabled by default + --without-syncapi disables Sync API support; zookeeper_mt library won't + be built, enabled by default + --disable-static do not build static libraries, enabled by default + --disable-shared do not build shared libraries, enabled by default + --without-cppunit do not build the test library, enabled by default. + +5) do a "make" or "make install" to build the libraries and install them. + Alternatively, you can also build and run a unit test suite (and + you probably should). Please make sure you have cppunit-1.10.x or + higher installed before you execute step 4. Once ./configure has + finished, do a "make check". It will build the libraries, build + the tests and run them. +6) to generate doxygen documentation do a "make doxygen-doc". All + documentations will be placed to a new subfolder named docs. By + default only HTML documentation is generated. For information on + other document formats please use "./configure --help" + +Alternatively you can use the CMake build system. On Windows, this is required. +Follow steps 1 and 2 above, and then continue here. + +1) do a "cmake [OPTIONS]" to generate the makefile or msbuild files (the correct + build system will be generated based on your platform). Some options from above + are supported: + -DCMAKE_BUILD_TYPE Debug by default, Release enables optimzation etc. + -DWANT_SYNCAPI ON by default, OFF disables the Sync API support + -DWANT_CPPUNIT ON except on Windows, OFF disables the tests + -DWITH_OPENSSL ON by default, OFF disables the SSL support. You can also + specify a custom path by -DWITH_OPENSSL=/path/to/openssl/ + -DBUILD_SHARED_LIBS not yet supported, only static libraries are built + other CMake options see "cmake --help" for generic options, such as generator + +2) do a "cmake --build ." to build the default targets. Alternatively you can + invoke "make" or "msbuild" manually. If the tests were enabled, use "ctest -V" + to run them. + +Current limitations of the CMake build system include lack of Solaris support, +no shared library option, no explicitly exported symbols (all are exported by +default), no versions on the libraries, and no documentation generation. +Features of CMake include a single, easily consumed cross-platform build system +to generate the ZooKeeper C Client libraries for any project, with little to no +configuration. + +EXAMPLE/SAMPLE C CLIENT SHELL + +NOTE: the ZooKeeper C client shell (cli_st and cli_mt) is meant as a +example/sample of ZooKeeper C client API usage. It is not a full +fledged client and not meant for production usage - see the Java +client shell for a fully featured shell. + +You can test your client by running a zookeeper server (see +instructions on the project wiki page on how to run it) and connecting +to it using the zookeeper shell application cli that is built as part +of the installation procedure. + +cli_mt (multithreaded, built against zookeeper_mt library) is shown in +this example, but you could also use cli_st (singlethreaded, built +against zookeeper_st library): + +$ cli_mt zookeeper_host:9876 + +To start a client with read-only mode enabled, use the -r flag: + +$ cli_mt -r zookeeper_host:9876 + +This is a client application that gives you a shell for executing +simple zookeeper commands. Once successfully started and connected to +the server it displays a shell prompt. + +You can now enter zookeeper commands. For example, to create a node: + +> create /my_new_node + +To verify that the node's been created: + +> ls / + +You should see a list of nodes who are the children of the root node "/". + +Here's a list of command supported by the cli shell: + +ls -- list children of a znode identified by . The + command set a children watch on the znode. +get -- get the value of a znode at +set -- set the value of a znode at to +create [+e|+s] -- create a znode as a child of znode ; + use +e option to create an ephemeral znode, + use +s option to create a znode with a sequence number + appended to the name. The operation will fail if + the parent znode (the one identified by ) doesn't + exist. +delete -- delete the znode at . The command will fail if the znode + has children. +sync -- make sure all pending updates have been applied to znode at +exists -- returns a result code indicating whether the znode at + exists. The command also sets a znode watch. +myid -- prints out the current zookeeper session id. +quit -- exit the shell. + +In order to be able to use the zookeeper API in your application you have to +1) remember to include the zookeeper header + #include +2) use -DTHREADED compiler option to enable Sync API; in this case you should + be linking your code against zookeeper_mt library + +Please take a look at cli.c to understand how to use the two API types. +(TODO: some kind of short tutorial would be helpful, I guess) diff --git a/zookeeper-client/zookeeper-client-c/acinclude.m4 b/zookeeper-client/zookeeper-client-c/acinclude.m4 new file mode 100644 index 0000000..9734f21 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/acinclude.m4 @@ -0,0 +1,327 @@ +# This file is part of Autoconf. -*- Autoconf -*- + +# Copyright (C) 2004 Oren Ben-Kiki +# This file is distributed under the same terms as the Autoconf macro files. + +# Generate automatic documentation using Doxygen. Works in concert with the +# aminclude.m4 file and a compatible doxygen configuration file. Defines the +# following public macros: +# +# DX_???_FEATURE(ON|OFF) - control the default setting of a Doxygen feature. +# Supported features are 'DOXYGEN' itself, 'DOT' for generating graphics, +# 'HTML' for plain HTML, 'CHM' for compressed HTML help (for MS users), 'CHI' +# for generating a seperate .chi file by the .chm file, and 'MAN', 'RTF', +# 'XML', 'PDF' and 'PS' for the appropriate output formats. The environment +# variable DOXYGEN_PAPER_SIZE may be specified to override the default 'a4wide' +# paper size. +# +# By default, HTML, PDF and PS documentation is generated as this seems to be +# the most popular and portable combination. MAN pages created by Doxygen are +# usually problematic, though by picking an appropriate subset and doing some +# massaging they might be better than nothing. CHM and RTF are specific for MS +# (note that you can't generate both HTML and CHM at the same time). The XML is +# rather useless unless you apply specialized post-processing to it. +# +# The macro mainly controls the default state of the feature. The use can +# override the default by specifying --enable or --disable. The macros ensure +# that contradictory flags are not given (e.g., --enable-doxygen-html and +# --enable-doxygen-chm, --enable-doxygen-anything with --disable-doxygen, etc.) +# Finally, each feature will be automatically disabled (with a warning) if the +# required programs are missing. +# +# Once all the feature defaults have been specified, call DX_INIT_DOXYGEN with +# the following parameters: a one-word name for the project for use as a +# filename base etc., an optional configuration file name (the default is +# 'Doxyfile', the same as Doxygen's default), and an optional output directory +# name (the default is 'doxygen-doc'). + +## ----------## +## Defaults. ## +## ----------## + +DX_ENV="" +AC_DEFUN([DX_FEATURE_doc], ON) +AC_DEFUN([DX_FEATURE_dot], ON) +AC_DEFUN([DX_FEATURE_man], OFF) +AC_DEFUN([DX_FEATURE_html], ON) +AC_DEFUN([DX_FEATURE_chm], OFF) +AC_DEFUN([DX_FEATURE_chi], OFF) +AC_DEFUN([DX_FEATURE_rtf], OFF) +AC_DEFUN([DX_FEATURE_xml], OFF) +AC_DEFUN([DX_FEATURE_pdf], ON) +AC_DEFUN([DX_FEATURE_ps], ON) + +## --------------- ## +## Private macros. ## +## --------------- ## + +# DX_ENV_APPEND(VARIABLE, VALUE) +# ------------------------------ +# Append VARIABLE="VALUE" to DX_ENV for invoking doxygen. +AC_DEFUN([DX_ENV_APPEND], [AC_SUBST([DX_ENV], ["$DX_ENV $1='$2'"])]) + +# DX_DIRNAME_EXPR +# --------------- +# Expand into a shell expression prints the directory part of a path. +AC_DEFUN([DX_DIRNAME_EXPR], + [[expr ".$1" : '\(\.\)[^/]*$' \| "x$1" : 'x\(.*\)/[^/]*$']]) + +# DX_IF_FEATURE(FEATURE, IF-ON, IF-OFF) +# ------------------------------------- +# Expands according to the M4 (static) status of the feature. +AC_DEFUN([DX_IF_FEATURE], [ifelse(DX_FEATURE_$1, ON, [$2], [$3])]) + +# DX_REQUIRE_PROG(VARIABLE, PROGRAM) +# ---------------------------------- +# Require the specified program to be found for the DX_CURRENT_FEATURE to work. +AC_DEFUN([DX_REQUIRE_PROG], [ +AC_PATH_TOOL([$1], [$2]) +if test "$DX_FLAG_$[DX_CURRENT_FEATURE$$1]" = 1; then + AC_MSG_WARN([$2 not found - will not DX_CURRENT_DESCRIPTION]) + AC_SUBST([DX_FLAG_]DX_CURRENT_FEATURE, 0) +fi +]) + +# DX_TEST_FEATURE(FEATURE) +# ------------------------ +# Expand to a shell expression testing whether the feature is active. +AC_DEFUN([DX_TEST_FEATURE], [test "$DX_FLAG_$1" = 1]) + +# DX_CHECK_DEPEND(REQUIRED_FEATURE, REQUIRED_STATE) +# ------------------------------------------------- +# Verify that a required features has the right state before trying to turn on +# the DX_CURRENT_FEATURE. +AC_DEFUN([DX_CHECK_DEPEND], [ +test "$DX_FLAG_$1" = "$2" \ +|| AC_MSG_ERROR([doxygen-DX_CURRENT_FEATURE ifelse([$2], 1, + requires, contradicts) doxygen-DX_CURRENT_FEATURE]) +]) + +# DX_CLEAR_DEPEND(FEATURE, REQUIRED_FEATURE, REQUIRED_STATE) +# ---------------------------------------------------------- +# Turn off the DX_CURRENT_FEATURE if the required feature is off. +AC_DEFUN([DX_CLEAR_DEPEND], [ +test "$DX_FLAG_$1" = "$2" || AC_SUBST([DX_FLAG_]DX_CURRENT_FEATURE, 0) +]) + +# DX_FEATURE_ARG(FEATURE, DESCRIPTION, +# CHECK_DEPEND, CLEAR_DEPEND, +# REQUIRE, DO-IF-ON, DO-IF-OFF) +# -------------------------------------------- +# Parse the command-line option controlling a feature. CHECK_DEPEND is called +# if the user explicitly turns the feature on (and invokes DX_CHECK_DEPEND), +# otherwise CLEAR_DEPEND is called to turn off the default state if a required +# feature is disabled (using DX_CLEAR_DEPEND). REQUIRE performs additional +# requirement tests (DX_REQUIRE_PROG). Finally, an automake flag is set and +# DO-IF-ON or DO-IF-OFF are called according to the final state of the feature. +AC_DEFUN([DX_ARG_ABLE], [ + AC_DEFUN([DX_CURRENT_FEATURE], [$1]) + AC_DEFUN([DX_CURRENT_DESCRIPTION], [$2]) + AC_ARG_ENABLE(doxygen-$1, + [AS_HELP_STRING(DX_IF_FEATURE([$1], [--disable-doxygen-$1], + [--enable-doxygen-$1]), + DX_IF_FEATURE([$1], [don't $2], [$2]))], + [ +case "$enableval" in +#( +y|Y|yes|Yes|YES) + AC_SUBST([DX_FLAG_$1], 1) + $3 +;; #( +n|N|no|No|NO) + AC_SUBST([DX_FLAG_$1], 0) +;; #( +*) + AC_MSG_ERROR([invalid value '$enableval' given to doxygen-$1]) +;; +esac +], [ +AC_SUBST([DX_FLAG_$1], [DX_IF_FEATURE([$1], 1, 0)]) +$4 +]) +if DX_TEST_FEATURE([$1]); then + $5 + : +fi +if DX_TEST_FEATURE([$1]); then + AM_CONDITIONAL(DX_COND_$1, :) + $6 + : +else + AM_CONDITIONAL(DX_COND_$1, false) + $7 + : +fi +]) + +## -------------- ## +## Public macros. ## +## -------------- ## + +# DX_XXX_FEATURE(DEFAULT_STATE) +# ----------------------------- +AC_DEFUN([DX_DOXYGEN_FEATURE], [AC_DEFUN([DX_FEATURE_doc], [$1])]) +AC_DEFUN([DX_MAN_FEATURE], [AC_DEFUN([DX_FEATURE_man], [$1])]) +AC_DEFUN([DX_HTML_FEATURE], [AC_DEFUN([DX_FEATURE_html], [$1])]) +AC_DEFUN([DX_CHM_FEATURE], [AC_DEFUN([DX_FEATURE_chm], [$1])]) +AC_DEFUN([DX_CHI_FEATURE], [AC_DEFUN([DX_FEATURE_chi], [$1])]) +AC_DEFUN([DX_RTF_FEATURE], [AC_DEFUN([DX_FEATURE_rtf], [$1])]) +AC_DEFUN([DX_XML_FEATURE], [AC_DEFUN([DX_FEATURE_xml], [$1])]) +AC_DEFUN([DX_XML_FEATURE], [AC_DEFUN([DX_FEATURE_xml], [$1])]) +AC_DEFUN([DX_PDF_FEATURE], [AC_DEFUN([DX_FEATURE_pdf], [$1])]) +AC_DEFUN([DX_PS_FEATURE], [AC_DEFUN([DX_FEATURE_ps], [$1])]) + +# DX_INIT_DOXYGEN(PROJECT, [CONFIG-FILE], [OUTPUT-DOC-DIR]) +# --------------------------------------------------------- +# PROJECT also serves as the base name for the documentation files. +# The default CONFIG-FILE is "Doxyfile" and OUTPUT-DOC-DIR is "doxygen-doc". +AC_DEFUN([DX_INIT_DOXYGEN], [ + +# Files: +AC_SUBST([DX_PROJECT], [$1]) +AC_SUBST([DX_CONFIG], [ifelse([$2], [], Doxyfile, [$2])]) +AC_SUBST([DX_DOCDIR], [ifelse([$3], [], doxygen-doc, [$3])]) + +# Environment variables used inside doxygen.cfg: +DX_ENV_APPEND(SRCDIR, $srcdir) +DX_ENV_APPEND(PROJECT, $DX_PROJECT) +DX_ENV_APPEND(DOCDIR, $DX_DOCDIR) +DX_ENV_APPEND(VERSION, $PACKAGE_VERSION) + +# Doxygen itself: +DX_ARG_ABLE(doc, [generate any doxygen documentation], + [], + [], + [DX_REQUIRE_PROG([DX_DOXYGEN], doxygen) + DX_REQUIRE_PROG([DX_PERL], perl)], + [DX_ENV_APPEND(PERL_PATH, $DX_PERL)]) + +# Dot for graphics: +DX_ARG_ABLE(dot, [generate graphics for doxygen documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_DOT], dot)], + [DX_ENV_APPEND(HAVE_DOT, YES) + DX_ENV_APPEND(DOT_PATH, [`DX_DIRNAME_EXPR($DX_DOT)`])], + [DX_ENV_APPEND(HAVE_DOT, NO)]) + +# Man pages generation: +DX_ARG_ABLE(man, [generate doxygen manual pages], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_MAN, YES)], + [DX_ENV_APPEND(GENERATE_MAN, NO)]) + +# RTF file generation: +DX_ARG_ABLE(rtf, [generate doxygen RTF documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_RTF, YES)], + [DX_ENV_APPEND(GENERATE_RTF, NO)]) + +# XML file generation: +DX_ARG_ABLE(xml, [generate doxygen XML documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_XML, YES)], + [DX_ENV_APPEND(GENERATE_XML, NO)]) + +# (Compressed) HTML help generation: +DX_ARG_ABLE(chm, [generate doxygen compressed HTML help documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_HHC], hhc)], + [DX_ENV_APPEND(HHC_PATH, $DX_HHC) + DX_ENV_APPEND(GENERATE_HTML, YES) + DX_ENV_APPEND(GENERATE_HTMLHELP, YES)], + [DX_ENV_APPEND(GENERATE_HTMLHELP, NO)]) + +# Seperate CHI file generation. +DX_ARG_ABLE(chi, [generate doxygen seperate compressed HTML help index file], + [DX_CHECK_DEPEND(chm, 1)], + [DX_CLEAR_DEPEND(chm, 1)], + [], + [DX_ENV_APPEND(GENERATE_CHI, YES)], + [DX_ENV_APPEND(GENERATE_CHI, NO)]) + +# Plain HTML pages generation: +DX_ARG_ABLE(html, [generate doxygen plain HTML documentation], + [DX_CHECK_DEPEND(doc, 1) DX_CHECK_DEPEND(chm, 0)], + [DX_CLEAR_DEPEND(doc, 1) DX_CLEAR_DEPEND(chm, 0)], + [], + [DX_ENV_APPEND(GENERATE_HTML, YES)], + [DX_TEST_FEATURE(chm) || DX_ENV_APPEND(GENERATE_HTML, NO)]) + +# PostScript file generation: +DX_ARG_ABLE(ps, [generate doxygen PostScript documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_LATEX], latex) + DX_REQUIRE_PROG([DX_MAKEINDEX], makeindex) + DX_REQUIRE_PROG([DX_DVIPS], dvips) + DX_REQUIRE_PROG([DX_EGREP], egrep)]) + +# PDF file generation: +DX_ARG_ABLE(pdf, [generate doxygen PDF documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_PDFLATEX], pdflatex) + DX_REQUIRE_PROG([DX_MAKEINDEX], makeindex) + DX_REQUIRE_PROG([DX_EGREP], egrep)]) + +# LaTeX generation for PS and/or PDF: +if DX_TEST_FEATURE(ps) || DX_TEST_FEATURE(pdf); then + AM_CONDITIONAL(DX_COND_latex, :) + DX_ENV_APPEND(GENERATE_LATEX, YES) +else + AM_CONDITIONAL(DX_COND_latex, false) + DX_ENV_APPEND(GENERATE_LATEX, NO) +fi + +# Paper size for PS and/or PDF: +AC_ARG_VAR(DOXYGEN_PAPER_SIZE, + [a4wide (default), a4, letter, legal or executive]) +case "$DOXYGEN_PAPER_SIZE" in +#( +"") + AC_SUBST(DOXYGEN_PAPER_SIZE, "") +;; #( +a4wide|a4|letter|legal|executive) + DX_ENV_APPEND(PAPER_SIZE, $DOXYGEN_PAPER_SIZE) +;; #( +*) + AC_MSG_ERROR([unknown DOXYGEN_PAPER_SIZE='$DOXYGEN_PAPER_SIZE']) +;; +esac + +#For debugging: +#echo DX_FLAG_doc=$DX_FLAG_doc +#echo DX_FLAG_dot=$DX_FLAG_dot +#echo DX_FLAG_man=$DX_FLAG_man +#echo DX_FLAG_html=$DX_FLAG_html +#echo DX_FLAG_chm=$DX_FLAG_chm +#echo DX_FLAG_chi=$DX_FLAG_chi +#echo DX_FLAG_rtf=$DX_FLAG_rtf +#echo DX_FLAG_xml=$DX_FLAG_xml +#echo DX_FLAG_pdf=$DX_FLAG_pdf +#echo DX_FLAG_ps=$DX_FLAG_ps +#echo DX_ENV=$DX_ENV +]) + +# CHECK_CPPUNIT +# ------------------ +# Check for cppunit presence. +AC_DEFUN([CHECK_CPPUNIT], [ + ifdef( + [AM_PATH_CPPUNIT], + [AM_PATH_CPPUNIT($1)], + [ifdef( + [PKG_CHECK_MODULES], + [PKG_CHECK_MODULES([CPPUNIT], [cppunit >= $1])], + [m4_fatal([Missing AM_PATH_CPPUNIT or PKG_CHECK_MODULES m4 macro.])] + )] + ) +]) diff --git a/zookeeper-client/zookeeper-client-c/aminclude.am b/zookeeper-client/zookeeper-client-c/aminclude.am new file mode 100644 index 0000000..420049e --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/aminclude.am @@ -0,0 +1,186 @@ +# Copyright (C) 2004 Oren Ben-Kiki +# This file is distributed under the same terms as the Automake macro files. + +# Generate automatic documentation using Doxygen. Goals and variables values +# are controlled by the various DX_COND_??? conditionals set by autoconf. +# +# The provided goals are: +# doxygen-doc: Generate all doxygen documentation. +# doxygen-run: Run doxygen, which will generate some of the documentation +# (HTML, CHM, CHI, MAN, RTF, XML) but will not do the post +# processing required for the rest of it (PS, PDF, and some MAN). +# doxygen-man: Rename some doxygen generated man pages. +# doxygen-ps: Generate doxygen PostScript documentation. +# doxygen-pdf: Generate doxygen PDF documentation. +# +# Note that by default these are not integrated into the automake goals. If +# doxygen is used to generate man pages, you can achieve this integration by +# setting man3_MANS to the list of man pages generated and then adding the +# dependency: +# +# $(man3_MANS): doxygen-doc +# +# This will cause make to run doxygen and generate all the documentation. +# +# The following variable is intended for use in Makefile.am: +# +# DX_CLEANFILES = everything to clean. +# +# This is usually added to MOSTLYCLEANFILES. + +## --------------------------------- ## +## Format-independent Doxygen rules. ## +## --------------------------------- ## + +if DX_COND_doc + +## ------------------------------- ## +## Rules specific for HTML output. ## +## ------------------------------- ## + +if DX_COND_html + +DX_CLEAN_HTML = @DX_DOCDIR@/html + +endif DX_COND_html + +## ------------------------------ ## +## Rules specific for CHM output. ## +## ------------------------------ ## + +if DX_COND_chm + +DX_CLEAN_CHM = @DX_DOCDIR@/chm + +if DX_COND_chi + +DX_CLEAN_CHI = @DX_DOCDIR@/@PACKAGE@.chi + +endif DX_COND_chi + +endif DX_COND_chm + +## ------------------------------ ## +## Rules specific for MAN output. ## +## ------------------------------ ## + +if DX_COND_man + +DX_CLEAN_MAN = @DX_DOCDIR@/man + +endif DX_COND_man + +## ------------------------------ ## +## Rules specific for RTF output. ## +## ------------------------------ ## + +if DX_COND_rtf + +DX_CLEAN_RTF = @DX_DOCDIR@/rtf + +endif DX_COND_rtf + +## ------------------------------ ## +## Rules specific for XML output. ## +## ------------------------------ ## + +if DX_COND_xml + +DX_CLEAN_XML = @DX_DOCDIR@/xml + +endif DX_COND_xml + +## ----------------------------- ## +## Rules specific for PS output. ## +## ----------------------------- ## + +if DX_COND_ps + +DX_CLEAN_PS = @DX_DOCDIR@/@PACKAGE@.ps + +DX_PS_GOAL = doxygen-ps + +doxygen-ps: @DX_DOCDIR@/@PACKAGE@.ps + +@DX_DOCDIR@/@PACKAGE@.ps: @DX_DOCDIR@/@PACKAGE@.tag + cd @DX_DOCDIR@/latex; \ + rm -f *.aux *.toc *.idx *.ind *.ilg *.log *.out; \ + $(DX_LATEX) refman.tex; \ + $(MAKEINDEX_PATH) refman.idx; \ + $(DX_LATEX) refman.tex; \ + countdown=5; \ + while $(DX_EGREP) 'Rerun (LaTeX|to get cross-references right)' \ + refman.log > /dev/null 2>&1 \ + && test $$countdown -gt 0; do \ + $(DX_LATEX) refman.tex; \ + countdown=`expr $$countdown - 1`; \ + done; \ + $(DX_DVIPS) -o ../@PACKAGE@.ps refman.dvi + +endif DX_COND_ps + +## ------------------------------ ## +## Rules specific for PDF output. ## +## ------------------------------ ## + +if DX_COND_pdf + +DX_CLEAN_PDF = @DX_DOCDIR@/@PACKAGE@.pdf + +DX_PDF_GOAL = doxygen-pdf + +doxygen-pdf: @DX_DOCDIR@/@PACKAGE@.pdf + +@DX_DOCDIR@/@PACKAGE@.pdf: @DX_DOCDIR@/@PACKAGE@.tag + cd @DX_DOCDIR@/latex; \ + rm -f *.aux *.toc *.idx *.ind *.ilg *.log *.out; \ + $(DX_PDFLATEX) refman.tex; \ + $(DX_MAKEINDEX) refman.idx; \ + $(DX_PDFLATEX) refman.tex; \ + countdown=5; \ + while $(DX_EGREP) 'Rerun (LaTeX|to get cross-references right)' \ + refman.log > /dev/null 2>&1 \ + && test $$countdown -gt 0; do \ + $(DX_PDFLATEX) refman.tex; \ + countdown=`expr $$countdown - 1`; \ + done; \ + mv refman.pdf ../@PACKAGE@.pdf + +endif DX_COND_pdf + +## ------------------------------------------------- ## +## Rules specific for LaTeX (shared for PS and PDF). ## +## ------------------------------------------------- ## + +if DX_COND_latex + +DX_CLEAN_LATEX = @DX_DOCDIR@/latex + +endif DX_COND_latex + +.PHONY: doxygen-run doxygen-doc $(DX_PS_GOAL) $(DX_PDF_GOAL) + +.INTERMEDIATE: doxygen-run $(DX_PS_GOAL) $(DX_PDF_GOAL) + +doxygen-run: @DX_DOCDIR@/@PACKAGE@.tag + +doxygen-doc: doxygen-run $(DX_PS_GOAL) $(DX_PDF_GOAL) + +@DX_DOCDIR@/@PACKAGE@.tag: $(DX_CONFIG) $(pkginclude_HEADERS) + rm -rf @DX_DOCDIR@ + $(DX_ENV) $(DX_DOXYGEN) $(srcdir)/$(DX_CONFIG) + +DX_CLEANFILES = \ + @DX_DOCDIR@/@PACKAGE@.tag \ + -r \ + $(DX_CLEAN_HTML) \ + $(DX_CLEAN_CHM) \ + $(DX_CLEAN_CHI) \ + $(DX_CLEAN_MAN) \ + $(DX_CLEAN_RTF) \ + $(DX_CLEAN_XML) \ + $(DX_CLEAN_PS) \ + $(DX_CLEAN_PDF) \ + $(DX_CLEAN_LATEX) + +endif DX_COND_doc diff --git a/zookeeper-client/zookeeper-client-c/c-doc.Doxyfile b/zookeeper-client/zookeeper-client-c/c-doc.Doxyfile new file mode 100644 index 0000000..e9029cb --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/c-doc.Doxyfile @@ -0,0 +1,1252 @@ +# Doxyfile 1.4.7 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = $(PROJECT)-$(VERSION) + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = $(DOCDIR) + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Brazilian, Catalan, Chinese, Chinese-Traditional, Croatian, Czech, Danish, +# Dutch, Finnish, French, German, Greek, Hungarian, Italian, Japanese, +# Japanese-en (Japanese with English messages), Korean, Korean-en, Norwegian, +# Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovene, Spanish, +# Swedish, and Ukrainian. + +OUTPUT_LANGUAGE = English + +# This tag can be used to specify the encoding used in the generated output. +# The encoding is not always determined by the language that is chosen, +# but also whether or not the output is meant for Windows or non-Windows users. +# In case there is a difference, setting the USE_WINDOWS_ENCODING tag to YES +# forces the Windows encoding (this is the default for the Windows binary), +# whereas setting the tag to NO uses a Unix-style encoding (the default for +# all platforms other than Windows). + +USE_WINDOWS_ENCODING = NO + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like the Qt-style comments (thus requiring an +# explicit @brief command for a brief description. + +JAVADOC_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the DETAILS_AT_TOP tag is set to YES then Doxygen +# will output the detailed description near the top, like JavaDoc. +# If set to NO, the detailed description appears after the member +# documentation. + +DETAILS_AT_TOP = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = YES + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for Java. +# For instance, namespaces will be presented as packages, qualified scopes +# will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want to +# include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = NO + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = YES + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = NO + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST = YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. The default is NO. + +SHOW_DIRECTORIES = NO + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from the +# version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = include/zookeeper.h + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py + +FILE_PATTERNS = + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES (the default) +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES (the default) +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. Otherwise they will link to the documentstion. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = $(GENERATE_HTML) + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compressed HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = $(GENERATE_HTMLHELP) + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = ../$(PROJECT).chm + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = $(HHC_PATH) + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = $(GENERATE_CHI) + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# If the GENERATE_TREEVIEW tag is set to YES, a side panel will be +# generated containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (for instance Mozilla 1.0+, +# Netscape 6.0+, Internet explorer 5.0+, or Konqueror). Windows users are +# probably better off using the HTML help feature. + +GENERATE_TREEVIEW = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = $(GENERATE_LATEX) + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = $(PAPER_SIZE) + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = NO + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = $(GENERATE_PDF) + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = $(GENERATE_RTF) + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = $(GENERATE_MAN) + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = $(GENERATE_XML) + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. This is useful +# if you want to understand what is going on. On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = $(DOCDIR)/$(PROJECT).tag + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = $(HAVE_DOT) + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a call dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable call graphs for selected +# functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a caller dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable caller graphs for selected +# functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = $(DOT_PATH) + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The MAX_DOT_GRAPH_WIDTH tag can be used to set the maximum allowed width +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_WIDTH = 1024 + +# The MAX_DOT_GRAPH_HEIGHT tag can be used to set the maximum allows height +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_HEIGHT = 1024 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that a graph may be further truncated if the graph's +# image dimensions are not sufficient to fit the graph (see MAX_DOT_GRAPH_WIDTH +# and MAX_DOT_GRAPH_HEIGHT). If 0 is used for the depth value (the default), +# the graph is not depth-constrained. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, which results in a white background. +# Warning: Depending on the platform used, enabling this option may lead to +# badly anti-aliased labels on the edges of a graph (i.e. they become hard to +# read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- + +# The SEARCHENGINE tag specifies whether or not a search engine should be +# used. If set to NO the values of all tags below this one will be ignored. + +SEARCHENGINE = NO diff --git a/zookeeper-client/zookeeper-client-c/cmake_config.h.in b/zookeeper-client/zookeeper-client-c/cmake_config.h.in new file mode 100644 index 0000000..33bcc6c --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/cmake_config.h.in @@ -0,0 +1,157 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef CONFIG_H_ +#define CONFIG_H_ + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_ARPA_INET_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_DLFCN_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_FCNTL_H 1 + +/* Define to 1 if you have the file `generated/zookeeper.jute.c'. */ +#cmakedefine HAVE_GENERATED_ZOOKEEPER_JUTE_C 1 + +/* Define to 1 if you have the file `generated/zookeeper.jute.h'. */ +#cmakedefine HAVE_GENERATED_ZOOKEEPER_JUTE_H 1 + +/* Define to 1 if you have the `getcwd' function. */ +#cmakedefine HAVE_GETCWD 1 + +/* Define to 1 if you have the `gethostbyname' function. */ +#cmakedefine HAVE_GETHOSTBYNAME 1 + +/* Define to 1 if you have the `gethostname' function. */ +#cmakedefine HAVE_GETHOSTNAME 1 + +/* Define to 1 if you have the `getlogin' function. */ +#cmakedefine HAVE_GETLOGIN 1 + +/* Define to 1 if you have the `getpwuid_r' function. */ +#cmakedefine HAVE_GETPWUID_R 1 + +/* Define to 1 if you have the `gettimeofday' function. */ +#cmakedefine HAVE_GETTIMEOFDAY 1 + +/* Define to 1 if you have the `getuid' function. */ +#cmakedefine HAVE_GETUID 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the `rt' library (-lrt). */ +#cmakedefine HAVE_LIBRT 1 + +/* Define to 1 if you have the `memmove' function. */ +#cmakedefine HAVE_MEMMOVE 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `memset' function. */ +#cmakedefine HAVE_MEMSET 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_NETDB_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_NETINET_IN_H 1 + +/* Define to 1 if you have the `poll' function. */ +#cmakedefine HAVE_POLL 1 + +/* Define to 1 if you have the `socket' function. */ +#cmakedefine HAVE_SOCKET 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_STDLIB_H 1 + +/* Define to 1 if you have the `strchr' function. */ +#cmakedefine HAVE_STRCHR 1 + +/* Define to 1 if you have the `strdup' function. */ +#cmakedefine HAVE_STRDUP 1 + +/* Define to 1 if you have the `strerror' function. */ +#cmakedefine HAVE_STRERROR 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_STRING_H 1 + +/* Define to 1 if you have the `strtol' function. */ +#cmakedefine HAVE_STRTOL 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_SOCKET_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_TIME_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_UTSNAME_H 1 + +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_UNISTD_H 1 + +/* Define to 1 if IPv6 support is available. */ +#cmakedefine ZOO_IPV6_ENABLED 1 + +/* Define to 1 if SOCK_CLOEXEC is available and wanted */ +#cmakedefine SOCK_CLOEXEC_ENABLED 1 + +/* poll() second argument type */ +#define POLL_NFDS_TYPE nfds_t + +/* Name of package */ +#define PACKAGE "${PROJECT_NAME}" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "${email}" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "${description}" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "${description} ${PROJECT_VERSION}" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "${PROJECT_NAME}" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "${PROJECT_VERSION}" + +/* Version number of package */ +#define VERSION "${PROJECT_VERSION}" + +#endif diff --git a/zookeeper-client/zookeeper-client-c/configure.ac b/zookeeper-client/zookeeper-client-c/configure.ac new file mode 100644 index 0000000..4f08bf3 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/configure.ac @@ -0,0 +1,245 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) + +AC_INIT([zookeeper C client],3.6.3,[user@zookeeper.apache.org],[zookeeper]) +AC_CONFIG_SRCDIR([src/zookeeper.c]) + +# Save initial CFLAGS and CXXFLAGS values before AC_PROG_CC and AC_PROG_CXX +init_cflags="$CFLAGS" +init_cxxflags="$CXXFLAGS" + +# initialize Doxygen support +DX_HTML_FEATURE(ON) +DX_CHM_FEATURE(OFF) +DX_CHI_FEATURE(OFF) +DX_MAN_FEATURE(OFF) +DX_RTF_FEATURE(OFF) +DX_XML_FEATURE(OFF) +DX_PDF_FEATURE(OFF) +DX_PS_FEATURE(OFF) +DX_INIT_DOXYGEN([zookeeper],[c-doc.Doxyfile],[docs]) + +# initialize automake +AM_INIT_AUTOMAKE([-Wall foreign]) + +AC_CONFIG_HEADER([config.h]) + +# Checks for programs. +AC_ARG_WITH(cppunit, + [ --without-cppunit do not use CPPUNIT]) + +if test "$with_cppunit" = "no" ; then + CPPUNIT_PATH="No_CPPUNIT" + CPPUNIT_INCLUDE= + CPPUNIT_LIBS= +else + CHECK_CPPUNIT(1.10.2) +fi + +dnl OpenSSL +AC_ARG_WITH(openssl, + [AC_HELP_STRING([--with-openssl[=DIR]], [build with openssl (autodetect openssl library by default) )])], + [], [with_openssl=yes]) +AC_MSG_NOTICE([configuring SSL using --with-openssl=$with_openssl]) +saved_CPPFLAGS="$CPPFLAGS" +saved_LDFLAGS="$LDFLAGS" +if test "x$with_openssl" != "xno" && test "x$with_openssl" != "xyes" ; then + CPPFLAGS="$CPPFLAGS -I$with_openssl/include" + LDFLAGS="$LDFLAGS -L$with_openssl/lib" +fi +have_openssl=no +AC_CHECK_HEADER(openssl/ssl.h, [ AC_CHECK_LIB(ssl, SSL_CTX_new, [have_openssl=yes]) ]) +if test "x$with_openssl" != "xno" && test "x$with_openssl" != "xyes" && test "x$have_openssl" != "xyes"; then + CPPFLAGS="$saved_CPPFLAGS" + LDFLAGS="$saved_LDFLAGS" +fi +if test "x$with_openssl" != xno && test "x$have_openssl" = xno; then + AC_MSG_WARN([cannot build SSL support -- openssl not found]) + with_openssl=no +fi +if test "x$with_openssl" != xno; then + AC_MSG_NOTICE([building with SSL support]) +else + AC_MSG_NOTICE([building without SSL support]) +fi +AM_CONDITIONAL([WANT_OPENSSL],[test "x$with_openssl" != xno]) + +if test "$CALLER" = "ANT" ; then +CPPUNIT_CFLAGS="$CPPUNIT_CFLAGS -DZKSERVER_CMD=\"\\\"${base_dir}/zookeeper-client/zookeeper-client-c/tests/zkServer.sh\\\"\"" +else +CPPUNIT_CFLAGS="$CPPUNIT_CFLAGS -DZKSERVER_CMD=\"\\\"./tests/zkServer.sh\\\"\"" +AC_CHECK_FILES([$srcdir/generated/zookeeper.jute.c $srcdir/generated/zookeeper.jute.h],[], + [AC_MSG_ERROR([jute files are missing! Please run "ant compile_jute" while in the zookeeper top level directory.]) +]) +fi +AC_SUBST(CPPUNIT_CFLAGS) + +AC_PROG_CC +AM_PROG_CC_C_O +AC_PROG_CXX +AC_PROG_INSTALL +AC_PROG_LN_S + +# AC_DISABLE_SHARED +AC_PROG_LIBTOOL + +#enable -D_GNU_SOURCE since the return code value of getaddrinfo +#ifdefed with __USE_GNU +#features.h header undef's __USE_GNU and defines it only if _GNU_SOURCE is defined +#hence this define for gcc +AC_ARG_ENABLE([debug], + [AS_HELP_STRING([--enable-debug],[enable debug build [default=no]])], + [],[enable_debug=no]) + +if test "x$enable_debug" = xyes; then + if test "x$init_cflags" = x; then + CFLAGS="" + fi + CFLAGS="$CFLAGS -g -O0 -D_GNU_SOURCE" +else + if test "x$init_cflags" = x; then + CFLAGS="-g -O2 -D_GNU_SOURCE" + fi +fi + +if test "x$enable_debug" = xyes; then + if test "x$init_cxxflags" = x; then + CXXFLAGS="" + fi + CXXFLAGS="$CXXFLAGS -g -O0" +else + if test "x$init_cxxflags" = x; then + CXXFLAGS="-g -O2" + fi +fi + +# Check whether to enable gcov (coverage test) +AC_ARG_ENABLE(gcov, [AS_HELP_STRING([--enable-gcov],[enable coverage test])]) +AC_MSG_CHECKING([whether to enable gcov]) +AS_IF([test "x${enable_gcov}" = "xyes"],AC_MSG_RESULT([yes]),AC_MSG_RESULT([no])) +AM_CONDITIONAL([ENABLEGCOV],[test "x${enable_gcov}" = "xyes"]) + + +CXXFLAGS="$CXXFLAGS -std=c++11" + +AC_ARG_WITH([syncapi], + [AS_HELP_STRING([--with-syncapi],[build with support for SyncAPI [default=yes]])], + [],[with_syncapi=yes]) + +# Checks for libraries. +AC_CHECK_LIB([pthread], [pthread_mutex_lock],[have_pthread=yes],[have_pthread=no]) + +if test "x$with_syncapi" != xno && test "x$have_pthread" = xno; then + AC_MSG_WARN([cannot build SyncAPI -- pthread not found]) + with_syncapi=no +fi +if test "x$with_syncapi" != xno; then + AC_MSG_NOTICE([building with SyncAPI support]) +else + AC_MSG_NOTICE([building without SyncAPI support]) +fi + +AM_CONDITIONAL([WANT_SYNCAPI],[test "x$with_syncapi" != xno]) + +# Checks for header files. +AC_HEADER_STDC +AC_CHECK_HEADERS([arpa/inet.h fcntl.h netdb.h netinet/in.h stdlib.h string.h sys/socket.h sys/time.h unistd.h sys/utsname.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_C_CONST +AC_C_INLINE +AC_HEADER_TIME +AC_CHECK_TYPE([nfds_t], + [AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[poll() second argument type])], + [AC_DEFINE([POLL_NFDS_TYPE],[unsigned int],[poll() second argument type])], + [#include ]) + +AC_MSG_CHECKING([whether to enable ipv6]) + +AC_TRY_RUN([ /* is AF_INET6 available? */ +#include +#include +main() +{ + if (socket(AF_INET6, SOCK_STREAM, 0) < 0) + exit(1); + else + exit(0); +} +], AC_MSG_RESULT(yes) + ipv6=yes, + AC_MSG_RESULT(no) + ipv6=no, + AC_MSG_RESULT(no) + ipv6=no) + +if test x"$ipv6" = xyes; then + USEIPV6="-DZOO_IPV6_ENABLED" + AC_SUBST(USEIPV6) +fi + +# use SOCK_CLOEXEC if available and wanted +AC_ARG_WITH([sock_cloexec], +[AS_HELP_STRING([--with-sock-cloexec],[build with SOCK_CLOEXEC flag set on the connections])], +[],[with_sock_cloexec=no]) + +AC_MSG_CHECKING([whether SOCK_CLOEXEC is available]) + +AC_TRY_RUN([ /* is SOCK_CLOEXEC available ? */ +#include +#include +#include +main() +{ +#ifdef SOCK_CLOEXEC + exit(0); +#else + exit(1); +#endif +} +], AC_MSG_RESULT(yes) + has_sock_cloexec=yes, + AC_MSG_RESULT(no) + has_sock_cloexec=no, + AC_MSG_RESULT(no) + has_sock_cloexec=no) + +if test "x$with_sock_cloexec" != xno && test "x$has_sock_cloexec" = xno; then + AC_MSG_WARN([cannot use SOCK_CLOEXEC -- SOCK_CLOEXEC undefined on this platform]) + with_sock_cloexec=no +fi + +if test "x$with_sock_cloexec" != xno; then +AC_MSG_NOTICE([building with SOCK_CLOEXEC]) +else +AC_MSG_NOTICE([building without SOCK_CLOEXEC]) +fi + +AS_IF([test x"$with_sock_cloexec" != xno], [AC_DEFINE([SOCK_CLOEXEC_ENABLED], [1], [Define to 1, if SOCK_CLOEXEC is defined and wanted])]) +AM_CONDITIONAL([SOCK_CLOEXEC_ENABLED],[test "x$with_sock_cloexec" != xno]) + +# Determine which libraries we need to use clock_gettime +saved_LIBS="$LIBS" +LIBS="" +AC_CHECK_LIB(rt, clock_gettime) +CLOCK_GETTIME_LIBS=$LIBS +AC_SUBST(CLOCK_GETTIME_LIBS) +LIBS="$saved_LIBS" + +# Checks for library functions. +AC_CHECK_FUNCS([getcwd gethostbyname gethostname getlogin getpwuid_r gettimeofday getuid memmove memset poll socket strchr strdup strerror strtol]) + +AC_CONFIG_FILES([Makefile]) +AC_CANONICAL_HOST +AM_CONDITIONAL([SOLARIS],[ + case "$host_os" in + *solaris*) + true + ;; + *) + false + ;; + esac ]) +AC_OUTPUT diff --git a/zookeeper-client/zookeeper-client-c/generated/zookeeper.jute.c b/zookeeper-client/zookeeper-client-c/generated/zookeeper.jute.c new file mode 100644 index 0000000..b8d0eae --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/generated/zookeeper.jute.c @@ -0,0 +1,1622 @@ +/** +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you 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. +*/ + +#include +#include "zookeeper.jute.h" + +int serialize_Id(struct oarchive *out, const char *tag, struct Id *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "scheme", &v->scheme); + rc = rc ? rc : out->serialize_String(out, "id", &v->id); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_Id(struct iarchive *in, const char *tag, struct Id*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "scheme", &v->scheme); + rc = rc ? rc : in->deserialize_String(in, "id", &v->id); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_Id(struct Id*v){ + deallocate_String(&v->scheme); + deallocate_String(&v->id); +} +int serialize_ACL(struct oarchive *out, const char *tag, struct ACL *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "perms", &v->perms); + rc = rc ? rc : serialize_Id(out, "id", &v->id); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ACL(struct iarchive *in, const char *tag, struct ACL*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "perms", &v->perms); + rc = rc ? rc : deserialize_Id(in, "id", &v->id); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ACL(struct ACL*v){ + deallocate_Id(&v->id); +} +int serialize_Stat(struct oarchive *out, const char *tag, struct Stat *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "czxid", &v->czxid); + rc = rc ? rc : out->serialize_Long(out, "mzxid", &v->mzxid); + rc = rc ? rc : out->serialize_Long(out, "ctime", &v->ctime); + rc = rc ? rc : out->serialize_Long(out, "mtime", &v->mtime); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->serialize_Int(out, "cversion", &v->cversion); + rc = rc ? rc : out->serialize_Int(out, "aversion", &v->aversion); + rc = rc ? rc : out->serialize_Long(out, "ephemeralOwner", &v->ephemeralOwner); + rc = rc ? rc : out->serialize_Int(out, "dataLength", &v->dataLength); + rc = rc ? rc : out->serialize_Int(out, "numChildren", &v->numChildren); + rc = rc ? rc : out->serialize_Long(out, "pzxid", &v->pzxid); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_Stat(struct iarchive *in, const char *tag, struct Stat*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "czxid", &v->czxid); + rc = rc ? rc : in->deserialize_Long(in, "mzxid", &v->mzxid); + rc = rc ? rc : in->deserialize_Long(in, "ctime", &v->ctime); + rc = rc ? rc : in->deserialize_Long(in, "mtime", &v->mtime); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->deserialize_Int(in, "cversion", &v->cversion); + rc = rc ? rc : in->deserialize_Int(in, "aversion", &v->aversion); + rc = rc ? rc : in->deserialize_Long(in, "ephemeralOwner", &v->ephemeralOwner); + rc = rc ? rc : in->deserialize_Int(in, "dataLength", &v->dataLength); + rc = rc ? rc : in->deserialize_Int(in, "numChildren", &v->numChildren); + rc = rc ? rc : in->deserialize_Long(in, "pzxid", &v->pzxid); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_Stat(struct Stat*v){ +} +int serialize_StatPersisted(struct oarchive *out, const char *tag, struct StatPersisted *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "czxid", &v->czxid); + rc = rc ? rc : out->serialize_Long(out, "mzxid", &v->mzxid); + rc = rc ? rc : out->serialize_Long(out, "ctime", &v->ctime); + rc = rc ? rc : out->serialize_Long(out, "mtime", &v->mtime); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->serialize_Int(out, "cversion", &v->cversion); + rc = rc ? rc : out->serialize_Int(out, "aversion", &v->aversion); + rc = rc ? rc : out->serialize_Long(out, "ephemeralOwner", &v->ephemeralOwner); + rc = rc ? rc : out->serialize_Long(out, "pzxid", &v->pzxid); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_StatPersisted(struct iarchive *in, const char *tag, struct StatPersisted*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "czxid", &v->czxid); + rc = rc ? rc : in->deserialize_Long(in, "mzxid", &v->mzxid); + rc = rc ? rc : in->deserialize_Long(in, "ctime", &v->ctime); + rc = rc ? rc : in->deserialize_Long(in, "mtime", &v->mtime); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->deserialize_Int(in, "cversion", &v->cversion); + rc = rc ? rc : in->deserialize_Int(in, "aversion", &v->aversion); + rc = rc ? rc : in->deserialize_Long(in, "ephemeralOwner", &v->ephemeralOwner); + rc = rc ? rc : in->deserialize_Long(in, "pzxid", &v->pzxid); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_StatPersisted(struct StatPersisted*v){ +} +int serialize_ConnectRequest(struct oarchive *out, const char *tag, struct ConnectRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "protocolVersion", &v->protocolVersion); + rc = rc ? rc : out->serialize_Long(out, "lastZxidSeen", &v->lastZxidSeen); + rc = rc ? rc : out->serialize_Int(out, "timeOut", &v->timeOut); + rc = rc ? rc : out->serialize_Long(out, "sessionId", &v->sessionId); + rc = rc ? rc : out->serialize_Buffer(out, "passwd", &v->passwd); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ConnectRequest(struct iarchive *in, const char *tag, struct ConnectRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "protocolVersion", &v->protocolVersion); + rc = rc ? rc : in->deserialize_Long(in, "lastZxidSeen", &v->lastZxidSeen); + rc = rc ? rc : in->deserialize_Int(in, "timeOut", &v->timeOut); + rc = rc ? rc : in->deserialize_Long(in, "sessionId", &v->sessionId); + rc = rc ? rc : in->deserialize_Buffer(in, "passwd", &v->passwd); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ConnectRequest(struct ConnectRequest*v){ + deallocate_Buffer(&v->passwd); +} +int serialize_ConnectResponse(struct oarchive *out, const char *tag, struct ConnectResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "protocolVersion", &v->protocolVersion); + rc = rc ? rc : out->serialize_Int(out, "timeOut", &v->timeOut); + rc = rc ? rc : out->serialize_Long(out, "sessionId", &v->sessionId); + rc = rc ? rc : out->serialize_Buffer(out, "passwd", &v->passwd); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ConnectResponse(struct iarchive *in, const char *tag, struct ConnectResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "protocolVersion", &v->protocolVersion); + rc = rc ? rc : in->deserialize_Int(in, "timeOut", &v->timeOut); + rc = rc ? rc : in->deserialize_Long(in, "sessionId", &v->sessionId); + rc = rc ? rc : in->deserialize_Buffer(in, "passwd", &v->passwd); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ConnectResponse(struct ConnectResponse*v){ + deallocate_Buffer(&v->passwd); +} +int allocate_String_vector(struct String_vector *v, int32_t len) { + if (!len) { + v->count = 0; + v->data = 0; + } else { + v->count = len; + v->data = calloc(sizeof(*v->data), len); + } + return 0; +} +int deallocate_String_vector(struct String_vector *v) { + if (v->data) { + int32_t i; + for(i=0;icount; i++) { + deallocate_String(&v->data[i]); + } + free(v->data); + v->data = 0; + } + return 0; +} +int serialize_String_vector(struct oarchive *out, const char *tag, struct String_vector *v) +{ + int32_t count = v->count; + int rc = 0; + int32_t i; + rc = out->start_vector(out, tag, &count); + for(i=0;icount;i++) { + rc = rc ? rc : out->serialize_String(out, "data", &v->data[i]); + } + rc = rc ? rc : out->end_vector(out, tag); + return rc; +} +int deserialize_String_vector(struct iarchive *in, const char *tag, struct String_vector *v) +{ + int rc = 0; + int32_t i; + rc = in->start_vector(in, tag, &v->count); + v->data = calloc(v->count, sizeof(*v->data)); + for(i=0;icount;i++) { + rc = rc ? rc : in->deserialize_String(in, "value", &v->data[i]); + } + rc = in->end_vector(in, tag); + return rc; +} +int serialize_SetWatches(struct oarchive *out, const char *tag, struct SetWatches *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "relativeZxid", &v->relativeZxid); + rc = rc ? rc : serialize_String_vector(out, "dataWatches", &v->dataWatches); + rc = rc ? rc : serialize_String_vector(out, "existWatches", &v->existWatches); + rc = rc ? rc : serialize_String_vector(out, "childWatches", &v->childWatches); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetWatches(struct iarchive *in, const char *tag, struct SetWatches*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "relativeZxid", &v->relativeZxid); + rc = rc ? rc : deserialize_String_vector(in, "dataWatches", &v->dataWatches); + rc = rc ? rc : deserialize_String_vector(in, "existWatches", &v->existWatches); + rc = rc ? rc : deserialize_String_vector(in, "childWatches", &v->childWatches); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetWatches(struct SetWatches*v){ + deallocate_String_vector(&v->dataWatches); + deallocate_String_vector(&v->existWatches); + deallocate_String_vector(&v->childWatches); +} +int serialize_SetWatches2(struct oarchive *out, const char *tag, struct SetWatches2 *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "relativeZxid", &v->relativeZxid); + rc = rc ? rc : serialize_String_vector(out, "dataWatches", &v->dataWatches); + rc = rc ? rc : serialize_String_vector(out, "existWatches", &v->existWatches); + rc = rc ? rc : serialize_String_vector(out, "childWatches", &v->childWatches); + rc = rc ? rc : serialize_String_vector(out, "persistentWatches", &v->persistentWatches); + rc = rc ? rc : serialize_String_vector(out, "persistentRecursiveWatches", &v->persistentRecursiveWatches); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetWatches2(struct iarchive *in, const char *tag, struct SetWatches2*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "relativeZxid", &v->relativeZxid); + rc = rc ? rc : deserialize_String_vector(in, "dataWatches", &v->dataWatches); + rc = rc ? rc : deserialize_String_vector(in, "existWatches", &v->existWatches); + rc = rc ? rc : deserialize_String_vector(in, "childWatches", &v->childWatches); + rc = rc ? rc : deserialize_String_vector(in, "persistentWatches", &v->persistentWatches); + rc = rc ? rc : deserialize_String_vector(in, "persistentRecursiveWatches", &v->persistentRecursiveWatches); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetWatches2(struct SetWatches2*v){ + deallocate_String_vector(&v->dataWatches); + deallocate_String_vector(&v->existWatches); + deallocate_String_vector(&v->childWatches); + deallocate_String_vector(&v->persistentWatches); + deallocate_String_vector(&v->persistentRecursiveWatches); +} +int serialize_RequestHeader(struct oarchive *out, const char *tag, struct RequestHeader *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "xid", &v->xid); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_RequestHeader(struct iarchive *in, const char *tag, struct RequestHeader*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "xid", &v->xid); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_RequestHeader(struct RequestHeader*v){ +} +int serialize_MultiHeader(struct oarchive *out, const char *tag, struct MultiHeader *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->serialize_Bool(out, "done", &v->done); + rc = rc ? rc : out->serialize_Int(out, "err", &v->err); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_MultiHeader(struct iarchive *in, const char *tag, struct MultiHeader*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->deserialize_Bool(in, "done", &v->done); + rc = rc ? rc : in->deserialize_Int(in, "err", &v->err); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_MultiHeader(struct MultiHeader*v){ +} +int serialize_AuthPacket(struct oarchive *out, const char *tag, struct AuthPacket *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->serialize_String(out, "scheme", &v->scheme); + rc = rc ? rc : out->serialize_Buffer(out, "auth", &v->auth); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_AuthPacket(struct iarchive *in, const char *tag, struct AuthPacket*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->deserialize_String(in, "scheme", &v->scheme); + rc = rc ? rc : in->deserialize_Buffer(in, "auth", &v->auth); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_AuthPacket(struct AuthPacket*v){ + deallocate_String(&v->scheme); + deallocate_Buffer(&v->auth); +} +int serialize_ReplyHeader(struct oarchive *out, const char *tag, struct ReplyHeader *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "xid", &v->xid); + rc = rc ? rc : out->serialize_Long(out, "zxid", &v->zxid); + rc = rc ? rc : out->serialize_Int(out, "err", &v->err); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ReplyHeader(struct iarchive *in, const char *tag, struct ReplyHeader*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "xid", &v->xid); + rc = rc ? rc : in->deserialize_Long(in, "zxid", &v->zxid); + rc = rc ? rc : in->deserialize_Int(in, "err", &v->err); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ReplyHeader(struct ReplyHeader*v){ +} +int serialize_GetDataRequest(struct oarchive *out, const char *tag, struct GetDataRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Bool(out, "watch", &v->watch); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetDataRequest(struct iarchive *in, const char *tag, struct GetDataRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Bool(in, "watch", &v->watch); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetDataRequest(struct GetDataRequest*v){ + deallocate_String(&v->path); +} +int serialize_SetDataRequest(struct oarchive *out, const char *tag, struct SetDataRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetDataRequest(struct iarchive *in, const char *tag, struct SetDataRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetDataRequest(struct SetDataRequest*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); +} +int serialize_ReconfigRequest(struct oarchive *out, const char *tag, struct ReconfigRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "joiningServers", &v->joiningServers); + rc = rc ? rc : out->serialize_String(out, "leavingServers", &v->leavingServers); + rc = rc ? rc : out->serialize_String(out, "newMembers", &v->newMembers); + rc = rc ? rc : out->serialize_Long(out, "curConfigId", &v->curConfigId); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ReconfigRequest(struct iarchive *in, const char *tag, struct ReconfigRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "joiningServers", &v->joiningServers); + rc = rc ? rc : in->deserialize_String(in, "leavingServers", &v->leavingServers); + rc = rc ? rc : in->deserialize_String(in, "newMembers", &v->newMembers); + rc = rc ? rc : in->deserialize_Long(in, "curConfigId", &v->curConfigId); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ReconfigRequest(struct ReconfigRequest*v){ + deallocate_String(&v->joiningServers); + deallocate_String(&v->leavingServers); + deallocate_String(&v->newMembers); +} +int serialize_SetDataResponse(struct oarchive *out, const char *tag, struct SetDataResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetDataResponse(struct iarchive *in, const char *tag, struct SetDataResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetDataResponse(struct SetDataResponse*v){ + deallocate_Stat(&v->stat); +} +int serialize_GetSASLRequest(struct oarchive *out, const char *tag, struct GetSASLRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Buffer(out, "token", &v->token); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetSASLRequest(struct iarchive *in, const char *tag, struct GetSASLRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Buffer(in, "token", &v->token); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetSASLRequest(struct GetSASLRequest*v){ + deallocate_Buffer(&v->token); +} +int serialize_SetSASLRequest(struct oarchive *out, const char *tag, struct SetSASLRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Buffer(out, "token", &v->token); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetSASLRequest(struct iarchive *in, const char *tag, struct SetSASLRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Buffer(in, "token", &v->token); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetSASLRequest(struct SetSASLRequest*v){ + deallocate_Buffer(&v->token); +} +int serialize_SetSASLResponse(struct oarchive *out, const char *tag, struct SetSASLResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Buffer(out, "token", &v->token); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetSASLResponse(struct iarchive *in, const char *tag, struct SetSASLResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Buffer(in, "token", &v->token); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetSASLResponse(struct SetSASLResponse*v){ + deallocate_Buffer(&v->token); +} +int allocate_ACL_vector(struct ACL_vector *v, int32_t len) { + if (!len) { + v->count = 0; + v->data = 0; + } else { + v->count = len; + v->data = calloc(sizeof(*v->data), len); + } + return 0; +} +int deallocate_ACL_vector(struct ACL_vector *v) { + if (v->data) { + int32_t i; + for(i=0;icount; i++) { + deallocate_ACL(&v->data[i]); + } + free(v->data); + v->data = 0; + } + return 0; +} +int serialize_ACL_vector(struct oarchive *out, const char *tag, struct ACL_vector *v) +{ + int32_t count = v->count; + int rc = 0; + int32_t i; + rc = out->start_vector(out, tag, &count); + for(i=0;icount;i++) { + rc = rc ? rc : serialize_ACL(out, "data", &v->data[i]); + } + rc = rc ? rc : out->end_vector(out, tag); + return rc; +} +int deserialize_ACL_vector(struct iarchive *in, const char *tag, struct ACL_vector *v) +{ + int rc = 0; + int32_t i; + rc = in->start_vector(in, tag, &v->count); + v->data = calloc(v->count, sizeof(*v->data)); + for(i=0;icount;i++) { + rc = rc ? rc : deserialize_ACL(in, "value", &v->data[i]); + } + rc = in->end_vector(in, tag); + return rc; +} +int serialize_CreateRequest(struct oarchive *out, const char *tag, struct CreateRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Int(out, "flags", &v->flags); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateRequest(struct iarchive *in, const char *tag, struct CreateRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Int(in, "flags", &v->flags); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateRequest(struct CreateRequest*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); + deallocate_ACL_vector(&v->acl); +} +int serialize_CreateTTLRequest(struct oarchive *out, const char *tag, struct CreateTTLRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Int(out, "flags", &v->flags); + rc = rc ? rc : out->serialize_Long(out, "ttl", &v->ttl); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateTTLRequest(struct iarchive *in, const char *tag, struct CreateTTLRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Int(in, "flags", &v->flags); + rc = rc ? rc : in->deserialize_Long(in, "ttl", &v->ttl); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateTTLRequest(struct CreateTTLRequest*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); + deallocate_ACL_vector(&v->acl); +} +int serialize_DeleteRequest(struct oarchive *out, const char *tag, struct DeleteRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_DeleteRequest(struct iarchive *in, const char *tag, struct DeleteRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_DeleteRequest(struct DeleteRequest*v){ + deallocate_String(&v->path); +} +int serialize_GetChildrenRequest(struct oarchive *out, const char *tag, struct GetChildrenRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Bool(out, "watch", &v->watch); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetChildrenRequest(struct iarchive *in, const char *tag, struct GetChildrenRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Bool(in, "watch", &v->watch); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetChildrenRequest(struct GetChildrenRequest*v){ + deallocate_String(&v->path); +} +int serialize_GetAllChildrenNumberRequest(struct oarchive *out, const char *tag, struct GetAllChildrenNumberRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetAllChildrenNumberRequest(struct iarchive *in, const char *tag, struct GetAllChildrenNumberRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetAllChildrenNumberRequest(struct GetAllChildrenNumberRequest*v){ + deallocate_String(&v->path); +} +int serialize_GetChildren2Request(struct oarchive *out, const char *tag, struct GetChildren2Request *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Bool(out, "watch", &v->watch); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetChildren2Request(struct iarchive *in, const char *tag, struct GetChildren2Request*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Bool(in, "watch", &v->watch); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetChildren2Request(struct GetChildren2Request*v){ + deallocate_String(&v->path); +} +int serialize_CheckVersionRequest(struct oarchive *out, const char *tag, struct CheckVersionRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CheckVersionRequest(struct iarchive *in, const char *tag, struct CheckVersionRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CheckVersionRequest(struct CheckVersionRequest*v){ + deallocate_String(&v->path); +} +int serialize_GetMaxChildrenRequest(struct oarchive *out, const char *tag, struct GetMaxChildrenRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetMaxChildrenRequest(struct iarchive *in, const char *tag, struct GetMaxChildrenRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetMaxChildrenRequest(struct GetMaxChildrenRequest*v){ + deallocate_String(&v->path); +} +int serialize_GetMaxChildrenResponse(struct oarchive *out, const char *tag, struct GetMaxChildrenResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "max", &v->max); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetMaxChildrenResponse(struct iarchive *in, const char *tag, struct GetMaxChildrenResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "max", &v->max); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetMaxChildrenResponse(struct GetMaxChildrenResponse*v){ +} +int serialize_SetMaxChildrenRequest(struct oarchive *out, const char *tag, struct SetMaxChildrenRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "max", &v->max); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetMaxChildrenRequest(struct iarchive *in, const char *tag, struct SetMaxChildrenRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "max", &v->max); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetMaxChildrenRequest(struct SetMaxChildrenRequest*v){ + deallocate_String(&v->path); +} +int serialize_SyncRequest(struct oarchive *out, const char *tag, struct SyncRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SyncRequest(struct iarchive *in, const char *tag, struct SyncRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SyncRequest(struct SyncRequest*v){ + deallocate_String(&v->path); +} +int serialize_SyncResponse(struct oarchive *out, const char *tag, struct SyncResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SyncResponse(struct iarchive *in, const char *tag, struct SyncResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SyncResponse(struct SyncResponse*v){ + deallocate_String(&v->path); +} +int serialize_GetACLRequest(struct oarchive *out, const char *tag, struct GetACLRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetACLRequest(struct iarchive *in, const char *tag, struct GetACLRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetACLRequest(struct GetACLRequest*v){ + deallocate_String(&v->path); +} +int serialize_SetACLRequest(struct oarchive *out, const char *tag, struct SetACLRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetACLRequest(struct iarchive *in, const char *tag, struct SetACLRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetACLRequest(struct SetACLRequest*v){ + deallocate_String(&v->path); + deallocate_ACL_vector(&v->acl); +} +int serialize_SetACLResponse(struct oarchive *out, const char *tag, struct SetACLResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetACLResponse(struct iarchive *in, const char *tag, struct SetACLResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetACLResponse(struct SetACLResponse*v){ + deallocate_Stat(&v->stat); +} +int serialize_AddWatchRequest(struct oarchive *out, const char *tag, struct AddWatchRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "mode", &v->mode); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_AddWatchRequest(struct iarchive *in, const char *tag, struct AddWatchRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "mode", &v->mode); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_AddWatchRequest(struct AddWatchRequest*v){ + deallocate_String(&v->path); +} +int serialize_WatcherEvent(struct oarchive *out, const char *tag, struct WatcherEvent *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->serialize_Int(out, "state", &v->state); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_WatcherEvent(struct iarchive *in, const char *tag, struct WatcherEvent*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->deserialize_Int(in, "state", &v->state); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_WatcherEvent(struct WatcherEvent*v){ + deallocate_String(&v->path); +} +int serialize_ErrorResponse(struct oarchive *out, const char *tag, struct ErrorResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "err", &v->err); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ErrorResponse(struct iarchive *in, const char *tag, struct ErrorResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "err", &v->err); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ErrorResponse(struct ErrorResponse*v){ +} +int serialize_CreateResponse(struct oarchive *out, const char *tag, struct CreateResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateResponse(struct iarchive *in, const char *tag, struct CreateResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateResponse(struct CreateResponse*v){ + deallocate_String(&v->path); +} +int serialize_Create2Response(struct oarchive *out, const char *tag, struct Create2Response *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_Create2Response(struct iarchive *in, const char *tag, struct Create2Response*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_Create2Response(struct Create2Response*v){ + deallocate_String(&v->path); + deallocate_Stat(&v->stat); +} +int serialize_ExistsRequest(struct oarchive *out, const char *tag, struct ExistsRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Bool(out, "watch", &v->watch); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ExistsRequest(struct iarchive *in, const char *tag, struct ExistsRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Bool(in, "watch", &v->watch); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ExistsRequest(struct ExistsRequest*v){ + deallocate_String(&v->path); +} +int serialize_ExistsResponse(struct oarchive *out, const char *tag, struct ExistsResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ExistsResponse(struct iarchive *in, const char *tag, struct ExistsResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ExistsResponse(struct ExistsResponse*v){ + deallocate_Stat(&v->stat); +} +int serialize_GetDataResponse(struct oarchive *out, const char *tag, struct GetDataResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetDataResponse(struct iarchive *in, const char *tag, struct GetDataResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetDataResponse(struct GetDataResponse*v){ + deallocate_Buffer(&v->data); + deallocate_Stat(&v->stat); +} +int serialize_GetChildrenResponse(struct oarchive *out, const char *tag, struct GetChildrenResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_String_vector(out, "children", &v->children); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetChildrenResponse(struct iarchive *in, const char *tag, struct GetChildrenResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_String_vector(in, "children", &v->children); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetChildrenResponse(struct GetChildrenResponse*v){ + deallocate_String_vector(&v->children); +} +int serialize_GetAllChildrenNumberResponse(struct oarchive *out, const char *tag, struct GetAllChildrenNumberResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "totalNumber", &v->totalNumber); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetAllChildrenNumberResponse(struct iarchive *in, const char *tag, struct GetAllChildrenNumberResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "totalNumber", &v->totalNumber); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetAllChildrenNumberResponse(struct GetAllChildrenNumberResponse*v){ +} +int serialize_GetChildren2Response(struct oarchive *out, const char *tag, struct GetChildren2Response *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_String_vector(out, "children", &v->children); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetChildren2Response(struct iarchive *in, const char *tag, struct GetChildren2Response*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_String_vector(in, "children", &v->children); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetChildren2Response(struct GetChildren2Response*v){ + deallocate_String_vector(&v->children); + deallocate_Stat(&v->stat); +} +int serialize_GetACLResponse(struct oarchive *out, const char *tag, struct GetACLResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : serialize_Stat(out, "stat", &v->stat); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetACLResponse(struct iarchive *in, const char *tag, struct GetACLResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : deserialize_Stat(in, "stat", &v->stat); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetACLResponse(struct GetACLResponse*v){ + deallocate_ACL_vector(&v->acl); + deallocate_Stat(&v->stat); +} +int serialize_CheckWatchesRequest(struct oarchive *out, const char *tag, struct CheckWatchesRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CheckWatchesRequest(struct iarchive *in, const char *tag, struct CheckWatchesRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CheckWatchesRequest(struct CheckWatchesRequest*v){ + deallocate_String(&v->path); +} +int serialize_RemoveWatchesRequest(struct oarchive *out, const char *tag, struct RemoveWatchesRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_RemoveWatchesRequest(struct iarchive *in, const char *tag, struct RemoveWatchesRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_RemoveWatchesRequest(struct RemoveWatchesRequest*v){ + deallocate_String(&v->path); +} +int serialize_GetEphemeralsRequest(struct oarchive *out, const char *tag, struct GetEphemeralsRequest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "prefixPath", &v->prefixPath); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetEphemeralsRequest(struct iarchive *in, const char *tag, struct GetEphemeralsRequest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "prefixPath", &v->prefixPath); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetEphemeralsRequest(struct GetEphemeralsRequest*v){ + deallocate_String(&v->prefixPath); +} +int serialize_GetEphemeralsResponse(struct oarchive *out, const char *tag, struct GetEphemeralsResponse *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_String_vector(out, "ephemerals", &v->ephemerals); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_GetEphemeralsResponse(struct iarchive *in, const char *tag, struct GetEphemeralsResponse*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_String_vector(in, "ephemerals", &v->ephemerals); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_GetEphemeralsResponse(struct GetEphemeralsResponse*v){ + deallocate_String_vector(&v->ephemerals); +} +int serialize_LearnerInfo(struct oarchive *out, const char *tag, struct LearnerInfo *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "serverid", &v->serverid); + rc = rc ? rc : out->serialize_Int(out, "protocolVersion", &v->protocolVersion); + rc = rc ? rc : out->serialize_Long(out, "configVersion", &v->configVersion); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_LearnerInfo(struct iarchive *in, const char *tag, struct LearnerInfo*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "serverid", &v->serverid); + rc = rc ? rc : in->deserialize_Int(in, "protocolVersion", &v->protocolVersion); + rc = rc ? rc : in->deserialize_Long(in, "configVersion", &v->configVersion); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_LearnerInfo(struct LearnerInfo*v){ +} +int allocate_Id_vector(struct Id_vector *v, int32_t len) { + if (!len) { + v->count = 0; + v->data = 0; + } else { + v->count = len; + v->data = calloc(sizeof(*v->data), len); + } + return 0; +} +int deallocate_Id_vector(struct Id_vector *v) { + if (v->data) { + int32_t i; + for(i=0;icount; i++) { + deallocate_Id(&v->data[i]); + } + free(v->data); + v->data = 0; + } + return 0; +} +int serialize_Id_vector(struct oarchive *out, const char *tag, struct Id_vector *v) +{ + int32_t count = v->count; + int rc = 0; + int32_t i; + rc = out->start_vector(out, tag, &count); + for(i=0;icount;i++) { + rc = rc ? rc : serialize_Id(out, "data", &v->data[i]); + } + rc = rc ? rc : out->end_vector(out, tag); + return rc; +} +int deserialize_Id_vector(struct iarchive *in, const char *tag, struct Id_vector *v) +{ + int rc = 0; + int32_t i; + rc = in->start_vector(in, tag, &v->count); + v->data = calloc(v->count, sizeof(*v->data)); + for(i=0;icount;i++) { + rc = rc ? rc : deserialize_Id(in, "value", &v->data[i]); + } + rc = in->end_vector(in, tag); + return rc; +} +int serialize_QuorumPacket(struct oarchive *out, const char *tag, struct QuorumPacket *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->serialize_Long(out, "zxid", &v->zxid); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_Id_vector(out, "authinfo", &v->authinfo); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_QuorumPacket(struct iarchive *in, const char *tag, struct QuorumPacket*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->deserialize_Long(in, "zxid", &v->zxid); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_Id_vector(in, "authinfo", &v->authinfo); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_QuorumPacket(struct QuorumPacket*v){ + deallocate_Buffer(&v->data); + deallocate_Id_vector(&v->authinfo); +} +int serialize_QuorumAuthPacket(struct oarchive *out, const char *tag, struct QuorumAuthPacket *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "magic", &v->magic); + rc = rc ? rc : out->serialize_Int(out, "status", &v->status); + rc = rc ? rc : out->serialize_Buffer(out, "token", &v->token); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_QuorumAuthPacket(struct iarchive *in, const char *tag, struct QuorumAuthPacket*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "magic", &v->magic); + rc = rc ? rc : in->deserialize_Int(in, "status", &v->status); + rc = rc ? rc : in->deserialize_Buffer(in, "token", &v->token); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_QuorumAuthPacket(struct QuorumAuthPacket*v){ + deallocate_Buffer(&v->token); +} +int serialize_FileHeader(struct oarchive *out, const char *tag, struct FileHeader *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "magic", &v->magic); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->serialize_Long(out, "dbid", &v->dbid); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_FileHeader(struct iarchive *in, const char *tag, struct FileHeader*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "magic", &v->magic); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->deserialize_Long(in, "dbid", &v->dbid); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_FileHeader(struct FileHeader*v){ +} +int serialize_TxnDigest(struct oarchive *out, const char *tag, struct TxnDigest *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->serialize_Long(out, "treeDigest", &v->treeDigest); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_TxnDigest(struct iarchive *in, const char *tag, struct TxnDigest*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->deserialize_Long(in, "treeDigest", &v->treeDigest); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_TxnDigest(struct TxnDigest*v){ +} +int serialize_TxnHeader(struct oarchive *out, const char *tag, struct TxnHeader *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Long(out, "clientId", &v->clientId); + rc = rc ? rc : out->serialize_Int(out, "cxid", &v->cxid); + rc = rc ? rc : out->serialize_Long(out, "zxid", &v->zxid); + rc = rc ? rc : out->serialize_Long(out, "time", &v->time); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_TxnHeader(struct iarchive *in, const char *tag, struct TxnHeader*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Long(in, "clientId", &v->clientId); + rc = rc ? rc : in->deserialize_Int(in, "cxid", &v->cxid); + rc = rc ? rc : in->deserialize_Long(in, "zxid", &v->zxid); + rc = rc ? rc : in->deserialize_Long(in, "time", &v->time); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_TxnHeader(struct TxnHeader*v){ +} +int serialize_CreateTxnV0(struct oarchive *out, const char *tag, struct CreateTxnV0 *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Bool(out, "ephemeral", &v->ephemeral); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateTxnV0(struct iarchive *in, const char *tag, struct CreateTxnV0*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Bool(in, "ephemeral", &v->ephemeral); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateTxnV0(struct CreateTxnV0*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); + deallocate_ACL_vector(&v->acl); +} +int serialize_CreateTxn(struct oarchive *out, const char *tag, struct CreateTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Bool(out, "ephemeral", &v->ephemeral); + rc = rc ? rc : out->serialize_Int(out, "parentCVersion", &v->parentCVersion); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateTxn(struct iarchive *in, const char *tag, struct CreateTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Bool(in, "ephemeral", &v->ephemeral); + rc = rc ? rc : in->deserialize_Int(in, "parentCVersion", &v->parentCVersion); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateTxn(struct CreateTxn*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); + deallocate_ACL_vector(&v->acl); +} +int serialize_CreateTTLTxn(struct oarchive *out, const char *tag, struct CreateTTLTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Int(out, "parentCVersion", &v->parentCVersion); + rc = rc ? rc : out->serialize_Long(out, "ttl", &v->ttl); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateTTLTxn(struct iarchive *in, const char *tag, struct CreateTTLTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Int(in, "parentCVersion", &v->parentCVersion); + rc = rc ? rc : in->deserialize_Long(in, "ttl", &v->ttl); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateTTLTxn(struct CreateTTLTxn*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); + deallocate_ACL_vector(&v->acl); +} +int serialize_CreateContainerTxn(struct oarchive *out, const char *tag, struct CreateContainerTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Int(out, "parentCVersion", &v->parentCVersion); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateContainerTxn(struct iarchive *in, const char *tag, struct CreateContainerTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Int(in, "parentCVersion", &v->parentCVersion); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateContainerTxn(struct CreateContainerTxn*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); + deallocate_ACL_vector(&v->acl); +} +int serialize_DeleteTxn(struct oarchive *out, const char *tag, struct DeleteTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_DeleteTxn(struct iarchive *in, const char *tag, struct DeleteTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_DeleteTxn(struct DeleteTxn*v){ + deallocate_String(&v->path); +} +int serialize_SetDataTxn(struct oarchive *out, const char *tag, struct SetDataTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetDataTxn(struct iarchive *in, const char *tag, struct SetDataTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetDataTxn(struct SetDataTxn*v){ + deallocate_String(&v->path); + deallocate_Buffer(&v->data); +} +int serialize_CheckVersionTxn(struct oarchive *out, const char *tag, struct CheckVersionTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CheckVersionTxn(struct iarchive *in, const char *tag, struct CheckVersionTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CheckVersionTxn(struct CheckVersionTxn*v){ + deallocate_String(&v->path); +} +int serialize_SetACLTxn(struct oarchive *out, const char *tag, struct SetACLTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : serialize_ACL_vector(out, "acl", &v->acl); + rc = rc ? rc : out->serialize_Int(out, "version", &v->version); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetACLTxn(struct iarchive *in, const char *tag, struct SetACLTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : deserialize_ACL_vector(in, "acl", &v->acl); + rc = rc ? rc : in->deserialize_Int(in, "version", &v->version); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetACLTxn(struct SetACLTxn*v){ + deallocate_String(&v->path); + deallocate_ACL_vector(&v->acl); +} +int serialize_SetMaxChildrenTxn(struct oarchive *out, const char *tag, struct SetMaxChildrenTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_String(out, "path", &v->path); + rc = rc ? rc : out->serialize_Int(out, "max", &v->max); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_SetMaxChildrenTxn(struct iarchive *in, const char *tag, struct SetMaxChildrenTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_String(in, "path", &v->path); + rc = rc ? rc : in->deserialize_Int(in, "max", &v->max); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_SetMaxChildrenTxn(struct SetMaxChildrenTxn*v){ + deallocate_String(&v->path); +} +int serialize_CreateSessionTxn(struct oarchive *out, const char *tag, struct CreateSessionTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "timeOut", &v->timeOut); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CreateSessionTxn(struct iarchive *in, const char *tag, struct CreateSessionTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "timeOut", &v->timeOut); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CreateSessionTxn(struct CreateSessionTxn*v){ +} +int serialize_CloseSessionTxn(struct oarchive *out, const char *tag, struct CloseSessionTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_String_vector(out, "paths2Delete", &v->paths2Delete); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_CloseSessionTxn(struct iarchive *in, const char *tag, struct CloseSessionTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_String_vector(in, "paths2Delete", &v->paths2Delete); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_CloseSessionTxn(struct CloseSessionTxn*v){ + deallocate_String_vector(&v->paths2Delete); +} +int serialize_ErrorTxn(struct oarchive *out, const char *tag, struct ErrorTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "err", &v->err); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_ErrorTxn(struct iarchive *in, const char *tag, struct ErrorTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "err", &v->err); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_ErrorTxn(struct ErrorTxn*v){ +} +int serialize_Txn(struct oarchive *out, const char *tag, struct Txn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : out->serialize_Int(out, "type", &v->type); + rc = rc ? rc : out->serialize_Buffer(out, "data", &v->data); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_Txn(struct iarchive *in, const char *tag, struct Txn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : in->deserialize_Int(in, "type", &v->type); + rc = rc ? rc : in->deserialize_Buffer(in, "data", &v->data); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_Txn(struct Txn*v){ + deallocate_Buffer(&v->data); +} +int allocate_Txn_vector(struct Txn_vector *v, int32_t len) { + if (!len) { + v->count = 0; + v->data = 0; + } else { + v->count = len; + v->data = calloc(sizeof(*v->data), len); + } + return 0; +} +int deallocate_Txn_vector(struct Txn_vector *v) { + if (v->data) { + int32_t i; + for(i=0;icount; i++) { + deallocate_Txn(&v->data[i]); + } + free(v->data); + v->data = 0; + } + return 0; +} +int serialize_Txn_vector(struct oarchive *out, const char *tag, struct Txn_vector *v) +{ + int32_t count = v->count; + int rc = 0; + int32_t i; + rc = out->start_vector(out, tag, &count); + for(i=0;icount;i++) { + rc = rc ? rc : serialize_Txn(out, "data", &v->data[i]); + } + rc = rc ? rc : out->end_vector(out, tag); + return rc; +} +int deserialize_Txn_vector(struct iarchive *in, const char *tag, struct Txn_vector *v) +{ + int rc = 0; + int32_t i; + rc = in->start_vector(in, tag, &v->count); + v->data = calloc(v->count, sizeof(*v->data)); + for(i=0;icount;i++) { + rc = rc ? rc : deserialize_Txn(in, "value", &v->data[i]); + } + rc = in->end_vector(in, tag); + return rc; +} +int serialize_MultiTxn(struct oarchive *out, const char *tag, struct MultiTxn *v){ + int rc; + rc = out->start_record(out, tag); + rc = rc ? rc : serialize_Txn_vector(out, "txns", &v->txns); + rc = rc ? rc : out->end_record(out, tag); + return rc; +} +int deserialize_MultiTxn(struct iarchive *in, const char *tag, struct MultiTxn*v){ + int rc; + rc = in->start_record(in, tag); + rc = rc ? rc : deserialize_Txn_vector(in, "txns", &v->txns); + rc = rc ? rc : in->end_record(in, tag); + return rc; +} +void deallocate_MultiTxn(struct MultiTxn*v){ + deallocate_Txn_vector(&v->txns); +} diff --git a/zookeeper-client/zookeeper-client-c/generated/zookeeper.jute.h b/zookeeper-client/zookeeper-client-c/generated/zookeeper.jute.h new file mode 100644 index 0000000..b02063a --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/generated/zookeeper.jute.h @@ -0,0 +1,595 @@ +/** +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you 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. +*/ + +#ifndef __ZOOKEEPER_JUTE__ +#define __ZOOKEEPER_JUTE__ +#include "recordio.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct Id { + char * scheme; + char * id; +}; +int serialize_Id(struct oarchive *out, const char *tag, struct Id *v); +int deserialize_Id(struct iarchive *in, const char *tag, struct Id*v); +void deallocate_Id(struct Id*); +struct ACL { + int32_t perms; + struct Id id; +}; +int serialize_ACL(struct oarchive *out, const char *tag, struct ACL *v); +int deserialize_ACL(struct iarchive *in, const char *tag, struct ACL*v); +void deallocate_ACL(struct ACL*); +struct Stat { + int64_t czxid; + int64_t mzxid; + int64_t ctime; + int64_t mtime; + int32_t version; + int32_t cversion; + int32_t aversion; + int64_t ephemeralOwner; + int32_t dataLength; + int32_t numChildren; + int64_t pzxid; +}; +int serialize_Stat(struct oarchive *out, const char *tag, struct Stat *v); +int deserialize_Stat(struct iarchive *in, const char *tag, struct Stat*v); +void deallocate_Stat(struct Stat*); +struct StatPersisted { + int64_t czxid; + int64_t mzxid; + int64_t ctime; + int64_t mtime; + int32_t version; + int32_t cversion; + int32_t aversion; + int64_t ephemeralOwner; + int64_t pzxid; +}; +int serialize_StatPersisted(struct oarchive *out, const char *tag, struct StatPersisted *v); +int deserialize_StatPersisted(struct iarchive *in, const char *tag, struct StatPersisted*v); +void deallocate_StatPersisted(struct StatPersisted*); +struct ConnectRequest { + int32_t protocolVersion; + int64_t lastZxidSeen; + int32_t timeOut; + int64_t sessionId; + struct buffer passwd; +}; +int serialize_ConnectRequest(struct oarchive *out, const char *tag, struct ConnectRequest *v); +int deserialize_ConnectRequest(struct iarchive *in, const char *tag, struct ConnectRequest*v); +void deallocate_ConnectRequest(struct ConnectRequest*); +struct ConnectResponse { + int32_t protocolVersion; + int32_t timeOut; + int64_t sessionId; + struct buffer passwd; +}; +int serialize_ConnectResponse(struct oarchive *out, const char *tag, struct ConnectResponse *v); +int deserialize_ConnectResponse(struct iarchive *in, const char *tag, struct ConnectResponse*v); +void deallocate_ConnectResponse(struct ConnectResponse*); +struct String_vector { + int32_t count; + char * *data; + +}; +int serialize_String_vector(struct oarchive *out, const char *tag, struct String_vector *v); +int deserialize_String_vector(struct iarchive *in, const char *tag, struct String_vector *v); +int allocate_String_vector(struct String_vector *v, int32_t len); +int deallocate_String_vector(struct String_vector *v); +struct SetWatches { + int64_t relativeZxid; + struct String_vector dataWatches; + struct String_vector existWatches; + struct String_vector childWatches; +}; +int serialize_SetWatches(struct oarchive *out, const char *tag, struct SetWatches *v); +int deserialize_SetWatches(struct iarchive *in, const char *tag, struct SetWatches*v); +void deallocate_SetWatches(struct SetWatches*); +struct SetWatches2 { + int64_t relativeZxid; + struct String_vector dataWatches; + struct String_vector existWatches; + struct String_vector childWatches; + struct String_vector persistentWatches; + struct String_vector persistentRecursiveWatches; +}; +int serialize_SetWatches2(struct oarchive *out, const char *tag, struct SetWatches2 *v); +int deserialize_SetWatches2(struct iarchive *in, const char *tag, struct SetWatches2*v); +void deallocate_SetWatches2(struct SetWatches2*); +struct RequestHeader { + int32_t xid; + int32_t type; +}; +int serialize_RequestHeader(struct oarchive *out, const char *tag, struct RequestHeader *v); +int deserialize_RequestHeader(struct iarchive *in, const char *tag, struct RequestHeader*v); +void deallocate_RequestHeader(struct RequestHeader*); +struct MultiHeader { + int32_t type; + int32_t done; + int32_t err; +}; +int serialize_MultiHeader(struct oarchive *out, const char *tag, struct MultiHeader *v); +int deserialize_MultiHeader(struct iarchive *in, const char *tag, struct MultiHeader*v); +void deallocate_MultiHeader(struct MultiHeader*); +struct AuthPacket { + int32_t type; + char * scheme; + struct buffer auth; +}; +int serialize_AuthPacket(struct oarchive *out, const char *tag, struct AuthPacket *v); +int deserialize_AuthPacket(struct iarchive *in, const char *tag, struct AuthPacket*v); +void deallocate_AuthPacket(struct AuthPacket*); +struct ReplyHeader { + int32_t xid; + int64_t zxid; + int32_t err; +}; +int serialize_ReplyHeader(struct oarchive *out, const char *tag, struct ReplyHeader *v); +int deserialize_ReplyHeader(struct iarchive *in, const char *tag, struct ReplyHeader*v); +void deallocate_ReplyHeader(struct ReplyHeader*); +struct GetDataRequest { + char * path; + int32_t watch; +}; +int serialize_GetDataRequest(struct oarchive *out, const char *tag, struct GetDataRequest *v); +int deserialize_GetDataRequest(struct iarchive *in, const char *tag, struct GetDataRequest*v); +void deallocate_GetDataRequest(struct GetDataRequest*); +struct SetDataRequest { + char * path; + struct buffer data; + int32_t version; +}; +int serialize_SetDataRequest(struct oarchive *out, const char *tag, struct SetDataRequest *v); +int deserialize_SetDataRequest(struct iarchive *in, const char *tag, struct SetDataRequest*v); +void deallocate_SetDataRequest(struct SetDataRequest*); +struct ReconfigRequest { + char * joiningServers; + char * leavingServers; + char * newMembers; + int64_t curConfigId; +}; +int serialize_ReconfigRequest(struct oarchive *out, const char *tag, struct ReconfigRequest *v); +int deserialize_ReconfigRequest(struct iarchive *in, const char *tag, struct ReconfigRequest*v); +void deallocate_ReconfigRequest(struct ReconfigRequest*); +struct SetDataResponse { + struct Stat stat; +}; +int serialize_SetDataResponse(struct oarchive *out, const char *tag, struct SetDataResponse *v); +int deserialize_SetDataResponse(struct iarchive *in, const char *tag, struct SetDataResponse*v); +void deallocate_SetDataResponse(struct SetDataResponse*); +struct GetSASLRequest { + struct buffer token; +}; +int serialize_GetSASLRequest(struct oarchive *out, const char *tag, struct GetSASLRequest *v); +int deserialize_GetSASLRequest(struct iarchive *in, const char *tag, struct GetSASLRequest*v); +void deallocate_GetSASLRequest(struct GetSASLRequest*); +struct SetSASLRequest { + struct buffer token; +}; +int serialize_SetSASLRequest(struct oarchive *out, const char *tag, struct SetSASLRequest *v); +int deserialize_SetSASLRequest(struct iarchive *in, const char *tag, struct SetSASLRequest*v); +void deallocate_SetSASLRequest(struct SetSASLRequest*); +struct SetSASLResponse { + struct buffer token; +}; +int serialize_SetSASLResponse(struct oarchive *out, const char *tag, struct SetSASLResponse *v); +int deserialize_SetSASLResponse(struct iarchive *in, const char *tag, struct SetSASLResponse*v); +void deallocate_SetSASLResponse(struct SetSASLResponse*); +struct ACL_vector { + int32_t count; + struct ACL *data; + +}; +int serialize_ACL_vector(struct oarchive *out, const char *tag, struct ACL_vector *v); +int deserialize_ACL_vector(struct iarchive *in, const char *tag, struct ACL_vector *v); +int allocate_ACL_vector(struct ACL_vector *v, int32_t len); +int deallocate_ACL_vector(struct ACL_vector *v); +struct CreateRequest { + char * path; + struct buffer data; + struct ACL_vector acl; + int32_t flags; +}; +int serialize_CreateRequest(struct oarchive *out, const char *tag, struct CreateRequest *v); +int deserialize_CreateRequest(struct iarchive *in, const char *tag, struct CreateRequest*v); +void deallocate_CreateRequest(struct CreateRequest*); +struct CreateTTLRequest { + char * path; + struct buffer data; + struct ACL_vector acl; + int32_t flags; + int64_t ttl; +}; +int serialize_CreateTTLRequest(struct oarchive *out, const char *tag, struct CreateTTLRequest *v); +int deserialize_CreateTTLRequest(struct iarchive *in, const char *tag, struct CreateTTLRequest*v); +void deallocate_CreateTTLRequest(struct CreateTTLRequest*); +struct DeleteRequest { + char * path; + int32_t version; +}; +int serialize_DeleteRequest(struct oarchive *out, const char *tag, struct DeleteRequest *v); +int deserialize_DeleteRequest(struct iarchive *in, const char *tag, struct DeleteRequest*v); +void deallocate_DeleteRequest(struct DeleteRequest*); +struct GetChildrenRequest { + char * path; + int32_t watch; +}; +int serialize_GetChildrenRequest(struct oarchive *out, const char *tag, struct GetChildrenRequest *v); +int deserialize_GetChildrenRequest(struct iarchive *in, const char *tag, struct GetChildrenRequest*v); +void deallocate_GetChildrenRequest(struct GetChildrenRequest*); +struct GetAllChildrenNumberRequest { + char * path; +}; +int serialize_GetAllChildrenNumberRequest(struct oarchive *out, const char *tag, struct GetAllChildrenNumberRequest *v); +int deserialize_GetAllChildrenNumberRequest(struct iarchive *in, const char *tag, struct GetAllChildrenNumberRequest*v); +void deallocate_GetAllChildrenNumberRequest(struct GetAllChildrenNumberRequest*); +struct GetChildren2Request { + char * path; + int32_t watch; +}; +int serialize_GetChildren2Request(struct oarchive *out, const char *tag, struct GetChildren2Request *v); +int deserialize_GetChildren2Request(struct iarchive *in, const char *tag, struct GetChildren2Request*v); +void deallocate_GetChildren2Request(struct GetChildren2Request*); +struct CheckVersionRequest { + char * path; + int32_t version; +}; +int serialize_CheckVersionRequest(struct oarchive *out, const char *tag, struct CheckVersionRequest *v); +int deserialize_CheckVersionRequest(struct iarchive *in, const char *tag, struct CheckVersionRequest*v); +void deallocate_CheckVersionRequest(struct CheckVersionRequest*); +struct GetMaxChildrenRequest { + char * path; +}; +int serialize_GetMaxChildrenRequest(struct oarchive *out, const char *tag, struct GetMaxChildrenRequest *v); +int deserialize_GetMaxChildrenRequest(struct iarchive *in, const char *tag, struct GetMaxChildrenRequest*v); +void deallocate_GetMaxChildrenRequest(struct GetMaxChildrenRequest*); +struct GetMaxChildrenResponse { + int32_t max; +}; +int serialize_GetMaxChildrenResponse(struct oarchive *out, const char *tag, struct GetMaxChildrenResponse *v); +int deserialize_GetMaxChildrenResponse(struct iarchive *in, const char *tag, struct GetMaxChildrenResponse*v); +void deallocate_GetMaxChildrenResponse(struct GetMaxChildrenResponse*); +struct SetMaxChildrenRequest { + char * path; + int32_t max; +}; +int serialize_SetMaxChildrenRequest(struct oarchive *out, const char *tag, struct SetMaxChildrenRequest *v); +int deserialize_SetMaxChildrenRequest(struct iarchive *in, const char *tag, struct SetMaxChildrenRequest*v); +void deallocate_SetMaxChildrenRequest(struct SetMaxChildrenRequest*); +struct SyncRequest { + char * path; +}; +int serialize_SyncRequest(struct oarchive *out, const char *tag, struct SyncRequest *v); +int deserialize_SyncRequest(struct iarchive *in, const char *tag, struct SyncRequest*v); +void deallocate_SyncRequest(struct SyncRequest*); +struct SyncResponse { + char * path; +}; +int serialize_SyncResponse(struct oarchive *out, const char *tag, struct SyncResponse *v); +int deserialize_SyncResponse(struct iarchive *in, const char *tag, struct SyncResponse*v); +void deallocate_SyncResponse(struct SyncResponse*); +struct GetACLRequest { + char * path; +}; +int serialize_GetACLRequest(struct oarchive *out, const char *tag, struct GetACLRequest *v); +int deserialize_GetACLRequest(struct iarchive *in, const char *tag, struct GetACLRequest*v); +void deallocate_GetACLRequest(struct GetACLRequest*); +struct SetACLRequest { + char * path; + struct ACL_vector acl; + int32_t version; +}; +int serialize_SetACLRequest(struct oarchive *out, const char *tag, struct SetACLRequest *v); +int deserialize_SetACLRequest(struct iarchive *in, const char *tag, struct SetACLRequest*v); +void deallocate_SetACLRequest(struct SetACLRequest*); +struct SetACLResponse { + struct Stat stat; +}; +int serialize_SetACLResponse(struct oarchive *out, const char *tag, struct SetACLResponse *v); +int deserialize_SetACLResponse(struct iarchive *in, const char *tag, struct SetACLResponse*v); +void deallocate_SetACLResponse(struct SetACLResponse*); +struct AddWatchRequest { + char * path; + int32_t mode; +}; +int serialize_AddWatchRequest(struct oarchive *out, const char *tag, struct AddWatchRequest *v); +int deserialize_AddWatchRequest(struct iarchive *in, const char *tag, struct AddWatchRequest*v); +void deallocate_AddWatchRequest(struct AddWatchRequest*); +struct WatcherEvent { + int32_t type; + int32_t state; + char * path; +}; +int serialize_WatcherEvent(struct oarchive *out, const char *tag, struct WatcherEvent *v); +int deserialize_WatcherEvent(struct iarchive *in, const char *tag, struct WatcherEvent*v); +void deallocate_WatcherEvent(struct WatcherEvent*); +struct ErrorResponse { + int32_t err; +}; +int serialize_ErrorResponse(struct oarchive *out, const char *tag, struct ErrorResponse *v); +int deserialize_ErrorResponse(struct iarchive *in, const char *tag, struct ErrorResponse*v); +void deallocate_ErrorResponse(struct ErrorResponse*); +struct CreateResponse { + char * path; +}; +int serialize_CreateResponse(struct oarchive *out, const char *tag, struct CreateResponse *v); +int deserialize_CreateResponse(struct iarchive *in, const char *tag, struct CreateResponse*v); +void deallocate_CreateResponse(struct CreateResponse*); +struct Create2Response { + char * path; + struct Stat stat; +}; +int serialize_Create2Response(struct oarchive *out, const char *tag, struct Create2Response *v); +int deserialize_Create2Response(struct iarchive *in, const char *tag, struct Create2Response*v); +void deallocate_Create2Response(struct Create2Response*); +struct ExistsRequest { + char * path; + int32_t watch; +}; +int serialize_ExistsRequest(struct oarchive *out, const char *tag, struct ExistsRequest *v); +int deserialize_ExistsRequest(struct iarchive *in, const char *tag, struct ExistsRequest*v); +void deallocate_ExistsRequest(struct ExistsRequest*); +struct ExistsResponse { + struct Stat stat; +}; +int serialize_ExistsResponse(struct oarchive *out, const char *tag, struct ExistsResponse *v); +int deserialize_ExistsResponse(struct iarchive *in, const char *tag, struct ExistsResponse*v); +void deallocate_ExistsResponse(struct ExistsResponse*); +struct GetDataResponse { + struct buffer data; + struct Stat stat; +}; +int serialize_GetDataResponse(struct oarchive *out, const char *tag, struct GetDataResponse *v); +int deserialize_GetDataResponse(struct iarchive *in, const char *tag, struct GetDataResponse*v); +void deallocate_GetDataResponse(struct GetDataResponse*); +struct GetChildrenResponse { + struct String_vector children; +}; +int serialize_GetChildrenResponse(struct oarchive *out, const char *tag, struct GetChildrenResponse *v); +int deserialize_GetChildrenResponse(struct iarchive *in, const char *tag, struct GetChildrenResponse*v); +void deallocate_GetChildrenResponse(struct GetChildrenResponse*); +struct GetAllChildrenNumberResponse { + int32_t totalNumber; +}; +int serialize_GetAllChildrenNumberResponse(struct oarchive *out, const char *tag, struct GetAllChildrenNumberResponse *v); +int deserialize_GetAllChildrenNumberResponse(struct iarchive *in, const char *tag, struct GetAllChildrenNumberResponse*v); +void deallocate_GetAllChildrenNumberResponse(struct GetAllChildrenNumberResponse*); +struct GetChildren2Response { + struct String_vector children; + struct Stat stat; +}; +int serialize_GetChildren2Response(struct oarchive *out, const char *tag, struct GetChildren2Response *v); +int deserialize_GetChildren2Response(struct iarchive *in, const char *tag, struct GetChildren2Response*v); +void deallocate_GetChildren2Response(struct GetChildren2Response*); +struct GetACLResponse { + struct ACL_vector acl; + struct Stat stat; +}; +int serialize_GetACLResponse(struct oarchive *out, const char *tag, struct GetACLResponse *v); +int deserialize_GetACLResponse(struct iarchive *in, const char *tag, struct GetACLResponse*v); +void deallocate_GetACLResponse(struct GetACLResponse*); +struct CheckWatchesRequest { + char * path; + int32_t type; +}; +int serialize_CheckWatchesRequest(struct oarchive *out, const char *tag, struct CheckWatchesRequest *v); +int deserialize_CheckWatchesRequest(struct iarchive *in, const char *tag, struct CheckWatchesRequest*v); +void deallocate_CheckWatchesRequest(struct CheckWatchesRequest*); +struct RemoveWatchesRequest { + char * path; + int32_t type; +}; +int serialize_RemoveWatchesRequest(struct oarchive *out, const char *tag, struct RemoveWatchesRequest *v); +int deserialize_RemoveWatchesRequest(struct iarchive *in, const char *tag, struct RemoveWatchesRequest*v); +void deallocate_RemoveWatchesRequest(struct RemoveWatchesRequest*); +struct GetEphemeralsRequest { + char * prefixPath; +}; +int serialize_GetEphemeralsRequest(struct oarchive *out, const char *tag, struct GetEphemeralsRequest *v); +int deserialize_GetEphemeralsRequest(struct iarchive *in, const char *tag, struct GetEphemeralsRequest*v); +void deallocate_GetEphemeralsRequest(struct GetEphemeralsRequest*); +struct GetEphemeralsResponse { + struct String_vector ephemerals; +}; +int serialize_GetEphemeralsResponse(struct oarchive *out, const char *tag, struct GetEphemeralsResponse *v); +int deserialize_GetEphemeralsResponse(struct iarchive *in, const char *tag, struct GetEphemeralsResponse*v); +void deallocate_GetEphemeralsResponse(struct GetEphemeralsResponse*); +struct LearnerInfo { + int64_t serverid; + int32_t protocolVersion; + int64_t configVersion; +}; +int serialize_LearnerInfo(struct oarchive *out, const char *tag, struct LearnerInfo *v); +int deserialize_LearnerInfo(struct iarchive *in, const char *tag, struct LearnerInfo*v); +void deallocate_LearnerInfo(struct LearnerInfo*); +struct Id_vector { + int32_t count; + struct Id *data; + +}; +int serialize_Id_vector(struct oarchive *out, const char *tag, struct Id_vector *v); +int deserialize_Id_vector(struct iarchive *in, const char *tag, struct Id_vector *v); +int allocate_Id_vector(struct Id_vector *v, int32_t len); +int deallocate_Id_vector(struct Id_vector *v); +struct QuorumPacket { + int32_t type; + int64_t zxid; + struct buffer data; + struct Id_vector authinfo; +}; +int serialize_QuorumPacket(struct oarchive *out, const char *tag, struct QuorumPacket *v); +int deserialize_QuorumPacket(struct iarchive *in, const char *tag, struct QuorumPacket*v); +void deallocate_QuorumPacket(struct QuorumPacket*); +struct QuorumAuthPacket { + int64_t magic; + int32_t status; + struct buffer token; +}; +int serialize_QuorumAuthPacket(struct oarchive *out, const char *tag, struct QuorumAuthPacket *v); +int deserialize_QuorumAuthPacket(struct iarchive *in, const char *tag, struct QuorumAuthPacket*v); +void deallocate_QuorumAuthPacket(struct QuorumAuthPacket*); +struct FileHeader { + int32_t magic; + int32_t version; + int64_t dbid; +}; +int serialize_FileHeader(struct oarchive *out, const char *tag, struct FileHeader *v); +int deserialize_FileHeader(struct iarchive *in, const char *tag, struct FileHeader*v); +void deallocate_FileHeader(struct FileHeader*); +struct TxnDigest { + int32_t version; + int64_t treeDigest; +}; +int serialize_TxnDigest(struct oarchive *out, const char *tag, struct TxnDigest *v); +int deserialize_TxnDigest(struct iarchive *in, const char *tag, struct TxnDigest*v); +void deallocate_TxnDigest(struct TxnDigest*); +struct TxnHeader { + int64_t clientId; + int32_t cxid; + int64_t zxid; + int64_t time; + int32_t type; +}; +int serialize_TxnHeader(struct oarchive *out, const char *tag, struct TxnHeader *v); +int deserialize_TxnHeader(struct iarchive *in, const char *tag, struct TxnHeader*v); +void deallocate_TxnHeader(struct TxnHeader*); +struct CreateTxnV0 { + char * path; + struct buffer data; + struct ACL_vector acl; + int32_t ephemeral; +}; +int serialize_CreateTxnV0(struct oarchive *out, const char *tag, struct CreateTxnV0 *v); +int deserialize_CreateTxnV0(struct iarchive *in, const char *tag, struct CreateTxnV0*v); +void deallocate_CreateTxnV0(struct CreateTxnV0*); +struct CreateTxn { + char * path; + struct buffer data; + struct ACL_vector acl; + int32_t ephemeral; + int32_t parentCVersion; +}; +int serialize_CreateTxn(struct oarchive *out, const char *tag, struct CreateTxn *v); +int deserialize_CreateTxn(struct iarchive *in, const char *tag, struct CreateTxn*v); +void deallocate_CreateTxn(struct CreateTxn*); +struct CreateTTLTxn { + char * path; + struct buffer data; + struct ACL_vector acl; + int32_t parentCVersion; + int64_t ttl; +}; +int serialize_CreateTTLTxn(struct oarchive *out, const char *tag, struct CreateTTLTxn *v); +int deserialize_CreateTTLTxn(struct iarchive *in, const char *tag, struct CreateTTLTxn*v); +void deallocate_CreateTTLTxn(struct CreateTTLTxn*); +struct CreateContainerTxn { + char * path; + struct buffer data; + struct ACL_vector acl; + int32_t parentCVersion; +}; +int serialize_CreateContainerTxn(struct oarchive *out, const char *tag, struct CreateContainerTxn *v); +int deserialize_CreateContainerTxn(struct iarchive *in, const char *tag, struct CreateContainerTxn*v); +void deallocate_CreateContainerTxn(struct CreateContainerTxn*); +struct DeleteTxn { + char * path; +}; +int serialize_DeleteTxn(struct oarchive *out, const char *tag, struct DeleteTxn *v); +int deserialize_DeleteTxn(struct iarchive *in, const char *tag, struct DeleteTxn*v); +void deallocate_DeleteTxn(struct DeleteTxn*); +struct SetDataTxn { + char * path; + struct buffer data; + int32_t version; +}; +int serialize_SetDataTxn(struct oarchive *out, const char *tag, struct SetDataTxn *v); +int deserialize_SetDataTxn(struct iarchive *in, const char *tag, struct SetDataTxn*v); +void deallocate_SetDataTxn(struct SetDataTxn*); +struct CheckVersionTxn { + char * path; + int32_t version; +}; +int serialize_CheckVersionTxn(struct oarchive *out, const char *tag, struct CheckVersionTxn *v); +int deserialize_CheckVersionTxn(struct iarchive *in, const char *tag, struct CheckVersionTxn*v); +void deallocate_CheckVersionTxn(struct CheckVersionTxn*); +struct SetACLTxn { + char * path; + struct ACL_vector acl; + int32_t version; +}; +int serialize_SetACLTxn(struct oarchive *out, const char *tag, struct SetACLTxn *v); +int deserialize_SetACLTxn(struct iarchive *in, const char *tag, struct SetACLTxn*v); +void deallocate_SetACLTxn(struct SetACLTxn*); +struct SetMaxChildrenTxn { + char * path; + int32_t max; +}; +int serialize_SetMaxChildrenTxn(struct oarchive *out, const char *tag, struct SetMaxChildrenTxn *v); +int deserialize_SetMaxChildrenTxn(struct iarchive *in, const char *tag, struct SetMaxChildrenTxn*v); +void deallocate_SetMaxChildrenTxn(struct SetMaxChildrenTxn*); +struct CreateSessionTxn { + int32_t timeOut; +}; +int serialize_CreateSessionTxn(struct oarchive *out, const char *tag, struct CreateSessionTxn *v); +int deserialize_CreateSessionTxn(struct iarchive *in, const char *tag, struct CreateSessionTxn*v); +void deallocate_CreateSessionTxn(struct CreateSessionTxn*); +struct CloseSessionTxn { + struct String_vector paths2Delete; +}; +int serialize_CloseSessionTxn(struct oarchive *out, const char *tag, struct CloseSessionTxn *v); +int deserialize_CloseSessionTxn(struct iarchive *in, const char *tag, struct CloseSessionTxn*v); +void deallocate_CloseSessionTxn(struct CloseSessionTxn*); +struct ErrorTxn { + int32_t err; +}; +int serialize_ErrorTxn(struct oarchive *out, const char *tag, struct ErrorTxn *v); +int deserialize_ErrorTxn(struct iarchive *in, const char *tag, struct ErrorTxn*v); +void deallocate_ErrorTxn(struct ErrorTxn*); +struct Txn { + int32_t type; + struct buffer data; +}; +int serialize_Txn(struct oarchive *out, const char *tag, struct Txn *v); +int deserialize_Txn(struct iarchive *in, const char *tag, struct Txn*v); +void deallocate_Txn(struct Txn*); +struct Txn_vector { + int32_t count; + struct Txn *data; + +}; +int serialize_Txn_vector(struct oarchive *out, const char *tag, struct Txn_vector *v); +int deserialize_Txn_vector(struct iarchive *in, const char *tag, struct Txn_vector *v); +int allocate_Txn_vector(struct Txn_vector *v, int32_t len); +int deallocate_Txn_vector(struct Txn_vector *v); +struct MultiTxn { + struct Txn_vector txns; +}; +int serialize_MultiTxn(struct oarchive *out, const char *tag, struct MultiTxn *v); +int deserialize_MultiTxn(struct iarchive *in, const char *tag, struct MultiTxn*v); +void deallocate_MultiTxn(struct MultiTxn*); + +#ifdef __cplusplus +} +#endif + +#endif //ZOOKEEPER_JUTE__ diff --git a/zookeeper-client/zookeeper-client-c/include/proto.h b/zookeeper-client/zookeeper-client-c/include/proto.h new file mode 100644 index 0000000..88774ff --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/proto.h @@ -0,0 +1,54 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#ifndef PROTO_H_ +#define PROTO_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZOO_NOTIFY_OP 0 +#define ZOO_CREATE_OP 1 +#define ZOO_DELETE_OP 2 +#define ZOO_EXISTS_OP 3 +#define ZOO_GETDATA_OP 4 +#define ZOO_SETDATA_OP 5 +#define ZOO_GETACL_OP 6 +#define ZOO_SETACL_OP 7 +#define ZOO_GETCHILDREN_OP 8 +#define ZOO_SYNC_OP 9 +#define ZOO_PING_OP 11 +#define ZOO_GETCHILDREN2_OP 12 +#define ZOO_CHECK_OP 13 +#define ZOO_MULTI_OP 14 +#define ZOO_CREATE2_OP 15 +#define ZOO_RECONFIG_OP 16 +#define ZOO_CHECK_WATCHES 17 +#define ZOO_REMOVE_WATCHES 18 +#define ZOO_CREATE_CONTAINER_OP 19 +#define ZOO_DELETE_CONTAINER_OP 20 +#define ZOO_CREATE_TTL_OP 21 +#define ZOO_CLOSE_OP -11 +#define ZOO_SETAUTH_OP 100 +#define ZOO_SETWATCHES_OP 101 + +#ifdef __cplusplus +} +#endif + +#endif /*PROTO_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/include/recordio.h b/zookeeper-client/zookeeper-client-c/include/recordio.h new file mode 100644 index 0000000..876ad32 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/recordio.h @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#ifndef __RECORDIO_H__ +#define __RECORDIO_H__ + +#include +#include /* for int64_t */ +#ifdef WIN32 +#include "winconfig.h" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +struct buffer { + int32_t len; + char *buff; +}; + +void deallocate_String(char **s); +void deallocate_Buffer(struct buffer *b); +void deallocate_vector(void *d); +struct iarchive { + int (*start_record)(struct iarchive *ia, const char *tag); + int (*end_record)(struct iarchive *ia, const char *tag); + int (*start_vector)(struct iarchive *ia, const char *tag, int32_t *count); + int (*end_vector)(struct iarchive *ia, const char *tag); + int (*deserialize_Bool)(struct iarchive *ia, const char *name, int32_t *); + int (*deserialize_Int)(struct iarchive *ia, const char *name, int32_t *); + int (*deserialize_Long)(struct iarchive *ia, const char *name, int64_t *); + int (*deserialize_Buffer)(struct iarchive *ia, const char *name, + struct buffer *); + int (*deserialize_String)(struct iarchive *ia, const char *name, char **); + void *priv; +}; +struct oarchive { + int (*start_record)(struct oarchive *oa, const char *tag); + int (*end_record)(struct oarchive *oa, const char *tag); + int (*start_vector)(struct oarchive *oa, const char *tag, const int32_t *count); + int (*end_vector)(struct oarchive *oa, const char *tag); + int (*serialize_Bool)(struct oarchive *oa, const char *name, const int32_t *); + int (*serialize_Int)(struct oarchive *oa, const char *name, const int32_t *); + int (*serialize_Long)(struct oarchive *oa, const char *name, + const int64_t *); + int (*serialize_Buffer)(struct oarchive *oa, const char *name, + const struct buffer *); + int (*serialize_String)(struct oarchive *oa, const char *name, char **); + void *priv; +}; + +struct oarchive *create_buffer_oarchive(void); +void close_buffer_oarchive(struct oarchive **oa, int free_buffer); +struct iarchive *create_buffer_iarchive(char *buffer, int len); +void close_buffer_iarchive(struct iarchive **ia); +char *get_buffer(struct oarchive *); +int get_buffer_len(struct oarchive *); + +int64_t zoo_htonll(int64_t v); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/zookeeper-client/zookeeper-client-c/include/win_getopt.h b/zookeeper-client/zookeeper-client-c/include/win_getopt.h new file mode 100644 index 0000000..c50c7a4 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/win_getopt.h @@ -0,0 +1,674 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/** + * DISCLAIMER + * This file is part of the mingw-w64 runtime package. + * + * The mingw-w64 runtime package and its code is distributed in the hope that it + * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR + * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to + * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + + /* + * Copyright (c) 2002 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GETOPT_H__ + +#pragma warning(disable:4996); + +#define __GETOPT_H__ + +/* All the headers include this file. */ +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ + +#ifdef REPLACE_GETOPT +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +#undef optreset /* see getopt.h */ +#define optreset __mingw_optreset +int optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ +#endif + +//extern int optind; /* index of first non-option in argv */ +//extern int optopt; /* single option character, as parsed */ +//extern int opterr; /* flag to enable built-in diagnostics... */ +// /* (user may set to zero, to suppress) */ +// +//extern char *optarg; /* pointer to argument of current option */ + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#ifndef __CYGWIN__ +#define __progname __argv[0] +#else +extern char __declspec(dllimport) *__progname; +#endif + +#ifdef __CYGWIN__ +static char EMSG[] = ""; +#else +#define EMSG "" +#endif + +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int); +static int gcd(int, int); +static void permute_args(int, int, int, char * const *); + +static char *place = EMSG; /* option letter processing */ + +/* XXX: set optreset to 1 rather than these two */ +static int nonopt_start = -1; /* first non option argument (for permute) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; + +static void +_vwarnx(const char *fmt,va_list ap) +{ + (void)fprintf(stderr,"%s: ",__progname); + if (fmt != NULL) + (void)vfprintf(stderr,fmt,ap); + (void)fprintf(stderr,"\n"); +} + +static void +warnx(const char *fmt,...) +{ + va_list ap; + va_start(ap,fmt); + _vwarnx(fmt,ap); + va_end(ap); +} + +/* + * Compute the greatest common divisor of a and b. + */ +static int +gcd(int a, int b) +{ + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void +permute_args(int panonopt_start, int panonopt_end, int opt_end, + char * const *nargv) +{ + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char *swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end+i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char **) nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char **)nargv)[cstart] = swap; + } + } +} + +#ifdef REPLACE_GETOPT +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int +getopt(int nargc, char * const *nargv, const char *options) +{ + + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} +#endif /* REPLACE_GETOPT */ + +//extern int getopt(int nargc, char * const *nargv, const char *options); + +#ifdef _BSD_SOURCE +/* + * BSD adds the non-standard `optreset' feature, for reinitialisation + * of `getopt' parsing. We support this feature, for applications which + * proclaim their BSD heritage, before including this header; however, + * to maintain portability, developers are advised to avoid it. + */ +# define optreset __mingw_optreset +extern int optreset; +#endif +#ifdef __cplusplus +} +#endif +/* + * POSIX requires the `getopt' API to be specified in `unistd.h'; + * thus, `unistd.h' includes this header. However, we do not want + * to expose the `getopt_long' or `getopt_long_only' APIs, when + * included in this manner. Thus, close the standard __GETOPT_H__ + * declarations block, and open an additional __GETOPT_LONG_H__ + * specific block, only when *not* __UNISTD_H_SOURCED__, in which + * to declare the extended API. + */ +#endif /* !defined(__GETOPT_H__) */ + +#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) +#define __GETOPT_LONG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct option /* specification for a long form option... */ +{ + const char *name; /* option name, without leading hyphens */ + int has_arg; /* does it take an argument? */ + int *flag; /* where to save its status, or NULL */ + int val; /* its associated status value */ +}; + +enum /* permitted values for its `has_arg' field... */ +{ + no_argument = 0, /* option never takes an argument */ + required_argument, /* option always requires an argument */ + optional_argument /* option may take an argument */ +}; + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too) +{ + char *current_argv, *has_equal; + size_t current_argv_len; + int i, ambiguous, match; + +#define IDENTICAL_INTERPRETATION(_x, _y) \ + (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ + long_options[(_x)].flag == long_options[(_y)].flag && \ + long_options[(_x)].val == long_options[(_y)].val) + + current_argv = place; + match = -1; + ambiguous = 0; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + ambiguous = 0; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* partial match */ + match = i; + else if (!IDENTICAL_INTERPRETATION(i, match)) + ambiguous = 1; + } + if (ambiguous) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, + current_argv); + optopt = 0; + return (BADCH); + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +#undef IDENTICAL_INTERPRETATION +} + +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) +{ + char *oli; /* option letter list index */ + int optchar, short_too; + static int posixly_correct = -1; + + if (options == NULL) + return (-1); + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + * + * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or + * optreset != 0 for GNU compatibility. + */ + if (posixly_correct == -1 || optreset != 0) + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + if (*options == '+' || *options == '-') + options++; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || + (place[1] == '\0' && strchr(options, '-') == NULL)) { + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + nonopt_start = optind - + (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || + (optchar == (int)'-' && *place != '\0') || + (oli = (char*)strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; + if (PRINT_ERROR) + warnx(illoptchar, optchar); + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, + idx, 0); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); +} + +//extern int getopt_long(int nargc, char * const *nargv, const char *options, +// const struct option *long_options, int *idx); +//extern int getopt_long_only(int nargc, char * const *nargv, const char *options, +// const struct option *long_options, int *idx); +/* + * Previous MinGW implementation had... + */ +#ifndef HAVE_DECL_GETOPT +/* + * ...for the long form API only; keep this for compatibility. + */ +# define HAVE_DECL_GETOPT 1 +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */ diff --git a/zookeeper-client/zookeeper-client-c/include/winconfig.h b/zookeeper-client/zookeeper-client-c/include/winconfig.h new file mode 100644 index 0000000..c273a93 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/winconfig.h @@ -0,0 +1,15 @@ +#ifndef WINCONFIG_H_ +#define WINCONFIG_H_ + +/* Define to `__inline__' or `__inline' if that's what the C compiler + calls it, or to nothing if 'inline' is not supported under any name. */ +#ifndef __cplusplus +#define inline __inline +#endif + +#define __attribute__(x) +#define __func__ __FUNCTION__ + +#define ACL ZKACL /* Conflict with windows API */ + +#endif diff --git a/zookeeper-client/zookeeper-client-c/include/zookeeper.h b/zookeeper-client/zookeeper-client-c/include/zookeeper.h new file mode 100644 index 0000000..3a6a689 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/zookeeper.h @@ -0,0 +1,2336 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ZOOKEEPER_H_ +#define ZOOKEEPER_H_ + +#include + +/* we must not include config.h as a public header */ +#ifndef WIN32 +#include +#include +#endif + +#ifdef WIN32 +#include /* must always be included before ws2tcpip.h */ +#include /* for struct sock_addr and socklen_t */ +#endif + +#ifdef HAVE_OPENSSL_H +#include +#endif + +#include +#include + +#include "proto.h" +#include "zookeeper_version.h" +#include "recordio.h" +#include "zookeeper.jute.h" + +/** + * \file zookeeper.h + * \brief ZooKeeper functions and definitions. + * + * ZooKeeper is a network service that may be backed by a cluster of + * synchronized servers. The data in the service is represented as a tree + * of data nodes. Each node has data, children, an ACL, and status information. + * The data for a node is read and write in its entirety. + * + * ZooKeeper clients can leave watches when they queries the data or children + * of a node. If a watch is left, that client will be notified of the change. + * The notification is a one time trigger. Subsequent chances to the node will + * not trigger a notification unless the client issues a query with the watch + * flag set. If the client is ever disconnected from the service, the watches do + * not need to be reset. The client automatically resets the watches. + * + * When a node is created, it may be flagged as an ephemeral node. Ephemeral + * nodes are automatically removed when a client session is closed or when + * a session times out due to inactivity (the ZooKeeper runtime fills in + * periods of inactivity with pings). Ephemeral nodes cannot have children. + * + * ZooKeeper clients are identified by a server assigned session id. For + * security reasons The server + * also generates a corresponding password for a session. A client may save its + * id and corresponding password to persistent storage in order to use the + * session across program invocation boundaries. + */ + +/* Support for building on various platforms */ + +// on cygwin we should take care of exporting/importing symbols properly +#ifdef DLL_EXPORT +# define ZOOAPI __declspec(dllexport) +#else +# if (defined(__CYGWIN__) || defined(WIN32)) && !defined(USE_STATIC_LIB) +# define ZOOAPI __declspec(dllimport) +# else +# define ZOOAPI +# endif +#endif + +/** zookeeper return constants **/ + +enum ZOO_ERRORS { + ZOK = 0, /*!< Everything is OK */ + + /** System and server-side errors. + * This is never thrown by the server, it shouldn't be used other than + * to indicate a range. Specifically error codes greater than this + * value, but lesser than {@link #ZAPIERROR}, are system errors. */ + ZSYSTEMERROR = -1, + ZRUNTIMEINCONSISTENCY = -2, /*!< A runtime inconsistency was found */ + ZDATAINCONSISTENCY = -3, /*!< A data inconsistency was found */ + ZCONNECTIONLOSS = -4, /*!< Connection to the server has been lost */ + ZMARSHALLINGERROR = -5, /*!< Error while marshalling or unmarshalling data */ + ZUNIMPLEMENTED = -6, /*!< Operation is unimplemented */ + ZOPERATIONTIMEOUT = -7, /*!< Operation timeout */ + ZBADARGUMENTS = -8, /*!< Invalid arguments */ + ZINVALIDSTATE = -9, /*!< Invliad zhandle state */ + ZNEWCONFIGNOQUORUM = -13, /*!< No quorum of new config is connected and + up-to-date with the leader of last commmitted + config - try invoking reconfiguration after new + servers are connected and synced */ + ZRECONFIGINPROGRESS = -14, /*!< Reconfiguration requested while another + reconfiguration is currently in progress. This + is currently not supported. Please retry. */ + ZSSLCONNECTIONERROR = -15, /*!< The SSL connection Error */ + + /** API errors. + * This is never thrown by the server, it shouldn't be used other than + * to indicate a range. Specifically error codes greater than this + * value are API errors (while values less than this indicate a + * {@link #ZSYSTEMERROR}). + */ + ZAPIERROR = -100, + ZNONODE = -101, /*!< Node does not exist */ + ZNOAUTH = -102, /*!< Not authenticated */ + ZBADVERSION = -103, /*!< Version conflict */ + ZNOCHILDRENFOREPHEMERALS = -108, /*!< Ephemeral nodes may not have children */ + ZNODEEXISTS = -110, /*!< The node already exists */ + ZNOTEMPTY = -111, /*!< The node has children */ + ZSESSIONEXPIRED = -112, /*!< The session has been expired by the server */ + ZINVALIDCALLBACK = -113, /*!< Invalid callback specified */ + ZINVALIDACL = -114, /*!< Invalid ACL specified */ + ZAUTHFAILED = -115, /*!< Client authentication failed */ + ZCLOSING = -116, /*!< ZooKeeper is closing */ + ZNOTHING = -117, /*!< (not error) no server responses to process */ + ZSESSIONMOVED = -118, /*! + * The legacy style, an application wishing to receive events from ZooKeeper must + * first implement a function with this signature and pass a pointer to the function + * to \ref zookeeper_init. Next, the application sets a watch by calling one of + * the getter API that accept the watch integer flag (for example, \ref zoo_aexists, + * \ref zoo_get, etc). + *

+ * The watcher object style uses an instance of a "watcher object" which in + * the C world is represented by a pair: a pointer to a function implementing this + * signature and a pointer to watcher context -- handback user-specific data. + * When a watch is triggered this function will be called along with + * the watcher context. An application wishing to use this style must use + * the getter API functions with the "w" prefix in their names (for example, \ref + * zoo_awexists, \ref zoo_wget, etc). + * + * \param zh zookeeper handle + * \param type event type. This is one of the *_EVENT constants. + * \param state connection state. The state value will be one of the *_STATE constants. + * \param path znode path for which the watcher is triggered. NULL if the event + * type is ZOO_SESSION_EVENT + * \param watcherCtx watcher context. + */ +typedef void (*watcher_fn)(zhandle_t *zh, int type, + int state, const char *path,void *watcherCtx); + +/** + * \brief typedef for setting the log callback. It's a function pointer which + * returns void and accepts a const char* as its only argument. + * + * \param message message to be passed to the callback function. + */ +typedef void (*log_callback_fn)(const char *message); + +/** + * \brief create a handle to used communicate with zookeeper. + * + * This method creates a new handle and a zookeeper session that corresponds + * to that handle. Session establishment is asynchronous, meaning that the + * session should not be considered established until (and unless) an + * event of state ZOO_CONNECTED_STATE is received. + * \param host comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * \param fn the global watcher callback function. When notifications are + * triggered this function will be invoked. + * \param clientid the id of a previously established session that this + * client will be reconnecting to. Pass 0 if not reconnecting to a previous + * session. Clients can access the session id of an established, valid, + * connection by calling \ref zoo_client_id. If the session corresponding to + * the specified clientid has expired, or if the clientid is invalid for + * any reason, the returned zhandle_t will be invalid -- the zhandle_t + * state will indicate the reason for failure (typically + * ZOO_EXPIRED_SESSION_STATE). + * \param context the handback object that will be associated with this instance + * of zhandle_t. Application can access it (for example, in the watcher + * callback) using \ref zoo_get_context. The object is not used by zookeeper + * internally and can be null. + * \param flags reserved for future use. Should be set to zero. + * \return a pointer to the opaque zhandle structure. If it fails to create + * a new zhandle the function returns NULL and the errno variable + * indicates the reason. + */ +ZOOAPI zhandle_t *zookeeper_init(const char *host, watcher_fn fn, + int recv_timeout, const clientid_t *clientid, void *context, int flags); + +#ifdef HAVE_OPENSSL_H +ZOOAPI zhandle_t *zookeeper_init_ssl(const char *host, const char *cert, watcher_fn fn, + int recv_timeout, const clientid_t *clientid, void *context, int flags); +#endif + +ZOOAPI void close_zsock(zsock_t *zsock); + +/** + * \brief create a handle to communicate with zookeeper. + * + * This function is identical to \ref zookeeper_init except it allows one + * to specify an additional callback to be used for all logging for that + * specific connection. For more details on the logging callback see + * \ref zoo_get_log_callback and \ref zoo_set_log_callback. + * + * This method creates a new handle and a zookeeper session that corresponds + * to that handle. Session establishment is asynchronous, meaning that the + * session should not be considered established until (and unless) an + * event of state ZOO_CONNECTED_STATE is received. + * \param host comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * \param fn the global watcher callback function. When notifications are + * triggered this function will be invoked. + * \param clientid the id of a previously established session that this + * client will be reconnecting to. Pass 0 if not reconnecting to a previous + * session. Clients can access the session id of an established, valid, + * connection by calling \ref zoo_client_id. If the session corresponding to + * the specified clientid has expired, or if the clientid is invalid for + * any reason, the returned zhandle_t will be invalid -- the zhandle_t + * state will indicate the reason for failure (typically + * ZOO_EXPIRED_SESSION_STATE). + * \param context the handback object that will be associated with this instance + * of zhandle_t. Application can access it (for example, in the watcher + * callback) using \ref zoo_get_context. The object is not used by zookeeper + * internally and can be null. + * \param flags reserved for future use. Should be set to zero. + * \param log_callback All log messages will be passed to this callback function. + * For more details see \ref zoo_get_log_callback and \ref zoo_set_log_callback. + * \return a pointer to the opaque zhandle structure. If it fails to create + * a new zhandle the function returns NULL and the errno variable + * indicates the reason. + */ +ZOOAPI zhandle_t *zookeeper_init2(const char *host, watcher_fn fn, + int recv_timeout, const clientid_t *clientid, void *context, int flags, + log_callback_fn log_callback); + +/** + * \brief update the list of servers this client will connect to. + * + * This method allows a client to update the connection string by providing + * a new comma separated list of host:port pairs, each corresponding to a + * ZooKeeper server. + * + * This function invokes a probabilistic load-balancing algorithm which may cause + * the client to disconnect from its current host to achieve expected uniform + * connections per server in the new list. In case the current host to which the + * client is connected is not in the new list this call will always cause the + * connection to be dropped. Otherwise, the decision is based on whether the + * number of servers has increased or decreased and by how much. + * + * If the connection is dropped, the client moves to a special "reconfig" mode + * where he chooses a new server to connect to using the probabilistic algorithm. + * After finding a server or exhaustively trying all the servers in the new list, + * the client moves back to the normal mode of operation where it will pick an + * arbitrary server from the 'host' string. + * + * See {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1355} for the + * protocol and its evaluation, + * + * \param host comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZSYSTEMERROR -- a system (OS) error occured; it's worth checking errno to get details + */ +ZOOAPI int zoo_set_servers(zhandle_t *zh, const char *hosts); + +/** + * \brief cycle to the next server on the next connection attempt. + * + * Note: typically this method should NOT be used outside of testing. + * + * This method allows a client to cycle through the list of servers in it's + * connection pool to be used on the next connection attempt. This function does + * not actually trigger a connection or state change in any way. Its purpose is + * to allow testing changing servers on the fly and the probabilistic load + * balancing algorithm. + */ +ZOOAPI void zoo_cycle_next_server(zhandle_t *zh); + +/** + * \brief get current host:port this client is connecting/connected to. + * + * Note: typically this method should NOT be used outside of testing. + * + * This method allows a client to get the current host:port that this client + * is either in the process of connecting to or is currently connected to. This + * is mainly used for testing purposes but might also come in handy as a general + * purpose tool to be used by other clients. + */ +ZOOAPI const char* zoo_get_current_server(zhandle_t* zh); + +/** + * \brief close the zookeeper handle and free up any resources. + * + * After this call, the client session will no longer be valid. The function + * will flush any outstanding send requests before return. As a result it may + * block. + * + * This method should only be called only once on a zookeeper handle. Calling + * twice will cause undefined (and probably undesirable behavior). Calling any other + * zookeeper method after calling close is undefined behaviour and should be avoided. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \return a result code. Regardless of the error code returned, the zhandle + * will be destroyed and all resources freed. + * + * ZOK - success + * ZBADARGUMENTS - invalid input parameters + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZOPERATIONTIMEOUT - failed to flush the buffers within the specified timeout. + * ZCONNECTIONLOSS - a network error occurred while attempting to send request to server + * ZSYSTEMERROR -- a system (OS) error occurred; it's worth checking errno to get details + */ +ZOOAPI int zookeeper_close(zhandle_t *zh); + +/** + * \brief return the client session id, only valid if the connections + * is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE) + */ +ZOOAPI const clientid_t *zoo_client_id(zhandle_t *zh); + +/** + * \brief return the timeout for this session, only valid if the connections + * is currently connected (ie. last watcher state is ZOO_CONNECTED_STATE). This + * value may change after a server re-connect. + */ +ZOOAPI int zoo_recv_timeout(zhandle_t *zh); + +/** + * \brief return the context for this handle. + */ +ZOOAPI const void *zoo_get_context(zhandle_t *zh); + +/** + * \brief set the context for this handle. + */ +ZOOAPI void zoo_set_context(zhandle_t *zh, void *context); + +/** + * \brief set a watcher function + * \return previous watcher function + */ +ZOOAPI watcher_fn zoo_set_watcher(zhandle_t *zh,watcher_fn newFn); + +/** + * \brief returns the socket address for the current connection + * \return socket address of the connected host or NULL on failure, only valid if the + * connection is current connected + */ +ZOOAPI struct sockaddr* zookeeper_get_connected_host(zhandle_t *zh, + struct sockaddr *addr, socklen_t *addr_len); + +#ifndef THREADED +/** + * \brief Returns the events that zookeeper is interested in. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param fd is the file descriptor of interest + * \param interest is an or of the ZOOKEEPER_WRITE and ZOOKEEPER_READ flags to + * indicate the I/O of interest on fd. + * \param tv a timeout value to be used with select/poll system call + * \return a result code. + * ZOK - success + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZCONNECTIONLOSS - a network error occurred while attempting to establish + * a connection to the server + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZOPERATIONTIMEOUT - hasn't received anything from the server for 2/3 of the + * timeout value specified in zookeeper_init() + * ZSYSTEMERROR -- a system (OS) error occurred; it's worth checking errno to get details + */ +#ifdef WIN32 +ZOOAPI int zookeeper_interest(zhandle_t *zh, SOCKET *fd, int *interest, + struct timeval *tv); +#else +ZOOAPI int zookeeper_interest(zhandle_t *zh, int *fd, int *interest, + struct timeval *tv); +#endif + +/** + * \brief Notifies zookeeper that an event of interest has happened. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param events will be an OR of the ZOOKEEPER_WRITE and ZOOKEEPER_READ flags. + * \return a result code. + * ZOK - success + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZCONNECTIONLOSS - a network error occurred while attempting to send request to server + * ZSESSIONEXPIRED - connection attempt failed -- the session's expired + * ZAUTHFAILED - authentication request failed, e.i. invalid credentials + * ZRUNTIMEINCONSISTENCY - a server response came out of order + * ZSYSTEMERROR -- a system (OS) error occurred; it's worth checking errno to get details + * ZNOTHING -- not an error; simply indicates that there no more data from the server + * to be processed (when called with ZOOKEEPER_READ flag). + */ +ZOOAPI int zookeeper_process(zhandle_t *zh, int events); +#endif + +/** + * \brief signature of a completion function for a call that returns void. + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*void_completion_t)(int rc, const void *data); + +/** + * \brief signature of a completion function that returns a Stat structure. + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param stat a pointer to the stat information for the node involved in + * this function. If a non zero error code is returned, the content of + * stat is undefined. The programmer is NOT responsible for freeing stat. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*stat_completion_t)(int rc, const struct Stat *stat, + const void *data); + +/** + * \brief signature of a completion function that returns data. + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param value the value of the information returned by the asynchronous call. + * If a non zero error code is returned, the content of value is undefined. + * The programmer is NOT responsible for freeing value. + * \param value_len the number of bytes in value. + * \param stat a pointer to the stat information for the node involved in + * this function. If a non zero error code is returned, the content of + * stat is undefined. The programmer is NOT responsible for freeing stat. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*data_completion_t)(int rc, const char *value, int value_len, + const struct Stat *stat, const void *data); + +/** + * \brief signature of a completion function that returns a list of strings. + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param strings a pointer to the structure containng the list of strings of the + * names of the children of a node. If a non zero error code is returned, + * the content of strings is undefined. The programmer is NOT responsible + * for freeing strings. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*strings_completion_t)(int rc, + const struct String_vector *strings, const void *data); + +/** + * \brief signature of a completion function that returns a string and stat. + * . + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param value the value of the string returned. + * \param stat a pointer to the stat information for the node involved in + * this function. If a non zero error code is returned, the content of + * stat is undefined. The programmer is NOT responsible for freeing stat. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*string_stat_completion_t)(int rc, + const char *string, const struct Stat *stat, const void *data); + +/** + * \brief signature of a completion function that returns a list of strings and stat. + * . + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param strings a pointer to the structure containng the list of strings of the + * names of the children of a node. If a non zero error code is returned, + * the content of strings is undefined. The programmer is NOT responsible + * for freeing strings. + * \param stat a pointer to the stat information for the node involved in + * this function. If a non zero error code is returned, the content of + * stat is undefined. The programmer is NOT responsible for freeing stat. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*strings_stat_completion_t)(int rc, + const struct String_vector *strings, const struct Stat *stat, + const void *data); + +/** + * \brief signature of a completion function that returns a list of strings. + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param value the value of the string returned. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void + (*string_completion_t)(int rc, const char *value, const void *data); + +/** + * \brief signature of a completion function that returns an ACL. + * + * This method will be invoked at the end of a asynchronous call and also as + * a result of connection loss or timeout. + * \param rc the error code of the call. Connection loss/timeout triggers + * the completion with one of the following error codes: + * ZCONNECTIONLOSS -- lost connection to the server + * ZOPERATIONTIMEOUT -- connection timed out + * Data related events trigger the completion with error codes listed the + * Exceptions section of the documentation of the function that initiated the + * call. (Zero indicates call was successful.) + * \param acl a pointer to the structure containng the ACL of a node. If a non + * zero error code is returned, the content of strings is undefined. The + * programmer is NOT responsible for freeing acl. + * \param stat a pointer to the stat information for the node involved in + * this function. If a non zero error code is returned, the content of + * stat is undefined. The programmer is NOT responsible for freeing stat. + * \param data the pointer that was passed by the caller when the function + * that this completion corresponds to was invoked. The programmer + * is responsible for any memory freeing associated with the data + * pointer. + */ +typedef void (*acl_completion_t)(int rc, struct ACL_vector *acl, + struct Stat *stat, const void *data); + +/** + * \brief get the state of the zookeeper connection. + * + * The return value will be one of the \ref State Consts. + */ +ZOOAPI int zoo_state(zhandle_t *zh); + +/** + * \brief create a node. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * \param data The data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_acreate(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + string_completion_t completion, const void *data); + +/** + * \brief create a node. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. When ZOO_*_WITH_TTL is selected, a ttl node will be + * created. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param ttl the value of ttl in milliseconds. It must be positive for ZOO_*_WITH_TTL + * Create modes, otherwise it must be -1. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * \param data The data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_acreate_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, int64_t ttl, + string_completion_t completion, const void *data); + +/** + * \brief create a node asynchronously and returns stat details. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * \param data The data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_acreate2(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + string_stat_completion_t completion, const void *data); + +/** + * \brief create a node asynchronously and returns stat details. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. When ZOO_*_WITH_TTL is selected, a ttl node will be + * created. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param ttl the value of ttl in milliseconds. It must be positive for ZOO_*_WITH_TTL + * Create modes, otherwise it must be -1. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * \param data The data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_acreate2_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, int64_t ttl, + string_stat_completion_t completion, const void *data); + +/** + * \brief delete a node in zookeeper. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param version the expected version of the node. The function will fail if the + * actual version of the node does not match the expected version. + * If -1 is used the version check will not take place. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADVERSION expected version does not match actual version. + * ZNOTEMPTY children are present; node cannot be deleted. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_adelete(zhandle_t *zh, const char *path, int version, + void_completion_t completion, const void *data); + +/** + * \brief checks the existence of a node in zookeeper. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify the + * client if the node changes. The watch will be set even if the node does not + * exist. This allows clients to watch for nodes to appear. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aexists(zhandle_t *zh, const char *path, int watch, + stat_completion_t completion, const void *data); + +/** + * \brief checks the existence of a node in zookeeper. + * + * This function is similar to \ref zoo_axists except it allows one specify + * a watcher object - a function pointer and associated context. The function + * will be called once the watch has fired. The associated context data will be + * passed to the function as the watcher context parameter. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null a watch will set on the specified znode on the server. + * The watch will be set even if the node does not exist. This allows clients + * to watch for nodes to appear. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_awexists(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + stat_completion_t completion, const void *data); + +/** + * \brief gets the data associated with a node. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aget(zhandle_t *zh, const char *path, int watch, + data_completion_t completion, const void *data); + +/** + * \brief gets the data associated with a node. + * + * This function is similar to \ref zoo_aget except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_awget(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + data_completion_t completion, const void *data); + +/** + * \brief gets the last committed configuration of the ZooKeeper cluster as it is known to + * the server to which the client is connected. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the configuration changes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the configuration node (/zookeeper/config) does not exist. + * ZNOAUTH the client does not have permission to access the configuration node. + * \param data the configuration data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_agetconfig(zhandle_t *zh, int watch, + data_completion_t completion, const void *data); + +/** + * \brief gets the last committed configuration of the ZooKeeper cluster as it is known to + * the server to which the client is connected. + * + * This function is similar to \ref zoo_agetconfig except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the configuration node (/zookeeper/config) does not exist. + * ZNOAUTH the client does not have permission to access the configuration node. + * \param data the configuration data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_awgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, + data_completion_t completion, const void *data); + +/** + * \brief asynchronous reconfiguration interface - allows changing ZK cluster + * ensemble membership and roles of ensemble peers. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param joining - comma separated list of servers to be added to the ensemble. + * Each has a configuration line for a server to be added (as would appear in a + * configuration file), only for maj. quorums. NULL for non-incremental reconfiguration. + * \param leaving - comma separated list of server IDs to be removed from the ensemble. + * Each has an id of a server to be removed, only for maj. quorums. NULL for + * non-incremental reconfiguration. + * \param members - comma separated list of new membership (e.g., contents of a + * membership configuration file) - for use only with a non-incremental + * reconfiguration. NULL for incremental reconfiguration. + * \param version - version of config from which we want to reconfigure - if + * current config is different reconfiguration will fail. Should be -1 to disable + * this option. + * \param completion - the routine to invoke when the request completes. The + * completion will be triggered with one of the following codes passed in as the + * rc argument: + * ZOK operation completed successfully + * \param data the configuration data that will be passed to the completion routine + * when the function completes. + * \return return value of the function call. + * ZOK operation completed successfully + * ZBADARGUMENTS - invalid input parameters (one case when this is returned is + * when the new config has less than 2 servers) + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZNEWCONFIGNOQUORUM - no quorum of new config is connected and up-to-date with + * the leader of last committed config - try invoking reconfiguration after new servers are connected and synced + * ZRECONFIGINPROGRESS - another reconfig is currently in progress + */ +ZOOAPI int zoo_areconfig(zhandle_t *zh, const char *joining, const char *leaving, + const char *members, int64_t version, data_completion_t dc, const void *data); + +/** + * \brief sets the data associated with a node. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param buffer the buffer holding data to be written to the node. + * \param buflen the number of bytes from buffer to write. + * \param version the expected version of the node. The function will fail if + * the actual version of the node does not match the expected version. If -1 is + * used the version check will not take place. * completion: If null, + * the function will execute synchronously. Otherwise, the function will return + * immediately and invoke the completion routine when the request completes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADVERSION expected version does not match actual version. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aset(zhandle_t *zh, const char *path, const char *buffer, int buflen, + int version, stat_completion_t completion, const void *data); + +/** + * \brief lists the children of a node. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aget_children(zhandle_t *zh, const char *path, int watch, + strings_completion_t completion, const void *data); + +/** + * \brief lists the children of a node. + * + * This function is similar to \ref zoo_aget_children except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_awget_children(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + strings_completion_t completion, const void *data); + +/** + * \brief lists the children of a node, and get the parent stat. + * + * This function is new in version 3.3.0 + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aget_children2(zhandle_t *zh, const char *path, int watch, + strings_stat_completion_t completion, const void *data); + +/** + * \brief lists the children of a node, and get the parent stat. + * + * This function is similar to \ref zoo_aget_children2 except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * This function is new in version 3.3.0 + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_awget_children2(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + strings_stat_completion_t completion, const void *data); + +/** + * \brief Flush leader channel. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ + +ZOOAPI int zoo_async(zhandle_t *zh, const char *path, + string_completion_t completion, const void *data); + + +/** + * \brief gets the acl associated with a node. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aget_acl(zhandle_t *zh, const char *path, acl_completion_t completion, + const void *data); + +/** + * \brief sets the acl associated with a node. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param buffer the buffer holding the acls to be written to the node. + * \param buflen the number of bytes from buffer to write. + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with one of the following codes passed in as the rc argument: + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZINVALIDACL invalid ACL specified + * ZBADVERSION expected version does not match actual version. + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_aset_acl(zhandle_t *zh, const char *path, int version, + struct ACL_vector *acl, void_completion_t, const void *data); + +/** + * \brief atomically commits multiple zookeeper operations. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param count the number of operations + * \param ops an array of operations to commit + * \param results an array to hold the results of the operations + * \param completion the routine to invoke when the request completes. The completion + * will be triggered with any of the error codes that can that can be returned by the + * ops supported by a multi op (see \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset). + * \param data the data that will be passed to the completion routine when + * the function completes. + * \return the return code for the function call. This can be any of the + * values that can be returned by the ops supported by a multi op (see + * \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset). + */ +ZOOAPI int zoo_amulti(zhandle_t *zh, int count, const zoo_op_t *ops, + zoo_op_result_t *results, void_completion_t, const void *data); + +/** + * \brief return an error string. + * + * \param return code + * \return string corresponding to the return code + */ +ZOOAPI const char* zerror(int c); + +/** + * \brief specify application credentials. + * + * The application calls this function to specify its credentials for purposes + * of authentication. The server will use the security provider specified by + * the scheme parameter to authenticate the client connection. If the + * authentication request has failed: + * - the server connection is dropped + * - the watcher is called with the ZOO_AUTH_FAILED_STATE value as the state + * parameter. + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param scheme the id of authentication scheme. Natively supported: + * "digest" password-based authentication + * \param cert application credentials. The actual value depends on the scheme. + * \param certLen the length of the data parameter + * \param completion the routine to invoke when the request completes. One of + * the following result codes may be passed into the completion callback: + * ZOK operation completed successfully + * ZAUTHFAILED authentication failed + * \param data the data that will be passed to the completion routine when the + * function completes. + * \return ZOK on success or one of the following errcodes on failure: + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZSYSTEMERROR - a system error occurred + */ +ZOOAPI int zoo_add_auth(zhandle_t *zh,const char* scheme,const char* cert, + int certLen, void_completion_t completion, const void *data); + +/** + * \brief checks if the current zookeeper connection state can't be recovered. + * + * The application must close the zhandle and try to reconnect. + * + * \param zh the zookeeper handle (see \ref zookeeper_init) + * \return ZINVALIDSTATE if connection is unrecoverable + */ +ZOOAPI int is_unrecoverable(zhandle_t *zh); + +/** + * \brief sets the debugging level for the library + */ +ZOOAPI void zoo_set_debug_level(ZooLogLevel logLevel); + +/** + * \brief sets the stream to be used by the library for logging + * + * The zookeeper library uses stderr as its default log stream. Application + * must make sure the stream is writable. Passing in NULL resets the stream + * to its default value (stderr). + */ +ZOOAPI void zoo_set_log_stream(FILE* logStream); + +/** + * \brief gets the callback to be used by this connection for logging. + * + * This is a per-connection logging mechanism that will take priority over + * the library-wide default log stream. That is, zookeeper library will first + * try to use a per-connection callback if available and if not, will fallback + * to using the logging stream. Passing in NULL resets the callback and will + * cause it to then fallback to using the logging stream as described in \ref + * zoo_set_log_stream. + */ +ZOOAPI log_callback_fn zoo_get_log_callback(const zhandle_t *zh); + +/** + * \brief sets the callback to be used by the library for logging + * + * Setting this callback has the effect of overriding the default log stream. + * Zookeeper will first try to use a per-connection callback if available + * and if not, will fallback to using the logging stream. Passing in NULL + * resets the callback and will cause it to then fallback to using the logging + * stream as described in \ref zoo_set_log_stream. + * + * Note: The provided callback will be invoked by multiple threads and therefore + * it needs to be thread-safe. + */ +ZOOAPI void zoo_set_log_callback(zhandle_t *zh, log_callback_fn callback); + +/** + * \brief enable/disable quorum endpoint order randomization + * + * Note: typically this method should NOT be used outside of testing. + * + * If passed a non-zero value, will make the client connect to quorum peers + * in the order as specified in the zookeeper_init() call. + * A zero value causes zookeeper_init() to permute the peer endpoints + * which is good for more even client connection distribution among the + * quorum peers. + */ +ZOOAPI void zoo_deterministic_conn_order(int yesOrNo); + +/** + * Type of watches: used to select which type of watches should be removed + */ +typedef enum { + ZWATCHTYPE_CHILD = 1, + ZWATCHTYPE_DATA = 2, + ZWATCHTYPE_ANY = 3 +} ZooWatcherType; + +/** + * \brief removes the watches for the given path and watcher type. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the path for which watches will be removed + * \param wtype the watcher type to be removed + * \param watcher the watcher to be removed, if null all watches for that + * path (and watcher type) will be removed + * \param watcherCtx the contex associated with the watcher to be removed + * \param local whether the watches will be removed locally even if there is + * no server connection + * \return the return code for the function call. + * ZOK - operation completed successfully + * ZNOWATCHER - the watcher couldn't be found. + * ZINVALIDSTATE - if !local, zhandle state is either ZOO_SESSION_EXPIRED_STATE + * or ZOO_AUTH_FAILED_STATE + * ZBADARGUMENTS - invalid input parameters + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZSYSTEMERROR - a system error occured + */ +ZOOAPI int zoo_aremove_watches(zhandle_t *zh, const char *path, + ZooWatcherType wtype, watcher_fn watcher, void *watcherCtx, int local, + void_completion_t *completion, const void *data); + +/** + * \brief removes all the watches for the given path and watcher type. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the path for which watches will be removed + * \param wtype the watcher type to be removed + * \param local whether the watches will be removed locally even if there is + * no server connection + * \return the return code for the function call. + * ZOK - operation completed successfully + * ZNOWATCHER - the watcher couldn't be found. + * ZINVALIDSTATE - if !local, zhandle state is either ZOO_SESSION_EXPIRED_STATE + * or ZOO_AUTH_FAILED_STATE + * ZBADARGUMENTS - invalid input parameters + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZSYSTEMERROR - a system error occured + */ +ZOOAPI int zoo_remove_all_watches(zhandle_t *zh, const char *path, + ZooWatcherType wtype, int local); + +/** + * \brief removes all the watches for the given path and watcher type. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the path for which watches will be removed + * \param wtype the watcher type to be removed + * \param local whether the watches will be removed locally even if there is + * no server connection + * \return the return code for the function call. + * ZOK - operation completed successfully + * ZNOWATCHER - the watcher couldn't be found. + * ZINVALIDSTATE - if !local, zhandle state is either ZOO_SESSION_EXPIRED_STATE + * or ZOO_AUTH_FAILED_STATE + * ZBADARGUMENTS - invalid input parameters + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZSYSTEMERROR - a system error occured +*/ +ZOOAPI int zoo_aremove_all_watches(zhandle_t *zh, const char *path, + ZooWatcherType wtype, int local, void_completion_t *completion, + const void *data); + +#ifdef THREADED +/** + * \brief create a node synchronously. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. To set the data to be NULL use + * value as NULL and valuelen as -1. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param path_buffer Buffer which will be filled with the path of the + * new node (this might be different than the supplied path + * because of the ZOO_SEQUENCE flag). The path string will always be + * null-terminated. This parameter may be NULL if path_buffer_len = 0. + * \param path_buffer_len Size of path buffer; if the path of the new + * node (including space for the null terminator) exceeds the buffer size, + * the path string will be truncated to fit. The actual path of the + * new node in the server will not be affected by the truncation. + * The path string will always be null-terminated. + * \return one of the following codes are returned: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_create(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len); + +/** + * \brief create a node synchronously. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. When ZOO_*_WITH_TTL is selected, a ttl node will be + * created. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. To set the data to be NULL use + * value as NULL and valuelen as -1. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param ttl the value of ttl in milliseconds. It must be positive for ZOO_*_WITH_TTL + * Create modes, otherwise it must be -1. + * \param path_buffer Buffer which will be filled with the path of the + * new node (this might be different than the supplied path + * because of the ZOO_SEQUENCE flag). The path string will always be + * null-terminated. This parameter may be NULL if path_buffer_len = 0. + * \param path_buffer_len Size of path buffer; if the path of the new + * node (including space for the null terminator) exceeds the buffer size, + * the path string will be truncated to fit. The actual path of the + * new node in the server will not be affected by the truncation. + * The path string will always be null-terminated. + * \return one of the following codes are returned: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_create_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, int64_t ttl, + char *path_buffer, int path_buffer_len); + +/** + * \brief create a node synchronously and collect stat details. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. To set the data to be NULL use + * value as NULL and valuelen as -1. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param path_buffer Buffer which will be filled with the path of the + * new node (this might be different than the supplied path + * because of the ZOO_SEQUENCE flag). The path string will always be + * null-terminated. This parameter may be NULL if path_buffer_len = 0. + * \param path_buffer_len Size of path buffer; if the path of the new + * node (including space for the null terminator) exceeds the buffer size, + * the path string will be truncated to fit. The actual path of the + * new node in the server will not be affected by the truncation. + * The path string will always be null-terminated. + * \param stat The Stat struct to store Stat info into. + * \return one of the following codes are returned: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_create2(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len, struct Stat *stat); + +/** + * \brief create a node synchronously and collect stat details. + * + * This method will create a node in ZooKeeper. A node can only be created if + * it does not already exist. The Create Mode affects the creation of nodes. + * If ZOO_EPHEMERAL mode is chosen, the node will automatically get removed if the + * client session goes away. If ZOO_CONTAINER flag is set, a container node will be + * created. For ZOO_*_SEQUENTIAL modes, a unique monotonically increasing + * sequence number is appended to the path name. The sequence number is always fixed + * length of 10 digits, 0 padded. When ZOO_*_WITH_TTL is selected, a ttl node will be + * created. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path The name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param value The data to be stored in the node. + * \param valuelen The number of bytes in data. To set the data to be NULL use + * value as NULL and valuelen as -1. + * \param acl The initial ACL of the node. The ACL must not be null or empty. + * \param mode this parameter should be one of the Create Modes. + * \param ttl the value of ttl in milliseconds. It must be positive for ZOO_*_WITH_TTL + * Create modes, otherwise it must be -1. + * \param path_buffer Buffer which will be filled with the path of the + * new node (this might be different than the supplied path + * because of the ZOO_SEQUENCE flag). The path string will always be + * null-terminated. This parameter may be NULL if path_buffer_len = 0. + * \param path_buffer_len Size of path buffer; if the path of the new + * node (including space for the null terminator) exceeds the buffer size, + * the path string will be truncated to fit. The actual path of the + * new node in the server will not be affected by the truncation. + * The path string will always be null-terminated. + * \param stat The Stat struct to store Stat info into. + * \return one of the following codes are returned: + * ZOK operation completed successfully + * ZNONODE the parent node does not exist. + * ZNODEEXISTS the node already exists + * ZNOAUTH the client does not have permission. + * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_create2_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, int64_t ttl, + char *path_buffer, int path_buffer_len, struct Stat *stat); + +/** + * \brief delete a node in zookeeper synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param version the expected version of the node. The function will fail if the + * actual version of the node does not match the expected version. + * If -1 is used the version check will not take place. + * \return one of the following values is returned. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADVERSION expected version does not match actual version. + * ZNOTEMPTY children are present; node cannot be deleted. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_delete(zhandle_t *zh, const char *path, int version); + +/** + * \brief checks the existence of a node in zookeeper synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify the + * client if the node changes. The watch will be set even if the node does not + * exist. This allows clients to watch for nodes to appear. + * \param the return stat value of the node. + * \return return code of the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_exists(zhandle_t *zh, const char *path, int watch, struct Stat *stat); + +/** + * \brief checks the existence of a node in zookeeper synchronously. + * + * This function is similar to \ref zoo_exists except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null a watch will set on the specified znode on the server. + * The watch will be set even if the node does not exist. This allows clients + * to watch for nodes to appear. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param the return stat value of the node. + * \return return code of the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_wexists(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, struct Stat *stat); + +/** + * \brief gets the data associated with a node synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param buffer the buffer holding the node data returned by the server + * \param buffer_len is the size of the buffer pointed to by the buffer parameter. + * It'll be set to the actual data length upon return. If the data is NULL, length is -1. + * \param stat if not NULL, will hold the value of stat for the path on return. + * \return return value of the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_get(zhandle_t *zh, const char *path, int watch, char *buffer, + int* buffer_len, struct Stat *stat); +/** + * \brief gets the data associated with a node synchronously. + * + * This function is similar to \ref zoo_get except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param buffer the buffer holding the node data returned by the server + * \param buffer_len is the size of the buffer pointed to by the buffer parameter. + * It'll be set to the actual data length upon return. If the data is NULL, length is -1. + * \param stat if not NULL, will hold the value of stat for the path on return. + * \return return value of the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_wget(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + char *buffer, int* buffer_len, struct Stat *stat); + +/** + * \brief gets the last committed configuration of the ZooKeeper cluster as it is known to + * the server to which the client is connected, synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param buffer the buffer holding the configuration data returned by the server + * \param buffer_len is the size of the buffer pointed to by the buffer parameter. + * It'll be set to the actual data length upon return. If the data is NULL, length is -1. + * \param stat if not NULL, will hold the value of stat for the path on return. + * \return return value of the function call. + * ZOK operation completed successfully + * ZNONODE the configuration node (/zookeeper/config) does not exist. + * ZNOAUTH the client does not have permission to access the configuration node. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_getconfig(zhandle_t *zh, int watch, char *buffer, + int* buffer_len, struct Stat *stat); + +/** + * \brief gets the last committed configuration of the ZooKeeper cluster as it is known to + * the server to which the client is connected, synchronously. + * + * This function is similar to \ref zoo_getconfig except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param buffer the buffer holding the configuration data returned by the server + * \param buffer_len is the size of the buffer pointed to by the buffer parameter. + * It'll be set to the actual data length upon return. If the data is NULL, length is -1. + * \param stat if not NULL, will hold the value of stat for the path on return. + * \return return value of the function call. + * ZOK operation completed successfully + * ZNONODE the configuration node (/zookeeper/config) does not exist. + * ZNOAUTH the client does not have permission to access the configuration node. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_wgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, + char *buffer, int* buffer_len, struct Stat *stat); + +/** + * \brief synchronous reconfiguration interface - allows changing ZK cluster + * ensemble membership and roles of ensemble peers. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param joining - comma separated list of servers to be added to the ensemble. + * Each has a configuration line for a server to be added (as would appear in a + * configuration file), only for maj. quorums. NULL for non-incremental reconfiguration. + * \param leaving - comma separated list of server IDs to be removed from the ensemble. + * Each has an id of a server to be removed, only for maj. quorums. NULL for + * non-incremental reconfiguration. + * \param members - comma separated list of new membership (e.g., contents of a + * membership configuration file) - for use only with a non-incremental + * reconfiguration. NULL for incremental reconfiguration. + * \param version - zxid of config from which we want to reconfigure - if + * current config is different reconfiguration will fail. Should be -1 to + * disable this option. + * \param buffer the buffer holding the configuration data returned by the server + * \param buffer_len is the size of the buffer pointed to by the buffer parameter. + * It'll be set to the actual data length upon return. If the data is NULL, length + * is -1. + * \param stat if not NULL, will hold the value of stat for the path on return. + * \return return value of the function call. + * ZOK operation completed successfully + * ZBADARGUMENTS - invalid input parameters (one case when this is returned is + * when the new config has less than 2 servers) + * ZINVALIDSTATE - zhandle state is either in ZOO_SESSION_EXPIRED_STATE or + * ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZNEWCONFIGNOQUORUM - no quorum of new config is connected and up-to-date with + * the leader of last committed config - try invoking reconfiguration after new + * servers are connected and synced + * ZRECONFIGINPROGRESS - another reconfig is currently in progress + */ +ZOOAPI int zoo_reconfig(zhandle_t *zh, const char *joining, const char *leaving, + const char *members, int64_t version, char *buffer, int* buffer_len, + struct Stat *stat); + +/** + * \brief sets the data associated with a node. See zoo_set2 function if + * you require access to the stat information associated with the znode. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param buffer the buffer holding data to be written to the node. + * \param buflen the number of bytes from buffer to write. To set NULL as data + * use buffer as NULL and buflen as -1. + * \param version the expected version of the node. The function will fail if + * the actual version of the node does not match the expected version. If -1 is + * used the version check will not take place. + * \return the return code for the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADVERSION expected version does not match actual version. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_set(zhandle_t *zh, const char *path, const char *buffer, + int buflen, int version); + +/** + * \brief sets the data associated with a node. This function is the same + * as zoo_set except that it also provides access to stat information + * associated with the znode. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param buffer the buffer holding data to be written to the node. + * \param buflen the number of bytes from buffer to write. To set NULL as data + * use buffer as NULL and buflen as -1. + * \param version the expected version of the node. The function will fail if + * the actual version of the node does not match the expected version. If -1 is + * used the version check will not take place. + * \param stat if not NULL, will hold the value of stat for the path on return. + * \return the return code for the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADVERSION expected version does not match actual version. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_set2(zhandle_t *zh, const char *path, const char *buffer, + int buflen, int version, struct Stat *stat); + +/** + * \brief lists the children of a node synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param strings return value of children paths. + * \return the return code of the function. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_get_children(zhandle_t *zh, const char *path, int watch, + struct String_vector *strings); + +/** + * \brief lists the children of a node synchronously. + * + * This function is similar to \ref zoo_get_children except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param strings return value of children paths. + * \return the return code of the function. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_wget_children(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + struct String_vector *strings); + +/** + * \brief lists the children of a node and get its stat synchronously. + * + * This function is new in version 3.3.0 + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watch if nonzero, a watch will be set at the server to notify + * the client if the node changes. + * \param strings return value of children paths. + * \param stat return value of node stat. + * \return the return code of the function. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_get_children2(zhandle_t *zh, const char *path, int watch, + struct String_vector *strings, struct Stat *stat); + +/** + * \brief lists the children of a node and get its stat synchronously. + * + * This function is similar to \ref zoo_get_children except it allows one specify + * a watcher object rather than a boolean watch flag. + * + * This function is new in version 3.3.0 + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param watcher if non-null, a watch will be set at the server to notify + * the client if the node changes. + * \param watcherCtx user specific data, will be passed to the watcher callback. + * Unlike the global context set by \ref zookeeper_init, this watcher context + * is associated with the given instance of the watcher only. + * \param strings return value of children paths. + * \param stat return value of node stat. + * \return the return code of the function. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_wget_children2(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + struct String_vector *strings, struct Stat *stat); + +/** + * \brief gets the acl associated with a node synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param acl the return value of acls on the path. + * \param stat returns the stat of the path specified. + * \return the return code for the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_get_acl(zhandle_t *zh, const char *path, struct ACL_vector *acl, + struct Stat *stat); + +/** + * \brief sets the acl associated with a node synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the name of the node. Expressed as a file name with slashes + * separating ancestors of the node. + * \param version the expected version of the path. + * \param acl the acl to be set on the path. + * \return the return code for the function call. + * ZOK operation completed successfully + * ZNONODE the node does not exist. + * ZNOAUTH the client does not have permission. + * ZINVALIDACL invalid ACL specified + * ZBADVERSION expected version does not match actual version. + * ZBADARGUMENTS - invalid input parameters + * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + */ +ZOOAPI int zoo_set_acl(zhandle_t *zh, const char *path, int version, + const struct ACL_vector *acl); + +/** + * \brief atomically commits multiple zookeeper operations synchronously. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param count the number of operations + * \param ops an array of operations to commit + * \param results an array to hold the results of the operations + * \return the return code for the function call. This can be any of the + * values that can be returned by the ops supported by a multi op (see + * \ref zoo_acreate, \ref zoo_adelete, \ref zoo_aset). + */ +ZOOAPI int zoo_multi(zhandle_t *zh, int count, const zoo_op_t *ops, zoo_op_result_t *results); + +/** + * \brief removes the watches for the given path and watcher type. + * + * \param zh the zookeeper handle obtained by a call to \ref zookeeper_init + * \param path the path for which watches will be removed + * \param wtype the watcher type to be removed + * \param watcher the watcher to be removed, if null all watches for that + * path (and watcher type) will be removed + * \param watcherCtx the contex associated with the watcher to be removed + * \param local whether the watches will be removed locally even if there is + * no server connection + * \return the return code for the function call. + * ZOK - operation completed successfully + * ZNOWATCHER - the watcher couldn't be found. + * ZINVALIDSTATE - if !local, zhandle state is either ZOO_SESSION_EXPIRED_STATE + * or ZOO_AUTH_FAILED_STATE + * ZBADARGUMENTS - invalid input parameters + * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory + * ZSYSTEMERROR - a system error occured + */ +ZOOAPI int zoo_remove_watches(zhandle_t *zh, const char *path, + ZooWatcherType wtype, watcher_fn watcher, void *watcherCtx, int local); +#endif +#ifdef __cplusplus +} +#endif + +#endif /*ZOOKEEPER_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/include/zookeeper_log.h b/zookeeper-client/zookeeper-client-c/include/zookeeper_log.h new file mode 100644 index 0000000..56a9b73 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/zookeeper_log.h @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ZK_LOG_H_ +#define ZK_LOG_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern ZOOAPI ZooLogLevel logLevel; +#define LOGCALLBACK(_zh) zoo_get_log_callback(_zh) +#define LOGSTREAM NULL + +#define LOG_ERROR(_cb, ...) if(logLevel>=ZOO_LOG_LEVEL_ERROR) \ + log_message(_cb, ZOO_LOG_LEVEL_ERROR, __LINE__, __func__, __VA_ARGS__) +#define LOG_WARN(_cb, ...) if(logLevel>=ZOO_LOG_LEVEL_WARN) \ + log_message(_cb, ZOO_LOG_LEVEL_WARN, __LINE__, __func__, __VA_ARGS__) +#define LOG_INFO(_cb, ...) if(logLevel>=ZOO_LOG_LEVEL_INFO) \ + log_message(_cb, ZOO_LOG_LEVEL_INFO, __LINE__, __func__, __VA_ARGS__) +#define LOG_DEBUG(_cb, ...) if(logLevel==ZOO_LOG_LEVEL_DEBUG) \ + log_message(_cb, ZOO_LOG_LEVEL_DEBUG, __LINE__, __func__, __VA_ARGS__) + +ZOOAPI void log_message(log_callback_fn callback, ZooLogLevel curLevel, + int line, const char* funcName, const char* format, ...); + +FILE* zoo_get_log_stream(); + +#ifdef __cplusplus +} +#endif + +#endif /*ZK_LOG_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/include/zookeeper_version.h b/zookeeper-client/zookeeper-client-c/include/zookeeper_version.h new file mode 100644 index 0000000..63b6a5c --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/include/zookeeper_version.h @@ -0,0 +1,31 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#ifndef ZOOKEEPER_VERSION_H_ +#define ZOOKEEPER_VERSION_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZOO_VERSION "3.6.0" + +#ifdef __cplusplus +} +#endif + +#endif /* ZOOKEEPER_VERSION_H_ */ diff --git a/zookeeper-client/zookeeper-client-c/pom.xml b/zookeeper-client/zookeeper-client-c/pom.xml new file mode 100644 index 0000000..96b4e84 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/pom.xml @@ -0,0 +1,182 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-client + 3.6.3 + .. + + + zookeeper-client-c + jar + Apache ZooKeeper - Client - C + ZooKeeper c client + + + + c-test-coverage + + --enable-gcov + + + + no-c-test-coverage + + true + + + + + + + + + + + org.codehaus.mojo + exec-maven-plugin + + + autoreconf + process-sources + + exec + + + ${project.basedir} + autoreconf + + aclocal -I /usr/share/aclocal + + + -if + + + + + configure + process-sources + + exec + + + ${project.build.directory}/c + ${project.basedir}/configure + + ${project.basedir}/../.. + ANT + + + --with-openssl=${c-client-openssl} + --prefix=${project.build.directory}/c + ${c-test-coverage-arg} + + + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + generate-sources + generate-sources + + + + + + + run + + + + build-c-client + compile + + + + + + + + + + + + + + run + + + + test-cppunit + test + + + ${skipTests} + + + + + + + + + + + + + run + + + + replace-cclient-files-during-release + none + + run + + + + + + -SNAPSHOT + + + + -SNAPSHOT + + + + -SNAPSHOT + + + + + + + + + + diff --git a/zookeeper-client/zookeeper-client-c/src/addrvec.c b/zookeeper-client/zookeeper-client-c/src/addrvec.c new file mode 100644 index 0000000..b7f244e --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/addrvec.c @@ -0,0 +1,271 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include +#ifdef WIN32 +#define random rand /* replace POSIX random with Windows rand */ +#include /* must always be included before ws2tcpip.h */ +#include /* for sockaddr_storage */ +#include "winport.h" +#endif + +#include "addrvec.h" + +#define ADDRVEC_DEFAULT_GROW_AMOUNT 16 + +void addrvec_init(addrvec_t *avec) +{ + assert(avec); + avec->next = 0; + avec->count = 0; + avec->capacity = 0; + avec->data = NULL; +} + +void addrvec_free(addrvec_t *avec) +{ + if (avec == NULL) + { + return; + } + + avec->next = 0; + avec->count = 0; + avec->capacity = 0; + if (avec->data) { + free(avec->data); + avec->data = NULL; + } +} + +int addrvec_alloc(addrvec_t *avec) +{ + addrvec_init(avec); + return addrvec_grow_default(avec); +} + +int addrvec_alloc_capacity(addrvec_t* avec, uint32_t capacity) +{ + addrvec_init(avec); + return addrvec_grow(avec, capacity); +} + +int addrvec_grow(addrvec_t *avec, uint32_t grow_amount) +{ + unsigned int old_capacity = 0; + struct sockaddr_storage *old_data = NULL; + assert(avec); + + if (grow_amount == 0) + { + return 0; + } + + // Save off old data and capacity in case there is a realloc failure + old_capacity = avec->capacity; + old_data = avec->data; + + avec->capacity += grow_amount; + avec->data = realloc(avec->data, sizeof(*avec->data) * avec->capacity); + if (avec->data == NULL) + { + avec->capacity = old_capacity; + avec->data = old_data; + errno = ENOMEM; + return 1; + } + + return 0; +} + +int addrvec_grow_default(addrvec_t *avec) +{ + return addrvec_grow(avec, ADDRVEC_DEFAULT_GROW_AMOUNT); +} + +static int addrvec_grow_if_full(addrvec_t *avec) +{ + assert(avec); + if (avec->count == avec->capacity) + { + int rc = addrvec_grow_default(avec); + if (rc != 0) + { + return rc; + } + } + + return 0; +} + +int addrvec_contains(const addrvec_t *avec, const struct sockaddr_storage *addr) +{ + uint32_t i = 0; + if (!avec || !addr) + { + return 0; + } + + for (i = 0; i < avec->count; i++) + { + if (avec->data[i].ss_family != addr->ss_family) + continue; + switch (addr->ss_family) { + case AF_INET: + if (memcmp(&((struct sockaddr_in*)&avec->data[i])->sin_addr, + &((struct sockaddr_in*)addr)->sin_addr, + sizeof(struct in_addr)) == 0) + return 1; + break; +#ifdef AF_INET6 + case AF_INET6: + if (memcmp(&((struct sockaddr_in6*)&avec->data[i])->sin6_addr, + &((struct sockaddr_in6*)addr)->sin6_addr, + sizeof(struct in6_addr)) == 0) + return 1; + break; +#endif + default: + break; + } + } + + return 0; +} + +int addrvec_append(addrvec_t *avec, const struct sockaddr_storage *addr) +{ + int rc = 0; + assert(avec); + assert(addr); + + rc = addrvec_grow_if_full(avec); + if (rc != 0) + { + return rc; + } + + // Copy addrinfo into address list + memcpy(avec->data + avec->count, addr, sizeof(*addr)); + ++avec->count; + + return 0; +} + +int addrvec_append_addrinfo(addrvec_t *avec, const struct addrinfo *addrinfo) +{ + int rc = 0; + assert(avec); + assert(addrinfo); + + rc = addrvec_grow_if_full(avec); + if (rc != 0) + { + return rc; + } + + // Copy addrinfo into address list + memcpy(avec->data + avec->count, addrinfo->ai_addr, addrinfo->ai_addrlen); + ++avec->count; + + return 0; +} + +void addrvec_shuffle(addrvec_t *avec) +{ + int i = 0; + for (i = avec->count - 1; i > 0; --i) { + long int j = random()%(i+1); + if (i != j) { + struct sockaddr_storage t = avec->data[i]; + avec->data[i] = avec->data[j]; + avec->data[j] = t; + } + } +} + +int addrvec_hasnext(const addrvec_t *avec) +{ + return avec->count > 0 && (avec->next < avec->count); +} + +int addrvec_atend(const addrvec_t *avec) +{ + return avec->count > 0 && avec->next >= avec->count; +} + +void addrvec_next(addrvec_t *avec, struct sockaddr_storage *next) +{ + int index; + + // If we're at the end of the list, then reset index to start + if (addrvec_atend(avec)) { + avec->next = 0; + } + + if (!addrvec_hasnext(avec)) { + if (next) { + memset(next, 0, sizeof(*next)); + } + + return; + } + + index = avec->next++; + + if (next) { + *next = avec->data[index]; + } +} + +void addrvec_peek(addrvec_t *avec, struct sockaddr_storage *next) +{ + int index = avec->next; + + if (avec->count == 0) { + memset(next, 0, sizeof(*next)); + return; + } + + if (addrvec_atend(avec)) { + index = 0; + } + + *next = avec->data[index]; +} + + +int addrvec_eq(const addrvec_t *a1, const addrvec_t *a2) +{ + uint32_t i = 0; + if (a1->count != a2->count) + { + return 0; + } + + for (i = 0; i < a1->count; ++i) + { + if (!addrvec_contains(a2, &a1->data[i])) + return 0; + } + + return 1; +} diff --git a/zookeeper-client/zookeeper-client-c/src/addrvec.h b/zookeeper-client/zookeeper-client-c/src/addrvec.h new file mode 100644 index 0000000..a126429 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/addrvec.h @@ -0,0 +1,138 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ADDRVEC_H_ +#define ADDRVEC_H_ + +#ifndef WIN32 +#include +#include +#include +#include +#else +#include +#include +#endif + +/** + * This structure represents a list of addresses. It stores the count of the + * number of elements that have been inserted via calls to addrvec_append and + * addrvec_append_addrinfo. It also has a capacity field for the number of + * addresses it has the ability to hold without needing to be enlarged. + */ +typedef struct _addrvec { + unsigned int next; // next index to use + unsigned int count; // number of addresses in this list + unsigned int capacity; // number of address this list can hold + struct sockaddr_storage *data; // list of addresses +} addrvec_t; + +/** + * Initialize an addrvec by clearing out all its state. + */ +void addrvec_init(addrvec_t *avec); + +/** + * Free any memory used internally by an addrvec + */ +void addrvec_free(addrvec_t *avec); + +/** + * Allocate an addrvec with a default capacity (16) + */ +int addrvec_alloc(addrvec_t *avec); + +/** + * Allocates an addrvec with a specified capacity + */ +int addrvec_alloc_capacity(addrvec_t *avec, uint32_t capacity); + +/** + * Grow an addrvec by the specified amount. This will increase the capacity + * of the vector and not the contents. + */ +int addrvec_grow(addrvec_t *avec, uint32_t grow_amount); + +/** + * Similar to addrvec_grow but uses a default growth amount of 16. + */ +int addrvec_grow_default(addrvec_t *avec); + +/** + * Check if an addrvec contains the specificed sockaddr_storage value. + * \returns 1 if it contains the value and 0 otherwise. + */ +int addrvec_contains(const addrvec_t *avec, const struct sockaddr_storage *addr); + +/** + * Append the given sockaddr_storage pointer into the addrvec. The contents of + * the given 'addr' are copied into the addrvec via memcpy. + */ +int addrvec_append(addrvec_t *avec, const struct sockaddr_storage *addr); + +/** + * Append the given addrinfo pointer into the addrvec. The contents of the given + * 'addrinfo' are copied into the addrvec via memcpy. + */ +int addrvec_append_addrinfo(addrvec_t *avec, const struct addrinfo *addrinfo); + +/** + * Shuffle the addrvec so that it's internal list of addresses are randomized. + * Uses random() and assumes it has been properly seeded. + */ +void addrvec_shuffle(addrvec_t *avec); + +/** + * Determine if the addrvec has a next element (e.g. it's safe to call addrvec_next) + * + * \returns 1 if it has a next element and 0 otherwise + */ +int addrvec_hasnext(const addrvec_t *avec); + +/** + * Determine if the addrvec is at the end or not. Specifically, this means a + * subsequent call to addrvec_next will loop around to the start again. + */ +int addrvec_atend(const addrvec_t *avec); + +/** + * Get the next entry from the addrvec and update the associated index. + * + * If next is NULL, the index will still be updated. + * + * If the current index points at (or after) the last element in the vector then + * it will loop back around and start at the beginning of the list. + */ +void addrvec_next(addrvec_t *avec, struct sockaddr_storage *next); + +/** + * Retrieves the next entry from the addrvec but doesn't update the index. + */ +void addrvec_peek(addrvec_t *avec, struct sockaddr_storage *next); + +/** + * Compare two addrvecs for equality. + * + * \returns 1 if the contents of the two lists are identical and and 0 otherwise. + */ +int addrvec_eq(const addrvec_t *a1, const addrvec_t *a2); + +#endif // ADDRVEC_H + + + diff --git a/zookeeper-client/zookeeper-client-c/src/cli.c b/zookeeper-client/zookeeper-client-c/src/cli.c new file mode 100644 index 0000000..34f2b99 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/cli.c @@ -0,0 +1,1015 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/** + * cli.c is a example/sample C client shell for ZooKeeper. It contains + * basic shell functionality which exercises some of the features of + * the ZooKeeper C client API. It is not a full fledged client and is + * not meant for production usage - see the Java client shell for a + * fully featured shell. + */ + +#include +#include +#include +#include +#include + +#ifndef WIN32 +#include +#include +#include +#include +#else +#include "winport.h" +//#include <-- can't include, conflicting definitions of close() +int read(int _FileHandle, void * _DstBuf, unsigned int _MaxCharCount); +int write(int _Filehandle, const void * _Buf, unsigned int _MaxCharCount); +#define ctime_r(tctime, buffer) ctime_s (buffer, 40, tctime) +#include "win_getopt.h" // VisualStudio doesn't contain 'getopt' +#endif + +#include +#include +#include + +#ifdef YCA +#include +#endif + +#define _LL_CAST_ (long long) + +static zhandle_t *zh; +static clientid_t myid; +static const char *clientIdFile = 0; +struct timeval startTime; +static const char *cmd; +static const char *cert; +static int batchMode=0; + +static int to_send=0; +static int sent=0; +static int recvd=0; + +static int shutdownThisThing=0; + +static __attribute__ ((unused)) void +printProfileInfo(struct timeval start, struct timeval end, int thres, + const char* msg) +{ + int delay=(end.tv_sec*1000+end.tv_usec/1000)- + (start.tv_sec*1000+start.tv_usec/1000); + if(delay>thres) + fprintf(stderr,"%s: execution time=%dms\n",msg,delay); +} + +static const char* state2String(int state){ + if (state == 0) + return "CLOSED_STATE"; + if (state == ZOO_CONNECTING_STATE) + return "CONNECTING_STATE"; + if (state == ZOO_ASSOCIATING_STATE) + return "ASSOCIATING_STATE"; + if (state == ZOO_CONNECTED_STATE) + return "CONNECTED_STATE"; + if (state == ZOO_READONLY_STATE) + return "READONLY_STATE"; + if (state == ZOO_EXPIRED_SESSION_STATE) + return "EXPIRED_SESSION_STATE"; + if (state == ZOO_AUTH_FAILED_STATE) + return "AUTH_FAILED_STATE"; + + return "INVALID_STATE"; +} + +static const char* type2String(int state){ + if (state == ZOO_CREATED_EVENT) + return "CREATED_EVENT"; + if (state == ZOO_DELETED_EVENT) + return "DELETED_EVENT"; + if (state == ZOO_CHANGED_EVENT) + return "CHANGED_EVENT"; + if (state == ZOO_CHILD_EVENT) + return "CHILD_EVENT"; + if (state == ZOO_SESSION_EVENT) + return "SESSION_EVENT"; + if (state == ZOO_NOTWATCHING_EVENT) + return "NOTWATCHING_EVENT"; + + return "UNKNOWN_EVENT_TYPE"; +} + +void watcher(zhandle_t *zzh, int type, int state, const char *path, + void* context) +{ + /* Be careful using zh here rather than zzh - as this may be mt code + * the client lib may call the watcher before zookeeper_init returns */ + + fprintf(stderr, "Watcher %s state = %s", type2String(type), state2String(state)); + if (path && strlen(path) > 0) { + fprintf(stderr, " for path %s", path); + } + fprintf(stderr, "\n"); + + if (type == ZOO_SESSION_EVENT) { + if (state == ZOO_CONNECTED_STATE) { + const clientid_t *id = zoo_client_id(zzh); + if (myid.client_id == 0 || myid.client_id != id->client_id) { + myid = *id; + fprintf(stderr, "Got a new session id: 0x%llx\n", + _LL_CAST_ myid.client_id); + if (clientIdFile) { + FILE *fh = fopen(clientIdFile, "w"); + if (!fh) { + perror(clientIdFile); + } else { + int rc = fwrite(&myid, sizeof(myid), 1, fh); + if (rc != sizeof(myid)) { + perror("writing client id"); + } + fclose(fh); + } + } + } + } else if (state == ZOO_AUTH_FAILED_STATE) { + fprintf(stderr, "Authentication failure. Shutting down...\n"); + zookeeper_close(zzh); + shutdownThisThing=1; + zh=0; + } else if (state == ZOO_EXPIRED_SESSION_STATE) { + fprintf(stderr, "Session expired. Shutting down...\n"); + zookeeper_close(zzh); + shutdownThisThing=1; + zh=0; + } + } +} + +void dumpStat(const struct Stat *stat) { + char tctimes[40]; + char tmtimes[40]; + time_t tctime; + time_t tmtime; + + if (!stat) { + fprintf(stderr,"null\n"); + return; + } + tctime = stat->ctime/1000; + tmtime = stat->mtime/1000; + + ctime_r(&tmtime, tmtimes); + ctime_r(&tctime, tctimes); + + fprintf(stderr, "\tctime = %s\tczxid=%llx\n" + "\tmtime=%s\tmzxid=%llx\n" + "\tversion=%x\taversion=%x\n" + "\tephemeralOwner = %llx\n", + tctimes, _LL_CAST_ stat->czxid, tmtimes, + _LL_CAST_ stat->mzxid, + (unsigned int)stat->version, (unsigned int)stat->aversion, + _LL_CAST_ stat->ephemeralOwner); +} + +void my_string_completion(int rc, const char *name, const void *data) { + fprintf(stderr, "[%s]: rc = %d\n", (char*)(data==0?"null":data), rc); + if (!rc) { + fprintf(stderr, "\tname = %s\n", name); + } + if(batchMode) + shutdownThisThing=1; +} + +void my_string_completion_free_data(int rc, const char *name, const void *data) { + my_string_completion(rc, name, data); + free((void*)data); +} + +void my_string_stat_completion(int rc, const char *name, const struct Stat *stat, + const void *data) { + my_string_completion(rc, name, data); + dumpStat(stat); +} + +void my_string_stat_completion_free_data(int rc, const char *name, + const struct Stat *stat, const void *data) { + my_string_stat_completion(rc, name, stat, data); + free((void*)data); +} + +void my_data_completion(int rc, const char *value, int value_len, + const struct Stat *stat, const void *data) { + struct timeval tv; + int sec; + int usec; + gettimeofday(&tv, 0); + sec = tv.tv_sec - startTime.tv_sec; + usec = tv.tv_usec - startTime.tv_usec; + fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000); + fprintf(stderr, "%s: rc = %d\n", (char*)data, rc); + if (value) { + fprintf(stderr, " value_len = %d\n", value_len); + assert(write(2, value, value_len) == value_len); + } + fprintf(stderr, "\nStat:\n"); + dumpStat(stat); + free((void*)data); + if(batchMode) + shutdownThisThing=1; +} + +void my_silent_data_completion(int rc, const char *value, int value_len, + const struct Stat *stat, const void *data) { + recvd++; + fprintf(stderr, "Data completion %s rc = %d\n",(char*)data,rc); + free((void*)data); + if (recvd==to_send) { + fprintf(stderr,"Recvd %d responses for %d requests sent\n",recvd,to_send); + if(batchMode) + shutdownThisThing=1; + } +} + +void my_strings_completion(int rc, const struct String_vector *strings, + const void *data) { + struct timeval tv; + int sec; + int usec; + int i; + + gettimeofday(&tv, 0); + sec = tv.tv_sec - startTime.tv_sec; + usec = tv.tv_usec - startTime.tv_usec; + fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000); + fprintf(stderr, "%s: rc = %d\n", (char*)data, rc); + if (strings) + for (i=0; i < strings->count; i++) { + fprintf(stderr, "\t%s\n", strings->data[i]); + } + free((void*)data); + gettimeofday(&tv, 0); + sec = tv.tv_sec - startTime.tv_sec; + usec = tv.tv_usec - startTime.tv_usec; + fprintf(stderr, "time = %d msec\n", sec*1000 + usec/1000); + if(batchMode) + shutdownThisThing=1; +} + +void my_strings_stat_completion(int rc, const struct String_vector *strings, + const struct Stat *stat, const void *data) { + my_strings_completion(rc, strings, data); + dumpStat(stat); + if(batchMode) + shutdownThisThing=1; +} + +void my_void_completion(int rc, const void *data) { + fprintf(stderr, "%s: rc = %d\n", (char*)data, rc); + free((void*)data); + if(batchMode) + shutdownThisThing=1; +} + +void my_stat_completion(int rc, const struct Stat *stat, const void *data) { + fprintf(stderr, "%s: rc = %d Stat:\n", (char*)data, rc); + dumpStat(stat); + free((void*)data); + if(batchMode) + shutdownThisThing=1; +} + +void my_silent_stat_completion(int rc, const struct Stat *stat, + const void *data) { + // fprintf(stderr, "State completion: [%s] rc = %d\n", (char*)data, rc); + sent++; + free((void*)data); +} + +static void sendRequest(const char* data) { + zoo_aset(zh, "/od", data, strlen(data), -1, my_silent_stat_completion, + strdup("/od")); + zoo_aget(zh, "/od", 1, my_silent_data_completion, strdup("/od")); +} + +void od_completion(int rc, const struct Stat *stat, const void *data) { + int i; + fprintf(stderr, "od command response: rc = %d Stat:\n", rc); + dumpStat(stat); + // send a whole bunch of requests + recvd=0; + sent=0; + to_send=200; + for (i=0; i\n"); + fprintf(stderr, " create2 [+[e|s|c|t=ttl]] \n"); + fprintf(stderr, " delete \n"); + fprintf(stderr, " set \n"); + fprintf(stderr, " get \n"); + fprintf(stderr, " ls \n"); + fprintf(stderr, " ls2 \n"); + fprintf(stderr, " sync \n"); + fprintf(stderr, " exists \n"); + fprintf(stderr, " wexists \n"); + fprintf(stderr, " myid\n"); + fprintf(stderr, " verbose\n"); + fprintf(stderr, " addauth \n"); + fprintf(stderr, " config\n"); + fprintf(stderr, " reconfig [-file | -members ,... | " + " -add ,... | -remove ,...] [-version ]\n"); + fprintf(stderr, " quit\n"); + fprintf(stderr, "\n"); + fprintf(stderr, " prefix the command with the character 'a' to run the command asynchronously.\n"); + fprintf(stderr, " run the 'verbose' command to toggle verbose logging.\n"); + fprintf(stderr, " i.e. 'aget /foo' to get /foo asynchronously\n"); + } else if (startsWith(line, "verbose")) { + if (verbose) { + verbose = 0; + zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); + fprintf(stderr, "logging level set to WARN\n"); + } else { + verbose = 1; + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + fprintf(stderr, "logging level set to DEBUG\n"); + } + } else if (startsWith(line, "get ")) { + line += 4; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + + rc = zoo_aget(zh, line, 1, my_data_completion, strdup(line)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (strcmp(line, "config") == 0) { + gettimeofday(&startTime, 0); + rc = zoo_agetconfig(zh, 1, my_data_completion, strdup(ZOO_CONFIG_NODE)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "reconfig ")) { + int syntaxError = 0; + char* p = NULL; + char* joining = NULL; + char* leaving = NULL; + char* members = NULL; + size_t members_size = 0; + + int mode = 0; // 0 = not set, 1 = incremental, 2 = non-incremental + int64_t version = -1; + + line += 9; + p = strtok (strdup(line)," "); + while (p != NULL) { + if (strcmp(p, "-add")==0) { + p = strtok (NULL," "); + if (mode == 2 || p == NULL) { + syntaxError = 1; + break; + } + mode = 1; + joining = strdup(p); + } else if (strcmp(p, "-remove")==0){ + p = strtok (NULL," "); + if (mode == 2 || p == NULL) { + syntaxError = 1; + break; + } + mode = 1; + leaving = strdup(p); + } else if (strcmp(p, "-members")==0) { + p = strtok (NULL," "); + if (mode == 1 || p == NULL) { + syntaxError = 1; + break; + } + mode = 2; + members = strdup(p); + } else if (strcmp(p, "-file")==0){ + FILE *fp = NULL; + p = strtok (NULL," "); + if (mode == 1 || p == NULL) { + syntaxError = 1; + break; + } + mode = 2; + fp = fopen(p, "r"); + if (fp == NULL) { + fprintf(stderr, "Error reading file: %s\n", p); + syntaxError = 1; + break; + } + fseek(fp, 0L, SEEK_END); /* Position to end of file */ + members_size = ftell(fp); /* Get file length */ + rewind(fp); /* Back to start of file */ + members = calloc(members_size + 1, sizeof(char)); + if(members == NULL ) + { + fprintf(stderr, "\nInsufficient memory to read file: %s\n", p); + syntaxError = 1; + fclose(fp); + break; + } + + /* Read the entire file into members + * NOTE: -- fread returns number of items successfully read + * not the number of bytes. We're requesting one item of + * members_size bytes. So we expect the return value here + * to be 1. + */ + if (fread(members, members_size, 1, fp) != 1){ + fprintf(stderr, "Error reading file: %s\n", p); + syntaxError = 1; + fclose(fp); + break; + } + fclose(fp); + } else if (strcmp(p, "-version")==0){ + p = strtok (NULL," "); + if (version != -1 || p == NULL){ + syntaxError = 1; + break; + } +#ifdef WIN32 + version = _strtoui64(p, NULL, 16); +#else + version = strtoull(p, NULL, 16); +#endif + if (version < 0) { + syntaxError = 1; + break; + } + } else { + syntaxError = 1; + break; + } + p = strtok (NULL," "); + } + if (syntaxError) return; + + rc = zoo_areconfig(zh, joining, leaving, members, version, my_data_completion, strdup(line)); + free(joining); + free(leaving); + free(members); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + + } else if (startsWith(line, "set ")) { + char *ptr; + line += 4; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + ptr = strchr(line, ' '); + if (!ptr) { + fprintf(stderr, "No data found after path\n"); + return; + } + *ptr = '\0'; + ptr++; + rc = zoo_aset(zh, line, ptr, strlen(ptr), -1, my_stat_completion, + strdup(line)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "ls ")) { + line += 3; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + gettimeofday(&startTime, 0); + rc= zoo_aget_children(zh, line, 1, my_strings_completion, strdup(line)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "ls2 ")) { + line += 4; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + gettimeofday(&startTime, 0); + rc= zoo_aget_children2(zh, line, 1, my_strings_stat_completion, strdup(line)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "create ") || startsWith(line, "create2 ")) { + int mode = 0; + int64_t ttl_value = -1; + int is_create2 = startsWith(line, "create2 "); + line += is_create2 ? 8 : 7; + + if (line[0] == '+') { + int ephemeral = 0; + int sequential = 0; + int container = 0; + int ttl = 0; + char *p = NULL; + + line++; + + while (*line != ' ' && *line != '\0') { + switch (*line) { + case 'e': + ephemeral = 1; + break; + case 's': + sequential = 1; + break; + case 'c': + container = 1; + break; + case 't': + ttl = 1; + + line++; + + if (*line != '=') { + fprintf(stderr, "Missing ttl value after +t\n"); + return; + } + + line++; + + ttl_value = strtol(line, &p, 10); + + if (ttl_value <= 0) { + fprintf(stderr, "ttl value must be a positive integer\n"); + return; + } + + // move back line pointer to the last digit + line = p - 1; + + break; + default: + fprintf(stderr, "Unknown option: %c\n", *line); + return; + } + + line++; + } + + if (ephemeral != 0 && sequential == 0 && container == 0 && ttl == 0) { + mode = ZOO_EPHEMERAL; + } else if (ephemeral == 0 && sequential != 0 && container == 0 && ttl == 0) { + mode = ZOO_PERSISTENT_SEQUENTIAL; + } else if (ephemeral != 0 && sequential != 0 && container == 0 && ttl == 0) { + mode = ZOO_EPHEMERAL_SEQUENTIAL; + } else if (ephemeral == 0 && sequential == 0 && container != 0 && ttl == 0) { + mode = ZOO_CONTAINER; + } else if (ephemeral == 0 && sequential == 0 && container == 0 && ttl != 0) { + mode = ZOO_PERSISTENT_WITH_TTL; + } else if (ephemeral == 0 && sequential != 0 && container == 0 && ttl != 0) { + mode = ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL; + } else { + fprintf(stderr, "Invalid mode.\n"); + return; + } + + if (*line == ' ') { + line++; + } + } + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + fprintf(stderr, "Creating [%s] node (mode: %d)\n", line, mode); +// { +// struct ACL _CREATE_ONLY_ACL_ACL[] = {{ZOO_PERM_CREATE, ZOO_ANYONE_ID_UNSAFE}}; +// struct ACL_vector CREATE_ONLY_ACL = {1,_CREATE_ONLY_ACL_ACL}; +// rc = zoo_acreate(zh, line, "new", 3, &CREATE_ONLY_ACL, flags, +// my_string_completion, strdup(line)); +// } + if (is_create2) { + rc = zoo_acreate2_ttl(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, mode, ttl_value, + my_string_stat_completion_free_data, strdup(line)); + } else { + rc = zoo_acreate_ttl(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, mode, ttl_value, + my_string_completion_free_data, strdup(line)); + } + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "delete ")) { + line += 7; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + rc = zoo_adelete(zh, line, -1, my_void_completion, strdup(line)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "sync ")) { + line += 5; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } + rc = zoo_async(zh, line, my_string_completion_free_data, strdup(line)); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "wexists ")) { +#ifdef THREADED + struct Stat stat; +#endif + line += 8; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } +#ifndef THREADED + rc = zoo_awexists(zh, line, watcher, (void*) 0, my_stat_completion, strdup(line)); +#else + rc = zoo_wexists(zh, line, watcher, (void*) 0, &stat); +#endif + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (startsWith(line, "exists ")) { +#ifdef THREADED + struct Stat stat; +#endif + line += 7; + if (line[0] != '/') { + fprintf(stderr, "Path must start with /, found: %s\n", line); + return; + } +#ifndef THREADED + rc = zoo_aexists(zh, line, 1, my_stat_completion, strdup(line)); +#else + rc = zoo_exists(zh, line, 1, &stat); +#endif + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, line); + } + } else if (strcmp(line, "myid") == 0) { + printf("session Id = %llx\n", _LL_CAST_ zoo_client_id(zh)->client_id); + } else if (strcmp(line, "reinit") == 0) { + zookeeper_close(zh); + // we can't send myid to the server here -- zookeeper_close() removes + // the session on the server. We must start anew. + zh = zookeeper_init(hostPort, watcher, 30000, 0, 0, 0); + } else if (startsWith(line, "quit")) { + fprintf(stderr, "Quitting...\n"); + shutdownThisThing=1; + } else if (startsWith(line, "od")) { + const char val[]="fire off"; + fprintf(stderr, "Overdosing...\n"); + rc = zoo_aset(zh, "/od", val, sizeof(val)-1, -1, od_completion, 0); + if (rc) + fprintf(stderr, "od command failed: %d\n", rc); + } else if (startsWith(line, "addauth ")) { + char *ptr; + line += 8; + ptr = strchr(line, ' '); + if (ptr) { + *ptr = '\0'; + ptr++; + } + zoo_add_auth(zh, line, ptr, ptr ? strlen(ptr) : 0, NULL, NULL); + } +} + +/* + * Look for a command in the form 'cmd:command', and store a pointer + * to the command (without its prefix) into *buf if found. + * + * Returns 0 if the argument does not start with the prefix. + * Returns 1 in case of success. + */ +int handleBatchMode(const char* arg, const char** buf) { + size_t cmdlen = strlen(arg); + if (cmdlen < 4) { + // too short + return 0; + } + cmdlen -= 4; + if(strncmp("cmd:", arg, 4) != 0){ + return 0; + } + *buf = arg + 4; + return 1; +} + +#ifdef THREADED +static void millisleep(int ms) { +#ifdef WIN32 + Sleep(ms); +#else /* !WIN32 */ + struct timespec ts; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000; // to nanoseconds + nanosleep(&ts, NULL); +#endif /* WIN32 */ +} +#endif /* THREADED */ + +int main(int argc, char **argv) { + static struct option long_options[] = { + {"host", required_argument, NULL, 'h'}, //hostPort + {"ssl", required_argument, NULL, 's'}, //certificate files + {"myid", required_argument, NULL, 'm'}, //myId file + {"cmd", required_argument, NULL, 'c'}, //cmd + {"readonly", no_argument, NULL, 'r'}, //read-only + {"debug", no_argument, NULL, 'd'}, //set log level to DEBUG from the beginning + {NULL, 0, NULL, 0}, + }; +#ifndef THREADED + fd_set rfds, wfds, efds; + int processed=0; +#endif + char buffer[4096]; + char p[2048]; +#ifdef YCA + char *cert=0; + char appId[64]; +#endif + int bufoff = 0; + int flags; + FILE *fh; + + int opt; + int option_index = 0; + + verbose = 0; + zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); + + flags = 0; + while ((opt = getopt_long(argc, argv, "h:s:m:c:rd", long_options, &option_index)) != -1) { + switch (opt) { + case 'h': + hostPort = optarg; + break; + case 'm': + clientIdFile = optarg; + break; + case 'r': + flags = ZOO_READONLY; + break; + case 'c': + cmd = optarg; + batchMode = 1; + fprintf(stderr,"Batch mode: %s\n",cmd); + break; + case 's': + cert = optarg; + break; + case 'd': + verbose = 1; + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + fprintf(stderr, "logging level set to DEBUG\n"); + break; + case '?': + if (optopt == 'h') { + fprintf (stderr, "Option -%c requires host list.\n", optopt); + } else if (isprint (optopt)) { + fprintf (stderr, "Unknown option `-%c'.\n", optopt); + } else { + fprintf (stderr, + "Unknown option character `\\x%x'.\n", + optopt); + return 1; + } + } + } + + if (!hostPort && optind < argc) { + /* + * getopt_long did not find a '-h ' option. + * + * The invoker may be using using the "old-style" command + * syntax, with positional parameters and "magical" prefixes + * such as 'cmd:'; let's see if we can make sense of it. + */ + hostPort = argv[optind++]; + + if (optind < argc && !cmd && !clientIdFile) { + int batchModeRes = handleBatchMode(argv[optind], &cmd); + if (batchModeRes == 1) { + batchMode=1; + fprintf(stderr, "Batch mode: '%s'\n", cmd); + } else { + clientIdFile = argv[optind]; + } + + optind++; + } + } + + if (!hostPort || optind < argc) { + fprintf(stderr, + "\nUSAGE: %s -h zk_host_1:port_1,zk_host_2:port_2,... [OPTIONAL ARGS]\n\n" + "MANDATORY ARGS:\n" + "-h, --host Comma separated list of ZooKeeper host:port pairs\n\n" + "OPTIONAL ARGS:\n" + "-m, --myid Path to the file contains the client ID\n" + "-c, --cmd Command to execute, e.g. ls|ls2|create|create2|od|...\n" +#ifdef HAVE_OPENSSL_H + "-s, --ssl Comma separated parameters to initiate SSL connection\n" + " e.g.: server_cert.crt,client_cert.crt,client_priv_key.pem,passwd\n" +#endif + "-r, --readonly Connect in read-only mode\n" + "-d, --debug Activate debug logs right from the beginning (you can also use the \n" + " command 'verbose' later to activate debug logs in the cli shell)\n\n", + argv[0]); + fprintf(stderr, + "Version: ZooKeeper cli (c client) version %s\n", + ZOO_VERSION); + return 2; + } + + if (clientIdFile) { + fh = fopen(clientIdFile, "r"); + if (fh) { + if (fread(&myid, sizeof(myid), 1, fh) != sizeof(myid)) { + memset(&myid, 0, sizeof(myid)); + } + fclose(fh); + } + } + +#ifdef YCA + strcpy(appId,"yahoo.example.yca_test"); + cert = yca_get_cert_once(appId); + if(cert!=0) { + fprintf(stderr,"Certificate for appid [%s] is [%s]\n",appId,cert); + strncpy(p,cert,sizeof(p)-1); + free(cert); + } else { + fprintf(stderr,"Certificate for appid [%s] not found\n",appId); + strcpy(p,"dummy"); + } +#else + strcpy(p, "dummy"); +#endif + zoo_deterministic_conn_order(1); // enable deterministic order + +#ifdef HAVE_OPENSSL_H + if (!cert) { + zh = zookeeper_init(hostPort, watcher, 30000, &myid, NULL, flags); + } else { + zh = zookeeper_init_ssl(hostPort, cert, watcher, 30000, &myid, NULL, flags); + } +#else + zh = zookeeper_init(hostPort, watcher, 30000, &myid, NULL, flags); +#endif + + if (!zh) { + return errno; + } + +#ifdef YCA + if(zoo_add_auth(zh,"yca",p,strlen(p),0,0)!=ZOK) + return 2; +#endif + +#ifdef THREADED + if (batchMode) { + processline(cmd); + } + while(!shutdownThisThing) { + int rc, len; + if (batchMode) { + // We are just waiting for the asynchronous command to complete. + millisleep(10); + continue; + } + len = sizeof(buffer) - bufoff -1; + if (len <= 0) { + fprintf(stderr, "Can't handle lines that long!\n"); + exit(2); + } + rc = read(0, buffer+bufoff, len); + if (rc <= 0) { + fprintf(stderr, "bye\n"); + shutdownThisThing=1; + break; + } + bufoff += rc; + buffer[bufoff] = '\0'; + while (strchr(buffer, '\n')) { + char *ptr = strchr(buffer, '\n'); + *ptr = '\0'; + processline(buffer); + ptr++; + memmove(buffer, ptr, strlen(ptr)+1); + bufoff = 0; + } + } +#else + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&efds); + while (!shutdownThisThing) { + int fd; + int interest; + int events; + struct timeval tv; + int rc; + zookeeper_interest(zh, &fd, &interest, &tv); + if (fd != -1) { + if (interest&ZOOKEEPER_READ) { + FD_SET(fd, &rfds); + } else { + FD_CLR(fd, &rfds); + } + if (interest&ZOOKEEPER_WRITE) { + FD_SET(fd, &wfds); + } else { + FD_CLR(fd, &wfds); + } + } else { + fd = 0; + } + FD_SET(0, &rfds); + rc = select(fd+1, &rfds, &wfds, &efds, &tv); + events = 0; + if (rc > 0) { + if (FD_ISSET(fd, &rfds)) { + events |= ZOOKEEPER_READ; + } + if (FD_ISSET(fd, &wfds)) { + events |= ZOOKEEPER_WRITE; + } + } + if(batchMode && processed==0){ + //batch mode + processline(cmd); + processed=1; + } + if (!processed && FD_ISSET(0, &rfds)) { + int rc; + int len = sizeof(buffer) - bufoff -1; + if (len <= 0) { + fprintf(stderr, "Can't handle lines that long!\n"); + exit(2); + } + rc = read(0, buffer+bufoff, len); + if (rc <= 0) { + fprintf(stderr, "bye\n"); + break; + } + bufoff += rc; + buffer[bufoff] = '\0'; + while (strchr(buffer, '\n')) { + char *ptr = strchr(buffer, '\n'); + *ptr = '\0'; + processline(buffer); + ptr++; + memmove(buffer, ptr, strlen(ptr)+1); + bufoff = 0; + } + } + zookeeper_process(zh, events); + } +#endif + if (to_send!=0) + fprintf(stderr,"Recvd %d responses for %d requests sent\n",recvd,sent); + zookeeper_close(zh); + return 0; +} diff --git a/zookeeper-client/zookeeper-client-c/src/hashtable/LICENSE.txt b/zookeeper-client/zookeeper-client-c/src/hashtable/LICENSE.txt new file mode 100644 index 0000000..674a624 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/hashtable/LICENSE.txt @@ -0,0 +1,30 @@ +Copyright (c) 2002, 2004, Christopher Clark +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + + * Neither the name of the original author; nor the names of any contributors +may be used to endorse or promote products derived from this software +without specific prior written permission. + + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable.c b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable.c new file mode 100644 index 0000000..caa5f6c --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable.c @@ -0,0 +1,274 @@ +/* Copyright (C) 2004 Christopher Clark */ + +#include "hashtable.h" +#include "hashtable_private.h" +#include +#include +#include +#include + +/* +Credit for primes table: Aaron Krowne + http://br.endernet.org/~akrowne/ + http://planetmath.org/encyclopedia/GoodHashTablePrimes.html +*/ +static const unsigned int primes[] = { +53, 97, 193, 389, +769, 1543, 3079, 6151, +12289, 24593, 49157, 98317, +196613, 393241, 786433, 1572869, +3145739, 6291469, 12582917, 25165843, +50331653, 100663319, 201326611, 402653189, +805306457, 1610612741 +}; +const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]); +const float max_load_factor = 0.65; + +/*****************************************************************************/ +struct hashtable * +create_hashtable(unsigned int minsize, + unsigned int (*hashf) (void*), + int (*eqf) (void*,void*)) +{ + struct hashtable *h; + unsigned int pindex, size = primes[0]; + /* Check requested hashtable isn't too large */ + if (minsize > (1u << 30)) return NULL; + /* Enforce size as prime */ + for (pindex=0; pindex < prime_table_length; pindex++) { + if (primes[pindex] > minsize) { size = primes[pindex]; break; } + } + h = (struct hashtable *)malloc(sizeof(struct hashtable)); + if (NULL == h) return NULL; /*oom*/ + h->table = (struct entry **)malloc(sizeof(struct entry*) * size); + if (NULL == h->table) { free(h); return NULL; } /*oom*/ + memset(h->table, 0, size * sizeof(struct entry *)); + h->tablelength = size; + h->primeindex = pindex; + h->entrycount = 0; + h->hashfn = hashf; + h->eqfn = eqf; + h->loadlimit = (unsigned int) ceil(size * max_load_factor); + return h; +} + +/*****************************************************************************/ +unsigned int +hash(struct hashtable *h, void *k) +{ + /* Aim to protect against poor hash functions by adding logic here + * - logic taken from java 1.4 hashtable source */ + unsigned int i = h->hashfn(k); + i += ~(i << 9); + i ^= ((i >> 14) | (i << 18)); /* >>> */ + i += (i << 4); + i ^= ((i >> 10) | (i << 22)); /* >>> */ + return i; +} + +/*****************************************************************************/ +static int +hashtable_expand(struct hashtable *h) +{ + /* Double the size of the table to accomodate more entries */ + struct entry **newtable; + struct entry *e; + struct entry **pE; + unsigned int newsize, i, index; + /* Check we're not hitting max capacity */ + if (h->primeindex == (prime_table_length - 1)) return 0; + newsize = primes[++(h->primeindex)]; + + newtable = (struct entry **)malloc(sizeof(struct entry*) * newsize); + if (NULL != newtable) + { + memset(newtable, 0, newsize * sizeof(struct entry *)); + /* This algorithm is not 'stable'. ie. it reverses the list + * when it transfers entries between the tables */ + for (i = 0; i < h->tablelength; i++) { + while (NULL != (e = h->table[i])) { + h->table[i] = e->next; + index = indexFor(newsize,e->h); + e->next = newtable[index]; + newtable[index] = e; + } + } + free(h->table); + h->table = newtable; + } + /* Plan B: realloc instead */ + else + { + newtable = (struct entry **) + realloc(h->table, newsize * sizeof(struct entry *)); + if (NULL == newtable) { (h->primeindex)--; return 0; } + h->table = newtable; + memset(newtable[h->tablelength], 0, newsize - h->tablelength); + for (i = 0; i < h->tablelength; i++) { + for (pE = &(newtable[i]), e = *pE; e != NULL; e = *pE) { + index = indexFor(newsize,e->h); + if (index == i) + { + pE = &(e->next); + } + else + { + *pE = e->next; + e->next = newtable[index]; + newtable[index] = e; + } + } + } + } + h->tablelength = newsize; + h->loadlimit = (unsigned int) ceil(newsize * max_load_factor); + return -1; +} + +/*****************************************************************************/ +unsigned int +hashtable_count(struct hashtable *h) +{ + return h->entrycount; +} + +/*****************************************************************************/ +int +hashtable_insert(struct hashtable *h, void *k, void *v) +{ + /* This method allows duplicate keys - but they shouldn't be used */ + unsigned int index; + struct entry *e; + if (++(h->entrycount) > h->loadlimit) + { + /* Ignore the return value. If expand fails, we should + * still try cramming just this value into the existing table + * -- we may not have memory for a larger table, but one more + * element may be ok. Next time we insert, we'll try expanding again.*/ + hashtable_expand(h); + } + e = (struct entry *)malloc(sizeof(struct entry)); + if (NULL == e) { --(h->entrycount); return 0; } /*oom*/ + e->h = hash(h,k); + index = indexFor(h->tablelength,e->h); + e->k = k; + e->v = v; + e->next = h->table[index]; + h->table[index] = e; + return -1; +} + +/*****************************************************************************/ +void * /* returns value associated with key */ +hashtable_search(struct hashtable *h, void *k) +{ + struct entry *e; + unsigned int hashvalue, index; + hashvalue = hash(h,k); + index = indexFor(h->tablelength,hashvalue); + e = h->table[index]; + while (NULL != e) + { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) return e->v; + e = e->next; + } + return NULL; +} + +/*****************************************************************************/ +void * /* returns value associated with key */ +hashtable_remove(struct hashtable *h, void *k) +{ + /* TODO: consider compacting the table when the load factor drops enough, + * or provide a 'compact' method. */ + + struct entry *e; + struct entry **pE; + void *v; + unsigned int hashvalue, index; + + hashvalue = hash(h,k); + index = indexFor(h->tablelength, hashvalue); + pE = &(h->table[index]); + e = *pE; + while (NULL != e) + { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) + { + *pE = e->next; + h->entrycount--; + v = e->v; + freekey(e->k); + free(e); + return v; + } + pE = &(e->next); + e = e->next; + } + return NULL; +} + +/*****************************************************************************/ +/* destroy */ +void +hashtable_destroy(struct hashtable *h, int free_values) +{ + unsigned int i; + struct entry *e, *f; + struct entry **table = h->table; + if (free_values) + { + for (i = 0; i < h->tablelength; i++) + { + e = table[i]; + while (NULL != e) + { f = e; e = e->next; freekey(f->k); free(f->v); free(f); } + } + } + else + { + for (i = 0; i < h->tablelength; i++) + { + e = table[i]; + while (NULL != e) + { f = e; e = e->next; freekey(f->k); free(f); } + } + } + free(h->table); + free(h); +} + +/* + * Copyright (c) 2002, Christopher Clark + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ diff --git a/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable.h b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable.h new file mode 100644 index 0000000..cbead18 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable.h @@ -0,0 +1,209 @@ +/* Copyright (C) 2002 Christopher Clark */ + +#ifndef __HASHTABLE_CWC22_H__ +#define __HASHTABLE_CWC22_H__ +#ifdef WIN32 +#include "winconfig.h" +#endif +#ifdef __cplusplus +extern "C" { +#endif + +struct hashtable; + +/* Example of use: + * + * struct hashtable *h; + * struct some_key *k; + * struct some_value *v; + * + * static unsigned int hash_from_key_fn( void *k ); + * static int keys_equal_fn ( void *key1, void *key2 ); + * + * h = create_hashtable(16, hash_from_key_fn, keys_equal_fn); + * k = (struct some_key *) malloc(sizeof(struct some_key)); + * v = (struct some_value *) malloc(sizeof(struct some_value)); + * + * (initialise k and v to suitable values) + * + * if (! hashtable_insert(h,k,v) ) + * { exit(-1); } + * + * if (NULL == (found = hashtable_search(h,k) )) + * { printf("not found!"); } + * + * if (NULL == (found = hashtable_remove(h,k) )) + * { printf("Not found\n"); } + * + */ + +/* Macros may be used to define type-safe(r) hashtable access functions, with + * methods specialized to take known key and value types as parameters. + * + * Example: + * + * Insert this at the start of your file: + * + * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value); + * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value); + * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value); + * + * This defines the functions 'insert_some', 'search_some' and 'remove_some'. + * These operate just like hashtable_insert etc., with the same parameters, + * but their function signatures have 'struct some_key *' rather than + * 'void *', and hence can generate compile time errors if your program is + * supplying incorrect data as a key (and similarly for value). + * + * Note that the hash and key equality functions passed to create_hashtable + * still take 'void *' parameters instead of 'some key *'. This shouldn't be + * a difficult issue as they're only defined and passed once, and the other + * functions will ensure that only valid keys are supplied to them. + * + * The cost for this checking is increased code size and runtime overhead + * - if performance is important, it may be worth switching back to the + * unsafe methods once your program has been debugged with the safe methods. + * This just requires switching to some simple alternative defines - eg: + * #define insert_some hashtable_insert + * + */ + +/***************************************************************************** + * create_hashtable + + * @name create_hashtable + * @param minsize minimum initial size of hashtable + * @param hashfunction function for hashing keys + * @param key_eq_fn function for determining key equality + * @return newly created hashtable or NULL on failure + */ + +struct hashtable * +create_hashtable(unsigned int minsize, + unsigned int (*hashfunction) (void*), + int (*key_eq_fn) (void*,void*)); + +/***************************************************************************** + * hashtable_insert + + * @name hashtable_insert + * @param h the hashtable to insert into + * @param k the key - hashtable claims ownership and will free on removal + * @param v the value - does not claim ownership + * @return non-zero for successful insertion + * + * This function will cause the table to expand if the insertion would take + * the ratio of entries to table size over the maximum load factor. + * + * This function does not check for repeated insertions with a duplicate key. + * The value returned when using a duplicate key is undefined -- when + * the hashtable changes size, the order of retrieval of duplicate key + * entries is reversed. + * If in doubt, remove before insert. + */ + +int +hashtable_insert(struct hashtable *h, void *k, void *v); + +#define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \ +int fnname (struct hashtable *h, keytype *k, valuetype *v) \ +{ \ + return hashtable_insert(h,k,v); \ +} + +/***************************************************************************** + * hashtable_search + + * @name hashtable_search + * @param h the hashtable to search + * @param k the key to search for - does not claim ownership + * @return the value associated with the key, or NULL if none found + */ + +void * +hashtable_search(struct hashtable *h, void *k); + +#define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \ +valuetype * fnname (struct hashtable *h, keytype *k) \ +{ \ + return (valuetype *) (hashtable_search(h,k)); \ +} + +/***************************************************************************** + * hashtable_remove + + * @name hashtable_remove + * @param h the hashtable to remove the item from + * @param k the key to search for - does not claim ownership + * @return the value associated with the key, or NULL if none found + */ + +void * /* returns value */ +hashtable_remove(struct hashtable *h, void *k); + +#define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \ +valuetype * fnname (struct hashtable *h, keytype *k) \ +{ \ + return (valuetype *) (hashtable_remove(h,k)); \ +} + + +/***************************************************************************** + * hashtable_count + + * @name hashtable_count + * @param h the hashtable + * @return the number of items stored in the hashtable + */ +unsigned int +hashtable_count(struct hashtable *h); + + +/***************************************************************************** + * hashtable_destroy + + * @name hashtable_destroy + * @param h the hashtable + * @param free_values whether to call 'free' on the remaining values + */ + +void +hashtable_destroy(struct hashtable *h, int free_values); + +#ifdef __cplusplus +} +#endif + +#endif /* __HASHTABLE_CWC22_H__ */ + +/* + * Copyright (c) 2002, Christopher Clark + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ diff --git a/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_itr.c b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_itr.c new file mode 100644 index 0000000..defac69 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_itr.c @@ -0,0 +1,176 @@ +/* Copyright (C) 2002, 2004 Christopher Clark */ + +#include "hashtable.h" +#include "hashtable_private.h" +#include "hashtable_itr.h" +#include /* defines NULL */ + +/*****************************************************************************/ +/* hashtable_iterator - iterator constructor */ + +struct hashtable_itr * +hashtable_iterator(struct hashtable *h) +{ + unsigned int i, tablelength; + struct hashtable_itr *itr = (struct hashtable_itr *) + malloc(sizeof(struct hashtable_itr)); + if (NULL == itr) return NULL; + itr->h = h; + itr->e = NULL; + itr->parent = NULL; + tablelength = h->tablelength; + itr->index = tablelength; + if (0 == h->entrycount) return itr; + + for (i = 0; i < tablelength; i++) + { + if (NULL != h->table[i]) + { + itr->e = h->table[i]; + itr->index = i; + break; + } + } + return itr; +} + +/*****************************************************************************/ +/* advance - advance the iterator to the next element + * returns zero if advanced to end of table */ + +int +hashtable_iterator_advance(struct hashtable_itr *itr) +{ + unsigned int j,tablelength; + struct entry **table; + struct entry *next; + if (NULL == itr->e) return 0; /* stupidity check */ + + next = itr->e->next; + if (NULL != next) + { + itr->parent = itr->e; + itr->e = next; + return -1; + } + tablelength = itr->h->tablelength; + itr->parent = NULL; + if (tablelength <= (j = ++(itr->index))) + { + itr->e = NULL; + return 0; + } + table = itr->h->table; + while (NULL == (next = table[j])) + { + if (++j >= tablelength) + { + itr->index = tablelength; + itr->e = NULL; + return 0; + } + } + itr->index = j; + itr->e = next; + return -1; +} + +/*****************************************************************************/ +/* remove - remove the entry at the current iterator position + * and advance the iterator, if there is a successive + * element. + * If you want the value, read it before you remove: + * beware memory leaks if you don't. + * Returns zero if end of iteration. */ + +int +hashtable_iterator_remove(struct hashtable_itr *itr) +{ + struct entry *remember_e, *remember_parent; + int ret; + + /* Do the removal */ + if (NULL == (itr->parent)) + { + /* element is head of a chain */ + itr->h->table[itr->index] = itr->e->next; + } else { + /* element is mid-chain */ + itr->parent->next = itr->e->next; + } + /* itr->e is now outside the hashtable */ + remember_e = itr->e; + itr->h->entrycount--; + freekey(remember_e->k); + + /* Advance the iterator, correcting the parent */ + remember_parent = itr->parent; + ret = hashtable_iterator_advance(itr); + if (itr->parent == remember_e) { itr->parent = remember_parent; } + free(remember_e); + return ret; +} + +/*****************************************************************************/ +int /* returns zero if not found */ +hashtable_iterator_search(struct hashtable_itr *itr, + struct hashtable *h, void *k) +{ + struct entry *e, *parent; + unsigned int hashvalue, index; + + hashvalue = hash(h,k); + index = indexFor(h->tablelength,hashvalue); + + e = h->table[index]; + parent = NULL; + while (NULL != e) + { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) + { + itr->index = index; + itr->e = e; + itr->parent = parent; + itr->h = h; + return -1; + } + parent = e; + e = e->next; + } + return 0; +} + + +/* + * Copyright (c) 2002, 2004, Christopher Clark + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ diff --git a/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_itr.h b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_itr.h new file mode 100644 index 0000000..30379c7 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_itr.h @@ -0,0 +1,119 @@ +/* Copyright (C) 2002, 2004 Christopher Clark */ + +#ifndef __HASHTABLE_ITR_CWC22__ +#define __HASHTABLE_ITR_CWC22__ +#include "hashtable.h" +#include "hashtable_private.h" /* needed to enable inlining */ + +#ifdef __cplusplus +extern "C" { +#endif + +/*****************************************************************************/ +/* This struct is only concrete here to allow the inlining of two of the + * accessor functions. */ +struct hashtable_itr +{ + struct hashtable *h; + struct entry *e; + struct entry *parent; + unsigned int index; +}; + + +/*****************************************************************************/ +/* hashtable_iterator + */ + +struct hashtable_itr * +hashtable_iterator(struct hashtable *h); + +/*****************************************************************************/ +/* hashtable_iterator_key + * - return the value of the (key,value) pair at the current position */ + +static inline void * +hashtable_iterator_key(struct hashtable_itr *i) +{ + return i->e->k; +} + +/*****************************************************************************/ +/* value - return the value of the (key,value) pair at the current position */ + +static inline void * +hashtable_iterator_value(struct hashtable_itr *i) +{ + return i->e->v; +} + +/*****************************************************************************/ +/* advance - advance the iterator to the next element + * returns zero if advanced to end of table */ + +int +hashtable_iterator_advance(struct hashtable_itr *itr); + +/*****************************************************************************/ +/* remove - remove current element and advance the iterator to the next element + * NB: if you need the value to free it, read it before + * removing. ie: beware memory leaks! + * returns zero if advanced to end of table */ + +int +hashtable_iterator_remove(struct hashtable_itr *itr); + +/*****************************************************************************/ +/* search - overwrite the supplied iterator, to point to the entry + * matching the supplied key. + h points to the hashtable to be searched. + * returns zero if not found. */ +int +hashtable_iterator_search(struct hashtable_itr *itr, + struct hashtable *h, void *k); + +#define DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \ +int fnname (struct hashtable_itr *i, struct hashtable *h, keytype *k) \ +{ \ + return (hashtable_iterator_search(i,h,k)); \ +} + + +#ifdef __cplusplus +} +#endif + +#endif /* __HASHTABLE_ITR_CWC22__*/ + +/* + * Copyright (c) 2002, 2004, Christopher Clark + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ diff --git a/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_private.h b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_private.h new file mode 100644 index 0000000..3e95f60 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/hashtable/hashtable_private.h @@ -0,0 +1,85 @@ +/* Copyright (C) 2002, 2004 Christopher Clark */ + +#ifndef __HASHTABLE_PRIVATE_CWC22_H__ +#define __HASHTABLE_PRIVATE_CWC22_H__ + +#include "hashtable.h" + +/*****************************************************************************/ +struct entry +{ + void *k, *v; + unsigned int h; + struct entry *next; +}; + +struct hashtable { + unsigned int tablelength; + struct entry **table; + unsigned int entrycount; + unsigned int loadlimit; + unsigned int primeindex; + unsigned int (*hashfn) (void *k); + int (*eqfn) (void *k1, void *k2); +}; + +/*****************************************************************************/ +unsigned int +hash(struct hashtable *h, void *k); + +/*****************************************************************************/ +/* indexFor */ +static inline unsigned int +indexFor(unsigned int tablelength, unsigned int hashvalue) { + return (hashvalue % tablelength); +}; + +/* Only works if tablelength == 2^N */ +/*static inline unsigned int +indexFor(unsigned int tablelength, unsigned int hashvalue) +{ + return (hashvalue & (tablelength - 1u)); +} +*/ + +/*****************************************************************************/ +#define freekey(X) free(X) +/*define freekey(X) ; */ + + +/*****************************************************************************/ + +#endif /* __HASHTABLE_PRIVATE_CWC22_H__*/ + +/* + * Copyright (c) 2002, Christopher Clark + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ diff --git a/zookeeper-client/zookeeper-client-c/src/load_gen.c b/zookeeper-client/zookeeper-client-c/src/load_gen.c new file mode 100644 index 0000000..f25edcb --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/load_gen.c @@ -0,0 +1,280 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "zookeeper_log.h" +#include +#ifdef THREADED +#include +#endif +#include +#include + +static zhandle_t *zh; + +// ***************************************************************************** +// +static pthread_cond_t cond=PTHREAD_COND_INITIALIZER; +static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER; + +static pthread_cond_t counterCond=PTHREAD_COND_INITIALIZER; +static pthread_mutex_t counterLock=PTHREAD_MUTEX_INITIALIZER; +static int counter; + + + +void ensureConnected(){ + pthread_mutex_lock(&lock); + while (zoo_state(zh)!=ZOO_CONNECTED_STATE) { + pthread_cond_wait(&cond,&lock); + } + pthread_mutex_unlock(&lock); +} + +void incCounter(int delta){ + pthread_mutex_lock(&counterLock); + counter+=delta; + pthread_cond_broadcast(&counterCond); + pthread_mutex_unlock(&counterLock); + +} +void setCounter(int cnt){ + pthread_mutex_lock(&counterLock); + counter=cnt; + pthread_cond_broadcast(&counterCond); + pthread_mutex_unlock(&counterLock); + +} +void waitCounter(){ + pthread_mutex_lock(&counterLock); + while (counter>0) { + pthread_cond_wait(&counterCond,&counterLock); + } + pthread_mutex_unlock(&counterLock); +} + +void listener(zhandle_t *zzh, int type, int state, const char *path,void* ctx) { + if (type == ZOO_SESSION_EVENT) { + if (state == ZOO_CONNECTED_STATE || state == ZOO_READONLY_STATE) { + pthread_mutex_lock(&lock); + pthread_cond_broadcast(&cond); + pthread_mutex_unlock(&lock); + } + setCounter(0); + } +} + +void create_completion(int rc, const char *name, const void *data) { + incCounter(-1); + if(rc!=ZOK){ + LOG_ERROR(LOGSTREAM, "Failed to create a node rc=%d",rc); + } +} + +int doCreateNodes(const char* root, int count){ + char nodeName[1024]; + int i; + for(i=0; idata) { + int32_t i; + for(i=0;icount; i++) { + free(v->data[i]); + } + free(v->data); + v->data = 0; + } + return 0; +} + +static int deletedCounter; + +int recursiveDelete(const char* root){ + struct String_vector children; + int i; + int rc=zoo_get_children(zh,root,0,&children); + if(rc!=ZNONODE){ + if(rc!=ZOK){ + LOG_ERROR(LOGSTREAM, "Failed to get children of %s, rc=%d",root,rc); + return rc; + } + for(i=0;i +#include +#include +#include +#include +#include + +#ifndef WIN32 +#include +#include +#include +#include +#endif + +int zoo_lock_auth(zhandle_t *zh) +{ + return pthread_mutex_lock(&zh->auth_h.lock); +} +int zoo_unlock_auth(zhandle_t *zh) +{ + return pthread_mutex_unlock(&zh->auth_h.lock); +} +int lock_buffer_list(buffer_head_t *l) +{ + return pthread_mutex_lock(&l->lock); +} +int unlock_buffer_list(buffer_head_t *l) +{ + return pthread_mutex_unlock(&l->lock); +} +int lock_completion_list(completion_head_t *l) +{ + return pthread_mutex_lock(&l->lock); +} +int unlock_completion_list(completion_head_t *l) +{ + pthread_cond_broadcast(&l->cond); + return pthread_mutex_unlock(&l->lock); +} +struct sync_completion *alloc_sync_completion(void) +{ + struct sync_completion *sc = (struct sync_completion*)calloc(1, sizeof(struct sync_completion)); + if (sc) { + pthread_cond_init(&sc->cond, 0); + pthread_mutex_init(&sc->lock, 0); + } + return sc; +} +int wait_sync_completion(struct sync_completion *sc) +{ + pthread_mutex_lock(&sc->lock); + while (!sc->complete) { + pthread_cond_wait(&sc->cond, &sc->lock); + } + pthread_mutex_unlock(&sc->lock); + return 0; +} + +void free_sync_completion(struct sync_completion *sc) +{ + if (sc) { + pthread_mutex_destroy(&sc->lock); + pthread_cond_destroy(&sc->cond); + free(sc); + } +} + +void notify_sync_completion(struct sync_completion *sc) +{ + pthread_mutex_lock(&sc->lock); + sc->complete = 1; + pthread_cond_broadcast(&sc->cond); + pthread_mutex_unlock(&sc->lock); +} + +int process_async(int outstanding_sync) +{ + return 0; +} + +#ifdef WIN32 +unsigned __stdcall do_io( void * ); +unsigned __stdcall do_completion( void * ); + +int handle_error(zhandle_t* zh, SOCKET sock, char* message) +{ + LOG_ERROR(LOGCALLBACK(zh), "%s. %d",message, WSAGetLastError()); + closesocket (sock); + return -1; +} + +//--create socket pair for interupting selects. +int create_socket_pair(zhandle_t* zh, SOCKET fds[2]) +{ + struct sockaddr_in inaddr; + struct sockaddr addr; + int yes=1; + int len=0; + + SOCKET lst=socket(AF_INET, SOCK_STREAM,IPPROTO_TCP); + if (lst == INVALID_SOCKET ){ + LOG_ERROR(LOGCALLBACK(zh), "Error creating socket. %d",WSAGetLastError()); + return -1; + } + memset(&inaddr, 0, sizeof(inaddr)); + memset(&addr, 0, sizeof(addr)); + inaddr.sin_family = AF_INET; + inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + inaddr.sin_port = 0; //--system assigns the port + + if ( setsockopt(lst,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes)) == SOCKET_ERROR ) { + return handle_error(zh, lst,"Error trying to set socket option."); + } + if (bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr)) == SOCKET_ERROR){ + return handle_error(zh, lst,"Error trying to bind socket."); + } + if (listen(lst,1) == SOCKET_ERROR){ + return handle_error(zh, lst,"Error trying to listen on socket."); + } + len=sizeof(inaddr); + getsockname(lst, &addr,&len); + fds[0]=socket(AF_INET, SOCK_STREAM,0); + if (connect(fds[0],&addr,len) == SOCKET_ERROR){ + return handle_error(zh, lst, "Error while connecting to socket."); + } + if ((fds[1]=accept(lst,0,0)) == INVALID_SOCKET){ + closesocket(fds[0]); + return handle_error(zh, lst, "Error while accepting socket connection."); + } + closesocket(lst); + return 0; +} +#else +void *do_io(void *); +void *do_completion(void *); +#endif + + +int wakeup_io_thread(zhandle_t *zh); + +#ifdef WIN32 +static int set_nonblock(SOCKET fd){ + ULONG nonblocking_flag = 1; + if (ioctlsocket(fd, FIONBIO, &nonblocking_flag) == 0) + return 1; + else + return -1; +} +#else +static int set_nonblock(int fd){ + long l = fcntl(fd, F_GETFL); + if(l & O_NONBLOCK) return 0; + return fcntl(fd, F_SETFL, l | O_NONBLOCK); +} +#endif + +void wait_for_others(zhandle_t* zh) +{ + struct adaptor_threads* adaptor=zh->adaptor_priv; + pthread_mutex_lock(&adaptor->lock); + while(adaptor->threadsToWait>0) + pthread_cond_wait(&adaptor->cond,&adaptor->lock); + pthread_mutex_unlock(&adaptor->lock); +} + +void notify_thread_ready(zhandle_t* zh) +{ + struct adaptor_threads* adaptor=zh->adaptor_priv; + pthread_mutex_lock(&adaptor->lock); + adaptor->threadsToWait--; + pthread_cond_broadcast(&adaptor->cond); + while(adaptor->threadsToWait>0) + pthread_cond_wait(&adaptor->cond,&adaptor->lock); + pthread_mutex_unlock(&adaptor->lock); +} + + +void start_threads(zhandle_t* zh) +{ + int rc = 0; + struct adaptor_threads* adaptor=zh->adaptor_priv; + pthread_cond_init(&adaptor->cond,0); + pthread_mutex_init(&adaptor->lock,0); + adaptor->threadsToWait=2; // wait for 2 threads before opening the barrier + + // use api_prolog() to make sure zhandle doesn't get destroyed + // while initialization is in progress + api_prolog(zh); + LOG_DEBUG(LOGCALLBACK(zh), "starting threads..."); + rc=pthread_create(&adaptor->io, 0, do_io, zh); + assert("pthread_create() failed for the IO thread"&&!rc); + rc=pthread_create(&adaptor->completion, 0, do_completion, zh); + assert("pthread_create() failed for the completion thread"&&!rc); + wait_for_others(zh); + api_epilog(zh, 0); +} + +int adaptor_init(zhandle_t *zh) +{ + pthread_mutexattr_t recursive_mx_attr; + struct adaptor_threads *adaptor_threads = calloc(1, sizeof(*adaptor_threads)); + if (!adaptor_threads) { + LOG_ERROR(LOGCALLBACK(zh), "Out of memory"); + return -1; + } + + /* We use a pipe for interrupting select() in unix/sol and socketpair in windows. */ +#ifdef WIN32 + if (create_socket_pair(zh, adaptor_threads->self_pipe) == -1){ + LOG_ERROR(LOGCALLBACK(zh), "Can't make a socket."); +#else + if(pipe(adaptor_threads->self_pipe)==-1) { + LOG_ERROR(LOGCALLBACK(zh), "Can't make a pipe %d",errno); +#endif + free(adaptor_threads); + return -1; + } + set_nonblock(adaptor_threads->self_pipe[1]); + set_nonblock(adaptor_threads->self_pipe[0]); + + pthread_mutex_init(&zh->auth_h.lock,0); + + zh->adaptor_priv = adaptor_threads; + pthread_mutex_init(&zh->to_process.lock,0); + pthread_mutex_init(&adaptor_threads->zh_lock,0); + pthread_mutex_init(&adaptor_threads->reconfig_lock,0); + pthread_mutex_init(&adaptor_threads->watchers_lock,0); + // to_send must be recursive mutex + pthread_mutexattr_init(&recursive_mx_attr); + pthread_mutexattr_settype(&recursive_mx_attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&zh->to_send.lock,&recursive_mx_attr); + pthread_mutexattr_destroy(&recursive_mx_attr); + + pthread_mutex_init(&zh->sent_requests.lock,0); + pthread_cond_init(&zh->sent_requests.cond,0); + pthread_mutex_init(&zh->completions_to_process.lock,0); + pthread_cond_init(&zh->completions_to_process.cond,0); + start_threads(zh); + return 0; +} + +void adaptor_finish(zhandle_t *zh) +{ + struct adaptor_threads *adaptor_threads; + // make sure zh doesn't get destroyed until after we're done here + api_prolog(zh); + adaptor_threads = zh->adaptor_priv; + if(adaptor_threads==0) { + api_epilog(zh,0); + return; + } + + if(!pthread_equal(adaptor_threads->io,pthread_self())){ + wakeup_io_thread(zh); + pthread_join(adaptor_threads->io, 0); + }else + pthread_detach(adaptor_threads->io); + + if(!pthread_equal(adaptor_threads->completion,pthread_self())){ + pthread_mutex_lock(&zh->completions_to_process.lock); + pthread_cond_broadcast(&zh->completions_to_process.cond); + pthread_mutex_unlock(&zh->completions_to_process.lock); + pthread_join(adaptor_threads->completion, 0); + }else + pthread_detach(adaptor_threads->completion); + + api_epilog(zh,0); +} + +void adaptor_destroy(zhandle_t *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if(adaptor==0) return; + + pthread_cond_destroy(&adaptor->cond); + pthread_mutex_destroy(&adaptor->lock); + pthread_mutex_destroy(&zh->to_process.lock); + pthread_mutex_destroy(&zh->to_send.lock); + pthread_mutex_destroy(&zh->sent_requests.lock); + pthread_cond_destroy(&zh->sent_requests.cond); + pthread_mutex_destroy(&zh->completions_to_process.lock); + pthread_cond_destroy(&zh->completions_to_process.cond); + pthread_mutex_destroy(&adaptor->zh_lock); + + pthread_mutex_destroy(&zh->auth_h.lock); + + close(adaptor->self_pipe[0]); + close(adaptor->self_pipe[1]); + free(adaptor); + zh->adaptor_priv=0; +} + +int wakeup_io_thread(zhandle_t *zh) +{ + struct adaptor_threads *adaptor_threads = zh->adaptor_priv; + char c=0; +#ifndef WIN32 + return write(adaptor_threads->self_pipe[1],&c,1)==1? ZOK: ZSYSTEMERROR; +#else + return send(adaptor_threads->self_pipe[1], &c, 1, 0)==1? ZOK: ZSYSTEMERROR; +#endif +} + +int adaptor_send_queue(zhandle_t *zh, int timeout) +{ + if(!zh->close_requested) + return wakeup_io_thread(zh); + // don't rely on the IO thread to send the messages if the app has + // requested to close + return flush_send_queue(zh, timeout); +} + +/* These two are declared here because we will run the event loop + * and not the client */ +#ifdef WIN32 +int zookeeper_interest(zhandle_t *zh, SOCKET *fd, int *interest, + struct timeval *tv); +#else +int zookeeper_interest(zhandle_t *zh, int *fd, int *interest, + struct timeval *tv); +#endif +int zookeeper_process(zhandle_t *zh, int events); + +#ifdef WIN32 +unsigned __stdcall do_io( void * v) +#else +void *do_io(void *v) +#endif +{ + zhandle_t *zh = (zhandle_t*)v; +#ifndef WIN32 + struct pollfd fds[2]; + struct adaptor_threads *adaptor_threads = zh->adaptor_priv; + + api_prolog(zh); + notify_thread_ready(zh); + LOG_DEBUG(LOGCALLBACK(zh), "started IO thread"); + fds[0].fd=adaptor_threads->self_pipe[0]; + fds[0].events=POLLIN; + while(!zh->close_requested) { + zh->io_count++; + struct timeval tv; + int fd; + int interest; + int timeout; + int maxfd=1; + + zookeeper_interest(zh, &fd, &interest, &tv); + if (fd != -1) { + fds[1].fd=fd; + fds[1].events=(interest&ZOOKEEPER_READ)?POLLIN:0; + fds[1].events|=(interest&ZOOKEEPER_WRITE)?POLLOUT:0; + maxfd=2; + } + timeout=tv.tv_sec * 1000 + (tv.tv_usec/1000); + + poll(fds,maxfd,timeout); + if (fd != -1) { + interest=(fds[1].revents&POLLIN)?ZOOKEEPER_READ:0; + interest|=((fds[1].revents&POLLOUT)||(fds[1].revents&POLLHUP))?ZOOKEEPER_WRITE:0; + } + if(fds[0].revents&POLLIN){ + // flush the pipe + char b[128]; + while(read(adaptor_threads->self_pipe[0],b,sizeof(b))==sizeof(b)){} + } +#else + fd_set rfds, wfds; + struct adaptor_threads *adaptor_threads = zh->adaptor_priv; + api_prolog(zh); + notify_thread_ready(zh); + LOG_DEBUG(LOGCALLBACK(zh), "started IO thread"); + + while(!zh->close_requested) { + struct timeval tv; + SOCKET fd; + int interest; + int rc; + + zookeeper_interest(zh, &fd, &interest, &tv); + + // FD_ZERO is cheap on Win32, it just sets count of elements to zero. + // It needs to be done to ensure no stale entries. + FD_ZERO(&rfds); + FD_ZERO(&wfds); + + if (fd != -1) { + if (interest&ZOOKEEPER_READ) { + FD_SET(fd, &rfds); + } + + if (interest&ZOOKEEPER_WRITE) { + FD_SET(fd, &wfds); + } + } + + // Always interested in self_pipe. + FD_SET(adaptor_threads->self_pipe[0], &rfds); + + rc = select(/* unused */0, &rfds, &wfds, NULL, &tv); + if (rc > 0) { + interest=(FD_ISSET(fd, &rfds))? ZOOKEEPER_READ: 0; + interest|=(FD_ISSET(fd, &wfds))? ZOOKEEPER_WRITE: 0; + + if (FD_ISSET(adaptor_threads->self_pipe[0], &rfds)){ + // flush the pipe/socket + char b[128]; + while(recv(adaptor_threads->self_pipe[0],b,sizeof(b), 0)==sizeof(b)){} + } + } + else if (rc < 0) { + LOG_ERROR(LOGCALLBACK(zh), ("select() failed %d [%d].", rc, WSAGetLastError())); + + // Clear interest events for zookeeper_process if select() fails. + interest = 0; + } + +#endif + // dispatch zookeeper events + zookeeper_process(zh, interest); + // check the current state of the zhandle and terminate + // if it is_unrecoverable() + if(is_unrecoverable(zh)) + break; + } + api_epilog(zh, 0); + LOG_DEBUG(LOGCALLBACK(zh), "IO thread terminated"); + return 0; +} + +#ifdef WIN32 +unsigned __stdcall do_completion( void * v) +#else +void *do_completion(void *v) +#endif +{ + zhandle_t *zh = v; + api_prolog(zh); + notify_thread_ready(zh); + LOG_DEBUG(LOGCALLBACK(zh), "started completion thread"); + while(!zh->close_requested) { + pthread_mutex_lock(&zh->completions_to_process.lock); + while(!zh->completions_to_process.head && !zh->close_requested) { + pthread_cond_wait(&zh->completions_to_process.cond, &zh->completions_to_process.lock); + } + pthread_mutex_unlock(&zh->completions_to_process.lock); + process_completions(zh); + } + api_epilog(zh, 0); + LOG_DEBUG(LOGCALLBACK(zh), "completion thread terminated"); + return 0; +} + +int32_t inc_ref_counter(zhandle_t* zh,int i) +{ + int incr=(i<0?-1:(i>0?1:0)); + // fetch_and_add implements atomic post-increment + int v=fetch_and_add(&zh->ref_counter,incr); + // inc_ref_counter wants pre-increment + v+=incr; // simulate pre-increment + return v; +} + +int32_t fetch_and_add(volatile int32_t* operand, int incr) +{ +#ifndef WIN32 + return __sync_fetch_and_add(operand, incr); +#else + return InterlockedExchangeAdd(operand, incr); +#endif +} + +// make sure the static xid is initialized before any threads started +__attribute__((constructor)) int32_t get_xid() +{ + static int32_t xid = -1; + if (xid == -1) { + xid = time(0); + } + return fetch_and_add(&xid,1); +} + +int lock_reconfig(struct _zhandle *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_lock(&adaptor->reconfig_lock); + } else { + return 0; + } +} +int unlock_reconfig(struct _zhandle *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_unlock(&adaptor->reconfig_lock); + } else { + return 0; + } +} + +int lock_watchers(struct _zhandle *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_lock(&adaptor->watchers_lock); + } else { + return 0; + } +} +int unlock_watchers(struct _zhandle *zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_unlock(&adaptor->watchers_lock); + } else { + return 0; + } +} + +int enter_critical(zhandle_t* zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_lock(&adaptor->zh_lock); + } else { + return 0; + } +} + +int leave_critical(zhandle_t* zh) +{ + struct adaptor_threads *adaptor = zh->adaptor_priv; + if (adaptor) { + return pthread_mutex_unlock(&adaptor->zh_lock); + } else { + return 0; + } +} diff --git a/zookeeper-client/zookeeper-client-c/src/recordio.c b/zookeeper-client/zookeeper-client-c/src/recordio.c new file mode 100644 index 0000000..77fae28 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/recordio.c @@ -0,0 +1,372 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include +#include +#ifndef WIN32 +#include +#else +#include /* for _htonl and _ntohl */ +#endif + +void deallocate_String(char **s) +{ + if (*s) + free(*s); + *s = 0; +} + +void deallocate_Buffer(struct buffer *b) +{ + if (b->buff) + free(b->buff); + b->buff = 0; +} + +struct buff_struct { + int32_t len; + int32_t off; + char *buffer; +}; + +static int resize_buffer(struct buff_struct *s, int newlen) +{ + char *buffer= NULL; + while (s->len < newlen) { + s->len *= 2; + } + buffer = (char*)realloc(s->buffer, s->len); + if (!buffer) { + s->buffer = 0; + return -ENOMEM; + } + s->buffer = buffer; + return 0; +} + +int oa_start_record(struct oarchive *oa, const char *tag) +{ + return 0; +} +int oa_end_record(struct oarchive *oa, const char *tag) +{ + return 0; +} +int oa_serialize_int(struct oarchive *oa, const char *tag, const int32_t *d) +{ + struct buff_struct *priv = oa->priv; + int32_t i = htonl(*d); + if ((priv->len - priv->off) < sizeof(i)) { + int rc = resize_buffer(priv, priv->len + sizeof(i)); + if (rc < 0) return rc; + } + memcpy(priv->buffer+priv->off, &i, sizeof(i)); + priv->off+=sizeof(i); + return 0; +} +int64_t zoo_htonll(int64_t v) +{ + int i = 0; + char *s = (char *)&v; + if (htonl(1) == 1) { + return v; + } + for (i = 0; i < 4; i++) { + int tmp = s[i]; + s[i] = s[8-i-1]; + s[8-i-1] = tmp; + } + + return v; +} + +int oa_serialize_long(struct oarchive *oa, const char *tag, const int64_t *d) +{ + const int64_t i = zoo_htonll(*d); + struct buff_struct *priv = oa->priv; + if ((priv->len - priv->off) < sizeof(i)) { + int rc = resize_buffer(priv, priv->len + sizeof(i)); + if (rc < 0) return rc; + } + memcpy(priv->buffer+priv->off, &i, sizeof(i)); + priv->off+=sizeof(i); + return 0; +} +int oa_start_vector(struct oarchive *oa, const char *tag, const int32_t *count) +{ + return oa_serialize_int(oa, tag, count); +} +int oa_end_vector(struct oarchive *oa, const char *tag) +{ + return 0; +} +int oa_serialize_bool(struct oarchive *oa, const char *name, const int32_t *i) +{ + //return oa_serialize_int(oa, name, i); + struct buff_struct *priv = oa->priv; + if ((priv->len - priv->off) < 1) { + int rc = resize_buffer(priv, priv->len + 1); + if (rc < 0) + return rc; + } + priv->buffer[priv->off] = (*i == 0 ? '\0' : '\1'); + priv->off++; + return 0; +} +static const int32_t negone = -1; +int oa_serialize_buffer(struct oarchive *oa, const char *name, + const struct buffer *b) +{ + struct buff_struct *priv = oa->priv; + int rc; + if (!b) { + return oa_serialize_int(oa, "len", &negone); + } + rc = oa_serialize_int(oa, "len", &b->len); + if (rc < 0) + return rc; + // this means a buffer of NUll + // with size of -1. This is + // waht we use in java serialization for NULL + if (b->len == -1) { + return rc; + } + if ((priv->len - priv->off) < b->len) { + rc = resize_buffer(priv, priv->len + b->len); + if (rc < 0) + return rc; + } + memcpy(priv->buffer+priv->off, b->buff, b->len); + priv->off += b->len; + return 0; +} +int oa_serialize_string(struct oarchive *oa, const char *name, char **s) +{ + struct buff_struct *priv = oa->priv; + int32_t len; + int rc; + if (!*s) { + oa_serialize_int(oa, "len", &negone); + return 0; + } + len = strlen(*s); + rc = oa_serialize_int(oa, "len", &len); + if (rc < 0) + return rc; + if ((priv->len - priv->off) < len) { + rc = resize_buffer(priv, priv->len + len); + if (rc < 0) + return rc; + } + memcpy(priv->buffer+priv->off, *s, len); + priv->off += len; + return 0; +} +int ia_start_record(struct iarchive *ia, const char *tag) +{ + return 0; +} +int ia_end_record(struct iarchive *ia, const char *tag) +{ + return 0; +} +int ia_deserialize_int(struct iarchive *ia, const char *tag, int32_t *count) +{ + struct buff_struct *priv = ia->priv; + if ((priv->len - priv->off) < sizeof(*count)) { + return -E2BIG; + } + memcpy(count, priv->buffer+priv->off, sizeof(*count)); + priv->off+=sizeof(*count); + *count = ntohl(*count); + return 0; +} + +int ia_deserialize_long(struct iarchive *ia, const char *tag, int64_t *count) +{ + struct buff_struct *priv = ia->priv; + int64_t v = 0; + if ((priv->len - priv->off) < sizeof(*count)) { + return -E2BIG; + } + memcpy(count, priv->buffer+priv->off, sizeof(*count)); + priv->off+=sizeof(*count); + v = zoo_htonll(*count); // htonll and ntohll do the same + *count = v; + return 0; +} +int ia_start_vector(struct iarchive *ia, const char *tag, int32_t *count) +{ + return ia_deserialize_int(ia, tag, count); +} +int ia_end_vector(struct iarchive *ia, const char *tag) +{ + return 0; +} +int ia_deserialize_bool(struct iarchive *ia, const char *name, int32_t *v) +{ + struct buff_struct *priv = ia->priv; + //fprintf(stderr, "Deserializing bool %d\n", priv->off); + //return ia_deserialize_int(ia, name, v); + if ((priv->len - priv->off) < 1) { + return -E2BIG; + } + *v = priv->buffer[priv->off]; + priv->off+=1; + //fprintf(stderr, "Deserializing bool end %d\n", priv->off); + return 0; +} +int ia_deserialize_buffer(struct iarchive *ia, const char *name, + struct buffer *b) +{ + struct buff_struct *priv = ia->priv; + int rc = ia_deserialize_int(ia, "len", &b->len); + if (rc < 0) + return rc; + if ((priv->len - priv->off) < b->len) { + return -E2BIG; + } + // set the buffer to null + if (b->len == -1) { + b->buff = NULL; + return rc; + } + b->buff = malloc(b->len); + if (!b->buff) { + return -ENOMEM; + } + memcpy(b->buff, priv->buffer+priv->off, b->len); + priv->off += b->len; + return 0; +} +int ia_deserialize_string(struct iarchive *ia, const char *name, char **s) +{ + struct buff_struct *priv = ia->priv; + int32_t len; + int rc = ia_deserialize_int(ia, "len", &len); + if (rc < 0) + return rc; + if ((priv->len - priv->off) < len) { + return -E2BIG; + } + if (len < 0) { + return -EINVAL; + } + *s = malloc(len+1); + if (!*s) { + return -ENOMEM; + } + memcpy(*s, priv->buffer+priv->off, len); + (*s)[len] = '\0'; + priv->off += len; + return 0; +} + +static struct iarchive ia_default = { + ia_start_record, + ia_end_record, + ia_start_vector, + ia_end_vector, + ia_deserialize_bool, + ia_deserialize_int, + ia_deserialize_long , + ia_deserialize_buffer, + ia_deserialize_string}; + +static struct oarchive oa_default = { + oa_start_record, + oa_end_record, + oa_start_vector, + oa_end_vector, + oa_serialize_bool, + oa_serialize_int, + oa_serialize_long , + oa_serialize_buffer, + oa_serialize_string}; + +struct iarchive *create_buffer_iarchive(char *buffer, int len) +{ + struct iarchive *ia; + struct buff_struct *buff; + ia = malloc(sizeof(*ia)); + if (!ia) return 0; + buff = malloc(sizeof(struct buff_struct)); + if (!buff) { + free(ia); + return 0; + } + *ia = ia_default; + buff->off = 0; + buff->buffer = buffer; + buff->len = len; + ia->priv = buff; + return ia; +} + +struct oarchive *create_buffer_oarchive() +{ + struct oarchive *oa; + struct buff_struct *buff; + oa = malloc(sizeof(*oa)); + if (!oa) return 0; + buff = malloc(sizeof(struct buff_struct)); + if (!buff) { + free(oa); + return 0; + } + *oa = oa_default; + buff->off = 0; + buff->buffer = malloc(128); + buff->len = 128; + oa->priv = buff; + return oa; +} + +void close_buffer_iarchive(struct iarchive **ia) +{ + free((*ia)->priv); + free(*ia); + *ia = 0; +} + +void close_buffer_oarchive(struct oarchive **oa, int free_buffer) +{ + if (free_buffer) { + struct buff_struct *buff = (struct buff_struct *)(*oa)->priv; + if (buff->buffer) { + free(buff->buffer); + } + } + free((*oa)->priv); + free(*oa); + *oa = 0; +} + +char *get_buffer(struct oarchive *oa) +{ + struct buff_struct *buff = oa->priv; + return buff->buffer; +} +int get_buffer_len(struct oarchive *oa) +{ + struct buff_struct *buff = oa->priv; + return buff->off; +} diff --git a/zookeeper-client/zookeeper-client-c/src/st_adaptor.c b/zookeeper-client/zookeeper-client-c/src/st_adaptor.c new file mode 100644 index 0000000..07540b7 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/st_adaptor.c @@ -0,0 +1,115 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#if !defined(DLL_EXPORT) && !defined(USE_STATIC_LIB) +# define USE_STATIC_LIB +#endif + +#include "zk_adaptor.h" +#include +#include + +int zoo_lock_auth(zhandle_t *zh) +{ + return 0; +} +int zoo_unlock_auth(zhandle_t *zh) +{ + return 0; +} +int lock_buffer_list(buffer_head_t *l) +{ + return 0; +} +int unlock_buffer_list(buffer_head_t *l) +{ + return 0; +} +int lock_completion_list(completion_head_t *l) +{ + return 0; +} +int unlock_completion_list(completion_head_t *l) +{ + return 0; +} +int process_async(int outstanding_sync) +{ + return outstanding_sync == 0; +} + +int adaptor_init(zhandle_t *zh) +{ + return 0; +} + +void adaptor_finish(zhandle_t *zh){} + +void adaptor_destroy(zhandle_t *zh){} + +int flush_send_queue(zhandle_t *, int); + +int adaptor_send_queue(zhandle_t *zh, int timeout) +{ + return flush_send_queue(zh, timeout); +} + +int32_t inc_ref_counter(zhandle_t* zh,int i) +{ + zh->ref_counter+=(i<0?-1:(i>0?1:0)); + return zh->ref_counter; +} + +int32_t get_xid() +{ + static int32_t xid = -1; + if (xid == -1) { + xid = time(0); + } + return xid++; +} + +int lock_reconfig(struct _zhandle *zh) +{ + return 0; +} + +int unlock_reconfig(struct _zhandle *zh) +{ + return 0; +} + +int lock_watchers(struct _zhandle *zh) +{ + return 0; +} + +int unlock_watchers(struct _zhandle *zh) +{ + return 0; +} + +int enter_critical(zhandle_t* zh) +{ + return 0; +} + +int leave_critical(zhandle_t* zh) +{ + return 0; +} diff --git a/zookeeper-client/zookeeper-client-c/src/winport.c b/zookeeper-client/zookeeper-client-c/src/winport.c new file mode 100644 index 0000000..d40614c --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/winport.c @@ -0,0 +1,307 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifdef WIN32 +#include "winport.h" +#include +#include /* for int64_t */ +#include /* must always be included before ws2tcpip.h */ +#include /* for SOCKET */ + +int pthread_mutex_lock(pthread_mutex_t* _mutex ){ + int rc = WaitForSingleObject( *_mutex, // handle to mutex + INFINITE); // no time-out interval + return ((rc == WAIT_OBJECT_0) ? 0: rc); +} + +int pthread_mutex_unlock( pthread_mutex_t* _mutex ){ + int rc = ReleaseMutex(*_mutex); + return ((rc != 0)? 0: GetLastError()); +} + +int pthread_mutex_init(pthread_mutex_t* _mutex, void* ignoredAttr){ + //use CreateMutex as we are using the HANDLES in pthread_cond + *_mutex = CreateMutex( + NULL, // default security attributes + FALSE, // initially not owned + NULL); // unnamed mutex + return ((*_mutex == NULL) ? GetLastError() : 0); +} + +int pthread_mutex_destroy(pthread_mutex_t* _mutex) +{ + int rc = CloseHandle(*_mutex); + return ((rc != 0)? 0: GetLastError()); +} + +int pthread_create(pthread_t *thread, const pthread_attr_t *attr, unsigned (__stdcall* start_routine)(void* a), void *arg) +{ + int _intThreadId; + (*thread).thread_handle = (HANDLE)_beginthreadex( NULL, 0, start_routine , arg, 0, (unsigned int*)&_intThreadId ); + (*thread).thread_id = _intThreadId; + return (((*thread).thread_handle == 0 ) ? errno : 0 ); +} + + +int pthread_equal(pthread_t t1, pthread_t t2){ +//Is there a better way to do this? GetThreadId(handle) is only supported Windows 2003 n above. + return ((t1.thread_id == t2.thread_id) ? 1:0); +} + +pthread_t pthread_self(){ + pthread_t thread_self; + thread_self.thread_handle = GetCurrentThread(); + thread_self.thread_id = GetCurrentThreadId(); + return thread_self; +} + +int pthread_join(pthread_t _thread, void** ignore) +{ + int rc = WaitForSingleObject( _thread.thread_handle, INFINITE ); + return ((rc == WAIT_OBJECT_0) ? 0: rc); +} + +int pthread_detach(pthread_t _thread) +{ + int rc = CloseHandle(_thread.thread_handle) ; + return (rc != 0) ? 0: GetLastError(); +} + +void pthread_mutexattr_init(pthread_mutexattr_t* ignore){} +void pthread_mutexattr_settype(pthread_mutexattr_t* ingore_attr, int ignore){} +void pthread_mutexattr_destroy(pthread_mutexattr_t* ignore_attr){} + +int +pthread_cond_init (pthread_cond_t *cv, + const pthread_condattr_t * ignore) +{ + cv->waiters_count_ = 0; + cv->was_broadcast_ = 0; + cv->sema_ = CreateSemaphore (NULL, // no security + 0, // initially 0 + 0x7fffffff, // max count + NULL); // unnamed + if (cv->sema_ == NULL ) + return GetLastError(); + InitializeCriticalSection (&cv->waiters_count_lock_); + cv->waiters_done_ = CreateEvent (NULL, // no security + FALSE, // auto-reset + FALSE, // non-signaled initially + NULL); // unnamed + return (cv->waiters_done_ == NULL) ? GetLastError() : 0; + +} + + +int pthread_cond_destroy(pthread_cond_t *cond) +{ + CloseHandle( cond->sema_); + DeleteCriticalSection(&cond->waiters_count_lock_); + return (CloseHandle( cond->waiters_done_ ) == 0)? GetLastError(): 0 ; +} + + +int +pthread_cond_signal (pthread_cond_t *cv) +{ + int have_waiters; + EnterCriticalSection (& (cv->waiters_count_lock_)); + have_waiters = cv->waiters_count_ > 0; + LeaveCriticalSection (&cv->waiters_count_lock_); + + // If there aren't any waiters, then this is a no-op. + if (have_waiters){ + return (ReleaseSemaphore (cv->sema_, 1, 0) == 0 ) ? GetLastError() : 0 ; + }else + return 0; +} + + +int +pthread_cond_broadcast (pthread_cond_t *cv) +{ + // This is needed to ensure that and are + // consistent relative to each other. + int have_waiters = 0; + EnterCriticalSection (&cv->waiters_count_lock_); + + if (cv->waiters_count_ > 0) { + // We are broadcasting, even if there is just one waiter... + // Record that we are broadcasting, which helps optimize + // for the non-broadcast case. + cv->was_broadcast_ = 1; + have_waiters = 1; + } + + if (have_waiters) { + // Wake up all the waiters atomically. + ReleaseSemaphore (cv->sema_, cv->waiters_count_, 0); + + LeaveCriticalSection (&cv->waiters_count_lock_); + + // Wait for all the awakened threads to acquire the counting + // semaphore. + WaitForSingleObject (cv->waiters_done_, INFINITE); + // This assignment is okay, even without the held + // because no other waiter threads can wake up to access it. + cv->was_broadcast_ = 0; + } + else + LeaveCriticalSection (&cv->waiters_count_lock_); +} + + +int +pthread_cond_wait (pthread_cond_t *cv, + pthread_mutex_t *external_mutex) +{ + int last_waiter; + // Avoid race conditions. + EnterCriticalSection (&cv->waiters_count_lock_); + cv->waiters_count_++; + LeaveCriticalSection (&cv->waiters_count_lock_); + + // This call atomically releases the mutex and waits on the + // semaphore until or + // are called by another thread. + SignalObjectAndWait (*external_mutex, cv->sema_, INFINITE, FALSE); + + // Reacquire lock to avoid race conditions. + EnterCriticalSection (&cv->waiters_count_lock_); + + // We're no longer waiting... + cv->waiters_count_--; + + // Check to see if we're the last waiter after . + last_waiter = cv->was_broadcast_ && cv->waiters_count_ == 0; + + LeaveCriticalSection (&cv->waiters_count_lock_); + + // If we're the last waiter thread during this particular broadcast + // then let all the other threads proceed. + if (last_waiter) + // This call atomically signals the event and waits until + // it can acquire the . This is required to ensure fairness. + SignalObjectAndWait (cv->waiters_done_, *external_mutex, INFINITE, FALSE); + else + // Always regain the external mutex since that's the guarantee we + // give to our callers. + WaitForSingleObject (*external_mutex, INFINITE); +} + +int pthread_key_create(pthread_key_t *key, void (*destructor)(void *) ) +{ + int result = 0; + pthread_key_t* newkey; + + if ((newkey = (pthread_key_t*) calloc (1, sizeof (pthread_key_t))) == NULL) + { + result = ENOMEM; + } + else if ((newkey->key = TlsAlloc ()) == TLS_OUT_OF_INDEXES) + { + result = EAGAIN; + free (newkey); + newkey = NULL; + } + else if (destructor != NULL) + { + //--we have to store the function pointer for destructor, so that we can call it + //--to free up the user allocated storage-- + newkey->destructor = destructor; + } + key = newkey; + return (result); +} + +int pthread_key_delete(pthread_key_t key) +{ + int rc = 0; + LPVOID lpvData = TlsGetValue(key.key); + rc = TlsFree (key.key); + rc = (rc != 0 ) ? 0 : GetLastError(); + if (key.destructor != NULL && lpvData != 0){ + key.destructor(lpvData); //we take control of calling destructor, instead of calling it on thread exit. + } + free (&key); + return (rc); +} + +void *pthread_getspecific(pthread_key_t key) +{ + LPVOID lpvData = TlsGetValue(key.key); + if ((lpvData == 0) && (GetLastError() != ERROR_SUCCESS)) + return NULL; + else + return lpvData; +} + +int pthread_setspecific(pthread_key_t key, const void *value) +{ + int rc = TlsSetValue (key.key, value); + return ((rc != 0 ) ? 0 : GetLastError()); +} + +int gettimeofday(struct timeval *tp, void *tzp) { + int64_t now = 0; + if (tzp != 0) { errno = EINVAL; return -1; } + GetSystemTimeAsFileTime( (LPFILETIME)&now ); + tp->tv_sec = (long)(now / 10000000 - 11644473600LL); + tp->tv_usec = (now / 10) % 1000000; + return 0; +} + +int close(SOCKET fd) { + return closesocket(fd); +} + +int Win32WSAStartup() +{ + WORD wVersionRq; + WSADATA wsaData; + int err; + + wVersionRq = MAKEWORD(2,0); + err = WSAStartup(wVersionRq, &wsaData); + if (err != 0) + return 1; + + // confirm the version information + if ((LOBYTE(wsaData.wVersion) != 2) || + (HIBYTE(wsaData.wVersion) != 0)) + { + Win32WSACleanup(); + return 1; + } + return 0; +} + +void Win32WSACleanup() +{ + WSACleanup(); +} + +double drand48(void) +{ + return (double)(rand()) / RAND_MAX; +} + +#endif //WIN32 + + + diff --git a/zookeeper-client/zookeeper-client-c/src/winport.h b/zookeeper-client/zookeeper-client-c/src/winport.h new file mode 100644 index 0000000..d216f7f --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/winport.h @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/** + * This header file is to port pthread lib , sockets and other utility methods on windows. + * Specifically the threads function, mutexes, keys, and socket initialization. + */ + +#ifndef WINPORT_H_ +#define WINPORT_H_ + +#ifdef WIN32 +#include "winconfig.h" + +#define _WINSOCK_DEPRECATED_NO_WARNINGS +#include /* must always be included before ws2tcpip.h */ +#include /* for struct sock_addr used in zookeeper.h */ + +/* POSIX names are deprecated, use ISO conformant names instead. */ +#define strdup _strdup +#define getcwd _getcwd +#define getpid _getpid + +/* Windows "secure" versions of POSIX reentrant functions */ +#define strtok_r strtok_s +#define localtime_r(a,b) localtime_s(b,a) + +/* After this version of MSVC, snprintf became a defined function, + and so cannot be redefined, nor can #ifndef be used to guard it. */ +#if ((defined(_MSC_VER) && _MSC_VER < 1900) || !defined(_MSC_VER)) +#define snprintf _snprintf +#endif + + +#include +#include +#include /* for int64_t */ +#include +#include + + +typedef int ssize_t; +typedef HANDLE pthread_mutex_t; + +struct pthread_t_ +{ + HANDLE thread_handle; + DWORD thread_id; +}; + +typedef struct pthread_t_ pthread_t; +typedef int pthread_mutexattr_t; +typedef int pthread_condattr_t; +typedef int pthread_attr_t; +#define PTHREAD_MUTEX_RECURSIVE 0 + +int pthread_mutex_lock(pthread_mutex_t* _mutex ); +int pthread_mutex_unlock( pthread_mutex_t* _mutex ); +int pthread_mutex_init(pthread_mutex_t* _mutex, void* ignoredAttr); +int pthread_mutex_destroy(pthread_mutex_t* _mutex); +int pthread_create(pthread_t *thread, const pthread_attr_t *attr, unsigned (__stdcall* start_routine)(void* a), void *arg); +int pthread_equal(pthread_t t1, pthread_t t2); +pthread_t pthread_self(); +int pthread_join(pthread_t _thread, void** ignore); +int pthread_detach(pthread_t _thread); + +void pthread_mutexattr_init(pthread_mutexattr_t* ignore); +void pthread_mutexattr_settype(pthread_mutexattr_t* ingore_attr, int ignore); +void pthread_mutexattr_destroy(pthread_mutexattr_t* ignore_attr); + + +// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html + +typedef struct +{ + int waiters_count_; + // Number of waiting threads. + + CRITICAL_SECTION waiters_count_lock_; + // Serialize access to . + + HANDLE sema_; + // Semaphore used to queue up threads waiting for the condition to + // become signaled. + + HANDLE waiters_done_; + // An auto-reset event used by the broadcast/signal thread to wait + // for all the waiting thread(s) to wake up and be released from the + // semaphore. + + size_t was_broadcast_; + // Keeps track of whether we were broadcasting or signaling. This + // allows us to optimize the code if we're just signaling. +}pthread_cond_t; + +int pthread_cond_init (pthread_cond_t *cv,const pthread_condattr_t * ignore); +int pthread_cond_destroy(pthread_cond_t *cond); +int pthread_cond_signal (pthread_cond_t *cv); +int pthread_cond_broadcast (pthread_cond_t *cv); +int pthread_cond_wait (pthread_cond_t *cv, pthread_mutex_t *external_mutex); + + +struct pthread_key_t_ +{ + DWORD key; + void (*destructor) (void *); +}; + +typedef struct pthread_key_t_ pthread_key_t; +int pthread_key_create(pthread_key_t *key, void (*destructor)(void *) ); +int pthread_key_delete(pthread_key_t key); +void *pthread_getspecific(pthread_key_t key); +int pthread_setspecific(pthread_key_t key, const void *value); + +int gettimeofday(struct timeval *tp, void *tzp); +int close(SOCKET fd); +int Win32WSAStartup(); +void Win32WSACleanup(); +double drand48(void); +#endif //WIN32 + + + +#endif //WINPORT_H_ diff --git a/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h b/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h new file mode 100644 index 0000000..305efbd --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/zk_adaptor.h @@ -0,0 +1,327 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ZK_ADAPTOR_H_ +#define ZK_ADAPTOR_H_ +#include +#ifdef THREADED +#ifndef WIN32 +#include +#else +#include "winport.h" +#endif +#endif +#include "zookeeper.h" +#include "zk_hashtable.h" +#include "addrvec.h" + +/* predefined xid's values recognized as special by the server */ +#define WATCHER_EVENT_XID -1 +#define PING_XID -2 +#define AUTH_XID -4 +#define SET_WATCHES_XID -8 + +/* zookeeper state constants */ +#define EXPIRED_SESSION_STATE_DEF -112 +#define AUTH_FAILED_STATE_DEF -113 +#define CONNECTING_STATE_DEF 1 +#define ASSOCIATING_STATE_DEF 2 +#define CONNECTED_STATE_DEF 3 +#define READONLY_STATE_DEF 5 +#define SSL_CONNECTING_STATE_DEF 7 +#define NOTCONNECTED_STATE_DEF 999 + +/* zookeeper event type constants */ +#define CREATED_EVENT_DEF 1 +#define DELETED_EVENT_DEF 2 +#define CHANGED_EVENT_DEF 3 +#define CHILD_EVENT_DEF 4 +#define SESSION_EVENT_DEF -1 +#define NOTWATCHING_EVENT_DEF -2 + +#ifdef __cplusplus +extern "C" { +#endif + +struct _buffer_list; +struct _completion_list; + +typedef struct _buffer_head { + struct _buffer_list *volatile head; + struct _buffer_list *last; +#ifdef THREADED + pthread_mutex_t lock; +#endif +} buffer_head_t; + +typedef struct _completion_head { + struct _completion_list *volatile head; + struct _completion_list *last; +#ifdef THREADED + pthread_cond_t cond; + pthread_mutex_t lock; +#endif +} completion_head_t; + +int lock_buffer_list(buffer_head_t *l); +int unlock_buffer_list(buffer_head_t *l); +int lock_completion_list(completion_head_t *l); +int unlock_completion_list(completion_head_t *l); + +struct sync_completion { + int rc; + union { + struct { + char *str; + int str_len; + } str; + struct Stat stat; + struct { + char *buffer; + int buff_len; + struct Stat stat; + } data; + struct { + struct ACL_vector acl; + struct Stat stat; + } acl; + struct String_vector strs2; + struct { + struct String_vector strs2; + struct Stat stat2; + } strs_stat; + } u; + int complete; +#ifdef THREADED + pthread_cond_t cond; + pthread_mutex_t lock; +#endif +}; + +typedef struct _auth_info { + int state; /* 0=>inactive, >0 => active */ + char* scheme; + struct buffer auth; + void_completion_t completion; + const char* data; + struct _auth_info *next; +} auth_info; + +/** + * This structure represents a packet being read or written. + */ +typedef struct _buffer_list { + char *buffer; + int len; /* This represents the length of sizeof(header) + length of buffer */ + int curr_offset; /* This is the offset into the header followed by offset into the buffer */ + struct _buffer_list *next; +} buffer_list_t; + +/* the size of connect request */ +#define HANDSHAKE_REQ_SIZE 45 +/* connect request */ +struct connect_req { + int32_t protocolVersion; + int64_t lastZxidSeen; + int32_t timeOut; + int64_t sessionId; + int32_t passwd_len; + char passwd[16]; + char readOnly; +}; + +/* the connect response */ +struct prime_struct { + int32_t len; + int32_t protocolVersion; + int32_t timeOut; + int64_t sessionId; + int32_t passwd_len; + char passwd[16]; + char readOnly; +}; + +#ifdef THREADED +/* this is used by mt_adaptor internally for thread management */ +struct adaptor_threads { + pthread_t io; + pthread_t completion; + int threadsToWait; // barrier + pthread_cond_t cond; // barrier's conditional + pthread_mutex_t lock; // ... and a lock + pthread_mutex_t zh_lock; // critical section lock + pthread_mutex_t reconfig_lock; // lock for reconfiguring cluster's ensemble + pthread_mutex_t watchers_lock; // lock for watcher operations +#ifdef WIN32 + SOCKET self_pipe[2]; +#else + int self_pipe[2]; +#endif +}; +#endif + +/** the auth list for adding auth */ +typedef struct _auth_list_head { + auth_info *auth; +#ifdef THREADED + pthread_mutex_t lock; +#endif +} auth_list_head_t; + +/** + * This structure represents the connection to zookeeper. + */ +struct _zhandle { + zsock_t *fd; + + // Hostlist and list of addresses + char *hostname; // hostname contains list of zookeeper servers to connect to + struct sockaddr_storage addr_cur; // address of server we're currently connecting/connected to + struct sockaddr_storage addr_rw_server; // address of last known read/write server found. + + addrvec_t addrs; // current list of addresses we're connected to + addrvec_t addrs_old; // old list of addresses that we are no longer connected to + addrvec_t addrs_new; // new list of addresses to connect to if we're reconfiguring + + int reconfig; // Are we in the process of reconfiguring cluster's ensemble + double pOld, pNew; // Probability for selecting between 'addrs_old' and 'addrs_new' + int delay; + int disable_reconnection_attempt; // When set, client will not try reconnect to a different server in + // server list. This makes a sticky server for client, and is useful + // for testing if a sticky server is required, or if client wants to + // explicitly shuffle server by calling zoo_cycle_next_server. + // The default value is 0. + + watcher_fn watcher; // the registered watcher + + // Message timings + struct timeval last_recv; // time last message was received + struct timeval last_send; // time last message was sent + struct timeval last_ping; // time last PING was sent + struct timeval next_deadline; // time of the next deadline + int recv_timeout; // max receive timeout for messages from server + + // Buffers + buffer_list_t *input_buffer; // current buffer being read in + buffer_head_t to_process; // buffers that have been read and ready to be processed + buffer_head_t to_send; // packets queued to send + completion_head_t sent_requests; // outstanding requests + completion_head_t completions_to_process; // completions that are ready to run + int outstanding_sync; // number of outstanding synchronous requests + + /* read-only mode specific fields */ + struct timeval last_ping_rw; /* The last time we checked server for being r/w */ + int ping_rw_timeout; /* The time that can go by before checking next server */ + + // State info + volatile int state; // Current zookeeper state + void *context; // client-side provided context + clientid_t client_id; // client-id + long long last_zxid; // last zookeeper ID + auth_list_head_t auth_h; // authentication data list + log_callback_fn log_callback; // Callback for logging (falls back to logging to stderr) + int io_count; // counts the number of iterations of do_io + + // Primer storage + struct _buffer_list primer_buffer; // The buffer used for the handshake at the start of a connection + struct prime_struct primer_storage; // the connect response + char primer_storage_buffer[41]; // the true size of primer_storage + + /* zookeeper_close is not reentrant because it de-allocates the zhandler. + * This guard variable is used to defer the destruction of zhandle till + * right before top-level API call returns to the caller */ + int32_t ref_counter; + volatile int close_requested; + void *adaptor_priv; + + /* Used for debugging only: non-zero value indicates the time when the zookeeper_process + * call returned while there was at least one unprocessed server response + * available in the socket recv buffer */ + struct timeval socket_readable; + + // Watchers + zk_hashtable* active_node_watchers; + zk_hashtable* active_exist_watchers; + zk_hashtable* active_child_watchers; + + /** used for chroot path at the client side **/ + char *chroot; + + /** Indicates if this client is allowed to go to r/o mode */ + char allow_read_only; + /** Indicates if we connected to a majority server before */ + char seen_rw_server_before; +}; + + +int adaptor_init(zhandle_t *zh); +void adaptor_finish(zhandle_t *zh); +void adaptor_destroy(zhandle_t *zh); +#if THREADED +struct sync_completion *alloc_sync_completion(void); +int wait_sync_completion(struct sync_completion *sc); +void free_sync_completion(struct sync_completion *sc); +void notify_sync_completion(struct sync_completion *sc); +#endif +int adaptor_send_queue(zhandle_t *zh, int timeout); +int process_async(int outstanding_sync); +void process_completions(zhandle_t *zh); +int flush_send_queue(zhandle_t*zh, int timeout); +char* sub_string(zhandle_t *zh, const char* server_path); +void free_duplicate_path(const char* free_path, const char* path); +int zoo_lock_auth(zhandle_t *zh); +int zoo_unlock_auth(zhandle_t *zh); + +// ensemble reconfigure access guards +int lock_reconfig(struct _zhandle *zh); +int unlock_reconfig(struct _zhandle *zh); + +// watchers hashtable lock +int lock_watchers(struct _zhandle *zh); +int unlock_watchers(struct _zhandle *zh); + +// critical section guards +int enter_critical(zhandle_t* zh); +int leave_critical(zhandle_t* zh); + +// zhandle object reference counting +void api_prolog(zhandle_t* zh); +int api_epilog(zhandle_t *zh, int rc); +int32_t get_xid(); + +// returns the new value of the ref counter +int32_t inc_ref_counter(zhandle_t* zh,int i); + +#ifdef THREADED +// atomic post-increment +int32_t fetch_and_add(volatile int32_t* operand, int incr); +// in mt mode process session event asynchronously by the completion thread +#define PROCESS_SESSION_EVENT(zh,newstate) queue_session_event(zh,newstate) +#else +// in single-threaded mode process session event immediately +//#define PROCESS_SESSION_EVENT(zh,newstate) deliverWatchers(zh,ZOO_SESSION_EVENT,newstate,0) +#define PROCESS_SESSION_EVENT(zh,newstate) queue_session_event(zh,newstate) +#endif + +#ifdef __cplusplus +} +#endif + +#endif /*ZK_ADAPTOR_H_*/ + + diff --git a/zookeeper-client/zookeeper-client-c/src/zk_hashtable.c b/zookeeper-client/zookeeper-client-c/src/zk_hashtable.c new file mode 100644 index 0000000..9d858e2 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/zk_hashtable.c @@ -0,0 +1,476 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "zk_hashtable.h" +#include "zk_adaptor.h" +#include "hashtable/hashtable.h" +#include "hashtable/hashtable_itr.h" +#include +#include +#include + +typedef struct _watcher_object { + watcher_fn watcher; + void* context; + struct _watcher_object* next; +} watcher_object_t; + + +struct _zk_hashtable { + struct hashtable* ht; +}; + +struct watcher_object_list { + watcher_object_t* head; +}; + +/* the following functions are for testing only */ +typedef struct hashtable hashtable_impl; + +hashtable_impl* getImpl(zk_hashtable* ht){ + return ht->ht; +} + +watcher_object_t* getFirstWatcher(zk_hashtable* ht,const char* path) +{ + watcher_object_list_t* wl=hashtable_search(ht->ht,(void*)path); + if(wl!=0) + return wl->head; + return 0; +} +/* end of testing functions */ + +watcher_object_t* clone_watcher_object(watcher_object_t* wo) +{ + watcher_object_t* res=calloc(1,sizeof(watcher_object_t)); + assert(res); + res->watcher=wo->watcher; + res->context=wo->context; + return res; +} + +static unsigned int string_hash_djb2(void *str) +{ + unsigned int hash = 5381; + int c; + const char* cstr = (const char*)str; + while ((c = *cstr++)) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; +} + +static int string_equal(void *key1,void *key2) +{ + return strcmp((const char*)key1,(const char*)key2)==0; +} + +static watcher_object_t* create_watcher_object(watcher_fn watcher,void* ctx) +{ + watcher_object_t* wo=calloc(1,sizeof(watcher_object_t)); + assert(wo); + wo->watcher=watcher; + wo->context=ctx; + return wo; +} + +static watcher_object_list_t* create_watcher_object_list(watcher_object_t* head) +{ + watcher_object_list_t* wl=calloc(1,sizeof(watcher_object_list_t)); + assert(wl); + wl->head=head; + return wl; +} + +static void destroy_watcher_object_list(watcher_object_list_t* list) +{ + watcher_object_t* e = NULL; + + if(list==0) + return; + e=list->head; + while(e!=0){ + watcher_object_t* this=e; + e=e->next; + free(this); + } + free(list); +} + +zk_hashtable* create_zk_hashtable() +{ + struct _zk_hashtable *ht=calloc(1,sizeof(struct _zk_hashtable)); + assert(ht); + ht->ht=create_hashtable(32,string_hash_djb2,string_equal); + return ht; +} + +static void do_clean_hashtable(zk_hashtable* ht) +{ + struct hashtable_itr *it; + int hasMore; + if(hashtable_count(ht->ht)==0) + return; + it=hashtable_iterator(ht->ht); + do { + watcher_object_list_t* w=hashtable_iterator_value(it); + destroy_watcher_object_list(w); + hasMore=hashtable_iterator_remove(it); + } while(hasMore); + free(it); +} + +void destroy_zk_hashtable(zk_hashtable* ht) +{ + if(ht!=0){ + do_clean_hashtable(ht); + hashtable_destroy(ht->ht,0); + free(ht); + } +} + +// searches for a watcher object instance in a watcher object list; +// two watcher objects are equal if their watcher function and context pointers +// are equal +static watcher_object_t* search_watcher(watcher_object_list_t** wl,watcher_object_t* wo) +{ + watcher_object_t* wobj=(*wl)->head; + while(wobj!=0){ + if(wobj->watcher==wo->watcher && wobj->context==wo->context) + return wobj; + wobj=wobj->next; + } + return 0; +} + +static int add_to_list(watcher_object_list_t **wl, watcher_object_t *wo, + int clone) +{ + if (search_watcher(wl, wo)==0) { + watcher_object_t* cloned=wo; + if (clone) { + cloned = clone_watcher_object(wo); + assert(cloned); + } + cloned->next = (*wl)->head; + (*wl)->head = cloned; + return 1; + } else if (!clone) { + // If it's here and we aren't supposed to clone, we must destroy + free(wo); + } + return 0; +} + +static int do_insert_watcher_object(zk_hashtable *ht, const char *path, watcher_object_t* wo) +{ + int res=1; + watcher_object_list_t* wl; + + wl=hashtable_search(ht->ht,(void*)path); + if(wl==0){ + int res; + /* inserting a new path element */ + res=hashtable_insert(ht->ht,strdup(path),create_watcher_object_list(wo)); + assert(res); + }else{ + /* + * Path already exists; check if the watcher already exists. + * Don't clone the watcher since it's allocated on the heap --- avoids + * a memory leak and saves a clone operation (calloc + copy). + */ + res = add_to_list(&wl, wo, 0); + } + return res; +} + + +char **collect_keys(zk_hashtable *ht, int *count) +{ + char **list; + struct hashtable_itr *it; + int i; + + *count = hashtable_count(ht->ht); + list = calloc(*count, sizeof(char*)); + it=hashtable_iterator(ht->ht); + for(i = 0; i < *count; i++) { + list[i] = strdup(hashtable_iterator_key(it)); + hashtable_iterator_advance(it); + } + free(it); + return list; +} + +static int insert_watcher_object(zk_hashtable *ht, const char *path, + watcher_object_t* wo) +{ + int res; + res=do_insert_watcher_object(ht,path,wo); + return res; +} + +static void copy_watchers(watcher_object_list_t *from, watcher_object_list_t *to, int clone) +{ + watcher_object_t* wo=from->head; + while(wo){ + watcher_object_t *next = wo->next; + add_to_list(&to, wo, clone); + wo=next; + } +} + +static void copy_table(zk_hashtable *from, watcher_object_list_t *to) { + struct hashtable_itr *it; + int hasMore; + if(hashtable_count(from->ht)==0) + return; + it=hashtable_iterator(from->ht); + do { + watcher_object_list_t *w = hashtable_iterator_value(it); + copy_watchers(w, to, 1); + hasMore=hashtable_iterator_advance(it); + } while(hasMore); + free(it); +} + +static void collect_session_watchers(zhandle_t *zh, + watcher_object_list_t **list) +{ + copy_table(zh->active_node_watchers, *list); + copy_table(zh->active_exist_watchers, *list); + copy_table(zh->active_child_watchers, *list); +} + +static void add_for_event(zk_hashtable *ht, char *path, watcher_object_list_t **list) +{ + watcher_object_list_t* wl; + wl = (watcher_object_list_t*)hashtable_remove(ht->ht, path); + if (wl) { + copy_watchers(wl, *list, 0); + // Since we move, not clone the watch_objects, we just need to free the + // head pointer + free(wl); + } +} + +static void do_foreach_watcher(watcher_object_t* wo,zhandle_t* zh, + const char* path,int type,int state) +{ + // session event's don't have paths + const char *client_path = + (type != ZOO_SESSION_EVENT ? sub_string(zh, path) : path); + while(wo!=0){ + wo->watcher(zh,type,state,client_path,wo->context); + wo=wo->next; + } + free_duplicate_path(client_path, path); +} + +watcher_object_list_t *collectWatchers(zhandle_t *zh,int type, char *path) +{ + struct watcher_object_list *list = create_watcher_object_list(0); + + if(type==ZOO_SESSION_EVENT){ + watcher_object_t defWatcher; + defWatcher.watcher=zh->watcher; + defWatcher.context=zh->context; + add_to_list(&list, &defWatcher, 1); + collect_session_watchers(zh, &list); + return list; + } + switch(type){ + case CREATED_EVENT_DEF: + case CHANGED_EVENT_DEF: + // look up the watchers for the path and move them to a delivery list + add_for_event(zh->active_node_watchers,path,&list); + add_for_event(zh->active_exist_watchers,path,&list); + break; + case CHILD_EVENT_DEF: + // look up the watchers for the path and move them to a delivery list + add_for_event(zh->active_child_watchers,path,&list); + break; + case DELETED_EVENT_DEF: + // look up the watchers for the path and move them to a delivery list + add_for_event(zh->active_node_watchers,path,&list); + add_for_event(zh->active_exist_watchers,path,&list); + add_for_event(zh->active_child_watchers,path,&list); + break; + } + return list; +} + +void deliverWatchers(zhandle_t *zh, int type,int state, char *path, watcher_object_list_t **list) +{ + if (!list || !(*list)) return; + do_foreach_watcher((*list)->head, zh, path, type, state); + destroy_watcher_object_list(*list); + *list = 0; +} + +void activateWatcher(zhandle_t *zh, watcher_registration_t* reg, int rc) +{ + if(reg){ + /* in multithreaded lib, this code is executed + * by the IO thread */ + zk_hashtable *ht = reg->checker(zh, rc); + if(ht){ + insert_watcher_object(ht,reg->path, + create_watcher_object(reg->watcher, reg->context)); + } + } +} + +/* If watcher is NULL, we return TRUE since we consider it a match */ +static int containsWatcher(zk_hashtable *watchers, const char *path, + watcher_fn watcher, void *watcherCtx) +{ + watcher_object_list_t *wl; + watcher_object_t e; + + if (!watcher) + return 1; + + wl = hashtable_search(watchers->ht, (void *)path); + if (!wl) + return 0; + + e.watcher = watcher; + e.context = watcherCtx; + + return search_watcher(&wl, &e) ? 1 : 0; +} + +/** + * remove any watcher_object that has a matching (watcher, watcherCtx) + */ +static void removeWatcherFromList(watcher_object_list_t *wl, watcher_fn watcher, + void *watcherCtx) +{ + watcher_object_t *e = NULL; + + if (!wl || (wl && !wl->head)) + return; + + e = wl->head; + while (e){ + if (e->next && + e->next->watcher == watcher && + e->next->context == watcherCtx) { + watcher_object_t *this = e->next; + e->next = e->next->next; + free(this); + break; + } + e = e->next; + } + + if (wl->head && + wl->head->watcher == watcher && wl->head->context == watcherCtx) { + watcher_object_t *this = wl->head; + wl->head = wl->head->next; + free(this); + } +} + +static void removeWatcher(zk_hashtable *watchers, const char *path, + watcher_fn watcher, void *watcherCtx) +{ + watcher_object_list_t *wl = hashtable_search(watchers->ht, (void *)path); + + if (!wl) + return; + + if (!watcher) { + wl = (watcher_object_list_t *) hashtable_remove(watchers->ht, + (void *)path); + destroy_watcher_object_list(wl); + return; + } + + removeWatcherFromList(wl, watcher, watcherCtx); + + if (!wl->head) { + wl = (watcher_object_list_t *) hashtable_remove(watchers->ht, + (void *)path); + destroy_watcher_object_list(wl); + } +} + +void deactivateWatcher(zhandle_t *zh, watcher_deregistration_t *dereg, int rc) +{ + if (rc != ZOK || !dereg) + return; + + removeWatchers(zh, dereg->path, dereg->type, dereg->watcher, + dereg->context); +} + +void removeWatchers(zhandle_t *zh, const char* path, ZooWatcherType type, + watcher_fn watcher, void *watcherCtx) +{ + switch (type) { + case ZWATCHTYPE_CHILD: + removeWatcher(zh->active_child_watchers, path, watcher, watcherCtx); + break; + case ZWATCHTYPE_DATA: + removeWatcher(zh->active_node_watchers, path, watcher, watcherCtx); + removeWatcher(zh->active_exist_watchers, path, watcher, watcherCtx); + break; + case ZWATCHTYPE_ANY: + removeWatcher(zh->active_child_watchers, path, watcher, watcherCtx); + removeWatcher(zh->active_node_watchers, path, watcher, watcherCtx); + removeWatcher(zh->active_exist_watchers, path, watcher, watcherCtx); + break; + } +} + +int pathHasWatcher(zhandle_t *zh, const char *path, int wtype, + watcher_fn watcher, void *watcherCtx) +{ + int watcher_found = 0; + + switch (wtype) { + case ZWATCHTYPE_CHILD: + watcher_found = containsWatcher(zh->active_child_watchers, + path, watcher, watcherCtx); + break; + case ZWATCHTYPE_DATA: + watcher_found = containsWatcher(zh->active_node_watchers, path, + watcher, watcherCtx); + if (!watcher_found) { + watcher_found = containsWatcher(zh->active_exist_watchers, path, + watcher, watcherCtx); + } + break; + case ZWATCHTYPE_ANY: + watcher_found = containsWatcher(zh->active_child_watchers, path, + watcher, watcherCtx); + if (!watcher_found) { + watcher_found = containsWatcher(zh->active_node_watchers, path, + watcher, watcherCtx); + } + if (!watcher_found) { + watcher_found = containsWatcher(zh->active_exist_watchers, path, + watcher, watcherCtx); + } + break; + } + + return watcher_found; +} diff --git a/zookeeper-client/zookeeper-client-c/src/zk_hashtable.h b/zookeeper-client/zookeeper-client-c/src/zk_hashtable.h new file mode 100644 index 0000000..5227e07 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/zk_hashtable.h @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ZK_HASHTABLE_H_ +#define ZK_HASHTABLE_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct watcher_object_list watcher_object_list_t; +typedef struct _zk_hashtable zk_hashtable; + +/** + * The function must return a non-zero value if the watcher object can be activated + * as a result of the server response. Normally, a watch can only be activated + * if the server returns a success code (ZOK). However in the case when zoo_exists() + * returns a ZNONODE code the watcher should be activated nevertheless. + */ +typedef zk_hashtable *(*result_checker_fn)(zhandle_t *, int rc); + +/** + * A watcher object gets temporarily stored with the completion entry until + * the server response comes back at which moment the watcher object is moved + * to the active watchers map. + */ +typedef struct _watcher_registration { + watcher_fn watcher; + void* context; + result_checker_fn checker; + const char* path; +} watcher_registration_t; + +/** + * A watcher deregistration gets temporarily stored with the completion entry until + * the server response comes back at which moment we can remove the watchers from + * the active watchers map. + */ +typedef struct _watcher_deregistration { + watcher_fn watcher; + void* context; + ZooWatcherType type; + const char* path; +} watcher_deregistration_t; + +zk_hashtable* create_zk_hashtable(); +void destroy_zk_hashtable(zk_hashtable* ht); + +char **collect_keys(zk_hashtable *ht, int *count); + +/** + * check if the completion has a watcher object associated + * with it. If it does, move the watcher object to the map of + * active watchers (only if the checker allows to do so) + */ + void activateWatcher(zhandle_t *zh, watcher_registration_t* reg, int rc); + void deactivateWatcher(zhandle_t *zh, watcher_deregistration_t *dereg, int rc); + watcher_object_list_t *collectWatchers(zhandle_t *zh,int type, char *path); + void deliverWatchers(zhandle_t *zh, int type, int state, char *path, struct watcher_object_list **list); + void removeWatchers(zhandle_t *zh, const char* path, ZooWatcherType type, + watcher_fn watcher, void *watcherCtx); + int pathHasWatcher(zhandle_t *zh, const char *path, int wtype, + watcher_fn watcher, void *watcherCtx); + +#ifdef __cplusplus +} +#endif + +#endif /*ZK_HASHTABLE_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/src/zk_log.c b/zookeeper-client/zookeeper-client-c/src/zk_log.c new file mode 100644 index 0000000..436485e --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/zk_log.c @@ -0,0 +1,203 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#if !defined(DLL_EXPORT) && !defined(USE_STATIC_LIB) +# define USE_STATIC_LIB +#endif + +#include "zookeeper_log.h" +#ifndef WIN32 +#include +#else +typedef DWORD pid_t; +#include /* for getpid */ +#endif + +#include +#include + +#define TIME_NOW_BUF_SIZE 1024 +#define FORMAT_LOG_BUF_SIZE 4096 + +#ifdef THREADED +#ifndef WIN32 +#include +#else +#include "winport.h" +#endif + +static pthread_key_t time_now_buffer; +static pthread_key_t format_log_msg_buffer; + +void freeBuffer(void* p){ + if(p) free(p); +} + +__attribute__((constructor)) void prepareTSDKeys() { + pthread_key_create (&time_now_buffer, freeBuffer); + pthread_key_create (&format_log_msg_buffer, freeBuffer); +} + +char* getTSData(pthread_key_t key,int size){ + char* p=pthread_getspecific(key); + if(p==0){ + int res; + p=calloc(1,size); + res=pthread_setspecific(key,p); + if(res!=0){ + fprintf(stderr,"Failed to set TSD key: %d",res); + } + } + return p; +} + +char* get_time_buffer(){ + return getTSData(time_now_buffer,TIME_NOW_BUF_SIZE); +} + +char* get_format_log_buffer(){ + return getTSData(format_log_msg_buffer,FORMAT_LOG_BUF_SIZE); +} +#else +char* get_time_buffer(){ + static char buf[TIME_NOW_BUF_SIZE]; + return buf; +} + +char* get_format_log_buffer(){ + static char buf[FORMAT_LOG_BUF_SIZE]; + return buf; +} + +#endif + +ZooLogLevel logLevel=ZOO_LOG_LEVEL_INFO; + +static FILE* logStream=0; +FILE* zoo_get_log_stream(){ + if(logStream==0) + logStream=stderr; + return logStream; +} + +void zoo_set_log_stream(FILE* stream){ + logStream=stream; +} + +static const char* time_now(char* now_str){ + struct timeval tv; + struct tm lt; + time_t now = 0; + size_t len = 0; + + gettimeofday(&tv,0); + + now = tv.tv_sec; + localtime_r(&now, <); + + // clone the format used by log4j ISO8601DateFormat + // specifically: "yyyy-MM-dd HH:mm:ss,SSS" + + len = strftime(now_str, TIME_NOW_BUF_SIZE, + "%Y-%m-%d %H:%M:%S", + <); + + len += snprintf(now_str + len, + TIME_NOW_BUF_SIZE - len, + ",%03d", + (int)(tv.tv_usec/1000)); + + return now_str; +} + +void log_message(log_callback_fn callback, ZooLogLevel curLevel, + int line, const char* funcName, const char* format, ...) +{ + static const char* dbgLevelStr[]={"ZOO_INVALID","ZOO_ERROR","ZOO_WARN", + "ZOO_INFO","ZOO_DEBUG"}; + static pid_t pid=0; + va_list va; + int ofs = 0; +#ifdef THREADED + unsigned long int tid = 0; +#endif +#ifdef WIN32 + char timebuf [TIME_NOW_BUF_SIZE]; + const char* time = time_now(timebuf); +#else + const char* time = time_now(get_time_buffer()); +#endif + + char* buf = get_format_log_buffer(); + if(!buf) + { + fprintf(stderr, "log_message: Unable to allocate memory buffer"); + return; + } + + if(pid==0) + { + pid=getpid(); + } + + +#ifndef THREADED + + // pid_t is long on Solaris + ofs = snprintf(buf, FORMAT_LOG_BUF_SIZE, + "%s:%ld:%s@%s@%d: ", time, (long)pid, + dbgLevelStr[curLevel], funcName, line); +#else + + #ifdef WIN32 + tid = (unsigned long int)(pthread_self().thread_id); + #else + tid = (unsigned long int)(pthread_self()); + #endif + + ofs = snprintf(buf, FORMAT_LOG_BUF_SIZE-1, + "%s:%ld(0x%lx):%s@%s@%d: ", time, (long)pid, tid, + dbgLevelStr[curLevel], funcName, line); +#endif + + // Now grab the actual message out of the variadic arg list + va_start(va, format); + vsnprintf(buf+ofs, FORMAT_LOG_BUF_SIZE-1-ofs, format, va); + va_end(va); + + if (callback) + { + callback(buf); + } else { + fprintf(zoo_get_log_stream(), "%s\n", buf); + fflush(zoo_get_log_stream()); + } +} + +void zoo_set_debug_level(ZooLogLevel level) +{ + if(level==0){ + // disable logging (unit tests do this) + logLevel=(ZooLogLevel)0; + return; + } + if(levelZOO_LOG_LEVEL_DEBUG)level=ZOO_LOG_LEVEL_DEBUG; + logLevel=level; +} + diff --git a/zookeeper-client/zookeeper-client-c/src/zookeeper.c b/zookeeper-client/zookeeper-client-c/src/zookeeper.c new file mode 100644 index 0000000..89d61fb --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/src/zookeeper.c @@ -0,0 +1,5405 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#if !defined(DLL_EXPORT) && !defined(USE_STATIC_LIB) +# define USE_STATIC_LIB +#endif + +#if defined(__CYGWIN__) +#define USE_IPV6 +#endif + +#include "config.h" +#include +#include +#include +#include "zk_adaptor.h" +#include "zookeeper_log.h" +#include "zk_hashtable.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef HAVE_SYS_TIME_H +#include +#endif + +#ifdef HAVE_SYS_SOCKET_H +#include +#endif + +#ifdef HAVE_POLL +#include +#endif + +#ifdef HAVE_NETINET_IN_H +#include +#include +#endif + +#ifdef HAVE_ARPA_INET_H +#include +#endif + +#ifdef HAVE_NETDB_H +#include +#endif + +#ifdef HAVE_UNISTD_H +#include // needed for _POSIX_MONOTONIC_CLOCK +#endif + +#ifdef HAVE_SYS_UTSNAME_H +#include +#endif + +#ifdef HAVE_GETPWUID_R +#include +#endif + +#ifdef HAVE_OPENSSL_H +#include +#include +#endif + +#ifdef __MACH__ // OS X +#include +#include +#include +#endif + +#ifdef WIN32 +#include /* for getpid */ +#include /* for getcwd */ +#define EAI_ADDRFAMILY WSAEINVAL /* is this still needed? */ +#define EHOSTDOWN EPIPE +#define ESTALE ENODEV +#endif + +#define IF_DEBUG(x) if(logLevel==ZOO_LOG_LEVEL_DEBUG) {x;} + +const int ZOOKEEPER_WRITE = 1 << 0; +const int ZOOKEEPER_READ = 1 << 1; + +const int ZOO_PERSISTENT = 0; +const int ZOO_EPHEMERAL = 1; +const int ZOO_PERSISTENT_SEQUENTIAL = 2; +const int ZOO_EPHEMERAL_SEQUENTIAL = 3; +const int ZOO_CONTAINER = 4; +const int ZOO_PERSISTENT_WITH_TTL = 5; +const int ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL = 6; + +#define ZOOKEEPER_IS_SEQUENCE(mode) \ + ((mode) == ZOO_PERSISTENT_SEQUENTIAL || \ + (mode) == ZOO_EPHEMERAL_SEQUENTIAL || \ + (mode) == ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL) +#define ZOOKEEPER_IS_TTL(mode) \ + ((mode) == ZOO_PERSISTENT_WITH_TTL || \ + (mode) == ZOO_PERSISTENT_SEQUENTIAL_WITH_TTL) + +// keep ZOO_SEQUENCE as a bitmask for compatibility reasons +const int ZOO_SEQUENCE = 1 << 1; + +#define ZOO_MAX_TTL 0xFFFFFFFFFFLL + +const int ZOO_EXPIRED_SESSION_STATE = EXPIRED_SESSION_STATE_DEF; +const int ZOO_AUTH_FAILED_STATE = AUTH_FAILED_STATE_DEF; +const int ZOO_CONNECTING_STATE = CONNECTING_STATE_DEF; +const int ZOO_ASSOCIATING_STATE = ASSOCIATING_STATE_DEF; +const int ZOO_CONNECTED_STATE = CONNECTED_STATE_DEF; +const int ZOO_READONLY_STATE = READONLY_STATE_DEF; +const int ZOO_SSL_CONNECTING_STATE = SSL_CONNECTING_STATE_DEF; +const int ZOO_NOTCONNECTED_STATE = NOTCONNECTED_STATE_DEF; + +static __attribute__ ((unused)) const char* state2String(int state){ + switch(state){ + case 0: + return "ZOO_CLOSED_STATE"; + case CONNECTING_STATE_DEF: + return "ZOO_CONNECTING_STATE"; + case SSL_CONNECTING_STATE_DEF: + return "ZOO_SSL_CONNECTING_STATE"; + case ASSOCIATING_STATE_DEF: + return "ZOO_ASSOCIATING_STATE"; + case CONNECTED_STATE_DEF: + return "ZOO_CONNECTED_STATE"; + case READONLY_STATE_DEF: + return "ZOO_READONLY_STATE"; + case EXPIRED_SESSION_STATE_DEF: + return "ZOO_EXPIRED_SESSION_STATE"; + case AUTH_FAILED_STATE_DEF: + return "ZOO_AUTH_FAILED_STATE"; + } + return "INVALID_STATE"; +} + +const int ZOO_CREATED_EVENT = CREATED_EVENT_DEF; +const int ZOO_DELETED_EVENT = DELETED_EVENT_DEF; +const int ZOO_CHANGED_EVENT = CHANGED_EVENT_DEF; +const int ZOO_CHILD_EVENT = CHILD_EVENT_DEF; +const int ZOO_SESSION_EVENT = SESSION_EVENT_DEF; +const int ZOO_NOTWATCHING_EVENT = NOTWATCHING_EVENT_DEF; +static __attribute__ ((unused)) const char* watcherEvent2String(int ev){ + switch(ev){ + case 0: + return "ZOO_ERROR_EVENT"; + case CREATED_EVENT_DEF: + return "ZOO_CREATED_EVENT"; + case DELETED_EVENT_DEF: + return "ZOO_DELETED_EVENT"; + case CHANGED_EVENT_DEF: + return "ZOO_CHANGED_EVENT"; + case CHILD_EVENT_DEF: + return "ZOO_CHILD_EVENT"; + case SESSION_EVENT_DEF: + return "ZOO_SESSION_EVENT"; + case NOTWATCHING_EVENT_DEF: + return "ZOO_NOTWATCHING_EVENT"; + } + return "INVALID_EVENT"; +} + +const int ZOO_PERM_READ = 1 << 0; +const int ZOO_PERM_WRITE = 1 << 1; +const int ZOO_PERM_CREATE = 1 << 2; +const int ZOO_PERM_DELETE = 1 << 3; +const int ZOO_PERM_ADMIN = 1 << 4; +const int ZOO_PERM_ALL = 0x1f; +struct Id ZOO_ANYONE_ID_UNSAFE = {"world", "anyone"}; +struct Id ZOO_AUTH_IDS = {"auth", ""}; +static struct ACL _OPEN_ACL_UNSAFE_ACL[] = {{0x1f, {"world", "anyone"}}}; +static struct ACL _READ_ACL_UNSAFE_ACL[] = {{0x01, {"world", "anyone"}}}; +static struct ACL _CREATOR_ALL_ACL_ACL[] = {{0x1f, {"auth", ""}}}; +struct ACL_vector ZOO_OPEN_ACL_UNSAFE = { 1, _OPEN_ACL_UNSAFE_ACL}; +struct ACL_vector ZOO_READ_ACL_UNSAFE = { 1, _READ_ACL_UNSAFE_ACL}; +struct ACL_vector ZOO_CREATOR_ALL_ACL = { 1, _CREATOR_ALL_ACL_ACL}; + +#define COMPLETION_WATCH -1 +#define COMPLETION_VOID 0 +#define COMPLETION_STAT 1 +#define COMPLETION_DATA 2 +#define COMPLETION_STRINGLIST 3 +#define COMPLETION_STRINGLIST_STAT 4 +#define COMPLETION_ACLLIST 5 +#define COMPLETION_STRING 6 +#define COMPLETION_MULTI 7 +#define COMPLETION_STRING_STAT 8 + +typedef struct _auth_completion_list { + void_completion_t completion; + const char *auth_data; + struct _auth_completion_list *next; +} auth_completion_list_t; + +typedef struct completion { + int type; /* one of COMPLETION_* values above */ + union { + void_completion_t void_result; + stat_completion_t stat_result; + data_completion_t data_result; + strings_completion_t strings_result; + strings_stat_completion_t strings_stat_result; + acl_completion_t acl_result; + string_completion_t string_result; + string_stat_completion_t string_stat_result; + struct watcher_object_list *watcher_result; + }; + completion_head_t clist; /* For multi-op */ +} completion_t; + +typedef struct _completion_list { + int xid; + completion_t c; + const void *data; + buffer_list_t *buffer; + struct _completion_list *next; + watcher_registration_t* watcher; + watcher_deregistration_t* watcher_deregistration; +} completion_list_t; + +const char*err2string(int err); +static int queue_session_event(zhandle_t *zh, int state); +static const char* format_endpoint_info(const struct sockaddr_storage* ep); + +/* deserialize forward declarations */ +static void deserialize_response(zhandle_t *zh, int type, int xid, int failed, int rc, completion_list_t *cptr, struct iarchive *ia); +static int deserialize_multi(zhandle_t *zh, int xid, completion_list_t *cptr, struct iarchive *ia); + +/* completion routine forward declarations */ +static int add_completion(zhandle_t *zh, int xid, int completion_type, + const void *dc, const void *data, int add_to_front, + watcher_registration_t* wo, completion_head_t *clist); +static int add_completion_deregistration(zhandle_t *zh, int xid, + int completion_type, const void *dc, const void *data, + int add_to_front, watcher_deregistration_t* wo, + completion_head_t *clist); +static int do_add_completion(zhandle_t *zh, const void *dc, completion_list_t *c, + int add_to_front); + +static completion_list_t* create_completion_entry(zhandle_t *zh, int xid, int completion_type, + const void *dc, const void *data, watcher_registration_t* wo, + completion_head_t *clist); +static completion_list_t* create_completion_entry_deregistration(zhandle_t *zh, + int xid, int completion_type, const void *dc, const void *data, + watcher_deregistration_t* wo, completion_head_t *clist); +static completion_list_t* do_create_completion_entry(zhandle_t *zh, + int xid, int completion_type, const void *dc, const void *data, + watcher_registration_t* wo, completion_head_t *clist, + watcher_deregistration_t* wdo); +static void destroy_completion_entry(completion_list_t* c); +static void queue_completion_nolock(completion_head_t *list, completion_list_t *c, + int add_to_front); +static void queue_completion(completion_head_t *list, completion_list_t *c, + int add_to_front); +static int handle_socket_error_msg(zhandle_t *zh, int line, int rc, + const char* format,...); +static void cleanup_bufs(zhandle_t *zh,int callCompletion,int rc); + +static int disable_conn_permute=0; // permute enabled by default +static struct sockaddr_storage *addr_rw_server = 0; + +static void *SYNCHRONOUS_MARKER = (void*)&SYNCHRONOUS_MARKER; +static int isValidPath(const char* path, const int mode); +#ifdef HAVE_OPENSSL_H +static int init_ssl_for_handler(zhandle_t *zh); +static int init_ssl_for_socket(zsock_t *fd, zhandle_t *zh, int fail_on_error); +#endif + +static int aremove_watches( + zhandle_t *zh, const char *path, ZooWatcherType wtype, + watcher_fn watcher, void *watcherCtx, int local, + void_completion_t *completion, const void *data, int all); + +#ifdef THREADED +static void process_sync_completion(zhandle_t *zh, + completion_list_t *cptr, + struct sync_completion *sc, + struct iarchive *ia); + +static int remove_watches( + zhandle_t *zh, const char *path, ZooWatcherType wtype, + watcher_fn watcher, void *watcherCtx, int local, int all); +#endif + +#ifdef _WIN32 +typedef SOCKET socket_t; +typedef int sendsize_t; +#define SEND_FLAGS 0 +#else +#ifdef __APPLE__ +#define SEND_FLAGS SO_NOSIGPIPE +#endif +#ifdef __linux__ +#define SEND_FLAGS MSG_NOSIGNAL +#endif +#ifndef SEND_FLAGS +#define SEND_FLAGS 0 +#endif +typedef int socket_t; +typedef ssize_t sendsize_t; +#endif + +static void zookeeper_set_sock_nodelay(zhandle_t *, socket_t); +static void zookeeper_set_sock_noblock(zhandle_t *, socket_t); +static void zookeeper_set_sock_timeout(zhandle_t *, socket_t, int); +static socket_t zookeeper_connect(zhandle_t *, struct sockaddr_storage *, socket_t); + + +/* + * abort due to the use of a sync api in a singlethreaded environment + */ +#ifndef THREADED +static void abort_singlethreaded(zhandle_t *zh) +{ + LOG_ERROR(LOGCALLBACK(zh), "Sync completion used without threads"); + abort(); +} +#endif /* ifndef THREADED */ + +static ssize_t zookeeper_send(zsock_t *fd, const void* buf, size_t len) +{ +#ifdef HAVE_OPENSSL_H + if (fd->ssl_sock) + return (ssize_t)SSL_write(fd->ssl_sock, buf, (int)len); +#endif + return send(fd->sock, buf, len, SEND_FLAGS); +} + +static ssize_t zookeeper_recv(zsock_t *fd, void *buf, size_t len, int flags) +{ +#ifdef HAVE_OPENSSL_H + if (fd->ssl_sock) + return (ssize_t)SSL_read(fd->ssl_sock, buf, (int)len); +#endif + return recv(fd->sock, buf, len, flags); +} + +/** + * Get the system time. + * + * If the monotonic clock is available, we use that. The monotonic clock does + * not change when the wall-clock time is adjusted by NTP or the system + * administrator. The monotonic clock returns a value which is monotonically + * increasing. + * + * If POSIX monotonic clocks are not available, we fall back on the wall-clock. + * + * @param tv (out param) The time. + */ +void get_system_time(struct timeval *tv) +{ + int ret; + +#ifdef __MACH__ // OS X + clock_serv_t cclock; + mach_timespec_t mts; + ret = host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); + if (!ret) { + ret += clock_get_time(cclock, &mts); + ret += mach_port_deallocate(mach_task_self(), cclock); + if (!ret) { + tv->tv_sec = mts.tv_sec; + tv->tv_usec = mts.tv_nsec / 1000; + } + } + if (ret) { + // Default to gettimeofday in case of failure. + ret = gettimeofday(tv, NULL); + } +#elif defined CLOCK_MONOTONIC_RAW + // On Linux, CLOCK_MONOTONIC is affected by ntp slew but CLOCK_MONOTONIC_RAW + // is not. We want the non-slewed (constant rate) CLOCK_MONOTONIC_RAW if it + // is available. + struct timespec ts = { 0 }; + ret = clock_gettime(CLOCK_MONOTONIC_RAW, &ts); + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; +#elif _POSIX_MONOTONIC_CLOCK + struct timespec ts = { 0 }; + ret = clock_gettime(CLOCK_MONOTONIC, &ts); + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; +#elif _WIN32 + LARGE_INTEGER counts, countsPerSecond, countsPerMicrosecond; + if (QueryPerformanceFrequency(&countsPerSecond) && + QueryPerformanceCounter(&counts)) { + countsPerMicrosecond.QuadPart = countsPerSecond.QuadPart / 1000000; + tv->tv_sec = (long)(counts.QuadPart / countsPerSecond.QuadPart); + tv->tv_usec = (long)((counts.QuadPart % countsPerSecond.QuadPart) / + countsPerMicrosecond.QuadPart); + ret = 0; + } else { + ret = gettimeofday(tv, NULL); + } +#else + ret = gettimeofday(tv, NULL); +#endif + if (ret) { + abort(); + } +} + +const void *zoo_get_context(zhandle_t *zh) +{ + return zh->context; +} + +void zoo_set_context(zhandle_t *zh, void *context) +{ + if (zh != NULL) { + zh->context = context; + } +} + +int zoo_recv_timeout(zhandle_t *zh) +{ + return zh->recv_timeout; +} + +/** these functions are thread unsafe, so make sure that + zoo_lock_auth is called before you access them **/ +static auth_info* get_last_auth(auth_list_head_t *auth_list) { + auth_info *element; + element = auth_list->auth; + if (element == NULL) { + return NULL; + } + while (element->next != NULL) { + element = element->next; + } + return element; +} + +static void free_auth_completion(auth_completion_list_t *a_list) { + auth_completion_list_t *tmp, *ftmp; + if (a_list == NULL) { + return; + } + tmp = a_list->next; + while (tmp != NULL) { + ftmp = tmp; + tmp = tmp->next; + ftmp->completion = NULL; + ftmp->auth_data = NULL; + free(ftmp); + } + a_list->completion = NULL; + a_list->auth_data = NULL; + a_list->next = NULL; + return; +} + +static void add_auth_completion(auth_completion_list_t* a_list, void_completion_t* completion, + const char *data) { + auth_completion_list_t *element; + auth_completion_list_t *n_element; + element = a_list; + if (a_list->completion == NULL) { + //this is the first element + a_list->completion = *completion; + a_list->next = NULL; + a_list->auth_data = data; + return; + } + while (element->next != NULL) { + element = element->next; + } + n_element = (auth_completion_list_t*) malloc(sizeof(auth_completion_list_t)); + n_element->next = NULL; + n_element->completion = *completion; + n_element->auth_data = data; + element->next = n_element; + return; +} + +static void get_auth_completions(auth_list_head_t *auth_list, auth_completion_list_t *a_list) { + auth_info *element; + element = auth_list->auth; + if (element == NULL) { + return; + } + while (element) { + if (element->completion) { + add_auth_completion(a_list, &element->completion, element->data); + } + element->completion = NULL; + element = element->next; + } + return; +} + +static void add_last_auth(auth_list_head_t *auth_list, auth_info *add_el) { + auth_info *element; + element = auth_list->auth; + if (element == NULL) { + //first element in the list + auth_list->auth = add_el; + return; + } + while (element->next != NULL) { + element = element->next; + } + element->next = add_el; + return; +} + +static void init_auth_info(auth_list_head_t *auth_list) +{ + auth_list->auth = NULL; +} + +static void mark_active_auth(zhandle_t *zh) { + auth_list_head_t auth_h = zh->auth_h; + auth_info *element; + if (auth_h.auth == NULL) { + return; + } + element = auth_h.auth; + while (element != NULL) { + element->state = 1; + element = element->next; + } +} + +static void free_auth_info(auth_list_head_t *auth_list) +{ + auth_info *auth = auth_list->auth; + while (auth != NULL) { + auth_info* old_auth = NULL; + if(auth->scheme!=NULL) + free(auth->scheme); + deallocate_Buffer(&auth->auth); + old_auth = auth; + auth = auth->next; + free(old_auth); + } + init_auth_info(auth_list); +} + +int is_unrecoverable(zhandle_t *zh) +{ + return (zh->state<0)? ZINVALIDSTATE: ZOK; +} + +zk_hashtable *exists_result_checker(zhandle_t *zh, int rc) +{ + if (rc == ZOK) { + return zh->active_node_watchers; + } else if (rc == ZNONODE) { + return zh->active_exist_watchers; + } + return 0; +} + +zk_hashtable *data_result_checker(zhandle_t *zh, int rc) +{ + return rc==ZOK ? zh->active_node_watchers : 0; +} + +zk_hashtable *child_result_checker(zhandle_t *zh, int rc) +{ + return rc==ZOK ? zh->active_child_watchers : 0; +} + +void close_zsock(zsock_t *fd) +{ + if (fd->sock != -1) { +#ifdef HAVE_OPENSSL_H + if (fd->ssl_sock) { + SSL_free(fd->ssl_sock); + fd->ssl_sock = NULL; + SSL_CTX_free(fd->ssl_ctx); + fd->ssl_ctx = NULL; + } +#endif + close(fd->sock); + fd->sock = -1; + } +} + +/** + * Frees and closes everything associated with a handle, + * including the handle itself. + */ +static void destroy(zhandle_t *zh) +{ + if (zh == NULL) { + return; + } + /* call any outstanding completions with a special error code */ + cleanup_bufs(zh,1,ZCLOSING); + if (process_async(zh->outstanding_sync)) { + process_completions(zh); + } + if (zh->hostname != 0) { + free(zh->hostname); + zh->hostname = NULL; + } + if (zh->fd->sock != -1) { + close_zsock(zh->fd); + memset(&zh->addr_cur, 0, sizeof(zh->addr_cur)); + zh->state = 0; + } + addrvec_free(&zh->addrs); + + if (zh->chroot != NULL) { + free(zh->chroot); + zh->chroot = NULL; + } +#ifdef HAVE_OPENSSL_H + if (zh->fd->cert) { + free(zh->fd->cert->certstr); + free(zh->fd->cert); + zh->fd->cert = NULL; + } +#endif + free_auth_info(&zh->auth_h); + destroy_zk_hashtable(zh->active_node_watchers); + destroy_zk_hashtable(zh->active_exist_watchers); + destroy_zk_hashtable(zh->active_child_watchers); + addrvec_free(&zh->addrs_old); + addrvec_free(&zh->addrs_new); +} + +static void setup_random() +{ +#ifndef _WIN32 // TODO: better seed + int seed; + int fd = open("/dev/urandom", O_RDONLY); + if (fd == -1) { + seed = getpid(); + } else { + int seed_len = 0; + + /* Enter a loop to fill in seed with random data from /dev/urandom. + * This is done in a loop so that we can safely handle short reads + * which can happen due to signal interruptions. + */ + while (seed_len < sizeof(seed)) { + /* Assert we either read something or we were interrupted due to a + * signal (errno == EINTR) in which case we need to retry. + */ + int rc = read(fd, &seed + seed_len, sizeof(seed) - seed_len); + assert(rc > 0 || errno == EINTR); + if (rc > 0) { + seed_len += rc; + } + } + close(fd); + } + srandom(seed); + srand48(seed); +#endif +} + +#ifndef __CYGWIN__ +/** + * get the errno from the return code + * of get addrinfo. Errno is not set + * with the call to getaddrinfo, so thats + * why we have to do this. + */ +static int getaddrinfo_errno(int rc) { + switch(rc) { + case EAI_NONAME: +// ZOOKEEPER-1323 EAI_NODATA and EAI_ADDRFAMILY are deprecated in FreeBSD. +#if defined EAI_NODATA && EAI_NODATA != EAI_NONAME + case EAI_NODATA: +#endif + return ENOENT; + case EAI_MEMORY: + return ENOMEM; + default: + return EINVAL; + } +} +#endif + +/** + * Count the number of hosts in the connection host string. This assumes it's + * a well-formed connection string whereby each host is separated by a comma. + */ +static int count_hosts(char *hosts) +{ + uint32_t count = 0; + char *loc = hosts; + if (!hosts || strlen(hosts) == 0) { + return 0; + } + + while ((loc = strchr(loc, ','))) { + count++; + loc+=1; + } + + return count+1; +} + +/** + * Resolve hosts and populate provided address vector with shuffled results. + * The contents of the provided address vector will be initialized to an + * empty state. + */ +static int resolve_hosts(const zhandle_t *zh, const char *hosts_in, addrvec_t *avec) +{ + int rc = ZOK; + char *host = NULL; + char *hosts = NULL; + int num_hosts = 0; + char *strtok_last = NULL; + + if (zh == NULL || hosts_in == NULL || avec == NULL) { + return ZBADARGUMENTS; + } + + // initialize address vector + addrvec_init(avec); + + hosts = strdup(hosts_in); + if (hosts == NULL) { + LOG_ERROR(LOGCALLBACK(zh), "out of memory"); + errno=ENOMEM; + rc=ZSYSTEMERROR; + goto fail; + } + + num_hosts = count_hosts(hosts); + if (num_hosts == 0) { + free(hosts); + return ZOK; + } + + // Allocate list inside avec + rc = addrvec_alloc_capacity(avec, num_hosts); + if (rc != 0) { + LOG_ERROR(LOGCALLBACK(zh), "out of memory"); + errno=ENOMEM; + rc=ZSYSTEMERROR; + goto fail; + } + + host = strtok_r(hosts, ",", &strtok_last); + while(host) { + char *port_spec = strrchr(host, ':'); + char *end_port_spec; + int port; + if (!port_spec) { + LOG_ERROR(LOGCALLBACK(zh), "no port in %s", host); + errno=EINVAL; + rc=ZBADARGUMENTS; + goto fail; + } + *port_spec = '\0'; + port_spec++; + port = strtol(port_spec, &end_port_spec, 0); + if (!*port_spec || *end_port_spec || port == 0) { + LOG_ERROR(LOGCALLBACK(zh), "invalid port in %s", host); + errno=EINVAL; + rc=ZBADARGUMENTS; + goto fail; + } +#if defined(__CYGWIN__) + // sadly CYGWIN doesn't have getaddrinfo + // but happily gethostbyname is threadsafe in windows + { + struct hostent *he; + char **ptr; + struct sockaddr_in *addr4; + + he = gethostbyname(host); + if (!he) { + LOG_ERROR(LOGCALLBACK(zh), "could not resolve %s", host); + errno=ENOENT; + rc=ZBADARGUMENTS; + goto fail; + } + + // Setup the address array + for(ptr = he->h_addr_list;*ptr != 0; ptr++) { + if (addrs->count == addrs->capacity) { + rc = addrvec_grow_default(addrs); + if (rc != 0) { + LOG_ERROR(LOGCALLBACK(zh), "out of memory"); + errno=ENOMEM; + rc=ZSYSTEMERROR; + goto fail; + } + } + addr = &addrs->list[addrs->count]; + addr4 = (struct sockaddr_in*)addr; + addr->ss_family = he->h_addrtype; + if (addr->ss_family == AF_INET) { + addr4->sin_port = htons(port); + memset(&addr4->sin_zero, 0, sizeof(addr4->sin_zero)); + memcpy(&addr4->sin_addr, *ptr, he->h_length); + zh->addrs.count++; + } +#if defined(AF_INET6) + else if (addr->ss_family == AF_INET6) { + struct sockaddr_in6 *addr6; + + addr6 = (struct sockaddr_in6*)addr; + addr6->sin6_port = htons(port); + addr6->sin6_scope_id = 0; + addr6->sin6_flowinfo = 0; + memcpy(&addr6->sin6_addr, *ptr, he->h_length); + zh->addrs.count++; + } +#endif + else { + LOG_WARN(LOGCALLBACK(zh), "skipping unknown address family %x for %s", + addr->ss_family, hosts_in); + } + } + host = strtok_r(0, ",", &strtok_last); + } +#else + { + struct addrinfo hints, *res, *res0; + + memset(&hints, 0, sizeof(hints)); +#ifdef AI_ADDRCONFIG + hints.ai_flags = AI_ADDRCONFIG; +#else + hints.ai_flags = 0; +#endif + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; + + while(isspace(*host) && host != strtok_last) + host++; + + if ((rc = getaddrinfo(host, port_spec, &hints, &res0)) != 0) { + //bug in getaddrinfo implementation when it returns + //EAI_BADFLAGS or EAI_ADDRFAMILY with AF_UNSPEC and + // ai_flags as AI_ADDRCONFIG +#ifdef AI_ADDRCONFIG + if ((hints.ai_flags == AI_ADDRCONFIG) && +// ZOOKEEPER-1323 EAI_NODATA and EAI_ADDRFAMILY are deprecated in FreeBSD. +#ifdef EAI_ADDRFAMILY + ((rc ==EAI_BADFLAGS) || (rc == EAI_ADDRFAMILY))) { +#else + (rc == EAI_BADFLAGS)) { +#endif + //reset ai_flags to null + hints.ai_flags = 0; + //retry getaddrinfo + rc = getaddrinfo(host, port_spec, &hints, &res0); + } +#endif + if (rc != 0) { + errno = getaddrinfo_errno(rc); +#ifdef _WIN32 + LOG_ERROR(LOGCALLBACK(zh), "Win32 message: %s\n", gai_strerror(rc)); +#elif __linux__ && __GNUC__ + LOG_ERROR(LOGCALLBACK(zh), "getaddrinfo: %s\n", gai_strerror(rc)); +#else + LOG_ERROR(LOGCALLBACK(zh), "getaddrinfo: %s\n", strerror(errno)); +#endif + rc=ZSYSTEMERROR; + goto next; + } + } + + for (res = res0; res; res = res->ai_next) { + // Expand address list if needed + if (avec->count == avec->capacity) { + rc = addrvec_grow_default(avec); + if (rc != 0) { + LOG_ERROR(LOGCALLBACK(zh), "out of memory"); + errno=ENOMEM; + rc=ZSYSTEMERROR; + goto fail; + } + } + + // Copy addrinfo into address list + switch (res->ai_family) { + case AF_INET: +#if defined(AF_INET6) + case AF_INET6: +#endif + addrvec_append_addrinfo(avec, res); + break; + default: + LOG_WARN(LOGCALLBACK(zh), "skipping unknown address family %x for %s", + res->ai_family, hosts_in); + break; + } + } + + freeaddrinfo(res0); +next: + host = strtok_r(0, ",", &strtok_last); + } +#endif + } + if (avec->count == 0) { + rc = ZSYSTEMERROR; // not a single host resolved + goto fail; + } + + free(hosts); + + if(!disable_conn_permute){ + setup_random(); + addrvec_shuffle(avec); + } + + return ZOK; + +fail: + addrvec_free(avec); + + if (hosts) { + free(hosts); + hosts = NULL; + } + + return rc; +} + +/** + * Updates the list of servers and determine if changing connections is necessary. + * Permutes server list for proper load balancing. + * + * Changing connections is necessary if one of the following holds: + * a) the server this client is currently connected is not in new address list. + * Otherwise (if currentHost is in the new list): + * b) the number of servers in the cluster is increasing - in this case the load + * on currentHost should decrease, which means that SOME of the clients + * connected to it will migrate to the new servers. The decision whether this + * client migrates or not is probabilistic so that the expected number of + * clients connected to each server is the same. + * + * If reconfig is set to true, the function sets pOld and pNew that correspond + * to the probability to migrate to ones of the new servers or one of the old + * servers (migrating to one of the old servers is done only if our client's + * currentHost is not in new list). + * + * See zoo_cycle_next_server for the selection logic. + * + * See {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1355} for the + * protocol and its evaluation, + */ +int update_addrs(zhandle_t *zh) +{ + int rc = ZOK; + char *hosts = NULL; + uint32_t num_old = 0; + uint32_t num_new = 0; + uint32_t i = 0; + int found_current = 0; + addrvec_t resolved = { 0 }; + + // Verify we have a valid handle + if (zh == NULL) { + return ZBADARGUMENTS; + } + + // zh->hostname should always be set + if (zh->hostname == NULL) + { + return ZSYSTEMERROR; + } + + // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new} + lock_reconfig(zh); + + // Copy zh->hostname for local use + hosts = strdup(zh->hostname); + if (hosts == NULL) { + rc = ZSYSTEMERROR; + goto fail; + } + + rc = resolve_hosts(zh, hosts, &resolved); + if (rc != ZOK) + { + goto fail; + } + + // If the addrvec list is identical to last time we ran don't do anything + if (addrvec_eq(&zh->addrs, &resolved)) + { + goto fail; + } + + // Is the server we're connected to in the new resolved list? + found_current = addrvec_contains(&resolved, &zh->addr_cur); + + // Clear out old and new address lists + zh->reconfig = 1; + addrvec_free(&zh->addrs_old); + addrvec_free(&zh->addrs_new); + + // Divide server list into addrs_old if in previous list and addrs_new if not + for (i = 0; i < resolved.count; i++) + { + struct sockaddr_storage *resolved_address = &resolved.data[i]; + if (addrvec_contains(&zh->addrs, resolved_address)) + { + rc = addrvec_append(&zh->addrs_old, resolved_address); + if (rc != ZOK) + { + goto fail; + } + } + else { + rc = addrvec_append(&zh->addrs_new, resolved_address); + if (rc != ZOK) + { + goto fail; + } + } + } + + num_old = zh->addrs_old.count; + num_new = zh->addrs_new.count; + + // Number of servers increased + if (num_old + num_new > zh->addrs.count) + { + if (found_current) { + // my server is in the new config, but load should be decreased. + // Need to decide if the client is moving to one of the new servers + if (drand48() <= (1 - ((double)zh->addrs.count) / (num_old + num_new))) { + zh->pNew = 1; + zh->pOld = 0; + } else { + // do nothing special -- stay with the current server + zh->reconfig = 0; + } + } else { + // my server is not in the new config, and load on old servers must + // be decreased, so connect to one of the new servers + zh->pNew = 1; + zh->pOld = 0; + } + } + + // Number of servers stayed the same or decreased + else { + if (found_current) { + // my server is in the new config, and load should be increased, so + // stay with this server and do nothing special + zh->reconfig = 0; + } else { + zh->pOld = ((double) (num_old * (zh->addrs.count - (num_old + num_new)))) / ((num_old + num_new) * (zh->addrs.count - num_old)); + zh->pNew = 1 - zh->pOld; + } + } + + addrvec_free(&zh->addrs); + zh->addrs = resolved; + + // If we need to do a reconfig and we're currently connected to a server, + // then force close that connection so on next interest() call we'll make a + // new connection + if (zh->reconfig == 1 && zh->fd->sock != -1) + { + close_zsock(zh->fd); + zh->state = ZOO_NOTCONNECTED_STATE; + } + +fail: + + unlock_reconfig(zh); + + // If we short-circuited out and never assigned resolved to zh->addrs then we + // need to free resolved to avoid a memleak. + if (zh->addrs.data != resolved.data) + { + addrvec_free(&resolved); + } + + if (hosts) { + free(hosts); + hosts = NULL; + } + + return rc; +} + +const clientid_t *zoo_client_id(zhandle_t *zh) +{ + return &zh->client_id; +} + +static void null_watcher_fn(zhandle_t* p1, int p2, int p3,const char* p4,void*p5){} + +watcher_fn zoo_set_watcher(zhandle_t *zh,watcher_fn newFn) +{ + watcher_fn oldWatcher=zh->watcher; + if (newFn) { + zh->watcher = newFn; + } else { + zh->watcher = null_watcher_fn; + } + return oldWatcher; +} + +struct sockaddr* zookeeper_get_connected_host(zhandle_t *zh, + struct sockaddr *addr, socklen_t *addr_len) +{ + if (zh->state!=ZOO_CONNECTED_STATE) { + return NULL; + } + if (getpeername(zh->fd->sock, addr, addr_len)==-1) { + return NULL; + } + return addr; +} + +static void log_env(zhandle_t *zh) { + char buf[2048]; +#ifdef HAVE_SYS_UTSNAME_H + struct utsname utsname; +#endif + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID_R) + struct passwd pw; + struct passwd *pwp = NULL; + uid_t uid = 0; +#endif + + LOG_INFO(LOGCALLBACK(zh), "Client environment:zookeeper.version=%s", PACKAGE_STRING); + +#ifdef HAVE_GETHOSTNAME + gethostname(buf, sizeof(buf)); + LOG_INFO(LOGCALLBACK(zh), "Client environment:host.name=%s", buf); +#else + LOG_INFO(LOGCALLBACK(zh), "Client environment:host.name="); +#endif + +#ifdef HAVE_SYS_UTSNAME_H + uname(&utsname); + LOG_INFO(LOGCALLBACK(zh), "Client environment:os.name=%s", utsname.sysname); + LOG_INFO(LOGCALLBACK(zh), "Client environment:os.arch=%s", utsname.release); + LOG_INFO(LOGCALLBACK(zh), "Client environment:os.version=%s", utsname.version); +#else + LOG_INFO(LOGCALLBACK(zh), "Client environment:os.name="); + LOG_INFO(LOGCALLBACK(zh), "Client environment:os.arch="); + LOG_INFO(LOGCALLBACK(zh), "Client environment:os.version="); +#endif + +#ifdef HAVE_GETLOGIN + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.name=%s", getlogin()); +#else + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.name="); +#endif + +#if defined(HAVE_GETUID) && defined(HAVE_GETPWUID_R) + uid = getuid(); + if (!getpwuid_r(uid, &pw, buf, sizeof(buf), &pwp) && pwp) { + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.home=%s", pw.pw_dir); + } else { + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.home="); + } +#else + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.home="); +#endif + +#ifdef HAVE_GETCWD + if (!getcwd(buf, sizeof(buf))) { + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.dir="); + } else { + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.dir=%s", buf); + } +#else + LOG_INFO(LOGCALLBACK(zh), "Client environment:user.dir="); +#endif +} + +/** + * Create a zookeeper handle associated with the given host and port. + */ +static zhandle_t *zookeeper_init_internal(const char *host, watcher_fn watcher, + int recv_timeout, const clientid_t *clientid, void *context, int flags, + log_callback_fn log_callback, zcert_t *cert) +{ + int errnosave = 0; + zhandle_t *zh = NULL; + char *index_chroot = NULL; + + // Create our handle + zh = calloc(1, sizeof(*zh)); + if (!zh) { + return 0; + } + + // Set log callback before calling into log_env + zh->log_callback = log_callback; + + if (!(flags & ZOO_NO_LOG_CLIENTENV)) { + log_env(zh); + } + + zh->fd = calloc(1, sizeof(zsock_t)); + zh->fd->sock = -1; + if (cert) { + zh->fd->cert = calloc(1, sizeof(zcert_t)); + memcpy(zh->fd->cert, cert, sizeof(zcert_t)); + } + +#ifdef _WIN32 + if (Win32WSAStartup()){ + LOG_ERROR(LOGCALLBACK(zh), "Error initializing ws2_32.dll"); + return 0; + } +#endif + LOG_INFO(LOGCALLBACK(zh), "Initiating client connection, host=%s sessionTimeout=%d watcher=%p" + " sessionId=%#llx sessionPasswd=%s context=%p flags=%d", + host, + recv_timeout, + watcher, + (clientid == 0 ? 0 : clientid->client_id), + ((clientid == 0) || (clientid->passwd[0] == 0) ? + "" : ""), + context, + flags); + + zh->hostname = NULL; + zh->state = ZOO_NOTCONNECTED_STATE; + zh->context = context; + zh->recv_timeout = recv_timeout; + zh->allow_read_only = flags & ZOO_READONLY; + // non-zero clientid implies we've seen r/w server already + zh->seen_rw_server_before = (clientid != 0 && clientid->client_id != 0); + init_auth_info(&zh->auth_h); + if (watcher) { + zh->watcher = watcher; + } else { + zh->watcher = null_watcher_fn; + } + if (host == 0 || *host == 0) { // what we shouldn't dup + errno=EINVAL; + goto abort; + } + //parse the host to get the chroot if available + index_chroot = strchr(host, '/'); + if (index_chroot) { + zh->chroot = strdup(index_chroot); + if (zh->chroot == NULL) { + goto abort; + } + // if chroot is just / set it to null + if (strlen(zh->chroot) == 1) { + free(zh->chroot); + zh->chroot = NULL; + } + // cannot use strndup so allocate and strcpy + zh->hostname = (char *) malloc(index_chroot - host + 1); + zh->hostname = strncpy(zh->hostname, host, (index_chroot - host)); + //strncpy does not null terminate + *(zh->hostname + (index_chroot - host)) = '\0'; + + } else { + zh->chroot = NULL; + zh->hostname = strdup(host); + } + if (zh->chroot && !isValidPath(zh->chroot, 0)) { + errno = EINVAL; + goto abort; + } + if (zh->hostname == 0) { + goto abort; + } + if(update_addrs(zh) != 0) { + goto abort; + } + + if (clientid) { + memcpy(&zh->client_id, clientid, sizeof(zh->client_id)); + } else { + memset(&zh->client_id, 0, sizeof(zh->client_id)); + } + zh->io_count = 0; + zh->primer_buffer.buffer = zh->primer_storage_buffer; + zh->primer_buffer.curr_offset = 0; + zh->primer_buffer.len = sizeof(zh->primer_storage_buffer); + zh->primer_buffer.next = 0; + zh->last_zxid = 0; + zh->next_deadline.tv_sec=zh->next_deadline.tv_usec=0; + zh->socket_readable.tv_sec=zh->socket_readable.tv_usec=0; + zh->active_node_watchers=create_zk_hashtable(); + zh->active_exist_watchers=create_zk_hashtable(); + zh->active_child_watchers=create_zk_hashtable(); + zh->disable_reconnection_attempt = 0; + + if (adaptor_init(zh) == -1) { + goto abort; + } + + return zh; +abort: + errnosave=errno; + destroy(zh); + free(zh->fd); + free(zh); + errno=errnosave; + return 0; +} + +zhandle_t *zookeeper_init(const char *host, watcher_fn watcher, + int recv_timeout, const clientid_t *clientid, void *context, int flags) +{ + return zookeeper_init_internal(host, watcher, recv_timeout, clientid, context, flags, NULL, NULL); +} + +zhandle_t *zookeeper_init2(const char *host, watcher_fn watcher, + int recv_timeout, const clientid_t *clientid, void *context, int flags, + log_callback_fn log_callback) +{ + return zookeeper_init_internal(host, watcher, recv_timeout, clientid, context, flags, log_callback, NULL); +} + +#ifdef HAVE_OPENSSL_H +zhandle_t *zookeeper_init_ssl(const char *host, const char *cert, watcher_fn watcher, + int recv_timeout, const clientid_t *clientid, void *context, int flags) +{ + zcert_t zcert; + zcert.certstr = strdup(cert); + zcert.ca = strtok(strdup(cert), ","); + zcert.cert = strtok(NULL, ","); + zcert.key = strtok(NULL, ","); + zcert.passwd = strtok(NULL, ","); + return zookeeper_init_internal(host, watcher, recv_timeout, clientid, context, flags, NULL, &zcert); +} +#endif + +/** + * Set a new list of zk servers to connect to. Disconnect will occur if + * current connection endpoint is not in the list. + */ +int zoo_set_servers(zhandle_t *zh, const char *hosts) +{ + if (hosts == NULL) + { + LOG_ERROR(LOGCALLBACK(zh), "New server list cannot be empty"); + return ZBADARGUMENTS; + } + + // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new} + lock_reconfig(zh); + + // Reset hostname to new set of hosts to connect to + if (zh->hostname) { + free(zh->hostname); + } + + zh->hostname = strdup(hosts); + + unlock_reconfig(zh); + + return update_addrs(zh); +} + +/** + * Get the next server to connect to, when in 'reconfig' mode, which means that + * we've updated the server list to connect to, and are now trying to find some + * server to connect to. Once we get successfully connected, 'reconfig' mode is + * set to false. Similarly, if we tried to connect to all servers in new config + * and failed, 'reconfig' mode is set to false. + * + * While in 'reconfig' mode, we should connect to a server in the new set of + * servers (addrs_new) with probability pNew and to servers in the old set of + * servers (addrs_old) with probability pOld (which is just 1-pNew). If we tried + * out all servers in either, we continue to try servers from the other set, + * regardless of pNew or pOld. If we tried all servers we give up and go back to + * the normal round robin mode + * + * When called, must be protected by lock_reconfig(zh). + */ +static int get_next_server_in_reconfig(zhandle_t *zh) +{ + int take_new = drand48() <= zh->pNew; + + LOG_DEBUG(LOGCALLBACK(zh), "[OLD] count=%d capacity=%d next=%d hasnext=%d", + zh->addrs_old.count, zh->addrs_old.capacity, zh->addrs_old.next, + addrvec_hasnext(&zh->addrs_old)); + LOG_DEBUG(LOGCALLBACK(zh), "[NEW] count=%d capacity=%d next=%d hasnext=%d", + zh->addrs_new.count, zh->addrs_new.capacity, zh->addrs_new.next, + addrvec_hasnext(&zh->addrs_new)); + + // Take one of the new servers if we haven't tried them all yet + // and either the probability tells us to connect to one of the new servers + // or if we already tried them all then use one of the old servers + if (addrvec_hasnext(&zh->addrs_new) + && (take_new || !addrvec_hasnext(&zh->addrs_old))) + { + addrvec_next(&zh->addrs_new, &zh->addr_cur); + LOG_DEBUG(LOGCALLBACK(zh), "Using next from NEW=%s", format_endpoint_info(&zh->addr_cur)); + return 0; + } + + // start taking old servers + if (addrvec_hasnext(&zh->addrs_old)) { + addrvec_next(&zh->addrs_old, &zh->addr_cur); + LOG_DEBUG(LOGCALLBACK(zh), "Using next from OLD=%s", format_endpoint_info(&zh->addr_cur)); + return 0; + } + + LOG_DEBUG(LOGCALLBACK(zh), "Failed to find either new or old"); + memset(&zh->addr_cur, 0, sizeof(zh->addr_cur)); + return 1; +} + +/** + * Cycle through our server list to the correct 'next' server. The 'next' server + * to connect to depends upon whether we're in a 'reconfig' mode or not. Reconfig + * mode means we've upated the server list and are now trying to find a server + * to connect to. Once we get connected, we are no longer in the reconfig mode. + * Similarly, if we try to connect to all the servers in the new configuration + * and failed, reconfig mode is set to false. + * + * For more algorithm details, see get_next_server_in_reconfig. + */ +void zoo_cycle_next_server(zhandle_t *zh) +{ + // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new} + lock_reconfig(zh); + + memset(&zh->addr_cur, 0, sizeof(zh->addr_cur)); + + if (zh->reconfig) + { + if (get_next_server_in_reconfig(zh) == 0) { + unlock_reconfig(zh); + return; + } + + // tried all new and old servers and couldn't connect + zh->reconfig = 0; + } + + addrvec_next(&zh->addrs, &zh->addr_cur); + + unlock_reconfig(zh); + + return; +} + +/** + * Get the host:port for the server we are currently connecting to or connected + * to. This is largely for testing purposes but is also generally useful for + * other client software built on top of this client. + */ +const char* zoo_get_current_server(zhandle_t* zh) +{ + const char *endpoint_info = NULL; + + // NOTE: guard access to {hostname, addr_cur, addrs, addrs_old, addrs_new} + // Need the lock here as it is changed in update_addrs() + lock_reconfig(zh); + + endpoint_info = format_endpoint_info(&zh->addr_cur); + unlock_reconfig(zh); + return endpoint_info; +} + +/** + * deallocated the free_path only its beeen allocated + * and not equal to path + */ +void free_duplicate_path(const char *free_path, const char* path) { + if (free_path != path) { + free((void*)free_path); + } +} + +/** + prepend the chroot path if available else return the path +*/ +static char* prepend_string(zhandle_t *zh, const char* client_path) { + char *ret_str; + if (zh == NULL || zh->chroot == NULL) + return (char *) client_path; + // handle the chroot itself, client_path = "/" + if (strlen(client_path) == 1) { + return strdup(zh->chroot); + } + ret_str = (char *) malloc(strlen(zh->chroot) + strlen(client_path) + 1); + strcpy(ret_str, zh->chroot); + return strcat(ret_str, client_path); +} + +/** + strip off the chroot string from the server path + if there is one else return the exact path + */ +char* sub_string(zhandle_t *zh, const char* server_path) { + char *ret_str; + if (zh->chroot == NULL) + return (char *) server_path; + //ZOOKEEPER-1027 + if (strncmp(server_path, zh->chroot, strlen(zh->chroot)) != 0) { + LOG_ERROR(LOGCALLBACK(zh), "server path %s does not include chroot path %s", + server_path, zh->chroot); + return (char *) server_path; + } + if (strlen(server_path) == strlen(zh->chroot)) { + //return "/" + ret_str = strdup("/"); + return ret_str; + } + ret_str = strdup(server_path + strlen(zh->chroot)); + return ret_str; +} + +static buffer_list_t *allocate_buffer(char *buff, int len) +{ + buffer_list_t *buffer = calloc(1, sizeof(*buffer)); + if (buffer == 0) + return 0; + + buffer->len = len==0?sizeof(*buffer):len; + buffer->curr_offset = 0; + buffer->buffer = buff; + buffer->next = 0; + return buffer; +} + +static void free_buffer(buffer_list_t *b) +{ + if (!b) { + return; + } + if (b->buffer) { + free(b->buffer); + } + free(b); +} + +static buffer_list_t *dequeue_buffer(buffer_head_t *list) +{ + buffer_list_t *b; + lock_buffer_list(list); + b = list->head; + if (b) { + list->head = b->next; + if (!list->head) { + assert(b == list->last); + list->last = 0; + } + } + unlock_buffer_list(list); + return b; +} + +static int remove_buffer(buffer_head_t *list) +{ + buffer_list_t *b = dequeue_buffer(list); + if (!b) { + return 0; + } + free_buffer(b); + return 1; +} + +static void queue_buffer(buffer_head_t *list, buffer_list_t *b, int add_to_front) +{ + b->next = 0; + lock_buffer_list(list); + if (list->head) { + assert(list->last); + // The list is not empty + if (add_to_front) { + b->next = list->head; + list->head = b; + } else { + list->last->next = b; + list->last = b; + } + }else{ + // The list is empty + assert(!list->head); + list->head = b; + list->last = b; + } + unlock_buffer_list(list); +} + +static int queue_buffer_bytes(buffer_head_t *list, char *buff, int len) +{ + buffer_list_t *b = allocate_buffer(buff,len); + if (!b) + return ZSYSTEMERROR; + queue_buffer(list, b, 0); + return ZOK; +} + +static int queue_front_buffer_bytes(buffer_head_t *list, char *buff, int len) +{ + buffer_list_t *b = allocate_buffer(buff,len); + if (!b) + return ZSYSTEMERROR; + queue_buffer(list, b, 1); + return ZOK; +} + +static __attribute__ ((unused)) int get_queue_len(buffer_head_t *list) +{ + int i; + buffer_list_t *ptr; + lock_buffer_list(list); + ptr = list->head; + for (i=0; ptr!=0; ptr=ptr->next, i++) + ; + unlock_buffer_list(list); + return i; +} +/* returns: + * -1 if send failed, + * 0 if send would block while sending the buffer (or a send was incomplete), + * 1 if success + */ +static int send_buffer(zhandle_t *zh, buffer_list_t *buff) +{ + int len = buff->len; + int off = buff->curr_offset; + int rc = -1; + + if (off < 4) { + /* we need to send the length at the beginning */ + int nlen = htonl(len); + char *b = (char*)&nlen; + rc = zookeeper_send(zh->fd, b + off, sizeof(nlen) - off); + if (rc == -1) { +#ifdef _WIN32 + if (WSAGetLastError() != WSAEWOULDBLOCK) { +#else + if (errno != EAGAIN) { +#endif + return -1; + } else { + return 0; + } + } else { + buff->curr_offset += rc; + } + off = buff->curr_offset; + } + if (off >= 4) { + /* want off to now represent the offset into the buffer */ + off -= sizeof(buff->len); + rc = zookeeper_send(zh->fd, buff->buffer + off, len - off); + if (rc == -1) { +#ifdef _WIN32 + if (WSAGetLastError() != WSAEWOULDBLOCK) { +#else + if (errno != EAGAIN) { +#endif + return -1; + } + } else { + buff->curr_offset += rc; + } + } + return buff->curr_offset == len + sizeof(buff->len); +} + +/* returns: + * -1 if recv call failed, + * 0 if recv would block, + * 1 if success + */ +static int recv_buffer(zhandle_t *zh, buffer_list_t *buff) +{ + int off = buff->curr_offset; + int rc = 0; + + /* if buffer is less than 4, we are reading in the length */ + if (off < 4) { + char *buffer = (char*)&(buff->len); + rc = zookeeper_recv(zh->fd, buffer+off, sizeof(int)-off, 0); + switch (rc) { + case 0: + errno = EHOSTDOWN; + case -1: +#ifdef _WIN32 + if (WSAGetLastError() == WSAEWOULDBLOCK) { +#else + if (errno == EAGAIN) { +#endif + return 0; + } + return -1; + default: + buff->curr_offset += rc; + } + off = buff->curr_offset; + if (buff->curr_offset == sizeof(buff->len)) { + buff->len = ntohl(buff->len); + buff->buffer = calloc(1, buff->len); + } + } + if (buff->buffer) { + /* want off to now represent the offset into the buffer */ + off -= sizeof(buff->len); + + rc = zookeeper_recv(zh->fd, buff->buffer+off, buff->len-off, 0); + + /* dirty hack to make new client work against old server + * old server sends 40 bytes to finish connection handshake, + * while we're expecting 41 (1 byte for read-only mode data) */ + if (buff == &zh->primer_buffer && rc == buff->len - 1) ++rc; + + switch(rc) { + case 0: + errno = EHOSTDOWN; + case -1: +#ifdef _WIN32 + if (WSAGetLastError() == WSAEWOULDBLOCK) { +#else + if (errno == EAGAIN) { +#endif + break; + } + return -1; + default: + buff->curr_offset += rc; + } + } + return buff->curr_offset == buff->len + sizeof(buff->len); +} + +void free_buffers(buffer_head_t *list) +{ + while (remove_buffer(list)) + ; +} + +void free_completions(zhandle_t *zh,int callCompletion,int reason) +{ + completion_head_t tmp_list; + struct oarchive *oa; + struct ReplyHeader h; + void_completion_t auth_completion = NULL; + auth_completion_list_t a_list, *a_tmp; + + if (lock_completion_list(&zh->sent_requests) == 0) { + tmp_list = zh->sent_requests; + zh->sent_requests.head = 0; + zh->sent_requests.last = 0; + unlock_completion_list(&zh->sent_requests); + while (tmp_list.head) { + completion_list_t *cptr = tmp_list.head; + + tmp_list.head = cptr->next; + if (cptr->c.data_result == SYNCHRONOUS_MARKER) { +#ifdef THREADED + struct sync_completion + *sc = (struct sync_completion*)cptr->data; + sc->rc = reason; + notify_sync_completion(sc); + zh->outstanding_sync--; + destroy_completion_entry(cptr); +#else + abort_singlethreaded(zh); +#endif + } else if (callCompletion) { + // Fake the response + buffer_list_t *bptr; + h.xid = cptr->xid; + h.zxid = -1; + h.err = reason; + oa = create_buffer_oarchive(); + serialize_ReplyHeader(oa, "header", &h); + bptr = calloc(sizeof(*bptr), 1); + assert(bptr); + bptr->len = get_buffer_len(oa); + bptr->buffer = get_buffer(oa); + close_buffer_oarchive(&oa, 0); + cptr->buffer = bptr; + queue_completion(&zh->completions_to_process, cptr, 0); + } + } + } + + zoo_lock_auth(zh); + a_list.completion = NULL; + a_list.next = NULL; + get_auth_completions(&zh->auth_h, &a_list); + zoo_unlock_auth(zh); + + a_tmp = &a_list; + // chain call user's completion function + while (a_tmp->completion != NULL) { + auth_completion = a_tmp->completion; + auth_completion(reason, a_tmp->auth_data); + a_tmp = a_tmp->next; + if (a_tmp == NULL) + break; + } + + free_auth_completion(&a_list); +} + +static void cleanup_bufs(zhandle_t *zh,int callCompletion,int rc) +{ + enter_critical(zh); + free_buffers(&zh->to_send); + free_buffers(&zh->to_process); + free_completions(zh,callCompletion,rc); + leave_critical(zh); + if (zh->input_buffer && zh->input_buffer != &zh->primer_buffer) { + free_buffer(zh->input_buffer); + zh->input_buffer = 0; + } +} + +/* return 1 if zh's state is ZOO_CONNECTED_STATE or ZOO_READONLY_STATE, + * 0 otherwise */ +static int is_connected(zhandle_t* zh) +{ + return (zh->state==ZOO_CONNECTED_STATE || zh->state==ZOO_READONLY_STATE); +} + +static void cleanup(zhandle_t *zh,int rc) +{ + close_zsock(zh->fd); + if (is_unrecoverable(zh)) { + LOG_DEBUG(LOGCALLBACK(zh), "Calling a watcher for a ZOO_SESSION_EVENT and the state=%s", + state2String(zh->state)); + PROCESS_SESSION_EVENT(zh, zh->state); + } else if (is_connected(zh)) { + LOG_DEBUG(LOGCALLBACK(zh), "Calling a watcher for a ZOO_SESSION_EVENT and the state=CONNECTING_STATE"); + PROCESS_SESSION_EVENT(zh, ZOO_CONNECTING_STATE); + } + cleanup_bufs(zh,1,rc); + + LOG_DEBUG(LOGCALLBACK(zh), "Previous connection=%s delay=%d", zoo_get_current_server(zh), zh->delay); + + if (!is_unrecoverable(zh)) { + zh->state = 0; + } + if (process_async(zh->outstanding_sync)) { + process_completions(zh); + } +} + +static void handle_error(zhandle_t *zh,int rc) +{ + cleanup(zh, rc); + // NOTE: If we're at the end of the list of addresses to connect to, then + // we want to delay the next connection attempt to avoid spinning. + // Then increment what host we'll connect to since we failed to connect to current + zh->delay = addrvec_atend(&zh->addrs); + addrvec_next(&zh->addrs, &zh->addr_cur); +} + +static int handle_socket_error_msg(zhandle_t *zh, int line, int rc, + const char* format, ...) +{ + if(logLevel>=ZOO_LOG_LEVEL_ERROR){ + va_list va; + char buf[1024]; + va_start(va,format); + vsnprintf(buf, sizeof(buf)-1,format,va); + log_message(LOGCALLBACK(zh), ZOO_LOG_LEVEL_ERROR,line,__func__, + "Socket %s zk retcode=%d, errno=%d(%s): %s", + zoo_get_current_server(zh),rc,errno,strerror(errno),buf); + va_end(va); + } + handle_error(zh,rc); + return rc; +} + +static void auth_completion_func(int rc, zhandle_t* zh) +{ + void_completion_t auth_completion = NULL; + auth_completion_list_t a_list; + auth_completion_list_t *a_tmp; + + if(zh==NULL) + return; + + zoo_lock_auth(zh); + + if(rc!=0){ + zh->state=ZOO_AUTH_FAILED_STATE; + }else{ + //change state for all auths + mark_active_auth(zh); + } + a_list.completion = NULL; + a_list.next = NULL; + get_auth_completions(&zh->auth_h, &a_list); + zoo_unlock_auth(zh); + if (rc) { + LOG_ERROR(LOGCALLBACK(zh), "Authentication scheme %s failed. Connection closed.", + zh->auth_h.auth->scheme); + } + else { + LOG_INFO(LOGCALLBACK(zh), "Authentication scheme %s succeeded", zh->auth_h.auth->scheme); + } + a_tmp = &a_list; + // chain call user's completion function + while (a_tmp->completion != NULL) { + auth_completion = a_tmp->completion; + auth_completion(rc, a_tmp->auth_data); + a_tmp = a_tmp->next; + if (a_tmp == NULL) + break; + } + free_auth_completion(&a_list); +} + +static int send_info_packet(zhandle_t *zh, auth_info* auth) { + struct oarchive *oa; + struct RequestHeader h = {AUTH_XID, ZOO_SETAUTH_OP}; + struct AuthPacket req; + int rc; + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + req.type=0; // ignored by the server + req.scheme = auth->scheme; + req.auth = auth->auth; + rc = rc < 0 ? rc : serialize_AuthPacket(oa, "req", &req); + /* add this buffer to the head of the send queue */ + rc = rc < 0 ? rc : queue_front_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + return rc; +} + +/** send all auths, not just the last one **/ +static int send_auth_info(zhandle_t *zh) { + int rc = 0; + auth_info *auth = NULL; + + zoo_lock_auth(zh); + auth = zh->auth_h.auth; + if (auth == NULL) { + zoo_unlock_auth(zh); + return ZOK; + } + while (auth != NULL) { + rc = send_info_packet(zh, auth); + auth = auth->next; + } + zoo_unlock_auth(zh); + LOG_DEBUG(LOGCALLBACK(zh), "Sending all auth info request to %s", zoo_get_current_server(zh)); + return (rc <0) ? ZMARSHALLINGERROR:ZOK; +} + +static int send_last_auth_info(zhandle_t *zh) +{ + int rc = 0; + auth_info *auth = NULL; + + zoo_lock_auth(zh); + auth = get_last_auth(&zh->auth_h); + if(auth==NULL) { + zoo_unlock_auth(zh); + return ZOK; // there is nothing to send + } + rc = send_info_packet(zh, auth); + zoo_unlock_auth(zh); + LOG_DEBUG(LOGCALLBACK(zh), "Sending auth info request to %s",zoo_get_current_server(zh)); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +static void free_key_list(char **list, int count) +{ + int i; + + for(i = 0; i < count; i++) { + free(list[i]); + } + free(list); +} + +static int send_set_watches(zhandle_t *zh) +{ + struct oarchive *oa; + struct RequestHeader h = {SET_WATCHES_XID, ZOO_SETWATCHES_OP}; + struct SetWatches req; + int rc; + + req.relativeZxid = zh->last_zxid; + lock_watchers(zh); + req.dataWatches.data = collect_keys(zh->active_node_watchers, (int*)&req.dataWatches.count); + req.existWatches.data = collect_keys(zh->active_exist_watchers, (int*)&req.existWatches.count); + req.childWatches.data = collect_keys(zh->active_child_watchers, (int*)&req.childWatches.count); + unlock_watchers(zh); + + // return if there are no pending watches + if (!req.dataWatches.count && !req.existWatches.count && + !req.childWatches.count) { + free_key_list(req.dataWatches.data, req.dataWatches.count); + free_key_list(req.existWatches.data, req.existWatches.count); + free_key_list(req.childWatches.data, req.childWatches.count); + return ZOK; + } + + + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_SetWatches(oa, "req", &req); + /* add this buffer to the head of the send queue */ + rc = rc < 0 ? rc : queue_front_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + free_key_list(req.dataWatches.data, req.dataWatches.count); + free_key_list(req.existWatches.data, req.existWatches.count); + free_key_list(req.childWatches.data, req.childWatches.count); + LOG_DEBUG(LOGCALLBACK(zh), "Sending set watches request to %s",zoo_get_current_server(zh)); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +static int serialize_prime_connect(struct connect_req *req, char* buffer){ + //this should be the order of serialization + int offset = 0; + req->protocolVersion = htonl(req->protocolVersion); + memcpy(buffer + offset, &req->protocolVersion, sizeof(req->protocolVersion)); + offset = offset + sizeof(req->protocolVersion); + + req->lastZxidSeen = zoo_htonll(req->lastZxidSeen); + memcpy(buffer + offset, &req->lastZxidSeen, sizeof(req->lastZxidSeen)); + offset = offset + sizeof(req->lastZxidSeen); + + req->timeOut = htonl(req->timeOut); + memcpy(buffer + offset, &req->timeOut, sizeof(req->timeOut)); + offset = offset + sizeof(req->timeOut); + + req->sessionId = zoo_htonll(req->sessionId); + memcpy(buffer + offset, &req->sessionId, sizeof(req->sessionId)); + offset = offset + sizeof(req->sessionId); + + req->passwd_len = htonl(req->passwd_len); + memcpy(buffer + offset, &req->passwd_len, sizeof(req->passwd_len)); + offset = offset + sizeof(req->passwd_len); + + memcpy(buffer + offset, req->passwd, sizeof(req->passwd)); + offset = offset + sizeof(req->passwd); + + memcpy(buffer + offset, &req->readOnly, sizeof(req->readOnly)); + + return 0; +} + +static int deserialize_prime_response(struct prime_struct *resp, char* buffer) +{ + //this should be the order of deserialization + int offset = 0; + memcpy(&resp->len, buffer + offset, sizeof(resp->len)); + offset = offset + sizeof(resp->len); + + resp->len = ntohl(resp->len); + memcpy(&resp->protocolVersion, + buffer + offset, + sizeof(resp->protocolVersion)); + offset = offset + sizeof(resp->protocolVersion); + + resp->protocolVersion = ntohl(resp->protocolVersion); + memcpy(&resp->timeOut, buffer + offset, sizeof(resp->timeOut)); + offset = offset + sizeof(resp->timeOut); + + resp->timeOut = ntohl(resp->timeOut); + memcpy(&resp->sessionId, buffer + offset, sizeof(resp->sessionId)); + offset = offset + sizeof(resp->sessionId); + + resp->sessionId = zoo_htonll(resp->sessionId); + memcpy(&resp->passwd_len, buffer + offset, sizeof(resp->passwd_len)); + offset = offset + sizeof(resp->passwd_len); + + resp->passwd_len = ntohl(resp->passwd_len); + memcpy(resp->passwd, buffer + offset, sizeof(resp->passwd)); + offset = offset + sizeof(resp->passwd); + + memcpy(&resp->readOnly, buffer + offset, sizeof(resp->readOnly)); + + return 0; +} + +static int prime_connection(zhandle_t *zh) +{ + int rc; + /*this is the size of buffer to serialize req into*/ + char buffer_req[HANDSHAKE_REQ_SIZE]; + int len = sizeof(buffer_req); + int hlen = 0; + struct connect_req req; + + if (zh->state == ZOO_SSL_CONNECTING_STATE) { + // The SSL connection is yet to happen. + return ZOK; + } + req.protocolVersion = 0; + req.sessionId = zh->seen_rw_server_before ? zh->client_id.client_id : 0; + req.passwd_len = sizeof(req.passwd); + memcpy(req.passwd, zh->client_id.passwd, sizeof(zh->client_id.passwd)); + req.timeOut = zh->recv_timeout; + req.lastZxidSeen = zh->last_zxid; + req.readOnly = zh->allow_read_only; + hlen = htonl(len); + /* We are running fast and loose here, but this string should fit in the initial buffer! */ + rc=zookeeper_send(zh->fd, &hlen, sizeof(len)); + serialize_prime_connect(&req, buffer_req); + rc=rc<0 ? rc : zookeeper_send(zh->fd, buffer_req, len); + if (rc<0) { + return handle_socket_error_msg(zh, __LINE__, ZCONNECTIONLOSS, + "failed to send a handshake packet: %s", strerror(errno)); + } + zh->state = ZOO_ASSOCIATING_STATE; + + zh->input_buffer = &zh->primer_buffer; + memset(zh->input_buffer->buffer, 0, zh->input_buffer->len); + + /* This seems a bit weird to to set the offset to 4, but we already have a + * length, so we skip reading the length (and allocating the buffer) by + * saying that we are already at offset 4 */ + zh->input_buffer->curr_offset = 4; + + return ZOK; +} + +static inline int calculate_interval(const struct timeval *start, + const struct timeval *end) +{ + int interval; + struct timeval i = *end; + i.tv_sec -= start->tv_sec; + i.tv_usec -= start->tv_usec; + interval = i.tv_sec * 1000 + (i.tv_usec/1000); + return interval; +} + +static struct timeval get_timeval(int interval) +{ + struct timeval tv; + if (interval < 0) { + interval = 0; + } + tv.tv_sec = interval/1000; + tv.tv_usec = (interval%1000)*1000; + return tv; +} + + static int add_void_completion(zhandle_t *zh, int xid, void_completion_t dc, + const void *data); + static int add_string_completion(zhandle_t *zh, int xid, + string_completion_t dc, const void *data); + static int add_string_stat_completion(zhandle_t *zh, int xid, + string_stat_completion_t dc, const void *data); + + + int send_ping(zhandle_t* zh) + { + int rc; + struct oarchive *oa = create_buffer_oarchive(); + struct RequestHeader h = {PING_XID, ZOO_PING_OP}; + + rc = serialize_RequestHeader(oa, "header", &h); + enter_critical(zh); + get_system_time(&zh->last_ping); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + close_buffer_oarchive(&oa, 0); + return rc<0 ? rc : adaptor_send_queue(zh, 0); +} + +/* upper bound of a timeout for seeking for r/w server when in read-only mode */ +const int MAX_RW_TIMEOUT = 60000; +const int MIN_RW_TIMEOUT = 200; + +static int ping_rw_server(zhandle_t* zh) +{ + char buf[10]; + zsock_t fd; + int rc; + sendsize_t ssize; + int sock_flags; + + addrvec_peek(&zh->addrs, &zh->addr_rw_server); + +#ifdef SOCK_CLOEXEC_ENABLED + sock_flags = SOCK_STREAM | SOCK_CLOEXEC; +#else + sock_flags = SOCK_STREAM; +#endif + fd.sock = socket(zh->addr_rw_server.ss_family, sock_flags, 0); + if (fd.sock < 0) { + return 0; + } + + zookeeper_set_sock_nodelay(zh, fd.sock); + zookeeper_set_sock_timeout(zh, fd.sock, 1); + + rc = zookeeper_connect(zh, &zh->addr_rw_server, fd.sock); + if (rc < 0) { + return 0; + } + +#ifdef HAVE_OPENSSL_H + fd.ssl_sock = NULL; + fd.ssl_ctx = NULL; + + if (zh->fd->cert != NULL) { + fd.cert = zh->fd->cert; + rc = init_ssl_for_socket(&fd, zh, 0); + if (rc != ZOK) { + rc = 0; + goto out; + } + } +#endif + + ssize = zookeeper_send(&fd, "isro", 4); + if (ssize < 0) { + rc = 0; + goto out; + } + + memset(buf, 0, sizeof(buf)); + rc = zookeeper_recv(&fd, buf, sizeof(buf), 0); + if (rc < 0) { + rc = 0; + goto out; + } + + rc = strcmp("rw", buf) == 0; + +out: + close_zsock(&fd); + addr_rw_server = rc ? &zh->addr_rw_server : 0; + return rc; +} + +#if !defined(WIN32) && !defined(min) +static inline int min(int a, int b) +{ + return a < b ? a : b; +} +#endif + +static void zookeeper_set_sock_noblock(zhandle_t *zh, socket_t sock) +{ +#ifdef _WIN32 + ULONG nonblocking_flag = 1; + + ioctlsocket(sock, FIONBIO, &nonblocking_flag); +#else + fcntl(sock, F_SETFL, O_NONBLOCK|fcntl(sock, F_GETFL, 0)); +#endif +} + +static void zookeeper_set_sock_timeout(zhandle_t *zh, socket_t s, int timeout) +{ + struct timeval tv; + + tv.tv_sec = timeout; + setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)); + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)); +} + +static void zookeeper_set_sock_nodelay(zhandle_t *zh, socket_t sock) +{ +#ifdef _WIN32 + char enable_tcp_nodelay = 1; +#else + int enable_tcp_nodelay = 1; +#endif + int rc; + + rc = setsockopt(sock, + IPPROTO_TCP, + TCP_NODELAY, + &enable_tcp_nodelay, + sizeof(enable_tcp_nodelay)); + + if (rc) { + LOG_WARN(LOGCALLBACK(zh), + "Unable to set TCP_NODELAY, latency may be effected"); + } +} + +static socket_t zookeeper_connect(zhandle_t *zh, + struct sockaddr_storage *addr, + socket_t fd) +{ + int rc; + int addr_len; + +#if defined(AF_INET6) + if (addr->ss_family == AF_INET6) { + addr_len = sizeof(struct sockaddr_in6); + } else { + addr_len = sizeof(struct sockaddr_in); + } +#else + addr_len = sizeof(struct sockaddr_in); +#endif + + LOG_DEBUG(LOGCALLBACK(zh), "[zk] connect()\n"); + rc = connect(fd, (struct sockaddr *)addr, addr_len); + +#ifdef _WIN32 + errno = GetLastError(); + +#ifndef EWOULDBLOCK +#define EWOULDBLOCK WSAEWOULDBLOCK +#endif + +#ifndef EINPROGRESS +#define EINPROGRESS WSAEINPROGRESS +#endif + +#if _MSC_VER >= 1600 + switch(errno) { + case WSAEWOULDBLOCK: + errno = EWOULDBLOCK; + break; + case WSAEINPROGRESS: + errno = EINPROGRESS; + break; + } +#endif +#endif + + return rc; +} + +int zookeeper_interest(zhandle_t *zh, socket_t *fd, int *interest, + struct timeval *tv) +{ + int sock_flags; + int rc = 0; + struct timeval now; + +#ifdef SOCK_CLOEXEC_ENABLED + sock_flags = SOCK_STREAM | SOCK_CLOEXEC; +#else + sock_flags = SOCK_STREAM; +#endif + + if(zh==0 || fd==0 ||interest==0 || tv==0) + return ZBADARGUMENTS; + if (is_unrecoverable(zh)) + return ZINVALIDSTATE; + get_system_time(&now); + if(zh->next_deadline.tv_sec!=0 || zh->next_deadline.tv_usec!=0){ + int time_left = calculate_interval(&zh->next_deadline, &now); + int max_exceed = zh->recv_timeout / 10 > 200 ? 200 : + (zh->recv_timeout / 10); + if (time_left > max_exceed) + LOG_WARN(LOGCALLBACK(zh), "Exceeded deadline by %dms", time_left); + } + api_prolog(zh); + + rc = update_addrs(zh); + if (rc != ZOK) { + return api_epilog(zh, rc); + } + + *fd = zh->fd->sock; + *interest = 0; + tv->tv_sec = 0; + tv->tv_usec = 0; + + if (*fd == -1) { + /* + * If we previously failed to connect to server pool (zh->delay == 1) + * then we need delay our connection on this iteration 1/60 of the + * recv timeout before trying again so we don't spin. + * + * We always clear the delay setting. If we fail again, we'll set delay + * again and on the next iteration we'll do the same. + * + * We will also delay if the disable_reconnection_attempt is set. + */ + if (zh->delay == 1 || zh->disable_reconnection_attempt == 1) { + *tv = get_timeval(zh->recv_timeout/60); + zh->delay = 0; + + LOG_WARN(LOGCALLBACK(zh), "Delaying connection after exhaustively trying all servers [%s]", + zh->hostname); + } else { + if (addr_rw_server) { + zh->addr_cur = *addr_rw_server; + addr_rw_server = 0; + } else { + // No need to delay -- grab the next server and attempt connection + zoo_cycle_next_server(zh); + } + zh->fd->sock = socket(zh->addr_cur.ss_family, sock_flags, 0); + if (zh->fd->sock < 0) { + rc = handle_socket_error_msg(zh, + __LINE__, + ZSYSTEMERROR, + "socket() call failed"); + return api_epilog(zh, rc); + } + + zookeeper_set_sock_nodelay(zh, zh->fd->sock); + zookeeper_set_sock_noblock(zh, zh->fd->sock); + + rc = zookeeper_connect(zh, &zh->addr_cur, zh->fd->sock); + + if (rc == -1) { + /* we are handling the non-blocking connect according to + * the description in section 16.3 "Non-blocking connect" + * in UNIX Network Programming vol 1, 3rd edition */ + if (errno == EWOULDBLOCK || errno == EINPROGRESS) { + // For SSL, we first go to ZOO_SSL_CONNECTING_STATE + if (zh->fd->cert != NULL) + zh->state = ZOO_SSL_CONNECTING_STATE; + else + zh->state = ZOO_CONNECTING_STATE; + } else { + rc = handle_socket_error_msg(zh, + __LINE__, + ZCONNECTIONLOSS, + "connect() call failed"); + return api_epilog(zh, rc); + } + } else { +#ifdef HAVE_OPENSSL_H + if (zh->fd->cert != NULL) { + // We do SSL_connect() here + if (init_ssl_for_handler(zh) != ZOK) { + return ZSSLCONNECTIONERROR; + } + } +#endif + rc = prime_connection(zh); + if (rc != 0) { + return api_epilog(zh,rc); + } + + LOG_INFO(LOGCALLBACK(zh), + "Initiated connection to server %s", + format_endpoint_info(&zh->addr_cur)); + } + *tv = get_timeval(zh->recv_timeout/3); + } + *fd = zh->fd->sock; + zh->last_recv = now; + zh->last_send = now; + zh->last_ping = now; + zh->last_ping_rw = now; + zh->ping_rw_timeout = MIN_RW_TIMEOUT; + } + + if (zh->fd->sock != -1) { + int idle_recv = calculate_interval(&zh->last_recv, &now); + int idle_send = calculate_interval(&zh->last_send, &now); + int recv_to = zh->recv_timeout*2/3 - idle_recv; + int send_to = zh->recv_timeout/3; + // have we exceeded the receive timeout threshold? + if (recv_to <= 0 && zh->state != ZOO_SSL_CONNECTING_STATE) { + // We gotta cut our losses and connect to someone else +#ifdef _WIN32 + errno = WSAETIMEDOUT; +#else + errno = ETIMEDOUT; +#endif + *interest=0; + *tv = get_timeval(0); + return api_epilog(zh,handle_socket_error_msg(zh, + __LINE__,ZOPERATIONTIMEOUT, + "connection to %s timed out (exceeded timeout by %dms)", + format_endpoint_info(&zh->addr_cur), + -recv_to)); + + } + + // We only allow 1/3 of our timeout time to expire before sending + // a PING + if (is_connected(zh)) { + send_to = zh->recv_timeout/3 - idle_send; + if (send_to <= 0) { + if (zh->sent_requests.head == 0) { + rc = send_ping(zh); + if (rc < 0) { + LOG_ERROR(LOGCALLBACK(zh), "failed to send PING request (zk retcode=%d)",rc); + return api_epilog(zh,rc); + } + } + send_to = zh->recv_timeout/3; + } + } + + // If we are in read-only mode, seek for read/write server + if (zh->state == ZOO_READONLY_STATE) { + int idle_ping_rw = calculate_interval(&zh->last_ping_rw, &now); + if (idle_ping_rw >= zh->ping_rw_timeout) { + zh->last_ping_rw = now; + idle_ping_rw = 0; + zh->ping_rw_timeout = min(zh->ping_rw_timeout * 2, + MAX_RW_TIMEOUT); + if (ping_rw_server(zh)) { + struct sockaddr_storage addr; + addrvec_peek(&zh->addrs, &addr); + zh->ping_rw_timeout = MIN_RW_TIMEOUT; + LOG_INFO(LOGCALLBACK(zh), + "r/w server found at %s", + format_endpoint_info(&addr)); + cleanup(zh, ZOK); + } else { + addrvec_next(&zh->addrs, NULL); + } + } + send_to = min(send_to, zh->ping_rw_timeout - idle_ping_rw); + } + + // choose the lesser value as the timeout + *tv = get_timeval(min(recv_to, send_to)); + + zh->next_deadline.tv_sec = now.tv_sec + tv->tv_sec; + zh->next_deadline.tv_usec = now.tv_usec + tv->tv_usec; + if (zh->next_deadline.tv_usec > 1000000) { + zh->next_deadline.tv_sec += zh->next_deadline.tv_usec / 1000000; + zh->next_deadline.tv_usec = zh->next_deadline.tv_usec % 1000000; + } + *interest = ZOOKEEPER_READ; + /* we are interested in a write if we are connected and have something + * to send, or we are waiting for a connect to finish. */ + if ((zh->to_send.head && is_connected(zh)) + || zh->state == ZOO_CONNECTING_STATE + || zh->state == ZOO_SSL_CONNECTING_STATE) { + *interest |= ZOOKEEPER_WRITE; + } + } + return api_epilog(zh,ZOK); +} + +#ifdef HAVE_OPENSSL_H + +/* + * use this function, if you want to init SSL for the socket currently registered in the zookeeper handler + */ +static int init_ssl_for_handler(zhandle_t *zh) +{ + int rc = init_ssl_for_socket(zh->fd, zh, 1); + if (rc == ZOK) { + // (SUCCESS) Now mark the ZOO_CONNECTING_STATE so that + // prime_connection() happen. + // prime_connection() only happens in ZOO_CONNECTING_STATE + zh->state = ZOO_CONNECTING_STATE; + } + return rc; +} + +/* + * use this function, if you want to init SSL for a socket, pointing to a different server address than the one + * currently registered in the zookeeper handler (e.g. ping other servers when you are connected to a read-only one) + */ +static int init_ssl_for_socket(zsock_t *fd, zhandle_t *zh, int fail_on_error) { + + SSL_CTX **ctx; + + if (!fd->ssl_sock) { + const SSL_METHOD *method; + +#if OPENSSL_VERSION_NUMBER < 0x10100000L + OpenSSL_add_all_algorithms(); + ERR_load_BIO_strings(); + ERR_load_crypto_strings(); + SSL_load_error_strings(); + SSL_library_init(); + method = SSLv23_client_method(); +#else + OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL); + method = TLS_client_method(); +#endif + if (FIPS_mode() == 0) { + LOG_INFO(LOGCALLBACK(zh), "FIPS mode is OFF "); + } else { + LOG_INFO(LOGCALLBACK(zh), "FIPS mode is ON "); + } + fd->ssl_ctx = SSL_CTX_new(method); + ctx = &fd->ssl_ctx; + + SSL_CTX_set_verify(*ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0); + /*SERVER CA FILE*/ + if (SSL_CTX_load_verify_locations(*ctx, fd->cert->ca, 0) != 1) { + SSL_CTX_free(*ctx); + LOG_ERROR(LOGCALLBACK(zh), "Failed to load CA file %s", fd->cert->ca); + errno = EINVAL; + return ZBADARGUMENTS; + } + if (SSL_CTX_set_default_verify_paths(*ctx) != 1) { + SSL_CTX_free(*ctx); + LOG_ERROR(LOGCALLBACK(zh), "Call to SSL_CTX_set_default_verify_paths failed"); + errno = EINVAL; + return ZBADARGUMENTS; + } + /*CLIENT CA FILE (With Certificate Chain)*/ + if (SSL_CTX_use_certificate_chain_file(*ctx, fd->cert->cert) != 1) { + SSL_CTX_free(*ctx); + LOG_ERROR(LOGCALLBACK(zh), "Failed to load client certificate chain from %s", fd->cert->cert); + errno = EINVAL; + return ZBADARGUMENTS; + } + /*CLIENT PRIVATE KEY*/ + SSL_CTX_set_default_passwd_cb_userdata(*ctx, fd->cert->passwd); + if (SSL_CTX_use_PrivateKey_file(*ctx, fd->cert->key, SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(*ctx); + LOG_ERROR(LOGCALLBACK(zh), "Failed to load client private key from %s", fd->cert->key); + errno = EINVAL; + return ZBADARGUMENTS; + } + /*CHECK*/ + if (SSL_CTX_check_private_key(*ctx) != 1) { + SSL_CTX_free(*ctx); + LOG_ERROR(LOGCALLBACK(zh), "SSL_CTX_check_private_key failed"); + errno = EINVAL; + return ZBADARGUMENTS; + } + /*MULTIPLE HANDSHAKE*/ + SSL_CTX_set_mode(*ctx, SSL_MODE_AUTO_RETRY); + + fd->ssl_sock = SSL_new(*ctx); + if (fd->ssl_sock == NULL) { + if (fail_on_error) { + return handle_socket_error_msg(zh,__LINE__,ZSSLCONNECTIONERROR, "error creating ssl context"); + } else { + LOG_ERROR(LOGCALLBACK(zh), "error creating ssl context"); + return ZSSLCONNECTIONERROR; + } + + } + SSL_set_fd(fd->ssl_sock, fd->sock); + } + while(1) { + int rc; + int sock = fd->sock; + struct timeval tv; + fd_set s_rfds, s_wfds; + tv.tv_sec = 1; + tv.tv_usec = 0; + FD_ZERO(&s_rfds); + FD_ZERO(&s_wfds); + rc = SSL_connect(fd->ssl_sock); + if (rc == 1) { + return ZOK; + } else { + rc = SSL_get_error(fd->ssl_sock, rc); + if (rc == SSL_ERROR_WANT_READ) { + FD_SET(sock, &s_rfds); + FD_CLR(sock, &s_wfds); + } else if (rc == SSL_ERROR_WANT_WRITE) { + FD_SET(sock, &s_wfds); + FD_CLR(sock, &s_rfds); + } else { + if (fail_on_error) { + return handle_socket_error_msg(zh,__LINE__,ZSSLCONNECTIONERROR, "error in ssl connect"); + } else { + LOG_ERROR(LOGCALLBACK(zh), "error in ssl connect"); + return ZSSLCONNECTIONERROR; + } + } + rc = select(sock + 1, &s_rfds, &s_wfds, NULL, &tv); + if (rc == -1) { + if (fail_on_error) { + return handle_socket_error_msg(zh,__LINE__,ZSSLCONNECTIONERROR, "error in ssl connect (after select)"); + } else { + LOG_ERROR(LOGCALLBACK(zh), "error in ssl connect (after select)"); + return ZSSLCONNECTIONERROR; + } + } + } + } +} + + +#endif + +static int check_events(zhandle_t *zh, int events) +{ + if (zh->fd->sock == -1) + return ZINVALIDSTATE; + +#ifdef HAVE_OPENSSL_H + if ((events&ZOOKEEPER_WRITE) && (zh->state == ZOO_SSL_CONNECTING_STATE) && zh->fd->cert != NULL) { + int rc, error; + socklen_t len = sizeof(error); + rc = getsockopt(zh->fd->sock, SOL_SOCKET, SO_ERROR, &error, &len); + /* the description in section 16.4 "Non-blocking connect" + * in UNIX Network Programming vol 1, 3rd edition, points out + * that sometimes the error is in errno and sometimes in error */ + if (rc < 0 || error) { + if (rc == 0) + errno = error; + return handle_socket_error_msg(zh, __LINE__,ZCONNECTIONLOSS, + "server refused to accept the client"); + } + // We do SSL_connect() here + if (init_ssl_for_handler(zh) != ZOK) { + return ZSSLCONNECTIONERROR; + } + } +#endif + + if ((events&ZOOKEEPER_WRITE)&&(zh->state == ZOO_CONNECTING_STATE)) { + int rc, error; + socklen_t len = sizeof(error); + rc = getsockopt(zh->fd->sock, SOL_SOCKET, SO_ERROR, &error, &len); + /* the description in section 16.4 "Non-blocking connect" + * in UNIX Network Programming vol 1, 3rd edition, points out + * that sometimes the error is in errno and sometimes in error */ + if (rc < 0 || error) { + if (rc == 0) + errno = error; + return handle_socket_error_msg(zh, __LINE__,ZCONNECTIONLOSS, + "server refused to accept the client"); + } + + if((rc=prime_connection(zh))!=0) + return rc; + + LOG_INFO(LOGCALLBACK(zh), "initiated connection to server %s", format_endpoint_info(&zh->addr_cur)); + return ZOK; + } + + if (zh->to_send.head && (events&ZOOKEEPER_WRITE)) { + /* make the flush call non-blocking by specifying a 0 timeout */ + int rc=flush_send_queue(zh,0); + if (rc < 0) + return handle_socket_error_msg(zh,__LINE__,ZCONNECTIONLOSS, + "failed while flushing send queue"); + } + if (events&ZOOKEEPER_READ) { + int rc; + if (zh->input_buffer == 0) { + zh->input_buffer = allocate_buffer(0,0); + } + + rc = recv_buffer(zh, zh->input_buffer); + if (rc < 0) { + return handle_socket_error_msg(zh, __LINE__,ZCONNECTIONLOSS, + "failed while receiving a server response"); + } + if (rc > 0) { + get_system_time(&zh->last_recv); + if (zh->input_buffer != &zh->primer_buffer) { + queue_buffer(&zh->to_process, zh->input_buffer, 0); + } else { + int64_t oldid, newid; + //deserialize + deserialize_prime_response(&zh->primer_storage, zh->primer_buffer.buffer); + /* We are processing the primer_buffer, so we need to finish + * the connection handshake */ + oldid = zh->seen_rw_server_before ? zh->client_id.client_id : 0; + zh->seen_rw_server_before |= !zh->primer_storage.readOnly; + newid = zh->primer_storage.sessionId; + if (oldid != 0 && oldid != newid) { + zh->state = ZOO_EXPIRED_SESSION_STATE; + errno = ESTALE; + return handle_socket_error_msg(zh,__LINE__,ZSESSIONEXPIRED, + "sessionId=%#llx has expired.",oldid); + } else { + zh->recv_timeout = zh->primer_storage.timeOut; + zh->client_id.client_id = newid; + + memcpy(zh->client_id.passwd, &zh->primer_storage.passwd, + sizeof(zh->client_id.passwd)); + zh->state = zh->primer_storage.readOnly ? + ZOO_READONLY_STATE : ZOO_CONNECTED_STATE; + zh->reconfig = 0; + LOG_INFO(LOGCALLBACK(zh), + "session establishment complete on server %s, sessionId=%#llx, negotiated timeout=%d %s", + format_endpoint_info(&zh->addr_cur), + newid, zh->recv_timeout, + zh->primer_storage.readOnly ? "(READ-ONLY mode)" : ""); + /* we want the auth to be sent for, but since both call push to front + we need to call send_watch_set first */ + send_set_watches(zh); + /* send the authentication packet now */ + send_auth_info(zh); + LOG_DEBUG(LOGCALLBACK(zh), "Calling a watcher for a ZOO_SESSION_EVENT and the state=ZOO_CONNECTED_STATE"); + zh->input_buffer = 0; // just in case the watcher calls zookeeper_process() again + PROCESS_SESSION_EVENT(zh, zh->state); + } + } + zh->input_buffer = 0; + } else { + // zookeeper_process was called but there was nothing to read + // from the socket + return ZNOTHING; + } + } + return ZOK; +} + +void api_prolog(zhandle_t* zh) +{ + inc_ref_counter(zh,1); +} + +int api_epilog(zhandle_t *zh,int rc) +{ + if(inc_ref_counter(zh,-1)==0 && zh->close_requested!=0) + zookeeper_close(zh); + return rc; +} + +//#ifdef THREADED +// IO thread queues session events to be processed by the completion thread +static int queue_session_event(zhandle_t *zh, int state) +{ + int rc; + struct WatcherEvent evt = { ZOO_SESSION_EVENT, state, "" }; + struct ReplyHeader hdr = { WATCHER_EVENT_XID, 0, 0 }; + struct oarchive *oa; + completion_list_t *cptr; + + if ((oa=create_buffer_oarchive())==NULL) { + LOG_ERROR(LOGCALLBACK(zh), "out of memory"); + goto error; + } + rc = serialize_ReplyHeader(oa, "hdr", &hdr); + rc = rc<0?rc: serialize_WatcherEvent(oa, "event", &evt); + if(rc<0){ + close_buffer_oarchive(&oa, 1); + goto error; + } + cptr = create_completion_entry(zh, WATCHER_EVENT_XID,-1,0,0,0,0); + cptr->buffer = allocate_buffer(get_buffer(oa), get_buffer_len(oa)); + cptr->buffer->curr_offset = get_buffer_len(oa); + if (!cptr->buffer) { + free(cptr); + close_buffer_oarchive(&oa, 1); + goto error; + } + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + lock_watchers(zh); + cptr->c.watcher_result = collectWatchers(zh, ZOO_SESSION_EVENT, ""); + unlock_watchers(zh); + queue_completion(&zh->completions_to_process, cptr, 0); + if (process_async(zh->outstanding_sync)) { + process_completions(zh); + } + return ZOK; +error: + errno=ENOMEM; + return ZSYSTEMERROR; +} +//#endif + +completion_list_t *dequeue_completion(completion_head_t *list) +{ + completion_list_t *cptr; + lock_completion_list(list); + cptr = list->head; + if (cptr) { + list->head = cptr->next; + if (!list->head) { + assert(list->last == cptr); + list->last = 0; + } + } + unlock_completion_list(list); + return cptr; +} + +// cleanup completion list of a failed multi request +static void cleanup_failed_multi(zhandle_t *zh, int xid, int rc, completion_list_t *cptr) { + completion_list_t *entry; + completion_head_t *clist = &cptr->c.clist; + while ((entry = dequeue_completion(clist)) != NULL) { + // Fake failed response for all sub-requests + deserialize_response(zh, entry->c.type, xid, 1, rc, entry, NULL); + destroy_completion_entry(entry); + } +} + +static int deserialize_multi(zhandle_t *zh, int xid, completion_list_t *cptr, struct iarchive *ia) +{ + int rc = 0; + completion_head_t *clist = &cptr->c.clist; + struct MultiHeader mhdr = {0, 0, 0}; + assert(clist); + deserialize_MultiHeader(ia, "multiheader", &mhdr); + while (!mhdr.done) { + completion_list_t *entry = dequeue_completion(clist); + assert(entry); + + if (mhdr.type == -1) { + struct ErrorResponse er; + deserialize_ErrorResponse(ia, "error", &er); + mhdr.err = er.err ; + if (rc == 0 && er.err != 0 && er.err != ZRUNTIMEINCONSISTENCY) { + rc = er.err; + } + } + + deserialize_response(zh, entry->c.type, xid, mhdr.type == -1, mhdr.err, entry, ia); + deserialize_MultiHeader(ia, "multiheader", &mhdr); + //While deserializing the response we must destroy completion entry for each operation in + //the zoo_multi transaction. Otherwise this results in memory leak when client invokes zoo_multi + //operation. + destroy_completion_entry(entry); + } + + return rc; +} + +static void deserialize_response(zhandle_t *zh, int type, int xid, int failed, int rc, completion_list_t *cptr, struct iarchive *ia) +{ + switch (type) { + case COMPLETION_DATA: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_DATA for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.data_result(rc, 0, 0, 0, cptr->data); + } else { + struct GetDataResponse res; + deserialize_GetDataResponse(ia, "reply", &res); + cptr->c.data_result(rc, res.data.buff, res.data.len, + &res.stat, cptr->data); + deallocate_GetDataResponse(&res); + } + break; + case COMPLETION_STAT: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_STAT for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.stat_result(rc, 0, cptr->data); + } else { + struct SetDataResponse res; + deserialize_SetDataResponse(ia, "reply", &res); + cptr->c.stat_result(rc, &res.stat, cptr->data); + deallocate_SetDataResponse(&res); + } + break; + case COMPLETION_STRINGLIST: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_STRINGLIST for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.strings_result(rc, 0, cptr->data); + } else { + struct GetChildrenResponse res; + deserialize_GetChildrenResponse(ia, "reply", &res); + cptr->c.strings_result(rc, &res.children, cptr->data); + deallocate_GetChildrenResponse(&res); + } + break; + case COMPLETION_STRINGLIST_STAT: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_STRINGLIST_STAT for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.strings_stat_result(rc, 0, 0, cptr->data); + } else { + struct GetChildren2Response res; + deserialize_GetChildren2Response(ia, "reply", &res); + cptr->c.strings_stat_result(rc, &res.children, &res.stat, cptr->data); + deallocate_GetChildren2Response(&res); + } + break; + case COMPLETION_STRING: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_STRING for xid=%#x failed=%d, rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.string_result(rc, 0, cptr->data); + } else { + struct CreateResponse res; + const char *client_path; + memset(&res, 0, sizeof(res)); + deserialize_CreateResponse(ia, "reply", &res); + client_path = sub_string(zh, res.path); + cptr->c.string_result(rc, client_path, cptr->data); + free_duplicate_path(client_path, res.path); + deallocate_CreateResponse(&res); + } + break; + case COMPLETION_STRING_STAT: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_STRING_STAT for xid=%#x failed=%d, rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.string_stat_result(rc, 0, 0, cptr->data); + } else { + struct Create2Response res; + const char *client_path; + deserialize_Create2Response(ia, "reply", &res); + client_path = sub_string(zh, res.path); + cptr->c.string_stat_result(rc, client_path, &res.stat, cptr->data); + free_duplicate_path(client_path, res.path); + deallocate_Create2Response(&res); + } + break; + case COMPLETION_ACLLIST: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_ACLLIST for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + if (failed) { + cptr->c.acl_result(rc, 0, 0, cptr->data); + } else { + struct GetACLResponse res; + deserialize_GetACLResponse(ia, "reply", &res); + cptr->c.acl_result(rc, &res.acl, &res.stat, cptr->data); + deallocate_GetACLResponse(&res); + } + break; + case COMPLETION_VOID: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_VOID for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + assert(cptr->c.void_result); + cptr->c.void_result(rc, cptr->data); + break; + case COMPLETION_MULTI: + LOG_DEBUG(LOGCALLBACK(zh), "Calling COMPLETION_MULTI for xid=%#x failed=%d rc=%d", + cptr->xid, failed, rc); + assert(cptr->c.void_result); + if (failed) { + cleanup_failed_multi(zh, xid, rc, cptr); + } else { + rc = deserialize_multi(zh, xid, cptr, ia); + } + cptr->c.void_result(rc, cptr->data); + break; + default: + LOG_DEBUG(LOGCALLBACK(zh), "Unsupported completion type=%d", cptr->c.type); + } +} + + +/* handles async completion (both single- and multithreaded) */ +void process_completions(zhandle_t *zh) +{ + completion_list_t *cptr; + while ((cptr = dequeue_completion(&zh->completions_to_process)) != 0) { + struct ReplyHeader hdr; + buffer_list_t *bptr = cptr->buffer; + struct iarchive *ia = create_buffer_iarchive(bptr->buffer, + bptr->len); + deserialize_ReplyHeader(ia, "hdr", &hdr); + + if (hdr.xid == WATCHER_EVENT_XID) { + int type, state; + struct WatcherEvent evt; + deserialize_WatcherEvent(ia, "event", &evt); + /* We are doing a notification, so there is no pending request */ + type = evt.type; + state = evt.state; + /* This is a notification so there aren't any pending requests */ + LOG_DEBUG(LOGCALLBACK(zh), "Calling a watcher for node [%s], type = %d event=%s", + (evt.path==NULL?"NULL":evt.path), cptr->c.type, + watcherEvent2String(type)); + deliverWatchers(zh,type,state,evt.path, &cptr->c.watcher_result); + deallocate_WatcherEvent(&evt); + } else { + deserialize_response(zh, cptr->c.type, hdr.xid, hdr.err != 0, hdr.err, cptr, ia); + } + destroy_completion_entry(cptr); + close_buffer_iarchive(&ia); + } +} + +static void isSocketReadable(zhandle_t* zh) +{ +#ifndef _WIN32 + struct pollfd fds; + fds.fd = zh->fd->sock; + fds.events = POLLIN; + if (poll(&fds,1,0)<=0) { + // socket not readable -- no more responses to process + zh->socket_readable.tv_sec=zh->socket_readable.tv_usec=0; + } +#else + fd_set rfds; + struct timeval waittime = {0, 0}; + FD_ZERO(&rfds); + FD_SET( zh->fd , &rfds); + if (select(0, &rfds, NULL, NULL, &waittime) <= 0){ + // socket not readable -- no more responses to process + zh->socket_readable.tv_sec=zh->socket_readable.tv_usec=0; + } +#endif + else{ + get_system_time(&zh->socket_readable); + } +} + +static void checkResponseLatency(zhandle_t* zh) +{ + int delay; + struct timeval now; + + if(zh->socket_readable.tv_sec==0) + return; + + get_system_time(&now); + delay=calculate_interval(&zh->socket_readable, &now); + if(delay>20) + LOG_DEBUG(LOGCALLBACK(zh), "The following server response has spent at least %dms sitting in the client socket recv buffer",delay); + + zh->socket_readable.tv_sec=zh->socket_readable.tv_usec=0; +} + +int zookeeper_process(zhandle_t *zh, int events) +{ + buffer_list_t *bptr; + int rc; + + if (zh==NULL) + return ZBADARGUMENTS; + if (is_unrecoverable(zh)) + return ZINVALIDSTATE; + api_prolog(zh); + IF_DEBUG(checkResponseLatency(zh)); + rc = check_events(zh, events); + if (rc!=ZOK) + return api_epilog(zh, rc); + + IF_DEBUG(isSocketReadable(zh)); + + while (rc >= 0 && (bptr=dequeue_buffer(&zh->to_process))) { + struct ReplyHeader hdr; + struct iarchive *ia = create_buffer_iarchive( + bptr->buffer, bptr->curr_offset); + deserialize_ReplyHeader(ia, "hdr", &hdr); + + if (hdr.xid == PING_XID) { + // Ping replies can arrive out-of-order + int elapsed = 0; + struct timeval now; + gettimeofday(&now, 0); + elapsed = calculate_interval(&zh->last_ping, &now); + LOG_DEBUG(LOGCALLBACK(zh), "Got ping response in %d ms", elapsed); + free_buffer(bptr); + } else if (hdr.xid == WATCHER_EVENT_XID) { + struct WatcherEvent evt; + int type = 0; + char *path = NULL; + completion_list_t *c = NULL; + + LOG_DEBUG(LOGCALLBACK(zh), "Processing WATCHER_EVENT"); + + deserialize_WatcherEvent(ia, "event", &evt); + type = evt.type; + path = evt.path; + /* We are doing a notification, so there is no pending request */ + c = create_completion_entry(zh, WATCHER_EVENT_XID,-1,0,0,0,0); + c->buffer = bptr; + lock_watchers(zh); + c->c.watcher_result = collectWatchers(zh, type, path); + unlock_watchers(zh); + + // We cannot free until now, otherwise path will become invalid + deallocate_WatcherEvent(&evt); + queue_completion(&zh->completions_to_process, c, 0); + } else if (hdr.xid == SET_WATCHES_XID) { + LOG_DEBUG(LOGCALLBACK(zh), "Processing SET_WATCHES"); + free_buffer(bptr); + } else if (hdr.xid == AUTH_XID){ + LOG_DEBUG(LOGCALLBACK(zh), "Processing AUTH_XID"); + + /* special handling for the AUTH response as it may come back + * out-of-band */ + auth_completion_func(hdr.err,zh); + free_buffer(bptr); + /* authentication completion may change the connection state to + * unrecoverable */ + if(is_unrecoverable(zh)){ + handle_error(zh, ZAUTHFAILED); + close_buffer_iarchive(&ia); + return api_epilog(zh, ZAUTHFAILED); + } + } else { + int rc = hdr.err; + /* Find the request corresponding to the response */ + completion_list_t *cptr = dequeue_completion(&zh->sent_requests); + + /* [ZOOKEEPER-804] Don't assert if zookeeper_close has been called. */ + if (zh->close_requested == 1 && cptr == NULL) { + LOG_DEBUG(LOGCALLBACK(zh), "Completion queue has been cleared by zookeeper_close()"); + close_buffer_iarchive(&ia); + free_buffer(bptr); + return api_epilog(zh,ZINVALIDSTATE); + } + assert(cptr); + /* The requests are going to come back in order */ + if (cptr->xid != hdr.xid) { + LOG_DEBUG(LOGCALLBACK(zh), "Processing unexpected or out-of-order response!"); + + // received unexpected (or out-of-order) response + close_buffer_iarchive(&ia); + free_buffer(bptr); + // put the completion back on the queue (so it gets properly + // signaled and deallocated) and disconnect from the server + queue_completion(&zh->sent_requests,cptr,1); + return api_epilog(zh, + handle_socket_error_msg(zh, __LINE__,ZRUNTIMEINCONSISTENCY, + "unexpected server response: expected %#x, but received %#x", + hdr.xid,cptr->xid)); + } + + if (hdr.zxid > 0) { + // Update last_zxid only when it is a request response + zh->last_zxid = hdr.zxid; + } + lock_watchers(zh); + activateWatcher(zh, cptr->watcher, rc); + deactivateWatcher(zh, cptr->watcher_deregistration, rc); + unlock_watchers(zh); + + if (cptr->c.void_result != SYNCHRONOUS_MARKER) { + LOG_DEBUG(LOGCALLBACK(zh), "Queueing asynchronous response"); + cptr->buffer = bptr; + queue_completion(&zh->completions_to_process, cptr, 0); + } else { +#ifdef THREADED + struct sync_completion + *sc = (struct sync_completion*)cptr->data; + sc->rc = rc; + + process_sync_completion(zh, cptr, sc, ia); + + notify_sync_completion(sc); + free_buffer(bptr); + zh->outstanding_sync--; + destroy_completion_entry(cptr); +#else + abort_singlethreaded(zh); +#endif + } + } + + close_buffer_iarchive(&ia); + + } + if (process_async(zh->outstanding_sync)) { + process_completions(zh); + } + + return api_epilog(zh, ZOK); +} + +int zoo_state(zhandle_t *zh) +{ + if(zh!=0) + return zh->state; + return 0; +} + +static watcher_registration_t* create_watcher_registration(const char* path, + result_checker_fn checker,watcher_fn watcher,void* ctx){ + watcher_registration_t* wo; + if(watcher==0) + return 0; + wo=calloc(1,sizeof(watcher_registration_t)); + wo->path=strdup(path); + wo->watcher=watcher; + wo->context=ctx; + wo->checker=checker; + return wo; +} + +static watcher_deregistration_t* create_watcher_deregistration(const char* path, + watcher_fn watcher, void *watcherCtx, ZooWatcherType wtype) { + watcher_deregistration_t *wdo; + + wdo = calloc(1, sizeof(watcher_deregistration_t)); + if (!wdo) { + return NULL; + } + wdo->path = strdup(path); + wdo->watcher = watcher; + wdo->context = watcherCtx; + wdo->type = wtype; + return wdo; +} + +static void destroy_watcher_registration(watcher_registration_t* wo){ + if(wo!=0){ + free((void*)wo->path); + free(wo); + } +} + +static void destroy_watcher_deregistration(watcher_deregistration_t *wdo) { + if (wdo) { + free((void *)wdo->path); + free(wdo); + } +} + +static completion_list_t* create_completion_entry(zhandle_t *zh, int xid, int completion_type, + const void *dc, const void *data,watcher_registration_t* wo, completion_head_t *clist) +{ + return do_create_completion_entry(zh, xid, completion_type, dc, data, wo, + clist, NULL); +} + +static completion_list_t* create_completion_entry_deregistration(zhandle_t *zh, + int xid, int completion_type, const void *dc, const void *data, + watcher_deregistration_t* wdo, completion_head_t *clist) +{ + return do_create_completion_entry(zh, xid, completion_type, dc, data, NULL, + clist, wdo); +} + +static completion_list_t* do_create_completion_entry(zhandle_t *zh, int xid, + int completion_type, const void *dc, const void *data, + watcher_registration_t* wo, completion_head_t *clist, + watcher_deregistration_t* wdo) +{ + completion_list_t *c = calloc(1, sizeof(completion_list_t)); + if (!c) { + LOG_ERROR(LOGCALLBACK(zh), "out of memory"); + return 0; + } + c->c.type = completion_type; + c->data = data; + switch(c->c.type) { + case COMPLETION_VOID: + c->c.void_result = (void_completion_t)dc; + break; + case COMPLETION_STRING: + c->c.string_result = (string_completion_t)dc; + break; + case COMPLETION_DATA: + c->c.data_result = (data_completion_t)dc; + break; + case COMPLETION_STAT: + c->c.stat_result = (stat_completion_t)dc; + break; + case COMPLETION_STRINGLIST: + c->c.strings_result = (strings_completion_t)dc; + break; + case COMPLETION_STRINGLIST_STAT: + c->c.strings_stat_result = (strings_stat_completion_t)dc; + break; + case COMPLETION_STRING_STAT: + c->c.string_stat_result = (string_stat_completion_t)dc; + case COMPLETION_ACLLIST: + c->c.acl_result = (acl_completion_t)dc; + break; + case COMPLETION_MULTI: + assert(clist); + c->c.void_result = (void_completion_t)dc; + c->c.clist = *clist; + break; + } + c->xid = xid; + c->watcher = wo; + c->watcher_deregistration = wdo; + + return c; +} + +static void destroy_completion_entry(completion_list_t* c){ + if(c!=0){ + destroy_watcher_registration(c->watcher); + destroy_watcher_deregistration(c->watcher_deregistration); + if(c->buffer!=0) + free_buffer(c->buffer); + free(c); + } +} + +static void queue_completion_nolock(completion_head_t *list, + completion_list_t *c, + int add_to_front) +{ + c->next = 0; + /* appending a new entry to the back of the list */ + if (list->last) { + assert(list->head); + // List is not empty + if (!add_to_front) { + list->last->next = c; + list->last = c; + } else { + c->next = list->head; + list->head = c; + } + } else { + // List is empty + assert(!list->head); + list->head = c; + list->last = c; + } +} + +static void queue_completion(completion_head_t *list, completion_list_t *c, + int add_to_front) +{ + + lock_completion_list(list); + queue_completion_nolock(list, c, add_to_front); + unlock_completion_list(list); +} + +static int add_completion(zhandle_t *zh, int xid, int completion_type, + const void *dc, const void *data, int add_to_front, + watcher_registration_t* wo, completion_head_t *clist) +{ + completion_list_t *c =create_completion_entry(zh, xid, completion_type, dc, + data, wo, clist); + return do_add_completion(zh, dc, c, add_to_front); +} + +static int add_completion_deregistration(zhandle_t *zh, int xid, + int completion_type, const void *dc, const void *data, int add_to_front, + watcher_deregistration_t* wdo, completion_head_t *clist) +{ + completion_list_t *c = create_completion_entry_deregistration(zh, xid, + completion_type, dc, data, wdo, clist); + return do_add_completion(zh, dc, c, add_to_front); +} + +static int do_add_completion(zhandle_t *zh, const void *dc, + completion_list_t *c, int add_to_front) +{ + int rc = 0; + if (!c) + return ZSYSTEMERROR; + lock_completion_list(&zh->sent_requests); + if (zh->close_requested != 1) { + queue_completion_nolock(&zh->sent_requests, c, add_to_front); + if (dc == SYNCHRONOUS_MARKER) { + zh->outstanding_sync++; + } + rc = ZOK; + } else { + free(c); + rc = ZINVALIDSTATE; + } + unlock_completion_list(&zh->sent_requests); + return rc; +} + +static int add_data_completion(zhandle_t *zh, int xid, data_completion_t dc, + const void *data,watcher_registration_t* wo) +{ + return add_completion(zh, xid, COMPLETION_DATA, dc, data, 0, wo, 0); +} + +static int add_stat_completion(zhandle_t *zh, int xid, stat_completion_t dc, + const void *data,watcher_registration_t* wo) +{ + return add_completion(zh, xid, COMPLETION_STAT, dc, data, 0, wo, 0); +} + +static int add_strings_completion(zhandle_t *zh, int xid, + strings_completion_t dc, const void *data,watcher_registration_t* wo) +{ + return add_completion(zh, xid, COMPLETION_STRINGLIST, dc, data, 0, wo, 0); +} + +static int add_strings_stat_completion(zhandle_t *zh, int xid, + strings_stat_completion_t dc, const void *data,watcher_registration_t* wo) +{ + return add_completion(zh, xid, COMPLETION_STRINGLIST_STAT, dc, data, 0, wo, 0); +} + +static int add_acl_completion(zhandle_t *zh, int xid, acl_completion_t dc, + const void *data) +{ + return add_completion(zh, xid, COMPLETION_ACLLIST, dc, data, 0, 0, 0); +} + +static int add_void_completion(zhandle_t *zh, int xid, void_completion_t dc, + const void *data) +{ + return add_completion(zh, xid, COMPLETION_VOID, dc, data, 0, 0, 0); +} + +static int add_string_completion(zhandle_t *zh, int xid, + string_completion_t dc, const void *data) +{ + return add_completion(zh, xid, COMPLETION_STRING, dc, data, 0, 0, 0); +} + +static int add_string_stat_completion(zhandle_t *zh, int xid, + string_stat_completion_t dc, const void *data) +{ + return add_completion(zh, xid, COMPLETION_STRING_STAT, dc, data, 0, 0, 0); +} + +static int add_multi_completion(zhandle_t *zh, int xid, void_completion_t dc, + const void *data, completion_head_t *clist) +{ + return add_completion(zh, xid, COMPLETION_MULTI, dc, data, 0,0, clist); +} + +/** + * After sending the close request, we are waiting for a given millisecs for + * getting the answer and/or for the socket to be closed by the server. + * + * This function should not be called while we still want to process + * any response from the server. It must be called after adaptor_finish called, + * in order not to mess with the I/O receiver thread in multi-threaded mode. + */ +int wait_for_session_to_be_closed(zhandle_t *zh, int timeout_ms) +{ + int ret = 0; +#ifndef WIN32 + struct pollfd fd_s[1]; +#else + fd_set rfds; + struct timeval waittime = {timeout_ms / 1000, (timeout_ms % 1000) * 1000}; +#endif + + if (zh == NULL) { + return ZBADARGUMENTS; + } + +#ifndef WIN32 + fd_s[0].fd = zh->fd->sock; + fd_s[0].events = POLLIN; + ret = poll(fd_s, 1, timeout_ms); +#else + FD_ZERO(&rfds); + FD_SET(zh->fd->sock , &rfds); + ret = select(zh->fd->sock + 1, &rfds, NULL, NULL, &waittime); +#endif + + if (ret == 0){ + LOG_WARN(LOGCALLBACK(zh), "Timed out (%dms) during waiting for server's reply after sending a close request, sessionId=%#llx\n", + timeout_ms, zh->client_id.client_id); + } else if (ret < 0) { + LOG_WARN(LOGCALLBACK(zh), "System error (%d) happened while waiting for server's reply, sessionId=%#llx\n", + ret, zh->client_id.client_id); + } + + return ZOK; +} + +int zookeeper_close(zhandle_t *zh) +{ + int rc=ZOK; + if (zh==0) + return ZBADARGUMENTS; + + zh->close_requested=1; + if (inc_ref_counter(zh,1)>1) { + /* We have incremented the ref counter to prevent the + * completions from calling zookeeper_close before we have + * completed the adaptor_finish call below. */ + + /* Signal any syncronous completions before joining the threads */ + enter_critical(zh); + free_completions(zh,1,ZCLOSING); + leave_critical(zh); + + adaptor_finish(zh); + /* Now we can allow the handle to be cleaned up, if the completion + * threads finished during the adaptor_finish call. */ + api_epilog(zh, 0); + return ZOK; + } + /* No need to decrement the counter since we're just going to + * destroy the handle later. */ + if (is_connected(zh)) { + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_CLOSE_OP}; + LOG_INFO(LOGCALLBACK(zh), "Closing zookeeper sessionId=%#llx to %s\n", + zh->client_id.client_id, zoo_get_current_server(zh)); + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), get_buffer_len(oa)); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + if (rc < 0) { + LOG_DEBUG(LOGCALLBACK(zh), "Error during closing zookeeper session, sessionId=%#llx to %s (error: %d)\n", + zh->client_id.client_id, zoo_get_current_server(zh), rc); + rc = ZMARSHALLINGERROR; + } else { + /* make sure the close request is sent; we set timeout to an arbitrary + * (but reasonable) number of milliseconds since we want the call to block*/ + rc = adaptor_send_queue(zh, 3000); + + /* give some time to the server to process the session close request properly */ + rc = rc < 0 ? rc : wait_for_session_to_be_closed(zh, 1500); + } + } else { + rc = ZOK; + } + + LOG_INFO(LOGCALLBACK(zh), "Freeing zookeeper resources for sessionId=%#llx\n", zh->client_id.client_id); + destroy(zh); + adaptor_destroy(zh); + free(zh->fd); + free(zh); +#ifdef _WIN32 + Win32WSACleanup(); +#endif + return rc; +} + +static int isValidPath(const char* path, const int mode) { + int len = 0; + char lastc = '/'; + char c; + int i = 0; + + if (path == 0) + return 0; + len = strlen(path); + if (len == 0) + return 0; + if (path[0] != '/') + return 0; + if (len == 1) // done checking - it's the root + return 1; + if (path[len - 1] == '/' && !ZOOKEEPER_IS_SEQUENCE(mode)) + return 0; + + i = 1; + for (; i < len; lastc = path[i], i++) { + c = path[i]; + + if (c == 0) { + return 0; + } else if (c == '/' && lastc == '/') { + return 0; + } else if (c == '.' && lastc == '.') { + if (path[i-2] == '/' && (((i + 1 == len) && !ZOOKEEPER_IS_SEQUENCE(mode)) + || path[i+1] == '/')) { + return 0; + } + } else if (c == '.') { + if ((path[i-1] == '/') && (((i + 1 == len) && !ZOOKEEPER_IS_SEQUENCE(mode)) + || path[i+1] == '/')) { + return 0; + } + } else if (c > 0x00 && c < 0x1f) { + return 0; + } + } + + return 1; +} + +/*---------------------------------------------------------------------------* + * REQUEST INIT HELPERS + *---------------------------------------------------------------------------*/ +/* Common Request init helper functions to reduce code duplication */ +static int Request_path_init(zhandle_t *zh, int mode, + char **path_out, const char *path) +{ + assert(path_out); + + *path_out = prepend_string(zh, path); + if (zh == NULL || !isValidPath(*path_out, mode)) { + free_duplicate_path(*path_out, path); + return ZBADARGUMENTS; + } + if (is_unrecoverable(zh)) { + free_duplicate_path(*path_out, path); + return ZINVALIDSTATE; + } + + return ZOK; +} + +static int Request_path_watch_init(zhandle_t *zh, int mode, + char **path_out, const char *path, + int32_t *watch_out, uint32_t watch) +{ + int rc = Request_path_init(zh, mode, path_out, path); + if (rc != ZOK) { + return rc; + } + *watch_out = watch; + return ZOK; +} + +/*---------------------------------------------------------------------------* + * ASYNC API + *---------------------------------------------------------------------------*/ +int zoo_aget(zhandle_t *zh, const char *path, int watch, data_completion_t dc, + const void *data) +{ + return zoo_awget(zh,path,watch?zh->watcher:0,zh->context,dc,data); +} + +int zoo_awget(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + data_completion_t dc, const void *data) +{ + struct oarchive *oa; + char *server_path = prepend_string(zh, path); + struct RequestHeader h = {get_xid(), ZOO_GETDATA_OP}; + struct GetDataRequest req = { (char*)server_path, watcher!=0 }; + int rc; + + if (zh==0 || !isValidPath(server_path, 0)) { + free_duplicate_path(server_path, path); + return ZBADARGUMENTS; + } + if (is_unrecoverable(zh)) { + free_duplicate_path(server_path, path); + return ZINVALIDSTATE; + } + oa=create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_GetDataRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_data_completion(zh, h.xid, dc, data, + create_watcher_registration(server_path,data_result_checker,watcher,watcherCtx)); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(server_path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_agetconfig(zhandle_t *zh, int watch, data_completion_t dc, + const void *data) +{ + return zoo_awgetconfig(zh,watch?zh->watcher:0,zh->context,dc,data); +} + +int zoo_awgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, + data_completion_t dc, const void *data) +{ + struct oarchive *oa; + char *path = ZOO_CONFIG_NODE; + char *server_path = ZOO_CONFIG_NODE; + struct RequestHeader h = { get_xid(), ZOO_GETDATA_OP }; + struct GetDataRequest req = { (char*)server_path, watcher!=0 }; + int rc; + + if (zh==0 || !isValidPath(server_path, 0)) { + free_duplicate_path(server_path, path); + return ZBADARGUMENTS; + } + if (is_unrecoverable(zh)) { + free_duplicate_path(server_path, path); + return ZINVALIDSTATE; + } + oa=create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_GetDataRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_data_completion(zh, h.xid, dc, data, + create_watcher_registration(server_path,data_result_checker,watcher,watcherCtx)); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(server_path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_areconfig(zhandle_t *zh, const char *joining, const char *leaving, + const char *members, int64_t version, data_completion_t dc, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = { get_xid(), ZOO_RECONFIG_OP }; + struct ReconfigRequest req; + int rc = 0; + + if (zh==0) { + return ZBADARGUMENTS; + } + if (is_unrecoverable(zh)) { + return ZINVALIDSTATE; + } + + oa=create_buffer_oarchive(); + req.joiningServers = (char *)joining; + req.leavingServers = (char *)leaving; + req.newMembers = (char *)members; + req.curConfigId = version; + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_ReconfigRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_data_completion(zh, h.xid, dc, data, NULL); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending Reconfig request xid=%#x to %s",h.xid, zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +static int SetDataRequest_init(zhandle_t *zh, struct SetDataRequest *req, + const char *path, const char *buffer, int buflen, int version) +{ + int rc; + assert(req); + rc = Request_path_init(zh, 0, &req->path, path); + if (rc != ZOK) { + return rc; + } + req->data.buff = (char*)buffer; + req->data.len = buflen; + req->version = version; + + return ZOK; +} + +int zoo_aset(zhandle_t *zh, const char *path, const char *buffer, int buflen, + int version, stat_completion_t dc, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_SETDATA_OP}; + struct SetDataRequest req; + int rc = SetDataRequest_init(zh, &req, path, buffer, buflen, version); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_SetDataRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_stat_completion(zh, h.xid, dc, data,0); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +static int CreateRequest_init(zhandle_t *zh, struct CreateRequest *req, + const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int mode) +{ + int rc; + assert(req); + rc = Request_path_init(zh, mode, &req->path, path); + assert(req); + if (rc != ZOK) { + return rc; + } + req->flags = mode; + req->data.buff = (char*)value; + req->data.len = valuelen; + if (acl_entries == 0) { + req->acl.count = 0; + req->acl.data = 0; + } else { + req->acl = *acl_entries; + } + + return ZOK; +} + +static int CreateTTLRequest_init(zhandle_t *zh, struct CreateTTLRequest *req, + const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int mode, int64_t ttl) +{ + int rc; + assert(req); + rc = Request_path_init(zh, mode, &req->path, path); + assert(req); + if (rc != ZOK) { + return rc; + } + req->flags = mode; + req->data.buff = (char*)value; + req->data.len = valuelen; + if (acl_entries == 0) { + req->acl.count = 0; + req->acl.data = 0; + } else { + req->acl = *acl_entries; + } + req->ttl = ttl; + + return ZOK; +} + +static int get_create_op_type(int mode, int default_op) { + if (mode == ZOO_CONTAINER) { + return ZOO_CREATE_CONTAINER_OP; + } else if (ZOOKEEPER_IS_TTL(mode)) { + return ZOO_CREATE_TTL_OP; + } else { + return default_op; + } +} + +int zoo_acreate(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int mode, + string_completion_t completion, const void *data) +{ + return zoo_acreate_ttl(zh, path, value, valuelen, acl_entries, mode, -1, completion, data); +} + +int zoo_acreate_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int mode, int64_t ttl, + string_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), get_create_op_type(mode, ZOO_CREATE_OP)}; + int rc; + char *req_path; + + if (ZOOKEEPER_IS_TTL(mode)) { + struct CreateTTLRequest req; + + if (ttl <= 0 || ttl > ZOO_MAX_TTL) { + return ZBADARGUMENTS; + } + + rc = CreateTTLRequest_init(zh, &req, + path, value, valuelen, acl_entries, mode, ttl); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_CreateTTLRequest(oa, "req", &req); + + req_path = req.path; + } else { + struct CreateRequest req; + + if (ttl >= 0) { + return ZBADARGUMENTS; + } + + rc = CreateRequest_init(zh, &req, + path, value, valuelen, acl_entries, mode); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_CreateRequest(oa, "req", &req); + + req_path = req.path; + } + + enter_critical(zh); + rc = rc < 0 ? rc : add_string_completion(zh, h.xid, completion, data); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req_path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_acreate2(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int mode, + string_stat_completion_t completion, const void *data) +{ + return zoo_acreate2_ttl(zh, path, value, valuelen, acl_entries, mode, -1, completion, data); +} + +int zoo_acreate2_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl_entries, int mode, int64_t ttl, + string_stat_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = { get_xid(), get_create_op_type(mode, ZOO_CREATE2_OP) }; + int rc; + char *req_path; + + if (ZOOKEEPER_IS_TTL(mode)) { + struct CreateTTLRequest req; + + if (ttl <= 0 || ttl > ZOO_MAX_TTL) { + return ZBADARGUMENTS; + } + + rc = CreateTTLRequest_init(zh, &req, + path, value, valuelen, acl_entries, mode, ttl); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_CreateTTLRequest(oa, "req", &req); + + req_path = req.path; + } else { + struct CreateRequest req; + + if (ttl >= 0) { + return ZBADARGUMENTS; + } + + rc = CreateRequest_init(zh, &req, path, value, valuelen, acl_entries, mode); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_CreateRequest(oa, "req", &req); + + req_path = req.path; + } + + enter_critical(zh); + rc = rc < 0 ? rc : add_string_stat_completion(zh, h.xid, completion, data); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req_path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int DeleteRequest_init(zhandle_t *zh, struct DeleteRequest *req, + const char *path, int version) +{ + int rc = Request_path_init(zh, 0, &req->path, path); + if (rc != ZOK) { + return rc; + } + req->version = version; + return ZOK; +} + +int zoo_adelete(zhandle_t *zh, const char *path, int version, + void_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_DELETE_OP}; + struct DeleteRequest req; + int rc = DeleteRequest_init(zh, &req, path, version); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_DeleteRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_void_completion(zh, h.xid, completion, data); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_aexists(zhandle_t *zh, const char *path, int watch, + stat_completion_t sc, const void *data) +{ + return zoo_awexists(zh,path,watch?zh->watcher:0,zh->context,sc,data); +} + +int zoo_awexists(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + stat_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_EXISTS_OP}; + struct ExistsRequest req; + int rc = Request_path_watch_init(zh, 0, &req.path, path, + &req.watch, watcher != NULL); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_ExistsRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_stat_completion(zh, h.xid, completion, data, + create_watcher_registration(req.path,exists_result_checker, + watcher,watcherCtx)); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +static int zoo_awget_children_(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + strings_completion_t sc, + const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_GETCHILDREN_OP}; + struct GetChildrenRequest req ; + int rc = Request_path_watch_init(zh, 0, &req.path, path, + &req.watch, watcher != NULL); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_GetChildrenRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_strings_completion(zh, h.xid, sc, data, + create_watcher_registration(req.path,child_result_checker,watcher,watcherCtx)); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_aget_children(zhandle_t *zh, const char *path, int watch, + strings_completion_t dc, const void *data) +{ + return zoo_awget_children_(zh,path,watch?zh->watcher:0,zh->context,dc,data); +} + +int zoo_awget_children(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + strings_completion_t dc, + const void *data) +{ + return zoo_awget_children_(zh,path,watcher,watcherCtx,dc,data); +} + +static int zoo_awget_children2_(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + strings_stat_completion_t ssc, + const void *data) +{ + /* invariant: (sc == NULL) != (sc == NULL) */ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_GETCHILDREN2_OP}; + struct GetChildren2Request req ; + int rc = Request_path_watch_init(zh, 0, &req.path, path, + &req.watch, watcher != NULL); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_GetChildren2Request(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_strings_stat_completion(zh, h.xid, ssc, data, + create_watcher_registration(req.path,child_result_checker,watcher,watcherCtx)); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_aget_children2(zhandle_t *zh, const char *path, int watch, + strings_stat_completion_t dc, const void *data) +{ + return zoo_awget_children2_(zh,path,watch?zh->watcher:0,zh->context,dc,data); +} + +int zoo_awget_children2(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + strings_stat_completion_t dc, + const void *data) +{ + return zoo_awget_children2_(zh,path,watcher,watcherCtx,dc,data); +} + +int zoo_async(zhandle_t *zh, const char *path, + string_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_SYNC_OP}; + struct SyncRequest req; + int rc = Request_path_init(zh, 0, &req.path, path); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_SyncRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_string_completion(zh, h.xid, completion, data); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + + +int zoo_aget_acl(zhandle_t *zh, const char *path, acl_completion_t completion, + const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_GETACL_OP}; + struct GetACLRequest req; + int rc = Request_path_init(zh, 0, &req.path, path) ; + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_GetACLRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_acl_completion(zh, h.xid, completion, data); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +int zoo_aset_acl(zhandle_t *zh, const char *path, int version, + struct ACL_vector *acl, void_completion_t completion, const void *data) +{ + struct oarchive *oa; + struct RequestHeader h = {get_xid(), ZOO_SETACL_OP}; + struct SetACLRequest req; + int rc = Request_path_init(zh, 0, &req.path, path); + if (rc != ZOK) { + return rc; + } + oa = create_buffer_oarchive(); + req.acl = *acl; + req.version = version; + rc = serialize_RequestHeader(oa, "header", &h); + rc = rc < 0 ? rc : serialize_SetACLRequest(oa, "req", &req); + enter_critical(zh); + rc = rc < 0 ? rc : add_void_completion(zh, h.xid, completion, data); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + free_duplicate_path(req.path, path); + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s",h.xid,path, + zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + return (rc < 0)?ZMARSHALLINGERROR:ZOK; +} + +/* Completions for multi-op results */ +static void op_result_string_completion(int err, const char *value, const void *data) +{ + struct zoo_op_result *result = (struct zoo_op_result *)data; + assert(result); + result->err = err; + + if (result->value && value) { + int len = strlen(value) + 1; + if (len > result->valuelen) { + len = result->valuelen; + } + if (len > 0) { + memcpy(result->value, value, len - 1); + result->value[len - 1] = '\0'; + } + } else { + result->value = NULL; + } +} + +static void op_result_void_completion(int err, const void *data) +{ + struct zoo_op_result *result = (struct zoo_op_result *)data; + assert(result); + result->err = err; +} + +static void op_result_stat_completion(int err, const struct Stat *stat, const void *data) +{ + struct zoo_op_result *result = (struct zoo_op_result *)data; + assert(result); + result->err = err; + + if (result->stat && err == 0 && stat) { + *result->stat = *stat; + } else { + result->stat = NULL ; + } +} + +static int CheckVersionRequest_init(zhandle_t *zh, struct CheckVersionRequest *req, + const char *path, int version) +{ + int rc ; + assert(req); + rc = Request_path_init(zh, 0, &req->path, path); + if (rc != ZOK) { + return rc; + } + req->version = version; + + return ZOK; +} + +int zoo_amulti(zhandle_t *zh, int count, const zoo_op_t *ops, + zoo_op_result_t *results, void_completion_t completion, const void *data) +{ + struct RequestHeader h = {get_xid(), ZOO_MULTI_OP}; + struct MultiHeader mh = {-1, 1, -1}; + struct oarchive *oa = create_buffer_oarchive(); + completion_head_t clist = { 0 }; + + int rc = serialize_RequestHeader(oa, "header", &h); + + int index = 0; + for (index=0; index < count; index++) { + const zoo_op_t *op = ops+index; + zoo_op_result_t *result = results+index; + completion_list_t *entry = NULL; + + struct MultiHeader mh = {op->type, 0, -1}; + rc = rc < 0 ? rc : serialize_MultiHeader(oa, "multiheader", &mh); + + switch(op->type) { + case ZOO_CREATE_CONTAINER_OP: + case ZOO_CREATE_OP: { + struct CreateRequest req; + + rc = rc < 0 ? rc : CreateRequest_init(zh, &req, + op->create_op.path, op->create_op.data, + op->create_op.datalen, op->create_op.acl, + op->create_op.flags); + rc = rc < 0 ? rc : serialize_CreateRequest(oa, "req", &req); + result->value = op->create_op.buf; + result->valuelen = op->create_op.buflen; + + enter_critical(zh); + entry = create_completion_entry(zh, h.xid, COMPLETION_STRING, op_result_string_completion, result, 0, 0); + leave_critical(zh); + free_duplicate_path(req.path, op->create_op.path); + break; + } + + case ZOO_DELETE_OP: { + struct DeleteRequest req; + rc = rc < 0 ? rc : DeleteRequest_init(zh, &req, op->delete_op.path, op->delete_op.version); + rc = rc < 0 ? rc : serialize_DeleteRequest(oa, "req", &req); + + enter_critical(zh); + entry = create_completion_entry(zh, h.xid, COMPLETION_VOID, op_result_void_completion, result, 0, 0); + leave_critical(zh); + free_duplicate_path(req.path, op->delete_op.path); + break; + } + + case ZOO_SETDATA_OP: { + struct SetDataRequest req; + rc = rc < 0 ? rc : SetDataRequest_init(zh, &req, + op->set_op.path, op->set_op.data, + op->set_op.datalen, op->set_op.version); + rc = rc < 0 ? rc : serialize_SetDataRequest(oa, "req", &req); + result->stat = op->set_op.stat; + + enter_critical(zh); + entry = create_completion_entry(zh, h.xid, COMPLETION_STAT, op_result_stat_completion, result, 0, 0); + leave_critical(zh); + free_duplicate_path(req.path, op->set_op.path); + break; + } + + case ZOO_CHECK_OP: { + struct CheckVersionRequest req; + rc = rc < 0 ? rc : CheckVersionRequest_init(zh, &req, + op->check_op.path, op->check_op.version); + rc = rc < 0 ? rc : serialize_CheckVersionRequest(oa, "req", &req); + + enter_critical(zh); + entry = create_completion_entry(zh, h.xid, COMPLETION_VOID, op_result_void_completion, result, 0, 0); + leave_critical(zh); + free_duplicate_path(req.path, op->check_op.path); + break; + } + + default: + LOG_ERROR(LOGCALLBACK(zh), "Unimplemented sub-op type=%d in multi-op", op->type); + return ZUNIMPLEMENTED; + } + + queue_completion(&clist, entry, 0); + } + + rc = rc < 0 ? rc : serialize_MultiHeader(oa, "multiheader", &mh); + + /* BEGIN: CRTICIAL SECTION */ + enter_critical(zh); + rc = rc < 0 ? rc : add_multi_completion(zh, h.xid, completion, data, &clist); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + leave_critical(zh); + + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending multi request xid=%#x with %d subrequests to %s", + h.xid, index, zoo_get_current_server(zh)); + /* make a best (non-blocking) effort to send the requests asap */ + adaptor_send_queue(zh, 0); + + return (rc < 0) ? ZMARSHALLINGERROR : ZOK; +} + +typedef union WatchesRequest WatchesRequest; + +union WatchesRequest { + struct CheckWatchesRequest check; + struct RemoveWatchesRequest remove; +}; + +static int aremove_watches( + zhandle_t *zh, const char *path, ZooWatcherType wtype, + watcher_fn watcher, void *watcherCtx, int local, + void_completion_t *completion, const void *data, int all) +{ + char *server_path = prepend_string(zh, path); + int rc; + struct oarchive *oa; + struct RequestHeader h = { + get_xid(), + all ? ZOO_REMOVE_WATCHES : ZOO_CHECK_WATCHES + }; + WatchesRequest req; + watcher_deregistration_t *wdo; + + if (!zh || !isValidPath(server_path, 0)) { + rc = ZBADARGUMENTS; + goto done; + } + + if (!local && is_unrecoverable(zh)) { + rc = ZINVALIDSTATE; + goto done; + } + + lock_watchers(zh); + if (!pathHasWatcher(zh, server_path, wtype, watcher, watcherCtx)) { + rc = ZNOWATCHER; + unlock_watchers(zh); + goto done; + } + + if (local) { + removeWatchers(zh, server_path, wtype, watcher, watcherCtx); + unlock_watchers(zh); +#ifdef THREADED + notify_sync_completion((struct sync_completion *)data); +#endif + rc = ZOK; + goto done; + } + unlock_watchers(zh); + + oa = create_buffer_oarchive(); + rc = serialize_RequestHeader(oa, "header", &h); + + if (all) { + req.remove.path = (char*)server_path; + req.remove.type = wtype; + rc = rc < 0 ? rc : serialize_RemoveWatchesRequest(oa, "req", &req.remove); + } else { + req.check.path = (char*)server_path; + req.check.type = wtype; + rc = rc < 0 ? rc : serialize_CheckWatchesRequest(oa, "req", &req.check); + } + + if (rc < 0) { + goto done; + } + + wdo = create_watcher_deregistration( + server_path, watcher, watcherCtx, wtype); + + if (!wdo) { + rc = ZSYSTEMERROR; + goto done; + } + + enter_critical(zh); + rc = add_completion_deregistration( + zh, h.xid, COMPLETION_VOID, completion, data, 0, wdo, 0); + rc = rc < 0 ? rc : queue_buffer_bytes(&zh->to_send, get_buffer(oa), + get_buffer_len(oa)); + rc = rc < 0 ? ZMARSHALLINGERROR : ZOK; + leave_critical(zh); + + /* We queued the buffer, so don't free it */ + close_buffer_oarchive(&oa, 0); + + LOG_DEBUG(LOGCALLBACK(zh), "Sending request xid=%#x for path [%s] to %s", + h.xid, path, zoo_get_current_server(zh)); + + adaptor_send_queue(zh, 0); + +done: + free_duplicate_path(server_path, path); + return rc; +} +void zoo_create_op_init(zoo_op_t *op, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len) +{ + assert(op); + op->type = get_create_op_type(mode, ZOO_CREATE_OP); + op->create_op.path = path; + op->create_op.data = value; + op->create_op.datalen = valuelen; + op->create_op.acl = acl; + op->create_op.flags = mode; + op->create_op.ttl = 0; + op->create_op.buf = path_buffer; + op->create_op.buflen = path_buffer_len; +} + +void zoo_create2_op_init(zoo_op_t *op, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len) +{ + assert(op); + op->type = get_create_op_type(mode, ZOO_CREATE2_OP); + op->create_op.path = path; + op->create_op.data = value; + op->create_op.datalen = valuelen; + op->create_op.acl = acl; + op->create_op.flags = mode; + op->create_op.buf = path_buffer; + op->create_op.buflen = path_buffer_len; +} + +void zoo_delete_op_init(zoo_op_t *op, const char *path, int version) +{ + assert(op); + op->type = ZOO_DELETE_OP; + op->delete_op.path = path; + op->delete_op.version = version; +} + +void zoo_set_op_init(zoo_op_t *op, const char *path, const char *buffer, + int buflen, int version, struct Stat *stat) +{ + assert(op); + op->type = ZOO_SETDATA_OP; + op->set_op.path = path; + op->set_op.data = buffer; + op->set_op.datalen = buflen; + op->set_op.version = version; + op->set_op.stat = stat; +} + +void zoo_check_op_init(zoo_op_t *op, const char *path, int version) +{ + assert(op); + op->type = ZOO_CHECK_OP; + op->check_op.path = path; + op->check_op.version = version; +} + +/* specify timeout of 0 to make the function non-blocking */ +/* timeout is in milliseconds */ +int flush_send_queue(zhandle_t*zh, int timeout) +{ + int rc= ZOK; + struct timeval started; +#ifdef _WIN32 + fd_set pollSet; + struct timeval wait; +#endif + get_system_time(&started); + // we can't use dequeue_buffer() here because if (non-blocking) send_buffer() + // returns EWOULDBLOCK we'd have to put the buffer back on the queue. + // we use a recursive lock instead and only dequeue the buffer if a send was + // successful + lock_buffer_list(&zh->to_send); + while (zh->to_send.head != 0 && is_connected(zh)) { + if(timeout!=0){ +#ifndef _WIN32 + struct pollfd fds; +#endif + int elapsed; + struct timeval now; + get_system_time(&now); + elapsed=calculate_interval(&started,&now); + if (elapsed>timeout) { + rc = ZOPERATIONTIMEOUT; + break; + } + +#ifdef _WIN32 + wait = get_timeval(timeout-elapsed); + FD_ZERO(&pollSet); + FD_SET(zh->fd->sock, &pollSet); + // Poll the socket + rc = select((int)(zh->fd->sock)+1, NULL, &pollSet, NULL, &wait); +#else + fds.fd = zh->fd->sock; + fds.events = POLLOUT; + fds.revents = 0; + rc = poll(&fds, 1, timeout-elapsed); +#endif + if (rc<=0) { + /* timed out or an error or POLLERR */ + rc = rc==0 ? ZOPERATIONTIMEOUT : ZSYSTEMERROR; + break; + } + } + + rc = send_buffer(zh, zh->to_send.head); + if(rc==0 && timeout==0){ + /* send_buffer would block while sending this buffer */ + rc = ZOK; + break; + } + if (rc < 0) { + rc = ZCONNECTIONLOSS; + break; + } + // if the buffer has been sent successfully, remove it from the queue + if (rc > 0) + remove_buffer(&zh->to_send); + get_system_time(&zh->last_send); + rc = ZOK; + } + unlock_buffer_list(&zh->to_send); + return rc; +} + +const char* zerror(int c) +{ + switch (c){ + case ZOK: + return "ok"; + case ZSYSTEMERROR: + return "system error"; + case ZRUNTIMEINCONSISTENCY: + return "run time inconsistency"; + case ZDATAINCONSISTENCY: + return "data inconsistency"; + case ZCONNECTIONLOSS: + return "connection loss"; + case ZMARSHALLINGERROR: + return "marshalling error"; + case ZUNIMPLEMENTED: + return "unimplemented"; + case ZOPERATIONTIMEOUT: + return "operation timeout"; + case ZBADARGUMENTS: + return "bad arguments"; + case ZINVALIDSTATE: + return "invalid zhandle state"; + case ZNEWCONFIGNOQUORUM: + return "no quorum of new config is connected and up-to-date with the leader of last commmitted config - try invoking reconfiguration after new servers are connected and synced"; + case ZRECONFIGINPROGRESS: + return "Another reconfiguration is in progress -- concurrent reconfigs not supported (yet)"; + case ZAPIERROR: + return "api error"; + case ZNONODE: + return "no node"; + case ZNOAUTH: + return "not authenticated"; + case ZBADVERSION: + return "bad version"; + case ZNOCHILDRENFOREPHEMERALS: + return "no children for ephemerals"; + case ZNODEEXISTS: + return "node exists"; + case ZNOTEMPTY: + return "not empty"; + case ZSESSIONEXPIRED: + return "session expired"; + case ZINVALIDCALLBACK: + return "invalid callback"; + case ZINVALIDACL: + return "invalid acl"; + case ZAUTHFAILED: + return "authentication failed"; + case ZCLOSING: + return "zookeeper is closing"; + case ZNOTHING: + return "(not error) no server responses to process"; + case ZSESSIONMOVED: + return "session moved to another server, so operation is ignored"; + case ZNOTREADONLY: + return "state-changing request is passed to read-only server"; + case ZEPHEMERALONLOCALSESSION: + return "attempt to create ephemeral node on a local session"; + case ZNOWATCHER: + return "the watcher couldn't be found"; + case ZRECONFIGDISABLED: + return "attempts to perform a reconfiguration operation when reconfiguration feature is disable"; + } + if (c > 0) { + return strerror(c); + } + return "unknown error"; +} + +int zoo_add_auth(zhandle_t *zh,const char* scheme,const char* cert, + int certLen,void_completion_t completion, const void *data) +{ + struct buffer auth; + auth_info *authinfo; + if(scheme==NULL || zh==NULL) + return ZBADARGUMENTS; + + if (is_unrecoverable(zh)) + return ZINVALIDSTATE; + + // [ZOOKEEPER-800] zoo_add_auth should return ZINVALIDSTATE if + // the connection is closed. + if (zoo_state(zh) == 0) { + return ZINVALIDSTATE; + } + + if(cert!=NULL && certLen!=0){ + auth.buff=calloc(1,certLen); + if(auth.buff==0) { + return ZSYSTEMERROR; + } + memcpy(auth.buff,cert,certLen); + auth.len=certLen; + } else { + auth.buff = 0; + auth.len = 0; + } + + zoo_lock_auth(zh); + authinfo = (auth_info*) malloc(sizeof(auth_info)); + authinfo->scheme=strdup(scheme); + authinfo->auth=auth; + authinfo->completion=completion; + authinfo->data=data; + authinfo->next = NULL; + add_last_auth(&zh->auth_h, authinfo); + zoo_unlock_auth(zh); + + if (is_connected(zh) || zh->state == ZOO_ASSOCIATING_STATE) + return send_last_auth_info(zh); + + return ZOK; +} + +static const char* format_endpoint_info(const struct sockaddr_storage* ep) +{ + static char buf[134] = { 0 }; + char addrstr[INET6_ADDRSTRLEN] = { 0 }; + const char *fmtstring; + void *inaddr; + char is_inet6 = 0; // poor man's boolean +#ifdef _WIN32 + char * addrstring; +#endif + int port; + if(ep==0) + return "null"; + +#if defined(AF_INET6) + if(ep->ss_family==AF_INET6){ + inaddr=&((struct sockaddr_in6*)ep)->sin6_addr; + port=((struct sockaddr_in6*)ep)->sin6_port; + is_inet6 = 1; + } else { +#endif + inaddr=&((struct sockaddr_in*)ep)->sin_addr; + port=((struct sockaddr_in*)ep)->sin_port; +#if defined(AF_INET6) + } +#endif + fmtstring = (is_inet6 ? "[%s]:%d" : "%s:%d"); +#ifdef _WIN32 + addrstring = inet_ntoa (*(struct in_addr*)inaddr); + sprintf(buf,fmtstring,addrstring,ntohs(port)); +#else + inet_ntop(ep->ss_family,inaddr,addrstr,sizeof(addrstr)-1); + sprintf(buf,fmtstring,addrstr,ntohs(port)); +#endif + return buf; +} + +log_callback_fn zoo_get_log_callback(const zhandle_t* zh) +{ + // Verify we have a valid handle + if (zh == NULL) { + return NULL; + } + + return zh->log_callback; +} + +void zoo_set_log_callback(zhandle_t *zh, log_callback_fn callback) +{ + // Verify we have a valid handle + if (zh == NULL) { + return; + } + + zh->log_callback = callback; +} + +void zoo_deterministic_conn_order(int yesOrNo) +{ + disable_conn_permute=yesOrNo; +} + +#ifdef THREADED + +static void process_sync_completion(zhandle_t *zh, + completion_list_t *cptr, + struct sync_completion *sc, + struct iarchive *ia) +{ + LOG_DEBUG(LOGCALLBACK(zh), "Processing sync_completion with type=%d xid=%#x rc=%d", + cptr->c.type, cptr->xid, sc->rc); + + switch(cptr->c.type) { + case COMPLETION_DATA: + if (sc->rc==0) { + struct GetDataResponse res; + int len; + deserialize_GetDataResponse(ia, "reply", &res); + if (res.data.len <= sc->u.data.buff_len) { + len = res.data.len; + } else { + len = sc->u.data.buff_len; + } + sc->u.data.buff_len = len; + // check if len is negative + // just of NULL which is -1 int + if (len == -1) { + sc->u.data.buffer = NULL; + } else { + memcpy(sc->u.data.buffer, res.data.buff, len); + } + sc->u.data.stat = res.stat; + deallocate_GetDataResponse(&res); + } + break; + case COMPLETION_STAT: + if (sc->rc==0) { + struct SetDataResponse res; + deserialize_SetDataResponse(ia, "reply", &res); + sc->u.stat = res.stat; + deallocate_SetDataResponse(&res); + } + break; + case COMPLETION_STRINGLIST: + if (sc->rc==0) { + struct GetChildrenResponse res; + deserialize_GetChildrenResponse(ia, "reply", &res); + sc->u.strs2 = res.children; + /* We don't deallocate since we are passing it back */ + // deallocate_GetChildrenResponse(&res); + } + break; + case COMPLETION_STRINGLIST_STAT: + if (sc->rc==0) { + struct GetChildren2Response res; + deserialize_GetChildren2Response(ia, "reply", &res); + sc->u.strs_stat.strs2 = res.children; + sc->u.strs_stat.stat2 = res.stat; + /* We don't deallocate since we are passing it back */ + // deallocate_GetChildren2Response(&res); + } + break; + case COMPLETION_STRING: + if (sc->rc==0) { + struct CreateResponse res; + int len; + const char * client_path; + deserialize_CreateResponse(ia, "reply", &res); + //ZOOKEEPER-1027 + client_path = sub_string(zh, res.path); + len = strlen(client_path) + 1;if (len > sc->u.str.str_len) { + len = sc->u.str.str_len; + } + if (len > 0) { + memcpy(sc->u.str.str, client_path, len - 1); + sc->u.str.str[len - 1] = '\0'; + } + free_duplicate_path(client_path, res.path); + deallocate_CreateResponse(&res); + } + break; + case COMPLETION_STRING_STAT: + if (sc->rc==0) { + struct Create2Response res; + int len; + const char * client_path; + deserialize_Create2Response(ia, "reply", &res); + client_path = sub_string(zh, res.path); + len = strlen(client_path) + 1; + if (len > sc->u.str.str_len) { + len = sc->u.str.str_len; + } + if (len > 0) { + memcpy(sc->u.str.str, client_path, len - 1); + sc->u.str.str[len - 1] = '\0'; + } + free_duplicate_path(client_path, res.path); + sc->u.stat = res.stat; + deallocate_Create2Response(&res); + } + break; + case COMPLETION_ACLLIST: + if (sc->rc==0) { + struct GetACLResponse res; + deserialize_GetACLResponse(ia, "reply", &res); + sc->u.acl.acl = res.acl; + sc->u.acl.stat = res.stat; + /* We don't deallocate since we are passing it back */ + //deallocate_GetACLResponse(&res); + } + break; + case COMPLETION_VOID: + break; + case COMPLETION_MULTI: + sc->rc = deserialize_multi(zh, cptr->xid, cptr, ia); + break; + default: + LOG_DEBUG(LOGCALLBACK(zh), "Unsupported completion type=%d", cptr->c.type); + break; + } +} + +/*---------------------------------------------------------------------------* + * SYNC API + *---------------------------------------------------------------------------*/ +int zoo_create(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len) +{ + return zoo_create_ttl(zh, path, value, valuelen, acl, mode, -1, + path_buffer, path_buffer_len); +} + +int zoo_create_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, int64_t ttl, + char *path_buffer, int path_buffer_len) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + sc->u.str.str = path_buffer; + sc->u.str.str_len = path_buffer_len; + rc=zoo_acreate_ttl(zh, path, value, valuelen, acl, mode, ttl, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + } + free_sync_completion(sc); + return rc; +} + +int zoo_create2(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len, struct Stat *stat) +{ + return zoo_create2_ttl(zh, path, value, valuelen, acl, mode, -1, + path_buffer, path_buffer_len, stat); +} + +int zoo_create2_ttl(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, int64_t ttl, + char *path_buffer, int path_buffer_len, struct Stat *stat) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + + sc->u.str.str = path_buffer; + sc->u.str.str_len = path_buffer_len; + rc=zoo_acreate2_ttl(zh, path, value, valuelen, acl, mode, ttl, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0 && stat) { + *stat = sc->u.stat; + } + } + free_sync_completion(sc); + return rc; +} + +int zoo_delete(zhandle_t *zh, const char *path, int version) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc=zoo_adelete(zh, path, version, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + } + free_sync_completion(sc); + return rc; +} + +int zoo_exists(zhandle_t *zh, const char *path, int watch, struct Stat *stat) +{ + return zoo_wexists(zh,path,watch?zh->watcher:0,zh->context,stat); +} + +int zoo_wexists(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, struct Stat *stat) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc=zoo_awexists(zh,path,watcher,watcherCtx,SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0&& stat) { + *stat = sc->u.stat; + } + } + free_sync_completion(sc); + return rc; +} + +int zoo_get(zhandle_t *zh, const char *path, int watch, char *buffer, + int* buffer_len, struct Stat *stat) +{ + return zoo_wget(zh,path,watch?zh->watcher:0,zh->context, + buffer,buffer_len,stat); +} + +int zoo_wget(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + char *buffer, int* buffer_len, struct Stat *stat) +{ + struct sync_completion *sc; + int rc=0; + + if(buffer_len==NULL) + return ZBADARGUMENTS; + if((sc=alloc_sync_completion())==NULL) + return ZSYSTEMERROR; + + sc->u.data.buffer = buffer; + sc->u.data.buff_len = *buffer_len; + rc=zoo_awget(zh, path, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0) { + if(stat) + *stat = sc->u.data.stat; + *buffer_len = sc->u.data.buff_len; + } + } + free_sync_completion(sc); + return rc; +} + +int zoo_getconfig(zhandle_t *zh, int watch, char *buffer, + int* buffer_len, struct Stat *stat) +{ + return zoo_wget(zh,ZOO_CONFIG_NODE,watch?zh->watcher:0,zh->context, buffer,buffer_len,stat); +} + +int zoo_wgetconfig(zhandle_t *zh, watcher_fn watcher, void* watcherCtx, + char *buffer, int* buffer_len, struct Stat *stat) +{ + return zoo_wget(zh, ZOO_CONFIG_NODE, watcher, watcherCtx, buffer, buffer_len, stat); +} + + +int zoo_reconfig(zhandle_t *zh, const char *joining, const char *leaving, + const char *members, int64_t version, char *buffer, int* buffer_len, + struct Stat *stat) +{ + struct sync_completion *sc; + int rc=0; + + if(buffer_len==NULL) + return ZBADARGUMENTS; + if((sc=alloc_sync_completion())==NULL) + return ZSYSTEMERROR; + + sc->u.data.buffer = buffer; + sc->u.data.buff_len = *buffer_len; + rc=zoo_areconfig(zh, joining, leaving, members, version, SYNCHRONOUS_MARKER, sc); + + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0) { + if(stat) + *stat = sc->u.data.stat; + *buffer_len = sc->u.data.buff_len; + } + } + free_sync_completion(sc); + return rc; +} + +int zoo_set(zhandle_t *zh, const char *path, const char *buffer, int buflen, + int version) +{ + return zoo_set2(zh, path, buffer, buflen, version, 0); +} + +int zoo_set2(zhandle_t *zh, const char *path, const char *buffer, int buflen, + int version, struct Stat *stat) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc=zoo_aset(zh, path, buffer, buflen, version, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0 && stat) { + *stat = sc->u.stat; + } + } + free_sync_completion(sc); + return rc; +} + +static int zoo_wget_children_(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + struct String_vector *strings) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc= zoo_awget_children (zh, path, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0) { + if (strings) { + *strings = sc->u.strs2; + } else { + deallocate_String_vector(&sc->u.strs2); + } + } + } + free_sync_completion(sc); + return rc; +} + +static int zoo_wget_children2_(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + struct String_vector *strings, struct Stat *stat) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc= zoo_awget_children2(zh, path, watcher, watcherCtx, SYNCHRONOUS_MARKER, sc); + + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0) { + *stat = sc->u.strs_stat.stat2; + if (strings) { + *strings = sc->u.strs_stat.strs2; + } else { + deallocate_String_vector(&sc->u.strs_stat.strs2); + } + } + } + free_sync_completion(sc); + return rc; +} + +int zoo_get_children(zhandle_t *zh, const char *path, int watch, + struct String_vector *strings) +{ + return zoo_wget_children_(zh,path,watch?zh->watcher:0,zh->context,strings); +} + +int zoo_wget_children(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + struct String_vector *strings) +{ + return zoo_wget_children_(zh,path,watcher,watcherCtx,strings); +} + +int zoo_get_children2(zhandle_t *zh, const char *path, int watch, + struct String_vector *strings, struct Stat *stat) +{ + return zoo_wget_children2_(zh,path,watch?zh->watcher:0,zh->context,strings,stat); +} + +int zoo_wget_children2(zhandle_t *zh, const char *path, + watcher_fn watcher, void* watcherCtx, + struct String_vector *strings, struct Stat *stat) +{ + return zoo_wget_children2_(zh,path,watcher,watcherCtx,strings,stat); +} + +int zoo_get_acl(zhandle_t *zh, const char *path, struct ACL_vector *acl, + struct Stat *stat) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc=zoo_aget_acl(zh, path, SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + if (rc == 0&& stat) { + *stat = sc->u.acl.stat; + } + if (rc == 0) { + if (acl) { + *acl = sc->u.acl.acl; + } else { + deallocate_ACL_vector(&sc->u.acl.acl); + } + } + } + free_sync_completion(sc); + return rc; +} + +int zoo_set_acl(zhandle_t *zh, const char *path, int version, + const struct ACL_vector *acl) +{ + struct sync_completion *sc = alloc_sync_completion(); + int rc; + if (!sc) { + return ZSYSTEMERROR; + } + rc=zoo_aset_acl(zh, path, version, (struct ACL_vector*)acl, + SYNCHRONOUS_MARKER, sc); + if(rc==ZOK){ + wait_sync_completion(sc); + rc = sc->rc; + } + free_sync_completion(sc); + return rc; +} + +static int remove_watches( + zhandle_t *zh, const char *path, ZooWatcherType wtype, + watcher_fn watcher, void *wctx, int local, int all) +{ + int rc = 0; + struct sync_completion *sc; + + if (!path) + return ZBADARGUMENTS; + + sc = alloc_sync_completion(); + if (!sc) + return ZSYSTEMERROR; + + rc = aremove_watches(zh, path, wtype, watcher, wctx, local, + SYNCHRONOUS_MARKER, sc, all); + if (rc == ZOK) { + wait_sync_completion(sc); + rc = sc->rc; + } + free_sync_completion(sc); + return rc; +} + +int zoo_multi(zhandle_t *zh, int count, const zoo_op_t *ops, zoo_op_result_t *results) +{ + int rc; + + struct sync_completion *sc = alloc_sync_completion(); + if (!sc) { + return ZSYSTEMERROR; + } + + rc = zoo_amulti(zh, count, ops, results, SYNCHRONOUS_MARKER, sc); + if (rc == ZOK) { + wait_sync_completion(sc); + rc = sc->rc; + } + free_sync_completion(sc); + + return rc; +} + +int zoo_remove_watches(zhandle_t *zh, const char *path, ZooWatcherType wtype, + watcher_fn watcher, void *watcherCtx, int local) +{ + return remove_watches(zh, path, wtype, watcher, watcherCtx, local, 0); +} + +int zoo_remove_all_watches( + zhandle_t *zh, const char *path, ZooWatcherType wtype, int local) +{ + return remove_watches(zh, path, wtype, NULL, NULL, local, 1); + +} +#endif + +int zoo_aremove_watches(zhandle_t *zh, const char *path, ZooWatcherType wtype, + watcher_fn watcher, void *watcherCtx, int local, + void_completion_t *completion, const void *data) +{ + return aremove_watches( + zh, path, wtype, watcher, watcherCtx, local, completion, data, 0); +} + +int zoo_aremove_all_watches(zhandle_t *zh, const char *path, + ZooWatcherType wtype, int local, void_completion_t *completion, + const void *data) +{ + return aremove_watches( + zh, path, wtype, NULL, NULL, local, completion, data, 1); +} diff --git a/zookeeper-client/zookeeper-client-c/ssl/gencerts.sh b/zookeeper-client/zookeeper-client-c/ssl/gencerts.sh new file mode 100644 index 0000000..f32cf58 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/ssl/gencerts.sh @@ -0,0 +1,127 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# This script cleans up old transaction logs and snapshots +# + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + + +# determining the domain name in the certificates: +# - use the first commandline argument, if present +# - if not, then use the fully qualified domain name +# - if `hostname` command fails, fall back to zookeeper.apache.org +FQDN=`hostname -f` +FQDN=${1:-$FQDN} +FQDN=${FQDN:-"zookeeper.apache.org"} + +# Generate the root key +openssl genrsa -out rootkey.pem 2048 + +#Generate the root Cert +openssl req -x509 -new -key rootkey.pem -out root.crt -config <( +cat <<-EOF +[ req ] +default_bits = 2048 +prompt = no +default_md = sha256 +distinguished_name = dn + +[ dn ] +C = US +ST = California +L = San Francisco +O = ZooKeeper +emailAddress = dev@$FQDN +CN = $FQDN +EOF +) + +#Generate Client Key +openssl genrsa -out clientkey.pem 2048 + +#Generate Client Cert +openssl req -new -key clientkey.pem -out client.csr -config <( +cat <<-EOF +[ req ] +default_bits = 2048 +prompt = no +default_md = sha256 +distinguished_name = dn + +[ dn ] +C = US +ST = California +L = San Francisco +O = ZooKeeper +emailAddress = dev@$FQDN +CN = $FQDN +EOF +) +openssl x509 -req -in client.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -days 3650 -out client.crt + +#Export in pkcs12 format +openssl pkcs12 -export -in client.crt -inkey clientkey.pem -out client.pkcs12 -password pass:password + +# Import Keystore in JKS +keytool -importkeystore -srckeystore client.pkcs12 -destkeystore client.jks -srcstoretype pkcs12 -srcstorepass password -deststorepass password + +############################################################ + +#Generate Server key +openssl genrsa -out serverkey.pem 2048 + +#Generate Server Cert +openssl req -new -key serverkey.pem -out server.csr -config <( +cat <<-EOF +[ req ] +default_bits = 2048 +prompt = no +default_md = sha256 +distinguished_name = dn + +[ dn ] +C = US +ST = California +L = San Francisco +O = ZooKeeper +emailAddress = dev@$FQDN +CN = $FQDN +EOF +) +openssl x509 -req -in server.csr -CA root.crt -CAkey rootkey.pem -CAcreateserial -days 3650 -out server.crt + +#Export in pkcs12 format +openssl pkcs12 -export -in server.crt -inkey serverkey.pem -out server.pkcs12 -password pass:password + +# Import Keystore in JKS +keytool -importkeystore -srckeystore server.pkcs12 -destkeystore server.jks -srcstoretype pkcs12 -srcstorepass password -deststorepass password + + +keytool -importcert -keystore server.jks -file root.crt -storepass password -noprompt + +keytool -importcert -alias ca -file root.crt -keystore clienttrust.jks -storepass password -noprompt + +keytool -importcert -alias clientcert -file client.crt -keystore clienttrust.jks -storepass password -noprompt + +keytool -importcert -alias ca -file root.crt -keystore servertrust.jks -storepass password -noprompt +keytool -importcert -alias servercert -file server.crt -keystore servertrust.jks -storepass password -noprompt diff --git a/zookeeper-client/zookeeper-client-c/tests/CollectionUtil.h b/zookeeper-client/zookeeper-client-c/tests/CollectionUtil.h new file mode 100644 index 0000000..dd34811 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/CollectionUtil.h @@ -0,0 +1,195 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#ifndef _COLLECTION_UTIL_H_ +#define _COLLECTION_UTIL_H_ + +/** + * \file + * CollectionBuilder and DictionaryBuilder classes and collection utility functions + */ + +namespace Util +{ + +// ********************************************************* +/** A shortcut to use for building collections. + * This class is a wrapper around standard STL collection containers such as vector. + * It allows one to conveniently build collections at the variable initialization time: + * \code + * #include "CollectionUtil.h" + * #include "Vector.h" // for ostream << operator overload for STL vector + * using Util; + * + * int main() + * { + * typedef vector MyVector; + * MyVector myVector=CollectionBuilder()("str1")("str2")("str3"); + * cout< +class CollectionBuilder +{ +public: + /// Type of the collection container. + typedef CONT CollectionType; + /// Container's value type. + typedef typename CollectionType::value_type value_type; + /// Container's constant iterator type. + typedef typename CollectionType::const_iterator const_iterator; + /// Container's size type. + typedef typename CollectionType::size_type size_type; + + /** Operator function call overload to allow call chaining. + * \param value the value to be inserted into the container + */ + CollectionBuilder& operator()(const value_type& value){ + return push_back(value); + } + /** Same as regular STL push_back() but allows call chaining. + * \param value the value to be inserted into the container + */ + CollectionBuilder& push_back(const value_type& value){ + collection_.push_back(value); + return *this; + } + /// \name Standard STL container interface + /// @{ + const_iterator begin() const{return collection_.begin();} + const_iterator end() const{return collection_.end();} + size_type size() const{return collection_.size();} + void clear() {collection_.clear();} + ///@} + /// Explicit typecast operator. + operator const CollectionType&() const {return collection_;} +private: + /// \cond PRIVATE + CollectionType collection_; + /// \endcond +}; + + +// ********************************************************* +/** A shortcut to use for building dictionaries. + * This class is a wrapper around standard STL associative containers such as map. + * It allows one to conveniently build dictionaries at the variable initialization time: + * \code + * #include "CollectionUtil.h" + * #include "Map.h" // for ostream << operator overload for STL map + * using Util; + * + * int main() + * { + * typedef map MyMap; + * MyMap myMap=DictionaryBuilder()("str1",1)("str2",2)("str3",3); + * cout< +class DictionaryBuilder +{ +public: + /// The type of the associative container + typedef CONT DictionaryType; + /// Container's element type (usually a pair) + typedef typename DictionaryType::value_type value_type; + /// Container's key type + typedef typename DictionaryType::key_type key_type; + /// Container's value type + typedef typename DictionaryType::mapped_type mapped_type; + /// Container's constant iterator type + typedef typename DictionaryType::const_iterator const_iterator; + /// Container's writable iterator type + typedef typename DictionaryType::iterator iterator; + /// Container's size type + typedef typename DictionaryType::size_type size_type; + + /** Operator function call overload to allow call chaining. + * \param key the value key to be inserted + * \param value the value to be inserted into the container + * \return a non-const reference to self + */ + DictionaryBuilder& operator()(const key_type& key,const mapped_type& value){ + dict_.insert(value_type(key,value)); + return *this; + } + /** Lookup value by key. + * \param key the key associated with the value. + * \return a non-const iterator pointing to the element whose key matched the \a key parameter + */ + iterator find(const key_type& key){ + return dict_.find(key); + } + /** Lookup value by key. + * \param key the key associated with the value. + * \return a const iterator pointing to the element whose key matched the \a key parameter + */ + const_iterator find(const key_type& key) const{ + return dict_.find(key); + } + + /// \name Standard STL container interface + /// @{ + const_iterator begin() const{return dict_.begin();} + const_iterator end() const{return dict_.end();} + size_type size() const{return dict_.size();} + void clear() {dict_.clear();} + ///@} + /// Explicit typecast operator. + operator const DictionaryType&() const {return dict_;} +private: + DictionaryType dict_; +}; + + +// *********************************************************** +/** Deletes all dynamically allocated elements of a collection. + * C::value_type is expected to be a pointer to a dynamically allocated object, or it won't compile. + * The function will iterate over all container elements and call delete for each of them. + * \param c a collection (vector,set) whose elements are being deleted. + */ +template +void clearCollection(C& c){ + for(typename C::const_iterator it=c.begin();it!=c.end();++it) + delete *it; + c.clear(); +} + +/** Deletes all dynamically allocated values of the assotiative container. + * The function expects the M::value_type to be a pair<..., ptr_to_type>, or it won't compile. + * It first deletes the objects pointed to by ptr_to_type + * and then clears (calls m.clear()) the container. + * \param m an associative container (map,hash_map) whose elements are being deleted. + */ +template +void clearMap(M& m){ + for(typename M::const_iterator it=m.begin();it!=m.end();++it) + delete it->second; + m.clear(); +} + +} // namespace Util + + +#endif // _COLLECTION_UTIL_H_ diff --git a/zookeeper-client/zookeeper-client-c/tests/CppAssertHelper.h b/zookeeper-client/zookeeper-client-c/tests/CppAssertHelper.h new file mode 100644 index 0000000..3926f51 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/CppAssertHelper.h @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef CPPASSERTHELPER_H_ +#define CPPASSERTHELPER_H_ + +#include + +// make it possible to specify location of the ASSERT call +#define CPPUNIT_ASSERT_EQUAL_LOC(expected,actual,file,line) \ + ( CPPUNIT_NS::assertEquals( (expected), \ + (actual), \ + CPPUNIT_NS::SourceLine(file,line), \ + "" ) ) + +#define CPPUNIT_ASSERT_EQUAL_MESSAGE_LOC(message,expected,actual,file,line) \ + ( CPPUNIT_NS::assertEquals( (expected), \ + (actual), \ + CPPUNIT_NS::SourceLine(file,line), \ + (message) ) ) + +#endif /*CPPASSERTHELPER_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/tests/LibCMocks.cc b/zookeeper-client/zookeeper-client-c/tests/LibCMocks.cc new file mode 100644 index 0000000..8a46ee5 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/LibCMocks.cc @@ -0,0 +1,352 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include // needed for _POSIX_MONOTONIC_CLOCK +#include + +#include "Util.h" +#include "LibCMocks.h" + +#undef USING_DUMA + +using namespace std; + +// ***************************************************************************** +// gethostbyname + +struct hostent* gethostbyname(const char *name) { + if(!Mock_gethostbyname::mock_) + return LIBC_SYMBOLS.gethostbyname(name); + return Mock_gethostbyname::mock_->call(name); +} + +Mock_gethostbyname* Mock_gethostbyname::mock_=0; + +Mock_gethostbyname::~Mock_gethostbyname(){ + mock_=0; + for(unsigned int i=0;icall(p1,p2); +} +#endif + +void* Mock_calloc::call(size_t p1, size_t p2){ +#ifndef USING_DUMA + if(counter++ ==callsBeforeFailure){ + counter=0; + errno=errnoOnFailure; + return 0; + } + return CALL_REAL(calloc,(p1,p2)); +#else + return 0; +#endif +} + +Mock_calloc* Mock_calloc::mock_=0; + +// ***************************************************************************** +// realloc + +#ifndef USING_DUMA +DECLARE_WRAPPER(void*,realloc,(void* p, size_t s)){ + if(!Mock_realloc::mock_) + return LIBC_SYMBOLS.realloc(p,s); + return Mock_realloc::mock_->call(p,s); +} +#endif + +Mock_realloc* Mock_realloc::mock_=0; + +void* Mock_realloc::call(void* p, size_t s){ + if(counter++ ==callsBeforeFailure){ + counter=0; + errno=errnoOnFailure; + return 0; + } + return LIBC_SYMBOLS.realloc(p,s); +} + +// ***************************************************************************** +// random +RANDOM_RET_TYPE random(){ + if(!Mock_random::mock_) + return LIBC_SYMBOLS.random(); + return Mock_random::mock_->call(); +} + +void srandom(unsigned long seed){ + if (!Mock_random::mock_) + LIBC_SYMBOLS.srandom(seed); + else + Mock_random::mock_->setSeed(seed); +} + +Mock_random* Mock_random::mock_=0; + +int Mock_random::call(){ + assert("Must specify one or more random integers"&&(randomReturns.size()!=0)); + return randomReturns[currentIdx++ % randomReturns.size()]; +} + +// ***************************************************************************** +// free +#ifndef USING_DUMA +DECLARE_WRAPPER(void,free,(void* p)){ + if(Mock_free_noop::mock_ && !Mock_free_noop::mock_->nested) + Mock_free_noop::mock_->call(p); + else + CALL_REAL(free,(p)); +} +#endif + +void Mock_free_noop::call(void* p){ + // on cygwin libc++ is linked statically + // push_back() may call free(), hence the nesting guards + synchronized(mx); + nested++; + callCounter++; + requested.push_back(p); + nested--; +} +void Mock_free_noop::freeRequested(){ +#ifndef USING_DUMA + synchronized(mx); + for(unsigned i=0; icallSocket(domain,type,protocol); +} + +int close(int fd){ + if (!Mock_socket::mock_) + return LIBC_SYMBOLS.close(fd); + return Mock_socket::mock_->callClose(fd); +} + +int getsockopt(int s,int level,int optname,void *optval,socklen_t *optlen){ + if (!Mock_socket::mock_) + return LIBC_SYMBOLS.getsockopt(s,level,optname,optval,optlen); + return Mock_socket::mock_->callGet(s,level,optname,optval,optlen); +} + +int setsockopt(int s,int level,int optname,const void *optval,socklen_t optlen){ + if (!Mock_socket::mock_) + return LIBC_SYMBOLS.setsockopt(s,level,optname,optval,optlen); + return Mock_socket::mock_->callSet(s,level,optname,optval,optlen); +} +int connect(int s,const struct sockaddr *addr,socklen_t len){ +#ifdef AF_UNIX + /* don't mock UNIX domain sockets */ + if (!Mock_socket::mock_ || addr->sa_family == AF_UNIX) +#else + if (!Mock_socket::mock_) +#endif + return LIBC_SYMBOLS.connect(s,addr,len); + return Mock_socket::mock_->callConnect(s,addr,len); +} +ssize_t send(int s,const void *buf,size_t len,int flags){ + if (!Mock_socket::mock_) + return LIBC_SYMBOLS.send(s,buf,len,flags); + return Mock_socket::mock_->callSend(s,buf,len,flags); +} + +ssize_t recv(int s,void *buf,size_t len,int flags){ + if (!Mock_socket::mock_) + return LIBC_SYMBOLS.recv(s,buf,len,flags); + return Mock_socket::mock_->callRecv(s,buf,len,flags); +} + +Mock_socket* Mock_socket::mock_=0; + +// ***************************************************************************** +// fcntl +extern "C" int fcntl(int fd,int cmd,...){ + va_list va; + va_start(va,cmd); + void* arg = va_arg(va, void *); + va_end (va); + if (!Mock_fcntl::mock_) + return LIBC_SYMBOLS.fcntl(fd,cmd,arg); + return Mock_fcntl::mock_->call(fd,cmd,arg); +} + +Mock_fcntl* Mock_fcntl::mock_=0; + +// ***************************************************************************** +// select +int select(int nfds,fd_set *rfds,fd_set *wfds,fd_set *efds,struct timeval *timeout){ + if (!Mock_select::mock_) + return LIBC_SYMBOLS.select(nfds,rfds,wfds,efds,timeout); + return Mock_select::mock_->call(nfds,rfds,wfds,efds,timeout); +} + +Mock_select* Mock_select::mock_=0; + +// ***************************************************************************** +// poll +Mock_poll* Mock_poll::mock_=0; +int poll(struct pollfd *fds, POLL_NFDS_TYPE nfds, int timeout){ + if (!Mock_poll::mock_) + return LIBC_SYMBOLS.poll(fds,nfds,timeout); + return Mock_poll::mock_->call(fds,nfds,timeout); + +} + +/* + * Recent gcc with -O2 and glibc FORTIFY feature may cause our poll + * mock to be ignored. + */ +#if __USE_FORTIFY_LEVEL > 0 +int __poll_chk (struct pollfd *__fds, nfds_t __nfds, int __timeout, + __SIZE_TYPE__ __fdslen) { + return poll(__fds, __nfds, __timeout); +} +#endif + +// ***************************************************************************** +// gettimeofday +int gettimeofday(struct timeval *tp, GETTIMEOFDAY_ARG2_TYPE tzp){ + if (!Mock_gettimeofday::mock_) + return LIBC_SYMBOLS.gettimeofday(tp,tzp); + return Mock_gettimeofday::mock_->call(tp,tzp); +} + +Mock_gettimeofday* Mock_gettimeofday::mock_=0; + +// ***************************************************************************** +#ifdef _POSIX_MONOTONIC_CLOCK +// clock_gettime +int clock_gettime(clockid_t id, struct timespec *tp) { + if (!Mock_gettimeofday::mock_) + return LIBC_SYMBOLS.clock_gettime(id,tp); + struct timeval tv = { 0 }; + int res = Mock_gettimeofday::mock_->call(&tv, NULL); + tp->tv_sec = tv.tv_sec; + tp->tv_nsec = tv.tv_usec * 1000; + return res; +} +#endif diff --git a/zookeeper-client/zookeeper-client-c/tests/LibCMocks.h b/zookeeper-client/zookeeper-client-c/tests/LibCMocks.h new file mode 100644 index 0000000..5b07cda --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/LibCMocks.h @@ -0,0 +1,408 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef LIBCMOCKS_H_ +#define LIBCMOCKS_H_ + +#include +#include +#include + +#include +#include + +#include "MocksBase.h" +#include "LibCSymTable.h" +#include "ThreadingUtil.h" + +// ***************************************************************************** +// gethostbyname + +class Mock_gethostbyname: public Mock +{ +public: + struct HostEntry: public hostent { + HostEntry(const char* hostName,short addrtype); + ~HostEntry(); + HostEntry& addAlias(const char* alias); + HostEntry& addAddress(const char* addr4); + }; + + Mock_gethostbyname():current(0){mock_=this;} + virtual ~Mock_gethostbyname(); + HostEntry& addHostEntry(const char* hostName,short addrtype=AF_INET); + virtual hostent* call(const char* name); + + typedef std::vector HostEntryCollection; + HostEntryCollection gethostbynameReturns; + int current; + static Mock_gethostbyname* mock_; +}; + +class MockFailed_gethostbyname: public Mock_gethostbyname +{ +public: + MockFailed_gethostbyname():h_errnoReturn(HOST_NOT_FOUND) {} + + int h_errnoReturn; + virtual hostent* call(const char* name) { + h_errno=h_errnoReturn; + return 0; + } +}; + +// ***************************************************************************** +// calloc + +class Mock_calloc: public Mock +{ +public: + Mock_calloc():errnoOnFailure(ENOMEM),callsBeforeFailure(-1),counter(0) { + mock_=this; + } + virtual ~Mock_calloc() {mock_=0;} + + int errnoOnFailure; + int callsBeforeFailure; + int counter; + virtual void* call(size_t p1, size_t p2); + + static Mock_calloc* mock_; +}; + +// ***************************************************************************** +// realloc + +class Mock_realloc: public Mock +{ +public: + Mock_realloc():errnoOnFailure(ENOMEM),callsBeforeFailure(-1),counter(0) { + mock_=this; + } + virtual ~Mock_realloc() {mock_=0;} + + int errnoOnFailure; + int callsBeforeFailure; + int counter; + virtual void* call(void* p, size_t s); + + static Mock_realloc* mock_; +}; + +// ***************************************************************************** +// random + +class Mock_random: public Mock +{ +public: + Mock_random():currentIdx(0) {mock_=this;} + virtual ~Mock_random() {mock_=0;} + + int currentIdx; + std::vector randomReturns; + virtual int call(); + void setSeed(unsigned long){currentIdx=0;} + + static Mock_random* mock_; +}; + +// ***************************************************************************** +// no-op free; keeps track of all deallocation requests +class Mock_free_noop: public Mock +{ + Mutex mx; + std::vector requested; +public: + Mock_free_noop():nested(0),callCounter(0){mock_=this;} + virtual ~Mock_free_noop(){ + mock_=0; + freeRequested(); + } + + int nested; + int callCounter; + virtual void call(void* p); + void freeRequested(); + void disable(){mock_=0;} + // returns number of times the pointer was freed + int getFreeCount(void*); + bool isFreed(void*); + + static Mock_free_noop* mock_; +}; + +// ***************************************************************************** +// socket and related system calls + +class Mock_socket: public Mock +{ +public: + static const int FD=63; + Mock_socket():socketReturns(FD),closeReturns(0),getsocketoptReturns(0), + optvalSO_ERROR(0), + setsockoptReturns(0),connectReturns(0),connectErrno(0), + sendErrno(0),recvErrno(0) + { + mock_=this; + } + virtual ~Mock_socket(){mock_=0;} + + int socketReturns; + virtual int callSocket(int domain, int type, int protocol){ + return socketReturns; + } + int closeReturns; + virtual int callClose(int fd){ + return closeReturns; + } + int getsocketoptReturns; + int optvalSO_ERROR; + virtual int callGet(int s,int level,int optname,void *optval,socklen_t *len){ + if(level==SOL_SOCKET && optname==SO_ERROR){ + setSO_ERROR(optval,*len); + } + return getsocketoptReturns; + } + virtual void setSO_ERROR(void *optval,socklen_t len){ + memcpy(optval,&optvalSO_ERROR,len); + } + + int setsockoptReturns; + virtual int callSet(int s,int level,int optname,const void *optval,socklen_t len){ + return setsockoptReturns; + } + int connectReturns; + int connectErrno; + virtual int callConnect(int s,const struct sockaddr *addr,socklen_t len){ + errno=connectErrno; + return connectReturns; + } + + virtual void notifyBufferSent(const std::string& buffer){} + + int sendErrno; + std::string sendBuffer; + virtual ssize_t callSend(int s,const void *buf,size_t len,int flags){ + if(sendErrno!=0){ + errno=sendErrno; + return -1; + } + // first call to send() is always the length of the buffer to follow + bool sendingLength=sendBuffer.size()==0; + // overwrite the length bytes + sendBuffer.assign((const char*)buf,len); + if(!sendingLength){ + notifyBufferSent(sendBuffer); + sendBuffer.erase(); + } + return len; + } + + int recvErrno; + std::string recvReturnBuffer; + virtual ssize_t callRecv(int s,void *buf,size_t len,int flags){ + if(recvErrno!=0){ + errno=recvErrno; + return -1; + } + int k=std::min(len,recvReturnBuffer.length()); + if(k==0) + return 0; + memcpy(buf,recvReturnBuffer.data(),k); + recvReturnBuffer.erase(0,k); + return k; + } + virtual bool hasMoreRecv() const{ + return recvReturnBuffer.size()!=0; + } + static Mock_socket* mock_; +}; + +// ***************************************************************************** +// fcntl +class Mock_fcntl: public Mock +{ +public: + Mock_fcntl():callReturns(0),trapFD(-1){mock_=this;} + ~Mock_fcntl(){mock_=0;} + + int callReturns; + int trapFD; + virtual int call(int fd, int cmd, void* arg){ + if(trapFD==-1) + return LIBC_SYMBOLS.fcntl(fd,cmd,arg); + return callReturns; + } + + static Mock_fcntl* mock_; +}; + +// ***************************************************************************** +// select +class Mock_select: public Mock +{ +public: + Mock_select(Mock_socket* s,int fd):sock(s), + callReturns(0),myFD(fd),timeout(50) + { + mock_=this; + } + ~Mock_select(){mock_=0;} + + Mock_socket* sock; + int callReturns; + int myFD; + int timeout; //in millis + virtual int call(int nfds,fd_set *rfds,fd_set *wfds,fd_set *efds,struct timeval *tv){ + bool isWritableRequested=(wfds && FD_ISSET(myFD,wfds)); + if(rfds) FD_CLR(myFD,rfds); + if(wfds) FD_CLR(myFD,wfds); + // this timeout is only to prevent a tight loop + timeval myTimeout={0,0}; + if(!isWritableRequested && !isFDReadable()){ + myTimeout.tv_sec=timeout/1000; + myTimeout.tv_usec=(timeout%1000)*1000; + } + LIBC_SYMBOLS.select(nfds,rfds,wfds,efds,&myTimeout); + // myFD is always writable + if(isWritableRequested) FD_SET(myFD,wfds); + // myFD is only readable if the socket has anything to read + if(isFDReadable() && rfds) FD_SET(myFD,rfds); + return callReturns; + } + + virtual bool isFDReadable() const { + return sock->hasMoreRecv(); + } + + static Mock_select* mock_; +}; + +// ***************************************************************************** +// poll +// the last element of the pollfd array is expected to be test FD +class Mock_poll: public Mock +{ +public: + Mock_poll(Mock_socket* s,int fd):sock(s), + callReturns(1),myFD(fd),timeout(50) + { + mock_=this; + } + ~Mock_poll(){mock_=0;} + + Mock_socket* sock; + int callReturns; + int myFD; + int timeout; //in millis + virtual int call(struct pollfd *fds, POLL_NFDS_TYPE nfds, int to) { + pollfd* myPoll=0; + if(fds[nfds-1].fd==myFD) + myPoll=&fds[nfds-1]; + bool isWritableRequested=false; + if(myPoll!=0){ + isWritableRequested=myPoll->events&POLLOUT; + nfds--; + } + LIBC_SYMBOLS.poll(fds,nfds,(!isWritableRequested&&!isFDReadable())?timeout:0); + if(myPoll!=0){ + // myFD is always writable if requested + myPoll->revents=isWritableRequested?POLLOUT:0; + // myFD is only readable if the socket has anything to read + myPoll->revents|=isFDReadable()?POLLIN:0; + } + return callReturns; + } + + virtual bool isFDReadable() const { + return sock->hasMoreRecv(); + } + + static Mock_poll* mock_; +}; + +// ***************************************************************************** +// gettimeofday +class Mock_gettimeofday: public Mock +{ +public: + Mock_gettimeofday(){ + LIBC_SYMBOLS.gettimeofday(&tv,0); + mock_=this; + } + Mock_gettimeofday(const Mock_gettimeofday& other):tv(other.tv){} + Mock_gettimeofday(int32_t sec,int32_t usec){ + tv.tv_sec=sec; + tv.tv_usec=usec; + } + ~Mock_gettimeofday(){mock_=0;} + + timeval tv; + virtual int call(struct timeval *tp, GETTIMEOFDAY_ARG2_TYPE tzp){ + *tp=tv; + return 0; + } + operator timeval() const{ + return tv; + } + // advance secs + virtual void tick(int howmuch=1){tv.tv_sec+=howmuch;} + // advance milliseconds + // can move the clock forward as well as backward by providing a negative + // number + virtual void millitick(int howmuch=1){ + int ms=tv.tv_usec/1000+howmuch; + tv.tv_sec+=ms/1000; + // going backward? + if(ms<0){ + ms=1000-(-ms%1000); //wrap millis around + } + tv.tv_usec=(ms%1000)*1000; + } + virtual void tick(const timeval& howmuch){ + // add milliseconds (discarding microsecond portion) + long ms=tv.tv_usec/1000+howmuch.tv_usec/1000; + tv.tv_sec+=howmuch.tv_sec+ms/1000; + tv.tv_usec=(ms%1000)*1000; + } + static Mock_gettimeofday* mock_; +}; + +// discard microseconds! +inline bool operator==(const timeval& lhs, const timeval& rhs){ + return rhs.tv_sec==lhs.tv_sec && rhs.tv_usec/1000==lhs.tv_usec/1000; +} + +// simplistic implementation: no normalization, assume lhs >= rhs, +// discarding microseconds +inline timeval operator-(const timeval& lhs, const timeval& rhs){ + timeval res; + res.tv_sec=lhs.tv_sec-rhs.tv_sec; + res.tv_usec=(lhs.tv_usec/1000-rhs.tv_usec/1000)*1000; + if(res.tv_usec<0){ + res.tv_sec--; + res.tv_usec=1000000+res.tv_usec%1000000; // wrap the millis around + } + return res; +} + +inline int32_t toMilliseconds(const timeval& tv){ + return tv.tv_sec*1000+tv.tv_usec/1000; +} + +#endif /*LIBCMOCKS_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/tests/LibCSymTable.cc b/zookeeper-client/zookeeper-client-c/tests/LibCSymTable.cc new file mode 100644 index 0000000..23862f3 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/LibCSymTable.cc @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "LibCSymTable.h" +#include // needed for _POSIX_MONOTONIC_CLOCK + +#define LOAD_SYM(sym) \ + sym=(sym##_sig)dlsym(handle,#sym); \ + assert("Unable to load "#sym" from libc"&&sym) + + +LibCSymTable& LibCSymTable::instance(){ + static LibCSymTable tbl; + return tbl; +} + +//****************************************************************************** +// preload original libc symbols +LibCSymTable::LibCSymTable() +{ + void* handle=getHandle(); + LOAD_SYM(gethostbyname); + LOAD_SYM(calloc); + LOAD_SYM(realloc); + LOAD_SYM(free); + LOAD_SYM(random); + LOAD_SYM(srandom); + LOAD_SYM(printf); + LOAD_SYM(socket); + LOAD_SYM(close); + LOAD_SYM(getsockopt); + LOAD_SYM(setsockopt); + LOAD_SYM(fcntl); + LOAD_SYM(connect); + LOAD_SYM(send); + LOAD_SYM(recv); + LOAD_SYM(select); + LOAD_SYM(poll); + LOAD_SYM(gettimeofday); +#ifdef _POSIX_MONOTONIC_CLOCK + LOAD_SYM(clock_gettime); +#endif +#ifdef THREADED + LOAD_SYM(pthread_create); + LOAD_SYM(pthread_detach); + LOAD_SYM(pthread_cond_broadcast); + LOAD_SYM(pthread_cond_destroy); + LOAD_SYM(pthread_cond_init); + LOAD_SYM(pthread_cond_signal); + LOAD_SYM(pthread_cond_timedwait); + LOAD_SYM(pthread_cond_wait); + LOAD_SYM(pthread_join); + LOAD_SYM(pthread_mutex_destroy); + LOAD_SYM(pthread_mutex_init); + LOAD_SYM(pthread_mutex_lock); + LOAD_SYM(pthread_mutex_trylock); + LOAD_SYM(pthread_mutex_unlock); +#endif +} + +void* LibCSymTable::getHandle(){ + static void* handle=0; + if(!handle){ +#ifdef __CYGWIN__ + handle=dlopen("cygwin1.dll",RTLD_LAZY); + assert("Unable to dlopen global sym table"&&handle); +#else + handle=RTLD_NEXT; +#endif + } + return handle; +} diff --git a/zookeeper-client/zookeeper-client-c/tests/LibCSymTable.h b/zookeeper-client/zookeeper-client-c/tests/LibCSymTable.h new file mode 100644 index 0000000..1b6f9db --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/LibCSymTable.h @@ -0,0 +1,111 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef LIBCSYMTABLE_H_ +#define LIBCSYMTABLE_H_ + +#include +#include +#include +#include +#include +#include +#include +#include // needed for _POSIX_MONOTONIC_CLOCK + +#ifdef THREADED +#include +#endif + +#include "config.h" + +// TODO: move all these macros to config.h (generated by autoconf) +#ifdef __CYGWIN__ +#if (CYGWIN_VERSION_DLL_MAJOR < 1007) +#define RANDOM_RET_TYPE int +#else +#define RANDOM_RET_TYPE long int +#endif +#define GETTIMEOFDAY_ARG2_TYPE void* +#else +#define RANDOM_RET_TYPE long int +#define GETTIMEOFDAY_ARG2_TYPE struct timezone* +#endif + +#define DECLARE_SYM(ret,sym,sig) \ + typedef ret (*sym##_sig)sig; \ + static sym##_sig preload_##sym () { \ + static sym##_sig ptr=0;\ + if(!ptr){ void* h=getHandle(); ptr=(sym##_sig)dlsym(h,#sym); } \ + assert("Unable to load "#sym" from libc"&&ptr); \ + return ptr; \ + } \ + sym##_sig sym + +#define LIBC_SYMBOLS LibCSymTable::instance() + +//****************************************************************************** +// preload original libc symbols +struct LibCSymTable +{ + DECLARE_SYM(hostent*,gethostbyname,(const char*)); + DECLARE_SYM(void*,calloc,(size_t, size_t)); + DECLARE_SYM(void*,realloc,(void*, size_t)); + DECLARE_SYM(void,free,(void*)); + DECLARE_SYM(RANDOM_RET_TYPE,random,(void)); + DECLARE_SYM(void,srandom,(unsigned long)); + DECLARE_SYM(int,printf,(const char*, ...)); + DECLARE_SYM(int,socket,(int,int,int)); + DECLARE_SYM(int,close,(int)); + DECLARE_SYM(int,getsockopt,(int,int,int,void*,socklen_t*)); + DECLARE_SYM(int,setsockopt,(int,int,int,const void*,socklen_t)); + DECLARE_SYM(int,fcntl,(int,int,...)); + DECLARE_SYM(int,connect,(int,const struct sockaddr*,socklen_t)); + DECLARE_SYM(ssize_t,send,(int,const void*,size_t,int)); + DECLARE_SYM(ssize_t,recv,(int,const void*,size_t,int)); + DECLARE_SYM(int,select,(int,fd_set*,fd_set*,fd_set*,struct timeval*)); + DECLARE_SYM(int,poll,(struct pollfd*,POLL_NFDS_TYPE,int)); + DECLARE_SYM(int,gettimeofday,(struct timeval*,GETTIMEOFDAY_ARG2_TYPE)); +#ifdef _POSIX_MONOTONIC_CLOCK + DECLARE_SYM(int,clock_gettime,(clockid_t clk_id, struct timespec*)); +#endif +#ifdef THREADED + DECLARE_SYM(int,pthread_create,(pthread_t *, const pthread_attr_t *, + void *(*)(void *), void *)); + DECLARE_SYM(int,pthread_detach,(pthread_t)); + DECLARE_SYM(int,pthread_cond_broadcast,(pthread_cond_t *)); + DECLARE_SYM(int,pthread_cond_destroy,(pthread_cond_t *)); + DECLARE_SYM(int,pthread_cond_init,(pthread_cond_t *, const pthread_condattr_t *)); + DECLARE_SYM(int,pthread_cond_signal,(pthread_cond_t *)); + DECLARE_SYM(int,pthread_cond_timedwait,(pthread_cond_t *, + pthread_mutex_t *, const struct timespec *)); + DECLARE_SYM(int,pthread_cond_wait,(pthread_cond_t *, pthread_mutex_t *)); + DECLARE_SYM(int,pthread_join,(pthread_t, void **)); + DECLARE_SYM(int,pthread_mutex_destroy,(pthread_mutex_t *)); + DECLARE_SYM(int,pthread_mutex_init,(pthread_mutex_t *, const pthread_mutexattr_t *)); + DECLARE_SYM(int,pthread_mutex_lock,(pthread_mutex_t *)); + DECLARE_SYM(int,pthread_mutex_trylock,(pthread_mutex_t *)); + DECLARE_SYM(int,pthread_mutex_unlock,(pthread_mutex_t *)); +#endif + LibCSymTable(); + + static void* getHandle(); + static LibCSymTable& instance(); +}; + +#endif /*LIBCSYMTABLE_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/tests/MocksBase.cc b/zookeeper-client/zookeeper-client-c/tests/MocksBase.cc new file mode 100644 index 0000000..60b2c75 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/MocksBase.cc @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include + +#include "MocksBase.h" +#include "LibCSymTable.h" + +// ***************************************************************************** +// Mock base +void* Mock::operator new(std::size_t s){ + void* p=malloc(s); + if(!p) + throw std::bad_alloc(); + return p; +} + +void Mock::operator delete(void* p){ + LIBC_SYMBOLS.free(p); +} diff --git a/zookeeper-client/zookeeper-client-c/tests/MocksBase.h b/zookeeper-client/zookeeper-client-c/tests/MocksBase.h new file mode 100644 index 0000000..5b54251 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/MocksBase.h @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef MOCKSBASE_H_ +#define MOCKSBASE_H_ + +#include + +// ***************************************************************************** +// Mock base + +class Mock +{ +public: + virtual ~Mock(){} + + static void* operator new(std::size_t s); + static void operator delete(void* p); +}; + +#endif /*MOCKSBASE_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/tests/PthreadMocks.cc b/zookeeper-client/zookeeper-client-c/tests/PthreadMocks.cc new file mode 100644 index 0000000..490cebf --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/PthreadMocks.cc @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "PthreadMocks.h" + +MockPthreadsBase* MockPthreadsBase::mock_=0; + +#undef USING_DUMA + +#ifndef USING_DUMA +int pthread_cond_broadcast (pthread_cond_t *c){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_cond_broadcast(c); + return MockPthreadsBase::mock_->pthread_cond_broadcast(c); +} +int pthread_cond_destroy (pthread_cond_t *c){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_cond_destroy(c); + return MockPthreadsBase::mock_->pthread_cond_destroy(c); +} +int pthread_cond_init (pthread_cond_t *c, const pthread_condattr_t *a){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_cond_init(c,a); + return MockPthreadsBase::mock_->pthread_cond_init(c,a); +} +int pthread_cond_signal (pthread_cond_t *c){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_cond_signal(c); + return MockPthreadsBase::mock_->pthread_cond_signal(c); +} +int pthread_cond_timedwait (pthread_cond_t *c, + pthread_mutex_t *m, const struct timespec *t){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_cond_timedwait(c,m,t); + return MockPthreadsBase::mock_->pthread_cond_timedwait(c,m,t); +} +int pthread_cond_wait (pthread_cond_t *c, pthread_mutex_t *m){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_cond_wait(c,m); + return MockPthreadsBase::mock_->pthread_cond_wait(c,m); +} +int pthread_create (pthread_t *t, const pthread_attr_t *a, + void *(*f)(void *), void *d){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_create(t,a,f,d); + return MockPthreadsBase::mock_->pthread_create(t,a,f,d); +} +int pthread_detach(pthread_t t){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_detach(t); + return MockPthreadsBase::mock_->pthread_detach(t); +} +int pthread_join (pthread_t t, void **r){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_join(t,r); + return MockPthreadsBase::mock_->pthread_join(t,r); +} +int pthread_mutex_destroy (pthread_mutex_t *m){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_mutex_destroy(m); + return MockPthreadsBase::mock_->pthread_mutex_destroy(m); +} +int pthread_mutex_init (pthread_mutex_t *m, const pthread_mutexattr_t *a){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_mutex_init(m,a); + return MockPthreadsBase::mock_->pthread_mutex_init(m,a); +} + +DECLARE_WRAPPER(int,pthread_mutex_lock,(pthread_mutex_t *m)){ + if(!MockPthreadsBase::mock_) + return CALL_REAL(pthread_mutex_lock,(m)); + return MockPthreadsBase::mock_->pthread_mutex_lock(m); +} + +int pthread_mutex_trylock (pthread_mutex_t *m){ + if(!MockPthreadsBase::mock_) + return LIBC_SYMBOLS.pthread_mutex_trylock(m); + return MockPthreadsBase::mock_->pthread_mutex_trylock(m); +} + +DECLARE_WRAPPER(int,pthread_mutex_unlock,(pthread_mutex_t *m)){ + if(!MockPthreadsBase::mock_) + return CALL_REAL(pthread_mutex_unlock,(m)); + return MockPthreadsBase::mock_->pthread_mutex_unlock(m); +} +#endif + +CheckedPthread::ThreadMap CheckedPthread::tmap_; +CheckedPthread::MutexMap CheckedPthread::mmap_; +CheckedPthread::CVMap CheckedPthread::cvmap_; +Mutex CheckedPthread::mx; diff --git a/zookeeper-client/zookeeper-client-c/tests/PthreadMocks.h b/zookeeper-client/zookeeper-client-c/tests/PthreadMocks.h new file mode 100644 index 0000000..3584ec3 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/PthreadMocks.h @@ -0,0 +1,449 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef PTHREADMOCKS_H_ +#define PTHREADMOCKS_H_ + +#include +#include +#include + +#include "src/zk_adaptor.h" + +#include "Util.h" +#include "MocksBase.h" +#include "LibCSymTable.h" +#include "ThreadingUtil.h" + +// an ABC for pthreads +class MockPthreadsBase: public Mock +{ +public: + MockPthreadsBase(){mock_=this;} + virtual ~MockPthreadsBase(){mock_=0;} + + virtual int pthread_create(pthread_t * t, const pthread_attr_t *a, + void *(*f)(void *), void *d) =0; + virtual int pthread_join(pthread_t t, void ** r) =0; + virtual int pthread_detach(pthread_t t) =0; + virtual int pthread_cond_broadcast(pthread_cond_t *c) =0; + virtual int pthread_cond_destroy(pthread_cond_t *c) =0; + virtual int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a) =0; + virtual int pthread_cond_signal(pthread_cond_t *c) =0; + virtual int pthread_cond_timedwait(pthread_cond_t *c, + pthread_mutex_t *m, const struct timespec *t) =0; + virtual int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m) =0; + virtual int pthread_mutex_destroy(pthread_mutex_t *m) =0; + virtual int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a) =0; + virtual int pthread_mutex_lock(pthread_mutex_t *m) =0; + virtual int pthread_mutex_trylock(pthread_mutex_t *m) =0; + virtual int pthread_mutex_unlock(pthread_mutex_t *m) =0; + + static MockPthreadsBase* mock_; +}; + +// all pthread functions simply return an error code +// and increment their invocation counter. No actual threads are spawned. +class MockPthreadsNull: public MockPthreadsBase +{ +public: + MockPthreadsNull(): + pthread_createReturns(0),pthread_createCounter(0), + pthread_joinReturns(0),pthread_joinCounter(0),pthread_joinResultReturn(0), + pthread_detachReturns(0),pthread_detachCounter(0), + pthread_cond_broadcastReturns(0),pthread_cond_broadcastCounter(0), + pthread_cond_destroyReturns(0),pthread_cond_destroyCounter(0), + pthread_cond_initReturns(0),pthread_cond_initCounter(0), + pthread_cond_signalReturns(0),pthread_cond_signalCounter(0), + pthread_cond_timedwaitReturns(0),pthread_cond_timedwaitCounter(0), + pthread_cond_waitReturns(0),pthread_cond_waitCounter(0), + pthread_mutex_destroyReturns(0),pthread_mutex_destroyCounter(0), + pthread_mutex_initReturns(0),pthread_mutex_initCounter(0), + pthread_mutex_lockReturns(0),pthread_mutex_lockCounter(0), + pthread_mutex_trylockReturns(0),pthread_mutex_trylockCounter(0), + pthread_mutex_unlockReturns(0),pthread_mutex_unlockCounter(0) + { + memset(threads,0,sizeof(threads)); + } + + short threads[512]; + + int pthread_createReturns; + int pthread_createCounter; + virtual int pthread_create(pthread_t * t, const pthread_attr_t *a, + void *(*f)(void *), void *d){ + char* p=(char*)&threads[pthread_createCounter++]; + p[0]='i'; // mark as created + *t=(pthread_t)p; + return pthread_createReturns; + } + int pthread_joinReturns; + int pthread_joinCounter; + void* pthread_joinResultReturn; + virtual int pthread_join(pthread_t t, void ** r){ + pthread_joinCounter++; + if(r!=0) + *r=pthread_joinResultReturn; + char* p=(char*)t; + p[0]='x';p[1]+=1; + return pthread_joinReturns; + } + int pthread_detachReturns; + int pthread_detachCounter; + virtual int pthread_detach(pthread_t t){ + pthread_detachCounter++; + char* p=(char*)t; + p[0]='x';p[1]+=1; + return pthread_detachReturns; + } + + template + static bool isInitialized(const T& t){ + return ((char*)t)[0]=='i'; + } + template + static bool isDestroyed(const T& t){ + return ((char*)t)[0]=='x'; + } + template + static int getDestroyCounter(const T& t){ + return ((char*)t)[1]; + } + template + static int getInvalidAccessCounter(const T& t){ + return ((char*)t)[2]; + } + int pthread_cond_broadcastReturns; + int pthread_cond_broadcastCounter; + virtual int pthread_cond_broadcast(pthread_cond_t *c){ + pthread_cond_broadcastCounter++; + if(isDestroyed(c))((char*)c)[2]++; + return pthread_cond_broadcastReturns; + } + int pthread_cond_destroyReturns; + int pthread_cond_destroyCounter; + virtual int pthread_cond_destroy(pthread_cond_t *c){ + pthread_cond_destroyCounter++; + char* p=(char*)c; + p[0]='x';p[1]+=1; + return pthread_cond_destroyReturns; + } + int pthread_cond_initReturns; + int pthread_cond_initCounter; + virtual int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a){ + pthread_cond_initCounter++; + char* p=(char*)c; + p[0]='i'; // mark as created + p[1]=0; // destruction counter + p[2]=0; // access after destruction counter + return pthread_cond_initReturns; + } + int pthread_cond_signalReturns; + int pthread_cond_signalCounter; + virtual int pthread_cond_signal(pthread_cond_t *c){ + pthread_cond_signalCounter++; + if(isDestroyed(c))((char*)c)[2]++; + return pthread_cond_signalReturns; + } + int pthread_cond_timedwaitReturns; + int pthread_cond_timedwaitCounter; + virtual int pthread_cond_timedwait(pthread_cond_t *c, + pthread_mutex_t *m, const struct timespec *t){ + pthread_cond_timedwaitCounter++; + if(isDestroyed(c))((char*)c)[2]++; + return pthread_cond_timedwaitReturns; + } + int pthread_cond_waitReturns; + int pthread_cond_waitCounter; + virtual int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m){ + pthread_cond_waitCounter++; + if(isDestroyed(c))((char*)c)[2]++; + return pthread_cond_waitReturns; + } + int pthread_mutex_destroyReturns; + int pthread_mutex_destroyCounter; + virtual int pthread_mutex_destroy(pthread_mutex_t *m){ + pthread_mutex_destroyCounter++; + char* p=(char*)m; + p[0]='x';p[1]+=1; + return pthread_mutex_destroyReturns; + } + int pthread_mutex_initReturns; + int pthread_mutex_initCounter; + virtual int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a){ + pthread_mutex_initCounter++; + char* p=(char*)m; + p[0]='i'; // mark as created + p[1]=0; // destruction counter + p[2]=0; // access after destruction counter + return pthread_mutex_initReturns; + } + int pthread_mutex_lockReturns; + int pthread_mutex_lockCounter; + virtual int pthread_mutex_lock(pthread_mutex_t *m){ + pthread_mutex_lockCounter++; + if(isDestroyed(m))((char*)m)[2]++; + return pthread_mutex_lockReturns; + } + int pthread_mutex_trylockReturns; + int pthread_mutex_trylockCounter; + virtual int pthread_mutex_trylock(pthread_mutex_t *m){ + pthread_mutex_trylockCounter++; + if(isDestroyed(m))((char*)m)[2]++; + return pthread_mutex_trylockReturns; + } + int pthread_mutex_unlockReturns; + int pthread_mutex_unlockCounter; + virtual int pthread_mutex_unlock(pthread_mutex_t *m){ + pthread_mutex_unlockCounter++; + if(isDestroyed(m))((char*)m)[2]++; + return pthread_mutex_unlockReturns; + } +}; + +// simulates the way zookeeper threads make use of api_prolog/epilog and +// +class MockPthreadZKNull: public MockPthreadsNull +{ + typedef std::map Map; + Map map_; +public: + virtual int pthread_create(pthread_t * t, const pthread_attr_t *a, + void *(*f)(void *), void *d){ + int ret=MockPthreadsNull::pthread_create(t,a,f,d); + zhandle_t* zh=(zhandle_t*)d; + adaptor_threads* ad=(adaptor_threads*)zh->adaptor_priv; + api_prolog(zh); + ad->threadsToWait--; + putValue(map_,*t,zh); + return ret; + } + virtual int pthread_join(pthread_t t, void ** r){ + zhandle_t* zh=0; + if(getValue(map_,t,zh)) + api_epilog(zh,0); + return MockPthreadsNull::pthread_join(t,r); + } +}; + +struct ThreadInfo{ + typedef enum {RUNNING,TERMINATED} ThreadState; + + ThreadInfo(): + destructionCounter_(0),invalidAccessCounter_(0),state_(RUNNING) + { + } + + ThreadInfo& incDestroyed() { + destructionCounter_++; + return *this; + } + ThreadInfo& incInvalidAccess(){ + invalidAccessCounter_++; + return *this; + } + ThreadInfo& setTerminated(){ + state_=TERMINATED; + return *this; + } + int destructionCounter_; + int invalidAccessCounter_; + ThreadState state_; +}; + +class CheckedPthread: public MockPthreadsBase +{ + // first => destruction counter + // second => invalid access counter + //typedef std::pair Entry; + typedef ThreadInfo Entry; + typedef std::map ThreadMap; + static ThreadMap tmap_; + static ThreadMap& getMap(const TypeOp::BareT&){return tmap_;} + typedef std::map MutexMap; + static MutexMap mmap_; + static MutexMap& getMap(const TypeOp::BareT&){return mmap_;} + typedef std::map CVMap; + static CVMap cvmap_; + static CVMap& getMap(const TypeOp::BareT&){return cvmap_;} + + static Mutex mx; + + template + static void markDestroyed(T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + if(getValue(getMap(Type()),t,e)){ + putValue(getMap(Type()),t,Entry(e).incDestroyed()); + }else{ + putValue(getMap(Type()),t,Entry().incDestroyed()); + } + } + template + static void markCreated(T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + if(!getValue(getMap(Type()),t,e)) + putValue(getMap(Type()),t,Entry()); + } + template + static void checkAccessed(T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + if(getValue(getMap(Type()),t,e) && e.destructionCounter_>0) + putValue(getMap(Type()),t,Entry(e).incInvalidAccess()); + } + static void setTerminated(pthread_t t){ + Entry e; + synchronized(mx); + if(getValue(tmap_,t,e)) + putValue(tmap_,t,Entry(e).setTerminated()); + } +public: + bool verbose; + CheckedPthread():verbose(false){ + tmap_.clear(); + mmap_.clear(); + cvmap_.clear(); + mx.release(); + } + template + static bool isInitialized(const T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + return getValue(getMap(Type()),t,e) && e.destructionCounter_==0; + } + template + static bool isDestroyed(const T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + return getValue(getMap(Type()),t,e) && e.destructionCounter_>0; + } + static bool isTerminated(pthread_t t){ + Entry e; + synchronized(mx); + return getValue(tmap_,t,e) && e.state_==ThreadInfo::TERMINATED; + } + template + static int getDestroyCounter(const T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + return getValue(getMap(Type()),t,e)?e.destructionCounter_:-1; + } + template + static int getInvalidAccessCounter(const T& t){ + typedef typename TypeOp::BareT Type; + Entry e; + synchronized(mx); + return getValue(getMap(Type()),t,e)?e.invalidAccessCounter_:-1; + } + + struct ThreadContext{ + typedef void *(*ThreadFunc)(void *); + + ThreadContext(ThreadFunc func,void* param):func_(func),param_(param){} + ThreadFunc func_; + void* param_; + }; + static void* threadFuncWrapper(void* v){ + ThreadContext* ctx=(ThreadContext*)v; + pthread_t t=pthread_self(); + markCreated(t); + void* res=ctx->func_(ctx->param_); + setTerminated(pthread_self()); + delete ctx; + return res; + } + virtual int pthread_create(pthread_t * t, const pthread_attr_t *a, + void *(*f)(void *), void *d) + { + int ret=LIBC_SYMBOLS.pthread_create(t,a,threadFuncWrapper, + new ThreadContext(f,d)); + if(verbose) + TEST_TRACE("thread created %p",*t); + return ret; + } + virtual int pthread_join(pthread_t t, void ** r){ + if(verbose) TEST_TRACE("thread joined %p",t); + int ret=LIBC_SYMBOLS.pthread_join(t,r); + if(ret==0) + markDestroyed(t); + return ret; + } + virtual int pthread_detach(pthread_t t){ + if(verbose) TEST_TRACE("thread detached %p",t); + int ret=LIBC_SYMBOLS.pthread_detach(t); + if(ret==0) + markDestroyed(t); + return ret; + } + virtual int pthread_cond_broadcast(pthread_cond_t *c){ + checkAccessed(c); + return LIBC_SYMBOLS.pthread_cond_broadcast(c); + } + virtual int pthread_cond_destroy(pthread_cond_t *c){ + markDestroyed(c); + return LIBC_SYMBOLS.pthread_cond_destroy(c); + } + virtual int pthread_cond_init(pthread_cond_t *c, const pthread_condattr_t *a){ + markCreated(c); + return LIBC_SYMBOLS.pthread_cond_init(c,a); + } + virtual int pthread_cond_signal(pthread_cond_t *c){ + checkAccessed(c); + return LIBC_SYMBOLS.pthread_cond_signal(c); + } + virtual int pthread_cond_timedwait(pthread_cond_t *c, + pthread_mutex_t *m, const struct timespec *t){ + checkAccessed(c); + return LIBC_SYMBOLS.pthread_cond_timedwait(c,m,t); + } + virtual int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m){ + checkAccessed(c); + return LIBC_SYMBOLS.pthread_cond_wait(c,m); + } + virtual int pthread_mutex_destroy(pthread_mutex_t *m){ + markDestroyed(m); + return LIBC_SYMBOLS.pthread_mutex_destroy(m); + } + virtual int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *a){ + markCreated(m); + return LIBC_SYMBOLS.pthread_mutex_init(m,a); + } + virtual int pthread_mutex_lock(pthread_mutex_t *m){ + checkAccessed(m); + return LIBC_SYMBOLS.pthread_mutex_lock(m); + } + virtual int pthread_mutex_trylock(pthread_mutex_t *m){ + checkAccessed(m); + return LIBC_SYMBOLS.pthread_mutex_trylock(m); + } + virtual int pthread_mutex_unlock(pthread_mutex_t *m){ + checkAccessed(m); + return LIBC_SYMBOLS.pthread_mutex_unlock(m); + } +}; + +#endif /*PTHREADMOCKS_H_*/ + diff --git a/zookeeper-client/zookeeper-client-c/tests/TestClient.cc b/zookeeper-client/zookeeper-client-c/tests/TestClient.cc new file mode 100644 index 0000000..a7d055f --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestClient.cc @@ -0,0 +1,1508 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "CppAssertHelper.h" + +#include +#include +#include +#include + +#include "CollectionUtil.h" +#include "ThreadingUtil.h" + +using namespace Util; + +#include "Vector.h" +using namespace std; + +#include +#include + +#include +#include +#include +#include "Util.h" +#include "ZKMocks.h" + +struct buff_struct_2 { + int32_t len; + int32_t off; + char *buffer; +}; + +// TODO(br33d): the vast majority of this test is not usable with single threaded. +// it needs a overhaul to work properly with both threaded and single +// threaded (ZOOKEEPER-2640) +#ifdef THREADED +// For testing LogMessage Callback functionality +list logMessages; +void logMessageHandler(const char* message) { + cout << "Log Message Received: [" << message << "]" << endl; + logMessages.push_back(message); +} + +static int Stat_eq(struct Stat* a, struct Stat* b) +{ + if (a->czxid != b->czxid) return 0; + if (a->mzxid != b->mzxid) return 0; + if (a->ctime != b->ctime) return 0; + if (a->mtime != b->mtime) return 0; + if (a->version != b->version) return 0; + if (a->cversion != b->cversion) return 0; + if (a->aversion != b->aversion) return 0; + if (a->ephemeralOwner != b->ephemeralOwner) return 0; + if (a->dataLength != b->dataLength) return 0; + if (a->numChildren != b->numChildren) return 0; + if (a->pzxid != b->pzxid) return 0; + return 1; +} +#ifdef THREADED + static void yield(zhandle_t *zh, int i) + { + sleep(i); + } +#else + static void yield(zhandle_t *zh, int seconds) + { + int fd; + int interest; + int events; + struct timeval tv; + int rc; + time_t expires = time(0) + seconds; + time_t timeLeft = seconds; + fd_set rfds, wfds, efds; + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&efds); + + while(timeLeft >= 0) { + zookeeper_interest(zh, &fd, &interest, &tv); + if (fd != -1) { + if (interest&ZOOKEEPER_READ) { + FD_SET(fd, &rfds); + } else { + FD_CLR(fd, &rfds); + } + if (interest&ZOOKEEPER_WRITE) { + FD_SET(fd, &wfds); + } else { + FD_CLR(fd, &wfds); + } + } else { + fd = 0; + } + FD_SET(0, &rfds); + if (tv.tv_sec > timeLeft) { + tv.tv_sec = timeLeft; + } + rc = select(fd+1, &rfds, &wfds, &efds, &tv); + timeLeft = expires - time(0); + events = 0; + if (FD_ISSET(fd, &rfds)) { + events |= ZOOKEEPER_READ; + } + if (FD_ISSET(fd, &wfds)) { + events |= ZOOKEEPER_WRITE; + } + zookeeper_process(zh, events); + } + } +#endif + +typedef struct evt { + string path; + int type; +} evt_t; + +typedef struct watchCtx { +private: + list events; + watchCtx(const watchCtx&); + watchCtx& operator=(const watchCtx&); +public: + bool connected; + zhandle_t *zh; + Mutex mutex; + + watchCtx() { + connected = false; + zh = 0; + } + ~watchCtx() { + if (zh) { + zookeeper_close(zh); + zh = 0; + } + } + + evt_t getEvent() { + evt_t evt; + mutex.acquire(); + CPPUNIT_ASSERT( events.size() > 0); + evt = events.front(); + events.pop_front(); + mutex.release(); + return evt; + } + + int countEvents() { + int count; + mutex.acquire(); + count = events.size(); + mutex.release(); + return count; + } + + void putEvent(evt_t evt) { + mutex.acquire(); + events.push_back(evt); + mutex.release(); + } + + bool waitForConnected(zhandle_t *zh) { + time_t expires = time(0) + 10; + while(!connected && time(0) < expires) { + yield(zh, 1); + } + return connected; + } + + bool waitForDisconnected(zhandle_t *zh) { + time_t expires = time(0) + 15; + while(connected && time(0) < expires) { + yield(zh, 1); + } + return !connected; + } + +} watchctx_t; + +class Zookeeper_simpleSystem : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_simpleSystem); + CPPUNIT_TEST(testLogCallbackSet); + CPPUNIT_TEST(testLogCallbackInit); + CPPUNIT_TEST(testLogCallbackClear); + CPPUNIT_TEST(testAsyncWatcherAutoReset); + CPPUNIT_TEST(testDeserializeString); + CPPUNIT_TEST(testFirstServerDown); + CPPUNIT_TEST(testNonexistentHost); +#ifdef THREADED + CPPUNIT_TEST(testNullData); +#ifdef ZOO_IPV6_ENABLED + CPPUNIT_TEST(testIPV6); +#endif +#ifdef HAVE_OPENSSL_H + CPPUNIT_TEST(testSSL); +#endif + CPPUNIT_TEST(testCreate); + CPPUNIT_TEST(testCreateContainer); + CPPUNIT_TEST(testCreateTtl); + CPPUNIT_TEST(testPath); + CPPUNIT_TEST(testPathValidation); + CPPUNIT_TEST(testPing); + CPPUNIT_TEST(testAcl); + CPPUNIT_TEST(testChroot); + CPPUNIT_TEST(testAuth); + CPPUNIT_TEST(testHangingClient); + CPPUNIT_TEST(testWatcherAutoResetWithGlobal); + CPPUNIT_TEST(testWatcherAutoResetWithLocal); + CPPUNIT_TEST(testGetChildren2); + CPPUNIT_TEST(testLastZxid); + CPPUNIT_TEST(testRemoveWatchers); +#endif + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ + watchctx_t *ctx = (watchctx_t*)v; + + if (state == ZOO_CONNECTED_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + + static const char hostPorts[]; + + const char *getHostPorts() { + return hostPorts; + } + + zhandle_t *createClient(watchctx_t *ctx) { + return createClient(hostPorts, ctx); + } + + zhandle_t *createClient(watchctx_t *ctx, log_callback_fn logCallback) { + zhandle_t *zk = zookeeper_init2(hostPorts, watcher, 10000, 0, ctx, 0, logCallback); + ctx->zh = zk; + sleep(1); + return zk; + } + + zhandle_t *createClient(const char *hp, watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init(hp, watcher, 10000, 0, ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } + +#ifdef HAVE_OPENSSL_H + zhandle_t *createSSLClient(const char *hp, const char *cert, watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init_ssl(hp, cert, watcher, 30000, 0, ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } +#endif + + zhandle_t *createchClient(watchctx_t *ctx, const char* chroot) { + zhandle_t *zk = zookeeper_init(chroot, watcher, 10000, 0, ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } + + FILE *logfile; +public: + + Zookeeper_simpleSystem() { + logfile = openlogfile("Zookeeper_simpleSystem"); + } + + ~Zookeeper_simpleSystem() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + } + + void startServer() { + char cmd[1024]; + sprintf(cmd, "%s start %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void stopServer() { + char cmd[1024]; + sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void tearDown() + { + } + + /** have a callback in the default watcher **/ + static void default_zoo_watcher(zhandle_t *zzh, int type, int state, const char *path, void *context){ + int zrc = 0; + struct String_vector str_vec = {0, NULL}; + zrc = zoo_wget_children(zzh, "/mytest", default_zoo_watcher, NULL, &str_vec); + } + + /** ZOOKEEPER-1057 This checks that the client connects to the second server when the first is not reachable **/ + void testFirstServerDown() { + watchctx_t ctx; + + zoo_deterministic_conn_order(true); + + zhandle_t* zk = createClient("127.0.0.1:22182,127.0.0.1:22181", &ctx); + CPPUNIT_ASSERT(zk != 0); + CPPUNIT_ASSERT(ctx.waitForConnected(zk)); + } + + /* Checks that a non-existent host will not block the connection*/ + void testNonexistentHost() { + char hosts[] = "jimmy:5555,127.0.0.1:22181"; + watchctx_t ctx; + zoo_deterministic_conn_order(true /* disable permute */); + zhandle_t *zh = createClient(hosts, &ctx); + CPPUNIT_ASSERT(ctx.waitForConnected(zh)); + zoo_deterministic_conn_order(false /* enable permute */); + } + + /** this checks for a deadlock in calling zookeeper_close and calls from a default watcher that might get triggered just when zookeeper_close() is in progress **/ + void testHangingClient() { + int zrc = 0; + char buff[10] = "testall"; + char path[512]; + watchctx_t *ctx; + struct String_vector str_vec = {0, NULL}; + zhandle_t *zh = zookeeper_init(hostPorts, NULL, 10000, 0, ctx, 0); + sleep(1); + zrc = zoo_create(zh, "/mytest", buff, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512); + zrc = zoo_wget_children(zh, "/mytest", default_zoo_watcher, NULL, &str_vec); + zrc = zoo_create(zh, "/mytest/test1", buff, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512); + zrc = zoo_wget_children(zh, "/mytest", default_zoo_watcher, NULL, &str_vec); + zrc = zoo_delete(zh, "/mytest/test1", -1); + zookeeper_close(zh); + } + + void testBadDescriptor() { + int zrc = 0; + watchctx_t *ctx; + zhandle_t *zh = zookeeper_init(hostPorts, NULL, 10000, 0, ctx, 0); + sleep(1); + zh->io_count = 0; + //close socket + close_zsock(zh->fd); + sleep(1); + //Check that doIo isn't spinning + CPPUNIT_ASSERT(zh->io_count < 2); + zookeeper_close(zh); + } + + + void testPing() + { + watchctx_t ctxIdle; + watchctx_t ctxWC; + zhandle_t *zkIdle = createClient(&ctxIdle); + zhandle_t *zkWatchCreator = createClient(&ctxWC); + + CPPUNIT_ASSERT(zkIdle); + CPPUNIT_ASSERT(zkWatchCreator); + + char path[80]; + sprintf(path, "/testping"); + int rc = zoo_create(zkWatchCreator, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + for(int i = 0; i < 30; i++) { + sprintf(path, "/testping/%i", i); + rc = zoo_create(zkWatchCreator, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + for(int i = 0; i < 30; i++) { + sprintf(path, "/testping/%i", i); + struct Stat stat; + rc = zoo_exists(zkIdle, path, 1, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + for(int i = 0; i < 30; i++) { + sprintf(path, "/testping/%i", i); + usleep(500000); + rc = zoo_delete(zkWatchCreator, path, -1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + struct Stat stat; + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, zoo_exists(zkIdle, "/testping/0", 0, &stat)); + } + + bool waitForEvent(zhandle_t *zh, watchctx_t *ctx, int seconds) { + time_t expires = time(0) + seconds; + while(ctx->countEvents() == 0 && time(0) < expires) { + yield(zh, 1); + } + return ctx->countEvents() > 0; + } + +#define COUNT 100 + + static zhandle_t *async_zk; + static volatile int count; + static const char* hp_chroot; + + static void statCompletion(int rc, const struct Stat *stat, const void *data) { + int tmp = (int) (long) data; + CPPUNIT_ASSERT_EQUAL(tmp, rc); + } + + static void stringCompletion(int rc, const char *value, const void *data) { + char *path = (char*)data; + + if (rc == ZCONNECTIONLOSS && path) { + // Try again + rc = zoo_acreate(async_zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, stringCompletion, 0); + } else if (rc != ZOK) { + // fprintf(stderr, "rc = %d with path = %s\n", rc, (path ? path : "null")); + } + if (path) { + free(path); + } + } + + static void stringStatCompletion(int rc, const char *value, const struct Stat *stat, + const void *data) { + stringCompletion(rc, value, data); + CPPUNIT_ASSERT(stat != 0); + } + + static void create_completion_fn(int rc, const char* value, const void *data) { + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + if (data) { + const char *expected_value = (const char *)data; + CPPUNIT_ASSERT_EQUAL(string(expected_value), string(value)); + } + count++; + } + + static void waitForCreateCompletion(int seconds) { + time_t expires = time(0) + seconds; + while(count == 0 && time(0) < expires) { + sleep(1); + } + count--; + } + + static void watcher_chroot_fn(zhandle_t *zh, int type, + int state, const char *path,void *watcherCtx) { + // check for path + char *client_path = (char *) watcherCtx; + CPPUNIT_ASSERT(strcmp(client_path, path) == 0); + count ++; + } + + static void waitForChrootWatch(int seconds) { + time_t expires = time(0) + seconds; + while (count == 0 && time(0) < expires) { + sleep(1); + } + count--; + } + + static void waitForVoidCompletion(int seconds) { + time_t expires = time(0) + seconds; + while(count == 0 && time(0) < expires) { + sleep(1); + } + count--; + } + + static void voidCompletion(int rc, const void *data) { + int tmp = (int) (long) data; + CPPUNIT_ASSERT_EQUAL(tmp, rc); + count++; + } + + static void verifyCreateFails(const char *path, zhandle_t *zk) { + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk, + path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0)); + } + + static void verifyCreateOk(const char *path, zhandle_t *zk) { + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk, + path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0)); + } + + static void verifyCreateFailsSeq(const char *path, zhandle_t *zk) { + CPPUNIT_ASSERT_EQUAL((int)ZBADARGUMENTS, zoo_create(zk, + path, "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, 0, 0)); + } + + static void verifyCreateOkSeq(const char *path, zhandle_t *zk) { + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_create(zk, + path, "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, 0, 0)); + } + + + /** + returns false if the vectors dont match + **/ + bool compareAcl(struct ACL_vector acl1, struct ACL_vector acl2) { + if (acl1.count != acl2.count) { + return false; + } + struct ACL *aclval1 = acl1.data; + struct ACL *aclval2 = acl2.data; + if (aclval1->perms != aclval2->perms) { + return false; + } + struct Id id1 = aclval1->id; + struct Id id2 = aclval2->id; + if (strcmp(id1.scheme, id2.scheme) != 0) { + return false; + } + if (strcmp(id1.id, id2.id) != 0) { + return false; + } + return true; + } + + void testDeserializeString() { + char *val_str; + int rc = 0; + int val = -1; + struct iarchive *ia; + struct buff_struct_2 *b; + struct oarchive *oa = create_buffer_oarchive(); + oa->serialize_Int(oa, "int", &val); + b = (struct buff_struct_2 *) oa->priv; + ia = create_buffer_iarchive(b->buffer, b->len); + rc = ia->deserialize_String(ia, "string", &val_str); + CPPUNIT_ASSERT_EQUAL(-EINVAL, rc); + } + + void testAcl() { + int rc; + struct ACL_vector aclvec; + struct Stat stat; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + rc = zoo_create(zk, "/acl", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_get_acl(zk, "/acl", &aclvec, &stat ); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + bool cmp = compareAcl(ZOO_OPEN_ACL_UNSAFE, aclvec); + CPPUNIT_ASSERT_EQUAL(true, cmp); + rc = zoo_set_acl(zk, "/acl", -1, &ZOO_READ_ACL_UNSAFE); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_get_acl(zk, "/acl", &aclvec, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + cmp = compareAcl(ZOO_READ_ACL_UNSAFE, aclvec); + CPPUNIT_ASSERT_EQUAL(true, cmp); + } + + + void testAuth() { + int rc; + count = 0; + watchctx_t ctx1, ctx2, ctx3, ctx4, ctx5; + zhandle_t *zk = createClient(&ctx1); + struct ACL_vector nodeAcl; + struct ACL acl_val; + rc = zoo_add_auth(0, "", 0, 0, voidCompletion, (void*)-1); + CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc); + + rc = zoo_add_auth(zk, 0, 0, 0, voidCompletion, (void*)-1); + CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc); + + // auth as pat, create /tauth1, close session + rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion, + (void*)ZOK); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + + rc = zoo_create(zk, "/tauth1", "", 0, &ZOO_CREATOR_ALL_ACL, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + { + //create a new client + zk = createClient(&ctx4); + rc = zoo_add_auth(zk, "digest", "", 0, voidCompletion, (void*)ZOK); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + + rc = zoo_add_auth(zk, "digest", "", 0, voidCompletion, (void*)ZOK); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + } + + //create a new client + zk = createClient(&ctx2); + + rc = zoo_add_auth(zk, "digest", "pat:passwd2", 11, voidCompletion, + (void*)ZOK); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + + char buf[1024]; + int blen = sizeof(buf); + struct Stat stat; + rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc); + // add auth pat w/correct pass verify success + rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion, + (void*)ZOK); + + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + //create a new client + zk = createClient(&ctx3); + rc = zoo_add_auth(zk, "digest", "pat:passwd", 10, voidCompletion, (void*) ZOK); + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + rc = zoo_add_auth(zk, "ip", "none", 4, voidCompletion, (void*)ZOK); + //make the server forget the auths + waitForVoidCompletion(3); + CPPUNIT_ASSERT(count == 0); + + stopServer(); + CPPUNIT_ASSERT(ctx3.waitForDisconnected(zk)); + startServer(); + CPPUNIT_ASSERT(ctx3.waitForConnected(zk)); + // now try getting the data + rc = zoo_get(zk, "/tauth1", 0, buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + // also check for get + rc = zoo_get_acl(zk, "/", &nodeAcl, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + // check if the acl has all the perms + CPPUNIT_ASSERT_EQUAL((int)1, (int)nodeAcl.count); + acl_val = *(nodeAcl.data); + CPPUNIT_ASSERT_EQUAL((int) acl_val.perms, ZOO_PERM_ALL); + // verify on root node + rc = zoo_set_acl(zk, "/", -1, &ZOO_CREATOR_ALL_ACL); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + rc = zoo_set_acl(zk, "/", -1, &ZOO_OPEN_ACL_UNSAFE); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + //[ZOOKEEPER-1108], test that auth info is sent to server, if client is not + //connected to server when zoo_add_auth was called. + zhandle_t *zk_auth = zookeeper_init(hostPorts, NULL, 10000, 0, NULL, 0); + rc = zoo_add_auth(zk_auth, "digest", "pat:passwd", 10, voidCompletion, (void*)ZOK); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + sleep(2); + CPPUNIT_ASSERT(count == 1); + count = 0; + CPPUNIT_ASSERT_EQUAL((int) ZOK, zookeeper_close(zk_auth)); + + struct sockaddr addr; + socklen_t addr_len = sizeof(addr); + zk = createClient(&ctx5); + stopServer(); + CPPUNIT_ASSERT(ctx5.waitForDisconnected(zk)); + CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) == NULL); + addr_len = sizeof(addr); + startServer(); + CPPUNIT_ASSERT(ctx5.waitForConnected(zk)); + CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) != NULL); + } + + void testCreate() { + watchctx_t ctx; + int rc = 0; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + char pathbuf[80]; + + struct Stat stat_a = {0}; + struct Stat stat_b = {0}; + rc = zoo_create2(zk, "/testcreateA", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strcmp(pathbuf, "/testcreateA") == 0); + CPPUNIT_ASSERT(stat_a.czxid > 0); + CPPUNIT_ASSERT(stat_a.mtime > 0); + + rc = zoo_create2(zk, "/testcreateB", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_b); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strcmp(pathbuf, "/testcreateB") == 0); + CPPUNIT_ASSERT(stat_b.czxid > 0); + CPPUNIT_ASSERT(stat_b.mtime > 0); + + // Should get different Stats back from different creates + CPPUNIT_ASSERT(Stat_eq(&stat_a, &stat_b) != 1); + } + + void testCreateContainer() { + watchctx_t ctx; + int rc = 0; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + char pathbuf[80]; + struct Stat stat = {0}; + + rc = zoo_create2(zk, "/testContainer", "", 0, &ZOO_OPEN_ACL_UNSAFE, + ZOO_CONTAINER, pathbuf, sizeof(pathbuf), &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + } + + void testCreateTtl() { + watchctx_t ctx; + int rc = 0; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + char pathbuf[80]; + struct Stat stat = {0}; + + rc = zoo_create2_ttl(zk, "/testTtl", "", 0, &ZOO_OPEN_ACL_UNSAFE, + ZOO_PERSISTENT_WITH_TTL, 1, pathbuf, sizeof(pathbuf), &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + sleep(1); + + rc = zoo_exists(zk, "/testTtl", 1, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZNONODE, rc); + } + + void testGetChildren2() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + + rc = zoo_create(zk, "/parent", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_create(zk, "/parent/child_a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_create(zk, "/parent/child_b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_create(zk, "/parent/child_c", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_create(zk, "/parent/child_d", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + struct String_vector strings; + struct Stat stat_a, stat_b; + + rc = zoo_get_children2(zk, "/parent", 0, &strings, &stat_a); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_exists(zk, "/parent", 0, &stat_b); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + CPPUNIT_ASSERT(Stat_eq(&stat_a, &stat_b)); + CPPUNIT_ASSERT(stat_a.numChildren == 4); + } + + void testIPV6() { + watchctx_t ctx; + zhandle_t *zk = createClient("::1:22181", &ctx); + CPPUNIT_ASSERT(zk); + int rc = 0; + rc = zoo_create(zk, "/ipv6", NULL, -1, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + } + +#ifdef HAVE_OPENSSL_H + void testSSL() { + watchctx_t ctx; + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + zhandle_t *zk = createSSLClient("127.0.0.1:22281", "/tmp/certs/server.crt,/tmp/certs/client.crt,/tmp/certs/clientkey.pem,password", &ctx); + CPPUNIT_ASSERT(zk); + int rc = 0; + rc = zoo_create(zk, "/ssl", NULL, -1, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + } +#endif + + void testNullData() { + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + int rc = 0; + rc = zoo_create(zk, "/mahadev", NULL, -1, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + char buffer[512]; + struct Stat stat; + int len = 512; + rc = zoo_wget(zk, "/mahadev", NULL, NULL, buffer, &len, &stat); + CPPUNIT_ASSERT_EQUAL( -1, len); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_set(zk, "/mahadev", NULL, -1, -1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_wget(zk, "/mahadev", NULL, NULL, buffer, &len, &stat); + CPPUNIT_ASSERT_EQUAL( -1, len); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + } + + void testPath() { + watchctx_t ctx; + char pathbuf[20]; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + int rc = 0; + + memset(pathbuf, 'X', 20); + rc = zoo_create(zk, "/testpathpath0", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT_EQUAL('X', pathbuf[0]); + + rc = zoo_create(zk, "/testpathpath1", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strlen(pathbuf) == 0); + + rc = zoo_create(zk, "/testpathpath2", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 2); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strcmp(pathbuf, "/") == 0); + + rc = zoo_create(zk, "/testpathpath3", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 3); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strcmp(pathbuf, "/t") == 0); + + rc = zoo_create(zk, "/testpathpath7", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 15); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strcmp(pathbuf, "/testpathpath7") == 0); + + rc = zoo_create(zk, "/testpathpath8", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 16); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT(strcmp(pathbuf, "/testpathpath8") == 0); + } + + void testPathValidation() { + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + + verifyCreateFails(0, zk); + verifyCreateFails("", zk); + verifyCreateFails("//", zk); + verifyCreateFails("///", zk); + verifyCreateFails("////", zk); + verifyCreateFails("/.", zk); + verifyCreateFails("/..", zk); + verifyCreateFails("/./", zk); + verifyCreateFails("/../", zk); + verifyCreateFails("/foo/./", zk); + verifyCreateFails("/foo/../", zk); + verifyCreateFails("/foo/.", zk); + verifyCreateFails("/foo/..", zk); + verifyCreateFails("/./.", zk); + verifyCreateFails("/../..", zk); + verifyCreateFails("/foo/bar/", zk); + verifyCreateFails("/foo//bar", zk); + verifyCreateFails("/foo/bar//", zk); + + verifyCreateFails("foo", zk); + verifyCreateFails("a", zk); + + // verify that trailing fails, except for seq which adds suffix + verifyCreateOk("/createseq", zk); + verifyCreateFails("/createseq/", zk); + verifyCreateOkSeq("/createseq/", zk); + verifyCreateOkSeq("/createseq/.", zk); + verifyCreateOkSeq("/createseq/..", zk); + verifyCreateFailsSeq("/createseq//", zk); + verifyCreateFailsSeq("/createseq/./", zk); + verifyCreateFailsSeq("/createseq/../", zk); + + verifyCreateOk("/.foo", zk); + verifyCreateOk("/.f.", zk); + verifyCreateOk("/..f", zk); + verifyCreateOk("/..f..", zk); + verifyCreateOk("/f.c", zk); + verifyCreateOk("/f", zk); + verifyCreateOk("/f/.f", zk); + verifyCreateOk("/f/f.", zk); + verifyCreateOk("/f/..f", zk); + verifyCreateOk("/f/f..", zk); + verifyCreateOk("/f/.f/f", zk); + verifyCreateOk("/f/f./f", zk); + } + + void testChroot() { + // the c client async callbacks do + // not callback with the path, so + // we dont need to test taht for now + // we should fix that though soon! + watchctx_t ctx, ctx_ch; + zhandle_t *zk, *zk_ch; + char buf[60]; + int rc, len; + struct Stat stat; + const char* data = "garbage"; + const char* retStr = "/chroot"; + const char* root= "/"; + zk_ch = createchClient(&ctx_ch, "127.0.0.1:22181/testch1/mahadev"); + CPPUNIT_ASSERT(zk_ch != NULL); + zk = createClient(&ctx); + // first test with a NULL zk handle, make sure client library does not + // dereference a null pointer, but instead returns ZBADARGUMENTS + rc = zoo_create(NULL, "/testch1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZBADARGUMENTS, rc); + rc = zoo_create(zk, "/testch1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_create(zk, "/testch1/mahadev", data, 7, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + // try an exists with / + len = 60; + rc = zoo_get(zk_ch, "/", 0, buf, &len, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + //check if the data is the same + CPPUNIT_ASSERT(strncmp(buf, data, 7) == 0); + //check for watches + rc = zoo_wexists(zk_ch, "/chroot", watcher_chroot_fn, (void *) retStr, &stat); + //now check if we can do create/delete/get/sets/acls/getChildren and others + //check create + rc = zoo_create(zk_ch, "/chroot", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0,0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForChrootWatch(3); + CPPUNIT_ASSERT(count == 0); + rc = zoo_create(zk_ch, "/chroot/child", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_exists(zk, "/testch1/mahadev/chroot/child", 0, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + rc = zoo_delete(zk_ch, "/chroot/child", -1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_exists(zk, "/testch1/mahadev/chroot/child", 0, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZNONODE, rc); + rc = zoo_wget(zk_ch, "/chroot", watcher_chroot_fn, (char*) retStr, + buf, &len, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_set(zk_ch, "/chroot",buf, 3, -1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForChrootWatch(3); + CPPUNIT_ASSERT(count == 0); + // check for getchildren + struct String_vector children; + rc = zoo_get_children(zk_ch, "/", 0, &children); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL((int)1, (int)children.count); + //check if te child if chroot + CPPUNIT_ASSERT(strcmp((retStr+1), children.data[0]) == 0); + // check for get/set acl + struct ACL_vector acl; + rc = zoo_get_acl(zk_ch, "/", &acl, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL((int)1, (int)acl.count); + CPPUNIT_ASSERT_EQUAL((int)ZOO_PERM_ALL, (int)acl.data->perms); + // set acl + rc = zoo_set_acl(zk_ch, "/chroot", -1, &ZOO_READ_ACL_UNSAFE); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + // see if you add children + rc = zoo_create(zk_ch, "/chroot/child1", "",0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, rc); + //add wget children test + rc = zoo_wget_children(zk_ch, "/", watcher_chroot_fn, (char*) root, &children); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + //now create a node + rc = zoo_create(zk_ch, "/child2", "",0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + waitForChrootWatch(3); + CPPUNIT_ASSERT(count == 0); + //check for one async call just to make sure + rc = zoo_acreate(zk_ch, "/child3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, + create_completion_fn, 0); + waitForCreateCompletion(3); + CPPUNIT_ASSERT(count == 0); + + //ZOOKEEPER-1027 correctly return path_buffer without prefixed chroot + const char* path = "/zookeeper1027"; + char path_buffer[1024]; + int path_buffer_len=sizeof(path_buffer); + rc = zoo_create(zk_ch, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, path_buffer, path_buffer_len); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT_EQUAL(string(path), string(path_buffer)); + + const char* path2282 = "/zookeeper2282"; + rc = zoo_acreate(zk_ch, path2282, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, + create_completion_fn, path2282); + waitForCreateCompletion(3); + CPPUNIT_ASSERT(count == 0); + } + + // Test creating normal handle via zookeeper_init then explicitly setting callback + void testLogCallbackSet() + { + watchctx_t ctx; + CPPUNIT_ASSERT(logMessages.empty()); + zhandle_t *zk = createClient(&ctx); + + zoo_set_log_callback(zk, &logMessageHandler); + CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler); + + // Log 10 messages and ensure all go to callback + int expected = 10; + for (int i = 0; i < expected; i++) + { + LOG_INFO(LOGCALLBACK(zk), "%s #%d", __FUNCTION__, i); + } + CPPUNIT_ASSERT(expected == logMessages.size()); + } + + // Test creating handle via zookeeper_init2 to ensure all connection messages go to callback + void testLogCallbackInit() + { + logMessages.clear(); + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx, &logMessageHandler); + CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler); + + // All the connection messages should have gone to the callback -- don't + // want this to be a maintenance issue so we're not asserting exact count + int numBefore = logMessages.size(); + CPPUNIT_ASSERT(numBefore != 0); + + // Log 10 messages and ensure all go to callback + int expected = 10; + for (int i = 0; i < expected; i++) + { + LOG_INFO(LOGCALLBACK(zk), "%s #%d", __FUNCTION__, i); + } + CPPUNIT_ASSERT(logMessages.size() == numBefore + expected); + } + + // Test clearing log callback -- logging should resume going to logstream + void testLogCallbackClear() + { + logMessages.clear(); + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx, &logMessageHandler); + CPPUNIT_ASSERT_EQUAL(zoo_get_log_callback(zk), &logMessageHandler); + + // All the connection messages should have gone to the callback -- again, we don't + // want this to be a maintenance issue so we're not asserting exact count + int numBefore = logMessages.size(); + CPPUNIT_ASSERT(numBefore > 0); + + // Clear log_callback + zoo_set_log_callback(zk, NULL); + + // Future log messages should go to logstream not callback + LOG_INFO(LOGCALLBACK(zk), __FUNCTION__); + int numAfter = logMessages.size(); + CPPUNIT_ASSERT_EQUAL(numBefore, numAfter); + } + + void testAsyncWatcherAutoReset() + { + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + watchctx_t lctx[COUNT]; + int i; + char path[80]; + int rc; + evt_t evt; + + async_zk = zk; + for(i = 0; i < COUNT; i++) { + sprintf(path, "/awar%d", i); + rc = zoo_awexists(zk, path, watcher, &lctx[i], statCompletion, (void*)ZNONODE); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + yield(zk, 0); + + for(i = 0; i < COUNT/4; i++) { + sprintf(path, "/awar%d", i); + rc = zoo_acreate(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, + stringCompletion, strdup(path)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + for(i = COUNT/4; i < COUNT/2; i++) { + sprintf(path, "/awar%d", i); + rc = zoo_acreate2(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, + stringStatCompletion, strdup(path)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + yield(zk, 3); + for(i = 0; i < COUNT/2; i++) { + sprintf(path, "/awar%d", i); + CPPUNIT_ASSERT_MESSAGE(path, waitForEvent(zk, &lctx[i], 5)); + evt = lctx[i].getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path.c_str(), ZOO_CREATED_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string(path), evt.path); + } + + for(i = COUNT/2 + 1; i < COUNT*10; i++) { + sprintf(path, "/awar%d", i); + rc = zoo_acreate(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, stringCompletion, strdup(path)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + yield(zk, 1); + stopServer(); + CPPUNIT_ASSERT(ctx.waitForDisconnected(zk)); + startServer(); + CPPUNIT_ASSERT(ctx.waitForConnected(zk)); + yield(zk, 3); + for(i = COUNT/2+1; i < COUNT; i++) { + sprintf(path, "/awar%d", i); + CPPUNIT_ASSERT_MESSAGE(path, waitForEvent(zk, &lctx[i], 5)); + evt = lctx[i].getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CREATED_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string(path), evt.path); + } + } + + void testWatcherAutoReset(zhandle_t *zk, watchctx_t *ctxGlobal, + watchctx_t *ctxLocal) + { + bool isGlobal = (ctxGlobal == ctxLocal); + int rc; + struct Stat stat; + char buf[1024]; + int blen; + struct String_vector strings; + const char *testName; + + rc = zoo_create(zk, "/watchtest", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_create(zk, "/watchtest/child", "", 0, + &ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + if (isGlobal) { + testName = "GlobalTest"; + rc = zoo_get_children(zk, "/watchtest", 1, &strings); + deallocate_String_vector(&strings); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + blen = sizeof(buf); + rc = zoo_get(zk, "/watchtest/child", 1, buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_exists(zk, "/watchtest/child2", 1, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + } else { + testName = "LocalTest"; + rc = zoo_wget_children(zk, "/watchtest", watcher, ctxLocal, + &strings); + deallocate_String_vector(&strings); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + blen = sizeof(buf); + rc = zoo_wget(zk, "/watchtest/child", watcher, ctxLocal, + buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_wexists(zk, "/watchtest/child2", watcher, ctxLocal, + &stat); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + } + + CPPUNIT_ASSERT(ctxLocal->countEvents() == 0); + + stopServer(); + CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk)); + startServer(); + CPPUNIT_ASSERT_MESSAGE(testName, ctxLocal->waitForConnected(zk)); + + CPPUNIT_ASSERT(ctxLocal->countEvents() == 0); + + rc = zoo_set(zk, "/watchtest/child", "1", 1, -1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + struct Stat stat1, stat2; + rc = zoo_set2(zk, "/watchtest/child", "1", 1, -1, &stat1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT(stat1.version >= 0); + rc = zoo_set2(zk, "/watchtest/child", "1", 1, stat1.version, &stat2); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_set(zk, "/watchtest/child", "1", 1, stat2.version); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_create(zk, "/watchtest/child2", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5)); + + evt_t evt = ctxLocal->getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHANGED_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string("/watchtest/child"), evt.path); + + CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5)); + // The create will trigget the get children and the + // exists watches + evt = ctxLocal->getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CREATED_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string("/watchtest/child2"), evt.path); + CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5)); + evt = ctxLocal->getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHILD_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string("/watchtest"), evt.path); + + // Make sure Pings are giving us problems + sleep(5); + + CPPUNIT_ASSERT(ctxLocal->countEvents() == 0); + + stopServer(); + CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk)); + startServer(); + CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForConnected(zk)); + + if (isGlobal) { + testName = "GlobalTest"; + rc = zoo_get_children(zk, "/watchtest", 1, &strings); + deallocate_String_vector(&strings); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + blen = sizeof(buf); + rc = zoo_get(zk, "/watchtest/child", 1, buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_exists(zk, "/watchtest/child2", 1, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } else { + testName = "LocalTest"; + rc = zoo_wget_children(zk, "/watchtest", watcher, ctxLocal, + &strings); + deallocate_String_vector(&strings); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + blen = sizeof(buf); + rc = zoo_wget(zk, "/watchtest/child", watcher, ctxLocal, + buf, &blen, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_wexists(zk, "/watchtest/child2", watcher, ctxLocal, + &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + zoo_delete(zk, "/watchtest/child2", -1); + + CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5)); + + evt = ctxLocal->getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_DELETED_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string("/watchtest/child2"), evt.path); + + CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5)); + evt = ctxLocal->getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_CHILD_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string("/watchtest"), evt.path); + + stopServer(); + CPPUNIT_ASSERT_MESSAGE(testName, ctxGlobal->waitForDisconnected(zk)); + startServer(); + CPPUNIT_ASSERT_MESSAGE(testName, ctxLocal->waitForConnected(zk)); + + zoo_delete(zk, "/watchtest/child", -1); + zoo_delete(zk, "/watchtest", -1); + + CPPUNIT_ASSERT_MESSAGE(testName, waitForEvent(zk, ctxLocal, 5)); + + evt = ctxLocal->getEvent(); + CPPUNIT_ASSERT_EQUAL_MESSAGE(evt.path, ZOO_DELETED_EVENT, evt.type); + CPPUNIT_ASSERT_EQUAL(string("/watchtest/child"), evt.path); + + // Make sure nothing is straggling + sleep(1); + CPPUNIT_ASSERT(ctxLocal->countEvents() == 0); + } + + void testWatcherAutoResetWithGlobal() + { + { + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int rc = zoo_create(zk, "/testarwg", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_create(zk, "/testarwg/arwg", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + } + + { + watchctx_t ctx; + zhandle_t *zk = createchClient(&ctx, "127.0.0.1:22181/testarwg/arwg"); + + testWatcherAutoReset(zk, &ctx, &ctx); + } + } + + void testWatcherAutoResetWithLocal() + { + { + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int rc = zoo_create(zk, "/testarwl", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + rc = zoo_create(zk, "/testarwl/arwl", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + } + + { + watchctx_t ctx; + watchctx_t lctx; + zhandle_t *zk = createchClient(&ctx, "127.0.0.1:22181/testarwl/arwl"); + testWatcherAutoReset(zk, &ctx, &lctx); + } + } + + void testLastZxid() { + // ZOOKEEPER-1417: Test that c-client only update last zxid upon + // receiving request response only. + const int timeout = 5000; + int rc; + watchctx_t ctx1, ctx2; + zhandle_t *zk1 = createClient(&ctx1); + zhandle_t *zk2 = createClient(&ctx2); + CPPUNIT_ASSERT(zk1); + CPPUNIT_ASSERT(zk2); + + int64_t original = zk2->last_zxid; + + // Create txn to increase system zxid + rc = zoo_create(zk1, "/lastzxid", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + // This should be enough time for zk2 to receive ping request + usleep(timeout/2 * 1000); + + // check that zk1's last zxid is updated + struct Stat stat; + rc = zoo_exists(zk1, "/lastzxid", 0, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + CPPUNIT_ASSERT_EQUAL((int64_t) zk1->last_zxid, stat.czxid); + // zk2's last zxid should remain the same + CPPUNIT_ASSERT_EQUAL(original, (int64_t) zk2->last_zxid); + + // Perform read and also register a watch + rc = zoo_wexists(zk2, "/lastzxid", watcher, &ctx2, &stat); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + int64_t updated = zk2->last_zxid; + // check that zk2's last zxid is updated + CPPUNIT_ASSERT_EQUAL(updated, stat.czxid); + + // Increment system zxid again + rc = zoo_set(zk1, "/lastzxid", NULL, -1, -1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + // Wait for zk2 to get watch event + CPPUNIT_ASSERT(waitForEvent(zk2, &ctx2, 5)); + // zk2's last zxid should remain the same + CPPUNIT_ASSERT_EQUAL(updated, (int64_t) zk2->last_zxid); + } + + static void watcher_rw(zhandle_t *zh, + int type, + int state, + const char *path, + void *ctx) { + count++; + } + + static void watcher_rw2(zhandle_t *zh, + int type, + int state, + const char *path, + void *ctx) { + count++; + } + + void testRemoveWatchers() { + const char *path = "/something"; + char buf[1024]; + int blen = sizeof(buf); + int rc; + watchctx_t ctx; + zhandle_t *zk; + + /* setup path */ + zk = createClient(&ctx); + CPPUNIT_ASSERT(zk); + + rc = zoo_create(zk, path, "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_create(zk, "/something2", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + /* remove all watchers */ + count = 0; + rc = zoo_wget(zk, path, watcher_rw, NULL, buf, &blen, NULL); + rc = zoo_wget(zk, path, watcher_rw2, NULL, buf, &blen, NULL); + rc = zoo_remove_all_watches(zk, path, ZWATCHTYPE_ANY, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_set(zk, path, "nowatch", 7, -1); + CPPUNIT_ASSERT(count == 0); + + /* remove a specific watcher before it's added (should fail) */ + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA, + watcher_rw, NULL, 0); + CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc); + + /* now add a specific watcher and then remove it */ + rc = zoo_wget(zk, path, watcher_rw, NULL, + buf, &blen, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA, + watcher_rw, NULL, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + /* ditto for children watcher */ + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_CHILD, + watcher_rw, NULL, 0); + CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc); + + struct String_vector str_vec = {0, NULL}; + rc = zoo_wget_children(zk, path, watcher_rw, NULL, + &str_vec); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_CHILD, + watcher_rw, NULL, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + /* add a watch, stop the server, and have remove fail */ + rc = zoo_wget(zk, path, watcher_rw, NULL, + buf, &blen, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + stopServer(); + ctx.waitForDisconnected(zk); + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA, + watcher_rw, NULL, 0); + CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS, rc); + + zookeeper_close(zk); + + /* bring the server back */ + startServer(); + zk = createClient(&ctx); + + /* add a watch, stop the server, and remove it locally */ + void* ctx1=(void*)0x1; + void* ctx2=(void*)0x2; + + rc = zoo_wget(zk, path, watcher_rw, ctx1, + buf, &blen, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_wget(zk, "/something2", watcher_rw, ctx2, + buf, &blen, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + stopServer(); + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA, + watcher_rw, ctx1, 1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + rc = zoo_remove_watches(zk, path, ZWATCHTYPE_DATA, + watcher_rw, ctx1, 1); + CPPUNIT_ASSERT_EQUAL((int)ZNOWATCHER, rc); + + rc = zoo_remove_watches(zk, "/something2", ZWATCHTYPE_DATA, + watcher_rw, ctx2, 1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + } +}; + +volatile int Zookeeper_simpleSystem::count; +zhandle_t *Zookeeper_simpleSystem::async_zk; +const char Zookeeper_simpleSystem::hostPorts[] = "127.0.0.1:22181"; +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_simpleSystem); +#endif diff --git a/zookeeper-client/zookeeper-client-c/tests/TestClientRetry.cc b/zookeeper-client/zookeeper-client-c/tests/TestClientRetry.cc new file mode 100644 index 0000000..c8e9e15 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestClientRetry.cc @@ -0,0 +1,147 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "CppAssertHelper.h" + +#include +#include +#include + +#include "Vector.h" +using namespace std; + +#include + +#include "Util.h" +#include "WatchUtil.h" + +class Zookeeper_clientretry : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_clientretry); +#ifdef THREADED + CPPUNIT_TEST(testRetry); +#endif + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ + watchctx_t *ctx = (watchctx_t*)v; + + if (state == ZOO_CONNECTED_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + + static const char hostPorts[]; + + const char *getHostPorts() { + return hostPorts; + } + + zhandle_t *createClient(watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, + ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } + + FILE *logfile; +public: + + Zookeeper_clientretry() { + logfile = openlogfile("Zookeeper_clientretry"); + } + + ~Zookeeper_clientretry() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + + char cmd[1024]; + sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + + /* we are testing that if max cnxns is exceeded the server does the right thing */ + sprintf(cmd, "ZKMAXCNXNS=1 %s startClean %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + + struct sigaction act; + act.sa_handler = SIG_IGN; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + CPPUNIT_ASSERT(sigaction(SIGPIPE, &act, NULL) == 0); + } + + void tearDown() + { + char cmd[1024]; + sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + + /* restart the server in "normal" mode */ + sprintf(cmd, "%s startClean %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + + struct sigaction act; + act.sa_handler = SIG_IGN; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + CPPUNIT_ASSERT(sigaction(SIGPIPE, &act, NULL) == 0); + } + + bool waitForEvent(zhandle_t *zh, watchctx_t *ctx, int seconds) { + time_t expires = time(0) + seconds; + while(ctx->countEvents() == 0 && time(0) < expires) { + yield(zh, 1); + } + return ctx->countEvents() > 0; + } + + static zhandle_t *async_zk; + + void testRetry() + { + watchctx_t ctx1, ctx2; + zhandle_t *zk1 = createClient(&ctx1); + CPPUNIT_ASSERT_EQUAL(true, ctx1.waitForConnected(zk1)); + zhandle_t *zk2 = createClient(&ctx2); + zookeeper_close(zk1); + CPPUNIT_ASSERT_EQUAL(true, ctx2.waitForConnected(zk2)); + ctx1.zh = 0; + } +}; + +zhandle_t *Zookeeper_clientretry::async_zk; +const char Zookeeper_clientretry::hostPorts[] = "127.0.0.1:22181"; +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_clientretry); diff --git a/zookeeper-client/zookeeper-client-c/tests/TestDriver.cc b/zookeeper-client/zookeeper-client-c/tests/TestDriver.cc new file mode 100644 index 0000000..d60db69 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestDriver.cc @@ -0,0 +1,173 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Util.h" +#include "zookeeper_log.h" + +using namespace std; + +CPPUNIT_NS_BEGIN + +class EclipseOutputter: public CompilerOutputter +{ +public: + EclipseOutputter(TestResultCollector *result,ostream &stream): + CompilerOutputter(result,stream,"%p:%l: "),stream_(stream) + { + } + virtual void printFailedTestName( TestFailure *failure ){} + virtual void printFailureMessage( TestFailure *failure ) + { + stream_<<": "; + Message msg = failure->thrownException()->message(); + stream_<< msg.shortDescription(); + + string text; + for(int i=0; i the output must be in the compiler error format. + //bool selfTest = (argc > 1) && (std::string("-ide") == argv[1]); + globalTestConfig.addConfigFromCmdLine(argc,argv); + + ZKServer zkserver; + + // Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + // Add a listener that colllects test result + CPPUNIT_NS::TestResultCollector result; + controller.addListener( &result ); + + // A listener that print dots as tests run. + // CPPUNIT_NS::TextTestProgressListener progress; + // CPPUNIT_NS::BriefTestProgressListener progress; + + // brief + elapsed time + TimingListener progress; + controller.addListener( &progress ); + + CPPUNIT_NS::TestRunner runner; + runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); + + try { + CPPUNIT_NS::stdCOut() << endl << "Running " << endl; + + zoo_set_debug_level(ZOO_LOG_LEVEL_INFO); + //zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + + runner.run( controller, globalTestConfig.getTestName()); + + // Print test in a compiler compatible format. + CPPUNIT_NS::EclipseOutputter outputter( &result,cout); + outputter.write(); + + // Uncomment this for XML output +#ifdef ENABLE_XML_OUTPUT + std::ofstream file( "tests.xml" ); + CPPUNIT_NS::XmlOutputter xml( &result, file ); + xml.setStyleSheet( "report.xsl" ); + xml.write(); + file.close(); +#endif + } catch ( std::invalid_argument &e ) { + // Test path not resolved + cout<<"\nERROR: "< +#include "CppAssertHelper.h" + +#include +#include + +class Zookeeper_logClientEnv : public CPPUNIT_NS::TestFixture { + CPPUNIT_TEST_SUITE(Zookeeper_logClientEnv); + CPPUNIT_TEST(testLogClientEnv); + CPPUNIT_TEST_SUITE_END(); + + static void log_no_clientenv(const char *message) { + CPPUNIT_ASSERT(::strstr(message, "Client environment") == NULL); + } + + static void log_clientenv(const char *message) { + static int first; + + if (!first) { + CPPUNIT_ASSERT(::strstr(message, "Client environment") != NULL); + first = 1; + } + } + +public: + + void testLogClientEnv() { + zhandle_t* zh; + + zh = zookeeper_init2("localhost:22181", NULL, 0, NULL, NULL, 0, log_clientenv); + CPPUNIT_ASSERT(zh != 0); + zookeeper_close(zh); + + zh = zookeeper_init2("localhost:22181", NULL, 0, NULL, NULL, ZOO_NO_LOG_CLIENTENV, log_no_clientenv); + CPPUNIT_ASSERT(zh != 0); + zookeeper_close(zh); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_logClientEnv); + diff --git a/zookeeper-client/zookeeper-client-c/tests/TestMulti.cc b/zookeeper-client/zookeeper-client-c/tests/TestMulti.cc new file mode 100644 index 0000000..226e470 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestMulti.cc @@ -0,0 +1,770 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "CppAssertHelper.h" + +#include +#include +#include +#include + +#include "CollectionUtil.h" +#include "ThreadingUtil.h" + +using namespace Util; + +#include "Vector.h" +using namespace std; + +#include +#include + +#include +#include +#include +#include "Util.h" + +#ifdef THREADED + static void yield(zhandle_t *zh, int i) + { + sleep(i); + } +#else + static void yield(zhandle_t *zh, int seconds) + { + int fd; + int interest; + int events; + struct timeval tv; + time_t expires = time(0) + seconds; + time_t timeLeft = seconds; + fd_set rfds, wfds, efds; + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&efds); + + while(timeLeft >= 0) { + zookeeper_interest(zh, &fd, &interest, &tv); + if (fd != -1) { + if (interest&ZOOKEEPER_READ) { + FD_SET(fd, &rfds); + } else { + FD_CLR(fd, &rfds); + } + if (interest&ZOOKEEPER_WRITE) { + FD_SET(fd, &wfds); + } else { + FD_CLR(fd, &wfds); + } + } else { + fd = 0; + } + FD_SET(0, &rfds); + if (tv.tv_sec > timeLeft) { + tv.tv_sec = timeLeft; + } + select(fd+1, &rfds, &wfds, &efds, &tv); + timeLeft = expires - time(0); + events = 0; + if (FD_ISSET(fd, &rfds)) { + events |= ZOOKEEPER_READ; + } + if (FD_ISSET(fd, &wfds)) { + events |= ZOOKEEPER_WRITE; + } + zookeeper_process(zh, events); + } + } +#endif + +typedef struct evt { + string path; + int type; +} evt_t; + +typedef struct watchCtx { +private: + list events; + watchCtx(const watchCtx&); + watchCtx& operator=(const watchCtx&); +public: + bool connected; + zhandle_t *zh; + Mutex mutex; + + watchCtx() { + connected = false; + zh = 0; + } + ~watchCtx() { + if (zh) { + zookeeper_close(zh); + zh = 0; + } + } + + evt_t getEvent() { + evt_t evt; + mutex.acquire(); + CPPUNIT_ASSERT( events.size() > 0); + evt = events.front(); + events.pop_front(); + mutex.release(); + return evt; + } + + int countEvents() { + int count; + mutex.acquire(); + count = events.size(); + mutex.release(); + return count; + } + + void putEvent(evt_t evt) { + mutex.acquire(); + events.push_back(evt); + mutex.release(); + } + + bool waitForConnected(zhandle_t *zh) { + time_t expires = time(0) + 10; + while(!connected && time(0) < expires) { + yield(zh, 1); + } + return connected; + } + bool waitForDisconnected(zhandle_t *zh) { + time_t expires = time(0) + 15; + while(connected && time(0) < expires) { + yield(zh, 1); + } + return !connected; + } +} watchctx_t; + +#ifdef THREADED +class Zookeeper_multi : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_multi); +//FIXME: None of these tests pass in single-threaded mode. It seems to be a +//flaw in the test suite setup. + CPPUNIT_TEST(testCreate); + CPPUNIT_TEST(testCreateDelete); + CPPUNIT_TEST(testInvalidVersion); + CPPUNIT_TEST(testNestedCreate); + CPPUNIT_TEST(testSetData); + CPPUNIT_TEST(testUpdateConflict); + CPPUNIT_TEST(testDeleteUpdateConflict); + CPPUNIT_TEST(testAsyncMulti); + CPPUNIT_TEST(testMultiFail); + CPPUNIT_TEST(testCheck); + CPPUNIT_TEST(testWatch); + CPPUNIT_TEST(testSequentialNodeCreateInAsyncMulti); + CPPUNIT_TEST(testBigAsyncMulti); + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ + watchctx_t *ctx = (watchctx_t*)v; + + if (state == ZOO_CONNECTED_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + + static const char hostPorts[]; + + const char *getHostPorts() { + return hostPorts; + } + + zhandle_t *createClient(watchctx_t *ctx) { + return createClient(hostPorts, ctx); + } + + zhandle_t *createClient(const char *hp, watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init(hp, watcher, 10000, 0, ctx, 0); + ctx->zh = zk; + CPPUNIT_ASSERT_EQUAL(true, ctx->waitForConnected(zk)); + return zk; + } + + FILE *logfile; +public: + + Zookeeper_multi() { + logfile = openlogfile("Zookeeper_multi"); + } + + ~Zookeeper_multi() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + } + + void tearDown() + { + } + + static volatile int count; + + static void multi_completion_fn(int rc, const void *data) { + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + count++; + } + + static void multi_completion_fn_no_assert(int rc, const void *data) { + count++; + } + + static void multi_completion_fn_rc(int rc, const void *data) { + count++; + *((int*) data) = rc; + } + + static void create_completion_fn_rc(int rc, const char* value, const void *data) { + count++; + *((int*) data) = rc; + } + + static void waitForMultiCompletion(int seconds) { + time_t expires = time(0) + seconds; + while(count == 0 && time(0) < expires) { + sleep(1); + } + count--; + } + + static void resetCounter() { + count = 0; + } + + /** + * Test basic multi-op create functionality + */ + void testCreate() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + + int sz = 512; + char p1[sz]; + char p2[sz]; + char p3[sz]; + p1[0] = p2[0] = p3[0] = '\0'; + + int nops = 3 ; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_create_op_init(&ops[1], "/multi1/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz); + zoo_create_op_init(&ops[2], "/multi1/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + CPPUNIT_ASSERT(strcmp(p1, "/multi1") == 0); + CPPUNIT_ASSERT(strcmp(p2, "/multi1/a") == 0); + CPPUNIT_ASSERT(strcmp(p3, "/multi1/b") == 0); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err); + } + + /** + * Test create followed by delete + */ + void testCreateDelete() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + char p1[sz]; + p1[0] = '\0'; + int nops = 2 ; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi2", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_delete_op_init(&ops[1], "/multi2", 0); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + // '/multi2' should have been deleted + rc = zoo_exists(zk, "/multi2", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + } + + /** + * Test invalid versions + */ + void testInvalidVersion() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int nops = 4; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0); + zoo_delete_op_init(&ops[1], "/multi3", 1); + zoo_create_op_init(&ops[2], "/multi3", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0); + zoo_create_op_init(&ops[3], "/multi3/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err); + CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[1].err); + CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[2].err); + CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[3].err); + } + + /** + * Test nested creates that rely on state in earlier op in multi + */ + void testNestedCreate() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + char p1[sz]; + p1[0] = '\0'; + int nops = 6; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + /* Create */ + zoo_create_op_init(&ops[0], "/multi4", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_create_op_init(&ops[1], "/multi4/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_create_op_init(&ops[2], "/multi4/a/1", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + + /* Delete */ + zoo_delete_op_init(&ops[3], "/multi4/a/1", 0); + zoo_delete_op_init(&ops[4], "/multi4/a", 0); + zoo_delete_op_init(&ops[5], "/multi4", 0); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + // Verify tree deleted + rc = zoo_exists(zk, "/multi4/a/1", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + + rc = zoo_exists(zk, "/multi4/a", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + + rc = zoo_exists(zk, "/multi4", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + } + + /** + * Test setdata functionality + */ + void testSetData() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + struct Stat s1; + + char buf[sz]; + int blen = sz ; + + char p1[sz], p2[sz]; + + int nops = 2; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi5", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_create_op_init(&ops[1], "/multi5/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + yield(zk, 5); + + zoo_op_t setdata_ops[nops]; + zoo_op_result_t setdata_results[nops]; + + zoo_set_op_init(&setdata_ops[0], "/multi5", "1", 1, 0, &s1); + zoo_set_op_init(&setdata_ops[1], "/multi5/a", "2", 1, 0, &s1); + + rc = zoo_multi(zk, nops, setdata_ops, setdata_results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err); + + memset(buf, '\0', blen); + rc = zoo_get(zk, "/multi5", 0, buf, &blen, &s1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL(1, blen); + CPPUNIT_ASSERT(strcmp("1", buf) == 0); + + memset(buf, '\0', blen); + rc = zoo_get(zk, "/multi5/a", 0, buf, &blen, &s1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL(1, blen); + CPPUNIT_ASSERT(strcmp("2", buf) == 0); + } + + /** + * Test update conflicts + */ + void testUpdateConflict() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + char buf[sz]; + int blen = sz; + char p1[sz]; + p1[0] = '\0'; + struct Stat s1; + int nops = 3; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi6", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_set_op_init(&ops[1], "/multi6", "X", 1, 0, &s1); + zoo_set_op_init(&ops[2], "/multi6", "Y", 1, 0, &s1); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc); + + //Updating version solves conflict -- order matters + ops[2].set_op.version = 1; + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + memset(buf, 0, sz); + rc = zoo_get(zk, "/multi6", 0, buf, &blen, &s1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL(blen, 1); + CPPUNIT_ASSERT(strncmp(buf, "Y", 1) == 0); + } + + /** + * Test delete-update conflicts + */ + void testDeleteUpdateConflict() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + char buf[sz]; + int blen; + char p1[sz]; + p1[0] = '\0'; + struct Stat stat; + int nops = 3; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi7", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_delete_op_init(&ops[1], "/multi7", 0); + zoo_set_op_init(&ops[2], "/multi7", "Y", 1, 0, &stat); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + + // '/multi' should never have been created as entire op should fail + rc = zoo_exists(zk, "/multi7", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + } + + void testAsyncMulti() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + + int sz = 512; + char p1[sz], p2[sz], p3[sz]; + p1[0] = '\0'; + p2[0] = '\0'; + p3[0] = '\0'; + + int nops = 3; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi8", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_create_op_init(&ops[1], "/multi8/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz); + zoo_create_op_init(&ops[2], "/multi8/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz); + + rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn, 0); + waitForMultiCompletion(10); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + CPPUNIT_ASSERT(strcmp(p1, "/multi8") == 0); + CPPUNIT_ASSERT(strcmp(p2, "/multi8/a") == 0); + CPPUNIT_ASSERT(strcmp(p3, "/multi8/b") == 0); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2].err); + } + + void testMultiFail() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + + int sz = 512; + char p1[sz], p2[sz], p3[sz]; + + p1[0] = '\0'; + p2[0] = '\0'; + p3[0] = '\0'; + + int nops = 3; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_create_op_init(&ops[0], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + zoo_create_op_init(&ops[1], "/multi9", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p2, sz); + zoo_create_op_init(&ops[2], "/multi9/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p3, sz); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, rc); + } + + /** + * Test basic multi-op check functionality + */ + void testCheck() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + char p1[sz]; + p1[0] = '\0'; + struct Stat s1; + + rc = zoo_create(zk, "/multi0", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + // Conditionally create /multi0/a' only if '/multi0' at version 0 + int nops = 2; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_check_op_init(&ops[0], "/multi0", 0); + zoo_create_op_init(&ops[1], "/multi0/a", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1].err); + + // '/multi0/a' should have been created as it passed version check + rc = zoo_exists(zk, "/multi0/a", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + // Only create '/multi0/b' if '/multi0' at version 10 (which it's not) + zoo_op_t ops2[nops]; + zoo_check_op_init(&ops2[0], "/multi0", 10); + zoo_create_op_init(&ops2[1], "/multi0/b", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, p1, sz); + + rc = zoo_multi(zk, nops, ops2, results); + CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, rc); + + CPPUNIT_ASSERT_EQUAL((int)ZBADVERSION, results[0].err); + CPPUNIT_ASSERT_EQUAL((int)ZRUNTIMEINCONSISTENCY, results[1].err); + + // '/multi0/b' should NOT have been created + rc = zoo_exists(zk, "/multi0/b", 0, NULL); + CPPUNIT_ASSERT_EQUAL((int)ZNONODE, rc); + } + + /** + * Do a multi op inside a watch callback context. + */ + static void doMultiInWatch(zhandle_t *zk, int type, int state, const char *path, void *ctx) { + int rc; + int sz = 512; + char p1[sz]; + p1[0] = '\0'; + struct Stat s1; + + int nops = 1; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + zoo_set_op_init(&ops[0], "/multiwatch", "1", 1, -1, NULL); + + rc = zoo_multi(zk, nops, ops, results); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0].err); + + memset(p1, '\0', sz); + rc = zoo_get(zk, "/multiwatch", 0, p1, &sz, &s1); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + CPPUNIT_ASSERT_EQUAL(1, sz); + CPPUNIT_ASSERT(strcmp("1", p1) == 0); + count++; + } + + /** + * Test multi-op called from a watch + */ + void testWatch() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + int sz = 512; + char p1[sz]; + p1[0] = '\0'; + + rc = zoo_create(zk, "/multiwatch", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + + // create a watch on node '/multiwatch' + rc = zoo_wget(zk, "/multiwatch", doMultiInWatch, &ctx, p1, &sz, NULL); + + // setdata on node '/multiwatch' this should trip the watch + rc = zoo_set(zk, "/multiwatch", NULL, -1, -1); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + // wait for multi completion in doMultiInWatch + waitForMultiCompletion(5); + } + + /** + * ZOOKEEPER-1636: If request is too large, the server will cut the + * connection without sending response packet. The client will try to + * process completion on multi request and eventually cause SIGSEGV + */ + void testBigAsyncMulti() { + int rc; + int callback_rc = (int) ZOK; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + + // The request should be more than 1MB which exceeds the default + // jute.maxbuffer and causes the server to drop client connection + const int iteration = 500; + const int type_count = 3; + const int nops = iteration * type_count; + char buff[1024]; + + zoo_op_result_t results[nops]; + zoo_op_t ops[nops]; + struct Stat* s[nops]; + int index = 0; + + // Test that we deliver error to 3 types of sub-request + for (int i = 0; i < iteration; ++i) { + zoo_set_op_init(&ops[index++], "/x", buff, sizeof(buff), -1, s[i]); + zoo_create_op_init(&ops[index++], "/x", buff, sizeof(buff), + &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0); + zoo_delete_op_init(&ops[index++], "/x", -1); + } + + rc = zoo_amulti(zk, nops, ops, results, multi_completion_fn_rc, + + &callback_rc); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + waitForMultiCompletion(10); + // With the bug, we will get SIGSEGV before reaching this point + CPPUNIT_ASSERT_EQUAL((int) ZCONNECTIONLOSS, callback_rc); + + // Make sure that all sub-request completions get processed + for (int i = 0; i < nops; ++i) { + CPPUNIT_ASSERT_EQUAL((int) ZCONNECTIONLOSS, results[i].err); + } + + // The handle should be able to recover itself. + ctx.waitForConnected(zk); + + // Try to submit another async request to see if it get processed + // correctly + rc = zoo_acreate(zk, "/target", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, + create_completion_fn_rc, &callback_rc); + CPPUNIT_ASSERT_EQUAL((int) ZOK, rc); + + waitForMultiCompletion(10); + CPPUNIT_ASSERT_EQUAL((int) ZOK, callback_rc); + } + + /** + * ZOOKEEPER-1624: PendingChanges of create sequential node request didn't + * get rollbacked correctly when multi-op failed. This caused + * create sequential node request in subsequent multi-op to failed because + * sequential node name generation is incorrect. + * + * The check is to make sure that each request in multi-op failed with + * the correct reason. + */ + void testSequentialNodeCreateInAsyncMulti() { + int rc; + watchctx_t ctx; + zhandle_t *zk = createClient(&ctx); + + int iteration = 4; + int nops = 2; + + zoo_op_result_t results[iteration][nops]; + zoo_op_t ops[nops]; + zoo_create_op_init(&ops[0], "/node-", "", 0, &ZOO_OPEN_ACL_UNSAFE, ZOO_SEQUENCE, NULL, 0); + zoo_create_op_init(&ops[1], "/dup", "", 0, &ZOO_OPEN_ACL_UNSAFE, 0, NULL, 0); + for (int i = 0; i < iteration ; ++i) { + rc = zoo_amulti(zk, nops, ops, results[i], multi_completion_fn_no_assert, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + waitForMultiCompletion(10); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[1][0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[2][0].err); + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[3][0].err); + + CPPUNIT_ASSERT_EQUAL((int)ZOK, results[0][1].err); + CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[1][1].err); + CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[2][1].err); + CPPUNIT_ASSERT_EQUAL((int)ZNODEEXISTS, results[3][1].err); + + resetCounter(); + } +}; + +volatile int Zookeeper_multi::count; +const char Zookeeper_multi::hostPorts[] = "127.0.0.1:22181"; +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_multi); +#endif diff --git a/zookeeper-client/zookeeper-client-c/tests/TestOperations.cc b/zookeeper-client/zookeeper-client-c/tests/TestOperations.cc new file mode 100644 index 0000000..ed8e9f4 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestOperations.cc @@ -0,0 +1,976 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "CppAssertHelper.h" + +#include "ZKMocks.h" +#include + +using namespace std; + +class Zookeeper_operations : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_operations); +#ifndef THREADED + CPPUNIT_TEST(testPing); + CPPUNIT_TEST(testUnsolicitedPing); + CPPUNIT_TEST(testTimeoutCausedByWatches1); + CPPUNIT_TEST(testTimeoutCausedByWatches2); + CPPUNIT_TEST(testCloseWhileInProgressFromMain); + CPPUNIT_TEST(testCloseWhileInProgressFromCompletion); + CPPUNIT_TEST(testCloseWhileMultiInProgressFromMain); + CPPUNIT_TEST(testCloseWhileMultiInProgressFromCompletion); +#else + CPPUNIT_TEST(testAsyncWatcher1); + CPPUNIT_TEST(testAsyncGetOperation); +#endif + CPPUNIT_TEST(testOperationsAndDisconnectConcurrently1); + CPPUNIT_TEST(testOperationsAndDisconnectConcurrently2); + CPPUNIT_TEST(testConcurrentOperations1); + CPPUNIT_TEST_SUITE_END(); + zhandle_t *zh; + FILE *logfile; + + static void watcher(zhandle_t *, int, int, const char *,void*){} +public: + Zookeeper_operations() { + logfile = openlogfile("Zookeeper_operations"); + } + + ~Zookeeper_operations() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + + zoo_deterministic_conn_order(0); + zh=0; + } + + void tearDown() + { + zookeeper_close(zh); + } + + class AsyncGetOperationCompletion: public AsyncCompletion{ + public: + AsyncGetOperationCompletion():called_(false),rc_(ZAPIERROR){} + virtual void dataCompl(int rc, const char *value, int len, const Stat *stat){ + synchronized(mx_); + called_=true; + rc_=rc; + value_.erase(); + if(rc!=ZOK) return; + value_.assign(value,len); + if(stat) + stat_=*stat; + } + bool operator()()const{ + synchronized(mx_); + return called_; + } + mutable Mutex mx_; + bool called_; + int rc_; + string value_; + NodeStat stat_; + }; + + class AsyncVoidOperationCompletion: public AsyncCompletion{ + public: + AsyncVoidOperationCompletion():called_(false),rc_(ZAPIERROR){} + virtual void voidCompl(int rc){ + synchronized(mx_); + called_=true; + rc_=rc; + } + bool operator()()const{ + synchronized(mx_); + return called_; + } + mutable Mutex mx_; + bool called_; + int rc_; + }; +#ifndef THREADED + // send two get data requests; verify that the corresponding completions called + void testConcurrentOperations1() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + int fd=0; + int interest=0; + timeval tv; + // first operation + AsyncGetOperationCompletion res1; + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // second operation + AsyncGetOperationCompletion res2; + zkServer.addOperationResponse(new ZooGetResponse("2",1)); + rc=zoo_aget(zh,"/x/y/2",0,asyncCompletion,&res2); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // process the send queue + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + while((rc=zookeeper_process(zh,interest))==ZOK) { + millisleep(100); + //printf("%d\n", rc); + } + //printf("RC = %d", rc); + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + CPPUNIT_ASSERT_EQUAL((int)ZOK,res1.rc_); + CPPUNIT_ASSERT_EQUAL(string("1"),res1.value_); + CPPUNIT_ASSERT_EQUAL((int)ZOK,res2.rc_); + CPPUNIT_ASSERT_EQUAL(string("2"),res2.value_); + } + // send two getData requests and disconnect while the second request is + // outstanding; + // verify the completions are called + void testOperationsAndDisconnectConcurrently1() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + int fd=0; + int interest=0; + timeval tv; + // first operation + AsyncGetOperationCompletion res1; + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // second operation + AsyncGetOperationCompletion res2; + zkServer.addOperationResponse(new ZooGetResponse("2",1)); + rc=zoo_aget(zh,"/x/y/2",0,asyncCompletion,&res2); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // process the send queue + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // simulate a disconnect + zkServer.setConnectionLost(); + rc=zookeeper_interest(zh,&fd,&interest,&tv); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc); + CPPUNIT_ASSERT_EQUAL((int)ZOK,res1.rc_); + CPPUNIT_ASSERT_EQUAL(string("1"),res1.value_); + CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,res2.rc_); + CPPUNIT_ASSERT_EQUAL(string(""),res2.value_); + } + // send two getData requests and simulate timeout while the both request + // are pending; + // verify the completions are called + void testOperationsAndDisconnectConcurrently2() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + int fd=0; + int interest=0; + timeval tv; + // first operation + AsyncGetOperationCompletion res1; + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // second operation + AsyncGetOperationCompletion res2; + zkServer.addOperationResponse(new ZooGetResponse("2",1)); + rc=zoo_aget(zh,"/x/y/2",0,asyncCompletion,&res2); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // simulate timeout + timeMock.tick(+10); // advance system time by 10 secs + // the next call to zookeeper_interest should return ZOPERATIONTIMEOUT + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOPERATIONTIMEOUT,rc); + // make sure the completions have been called + CPPUNIT_ASSERT_EQUAL((int)ZOPERATIONTIMEOUT,res1.rc_); + CPPUNIT_ASSERT_EQUAL((int)ZOPERATIONTIMEOUT,res2.rc_); + } + + class PingCountingServer: public ZookeeperServer{ + public: + PingCountingServer():pingCount_(0){} + // called when a client request is received + virtual void onMessageReceived(const RequestHeader& rh, iarchive* ia){ + if(rh.type==ZOO_PING_OP){ + pingCount_++; + } + } + int pingCount_; + }; + + // establish a connection; idle for a while + // verify ping was sent at least once + void testPing() + { + const int TIMEOUT=9; // timeout in secs + Mock_gettimeofday timeMock; + PingCountingServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + // receive timeout is in milliseconds + zh=zookeeper_init("localhost:1234",watcher,TIMEOUT*1000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + int fd=0; + int interest=0; + timeval tv; + // Round 1. + int rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // simulate waiting for the select() call to timeout; + // advance the system clock accordingly + timeMock.tick(tv); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + // verify no ping sent + CPPUNIT_ASSERT(zkServer.pingCount_==0); + + // Round 2. + // the client should have the idle threshold exceeded, by now + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // assume the socket is writable, so no idling here; move on to + // zookeeper_process immediately + rc=zookeeper_process(zh,interest); + // ZNOTHING means the client hasn't received a ping response yet + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + // verify a ping is sent + CPPUNIT_ASSERT_EQUAL(1,zkServer.pingCount_); + + // Round 3. + // we're going to receive a server PING response and make sure + // that the client has updated its last_recv timestamp + zkServer.addRecvResponse(new PingResponse); + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // pseudo-sleep for a short while (10 ms) + timeMock.millitick(10); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // only one ping so far? + CPPUNIT_ASSERT_EQUAL(1,zkServer.pingCount_); + CPPUNIT_ASSERT(timeMock==zh->last_recv); + + // Round 4 + // make sure that a ping is not sent if something is outstanding + AsyncGetOperationCompletion res1; + rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + timeMock.tick(tv); + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // pseudo-sleep for a short while (10 ms) + timeMock.millitick(10); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + // only one ping so far? + CPPUNIT_ASSERT_EQUAL(1,zkServer.pingCount_); + } + + // ZOOKEEPER-2253: Permit unsolicited pings + void testUnsolicitedPing() + { + const int TIMEOUT=9; // timeout in secs + Mock_gettimeofday timeMock; + PingCountingServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + // receive timeout is in milliseconds + zh=zookeeper_init("localhost:1234",watcher,TIMEOUT*1000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + int fd=0; + int interest=0; + timeval tv; + + int rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // verify no ping sent + CPPUNIT_ASSERT(zkServer.pingCount_==0); + + // we're going to receive a unsolicited PING response; ensure + // that the client has updated its last_recv timestamp + timeMock.tick(tv); + zkServer.addRecvResponse(new PingResponse); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT(timeMock==zh->last_recv); + } + + // simulate a watch arriving right before a ping is due + // assert the ping is sent nevertheless + void testTimeoutCausedByWatches1() + { + const int TIMEOUT=9; // timeout in secs + Mock_gettimeofday timeMock; + PingCountingServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + // receive timeout is in milliseconds + zh=zookeeper_init("localhost:1234",watcher,TIMEOUT*1000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + int fd=0; + int interest=0; + timeval tv; + // Round 1. + int rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // simulate waiting for the select() call to timeout; + // advance the system clock accordingly + timeMock.tick(tv); + timeMock.tick(-1); // set the clock to a millisecond before a ping is due + // trigger a watch now + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/x/y/z")); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // arrival of a watch sets the last_recv to the current time + CPPUNIT_ASSERT(timeMock==zh->last_recv); + // spend 1 millisecond by processing the watch + timeMock.tick(1); + + // Round 2. + // a ping is due; zookeeper_interest() must send it now + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // no delay here -- as if the socket is immediately writable + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + // verify a ping is sent + CPPUNIT_ASSERT_EQUAL(1,zkServer.pingCount_); + } + + // similar to testTimeoutCausedByWatches1, but this time the watch is + // triggered while the client has an outstanding request + // assert the ping is sent on time + void testTimeoutCausedByWatches2() + { + const int TIMEOUT=9; // timeout in secs + Mock_gettimeofday now; + PingCountingServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + // receive timeout is in milliseconds + zh=zookeeper_init("localhost:1234",watcher,TIMEOUT*1000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + // queue up a request; keep it pending (as if the server is busy or has died) + AsyncGetOperationCompletion res1; + zkServer.addOperationResponse(new ZooGetResponse("2",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + + int fd=0; + int interest=0; + timeval tv; + // Round 1. + // send the queued up zoo_aget() request + Mock_gettimeofday beginningOfTimes(now); // remember when we started + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // no delay -- the socket is writable + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // Round 2. + // what's next? + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // no response from the server yet -- waiting in the select() call + now.tick(tv); + // a watch has arrived, thus preventing the connection from timing out + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/x/y/z")); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); // read the watch message + CPPUNIT_ASSERT_EQUAL(0,zkServer.pingCount_); // not yet! + + //Round 3. + // now is the time to send a ping; make sure it's actually sent + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + // verify a ping is sent + CPPUNIT_ASSERT_EQUAL(1,zkServer.pingCount_); + // make sure only 1/3 of the timeout has passed + CPPUNIT_ASSERT_EQUAL((int32_t)TIMEOUT/3*1000,toMilliseconds(now-beginningOfTimes)); + } + + // ZOOKEEPER-2894: Memory and completions leak on zookeeper_close + // while there is a request waiting for being processed + // call zookeeper_close() from the main event loop + // assert the completion callback is called + void testCloseWhileInProgressFromMain() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + forceConnected(zh); + zhandle_t* savezh=zh; + + // issue a request + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + AsyncGetOperationCompletion res1; + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // but do not allow Zookeeper C Client to process the request + // and call zookeeper_close() from the main event loop immediately + Mock_free_noop freeMock; + rc=zookeeper_close(zh); zh=0; + freeMock.disable(); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // verify that memory for completions was freed (would be freed if no mock installed) + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT(savezh->completions_to_process.head==0); + CPPUNIT_ASSERT(savezh->completions_to_process.last==0); + + // verify that completion was called, and it was called with ZCLOSING status + CPPUNIT_ASSERT(res1.called_); + CPPUNIT_ASSERT_EQUAL((int)ZCLOSING,res1.rc_); + } + + // ZOOKEEPER-2894: Memory and completions leak on zookeeper_close + // send some request #1 + // then, while there is a request #2 waiting for being processed + // call zookeeper_close() from the completion callback of request #1 + // assert the completion callback #2 is called + void testCloseWhileInProgressFromCompletion() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + forceConnected(zh); + zhandle_t* savezh=zh; + + // will handle completion on request #1 and issue request #2 from it + class AsyncGetOperationCompletion1: public AsyncCompletion{ + public: + AsyncGetOperationCompletion1(zhandle_t **zh, ZookeeperServer *zkServer, + AsyncGetOperationCompletion *res2) + :zh_(zh),zkServer_(zkServer),res2_(res2){} + + virtual void dataCompl(int rc1, const char *value, int len, const Stat *stat){ + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc1); + + // from the completion #1 handler, issue request #2 + zkServer_->addOperationResponse(new ZooGetResponse("2",1)); + int rc2=zoo_aget(*zh_,"/x/y/2",0,asyncCompletion,res2_); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc2); + + // but do not allow Zookeeper C Client to process the request #2 + // and call zookeeper_close() from the completion callback of request #1 + rc2=zookeeper_close(*zh_); *zh_=0; + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc2); + + // do not disable freeMock here, let completion #2 handler + // return through ZooKeeper C Client internals to the main loop + // and fulfill the work + } + + zhandle_t **zh_; + ZookeeperServer *zkServer_; + AsyncGetOperationCompletion *res2_; + }; + + // issue request #1 + AsyncGetOperationCompletion res2; + AsyncGetOperationCompletion1 res1(&zh,&zkServer,&res2); + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // process the send queue + int fd; int interest; timeval tv; + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT(zh!=0); + Mock_free_noop freeMock; + while(zh!=0 && (rc=zookeeper_process(zh,interest))==ZOK) { + millisleep(100); + } + freeMock.disable(); + CPPUNIT_ASSERT(zh==0); + + // verify that memory for completions was freed (would be freed if no mock installed) + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT(savezh->completions_to_process.head==0); + CPPUNIT_ASSERT(savezh->completions_to_process.last==0); + + // verify that completion #2 was called, and it was called with ZCLOSING status + CPPUNIT_ASSERT(res2.called_); + CPPUNIT_ASSERT_EQUAL((int)ZCLOSING,res2.rc_); + } + + // ZOOKEEPER-2891: Invalid processing of zookeeper_close for mutli-request + // while there is a multi request waiting for being processed + // call zookeeper_close() from the main event loop + // assert the completion callback is called with status ZCLOSING + void testCloseWhileMultiInProgressFromMain() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + forceConnected(zh); + zhandle_t* savezh=zh; + + // issue a multi request + int nops=2; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + zoo_create_op_init(&ops[0],"/a",0,-1,&ZOO_OPEN_ACL_UNSAFE,0,0,0); + zoo_create_op_init(&ops[1],"/a/b",0,-1,&ZOO_OPEN_ACL_UNSAFE,0,0,0); + // TODO: Provide ZooMultiResponse. However, it's not required in this test. + // zkServer.addOperationResponse(new ZooMultiResponse(...)); + AsyncVoidOperationCompletion res1; + int rc=zoo_amulti(zh,nops,ops,results,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // but do not allow Zookeeper C Client to process the request + // and call zookeeper_close() from the main event loop immediately + Mock_free_noop freeMock; + rc=zookeeper_close(zh); zh=0; + freeMock.disable(); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // verify that memory for completions was freed (would be freed if no mock installed) + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT(savezh->completions_to_process.head==0); + CPPUNIT_ASSERT(savezh->completions_to_process.last==0); + + // verify that completion was called, and it was called with ZCLOSING status + CPPUNIT_ASSERT(res1.called_); + CPPUNIT_ASSERT_EQUAL((int)ZCLOSING,res1.rc_); + } + + // ZOOKEEPER-2891: Invalid processing of zookeeper_close for mutli-request + // send some request #1 (not a multi request) + // then, while there is a multi request #2 waiting for being processed + // call zookeeper_close() from the completion callback of request #1 + // assert the completion callback #2 is called with status ZCLOSING + void testCloseWhileMultiInProgressFromCompletion() + { + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + forceConnected(zh); + zhandle_t* savezh=zh; + + // these shall persist during the test + int nops=2; + zoo_op_t ops[nops]; + zoo_op_result_t results[nops]; + + // will handle completion on request #1 and issue request #2 from it + class AsyncGetOperationCompletion1: public AsyncCompletion{ + public: + AsyncGetOperationCompletion1(zhandle_t **zh, ZookeeperServer *zkServer, + AsyncVoidOperationCompletion *res2, + int nops, zoo_op_t* ops, zoo_op_result_t* results) + :zh_(zh),zkServer_(zkServer),res2_(res2),nops_(nops),ops_(ops),results_(results){} + + virtual void dataCompl(int rc1, const char *value, int len, const Stat *stat){ + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc1); + + // from the completion #1 handler, issue multi request #2 + assert(nops_>=2); + zoo_create_op_init(&ops_[0],"/a",0,-1,&ZOO_OPEN_ACL_UNSAFE,0,0,0); + zoo_create_op_init(&ops_[1],"/a/b",0,-1,&ZOO_OPEN_ACL_UNSAFE,0,0,0); + // TODO: Provide ZooMultiResponse. However, it's not required in this test. + // zkServer_->addOperationResponse(new ZooMultiResponse(...)); + int rc2=zoo_amulti(*zh_,nops_,ops_,results_,asyncCompletion,res2_); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc2); + + // but do not allow Zookeeper C Client to process the request #2 + // and call zookeeper_close() from the completion callback of request #1 + rc2=zookeeper_close(*zh_); *zh_=0; + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc2); + + // do not disable freeMock here, let completion #2 handler + // return through ZooKeeper C Client internals to the main loop + // and fulfill the work + } + + zhandle_t **zh_; + ZookeeperServer *zkServer_; + AsyncVoidOperationCompletion *res2_; + int nops_; + zoo_op_t* ops_; + zoo_op_result_t* results_; + }; + + // issue some request #1 (not a multi request) + AsyncVoidOperationCompletion res2; + AsyncGetOperationCompletion1 res1(&zh,&zkServer,&res2,nops,ops,results); + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&res1); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // process the send queue + int fd; int interest; timeval tv; + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT(zh!=0); + Mock_free_noop freeMock; + while(zh!=0 && (rc=zookeeper_process(zh,interest))==ZOK) { + millisleep(100); + } + freeMock.disable(); + CPPUNIT_ASSERT(zh==0); + + // verify that memory for completions was freed (would be freed if no mock installed) + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT(savezh->completions_to_process.head==0); + CPPUNIT_ASSERT(savezh->completions_to_process.last==0); + + // verify that completion #2 was called, and it was called with ZCLOSING status + CPPUNIT_ASSERT(res2.called_); + CPPUNIT_ASSERT_EQUAL((int)ZCLOSING,res2.rc_); + } + +#else + class TestGetDataJob: public TestJob{ + public: + TestGetDataJob(ZookeeperServer* svr,zhandle_t* zh, int reps=500) + :svr_(svr),zh_(zh),rc_(ZAPIERROR),reps_(reps){} + virtual void run(){ + int i; + for(i=0;iaddOperationResponse(new ZooGetResponse("1",1)); + rc_=zoo_get(zh_,"/x/y/z",0,&buf,&size,0); + if(rc_!=ZOK){ + break; + } + } + } + ZookeeperServer* svr_; + zhandle_t* zh_; + int rc_; + int reps_; + }; + class TestConcurrentOpJob: public TestGetDataJob{ + public: + static const int REPS=500; + TestConcurrentOpJob(ZookeeperServer* svr,zhandle_t* zh): + TestGetDataJob(svr,zh,REPS){} + virtual TestJob* clone() const { + return new TestConcurrentOpJob(svr_,zh_); + } + virtual void validate(const char* file, int line) const{ + CPPUNIT_ASSERT_EQUAL_MESSAGE_LOC("ZOK != rc",(int)ZOK,rc_,file,line); + } + }; + void testConcurrentOperations1() + { + for(int counter=0; counter<50; counter++){ + // frozen time -- no timeouts and no pings + Mock_gettimeofday timeMock; + + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + + TestJobManager jmgr(TestConcurrentOpJob(&zkServer,zh),10); + jmgr.startAllJobs(); + jmgr.wait(); + // validate test results + VALIDATE_JOBS(jmgr); + } + } + class ZKGetJob: public TestJob{ + public: + static const int REPS=1000; + ZKGetJob(zhandle_t* zh) + :zh_(zh),rc_(ZAPIERROR){} + virtual TestJob* clone() const { + return new ZKGetJob(zh_); + } + virtual void run(){ + int i; + for(i=0;i +#include "CppAssertHelper.h" + +#include +#include + +#include + +#include "Util.h" +#include "WatchUtil.h" + +#ifdef THREADED +class Zookeeper_readOnly : public CPPUNIT_NS::TestFixture { + CPPUNIT_TEST_SUITE(Zookeeper_readOnly); + CPPUNIT_TEST(testReadOnly); +#ifdef HAVE_OPENSSL_H + CPPUNIT_TEST(testReadOnlyWithSSL); +#endif + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t* zh, int type, int state, + const char* path, void* v) { + watchctx_t *ctx = (watchctx_t*)v; + + if (state==ZOO_CONNECTED_STATE || state==ZOO_READONLY_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + + FILE *logfile; +public: + + Zookeeper_readOnly() { + logfile = openlogfile("Zookeeper_readOnly"); + } + + ~Zookeeper_readOnly() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() { + zoo_set_log_stream(logfile); + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + } + + void startReadOnly() { + char cmd[1024]; + sprintf(cmd, "%s startCleanReadOnly", ZKSERVER_CMD); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void stopPeer() { + char cmd[1024]; + sprintf(cmd, "%s stop", ZKSERVER_CMD); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + zhandle_t* connectReadOnly(const char *address, watchctx_t *watch) { + zhandle_t* zh = zookeeper_init(address, watcher, 10000, NULL, watch, ZOO_READONLY); + watch->zh = zh; + CPPUNIT_ASSERT(zh != 0); + sleep(1); + return zh; + } + + void assertCanRead(zhandle_t* zh, const char *znode_path) { + int len = 1024; + char buf[len]; + int res = zoo_get(zh, znode_path, 0, buf, &len, 0); + CPPUNIT_ASSERT_EQUAL((int)ZOK, res); + } + + void assertCanNotWrite(zhandle_t* zh, const char *znode_path) { + char path[1024]; + char buf[1024]; + int res = zoo_create(zh, znode_path, buf, 10, &ZOO_OPEN_ACL_UNSAFE, 0, path, 512); + CPPUNIT_ASSERT_EQUAL((int)ZNOTREADONLY, res); + } + + void testReadOnly() + { + startReadOnly(); + + watchctx_t watch; + zhandle_t* zh = connectReadOnly("localhost:22181", &watch); + + assertCanRead(zh, "/"); + + assertCanNotWrite(zh, "/test"); + + stopPeer(); + } + +#ifdef HAVE_OPENSSL_H + + zhandle_t* connectReadOnlySSL(const char *address, const char *certs, watchctx_t *watch) { + zhandle_t* zh = zookeeper_init_ssl(address, certs, watcher, 10000, NULL, watch, ZOO_READONLY); + watch->zh = zh; + CPPUNIT_ASSERT(zh != 0); + sleep(1); + return zh; + } + + void testReadOnlyWithSSL() { + startReadOnly(); + + watchctx_t watch; + zhandle_t* zh = connectReadOnlySSL("localhost:22281", + "/tmp/certs/server.crt,/tmp/certs/client.crt,/tmp/certs/clientkey.pem,password", + &watch); + + assertCanRead(zh, "/"); + + assertCanNotWrite(zh, "/testSSL"); + + stopPeer(); + } +#endif + +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_readOnly); +#endif diff --git a/zookeeper-client/zookeeper-client-c/tests/TestReconfig.cc b/zookeeper-client/zookeeper-client-c/tests/TestReconfig.cc new file mode 100644 index 0000000..22f87e9 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestReconfig.cc @@ -0,0 +1,697 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern "C" { +#include +} + +#include "Util.h" +#include "LibCMocks.h" +#include "ZKMocks.h" + +using namespace std; + +static const int portOffset = 2000; + +class Client +{ + +private: + // Member variables + zhandle_t *zh; + unsigned int seed; + +public: + /** + * Create a client with given connection host string and add to our internal + * vector of clients. These are disconnected and cleaned up in tearDown(). + */ + Client(const string hosts, unsigned int seed) : + seed((seed * seed) + 0xAFAFAFAF) + { + reSeed(); + + zh = zookeeper_init(hosts.c_str(),0,1000,0,0,0); + CPPUNIT_ASSERT(zh); + + // Set the flag to disable ZK from reconnecting to a different server. + // Our reconfig test case will do explicit server shuffling through + // zoo_cycle_next_server, and the reconnection attempts would interfere + // with the server states the tests cases assume. + zh->disable_reconnection_attempt = 1; + reSeed(); + + cycleNextServer(); + } + + void close() + { + zookeeper_close(zh); + zh = NULL; + } + + bool isReconfig() + { + return zh->reconfig != 0; + } + + /** + * re-seed this client with it's own previously generated seed so its + * random choices are unique and separate from the other clients + */ + void reSeed() + { + srandom(seed); + srand48(seed); + } + + /** + * Get the server that this client is currently connected to. + */ + string getServer() + { + const char* addrstring = zoo_get_current_server(zh); + return string(addrstring); + } + + /** + * Get the server this client is currently connected to with no port + * specification. + */ + string getServerNoPort() + { + string addrstring = getServer(); + size_t found = addrstring.find_last_of(":"); + CPPUNIT_ASSERT(found != string::npos); + + // ipv6 address case (to remove leading and trailing bracket) + if (addrstring.find("[") != string::npos) + { + return addrstring.substr(1, found-2); + } + else + { + return addrstring.substr(0, found); + } + } + + /** + * Get the port of the server this client is currently connected to. + */ + uint32_t getServerPort() + { + string addrstring = getServer(); + + size_t found = addrstring.find_last_of(":"); + CPPUNIT_ASSERT(found != string::npos); + + string portStr = addrstring.substr(found+1); + + stringstream ss(portStr); + uint32_t port; + ss >> port; + + CPPUNIT_ASSERT(port >= portOffset); + + return port; + } + + /** + * Cycle to the next available server on the next connect attempt. It also + * calls into getServer (above) to return the server connected to. + */ + string cycleNextServer() + { + zoo_cycle_next_server(zh); + return getServer(); + } + + void cycleUntilServer(const string requested) + { + // Call cycleNextServer until the one it's connected to is the one + // specified (disregarding port). + string first; + + while(true) + { + string next = cycleNextServer(); + if (first.empty()) + { + first = next; + } + // Else we've looped around! + else if (first == next) + { + CPPUNIT_ASSERT(false); + } + + // Strip port off + string server = getServerNoPort(); + + // If it matches the requested host we're now 'connected' to the right host + if (server == requested) + { + break; + } + } + } + + /** + * Set servers for this client. + */ + void setServers(const string new_hosts) + { + int rc = zoo_set_servers(zh, new_hosts.c_str()); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + } + + /** + * Set servers for this client and validate reconfig value matches expected. + */ + void setServersAndVerifyReconfig(const string new_hosts, bool is_reconfig) + { + setServers(new_hosts); + CPPUNIT_ASSERT_EQUAL(is_reconfig, isReconfig()); + } + + /** + * Sets the server list this client is connecting to AND if this requires + * the client to be reconfigured (as dictated by internal client policy) + * then it will trigger a call to cycleNextServer. + */ + void setServersAndCycleIfNeeded(const string new_hosts) + { + setServers(new_hosts); + if (isReconfig()) + { + cycleNextServer(); + } + } +}; + +class Zookeeper_reconfig : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_reconfig); + + // Test cases + CPPUNIT_TEST(testcycleNextServer); + CPPUNIT_TEST(testMigrateOrNot); + CPPUNIT_TEST(testMigrationCycle); + CPPUNIT_TEST(testAddrVecContainsIPv4); +#ifdef AF_INET6 + CPPUNIT_TEST(testAddrVecContainsIPv6); +#endif + + // In threaded mode each 'create' is a thread -- it's not practical to create + // 10,000 threads to test load balancing. The load balancing code can easily + // be tested in single threaded mode as concurrency doesn't affect the algorithm. +#ifndef THREADED + CPPUNIT_TEST(testMigrateProbability); + CPPUNIT_TEST(testLoadBalancing); +#endif + + CPPUNIT_TEST_SUITE_END(); + + FILE *logfile; + + double slackPercent; + static const int numClients = 10000; + static const int portOffset = 2000; + + vector clients; + vector numClientsPerHost; + +public: + Zookeeper_reconfig() : + slackPercent(10.0) + { + logfile = openlogfile("Zookeeper_reconfig"); + } + + ~Zookeeper_reconfig() + { + if (logfile) + { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + zoo_deterministic_conn_order(1); + + numClientsPerHost.resize(numClients); + } + + void tearDown() + { + for (unsigned int i = 0; i < clients.size(); i++) + { + clients.at(i).close(); + } + } + + /** + * Create a client with given connection host string and add to our internal + * vector of clients. These are disconnected and cleaned up in tearDown(). + */ + Client& createClient(const string hosts) + { + Client client(hosts, clients.size()); + clients.push_back(client); + + return clients.back(); + } + + /** + * Same as createClient(hosts) only it takes a specific host that this client + * should simulate being connected to. + */ + Client& createClient(const string hosts, const string host) + { + // Ensure requested host is in the list + size_t found = hosts.find(host); + CPPUNIT_ASSERT(found != hosts.npos); + + Client client(hosts, clients.size()); + client.cycleUntilServer(host); + clients.push_back(client); + + return clients.back(); + } + + /** + * Create a connection host list starting at 'start' and stopping at 'stop' + * where start >= stop. This creates a connection string with host:port pairs + * separated by commas. The given 'octet' is the starting octet that is used + * as the last octet in the host's IP. This is decremented on each iteration. + * Each port will be portOffset + octet. + */ + string createHostList(uint32_t start, uint32_t stop = 1, uint32_t octet = 0) + { + if (octet == 0) + { + octet = start; + } + + stringstream ss; + + for (uint32_t i = start; i >= stop; i--, octet--) + { + ss << "10.10.10." << octet << ":" << portOffset + octet; + + if (i > stop) + { + ss << ", "; + } + } + + return ss.str(); + } + + /** + * Gets the lower bound of the number of clients per server that we expect + * based on the probabilistic load balancing algorithm implemented by the + * client code. + */ + double lowerboundClientsPerServer(int numClients, int numServers) + { + return (1 - slackPercent/100.0) * numClients / numServers; + } + + /** + * Gets the upper bound of the number of clients per server that we expect + * based on the probabilistic load balancing algorithm implemented by the + * client code. + */ + double upperboundClientsPerServer(int numClients, int numServers) + { + return (1 + slackPercent/100.0) * numClients / numServers; + } + + /** + * Update all the clients to use a new list of servers. This will also cause + * the client to cycle to the next server as needed (e.g. due to a reconfig). + * It then updates the number of clients connected to the server based on + * this change. + * + * Afterwards it validates that all of the servers have the correct amount of + * clients based on the probabilistic load balancing algorithm. + */ + void updateAllClientsAndServers(int start, int stop = 1) + { + string newServers = createHostList(start, stop); + int numServers = start - stop + 1; + + for (int i = 0; i < numClients; i++) { + + Client &client = clients.at(i); + client.reSeed(); + + client.setServersAndCycleIfNeeded(newServers); + numClientsPerHost.at(client.getServerPort() - portOffset - 1)++; + } + + int offset = stop - 1; + for (int index = offset; index < numServers; index++) { + + if (numClientsPerHost.at(index) > upperboundClientsPerServer(numClients, numServers)) + { + cout << "INDEX=" << index << " too many -- actual=" << numClientsPerHost.at(index) + << " expected=" << upperboundClientsPerServer(numClients, numServers) << endl; + } + + + CPPUNIT_ASSERT(numClientsPerHost.at(index) <= upperboundClientsPerServer(numClients, numServers)); + + if (numClientsPerHost.at(index) < lowerboundClientsPerServer(numClients, numServers)) + { + cout << "INDEX=" << index << " too few -- actual=" << numClientsPerHost.at(index) + << " expected=" << lowerboundClientsPerServer(numClients, numServers) << endl; + } + + CPPUNIT_ASSERT(numClientsPerHost.at(index) >= lowerboundClientsPerServer(numClients, numServers)); + numClientsPerHost.at(index) = 0; // prepare for next test + } + } + + /*-------------------------------------------------------------------------* + * TESTCASES + *------------------------------------------------------------------------*/ + + /** + * Very basic sunny day test to ensure basic functionality of zoo_set_servers + * and zoo_cycle_next_server. + */ + void testcycleNextServer() + { + const string initial_hosts = createHostList(10); // 2010..2001 + const string new_hosts = createHostList(4); // 2004..2001 + + Client &client = createClient(initial_hosts); + + client.setServersAndVerifyReconfig(new_hosts, true); + + for (int i = 0; i < 10; i++) + { + string next = client.cycleNextServer(); + } + } + + /** + * Test the migration policy implicit within the probabilistic load balancing + * algorithm the Client implements. Tests all the corner cases whereby the + * list of servers is decreased, increased, and stays the same. Also combines + * various combinations of the currently connected server being in the new + * configuration and not. + */ + void testMigrateOrNot() + { + const string initial_hosts = createHostList(4); // 2004..2001 + + Client &client = createClient(initial_hosts, "10.10.10.3"); + + // Ensemble size decreasing, my server is in the new list + client.setServersAndVerifyReconfig(createHostList(3), false); + + // Ensemble size decreasing, my server is NOT in the new list + client.setServersAndVerifyReconfig(createHostList(2), true); + + // Ensemble size stayed the same, my server is NOT in the new list + client.setServersAndVerifyReconfig(createHostList(2), true); + + // Ensemble size increased, my server is not in the new ensemble + client.setServers(createHostList(4)); + client.cycleUntilServer("10.10.10.1"); + client.setServersAndVerifyReconfig(createHostList(7,2), true); + } + + /** + * This tests that as a client is in reconfig mode it will properly try to + * connect to all the new servers first. Then it will try to connect to all + * the 'old' servers that are staying in the new configuration. Finally it + * will fallback to the normal behavior of trying servers in round-robin. + */ + void testMigrationCycle() + { + int num_initial = 4; + const string initial_hosts = createHostList(num_initial); // {2004..2001} + + int num_new = 10; + string new_hosts = createHostList(12, 3); // {2012..2003} + + // servers from the old list that appear in the new list {2004..2003} + int num_staying = 2; + string oldStaying = createHostList(4, 3); + + // servers in the new list that are not in the old list {2012..2005} + int num_coming = 8; + string newComing = createHostList(12, 5); + + // Ensemble in increasing in size, my server is not in the new ensemble + // load on the old servers must be decreased, so must connect to one of + // new servers (pNew = 1) + Client &client = createClient(initial_hosts, "10.10.10.1"); + client.setServersAndVerifyReconfig(new_hosts, true); + + // Since we're in reconfig mode, next connect should be from new list + // We should try all the new servers *BEFORE* trying any old servers + string seen; + for (int i = 0; i < num_coming; i++) { + client.cycleNextServer(); + + // Assert next server is in the 'new' list + stringstream next; + next << client.getServerNoPort() << ":" << client.getServerPort(); + size_t found = newComing.find(next.str()); + CPPUNIT_ASSERT_MESSAGE(next.str() + " not in newComing list", + found != string::npos); + + // Assert not in seen list then append + found = seen.find(next.str()); + CPPUNIT_ASSERT_MESSAGE(next.str() + " in seen list", + found == string::npos); + seen += found + ", "; + } + + // Now it should start connecting to the old servers + seen.clear(); + for (int i = 0; i < num_staying; i++) { + client.cycleNextServer(); + + // Assert it's in the old list + stringstream next; + next << client.getServerNoPort() << ":" << client.getServerPort(); + size_t found = oldStaying.find(next.str()); + CPPUNIT_ASSERT(found != string::npos); + + // Assert not in seen list then append + found = seen.find(next.str()); + CPPUNIT_ASSERT(found == string::npos); + seen += found + ", "; + } + + // NOW it goes back to normal as we've tried all the new and old + string first = client.cycleNextServer(); + for (int i = 0; i < num_new - 1; i++) { + client.cycleNextServer(); + } + + CPPUNIT_ASSERT_EQUAL(first, client.cycleNextServer()); + } + + /** + * Test the migration probability to ensure that it conforms to our expected + * lower and upper bounds of the number of clients per server as we are + * reconfigured. + * + * In this case, the list of servers is increased and the client's server is + * in the new list. Whether to move or not depends on the difference of + * server sizes with probability 1 - |old|/|new| the client disconnects. + * + * In the test below 1-9/10 = 1/10 chance of disconnecting + */ + void testMigrateProbability() + { + const string initial_hosts = createHostList(9); // 10.10.10.9:2009...10.10.10.1:2001 + string new_hosts = createHostList(10); // 10.10.10.10:2010...10.10.10.1:2001 + + uint32_t numDisconnects = 0; + for (int i = 0; i < numClients; i++) { + Client &client = createClient(initial_hosts, "10.10.10.3"); + client.setServers(new_hosts); + if (client.isReconfig()) + { + numDisconnects++; + } + } + + // should be numClients/10 in expectation, we test that it's numClients/10 +- slackPercent + CPPUNIT_ASSERT(numDisconnects < upperboundClientsPerServer(numClients, 10)); + } + + /** + * Tests the probabilistic load balancing algorithm implemented by the Client + * code. + * + * Test strategy: + * + * (1) Start with 9 servers and 10,000 clients. Remove a server, update + * everything, and ensure that the clients are redistributed properly. + * + * (2) Remove two more nodes and repeat the same validations of proper client + * redistribution. Ensure no clients are connected to the two removed + * nodes. + * + * (3) Remove the first server in the list and simultaneously add the three + * previously removed servers. Ensure everything is redistributed and + * no clients are connected to the one missing node. + * + * (4) Add the one missing server back into the mix and validate. + */ + void testLoadBalancing() + { + zoo_deterministic_conn_order(0); + + uint32_t numServers = 9; + const string initial_hosts = createHostList(numServers); // 10.10.10.9:2009...10.10.10.1:2001 + + // Create connections to servers + for (int i = 0; i < numClients; i++) { + Client &client = createClient(initial_hosts); + numClientsPerHost.at(client.getServerPort() - portOffset - 1)++; + } + + for (uint32_t i = 0; i < numServers; i++) { + CPPUNIT_ASSERT(numClientsPerHost.at(i) <= upperboundClientsPerServer(numClients, numServers)); + CPPUNIT_ASSERT(numClientsPerHost.at(i) >= lowerboundClientsPerServer(numClients, numServers)); + numClientsPerHost.at(i) = 0; // prepare for next test + } + + // remove last server + numServers = 8; + updateAllClientsAndServers(numServers); + CPPUNIT_ASSERT_EQUAL((uint32_t)0, numClientsPerHost.at(numServers)); + + // Remove two more nodes + numServers = 6; + updateAllClientsAndServers(numServers); + CPPUNIT_ASSERT_EQUAL((uint32_t)0, numClientsPerHost.at(numServers)); + CPPUNIT_ASSERT_EQUAL((uint32_t)0, numClientsPerHost.at(numServers+1)); + CPPUNIT_ASSERT_EQUAL((uint32_t)0, numClientsPerHost.at(numServers+2)); + + // remove host 0 (first one in list) and add back 6, 7, and 8 + numServers = 8; + updateAllClientsAndServers(numServers, 1); + CPPUNIT_ASSERT_EQUAL((uint32_t)0, numClientsPerHost.at(0)); + + // add back host number 0 + numServers = 9; + updateAllClientsAndServers(numServers); + } + + /** + * This tests that client can detect server's ipv4 address change. + * + * (1) We generate some address and put in addr, which saddr point to + * (2) Add all addresses that differ by one bit from the source + * (3) Add same address, but set ipv6 protocol + * (4) Ensure, that our address is not equal to any of generated, + * and that it equals to itself + */ + void testAddrVecContainsIPv4() { + addrvec_t vec; + addrvec_init(&vec); + + sockaddr_storage addr; + sockaddr_in* saddr = (sockaddr_in*)&addr; + saddr->sin_family = AF_INET; + saddr->sin_port = htons((u_short)1234); + saddr->sin_addr.s_addr = INADDR_ANY; + + CPPUNIT_ASSERT(sizeof(saddr->sin_addr.s_addr) == 4); + + for (int i = 0; i < 32; i++) { + saddr->sin_addr.s_addr ^= (1 << i); + addrvec_append(&vec, &addr); + saddr->sin_addr.s_addr ^= (1 << i); + } + + saddr->sin_family = AF_INET6; + addrvec_append(&vec, &addr); + saddr->sin_family = AF_INET; + + CPPUNIT_ASSERT(!addrvec_contains(&vec, &addr)); + addrvec_append(&vec, &addr); + CPPUNIT_ASSERT(addrvec_contains(&vec, &addr)); + addrvec_free(&vec); + } + + /** + * This tests that client can detect server's ipv6 address change. + * + * Same logic as in previous testAddrVecContainsIPv4 method, + * but we keep in mind, that ipv6 is 128-bit long. + */ +#ifdef AF_INET6 + void testAddrVecContainsIPv6() { + addrvec_t vec; + addrvec_init(&vec); + + sockaddr_storage addr; + sockaddr_in6* saddr = (sockaddr_in6*)&addr; + saddr->sin6_family = AF_INET6; + saddr->sin6_port = htons((u_short)1234); + saddr->sin6_addr = in6addr_any; + + CPPUNIT_ASSERT(sizeof(saddr->sin6_addr.s6_addr) == 16); + + for (int i = 0; i < 16; i++) { + for (int j = 0; j < 8; j++) { + saddr->sin6_addr.s6_addr[i] ^= (1 << j); + addrvec_append(&vec, &addr); + saddr->sin6_addr.s6_addr[i] ^= (1 << j); + } + } + + saddr->sin6_family = AF_INET; + addrvec_append(&vec, &addr); + saddr->sin6_family = AF_INET6; + + CPPUNIT_ASSERT(!addrvec_contains(&vec, &addr)); + addrvec_append(&vec, &addr); + CPPUNIT_ASSERT(addrvec_contains(&vec, &addr)); + addrvec_free(&vec); + } +#endif +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_reconfig); diff --git a/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc b/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc new file mode 100644 index 0000000..c15774e --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestReconfigServer.cc @@ -0,0 +1,420 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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. + */ +#include +#include +#include +#include +#include +#include +#include "zookeeper.h" + +#include "Util.h" +#include "ZooKeeperQuorumServer.h" + +#ifdef THREADED +class TestReconfigServer : public CPPUNIT_NS::TestFixture { + CPPUNIT_TEST_SUITE(TestReconfigServer); + CPPUNIT_TEST(testNonIncremental); + CPPUNIT_TEST(testRemoveConnectedFollower); + CPPUNIT_TEST(testRemoveFollower); + CPPUNIT_TEST(testReconfigFailureWithoutAuth); + CPPUNIT_TEST(testReconfigFailureWithoutServerSuperuserPasswordConfigured); + CPPUNIT_TEST_SUITE_END(); + + public: + TestReconfigServer(); + virtual ~TestReconfigServer(); + void setUp(); + void tearDown(); + void testNonIncremental(); + void testRemoveConnectedFollower(); + void testRemoveFollower(); + void testReconfigFailureWithoutAuth(); + void testReconfigFailureWithoutServerSuperuserPasswordConfigured(); + private: + static const uint32_t NUM_SERVERS; + FILE* logfile_; + std::vector cluster_; + int32_t getLeader(); + std::vector getFollowers(); + void parseConfig(char* buf, int len, std::vector& servers, + std::string& version); + bool waitForConnected(zhandle_t* zh, uint32_t timeout_sec); + zhandle_t* connectFollowers(std::vector &followers); +}; + +const uint32_t TestReconfigServer::NUM_SERVERS = 3; + +TestReconfigServer:: +TestReconfigServer() : + logfile_(openlogfile("TestReconfigServer")) { + zoo_set_log_stream(logfile_); +} + +TestReconfigServer:: +~TestReconfigServer() { + if (logfile_) { + fflush(logfile_); + fclose(logfile_); + logfile_ = NULL; + } +} + +void TestReconfigServer:: +setUp() { + ZooKeeperQuorumServer::tConfigPairs configs; + configs.push_back(std::make_pair("reconfigEnabled", "true")); + cluster_ = ZooKeeperQuorumServer::getCluster(NUM_SERVERS, configs, + "SERVER_JVMFLAGS=-Dzookeeper.DigestAuthenticationProvider.superDigest=super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is test */); +} + +void TestReconfigServer:: +tearDown() { + for (int i = 0; i < cluster_.size(); i++) { + delete cluster_[i]; + } + cluster_.clear(); +} + +int32_t TestReconfigServer:: +getLeader() { + for (int32_t i = 0; i < cluster_.size(); i++) { + if (cluster_[i]->isLeader()) { + return i; + } + } + return -1; +} + +std::vector TestReconfigServer:: +getFollowers() { + std::vector followers; + for (int32_t i = 0; i < cluster_.size(); i++) { + if (cluster_[i]->isFollower()) { + followers.push_back(i); + } + } + return followers; +} + +void TestReconfigServer:: +parseConfig(char* buf, int len, std::vector& servers, + std::string& version) { + std::string config(buf, len); + std::stringstream ss(config); + std::string line; + std::string serverPrefix("server."); + std::string versionPrefix("version="); + servers.clear(); + while(std::getline(ss, line, '\n')) { + if (line.compare(0, serverPrefix.size(), serverPrefix) == 0) { + servers.push_back(line); + } else if (line.compare(0, versionPrefix.size(), versionPrefix) == 0) { + version = line.substr(versionPrefix.size()); + } + } +} + +bool TestReconfigServer:: +waitForConnected(zhandle_t* zh, uint32_t timeout_sec) { + for (uint32_t i = 0; i < timeout_sec; i++) { + if (zoo_state(zh) == ZOO_CONNECTED_STATE) { + return true; + } + sleep(1); + } + return false; +} + +/** + * 1. Connect to the leader. + * 2. Remove a follower using incremental reconfig. + * 3. Add the follower back using incremental reconfig. + */ +void TestReconfigServer:: +testRemoveFollower() { + std::vector servers; + std::string version; + struct Stat stat; + int len = 1024; + char buf[len]; + + // get config from leader. + int32_t leader = getLeader(); + CPPUNIT_ASSERT(leader >= 0); + std::string host = cluster_[leader]->getHostPort(); + zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0); + CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK)); + // check if all the servers are listed in the config. + parseConfig(buf, len, servers, version); + // initially should be 1<<32, which is 0x100000000. This is the zxid + // of the first NEWLEADER message, used as the initial version + CPPUNIT_ASSERT_EQUAL(std::string("100000000"), version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + + // remove a follower. + std::vector followers = getFollowers(); + len = 1024; + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, + (uint32_t)(followers.size())); + std::stringstream ss; + ss << followers[0]; + int rc = zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, + &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + parseConfig(buf, len, servers, version); + CPPUNIT_ASSERT_EQUAL(std::string("100000002"), version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + if (i == followers[0]) { + continue; + } + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + + // add the follower back. + len = 1024; + std::string serverString = cluster_[followers[0]]->getServerString(); + rc = zoo_reconfig(zk, serverString.c_str(), NULL, NULL, -1, buf, &len, + &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + parseConfig(buf, len, servers, version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + zookeeper_close(zk); +} + +/** + * 1. Connect to the leader. + * 2. Remove a follower using non-incremental reconfig. + * 3. Add the follower back using non-incremental reconfig. + */ +void TestReconfigServer:: +testNonIncremental() { + std::vector servers; + std::string version; + struct Stat stat; + int len = 1024; + char buf[len]; + + // get config from leader. + int32_t leader = getLeader(); + CPPUNIT_ASSERT(leader >= 0); + std::string host = cluster_[leader]->getHostPort(); + zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0); + CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK)); + + // check if all the servers are listed in the config. + parseConfig(buf, len, servers, version); + // initially should be 1<<32, which is 0x100000000. This is the zxid + // of the first NEWLEADER message, used as the initial version + CPPUNIT_ASSERT_EQUAL(std::string("100000000"), version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + + // remove a follower. + std::vector followers = getFollowers(); + len = 1024; + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, + (uint32_t)(followers.size())); + std::stringstream ss; + for (int i = 1; i < followers.size(); i++) { + ss << cluster_[followers[i]]->getServerString() << ","; + } + ss << cluster_[leader]->getServerString(); + + int rc = zoo_reconfig(zk, NULL, NULL, ss.str().c_str(), -1, buf, &len, + &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + parseConfig(buf, len, servers, version); + CPPUNIT_ASSERT_EQUAL(std::string("100000002"), version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + if (i == followers[0]) { + continue; + } + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + + // add the follower back. + len = 1024; + ss.str(""); + for (int i = 0; i < cluster_.size(); i++) { + ss << cluster_[i]->getServerString() << ","; + } + rc = zoo_reconfig(zk, NULL, NULL, ss.str().c_str(), -1, buf, &len, + &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, rc); + parseConfig(buf, len, servers, version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + zookeeper_close(zk); +} + +zhandle_t* TestReconfigServer:: +connectFollowers(std::vector &followers) { + std::stringstream ss; + int32_t leader = getLeader(); + CPPUNIT_ASSERT(leader >= 0); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(followers.size())); + for (int i = 0; i < followers.size(); i++) { + ss << cluster_[followers[i]]->getHostPort() << ","; + } + ss << cluster_[leader]->getHostPort(); + std::string hosts = ss.str().c_str(); + zoo_deterministic_conn_order(true); + zhandle_t* zk = zookeeper_init(hosts.c_str(), NULL, 10000, NULL, NULL, 0); + CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10)); + + std::string connectedHost(zoo_get_current_server(zk)); + std::string portString = connectedHost.substr(connectedHost.find(":") + 1); + uint32_t port; + std::istringstream (portString) >> port; + CPPUNIT_ASSERT_EQUAL(cluster_[followers[0]]->getClientPort(), port); + return zk; +} + +/** + * 1. Connect to a follower. + * 2. Remove the follower the client is connected to. + */ +void TestReconfigServer:: +testRemoveConnectedFollower() { + std::vector servers; + std::string version; + struct Stat stat; + int len = 1024; + char buf[len]; + + // connect to a follower. + std::stringstream ss; + std::vector followers = getFollowers(); + zhandle_t* zk = connectFollowers(followers); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK)); + + // remove the follower. + len = 1024; + ss.str(""); + ss << followers[0]; + zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat)); + parseConfig(buf, len, servers, version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + if (i == followers[0]) { + continue; + } + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + zookeeper_close(zk); +} + +/** + * ZOOKEEPER-2014: only admin or users who are explicitly granted permission can do reconfig. + */ +void TestReconfigServer:: +testReconfigFailureWithoutAuth() { + std::vector servers; + std::string version; + struct Stat stat; + int len = 1024; + char buf[len]; + + // connect to a follower. + std::stringstream ss; + std::vector followers = getFollowers(); + zhandle_t* zk = connectFollowers(followers); + + // remove the follower. + len = 1024; + ss.str(""); + ss << followers[0]; + // No auth, should fail. + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat)); + // Wrong auth, should fail. + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:wrong", 11, NULL,(void*)ZOK)); + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat)); + // Right auth, should pass. + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat)); + parseConfig(buf, len, servers, version); + CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size())); + for (int i = 0; i < cluster_.size(); i++) { + if (i == followers[0]) { + continue; + } + CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(), + cluster_[i]->getServerString()) != servers.end()); + } + zookeeper_close(zk); +} + +void TestReconfigServer:: +testReconfigFailureWithoutServerSuperuserPasswordConfigured() { + std::vector servers; + std::string version; + struct Stat stat; + int len = 1024; + char buf[len]; + + // Create a new quorum with the super user's password not configured. + tearDown(); + ZooKeeperQuorumServer::tConfigPairs configs; + configs.push_back(std::make_pair("reconfigEnabled", "true")); + cluster_ = ZooKeeperQuorumServer::getCluster(NUM_SERVERS, configs, ""); + + // connect to a follower. + std::stringstream ss; + std::vector followers = getFollowers(); + zhandle_t* zk = connectFollowers(followers); + + // remove the follower. + len = 1024; + ss.str(""); + ss << followers[0]; + // All cases should fail as server ensemble was not configured with the super user's password. + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:", 11, NULL,(void*)ZOK)); + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat)); + CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK)); + CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat)); + zookeeper_close(zk); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(TestReconfigServer); +#endif diff --git a/zookeeper-client/zookeeper-client-c/tests/TestServerRequireClientSASLAuth.cc b/zookeeper-client/zookeeper-client-c/tests/TestServerRequireClientSASLAuth.cc new file mode 100644 index 0000000..2943888 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestServerRequireClientSASLAuth.cc @@ -0,0 +1,109 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "CppAssertHelper.h" + +#include +#include + +#include + +#include "Util.h" +#include "WatchUtil.h" + +ZOOAPI int zoo_create2(zhandle_t *zh, const char *path, const char *value, + int valuelen, const struct ACL_vector *acl, int mode, + char *path_buffer, int path_buffer_len, struct Stat *stat); + +class Zookeeper_serverRequireClientSASL : public CPPUNIT_NS::TestFixture { + CPPUNIT_TEST_SUITE(Zookeeper_serverRequireClientSASL); +#ifdef THREADED + CPPUNIT_TEST(testServerRequireClientSASL); +#endif + CPPUNIT_TEST_SUITE_END(); + FILE *logfile; + static const char hostPorts[]; + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ + watchctx_t *ctx = (watchctx_t*)v; + + if (state == ZOO_CONNECTED_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + +public: + Zookeeper_serverRequireClientSASL() { + logfile = openlogfile("Zookeeper_serverRequireClientSASL"); + } + + ~Zookeeper_serverRequireClientSASL() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() { + zoo_set_log_stream(logfile); + } + + void startServer() { + char cmd[1024]; + sprintf(cmd, "%s startRequireSASLAuth", ZKSERVER_CMD); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void stopServer() { + char cmd[1024]; + sprintf(cmd, "%s stop", ZKSERVER_CMD); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void testServerRequireClientSASL() { + startServer(); + + watchctx_t ctx; + int rc = 0; + zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, &ctx, 0); + ctx.zh = zk; + CPPUNIT_ASSERT(zk); + + char pathbuf[80]; + struct Stat stat_a = {0}; + + rc = zoo_create2(zk, "/serverRequireClientSASL", "", 0, + &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, sizeof(pathbuf), &stat_a); + CPPUNIT_ASSERT_EQUAL((int)ZSESSIONCLOSEDREQUIRESASLAUTH, rc); + + stopServer(); + } +}; + +const char Zookeeper_serverRequireClientSASL::hostPorts[] = "127.0.0.1:22181"; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_serverRequireClientSASL); diff --git a/zookeeper-client/zookeeper-client-c/tests/TestWatchers.cc b/zookeeper-client/zookeeper-client-c/tests/TestWatchers.cc new file mode 100644 index 0000000..0a26934 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestWatchers.cc @@ -0,0 +1,773 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "CppAssertHelper.h" + +#include "ZKMocks.h" +#include "CollectionUtil.h" +#include "Util.h" + +class Zookeeper_watchers : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_watchers); + CPPUNIT_TEST(testDefaultSessionWatcher1); + CPPUNIT_TEST(testDefaultSessionWatcher2); + CPPUNIT_TEST(testObjectSessionWatcher1); + CPPUNIT_TEST(testObjectSessionWatcher2); + CPPUNIT_TEST(testNodeWatcher1); + CPPUNIT_TEST(testChildWatcher1); + CPPUNIT_TEST(testChildWatcher2); + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t *, int, int, const char *,void*){} + zhandle_t *zh; + FILE *logfile; + +public: + + Zookeeper_watchers() { + logfile = openlogfile("Zookeeper_watchers"); + } + + ~Zookeeper_watchers() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + + zoo_deterministic_conn_order(0); + zh=0; + } + + void tearDown() + { + zookeeper_close(zh); + } + + class ConnectionWatcher: public WatcherAction{ + public: + ConnectionWatcher():connected_(false),counter_(0){} + virtual void onConnectionEstablished(zhandle_t*){ + synchronized(mx_); + counter_++; + connected_=true; + } + SyncedBoolCondition isConnectionEstablished() const{ + return SyncedBoolCondition(connected_,mx_); + } + bool connected_; + int counter_; + }; + + class DisconnectWatcher: public WatcherAction{ + public: + DisconnectWatcher():disconnected_(false),counter_(0){} + virtual void onConnectionLost(zhandle_t*){ + synchronized(mx_); + counter_++; + disconnected_=true; + } + SyncedBoolCondition isDisconnected() const{ + return SyncedBoolCondition(disconnected_,mx_); + } + bool disconnected_; + int counter_; + }; + + class CountingDataWatcher: public WatcherAction{ + public: + CountingDataWatcher():disconnected_(false),counter_(0){} + virtual void onNodeValueChanged(zhandle_t*,const char* path){ + synchronized(mx_); + counter_++; + } + virtual void onConnectionLost(zhandle_t*){ + synchronized(mx_); + counter_++; + disconnected_=true; + } + bool disconnected_; + int counter_; + }; + + class DeletionCountingDataWatcher: public WatcherAction{ + public: + DeletionCountingDataWatcher():counter_(0){} + virtual void onNodeDeleted(zhandle_t*,const char* path){ + synchronized(mx_); + counter_++; + } + int counter_; + }; + + class ChildEventCountingWatcher: public WatcherAction{ + public: + ChildEventCountingWatcher():counter_(0){} + virtual void onChildChanged(zhandle_t*,const char* path){ + synchronized(mx_); + counter_++; + } + int counter_; + }; + +#ifndef THREADED + + // verify: the default watcher is called once for a session event + void testDefaultSessionWatcher1(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + ConnectionWatcher watcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &watcher,0); + CPPUNIT_ASSERT(zh!=0); + + int fd=0; + int interest=0; + timeval tv; + // open the socket + int rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTING_STATE,zoo_state(zh)); + // send the handshake packet to the server + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_ASSOCIATING_STATE,zoo_state(zh)); + // receive the server handshake response + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // verify connected + CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTED_STATE,zoo_state(zh)); + CPPUNIT_ASSERT(watcher.connected_); + CPPUNIT_ASSERT_EQUAL(1,watcher.counter_); + } + + // test case: connect to server, set a default watcher, disconnect from the server + // verify: the default watcher is called once + void testDefaultSessionWatcher2(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + DisconnectWatcher watcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &watcher,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + // first operation + AsyncCompletion ignored; + zkServer.addOperationResponse(new ZooGetResponse("1",1)); + int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // this will process the response and activate the watcher + rc=zookeeper_process(zh,ZOOKEEPER_READ); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // now, disconnect + zkServer.setConnectionLost(); + rc=zookeeper_process(zh,ZOOKEEPER_READ); + CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc); + // verify disconnected + CPPUNIT_ASSERT(watcher.disconnected_); + CPPUNIT_ASSERT_EQUAL(1,watcher.counter_); + } + + // testcase: connect to the server, set a watcher object on a node, + // disconnect from the server + // verify: the watcher object as well as the default watcher are called + void testObjectSessionWatcher1(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + DisconnectWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + AsyncCompletion ignored; + CountingDataWatcher wobject; + zkServer.addOperationResponse(new ZooStatResponse); + int rc=zoo_awexists(zh,"/x/y/1",activeWatcher,&wobject, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // this will process the response and activate the watcher + rc=zookeeper_process(zh,ZOOKEEPER_READ); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // now, disconnect + zkServer.setConnectionLost(); + rc=zookeeper_process(zh,ZOOKEEPER_READ); + CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc); + + // verify the default watcher has been triggered + CPPUNIT_ASSERT(defWatcher.disconnected_); + // and triggered only once + CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_); + + // the path-specific watcher has been triggered as well + CPPUNIT_ASSERT(wobject.disconnected_); + // only once! + CPPUNIT_ASSERT_EQUAL(1,wobject.counter_); + } + + // testcase: connect to the server, set a watcher object on a node, + // set a def watcher on another node,disconnect from the server + // verify: the watcher object as well as the default watcher are called + void testObjectSessionWatcher2(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + DisconnectWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + // set the default watcher + AsyncCompletion ignored; + zkServer.addOperationResponse(new ZooStatResponse); + int rc=zoo_aexists(zh,"/a/b/c",1,asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + CountingDataWatcher wobject; + zkServer.addOperationResponse(new ZooStatResponse); + rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // this will process the response and activate the watcher + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + // disconnect now + zkServer.setConnectionLost(); + rc=zookeeper_process(zh,ZOOKEEPER_READ); + CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc); + + // verify the default watcher has been triggered + CPPUNIT_ASSERT(defWatcher.disconnected_); + // and triggered only once + CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_); + + // the path-specific watcher has been triggered as well + CPPUNIT_ASSERT(wobject.disconnected_); + // only once! + CPPUNIT_ASSERT_EQUAL(1,wobject.counter_); + } + + // testcase: register 2 node watches for different paths, trigger the watches + // verify: the data watchers are processed, the default watcher is not called + void testNodeWatcher1(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + DisconnectWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + AsyncCompletion ignored; + CountingDataWatcher wobject1; + zkServer.addOperationResponse(new ZooStatResponse); + int rc=zoo_awexists(zh,"/a/b/c",activeWatcher,&wobject1, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + CountingDataWatcher wobject2; + zkServer.addOperationResponse(new ZooStatResponse); + rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject2, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // this will process the response and activate the watcher + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + // we are all set now; let's trigger the watches + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/a/b/c")); + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/x/y/z")); + // make sure all watchers have been processed + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_); + CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_); + CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_); + } + + // testcase: set up both a children and a data watchers on the node /a, then + // delete the node by sending a DELETE_EVENT event + // verify: both watchers are triggered + void testChildWatcher1(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + DeletionCountingDataWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + AsyncCompletion ignored; + DeletionCountingDataWatcher wobject1; + zkServer.addOperationResponse(new ZooStatResponse); + int rc=zoo_awexists(zh,"/a",activeWatcher,&wobject1, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + typedef ZooGetChildrenResponse::StringVector ZooVector; + zkServer.addOperationResponse(new ZooGetChildrenResponse( + Util::CollectionBuilder()("/a/1")("/a/2") + )); + DeletionCountingDataWatcher wobject2; + rc=zoo_awget_children(zh,"/a",activeWatcher, + &wobject2,asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // this will process the response and activate the watcher + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + // we are all set now; let's trigger the watches + zkServer.addRecvResponse(new ZNodeEvent(ZOO_DELETED_EVENT,"/a")); + // make sure the watchers have been processed + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_); + CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_); + CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_); + } + + // testcase: create both a child and data watch on the node /a, send a ZOO_CHILD_EVENT + // verify: only the child watch triggered + void testChildWatcher2(){ + Mock_gettimeofday timeMock; + ZookeeperServer zkServer; + // must call zookeeper_close() while all the mocks are in scope + CloseFinally guard(&zh); + + ChildEventCountingWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + forceConnected(zh); + + AsyncCompletion ignored; + ChildEventCountingWatcher wobject1; + zkServer.addOperationResponse(new ZooStatResponse); + int rc=zoo_awexists(zh,"/a",activeWatcher,&wobject1, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + typedef ZooGetChildrenResponse::StringVector ZooVector; + zkServer.addOperationResponse(new ZooGetChildrenResponse( + Util::CollectionBuilder()("/a/1")("/a/2") + )); + ChildEventCountingWatcher wobject2; + rc=zoo_awget_children(zh,"/a",activeWatcher, + &wobject2,asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // this will process the response and activate the watcher + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + // we are all set now; let's trigger the watches + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHILD_EVENT,"/a")); + // make sure the watchers have been processed + while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) { + millisleep(100); + } + CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc); + + CPPUNIT_ASSERT_EQUAL(0,wobject1.counter_); + CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_); + CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_); + } + +#else + // verify: the default watcher is called once for a session event + void testDefaultSessionWatcher1(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTED_STATE); + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + ConnectionWatcher watcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &watcher,0); + CPPUNIT_ASSERT(zh!=0); + // wait till watcher proccessing has completed (the connection + // established event) + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.isWatcherProcessingCompleted(),1000)<1000); + + // verify the watcher has been triggered + CPPUNIT_ASSERT(ensureCondition(watcher.isConnectionEstablished(),1000)<1000); + // triggered only once + CPPUNIT_ASSERT_EQUAL(1,watcher.counter_); + } + + // test case: connect to server, set a default watcher, disconnect from the server + // verify: the default watcher is called once + void testDefaultSessionWatcher2(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTING_STATE); + DisconnectWatcher watcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &watcher,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + // set a default watch + AsyncCompletion ignored; + // a successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + int rc=zoo_aexists(zh,"/x/y/z",1,asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // now, initiate a disconnect + zkServer.setConnectionLost(); + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.isWatcherProcessingCompleted(),1000)<1000); + + // verify the watcher has been triggered + CPPUNIT_ASSERT(watcher.disconnected_); + // triggered only once + CPPUNIT_ASSERT_EQUAL(1,watcher.counter_); + } + + // testcase: connect to the server, set a watcher object on a node, + // disconnect from the server + // verify: the watcher object as well as the default watcher are called + void testObjectSessionWatcher1(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTING_STATE); + DisconnectWatcher defWatcher; + // use the tracker to find out when the watcher has been activated + WatcherActivationTracker activationTracker; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + + AsyncCompletion ignored; + // this successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + CountingDataWatcher wobject; + activationTracker.track(&wobject); + // set a path-specific watcher + int rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // make sure the watcher gets activated before we continue + CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000); + + // now, initiate a disconnect + zkServer.setConnectionLost(); + // make sure all watchers have been processed + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.isWatcherProcessingCompleted(),1000)<1000); + + // verify the default watcher has been triggered + CPPUNIT_ASSERT(defWatcher.disconnected_); + // and triggered only once + CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_); + + // the path-specific watcher has been triggered as well + CPPUNIT_ASSERT(wobject.disconnected_); + // only once! + CPPUNIT_ASSERT_EQUAL(1,wobject.counter_); + } + + // testcase: connect to the server, set a watcher object on a node, + // set a def watcher on another node,disconnect from the server + // verify: the watcher object as well as the default watcher are called + void testObjectSessionWatcher2(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTING_STATE); + DisconnectWatcher defWatcher; + // use the tracker to find out when the watcher has been activated + WatcherActivationTracker activationTracker; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + + // set a default watch + AsyncCompletion ignored; + // a successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + activationTracker.track(&defWatcher); + int rc=zoo_aexists(zh,"/a/b/c",1,asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // make sure the watcher gets activated before we continue + CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000); + + // this successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + CountingDataWatcher wobject; + activationTracker.track(&wobject); + // set a path-specific watcher + rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // make sure the watcher gets activated before we continue + CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000); + + // now, initiate a disconnect + zkServer.setConnectionLost(); + // make sure all watchers have been processed + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.isWatcherProcessingCompleted(),1000)<1000); + + // verify the default watcher has been triggered + CPPUNIT_ASSERT(defWatcher.disconnected_); + // and triggered only once + CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_); + + // the path-specific watcher has been triggered as well + CPPUNIT_ASSERT(wobject.disconnected_); + // only once! + CPPUNIT_ASSERT_EQUAL(1,wobject.counter_); + } + + // testcase: register 2 node watches for different paths, trigger the watches + // verify: the data watchers are processed, the default watcher is not called + void testNodeWatcher1(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_CHANGED_EVENT,0,false); + CountingDataWatcher defWatcher; + // use the tracker to find out when the watcher has been activated + WatcherActivationTracker activationTracker; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + + // don't care about completions + AsyncCompletion ignored; + // set a one-shot watch + // a successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + CountingDataWatcher wobject1; + activationTracker.track(&wobject1); + int rc=zoo_awexists(zh,"/a/b/c",activeWatcher,&wobject1, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // make sure the watcher gets activated before we continue + CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000); + + // this successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + CountingDataWatcher wobject2; + activationTracker.track(&wobject2); + // set a path-specific watcher + rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject2, + asyncCompletion,&ignored); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // make sure the watcher gets activated before we continue + CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000); + + // we are all set now; let's trigger the watches + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/a/b/c")); + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/x/y/z")); + // make sure all watchers have been processed + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.deliveryCounterEquals(2),1000)<1000); + + CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_); + CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_); + CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_); + } + + // testcase: set up both a children and a data watchers on the node /a, then + // delete the node (that is, send a DELETE_EVENT) + // verify: both watchers are triggered + void testChildWatcher1(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_DELETED_EVENT,0); + DeletionCountingDataWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + + // a successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + DeletionCountingDataWatcher wobject1; + Stat stat; + // add a node watch + int rc=zoo_wexists(zh,"/a",activeWatcher,&wobject1,&stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + typedef ZooGetChildrenResponse::StringVector ZooVector; + zkServer.addOperationResponse(new ZooGetChildrenResponse( + Util::CollectionBuilder()("/a/1")("/a/2") + )); + DeletionCountingDataWatcher wobject2; + String_vector children; + rc=zoo_wget_children(zh,"/a",activeWatcher,&wobject2,&children); + deallocate_String_vector(&children); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // we are all set now; let's trigger the watches + zkServer.addRecvResponse(new ZNodeEvent(ZOO_DELETED_EVENT,"/a")); + // make sure the watchers have been processed + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.isWatcherProcessingCompleted(),1000)<1000); + + CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_); + CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_); + CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_); + } + + // testcase: create both a child and data watch on the node /a, send a ZOO_CHILD_EVENT + // verify: only the child watch triggered + void testChildWatcher2(){ + Mock_gettimeofday timeMock; + // zookeeper simulator + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // must call zookeeper_close() while all the mocks are in the scope! + CloseFinally guard(&zh); + + // detects when all watchers have been delivered + WatcherDeliveryTracker deliveryTracker(ZOO_CHILD_EVENT,0); + ChildEventCountingWatcher defWatcher; + zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID, + &defWatcher,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + // a successful server response will activate the watcher + zkServer.addOperationResponse(new ZooStatResponse); + ChildEventCountingWatcher wobject1; + Stat stat; + // add a node watch + int rc=zoo_wexists(zh,"/a",activeWatcher,&wobject1,&stat); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + typedef ZooGetChildrenResponse::StringVector ZooVector; + zkServer.addOperationResponse(new ZooGetChildrenResponse( + Util::CollectionBuilder()("/a/1")("/a/2") + )); + ChildEventCountingWatcher wobject2; + String_vector children; + rc=zoo_wget_children(zh,"/a",activeWatcher,&wobject2,&children); + deallocate_String_vector(&children); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // we are all set now; let's trigger the watches + zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHILD_EVENT,"/a")); + // make sure the watchers have been processed + CPPUNIT_ASSERT(ensureCondition( + deliveryTracker.isWatcherProcessingCompleted(),1000)<1000); + + CPPUNIT_ASSERT_EQUAL(0,wobject1.counter_); + CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_); + CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_); + } + +#endif //THREADED +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_watchers); diff --git a/zookeeper-client/zookeeper-client-c/tests/TestZookeeperClose.cc b/zookeeper-client/zookeeper-client-c/tests/TestZookeeperClose.cc new file mode 100644 index 0000000..f20910f --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestZookeeperClose.cc @@ -0,0 +1,473 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include + +#include "ZKMocks.h" + +#ifdef THREADED +#include "PthreadMocks.h" +#endif + +using namespace std; + +class Zookeeper_close : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_close); +#ifdef THREADED + CPPUNIT_TEST(testIOThreadStoppedOnExpire); +#endif + CPPUNIT_TEST(testCloseUnconnected); + CPPUNIT_TEST(testCloseUnconnected1); + CPPUNIT_TEST(testCloseConnected1); + CPPUNIT_TEST(testCloseFromWatcher1); + CPPUNIT_TEST_SUITE_END(); + zhandle_t *zh; + static void watcher(zhandle_t *, int, int, const char *,void*){} + FILE *logfile; +public: + + Zookeeper_close() { + logfile = openlogfile("Zookeeper_close"); + } + + ~Zookeeper_close() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + + zoo_deterministic_conn_order(0); + zh=0; + } + + void tearDown() + { + zookeeper_close(zh); + } + + class CloseOnSessionExpired: public WatcherAction{ + public: + CloseOnSessionExpired(bool callClose=true): + callClose_(callClose),rc(ZOK){} + virtual void onSessionExpired(zhandle_t* zh){ + memcpy(&lzh,zh,sizeof(lzh)); + if(callClose_) + rc=zookeeper_close(zh); + } + zhandle_t lzh; + bool callClose_; + int rc; + }; + +#ifndef THREADED + void testCloseUnconnected() + { + zh=zookeeper_init("localhost:2121",watcher,10000,0,0,0); + CPPUNIT_ASSERT(zh!=0); + + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + // make a copy of zhandle before close() overwrites some of + // it members with NULLs + zhandle_t lzh; + memcpy(&lzh,zh,sizeof(lzh)); + int rc=zookeeper_close(zh); + zhandle_t* savezh=zh; zh=0; + freeMock.disable(); // disable mock's fake free()- use libc's free() instead + + // verify that zookeeper_close has done its job + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.addrs.data)); + // This cannot be maintained properly CPPUNIT_ASSERT_EQUAL(9,freeMock.callCounter); + } + void testCloseUnconnected1() + { + zh=zookeeper_init("localhost:2121",watcher,10000,0,0,0); + CPPUNIT_ASSERT(zh!=0); + // simulate connected state + zh->fd->sock=ZookeeperServer::FD; + zh->state=ZOO_CONNECTED_STATE; + Mock_flush_send_queue zkMock; + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + // make a copy of zhandle before close() overwrites some of + // it members with NULLs + zhandle_t lzh; + memcpy(&lzh,zh,sizeof(lzh)); + int rc=zookeeper_close(zh); + zhandle_t* savezh=zh; zh=0; + freeMock.disable(); // disable mock's fake free()- use libc's free() instead + + // verify that zookeeper_close has done its job + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.addrs.data)); + // the close request sent? + CPPUNIT_ASSERT_EQUAL(1,zkMock.counter); + } + void testCloseConnected1() + { + ZookeeperServer zkServer; + // poll() will called from zookeeper_close() + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT_EQUAL(ZOO_NOTCONNECTED_STATE, zoo_state(zh)); + + Mock_gettimeofday timeMock; + + int fd=0; + int interest=0; + timeval tv; + int rc=zookeeper_interest(zh,&fd,&interest,&tv); + + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTING_STATE,zoo_state(zh)); + CPPUNIT_ASSERT_EQUAL(ZOOKEEPER_READ|ZOOKEEPER_WRITE,interest); + + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_ASSOCIATING_STATE,zoo_state(zh)); + + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + rc=zookeeper_process(zh,interest); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTED_STATE,zoo_state(zh)); + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + // make a copy of zhandle before close() overwrites some of + // it members with NULLs + zhandle_t lzh; + memcpy(&lzh,zh,sizeof(lzh)); + zookeeper_close(zh); + zhandle_t* savezh=zh; zh=0; + freeMock.disable(); // disable mock's fake free()- use libc's free() instead + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.addrs.data)); + // the close request sent? + CPPUNIT_ASSERT_EQUAL(1,(int)zkServer.closeSent); + } + void testCloseFromWatcher1() + { + Mock_gettimeofday timeMock; + + ZookeeperServer zkServer; + // make the server return a non-matching session id + zkServer.returnSessionExpired(); + // poll() will called from zookeeper_close() + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + + CloseOnSessionExpired closeAction; + zh=zookeeper_init("localhost:2121",activeWatcher,10000, + TEST_CLIENT_ID,&closeAction,0); + CPPUNIT_ASSERT(zh!=0); + + int fd=0; + int interest=0; + timeval tv; + // initiate connection + int rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTING_STATE,zoo_state(zh)); + CPPUNIT_ASSERT_EQUAL(ZOOKEEPER_READ|ZOOKEEPER_WRITE,interest); + rc=zookeeper_process(zh,interest); + // make sure the handshake in progress + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + CPPUNIT_ASSERT_EQUAL(ZOO_ASSOCIATING_STATE,zoo_state(zh)); + rc=zookeeper_interest(zh,&fd,&interest,&tv); + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + // should call the watcher with ZOO_EXPIRED_SESSION_STATE state + rc=zookeeper_process(zh,interest); + zhandle_t* savezh=zh; zh=0; + freeMock.disable(); // disable mock's fake free()- use libc's free() instead + + CPPUNIT_ASSERT_EQUAL(ZOO_EXPIRED_SESSION_STATE,zoo_state(savezh)); + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(closeAction.lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(closeAction.lzh.addrs.data)); + // make sure the close request NOT sent + CPPUNIT_ASSERT_EQUAL(0,(int)zkServer.closeSent); + } +#else + void testCloseUnconnected() + { + // disable threading + MockPthreadZKNull pthreadMock; + zh=zookeeper_init("localhost:2121",watcher,10000,0,0,0); + + CPPUNIT_ASSERT(zh!=0); + adaptor_threads* adaptor=(adaptor_threads*)zh->adaptor_priv; + CPPUNIT_ASSERT(adaptor!=0); + + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + // make a copy of zhandle before close() overwrites some of + // it members with NULLs + zhandle_t lzh; + memcpy(&lzh,zh,sizeof(lzh)); + int rc=zookeeper_close(zh); + zhandle_t* savezh=zh; zh=0; + // we're done, disable mock's fake free(), use libc's free() instead + freeMock.disable(); + + // verify that zookeeper_close has done its job + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.addrs.data)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(adaptor)); + // Cannot be maintained accurately: CPPUNIT_ASSERT_EQUAL(10,freeMock.callCounter); + // threads + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(adaptor->io)); + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(adaptor->completion)); + // mutexes + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(&savezh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,MockPthreadsNull::getInvalidAccessCounter(&savezh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(&savezh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(0,MockPthreadsNull::getInvalidAccessCounter(&savezh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(&savezh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(0,MockPthreadsNull::getInvalidAccessCounter(&savezh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(&savezh->completions_to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,MockPthreadsNull::getInvalidAccessCounter(&savezh->completions_to_process.lock)); + // conditionals + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(&savezh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(0,MockPthreadsNull::getInvalidAccessCounter(&savezh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(1,MockPthreadsNull::getDestroyCounter(&savezh->completions_to_process.cond)); + CPPUNIT_ASSERT_EQUAL(0,MockPthreadsNull::getInvalidAccessCounter(&savezh->completions_to_process.cond)); + } + void testCloseUnconnected1() + { + for(int i=0; i<100;i++){ + zh=zookeeper_init("localhost:2121",watcher,10000,0,0,0); + CPPUNIT_ASSERT(zh!=0); + adaptor_threads* adaptor=(adaptor_threads*)zh->adaptor_priv; + CPPUNIT_ASSERT(adaptor!=0); + int rc=zookeeper_close(zh); + zh=0; + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + } + } + void testCloseConnected1() + { + // frozen time -- no timeouts and no pings + Mock_gettimeofday timeMock; + + for(int i=0;i<100;i++){ + ZookeeperServer zkServer; + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // use a checked version of pthread calls + CheckedPthread threadMock; + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + + zh=zookeeper_init("localhost:2121",watcher,10000,TEST_CLIENT_ID,0,0); + CPPUNIT_ASSERT(zh!=0); + // make sure the client has connected + CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000); + // make a copy of zhandle before close() overwrites some of + // its members with NULLs + zhandle_t lzh; + memcpy(&lzh,zh,sizeof(lzh)); + int rc=zookeeper_close(zh); + zhandle_t* savezh=zh; zh=0; + // we're done, disable mock's fake free(), use libc's free() instead + freeMock.disable(); + + CPPUNIT_ASSERT_EQUAL((int)ZOK,rc); + adaptor_threads* adaptor=(adaptor_threads*)lzh.adaptor_priv; + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(savezh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh.addrs.data)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(adaptor)); + // threads + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(adaptor->io)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(adaptor->completion)); + // mutexes + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&savezh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&savezh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&savezh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&savezh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&savezh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&savezh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&savezh->completions_to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&savezh->completions_to_process.lock)); + // conditionals + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&savezh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&savezh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&savezh->completions_to_process.cond)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&savezh->completions_to_process.cond)); + } + } + + struct PointerFreed{ + PointerFreed(Mock_free_noop& freeMock,void* ptr): + freeMock_(freeMock),ptr_(ptr){} + bool operator()() const{return freeMock_.isFreed(ptr_); } + Mock_free_noop& freeMock_; + void* ptr_; + }; + // test if zookeeper_close may be called from a watcher callback on + // SESSION_EXPIRED event + void testCloseFromWatcher1() + { + // frozen time -- no timeouts and no pings + Mock_gettimeofday timeMock; + + for(int i=0;i<100;i++){ + ZookeeperServer zkServer; + // make the server return a non-matching session id + zkServer.returnSessionExpired(); + + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // use a checked version of pthread calls + CheckedPthread threadMock; + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + + CloseOnSessionExpired closeAction; + zh=zookeeper_init("localhost:2121",activeWatcher,10000, + TEST_CLIENT_ID,&closeAction,0); + + CPPUNIT_ASSERT(zh!=0); + // we rely on the fact that zh is freed the last right before + // zookeeper_close() returns... + CPPUNIT_ASSERT(ensureCondition(PointerFreed(freeMock,zh),1000)<1000); + zhandle_t* lzh=zh; + zh=0; + // we're done, disable mock's fake free(), use libc's free() instead + freeMock.disable(); + + CPPUNIT_ASSERT_EQUAL((int)ZOK,closeAction.rc); + adaptor_threads* adaptor=(adaptor_threads*)closeAction.lzh.adaptor_priv; + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(closeAction.lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(closeAction.lzh.addrs.data)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(adaptor)); + // threads + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(adaptor->io)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(adaptor->completion)); + // mutexes + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->completions_to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->completions_to_process.lock)); + // conditionals + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->completions_to_process.cond)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->completions_to_process.cond)); + } + } + + void testIOThreadStoppedOnExpire() + { + // frozen time -- no timeouts and no pings + Mock_gettimeofday timeMock; + + for(int i=0;i<100;i++){ + ZookeeperServer zkServer; + // make the server return a non-matching session id + zkServer.returnSessionExpired(); + + Mock_poll pollMock(&zkServer,ZookeeperServer::FD); + // use a checked version of pthread calls + CheckedPthread threadMock; + // do not call zookeeper_close() from the watcher + CloseOnSessionExpired closeAction(false); + zh=zookeeper_init("localhost:2121",activeWatcher,10000, + &testClientId,&closeAction,0); + + // this is to ensure that if any assert fires, zookeeper_close() + // will still be called while all the mocks are in the scope! + CloseFinally guard(&zh); + + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT(ensureCondition(SessionExpired(zh),1000)<1000); + CPPUNIT_ASSERT(ensureCondition(IOThreadStopped(zh),1000)<1000); + // make sure the watcher has been processed + CPPUNIT_ASSERT(ensureCondition(closeAction.isWatcherTriggered(),1000)<1000); + // make sure the threads have not been destroyed yet + adaptor_threads* adaptor=(adaptor_threads*)zh->adaptor_priv; + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getDestroyCounter(adaptor->io)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getDestroyCounter(adaptor->completion)); + // about to call zookeeper_close() -- no longer need the guard + guard.disarm(); + + // do not actually free the memory while in zookeeper_close() + Mock_free_noop freeMock; + zookeeper_close(zh); + zhandle_t* lzh=zh; zh=0; + // we're done, disable mock's fake free(), use libc's free() instead + freeMock.disable(); + + // memory + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(lzh)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(closeAction.lzh.hostname)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(closeAction.lzh.addrs.data)); + CPPUNIT_ASSERT_EQUAL(1,freeMock.getFreeCount(adaptor)); + // threads + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(adaptor->io)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(adaptor->completion)); + // mutexes + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->to_process.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->to_send.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->sent_requests.lock)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->completions_to_process.lock)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->completions_to_process.lock)); + // conditionals + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->sent_requests.cond)); + CPPUNIT_ASSERT_EQUAL(1,CheckedPthread::getDestroyCounter(&lzh->completions_to_process.cond)); + CPPUNIT_ASSERT_EQUAL(0,CheckedPthread::getInvalidAccessCounter(&lzh->completions_to_process.cond)); + } + } + +#endif +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_close); diff --git a/zookeeper-client/zookeeper-client-c/tests/TestZookeeperInit.cc b/zookeeper-client/zookeeper-client-c/tests/TestZookeeperInit.cc new file mode 100644 index 0000000..61da41d --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/TestZookeeperInit.cc @@ -0,0 +1,301 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include + +#include "Util.h" +#include "LibCMocks.h" +#include "ZKMocks.h" + +#ifdef THREADED +#include "PthreadMocks.h" +#else +class MockPthreadsNull; +#endif + +using namespace std; + +class Zookeeper_init : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_init); + CPPUNIT_TEST(testBasic); + CPPUNIT_TEST(testAddressResolution); + CPPUNIT_TEST(testMultipleAddressResolution); + CPPUNIT_TEST(testNullAddressString); + CPPUNIT_TEST(testEmptyAddressString); + CPPUNIT_TEST(testOneSpaceAddressString); + CPPUNIT_TEST(testTwoSpacesAddressString); + CPPUNIT_TEST(testInvalidAddressString1); + CPPUNIT_TEST(testInvalidAddressString2); + CPPUNIT_TEST(testNonexistentHost); + CPPUNIT_TEST(testOutOfMemory_init); + CPPUNIT_TEST(testOutOfMemory_getaddrs1); +#if !defined(__CYGWIN__) // not valid for cygwin + CPPUNIT_TEST(testOutOfMemory_getaddrs2); +#endif + CPPUNIT_TEST(testPermuteAddrsList); + CPPUNIT_TEST_SUITE_END(); + zhandle_t *zh; + MockPthreadsNull* pthreadMock; + static void watcher(zhandle_t *, int , int , const char *,void*){} + FILE *logfile; +public: + Zookeeper_init():zh(0),pthreadMock(0){ + logfile = openlogfile("Zookeeper_init"); + } + + ~Zookeeper_init() { + if (logfile) { + fflush(logfile); + fclose(logfile); + logfile = 0; + } + } + + void setUp() + { + zoo_set_log_stream(logfile); + + zoo_deterministic_conn_order(0); +#ifdef THREADED + // disable threading + pthreadMock=new MockPthreadZKNull; +#endif + zh=0; + } + + void tearDown() + { + zookeeper_close(zh); +#ifdef THREADED + delete pthreadMock; +#endif + } + + void testBasic() + { + const string EXPECTED_HOST("127.0.0.1:2121"); + const unsigned int EXPECTED_ADDRS_COUNT =1; + const int EXPECTED_RECV_TIMEOUT=10000; + clientid_t cid; + memset(&cid,0xFE,sizeof(cid)); + + zh=zookeeper_init(EXPECTED_HOST.c_str(),watcher,EXPECTED_RECV_TIMEOUT, + &cid,(void*)1,0); + + CPPUNIT_ASSERT(zh != NULL); + CPPUNIT_ASSERT(zh->fd->sock == -1); + CPPUNIT_ASSERT(zh->hostname != NULL); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count); + CPPUNIT_ASSERT_EQUAL(EXPECTED_HOST,string(zh->hostname)); + CPPUNIT_ASSERT(zh->state == ZOO_NOTCONNECTED_STATE); + CPPUNIT_ASSERT(zh->context == (void*)1); + CPPUNIT_ASSERT_EQUAL(EXPECTED_RECV_TIMEOUT,zh->recv_timeout); + CPPUNIT_ASSERT(zh->watcher == watcher); + CPPUNIT_ASSERT(zh->addrs.next==0); + CPPUNIT_ASSERT(zh->primer_buffer.buffer==zh->primer_storage_buffer); + CPPUNIT_ASSERT(zh->primer_buffer.curr_offset ==0); + CPPUNIT_ASSERT(zh->primer_buffer.len == sizeof(zh->primer_storage_buffer)); + CPPUNIT_ASSERT(zh->primer_buffer.next == 0); + CPPUNIT_ASSERT(zh->last_zxid ==0); + CPPUNIT_ASSERT(memcmp(&zh->client_id,&cid,sizeof(cid))==0); + +#ifdef THREADED + // thread specific checks + adaptor_threads* adaptor=(adaptor_threads*)zh->adaptor_priv; + CPPUNIT_ASSERT(adaptor!=0); + CPPUNIT_ASSERT(pthreadMock->pthread_createCounter==2); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(adaptor->io)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(adaptor->completion)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->to_process.lock)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->to_send.lock)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->sent_requests.lock)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->completions_to_process.lock)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->sent_requests.cond)); + CPPUNIT_ASSERT(MockPthreadsNull::isInitialized(&zh->completions_to_process.cond)); +#endif + } + void testAddressResolution() + { + const char EXPECTED_IPS[][4]={{127,0,0,1}}; + const unsigned int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS); + + zoo_deterministic_conn_order(1); + zh=zookeeper_init("127.0.0.1:2121",0,10000,0,0,0); + + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count); + for(unsigned int i=0;iaddrs.count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i]; + CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0); + CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port)); + } + } + void testMultipleAddressResolution() + { + const string EXPECTED_HOST("127.0.0.1:2121,127.0.0.2:3434"); + const char EXPECTED_IPS[][4]={{127,0,0,1},{127,0,0,2}}; + const unsigned int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS); + + zoo_deterministic_conn_order(1); + zh=zookeeper_init(EXPECTED_HOST.c_str(),0,1000,0,0,0); + + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count); + + for(unsigned int i=0;iaddrs.count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i]; + CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0); + if(i<1) + CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port)); + else + CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port)); + } + } + void testMultipleAddressWithSpace() + { + const string EXPECTED_HOST("127.0.0.1:2121, 127.0.0.2:3434"); + const char EXPECTED_IPS[][4]={{127,0,0,1},{127,0,0,2}}; + const unsigned int EXPECTED_ADDRS_COUNT =COUNTOF(EXPECTED_IPS); + + zoo_deterministic_conn_order(1); + zh=zookeeper_init(EXPECTED_HOST.c_str(),0,1000,0,0,0); + + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDRS_COUNT,zh->addrs.count); + + for(unsigned int i=0;iaddrs.count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i]; + CPPUNIT_ASSERT(memcmp(EXPECTED_IPS[i],&addr->sin_addr,sizeof(addr->sin_addr))==0); + if(i<1) + CPPUNIT_ASSERT_EQUAL(2121,(int)ntohs(addr->sin_port)); + else + CPPUNIT_ASSERT_EQUAL(3434,(int)ntohs(addr->sin_port)); + } + } + void testNullAddressString() + { + zh=zookeeper_init(NULL,0,0,0,0,0); + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(EINVAL,errno); + } + void testEmptyAddressString() + { + const string INVALID_HOST(""); + zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0); + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(EINVAL,errno); + } + void testOneSpaceAddressString() + { + const string INVALID_HOST(" "); + zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0); + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(EINVAL,errno); + } + void testTwoSpacesAddressString() + { + const string INVALID_HOST(" "); + zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0); + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(EINVAL,errno); + } + void testInvalidAddressString1() + { + const string INVALID_HOST("host1"); + zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0); + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(EINVAL,errno); + } + void testInvalidAddressString2() + { + const string INVALID_HOST("host1:1111+host:123"); + zh=zookeeper_init(INVALID_HOST.c_str(),0,0,0,0,0); + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT((ENOENT|EINVAL) & errno); + } + void testNonexistentHost() + { + const string EXPECTED_HOST("host1.blabadibla.bla.:1111"); + + zh=zookeeper_init(EXPECTED_HOST.c_str(),0,0,0,0,0); + + CPPUNIT_ASSERT(zh==0); + //With the switch to thread safe getaddrinfo, we don't get + //these global variables + //CPPUNIT_ASSERT_EQUAL(EINVAL,errno); + //CPPUNIT_ASSERT_EQUAL(HOST_NOT_FOUND,h_errno); + } + void testOutOfMemory_init() + { + Mock_calloc mock; + mock.callsBeforeFailure=0; // fail first calloc in init() + + zh=zookeeper_init("ahost:123",watcher,10000,0,0,0); + + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(ENOMEM,errno); + } + void testOutOfMemory_getaddrs1() + { + Mock_realloc reallocMock; + reallocMock.callsBeforeFailure=0; // fail on first call to realloc + + zh=zookeeper_init("127.0.0.1:123",0,0,0,0,0); + + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(ENOMEM,errno); + } + void testOutOfMemory_getaddrs2() + { + Mock_realloc reallocMock; + reallocMock.callsBeforeFailure=1; // fail on the second call to realloc + + zh=zookeeper_init("127.0.0.1:123,127.0.0.2:123,127.0.0.3:123,127.0.0.4:123,127.0.0.5:123,127.0.0.6:123,127.0.0.7:123,127.0.0.8:123,127.0.0.9:123,127.0.0.10:123,127.0.0.11:123,127.0.0.12:123,127.0.0.13:123,127.0.0.14:123,127.0.0.15:123,127.0.0.16:123,127.0.0.17:123",0,0,0,0,0); + + CPPUNIT_ASSERT(zh==0); + CPPUNIT_ASSERT_EQUAL(ENOMEM,errno); + } + void testPermuteAddrsList() + { + const char EXPECTED[][5]={"\0\0\0\0","\1\1\1\1","\2\2\2\2","\3\3\3\3"}; + const unsigned int EXPECTED_ADDR_COUNT=COUNTOF(EXPECTED); + + const int RAND_SEQ[]={0,1,1,-1}; + const int RAND_SIZE=COUNTOF(RAND_SEQ); + Mock_random randomMock; + randomMock.randomReturns.assign(RAND_SEQ,RAND_SEQ+RAND_SIZE-1); + zh=zookeeper_init("0.0.0.0:123,1.1.1.1:123,2.2.2.2:123,3.3.3.3:123",0,1000,0,0,0); + + CPPUNIT_ASSERT(zh!=0); + CPPUNIT_ASSERT_EQUAL(EXPECTED_ADDR_COUNT,zh->addrs.count); + const string EXPECTED_SEQ("3210"); + char ACTUAL_SEQ[EXPECTED_ADDR_COUNT+1]; ACTUAL_SEQ[EXPECTED_ADDR_COUNT]=0; + for(unsigned int i=0;iaddrs.count;i++){ + sockaddr_in* addr=(struct sockaddr_in*)&zh->addrs.data[i]; + // match the first byte of the EXPECTED and of the actual address + ACTUAL_SEQ[i]=((char*)&addr->sin_addr)[0]+'0'; + } + CPPUNIT_ASSERT_EQUAL(EXPECTED_SEQ,string(ACTUAL_SEQ)); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_init); diff --git a/zookeeper-client/zookeeper-client-c/tests/ThreadingUtil.cc b/zookeeper-client/zookeeper-client-c/tests/ThreadingUtil.cc new file mode 100644 index 0000000..3b5170a --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/ThreadingUtil.cc @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include "ThreadingUtil.h" +#include "LibCSymTable.h" + +#ifdef THREADED + +// **************************************************************************** +// Mutex wrapper +struct Mutex::Impl{ + Impl(){ + LIBC_SYMBOLS.pthread_mutex_init(&mut_, 0); + } + ~Impl(){ + LIBC_SYMBOLS.pthread_mutex_destroy(&mut_); + } + pthread_mutex_t mut_; +}; + +Mutex::Mutex():impl_(new Impl) {} +Mutex::~Mutex() { delete impl_;} +void Mutex::acquire() { + LIBC_SYMBOLS.pthread_mutex_lock(&impl_->mut_); +} +void Mutex::release() { + LIBC_SYMBOLS.pthread_mutex_unlock(&impl_->mut_); +} + +// **************************************************************************** +// Atomics +int32_t atomic_post_incr(volatile int32_t* operand, int32_t incr) +{ +#if defined(__GNUC__) + return __sync_fetch_and_add(operand,incr); +#else + int32_t result; + __asm__ __volatile__( + "lock xaddl %0,%1\n" + : "=r"(result), "=m"(*operand) + : "0"(incr) + : "memory"); + return result; +#endif +} +int32_t atomic_fetch_store(volatile int32_t *ptr, int32_t value) +{ +#if defined(__GNUC__) + return __sync_lock_test_and_set(ptr,value); +#else + int32_t result; + __asm__ __volatile__("lock xchgl %0,%1\n" + : "=r"(result), "=m"(*ptr) + : "0"(value) + : "memory"); + return result; +#endif +} +#else +int32_t atomic_post_incr(volatile int32_t* operand, int32_t incr){ + int32_t v=*operand; + *operand+=incr; + return v; +} +int32_t atomic_fetch_store(volatile int32_t *ptr, int32_t value) +{ + int32_t result=*ptr; + *ptr=value; + return result; +} +#endif // THREADED diff --git a/zookeeper-client/zookeeper-client-c/tests/ThreadingUtil.h b/zookeeper-client/zookeeper-client-c/tests/ThreadingUtil.h new file mode 100644 index 0000000..9165412 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/ThreadingUtil.h @@ -0,0 +1,261 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef THREADINGUTIL_H_ +#define THREADINGUTIL_H_ + +#include + +#ifdef THREADED +#include "pthread.h" +#endif + +// ***************************************************************************** +// Threading primitives + +// atomic post-increment; returns the previous value of the operand +int32_t atomic_post_incr(volatile int32_t* operand, int32_t incr); +// atomic fetch&store; returns the previous value of the operand +int32_t atomic_fetch_store(volatile int32_t *operand, int32_t value); + +// a partial implementation of an atomic integer type +class AtomicInt{ +public: + explicit AtomicInt(int32_t init=0):v_(init){} + AtomicInt(const AtomicInt& other):v_(other){} + // assigment + AtomicInt& operator=(const AtomicInt& lhs){ + atomic_fetch_store(&v_,lhs); + return *this; + } + AtomicInt& operator=(int32_t i){ + atomic_fetch_store(&v_,i); + return *this; + } + // pre-increment + AtomicInt& operator++() { + atomic_post_incr(&v_,1); + return *this; + } + // pre-decrement + AtomicInt& operator--() { + atomic_post_incr(&v_,-1); + return *this; + } + // post-increment + AtomicInt operator++(int){ + return AtomicInt(atomic_post_incr(&v_,1)); + } + // post-decrement + AtomicInt operator--(int){ + return AtomicInt(atomic_post_incr(&v_,-1)); + } + + operator int() const{ + return atomic_post_incr(&v_,0); + } + int get() const{ + return atomic_post_incr(&v_,0); + } +private: + mutable int32_t v_; +}; + +#ifdef THREADED +// **************************************************************************** +#define VALIDATE_JOBS(jm) jm.validateJobs(__FILE__,__LINE__) +#define VALIDATE_JOB(j) j.validate(__FILE__,__LINE__) + +class Mutex{ +public: + Mutex(); + ~Mutex(); + void acquire(); + void release(); +private: + Mutex(const Mutex&); + Mutex& operator=(const Mutex&); + struct Impl; + Impl* impl_; +}; + +class MTLock{ +public: + MTLock(Mutex& m):m_(m){m.acquire();} + ~MTLock(){m_.release();} + Mutex& m_; +}; + +#define synchronized(m) MTLock __lock(m) + +// **************************************************************************** +class Latch { +public: + virtual ~Latch() {} + virtual void await() const =0; + virtual void signalAndWait() =0; + virtual void signal() =0; +}; + +class CountDownLatch: public Latch { +public: + CountDownLatch(int count):count_(count) { + pthread_cond_init(&cond_,0); + pthread_mutex_init(&mut_,0); + } + virtual ~CountDownLatch() { + pthread_mutex_lock(&mut_); + if(count_!=0) { + count_=0; + pthread_cond_broadcast(&cond_); + } + pthread_mutex_unlock(&mut_); + + pthread_cond_destroy(&cond_); + pthread_mutex_destroy(&mut_); + } + + virtual void await() const { + pthread_mutex_lock(&mut_); + awaitImpl(); + pthread_mutex_unlock(&mut_); + } + virtual void signalAndWait() { + pthread_mutex_lock(&mut_); + signalImpl(); + awaitImpl(); + pthread_mutex_unlock(&mut_); + } + virtual void signal() { + pthread_mutex_lock(&mut_); + signalImpl(); + pthread_mutex_unlock(&mut_); + } +private: + void awaitImpl() const{ + while(count_!=0) + pthread_cond_wait(&cond_,&mut_); + } + void signalImpl() { + if(count_>0) { + count_--; + pthread_cond_broadcast(&cond_); + } + } + int count_; + mutable pthread_mutex_t mut_; + mutable pthread_cond_t cond_; +}; + +class TestJob { +public: + typedef long JobId; + TestJob():hasRun_(false),startLatch_(0),endLatch_(0) {} + virtual ~TestJob() { + join(); + } + virtual TestJob* clone() const =0; + + virtual void run() =0; + virtual void validate(const char* file, int line) const =0; + + virtual void start(Latch* startLatch=0,Latch* endLatch=0) { + startLatch_=startLatch;endLatch_=endLatch; + hasRun_=true; + pthread_create(&thread_, 0, thread, this); + } + virtual JobId getJobId() const { + return (JobId)thread_; + } + virtual void join() { + if(!hasRun_) + return; + if(!pthread_equal(thread_,pthread_self())) + pthread_join(thread_,0); + else + pthread_detach(thread_); + } +private: + void awaitStart() { + if(startLatch_==0) return; + startLatch_->signalAndWait(); + } + void signalFinished() { + if(endLatch_==0) return; + endLatch_->signal(); + } + static void* thread(void* p) { + TestJob* j=(TestJob*)p; + j->awaitStart(); // wait for the start command + j->run(); + j->signalFinished(); + return 0; + } + bool hasRun_; + Latch* startLatch_; + Latch* endLatch_; + pthread_t thread_; +}; + +class TestJobManager { + typedef std::vector JobList; +public: + TestJobManager(const TestJob& tj,int threadCount=1): + startLatch_(threadCount),endLatch_(threadCount) + { + for(int i=0;istart(&startLatch_,&endLatch_); + } + virtual void startJobsImmediately() { + for(unsigned i=0;istart(0,&endLatch_); + } + virtual void wait() const { + endLatch_.await(); + } + virtual void validateJobs(const char* file, int line) const{ + for(unsigned i=0;ivalidate(file,line); + } +private: + JobList jobs_; + CountDownLatch startLatch_; + CountDownLatch endLatch_; +}; + +#else // THREADED +// single THREADED +class Mutex{ +public: + void acquire(){} + void release(){} +}; +#define synchronized(m) + +#endif // THREADED + +#endif /*THREADINGUTIL_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/tests/Util.cc b/zookeeper-client/zookeeper-client-c/tests/Util.cc new file mode 100644 index 0000000..2b9da84 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/Util.cc @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "Util.h" +#include "string.h" + +const std::string EMPTY_STRING; + +TestConfig globalTestConfig; + +void millisleep(int ms){ + timespec ts; + ts.tv_sec=ms/1000; + ts.tv_nsec=(ms%1000)*1000000; // to nanoseconds + nanosleep(&ts,0); +} + +FILE *openlogfile(const char* testname) { + char name[1024]; + strcpy(name, "TEST-"); + strncpy(name + 5, testname, sizeof(name) - 5); +#ifdef THREADED + strcpy(name + strlen(name), "-mt.txt"); +#else + strcpy(name + strlen(name), "-st.txt"); +#endif + + FILE *logfile = fopen(name, "a"); + + if (logfile == 0) { + fprintf(stderr, "Can't open log file %s!\n", name); + return 0; + } + + return logfile; +} diff --git a/zookeeper-client/zookeeper-client-c/tests/Util.h b/zookeeper-client/zookeeper-client-c/tests/Util.h new file mode 100644 index 0000000..0fd178b --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/Util.h @@ -0,0 +1,137 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +#include +#include +#include + +#include "zookeeper_log.h" + +// number of elements in array +#define COUNTOF(array) sizeof(array)/sizeof(array[0]) + +#define DECLARE_WRAPPER(ret,sym,sig) \ + extern "C" ret __real_##sym sig; \ + extern "C" ret __wrap_##sym sig + +#define CALL_REAL(sym,params) \ + __real_##sym params + +// must include "src/zookeeper_log.h" to be able to use this macro +#define TEST_TRACE(x...) \ + log_message(LOGSTREAM, ZOO_LOG_LEVEL_DEBUG,__LINE__,__func__,x) + +extern const std::string EMPTY_STRING; + +// ***************************************************************************** +// A bit of wizardry to get to the bare type from a reference or a pointer +// to the type +template +struct TypeOp { + typedef T BareT; + typedef T ArgT; +}; + +// partial specialization for reference types +template +struct TypeOp{ + typedef T& ArgT; + typedef typename TypeOp::BareT BareT; +}; + +// partial specialization for pointers +template +struct TypeOp{ + typedef T* ArgT; + typedef typename TypeOp::BareT BareT; +}; + +// ***************************************************************************** +// Container utilities + +template +void putValue(std::map& map,const K& k, const V& v){ + typedef std::map Map; + typename Map::const_iterator it=map.find(k); + if(it==map.end()) + map.insert(typename Map::value_type(k,v)); + else + map[k]=v; +} + +template +bool getValue(const std::map& map,const K& k,V& v){ + typedef std::map Map; + typename Map::const_iterator it=map.find(k); + if(it==map.end()) + return false; + v=it->second; + return true; +} + +// ***************************************************************************** +// misc utils + +// millisecond sleep +void millisleep(int ms); +FILE *openlogfile(const char* name); +// evaluate given predicate until it returns true or the timeout +// (in millis) has expired +template +int ensureCondition(const Predicate& p,int timeout){ + int elapsed=0; + while(!p() && elapsed CmdLineOptList; +public: + typedef CmdLineOptList::const_iterator const_iterator; + TestConfig(){} + ~TestConfig(){} + void addConfigFromCmdLine(int argc, char* argv[]){ + if(argc>=2) + testName_=argv[1]; + for(int i=2; i + +// function to conveniently stream vectors +template +std::ostream& operator<<(std::ostream& os,const std::vector& c){ + typedef std::vector V; + os<<"["; + if(c.size()>0){ + for(typename V::const_iterator it=c.begin();it!=c.end();++it) + os<<*it<<","; + os.seekp(-1,std::ios::cur); + } + os<<"]"; + return os; +} + +#endif // _VECTOR_UTIL_H diff --git a/zookeeper-client/zookeeper-client-c/tests/WatchUtil.h b/zookeeper-client/zookeeper-client-c/tests/WatchUtil.h new file mode 100644 index 0000000..8bc4dbc --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/WatchUtil.h @@ -0,0 +1,152 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef WATCH_UTIL_H_ +#define WATCH_UTIL_H_ + +#include +#include +#include + +using namespace std; + +#include "CollectionUtil.h" +#include "ThreadingUtil.h" + +using namespace Util; + +#ifdef THREADED + static void yield(zhandle_t *zh, int i) + { + sleep(i); + } +#else + static void yield(zhandle_t *zh, int seconds) + { + int fd; + int interest; + int events; + struct timeval tv; + time_t expires = time(0) + seconds; + time_t timeLeft = seconds; + fd_set rfds, wfds, efds; + FD_ZERO(&rfds); + FD_ZERO(&wfds); + FD_ZERO(&efds); + + while(timeLeft >= 0) { + zookeeper_interest(zh, &fd, &interest, &tv); + if (fd != -1) { + if (interest&ZOOKEEPER_READ) { + FD_SET(fd, &rfds); + } else { + FD_CLR(fd, &rfds); + } + if (interest&ZOOKEEPER_WRITE) { + FD_SET(fd, &wfds); + } else { + FD_CLR(fd, &wfds); + } + } else { + fd = 0; + } + FD_SET(0, &rfds); + if (tv.tv_sec > timeLeft) { + tv.tv_sec = timeLeft; + } + select(fd+1, &rfds, &wfds, &efds, &tv); + timeLeft = expires - time(0); + events = 0; + if (FD_ISSET(fd, &rfds)) { + events |= ZOOKEEPER_READ; + } + if (FD_ISSET(fd, &wfds)) { + events |= ZOOKEEPER_WRITE; + } + zookeeper_process(zh, events); + } + } +#endif + +typedef struct evt { + string path; + int type; +} evt_t; + +typedef struct watchCtx { +private: + list events; + watchCtx(const watchCtx&); + watchCtx& operator=(const watchCtx&); +public: + bool connected; + zhandle_t *zh; + Mutex mutex; + + watchCtx() { + connected = false; + zh = 0; + } + ~watchCtx() { + if (zh) { + zookeeper_close(zh); + zh = 0; + } + } + + evt_t getEvent() { + evt_t evt; + mutex.acquire(); + CPPUNIT_ASSERT( events.size() > 0); + evt = events.front(); + events.pop_front(); + mutex.release(); + return evt; + } + + int countEvents() { + int count; + mutex.acquire(); + count = events.size(); + mutex.release(); + return count; + } + + void putEvent(evt_t evt) { + mutex.acquire(); + events.push_back(evt); + mutex.release(); + } + + bool waitForConnected(zhandle_t *zh) { + time_t expires = time(0) + 10; + while(!connected && time(0) < expires) { + yield(zh, 1); + } + return connected; + } + bool waitForDisconnected(zhandle_t *zh) { + time_t expires = time(0) + 15; + while(connected && time(0) < expires) { + yield(zh, 1); + } + return !connected; + } +} watchctx_t; + +#endif /*WATCH_UTIL_H_*/ diff --git a/zookeeper-client/zookeeper-client-c/tests/ZKMocks.cc b/zookeeper-client/zookeeper-client-c/tests/ZKMocks.cc new file mode 100644 index 0000000..d26c295 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/ZKMocks.cc @@ -0,0 +1,544 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include // for htonl +#include + +#include +#include + +#ifdef THREADED +#include "PthreadMocks.h" +#endif +#include "ZKMocks.h" + +using namespace std; + +TestClientId testClientId; +const char* TestClientId::PASSWD="1234567890123456"; + +HandshakeRequest* HandshakeRequest::parse(const std::string& buf) { + unique_ptr req(new HandshakeRequest); + + memcpy(&req->protocolVersion,buf.data(), sizeof(req->protocolVersion)); + req->protocolVersion = htonl(req->protocolVersion); + + int offset=sizeof(req->protocolVersion); + + memcpy(&req->lastZxidSeen,buf.data()+offset,sizeof(req->lastZxidSeen)); + req->lastZxidSeen = zoo_htonll(req->lastZxidSeen); + offset+=sizeof(req->lastZxidSeen); + + memcpy(&req->timeOut,buf.data()+offset,sizeof(req->timeOut)); + req->timeOut = htonl(req->timeOut); + offset+=sizeof(req->timeOut); + + memcpy(&req->sessionId,buf.data()+offset,sizeof(req->sessionId)); + req->sessionId = zoo_htonll(req->sessionId); + offset+=sizeof(req->sessionId); + + memcpy(&req->passwd_len,buf.data()+offset,sizeof(req->passwd_len)); + req->passwd_len = htonl(req->passwd_len); + offset+=sizeof(req->passwd_len); + + memcpy(req->passwd,buf.data()+offset,sizeof(req->passwd)); + offset+=sizeof(req->passwd); + + memcpy(&req->readOnly,buf.data()+offset,sizeof(req->readOnly)); + + if(testClientId.client_id==req->sessionId && + !memcmp(testClientId.passwd,req->passwd,sizeof(req->passwd))) + return req.release(); + // the request didn't match -- may not be a handshake request after all + + return 0; +} + +// ***************************************************************************** +// watcher action implementation +void activeWatcher(zhandle_t *zh, + int type, int state, const char *path,void* ctx) { + + if (zh == 0 || ctx == 0) + return; + + WatcherAction* action = (WatcherAction *)ctx; + + if (type == ZOO_SESSION_EVENT) { + if (state == ZOO_EXPIRED_SESSION_STATE) + action->onSessionExpired(zh); + else if(state == ZOO_CONNECTING_STATE) + action->onConnectionLost(zh); + else if(state == ZOO_CONNECTED_STATE) + action->onConnectionEstablished(zh); + } else if (type == ZOO_CHANGED_EVENT) + action->onNodeValueChanged(zh,path); + else if (type == ZOO_DELETED_EVENT) + action->onNodeDeleted(zh,path); + else if (type == ZOO_CHILD_EVENT) + action->onChildChanged(zh,path); + + // TODO: implement for the rest of the event types + + action->setWatcherTriggered(); +} + +SyncedBoolCondition WatcherAction::isWatcherTriggered() const { + return SyncedBoolCondition(triggered_,mx_); +} + +// a set of async completion signatures + +void asyncCompletion(int rc, ACL_vector *acl,Stat *stat, const void *data){ + assert("Completion data is NULL"&&data); + static_cast((void*)data)->aclCompl(rc,acl,stat); +} + +void asyncCompletion(int rc, const char *value, int len, const Stat *stat, + const void *data) { + assert("Completion data is NULL"&&data); + static_cast((void*)data)->dataCompl(rc,value,len,stat); +} + +void asyncCompletion(int rc, const Stat *stat, const void *data) { + assert("Completion data is NULL"&&data); + static_cast((void*)data)->statCompl(rc,stat); +} + +void asyncCompletion(int rc, const char *value, const void *data) { + assert("Completion data is NULL"&&data); + static_cast((void*)data)->stringCompl(rc,value); +} + +void asyncCompletion(int rc,const String_vector *strings, const void *data) { + assert("Completion data is NULL"&&data); + static_cast((void*)data)->stringsCompl(rc,strings); +} + +void asyncCompletion(int rc, const void *data) { + assert("Completion data is NULL"&&data); + static_cast((void*)data)->voidCompl(rc); +} + +// a predicate implementation +bool IOThreadStopped::operator()() const{ +#ifdef THREADED + adaptor_threads* adaptor=(adaptor_threads*)zh_->adaptor_priv; + return CheckedPthread::isTerminated(adaptor->io); +#else + assert("IOThreadStopped predicate is only for use with THREADED client" && + false); + return false; +#endif +} + +//****************************************************************************** +// +DECLARE_WRAPPER(int,flush_send_queue,(zhandle_t*zh, int timeout)) +{ + if(!Mock_flush_send_queue::mock_) + return CALL_REAL(flush_send_queue,(zh,timeout)); + return Mock_flush_send_queue::mock_->call(zh,timeout); +} + +Mock_flush_send_queue* Mock_flush_send_queue::mock_=0; + +//****************************************************************************** +// +DECLARE_WRAPPER(int32_t,get_xid,()) +{ + if(!Mock_get_xid::mock_) + return CALL_REAL(get_xid,()); + return Mock_get_xid::mock_->call(); +} + +Mock_get_xid* Mock_get_xid::mock_=0; + +//****************************************************************************** +// activateWatcher mock + +DECLARE_WRAPPER(void,activateWatcher,(zhandle_t *zh, watcher_registration_t* reg, int rc)) +{ + if(!Mock_activateWatcher::mock_){ + CALL_REAL(activateWatcher,(zh, reg,rc)); + }else{ + Mock_activateWatcher::mock_->call(zh, reg,rc); + } +} +Mock_activateWatcher* Mock_activateWatcher::mock_=0; + +class ActivateWatcherWrapper: public Mock_activateWatcher{ +public: + ActivateWatcherWrapper():ctx_(0),activated_(false){} + + virtual void call(zhandle_t *zh, watcher_registration_t* reg, int rc){ + CALL_REAL(activateWatcher,(zh, reg,rc)); + synchronized(mx_); + if(reg->context==ctx_){ + activated_=true; + ctx_=0; + } + } + + void setContext(void* ctx){ + synchronized(mx_); + ctx_=ctx; + activated_=false; + } + + SyncedBoolCondition isActivated() const{ + return SyncedBoolCondition(activated_,mx_); + } + mutable Mutex mx_; + void* ctx_; + bool activated_; +}; + +WatcherActivationTracker::WatcherActivationTracker(): + wrapper_(new ActivateWatcherWrapper) +{ +} + +WatcherActivationTracker::~WatcherActivationTracker(){ + delete wrapper_; +} + +void WatcherActivationTracker::track(void* ctx){ + wrapper_->setContext(ctx); +} + +SyncedBoolCondition WatcherActivationTracker::isWatcherActivated() const{ + return wrapper_->isActivated(); +} + +//****************************************************************************** +// +DECLARE_WRAPPER(void,deliverWatchers,(zhandle_t* zh,int type,int state, const char* path, watcher_object_list_t **list)) +{ + if(!Mock_deliverWatchers::mock_){ + CALL_REAL(deliverWatchers,(zh,type,state,path, list)); + }else{ + Mock_deliverWatchers::mock_->call(zh,type,state,path, list); + } +} + +Mock_deliverWatchers* Mock_deliverWatchers::mock_=0; + +struct RefCounterValue{ + RefCounterValue(zhandle_t* const& zh,int32_t expectedCounter,Mutex& mx): + zh_(zh),expectedCounter_(expectedCounter),mx_(mx){} + bool operator()() const{ + { + synchronized(mx_); + if(zh_==0) + return false; + } + return inc_ref_counter(zh_,0)==expectedCounter_; + } + zhandle_t* const& zh_; + int32_t expectedCounter_; + Mutex& mx_; +}; + + +class DeliverWatchersWrapper: public Mock_deliverWatchers{ +public: + DeliverWatchersWrapper(int type,int state,bool terminate): + type_(type),state_(state), + allDelivered_(false),terminate_(terminate),zh_(0),deliveryCounter_(0){} + virtual void call(zhandle_t* zh, int type, int state, + const char* path, watcher_object_list **list) { + { + synchronized(mx_); + zh_=zh; + allDelivered_=false; + } + CALL_REAL(deliverWatchers,(zh,type,state,path, list)); + if(type_==type && state_==state){ + if(terminate_){ + // prevent zhandle_t from being prematurely distroyed; + // this will also ensure that zookeeper_close() cleanups the + // thread resources by calling finish_adaptor() + inc_ref_counter(zh,1); + terminateZookeeperThreads(zh); + } + synchronized(mx_); + allDelivered_=true; + deliveryCounter_++; + } + } + SyncedBoolCondition isDelivered() const{ + if(terminate_){ + int i=ensureCondition(RefCounterValue(zh_,1,mx_),1000); + assert(i<1000); + } + return SyncedBoolCondition(allDelivered_,mx_); + } + void resetDeliveryCounter(){ + synchronized(mx_); + deliveryCounter_=0; + } + SyncedIntegerEqual deliveryCounterEquals(int expected) const{ + if(terminate_){ + int i=ensureCondition(RefCounterValue(zh_,1,mx_),1000); + assert(i<1000); + } + return SyncedIntegerEqual(deliveryCounter_,expected,mx_); + } + int type_; + int state_; + mutable Mutex mx_; + bool allDelivered_; + bool terminate_; + zhandle_t* zh_; + int deliveryCounter_; +}; + +WatcherDeliveryTracker::WatcherDeliveryTracker( + int type,int state,bool terminateCompletionThread): + deliveryWrapper_(new DeliverWatchersWrapper( + type,state,terminateCompletionThread)){ +} + +WatcherDeliveryTracker::~WatcherDeliveryTracker(){ + delete deliveryWrapper_; +} + +SyncedBoolCondition WatcherDeliveryTracker::isWatcherProcessingCompleted() const { + return deliveryWrapper_->isDelivered(); +} + +void WatcherDeliveryTracker::resetDeliveryCounter(){ + deliveryWrapper_->resetDeliveryCounter(); +} + +SyncedIntegerEqual WatcherDeliveryTracker::deliveryCounterEquals(int expected) const { + return deliveryWrapper_->deliveryCounterEquals(expected); +} + +//****************************************************************************** +// +string HandshakeResponse::toString() const { + string buf; + int32_t tmp=htonl(protocolVersion); + buf.append((char*)&tmp,sizeof(tmp)); + tmp=htonl(timeOut); + buf.append((char*)&tmp,sizeof(tmp)); + int64_t tmp64=zoo_htonll(sessionId); + buf.append((char*)&tmp64,sizeof(sessionId)); + tmp=htonl(passwd_len); + buf.append((char*)&tmp,sizeof(tmp)); + buf.append(passwd,sizeof(passwd)); + buf.append(&readOnly,sizeof(readOnly)); + // finally set the buffer length + tmp=htonl(buf.size()+sizeof(tmp)); + buf.insert(0,(char*)&tmp, sizeof(tmp)); + return buf; +} + +string ZooGetResponse::toString() const{ + oarchive* oa=create_buffer_oarchive(); + + ReplyHeader h = {xid_,1,ZOK}; + serialize_ReplyHeader(oa, "hdr", &h); + + GetDataResponse resp; + char buf[1024]; + assert("GetDataResponse is too long"&&data_.size()<=sizeof(buf)); + resp.data.len=data_.size(); + resp.data.buff=buf; + data_.copy(resp.data.buff, data_.size()); + resp.stat=stat_; + serialize_GetDataResponse(oa, "reply", &resp); + int32_t len=htonl(get_buffer_len(oa)); + string res((char*)&len,sizeof(len)); + res.append(get_buffer(oa),get_buffer_len(oa)); + + close_buffer_oarchive(&oa,1); + return res; +} + +string ZooStatResponse::toString() const{ + oarchive* oa=create_buffer_oarchive(); + + ReplyHeader h = {xid_,1,rc_}; + serialize_ReplyHeader(oa, "hdr", &h); + + SetDataResponse resp; + resp.stat=stat_; + serialize_SetDataResponse(oa, "reply", &resp); + int32_t len=htonl(get_buffer_len(oa)); + string res((char*)&len,sizeof(len)); + res.append(get_buffer(oa),get_buffer_len(oa)); + + close_buffer_oarchive(&oa,1); + return res; +} + +string ZooGetChildrenResponse::toString() const{ + oarchive* oa=create_buffer_oarchive(); + + ReplyHeader h = {xid_,1,rc_}; + serialize_ReplyHeader(oa, "hdr", &h); + + GetChildrenResponse resp; + // populate the string vector + allocate_String_vector(&resp.children,strings_.size()); + for(int i=0;i<(int)strings_.size();++i) + resp.children.data[i]=strdup(strings_[i].c_str()); + serialize_GetChildrenResponse(oa, "reply", &resp); + deallocate_GetChildrenResponse(&resp); + + int32_t len=htonl(get_buffer_len(oa)); + string res((char*)&len,sizeof(len)); + res.append(get_buffer(oa),get_buffer_len(oa)); + + close_buffer_oarchive(&oa,1); + return res; +} + +string ZNodeEvent::toString() const{ + oarchive* oa=create_buffer_oarchive(); + struct WatcherEvent evt = {type_,0,(char*)path_.c_str()}; + struct ReplyHeader h = {WATCHER_EVENT_XID,0,ZOK }; + + serialize_ReplyHeader(oa, "hdr", &h); + serialize_WatcherEvent(oa, "event", &evt); + + int32_t len=htonl(get_buffer_len(oa)); + string res((char*)&len,sizeof(len)); + res.append(get_buffer(oa),get_buffer_len(oa)); + + close_buffer_oarchive(&oa,1); + return res; +} + +string PingResponse::toString() const{ + oarchive* oa=create_buffer_oarchive(); + + ReplyHeader h = {PING_XID,1,ZOK}; + serialize_ReplyHeader(oa, "hdr", &h); + + int32_t len=htonl(get_buffer_len(oa)); + string res((char*)&len,sizeof(len)); + res.append(get_buffer(oa),get_buffer_len(oa)); + + close_buffer_oarchive(&oa,1); + return res; +} + +//****************************************************************************** +// Zookeeper server simulator +// +bool ZookeeperServer::hasMoreRecv() const{ + return recvHasMore.get()!=0 || connectionLost; +} + +ssize_t ZookeeperServer::callRecv(int s,void *buf,size_t len,int flags){ + if(connectionLost){ + recvReturnBuffer.erase(); + return 0; + } + // done transmitting the current buffer? + if(recvReturnBuffer.size()==0){ + synchronized(recvQMx); + if(recvQueue.empty()){ + recvErrno=EAGAIN; + return Mock_socket::callRecv(s,buf,len,flags); + } + --recvHasMore; + Element& el=recvQueue.front(); + if(el.first!=0){ + recvReturnBuffer=el.first->toString(); + delete el.first; + } + recvErrno=el.second; + recvQueue.pop_front(); + } + return Mock_socket::callRecv(s,buf,len,flags); +} + +void ZookeeperServer::onMessageReceived(const RequestHeader& rh, iarchive* ia){ + // no-op by default +} + +void ZookeeperServer::notifyBufferSent(const std::string& buffer){ + if(HandshakeRequest::isValid(buffer)){ + // could be a connect request + unique_ptr req(HandshakeRequest::parse(buffer)); + if(req.get()!=0){ + // handle the handshake + int64_t sessId=sessionExpired?req->sessionId+1:req->sessionId; + sessionExpired=false; + addRecvResponse(new HandshakeResponse(sessId)); + return; + } + // not a connect request -- fall thru + } + // parse the buffer to extract the request type and its xid + iarchive *ia=create_buffer_iarchive((char*)buffer.data(), buffer.size()); + RequestHeader rh; + deserialize_RequestHeader(ia,"hdr",&rh); + // notify the "server" a client request has arrived + if (rh.xid == -8) { + Element e = Element(new ZooStatResponse,0); + e.first->setXID(-8); + addRecvResponse(e); + close_buffer_iarchive(&ia); + return; + } else { + onMessageReceived(rh,ia); + } + close_buffer_iarchive(&ia); + if(rh.type==ZOO_CLOSE_OP){ + ++closeSent; + return; // no reply for close requests + } + // get the next response from the response queue and append it to the + // receive list + Element e; + { + synchronized(respQMx); + if(respQueue.empty()) + return; + e=respQueue.front(); + respQueue.pop_front(); + } + e.first->setXID(rh.xid); + addRecvResponse(e); +} + +void forceConnected(zhandle_t* zh){ + // simulate connected state + zh->state=ZOO_CONNECTED_STATE; + + // Simulate we're connected to the first host in our host list + zh->fd->sock=ZookeeperServer::FD; + assert(zh->addrs.count > 0); + zh->addr_cur = zh->addrs.data[0]; + zh->addrs.next++; + + zh->input_buffer=0; + gettimeofday(&zh->last_recv,0); + gettimeofday(&zh->last_send,0); +} + +void terminateZookeeperThreads(zhandle_t* zh){ + // this will cause the zookeeper threads to terminate + zh->close_requested=1; +} diff --git a/zookeeper-client/zookeeper-client-c/tests/ZKMocks.h b/zookeeper-client/zookeeper-client-c/tests/ZKMocks.h new file mode 100644 index 0000000..2717ded --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/ZKMocks.h @@ -0,0 +1,511 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ZKMOCKS_H_ +#define ZKMOCKS_H_ + +#include +#include "src/zk_adaptor.h" + +#include "Util.h" +#include "LibCMocks.h" +#include "MocksBase.h" + +// ***************************************************************************** +// sets internal zhandle_t members to certain values to simulate the client +// connected state. This function should only be used with the single-threaded +// Async API tests! +void forceConnected(zhandle_t* zh); + +/** + * Gracefully terminates zookeeper I/O and completion threads. + */ +void terminateZookeeperThreads(zhandle_t* zh); + +// ***************************************************************************** +// Abstract watcher action +struct SyncedBoolCondition; + +class WatcherAction{ +public: + WatcherAction():triggered_(false){} + virtual ~WatcherAction(){} + + virtual void onSessionExpired(zhandle_t*){} + virtual void onConnectionEstablished(zhandle_t*){} + virtual void onConnectionLost(zhandle_t*){} + virtual void onNodeValueChanged(zhandle_t*,const char* path){} + virtual void onNodeDeleted(zhandle_t*,const char* path){} + virtual void onChildChanged(zhandle_t*,const char* path){} + + SyncedBoolCondition isWatcherTriggered() const; + void setWatcherTriggered(){ + synchronized(mx_); + triggered_=true; + } + +protected: + mutable Mutex mx_; + bool triggered_; +}; +// zh->context is a pointer to a WatcherAction instance +// based on the event type and state, the watcher calls a specific watcher +// action method +void activeWatcher(zhandle_t *zh, int type, int state, const char *path,void* ctx); + +// ***************************************************************************** +// a set of async completion signatures +class AsyncCompletion{ +public: + virtual ~AsyncCompletion(){} + virtual void aclCompl(int rc, ACL_vector *acl,Stat *stat){} + virtual void dataCompl(int rc, const char *value, int len, const Stat *stat){} + virtual void statCompl(int rc, const Stat *stat){} + virtual void stringCompl(int rc, const char *value){} + virtual void stringsCompl(int rc,const String_vector *strings){} + virtual void voidCompl(int rc){} +}; +void asyncCompletion(int rc, ACL_vector *acl,Stat *stat, const void *data); +void asyncCompletion(int rc, const char *value, int len, const Stat *stat, + const void *data); +void asyncCompletion(int rc, const Stat *stat, const void *data); +void asyncCompletion(int rc, const char *value, const void *data); +void asyncCompletion(int rc,const String_vector *strings, const void *data); +void asyncCompletion(int rc, const void *data); + +// ***************************************************************************** +// some common predicates to use with ensureCondition(): +// checks if the connection is established +struct ClientConnected{ + ClientConnected(zhandle_t* zh):zh_(zh){} + bool operator()() const{ + return zoo_state(zh_)==ZOO_CONNECTED_STATE; + } + zhandle_t* zh_; +}; +// check in the session expired +struct SessionExpired{ + SessionExpired(zhandle_t* zh):zh_(zh){} + bool operator()() const{ + return zoo_state(zh_)==ZOO_EXPIRED_SESSION_STATE; + } + zhandle_t* zh_; +}; +// checks if the IO thread has stopped; CheckedPthread must be active +struct IOThreadStopped{ + IOThreadStopped(zhandle_t* zh):zh_(zh){} + bool operator()() const; + zhandle_t* zh_; +}; + +// a synchronized boolean condition +struct SyncedBoolCondition{ + SyncedBoolCondition(const bool& cond,Mutex& mx):cond_(cond),mx_(mx){} + bool operator()() const{ + synchronized(mx_); + return cond_; + } + const bool& cond_; + Mutex& mx_; +}; + +// a synchronized integer comparison +struct SyncedIntegerEqual{ + SyncedIntegerEqual(const int& cond,int expected,Mutex& mx): + cond_(cond),expected_(expected),mx_(mx){} + bool operator()() const{ + synchronized(mx_); + return cond_==expected_; + } + const int& cond_; + const int expected_; + Mutex& mx_; +}; + +// ***************************************************************************** +// make sure to call zookeeper_close() even in presence of exceptions +struct CloseFinally{ + CloseFinally(zhandle_t** zh):zh_(zh){} + ~CloseFinally(){ + execute(); + } + int execute(){ + if(zh_==0)return ZOK; + zhandle_t* lzh=*zh_; + *zh_=0; + disarm(); + return zookeeper_close(lzh); + } + void disarm(){zh_=0;} + zhandle_t ** zh_; +}; + +struct TestClientId: clientid_t{ + static const int SESSION_ID=123456789; + static const char* PASSWD; + TestClientId(){ + client_id=SESSION_ID; + memcpy(passwd,PASSWD,sizeof(passwd)); + } +}; + +// ***************************************************************************** +// special client id recongnized by the ZK server simulator +extern TestClientId testClientId; +#define TEST_CLIENT_ID &testClientId + +// ***************************************************************************** +// +struct HandshakeRequest: public connect_req +{ + static HandshakeRequest* parse(const std::string& buf); + static bool isValid(const std::string& buf){ + // this is just quick and dirty check before we go and parse the request + return buf.size()==HANDSHAKE_REQ_SIZE; + } +}; + +// ***************************************************************************** +// flush_send_queue +class Mock_flush_send_queue: public Mock +{ +public: + Mock_flush_send_queue():counter(0),callReturns(ZOK){mock_=this;} + ~Mock_flush_send_queue(){mock_=0;} + + int counter; + int callReturns; + virtual int call(zhandle_t* zh, int timeout){ + counter++; + return callReturns; + } + + static Mock_flush_send_queue* mock_; +}; + +// ***************************************************************************** +// get_xid +class Mock_get_xid: public Mock +{ +public: + static const int32_t XID=123456; + Mock_get_xid(int retValue=XID):callReturns(retValue){mock_=this;} + ~Mock_get_xid(){mock_=0;} + + int callReturns; + virtual int call(){ + return callReturns; + } + + static Mock_get_xid* mock_; +}; + +// ***************************************************************************** +// activateWatcher +class Mock_activateWatcher: public Mock{ +public: + Mock_activateWatcher(){mock_=this;} + virtual ~Mock_activateWatcher(){mock_=0;} + + virtual void call(zhandle_t *zh, watcher_registration_t* reg, int rc){} + static Mock_activateWatcher* mock_; +}; + +class ActivateWatcherWrapper; +class WatcherActivationTracker{ +public: + WatcherActivationTracker(); + ~WatcherActivationTracker(); + + void track(void* ctx); + SyncedBoolCondition isWatcherActivated() const; +private: + ActivateWatcherWrapper* wrapper_; +}; + +// ***************************************************************************** +// deliverWatchers +class Mock_deliverWatchers: public Mock{ +public: + Mock_deliverWatchers(){mock_=this;} + virtual ~Mock_deliverWatchers(){mock_=0;} + + virtual void call(zhandle_t* zh,int type,int state, const char* path, watcher_object_list **){} + static Mock_deliverWatchers* mock_; +}; + +class DeliverWatchersWrapper; +class WatcherDeliveryTracker{ +public: + // filters deliveries by state and type + WatcherDeliveryTracker(int type,int state,bool terminateCompletionThread=true); + ~WatcherDeliveryTracker(); + + // if the thread termination requested (see the ctor params) + // this function will wait for the I/O and completion threads to + // terminate before returning a SyncBoolCondition instance + SyncedBoolCondition isWatcherProcessingCompleted() const; + void resetDeliveryCounter(); + SyncedIntegerEqual deliveryCounterEquals(int expected) const; +private: + DeliverWatchersWrapper* deliveryWrapper_; +}; + +// ***************************************************************************** +// a zookeeper Stat wrapper +struct NodeStat: public Stat +{ + NodeStat(){ + czxid=0; + mzxid=0; + ctime=0; + mtime=0; + version=1; + cversion=0; + aversion=0; + ephemeralOwner=0; + } + NodeStat(const Stat& other){ + memcpy(this,&other,sizeof(*this)); + } +}; + +// ***************************************************************************** +// Abstract server Response +class Response +{ +public: + virtual ~Response(){} + + virtual void setXID(int32_t xid){} + // this method is used by the ZookeeperServer class to serialize + // the instance of Response + virtual std::string toString() const =0; +}; + +// ***************************************************************************** +// Handshake response +class HandshakeResponse: public Response +{ +public: + HandshakeResponse(int64_t sessId=1): + protocolVersion(1),timeOut(10000),sessionId(sessId), + passwd_len(sizeof(passwd)),readOnly(0) + { + memcpy(passwd,"1234567890123456",sizeof(passwd)); + } + int32_t protocolVersion; + int32_t timeOut; + int64_t sessionId; + int32_t passwd_len; + char passwd[16]; + char readOnly; + virtual std::string toString() const ; +}; + +// zoo_get() response +class ZooGetResponse: public Response +{ +public: + ZooGetResponse(const char* data, int len,int32_t xid=0,int rc=ZOK,const Stat& stat=NodeStat()) + :xid_(xid),data_(data,len),rc_(rc),stat_(stat) + { + } + virtual std::string toString() const; + virtual void setXID(int32_t xid) {xid_=xid;} + +private: + int32_t xid_; + std::string data_; + int rc_; + Stat stat_; +}; + +// zoo_exists(), zoo_set() response +class ZooStatResponse: public Response +{ +public: + ZooStatResponse(int32_t xid=0,int rc=ZOK,const Stat& stat=NodeStat()) + :xid_(xid),rc_(rc),stat_(stat) + { + } + virtual std::string toString() const; + virtual void setXID(int32_t xid) {xid_=xid;} + +private: + int32_t xid_; + int rc_; + Stat stat_; +}; + +// zoo_get_children() +class ZooGetChildrenResponse: public Response +{ +public: + typedef std::vector StringVector; + ZooGetChildrenResponse(const StringVector& v,int rc=ZOK): + xid_(0),strings_(v),rc_(rc) + { + } + + virtual std::string toString() const; + virtual void setXID(int32_t xid) {xid_=xid;} + + int32_t xid_; + StringVector strings_; + int rc_; +}; + +// PING response +class PingResponse: public Response +{ +public: + virtual std::string toString() const; +}; + +// watcher znode event +class ZNodeEvent: public Response +{ +public: + ZNodeEvent(int type,const char* path):type_(type),path_(path){} + + virtual std::string toString() const; + +private: + int type_; + std::string path_; +}; + +// **************************************************************************** +// Zookeeper server simulator + +class ZookeeperServer: public Mock_socket +{ +public: + ZookeeperServer(): + serverDownSkipCount_(-1),sessionExpired(false),connectionLost(false) + { + connectReturns=-1; + connectErrno=EWOULDBLOCK; + } + virtual ~ZookeeperServer(){ + clearRecvQueue(); + clearRespQueue(); + } + virtual int callClose(int fd){ + if(fd!=FD) + return LIBC_SYMBOLS.close(fd); + clearRecvQueue(); + clearRespQueue(); + return Mock_socket::callClose(fd); + } + // connection handling + // what to do when the handshake request comes in? + int serverDownSkipCount_; + // this will cause getsockopt(zh->fd,SOL_SOCKET,SO_ERROR,&error,&len) return + // a failure after skipCount dropped to zero, thus simulating a server down + // condition + // passing skipCount==-1 will make every connect attempt succeed + void setServerDown(int skipCount=0){ + serverDownSkipCount_=skipCount; + optvalSO_ERROR=0; + } + virtual void setSO_ERROR(void *optval,socklen_t len){ + if(serverDownSkipCount_!=-1){ + if(serverDownSkipCount_==0) + optvalSO_ERROR=ECONNREFUSED; + else + serverDownSkipCount_--; + } + Mock_socket::setSO_ERROR(optval,len); + } + + // this is a trigger that gets reset back to false + // a connect request will return a non-matching session id thus causing + // the client throw SESSION_EXPIRED + volatile bool sessionExpired; + void returnSessionExpired(){ sessionExpired=true; } + + // this is a one shot trigger that gets reset back to false + // next recv call will return 0 length, thus simulating a connecton loss + volatile bool connectionLost; + void setConnectionLost() {connectionLost=true;} + + // recv + // this queue is used for server responses: client's recv() system call + // returns next available message from this queue + typedef std::pair Element; + typedef std::deque ResponseList; + ResponseList recvQueue; + mutable Mutex recvQMx; + AtomicInt recvHasMore; + ZookeeperServer& addRecvResponse(Response* resp, int errnum=0){ + synchronized(recvQMx); + recvQueue.push_back(Element(resp,errnum)); + ++recvHasMore; + return *this; + } + ZookeeperServer& addRecvResponse(int errnum){ + synchronized(recvQMx); + recvQueue.push_back(Element(0,errnum)); + ++recvHasMore; + return *this; + } + ZookeeperServer& addRecvResponse(const Element& e){ + synchronized(recvQMx); + recvQueue.push_back(e); + ++recvHasMore; + return *this; + } + void clearRecvQueue(){ + synchronized(recvQMx); + recvHasMore=0; + for(unsigned i=0; i +#include +#include +#include +#include +#include +#include +#include + +ZooKeeperQuorumServer:: +ZooKeeperQuorumServer(uint32_t id, uint32_t numServers, std::string config, std::string env) : + id_(id), + env_(env), + numServers_(numServers) { + const char* root = getenv("ZKROOT"); + if (root == NULL) { + assert(!"Environment variable 'ZKROOT' is not set"); + } + root_ = root; + createConfigFile(config); + createDataDirectory(); + start(); +} + +ZooKeeperQuorumServer:: +~ZooKeeperQuorumServer() { + stop(); +} + +std::string ZooKeeperQuorumServer:: +getHostPort() { + std::stringstream ss; + ss << "localhost:" << getClientPort(); + return ss.str(); +} + +uint32_t ZooKeeperQuorumServer:: +getClientPort() { + return CLIENT_PORT_BASE + id_; +} + +void ZooKeeperQuorumServer:: +start() { + std::string command = root_ + "/bin/zkServer.sh start " + + getConfigFileName(); + if (!env_.empty()) { + command = env_ + " " + command; + } + assert(system(command.c_str()) == 0); +} + +void ZooKeeperQuorumServer:: +stop() { + std::string command = root_ + "/bin/zkServer.sh stop " + + getConfigFileName(); + assert(system(command.c_str()) == 0); +} + +std::string ZooKeeperQuorumServer:: +getMode() { + char buf[1024]; + std::string result; + std::string command = root_ + "/bin/zkServer.sh status " + + getConfigFileName(); + FILE* output = popen(command.c_str(), "r"); + do { + if (fgets(buf, 1024, output) != NULL) { + result += buf; + } + } while (!feof(output)); + pclose(output); + if (result.find("Mode: leader") != std::string::npos) { + return "leader"; + } else if (result.find("Mode: follower") != std::string::npos) { + return "follower"; + } else { + printf("%s\n", result.c_str()); + return ""; + } +} + +bool ZooKeeperQuorumServer:: +isLeader() { + return getMode() == "leader"; +} + +bool ZooKeeperQuorumServer:: +isFollower() { + return getMode() == "follower"; +} + +void ZooKeeperQuorumServer:: +createConfigFile(std::string config) { + std::string command = "mkdir -p " + root_ + "/build/test/test-cppunit/conf"; + assert(system(command.c_str()) == 0); + std::ofstream confFile; + std::stringstream ss; + ss << id_ << ".conf"; + std::string fileName = root_ + "/build/test/test-cppunit/conf/" + ss.str(); + confFile.open(fileName.c_str()); + confFile << "tickTime=2000\n"; + confFile << "clientPort=" << getClientPort() << "\n"; + confFile << "initLimit=5\n"; + confFile << "syncLimit=2\n"; + confFile << "dataDir=" << getDataDirectory() << "\n"; + for (uint32_t i = 0; i < numServers_; i++) { + confFile << getServerString(i) << "\n"; + } + // Append additional config, if any. + if (!config.empty()) { + confFile << config << std::endl; + } + confFile.close(); +} + +std::string ZooKeeperQuorumServer:: +getConfigFileName() { + std::stringstream ss; + ss << id_ << ".conf"; + return root_ + "/build/test/test-cppunit/conf/" + ss.str(); +} + +void ZooKeeperQuorumServer:: +createDataDirectory() { + std::string dataDirectory = getDataDirectory(); + std::string command = "rm -rf " + dataDirectory; + assert(system(command.c_str()) == 0); + command = "mkdir -p " + dataDirectory; + assert(system(command.c_str()) == 0); + std::ofstream myidFile; + std::string fileName = dataDirectory + "/myid"; + myidFile.open(fileName.c_str()); + myidFile << id_ << "\n"; + myidFile.close(); + setenv("ZOO_LOG_DIR", dataDirectory.c_str(), true); +} + +std::string ZooKeeperQuorumServer:: +getServerString() { + return getServerString(id_); +} + +std::string ZooKeeperQuorumServer:: +getServerString(uint32_t id) { + std::stringstream ss; + ss << "server." << id << "=localhost:" << SERVER_PORT_BASE + id << + ":" << ELECTION_PORT_BASE + id << ":participant;localhost:" << + CLIENT_PORT_BASE + id; + return ss.str(); +} + +std::string ZooKeeperQuorumServer:: +getDataDirectory() { + std::stringstream ss; + ss << "data" << id_; + return root_ + "/build/test/test-cppunit/" + ss.str(); +} + +std::vector ZooKeeperQuorumServer:: +getCluster(uint32_t numServers) { + std::vector cluster; + for (uint32_t i = 0; i < numServers; i++) { + cluster.push_back(new ZooKeeperQuorumServer(i, numServers)); + } + + // Wait until all the servers start, and fail if they don't start within 10 + // seconds. + for (uint32_t i = 0; i < 10; i++) { + uint32_t j = 0; + for (; j < cluster.size(); j++) { + if (cluster[j]->getMode() == "") { + // The server hasn't started. + sleep(1); + break; + } + } + if (j == cluster.size()) { + return cluster; + } + } + assert(!"The cluster didn't start for 10 seconds"); +} + +std::vector ZooKeeperQuorumServer:: +getCluster(uint32_t numServers, ZooKeeperQuorumServer::tConfigPairs configs, std::string env) { + std::vector cluster; + std::string config; + for (ZooKeeperQuorumServer::tConfigPairs::const_iterator iter = configs.begin(); iter != configs.end(); ++iter) { + std::pair pair = *iter; + config += (pair.first + "=" + pair.second + "\n"); + } + for (uint32_t i = 0; i < numServers; i++) { + cluster.push_back(new ZooKeeperQuorumServer(i, numServers, config, env)); + } + + // Wait until all the servers start, and fail if they don't start within 10 + // seconds. + for (uint32_t i = 0; i < 10; i++) { + uint32_t j = 0; + for (; j < cluster.size(); j++) { + if (cluster[j]->getMode() == "") { + // The server hasn't started. + sleep(1); + break; + } + } + if (j == cluster.size()) { + return cluster; + } + } + assert(!"The cluster didn't start for 10 seconds"); +} diff --git a/zookeeper-client/zookeeper-client-c/tests/ZooKeeperQuorumServer.h b/zookeeper-client/zookeeper-client-c/tests/ZooKeeperQuorumServer.h new file mode 100644 index 0000000..18a8d63 --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/ZooKeeperQuorumServer.h @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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. + */ +#ifndef ZOOKEEPER_QUORUM_SERVER_H +#define ZOOKEEPER_QUORUM_SERVER_H + +#include +#include +#include +#include + +class ZooKeeperQuorumServer { + public: + ~ZooKeeperQuorumServer(); + typedef std::vector > tConfigPairs; + static std::vector getCluster(uint32_t numServers); + static std::vector getCluster(uint32_t numServers, + tConfigPairs configs, /* Additional config options as a list of key/value pairs. */ + std::string env /* Additional environment variables when starting zkServer.sh. */); + std::string getHostPort(); + uint32_t getClientPort(); + void start(); + void stop(); + bool isLeader(); + bool isFollower(); + std::string getServerString(); + + private: + ZooKeeperQuorumServer(); + ZooKeeperQuorumServer(uint32_t id, uint32_t numServers, std::string config = "", + std::string env = ""); + ZooKeeperQuorumServer(const ZooKeeperQuorumServer& that); + const ZooKeeperQuorumServer& operator=(const ZooKeeperQuorumServer& that); + void createConfigFile(std::string config = ""); + std::string getConfigFileName(); + void createDataDirectory(); + std::string getDataDirectory(); + static std::string getServerString(uint32_t id); + std::string getMode(); + + static const uint32_t SERVER_PORT_BASE = 2000; + static const uint32_t ELECTION_PORT_BASE = 3000; + static const uint32_t CLIENT_PORT_BASE = 4000; + + uint32_t id_; + std::string env_; + uint32_t numServers_; + std::string root_; +}; + +#endif // ZOOKEEPER_QUORUM_SERVER_H diff --git a/zookeeper-client/zookeeper-client-c/tests/wrappers-mt.opt b/zookeeper-client/zookeeper-client-c/tests/wrappers-mt.opt new file mode 100644 index 0000000..01046fb --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/wrappers-mt.opt @@ -0,0 +1,3 @@ +-Wl,--wrap -Wl,pthread_mutex_lock +-Wl,--wrap -Wl,pthread_mutex_trylock +-Wl,--wrap -Wl,pthread_mutex_unlock diff --git a/zookeeper-client/zookeeper-client-c/tests/wrappers.opt b/zookeeper-client/zookeeper-client-c/tests/wrappers.opt new file mode 100644 index 0000000..bce192f --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/wrappers.opt @@ -0,0 +1,7 @@ +-Wl,--wrap -Wl,calloc +-Wl,--wrap -Wl,free +-Wl,--wrap -Wl,flush_send_queue +-Wl,--wrap -Wl,get_xid +-Wl,--wrap -Wl,deliverWatchers +-Wl,--wrap -Wl,activateWatcher +-Wl,--wrap -Wl,realloc diff --git a/zookeeper-client/zookeeper-client-c/tests/zkServer.sh b/zookeeper-client/zookeeper-client-c/tests/zkServer.sh new file mode 100644 index 0000000..432786c --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/zkServer.sh @@ -0,0 +1,247 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# This is the port where zookeeper server runs on. +ZOOPORT=${ZOOPORT:-"22181"} + +# Some tests are setting the maxClientConnections. When it is not set, we fallback to default 100 +ZKMAXCNXNS=${ZKMAXCNXNS:-"100"} + +EXTRA_JVM_ARGS=${EXTRA_JVM_ARGS:-""} + +if [ "x$1" == "x" ] +then + echo "USAGE: $0 startClean|start|startCleanReadOnly|startRequireSASLAuth|stop" + exit 2 +fi + + + + +# ===== +# ===== cleanup old executions +# ===== + +case "`uname`" in + CYGWIN*) cygwin=true ;; + *) cygwin=false ;; +esac + + +if $cygwin +then + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +# Make sure nothing is left over from before +if [ -r "/tmp/zk.pid" ] +then +pid=`cat /tmp/zk.pid` +$KILL -9 $pid +rm -f /tmp/zk.pid +fi + +if [ -r "${base_dir}/build/tmp/zk.pid" ] +then +pid=`cat "${base_dir}/build/tmp/zk.pid"` +$KILL -9 $pid +rm -f "${base_dir}/build/tmp/zk.pid" +fi + +# [ZOOKEEPER-820] If lsof command is present, look for a process listening +# on ZOOPORT and kill it. +which lsof &> /dev/null +if [ $? -eq 0 ] +then + pid=`lsof -i :$ZOOPORT | grep LISTEN | awk '{print $2}'` + if [ -n "$pid" ] + then + $KILL -9 $pid + fi +fi + + + +# ===== +# ===== build classpath +# ===== + +if [ "x${base_dir}" == "x" ] +then +zk_base="../../../" +else +zk_base="${base_dir}" +fi + +CLASSPATH="$CLASSPATH:${zk_base}/build/classes" +CLASSPATH="$CLASSPATH:${zk_base}/conf" +CLASSPATH="$CLASSPATH:${zk_base}/zookeeper-server/target/classes" + +for i in "${zk_base}"/build/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +for d in "${zk_base}"/zookeeper-server/target/lib/*.jar +do + CLASSPATH="$d:$CLASSPATH" +done + +for i in "${zk_base}"/zookeeper-server/src/main/resource/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +CLASSPATH="$CLASSPATH:${CLOVER_HOME}/lib/clover*.jar" + +if $cygwin +then + CLASSPATH=`cygpath -wp "$CLASSPATH"` +fi + + + +# ===== +# ===== initialize JVM arguments +# ===== + +PROPERTIES="$EXTRA_JVM_ARGS -Dzookeeper.extendedTypesEnabled=true -Dznode.container.checkIntervalMs=100" +if [ "x$1" == "xstartRequireSASLAuth" ] +then + PROPERTIES="-Dzookeeper.sessionRequireClientSASLAuth=true $PROPERTIES" +fi +if [ "x$1" == "xstartCleanReadOnly" ] +then + PROPERTIES="-Dreadonlymode.enabled=true $PROPERTIES" +fi + + + +# ===== +# ===== initialize data and test directories +# ===== + +if [ "x${base_dir}" == "x" ] +then + tmp_dir="/tmp" + tests_dir="tests" +else + tmp_dir="${base_dir}/build/tmp" + tests_dir=${base_dir}/zookeeper-client/zookeeper-client-c/tests +fi + + + + +# ===== +# ===== start the ZooKeeper server +# ===== + +case $1 in +start|startClean|startRequireSASLAuth|startCleanReadOnly) + + if [ "x$1" == "xstartClean" ] || [ "x$1" == "xstartCleanReadOnly" ] + then + rm -rf "${tmp_dir}/zkdata" + fi + mkdir -p "${tmp_dir}/zkdata" + + + # ===== initialize certificates + certs_dir="/tmp/certs" + rm -rf "${certs_dir}" + mkdir -p "${certs_dir}" + cp ${tests_dir}/../ssl/gencerts.sh "${certs_dir}/" > /dev/null + cd ${certs_dir} > /dev/null + ./gencerts.sh > ./gencerts.stdout 2> ./gencerts.stderr + cd - > /dev/null + + + # ===== prepare the configs + sed "s#TMPDIR#${tmp_dir}#g;s#CERTDIR#${certs_dir}#g;s#MAXCLIENTCONNECTIONS#${ZKMAXCNXNS}#g;s#CLIENTPORT#${ZOOPORT}#g" ${tests_dir}/zoo.cfg > "${tmp_dir}/zoo.cfg" + if [ "x$1" == "xstartCleanReadOnly" ] + then + # we can put the new server to read-only mode by starting only a single instance of a three node server + echo "server.1=localhost:22881:33881" >> ${tmp_dir}/zoo.cfg + echo "server.2=localhost:22882:33882" >> ${tmp_dir}/zoo.cfg + echo "server.3=localhost:22883:33883" >> ${tmp_dir}/zoo.cfg + echo "1" > ${tmp_dir}/zkdata/myid + main_class="org.apache.zookeeper.server.quorum.QuorumPeerMain" + else + main_class="org.apache.zookeeper.server.ZooKeeperServerMain" + fi + + + # ===== start the server + java -cp "$CLASSPATH" $PROPERTIES ${main_class} ${tmp_dir}/zoo.cfg &> "${tmp_dir}/zk.log" & + pid=$! + echo -n $! > /tmp/zk.pid + + + # ===== wait for the server to start + if [ "x$1" == "xstartRequireSASLAuth" ] || [ "x$1" == "xstartCleanReadOnly" ] + then + # ===== in these cases we can not connect simply with the java client, so we are just waiting... + sleep 4 + success=true + else + # ===== wait max 120 seconds for server to be ready to server clients (this handles testing on slow hosts) + success=false + for i in {1..120} + do + if ps -p $pid > /dev/null + then + java -cp "$CLASSPATH" $PROPERTIES org.apache.zookeeper.ZooKeeperMain -server localhost:$ZOOPORT ls / > /dev/null 2>&1 + if [ $? -ne 0 ] + then + # server not up yet - wait + sleep 1 + else + # server is up and serving client connections + success=true + break + fi + else + # server died - exit now + echo -n " ZooKeeper server process failed" + break + fi + done + fi + + if $success + then + ## in case for debug, but generally don't use as it messes up the + ## console test output + echo -n " ZooKeeper server started" + else + echo -n " ZooKeeper server NOT started" + fi + + ;; +stop) + # Already killed above + ;; +*) + echo "Unknown command " + $1 + exit 2 +esac + diff --git a/zookeeper-client/zookeeper-client-c/tests/zoo.cfg b/zookeeper-client/zookeeper-client-c/tests/zoo.cfg new file mode 100644 index 0000000..b8e0b2a --- /dev/null +++ b/zookeeper-client/zookeeper-client-c/tests/zoo.cfg @@ -0,0 +1,14 @@ +tickTime=500 +initLimit=10 +syncLimit=5 +dataDir=TMPDIR/zkdata +maxClientCnxns=MAXCLIENTCONNECTIONS + +clientPort=CLIENTPORT +secureClientPort=22281 +serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory +ssl.keyStore.location=CERTDIR/server.jks +ssl.keyStore.password=password +ssl.trustStore.location=CERTDIR/servertrust.jks +ssl.trustStore.password=password + diff --git a/zookeeper-compatibility-tests/pom.xml b/zookeeper-compatibility-tests/pom.xml new file mode 100644 index 0000000..253bc89 --- /dev/null +++ b/zookeeper-compatibility-tests/pom.xml @@ -0,0 +1,58 @@ + + + + + org.apache.zookeeper + parent + 3.6.3 + + pom + 4.0.0 + + zookeeper-compatibility-tests + + + zookeeper-compatibility-tests-curator + + + Apache ZooKeeper - Compatibility Tests + Module for various compatibility tests + + + + + org.apache.maven.plugins + maven-install-plugin + + true + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + + diff --git a/zookeeper-compatibility-tests/zookeeper-compatibility-tests-curator/pom.xml b/zookeeper-compatibility-tests/zookeeper-compatibility-tests-curator/pom.xml new file mode 100644 index 0000000..a124c46 --- /dev/null +++ b/zookeeper-compatibility-tests/zookeeper-compatibility-tests-curator/pom.xml @@ -0,0 +1,77 @@ + + + + + org.apache.zookeeper + zookeeper-compatibility-tests + 3.6.3 + + 4.0.0 + + zookeeper-compatibility-tests-curator + + Apache ZooKeeper - Compatibility Tests - Curator + Module for Apache Curator compatibility tests + + + 5.0.0 + + + + + org.apache.zookeeper + zookeeper + ${project.version} + + + + org.apache.curator + curator-recipes + ${apache-curator-version} + + + org.apache.zookeeper + zookeeper + + + test + + + + org.apache.curator + curator-test + ${apache-curator-version} + + + org.apache.zookeeper + zookeeper + + + test + + + + junit + junit + test + + + \ No newline at end of file diff --git a/zookeeper-compatibility-tests/zookeeper-compatibility-tests-curator/src/test/java/org/apache/zookeeper/compatibility/TestApacheCuratorCompatibility.java b/zookeeper-compatibility-tests/zookeeper-compatibility-tests-curator/src/test/java/org/apache/zookeeper/compatibility/TestApacheCuratorCompatibility.java new file mode 100644 index 0000000..b60a004 --- /dev/null +++ b/zookeeper-compatibility-tests/zookeeper-compatibility-tests-curator/src/test/java/org/apache/zookeeper/compatibility/TestApacheCuratorCompatibility.java @@ -0,0 +1,77 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.compatibility; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.recipes.cache.CuratorCache; +import org.apache.curator.retry.RetryOneTime; +import org.apache.curator.test.TestingServer; +import org.junit.Test; + +/** + * Make sure minimal Apache Curator APIs work correctly. As it's a widely used ZooKeeper + * client library we should not break it. + */ +public class TestApacheCuratorCompatibility { + private static final int TIMEOUT_MS = 5000; + + @Test + public void testBasicUsageOfApisAndRecipes() throws Exception { + try (TestingServer server = new TestingServer()) { + RetryOneTime retryPolicy = new RetryOneTime(1); + try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy)) { + try (CuratorCache cache = CuratorCache.build(client, "/base/path")) { + client.start(); + cache.start(); + + BlockingQueue paths = new LinkedBlockingQueue<>(); + cache.listenable().addListener((dummy1, dummy2, data) -> paths.add(data.getPath())); + + client.create().creatingParentsIfNeeded().forPath("/base/path/1"); + client.create().creatingParentsIfNeeded().forPath("/base/path/2"); + client.create().creatingParentsIfNeeded().forPath("/base/path/1/a"); + client.create().creatingParentsIfNeeded().forPath("/base/path/2/a"); + + assertEquals("/base/path", poll(paths)); + assertEquals("/base/path/1", poll(paths)); + assertEquals("/base/path/2", poll(paths)); + assertEquals("/base/path/1/a", poll(paths)); + assertEquals("/base/path/2/a", poll(paths)); + } + } + } + } + + private static String poll(BlockingQueue queue) { + try { + String value = queue.poll(TIMEOUT_MS, TimeUnit.MILLISECONDS); + assertNotNull("Event poll timed out", value); + return value; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + } +} diff --git a/zookeeper-contrib/build-contrib.xml b/zookeeper-contrib/build-contrib.xml new file mode 100644 index 0000000..34f3c9b --- /dev/null +++ b/zookeeper-contrib/build-contrib.xml @@ -0,0 +1,233 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/build.xml b/zookeeper-contrib/build.xml new file mode 100644 index 0000000..bfb6f66 --- /dev/null +++ b/zookeeper-contrib/build.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/ivysettings.xml b/zookeeper-contrib/ivysettings.xml new file mode 100644 index 0000000..15c98b6 --- /dev/null +++ b/zookeeper-contrib/ivysettings.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/pom.xml b/zookeeper-contrib/pom.xml new file mode 100644 index 0000000..a7533f4 --- /dev/null +++ b/zookeeper-contrib/pom.xml @@ -0,0 +1,108 @@ + + + + 4.0.0 + + + org.apache.zookeeper + parent + 3.6.3 + + + zookeeper-contrib + pom + Apache ZooKeeper - Contrib + + Contrib projects to Apache ZooKeeper + + + + true + + + + + fatjar + + zookeeper-contrib-fatjar + + + + full-build + + zookeeper-contrib-fatjar + zookeeper-contrib-loggraph + zookeeper-contrib-rest + zookeeper-contrib-zooinspector + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + -Xlint:none + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.0 + + + com.puppycrawl.tools + checkstyle + ${checkstyle.version} + + + + checkstyle-simple.xml + checkstyleSuppressions.xml + UTF-8 + true + true + false + true + false + + + + checkstyle + validate + + check + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-fatjar/README.md b/zookeeper-contrib/zookeeper-contrib-fatjar/README.md new file mode 100644 index 0000000..baf2f95 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-fatjar/README.md @@ -0,0 +1,21 @@ +ZooKeeper Fatjar +================ + +This package contains build to create a fat zookeeper jar. Fatjar can be used to run: +- zookeeper server +- zookeeper client +- distributed load generator for testing (generateLoad) +- container that will instantiate classes as directed by an instance manager (ic) +- system test (systest) +- jmh micro benchmarks (jmh) + + +Use following command to build fatjar +``` +mvn clean install -P fatjar -DskipTests +``` + +To run the fatjar use: +``` +java -jar zoookeeper--fatjar.jar +``` diff --git a/zookeeper-contrib/zookeeper-contrib-fatjar/pom.xml b/zookeeper-contrib/zookeeper-contrib-fatjar/pom.xml new file mode 100644 index 0000000..693d33c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-fatjar/pom.xml @@ -0,0 +1,145 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-contrib + 3.6.3 + + + org.apache.zookeeper + zookeeper-contrib-fatjar + jar + Apache ZooKeeper - Contrib - Fatjar + + + true + true + + + + + org.apache.zookeeper + zookeeper-jute + ${project.version} + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.apache.zookeeper + zookeeper + ${project.version} + test-jar + + + org.apache.zookeeper + zookeeper-it + ${project.version} + + + org.slf4j + slf4j-api + + + commons-cli + commons-cli + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-servlet + + + com.fasterxml.jackson.core + jackson-databind + + + com.googlecode.json-simple + json-simple + + + jline + jline + + + io.dropwizard.metrics + metrics-core + + + org.xerial.snappy + snappy-java + + + log4j + log4j + + + + + + + ${project.basedir}/src/main/resources + + + ${project.basedir}/../../conf + + log4j.properties + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + jar-with-dependencies + + + + org.apache.zookeeper.util.FatJarMain + + + zookeeper-${project.version}-fatjar + false + false + + + + make-assembly + package + + single + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-fatjar/src/main/java/org/apache/zookeeper/util/FatJarMain.java b/zookeeper-contrib/zookeeper-contrib-fatjar/src/main/java/org/apache/zookeeper/util/FatJarMain.java new file mode 100644 index 0000000..283675d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-fatjar/src/main/java/org/apache/zookeeper/util/FatJarMain.java @@ -0,0 +1,128 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * This is a generic Main class that is completely driven by the + * /mainClasses resource on the class path. This resource has the + * format: + *

+ * cmd:mainClass:Description
+ * 
+ * Any lines starting with # will be skipped + * + */ +public class FatJarMain { + static class Cmd { + Cmd(String cmd, String clazz, String desc) { + this.cmd = cmd; + this.clazz = clazz; + this.desc = desc; + } + String cmd; + String clazz; + String desc; + } + static Map cmds = new HashMap(); + static List order = new ArrayList(); + + /** + * @param args the first parameter of args will be used as an + * index into the /mainClasses resource. The rest will be passed + * to the mainClass to run. + * @throws IOException + * @throws ClassNotFoundException + * @throws NoSuchMethodException + * @throws SecurityException + * @throws IllegalAccessException + * @throws IllegalArgumentException + */ + public static void main(String[] args) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException { + InputStream is = FatJarMain.class.getResourceAsStream("/mainClasses"); + if (is == null) { + System.err.println("Couldn't find /mainClasses in classpath."); + System.exit(3); + } + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String line; + while((line = br.readLine()) != null) { + String parts[] = line.split(":", 3); + if (parts.length != 3 || (parts[0].length() > 0 && parts[0].charAt(0) == '#')) { + continue; + } + if (parts[0].length() > 0) { + cmds.put(parts[0], new Cmd(parts[0], parts[1], parts[2])); + // We use the order array to preserve the order of the commands + // for help. The hashmap will not preserver order. (It may be overkill.) + order.add(parts[0]); + } else { + // Just put the description in + order.add(parts[2]); + } + } + if (args.length == 0) { + doHelp(); + return; + } + Cmd cmd = cmds.get(args[0]); + if (cmd == null) { + doHelp(); + return; + } + Class clazz = Class.forName(cmd.clazz); + Method main = clazz.getMethod("main", String[].class); + String newArgs[] = new String[args.length-1]; + System.arraycopy(args, 1, newArgs, 0, newArgs.length); + try { + main.invoke(null, (Object)newArgs); + } catch(InvocationTargetException e) { + if (e.getCause() != null) { + e.getCause().printStackTrace(); + } else { + e.printStackTrace(); + } + } + } + + private static void doHelp() { + System.err.println("USAGE: FatJarMain cmd args"); + System.err.println("Available cmds:"); + for(String c: order) { + Cmd cmd = cmds.get(c); + if (cmd != null) { + System.err.println(" " + c + " " + cmd.desc); + } else { + System.err.println(c); + } + } + System.exit(2); + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-fatjar/src/main/resources/mainClasses b/zookeeper-contrib/zookeeper-contrib-fatjar/src/main/resources/mainClasses new file mode 100644 index 0000000..c7b27a1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-fatjar/src/main/resources/mainClasses @@ -0,0 +1,9 @@ +::Client Commands +client:org.apache.zookeeper.ZooKeeperMain:Client shell to ZooKeeper +::Server Commands +server:org.apache.zookeeper.server.quorum.QuorumPeerMain:Start ZooKeeper server +::Test Commands +generateLoad:org.apache.zookeeper.test.system.GenerateLoad:A distributed load generator for testing +ic:org.apache.zookeeper.test.system.InstanceContainer:A container that will instantiate classes as directed by an instance manager +systest:org.apache.zookeeper.test.system.BaseSysTest:Start system test +jmh:org.apache.zookeeper.BenchMain:Run jmh micro benchmarks diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/README b/zookeeper-contrib/zookeeper-contrib-huebrowser/README new file mode 100644 index 0000000..c03ea90 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/README @@ -0,0 +1,62 @@ + +ZooKeeper Browser - Hue Application +=================================== + +The ZooKeeper Browser application allows you to see how the cluster nodes are working and also allows you to do CRUD operations on the znode hierarchy. + +Requirements +------------ + +Hue-1.0: + * http://github.com/downloads/cloudera/hue/hue-1.0.tgz + * http://github.com/downloads/cloudera/hue/release-notes-1.0.html + +ZooKeeper REST gateway: + * available as contrib: contrib/rest + +How to install? +--------------- + +First of all you need to install Hue 1.0 release: + + * http://archive.cloudera.com/cdh/3/hue/sdk/sdk.html + * http://github.com/cloudera/hue/tree/release-1.0 + +After you finish the previous step you should copy the zkui/ folder to apps/ and register the new application: + + * $ ./build/env/bin/python tools/app_reg/app_reg.py --install apps/zkui + * $ ./build/env/bin/python tools/app_reg/app_reg.py --list 2>&1 | grep zkui + zkui 0.1 /Users/philip/src/hue/apps/zkui + + +And restart the Hue application server. + +Configuration +------------- + +Edit zkui/src/zkui/settings.py: + +CLUSTERS = [{ + 'nice_name': 'Default', + 'hostport': 'localhost:2181,localhost:2182,localhost:2183', + 'rest_gateway': 'http://localhost:9998' + }, { + # ... and more clusters + } +] + +What is Hue? +------------ + +Wiki: http://wiki.github.com/cloudera/hue/ +Main Repo: http://github.com/cloudera/hue + +Hue is both a web UI for Hadoop and a framework to create interactive web applications. It features a FileBrowser for accessing HDFS, JobSub and JobBrowser applications for submitting and viewing MapReduce jobs, a Beeswax application for interacting with Hive. On top of that, the web frontend is mostly built from declarative widgets that require no JavaScript and are easy to learn. + +What is ZooKeeper? +------------------ + +http://zookeeper.apache.org/ + +ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed. + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/Makefile b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/Makefile new file mode 100644 index 0000000..9c22d1c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/Makefile @@ -0,0 +1,21 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +ifeq ($(ROOT),) + $(error "Error: Expect the environment variable $$ROOT to point to the Desktop installation") +endif + +include $(ROOT)/Makefile.sdk diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/setup.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/setup.py new file mode 100644 index 0000000..68d1352 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/setup.py @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +from setuptools import setup, find_packages +import os + +def expand_package_data(src_dirs, strip=""): + ret = [] + for src_dir in src_dirs: + for path, dnames, fnames in os.walk(src_dir): + for fname in fnames: + ret.append(os.path.join(path, fname).replace(strip, "")) + return ret + +os.chdir(os.path.dirname(os.path.abspath(__file__))) +setup( + name = "zkui", + version = "0.1", + url = 'http://zookeeper.apache.org/', + description = 'ZooKeeper Browser', + packages = find_packages('src'), + package_dir = {'': 'src'}, + install_requires = ['setuptools', 'desktop'], + entry_points = { 'desktop.sdk.application': 'zkui=zkui' }, + zip_safe = False, + package_data = { + # Include static resources. Package_data doesn't + # deal well with directory globs, so we enumerate + # the files manually. + 'zkui': expand_package_data( + ["src/zkui/templates", "src/zkui/static"], + "src/zkui/") + } +) diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/__init__.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/__init__.py new file mode 100644 index 0000000..eccc881 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/__init__.py @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/forms.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/forms.py new file mode 100644 index 0000000..6b1f178 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/forms.py @@ -0,0 +1,29 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from django import forms +from django.forms.widgets import Textarea, HiddenInput + +class CreateZNodeForm(forms.Form): + name = forms.CharField(max_length=64) + data = forms.CharField(required=False, widget=Textarea) + sequence = forms.BooleanField(required=False) + +class EditZNodeForm(forms.Form): + data = forms.CharField(required=False, widget=Textarea) + version = forms.IntegerField(required=False, widget=HiddenInput) + + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/models.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/models.py new file mode 100644 index 0000000..a46696b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/models.py @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/rest.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/rest.py new file mode 100644 index 0000000..e4874a1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/rest.py @@ -0,0 +1,230 @@ + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import urllib2 +import urllib +import simplejson + +from contextlib import contextmanager + +class RequestWithMethod(urllib2.Request): + """ Request class that know how to set the method name """ + def __init__(self, *args, **kwargs): + urllib2.Request.__init__(self, *args, **kwargs) + self._method = None + + def get_method(self): + return self._method or \ + urllib2.Request.get_method(self) + + def set_method(self, method): + self._method = method + +class ZooKeeper(object): + + class Error(Exception): pass + + class NotFound(Error): pass + + class ZNodeExists(Error): pass + + class InvalidSession(Error): pass + + class WrongVersion(Error): pass + + def __init__(self, uri = 'http://localhost:9998'): + self._base = uri + self._session = None + + def start_session(self, expire=5, id=None): + """ Create a session and return the ID """ + if id is None: + url = "%s/sessions/v1/?op=create&expire=%d" % (self._base, expire) + self._session = self._do_post(url)['id'] + else: + self._session = id + return self._session + + def close_session(self): + """ Close the session on the server """ + if self._session is not None: + url = '%s/sessions/v1/%s' % (self._base, self._session) + self._do_delete(url) + self._session = None + + def heartbeat(self): + """ Send a heartbeat request. This is needed in order to keep a session alive """ + if self._session is not None: + url = '%s/sessions/v1/%s' % (self._base, self._session) + self._do_put(url, '') + + @contextmanager + def session(self, *args, **kwargs): + """ Session handling using a context manager """ + yield self.start_session(*args, **kwargs) + self.close_session() + + def get(self, path): + """ Get a node """ + url = "%s/znodes/v1%s" % (self._base, path) + return self._do_get(url) + + def get_children(self, path): + """ Get all the children for a given path. This function creates a generator """ + for child_path in self.get_children_paths(path, uris=True): + try: + yield self._do_get(child_path) + except ZooKeeper.NotFound: + continue + + def get_children_paths(self, path, uris=False): + """ Get the paths for children nodes """ + url = "%s/znodes/v1%s?view=children" % (self._base, path) + resp = self._do_get(url) + for child in resp.get('children', []): + yield child if not uris else resp['child_uri_template']\ + .replace('{child}', urllib2.quote(child)) + + def create(self, path, data=None, sequence=False, ephemeral=False): + """ Create a new node. By default this call creates a persistent znode. + + You can also create an ephemeral or a sequential znode. + """ + ri = path.rindex('/') + head, name = path[:ri+1], path[ri+1:] + if head != '/': head = head[:-1] + + flags = { + 'null': 'true' if data is None else 'false', + 'ephemeral': 'true' if ephemeral else 'false', + 'sequence': 'true' if sequence else 'false' + } + if ephemeral: + if self._session: + flags['session'] = self._session + else: + raise ZooKeeper.Error, 'You need a session '\ + 'to create an ephemeral node' + flags = urllib.urlencode(flags) + + url = "%s/znodes/v1%s?op=create&name=%s&%s" % \ + (self._base, head, name, flags) + + return self._do_post(url, data) + + def set(self, path, data=None, version=-1, null=False): + """ Set the value of node """ + url = "%s/znodes/v1%s?%s" % (self._base, path, \ + urllib.urlencode({ + 'version': version, + 'null': 'true' if null else 'false' + })) + return self._do_put(url, data) + + def delete(self, path, version=-1): + """ Delete a znode """ + if type(path) is list: + map(lambda el: self.delete(el, version), path) + return + + url = '%s/znodes/v1%s?%s' % (self._base, path, \ + urllib.urlencode({ + 'version':version + })) + try: + return self._do_delete(url) + except urllib2.HTTPError, e: + if e.code == 412: + raise ZooKeeper.WrongVersion(path) + elif e.code == 404: + raise ZooKeeper.NotFound(path) + raise + + def recursive_delete(self, path): + """ Delete all the nodes from the tree """ + for child in self.get_children_paths(path): + fp = ("%s/%s" % (path, child)).replace('//', '/') + self.recursive_delete(fp) + self.delete(path) + + def exists(self, path): + """ Do a znode exists """ + try: + self.get(path) + return True + except ZooKeeper.NotFound: + return False + + def _do_get(self, uri): + """ Send a GET request and convert errors to exceptions """ + try: + req = urllib2.urlopen(uri) + resp = simplejson.load(req) + + if 'Error' in resp: + raise ZooKeeper.Error(resp['Error']) + + return resp + except urllib2.HTTPError, e: + if e.code == 404: + raise ZooKeeper.NotFound(uri) + raise + + def _do_post(self, uri, data=None): + """ Send a POST request and convert errors to exceptions """ + try: + req = urllib2.Request(uri, {}) + req.add_header('Content-Type', 'application/octet-stream') + if data is not None: + req.add_data(data) + + resp = simplejson.load(urllib2.urlopen(req)) + if 'Error' in resp: + raise ZooKeeper.Error(resp['Error']) + return resp + + except urllib2.HTTPError, e: + if e.code == 201: + return True + elif e.code == 409: + raise ZooKeeper.ZNodeExists(uri) + elif e.code == 401: + raise ZooKeeper.InvalidSession(uri) + raise + + def _do_delete(self, uri): + """ Send a DELETE request """ + req = RequestWithMethod(uri) + req.set_method('DELETE') + req.add_header('Content-Type', 'application/octet-stream') + return urllib2.urlopen(req).read() + + def _do_put(self, uri, data): + """ Send a PUT request """ + try: + req = RequestWithMethod(uri) + req.set_method('PUT') + req.add_header('Content-Type', 'application/octet-stream') + if data is not None: + req.add_data(data) + + return urllib2.urlopen(req).read() + except urllib2.HTTPError, e: + if e.code == 412: # precondition failed + raise ZooKeeper.WrongVersion(uri) + raise + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/settings.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/settings.py new file mode 100644 index 0000000..844c695 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/settings.py @@ -0,0 +1,30 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +DJANGO_APPS = [ "zkui" ] +NICE_NAME = "ZooKeeper Browser" +REQUIRES_HADOOP = False + +CLUSTERS = [{ + 'nice_name': 'Default', + 'hostport': 'localhost:2181,localhost:2182,localhost:2183', + 'rest_gateway': 'http://localhost:9998' + } +] + +DEPENDER_PACKAGE_YMLS = [ + "src/zkui/static/js/package.yml", +] diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/art/line_icons.png b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/art/line_icons.png new file mode 100644 index 0000000..1da4a29 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/art/line_icons.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/art/zkui.png b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/art/zkui.png new file mode 100644 index 0000000..cb40df3 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/art/zkui.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/bootstrap.js b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/bootstrap.js new file mode 100644 index 0000000..8e3fbfb --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/bootstrap.js @@ -0,0 +1,32 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. +CCS.Desktop.register({ + Zkui : { + name : 'ZooKeeper Browser', + css : '/zkui/static/css/zkui.css', + require: [ 'Zkui' ], + launch: function(path, options){ + return new Zkui(path || '/zkui/', options); + }, + menu: { + id: 'ccs-zkui-menu', + img: { + src: '/zkui/static/art/zkui.png' + } + }, + help: '/help/zkui/' + } +}); diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/css/zkui.css b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/css/zkui.css new file mode 100644 index 0000000..c49f392 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/css/zkui.css @@ -0,0 +1,56 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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. +*/ + +.zkui img.zkui_icon { + width: 55px; + height: 55px; + position: absolute; + top: 27px; + left: 3px; +} + +div.zkui .left_col li { + margin: 5px 0px; + font-size: 16px; + background-color: white; + color: black; + padding: 2px 1px 1px 5px; + -moz-border-radius: 3px; + -webkit-border-radius: 3px; + border: solid black 1px; +} + +div.zkui .left_col li:hover { + background-color: lightBlue; + color: white; +} + +div.zkui .left_col li a { + color: black; + display: block; +} + +div.zkui .left_col li a:hover { + text-decoration: none; +} + +div.zkui .createZnodeForm td, +div.zkui .editZnodeForm td { + padding: 5px; +} + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/help/index.html b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/help/index.html new file mode 100644 index 0000000..355c8cd --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/help/index.html @@ -0,0 +1,26 @@ + + +

ZooKeeper Browser

+ + +

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services

+ +

About

+ +

The ZooKeeper Browser application allows you to see how the cluster nodes are working and also allows you to do CRUD operations on the znode hierarchy.

+ diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/js/Source/Zkui/Zkui.js b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/js/Source/Zkui/Zkui.js new file mode 100644 index 0000000..c8bf383 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/js/Source/Zkui/Zkui.js @@ -0,0 +1,50 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you 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. + +/* +--- + +script: Zkui.js + +description: Defines Zkui; a Hue application that extends CCS.JBrowser. + +authors: +- Unknown + +requires: +- ccs-shared/CCS.JBrowser + +provides: [Zkui] + +... +*/ +ART.Sheet.define('window.art.browser.zkui', { + 'min-width': 620 +}); + +var Zkui = new Class({ + + Extends: CCS.JBrowser, + + options: { + className: 'art browser logo_header zkui' + }, + + initialize: function(path, options){ + this.parent(path || '/zkui/', options); + } + +}); diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/js/package.yml b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/js/package.yml new file mode 100644 index 0000000..c2c07ad --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/static/js/package.yml @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +copyright: Apache License v2.0 +version: 0.1 +description: ZooKeeper Browser +name: ZooKeeper Browser +sources: [Source/Zkui/Zkui.js] diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/stats.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/stats.py new file mode 100644 index 0000000..48f35dd --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/stats.py @@ -0,0 +1,170 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import socket +import re + +from StringIO import StringIO + +class Session(object): + + class BrokenLine(Exception): pass + + def __init__(self, session): + m = re.search('/(\d+\.\d+\.\d+\.\d+):(\d+)\[(\d+)\]\((.*)\)', session) + if m: + self.host = m.group(1) + self.port = m.group(2) + self.interest_ops = m.group(3) + for d in m.group(4).split(","): + k,v = d.split("=") + self.__dict__[k] = v + else: + raise Session.BrokenLine() + +class ZooKeeperStats(object): + + def __init__(self, host='localhost', port='2181', timeout=1): + self._address = (host, int(port)) + self._timeout = timeout + + def get_stats(self): + """ Get ZooKeeper server stats as a map """ + data = self._send_cmd('mntr') + if data: + return self._parse(data) + else: + data = self._send_cmd('stat') + return self._parse_stat(data) + + def get_clients(self): + """ Get ZooKeeper server clients """ + clients = [] + + stat = self._send_cmd('stat') + if not stat: + return clients + + sio = StringIO(stat) + + #skip two lines + sio.readline() + sio.readline() + + for line in sio: + if not line.strip(): + break + try: + clients.append(Session(line.strip())) + except Session.BrokenLine: + continue + + return clients + + def _create_socket(self): + return socket.socket() + + def _send_cmd(self, cmd): + """ Send a 4letter word command to the server """ + s = self._create_socket() + s.settimeout(self._timeout) + + s.connect(self._address) + s.send(cmd) + + data = s.recv(2048) + s.close() + + return data + + def _parse(self, data): + """ Parse the output from the 'mntr' 4letter word command """ + h = StringIO(data) + + result = {} + for line in h.readlines(): + try: + key, value = self._parse_line(line) + result[key] = value + except ValueError: + pass # ignore broken lines + + return result + + def _parse_stat(self, data): + """ Parse the output from the 'stat' 4letter word command """ + h = StringIO(data) + + result = {} + + version = h.readline() + if version: + result['zk_version'] = version[version.index(':')+1:].strip() + + # skip all lines until we find the empty one + while h.readline().strip(): pass + + for line in h.readlines(): + m = re.match('Latency min/avg/max: (\d+)/(\d+)/(\d+)', line) + if m is not None: + result['zk_min_latency'] = int(m.group(1)) + result['zk_avg_latency'] = int(m.group(2)) + result['zk_max_latency'] = int(m.group(3)) + continue + + m = re.match('Received: (\d+)', line) + if m is not None: + result['zk_packets_received'] = int(m.group(1)) + continue + + m = re.match('Sent: (\d+)', line) + if m is not None: + result['zk_packets_sent'] = int(m.group(1)) + continue + + m = re.match('Outstanding: (\d+)', line) + if m is not None: + result['zk_outstanding_requests'] = int(m.group(1)) + continue + + m = re.match('Mode: (.*)', line) + if m is not None: + result['zk_server_state'] = m.group(1) + continue + + m = re.match('Node count: (\d+)', line) + if m is not None: + result['zk_znode_count'] = int(m.group(1)) + continue + + return result + + def _parse_line(self, line): + try: + key, value = map(str.strip, line.split('\t')) + except ValueError: + raise ValueError('Found invalid line: %s' % line) + + if not key: + raise ValueError('The key is mandatory and should not be empty') + + try: + value = int(value) + except (TypeError, ValueError): + pass + + return key, value + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/clients.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/clients.mako new file mode 100644 index 0000000..2bee9a7 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/clients.mako @@ -0,0 +1,51 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> + +<%namespace name="shared" file="shared_components.mako" /> + +${shared.header("ZooKeeper Browser > Clients > %s:%s" % (host, port))} + +

${host}:${port} :: client connections

+
+ +% if clients: + + + + + + + + + + + % for client in clients: + + + + + + + + + % endfor +
HostPortInterest OpsQueuedReceivedSent
${client.host}${client.port}${client.interest_ops}${client.queued}${client.recved}${client.sent}
+% endif + +${shared.footer()} + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/create.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/create.mako new file mode 100644 index 0000000..2a8b8cc --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/create.mako @@ -0,0 +1,34 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> +<%namespace name="shared" file="shared_components.mako" /> + +${shared.header("ZooKeeper Browser > Create Znode")} + +

Create New Znode :: ${path}

+

+ +
+ + ${form.as_table()|n} + +
+ +
+
+ +${shared.footer()} diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/edit.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/edit.mako new file mode 100644 index 0000000..997bd07 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/edit.mako @@ -0,0 +1,34 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> +<%namespace name="shared" file="shared_components.mako" /> + +${shared.header("ZooKeeper Browser > Edit Znode > %s" % path)} + +

Edit Znode Data :: ${path}

+

+ +
+ + ${form.as_table()|n} + +
+ +
+
+ +${shared.footer()} diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/index.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/index.mako new file mode 100644 index 0000000..567919d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/index.mako @@ -0,0 +1,54 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> +<%namespace name="shared" file="shared_components.mako" /> + +${shared.header("ZooKeeper Browser")} + +

Overview

+ +
+ +% for i, c in enumerate(overview): +

${i+1}. ${c['nice_name']} Cluster Overview


+ + + + + + + + + + + + % for host, stats in c['stats'].items(): + + + + + + + + % endfor +
NodeRoleAvg LatencyWatch CountVersion
${host}${stats.get('zk_server_state', '')}${stats.get('zk_avg_latency', '')}${stats.get('zk_watch_count', '')}${stats.get('zk_version', '')}
+ +

+% endfor + +${shared.footer()} + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/shared_components.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/shared_components.mako new file mode 100644 index 0000000..f9a4589 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/shared_components.mako @@ -0,0 +1,66 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> + +<%! +import datetime +from django.template.defaultfilters import urlencode, escape +from zkui import settings +%> + +<%def name="header(title='ZooKeeper Browser', toolbar=True)"> + + + + ${title} + + + % if toolbar: +
+ +
+ % endif + +
+
+ +
+ +

Clusters

+ +
+ +
+ + +<%def name="info_button(url, text)"> + ${text} + + +<%def name="footer()"> +
+
+ + + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/tree.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/tree.mako new file mode 100644 index 0000000..07c91c3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/tree.mako @@ -0,0 +1,75 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> +<%namespace name="shared" file="shared_components.mako" /> + +${shared.header("ZooKeeper Browser > Tree > %s > %s" % (cluster['nice_name'], path))} + +

${cluster['nice_name'].lower()} :: ${path}

+
+ + + + + + % for child in children: + + % endfor +
Children
+ + ${child} + + Delete +
+
+ + ${shared.info_button(url('zkui.views.create', id=cluster['id'], path=path), 'Create New')} + + +
+ +

data :: base64 :: length :: ${znode.get('dataLength', 0)}

+
+ + +
+ + ${shared.info_button(url('zkui.views.edit_as_base64', id=cluster['id'], path=path), 'Edit as Base64')} + ${shared.info_button(url('zkui.views.edit_as_text', id=cluster['id'], path=path), 'Edit as Text')} + +
+
+ +

stat information

+
+ + + + + % for key in ('pzxid', 'ctime', 'aversion', 'mzxid', \ + 'ephemeralOwner', 'version', 'mtime', 'cversion', 'czxid'): + + % endfor +
KeyValue
${key}${znode[key]}
+ +
+Details on stat information. + +${shared.footer()} + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/view.mako b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/view.mako new file mode 100644 index 0000000..e046afc --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/templates/view.mako @@ -0,0 +1,128 @@ +<%! +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +%> +<%namespace name="shared" file="shared_components.mako" /> + +${shared.header("ZooKeeper Browser > %s" % cluster['nice_name'])} + +<%def name="show_stats(stats)"> + + Key + Value + + + Version + ${stats.get('zk_version')} + + + Latency + Min: ${stats.get('zk_min_latency', '')} + Avg: ${stats.get('zk_avg_latency', '')} + Max: ${stats.get('zk_max_latency', '')} + + + Packets + Sent: ${stats.get('zk_packets_sent', '')} + Received: ${stats.get('zk_packets_received', '')} + + + + Outstanding Requests + ${stats.get('zk_outstanding_requests', '')} + + + Watch Count + ${stats.get('zk_watch_count', '')} + + + Open FD Count + ${stats.get('zk_open_file_descriptor_count', '')} + + + Max FD Count + ${stats.get('zk_max_file_descriptor_count', '')} + + + + +

${cluster['nice_name']} Cluster Overview

+ +${shared.info_button(url('zkui.views.tree', id=cluster['id'], path='/'), 'View Znode Hierarchy')} + +

+ +% if leader: +

General

+ + + + + + + + + + + + + + + +
KeyValue
ZNode Count${leader.get('zk_znode_count', '')}
Ephemerals Count${leader.get('zk_ephemerals_count', '')}
Approximate Data Size${leader.get('zk_approximate_data_size', '')} bytes
+

+% endif + +% if leader: +

node :: ${leader['host']} :: leader

+ + ${shared.info_button(url('zkui.views.clients', host=leader['host']), 'View Client Connections')} + +

+ + ${show_stats(leader)} + + + + + + + + + + + + + +
Followers${leader.get('zk_followers', '')}
Synced Followers${leader.get('zk_synced_followers', '')}
Pending Syncs${leader.get('zk_pending_syncs', '')}
+

+% endif + +% for stats in followers: +

node :: ${stats['host']} :: follower

+
+ + ${shared.info_button(url('zkui.views.clients', host=stats['host']), 'View Client Connections')} + +

+ + ${show_stats(stats)} +
+

+% endfor + +${shared.footer()} + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/urls.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/urls.py new file mode 100644 index 0000000..f795f7e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/urls.py @@ -0,0 +1,28 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from django.conf.urls.defaults import patterns, url + +urlpatterns = patterns('zkui', + url(r'^$', 'views.index'), + url(r'view/(?P\d+)$', 'views.view'), + url(r'clients/(?P.+)$', 'views.clients'), + url(r'tree/(?P\d+)(?P.+)$', 'views.tree'), + url(r'create/(?P\d+)(?P.*)$', 'views.create'), + url(r'delete/(?P\d+)(?P.*)$', 'views.delete'), + url(r'edit/base64/(?P\d+)(?P.*)$', 'views.edit_as_base64'), + url(r'edit/text/(?P\d+)(?P.*)$', 'views.edit_as_text') +) diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/utils.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/utils.py new file mode 100644 index 0000000..fb01317 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/utils.py @@ -0,0 +1,33 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from zkui import settings + +from django.http import Http404 + +def get_cluster_or_404(id): + try: + id = int(id) + if not (0 <= id < len(settings.CLUSTERS)): + raise ValueError, 'Undefined cluster id.' + except (TypeError, ValueError): + raise Http404() + + cluster = settings.CLUSTERS[id] + cluster['id'] = id + + return cluster + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/views.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/views.py new file mode 100644 index 0000000..64d926b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/views.py @@ -0,0 +1,165 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from desktop.lib.django_util import render +from django.http import Http404 + +from zkui import settings +from zkui.stats import ZooKeeperStats +from zkui.rest import ZooKeeper +from zkui.utils import get_cluster_or_404 +from zkui.forms import CreateZNodeForm, EditZNodeForm + +def _get_global_overview(): + overview = [] + for c in settings.CLUSTERS: + overview.append(_get_overview(c)) + return overview + +def _get_overview(cluster): + stats = {} + for s in cluster['hostport'].split(','): + host, port = map(str.strip, s.split(':')) + + zks = ZooKeeperStats(host, port) + stats[s] = zks.get_stats() or {} + + cluster['stats'] = stats + return cluster + +def _group_stats_by_role(cluster): + leader, followers = None, [] + for host, stats in cluster['stats'].items(): + stats['host'] = host + + if stats.get('zk_server_state') == 'leader': + leader = stats + + elif stats.get('zk_server_state') == 'follower': + followers.append(stats) + + return leader, followers + +def index(request): + overview = _get_global_overview() + return render('index.mako', request, + dict(overview=overview)) + +def view(request, id): + cluster = get_cluster_or_404(id) + + cluster = _get_overview(cluster) + leader, followers = _group_stats_by_role(cluster) + + return render('view.mako', request, + dict(cluster=cluster, leader=leader, followers=followers)) + +def clients(request, host): + parts = host.split(':') + if len(parts) != 2: + raise Http404 + + host, port = parts + zks = ZooKeeperStats(host, port) + clients = zks.get_clients() + + return render('clients.mako', request, + dict(host=host, port=port, clients=clients)) + +def tree(request, id, path): + cluster = get_cluster_or_404(id) + zk = ZooKeeper(cluster['rest_gateway']) + + znode = zk.get(path) + children = sorted(zk.get_children_paths(path)) + + return render('tree.mako', request, + dict(cluster=cluster, path=path, \ + znode=znode, children=children)) + +def delete(request, id, path): + cluster = get_cluster_or_404(id) + if request.method == 'POST': + zk = ZooKeeper(cluster['rest_gateway']) + try: + zk.recursive_delete(path) + except ZooKeeper.NotFound: + pass + + return tree(request, id, path[:path.rindex('/')] or '/') + +def create(request, id, path): + cluster = get_cluster_or_404(id) + + if request.method == 'POST': + form = CreateZNodeForm(request.POST) + if form.is_valid(): + zk = ZooKeeper(cluster['rest_gateway']) + + full_path = ("%s/%s" % (path, form.cleaned_data['name']))\ + .replace('//', '/') + + zk.create(full_path, \ + form.cleaned_data['data'], \ + sequence = form.cleaned_data['sequence']) + return tree(request, id, path) + else: + form = CreateZNodeForm() + + return render('create.mako', request, + dict(path=path, form=form)) + +def edit_as_base64(request, id, path): + cluster = get_cluster_or_404(id) + zk = ZooKeeper(cluster['rest_gateway']) + node = zk.get(path) + + if request.method == 'POST': + form = EditZNodeForm(request.POST) + if form.is_valid(): + # TODO is valid base64 string? + data = form.cleaned_data['data'].decode('base64') + zk.set(path, data, form.cleaned_data['version']) + + return tree(request, id, path) + else: + form = EditZNodeForm(dict(\ + data=node.get('data64', ''), + version=node.get('version', '-1'))) + + return render('edit.mako', request, + dict(path=path, form=form)) + +def edit_as_text(request, id, path): + cluster = get_cluster_or_404(id) + zk = ZooKeeper(cluster['rest_gateway']) + node = zk.get(path) + + if request.method == 'POST': + form = EditZNodeForm(request.POST) + if form.is_valid(): + zk.set(path, form.cleaned_data['data']) + + return tree(request, id, path) + else: + form = EditZNodeForm(dict(data=node.get('data64', '')\ + .decode('base64').strip(), + version=node.get('version', '-1'))) + + return render('edit.mako', request, + dict(path=path, form=form)) + + diff --git a/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/windmilltests.py b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/windmilltests.py new file mode 100644 index 0000000..ba44e26 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-huebrowser/zkui/src/zkui/windmilltests.py @@ -0,0 +1,23 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from desktop.lib.windmill_util import logged_in_client + +def test_zkui(): + """ launches the default view for zkui """ + client = logged_in_client() + client.click(id='ccs-zkui-menu') + client.waits.forElement(classname='CCS-ZKUI', timeout='2000') diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/README.txt b/zookeeper-contrib/zookeeper-contrib-loggraph/README.txt new file mode 100644 index 0000000..8ccaa1c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/README.txt @@ -0,0 +1,70 @@ +LogGraph README + +1 - About +LogGraph is an application for viewing and filtering zookeeper logs. It can handle transaction logs and message logs. + +2 - Compiling + +Run "ant jar" in src/contrib/loggraph/. This will download all dependencies and compile all the loggraph code. + +Once compilation has finished, you can run it the the loggraph.sh script in zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources. +This will start and embedded web server on your machine. +Navigate to http://localhost:8182/graph/main.html + +3 - Usage +LogGraph presents the user with 4 views, + + a) Simple log view + This view simply displays the log text. This isn't very useful without filters (see "Filtering the logs"). + + b) Server view + The server view shows the interactions between the different servers in an ensemble. The X axis represents time. + * Exceptions show up as red dots. Hovering your mouse over them will give you more details of the exception + * The colour of the line represents the election state of the server. + - orange means LOOKING for leader + - dark green means the server is the leader + - light green means the server is following a leader + - yellow means there isn't enough information to determine the state of the server. + * The gray arrows denote election messages between servers. Pink dashed arrows are messages that were sent but never delivered. + + c) Session view + The session view shows the lifetime of sessions on a server. Use the time filter to narrow down the view. Any more than about 2000 events will take a long time to view in your browser. + The X axis represents time. Each line is a session. The black dots represent events on the session. You can click on the black dots for more details of the event. + + d) Stats view + There is currently only one statistics view, Transactions/minute. Suggestions for other statistic views are very welcome. + +4 - Filtering the logs +The logs can be filtered in 2 ways, by time and by content. + +To filter by time simply move the slider to the desired start time. The time window specifies how many milliseconds after and including the start time will be displayed. + +Content filtering uses a adhoc filtering language, using prefix notation. The language looks somewhat similar to lisp. A statement in the language takes the form (op arg arg ....). A statement resolves to a boolean value. Statements can be nested. + +4.1 - Filter arguments +An argument can be a number, a string or a symbol. A number is any argument which starts with -, + or 0 to 9. If the number starts with 0x it is interpretted as hexidecimal. Otherwise it is interpretted as decimal. If the argument begins with a double-quote, (") it is interpretted as a string. Anything else is interpretted as a symbol. + +4.2 - Filter symbols +The possible filter symbols are: + +client-id : number, the session id of the client who initiated a transaction. +cxid : number, the cxid of a transaction +zxid : number, the zxid of a transaction +operation : string, the operation being performed, for example "setData", "createSession", "closeSession", "error", "create" + +4.3 - Filter operations +The possible filter operations are: + +or : logical or, takes 1 or more arguments which must be other statements. +and : logical and, takes 1 or more arguments which must be other statements. +not : logical not, takes 1 argument which must be another statement. +xor : exclusive or, takes 1 or more arguments which must be other statements. += : equals, takes 1 or more arguments, which must all be equal to each other to return true. +> : greater than, takes 1 or more arguments, to return true the 1st argument must be greater than the 2nd argument which must be greater than the 3rd argument and so on... +< : less than, takes 1 or more arguments, to return true the 1st argument must be less than the 2nd argument which must be less than the 3rd argument and so on... + +4.3 - Filter examples +Give me all the setData operations with session id 0xdeadbeef or 0xcafeb33r but not with zxid 0x12341234 -> + +(and (= operation "setData") (or (= client-id 0xdeadbeef) (= client-id 0xcafeb33r)) (not (= zxid 0x12341234))) + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/build.xml b/zookeeper-contrib/zookeeper-contrib-loggraph/build.xml new file mode 100644 index 0000000..07809ff --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/build.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/ivy.xml b/zookeeper-contrib/zookeeper-contrib-loggraph/ivy.xml new file mode 100644 index 0000000..e3a1b48 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/ivy.xml @@ -0,0 +1,44 @@ + + + + + + + + ZooKeeper Graphing + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/pom.xml b/zookeeper-contrib/zookeeper-contrib-loggraph/pom.xml new file mode 100644 index 0000000..0b942f4 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/pom.xml @@ -0,0 +1,93 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-contrib + 3.6.3 + + + zookeeper-contrib-loggraph + jar + Apache ZooKeeper - Contrib - Loggraph + + LogGraph is an application for viewing and filtering zookeeper logs. It can handle transaction logs and message logs. + + + + + org.apache.zookeeper + zookeeper-jute + ${project.version} + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + * + * + + + + + log4j + log4j + + + * + * + + + + + org.eclipse.jetty + jetty-server + + + org.eclipse.jetty + jetty-servlet + + + com.googlecode.json-simple + json-simple + + + + + + + ${project.basedir}/src/main/resources/webapp + + + + + \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterException.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterException.java new file mode 100644 index 0000000..c0912fa --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterException.java @@ -0,0 +1,22 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +public class FilterException extends Exception { + public FilterException(String s) { super(s); } +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterOp.java new file mode 100644 index 0000000..b29c846 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterOp.java @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.graph.filterops.*; + +public abstract class FilterOp { + protected List subOps; + protected List args; + + public enum ArgType { + STRING, NUMBER, SYMBOL + } + + public FilterOp() { + subOps = new ArrayList(); + args = new ArrayList(); + } + + public static FilterOp newOp(String op) throws FilterException { + if (op.equals("or")) + return new OrOp(); + if (op.equals("and")) + return new AndOp(); + if (op.equals("not")) + return new NotOp(); + if (op.equals("xor")) + return new XorOp(); + if (op.equals("=")) + return new EqualsOp(); + if (op.equals("<")) + return new LessThanOp(); + if (op.equals(">")) + return new GreaterThanOp(); + + throw new FilterException("Invalid operation '"+op+"'"); + } + + public void addSubOp(FilterOp op) { + subOps.add(op); + } + + public void addArg(Arg arg) { + args.add(arg); + } + + public abstract boolean matches(LogEntry entry) throws FilterException; + + public String toString() { + String op = "(" + getClass().getName(); + for (FilterOp f : subOps) { + op += " " + f; + } + for (Arg a : args) { + op += " " + a; + } + return op + ")"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterParser.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterParser.java new file mode 100644 index 0000000..cf12e3a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/FilterParser.java @@ -0,0 +1,131 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.PushbackReader; +import java.io.StringReader; +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.zookeeper.graph.filterops.*; + +public class FilterParser { + private PushbackReader reader; + + public FilterParser(String s) { + reader = new PushbackReader(new StringReader(s)); + } + + private String readUntilSpace() throws IOException { + StringBuffer buffer = new StringBuffer(); + + int c = reader.read(); + while (!Character.isWhitespace(c) && c != ')' && c != '(') { + buffer.append((char)c); + c = reader.read(); + if (c == -1) { + break; + } + } + reader.unread(c); + + return buffer.toString().trim(); + } + + private StringArg readStringArg() throws IOException, FilterException { + int c = reader.read(); + int last = 0; + if (c != '"') { + throw new FilterException("Check the parser, trying to read a string that doesn't begin with quotes"); + } + StringBuffer buffer = new StringBuffer(); + while (reader.ready()) { + last = c; + c = reader.read(); + if (c == -1) { + break; + } + + if (c == '"' && last != '\\') { + return new StringArg(buffer.toString()); + } else { + buffer.append((char)c); + } + } + throw new FilterException("Unterminated string"); + } + + private NumberArg readNumberArg() throws IOException, FilterException { + String strval = readUntilSpace(); + + try { + if (strval.startsWith("0x")) { + return new NumberArg(Long.valueOf(strval.substring(2), 16)); + } else { + return new NumberArg(Long.valueOf(strval)); + } + } catch (NumberFormatException e) { + throw new FilterException("Not a number [" + strval + "]\n" + e); + } + } + + private SymbolArg readSymbolArg() throws IOException, FilterException { + return new SymbolArg(readUntilSpace()); + } + + public FilterOp parse() throws IOException, FilterException { + int c = reader.read(); + if (c != '(') { + throw new FilterException("Invalid format"); + } + + String opstr = readUntilSpace(); + FilterOp op = FilterOp.newOp(opstr); + + while (reader.ready()) { + c = reader.read(); + if (c == -1) { + break; + } + if (c == '(') { + reader.unread(c); + op.addSubOp(parse()); + } else if (c == ')') { + return op; + } else if (c == '"') { + reader.unread(c); + op.addArg(readStringArg()); + } else if (Character.isDigit(c) || c == '-' || c == '+') { + reader.unread(c); + op.addArg(readNumberArg()); + } else if (Character.isJavaIdentifierStart(c)) { + reader.unread(c); + op.addArg(readSymbolArg()); + } + } + throw new FilterException("Incomplete filter"); + } + + public static void main(String[] args) throws IOException, FilterException { + if (args.length == 1) { + System.out.println(new FilterParser(args[0]).parse()); + } else { + System.out.println(new FilterParser("(or (and (= session foobar) (= session barfoo)) (= session sdfs))").parse()); + } + } +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/JsonGenerator.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/JsonGenerator.java new file mode 100644 index 0000000..8215833 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/JsonGenerator.java @@ -0,0 +1,224 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import java.io.Writer; +import java.io.OutputStreamWriter; +import java.io.IOException; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.ListIterator; +import java.util.Set; + +public class JsonGenerator { + private JSONObject root; + private Set servers; + + private class Message { + private int from; + private int to; + private long zxid; + + public Message(int from, int to, long zxid) { + this.from = from; + this.to = to; + this.zxid = zxid; + } + + public boolean equals(Message m) { + return (m.from == this.from + && m.to == this.to + && m.zxid == this.zxid); + } + }; + + public JSONObject txnEntry(TransactionEntry e) { + JSONObject event = new JSONObject(); + + event.put("time", Long.toString(e.getTimestamp())); + event.put("client", Long.toHexString(e.getClientId())); + event.put("cxid", Long.toHexString(e.getCxid())); + event.put("zxid", Long.toHexString(e.getZxid())); + event.put("op", e.getOp()); + event.put("extra", e.getExtra()); + event.put("type", "transaction"); + + return event; + } + + /** + Assumes entries are sorted by timestamp. + */ + public JsonGenerator(LogIterator iter) { + servers = new HashSet(); + + Pattern stateChangeP = Pattern.compile("- (LOOKING|FOLLOWING|LEADING)"); + Pattern newElectionP = Pattern.compile("New election. My id = (\\d+), Proposed zxid = (\\d+)"); + Pattern receivedProposalP = Pattern.compile("Notification: (\\d+) \\(n.leader\\), (\\d+) \\(n.zxid\\), (\\d+) \\(n.round\\), .+ \\(n.state\\), (\\d+) \\(n.sid\\), .+ \\(my state\\)"); + Pattern exceptionP = Pattern.compile("xception"); + + root = new JSONObject(); + Matcher m = null; + JSONArray events = new JSONArray(); + root.put("events", events); + + long starttime = Long.MAX_VALUE; + long endtime = 0; + + int leader = 0; + long curEpoch = 0; + boolean newEpoch = false; + + while (iter.hasNext()) { + LogEntry ent = iter.next(); + + if (ent.getTimestamp() < starttime) { + starttime = ent.getTimestamp(); + } + if (ent.getTimestamp() > endtime) { + endtime = ent.getTimestamp(); + } + + if (ent.getType() == LogEntry.Type.TXN) { + events.add(txnEntry((TransactionEntry)ent)); + } else { + Log4JEntry e = (Log4JEntry)ent; + servers.add(e.getNode()); + + if ((m = stateChangeP.matcher(e.getEntry())).find()) { + JSONObject stateChange = new JSONObject(); + stateChange.put("type", "stateChange"); + stateChange.put("time", e.getTimestamp()); + stateChange.put("server", e.getNode()); + stateChange.put("state", m.group(1)); + events.add(stateChange); + + if (m.group(1).equals("LEADING")) { + leader = e.getNode(); + } + } else if ((m = newElectionP.matcher(e.getEntry())).find()) { + Iterator iterator = servers.iterator(); + long zxid = Long.valueOf(m.group(2)); + int count = (int)zxid;// & 0xFFFFFFFFL; + int epoch = (int)Long.rotateRight(zxid, 32);// >> 32; + + if (leader != 0 && epoch > curEpoch) { + JSONObject stateChange = new JSONObject(); + stateChange.put("type", "stateChange"); + stateChange.put("time", e.getTimestamp()); + stateChange.put("server", leader); + stateChange.put("state", "INIT"); + events.add(stateChange); + leader = 0; + } + + while (iterator.hasNext()) { + int dst = iterator.next(); + if (dst != e.getNode()) { + JSONObject msg = new JSONObject(); + msg.put("type", "postmessage"); + msg.put("src", e.getNode()); + msg.put("dst", dst); + msg.put("time", e.getTimestamp()); + msg.put("zxid", m.group(2)); + msg.put("count", count); + msg.put("epoch", epoch); + + events.add(msg); + } + } + } else if ((m = receivedProposalP.matcher(e.getEntry())).find()) { + // Pattern.compile("Notification: \\d+, (\\d+), (\\d+), \\d+, [^,]*, [^,]*, (\\d+)");//, LOOKING, LOOKING, 2 + int src = Integer.valueOf(m.group(4)); + long zxid = Long.valueOf(m.group(2)); + int dst = e.getNode(); + long epoch2 = Long.valueOf(m.group(3)); + + int count = (int)zxid;// & 0xFFFFFFFFL; + int epoch = (int)Long.rotateRight(zxid, 32);// >> 32; + + if (leader != 0 && epoch > curEpoch) { + JSONObject stateChange = new JSONObject(); + stateChange.put("type", "stateChange"); + stateChange.put("time", e.getTimestamp()); + stateChange.put("server", leader); + stateChange.put("state", "INIT"); + events.add(stateChange); + leader = 0; + } + + if (src != dst) { + JSONObject msg = new JSONObject(); + msg.put("type", "delivermessage"); + msg.put("src", src); + msg.put("dst", dst); + msg.put("time", e.getTimestamp()); + msg.put("zxid", zxid); + msg.put("epoch", epoch); + msg.put("count", count); + msg.put("epoch2", epoch2); + + events.add(msg); + } + } else if ((m = exceptionP.matcher(e.getEntry())).find()) { + JSONObject ex = new JSONObject(); + ex.put("type", "exception"); + ex.put("server", e.getNode()); + ex.put("time", e.getTimestamp()); + ex.put("text", e.getEntry()); + events.add(ex); + } + } + JSONObject ex = new JSONObject(); + ex.put("type", "text"); + ex.put("time", ent.getTimestamp()); + String txt = ent.toString(); + ex.put("text", txt); + events.add(ex); + } + // System.out.println("pending messages: "+pendingMessages.size()); + root.put("starttime", starttime); + root.put("endtime", endtime); + + JSONArray serversarray = new JSONArray(); + root.put("servers", serversarray); + + Iterator iterator = servers.iterator(); + while (iterator.hasNext()) { + serversarray.add(iterator.next()); + } + } + + public String toString() { + return JSONValue.toJSONString(root); + } + + public static void main(String[] args) throws Exception { + MergedLogSource src = new MergedLogSource(args); + LogIterator iter = src.iterator(); + System.out.println(new JsonGenerator(iter)); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/Log4JEntry.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/Log4JEntry.java new file mode 100644 index 0000000..0edc146 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/Log4JEntry.java @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +public class Log4JEntry extends LogEntry { + public Log4JEntry(long timestamp, int node, String entry) { + super(timestamp); + setAttribute("log-text", entry); + setAttribute("node", new Integer(node)); + } + + public String getEntry() { + return (String) getAttribute("log-text"); + } + + public String toString() { + return "" + getTimestamp() + "::::" + getNode() + "::::" + getEntry(); + } + + public int getNode() { + return (Integer) getAttribute("node"); + } + + public Type getType() { return LogEntry.Type.LOG4J; } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/Log4JSource.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/Log4JSource.java new file mode 100644 index 0000000..84a9d98 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/Log4JSource.java @@ -0,0 +1,391 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.File; +import java.io.InputStreamReader; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.util.ArrayList; +import java.util.Date; +import java.text.SimpleDateFormat; +import java.text.ParseException; +import java.util.Calendar; +import java.util.GregorianCalendar; + +import java.io.EOFException; +import java.io.Closeable; +import java.io.FileNotFoundException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Log4JSource implements LogSource { + private static final Logger LOG = LoggerFactory.getLogger(Log4JSource.class); + + private static final int skipN = 10000; + private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss,SSS"; + + private LogSkipList skiplist = null; + + private String file = null; + private long starttime = 0; + private long endtime = 0; + private int serverid = 0; + private long size = 0; + + private Pattern timep; + + public boolean overlapsRange(long starttime, long endtime) { + return (starttime <= this.endtime && endtime >= this.starttime); + } + + public long size() { return size; } + public long getStartTime() { return starttime; } + public long getEndTime() { return endtime; } + public LogSkipList getSkipList() { return skiplist; } + + private class Log4JSourceIterator implements LogIterator { + private RandomAccessFileReader in; + private LogEntry next = null; + private long starttime = 0; + private long endtime = 0; + private String buf = ""; + private Log4JSource src = null; + private long skippedAtStart = 0; + private SimpleDateFormat dateformat = null; + private FilterOp filter = null; + + public Log4JSourceIterator(Log4JSource src, long starttime, long endtime) throws IllegalArgumentException, FilterException { + this(src, starttime, endtime, null); + } + + public Log4JSourceIterator(Log4JSource src, long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException { + + this.dateformat = new SimpleDateFormat(DATE_FORMAT); + this.src = src; + this.starttime = starttime; + this.endtime = endtime; + + File f = new File(src.file); + try { + in = new RandomAccessFileReader(f); + } catch (FileNotFoundException e) { + throw new IllegalArgumentException("Bad file passed in (" + src.file +") cannot open:" + e); + } + + // skip to the offset of latest skip point before starttime + LogSkipList.Mark start = src.getSkipList().findMarkBefore(starttime); + try { + in.seek(start.getBytes()); + skippedAtStart = start.getEntriesSkipped(); + } catch (IOException ioe) { + // if we can't skip, we should just read from the start + } + + LogEntry e; + while ((e = readNextEntry()) != null && e.getTimestamp() < endtime) { + if (e.getTimestamp() >= starttime && (filter == null || filter.matches(e))) { + next = e; + return; + } + skippedAtStart++; + } + this.filter = filter; + } + + synchronized public long size() throws IOException { + if (LOG.isTraceEnabled()) { + LOG.trace("size() called"); + } + + if (this.endtime >= src.getEndTime()) { + return src.size() - skippedAtStart; + } + + long pos = in.getPosition(); + + if (LOG.isTraceEnabled()) { + LOG.trace("saved pos () = " + pos); + } + + LogEntry e; + + LogSkipList.Mark lastseg = src.getSkipList().findMarkBefore(this.endtime); + in.seek(lastseg.getBytes()); + buf = ""; // clear the buf so we don't get something we read before we sought + // number of entries skipped to get to the end of the iterator, less the number skipped to get to the start + long count = lastseg.getEntriesSkipped() - skippedAtStart; + + while ((e = readNextEntry()) != null) { + if (LOG.isTraceEnabled()) { + //LOG.trace(e); + } + if (e.getTimestamp() > this.endtime) { + break; + } + count++; + } + in.seek(pos); + buf = ""; + + if (LOG.isTraceEnabled()) { + LOG.trace("size() = " + count); + } + + return count; + } + + synchronized private LogEntry readNextEntry() { + try { + try { + while (true) { + String line = in.readLine(); + if (line == null) { + break; + } + + Matcher m = src.timep.matcher(line); + if (m.lookingAt()) { + if (buf.length() > 0) { + LogEntry e = new Log4JEntry(src.timestampFromText(dateformat, buf), src.getServerId(), buf); + buf = line; + return e; + } + buf = line; + } else if (buf.length() > 0) { + buf += line + "\n"; + } + } + } catch (EOFException eof) { + // ignore, we've simply come to the end of the file + } + if (buf.length() > 0) { + LogEntry e = new Log4JEntry(src.timestampFromText(dateformat, buf), src.getServerId(), buf); + buf = ""; + return e; + } + } catch (Exception e) { + LOG.error("Error reading next entry in file (" + src.file + "): " + e); + return null; + } + return null; + } + + public boolean hasNext() { + return next != null; + } + + public LogEntry next() throws NoSuchElementException { + LogEntry ret = next; + LogEntry e = readNextEntry(); + + if (filter != null) { + try { + while (e != null && !filter.matches(e)) { + e = readNextEntry(); + } + } catch (FilterException fe) { + throw new NoSuchElementException(e.toString()); + } + } + + if (e != null && e.getTimestamp() < endtime) { + next = e; + } else { + next = null; + } + return ret; + } + + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException("remove not supported for L4J logs"); + } + + public void close() throws IOException { + in.close(); + } + + public String toString() { + String size; + try { + size = new Long(size()).toString(); + } catch (IOException ioe) { + size = "Unable to read"; + } + return "Log4JSourceIterator(start=" + starttime + ", end=" + endtime + ", size=" + size + ")"; + } + } + + public LogIterator iterator(long starttime, long endtime) throws IllegalArgumentException { + try { + return iterator(starttime, endtime, null); + } catch (FilterException fe) { + assert(false); //"This should never happen, you can't have a filter exception without a filter"); + return null; + } + } + + public LogIterator iterator(long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException{ + // sanitise start and end times + if (endtime < starttime) { + throw new IllegalArgumentException("End time (" + endtime + ") must be greater or equal to starttime (" + starttime + ")"); + } + + return new Log4JSourceIterator(this, starttime, endtime, filter); + } + + public LogIterator iterator() throws IllegalArgumentException { + return iterator(starttime, endtime+1); + } + + public Log4JSource(String file) throws IOException { + this.file=file; + + timep = Pattern.compile("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3})"); + skiplist = new LogSkipList(); + init(); + } + + private static long timestampFromText(SimpleDateFormat format, String s) { + Date d = null; + try { + d = format.parse(s); + } catch (ParseException e) { + return 0; + } + Calendar c = new GregorianCalendar(); + c.setTime(d); + return c.getTimeInMillis(); + } + + private void init() throws IOException { + File f = new File(file); + RandomAccessFileReader in = new RandomAccessFileReader(f); + SimpleDateFormat dateformat = new SimpleDateFormat(DATE_FORMAT); + Pattern idp = Pattern.compile("\\[myid:(\\d+)\\]"); + + long lastFp = in.getPosition(); + String line = in.readLine(); + Matcher m = null; + + // if we have read data from the file, and it matchs the timep pattern + if ((line != null) && (m = timep.matcher(line)).lookingAt()) { + starttime = timestampFromText(dateformat, m.group(1)); + } else { + throw new IOException("Invalid log4j format. First line doesn't start with time"); + } + + /* + Count number of log entries. Any line starting with a timestamp counts as an entry + */ + String lastentry = line; + try { + while (line != null) { + m = timep.matcher(line); + if (m.lookingAt()) { + if (size % skipN == 0) { + long time = timestampFromText(dateformat, m.group(1)); + skiplist.addMark(time, lastFp, size); + } + size++; + lastentry = line; + } + if (serverid == 0 && (m = idp.matcher(line)).find()) { + serverid = Integer.valueOf(m.group(1)); + } + + lastFp = in.getPosition(); + line = in.readLine(); + } + } catch (EOFException eof) { + // ignore, simply end of file, though really (line!=null) should have caught this + } finally { + in.close(); + } + + m = timep.matcher(lastentry); + if (m.lookingAt()) { + endtime = timestampFromText(dateformat, m.group(1)); + } else { + throw new IOException("Invalid log4j format. Last line doesn't start with time"); + } + } + + public String toString() { + return "Log4JSource(file=" + file + ", size=" + size + ", start=" + starttime + ", end=" + endtime +", id=" + serverid +")"; + } + + public static void main(String[] args) throws IOException { + final Log4JSource s = new Log4JSource(args[0]); + System.out.println(s); + + LogIterator iter; + + if (args.length == 3) { + final long starttime = Long.valueOf(args[1]); + final long endtime = Long.valueOf(args[2]); + iter = s.iterator(starttime, endtime); + + Thread t1 = new Thread() { public void run () { + + LogIterator iter = s.iterator(starttime, endtime); + System.out.println(iter); + try { + iter.close(); + } catch (IOException ioe) { + System.out.println(ioe.getMessage()); + } + }; }; + Thread t2 = new Thread() { public void run () { + + LogIterator iter = s.iterator(starttime, endtime); + System.out.println(iter); + try { + iter.close(); + } catch (IOException ioe) { + System.out.println(ioe.getMessage()); + } + }; }; + Thread t3 = new Thread() { public void run () { + + LogIterator iter = s.iterator(starttime, endtime); + System.out.println(iter); + }; }; + t1.start(); + t2.start(); + // t3.start(); + } else { + iter = s.iterator(); + } + + /*while (iter.hasNext()) { + System.out.println(iter.next()); + }*/ + iter.close(); + } + + public int getServerId() { + return serverid; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogEntry.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogEntry.java new file mode 100644 index 0000000..27973ba --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogEntry.java @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public abstract class LogEntry implements Serializable { + private Map attributes; + + public enum Type { UNKNOWN, LOG4J, TXN }; + + public LogEntry(long timestamp) { + attributes = new HashMap(); + setAttribute("timestamp", new Long(timestamp)); + } + + public long getTimestamp() { + return (Long)getAttribute("timestamp"); + } + + public abstract Type getType(); + + public void setAttribute(String key, Object v) { + attributes.put(key, v); + } + + public Object getAttribute(String key) { + return attributes.get(key); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogIterator.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogIterator.java new file mode 100644 index 0000000..9af440b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogIterator.java @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.Closeable; +import java.util.Iterator; +import java.io.IOException; + +public interface LogIterator extends Iterator, Closeable { + long size() throws IOException;; +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogServer.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogServer.java new file mode 100644 index 0000000..5cffcdd --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogServer.java @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.ServletException; + +import java.io.IOException; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; + +import org.apache.zookeeper.graph.servlets.*; + +public class LogServer extends ServletContextHandler { + public LogServer(MergedLogSource src) throws Exception { + super(ServletContextHandler.SESSIONS); + setContextPath("/"); + + addServlet(new ServletHolder(new StaticContent()),"/graph/*"); + + addServlet(new ServletHolder(new Fs()),"/fs"); + addServlet(new ServletHolder(new GraphData(src)), "/data"); + addServlet(new ServletHolder(new FileLoader(src)), "/loadfile"); + addServlet(new ServletHolder(new NumEvents(src)), "/info"); + addServlet(new ServletHolder(new Throughput(src)), "/throughput"); + } + + public static void main(String[] args) { + try { + MergedLogSource src = new MergedLogSource(args); + System.out.println(src); + + Server server = new Server(8182); + server.setHandler(new LogServer(src)); + + server.start(); + server.join(); + + } catch (Exception e) { + // Something is wrong. + e.printStackTrace(); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogSkipList.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogSkipList.java new file mode 100644 index 0000000..e744442 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogSkipList.java @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.util.List; +import java.util.LinkedList; +import java.util.NoSuchElementException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** +Generic skip list for holding a rough index of a log file. When the log file is loaded, this +index is built by adding a mark every n entries. Then when a specific time position is requested +from the file, a point at most n-1 entries before the time position can be jumped to. + +*/ +public class LogSkipList { + private static final Logger LOG = LoggerFactory.getLogger(LogSkipList.class); + + private LinkedList marks; + + public class Mark { + private long time; + private long bytes; + private long skipped; + + public Mark(long time, long bytes, long skipped) { + this.time = time; + this.bytes = bytes; + this.skipped = skipped; + } + + public long getTime() { return this.time; } + public long getBytes() { return this.bytes; } + public long getEntriesSkipped() { return this.skipped; } + + public String toString() { + return "Mark(time=" + time + ", bytes=" + bytes + ", skipped=" + skipped + ")"; + } + }; + + public LogSkipList() { + if (LOG.isTraceEnabled()) { + LOG.trace("New skip list"); + } + marks = new LinkedList(); + } + + public void addMark(long time, long bytes, long skipped) { + if (LOG.isTraceEnabled()) { + LOG.trace("addMark (time:" + time + ", bytes: " + bytes + ", skipped: " + skipped + ")"); + } + marks.add(new Mark(time, bytes, skipped)); + } + + /** + Find the last mark in the skip list before time. + */ + public Mark findMarkBefore(long time) throws NoSuchElementException { + if (LOG.isTraceEnabled()) { + LOG.trace("findMarkBefore(" + time + ")"); + } + + Mark last = marks.getFirst(); + for (Mark m: marks) { + if (m.getTime() > time) { + break; + } + last = m; + } + + if (LOG.isTraceEnabled()) { + LOG.trace("return " + last ); + } + + return last; + } + +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogSource.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogSource.java new file mode 100644 index 0000000..9845c7f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/LogSource.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; +import java.util.Iterator; + +public interface LogSource extends Iterable { + public LogIterator iterator(long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException; + + public LogIterator iterator(long starttime, long endtime) throws IllegalArgumentException; + + public LogIterator iterator() throws IllegalArgumentException; + + public boolean overlapsRange(long starttime, long endtime); + + public long size(); + public long getStartTime(); + public long getEndTime(); +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/MeasureThroughput.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/MeasureThroughput.java new file mode 100644 index 0000000..a42354c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/MeasureThroughput.java @@ -0,0 +1,104 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.IOException; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.DataOutputStream; +import java.io.PrintStream; + +import java.util.HashSet; +import java.util.Set; + +public class MeasureThroughput { + private static final int MS_PER_SEC = 1000; + private static final int MS_PER_MIN = MS_PER_SEC*60; + private static final int MS_PER_HOUR = MS_PER_MIN*60; + + public static void main(String[] args) throws IOException { + MergedLogSource source = new MergedLogSource(args); + + PrintStream ps_ms = new PrintStream(new BufferedOutputStream(new FileOutputStream("throughput-ms.out"))); + PrintStream ps_sec = new PrintStream(new BufferedOutputStream(new FileOutputStream("throughput-sec.out"))); + PrintStream ps_min = new PrintStream(new BufferedOutputStream(new FileOutputStream("throughput-min.out"))); + PrintStream ps_hour = new PrintStream(new BufferedOutputStream(new FileOutputStream("throughput-hour.out"))); + LogIterator iter; + + System.out.println(source); + iter = source.iterator(); + long currentms = 0; + long currentsec = 0; + long currentmin = 0; + long currenthour = 0; + Set zxids_ms = new HashSet(); + long zxid_sec = 0; + long zxid_min = 0; + long zxid_hour = 0; + + while (iter.hasNext()) { + LogEntry e = iter.next(); + TransactionEntry cxn = (TransactionEntry)e; + + long ms = cxn.getTimestamp(); + long sec = ms/MS_PER_SEC; + long min = ms/MS_PER_MIN; + long hour = ms/MS_PER_HOUR; + + if (currentms != ms && currentms != 0) { + ps_ms.println("" + currentms + " " + zxids_ms.size()); + + zxid_sec += zxids_ms.size(); + zxid_min += zxids_ms.size(); + zxid_hour += zxids_ms.size(); + zxids_ms.clear(); + } + + if (currentsec != sec && currentsec != 0) { + ps_sec.println("" + currentsec*MS_PER_SEC + " " + zxid_sec); + + zxid_sec = 0; + } + + if (currentmin != min && currentmin != 0) { + ps_min.println("" + currentmin*MS_PER_MIN + " " + zxid_min); + + zxid_min = 0; + } + + if (currenthour != hour && currenthour != 0) { + ps_hour.println("" + currenthour*MS_PER_HOUR + " " + zxid_hour); + + zxid_hour = 0; + } + + currentms = ms; + currentsec = sec; + currentmin = min; + currenthour = hour; + + zxids_ms.add(cxn.getZxid()); + } + + iter.close(); + ps_ms.close(); + ps_sec.close(); + ps_min.close(); + ps_hour.close(); + } +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/MergedLogSource.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/MergedLogSource.java new file mode 100644 index 0000000..bb789d3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/MergedLogSource.java @@ -0,0 +1,219 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.IOException; +import java.text.DateFormat; +import java.util.Date; +import java.util.zip.Adler32; +import java.util.zip.Checksum; +import java.util.HashMap; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.TraceFormatter; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnHeader; + +import org.apache.zookeeper.ZooDefs.OpCode; + +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.SetACLTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; + +import java.io.Closeable; +import java.io.FileNotFoundException; +import java.util.Vector; +import java.util.Iterator; +import java.util.Collections; +import java.util.NoSuchElementException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MergedLogSource implements LogSource { + private static final Logger LOG = LoggerFactory.getLogger(MergedLogSource.class); + private Vector sources = null; + private long starttime = 0; + private long endtime = 0; + private long size = 0; + + public boolean overlapsRange(long starttime, long endtime) { + return (starttime <= this.endtime && endtime >= this.starttime); + } + + public long size() { return size; } + public long getStartTime() { return starttime; } + public long getEndTime() { return endtime; } + + private class MergedLogSourceIterator implements LogIterator { + private LogEntry next = null; + private long start = 0; + private long end = 0; + private MergedLogSource src = null; + private LogIterator[] sources = null; + private LogEntry[] nexts = null; + private FilterOp filter = null; + + public MergedLogSourceIterator(MergedLogSource src, long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException { + Vector iters = new Vector(); + for (LogSource s : src.sources) { + if (s.overlapsRange(starttime, endtime)) { + iters.add(s.iterator(starttime, endtime, filter)); + } + } + + sources = new LogIterator[iters.size()]; + sources = iters.toArray(sources); + nexts = new LogEntry[iters.size()]; + for (int i = 0; i < sources.length; i++) { + if (sources[i].hasNext()) + nexts[i] = sources[i].next(); + } + this.filter = filter; + } + + public MergedLogSourceIterator(MergedLogSource src, long starttime, long endtime) throws IllegalArgumentException, FilterException { + this(src, starttime, endtime, null); + } + + public long size() throws IOException { + long size = 0; + for (LogIterator i : sources) { + size += i.size(); + } + return size; + } + + public boolean hasNext() { + for (LogEntry n : nexts) { + if (n != null) return true; + } + return false; + } + + public LogEntry next() { + int min = -1; + for (int i = 0; i < nexts.length; i++) { + if (nexts[i] != null) { + if (min == -1) { + min = i; + } else if (nexts[i].getTimestamp() < nexts[min].getTimestamp()) { + min = i; + } + } + } + if (min == -1) { + return null; + } else { + LogEntry e = nexts[min]; + nexts[min] = sources[min].next(); + return e; + } + } + + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException("remove not supported for Merged logs"); + } + + public void close() throws IOException { + for (LogIterator i : sources) { + i.close(); + } + } + } + + public LogIterator iterator(long starttime, long endtime) throws IllegalArgumentException { + try { + return iterator(starttime, endtime, null); + } catch (FilterException fe) { + assert(false); // shouldn't happen without filter + return null; + } + } + + public LogIterator iterator(long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException { + // sanitise start and end times + if (endtime < starttime) { + throw new IllegalArgumentException("End time (" + endtime + ") must be greater or equal to starttime (" + starttime + ")"); + } + + return new MergedLogSourceIterator(this, starttime, endtime, filter); + } + + public LogIterator iterator() throws IllegalArgumentException { + return iterator(starttime, endtime+1); + } + + public MergedLogSource(String[] files) throws IOException { + sources = new Vector(); + for (String f : files) { + addSource(f); + } + } + + public void addSource(String f) throws IOException { + LogSource s = null; + if (TxnLogSource.isTransactionFile(f)) { + s = new TxnLogSource(f); + } else { + s = new Log4JSource(f); + } + + size += s.size(); + endtime = s.getEndTime() > endtime ? s.getEndTime() : endtime; + starttime = s.getStartTime() < starttime || starttime == 0 ? s.getStartTime() : starttime; + sources.add(s); + } + + public String toString() { + String s = "MergedLogSource(size=" + size + ", start=" + starttime + ", end=" + endtime +")"; + for (LogSource src : sources) { + s += "\n\t- " +src; + } + return s; + } + + public static void main(String[] args) throws IOException { + System.out.println("Time: " + System.currentTimeMillis()); + MergedLogSource s = new MergedLogSource(args); + System.out.println(s); + + LogIterator iter; + + iter = s.iterator(); + System.out.println("Time: " + System.currentTimeMillis()); + System.out.println("Iterator Size: " + iter.size()); + System.out.println("Time: " + System.currentTimeMillis()); + /* while (iter.hasNext()) { + System.out.println(iter.next()); + }*/ + iter.close(); + System.out.println("Time: " + System.currentTimeMillis()); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/RandomAccessFileReader.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/RandomAccessFileReader.java new file mode 100644 index 0000000..13a41a5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/RandomAccessFileReader.java @@ -0,0 +1,329 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.File; +import java.io.Reader; +import java.io.IOException; +import java.io.EOFException; +import java.io.RandomAccessFile; +import java.io.FileNotFoundException; + +import java.io.DataInputStream; +import java.io.ByteArrayInputStream; +import java.io.DataInput; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RandomAccessFileReader extends Reader implements DataInput { + private static final Logger LOG = LoggerFactory.getLogger(RandomAccessFileReader.class); + private RandomAccessFile file; + private byte[] buffer; + private int buffersize; + private int bufferoffset; + private long fileoffset; + private long fp; + + private static final int DEFAULT_BUFFER_SIZE = 512*1024; // 512k + private int point = 0; + + public RandomAccessFileReader(File f) throws FileNotFoundException { + file = new RandomAccessFile(f, "r"); + if (LOG.isDebugEnabled()) { + try { + LOG.debug("Opened file(" + f + ") with FD (" + file.getFD() + ")"); + } catch (IOException ioe) { + LOG.debug("Opened file(" + f + ") coulds get FD"); + } + } + + buffer = new byte[DEFAULT_BUFFER_SIZE]; + buffersize = 0; + bufferoffset = 0; + fileoffset = 0; + fp = 0; + } + + /** + fill the buffer from the file. + fp keeps track of the file pointer. + fileoffset is the offset into the file to where the buffer came from. + */ + private int fill() throws IOException { + fileoffset = fp; + int read = file.read(buffer, 0, buffer.length); + + if (LOG.isDebugEnabled()) { + String buf = new String(buffer, 0, 40, "UTF-8"); + LOG.debug("fill(buffer=" + buf + ")"); + } + + if (read == -1) { // eof reached + buffersize = 0; + } else { + buffersize = read; + } + fp += buffersize; + bufferoffset = 0; + + return buffersize; + } + + /** + * Reader interface + */ + public boolean markSupported() { return false; } + + /** + copy what we can from buffer. if it's not enough, fill buffer again and copy again + */ + synchronized public int read(char[] cbuf, int off, int len) throws IOException { + // This could be faster, but probably wont be used + byte[] b = new byte[2]; + int bytesread = 0; + while (len > 0) { + int read = read(b, 0, 2); + bytesread += read; + if (read < 2) { + return bytesread; + } + cbuf[off] = (char)((b[0] << 8) | (b[1] & 0xff)); + off += read; + len -= read; + } + + return bytesread; + } + + synchronized public int read(byte[] buf, int off, int len) throws IOException { + if (LOG.isTraceEnabled()) { + LOG.trace("read(buf, off=" + off + ", len=" + len); + } + + int read = 0; + while (len > 0) { + if (buffersize == 0) { + fill(); + if (buffersize == 0) { + break; + } + } + + int tocopy = Math.min(len, buffersize); + if (LOG.isTraceEnabled()) { + LOG.trace("tocopy=" + tocopy); + } + + System.arraycopy(buffer, bufferoffset, buf, off, tocopy); + buffersize -= tocopy; + bufferoffset += tocopy; + + len -= tocopy; + read += tocopy; + off += tocopy; + } + if (LOG.isTraceEnabled()) { + LOG.trace("read=" + read); + } + + return read; + } + + public void close() throws IOException { + file.close(); + } + + /** + * Seek interface + */ + public long getPosition() { + return bufferoffset + fileoffset; + } + + synchronized public void seek(long pos) throws IOException { + if (LOG.isDebugEnabled()) { + LOG.debug("seek(" + pos + ")"); + } + file.seek(pos); + fp = pos; + buffersize = 0; // force a buffer fill on next read + } + + /** + works like the usual readLine but disregards \r to make things easier + */ + synchronized public String readLine() throws IOException { + StringBuffer s = null; + + // go through buffer until i find a \n, if i reach end of buffer first, put whats in buffer into string buffer, + // repeat + buffering: + for (;;) { + if (buffersize == 0) { + fill(); + if (buffersize == 0) { + break; + } + } + + for (int i = 0; i < buffersize; i++) { + if (buffer[bufferoffset + i] == '\n') { + if (i > 0) { // if \n is first char in buffer, leave the string buffer empty + if (s == null) { s = new StringBuffer(); } + s.append(new String(buffer, bufferoffset, i, "UTF-8")); + } + bufferoffset += i+1; + buffersize -= i+1; + break buffering; + } + } + + // We didn't find \n, read the whole buffer into string buffer + if (s == null) { s = new StringBuffer(); } + s.append(new String(buffer, bufferoffset, buffersize, "UTF-8")); + buffersize = 0; + } + + if (s == null) { + return null; + } else { + return s.toString(); + } + } + + /** + DataInput interface + */ + public void readFully(byte[] b) throws IOException { + readFully(b, 0, b.length); + } + + public void readFully(byte[] b, int off, int len) throws IOException + { + while (len > 0) { + int read = read(b, off, len); + len -= read; + off += read; + + if (read == 0) { + throw new EOFException("End of file reached"); + } + } + } + + public int skipBytes(int n) throws IOException { + seek(getPosition() + n); + return n; + } + + public boolean readBoolean() throws IOException { + return (readByte() != 0); + } + + public byte readByte() throws IOException { + byte[] b = new byte[1]; + readFully(b, 0, 1); + return b[0]; + } + + public int readUnsignedByte() throws IOException { + return (int)readByte(); + } + + public short readShort() throws IOException { + byte[] b = new byte[2]; + readFully(b, 0, 2); + return (short)((b[0] << 8) | (b[1] & 0xff)); + } + + public int readUnsignedShort() throws IOException { + byte[] b = new byte[2]; + readFully(b, 0, 2); + return (((b[0] & 0xff) << 8) | (b[1] & 0xff)); + } + + public char readChar() throws IOException { + return (char)readShort(); + } + + public int readInt() throws IOException { + byte[] b = new byte[4]; + readFully(b, 0, 4); + return (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff)); + } + + public long readLong() throws IOException { + byte[] b = new byte[8]; + readFully(b, 0, 8); + + return (((long)(b[0] & 0xff) << 56) | ((long)(b[1] & 0xff) << 48) | + ((long)(b[2] & 0xff) << 40) | ((long)(b[3] & 0xff) << 32) | + ((long)(b[4] & 0xff) << 24) | ((long)(b[5] & 0xff) << 16) | + ((long)(b[6] & 0xff) << 8) | ((long)(b[7] & 0xff))); + } + + public float readFloat() throws IOException { + return Float.intBitsToFloat(readInt()); + } + + public double readDouble() throws IOException { + return Double.longBitsToDouble(readLong()); + } + + public String readUTF() throws IOException { + int len = readUnsignedShort(); + byte[] bytes = new byte[len+2]; + bytes[0] = (byte)((len >> 8) & 0xFF); + bytes[1] = (byte)(len & 0xFF); + readFully(bytes, 2, len); + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes)); + return dis.readUTF(); + } + + public static void main(String[] args) throws IOException { + RandomAccessFileReader f = new RandomAccessFileReader(new File(args[0])); + + long pos0 = f.getPosition(); + for (int i = 0; i < 5; i++) { + System.out.println(f.readLine()); + } + System.out.println("============="); + long pos1 = f.getPosition(); + System.out.println("pos: " + pos1); + for (int i = 0; i < 5; i++) { + System.out.println(f.readLine()); + } + System.out.println("============="); + f.seek(pos1); + for (int i = 0; i < 5; i++) { + System.out.println(f.readLine()); + } + System.out.println("============="); + f.seek(pos0); + for (int i = 0; i < 5; i++) { + System.out.println(f.readLine()); + } + long pos2 = f.getPosition(); + System.out.println("============="); + System.out.println(f.readLine()); + f.seek(pos2); + System.out.println(f.readLine()); + f.close(); + } +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/TransactionEntry.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/TransactionEntry.java new file mode 100644 index 0000000..33c7189 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/TransactionEntry.java @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +public class TransactionEntry extends LogEntry { + public TransactionEntry(long timestamp, long clientId, long Cxid, long Zxid, String op) { + this(timestamp, clientId, Cxid, Zxid, op, ""); + } + + public TransactionEntry(long timestamp, long clientId, long Cxid, long Zxid, String op, String extra) { + super(timestamp); + setAttribute("client-id", new Long(clientId)); + setAttribute("cxid", new Long(Cxid)); + setAttribute("zxid", new Long(Zxid)); + setAttribute("operation", op); + setAttribute("extra", extra); + } + + public long getClientId() { + return (Long)getAttribute("client-id"); + } + + public long getCxid() { + return (Long)getAttribute("cxid"); + } + + public long getZxid() { + return (Long)getAttribute("zxid"); + } + + public String getOp() { + return (String)getAttribute("operation"); + } + + public String getExtra() { + return (String)getAttribute("extra"); + } + + public String toString() { + return getTimestamp() + ":::session(0x" + Long.toHexString(getClientId()) + ") cxid(0x" + Long.toHexString(getCxid()) + ") zxid(0x" + Long.toHexString(getZxid()) + ") op(" + getOp() + ") extra(" + getExtra() +")"; + } + + public Type getType() { return LogEntry.Type.TXN; } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/TxnLogSource.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/TxnLogSource.java new file mode 100644 index 0000000..ad2e258 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/TxnLogSource.java @@ -0,0 +1,380 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.IOException; +import java.text.DateFormat; +import java.util.Date; +import java.util.zip.Adler32; +import java.util.zip.Checksum; +import java.util.HashMap; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.TraceFormatter; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnHeader; + +import org.apache.zookeeper.ZooDefs.OpCode; + +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.SetACLTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; + +import java.io.File; +import java.io.Closeable; +import java.io.FileNotFoundException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TxnLogSource implements LogSource { + private static final Logger LOG = LoggerFactory.getLogger(TxnLogSource.class); + + private LogSkipList skiplist = null; + private static final int skipN = 10000; + + private String file = null; + private long starttime = 0; + private long endtime = 0; + private long size = 0; + + public boolean overlapsRange(long starttime, long endtime) { + return (starttime <= this.endtime && endtime >= this.starttime); + } + + public long size() { return size; } + public long getStartTime() { return starttime; } + public long getEndTime() { return endtime; } + public LogSkipList getSkipList() { return skiplist; } + + public static boolean isTransactionFile(String file) throws IOException { + RandomAccessFileReader reader = new RandomAccessFileReader(new File(file)); + BinaryInputArchive logStream = new BinaryInputArchive(reader); + FileHeader fhdr = new FileHeader(); + fhdr.deserialize(logStream, "fileheader"); + reader.close(); + + return fhdr.getMagic() == FileTxnLog.TXNLOG_MAGIC; + } + + private class TxnLogSourceIterator implements LogIterator { + private LogEntry next = null; + private long starttime = 0; + private long endtime = 0; + private TxnLogSource src = null; + private RandomAccessFileReader reader = null; + private BinaryInputArchive logStream = null; + private long skippedAtStart = 0; + private FilterOp filter = null; + + public TxnLogSourceIterator(TxnLogSource src, long starttime, long endtime) throws IllegalArgumentException, FilterException { + this(src,starttime,endtime,null); + } + + public TxnLogSourceIterator(TxnLogSource src, long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException { + try { + this.src = src; + this.starttime = starttime; + this.endtime = endtime; + reader = new RandomAccessFileReader(new File(src.file)); + logStream = new BinaryInputArchive(reader); + FileHeader fhdr = new FileHeader(); + fhdr.deserialize(logStream, "fileheader"); + } catch (Exception e) { + throw new IllegalArgumentException("Cannot open transaction log ("+src.file+") :" + e); + } + + LogSkipList.Mark start = src.getSkipList().findMarkBefore(starttime); + try { + reader.seek(start.getBytes()); + skippedAtStart = start.getEntriesSkipped(); + } catch (IOException ioe) { + // if we can't skip, we should just read from the start + } + + this.filter = filter; + + LogEntry e; + while ((e = readNextEntry()) != null && e.getTimestamp() < endtime) { + if (e.getTimestamp() >= starttime && (filter == null || filter.matches(e)) ) { + next = e; + return; + } + skippedAtStart++; + } + + + } + + public long size() throws IOException { + if (this.endtime >= src.getEndTime()) { + return src.size() - skippedAtStart; + } + + long pos = reader.getPosition(); + LogEntry e; + + LogSkipList.Mark lastseg = src.getSkipList().findMarkBefore(this.endtime); + reader.seek(lastseg.getBytes()); + // number of entries skipped to get to the end of the iterator, less the number skipped to get to the start + long count = lastseg.getEntriesSkipped() - skippedAtStart; + + while ((e = readNextEntry()) != null) { + if (e.getTimestamp() > this.endtime) { + break; + } + count++; + } + reader.seek(pos);; + + return count; + } + + private LogEntry readNextEntry() { + LogEntry e = null; + try { + long crcValue; + byte[] bytes; + try { + crcValue = logStream.readLong("crcvalue"); + + bytes = logStream.readBuffer("txnEntry"); + } catch (EOFException ex) { + return null; + } + + if (bytes.length == 0) { + return null; + } + Checksum crc = new Adler32(); + crc.update(bytes, 0, bytes.length); + if (crcValue != crc.getValue()) { + throw new IOException("CRC doesn't match " + crcValue + + " vs " + crc.getValue()); + } + + TxnLogEntry logEntry = SerializeUtils.deserializeTxn(bytes); + TxnHeader hdr = logEntry.getHeader(); + Record r = logEntry.getTxn(); + + switch (hdr.getType()) { + case OpCode.createSession: { + e = new TransactionEntry(hdr.getTime(), hdr.getClientId(), hdr.getCxid(), hdr.getZxid(), "createSession"); + } + break; + case OpCode.closeSession: { + e = new TransactionEntry(hdr.getTime(), hdr.getClientId(), hdr.getCxid(), hdr.getZxid(), "closeSession"); + } + break; + case OpCode.create: + if (r != null) { + CreateTxn create = (CreateTxn)r; + String path = create.getPath(); + e = new TransactionEntry(hdr.getTime(), hdr.getClientId(), hdr.getCxid(), hdr.getZxid(), "create", path); + } + break; + case OpCode.setData: + if (r != null) { + SetDataTxn set = (SetDataTxn)r; + String path = set.getPath(); + e = new TransactionEntry(hdr.getTime(), hdr.getClientId(), hdr.getCxid(), hdr.getZxid(), "setData", path); + } + break; + case OpCode.setACL: + if (r != null) { + SetACLTxn setacl = (SetACLTxn)r; + String path = setacl.getPath(); + e = new TransactionEntry(hdr.getTime(), hdr.getClientId(), hdr.getCxid(), hdr.getZxid(), "setACL", path); + } + break; + case OpCode.error: + if (r != null) { + ErrorTxn error = (ErrorTxn)r; + + e = new TransactionEntry(hdr.getTime(), hdr.getClientId(), hdr.getCxid(), hdr.getZxid(), "error", "Error: " + error.getErr()); + } + break; + default: + LOG.info("Unknown op: " + hdr.getType()); + break; + } + + if (logStream.readByte("EOR") != 'B') { + throw new EOFException("Last transaction was partial."); + } + } catch (Exception ex) { + LOG.error("Error reading transaction from (" + src.file + ") :" + e); + return null; + } + return e; + } + + public boolean hasNext() { + return next != null; + } + + public LogEntry next() throws NoSuchElementException { + LogEntry ret = next; + LogEntry e = readNextEntry(); + + if (filter != null) { + try { + while (e != null && !filter.matches(e)) { + e = readNextEntry(); + } + } catch (FilterException fe) { + throw new NoSuchElementException(fe.toString()); + } + } + if (e != null && e.getTimestamp() < endtime) { + next = e; + } else { + next = null; + } + return ret; + } + + public void remove() throws UnsupportedOperationException { + throw new UnsupportedOperationException("remove not supported for Txn logs"); + } + + public void close() throws IOException { + reader.close(); + } + } + + public LogIterator iterator(long starttime, long endtime) throws IllegalArgumentException { + try { + return iterator(starttime, endtime, null); + } catch (FilterException fe) { + assert(false); // should never ever happen + return null; + } + } + + public LogIterator iterator(long starttime, long endtime, FilterOp filter) throws IllegalArgumentException, FilterException { + // sanitise start and end times + if (endtime < starttime) { + throw new IllegalArgumentException("End time (" + endtime + ") must be greater or equal to starttime (" + starttime + ")"); + } + + return new TxnLogSourceIterator(this, starttime, endtime, filter); + } + + public LogIterator iterator() throws IllegalArgumentException { + return iterator(starttime, endtime+1); + } + + public TxnLogSource(String file) throws IOException { + this.file = file; + + skiplist = new LogSkipList(); + + RandomAccessFileReader reader = new RandomAccessFileReader(new File(file)); + try { + BinaryInputArchive logStream = new BinaryInputArchive(reader); + FileHeader fhdr = new FileHeader(); + fhdr.deserialize(logStream, "fileheader"); + + byte[] bytes = null; + while (true) { + long lastFp = reader.getPosition(); + + long crcValue; + + try { + crcValue = logStream.readLong("crcvalue"); + bytes = logStream.readBuffer("txnEntry"); + } catch (EOFException e) { + break; + } + + if (bytes.length == 0) { + break; + } + Checksum crc = new Adler32(); + crc.update(bytes, 0, bytes.length); + if (crcValue != crc.getValue()) { + throw new IOException("CRC doesn't match " + crcValue + + " vs " + crc.getValue()); + } + if (logStream.readByte("EOR") != 'B') { + throw new EOFException("Last transaction was partial."); + } + TxnLogEntry logEntry = SerializeUtils.deserializeTxn(bytes); + TxnHeader hdr = logEntry.getHeader(); + Record r = logEntry.getTxn(); + + if (starttime == 0) { + starttime = hdr.getTime(); + } + endtime = hdr.getTime(); + + if (size % skipN == 0) { + skiplist.addMark(hdr.getTime(), lastFp, size); + } + size++; + } + if (bytes == null) { + throw new IOException("Nothing read from ("+file+")"); + } + } finally { + reader.close(); + } + } + + public String toString() { + return "TxnLogSource(file=" + file + ", size=" + size + ", start=" + starttime + ", end=" + endtime +")"; + } + + public static void main(String[] args) throws IOException, FilterException { + TxnLogSource s = new TxnLogSource(args[0]); + System.out.println(s); + + LogIterator iter; + + if (args.length == 3) { + long starttime = Long.valueOf(args[1]); + long endtime = Long.valueOf(args[2]); + FilterOp fo = new FilterParser("(or (and (> zxid 0x2f0bd6f5e0) (< zxid 0x2f0bd6f5e9)) (= operation \"error\"))").parse(); + System.out.println("fo: " + fo); + iter = s.iterator(starttime, endtime, fo); + } else { + iter = s.iterator(); + } + System.out.println(iter); + while (iter.hasNext()) { + System.out.println(iter.next()); + } + iter.close(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/AndOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/AndOp.java new file mode 100644 index 0000000..581bdaa --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/AndOp.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class AndOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + for (FilterOp f : subOps) { + if (!f.matches(entry)) { + return false; + } + } + return true; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/Arg.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/Arg.java new file mode 100644 index 0000000..4fda3cf --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/Arg.java @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.FilterOp.*; + +public class Arg { + private ArgType type; + protected T value; + + protected Arg(ArgType type) { + this.type = type; + } + + public ArgType getType() { return type; } + public T getValue() { return value; } + + public String toString() { + return "[" + type + ":" + value + "]"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/EqualsOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/EqualsOp.java new file mode 100644 index 0000000..409815a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/EqualsOp.java @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class EqualsOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + + Object last = null; + for (Arg a : args) { + Object v = a.getValue(); + if (a.getType() == FilterOp.ArgType.SYMBOL) { + String key = (String)a.getValue(); + v = entry.getAttribute(key); + } + + if (last != null + && !last.equals(v)) { + return false; + } + last = v; + } + + return true; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/GreaterThanOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/GreaterThanOp.java new file mode 100644 index 0000000..244dd3d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/GreaterThanOp.java @@ -0,0 +1,70 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class GreaterThanOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + Arg first = args.get(0); + + if (first != null) { + FilterOp.ArgType type = first.getType(); + if (type == FilterOp.ArgType.SYMBOL) { + String key = (String)first.getValue(); + Object v = entry.getAttribute(key); + if (v instanceof String) { + type = FilterOp.ArgType.STRING; + } else if (v instanceof Double || v instanceof Long || v instanceof Integer || v instanceof Short) { + type = FilterOp.ArgType.NUMBER; + } else { + throw new FilterException("LessThanOp: Invalid argument, first argument resolves to neither a String nor a Number"); + } + } + + Object last = null; + for (Arg a : args) { + Object v = a.getValue(); + if (a.getType() == FilterOp.ArgType.SYMBOL) { + String key = (String)a.getValue(); + v = entry.getAttribute(key); + } + + if (last != null) { + if (type == FilterOp.ArgType.STRING) { + if (((String)last).compareTo((String)v) <= 0) { + return false; + } + } else if (type == FilterOp.ArgType.NUMBER) { + // System.out.println("last[" + ((Number)last).longValue() + "] v["+ ((Number)v).longValue() + "]"); + if (((Number)last).longValue() <= ((Number)v).longValue()) { + return false; + } + } + } + last = v; + } + return true; + } else { + return true; + } + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/LessThanOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/LessThanOp.java new file mode 100644 index 0000000..b7d9e09 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/LessThanOp.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class LessThanOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + Arg first = args.get(0); + + if (first != null) { + FilterOp.ArgType type = first.getType(); + if (type == FilterOp.ArgType.SYMBOL) { + String key = (String)first.getValue(); + Object v = entry.getAttribute(key); + if (v instanceof String) { + type = FilterOp.ArgType.STRING; + } else if (v instanceof Double || v instanceof Long || v instanceof Integer || v instanceof Short) { + type = FilterOp.ArgType.NUMBER; + } else { + throw new FilterException("LessThanOp: Invalid argument, first argument resolves to neither a String nor a Number"); + } + } + + Object last = null; + for (Arg a : args) { + Object v = a.getValue(); + if (a.getType() == FilterOp.ArgType.SYMBOL) { + String key = (String)a.getValue(); + v = entry.getAttribute(key); + } + + if (last != null) { + if (type == FilterOp.ArgType.STRING) { + if (((String)last).compareTo((String)v) >= 0) { + return false; + } + } else if (type == FilterOp.ArgType.NUMBER) { + if (((Number)last).doubleValue() >= ((Number)v).doubleValue()) { + return false; + } + } + } + last = v; + } + return true; + } else { + return true; + } + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/NotOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/NotOp.java new file mode 100644 index 0000000..d8ed757 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/NotOp.java @@ -0,0 +1,31 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class NotOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + if (subOps.size() != 1) { + throw new FilterException("Not operation can only take one argument"); + } + return !subOps.get(0).matches(entry); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/NumberArg.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/NumberArg.java new file mode 100644 index 0000000..d6b584d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/NumberArg.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.FilterOp.*; + +public class NumberArg extends Arg { + public NumberArg(Long value) { + super(ArgType.NUMBER); + this.value = value; + } +}; + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/OrOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/OrOp.java new file mode 100644 index 0000000..d681589 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/OrOp.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class OrOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + for (FilterOp f : subOps) { + if (f.matches(entry)) { + return true; + } + } + return false; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/StringArg.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/StringArg.java new file mode 100644 index 0000000..7345d3c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/StringArg.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.FilterOp.*; + +public class StringArg extends Arg { + public StringArg(String value) { + super(ArgType.STRING); + this.value = value; + } +}; + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/SymbolArg.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/SymbolArg.java new file mode 100644 index 0000000..077553b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/SymbolArg.java @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.FilterOp.*; + +public class SymbolArg extends Arg { + public SymbolArg(String value) { + super(ArgType.SYMBOL); + this.value = value; + } +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/XorOp.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/XorOp.java new file mode 100644 index 0000000..9e778b1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/filterops/XorOp.java @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.filterops; + +import org.apache.zookeeper.graph.LogEntry; +import org.apache.zookeeper.graph.FilterOp; +import org.apache.zookeeper.graph.FilterException; + +public class XorOp extends FilterOp { + public boolean matches(LogEntry entry) throws FilterException { + int count = 0; + for (FilterOp f : subOps) { + if (f.matches(entry)) { + count++; + if (count > 1) { + return false; + } + } + } + if (count == 1) { + return true; + } + return false; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/FileLoader.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/FileLoader.java new file mode 100644 index 0000000..67e8945 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/FileLoader.java @@ -0,0 +1,60 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import org.apache.zookeeper.graph.*; + +public class FileLoader extends JsonServlet +{ + private MergedLogSource source = null; + + public FileLoader(MergedLogSource src) throws Exception { + source = src; + } + + String handleRequest(JsonRequest request) throws Exception + { + String output = ""; + + String file = request.getString("path", "/"); + JSONObject o = new JSONObject(); + try { + this.source.addSource(file); + o.put("status", "OK"); + + } catch (Exception e) { + o.put("status", "ERR"); + o.put("error", e.toString()); + } + + return JSONValue.toJSONString(o); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/Fs.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/Fs.java new file mode 100644 index 0000000..e5b1a01 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/Fs.java @@ -0,0 +1,69 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; +import java.util.Arrays; +import java.util.Comparator; + +public class Fs extends JsonServlet +{ + String handleRequest(JsonRequest request) throws Exception + { + String output = ""; + JSONArray filelist = new JSONArray(); + + File base = new File(request.getString("path", "/")); + if (!base.exists() || !base.isDirectory()) { + throw new FileNotFoundException("Couldn't find [" + request + "]"); + } + File[] files = base.listFiles(); + Arrays.sort(files, new Comparator() { + public int compare(File o1, File o2) { + if (o1.isDirectory() != o2.isDirectory()) { + if (o1.isDirectory()) { + return -1; + } else { + return 1; + } + } + return o1.getName().compareToIgnoreCase(o2.getName()); + } + }); + + for (File f : files) { + JSONObject o = new JSONObject(); + o.put("file", f.getName()); + o.put("type", f.isDirectory() ? "D" : "F"); + o.put("path", f.getCanonicalPath()); + filelist.add(o); + } + return JSONValue.toJSONString(filelist); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/GraphData.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/GraphData.java new file mode 100644 index 0000000..fc10eb1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/GraphData.java @@ -0,0 +1,85 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import org.apache.zookeeper.graph.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GraphData extends JsonServlet +{ + private static final Logger LOG = LoggerFactory.getLogger(GraphData.class); + private static final int DEFAULT_PERIOD = 1000; + + private LogSource source = null; + + public GraphData(LogSource src) throws Exception { + this.source = src; + } + + String handleRequest(JsonRequest request) throws Exception { + + + long starttime = 0; + long endtime = 0; + long period = 0; + FilterOp fo = null; + + starttime = request.getNumber("start", 0); + endtime = request.getNumber("end", 0); + period = request.getNumber("period", 0); + String filterstr = request.getString("filter", ""); + + if (filterstr.length() > 0) { + fo = new FilterParser(filterstr).parse(); + } + + if (starttime == 0) { starttime = source.getStartTime(); } + if (endtime == 0) { + if (period > 0) { + endtime = starttime + period; + } else { + endtime = starttime + DEFAULT_PERIOD; + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("handle(start= " + starttime + ", end=" + endtime + ", period=" + period + ")"); + } + + LogIterator iterator = (fo != null) ? + source.iterator(starttime, endtime, fo) : source.iterator(starttime, endtime); + return new JsonGenerator(iterator).toString(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/JsonServlet.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/JsonServlet.java new file mode 100644 index 0000000..910d44f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/JsonServlet.java @@ -0,0 +1,85 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import java.util.Map; + +abstract public class JsonServlet extends HttpServlet { + abstract String handleRequest(JsonRequest request) throws Exception; + + protected class JsonRequest { + private Map map; + + public JsonRequest(ServletRequest request) { + map = request.getParameterMap(); + } + + public long getNumber(String name, long defaultnum) { + String[] vals = (String[])map.get(name); + if (vals == null || vals.length == 0) { + return defaultnum; + } + + try { + return Long.valueOf(vals[0]); + } catch (NumberFormatException e) { + return defaultnum; + } + } + + public String getString(String name, String defaultstr) { + String[] vals = (String[])map.get(name); + if (vals == null || vals.length == 0) { + return defaultstr; + } else { + return vals[0]; + } + } + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("text/plain;charset=utf-8"); + response.setStatus(HttpServletResponse.SC_OK); + + try { + String req = request.getRequestURI().substring(request.getServletPath().length()); + + response.getWriter().println(handleRequest(new JsonRequest(request))); + } catch (Exception e) { + JSONObject o = new JSONObject(); + o.put("error", e.toString()); + response.getWriter().println(JSONValue.toJSONString(o)); + } catch (java.lang.OutOfMemoryError oom) { + JSONObject o = new JSONObject(); + o.put("error", "Out of memory. Perhaps you've requested too many logs. Try narrowing you're filter criteria."); + response.getWriter().println(JSONValue.toJSONString(o)); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/NumEvents.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/NumEvents.java new file mode 100644 index 0000000..5961a12 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/NumEvents.java @@ -0,0 +1,88 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.File; +import java.io.IOException; +import java.io.FileNotFoundException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +import org.apache.zookeeper.graph.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class NumEvents extends JsonServlet +{ + private static final Logger LOG = LoggerFactory.getLogger(NumEvents.class); + private static final int DEFAULT_PERIOD = 1000; + + private LogSource source = null; + + public NumEvents(LogSource src) throws Exception { + this.source = src; + } + + String handleRequest(JsonRequest request) throws Exception { + String output = ""; + + long starttime = 0; + long endtime = 0; + long period = 0; + + starttime = request.getNumber("start", 0); + endtime = request.getNumber("end", 0); + period = request.getNumber("period", 0); + + if (starttime == 0) { starttime = source.getStartTime(); } + if (endtime == 0) { + if (period > 0) { + endtime = starttime + period; + } else { + endtime = source.getEndTime(); + } + } + + LogIterator iter = source.iterator(starttime, endtime); + JSONObject data = new JSONObject(); + data.put("startTime", starttime); + data.put("endTime", endtime); + long size = 0; + + size = iter.size(); + + data.put("numEntries", size); + if (LOG.isDebugEnabled()) { + LOG.debug("handle(start= " + starttime + ", end=" + endtime + ", numEntries=" + size +")"); + } + iter.close(); + return JSONValue.toJSONString(data); + } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/StaticContent.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/StaticContent.java new file mode 100644 index 0000000..d91acb6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/StaticContent.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.BufferedReader; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class StaticContent extends HttpServlet { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + String path = request.getRequestURI().substring(request.getServletPath().length()); + + InputStream resource = ClassLoader.getSystemResourceAsStream("org/apache/zookeeper/graph/resources" + path); + if (resource == null) { + response.getWriter().println(path + " not found!"); + response.setStatus(HttpServletResponse.SC_NOT_FOUND); + return; + } + try { + while (resource.available() > 0) { + response.getWriter().write(resource.read()); + } + } finally { + resource.close(); + } + // response.setContentType("text/plain;charset=utf-8"); + response.setStatus(HttpServletResponse.SC_OK); + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/Throughput.java b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/Throughput.java new file mode 100644 index 0000000..80ed1dc --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/java/org/apache/zookeeper/graph/servlets/Throughput.java @@ -0,0 +1,126 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.graph.servlets; + +import java.io.IOException; +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.io.DataOutputStream; +import java.io.PrintStream; + +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Set; + +import org.apache.zookeeper.graph.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + + +public class Throughput extends JsonServlet +{ + private static final int MS_PER_SEC = 1000; + private static final int MS_PER_MIN = MS_PER_SEC*60; + private static final int MS_PER_HOUR = MS_PER_MIN*60; + + private LogSource source = null; + + public Throughput(LogSource src) throws Exception { + this.source = src; + } + + public String handleRequest(JsonRequest request) throws Exception { + long starttime = 0; + long endtime = 0; + long period = 0; + long scale = 0; + + starttime = request.getNumber("start", 0); + endtime = request.getNumber("end", 0); + period = request.getNumber("period", 0); + + + if (starttime == 0) { starttime = source.getStartTime(); } + if (endtime == 0) { + if (period > 0) { + endtime = starttime + period; + } else { + endtime = source.getEndTime(); + } + } + + String scalestr = request.getString("scale", "minutes"); + if (scalestr.equals("seconds")) { + scale = MS_PER_SEC; + } else if (scalestr.equals("hours")) { + scale = MS_PER_HOUR; + } else { + scale = MS_PER_MIN; + } + + LogIterator iter = source.iterator(starttime, endtime); + + long current = 0; + long currentms = 0; + Set zxids_ms = new HashSet(); + long zxidcount = 0; + + JSONArray events = new JSONArray(); + while (iter.hasNext()) { + LogEntry e = iter.next(); + if (e.getType() != LogEntry.Type.TXN) { + continue; + } + + TransactionEntry cxn = (TransactionEntry)e; + + long ms = cxn.getTimestamp(); + long inscale = ms/scale; + + if (currentms != ms && currentms != 0) { + zxidcount += zxids_ms.size(); + zxids_ms.clear(); + } + + if (inscale != current && current != 0) { + JSONObject o = new JSONObject(); + o.put("time", current*scale); + o.put("count", zxidcount); + events.add(o); + zxidcount = 0; + } + current = inscale; + currentms = ms; + + zxids_ms.add(cxn.getZxid()); + } + JSONObject o = new JSONObject(); + o.put("time", current*scale); + o.put("count", zxidcount); + events.add(o); + + iter.close(); + + return JSONValue.toJSONString(events); + } + +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/loggraph-dev.sh b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/loggraph-dev.sh new file mode 100644 index 0000000..e04434e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/loggraph-dev.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +make_canonical () { + cd $1; pwd; +} + +SCRIPTDIR=`dirname $0` +BUILDDIR=`make_canonical $SCRIPTDIR/../../../../../build/contrib/loggraph` +LIBDIR=`make_canonical $BUILDDIR/lib` +WEBDIR=`make_canonical $SCRIPTDIR/../web` +ZKDIR=`make_canonical $SCRIPTDIR/../../../../../build/` + +if [ ! -x $BUILDDIR ]; then + echo "\n\n*** You need to build loggraph before running it ***\n\n"; + exit; +fi + +for i in `ls $LIBDIR`; do + CLASSPATH=$LIBDIR/$i:$CLASSPATH +done + +for i in $ZKDIR/zookeeper-*.jar; do + CLASSPATH="$i:$CLASSPATH" +done + +CLASSPATH=$BUILDDIR/classes:$WEBDIR:$CLASSPATH +echo $CLASSPATH +java -Dlog4j.configuration=org/apache/zookeeper/graph/log4j.properties -Xdebug -Xrunjdwp:transport=dt_socket,address=4444,server=y,suspend=n -cp $CLASSPATH org.apache.zookeeper.graph.LogServer $* diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/loggraph.sh b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/loggraph.sh new file mode 100644 index 0000000..0259dc6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/loggraph.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +make_canonical () { + cd $1; pwd; +} + +SCRIPTDIR=`dirname $0` +BUILDDIR=`make_canonical $SCRIPTDIR/../../../../../build/contrib/loggraph` +LIBDIR=`make_canonical $BUILDDIR/lib` +ZKDIR=`make_canonical $SCRIPTDIR/../../../../../build/` + +if [ ! -x $BUILDDIR ]; then + echo "\n\n*** You need to build loggraph before running it ***\n\n"; + exit; +fi + +for i in `ls $LIBDIR`; do + CLASSPATH=$LIBDIR/$i:$CLASSPATH +done + +for i in `ls $BUILDDIR/*.jar`; do + CLASSPATH=$i:$CLASSPATH +done + +for i in $ZKDIR/zookeeper-*.jar; do + CLASSPATH="$i:$CLASSPATH" +done + +java -cp $CLASSPATH org.apache.zookeeper.graph.LogServer $* + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/log4j.properties b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/log4j.properties new file mode 100644 index 0000000..ab8960b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/log4j.properties @@ -0,0 +1,11 @@ +log4j.rootLogger=TRACE, CONSOLE + +# Print the date in ISO 8601 format +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.Threshold=TRACE +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L] - %m%n + +log4j.logger.org.apache.zookeeper.graph.LogSkipList=off +log4j.logger.org.apache.zookeeper.graph.RandomAccessFileReader=off +#log4j.logger.org.apache.zookeeper.graph.Log4JSource=off \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/date.format.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/date.format.js new file mode 100644 index 0000000..5515009 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/date.format.js @@ -0,0 +1,126 @@ +/* + * Date Format 1.2.3 + * (c) 2007-2009 Steven Levithan + * MIT license + * + * Includes enhancements by Scott Trenda + * and Kris Kowal + * + * Accepts a date, a mask, or a date and a mask. + * Returns a formatted version of the given date. + * The date defaults to the current date/time. + * The mask defaults to dateFormat.masks.default. + */ + +var dateFormat = function () { + var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g, + timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g, + timezoneClip = /[^-+\dA-Z]/g, + pad = function (val, len) { + val = String(val); + len = len || 2; + while (val.length < len) val = "0" + val; + return val; + }; + + // Regexes and supporting functions are cached through closure + return function (date, mask, utc) { + var dF = dateFormat; + + // You can't provide utc if you skip other args (use the "UTC:" mask prefix) + if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) { + mask = date; + date = undefined; + } + + // Passing date through Date applies Date.parse, if necessary + date = date ? new Date(date) : new Date; + if (isNaN(date)) throw SyntaxError("invalid date"); + + mask = String(dF.masks[mask] || mask || dF.masks["default"]); + + // Allow setting the utc argument via the mask + if (mask.slice(0, 4) == "UTC:") { + mask = mask.slice(4); + utc = true; + } + + var _ = utc ? "getUTC" : "get", + d = date[_ + "Date"](), + D = date[_ + "Day"](), + m = date[_ + "Month"](), + y = date[_ + "FullYear"](), + H = date[_ + "Hours"](), + M = date[_ + "Minutes"](), + s = date[_ + "Seconds"](), + L = date[_ + "Milliseconds"](), + o = utc ? 0 : date.getTimezoneOffset(), + flags = { + d: d, + dd: pad(d), + ddd: dF.i18n.dayNames[D], + dddd: dF.i18n.dayNames[D + 7], + m: m + 1, + mm: pad(m + 1), + mmm: dF.i18n.monthNames[m], + mmmm: dF.i18n.monthNames[m + 12], + yy: String(y).slice(2), + yyyy: y, + h: H % 12 || 12, + hh: pad(H % 12 || 12), + H: H, + HH: pad(H), + M: M, + MM: pad(M), + s: s, + ss: pad(s), + l: pad(L, 3), + L: pad(L > 99 ? Math.round(L / 10) : L), + t: H < 12 ? "a" : "p", + tt: H < 12 ? "am" : "pm", + T: H < 12 ? "A" : "P", + TT: H < 12 ? "AM" : "PM", + Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""), + o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4), + S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10] + }; + + return mask.replace(token, function ($0) { + return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1); + }); + }; +}(); + +// Some common format strings +dateFormat.masks = { + "default": "ddd mmm dd yyyy HH:MM:ss", + shortDate: "m/d/yy", + mediumDate: "mmm d, yyyy", + longDate: "mmmm d, yyyy", + fullDate: "dddd, mmmm d, yyyy", + shortTime: "h:MM TT", + mediumTime: "h:MM:ss TT", + longTime: "h:MM:ss TT Z", + isoDate: "yyyy-mm-dd", + isoTime: "HH:MM:ss", + isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", + isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" +}; + +// Internationalization strings +dateFormat.i18n = { + dayNames: [ + "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" + ], + monthNames: [ + "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" + ] +}; + +// For convenience... +Date.prototype.format = function (mask, utc) { + return dateFormat(this, mask, utc); +}; + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.bar.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.bar.js new file mode 100644 index 0000000..2f7212a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.bar.js @@ -0,0 +1,385 @@ +/* + * g.Raphael 0.4 - Charting library, based on Raphaël + * + * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com) + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. + */ +Raphael.fn.g.barchart = function (x, y, width, height, values, opts) { + opts = opts || {}; + var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square", + gutter = parseFloat(opts.gutter || "20%"), + chart = this.set(), + bars = this.set(), + covers = this.set(), + covers2 = this.set(), + total = Math.max.apply(Math, values), + stacktotal = [], + paper = this, + multi = 0, + colors = opts.colors || this.g.colors, + len = values.length; + if (this.raphael.is(values[0], "array")) { + total = []; + multi = len; + len = 0; + for (var i = values.length; i--;) { + bars.push(this.set()); + total.push(Math.max.apply(Math, values[i])); + len = Math.max(len, values[i].length); + } + if (opts.stacked) { + for (var i = len; i--;) { + var tot = 0; + for (var j = values.length; j--;) { + tot +=+ values[j][i] || 0; + } + stacktotal.push(tot); + } + } + for (var i = values.length; i--;) { + if (values[i].length < len) { + for (var j = len; j--;) { + values[i].push(0); + } + } + } + total = Math.max.apply(Math, opts.stacked ? stacktotal : total); + } + + total = (opts.to) || total; + var barwidth = width / (len * (100 + gutter) + gutter) * 100, + barhgutter = barwidth * gutter / 100, + barvgutter = opts.vgutter == null ? 20 : opts.vgutter, + stack = [], + X = x + barhgutter, + Y = (height - 2 * barvgutter) / total; + if (!opts.stretch) { + barhgutter = Math.round(barhgutter); + barwidth = Math.floor(barwidth); + } + !opts.stacked && (barwidth /= multi || 1); + for (var i = 0; i < len; i++) { + stack = []; + for (var j = 0; j < (multi || 1); j++) { + var h = Math.round((multi ? values[j][i] : values[i]) * Y), + top = y + height - barvgutter - h, + bar = this.g.finger(Math.round(X + barwidth / 2), top + h, barwidth, h, true, type).attr({stroke: colors[multi ? j : i], fill: colors[multi ? j : i]}); + if (multi) { + bars[j].push(bar); + } else { + bars.push(bar); + } + bar.y = top; + bar.x = Math.round(X + barwidth / 2); + bar.w = barwidth; + bar.h = h; + bar.value = multi ? values[j][i] : values[i]; + if (!opts.stacked) { + X += barwidth; + } else { + stack.push(bar); + } + } + if (opts.stacked) { + var cvr; + covers2.push(cvr = this.rect(stack[0].x - stack[0].w / 2, y, barwidth, height).attr(this.g.shim)); + cvr.bars = this.set(); + var size = 0; + for (var s = stack.length; s--;) { + stack[s].toFront(); + } + for (var s = 0, ss = stack.length; s < ss; s++) { + var bar = stack[s], + cover, + h = (size + bar.value) * Y, + path = this.g.finger(bar.x, y + height - barvgutter - !!size * .5, barwidth, h, true, type, 1); + cvr.bars.push(bar); + size && bar.attr({path: path}); + bar.h = h; + bar.y = y + height - barvgutter - !!size * .5 - h; + covers.push(cover = this.rect(bar.x - bar.w / 2, bar.y, barwidth, bar.value * Y).attr(this.g.shim)); + cover.bar = bar; + cover.value = bar.value; + size += bar.value; + } + X += barwidth; + } + X += barhgutter; + } + covers2.toFront(); + X = x + barhgutter; + if (!opts.stacked) { + for (var i = 0; i < len; i++) { + for (var j = 0; j < (multi || 1); j++) { + var cover; + covers.push(cover = this.rect(Math.round(X), y + barvgutter, barwidth, height - barvgutter).attr(this.g.shim)); + cover.bar = multi ? bars[j][i] : bars[i]; + cover.value = cover.bar.value; + X += barwidth; + } + X += barhgutter; + } + } + chart.label = function (labels, isBottom) { + labels = labels || []; + this.labels = paper.set(); + var L, l = -Infinity; + if (opts.stacked) { + for (var i = 0; i < len; i++) { + var tot = 0; + for (var j = 0; j < (multi || 1); j++) { + tot += multi ? values[j][i] : values[i]; + if (j == multi - 1) { + var label = paper.g.labelise(labels[i], tot, total); + L = paper.g.text(bars[i * (multi || 1) + j].x, y + height - barvgutter / 2, label).insertBefore(covers[i * (multi || 1) + j]); + var bb = L.getBBox(); + if (bb.x - 7 < l) { + L.remove(); + } else { + this.labels.push(L); + l = bb.x + bb.width; + } + } + } + } + } else { + for (var i = 0; i < len; i++) { + for (var j = 0; j < (multi || 1); j++) { + var label = paper.g.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total); + L = paper.g.text(bars[i * (multi || 1) + j].x, isBottom ? y + height - barvgutter / 2 : bars[i * (multi || 1) + j].y - 10, label).insertBefore(covers[i * (multi || 1) + j]); + var bb = L.getBBox(); + if (bb.x - 7 < l) { + L.remove(); + } else { + this.labels.push(L); + l = bb.x + bb.width; + } + } + } + } + return this; + }; + chart.hover = function (fin, fout) { + covers2.hide(); + covers.show(); + covers.mouseover(fin).mouseout(fout); + return this; + }; + chart.hoverColumn = function (fin, fout) { + covers.hide(); + covers2.show(); + fout = fout || function () {}; + covers2.mouseover(fin).mouseout(fout); + return this; + }; + chart.click = function (f) { + covers2.hide(); + covers.show(); + covers.click(f); + return this; + }; + chart.each = function (f) { + if (!Raphael.is(f, "function")) { + return this; + } + for (var i = covers.length; i--;) { + f.call(covers[i]); + } + return this; + }; + chart.eachColumn = function (f) { + if (!Raphael.is(f, "function")) { + return this; + } + for (var i = covers2.length; i--;) { + f.call(covers2[i]); + } + return this; + }; + chart.clickColumn = function (f) { + covers.hide(); + covers2.show(); + covers2.click(f); + return this; + }; + chart.push(bars, covers, covers2); + chart.bars = bars; + chart.covers = covers; + return chart; +}; +Raphael.fn.g.hbarchart = function (x, y, width, height, values, opts) { + opts = opts || {}; + var type = {round: "round", sharp: "sharp", soft: "soft"}[opts.type] || "square", + gutter = parseFloat(opts.gutter || "20%"), + chart = this.set(), + bars = this.set(), + covers = this.set(), + covers2 = this.set(), + total = Math.max.apply(Math, values), + stacktotal = [], + paper = this, + multi = 0, + colors = opts.colors || this.g.colors, + len = values.length; + if (this.raphael.is(values[0], "array")) { + total = []; + multi = len; + len = 0; + for (var i = values.length; i--;) { + bars.push(this.set()); + total.push(Math.max.apply(Math, values[i])); + len = Math.max(len, values[i].length); + } + if (opts.stacked) { + for (var i = len; i--;) { + var tot = 0; + for (var j = values.length; j--;) { + tot +=+ values[j][i] || 0; + } + stacktotal.push(tot); + } + } + for (var i = values.length; i--;) { + if (values[i].length < len) { + for (var j = len; j--;) { + values[i].push(0); + } + } + } + total = Math.max.apply(Math, opts.stacked ? stacktotal : total); + } + + total = (opts.to) || total; + var barheight = Math.floor(height / (len * (100 + gutter) + gutter) * 100), + bargutter = Math.floor(barheight * gutter / 100), + stack = [], + Y = y + bargutter, + X = (width - 1) / total; + !opts.stacked && (barheight /= multi || 1); + for (var i = 0; i < len; i++) { + stack = []; + for (var j = 0; j < (multi || 1); j++) { + var val = multi ? values[j][i] : values[i], + bar = this.g.finger(x, Y + barheight / 2, Math.round(val * X), barheight - 1, false, type).attr({stroke: colors[multi ? j : i], fill: colors[multi ? j : i]}); + if (multi) { + bars[j].push(bar); + } else { + bars.push(bar); + } + bar.x = x + Math.round(val * X); + bar.y = Y + barheight / 2; + bar.w = Math.round(val * X); + bar.h = barheight; + bar.value = +val; + if (!opts.stacked) { + Y += barheight; + } else { + stack.push(bar); + } + } + if (opts.stacked) { + var cvr = this.rect(x, stack[0].y - stack[0].h / 2, width, barheight).attr(this.g.shim); + covers2.push(cvr); + cvr.bars = this.set(); + var size = 0; + for (var s = stack.length; s--;) { + stack[s].toFront(); + } + for (var s = 0, ss = stack.length; s < ss; s++) { + var bar = stack[s], + cover, + val = Math.round((size + bar.value) * X), + path = this.g.finger(x, bar.y, val, barheight - 1, false, type, 1); + cvr.bars.push(bar); + size && bar.attr({path: path}); + bar.w = val; + bar.x = x + val; + covers.push(cover = this.rect(x + size * X, bar.y - bar.h / 2, bar.value * X, barheight).attr(this.g.shim)); + cover.bar = bar; + size += bar.value; + } + Y += barheight; + } + Y += bargutter; + } + covers2.toFront(); + Y = y + bargutter; + if (!opts.stacked) { + for (var i = 0; i < len; i++) { + for (var j = 0; j < multi; j++) { + var cover = this.rect(x, Y, width, barheight).attr(this.g.shim); + covers.push(cover); + cover.bar = bars[j][i]; + Y += barheight; + } + Y += bargutter; + } + } + chart.label = function (labels, isRight) { + labels = labels || []; + this.labels = paper.set(); + for (var i = 0; i < len; i++) { + for (var j = 0; j < multi; j++) { + var label = paper.g.labelise(multi ? labels[j] && labels[j][i] : labels[i], multi ? values[j][i] : values[i], total); + var X = isRight ? bars[i * (multi || 1) + j].x - barheight / 2 + 3 : x + 5, + A = isRight ? "end" : "start", + L; + this.labels.push(L = paper.g.text(X, bars[i * (multi || 1) + j].y, label).attr({"text-anchor": A}).insertBefore(covers[0])); + if (L.getBBox().x < x + 5) { + L.attr({x: x + 5, "text-anchor": "start"}); + } else { + bars[i * (multi || 1) + j].label = L; + } + } + } + return this; + }; + chart.hover = function (fin, fout) { + covers2.hide(); + covers.show(); + fout = fout || function () {}; + covers.mouseover(fin).mouseout(fout); + return this; + }; + chart.hoverColumn = function (fin, fout) { + covers.hide(); + covers2.show(); + fout = fout || function () {}; + covers2.mouseover(fin).mouseout(fout); + return this; + }; + chart.each = function (f) { + if (!Raphael.is(f, "function")) { + return this; + } + for (var i = covers.length; i--;) { + f.call(covers[i]); + } + return this; + }; + chart.eachColumn = function (f) { + if (!Raphael.is(f, "function")) { + return this; + } + for (var i = covers2.length; i--;) { + f.call(covers2[i]); + } + return this; + }; + chart.click = function (f) { + covers2.hide(); + covers.show(); + covers.click(f); + return this; + }; + chart.clickColumn = function (f) { + covers.hide(); + covers2.show(); + covers2.click(f); + return this; + }; + chart.push(bars, covers, covers2); + chart.bars = bars; + chart.covers = covers; + return chart; +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.dot.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.dot.js new file mode 100644 index 0000000..2821e62 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.dot.js @@ -0,0 +1,110 @@ +/* + * g.Raphael 0.4 - Charting library, based on Raphaël + * + * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com) + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. + */ +Raphael.fn.g.dotchart = function (x, y, width, height, valuesx, valuesy, size, opts) { + function drawAxis(ax) { + +ax[0] && (ax[0] = paper.g.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.axisxlabels || null, opts.axisxtype || "t")); + +ax[1] && (ax[1] = paper.g.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.axisylabels || null, opts.axisytype || "t")); + +ax[2] && (ax[2] = paper.g.axis(x + gutter, y + height - gutter + maxR, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.axisxlabels || null, opts.axisxtype || "t")); + +ax[3] && (ax[3] = paper.g.axis(x + gutter - maxR, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.axisylabels || null, opts.axisytype || "t")); + } + opts = opts || {}; + var xdim = this.g.snapEnds(Math.min.apply(Math, valuesx), Math.max.apply(Math, valuesx), valuesx.length - 1), + minx = xdim.from, + maxx = xdim.to, + gutter = opts.gutter || 10, + ydim = this.g.snapEnds(Math.min.apply(Math, valuesy), Math.max.apply(Math, valuesy), valuesy.length - 1), + miny = ydim.from, + maxy = ydim.to, + len = Math.max(valuesx.length, valuesy.length, size.length), + symbol = this.g.markers[opts.symbol] || "disc", + res = this.set(), + series = this.set(), + max = opts.max || 100, + top = Math.max.apply(Math, size), + R = [], + paper = this, + k = Math.sqrt(top / Math.PI) * 2 / max; + + for (var i = 0; i < len; i++) { + R[i] = Math.min(Math.sqrt(size[i] / Math.PI) * 2 / k, max); + } + gutter = Math.max.apply(Math, R.concat(gutter)); + var axis = this.set(), + maxR = Math.max.apply(Math, R); + if (opts.axis) { + var ax = (opts.axis + "").split(/[,\s]+/); + drawAxis(ax); + var g = [], b = []; + for (var i = 0, ii = ax.length; i < ii; i++) { + var bb = ax[i].all ? ax[i].all.getBBox()[["height", "width"][i % 2]] : 0; + g[i] = bb + gutter; + b[i] = bb; + } + gutter = Math.max.apply(Math, g.concat(gutter)); + for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) { + ax[i].remove(); + ax[i] = 1; + } + drawAxis(ax); + for (var i = 0, ii = ax.length; i < ii; i++) if (ax[i].all) { + axis.push(ax[i].all); + } + res.axis = axis; + } + var kx = (width - gutter * 2) / ((maxx - minx) || 1), + ky = (height - gutter * 2) / ((maxy - miny) || 1); + for (var i = 0, ii = valuesy.length; i < ii; i++) { + var sym = this.raphael.is(symbol, "array") ? symbol[i] : symbol, + X = x + gutter + (valuesx[i] - minx) * kx, + Y = y + height - gutter - (valuesy[i] - miny) * ky; + sym && R[i] && series.push(this.g[sym](X, Y, R[i]).attr({fill: opts.heat ? this.g.colorValue(R[i], maxR) : Raphael.fn.g.colors[0], "fill-opacity": opts.opacity ? R[i] / max : 1, stroke: "none"})); + } + var covers = this.set(); + for (var i = 0, ii = valuesy.length; i < ii; i++) { + var X = x + gutter + (valuesx[i] - minx) * kx, + Y = y + height - gutter - (valuesy[i] - miny) * ky; + covers.push(this.circle(X, Y, maxR).attr(this.g.shim)); + opts.href && opts.href[i] && covers[i].attr({href: opts.href[i]}); + covers[i].r = +R[i].toFixed(3); + covers[i].x = +X.toFixed(3); + covers[i].y = +Y.toFixed(3); + covers[i].X = valuesx[i]; + covers[i].Y = valuesy[i]; + covers[i].value = size[i] || 0; + covers[i].dot = series[i]; + } + res.covers = covers; + res.series = series; + res.push(series, axis, covers); + res.hover = function (fin, fout) { + covers.mouseover(fin).mouseout(fout); + return this; + }; + res.click = function (f) { + covers.click(f); + return this; + }; + res.each = function (f) { + if (!Raphael.is(f, "function")) { + return this; + } + for (var i = covers.length; i--;) { + f.call(covers[i]); + } + return this; + }; + res.href = function (map) { + var cover; + for (var i = covers.length; i--;) { + cover = covers[i]; + if (cover.X == map.x && cover.Y == map.y && cover.value == map.value) { + cover.attr({href: map.href}); + } + } + }; + return res; +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.line.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.line.js new file mode 100644 index 0000000..eb56e59 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.line.js @@ -0,0 +1,230 @@ +/* + * g.Raphael 0.4 - Charting library, based on Raphaël + * + * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com) + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. + */ +Raphael.fn.g.linechart = function (x, y, width, height, valuesx, valuesy, opts) { + function shrink(values, dim) { + var k = values.length / dim, + j = 0, + l = k, + sum = 0, + res = []; + while (j < values.length) { + l--; + if (l < 0) { + sum += values[j] * (1 + l); + res.push(sum / k); + sum = values[j++] * -l; + l += k; + } else { + sum += values[j++]; + } + } + return res; + } + opts = opts || {}; + if (!this.raphael.is(valuesx[0], "array")) { + valuesx = [valuesx]; + } + if (!this.raphael.is(valuesy[0], "array")) { + valuesy = [valuesy]; + } + var allx = Array.prototype.concat.apply([], valuesx), + ally = Array.prototype.concat.apply([], valuesy), + xdim = this.g.snapEnds(Math.min.apply(Math, allx), Math.max.apply(Math, allx), valuesx[0].length - 1), + minx = xdim.from, + maxx = xdim.to, + gutter = opts.gutter || 10, + kx = (width - gutter * 2) / (maxx - minx), + ydim = this.g.snapEnds(Math.min.apply(Math, ally), Math.max.apply(Math, ally), valuesy[0].length - 1), + miny = ydim.from, + maxy = ydim.to, + ky = (height - gutter * 2) / (maxy - miny), + len = Math.max(valuesx[0].length, valuesy[0].length), + symbol = opts.symbol || "", + colors = opts.colors || Raphael.fn.g.colors, + that = this, + columns = null, + dots = null, + chart = this.set(), + path = []; + + for (var i = 0, ii = valuesy.length; i < ii; i++) { + len = Math.max(len, valuesy[i].length); + } + var shades = this.set(); + for (var i = 0, ii = valuesy.length; i < ii; i++) { + if (opts.shade) { + shades.push(this.path().attr({stroke: "none", fill: colors[i], opacity: opts.nostroke ? 1 : .3})); + } + if (valuesy[i].length > width - 2 * gutter) { + valuesy[i] = shrink(valuesy[i], width - 2 * gutter); + len = width - 2 * gutter; + } + if (valuesx[i] && valuesx[i].length > width - 2 * gutter) { + valuesx[i] = shrink(valuesx[i], width - 2 * gutter); + } + } + var axis = this.set(); + if (opts.axis) { + var ax = (opts.axis + "").split(/[,\s]+/); + +ax[0] && axis.push(this.g.axis(x + gutter, y + gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 2, opts.northlabels)); + +ax[1] && axis.push(this.g.axis(x + width - gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 3, opts.eastlabels)); + +ax[2] && axis.push(this.g.axis(x + gutter, y + height - gutter, width - 2 * gutter, minx, maxx, opts.axisxstep || Math.floor((width - 2 * gutter) / 20), 0, opts.southlabels)); + +ax[3] && axis.push(this.g.axis(x + gutter, y + height - gutter, height - 2 * gutter, miny, maxy, opts.axisystep || Math.floor((height - 2 * gutter) / 20), 1, opts.westlabels)); + } + if (opts.northAxisLabel) { + this.g.text(x + gutter + width/2, gutter, opts.northAxisLabel); + } + if (opts.southAxisLabel) { + this.g.text(x + gutter + width/2, y + height + 20, opts.southAxisLabel); + } + if (opts.westAxisLabel) { + this.g.text(gutter, y + gutter + height/2, opts.westAxisLabel).attr({rotation: -90}); + } + if (opts.eastAxisLabel) { + this.g.text(x + gutter + width + 20, y + gutter + height/2, opts.eastAxisLabel).attr({rotation: 90}); + } + + var lines = this.set(), + symbols = this.set(), + line; + for (var i = 0, ii = valuesy.length; i < ii; i++) { + if (!opts.nostroke) { + lines.push(line = this.path().attr({ + stroke: colors[i], + "stroke-width": opts.width || 2, + "stroke-linejoin": "round", + "stroke-linecap": "round", + "stroke-dasharray": opts.dash || "" + })); + } + var sym = this.raphael.is(symbol, "array") ? symbol[i] : symbol, + symset = this.set(); + path = []; + for (var j = 0, jj = valuesy[i].length; j < jj; j++) { + var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx; + var Y = y + height - gutter - (valuesy[i][j] - miny) * ky; + (Raphael.is(sym, "array") ? sym[j] : sym) && symset.push(this.g[Raphael.fn.g.markers[this.raphael.is(sym, "array") ? sym[j] : sym]](X, Y, (opts.width || 2) * 3).attr({fill: colors[i], stroke: "none"})); + path = path.concat([j ? "L" : "M", X, Y]); + } + symbols.push(symset); + if (opts.shade) { + shades[i].attr({path: path.concat(["L", X, y + height - gutter, "L", x + gutter + ((valuesx[i] || valuesx[0])[0] - minx) * kx, y + height - gutter, "z"]).join(",")}); + } + !opts.nostroke && line.attr({path: path.join(",")}); + } + function createColumns(f) { + // unite Xs together + var Xs = []; + for (var i = 0, ii = valuesx.length; i < ii; i++) { + Xs = Xs.concat(valuesx[i]); + } + Xs.sort(); + // remove duplicates + var Xs2 = [], + xs = []; + for (var i = 0, ii = Xs.length; i < ii; i++) { + Xs[i] != Xs[i - 1] && Xs2.push(Xs[i]) && xs.push(x + gutter + (Xs[i] - minx) * kx); + } + Xs = Xs2; + ii = Xs.length; + var cvrs = f || that.set(); + for (var i = 0; i < ii; i++) { + var X = xs[i] - (xs[i] - (xs[i - 1] || x)) / 2, + w = ((xs[i + 1] || x + width) - xs[i]) / 2 + (xs[i] - (xs[i - 1] || x)) / 2, + C; + f ? (C = {}) : cvrs.push(C = that.rect(X - 1, y, Math.max(w + 1, 1), height).attr({stroke: "none", fill: "#000", opacity: 0})); + C.values = []; + C.symbols = that.set(); + C.y = []; + C.x = xs[i]; + C.axis = Xs[i]; + for (var j = 0, jj = valuesy.length; j < jj; j++) { + Xs2 = valuesx[j] || valuesx[0]; + for (var k = 0, kk = Xs2.length; k < kk; k++) { + if (Xs2[k] == Xs[i]) { + C.values.push(valuesy[j][k]); + C.y.push(y + height - gutter - (valuesy[j][k] - miny) * ky); + C.symbols.push(chart.symbols[j][k]); + } + } + } + f && f.call(C); + } + !f && (columns = cvrs); + } + function createDots(f) { + var cvrs = f || that.set(), + C; + for (var i = 0, ii = valuesy.length; i < ii; i++) { + for (var j = 0, jj = valuesy[i].length; j < jj; j++) { + var X = x + gutter + ((valuesx[i] || valuesx[0])[j] - minx) * kx, + nearX = x + gutter + ((valuesx[i] || valuesx[0])[j ? j - 1 : 1] - minx) * kx, + Y = y + height - gutter - (valuesy[i][j] - miny) * ky; + f ? (C = {}) : cvrs.push(C = that.circle(X, Y, Math.abs(nearX - X) / 2).attr({stroke: "none", fill: "#000", opacity: 0})); + C.x = X; + C.y = Y; + C.value = valuesy[i][j]; + C.line = chart.lines[i]; + C.shade = chart.shades[i]; + C.symbol = chart.symbols[i][j]; + C.symbols = chart.symbols[i]; + C.axis = (valuesx[i] || valuesx[0])[j]; + f && f.call(C); + } + } + !f && (dots = cvrs); + } + chart.push(lines, shades, symbols, axis, columns, dots); + chart.lines = lines; + chart.shades = shades; + chart.symbols = symbols; + chart.axis = axis; + chart.hoverColumn = function (fin, fout) { + !columns && createColumns(); + columns.mouseover(fin).mouseout(fout); + return this; + }; + chart.clickColumn = function (f) { + !columns && createColumns(); + columns.click(f); + return this; + }; + chart.hrefColumn = function (cols) { + var hrefs = that.raphael.is(arguments[0], "array") ? arguments[0] : arguments; + if (!(arguments.length - 1) && typeof cols == "object") { + for (var x in cols) { + for (var i = 0, ii = columns.length; i < ii; i++) if (columns[i].axis == x) { + columns[i].attr("href", cols[x]); + } + } + } + !columns && createColumns(); + for (var i = 0, ii = hrefs.length; i < ii; i++) { + columns[i] && columns[i].attr("href", hrefs[i]); + } + return this; + }; + chart.hover = function (fin, fout) { + !dots && createDots(); + dots.mouseover(fin).mouseout(fout); + return this; + }; + chart.click = function (f) { + !dots && createDots(); + dots.click(f); + return this; + }; + chart.each = function (f) { + createDots(f); + return this; + }; + chart.eachColumn = function (f) { + createColumns(f); + return this; + }; + return chart; +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.pie.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.pie.js new file mode 100644 index 0000000..8d20374 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.pie.js @@ -0,0 +1,205 @@ +/* + * g.Raphael 0.4 - Charting library, based on Raphaël + * + * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com) + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. + */ +Raphael.fn.g.piechart = function (cx, cy, r, values, opts) { + opts = opts || {}; + var paper = this, + sectors = [], + covers = this.set(), + chart = this.set(), + series = this.set(), + order = [], + len = values.length, + angle = 0, + total = 0, + others = 0, + cut = 9, + defcut = true; + chart.covers = covers; + if (len == 1) { + series.push(this.circle(cx, cy, r).attr({fill: this.g.colors[0], stroke: opt.stroke || "#fff", "stroke-width": opts.strokewidth == null ? 1 : opts.strokewidth})); + covers.push(this.circle(cx, cy, r).attr(this.g.shim)); + total = values[0]; + values[0] = {value: values[0], order: 0, valueOf: function () { return this.value; }}; + series[0].middle = {x: cx, y: cy}; + series[0].mangle = 180; + } else { + function sector(cx, cy, r, startAngle, endAngle, fill) { + var rad = Math.PI / 180, + x1 = cx + r * Math.cos(-startAngle * rad), + x2 = cx + r * Math.cos(-endAngle * rad), + xm = cx + r / 2 * Math.cos(-(startAngle + (endAngle - startAngle) / 2) * rad), + y1 = cy + r * Math.sin(-startAngle * rad), + y2 = cy + r * Math.sin(-endAngle * rad), + ym = cy + r / 2 * Math.sin(-(startAngle + (endAngle - startAngle) / 2) * rad), + res = ["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(Math.abs(endAngle - startAngle) > 180), 1, x2, y2, "z"]; + res.middle = {x: xm, y: ym}; + return res; + } + for (var i = 0; i < len; i++) { + total += values[i]; + values[i] = {value: values[i], order: i, valueOf: function () { return this.value; }}; + } + values.sort(function (a, b) { + return b.value - a.value; + }); + for (var i = 0; i < len; i++) { + if (defcut && values[i] * 360 / total <= 1.5) { + cut = i; + defcut = false; + } + if (i > cut) { + defcut = false; + values[cut].value += values[i]; + values[cut].others = true; + others = values[cut].value; + } + } + len = Math.min(cut + 1, values.length); + others && values.splice(len) && (values[cut].others = true); + for (var i = 0; i < len; i++) { + var mangle = angle - 360 * values[i] / total / 2; + if (!i) { + angle = 90 - mangle; + mangle = angle - 360 * values[i] / total / 2; + } + if (opts.init) { + var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(","); + } + var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total); + var p = this.path(opts.init ? ipath : path).attr({fill: opts.colors && opts.colors[i] || this.g.colors[i] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round"}); + p.value = values[i]; + p.middle = path.middle; + p.mangle = mangle; + sectors.push(p); + series.push(p); + opts.init && p.animate({path: path.join(",")}, (+opts.init - 1) || 1000, ">"); + } + for (var i = 0; i < len; i++) { + var p = paper.path(sectors[i].attr("path")).attr(this.g.shim); + opts.href && opts.href[i] && p.attr({href: opts.href[i]}); + p.attr = function () {}; + covers.push(p); + series.push(p); + } + } + + chart.hover = function (fin, fout) { + fout = fout || function () {}; + var that = this; + for (var i = 0; i < len; i++) { + (function (sector, cover, j) { + var o = { + sector: sector, + cover: cover, + cx: cx, + cy: cy, + mx: sector.middle.x, + my: sector.middle.y, + mangle: sector.mangle, + r: r, + value: values[j], + total: total, + label: that.labels && that.labels[j] + }; + cover.mouseover(function () { + fin.call(o); + }).mouseout(function () { + fout.call(o); + }); + })(series[i], covers[i], i); + } + return this; + }; + // x: where label could be put + // y: where label could be put + // value: value to show + // total: total number to count % + chart.each = function (f) { + var that = this; + for (var i = 0; i < len; i++) { + (function (sector, cover, j) { + var o = { + sector: sector, + cover: cover, + cx: cx, + cy: cy, + x: sector.middle.x, + y: sector.middle.y, + mangle: sector.mangle, + r: r, + value: values[j], + total: total, + label: that.labels && that.labels[j] + }; + f.call(o); + })(series[i], covers[i], i); + } + return this; + }; + chart.click = function (f) { + var that = this; + for (var i = 0; i < len; i++) { + (function (sector, cover, j) { + var o = { + sector: sector, + cover: cover, + cx: cx, + cy: cy, + mx: sector.middle.x, + my: sector.middle.y, + mangle: sector.mangle, + r: r, + value: values[j], + total: total, + label: that.labels && that.labels[j] + }; + cover.click(function () { f.call(o); }); + })(series[i], covers[i], i); + } + return this; + }; + chart.inject = function (element) { + element.insertBefore(covers[0]); + }; + var legend = function (labels, otherslabel, mark, dir) { + var x = cx + r + r / 5, + y = cy, + h = y + 10; + labels = labels || []; + dir = (dir && dir.toLowerCase && dir.toLowerCase()) || "east"; + mark = paper.g.markers[mark && mark.toLowerCase()] || "disc"; + chart.labels = paper.set(); + for (var i = 0; i < len; i++) { + var clr = series[i].attr("fill"), + j = values[i].order, + txt; + values[i].others && (labels[j] = otherslabel || "Others"); + labels[j] = paper.g.labelise(labels[j], values[i], total); + chart.labels.push(paper.set()); + chart.labels[i].push(paper.g[mark](x + 5, h, 5).attr({fill: clr, stroke: "none"})); + chart.labels[i].push(txt = paper.text(x + 20, h, labels[j] || values[j]).attr(paper.g.txtattr).attr({fill: opts.legendcolor || "#000", "text-anchor": "start"})); + covers[i].label = chart.labels[i]; + h += txt.getBBox().height * 1.2; + } + var bb = chart.labels.getBBox(), + tr = { + east: [0, -bb.height / 2], + west: [-bb.width - 2 * r - 20, -bb.height / 2], + north: [-r - bb.width / 2, -r - bb.height - 10], + south: [-r - bb.width / 2, r + 10] + }[dir]; + chart.labels.translate.apply(chart.labels, tr); + chart.push(chart.labels); + }; + if (opts.legend) { + legend(opts.legend, opts.legendothers, opts.legendmark, opts.legendpos); + } + chart.push(series, covers); + chart.series = series; + chart.covers = covers; + return chart; +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.raphael.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.raphael.js new file mode 100644 index 0000000..8e94c36 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/g.raphael.js @@ -0,0 +1,481 @@ +/* + * g.Raphael 0.4 - Charting library, based on Raphaël + * + * Copyright (c) 2009 Dmitry Baranovskiy (http://g.raphaeljs.com) + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. + */ + + +(function () { + Raphael.fn.g = Raphael.fn.g || {}; + Raphael.fn.g.markers = { + disc: "disc", + o: "disc", + flower: "flower", + f: "flower", + diamond: "diamond", + d: "diamond", + square: "square", + s: "square", + triangle: "triangle", + t: "triangle", + star: "star", + "*": "star", + cross: "cross", + x: "cross", + plus: "plus", + "+": "plus", + arrow: "arrow", + "->": "arrow" + }; + Raphael.fn.g.shim = {stroke: "none", fill: "#000", "fill-opacity": 0}; + Raphael.fn.g.txtattr = {font: "12px Arial, sans-serif"}; + Raphael.fn.g.colors = []; + var hues = [.6, .2, .05, .1333, .75, 0]; + for (var i = 0; i < 10; i++) { + if (i < hues.length) { + Raphael.fn.g.colors.push("hsb(" + hues[i] + ", .75, .75)"); + } else { + Raphael.fn.g.colors.push("hsb(" + hues[i - hues.length] + ", 1, .5)"); + } + } + Raphael.fn.g.text = function (x, y, text) { + return this.text(x, y, text).attr(this.g.txtattr); + }; + Raphael.fn.g.labelise = function (label, val, total) { + if (label) { + return (label + "").replace(/(##+(?:\.#+)?)|(%%+(?:\.%+)?)/g, function (all, value, percent) { + if (value) { + return (+val).toFixed(value.replace(/^#+\.?/g, "").length); + } + if (percent) { + return (val * 100 / total).toFixed(percent.replace(/^%+\.?/g, "").length) + "%"; + } + }); + } else { + return (+val).toFixed(0); + } + }; + + Raphael.fn.g.finger = function (x, y, width, height, dir, ending, isPath) { + // dir 0 for horisontal and 1 for vertical + if ((dir && !height) || (!dir && !width)) { + return isPath ? "" : this.path(); + } + ending = {square: "square", sharp: "sharp", soft: "soft"}[ending] || "round"; + var path; + height = Math.round(height); + width = Math.round(width); + x = Math.round(x); + y = Math.round(y); + switch (ending) { + case "round": + if (!dir) { + var r = Math.floor(height / 2); + if (width < r) { + r = width; + path = ["M", x + .5, y + .5 - Math.floor(height / 2), "l", 0, 0, "a", r, Math.floor(height / 2), 0, 0, 1, 0, height, "l", 0, 0, "z"]; + } else { + path = ["M", x + .5, y + .5 - r, "l", width - r, 0, "a", r, r, 0, 1, 1, 0, height, "l", r - width, 0, "z"]; + } + } else { + var r = Math.floor(width / 2); + if (height < r) { + r = height; + path = ["M", x - Math.floor(width / 2), y, "l", 0, 0, "a", Math.floor(width / 2), r, 0, 0, 1, width, 0, "l", 0, 0, "z"]; + } else { + path = ["M", x - r, y, "l", 0, r - height, "a", r, r, 0, 1, 1, width, 0, "l", 0, height - r, "z"]; + } + } + break; + case "sharp": + if (!dir) { + var half = Math.floor(height / 2); + path = ["M", x, y + half, "l", 0, -height, Math.max(width - half, 0), 0, Math.min(half, width), half, -Math.min(half, width), half + (half * 2 < height), "z"]; + } else { + var half = Math.floor(width / 2); + path = ["M", x + half, y, "l", -width, 0, 0, -Math.max(height - half, 0), half, -Math.min(half, height), half, Math.min(half, height), half, "z"]; + } + break; + case "square": + if (!dir) { + path = ["M", x, y + Math.floor(height / 2), "l", 0, -height, width, 0, 0, height, "z"]; + } else { + path = ["M", x + Math.floor(width / 2), y, "l", 1 - width, 0, 0, -height, width - 1, 0, "z"]; + } + break; + case "soft": + var r; + if (!dir) { + r = Math.min(width, Math.round(height / 5)); + path = ["M", x + .5, y + .5 - Math.floor(height / 2), "l", width - r, 0, "a", r, r, 0, 0, 1, r, r, "l", 0, height - r * 2, "a", r, r, 0, 0, 1, -r, r, "l", r - width, 0, "z"]; + } else { + r = Math.min(Math.round(width / 5), height); + path = ["M", x - Math.floor(width / 2), y, "l", 0, r - height, "a", r, r, 0, 0, 1, r, -r, "l", width - 2 * r, 0, "a", r, r, 0, 0, 1, r, r, "l", 0, height - r, "z"]; + } + } + if (isPath) { + return path.join(","); + } else { + return this.path(path); + } + }; + + // Symbols + Raphael.fn.g.disc = function (cx, cy, r) { + return this.circle(cx, cy, r); + }; + Raphael.fn.g.line = function (cx, cy, r) { + return this.rect(cx - r, cy - r / 5, 2 * r, 2 * r / 5); + }; + Raphael.fn.g.square = function (cx, cy, r) { + r = r * .7; + return this.rect(cx - r, cy - r, 2 * r, 2 * r); + }; + Raphael.fn.g.triangle = function (cx, cy, r) { + r *= 1.75; + return this.path("M".concat(cx, ",", cy, "m0-", r * .58, "l", r * .5, ",", r * .87, "-", r, ",0z")); + }; + Raphael.fn.g.diamond = function (cx, cy, r) { + return this.path(["M", cx, cy - r, "l", r, r, -r, r, -r, -r, r, -r, "z"]); + }; + Raphael.fn.g.flower = function (cx, cy, r, n) { + r = r * 1.25; + var rout = r, + rin = rout * .5; + n = +n < 3 || !n ? 5 : n; + var points = ["M", cx, cy + rin, "Q"], + R; + for (var i = 1; i < n * 2 + 1; i++) { + R = i % 2 ? rout : rin; + points = points.concat([+(cx + R * Math.sin(i * Math.PI / n)).toFixed(3), +(cy + R * Math.cos(i * Math.PI / n)).toFixed(3)]); + } + points.push("z"); + return this.path(points.join(",")); + }; + Raphael.fn.g.star = function (cx, cy, r, r2) { + r2 = r2 || r * .5; + var points = ["M", cx, cy + r2, "L"], + R; + for (var i = 1; i < 10; i++) { + R = i % 2 ? r : r2; + points = points.concat([(cx + R * Math.sin(i * Math.PI * .2)).toFixed(3), (cy + R * Math.cos(i * Math.PI * .2)).toFixed(3)]); + } + points.push("z"); + return this.path(points.join(",")); + }; + Raphael.fn.g.cross = function (cx, cy, r) { + r = r / 2.5; + return this.path("M".concat(cx - r, ",", cy, "l", [-r, -r, r, -r, r, r, r, -r, r, r, -r, r, r, r, -r, r, -r, -r, -r, r, -r, -r, "z"])); + }; + Raphael.fn.g.plus = function (cx, cy, r) { + r = r / 2; + return this.path("M".concat(cx - r / 2, ",", cy - r / 2, "l", [0, -r, r, 0, 0, r, r, 0, 0, r, -r, 0, 0, r, -r, 0, 0, -r, -r, 0, 0, -r, "z"])); + }; + Raphael.fn.g.arrow = function (cx, cy, r) { + return this.path("M".concat(cx - r * .7, ",", cy - r * .4, "l", [r * .6, 0, 0, -r * .4, r, r * .8, -r, r * .8, 0, -r * .4, -r * .6, 0], "z")); + }; + + // Tooltips + Raphael.fn.g.tag = function (x, y, text, angle, r) { + angle = angle || 0; + r = r == null ? 5 : r; + text = text == null ? "$9.99" : text; + var R = .5522 * r, + res = this.set(), + d = 3; + res.push(this.path().attr({fill: "#000", stroke: "none"})); + res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); + res.update = function () { + this.rotate(0, x, y); + var bb = this[1].getBBox(); + if (bb.height >= r * 2) { + this[0].attr({path: ["M", x, y + r, "a", r, r, 0, 1, 1, 0, -r * 2, r, r, 0, 1, 1, 0, r * 2, "m", 0, -r * 2 -d, "a", r + d, r + d, 0, 1, 0, 0, (r + d) * 2, "L", x + r + d, y + bb.height / 2 + d, "l", bb.width + 2 * d, 0, 0, -bb.height - 2 * d, -bb.width - 2 * d, 0, "L", x, y - r - d].join(",")}); + } else { + var dx = Math.sqrt(Math.pow(r + d, 2) - Math.pow(bb.height / 2 + d, 2)); + // ["c", -R, 0, -r, R - r, -r, -r, 0, -R, r - R, -r, r, -r, R, 0, r, r - R, r, r, 0, R, R - r, r, -r, r] + // "a", r, r, 0, 1, 1, 0, -r * 2, r, r, 0, 1, 1, 0, r * 2, + this[0].attr({path: ["M", x, y + r, "c", -R, 0, -r, R - r, -r, -r, 0, -R, r - R, -r, r, -r, R, 0, r, r - R, r, r, 0, R, R - r, r, -r, r, "M", x + dx, y - bb.height / 2 - d, "a", r + d, r + d, 0, 1, 0, 0, bb.height + 2 * d, "l", r + d - dx + bb.width + 2 * d, 0, 0, -bb.height - 2 * d, "L", x + dx, y - bb.height / 2 - d].join(",")}); + } + this[1].attr({x: x + r + d + bb.width / 2, y: y}); + angle = (360 - angle) % 360; + this.rotate(angle, x, y); + angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]}); + return this; + }; + res.update(); + return res; + }; + Raphael.fn.g.popupit = function (x, y, set, dir, size) { + dir = dir == null ? 2 : dir; + size = size || 5; + x = Math.round(x) + .5; + y = Math.round(y) + .5; + var bb = set.getBBox(), + w = Math.round(bb.width / 2), + h = Math.round(bb.height / 2), + dx = [0, w + size * 2, 0, -w - size * 2], + dy = [-h * 2 - size * 3, -h - size, 0, -h - size], + p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size, + "l", 0, -Math.max(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -Math.max(h - size, 0), "a", size, size, 0, 0, 1, size, -size, + "l", Math.max(w - size, 0), 0, size, !dir * -size, size, !dir * size, Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size, + "l", 0, Math.max(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, Math.max(h - size, 0), "a", size, size, 0, 0, 1, -size, size, + "l", -Math.max(w - size, 0), 0, "z"].join(","), + xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir]; + set.translate(xy.x - w - bb.x, xy.y - h - bb.y); + return this.path(p).attr({fill: "#000", stroke: "none"}).insertBefore(set.node ? set : set[0]); + }; + Raphael.fn.g.popup = function (x, y, text, dir, size) { + dir = dir == null ? 2 : dir; + size = size || 5; + text = text || "$9.99"; + var res = this.set(), + d = 3; + res.push(this.path().attr({fill: "#000", stroke: "none"})); + res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); + res.update = function (X, Y, withAnimation) { + X = X || x; + Y = Y || y; + var bb = this[1].getBBox(), + w = bb.width / 2, + h = bb.height / 2, + dx = [0, w + size * 2, 0, -w - size * 2], + dy = [-h * 2 - size * 3, -h - size, 0, -h - size], + p = ["M", X - dx[dir], Y - dy[dir], "l", -size, (dir == 2) * -size, -Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size, + "l", 0, -Math.max(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -Math.max(h - size, 0), "a", size, size, 0, 0, 1, size, -size, + "l", Math.max(w - size, 0), 0, size, !dir * -size, size, !dir * size, Math.max(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size, + "l", 0, Math.max(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, Math.max(h - size, 0), "a", size, size, 0, 0, 1, -size, size, + "l", -Math.max(w - size, 0), 0, "z"].join(","), + xy = [{x: X, y: Y + size * 2 + h}, {x: X - size * 2 - w, y: Y}, {x: X, y: Y - size * 2 - h}, {x: X + size * 2 + w, y: Y}][dir]; + if (withAnimation) { + this[0].animate({path: p}, 500, ">"); + this[1].animate(xy, 500, ">"); + } else { + this[0].attr({path: p}); + this[1].attr(xy); + } + return this; + }; + return res.update(x, y); + }; + Raphael.fn.g.flag = function (x, y, text, angle) { + angle = angle || 0; + text = text || "$9.99"; + var res = this.set(), + d = 3; + res.push(this.path().attr({fill: "#000", stroke: "none"})); + res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); + res.update = function (x, y) { + this.rotate(0, x, y); + var bb = this[1].getBBox(), + h = bb.height / 2; + this[0].attr({path: ["M", x, y, "l", h + d, -h - d, bb.width + 2 * d, 0, 0, bb.height + 2 * d, -bb.width - 2 * d, 0, "z"].join(",")}); + this[1].attr({x: x + h + d + bb.width / 2, y: y}); + angle = 360 - angle; + this.rotate(angle, x, y); + angle > 90 && angle < 270 && this[1].attr({x: x - r - d - bb.width / 2, y: y, rotation: [180 + angle, x, y]}); + return this; + }; + return res.update(x, y); + }; + Raphael.fn.g.label = function (x, y, text) { + var res = this.set(); + res.push(this.rect(x, y, 10, 10).attr({stroke: "none", fill: "#000"})); + res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff"})); + res.update = function () { + var bb = this[1].getBBox(), + r = Math.min(bb.width + 10, bb.height + 10) / 2; + this[0].attr({x: bb.x - r / 2, y: bb.y - r / 2, width: bb.width + r, height: bb.height + r, r: r}); + }; + res.update(); + return res; + }; + Raphael.fn.g.labelit = function (set) { + var bb = set.getBBox(), + r = Math.min(20, bb.width + 10, bb.height + 10) / 2; + return this.rect(bb.x - r / 2, bb.y - r / 2, bb.width + r, bb.height + r, r).attr({stroke: "none", fill: "#000"}).insertBefore(set[0]); + }; + Raphael.fn.g.drop = function (x, y, text, size, angle) { + size = size || 30; + angle = angle || 0; + var res = this.set(); + res.push(this.path(["M", x, y, "l", size, 0, "A", size * .4, size * .4, 0, 1, 0, x + size * .7, y - size * .7, "z"]).attr({fill: "#000", stroke: "none", rotation: [22.5 - angle, x, y]})); + angle = (angle + 90) * Math.PI / 180; + res.push(this.text(x + size * Math.sin(angle), y + size * Math.cos(angle), text).attr(this.g.txtattr).attr({"font-size": size * 12 / 30, fill: "#fff"})); + res.drop = res[0]; + res.text = res[1]; + return res; + }; + Raphael.fn.g.blob = function (x, y, text, angle, size) { + angle = (+angle + 1 ? angle : 45) + 90; + size = size || 12; + var rad = Math.PI / 180, + fontSize = size * 12 / 12; + var res = this.set(); + res.push(this.path().attr({fill: "#000", stroke: "none"})); + res.push(this.text(x + size * Math.sin((angle) * rad), y + size * Math.cos((angle) * rad) - fontSize / 2, text).attr(this.g.txtattr).attr({"font-size": fontSize, fill: "#fff"})); + res.update = function (X, Y, withAnimation) { + X = X || x; + Y = Y || y; + var bb = this[1].getBBox(), + w = Math.max(bb.width + fontSize, size * 25 / 12), + h = Math.max(bb.height + fontSize, size * 25 / 12), + x2 = X + size * Math.sin((angle - 22.5) * rad), + y2 = Y + size * Math.cos((angle - 22.5) * rad), + x1 = X + size * Math.sin((angle + 22.5) * rad), + y1 = Y + size * Math.cos((angle + 22.5) * rad), + dx = (x1 - x2) / 2, + dy = (y1 - y2) / 2, + rx = w / 2, + ry = h / 2, + k = -Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * dy * dy - ry * ry * dx * dx) / (rx * rx * dy * dy + ry * ry * dx * dx)), + cx = k * rx * dy / ry + (x1 + x2) / 2, + cy = k * -ry * dx / rx + (y1 + y2) / 2; + if (withAnimation) { + this.animate({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")}, 500, ">"); + } else { + this.attr({x: cx, y: cy, path: ["M", x, y, "L", x1, y1, "A", rx, ry, 0, 1, 1, x2, y2, "z"].join(",")}); + } + return this; + }; + res.update(x, y); + return res; + }; + + Raphael.fn.g.colorValue = function (value, total, s, b) { + return "hsb(" + [Math.min((1 - value / total) * .4, 1), s || .75, b || .75] + ")"; + }; + + Raphael.fn.g.snapEnds = function (from, to, steps) { + var f = from, + t = to; + if (f == t) { + return {from: f, to: t, power: 0}; + } + function round(a) { + return Math.abs(a - .5) < .25 ? Math.floor(a) + .5 : Math.round(a); + } + var d = (t - f) / steps, + r = Math.floor(d), + R = r, + i = 0; + if (r) { + while (R) { + i--; + R = Math.floor(d * Math.pow(10, i)) / Math.pow(10, i); + } + i ++; + } else { + while (!r) { + i = i || 1; + r = Math.floor(d * Math.pow(10, i)) / Math.pow(10, i); + i++; + } + i && i--; + } + var t = round(to * Math.pow(10, i)) / Math.pow(10, i); + if (t < to) { + t = round((to + .5) * Math.pow(10, i)) / Math.pow(10, i); + } + var f = round((from - (i > 0 ? 0 : .5)) * Math.pow(10, i)) / Math.pow(10, i); + return {from: f, to: t, power: i}; + }; + Raphael.fn.g.axis = function (x, y, length, from, to, steps, orientation, labels, type, dashsize) { + dashsize = dashsize == null ? 3 : dashsize; + type = type || "t"; + steps = steps || 10; + var path = type == "|" || type == " " ? ["M", x + .5, y, "l", 0, .001] : orientation == 1 || orientation == 3 ? ["M", x + .5, y, "l", 0, -length] : ["M", x, y + .5, "l", length, 0], + ends = this.g.snapEnds(from, to, steps), + f = ends.from, + t = ends.to, + i = ends.power, + j = 0, + text = this.set(); + d = (t - f) / steps; + var label = f, + rnd = i > 0 ? i : 0; + dx = length / steps; + if (+orientation == 1 || +orientation == 3) { + var Y = y, + addon = (orientation - 1 ? 1 : -1) * (dashsize + 3 + !!(orientation - 1)); + while (Y >= y - length) { + type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), Y + .5, "l", dashsize * 2 + 1, 0])); + text.push(this.text(x + addon, Y, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr).attr({"text-anchor": orientation - 1 ? "start" : "end"})); + label += d; + Y -= dx; + } + if (Math.round(Y + dx - (y - length))) { + type != "-" && type != " " && (path = path.concat(["M", x - (type == "+" || type == "|" ? dashsize : !(orientation - 1) * dashsize * 2), y - length + .5, "l", dashsize * 2 + 1, 0])); + text.push(this.text(x + addon, y - length, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr).attr({"text-anchor": orientation - 1 ? "start" : "end"})); + } + } else { + var X = x, + label = f, + rnd = i > 0 ? i : 0, + addon = (orientation ? -1 : 1) * (dashsize + 9 + !orientation), + dx = length / steps, + txt = 0, + prev = 0; + while (X <= x + length) { + + text.push(txt = this.text(X, y + addon, (labels && labels[j++]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr)); + var bb = txt.getBBox(); + var ds = dashsize; + if (prev >= bb.x - 5) { + text.pop(text.length - 1).remove(); + ds = 1; + } else { + prev = bb.x + bb.width; + } + + type != "-" && type != " " && (path = path.concat(["M", X + .5, y - (type == "+" ? ds : !!orientation * ds * 2), "l", 0, ds * 2 + 1])); + + label += d; + X += dx; + } + if (Math.round(X - dx - x - length)) { + type != "-" && type != " " && (path = path.concat(["M", x + length + .5, y - (type == "+" ? dashsize : !!orientation * dashsize * 2), "l", 0, dashsize * 2 + 1])); + text.push(this.text(x + length, y + addon, (labels && labels[j]) || (Math.round(label) == label ? label : +label.toFixed(rnd))).attr(this.g.txtattr)); + } + } + var res = this.path(path); + res.text = text; + res.all = this.set([res, text]); + res.remove = function () { + this.text.remove(); + this.constructor.prototype.remove.call(this); + }; + return res; + }; + + Raphael.el.lighter = function (times) { + times = times || 2; + var fs = [this.attrs.fill, this.attrs.stroke]; + this.fs = this.fs || [fs[0], fs[1]]; + fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex); + fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex); + fs[0].b = Math.min(fs[0].b * times, 1); + fs[0].s = fs[0].s / times; + fs[1].b = Math.min(fs[1].b * times, 1); + fs[1].s = fs[1].s / times; + this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"}); + }; + Raphael.el.darker = function (times) { + times = times || 2; + var fs = [this.attrs.fill, this.attrs.stroke]; + this.fs = this.fs || [fs[0], fs[1]]; + fs[0] = Raphael.rgb2hsb(Raphael.getRGB(fs[0]).hex); + fs[1] = Raphael.rgb2hsb(Raphael.getRGB(fs[1]).hex); + fs[0].s = Math.min(fs[0].s * times, 1); + fs[0].b = fs[0].b / times; + fs[1].s = Math.min(fs[1].s * times, 1); + fs[1].b = fs[1].b / times; + this.attr({fill: "hsb(" + [fs[0].h, fs[0].s, fs[0].b] + ")", stroke: "hsb(" + [fs[1].h, fs[1].s, fs[1].b] + ")"}); + }; + Raphael.el.original = function () { + if (this.fs) { + this.attr({fill: this.fs[0], stroke: this.fs[1]}); + delete this.fs; + } + }; +})(); \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/load-big.gif b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/load-big.gif new file mode 100644 index 0000000..ddb7ff1 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/load-big.gif differ diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/load.gif b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/load.gif new file mode 100644 index 0000000..d0bce15 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/load.gif differ diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.css b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.css new file mode 100644 index 0000000..a84d90e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.css @@ -0,0 +1,54 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +*/ +body { font-family: sans-serif; } + +div.fileSelector { border: solid 3px black; position: absolute; background: white; + -moz-border-radius: 10px; border-radius: 10px; + padding: 5px; font-family: sans-serif; + right: 10px; top: 10px; + } +div.fileSelector a { cursor: pointer; } +.fileSelector li.selectedFile { background: lightgreen; } + +div.selector { border: solid 3px black; position: absolute; background: white; + -moz-border-radius: 10px; border-radius: 10px; + padding: 5px; + right: 10px; top: 10px; background: #aaaaaa; opacity: 0.7; + } +div.selector a { cursor: pointer; } +.fileSelector li.selectedFile { background: lightgreen; } + +#fileLoader { -moz-border-radius: 10px; border-radius: 10px; background: #aaaaaa; opacity: 0.7; position: absolute; left: 20px; top: 20px; } +#loadingScreen { position: absolute; top: 100px; margin-left: 40%; margin-right: 40%; width: 500px; background: #aaaaaa; opacity: 0.7; -moz-border-radius: 10px; border-radius: 10px; text-align: center } +#filterinput { width: 500px; height: 100px; } +/* main interface */ +#actions { float: right; } +#views { float: left; } + +.closebutton { position: absolute; right: 5px; float: right; display: block; cursor: pointer; } + +.actionbutton { color: blue; text-decoration: none; padding: 3px; cursor: pointer; } +span:hover.actionbutton { background: lightblue; } + +#status { text-align: center; } + +#canvas { width: 100%; height: 1000px; } + +#logtable { width: 100%; } +.popUp { border: 3px solid black; -moz-border-radius: 10px; border-radius: 10px; position: absolute; background: white; padding: 10px; min-width: 300px; } + +.errorpage { position: absolute; top: 100px; margin-left: 40%; margin-right: 40%; width: 500px; background: #aaaaaa; opacity: 0.7; -moz-border-radius: 10px; border-radius: 10px; padding: 10px; } \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.js new file mode 100644 index 0000000..87bb7d8 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.js @@ -0,0 +1,262 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +LogGraph = function(canvas, status) { + this.canvas = document.getElementById(canvas); + this.status = document.getElementById(status); + this.starttime = 0; + this.endtime = 0; + this.period = 0; + this.numEntries = 0; + this.currentRender = 0; + this.filter = ""; + + this.saveFilters = function () { + localStorage.starttime = this.starttime; + localStorage.endtime = this.endtime; + localStorage.period = this.period; + localStorage.filter = this.filter; + + }; + this.loadFilters = function () { + if (localStorage.starttime) { this.starttime = parseInt(localStorage.starttime); } + if (localStorage.endtime) { this.endtime = parseInt(localStorage.endtime); } + if (localStorage.period) { this.period = parseInt(localStorage.period); } + if (localStorage.filter) { this.filter = localStorage.filter; } + }; + this.loadFilters(); + var self = this; + + var updateStatus = function (starttime, period, filter, numEntries) { + self.starttime = starttime; + self.endtime = starttime + period; + self.period = period; + self.filter = filter; + self.saveFilters(); + + self.status.innerHTML = dateFormat(starttime, "HH:MM:ss,l") + " ⇒ " + dateFormat(self.endtime, "HH:MM:ss,l") + "    |    " + numEntries + " entries    |    " + (filter ? filter : "No filter"); + + if (self.currentRender) { + self.currentRender(); + } + }; + + YUI().use("io-base", function(Y) { + var uri = "/info"; + if (self.starttime) { + var uri = "/info?start=" + self.starttime + "&period=" + self.period + "&filter=" + self.filter; + } + + function complete(id, o, args) { + var data = eval("(" + o.responseText + ")"); // Response data. + var period = data.endTime - data.startTime; + updateStatus(data.startTime, period, self.filter, data.numEntries); + }; + + Y.on('io:complete', complete, Y, []); + var request = Y.io(uri); + }); + + this.addLogs = function() { + new LogGraph.fileSelector(function (files) { new LogGraph.fileLoader(files); }); + }; + + this.editFilters = function() { + new LogGraph.filterSelector(this.starttime, this.period, this.filter, updateStatus); + }; + + this.getCleanCanvas = function () { + this.canvas.innerHTML = ""; + return this.canvas; + }; + + this.showLoadingScreen = function () { + this.loadingScreen = document.createElement("div"); + this.loadingScreen.id = "loadingScreen"; + this.loadingScreen.innerHTML = "

Loading...

"; + document.body.appendChild(this.loadingScreen); + }; + + this.hideLoadingScreen = function () { + document.body.removeChild(this.loadingScreen); + this.loadingScreen.style.visibility = "hidden"; + }; + + + /*** + * TODO: refactor these to load the data first, before handing to a draw funciton. + * We shouldn't pass the async q into the drawing function + */ + this.showLogs = function() { + var self= this; + YUI().use('async-queue', function(Y) { + var q = new Y.AsyncQueue(self.showLoadingScreen, + // The second callback will pause the Queue and send an XHR for data + function () { + q.pause(); + var loggraph = new LogGraph.LogTable(q, self.getCleanCanvas(), self.starttime, self.endtime, self.filter); + self.currentRender = self.showLogs; + }, + self.hideLoadingScreen); + q.run(); + } + ); + }; + + this.serverGraph = function() { + var self= this; + YUI().use('async-queue', function(Y) { + var q = new Y.AsyncQueue(self.showLoadingScreen, + // The second callback will pause the Queue and send an XHR for data + function () { + q.pause(); + var servergraph = new LogGraph.ServerGraph(q, self.getCleanCanvas(), self.starttime, self.endtime, self.filter); + self.currentRender = self.showLogs; + }, + self.hideLoadingScreen); + q.run(); + } + ); + }; + + this.sessionGraph = function() { + var self= this; + YUI().use('async-queue', function(Y) { + var q = new Y.AsyncQueue(self.showLoadingScreen, + // The second callback will pause the Queue and send an XHR for data + function () { + q.pause(); + var sessiongraph = new LogGraph.SessionGraph(q, self.getCleanCanvas(), self.starttime, self.endtime, self.filter); + self.currentRender = self.sessionGraph; + }, + self.hideLoadingScreen); + q.run(); + } + ); + }; + + this.showStats = function() { + var self= this; + YUI().use('async-queue', function(Y) { + var q = new Y.AsyncQueue(self.showLoadingScreen, + // The second callback will pause the Queue and send an XHR for data + function () { + q.pause(); + var statgraph = new LogGraph.StatsGraph(q, self.getCleanCanvas(), self.starttime, self.endtime, self.filter); + self.currentRender = self.showStats; + }, + self.hideLoadingScreen); + q.run(); + } + ); + }; +}; + +LogGraph.error = function(description) { + var errorPage = document.createElement("div"); + errorPage.className = "errorpage"; + var p = document.createElement("p"); + p.innerHTML = description; + errorPage.appendChild(p); + + var span = document.createElement("span"); + p = document.createElement("p"); + span.className = "actionButton"; + span.innerHTML = "OK"; + span.onclick = function (evt) { + document.body.removeChild(errorPage); + delete errorPage; + } + p.appendChild(span); + errorPage.appendChild(p); + + document.body.appendChild(errorPage); +}; + +LogGraph.ticker =function(allow_dups) { + this.ticks = new Array(); + this.current_tick = 0; + this.allow_dups = allow_dups;; + + this.tick = function(time) { + if (time == this.ticks[this.ticks.length - 1] && this.allow_dups == true) + return this.current_tick; + + this.ticks.push(time); + return this.current_tick++; + }; + + this.current = function() { + return this.current_tick; + }; + + this.reset = function() { + while (this.ticks.length) { + this.ticks.pop(); + } + this.current_tick = 0; + }; +}; + + +LogGraph.timescale = function(starttime, endtime) { + this.starttime = starttime; + this.endtime = endtime; + this.millis = endtime - starttime; + + this.draw = function(paper) { + var scale = paper.set(); + scale.push(paper.path("M0 0 L" + paper.width + " 0")); + + for (var i = 0; i < paper.width; i += 100) { + scale.push(paper.path("M" + i + " 0 L" + i + " 5")); + // var time = dateFormat((this.starttime + (i*ms_per_pixel)), "h:MM:ss,l"); + // paper.text(i + 5, 10, time); + } + + scale.attr({"stroke-width": 2}); + }; +}; + +/* + Fetch data from an uri and process it, the process data func returns true if any of the data is useful +*/ +LogGraph.loadData = function (asyncq, uri, processdata) { + YUI().use("io-base", function(Y) { + function success(id, o, args) { + var data = eval("(" + o.responseText + ")"); // Response data. + if (data.error) { + LogGraph.error(data.error); + } else { + if (!processdata(data)) { + LogGraph.error("No data. Perhaps you should loosen your filter criteria."); + } + } + asyncq.run(); + }; + function failure(id, o, args) { + LogGraph.error("Error contacting server: (" + o.status + ") " + o.statusText); + asyncq.run(); + }; + + Y.on('io:success', success, Y, []); + Y.on('io:failure', failure, Y, []); + + var request = Y.io(uri); + }); +} \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.log.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.log.js new file mode 100644 index 0000000..551ea4b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.log.js @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +LogGraph.LogTable = function (asyncq, canvas, starttime, endtime, filter) { + this.starttime = starttime; + this.endtime = endtime; + this.filter = filter; + + var table = document.createElement("table"); + table.id = "logtable"; + canvas.appendChild(table); + + this.addLogLine = function(time, text) { + var tr = document.createElement("tr"); + table.appendChild(tr); + + var td = document.createElement("td"); + td.innerHTML = dateFormat(time, "h:MM:ss,l"); + tr.appendChild(td); + + td = document.createElement("td"); + td.innerHTML = text; + tr.appendChild(td); + } + + var self = this; + var processdata = function(data) { + var events = data["events"]; + var count = 0; + for (var i in events) { + var e = events[i]; + if (e.type == "text") { + self.addLogLine(e.time, e.text); + count++; + } + } + return count != 0; + }; + + var uri = "/data?start=" + self.starttime + "&end=" + self.endtime + "&filter=" + self.filter; + LogGraph.loadData(asyncq, uri, processdata); +}; diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.server.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.server.js new file mode 100644 index 0000000..0a74b5c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.server.js @@ -0,0 +1,329 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +LogGraph.ServerGraph = function(asyncq, canvas, starttime, endtime, filter) { + this.starttime = starttime; + this.endtime = endtime; + this.millis = endtime - starttime; + this.nextserverid = 0; + this.serveroffset = 100; + this.filter = filter; + + this.pixels_per_tick = 20; + this.ticker = new LogGraph.ticker(); + + + var paper = Raphael(canvas, 1, 1); + + var self = this; + + this.timescale = new LogGraph.timescale(starttime, endtime); + this.objects = new Array(); + + this.add = function(obj) { + this.objects.push(obj); + } + + this.tick_to_x = function (timestamp) { + var x = timestamp * this.pixels_per_tick; + return x; + }; + + this._drawTime = function(paper, x, time) { + var p = paper.path("M" + x + " 0 L" + x + " " + paper.height); + var t = paper.text(x, 10, dateFormat(time, "h:MM:ss,l")); + + t.hide(); + p.mouseover(function(evt) { + t.show(); + p.attr({stroke: "red"}); + }); + p.mouseout(function(evt) { + t.hide(); + p.attr({stroke: "lightgray"}); + }); + + return p; + }; + + this.draw = function(paper) { + var grid = paper.set(); + for (var i = 0; i < paper.height; i += 20) { + grid.push(paper.path("M0 " + i + " L" + paper.width + " " + i)); + } + var lasttick = this.starttime; + var scale = 500; // 500 ms + + var y = 0; + + for (var t = 0, len = this.ticker.ticks.length; t < len; t++) { + var basex = t * this.pixels_per_tick; + var thistick = this.ticker.ticks[t]; + var nexttick = t + 1 == this.ticker.ticks.length ? this.endtime : this.ticker.ticks[t+1]; + if (nexttick == thistick) { + continue; + } + var time = thistick - lasttick; + var first = scale - (lasttick % scale); + + /* for (var i = 0; (first+scale*i) < time; i++) { + + var toffset = first+scale*i; + var x = basex + LogGraph._pixels_per_tick * toffset/time; + grid.push(this._drawTime(paper, x, lasttick + toffset, grid)); + + }*/ + + + //grid.push(paper.path("M" + i + " 0 L" + i + " " + paper.height)); + lasttick = thistick; + } + grid.attr({stroke: "lightgray"}); + this.timescale.draw(paper); + + for (o in this.objects) { + this.objects[o].draw(paper); + } + }; + + + var processdata = function(data) { + var servermap = {}; + var servers = data.servers; + var count = 0; + for (s in servers) { + var server = new LogGraph.ServerGraph.server(self, "Server " + servers[s]); + servermap[servers[s]] = server; + self.add(server); + count++; + } + + var messages = {}; + var events = data.events; + for (var i in events) { + var e = events[i]; + var t = e.time; + if (e.type == "stateChange") { + servermap[e.server].addState(e.state, self.ticker.tick(e.time)); + } + if (e.type == "postmessage") { + src = servermap[e.src]; + dst = servermap[e.dst]; + var key = "key:s" + e.src + ",d" + e.dst + ",z" + e.zxid; + + var m = new LogGraph.ServerGraph.message(self, src, self.ticker.tick(e.time), dst, e.zxid); + messages[key] = m; + } + if (e.type == "delivermessage") { + var key = "key:s" + e.src + ",d" + e.dst + ",z" + e.zxid; + + var m = messages[key]; + if (m) { + m.dsttime = self.ticker.tick(e.time); + m.name = "Propose"; + self.add(m); + delete messages[key]; + } + } + if (e.type == "exception") { + servermap[e.server].addException(self.ticker.tick(e.time), e.text, e.time); + } + count++; + } + + for (var i in messages) { + var m = messages[i]; + m.markIncomplete(); + self.add(m); + count++; + } + + if (count != 0) { + paper.setSize(self.tick_to_x(self.ticker.current()), 1000); + + var line = paper.path("M0 0 L0 1000"); + line.attr({"stroke": "red", "stroke-dasharray": "- "}); + var base = canvas.offsetLeft;// + ((canvas.offsetWidth - paper.width)/2); + canvas.onmousemove = function (evt) { + var x = evt.screenX - base; + + line.attr({"path": "M" + x + " 0 L"+ x +" 1000"}); + + }; + + self.draw(paper); + return true; + } else { + return false; + } + }; + + var uri = "/data?start=" + self.starttime + "&end=" + self.endtime + "&filter=" + filter; + + LogGraph.loadData(asyncq, uri, processdata); +}; + +LogGraph.ServerGraph.server = function (graph, name) { + this.graph = graph; + this.serverid = graph.nextserverid++; + this.name = name; + this.y = (this.serverid * 300 + graph.serveroffset); + this.states = new Array(); + this.exception = new Array(); + + this.addState = function(state, time) { + this.states.push([state, time]); + } + + this.addException = function(tick, exception, time) { + this.exception.push(new LogGraph.ServerGraph.exception(this.graph, tick, exception, time)); + } + + this.draw = function(paper) { + var st = paper.set(); + st.push(paper.path("M0 " + this.y + " L" + paper.width + " " + this.y)); + st.push(paper.text(20, this.y - 10, this.name)); + st.attr({stroke: "gray"}); + + var numstates = this.states.length; + + for (s = 0; s < numstates; s++) { + var style = {}; + switch (this.states[s][0]) { + case "INIT": style = {stroke: "yellow", "stroke-width":3}; break; + case "FOLLOWING": style = {stroke: "lightgreen", "stroke-width":7}; break; + case "LEADING": style = {stroke: "green", "stroke-width":10}; break; + case "LOOKING": style = {stroke: "orange", "stroke-width":5}; break; + } + var startx = this.graph.tick_to_x(this.states[s][1]); + var endx = s + 1 < numstates ? this.graph.tick_to_x(this.states[(s+1)][1]) : paper.width; + var p = paper.path("M" + startx + " " + this.y + " L" + endx + " " + this.y); + p.attr(style); + } + + for (e in this.exception) { + this.exception[e].draw(paper, this); + } + } +}; + +LogGraph.ServerGraph.message = function(graph, src, srctime, dst, zxid) { + this.graph = graph; + this.src = src; + this.srctime = srctime; + this.dst = dst; + this.dsttime = 0; //dsttime; + this.name = "Unknown"; + this.zxid = zxid; + this.moreinfo = "No extra information"; + this.incomplete = false; + + this.markIncomplete = function() { + this.incomplete = true; + this.dsttime = this.srctime; + } + + this.draw = function(paper) { + var srcx = this.graph.tick_to_x(this.srctime); + var dstx = this.graph.tick_to_x(this.dsttime); + + var arrow = paper.set(); + var p = paper.path("M" + srcx + " " + this.src.y + " L" + dstx + " " + this.dst.y); + arrow.push(p); + + var tx = (srcx + dstx)/2; + var ty = (this.src.y + this.dst.y)/2; + var t = paper.text(tx, ty, this.name); + + var gradiant = (this.dst.y - this.src.y)/(dstx - srcx); + var angle = Math.atan(gradiant) * 57.2958; + t.rotate(angle, true); + + var arrowl = paper.path("M" + dstx + " " + this.dst.y + " L" + (dstx - 10) +" " + this.dst.y); + arrowl.rotate(angle + 20, dstx, this.dst.y); + arrow.push(arrowl); + var arrowr = paper.path("M" + dstx + " " + this.dst.y + " L" + (dstx - 10) +" " + this.dst.y); + arrowr.rotate(angle - 20, dstx, this.dst.y); + arrow.push(arrowr); + + arrow.attr({"stroke-width": 2, stroke: "gray"}); + if (this.incomplete) { + arrow.attr({"stroke-dasharray": "- .", stroke: "pink", "stroke-width": 2}); + } + arrow.mouseover(function(evt) { + t.attr({"font-size": 20}); + arrow.attr({stroke: "red", "stroke-width": 3}); + }); + arrow.mouseout(function(evt) { + t.attr({"font-size": 10}); + + if (this.incomplete) { + arrow.attr({stroke: "pink", "stroke-width": 2}); + } else { + arrow.attr({stroke: "gray", "stroke-width": 2}); + } + }); + + + + arrow.click(function(evt) { + var popup = document.createElement("div"); + popup.className = "popUp"; + popup.innerHTML = "zxid: " + parseInt(this.zxid).toString(16); + + popup.style.top = evt.clientY; + popup.style.left = evt.clientX; + document.body.appendChild(popup); + + popup.onclick = function(evt) { + document.body.removeChild(popup); + }; + }); + } +}; + +LogGraph.ServerGraph.exception = function(graph, tick, exceptiontext, time) { + this.graph = graph; + this.time = time; + this.text = exceptiontext; + this.tick = tick; + + var self = this; + + this.draw = function(paper, server) { + var center = this.graph.tick_to_x(this.tick); + var p = paper.circle(center, server.y, 5); + p.attr({stroke: "orange", fill: "red"}); + + p.mouseover(function(evt) { + p.popup = document.createElement("div"); + p.popup.className = "popUp"; + p.popup.innerHTML = self.text.replace("\n", "
");; + p.popup.style.top = server.y + 50; + p.popup.style.left = center + 25; + document.body.appendChild(p.popup); + + p.animate({r: 10}, 500, "elastic"); + }); + p.mouseout(function(evt) { + document.body.removeChild(p.popup); + p.animate({r: 5}, 100); + }); + } +}; + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.session.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.session.js new file mode 100644 index 0000000..5a314d8 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.session.js @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +LogGraph.SessionGraph = function (asyncq, canvas, starttime, endtime, filter) { + this.sessions = new Array(); + this.counter = 0; + this.exceptions = new Array(); + + this.pix_per_ticks = 4; + this.pix_per_session = 7; + + var paper = Raphael(canvas, 1, 1); + this.ticker = new LogGraph.ticker(); + var self = this; + + this.starttime = starttime; + this.endtime = endtime; + this.filter = filter; + + this.findOrCreateSession = function(id) { + if (this.sessions[id] == undefined) { + this.sessions[id] = new LogGraph.SessionGraph.session(this, ++this.counter, id); + } + return this.sessions[id]; + } + + this.height = function () { return this.counter * this.pix_per_session + 10; }; + this.width = function () { return (self.ticker.current() * this.pix_per_ticks); }; + + this.draw = function(paper) { + + + var line = paper.path("M0 0 L0 " + this.height()); + line.attr({"stroke": "red", "stroke-dasharray": "- "}); + var base = canvas.offsetLeft; + var width = this.width(); + canvas.onmousemove = function (evt) { + var x = evt.clientX - base; + + line.attr({"path": "M" + x + " 0 L" + x + " " + self.height() }); + }; + + for (var i in this.sessions) { + var s = this.sessions[i]; + s.draw(paper); + } + }; + + var processdata = function(data) { + var count = 0; + for (var i in data.events) { + var e = data.events[i]; + if (e.type == "transaction") { + e.tick = self.ticker.tick(e.time, true); + var session = self.findOrCreateSession(e.client); + session.addEvent(e); + count++; + } + } + paper.setSize(self.width(), self.height()); + + if (count != 0) { + self.draw(paper); + return true; + } else { + return false; + } + }; + + var uri = "/data?start=" + self.starttime + "&end=" + self.endtime + "&filter=" + filter; + + LogGraph.loadData(asyncq, uri, processdata); +}; + +LogGraph.SessionGraph.sessionevent = function () { + this.time = time; + this.type = type; + this.client = client; + this.cxid = cxid; + this.zxid = zxid; + this.op = op; + this.extra = extra; +}; + +LogGraph.SessionGraph.sessionEventPopup = function (obj, e, x, y) { + obj.click(function(evt) { + var popup = document.createElement("div"); + popup.className = "popUp"; + + var closebutton = document.createElement("div"); + closebutton.className = "closebutton"; + closebutton.title = "Close popup"; + closebutton.innerHTML = "×"; + popup.appendChild(closebutton); + closebutton.onclick= function(evt) { popup.style.visibility = "hidden"; document.body.removeChild(popup) }; + var txt = document.createElement("span"); + txt.innerHTML = "session: " + e.client + "
op: " + e.op + "
zxid: " + e.zxid + "
time: " + e.time + "
extra: " + e.extra; + popup.appendChild(txt); + + popup.style.top = y; + popup.style.left = x; + document.body.appendChild(popup); + + YUI().use('dd-drag', function(Y) { + //Selector of the node to make draggable + var dd = new Y.DD.Drag({ + node: popup + }); + }); + }); +}; + +LogGraph.SessionGraph.session = function (graph, index, id) { + this.index = index; + this.id = id; + this.graph = graph; + + this.events = new Array(); + this.starttick = 0; + this.endtick = undefined; + + this.addEvent = function(e) { + this.events.push(e); + + if (e.op == "createSession") { + // document.write("createSession for " + id.toString(16)); + this.starttick = e.tick; + } else if (e.op == "closeSession") { + this.endtick = e.tick; + } + }, + + this._attach_action = function (sess, label) { + sess.mouseover(function(evt) { + label.show(); + sess.attr({stroke: "gray"}); + }); + + sess.mouseout(function(evt) { + label.hide(); + sess.attr({stroke: "black"}); + }); + }, + + this.drawEvent = function (paper, y, e) { + var x = e.tick * this.graph.pix_per_ticks;; + var s = paper.path("M" + x + " " + (y - 3) + " L" + x + " " + (y + 3)); + s.attr({"stroke-width": 2}); + if (e.op == "error") { + s.attr({"stroke": "red"}); + } + s.mouseover(function(evt) { + s.attr({"stroke-width": 5}); + }); + + s.mouseout(function(evt) { + s.attr({"stroke-width": 2}); + }); + + LogGraph.SessionGraph.sessionEventPopup(s, e, x, y); + }, + + this.draw = function(paper) { + var y = this.index*this.graph.pix_per_session;; + var start = this.starttick * this.graph.pix_per_ticks; + var end = this.endtick * this.graph.pix_per_ticks; + + var sess = paper.set(); + + if (this.endtick == undefined) { + end = this.graph.width(); + } + + sess.push(paper.path("M" + start + " " + y + " L" + end + " " + y)); + for (var i in this.events) { + var e = this.events[i]; + this.drawEvent(paper, y, e); + } + + //sess.attr({"stroke-width": 3}); + label = paper.text(start + 100, y, this.id); + label.attr({"font-size": "14px"}); + label.hide(); + this._attach_action(sess, label); + } +}; + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.stats.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.stats.js new file mode 100644 index 0000000..0a8ac4f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.stats.js @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +LogGraph.StatsGraph = function (asyncq, canvas, starttime, endtime, filter) { + var processdata = function(data) { + var r = Raphael(canvas); + var x = data.map(function (x) { return x.time; }); + var y = data.map(function (x) { return x.count; }); + var xlabels = data.map(function (x) { return dateFormat(x.time, "HH:MM:ss,l"); } ); + var h1 = function () { + this.tags = r.set(); + for (var i = 0, ii = this.y.length; i < ii; i++) { + this.tags.push(r.g.tag(this.x, this.y[i], this.values[i], 160, 10).insertBefore(this).attr([{fill: "#fff"}, {fill: this.symbols[i].attr("fill")}])); + } + }; + var h2 = function () { + this.tags && this.tags.remove(); + }; + r.g.linechart(40, 40, 1000, 500, x, y, {shade: true, axis: "0 0 1 1", symbol: "x", southlabels: xlabels, axisxstep: xlabels.length - 1 , westAxisLabel: "Write requests", southAxisLabel: "Time (min)"}).hoverColumn(h1, h2); + + return true; + //r.g.barchart(0, 0, 1000, 100, y, {shade: true, symbol: "x"}).hoverColumn(h1, h2); + }; + + var uri = "/throughput?scale=minutes"; + LogGraph.loadData(asyncq, uri, processdata); +}; + + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.ui.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.ui.js new file mode 100644 index 0000000..819765a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/loggraph.ui.js @@ -0,0 +1,377 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +// Opens a window to load files into the engine +LogGraph.fileSelector = function(callback) { + var self = this; + this.callback = callback; + this.selectedFiles = new Array(); + + var divTag = document.createElement("div"); + divTag.id = "fileSelector" + Math.round(Math.random()*100000); + // divTag.className = "popUp"; + divTag.className = "selector fileSelector"; + document.body.appendChild(divTag); + + YUI().use('dd-drag', function(Y) { + //Selector of the node to make draggable + var dd = new Y.DD.Drag({ + node: '#' + divTag.id + }); + }); + + var list = document.createElement("ul"); + divTag.appendChild(list); + var selectedList = document.createElement("selectedlist"); + divTag.appendChild(selectedList); + + var clearanchor = document.createElement("span"); + clearanchor.innerHTML = "Remove All"; + clearanchor.className = "actionbutton"; + clearanchor.style.cssFloat = "right"; + clearanchor.onclick = function () { + self.selectedFiles = new Array(); + self.updateSelectedList(); + }; + divTag.appendChild(clearanchor); + + var doneanchor = document.createElement("span"); + doneanchor.innerHTML = "Process Files"; + doneanchor.className = "actionbutton"; + doneanchor.style.cssFloat = "left"; + doneanchor.onclick = function () { + self.callback(self.selectedFiles); + document.body.removeChild(divTag); + delete divTag; + }; + divTag.appendChild(doneanchor); + + var cancelanchor = document.createElement("span"); + cancelanchor.innerHTML = "Cancel"; + cancelanchor.className = "actionbutton"; + cancelanchor.style.cssFloat = "left"; + cancelanchor.onclick = function () { + document.body.removeChild(divTag); + delete divTag; + }; + divTag.appendChild(cancelanchor); + + this.createFileListItem = function (file) { + var li = document.createElement("li"); + var a = document.createElement("a"); + if (file.type == "D") { + a.innerHTML = file.file + "/"; + a.onclick = function () { self.updateList(file.path); }; + } else { + a.innerHTML = file.file; + a.onclick = function () { self.addSelectedFile(file.path); }; + } + + a.fullpath = file.path;; + li.appendChild(a); + return li; + }; + + this.addSelectedFile = function (file) { + if (this.selectedFiles.indexOf(file) == -1) { + this.selectedFiles.push(file); + this.updateSelectedList(); + } + }; + + this.removeSelectedFile = function (file) { + this.selectedFiles = this.selectedFiles.filter(function(f) { return !(file == f); }); + this.updateSelectedList(); + }; + + this.createSelectedListItem = function (file) { + var li = document.createElement("li"); + var a = document.createElement("a"); + li.className = "selectedFile"; + a.onclick = function () { self.removeSelectedFile(file); }; + a.innerHTML = file; + li.appendChild(a); + return li; + }; + + this.updateSelectedList = function () { + while (selectedList.firstChild) { selectedList.removeChild(selectedList.firstChild); } + + for (var i in this.selectedFiles) { + var f = this.selectedFiles[i]; + selectedList.appendChild(this.createSelectedListItem(f)); + } + }; + + this.updateList = function (base) { + while (list.firstChild) list.removeChild(list.firstChild); + + // Create a YUI instance using io-base module. + YUI().use("io-base", function(Y) { + var uri = "/fs?path=" + base; + + // Define a function to handle the response data. + function complete(id, o, args) { + var id = id; // Transaction ID. + var data = eval("(" + o.responseText + ")"); // Response data. + var parts = base.split("/").slice(0,-1); + var parent = "" + if (parts.length < 2) { + parent = "/"; + } else { + parent = parts.join("/"); + } + if (base != "/") { + var li = self.createFileListItem({"file": "..", type: "D", path: parent}); + list.appendChild(li); + } + for (var i in data) { + var f = data[i]; + if (f.file[0] != '.') { + var li = self.createFileListItem(f); + list.appendChild(li); + } + } + }; + + Y.on('io:complete', complete, Y, []); + var request = Y.io(uri); + }); + }; + + this.updateList("/"); +}; + +// Open a window which loads files into the engine +LogGraph.fileLoader = function(files) { + var div = document.createElement("div"); + div.id = "fileLoader"; + + var imgArray = new Array(); + var pArray = new Array(); + for (var index in files) { + var f = files[index]; + var p = document.createElement("p"); + var i = document.createElement("img"); + i.src = "load.gif"; + i.style.visibility = "hidden"; + imgArray.push(i); + pArray.push(p); + var span = document.createElement("span"); + span.innerHTML = f; + + p.appendChild(span); + p.appendChild(i); + + div.appendChild(p); + } + + var loadFile = function (index) { + // Create a YUI instance using io-base module. + YUI().use("io-base", function(Y) { + var file = files[index]; + var uri = "/loadfile?path=" + file; + imgArray[index].style.visibility = "visible"; + + // Define a function to handle the response data. + function complete(id, o, args) { + var id = id; // Transaction ID. + var data = eval("(" + o.responseText + ")"); // Response data. + if (data.status == "ERR") { + var err = document.createElement("div"); + err.innerHTML = data.error; + pArray[index].appendChild(err); + } else if (data.status == "OK") { + var ok = document.createElement("div"); + ok.innerHTML = "OK"; + pArray[index].appendChild(ok); + } + + imgArray[index].style.visibility = "hidden"; + if (index + 1 < files.length) { + loadFile(index + 1); + } else { + //alert("DONE"); + } + }; + + Y.on('io:complete', complete, Y, []); + var request = Y.io(uri); + }); + }; + + var doneanchor = document.createElement("a"); + doneanchor.className = "actionbutton"; + doneanchor.innerHTML = "Done"; + doneanchor.onclick = function () { + document.body.removeChild(div); + delete div; + }; + + document.body.appendChild(div); + if (files.length > 0) { + loadFile(0); + } else { + div.innerHTML ="No files to load"; + } + div.appendChild(doneanchor); +} + +// select a time period +LogGraph.filterSelector = function(starttime, period, filter, callback) { + var self = this; + this.callback = callback; + + // Container other widgets will be in + var container = document.createElement("div"); + container.id = "filterSelector" + Math.round(Math.random()*100000); + container.className = "selector filterSelector"; + document.body.appendChild(container); + + YUI().use('dd-drag', function(Y) { + //Selector of the node to make draggable + var dd = new Y.DD.Drag({ + node: '#' + container.id + }); + }); + + // Temporary loading screen + var loadingp = document.createElement("p"); + loadingp.innerHTML = "Loading..."; + var loadimg = document.createElement("img"); + loadimg.src = "load.gif"; + loadingp.appendChild(loadimg); + container.appendChild(loadingp); + + var addWithLabel = function (container, labeltxt, object) { + var p = document.createElement("p"); + var label = document.createElement("label"); + label.innerHTML = labeltxt + ":"; + p.appendChild(label); + p.appendChild(object); + container.appendChild(p); + }; + var draw = function(minstart, maxstart, entries) { + container.removeChild(loadingp); + var inittime = minstart > starttime ? minstart : starttime; + + var numEntries = 0; + var startspan = document.createElement("span"); + addWithLabel(container, "Start time", startspan); + var startinput = document.createElement("input"); + startinput.type = "hidden"; + startinput.value = inittime; + container.appendChild(startinput); + var sliderspan = document.createElement("span"); + container.appendChild(sliderspan); + + var countspan = document.createElement("p"); + countspan.innerHTML = entries + " entries";; + container.appendChild(countspan); + + var windowinput = document.createElement("input"); + windowinput.type = "text"; + windowinput.value = period; + addWithLabel(container, "Time window (ms)", windowinput); + + var filterinput = document.createElement("textarea"); + filterinput.id = "filterinput"; + filterinput.value = filter; + addWithLabel(container, "Filter", filterinput); + + /* done link, when clicked time is updated, */ + var doneanchor = document.createElement("a"); + doneanchor.className = "actionbutton"; + doneanchor.innerHTML = "Done"; + doneanchor.onclick = function () { + var start = parseInt(startinput.value); + var period = parseInt(windowinput.value); + var filter = filterinput.value; + document.body.removeChild(container); + delete container; + + update(start, period, filter, function() { + callback(start, period, filter, numEntries); + }); + }; + container.appendChild(doneanchor); + + var update = function(start, period, filter, thenrun) { + startspan.innerHTML = dateFormat(start, "HH:MM:ss,l"); + // get the min and max start time + YUI().use("io-base", function(Y) { + var uri = "/info?start=" + start + "&period=" + period + "&filter=" + filter; + function complete(id, o, args) { + var data = eval("(" + o.responseText + ")"); + countspan.innerHTML = data.numEntries + " entries"; + numEntries = data.numEntries; + if (thenrun) { + thenrun(); + } + }; + + Y.on('io:complete', complete, Y, []); + var request = Y.io(uri); + }); + }; + + var updatewindow = function(evt) { + var start = parseInt(startinput.value); + var period = parseInt(windowinput.value); + var filter = filterinput.value; + update(start, period, filter); + }; + windowinput.onkeyup = updatewindow; + + + YUI().use("slider", function (Y) { + var input, slider; + + function updateInput( e ) { + this.set( "value", e.newVal ); + + update(parseInt(startinput.value), parseInt(windowinput.value), filterinput.value); + } + + xSlider = new Y.Slider({min: minstart, max: maxstart, value: inittime, length: "1000px" }); + + // Link the input value to the Slider + xInput = Y.one( startinput ); + xInput.setData( { slider: xSlider } ); + + // Pass the input as the 'this' object inside updateInput + xSlider.after( "valueChange", updateInput, xInput ); + + // Render the Slider next to the input + xSlider.render(sliderspan); + }); + update(inittime, windowinput.value, filterinput); + }; + + // get the min and max start time + YUI().use("io-base", function(Y) { + var uri = "/info"; + function complete(id, o, args) { + var data = eval("(" + o.responseText + ")"); + draw(data.startTime, data.endTime, data.numEntries); + }; + + Y.on('io:complete', complete, Y, []); + var request = Y.io(uri); + }); +} \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/main.html b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/main.html new file mode 100644 index 0000000..b9affe6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/main.html @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+ Edit Filters + Add logs +
+
+ Log view + Servers view + Sessions view + Statistics +
+
+
+
+
+ + diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/raphael.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/raphael.js new file mode 100644 index 0000000..3740d0f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/raphael.js @@ -0,0 +1,3296 @@ +/*! + * Raphael 1.3.2 - JavaScript Vector Library + * + * Copyright (c) 2009 Dmitry Baranovskiy (http://raphaeljs.com) + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license. + */ + +Raphael = (function () { + var separator = /[, ]+/, + elements = /^(circle|rect|path|ellipse|text|image)$/, + proto = "prototype", + has = "hasOwnProperty", + doc = document, + win = window, + oldRaphael = { + was: Object[proto][has].call(win, "Raphael"), + is: win.Raphael + }, + R = function () { + if (R.is(arguments[0], "array")) { + var a = arguments[0], + cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))), + res = cnv.set(); + for (var i = 0, ii = a[length]; i < ii; i++) { + var j = a[i] || {}; + elements.test(j.type) && res[push](cnv[j.type]().attr(j)); + } + return res; + } + return create[apply](R, arguments); + }, + Paper = function () {}, + appendChild = "appendChild", + apply = "apply", + concat = "concat", + E = "", + S = " ", + split = "split", + events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S), + join = "join", + length = "length", + lowerCase = String[proto].toLowerCase, + math = Math, + mmax = math.max, + mmin = math.min, + nu = "number", + toString = "toString", + objectToString = Object[proto][toString], + paper = {}, + pow = math.pow, + push = "push", + rg = /^(?=[\da-f]$)/, + ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i, //" + colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i, + round = math.round, + setAttribute = "setAttribute", + toFloat = parseFloat, + toInt = parseInt, + upperCase = String[proto].toUpperCase, + availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0}, + availableAnimAttrs = {along: "along", blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu}, + rp = "replace"; + R.version = "1.3.2"; + R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML"); + if (R.type == "VML") { + var d = doc.createElement("div"); + d.innerHTML = ''; + if (d.childNodes[length] != 2) { + return R.type = null; + } + d = null; + } + R.svg = !(R.vml = R.type == "VML"); + Paper[proto] = R[proto]; + R._id = 0; + R._oid = 0; + R.fn = {}; + R.is = function (o, type) { + type = lowerCase.call(type); + return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type; + }; + R.setWindow = function (newwin) { + win = newwin; + doc = win.document; + }; + // colour utilities + var toHex = function (color) { + if (R.vml) { + // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/ + var trim = /^\s+|\s+$/g; + toHex = cacher(function (color) { + var bod; + color = (color + E)[rp](trim, E); + try { + var docum = new win.ActiveXObject("htmlfile"); + docum.write(""); + docum.close(); + bod = docum.body; + } catch(e) { + bod = win.createPopup().document.body; + } + var range = bod.createTextRange(); + try { + bod.style.color = color; + var value = range.queryCommandValue("ForeColor"); + value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16); + return "#" + ("000000" + value[toString](16)).slice(-6); + } catch(e) { + return "none"; + } + }); + } else { + var i = doc.createElement("i"); + i.title = "Rapha\xebl Colour Picker"; + i.style.display = "none"; + doc.body[appendChild](i); + toHex = cacher(function (color) { + i.style.color = color; + return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color"); + }); + } + return toHex(color); + }; + var hsbtoString = function () { + return "hsb(" + [this.h, this.s, this.b] + ")"; + }, + rgbtoString = function () { + return this.hex; + }; + R.hsb2rgb = cacher(function (hue, saturation, brightness) { + if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) { + brightness = hue.b; + saturation = hue.s; + hue = hue.h; + } + var red, + green, + blue; + if (brightness == 0) { + return {r: 0, g: 0, b: 0, hex: "#000"}; + } + if (hue > 1 || saturation > 1 || brightness > 1) { + hue /= 255; + saturation /= 255; + brightness /= 255; + } + var i = ~~(hue * 6), + f = (hue * 6) - i, + p = brightness * (1 - saturation), + q = brightness * (1 - (saturation * f)), + t = brightness * (1 - (saturation * (1 - f))); + red = [brightness, q, p, p, t, brightness, brightness][i]; + green = [t, brightness, brightness, q, p, p, t][i]; + blue = [p, p, t, brightness, brightness, q, p][i]; + red *= 255; + green *= 255; + blue *= 255; + var rgb = {r: red, g: green, b: blue, toString: rgbtoString}, + r = (~~red)[toString](16), + g = (~~green)[toString](16), + b = (~~blue)[toString](16); + r = r[rp](rg, "0"); + g = g[rp](rg, "0"); + b = b[rp](rg, "0"); + rgb.hex = "#" + r + g + b; + return rgb; + }, R); + R.rgb2hsb = cacher(function (red, green, blue) { + if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) { + blue = red.b; + green = red.g; + red = red.r; + } + if (R.is(red, "string")) { + var clr = R.getRGB(red); + red = clr.r; + green = clr.g; + blue = clr.b; + } + if (red > 1 || green > 1 || blue > 1) { + red /= 255; + green /= 255; + blue /= 255; + } + var max = mmax(red, green, blue), + min = mmin(red, green, blue), + hue, + saturation, + brightness = max; + if (min == max) { + return {h: 0, s: 0, b: max}; + } else { + var delta = (max - min); + saturation = delta / max; + if (red == max) { + hue = (green - blue) / delta; + } else if (green == max) { + hue = 2 + ((blue - red) / delta); + } else { + hue = 4 + ((red - green) / delta); + } + hue /= 6; + hue < 0 && hue++; + hue > 1 && hue--; + } + return {h: hue, s: saturation, b: brightness, toString: hsbtoString}; + }, R); + var p2s = /,?([achlmqrstvxz]),?/gi; + R._path2string = function () { + return this.join(",")[rp](p2s, "$1"); + }; + function cacher(f, scope, postprocessor) { + function newf() { + var arg = Array[proto].slice.call(arguments, 0), + args = arg[join]("\u25ba"), + cache = newf.cache = newf.cache || {}, + count = newf.count = newf.count || []; + if (cache[has](args)) { + return postprocessor ? postprocessor(cache[args]) : cache[args]; + } + count[length] >= 1e3 && delete cache[count.shift()]; + count[push](args); + cache[args] = f[apply](scope, arg); + return postprocessor ? postprocessor(cache[args]) : cache[args]; + } + return newf; + } + + R.getRGB = cacher(function (colour) { + if (!colour || !!((colour = colour + E).indexOf("-") + 1)) { + return {r: -1, g: -1, b: -1, hex: "none", error: 1}; + } + if (colour == "none") { + return {r: -1, g: -1, b: -1, hex: "none"}; + } + !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour)); + var res, + red, + green, + blue, + t, + rgb = colour.match(colourRegExp); + if (rgb) { + if (rgb[2]) { + blue = toInt(rgb[2].substring(5), 16); + green = toInt(rgb[2].substring(3, 5), 16); + red = toInt(rgb[2].substring(1, 3), 16); + } + if (rgb[3]) { + blue = toInt((t = rgb[3].charAt(3)) + t, 16); + green = toInt((t = rgb[3].charAt(2)) + t, 16); + red = toInt((t = rgb[3].charAt(1)) + t, 16); + } + if (rgb[4]) { + rgb = rgb[4][split](/\s*,\s*/); + red = toFloat(rgb[0]); + green = toFloat(rgb[1]); + blue = toFloat(rgb[2]); + } + if (rgb[5]) { + rgb = rgb[5][split](/\s*,\s*/); + red = toFloat(rgb[0]) * 2.55; + green = toFloat(rgb[1]) * 2.55; + blue = toFloat(rgb[2]) * 2.55; + } + if (rgb[6]) { + rgb = rgb[6][split](/\s*,\s*/); + red = toFloat(rgb[0]); + green = toFloat(rgb[1]); + blue = toFloat(rgb[2]); + return R.hsb2rgb(red, green, blue); + } + if (rgb[7]) { + rgb = rgb[7][split](/\s*,\s*/); + red = toFloat(rgb[0]) * 2.55; + green = toFloat(rgb[1]) * 2.55; + blue = toFloat(rgb[2]) * 2.55; + return R.hsb2rgb(red, green, blue); + } + rgb = {r: red, g: green, b: blue}; + var r = (~~red)[toString](16), + g = (~~green)[toString](16), + b = (~~blue)[toString](16); + r = r[rp](rg, "0"); + g = g[rp](rg, "0"); + b = b[rp](rg, "0"); + rgb.hex = "#" + r + g + b; + return rgb; + } + return {r: -1, g: -1, b: -1, hex: "none", error: 1}; + }, R); + R.getColor = function (value) { + var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75}, + rgb = this.hsb2rgb(start.h, start.s, start.b); + start.h += .075; + if (start.h > 1) { + start.h = 0; + start.s -= .2; + start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b}); + } + return rgb.hex; + }; + R.getColor.reset = function () { + delete this.start; + }; + // path utilities + var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, + pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig; + R.parsePathString = cacher(function (pathString) { + if (!pathString) { + return null; + } + var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0}, + data = []; + if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption + data = pathClone(pathString); + } + if (!data[length]) { + (pathString + E)[rp](pathCommand, function (a, b, c) { + var params = [], + name = lowerCase.call(b); + c[rp](pathValues, function (a, b) { + b && params[push](+b); + }); + if (name == "m" && params[length] > 2) { + data[push]([b][concat](params.splice(0, 2))); + name = "l"; + b = b == "m" ? "l" : "L"; + } + while (params[length] >= paramCounts[name]) { + data[push]([b][concat](params.splice(0, paramCounts[name]))); + if (!paramCounts[name]) { + break; + } + } + }); + } + data[toString] = R._path2string; + return data; + }); + R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) { + var t1 = 1 - t, + x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x, + y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y, + mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x), + my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y), + nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x), + ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y), + ax = (1 - t) * p1x + t * c1x, + ay = (1 - t) * p1y + t * c1y, + cx = (1 - t) * c2x + t * p2x, + cy = (1 - t) * c2y + t * p2y, + alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI); + (mx > nx || my < ny) && (alpha += 180); + return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha}; + }; + var pathDimensions = cacher(function (path) { + if (!path) { + return {x: 0, y: 0, width: 0, height: 0}; + } + path = path2curve(path); + var x = 0, + y = 0, + X = [], + Y = [], + p; + for (var i = 0, ii = path[length]; i < ii; i++) { + p = path[i]; + if (p[0] == "M") { + x = p[1]; + y = p[2]; + X[push](x); + Y[push](y); + } else { + var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]); + X = X[concat](dim.min.x, dim.max.x); + Y = Y[concat](dim.min.y, dim.max.y); + x = p[5]; + y = p[6]; + } + } + var xmin = mmin[apply](0, X), + ymin = mmin[apply](0, Y); + return { + x: xmin, + y: ymin, + width: mmax[apply](0, X) - xmin, + height: mmax[apply](0, Y) - ymin + }; + }), + pathClone = function (pathArray) { + var res = []; + if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption + pathArray = R.parsePathString(pathArray); + } + for (var i = 0, ii = pathArray[length]; i < ii; i++) { + res[i] = []; + for (var j = 0, jj = pathArray[i][length]; j < jj; j++) { + res[i][j] = pathArray[i][j]; + } + } + res[toString] = R._path2string; + return res; + }, + pathToRelative = cacher(function (pathArray) { + if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption + pathArray = R.parsePathString(pathArray); + } + var res = [], + x = 0, + y = 0, + mx = 0, + my = 0, + start = 0; + if (pathArray[0][0] == "M") { + x = pathArray[0][1]; + y = pathArray[0][2]; + mx = x; + my = y; + start++; + res[push](["M", x, y]); + } + for (var i = start, ii = pathArray[length]; i < ii; i++) { + var r = res[i] = [], + pa = pathArray[i]; + if (pa[0] != lowerCase.call(pa[0])) { + r[0] = lowerCase.call(pa[0]); + switch (r[0]) { + case "a": + r[1] = pa[1]; + r[2] = pa[2]; + r[3] = pa[3]; + r[4] = pa[4]; + r[5] = pa[5]; + r[6] = +(pa[6] - x).toFixed(3); + r[7] = +(pa[7] - y).toFixed(3); + break; + case "v": + r[1] = +(pa[1] - y).toFixed(3); + break; + case "m": + mx = pa[1]; + my = pa[2]; + default: + for (var j = 1, jj = pa[length]; j < jj; j++) { + r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3); + } + } + } else { + r = res[i] = []; + if (pa[0] == "m") { + mx = pa[1] + x; + my = pa[2] + y; + } + for (var k = 0, kk = pa[length]; k < kk; k++) { + res[i][k] = pa[k]; + } + } + var len = res[i][length]; + switch (res[i][0]) { + case "z": + x = mx; + y = my; + break; + case "h": + x += +res[i][len - 1]; + break; + case "v": + y += +res[i][len - 1]; + break; + default: + x += +res[i][len - 2]; + y += +res[i][len - 1]; + } + } + res[toString] = R._path2string; + return res; + }, 0, pathClone), + pathToAbsolute = cacher(function (pathArray) { + if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption + pathArray = R.parsePathString(pathArray); + } + var res = [], + x = 0, + y = 0, + mx = 0, + my = 0, + start = 0; + if (pathArray[0][0] == "M") { + x = +pathArray[0][1]; + y = +pathArray[0][2]; + mx = x; + my = y; + start++; + res[0] = ["M", x, y]; + } + for (var i = start, ii = pathArray[length]; i < ii; i++) { + var r = res[i] = [], + pa = pathArray[i]; + if (pa[0] != upperCase.call(pa[0])) { + r[0] = upperCase.call(pa[0]); + switch (r[0]) { + case "A": + r[1] = pa[1]; + r[2] = pa[2]; + r[3] = pa[3]; + r[4] = pa[4]; + r[5] = pa[5]; + r[6] = +(pa[6] + x); + r[7] = +(pa[7] + y); + break; + case "V": + r[1] = +pa[1] + y; + break; + case "H": + r[1] = +pa[1] + x; + break; + case "M": + mx = +pa[1] + x; + my = +pa[2] + y; + default: + for (var j = 1, jj = pa[length]; j < jj; j++) { + r[j] = +pa[j] + ((j % 2) ? x : y); + } + } + } else { + for (var k = 0, kk = pa[length]; k < kk; k++) { + res[i][k] = pa[k]; + } + } + switch (r[0]) { + case "Z": + x = mx; + y = my; + break; + case "H": + x = r[1]; + break; + case "V": + y = r[1]; + break; + default: + x = res[i][res[i][length] - 2]; + y = res[i][res[i][length] - 1]; + } + } + res[toString] = R._path2string; + return res; + }, null, pathClone), + l2c = function (x1, y1, x2, y2) { + return [x1, y1, x2, y2, x2, y2]; + }, + q2c = function (x1, y1, ax, ay, x2, y2) { + var _13 = 1 / 3, + _23 = 2 / 3; + return [ + _13 * x1 + _23 * ax, + _13 * y1 + _23 * ay, + _13 * x2 + _23 * ax, + _13 * y2 + _23 * ay, + x2, + y2 + ]; + }, + a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) { + // for more information of where this math came from visit: + // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes + var PI = math.PI, + _120 = PI * 120 / 180, + rad = PI / 180 * (+angle || 0), + res = [], + xy, + rotate = cacher(function (x, y, rad) { + var X = x * math.cos(rad) - y * math.sin(rad), + Y = x * math.sin(rad) + y * math.cos(rad); + return {x: X, y: Y}; + }); + if (!recursive) { + xy = rotate(x1, y1, -rad); + x1 = xy.x; + y1 = xy.y; + xy = rotate(x2, y2, -rad); + x2 = xy.x; + y2 = xy.y; + var cos = math.cos(PI / 180 * angle), + sin = math.sin(PI / 180 * angle), + x = (x1 - x2) / 2, + y = (y1 - y2) / 2; + // rx = mmax(rx, math.abs(x)); + // ry = mmax(ry, math.abs(y)); + var h = (x * x) / (rx * rx) + (y * y) / (ry * ry); + if (h > 1) { + h = math.sqrt(h); + rx = h * rx; + ry = h * ry; + } + var rx2 = rx * rx, + ry2 = ry * ry, + k = (large_arc_flag == sweep_flag ? -1 : 1) * + math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))), + cx = k * rx * y / ry + (x1 + x2) / 2, + cy = k * -ry * x / rx + (y1 + y2) / 2, + f1 = math.asin(((y1 - cy) / ry).toFixed(7)), + f2 = math.asin(((y2 - cy) / ry).toFixed(7)); + + f1 = x1 < cx ? PI - f1 : f1; + f2 = x2 < cx ? PI - f2 : f2; + f1 < 0 && (f1 = PI * 2 + f1); + f2 < 0 && (f2 = PI * 2 + f2); + if (sweep_flag && f1 > f2) { + f1 = f1 - PI * 2; + } + if (!sweep_flag && f2 > f1) { + f2 = f2 - PI * 2; + } + } else { + f1 = recursive[0]; + f2 = recursive[1]; + cx = recursive[2]; + cy = recursive[3]; + } + var df = f2 - f1; + if (math.abs(df) > _120) { + var f2old = f2, + x2old = x2, + y2old = y2; + f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1); + x2 = cx + rx * math.cos(f2); + y2 = cy + ry * math.sin(f2); + res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]); + } + df = f2 - f1; + var c1 = math.cos(f1), + s1 = math.sin(f1), + c2 = math.cos(f2), + s2 = math.sin(f2), + t = math.tan(df / 4), + hx = 4 / 3 * rx * t, + hy = 4 / 3 * ry * t, + m1 = [x1, y1], + m2 = [x1 + hx * s1, y1 - hy * c1], + m3 = [x2 + hx * s2, y2 - hy * c2], + m4 = [x2, y2]; + m2[0] = 2 * m1[0] - m2[0]; + m2[1] = 2 * m1[1] - m2[1]; + if (recursive) { + return [m2, m3, m4][concat](res); + } else { + res = [m2, m3, m4][concat](res)[join]()[split](","); + var newres = []; + for (var i = 0, ii = res[length]; i < ii; i++) { + newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x; + } + // alert(newres); + return newres; + } + }, + findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) { + var t1 = 1 - t; + return { + x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x, + y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y + }; + }, + curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) { + var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x), + b = 2 * (c1x - p1x) - 2 * (c2x - c1x), + c = p1x - c1x, + t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a, + t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a, + y = [p1y, p2y], + x = [p1x, p2x], + dot; + math.abs(t1) > 1e12 && (t1 = .5); + math.abs(t2) > 1e12 && (t2 = .5); + if (t1 > 0 && t1 < 1) { + dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1); + x[push](dot.x); + y[push](dot.y); + } + if (t2 > 0 && t2 < 1) { + dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2); + x[push](dot.x); + y[push](dot.y); + } + a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y); + b = 2 * (c1y - p1y) - 2 * (c2y - c1y); + c = p1y - c1y; + t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a; + t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a; + math.abs(t1) > 1e12 && (t1 = .5); + math.abs(t2) > 1e12 && (t2 = .5); + if (t1 > 0 && t1 < 1) { + dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1); + x[push](dot.x); + y[push](dot.y); + } + if (t2 > 0 && t2 < 1) { + dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2); + x[push](dot.x); + y[push](dot.y); + } + return { + min: {x: mmin[apply](0, x), y: mmin[apply](0, y)}, + max: {x: mmax[apply](0, x), y: mmax[apply](0, y)} + }; + }), + path2curve = cacher(function (path, path2) { + var p = pathToAbsolute(path), + p2 = path2 && pathToAbsolute(path2), + attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null}, + attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null}, + processPath = function (path, d) { + var nx, ny; + if (!path) { + return ["C", d.x, d.y, d.x, d.y, d.x, d.y]; + } + !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null); + switch (path[0]) { + case "M": + d.X = path[1]; + d.Y = path[2]; + break; + case "A": + path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1)))); + break; + case "S": + nx = d.x + (d.x - (d.bx || d.x)); + ny = d.y + (d.y - (d.by || d.y)); + path = ["C", nx, ny][concat](path.slice(1)); + break; + case "T": + d.qx = d.x + (d.x - (d.qx || d.x)); + d.qy = d.y + (d.y - (d.qy || d.y)); + path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2])); + break; + case "Q": + d.qx = path[1]; + d.qy = path[2]; + path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4])); + break; + case "L": + path = ["C"][concat](l2c(d.x, d.y, path[1], path[2])); + break; + case "H": + path = ["C"][concat](l2c(d.x, d.y, path[1], d.y)); + break; + case "V": + path = ["C"][concat](l2c(d.x, d.y, d.x, path[1])); + break; + case "Z": + path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y)); + break; + } + return path; + }, + fixArc = function (pp, i) { + if (pp[i][length] > 7) { + pp[i].shift(); + var pi = pp[i]; + while (pi[length]) { + pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6))); + } + pp.splice(i, 1); + ii = mmax(p[length], p2 && p2[length] || 0); + } + }, + fixM = function (path1, path2, a1, a2, i) { + if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") { + path2.splice(i, 0, ["M", a2.x, a2.y]); + a1.bx = 0; + a1.by = 0; + a1.x = path1[i][1]; + a1.y = path1[i][2]; + ii = mmax(p[length], p2 && p2[length] || 0); + } + }; + for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) { + p[i] = processPath(p[i], attrs); + fixArc(p, i); + p2 && (p2[i] = processPath(p2[i], attrs2)); + p2 && fixArc(p2, i); + fixM(p, p2, attrs, attrs2, i); + fixM(p2, p, attrs2, attrs, i); + var seg = p[i], + seg2 = p2 && p2[i], + seglen = seg[length], + seg2len = p2 && seg2[length]; + attrs.x = seg[seglen - 2]; + attrs.y = seg[seglen - 1]; + attrs.bx = toFloat(seg[seglen - 4]) || attrs.x; + attrs.by = toFloat(seg[seglen - 3]) || attrs.y; + attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x); + attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y); + attrs2.x = p2 && seg2[seg2len - 2]; + attrs2.y = p2 && seg2[seg2len - 1]; + } + return p2 ? [p, p2] : p; + }, null, pathClone), + parseDots = cacher(function (gradient) { + var dots = []; + for (var i = 0, ii = gradient[length]; i < ii; i++) { + var dot = {}, + par = gradient[i].match(/^([^:]*):?([\d\.]*)/); + dot.color = R.getRGB(par[1]); + if (dot.color.error) { + return null; + } + dot.color = dot.color.hex; + par[2] && (dot.offset = par[2] + "%"); + dots[push](dot); + } + for (i = 1, ii = dots[length] - 1; i < ii; i++) { + if (!dots[i].offset) { + var start = toFloat(dots[i - 1].offset || 0), + end = 0; + for (var j = i + 1; j < ii; j++) { + if (dots[j].offset) { + end = dots[j].offset; + break; + } + } + if (!end) { + end = 100; + j = ii; + } + end = toFloat(end); + var d = (end - start) / (j - i + 1); + for (; i < j; i++) { + start += d; + dots[i].offset = start + "%"; + } + } + } + return dots; + }), + getContainer = function (x, y, w, h) { + var container; + if (R.is(x, "string") || R.is(x, "object")) { + container = R.is(x, "string") ? doc.getElementById(x) : x; + if (container.tagName) { + if (y == null) { + return { + container: container, + width: container.style.pixelWidth || container.offsetWidth, + height: container.style.pixelHeight || container.offsetHeight + }; + } else { + return {container: container, width: y, height: w}; + } + } + } else if (R.is(x, nu) && h != null) { + return {container: 1, x: x, y: y, width: w, height: h}; + } + }, + plugins = function (con, add) { + var that = this; + for (var prop in add) { + if (add[has](prop) && !(prop in con)) { + switch (typeof add[prop]) { + case "function": + (function (f) { + con[prop] = con === that ? f : function () { return f[apply](that, arguments); }; + })(add[prop]); + break; + case "object": + con[prop] = con[prop] || {}; + plugins.call(this, con[prop], add[prop]); + break; + default: + con[prop] = add[prop]; + break; + } + } + } + }, + tear = function (el, paper) { + el == paper.top && (paper.top = el.prev); + el == paper.bottom && (paper.bottom = el.next); + el.next && (el.next.prev = el.prev); + el.prev && (el.prev.next = el.next); + }, + tofront = function (el, paper) { + if (paper.top === el) { + return; + } + tear(el, paper); + el.next = null; + el.prev = paper.top; + paper.top.next = el; + paper.top = el; + }, + toback = function (el, paper) { + if (paper.bottom === el) { + return; + } + tear(el, paper); + el.next = paper.bottom; + el.prev = null; + paper.bottom.prev = el; + paper.bottom = el; + }, + insertafter = function (el, el2, paper) { + tear(el, paper); + el2 == paper.top && (paper.top = el); + el2.next && (el2.next.prev = el); + el.next = el2.next; + el.prev = el2; + el2.next = el; + }, + insertbefore = function (el, el2, paper) { + tear(el, paper); + el2 == paper.bottom && (paper.bottom = el); + el2.prev && (el2.prev.next = el); + el.prev = el2.prev; + el2.prev = el; + el.next = el2; + }, + removed = function (methodname) { + return function () { + throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object"); + }; + }, + radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/; + + // SVG + if (R.svg) { + Paper[proto].svgns = "http://www.w3.org/2000/svg"; + Paper[proto].xlink = "http://www.w3.org/1999/xlink"; + round = function (num) { + return +num + (~~num === num) * .5; + }; + var roundPath = function (path) { + for (var i = 0, ii = path[length]; i < ii; i++) { + if (lowerCase.call(path[i][0]) != "a") { + for (var j = 1, jj = path[i][length]; j < jj; j++) { + path[i][j] = round(path[i][j]); + } + } else { + path[i][6] = round(path[i][6]); + path[i][7] = round(path[i][7]); + } + } + return path; + }, + $ = function (el, attr) { + if (attr) { + for (var key in attr) { + if (attr[has](key)) { + el[setAttribute](key, attr[key] + E); + } + } + } else { + return doc.createElementNS(Paper[proto].svgns, el); + } + }; + R[toString] = function () { + return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version; + }; + var thePath = function (pathString, SVG) { + var el = $("path"); + SVG.canvas && SVG.canvas[appendChild](el); + var p = new Element(el, SVG); + p.type = "path"; + setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString}); + return p; + }; + var addGradientFill = function (o, gradient, SVG) { + var type = "linear", + fx = .5, fy = .5, + s = o.style; + gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) { + type = "radial"; + if (_fx && _fy) { + fx = toFloat(_fx); + fy = toFloat(_fy); + var dir = ((fy > .5) * 2 - 1); + pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && + (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) && + fy != .5 && + (fy = fy.toFixed(5) - 1e-5 * dir); + } + return E; + }); + gradient = gradient[split](/\s*\-\s*/); + if (type == "linear") { + var angle = gradient.shift(); + angle = -toFloat(angle); + if (isNaN(angle)) { + return null; + } + var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)], + max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1); + vector[2] *= max; + vector[3] *= max; + if (vector[2] < 0) { + vector[0] = -vector[2]; + vector[2] = 0; + } + if (vector[3] < 0) { + vector[1] = -vector[3]; + vector[3] = 0; + } + } + var dots = parseDots(gradient); + if (!dots) { + return null; + } + var id = o.getAttribute("fill"); + id = id.match(/^url\(#(.*)\)$/); + id && SVG.defs.removeChild(doc.getElementById(id[1])); + + var el = $(type + "Gradient"); + el.id = "r" + (R._id++)[toString](36); + $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]}); + SVG.defs[appendChild](el); + for (var i = 0, ii = dots[length]; i < ii; i++) { + var stop = $("stop"); + $(stop, { + offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%", + "stop-color": dots[i].color || "#fff" + }); + el[appendChild](stop); + } + $(o, { + fill: "url(#" + el.id + ")", + opacity: 1, + "fill-opacity": 1 + }); + s.fill = E; + s.opacity = 1; + s.fillOpacity = 1; + return 1; + }; + var updatePosition = function (o) { + var bbox = o.getBBox(); + $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)}); + }; + var setFillAndStroke = function (o, params) { + var dasharray = { + "": [0], + "none": [0], + "-": [3, 1], + ".": [1, 1], + "-.": [3, 1, 1, 1], + "-..": [3, 1, 1, 1, 1, 1], + ". ": [1, 3], + "- ": [4, 3], + "--": [8, 3], + "- .": [4, 3, 1, 3], + "--.": [8, 3, 1, 3], + "--..": [8, 3, 1, 3, 1, 3] + }, + node = o.node, + attrs = o.attrs, + rot = o.rotate(), + addDashes = function (o, value) { + value = dasharray[lowerCase.call(value)]; + if (value) { + var width = o.attrs["stroke-width"] || "1", + butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0, + dashes = []; + var i = value[length]; + while (i--) { + dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt; + } + $(node, {"stroke-dasharray": dashes[join](",")}); + } + }; + params[has]("rotation") && (rot = params.rotation); + var rotxy = (rot + E)[split](separator); + if (!(rotxy.length - 1)) { + rotxy = null; + } else { + rotxy[1] = +rotxy[1]; + rotxy[2] = +rotxy[2]; + } + toFloat(rot) && o.rotate(0, true); + for (var att in params) { + if (params[has](att)) { + if (!availableAttrs[has](att)) { + continue; + } + var value = params[att]; + attrs[att] = value; + switch (att) { + case "blur": + o.blur(value); + break; + case "rotation": + o.rotate(value, true); + break; + // Hyperlink + case "href": + case "title": + case "target": + var pn = node.parentNode; + if (lowerCase.call(pn.tagName) != "a") { + var hl = $("a"); + pn.insertBefore(hl, node); + hl[appendChild](node); + pn = hl; + } + pn.setAttributeNS(o.paper.xlink, att, value); + break; + case "cursor": + node.style.cursor = value; + break; + case "clip-rect": + var rect = (value + E)[split](separator); + if (rect[length] == 4) { + o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode); + var el = $("clipPath"), + rc = $("rect"); + el.id = "r" + (R._id++)[toString](36); + $(rc, { + x: rect[0], + y: rect[1], + width: rect[2], + height: rect[3] + }); + el[appendChild](rc); + o.paper.defs[appendChild](el); + $(node, {"clip-path": "url(#" + el.id + ")"}); + o.clip = rc; + } + if (!value) { + var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E)); + clip && clip.parentNode.removeChild(clip); + $(node, {"clip-path": E}); + delete o.clip; + } + break; + case "path": + if (o.type == "path") { + $(node, {d: value ? attrs.path = roundPath(pathToAbsolute(value)) : "M0,0"}); + } + break; + case "width": + node[setAttribute](att, value); + if (attrs.fx) { + att = "x"; + value = attrs.x; + } else { + break; + } + case "x": + if (attrs.fx) { + value = -attrs.x - (attrs.width || 0); + } + case "rx": + if (att == "rx" && o.type == "rect") { + break; + } + case "cx": + rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]); + node[setAttribute](att, round(value)); + o.pattern && updatePosition(o); + break; + case "height": + node[setAttribute](att, value); + if (attrs.fy) { + att = "y"; + value = attrs.y; + } else { + break; + } + case "y": + if (attrs.fy) { + value = -attrs.y - (attrs.height || 0); + } + case "ry": + if (att == "ry" && o.type == "rect") { + break; + } + case "cy": + rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]); + node[setAttribute](att, round(value)); + o.pattern && updatePosition(o); + break; + case "r": + if (o.type == "rect") { + $(node, {rx: value, ry: value}); + } else { + node[setAttribute](att, value); + } + break; + case "src": + if (o.type == "image") { + node.setAttributeNS(o.paper.xlink, "href", value); + } + break; + case "stroke-width": + node.style.strokeWidth = value; + // Need following line for Firefox + node[setAttribute](att, value); + if (attrs["stroke-dasharray"]) { + addDashes(o, attrs["stroke-dasharray"]); + } + break; + case "stroke-dasharray": + addDashes(o, value); + break; + case "translation": + var xy = (value + E)[split](separator); + xy[0] = +xy[0] || 0; + xy[1] = +xy[1] || 0; + if (rotxy) { + rotxy[1] += xy[0]; + rotxy[2] += xy[1]; + } + translate.call(o, xy[0], xy[1]); + break; + case "scale": + xy = (value + E)[split](separator); + o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]); + break; + case "fill": + var isURL = (value + E).match(ISURL); + if (isURL) { + el = $("pattern"); + var ig = $("image"); + el.id = "r" + (R._id++)[toString](36); + $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1}); + $(ig, {x: 0, y: 0}); + ig.setAttributeNS(o.paper.xlink, "href", isURL[1]); + el[appendChild](ig); + + var img = doc.createElement("img"); + img.style.cssText = "position:absolute;left:-9999em;top-9999em"; + img.onload = function () { + $(el, {width: this.offsetWidth, height: this.offsetHeight}); + $(ig, {width: this.offsetWidth, height: this.offsetHeight}); + doc.body.removeChild(this); + o.paper.safari(); + }; + doc.body[appendChild](img); + img.src = isURL[1]; + o.paper.defs[appendChild](el); + node.style.fill = "url(#" + el.id + ")"; + $(node, {fill: "url(#" + el.id + ")"}); + o.pattern = el; + o.pattern && updatePosition(o); + break; + } + if (!R.getRGB(value).error) { + delete params.gradient; + delete attrs.gradient; + !R.is(attrs.opacity, "undefined") && + R.is(params.opacity, "undefined") && + $(node, {opacity: attrs.opacity}); + !R.is(attrs["fill-opacity"], "undefined") && + R.is(params["fill-opacity"], "undefined") && + $(node, {"fill-opacity": attrs["fill-opacity"]}); + } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) { + attrs.gradient = value; + attrs.fill = "none"; + break; + } + case "stroke": + node[setAttribute](att, R.getRGB(value).hex); + break; + case "gradient": + (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper); + break; + case "opacity": + case "fill-opacity": + if (attrs.gradient) { + var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E)); + if (gradient) { + var stops = gradient.getElementsByTagName("stop"); + stops[stops[length] - 1][setAttribute]("stop-opacity", value); + } + break; + } + default: + att == "font-size" && (value = toInt(value, 10) + "px"); + var cssrule = att[rp](/(\-.)/g, function (w) { + return upperCase.call(w.substring(1)); + }); + node.style[cssrule] = value; + // Need following line for Firefox + node[setAttribute](att, value); + break; + } + } + } + + tuneText(o, params); + if (rotxy) { + o.rotate(rotxy.join(S)); + } else { + toFloat(rot) && o.rotate(rot, true); + } + }; + var leading = 1.2, + tuneText = function (el, params) { + if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) { + return; + } + var a = el.attrs, + node = el.node, + fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10; + + if (params[has]("text")) { + a.text = params.text; + while (node.firstChild) { + node.removeChild(node.firstChild); + } + var texts = (params.text + E)[split]("\n"); + for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) { + var tspan = $("tspan"); + i && $(tspan, {dy: fontSize * leading, x: a.x}); + tspan[appendChild](doc.createTextNode(texts[i])); + node[appendChild](tspan); + } + } else { + texts = node.getElementsByTagName("tspan"); + for (i = 0, ii = texts[length]; i < ii; i++) { + i && $(texts[i], {dy: fontSize * leading, x: a.x}); + } + } + $(node, {y: a.y}); + var bb = el.getBBox(), + dif = a.y - (bb.y + bb.height / 2); + dif && isFinite(dif) && $(node, {y: a.y + dif}); + }, + Element = function (node, svg) { + var X = 0, + Y = 0; + this[0] = node; + this.id = R._oid++; + this.node = node; + node.raphael = this; + this.paper = svg; + this.attrs = this.attrs || {}; + this.transformations = []; // rotate, translate, scale + this._ = { + tx: 0, + ty: 0, + rt: {deg: 0, cx: 0, cy: 0}, + sx: 1, + sy: 1 + }; + !svg.bottom && (svg.bottom = this); + this.prev = svg.top; + svg.top && (svg.top.next = this); + svg.top = this; + this.next = null; + }; + Element[proto].rotate = function (deg, cx, cy) { + if (this.removed) { + return this; + } + if (deg == null) { + if (this._.rt.cx) { + return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S); + } + return this._.rt.deg; + } + var bbox = this.getBBox(); + deg = (deg + E)[split](separator); + if (deg[length] - 1) { + cx = toFloat(deg[1]); + cy = toFloat(deg[2]); + } + deg = toFloat(deg[0]); + if (cx != null) { + this._.rt.deg = deg; + } else { + this._.rt.deg += deg; + } + (cy == null) && (cx = null); + this._.rt.cx = cx; + this._.rt.cy = cy; + cx = cx == null ? bbox.x + bbox.width / 2 : cx; + cy = cy == null ? bbox.y + bbox.height / 2 : cy; + if (this._.rt.deg) { + this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy); + this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)}); + } else { + this.transformations[0] = E; + this.clip && $(this.clip, {transform: E}); + } + $(this.node, {transform: this.transformations[join](S)}); + return this; + }; + Element[proto].hide = function () { + !this.removed && (this.node.style.display = "none"); + return this; + }; + Element[proto].show = function () { + !this.removed && (this.node.style.display = ""); + return this; + }; + Element[proto].remove = function () { + if (this.removed) { + return; + } + tear(this, this.paper); + this.node.parentNode.removeChild(this.node); + for (var i in this) { + delete this[i]; + } + this.removed = true; + }; + Element[proto].getBBox = function () { + if (this.removed) { + return this; + } + if (this.type == "path") { + return pathDimensions(this.attrs.path); + } + if (this.node.style.display == "none") { + this.show(); + var hide = true; + } + var bbox = {}; + try { + bbox = this.node.getBBox(); + } catch(e) { + // Firefox 3.0.x plays badly here + } finally { + bbox = bbox || {}; + } + if (this.type == "text") { + bbox = {x: bbox.x, y: Infinity, width: 0, height: 0}; + for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) { + var bb = this.node.getExtentOfChar(i); + (bb.y < bbox.y) && (bbox.y = bb.y); + (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y); + (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x); + } + } + hide && this.hide(); + return bbox; + }; + Element[proto].attr = function (name, value) { + if (this.removed) { + return this; + } + if (name == null) { + var res = {}; + for (var i in this.attrs) if (this.attrs[has](i)) { + res[i] = this.attrs[i]; + } + this._.rt.deg && (res.rotation = this.rotate()); + (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale()); + res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient; + return res; + } + if (value == null && R.is(name, "string")) { + if (name == "translation") { + return translate.call(this); + } + if (name == "rotation") { + return this.rotate(); + } + if (name == "scale") { + return this.scale(); + } + if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) { + return this.attrs.gradient; + } + return this.attrs[name]; + } + if (value == null && R.is(name, "array")) { + var values = {}; + for (var j = 0, jj = name.length; j < jj; j++) { + values[name[j]] = this.attr(name[j]); + } + return values; + } + if (value != null) { + var params = {}; + params[name] = value; + setFillAndStroke(this, params); + } else if (name != null && R.is(name, "object")) { + setFillAndStroke(this, name); + } + return this; + }; + Element[proto].toFront = function () { + if (this.removed) { + return this; + } + this.node.parentNode[appendChild](this.node); + var svg = this.paper; + svg.top != this && tofront(this, svg); + return this; + }; + Element[proto].toBack = function () { + if (this.removed) { + return this; + } + if (this.node.parentNode.firstChild != this.node) { + this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild); + toback(this, this.paper); + var svg = this.paper; + } + return this; + }; + Element[proto].insertAfter = function (element) { + if (this.removed) { + return this; + } + var node = element.node; + if (node.nextSibling) { + node.parentNode.insertBefore(this.node, node.nextSibling); + } else { + node.parentNode[appendChild](this.node); + } + insertafter(this, element, this.paper); + return this; + }; + Element[proto].insertBefore = function (element) { + if (this.removed) { + return this; + } + var node = element.node; + node.parentNode.insertBefore(this.node, node); + insertbefore(this, element, this.paper); + return this; + }; + Element[proto].blur = function (size) { + // Experimental. No Safari support. Use it on your own risk. + var t = this; + if (+size !== 0) { + var fltr = $("filter"), + blur = $("feGaussianBlur"); + t.attrs.blur = size; + fltr.id = "r" + (R._id++)[toString](36); + $(blur, {stdDeviation: +size || 1.5}); + fltr.appendChild(blur); + t.paper.defs.appendChild(fltr); + t._blur = fltr; + $(t.node, {filter: "url(#" + fltr.id + ")"}); + } else { + if (t._blur) { + t._blur.parentNode.removeChild(t._blur); + delete t._blur; + delete t.attrs.blur; + } + t.node.removeAttribute("filter"); + } + }; + var theCircle = function (svg, x, y, r) { + x = round(x); + y = round(y); + var el = $("circle"); + svg.canvas && svg.canvas[appendChild](el); + var res = new Element(el, svg); + res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"}; + res.type = "circle"; + $(el, res.attrs); + return res; + }; + var theRect = function (svg, x, y, w, h, r) { + x = round(x); + y = round(y); + var el = $("rect"); + svg.canvas && svg.canvas[appendChild](el); + var res = new Element(el, svg); + res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"}; + res.type = "rect"; + $(el, res.attrs); + return res; + }; + var theEllipse = function (svg, x, y, rx, ry) { + x = round(x); + y = round(y); + var el = $("ellipse"); + svg.canvas && svg.canvas[appendChild](el); + var res = new Element(el, svg); + res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"}; + res.type = "ellipse"; + $(el, res.attrs); + return res; + }; + var theImage = function (svg, src, x, y, w, h) { + var el = $("image"); + $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"}); + el.setAttributeNS(svg.xlink, "href", src); + svg.canvas && svg.canvas[appendChild](el); + var res = new Element(el, svg); + res.attrs = {x: x, y: y, width: w, height: h, src: src}; + res.type = "image"; + return res; + }; + var theText = function (svg, x, y, text) { + var el = $("text"); + $(el, {x: x, y: y, "text-anchor": "middle"}); + svg.canvas && svg.canvas[appendChild](el); + var res = new Element(el, svg); + res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"}; + res.type = "text"; + setFillAndStroke(res, res.attrs); + return res; + }; + var setSize = function (width, height) { + this.width = width || this.width; + this.height = height || this.height; + this.canvas[setAttribute]("width", this.width); + this.canvas[setAttribute]("height", this.height); + return this; + }; + var create = function () { + var con = getContainer[apply](0, arguments), + container = con && con.container, + x = con.x, + y = con.y, + width = con.width, + height = con.height; + if (!container) { + throw new Error("SVG container not found."); + } + var cnvs = $("svg"); + width = width || 512; + height = height || 342; + $(cnvs, { + xmlns: "http://www.w3.org/2000/svg", + version: 1.1, + width: width, + height: height + }); + if (container == 1) { + cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px"; + doc.body[appendChild](cnvs); + } else { + if (container.firstChild) { + container.insertBefore(cnvs, container.firstChild); + } else { + container[appendChild](cnvs); + } + } + container = new Paper; + container.width = width; + container.height = height; + container.canvas = cnvs; + plugins.call(container, container, R.fn); + container.clear(); + return container; + }; + Paper[proto].clear = function () { + var c = this.canvas; + while (c.firstChild) { + c.removeChild(c.firstChild); + } + this.bottom = this.top = null; + (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl")); + c[appendChild](this.desc); + c[appendChild](this.defs = $("defs")); + }; + Paper[proto].remove = function () { + this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas); + for (var i in this) { + this[i] = removed(i); + } + }; + } + + // VML + if (R.vml) { + var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"}, + bites = /([clmz]),?([^clmz]*)/gi, + val = /-?[^,\s-]+/g, + coordsize = 1e3 + S + 1e3, + zoom = 10, + path2vml = function (path) { + var total = /[ahqstv]/ig, + command = pathToAbsolute; + (path + E).match(total) && (command = path2curve); + total = /[clmz]/g; + if (command == pathToAbsolute && !(path + E).match(total)) { + var res = (path + E)[rp](bites, function (all, command, args) { + var vals = [], + isMove = lowerCase.call(command) == "m", + res = map[command]; + args[rp](val, function (value) { + if (isMove && vals[length] == 2) { + res += vals + map[command == "m" ? "l" : "L"]; + vals = []; + } + vals[push](round(value * zoom)); + }); + return res + vals; + }); + return res; + } + var pa = command(path), p, r; + res = []; + for (var i = 0, ii = pa[length]; i < ii; i++) { + p = pa[i]; + r = lowerCase.call(pa[i][0]); + r == "z" && (r = "x"); + for (var j = 1, jj = p[length]; j < jj; j++) { + r += round(p[j] * zoom) + (j != jj - 1 ? "," : E); + } + res[push](r); + } + return res[join](S); + }; + + R[toString] = function () { + return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version; + }; + thePath = function (pathString, vml) { + var g = createNode("group"); + g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px"; + g.coordsize = vml.coordsize; + g.coordorigin = vml.coordorigin; + var el = createNode("shape"), ol = el.style; + ol.width = vml.width + "px"; + ol.height = vml.height + "px"; + el.coordsize = coordsize; + el.coordorigin = vml.coordorigin; + g[appendChild](el); + var p = new Element(el, g, vml), + attr = {fill: "none", stroke: "#000"}; + pathString && (attr.path = pathString); + p.isAbsolute = true; + p.type = "path"; + p.path = []; + p.Path = E; + setFillAndStroke(p, attr); + vml.canvas[appendChild](g); + return p; + }; + setFillAndStroke = function (o, params) { + o.attrs = o.attrs || {}; + var node = o.node, + a = o.attrs, + s = node.style, + xy, + res = o; + for (var par in params) if (params[has](par)) { + a[par] = params[par]; + } + params.href && (node.href = params.href); + params.title && (node.title = params.title); + params.target && (node.target = params.target); + params.cursor && (s.cursor = params.cursor); + "blur" in params && o.blur(params.blur); + if (params.path && o.type == "path") { + a.path = params.path; + node.path = path2vml(a.path); + } + if (params.rotation != null) { + o.rotate(params.rotation, true); + } + if (params.translation) { + xy = (params.translation + E)[split](separator); + translate.call(o, xy[0], xy[1]); + if (o._.rt.cx != null) { + o._.rt.cx +=+ xy[0]; + o._.rt.cy +=+ xy[1]; + o.setBox(o.attrs, xy[0], xy[1]); + } + } + if (params.scale) { + xy = (params.scale + E)[split](separator); + o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null); + } + if ("clip-rect" in params) { + var rect = (params["clip-rect"] + E)[split](separator); + if (rect[length] == 4) { + rect[2] = +rect[2] + (+rect[0]); + rect[3] = +rect[3] + (+rect[1]); + var div = node.clipRect || doc.createElement("div"), + dstyle = div.style, + group = node.parentNode; + dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect); + if (!node.clipRect) { + dstyle.position = "absolute"; + dstyle.top = 0; + dstyle.left = 0; + dstyle.width = o.paper.width + "px"; + dstyle.height = o.paper.height + "px"; + group.parentNode.insertBefore(div, group); + div[appendChild](group); + node.clipRect = div; + } + } + if (!params["clip-rect"]) { + node.clipRect && (node.clipRect.style.clip = E); + } + } + if (o.type == "image" && params.src) { + node.src = params.src; + } + if (o.type == "image" && params.opacity) { + node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")"; + s.filter = (node.filterMatrix || E) + (node.filterOpacity || E); + } + params.font && (s.font = params.font); + params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"'); //' + params["font-size"] && (s.fontSize = params["font-size"]); + params["font-weight"] && (s.fontWeight = params["font-weight"]); + params["font-style"] && (s.fontStyle = params["font-style"]); + if (params.opacity != null || + params["stroke-width"] != null || + params.fill != null || + params.stroke != null || + params["stroke-width"] != null || + params["stroke-opacity"] != null || + params["fill-opacity"] != null || + params["stroke-dasharray"] != null || + params["stroke-miterlimit"] != null || + params["stroke-linejoin"] != null || + params["stroke-linecap"] != null) { + node = o.shape || node; + var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]), + newfill = false; + !fill && (newfill = fill = createNode("fill")); + if ("fill-opacity" in params || "opacity" in params) { + var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1); + opacity < 0 && (opacity = 0); + opacity > 1 && (opacity = 1); + fill.opacity = opacity; + } + params.fill && (fill.on = true); + if (fill.on == null || params.fill == "none") { + fill.on = false; + } + if (fill.on && params.fill) { + var isURL = params.fill.match(ISURL); + if (isURL) { + fill.src = isURL[1]; + fill.type = "tile"; + } else { + fill.color = R.getRGB(params.fill).hex; + fill.src = E; + fill.type = "solid"; + if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) { + a.fill = "none"; + a.gradient = params.fill; + } + } + } + newfill && node[appendChild](fill); + var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]), + newstroke = false; + !stroke && (newstroke = stroke = createNode("stroke")); + if ((params.stroke && params.stroke != "none") || + params["stroke-width"] || + params["stroke-opacity"] != null || + params["stroke-dasharray"] || + params["stroke-miterlimit"] || + params["stroke-linejoin"] || + params["stroke-linecap"]) { + stroke.on = true; + } + (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false); + stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex); + opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1); + var width = (toFloat(params["stroke-width"]) || 1) * .75; + opacity < 0 && (opacity = 0); + opacity > 1 && (opacity = 1); + params["stroke-width"] == null && (width = a["stroke-width"]); + params["stroke-width"] && (stroke.weight = width); + width && width < 1 && (opacity *= width) && (stroke.weight = 1); + stroke.opacity = opacity; + + params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter"); + stroke.miterlimit = params["stroke-miterlimit"] || 8; + params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round"); + if (params["stroke-dasharray"]) { + var dasharray = { + "-": "shortdash", + ".": "shortdot", + "-.": "shortdashdot", + "-..": "shortdashdotdot", + ". ": "dot", + "- ": "dash", + "--": "longdash", + "- .": "dashdot", + "--.": "longdashdot", + "--..": "longdashdotdot" + }; + stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E; + } + newstroke && node[appendChild](stroke); + } + if (res.type == "text") { + s = res.paper.span.style; + a.font && (s.font = a.font); + a["font-family"] && (s.fontFamily = a["font-family"]); + a["font-size"] && (s.fontSize = a["font-size"]); + a["font-weight"] && (s.fontWeight = a["font-weight"]); + a["font-style"] && (s.fontStyle = a["font-style"]); + res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/")); + res.W = a.w = res.paper.span.offsetWidth; + res.H = a.h = res.paper.span.offsetHeight; + res.X = a.x; + res.Y = a.y + round(res.H / 2); + + // text-anchor emulationm + switch (a["text-anchor"]) { + case "start": + res.node.style["v-text-align"] = "left"; + res.bbx = round(res.W / 2); + break; + case "end": + res.node.style["v-text-align"] = "right"; + res.bbx = -round(res.W / 2); + break; + default: + res.node.style["v-text-align"] = "center"; + break; + } + } + }; + addGradientFill = function (o, gradient) { + o.attrs = o.attrs || {}; + var attrs = o.attrs, + fill = o.node.getElementsByTagName("fill"), + type = "linear", + fxfy = ".5 .5"; + o.attrs.gradient = gradient; + gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) { + type = "radial"; + if (fx && fy) { + fx = toFloat(fx); + fy = toFloat(fy); + pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5); + fxfy = fx + S + fy; + } + return E; + }); + gradient = gradient[split](/\s*\-\s*/); + if (type == "linear") { + var angle = gradient.shift(); + angle = -toFloat(angle); + if (isNaN(angle)) { + return null; + } + } + var dots = parseDots(gradient); + if (!dots) { + return null; + } + o = o.shape || o.node; + fill = fill[0] || createNode("fill"); + if (dots[length]) { + fill.on = true; + fill.method = "none"; + fill.type = (type == "radial") ? "gradientradial" : "gradient"; + fill.color = dots[0].color; + fill.color2 = dots[dots[length] - 1].color; + var clrs = []; + for (var i = 0, ii = dots[length]; i < ii; i++) { + dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color); + } + fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color); + if (type == "radial") { + fill.focus = "100%"; + fill.focussize = fxfy; + fill.focusposition = fxfy; + } else { + fill.angle = (270 - angle) % 360; + } + } + return 1; + }; + Element = function (node, group, vml) { + var Rotation = 0, + RotX = 0, + RotY = 0, + Scale = 1; + this[0] = node; + this.id = R._oid++; + this.node = node; + node.raphael = this; + this.X = 0; + this.Y = 0; + this.attrs = {}; + this.Group = group; + this.paper = vml; + this._ = { + tx: 0, + ty: 0, + rt: {deg:0}, + sx: 1, + sy: 1 + }; + !vml.bottom && (vml.bottom = this); + this.prev = vml.top; + vml.top && (vml.top.next = this); + vml.top = this; + this.next = null; + }; + Element[proto].rotate = function (deg, cx, cy) { + if (this.removed) { + return this; + } + if (deg == null) { + if (this._.rt.cx) { + return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S); + } + return this._.rt.deg; + } + deg = (deg + E)[split](separator); + if (deg[length] - 1) { + cx = toFloat(deg[1]); + cy = toFloat(deg[2]); + } + deg = toFloat(deg[0]); + if (cx != null) { + this._.rt.deg = deg; + } else { + this._.rt.deg += deg; + } + cy == null && (cx = null); + this._.rt.cx = cx; + this._.rt.cy = cy; + this.setBox(this.attrs, cx, cy); + this.Group.style.rotation = this._.rt.deg; + // gradient fix for rotation. TODO + // var fill = (this.shape || this.node).getElementsByTagName("fill"); + // fill = fill[0] || {}; + // var b = ((360 - this._.rt.deg) - 270) % 360; + // !R.is(fill.angle, "undefined") && (fill.angle = b); + return this; + }; + Element[proto].setBox = function (params, cx, cy) { + if (this.removed) { + return this; + } + var gs = this.Group.style, + os = (this.shape && this.shape.style) || this.node.style; + params = params || {}; + for (var i in params) if (params[has](i)) { + this.attrs[i] = params[i]; + } + cx = cx || this._.rt.cx; + cy = cy || this._.rt.cy; + var attr = this.attrs, + x, + y, + w, + h; + switch (this.type) { + case "circle": + x = attr.cx - attr.r; + y = attr.cy - attr.r; + w = h = attr.r * 2; + break; + case "ellipse": + x = attr.cx - attr.rx; + y = attr.cy - attr.ry; + w = attr.rx * 2; + h = attr.ry * 2; + break; + case "rect": + case "image": + x = +attr.x; + y = +attr.y; + w = attr.width || 0; + h = attr.height || 0; + break; + case "text": + this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E); + x = attr.x - round(this.W / 2); + y = attr.y - this.H / 2; + w = this.W; + h = this.H; + break; + case "path": + if (!this.attrs.path) { + x = 0; + y = 0; + w = this.paper.width; + h = this.paper.height; + } else { + var dim = pathDimensions(this.attrs.path); + x = dim.x; + y = dim.y; + w = dim.width; + h = dim.height; + } + break; + default: + x = 0; + y = 0; + w = this.paper.width; + h = this.paper.height; + break; + } + cx = (cx == null) ? x + w / 2 : cx; + cy = (cy == null) ? y + h / 2 : cy; + var left = cx - this.paper.width / 2, + top = cy - this.paper.height / 2, t; + gs.left != (t = left + "px") && (gs.left = t); + gs.top != (t = top + "px") && (gs.top = t); + this.X = this.type == "path" ? -left : x; + this.Y = this.type == "path" ? -top : y; + this.W = w; + this.H = h; + if (this.type == "path") { + os.left != (t = -left * zoom + "px") && (os.left = t); + os.top != (t = -top * zoom + "px") && (os.top = t); + } else if (this.type == "text") { + os.left != (t = -left + "px") && (os.left = t); + os.top != (t = -top + "px") && (os.top = t); + } else { + gs.width != (t = this.paper.width + "px") && (gs.width = t); + gs.height != (t = this.paper.height + "px") && (gs.height = t); + os.left != (t = x - left + "px") && (os.left = t); + os.top != (t = y - top + "px") && (os.top = t); + os.width != (t = w + "px") && (os.width = t); + os.height != (t = h + "px") && (os.height = t); + var arcsize = (+params.r || 0) / mmin(w, h); + if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) { + // We should replace element with the new one + var o = createNode("roundrect"), + a = {}, + ii = this.events && this.events[length]; + i = 0; + o.arcsize = arcsize; + o.raphael = this; + this.Group[appendChild](o); + this.Group.removeChild(this.node); + this[0] = this.node = o; + this.arcsize = arcsize; + for (i in attr) { + a[i] = attr[i]; + } + delete a.scale; + this.attr(a); + if (this.events) for (; i < ii; i++) { + this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this); + } + } + } + }; + Element[proto].hide = function () { + !this.removed && (this.Group.style.display = "none"); + return this; + }; + Element[proto].show = function () { + !this.removed && (this.Group.style.display = "block"); + return this; + }; + Element[proto].getBBox = function () { + if (this.removed) { + return this; + } + if (this.type == "path") { + return pathDimensions(this.attrs.path); + } + return { + x: this.X + (this.bbx || 0), + y: this.Y, + width: this.W, + height: this.H + }; + }; + Element[proto].remove = function () { + if (this.removed) { + return; + } + tear(this, this.paper); + this.node.parentNode.removeChild(this.node); + this.Group.parentNode.removeChild(this.Group); + this.shape && this.shape.parentNode.removeChild(this.shape); + for (var i in this) { + delete this[i]; + } + this.removed = true; + }; + Element[proto].attr = function (name, value) { + if (this.removed) { + return this; + } + if (name == null) { + var res = {}; + for (var i in this.attrs) if (this.attrs[has](i)) { + res[i] = this.attrs[i]; + } + this._.rt.deg && (res.rotation = this.rotate()); + (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale()); + res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient; + return res; + } + if (value == null && R.is(name, "string")) { + if (name == "translation") { + return translate.call(this); + } + if (name == "rotation") { + return this.rotate(); + } + if (name == "scale") { + return this.scale(); + } + if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) { + return this.attrs.gradient; + } + return this.attrs[name]; + } + if (this.attrs && value == null && R.is(name, "array")) { + var ii, values = {}; + for (i = 0, ii = name[length]; i < ii; i++) { + values[name[i]] = this.attr(name[i]); + } + return values; + } + var params; + if (value != null) { + params = {}; + params[name] = value; + } + value == null && R.is(name, "object") && (params = name); + if (params) { + if (params.text && this.type == "text") { + this.node.string = params.text; + } + setFillAndStroke(this, params); + if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) { + addGradientFill(this, params.gradient); + } + (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs); + } + return this; + }; + Element[proto].toFront = function () { + !this.removed && this.Group.parentNode[appendChild](this.Group); + this.paper.top != this && tofront(this, this.paper); + return this; + }; + Element[proto].toBack = function () { + if (this.removed) { + return this; + } + if (this.Group.parentNode.firstChild != this.Group) { + this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild); + toback(this, this.paper); + } + return this; + }; + Element[proto].insertAfter = function (element) { + if (this.removed) { + return this; + } + if (element.Group.nextSibling) { + element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling); + } else { + element.Group.parentNode[appendChild](this.Group); + } + insertafter(this, element, this.paper); + return this; + }; + Element[proto].insertBefore = function (element) { + if (this.removed) { + return this; + } + element.Group.parentNode.insertBefore(this.Group, element.Group); + insertbefore(this, element, this.paper); + return this; + }; + var blurregexp = / progid:\S+Blur\([^\)]+\)/g; + Element[proto].blur = function (size) { + var s = this.node.style, + f = s.filter; + f = f.replace(blurregexp, ""); + if (+size !== 0) { + this.attrs.blur = size; + s.filter = f + " progid:DXImageTransform.Microsoft.Blur(pixelradius=" + (+size || 1.5) + ")"; + s.margin = Raphael.format("-{0}px 0 0 -{0}px", Math.round(+size || 1.5)); + } else { + s.filter = f; + s.margin = 0; + delete this.attrs.blur; + } + }; + + theCircle = function (vml, x, y, r) { + var g = createNode("group"), + o = createNode("oval"), + ol = o.style; + g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px"; + g.coordsize = coordsize; + g.coordorigin = vml.coordorigin; + g[appendChild](o); + var res = new Element(o, g, vml); + res.type = "circle"; + setFillAndStroke(res, {stroke: "#000", fill: "none"}); + res.attrs.cx = x; + res.attrs.cy = y; + res.attrs.r = r; + res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2}); + vml.canvas[appendChild](g); + return res; + }; + theRect = function (vml, x, y, w, h, r) { + var g = createNode("group"), + o = createNode("roundrect"), + arcsize = (+r || 0) / (mmin(w, h)); + g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px"; + g.coordsize = coordsize; + g.coordorigin = vml.coordorigin; + g[appendChild](o); + o.arcsize = arcsize; + var res = new Element(o, g, vml); + res.type = "rect"; + setFillAndStroke(res, {stroke: "#000"}); + res.arcsize = arcsize; + res.setBox({x: x, y: y, width: w, height: h, r: r}); + vml.canvas[appendChild](g); + return res; + }; + theEllipse = function (vml, x, y, rx, ry) { + var g = createNode("group"), + o = createNode("oval"), + ol = o.style; + g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px"; + g.coordsize = coordsize; + g.coordorigin = vml.coordorigin; + g[appendChild](o); + var res = new Element(o, g, vml); + res.type = "ellipse"; + setFillAndStroke(res, {stroke: "#000"}); + res.attrs.cx = x; + res.attrs.cy = y; + res.attrs.rx = rx; + res.attrs.ry = ry; + res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2}); + vml.canvas[appendChild](g); + return res; + }; + theImage = function (vml, src, x, y, w, h) { + var g = createNode("group"), + o = createNode("image"), + ol = o.style; + g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px"; + g.coordsize = coordsize; + g.coordorigin = vml.coordorigin; + o.src = src; + g[appendChild](o); + var res = new Element(o, g, vml); + res.type = "image"; + res.attrs.src = src; + res.attrs.x = x; + res.attrs.y = y; + res.attrs.w = w; + res.attrs.h = h; + res.setBox({x: x, y: y, width: w, height: h}); + vml.canvas[appendChild](g); + return res; + }; + theText = function (vml, x, y, text) { + var g = createNode("group"), + el = createNode("shape"), + ol = el.style, + path = createNode("path"), + ps = path.style, + o = createNode("textpath"); + g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px"; + g.coordsize = coordsize; + g.coordorigin = vml.coordorigin; + path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1); + path.textpathok = true; + ol.width = vml.width; + ol.height = vml.height; + o.string = text + E; + o.on = true; + el[appendChild](o); + el[appendChild](path); + g[appendChild](el); + var res = new Element(o, g, vml); + res.shape = el; + res.textpath = path; + res.type = "text"; + res.attrs.text = text; + res.attrs.x = x; + res.attrs.y = y; + res.attrs.w = 1; + res.attrs.h = 1; + setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"}); + res.setBox(); + vml.canvas[appendChild](g); + return res; + }; + setSize = function (width, height) { + var cs = this.canvas.style; + width == +width && (width += "px"); + height == +height && (height += "px"); + cs.width = width; + cs.height = height; + cs.clip = "rect(0 " + width + " " + height + " 0)"; + return this; + }; + var createNode; + doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)"); + try { + !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml"); + createNode = function (tagName) { + return doc.createElement(''); + }; + } catch (e) { + createNode = function (tagName) { + return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">'); + }; + } + create = function () { + var con = getContainer[apply](0, arguments), + container = con.container, + height = con.height, + s, + width = con.width, + x = con.x, + y = con.y; + if (!container) { + throw new Error("VML container not found."); + } + var res = new Paper, + c = res.canvas = doc.createElement("div"), + cs = c.style; + width = width || 512; + height = height || 342; + width == +width && (width += "px"); + height == +height && (height += "px"); + res.width = 1e3; + res.height = 1e3; + res.coordsize = zoom * 1e3 + S + zoom * 1e3; + res.coordorigin = "0 0"; + res.span = doc.createElement("span"); + res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;"; + c[appendChild](res.span); + cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height); + if (container == 1) { + doc.body[appendChild](c); + cs.left = x + "px"; + cs.top = y + "px"; + } else { + container.style.width = width; + container.style.height = height; + if (container.firstChild) { + container.insertBefore(c, container.firstChild); + } else { + container[appendChild](c); + } + } + plugins.call(res, res, R.fn); + return res; + }; + Paper[proto].clear = function () { + this.canvas.innerHTML = E; + this.span = doc.createElement("span"); + this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;"; + this.canvas[appendChild](this.span); + this.bottom = this.top = null; + }; + Paper[proto].remove = function () { + this.canvas.parentNode.removeChild(this.canvas); + for (var i in this) { + this[i] = removed(i); + } + return true; + }; + } + + // rest + // Safari or Chrome (WebKit) rendering bug workaround method + if ((/^Apple|^Google/).test(win.navigator.vendor) && !(win.navigator.userAgent.indexOf("Version/4.0") + 1)) { + Paper[proto].safari = function () { + var rect = this.rect(-99, -99, this.width + 99, this.height + 99); + win.setTimeout(function () {rect.remove();}); + }; + } else { + Paper[proto].safari = function () {}; + } + + // Events + var addEvent = (function () { + if (doc.addEventListener) { + return function (obj, type, fn, element) { + var f = function (e) { + return fn.call(element, e); + }; + obj.addEventListener(type, f, false); + return function () { + obj.removeEventListener(type, f, false); + return true; + }; + }; + } else if (doc.attachEvent) { + return function (obj, type, fn, element) { + var f = function (e) { + return fn.call(element, e || win.event); + }; + obj.attachEvent("on" + type, f); + var detacher = function () { + obj.detachEvent("on" + type, f); + return true; + }; + return detacher; + }; + } + })(); + for (var i = events[length]; i--;) { + (function (eventName) { + Element[proto][eventName] = function (fn) { + if (R.is(fn, "function")) { + this.events = this.events || []; + this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)}); + } + return this; + }; + Element[proto]["un" + eventName] = function (fn) { + var events = this.events, + l = events[length]; + while (l--) if (events[l].name == eventName && events[l].f == fn) { + events[l].unbind(); + events.splice(l, 1); + !events.length && delete this.events; + return this; + } + return this; + }; + })(events[i]); + } + Element[proto].hover = function (f_in, f_out) { + return this.mouseover(f_in).mouseout(f_out); + }; + Element[proto].unhover = function (f_in, f_out) { + return this.unmouseover(f_in).unmouseout(f_out); + }; + Paper[proto].circle = function (x, y, r) { + return theCircle(this, x || 0, y || 0, r || 0); + }; + Paper[proto].rect = function (x, y, w, h, r) { + return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0); + }; + Paper[proto].ellipse = function (x, y, rx, ry) { + return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0); + }; + Paper[proto].path = function (pathString) { + pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E); + return thePath(R.format[apply](R, arguments), this); + }; + Paper[proto].image = function (src, x, y, w, h) { + return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0); + }; + Paper[proto].text = function (x, y, text) { + return theText(this, x || 0, y || 0, text || E); + }; + Paper[proto].set = function (itemsArray) { + arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length])); + return new Set(itemsArray); + }; + Paper[proto].setSize = setSize; + Paper[proto].top = Paper[proto].bottom = null; + Paper[proto].raphael = R; + function x_y() { + return this.x + S + this.y; + } + Element[proto].scale = function (x, y, cx, cy) { + if (x == null && y == null) { + return { + x: this._.sx, + y: this._.sy, + toString: x_y + }; + } + y = y || x; + !+y && (y = x); + var dx, + dy, + dcx, + dcy, + a = this.attrs; + if (x != 0) { + var bb = this.getBBox(), + rcx = bb.x + bb.width / 2, + rcy = bb.y + bb.height / 2, + kx = x / this._.sx, + ky = y / this._.sy; + cx = (+cx || cx == 0) ? cx : rcx; + cy = (+cy || cy == 0) ? cy : rcy; + var dirx = ~~(x / math.abs(x)), + diry = ~~(y / math.abs(y)), + s = this.node.style, + ncx = cx + (rcx - cx) * kx, + ncy = cy + (rcy - cy) * ky; + switch (this.type) { + case "rect": + case "image": + var neww = a.width * dirx * kx, + newh = a.height * diry * ky; + this.attr({ + height: newh, + r: a.r * mmin(dirx * kx, diry * ky), + width: neww, + x: ncx - neww / 2, + y: ncy - newh / 2 + }); + break; + case "circle": + case "ellipse": + this.attr({ + rx: a.rx * dirx * kx, + ry: a.ry * diry * ky, + r: a.r * mmin(dirx * kx, diry * ky), + cx: ncx, + cy: ncy + }); + break; + case "path": + var path = pathToRelative(a.path), + skip = true; + for (var i = 0, ii = path[length]; i < ii; i++) { + var p = path[i], + P0 = upperCase.call(p[0]); + if (P0 == "M" && skip) { + continue; + } else { + skip = false; + } + if (P0 == "A") { + p[path[i][length] - 2] *= kx; + p[path[i][length] - 1] *= ky; + p[1] *= dirx * kx; + p[2] *= diry * ky; + p[5] = +!(dirx + diry ? !+p[5] : +p[5]); + } else if (P0 == "H") { + for (var j = 1, jj = p[length]; j < jj; j++) { + p[j] *= kx; + } + } else if (P0 == "V") { + for (j = 1, jj = p[length]; j < jj; j++) { + p[j] *= ky; + } + } else { + for (j = 1, jj = p[length]; j < jj; j++) { + p[j] *= (j % 2) ? kx : ky; + } + } + } + var dim2 = pathDimensions(path); + dx = ncx - dim2.x - dim2.width / 2; + dy = ncy - dim2.y - dim2.height / 2; + path[0][1] += dx; + path[0][2] += dy; + this.attr({path: path}); + break; + } + if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) { + if (this.transformations) { + this.transformations[2] = "scale("[concat](dirx, ",", diry, ")"); + this.node[setAttribute]("transform", this.transformations[join](S)); + dx = (dirx == -1) ? -a.x - (neww || 0) : a.x; + dy = (diry == -1) ? -a.y - (newh || 0) : a.y; + this.attr({x: dx, y: dy}); + a.fx = dirx - 1; + a.fy = diry - 1; + } else { + this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx, + ", M12=0, M21=0, M22=", diry, + ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')"); + s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E); + } + } else { + if (this.transformations) { + this.transformations[2] = E; + this.node[setAttribute]("transform", this.transformations[join](S)); + a.fx = 0; + a.fy = 0; + } else { + this.node.filterMatrix = E; + s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E); + } + } + a.scale = [x, y, cx, cy][join](S); + this._.sx = x; + this._.sy = y; + } + return this; + }; + Element[proto].clone = function () { + var attr = this.attr(); + delete attr.scale; + delete attr.translation; + return this.paper[this.type]().attr(attr); + }; + var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) { + var len = 0, + old; + for (var i = 0; i < 1.001; i+=.001) { + var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i); + i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5)); + if (len >= length) { + return dot; + } + old = dot; + } + }), + getLengthFactory = function (istotal, subpath) { + return function (path, length, onlystart) { + path = path2curve(path); + var x, y, p, l, sp = "", subpaths = {}, point, + len = 0; + for (var i = 0, ii = path.length; i < ii; i++) { + p = path[i]; + if (p[0] == "M") { + x = +p[1]; + y = +p[2]; + } else { + l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]); + if (len + l > length) { + if (subpath && !subpaths.start) { + point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len); + sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y]; + if (onlystart) {return sp;} + subpaths.start = sp; + sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join](); + len += l; + x = +p[5]; + y = +p[6]; + continue; + } + if (!istotal && !subpath) { + point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len); + return {x: point.x, y: point.y, alpha: point.alpha}; + } + } + len += l; + x = +p[5]; + y = +p[6]; + } + sp += p; + } + subpaths.end = sp; + point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1); + point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha}); + return point; + }; + }, + segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) { + var old = {x: 0, y: 0}, + len = 0; + for (var i = 0; i < 1.01; i+=.01) { + var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i); + i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5)); + old = dot; + } + return len; + }); + var getTotalLength = getLengthFactory(1), + getPointAtLength = getLengthFactory(), + getSubpathsAtLength = getLengthFactory(0, 1); + Element[proto].getTotalLength = function () { + if (this.type != "path") {return;} + return getTotalLength(this.attrs.path); + }; + Element[proto].getPointAtLength = function (length) { + if (this.type != "path") {return;} + return getPointAtLength(this.attrs.path, length); + }; + Element[proto].getSubpath = function (from, to) { + if (this.type != "path") {return;} + if (math.abs(this.getTotalLength() - to) < 1e-6) { + return getSubpathsAtLength(this.attrs.path, from).end; + } + var a = getSubpathsAtLength(this.attrs.path, to, 1); + return from ? getSubpathsAtLength(a, from).end : a; + }; + + // animation easing formulas + R.easing_formulas = { + linear: function (n) { + return n; + }, + "<": function (n) { + return pow(n, 3); + }, + ">": function (n) { + return pow(n - 1, 3) + 1; + }, + "<>": function (n) { + n = n * 2; + if (n < 1) { + return pow(n, 3) / 2; + } + n -= 2; + return (pow(n, 3) + 2) / 2; + }, + backIn: function (n) { + var s = 1.70158; + return n * n * ((s + 1) * n - s); + }, + backOut: function (n) { + n = n - 1; + var s = 1.70158; + return n * n * ((s + 1) * n + s) + 1; + }, + elastic: function (n) { + if (n == 0 || n == 1) { + return n; + } + var p = .3, + s = p / 4; + return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1; + }, + bounce: function (n) { + var s = 7.5625, + p = 2.75, + l; + if (n < (1 / p)) { + l = s * n * n; + } else { + if (n < (2 / p)) { + n -= (1.5 / p); + l = s * n * n + .75; + } else { + if (n < (2.5 / p)) { + n -= (2.25 / p); + l = s * n * n + .9375; + } else { + n -= (2.625 / p); + l = s * n * n + .984375; + } + } + } + return l; + } + }; + + var animationElements = {length : 0}, + animation = function () { + var Now = +new Date; + for (var l in animationElements) if (l != "length" && animationElements[has](l)) { + var e = animationElements[l]; + if (e.stop || e.el.removed) { + delete animationElements[l]; + animationElements[length]--; + continue; + } + var time = Now - e.start, + ms = e.ms, + easing = e.easing, + from = e.from, + diff = e.diff, + to = e.to, + t = e.t, + prev = e.prev || 0, + that = e.el, + callback = e.callback, + set = {}, + now; + if (time < ms) { + var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms; + for (var attr in from) if (from[has](attr)) { + switch (availableAnimAttrs[attr]) { + case "along": + now = pos * ms * diff[attr]; + to.back && (now = to.len - now); + var point = getPointAtLength(to[attr], now); + that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0); + diff.x = point.x; + diff.y = point.y; + that.translate(point.x - diff.sx, point.y - diff.sy); + to.rot && that.rotate(diff.r + point.alpha, point.x, point.y); + break; + case "number": + now = +from[attr] + pos * ms * diff[attr]; + break; + case "colour": + now = "rgb(" + [ + upto255(round(from[attr].r + pos * ms * diff[attr].r)), + upto255(round(from[attr].g + pos * ms * diff[attr].g)), + upto255(round(from[attr].b + pos * ms * diff[attr].b)) + ][join](",") + ")"; + break; + case "path": + now = []; + for (var i = 0, ii = from[attr][length]; i < ii; i++) { + now[i] = [from[attr][i][0]]; + for (var j = 1, jj = from[attr][i][length]; j < jj; j++) { + now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j]; + } + now[i] = now[i][join](S); + } + now = now[join](S); + break; + case "csv": + switch (attr) { + case "translation": + var x = diff[attr][0] * (time - prev), + y = diff[attr][1] * (time - prev); + t.x += x; + t.y += y; + now = x + S + y; + break; + case "rotation": + now = +from[attr][0] + pos * ms * diff[attr][0]; + from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]); + break; + case "scale": + now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in to[attr] ? to[attr][2] : E), (3 in to[attr] ? to[attr][3] : E)][join](S); + break; + case "clip-rect": + now = []; + i = 4; + while (i--) { + now[i] = +from[attr][i] + pos * ms * diff[attr][i]; + } + break; + } + break; + } + set[attr] = now; + } + that.attr(set); + that._run && that._run.call(that); + } else { + if (to.along) { + point = getPointAtLength(to.along, to.len * !to.back); + that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy); + to.rot && that.rotate(diff.r + point.alpha, point.x, point.y); + } + (t.x || t.y) && that.translate(-t.x, -t.y); + to.scale && (to.scale = to.scale + E); + that.attr(to); + delete animationElements[l]; + animationElements[length]--; + that.in_animation = null; + R.is(callback, "function") && callback.call(that); + } + e.prev = time; + } + R.svg && that && that.paper.safari(); + animationElements[length] && win.setTimeout(animation); + }, + upto255 = function (color) { + return color > 255 ? 255 : (color < 0 ? 0 : color); + }, + translate = function (x, y) { + if (x == null) { + return {x: this._.tx, y: this._.ty, toString: x_y}; + } + this._.tx += +x; + this._.ty += +y; + switch (this.type) { + case "circle": + case "ellipse": + this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy}); + break; + case "rect": + case "image": + case "text": + this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y}); + break; + case "path": + var path = pathToRelative(this.attrs.path); + path[0][1] += +x; + path[0][2] += +y; + this.attr({path: path}); + break; + } + return this; + }; + Element[proto].animateWith = function (element, params, ms, easing, callback) { + animationElements[element.id] && (params.start = animationElements[element.id].start); + return this.animate(params, ms, easing, callback); + }; + Element[proto].animateAlong = along(); + Element[proto].animateAlongBack = along(1); + function along(isBack) { + return function (path, ms, rotate, callback) { + var params = {back: isBack}; + R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate); + path && path.constructor == Element && (path = path.attrs.path); + path && (params.along = path); + return this.animate(params, ms, callback); + }; + } + Element[proto].onAnimation = function (f) { + this._run = f || 0; + return this; + }; + Element[proto].animate = function (params, ms, easing, callback) { + if (R.is(easing, "function") || !easing) { + callback = easing || null; + } + var from = {}, + to = {}, + diff = {}; + for (var attr in params) if (params[has](attr)) { + if (availableAnimAttrs[has](attr)) { + from[attr] = this.attr(attr); + (from[attr] == null) && (from[attr] = availableAttrs[attr]); + to[attr] = params[attr]; + switch (availableAnimAttrs[attr]) { + case "along": + var len = getTotalLength(params[attr]), + point = getPointAtLength(params[attr], len * !!params.back), + bb = this.getBBox(); + diff[attr] = len / ms; + diff.tx = bb.x; + diff.ty = bb.y; + diff.sx = point.x; + diff.sy = point.y; + to.rot = params.rot; + to.back = params.back; + to.len = len; + params.rot && (diff.r = toFloat(this.rotate()) || 0); + break; + case "number": + diff[attr] = (to[attr] - from[attr]) / ms; + break; + case "colour": + from[attr] = R.getRGB(from[attr]); + var toColour = R.getRGB(to[attr]); + diff[attr] = { + r: (toColour.r - from[attr].r) / ms, + g: (toColour.g - from[attr].g) / ms, + b: (toColour.b - from[attr].b) / ms + }; + break; + case "path": + var pathes = path2curve(from[attr], to[attr]); + from[attr] = pathes[0]; + var toPath = pathes[1]; + diff[attr] = []; + for (var i = 0, ii = from[attr][length]; i < ii; i++) { + diff[attr][i] = [0]; + for (var j = 1, jj = from[attr][i][length]; j < jj; j++) { + diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms; + } + } + break; + case "csv": + var values = (params[attr] + E)[split](separator), + from2 = (from[attr] + E)[split](separator); + switch (attr) { + case "translation": + from[attr] = [0, 0]; + diff[attr] = [values[0] / ms, values[1] / ms]; + break; + case "rotation": + from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]]; + diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0]; + break; + case "scale": + params[attr] = values; + from[attr] = (from[attr] + E)[split](separator); + diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0]; + break; + case "clip-rect": + from[attr] = (from[attr] + E)[split](separator); + diff[attr] = []; + i = 4; + while (i--) { + diff[attr][i] = (values[i] - from[attr][i]) / ms; + } + break; + } + to[attr] = values; + } + } + } + this.stop(); + this.in_animation = 1; + animationElements[this.id] = { + start: params.start || +new Date, + ms: ms, + easing: easing, + from: from, + diff: diff, + to: to, + el: this, + callback: callback, + t: {x: 0, y: 0} + }; + ++animationElements[length] == 1 && animation(); + return this; + }; + Element[proto].stop = function () { + animationElements[this.id] && animationElements[length]--; + delete animationElements[this.id]; + return this; + }; + Element[proto].translate = function (x, y) { + return this.attr({translation: x + " " + y}); + }; + Element[proto][toString] = function () { + return "Rapha\xebl\u2019s object"; + }; + R.ae = animationElements; + + // Set + var Set = function (items) { + this.items = []; + this[length] = 0; + if (items) { + for (var i = 0, ii = items[length]; i < ii; i++) { + if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) { + this[this.items[length]] = this.items[this.items[length]] = items[i]; + this[length]++; + } + } + } + }; + Set[proto][push] = function () { + var item, + len; + for (var i = 0, ii = arguments[length]; i < ii; i++) { + item = arguments[i]; + if (item && (item.constructor == Element || item.constructor == Set)) { + len = this.items[length]; + this[len] = this.items[len] = item; + this[length]++; + } + } + return this; + }; + Set[proto].pop = function () { + delete this[this[length]--]; + return this.items.pop(); + }; + for (var method in Element[proto]) if (Element[proto][has](method)) { + Set[proto][method] = (function (methodname) { + return function () { + for (var i = 0, ii = this.items[length]; i < ii; i++) { + this.items[i][methodname][apply](this.items[i], arguments); + } + return this; + }; + })(method); + } + Set[proto].attr = function (name, value) { + if (name && R.is(name, "array") && R.is(name[0], "object")) { + for (var j = 0, jj = name[length]; j < jj; j++) { + this.items[j].attr(name[j]); + } + } else { + for (var i = 0, ii = this.items[length]; i < ii; i++) { + this.items[i].attr(name, value); + } + } + return this; + }; + Set[proto].animate = function (params, ms, easing, callback) { + (R.is(easing, "function") || !easing) && (callback = easing || null); + var len = this.items[length], + i = len, + set = this, + collector; + callback && (collector = function () { + !--len && callback.call(set); + }); + this.items[--i].animate(params, ms, easing || collector, collector); + while (i--) { + this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector); + } + return this; + }; + Set[proto].insertAfter = function (el) { + var i = this.items[length]; + while (i--) { + this.items[i].insertAfter(el); + } + return this; + }; + Set[proto].getBBox = function () { + var x = [], + y = [], + w = [], + h = []; + for (var i = this.items[length]; i--;) { + var box = this.items[i].getBBox(); + x[push](box.x); + y[push](box.y); + w[push](box.x + box.width); + h[push](box.y + box.height); + } + x = mmin[apply](0, x); + y = mmin[apply](0, y); + return { + x: x, + y: y, + width: mmax[apply](0, w) - x, + height: mmax[apply](0, h) - y + }; + }; + Set[proto].clone = function (s) { + s = new Set; + for (var i = 0, ii = this.items[length]; i < ii; i++) { + s[push](this.items[i].clone()); + } + return s; + }; + + R.registerFont = function (font) { + if (!font.face) { + return font; + } + this.fonts = this.fonts || {}; + var fontcopy = { + w: font.w, + face: {}, + glyphs: {} + }, + family = font.face["font-family"]; + for (var prop in font.face) if (font.face[has](prop)) { + fontcopy.face[prop] = font.face[prop]; + } + if (this.fonts[family]) { + this.fonts[family][push](fontcopy); + } else { + this.fonts[family] = [fontcopy]; + } + if (!font.svg) { + fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10); + for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) { + var path = font.glyphs[glyph]; + fontcopy.glyphs[glyph] = { + w: path.w, + k: {}, + d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) { + return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M"; + }) + "z" + }; + if (path.k) { + for (var k in path.k) if (path[has](k)) { + fontcopy.glyphs[glyph].k[k] = path.k[k]; + } + } + } + } + return font; + }; + Paper[proto].getFont = function (family, weight, style, stretch) { + stretch = stretch || "normal"; + style = style || "normal"; + weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400; + var font = R.fonts[family]; + if (!font) { + var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i"); + for (var fontName in R.fonts) if (R.fonts[has](fontName)) { + if (name.test(fontName)) { + font = R.fonts[fontName]; + break; + } + } + } + var thefont; + if (font) { + for (var i = 0, ii = font[length]; i < ii; i++) { + thefont = font[i]; + if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) { + break; + } + } + } + return thefont; + }; + Paper[proto].print = function (x, y, string, font, size, origin) { + origin = origin || "middle"; // baseline|middle + var out = this.set(), + letters = (string + E)[split](E), + shift = 0, + path = E, + scale; + R.is(font, "string") && (font = this.getFont(font)); + if (font) { + scale = (size || 16) / font.face["units-per-em"]; + var bb = font.face.bbox.split(separator), + top = +bb[0], + height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2); + for (var i = 0, ii = letters[length]; i < ii; i++) { + var prev = i && font.glyphs[letters[i - 1]] || {}, + curr = font.glyphs[letters[i]]; + shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0; + curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]})); + } + out.scale(scale, scale, top, height).translate(x - top, y - height); + } + return out; + }; + + var formatrg = /\{(\d+)\}/g; + R.format = function (token, array) { + var args = R.is(array, "array") ? [0][concat](array) : arguments; + token && R.is(token, "string") && args[length] - 1 && (token = token[rp](formatrg, function (str, i) { + return args[++i] == null ? E : args[i]; + })); + return token || E; + }; + R.ninja = function () { + oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael; + return R; + }; + R.el = Element[proto]; + return R; +})(); \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/yui-min.js b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/yui-min.js new file mode 100644 index 0000000..55f017d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-loggraph/src/main/resources/webapp/org/apache/zookeeper/graph/resources/yui-min.js @@ -0,0 +1,12 @@ +/* +Copyright (c) 2010, Yahoo! Inc. All rights reserved. +Code licensed under the BSD License: +http://developer.yahoo.com/yui/license.html +version: 3.1.0 +build: 2026 +*/ +if(typeof YUI==="undefined"){var YUI=function(F,E,D,C,A){var B=this,J=arguments,I,G=J.length,H=(typeof YUI_config!=="undefined")&&YUI_config;if(!(B instanceof YUI)){return new YUI(F,E,D,C,A);}else{B._init();if(H){B._config(H);}for(I=0;I-1){M="3.0.0";}YUI.prototype={_config:function(Y){Y=Y||{};var T,V,W,U=this.config,X=U.modules,S=U.groups;for(V in Y){T=Y[V];if(X&&V=="modules"){for(W in T){X[W]=T[W];}}else{if(S&&V=="groups"){for(W in T){S[W]=T[W];}}else{if(V=="win"){U[V]=T.contentWindow||T;U.doc=U[V].document;}else{U[V]=T;}}}}},_init:function(){var U,V=this,S=YUI.Env,T=V.Env;V.version=M;if(!T){V.Env={mods:{},base:K,cdn:K+M+"/build/",bootstrapped:false,_idx:0,_used:{},_attached:{},_yidx:0,_uidx:0,_guidp:"y",_loaded:{},getBase:function(c,a){var W,X,Z,d,Y;X=(R&&R.getElementsByTagName("script"))||[];for(Z=0;ZJ)?H[J]:true;}}return L;};F.indexOf=(D.indexOf)?function(G,H){return D.indexOf.call(G,H);}:function(G,I){for(var H=0;H-1);};E.owns=F;E.each=function(K,J,L,I){var H=L||B,G;for(G in K){if(I||F(K,G)){J.call(H,K[G],G,K);}}return B;};E.some=function(K,J,L,I){var H=L||B,G;for(G in K){if(I||F(K,G)){if(J.call(H,K[G],G,K)){return true;}}}return false;};E.getValue=function(K,J){if(!B.Lang.isObject(K)){return D;}var H,I=B.Array(J),G=I.length;for(H=0;K!==D&&H=0){for(G=0;H!==D&&G0){C=D(I);if(C){return C;}else{E=I.lastIndexOf("-");if(E>=0){I=I.substring(0,E);if(E>=2&&I.charAt(E-2)==="-"){I=I.substring(0,E-2);}}else{break;}}}}return"";}});},"3.1.0",{requires:["yui-base"]});YUI.add("yui-log",function(A){(function(){var E,D=A,F="yui:log",B="undefined",C={debug:1,info:1,warn:1,error:1};D.log=function(I,Q,G,O){var K,N,L,J,M,H=D,P=H.config;if(P.debug){if(G){N=P.logExclude;L=P.logInclude;if(L&&!(G in L)){K=1;}else{if(N&&(G in N)){K=1;}}}if(!K){if(P.useBrowserConsole){J=(G)?G+": "+I:I;if(H.Lang.isFunction(P.logFn)){P.logFn(I,Q,G);}else{if(typeof console!=B&&console.log){M=(Q&&console[Q]&&(Q in C))?Q:"log";console[M](J);}else{if(typeof opera!=B){opera.postError(J);}}}}if(H.fire&&!O){if(!E){H.publish(F,{broadcast:2});E=1;}H.fire(F,{msg:I,cat:Q,src:G});}}}return H;};D.message=function(){return D.log.apply(D,arguments);};})();},"3.1.0",{requires:["yui-base"]});YUI.add("yui-later",function(A){(function(){var B=A.Lang,C=function(K,E,L,G,H){K=K||0;E=E||{};var F=L,J=A.Array(G),I,D;if(B.isString(L)){F=E[L];}if(!F){}I=function(){F.apply(E,J);};D=(H)?setInterval(I,K):setTimeout(I,K);return{id:D,interval:H,cancel:function(){if(this.interval){clearInterval(D);}else{clearTimeout(D);}}};};A.later=C;B.later=C;})();},"3.1.0",{requires:["yui-base"]});YUI.add("yui-throttle",function(Y){ +/* Based on work by Simon Willison: http://gist.github.com/292562 */ +var throttle=function(fn,ms){ms=(ms)?ms:(Y.config.throttleTime||150);if(ms===-1){return(function(){fn.apply(null,arguments);});}var last=(new Date()).getTime();return(function(){var now=(new Date()).getTime();if(now-last>ms){last=now;fn.apply(null,arguments);}});};Y.throttle=throttle;},"3.1.0",{requires:["yui-base"]});YUI.add("yui",function(A){},"3.1.0",{use:["yui-base","get","intl-base","yui-log","yui-later","yui-throttle"]}); \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/JMX-RESOURCES b/zookeeper-contrib/zookeeper-contrib-monitoring/JMX-RESOURCES new file mode 100644 index 0000000..1d1aa98 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/JMX-RESOURCES @@ -0,0 +1,38 @@ + +Resources for monitoring ZooKeeper using JMX +-------------------------------------------- + +JMX/REST Bridge +--------------- + +http://code.google.com/p/polarrose-jmx-rest-bridge/ + +"Simple Java Web Application that exposes JMX servers through HTTP. This was written so that external tools can easily query JMX attributes of Java applications. More specifically, this was written to allow Cacti to generate fancy graphs of ActiveMQ instances." + +JMXetric +-------- + +http://code.google.com/p/jmxetric/ + +"JMXetric is a 100% java, configurable JVM agent that periodically polls MBean attributes and reports their values to Ganglia." + +jmxquery +-------- + +http://code.google.com/p/jmxquery/ + +"a plugin for nagios to check jmx" + +check_jmx +--------- + +http://exchange.nagios.org/directory/Plugins/Java-Applications-and-Servers/check_jmx/details + + +jmx2snmp +-------- + +http://github.com/tcurdt/jmx2snmp + +Expose application JMX properties via SNMP + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/README b/zookeeper-contrib/zookeeper-contrib-monitoring/README new file mode 100644 index 0000000..81521fc --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/README @@ -0,0 +1,85 @@ + +Tools and Recipes for ZooKeeper Monitoring +------------------------------------------ + +How To Monitor +-------------- + +A ZooKeeper cluster can be monitored in two ways: + 1. by using the 'mntr' 4letterword command + 2. by using JMX to query the MBeans + +This repo contains tools and recipes for monitoring ZooKeeper using the first method. + +Check the file JMX-RESOURCE for some links to resources that could help you monitor a ZooKeeper cluster using the JMX interface. + +Requirements +------------ + +ZooKeeper 3.4.0 or later or you can apply ZOOKEEPER-744 patch over the latest 3.3.x release. +The server should understand the 'mntr' 4letterword command. + +$ echo 'mntr' | nc localhost 2181 +zk_version 3.4.0--1, built on 06/19/2010 15:07 GMT +zk_avg_latency 141 +zk_max_latency 1788 +zk_min_latency 0 +zk_packets_received 385466 +zk_packets_sent 435364 +zk_num_alive_connections 1 +zk_outstanding_requests 0 +zk_server_state follower +zk_znode_count 5 +zk_watch_count 0 +zk_ephemerals_count 0 +zk_approximate_data_size 41 +zk_open_file_descriptor_count 20 +zk_max_file_descriptor_count 1024 + +Python 2.6 (maybe it works on previous version but it's not tested yet). + +In a nutshell +------------- + +All you need is check_zookeeper.py It has no external dependencies. + + +*** On Nagios call the script like this: + +./check_zookeeper.py -o nagios -s "" -k -w -c + + +*** On Cacti define a custom data input method using the script like this: + +./check_zookeeper.py -o cacti -s "" -k --leader + +-- outputs a single value for the given key fetched from the cluster leader + +OR + +./check_zookeeper.py -o cacti -s "" -k + +-- outputs multiple values on for each cluster node +ex: localhost_2182:0 localhost_2183:0 localhost_2181:0 localhost_2184:0 localhost_2185:0 + +*** On Ganglia: + +install the plugin found in the ganglia/ subfolder OR + +./check_zookeeper.py -o ganglia -s "" + +it will use gmetric to send zookeeper node status data. + + +Check the subfolders for configuration details and samples for each platform. + +License +------- + +Apache License 2.0 or later. + +ZooKeeper 4letterwords Commands +------------------------------- + +http://zookeeper.apache.org/docs/current/zookeeperAdmin.html#sc_zkCommands + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/cacti/README b/zookeeper-contrib/zookeeper-contrib-monitoring/cacti/README new file mode 100644 index 0000000..8188723 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/cacti/README @@ -0,0 +1,56 @@ +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. + +Recipes for ZooKeeper monitoring using Cacti +-------------------------------------------- + +Cacti install guide: https://help.ubuntu.com/community/Cacti + +Cacti Manual: http://www.cacti.net/downloads/docs/html/ +PDF version: http://www.cacti.net/downloads/docs/pdf/manual.pdf + +Check Chapter 16: Simplest Method of Going from Script to Graph + http://www.cacti.net/downloads/docs/html/how_to.html#SCRIPT_TO_GRAPH + +WARNING: I have wrote these instructions while installing and configuring the plugin on my desktop computer running Ubuntu 9.10. I've installed Cacti using apt-get. + +WARNING: I'm going to make the assumption that you know how to work with Cacti and how to setup Data Input Methods for custom scripts. I'm also going to assume that you have already installed Cacti and everything works as expected. + +You can extend the Cacti's data gathering functionality through external scripts. Cacti comes with a number of scripts out of the box wich are localted in the scripts/ directory. + + +The check_zookeeper.py script can be used a custom data input method for Cacti. + +Single value (check cluster status by sending queries to the leader): +--------------------------------------------------------------------- + +python scripts/check_zookeeper.py -s "localhost:2181,localhost:2182,localhost:2183,localhost:2184,localhost:2185" -k -o cacti --leader + +When you will call the script this way it will about a single value representing the value attached to this . + + +Multiple values (one for each cluster node): +-------------------------------------------- + +python scripts/check_zookeeper.py -s "localhost:2181,localhost:2182,localhost:2183,localhost:2184,localhost:2185" -k -o cacti + +Output: +localhost_2182:0 localhost_2183:0 localhost_2181:0 localhost_2184:0 localhost_2185:0 + + +TBD: Step by step guide + + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/check_zookeeper.py b/zookeeper-contrib/zookeeper-contrib-monitoring/check_zookeeper.py new file mode 100644 index 0000000..0a08e84 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/check_zookeeper.py @@ -0,0 +1,409 @@ +#! /usr/bin/env python +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +""" Check Zookeeper Cluster + +Generic monitoring script that could be used with multiple platforms (Ganglia, Nagios, Cacti). + +It requires ZooKeeper 3.4.0 or greater. The script needs the 'mntr' 4letter word +command (patch ZOOKEEPER-744) that was now commited to the trunk. +The script also works with ZooKeeper 3.3.x but in a limited way. +""" + +import sys +import socket +import logging +import re +import subprocess + +from StringIO import StringIO +from optparse import OptionParser, OptionGroup + +__version__ = (0, 1, 0) + +log = logging.getLogger() +logging.basicConfig(level=logging.ERROR) + +class NagiosHandler(object): + + @classmethod + def register_options(cls, parser): + group = OptionGroup(parser, 'Nagios specific options') + + group.add_option('-w', '--warning', dest='warning') + group.add_option('-c', '--critical', dest='critical') + + parser.add_option_group(group) + + def analyze(self, opts, cluster_stats): + try: + warning = int(opts.warning) + critical = int(opts.critical) + + except (TypeError, ValueError): + print >>sys.stderr, 'Invalid values for "warning" and "critical".' + return 2 + + if opts.key is None: + print >>sys.stderr, 'You should specify a key name.' + return 2 + + warning_state, critical_state, values = [], [], [] + for host, stats in cluster_stats.items(): + if opts.key in stats: + + value = stats[opts.key] + values.append('%s=%s;%s;%s' % (host, value, warning, critical)) + + if warning >= value > critical or warning <= value < critical: + warning_state.append(host) + + elif (warning < critical and critical <= value) or (warning > critical and critical >= value): + critical_state.append(host) + + if not values: + # Zookeeper may be down, not serving requests or we may have a bad configuration + print 'Critical, %s not found' % opts.key + return 2 + + values = ' '.join(values) + if critical_state: + print 'Critical "%s" %s!|%s' % (opts.key, ', '.join(critical_state), values) + return 2 + + elif warning_state: + print 'Warning "%s" %s!|%s' % (opts.key, ', '.join(warning_state), values) + return 1 + + else: + print 'Ok "%s"!|%s' % (opts.key, values) + return 0 + +class CactiHandler(object): + + @classmethod + def register_options(cls, parser): + group = OptionGroup(parser, 'Cacti specific options') + + group.add_option('-l', '--leader', dest='leader', + action="store_true", help="only query the cluster leader") + + parser.add_option_group(group) + + def analyze(self, opts, cluster_stats): + if opts.key is None: + print >>sys.stderr, 'The key name is mandatory.' + return 1 + + if opts.leader is True: + try: + leader = [x for x in cluster_stats.values() \ + if x.get('zk_server_state', '') == 'leader'][0] + + except IndexError: + print >>sys.stderr, 'No leader found.' + return 3 + + if opts.key in leader: + print leader[opts.key] + return 0 + + else: + print >>sys.stderr, 'Unknown key: "%s"' % opts.key + return 2 + else: + for host, stats in cluster_stats.items(): + if opts.key not in stats: + continue + + host = host.replace(':', '_') + print '%s:%s' % (host, stats[opts.key]), + + +class GangliaHandler(object): + + @classmethod + def register_options(cls, parser): + group = OptionGroup(parser, 'Ganglia specific options') + + group.add_option('-g', '--gmetric', dest='gmetric', + default='/usr/bin/gmetric', help='ganglia gmetric binary '\ + 'location: /usr/bin/gmetric') + + parser.add_option_group(group) + + def call(self, *args, **kwargs): + subprocess.call(*args, **kwargs) + + def analyze(self, opts, cluster_stats): + if len(cluster_stats) != 1: + print >>sys.stderr, 'Only allowed to monitor a single node.' + return 1 + + for host, stats in cluster_stats.items(): + for k, v in stats.items(): + try: + self.call([opts.gmetric, '-n', k, '-v', str(int(v)), '-t', 'uint32']) + except (TypeError, ValueError): + pass + +class ZooKeeperServer(object): + + def __init__(self, host='localhost', port='2181', timeout=1): + self._address = (host, int(port)) + self._timeout = timeout + + def get_stats(self): + """ Get ZooKeeper server stats as a map """ + data = self._send_cmd('mntr') + stat = self._parse_stat(self._send_cmd('stat')) + if data: + mntr = self._parse(data) + missing = ['zk_zxid', 'zk_zxid_counter', 'zk_zxid_epoch'] + for m in missing: + if m in stat: + mntr[m] = stat[m] + return mntr + else: + return stat + + def _create_socket(self): + return socket.socket() + + def _send_cmd(self, cmd): + """ Send a 4letter word command to the server """ + s = self._create_socket() + s.settimeout(self._timeout) + + s.connect(self._address) + s.send(cmd) + + data = s.recv(2048) + s.close() + + return data + + def _parse(self, data): + """ Parse the output from the 'mntr' 4letter word command """ + h = StringIO(data) + + result = {} + for line in h.readlines(): + try: + key, value = self._parse_line(line) + result[key] = value + except ValueError: + pass # ignore broken lines + + return result + + def _parse_stat(self, data): + """ Parse the output from the 'stat' 4letter word command """ + h = StringIO(data) + + result = {} + + version = h.readline() + if version: + result['zk_version'] = version[version.index(':')+1:].strip() + + # skip all lines until we find the empty one + while h.readline().strip(): pass + + for line in h.readlines(): + m = re.match('Latency min/avg/max: (\d+)/(\d+)/(\d+)', line) + if m is not None: + result['zk_min_latency'] = int(m.group(1)) + result['zk_avg_latency'] = int(m.group(2)) + result['zk_max_latency'] = int(m.group(3)) + continue + + m = re.match('Received: (\d+)', line) + if m is not None: + result['zk_packets_received'] = int(m.group(1)) + continue + + m = re.match('Sent: (\d+)', line) + if m is not None: + result['zk_packets_sent'] = int(m.group(1)) + continue + + m = re.match('Alive connections: (\d+)', line) + if m is not None: + result['zk_num_alive_connections'] = int(m.group(1)) + continue + + m = re.match('Outstanding: (\d+)', line) + if m is not None: + result['zk_outstanding_requests'] = int(m.group(1)) + continue + + m = re.match('Mode: (.*)', line) + if m is not None: + result['zk_server_state'] = m.group(1) + continue + + m = re.match('Node count: (\d+)', line) + if m is not None: + result['zk_znode_count'] = int(m.group(1)) + continue + + m = re.match('Watch count: (\d+)', line) + if m is not None: + result['zk_watch_count'] = int(m.group(1)) + continue + + m = re.match('Ephemerals count: (\d+)', line) + if m is not None: + result['zk_ephemerals_count'] = int(m.group(1)) + continue + + m = re.match('Approximate data size: (\d+)', line) + if m is not None: + result['zk_approximate_data_size'] = int(m.group(1)) + continue + + m = re.match('Open file descriptor count: (\d+)', line) + if m is not None: + result['zk_open_file_descriptor_count'] = int(m.group(1)) + continue + + m = re.match('Max file descriptor count: (\d+)', line) + if m is not None: + result['zk_max_file_descriptor_count'] = int(m.group(1)) + continue + + m = re.match('Zxid: (0x[0-9a-fA-F]+)', line) + if m is not None: + result['zk_zxid'] = m.group(1) + result['zk_zxid_counter'] = int(m.group(1), 16) & int('0xffffffff', 16) # lower 32 bits + result['zk_zxid_epoch'] = int(m.group(1), 16) >>32 # high 32 bits + continue + + m = re.match('Proposal sizes last/min/max: (\d+)/(\d+)/(\d+)', line) + if m is not None: + result['zk_last_proposal_size'] = int(m.group(1)) + result['zk_min_proposal_size'] = int(m.group(2)) + result['zk_max_proposal_size'] = int(m.group(3)) + continue + + return result + + def _parse_line(self, line): + try: + key, value = map(str.strip, line.split('\t')) + except ValueError: + raise ValueError('Found invalid line: %s' % line) + + if not key: + raise ValueError('The key is mandatory and should not be empty') + + for typ in [int, float]: + try: + value = typ(value) + break + except (TypeError, ValueError): + pass + + return key, value + +def main(): + opts, args = parse_cli() + + cluster_stats = get_cluster_stats(opts.servers) + if opts.output is None: + dump_stats(cluster_stats) + return 0 + + handler = create_handler(opts.output) + if handler is None: + log.error('undefined handler: %s' % opts.output) + sys.exit(1) + + return handler.analyze(opts, cluster_stats) + +def create_handler(name): + """ Return an instance of a platform specific analyzer """ + try: + return globals()['%sHandler' % name.capitalize()]() + except KeyError: + return None + +def get_all_handlers(): + """ Get a list containing all the platform specific analyzers """ + return [NagiosHandler, CactiHandler, GangliaHandler] + +def dump_stats(cluster_stats): + """ Dump cluster statistics in an user friendly format """ + for server, stats in cluster_stats.items(): + print 'Server:', server + + for key, value in stats.items(): + print "%30s" % key, ' ', value + print + +def get_cluster_stats(servers): + """ Get stats for all the servers in the cluster """ + stats = {} + for host, port in servers: + try: + zk = ZooKeeperServer(host, port) + stats["%s:%s" % (host, port)] = zk.get_stats() + + except socket.error, e: + # ignore because the cluster can still work even + # if some servers fail completely + + # this error should be also visible in a variable + # exposed by the server in the statistics + + logging.info('unable to connect to server '\ + '"%s" on port "%s"' % (host, port)) + + return stats + + +def get_version(): + return '.'.join(map(str, __version__)) + + +def parse_cli(): + parser = OptionParser(usage='./check_zookeeper.py ', version=get_version()) + + parser.add_option('-s', '--servers', dest='servers', + help='a list of SERVERS', metavar='SERVERS') + + parser.add_option('-o', '--output', dest='output', + help='output HANDLER: nagios, ganglia, cacti', metavar='HANDLER') + + parser.add_option('-k', '--key', dest='key') + + for handler in get_all_handlers(): + handler.register_options(parser) + + opts, args = parser.parse_args() + + if opts.servers is None: + parser.error('The list of servers is mandatory') + + opts.servers = [s.split(':') for s in opts.servers.split(',')] + + return (opts, args) + + +if __name__ == '__main__': + sys.exit(main()) + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/README b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/README new file mode 100644 index 0000000..578adfe --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/README @@ -0,0 +1,48 @@ +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. + +Recipes for ZooKeeper monitoring using Ganglia +---------------------------------------------- + +Ganglia Install guide: http://sourceforge.net/apps/trac/ganglia/wiki/Ganglia%203.1.x%20Installation%20and%20Configuration + +Gmond configuration: http://sourceforge.net/apps/trac/ganglia/wiki/Gmond%203.1.x%20General%20Configuration + +WARNING: I have wrote these instructions while installing and configuring the plugin on my desktop computer running Ubuntu 9.10. I've installed Ganglia using apt-get. + +WARNING: I'm going to make the assumption that you know how to work with Ganglia. I'm also going to assume that you have already installed Gangli and everything works as expected. + +You can monitoring ZooKeeper using Ganglia in two ways: + +1. Using a python module: + + WARNING! The python module only works with Ganglia 3.1.x + + a. enable python modules: you can find instructions in modpython.confg + b. copy zookeeper.pyconf in /etc/ganglia/conf.d/ + c. copy zookeeper_ganglia.py in /usr/lib/ganglia/python_plugins + d. restart the ganglia-monitor + + This is the recommended way! + +2. OR Using check_zookeeper.py and gmetric: + + Monitoring ZooKeeper using Ganglia is a simple as calling: + + ./check_zookeeper.py -o ganglia -s localhost:2181 + + on each of the ZooKeeper cluster nodes. I'm making the assumption that you have already configured gmond and installed gmetric on each node. + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/Screenshot.png b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/Screenshot.png new file mode 100644 index 0000000..bc0e41d Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/Screenshot.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/modpython.conf b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/modpython.conf new file mode 100644 index 0000000..5cd051a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/modpython.conf @@ -0,0 +1,28 @@ +/* Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. */ + +/* Update gmond.conf */ + +modules { + module { + name = "python_module" + path = "/usr/lib/ganglia/modpython.so" + params = "/usr/lib/ganglia/python_modules" + } +} + +include ('/etc/ganglia/conf.d/*.pyconf') + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/zookeeper.pyconf b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/zookeeper.pyconf new file mode 100644 index 0000000..44acd93 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/zookeeper.pyconf @@ -0,0 +1,53 @@ +/* Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. */ + +/* Update /etc/ganglia/gmond.conf with the content of this file. */ + +/* ATTENTION: Change the host and the port to meet your setup. */ + +modules { + module { + name = "zookeeper_ganglia" + language = "python" + param host { value = "127.0.0.1" } + param port { value = 2181 } + } +} + +collection_group { + collect_every = 20 + time_threshold = 60 + metric { name = "zk_avg_latency" } + metric { name = "zk_max_latency" } + metric { name = "zk_min_latency" } + metric { name = "zk_packets_received" } + metric { name = "zk_packets_sent" } + metric { name = "zk_num_alive_connections" } + metric { name = "zk_outstanding_requests" } + metric { name = "zk_znode_count" } + metric { name = "zk_watch_count" } + metric { name = "zk_ephemerals_count" } + metric { name = "zk_approximate_data_size" } + metric { name = "zk_open_file_descriptor_count" } + metric { name = "zk_max_file_descriptor_count" } + metric { name = "zk_followers" } + metric { name = "zk_synced_followers" } + metric { name = "zk_pending_syncs" } + metric { name = "zk_last_proposal_size" } + metric { name = "zk_min_proposal_size" } + metric { name = "zk_max_proposal_size" } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/zookeeper_ganglia.py b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/zookeeper_ganglia.py new file mode 100644 index 0000000..bbb7a8e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/ganglia/zookeeper_ganglia.py @@ -0,0 +1,250 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +""" Python Ganglia Module for ZooKeeper monitoring + +Inspired by: http://gist.github.com/448007 + +Copy this file to /usr/lib/ganglia/python_plugins + +""" + +import sys +import socket +import time +import re +import copy + +from StringIO import StringIO + +TIME_BETWEEN_QUERIES = 20 +ZK_METRICS = { + 'time' : 0, + 'data' : {} +} +ZK_LAST_METRICS = copy.deepcopy(ZK_METRICS) + + +class ZooKeeperServer(object): + + def __init__(self, host='localhost', port='2181', timeout=1): + self._address = (host, int(port)) + self._timeout = timeout + + def get_stats(self): + """ Get ZooKeeper server stats as a map """ + global ZK_METRICS, ZK_LAST_METRICS + # update cache + ZK_METRICS = { + 'time' : time.time(), + 'data' : {} + } + data = self._send_cmd('mntr') + if data: + parsed_data = self._parse(data) + else: + data = self._send_cmd('stat') + parsed_data = self._parse_stat(data) + ZK_METRICS['data'] = parsed_data + ZK_LAST_METRICS = copy.deepcopy(ZK_METRICS) + return parsed_data + + def _create_socket(self): + return socket.socket() + + def _send_cmd(self, cmd): + """ Send a 4letter word command to the server """ + s = self._create_socket() + s.settimeout(self._timeout) + + s.connect(self._address) + s.send(cmd) + + data = s.recv(2048) + s.close() + + return data + + def _parse(self, data): + """ Parse the output from the 'mntr' 4letter word command """ + h = StringIO(data) + + result = {} + for line in h.readlines(): + try: + key, value = self._parse_line(line) + result[key] = value + except ValueError: + pass # ignore broken lines + + return result + + def _parse_stat(self, data): + """ Parse the output from the 'stat' 4letter word command """ + global ZK_METRICS, ZK_LAST_METRICS + + h = StringIO(data) + + result = {} + + version = h.readline() + if version: + result['zk_version'] = version[version.index(':')+1:].strip() + + # skip all lines until we find the empty one + while h.readline().strip(): pass + + for line in h.readlines(): + m = re.match('Latency min/avg/max: (\d+)/(\d+)/(\d+)', line) + if m is not None: + result['zk_min_latency'] = int(m.group(1)) + result['zk_avg_latency'] = int(m.group(2)) + result['zk_max_latency'] = int(m.group(3)) + continue + + m = re.match('Received: (\d+)', line) + if m is not None: + cur_packets = int(m.group(1)) + packet_delta = cur_packets - ZK_LAST_METRICS['data'].get('zk_packets_received_total', cur_packets) + time_delta = ZK_METRICS['time'] - ZK_LAST_METRICS['time'] + time_delta = 10.0 + try: + result['zk_packets_received_total'] = cur_packets + result['zk_packets_received'] = packet_delta / float(time_delta) + except ZeroDivisionError: + result['zk_packets_received'] = 0 + continue + + m = re.match('Sent: (\d+)', line) + if m is not None: + cur_packets = int(m.group(1)) + packet_delta = cur_packets - ZK_LAST_METRICS['data'].get('zk_packets_sent_total', cur_packets) + time_delta = ZK_METRICS['time'] - ZK_LAST_METRICS['time'] + try: + result['zk_packets_sent_total'] = cur_packets + result['zk_packets_sent'] = packet_delta / float(time_delta) + except ZeroDivisionError: + result['zk_packets_sent'] = 0 + continue + + m = re.match('Outstanding: (\d+)', line) + if m is not None: + result['zk_outstanding_requests'] = int(m.group(1)) + continue + + m = re.match('Mode: (.*)', line) + if m is not None: + result['zk_server_state'] = m.group(1) + continue + + m = re.match('Node count: (\d+)', line) + if m is not None: + result['zk_znode_count'] = int(m.group(1)) + continue + + return result + + def _parse_line(self, line): + try: + key, value = map(str.strip, line.split('\t')) + except ValueError: + raise ValueError('Found invalid line: %s' % line) + + if not key: + raise ValueError('The key is mandatory and should not be empty') + + try: + value = int(value) + except (TypeError, ValueError): + pass + + return key, value + +def metric_handler(name): + if time.time() - ZK_LAST_METRICS['time'] > TIME_BETWEEN_QUERIES: + zk = ZooKeeperServer(metric_handler.host, metric_handler.port, 5) + try: + metric_handler.info = zk.get_stats() + except Exception, e: + print >>sys.stderr, e + metric_handler.info = {} + + return metric_handler.info.get(name, 0) + +def metric_init(params=None): + params = params or {} + + metric_handler.host = params.get('host', 'localhost') + metric_handler.port = int(params.get('port', 2181)) + metric_handler.timestamp = 0 + + metrics = { + 'zk_avg_latency': {'units': 'ms'}, + 'zk_max_latency': {'units': 'ms'}, + 'zk_min_latency': {'units': 'ms'}, + 'zk_packets_received': { + 'units': 'pps', + 'value_type': 'float', + 'format': '%f' + }, + 'zk_packets_sent': { + 'units': 'pps', + 'value_type': 'double', + 'format': '%f' + }, + 'zk_num_alive_connections': {'units': 'connections'}, + 'zk_outstanding_requests': {'units': 'connections'}, + 'zk_znode_count': {'units': 'znodes'}, + 'zk_watch_count': {'units': 'watches'}, + 'zk_ephemerals_count': {'units': 'znodes'}, + 'zk_approximate_data_size': {'units': 'bytes'}, + 'zk_open_file_descriptor_count': {'units': 'descriptors'}, + 'zk_max_file_descriptor_count': {'units': 'descriptors'}, + 'zk_followers': {'units': 'nodes'}, + 'zk_synced_followers': {'units': 'nodes'}, + 'zk_pending_syncs': {'units': 'syncs'}, + 'zk_last_proposal_size': {'units': 'bytes'}, + 'zk_min_proposal_size': {'units': 'bytes'}, + 'zk_max_proposal_size': {'units': 'bytes'} + } + metric_handler.descriptors = {} + for name, updates in metrics.iteritems(): + descriptor = { + 'name': name, + 'call_back': metric_handler, + 'time_max': 90, + 'value_type': 'int', + 'units': '', + 'slope': 'both', + 'format': '%d', + 'groups': 'zookeeper', + } + descriptor.update(updates) + metric_handler.descriptors[name] = descriptor + + return metric_handler.descriptors.values() + +def metric_cleanup(): + pass + + +if __name__ == '__main__': + ds = metric_init({'host':'localhost', 'port': '2181'}) + while True: + for d in ds: + print "%s=%s" % (d['name'], metric_handler(d['name'])) + time.sleep(10) + + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/README.txt b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/README.txt new file mode 100644 index 0000000..317ae14 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/README.txt @@ -0,0 +1,86 @@ +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. + +Configuration Recipe for monitoring ZooKeeper using Nagios +---------------------------------------------------------- + +I will start by making the assumption that you already have an working Nagios install. + +WARNING: I have wrote these instructions while installing and configuring the plugin on my desktop computer running Ubuntu 9.10. I've installed Nagios using apt-get. + +WARNING: You should customize the config files as suggested in order to match your Nagios and Zookeeper install. + +WARNING: This README assumes you know how to configure Nagios and how it works. + +WARNING: You should customize the warning and critical levels on service checks to meet your own needs. + +1. Install the plugin + +$ cp check_zookeeper.py /usr/lib/nagios/plugins/ + +2. Install the new commands + +$ cp zookeeper.cfg /etc/nagios-plugins/config + +3. Update the list of servers in zookeeper.cfg for the command 'check_zookeeper' and update the port for the command 'check_zk_node' (default: 2181) + +4. Create a virtual host in Nagios used for monitoring the cluster as a whole -OR- Create a hostgroup named 'zookeeper-servers' and add all the zookeeper cluster nodes. + +5. Define service checks like I have ilustrated bellow or just use the provided definitions. + +define service { + use generic-service + host_name zookeeper-cluster + service_description ... + check_command check_zookeeper!!! +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Open_File_Descriptors_Count + check_command check_zk_node!!! +} + +Ex: + +a. check the number of open file descriptors + +define service{ + use generic-service + host_name zookeeper-cluster + service_description ZK_Open_File_Descriptor_Count + check_command check_zookeeper!zk_open_file_descriptor_count!500!800 +} + +b. check the number of ephemerals nodes + +define service { + use generic-service + host_name localhost + service_description ZK_Ephemerals_Count + check_command check_zookeeper!zk_ephemerals_count!10000!100000 +} + +c. check the number of open file descriptors for each host in the group + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Open_File_Descriptors_Count + check_command check_zk_node!zk_open_file_descriptor_count!500!800 +} + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/Screenshot-1.png b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/Screenshot-1.png new file mode 100644 index 0000000..2dc55c5 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/Screenshot-1.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/Screenshot.png b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/Screenshot.png new file mode 100644 index 0000000..3219fea Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/Screenshot.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/hostgroups.cfg b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/hostgroups.cfg new file mode 100644 index 0000000..ea59a66 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/hostgroups.cfg @@ -0,0 +1,25 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# A group containing all the ZooKeeper nodes + +define hostgroup { + hostgroup_name zookeeper-servers + alias ZooKeeper Servers + members localhost +} + + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/services.cfg b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/services.cfg new file mode 100644 index 0000000..dde6ab7 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/services.cfg @@ -0,0 +1,67 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# ZooKeeper Node specific services + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Open_File_Descriptors_Count + check_command check_zk_node!zk_open_file_descriptor_count!500!800 +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Ephemerals_Count + check_command check_zk_node!zk_ephemerals_count!10000!100000 +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Avg_Latency + check_command check_zk_node!zk_avg_latency!500!1000 +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Max_Latency + check_command check_zk_node!zk_max_latency!1000!2000 +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Min_Latency + check_command check_zk_node!zk_min_latency!500!1000 +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Outstanding_Requests + check_command check_zk_node!zk_outstanding_requests!20!50 +} + +define service { + hostgroup_name zookeeper-servers + use generic-service + service_description ZK_Watch_Count + check_command check_zk_node!zk_watch_count!100!500 +} + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/zookeeper.cfg b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/zookeeper.cfg new file mode 100644 index 0000000..ed4cfab --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/nagios/zookeeper.cfg @@ -0,0 +1,30 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# 'check_zookeeper' command definition +define command { + command_name check_zookeeper + command_line /usr/lib/nagios/plugins/check_zookeeper.py -s "localhost:2181,localhost:2182,localhost:2183" -o nagios -k '$ARG1$' -w '$ARG2$' -c '$ARG3$' + # ATTENTION: you should update the list of servers defined above +} + +# 'check_zk_node' command definition +define command { + command_name check_zk_node + command_line /usr/lib/nagios/plugins/check_zookeeper.py -s $HOSTADDRESS$:2181 -o nagios -k '$ARG1$' -w '$ARG2$' -c '$ARG3$' + # ATTENTION: you should update the port. default: 2181 +} + diff --git a/zookeeper-contrib/zookeeper-contrib-monitoring/test.py b/zookeeper-contrib/zookeeper-contrib-monitoring/test.py new file mode 100644 index 0000000..3941291 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-monitoring/test.py @@ -0,0 +1,282 @@ +#! /usr/bin/env python +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import unittest +import socket +import sys + +from StringIO import StringIO + +from check_zookeeper import ZooKeeperServer, NagiosHandler, CactiHandler, GangliaHandler + +ZK_MNTR_OUTPUT = """zk_version\t3.4.0--1, built on 06/19/2010 15:07 GMT +zk_avg_latency\t1 +zk_max_latency\t132 +zk_min_latency\t0 +zk_packets_received\t640 +zk_packets_sent\t639 +zk_outstanding_requests\t0 +zk_server_state\tfollower +zk_znode_count\t4 +zk_watch_count\t0 +zk_ephemerals_count\t0 +zk_approximate_data_size\t27 +zk_open_file_descriptor_count\t22 +zk_max_file_descriptor_count\t1024 +""" + +ZK_MNTR_OUTPUT_WITH_BROKEN_LINES = """zk_version\t3.4.0 +zk_avg_latency\t23 +broken-line + +""" + +ZK_STAT_OUTPUT = """Zookeeper version: 3.3.0-943314, built on 05/11/2010 22:20 GMT +Clients: + /0:0:0:0:0:0:0:1:34564[0](queued=0,recved=1,sent=0) + +Latency min/avg/max: 0/40/121 +Received: 11 +Sent: 10 +Outstanding: 0 +Zxid: 0x700000003 +Mode: follower +Node count: 4 +""" + +class SocketMock(object): + def __init__(self): + self.sent = [] + + def settimeout(self, timeout): + self.timeout = timeout + + def connect(self, address): + self.address = address + + def send(self, data): + self.sent.append(data) + return len(data) + + def recv(self, size): + return ZK_MNTR_OUTPUT[:size] + + def close(self): pass + +class ZK33xSocketMock(SocketMock): + def __init__(self): + SocketMock.__init__(self) + self.got_stat_cmd = False + + def recv(self, size): + if 'stat' in self.sent: + return ZK_STAT_OUTPUT[:size] + else: + return '' + +class UnableToConnectSocketMock(SocketMock): + def connect(self, _): + raise socket.error('[Errno 111] Connection refused') + +def create_server_mock(socket_class): + class ZooKeeperServerMock(ZooKeeperServer): + def _create_socket(self): + return socket_class() + return ZooKeeperServerMock() + +class TestCheckZookeeper(unittest.TestCase): + + def setUp(self): + self.zk = ZooKeeperServer() + + def test_parse_valid_line(self): + key, value = self.zk._parse_line('something\t5') + + self.assertEqual(key, 'something') + self.assertEqual(value, 5) + + def test_parse_line_raises_exception_on_invalid_output(self): + invalid_lines = ['something', '', 'a\tb\tc', '\t1'] + for line in invalid_lines: + self.assertRaises(ValueError, self.zk._parse_line, line) + + def test_parser_on_valid_output(self): + data = self.zk._parse(ZK_MNTR_OUTPUT) + + self.assertEqual(len(data), 14) + self.assertEqual(data['zk_znode_count'], 4) + + def test_parse_should_ignore_invalid_lines(self): + data = self.zk._parse(ZK_MNTR_OUTPUT_WITH_BROKEN_LINES) + + self.assertEqual(len(data), 2) + + def test_parse_stat_valid_output(self): + data = self.zk._parse_stat(ZK_STAT_OUTPUT) + + result = { + 'zk_version' : '3.3.0-943314, built on 05/11/2010 22:20 GMT', + 'zk_min_latency' : 0, + 'zk_avg_latency' : 40, + 'zk_max_latency' : 121, + 'zk_packets_received': 11, + 'zk_packets_sent': 10, + 'zk_server_state': 'follower', + 'zk_znode_count': 4 + } + for k, v in result.iteritems(): + self.assertEqual(v, data[k]) + + def test_recv_valid_output(self): + zk = create_server_mock(SocketMock) + + data = zk.get_stats() + self.assertEqual(len(data), 14) + self.assertEqual(data['zk_znode_count'], 4) + + def test_socket_unable_to_connect(self): + zk = create_server_mock(UnableToConnectSocketMock) + + self.assertRaises(socket.error, zk.get_stats) + + def test_use_stat_cmd_if_mntr_is_not_available(self): + zk = create_server_mock(ZK33xSocketMock) + + data = zk.get_stats() + self.assertEqual(data['zk_version'], '3.3.0-943314, built on 05/11/2010 22:20 GMT') + +class HandlerTestCase(unittest.TestCase): + + def setUp(self): + try: + sys._stdout + except: + sys._stdout = sys.stdout + + sys.stdout = StringIO() + + def tearDown(self): + sys.stdout = sys._stdout + + def output(self): + sys.stdout.seek(0) + return sys.stdout.read() + + +class TestNagiosHandler(HandlerTestCase): + + def _analyze(self, w, c, k, stats): + class Opts(object): + warning = w + critical = c + key = k + + return NagiosHandler().analyze(Opts(), {'localhost:2181':stats}) + + def test_ok_status(self): + r = self._analyze(10, 20, 'a', {'a': 5}) + + self.assertEqual(r, 0) + self.assertEqual(self.output(), 'Ok "a"!|localhost:2181=5;10;20\n') + + r = self._analyze(20, 10, 'a', {'a': 30}) + self.assertEqual(r, 0) + + def test_warning_status(self): + r = self._analyze(10, 20, 'a', {'a': 15}) + self.assertEqual(r, 1) + self.assertEqual(self.output(), + 'Warning "a" localhost:2181!|localhost:2181=15;10;20\n') + + r = self._analyze(20, 10, 'a', {'a': 15}) + self.assertEqual(r, 1) + + def test_critical_status(self): + r = self._analyze(10, 20, 'a', {'a': 30}) + self.assertEqual(r, 2) + self.assertEqual(self.output(), + 'Critical "a" localhost:2181!|localhost:2181=30;10;20\n') + + r = self._analyze(20, 10, 'a', {'a': 5}) + self.assertEqual(r, 2) + + def test_check_a_specific_key_on_all_hosts(self): + class Opts(object): + warning = 10 + critical = 20 + key = 'latency' + + r = NagiosHandler().analyze(Opts(), { + 's1:2181': {'latency': 5}, + 's2:2181': {'latency': 15}, + 's3:2181': {'latency': 35}, + }) + self.assertEqual(r, 2) + self.assertEqual(self.output(), + 'Critical "latency" s3:2181!|s1:2181=5;10;20 '\ + 's3:2181=35;10;20 s2:2181=15;10;20\n') + +class TestCactiHandler(HandlerTestCase): + class Opts(object): + key = 'a' + leader = False + + def __init__(self, leader=False): + self.leader = leader + + def test_output_values_for_all_hosts(self): + r = CactiHandler().analyze(TestCactiHandler.Opts(), { + 's1:2181':{'a':1}, + 's2:2181':{'a':2, 'b':3} + }) + self.assertEqual(r, None) + self.assertEqual(self.output(), 's1_2181:1 s2_2181:2') + + def test_output_single_value_for_leader(self): + r = CactiHandler().analyze(TestCactiHandler.Opts(leader=True), { + 's1:2181': {'a':1, 'zk_server_state': 'leader'}, + 's2:2181': {'a':2} + }) + self.assertEqual(r, 0) + self.assertEqual(self.output(), '1\n') + + +class TestGangliaHandler(unittest.TestCase): + + class TestableGangliaHandler(GangliaHandler): + def __init__(self): + GangliaHandler.__init__(self) + self.cli_calls = [] + + def call(self, cli): + self.cli_calls.append(' '.join(cli)) + + def test_send_single_metric(self): + class Opts(object): + @property + def gmetric(self): return '/usr/bin/gmetric' + opts = Opts() + + h = TestGangliaHandler.TestableGangliaHandler() + h.analyze(opts, {'localhost:2181':{'latency':10}}) + + cmd = "%s -n latency -v 10 -t uint32" % opts.gmetric + assert cmd in h.cli_calls + +if __name__ == '__main__': + unittest.main() + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/NOTICE.txt b/zookeeper-contrib/zookeeper-contrib-rest/NOTICE.txt new file mode 100644 index 0000000..2a92254 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/NOTICE.txt @@ -0,0 +1,7 @@ +This contrib module includes software developed under the +COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 + +This contrib depends on binary only jar libraries developed at: + +https://jersey.dev.java.net/ +https://grizzly.dev.java.net/ diff --git a/zookeeper-contrib/zookeeper-contrib-rest/README.txt b/zookeeper-contrib/zookeeper-contrib-rest/README.txt new file mode 100644 index 0000000..30f2e5d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/README.txt @@ -0,0 +1,72 @@ + +ZooKeeper REST implementation using Jersey JAX-RS. +-------------------------------------------------- + +This is an implementation of version 2 of the ZooKeeper REST spec. + +Note: This interface is currently experimental, may change at any time, +etc... In general you should be using the Java/C client bindings to access +the ZooKeeper server. + +This REST ZooKeeper gateway is useful because most of the languages +have built-in support for working with HTTP based protocols. + +See SPEC.txt for details on the REST binding. + +Quickstart: +----------- + +1) start a zookeeper server on localhost port 2181 + +2) run "ant run" + +3) use a REST client to access the data (see below for more details) + + curl http://localhost:9998/znodes/v1/ + +or use the provided src/python scripts + + zk_dump_tree.py + + +Tests: +---------- + +1) the full testsuite can be run via "ant test" target +2) the python client library also contains a test suite + +Examples Using CURL +------------------- + +First review the spec SPEC.txt in this directory. + +#get the root node data +curl http://localhost:9998/znodes/v1/ + +#get children of the root node +curl http://localhost:9998/znodes/v1/?view=children + +#get "/cluster1/leader" as xml (default is json) +curl -H'Accept: application/xml' http://localhost:9998/znodes/v1/cluster1/leader + +#get the data as text +curl -w "\n%{http_code}\n" "http://localhost:9998/znodes/v1/cluster1/leader?dataformat=utf8" + +#set a node (data.txt contains the ascii text you want to set on the node) +curl -T data.txt -w "\n%{http_code}\n" "http://localhost:9998/znodes/v1/cluster1/leader?dataformat=utf8" + +#create a node +curl -d "data1" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/znodes/v1/?op=create&name=cluster2&dataformat=utf8" + +curl -d "data2" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/znodes/v1/cluster2?op=create&name=leader&dataformat=utf8" + +#create a new session +curl -d "" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/sessions/v1/?op=create&expire=10" + +#session heartbeat +curl -X "PUT" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/sessions/v1/02dfdcc8-8667-4e53-a6f8-ca5c2b495a72" + +#delete a session +curl -X "DELETE" -H'Content-Type: application/octet-stream' -w "\n%{http_code}\n" "http://localhost:9998/sessions/v1/02dfdcc8-8667-4e53-a6f8-ca5c2b495a72" + + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/SPEC.txt b/zookeeper-contrib/zookeeper-contrib-rest/SPEC.txt new file mode 100644 index 0000000..8c5f701 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/SPEC.txt @@ -0,0 +1,355 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +A REST HTTP gateway for ZooKeeper +================================= + +Specification Version: 2 + +ZooKeeper is meant to enable distributed coordination and also store +system configuration and other relatively small amounts of information +that must be stored in a persistent and consistent manner. The +information stored in ZooKeeper is meant to be highly available to a +large number of nodes in a distributed-computing cluster. + +ZooKeeper offers a client-side library that supports rich semantics +that include strict ordering guarantees on operations, the creation of +ephemeral znodes, and the ability to watch for changes to state. +However, where clients need simple "CRUD" (create, read, update, +delete) operations, the ZooKeeper libraries can be cumbersome, both to +the programmers who have to use them (who are increasingly used to +REST-style APIs), and to the operators who have to deploy and update +them (for whom deploying and updating client libraries can be very +painful). + +It turns out that most languages comes with client libraries for HTTP +that are easy and familiar to program against, and deployed as part of +the language runtime. Thus, for simple CRUD clients, an HTTP gateway +would be a less cumbersome interface than the ZooKeeper library. + +This document describes a gatway for using HTTP to interact with a +ZooKeeper repository. + +Binding ZooKeeper to HTTP +------------------------- + +Encoding +-------- + +UTF-8 unless otherwise noted + +Paths +----- + +A ZooKeeper paths are mapped to IRIs and URIs as follows. ZK paths +are converted to IRIs by simply percent-encoding any characters in the +ZK path that are not allowed in IRI paths. ZK paths are converted to +URIs by mapping them first to IRIs, then converting to URIs in the +standard way. + +Going from URIs and IRIs is the reverse of the above but for one +difference: any "." and ".." segments in an IRI or URI must be folded +before conversion. (Fortunately, ZK does not allow "." and ".." +segments in its paths.) + +ZK and IRIs recommend the same practices when it comes to Unicode +normalization: ultimately, normalization is left to application +designers, but both recommend that application designers use NFC as a +best practice. + +Root +---- + +The following examples assume that the ZooKeeper znode heirarchy is +bound to the root of the HTTP servers namespace. This may not be the +case in practice however, the gateway may bind to some prefix, for +example the URL for accessing /a/b/c may be: + + http://localhost/zookeeper/znodes/v1/a/b/c + +This is perfectly valid. Users of the REST service should be aware of +this fact and code their clients to support any root (in this case +"/zookeeper" on the server localhost). + + +Basics: GET, PUT, HEAD, and DELETE +---------------------------------- + +HTTP's GET, PUT, HEAD, and DELETE operations map naturally to +ZooKeeper's "get," "set," "exists," and "delete" operations. + +ZooKeeper znodes have a version number that changes each time the +znode's value is updated. This number is returned by "get," "set," and +"exists" operations. The "set" and "delete" operations optionally take +a version number. If one is supplied, then "set" or "delete" will fail +if the current version of the znode doesn't match the version-number +supplied in the call. This mechanism supports atomic read-modify-write +cycles. Set/delete requests may include an optional parameter +"version" which defaults to no version check. + + +Getting ZooKeeper children +-------------------------- + +We overload the GET method to return the children of a ZooKeeper. In +particular, the GET method takes an optional parameter "view" which +could be set to one of type values, either "data" or "children". The +default is "data". Thus, to get the children of a znode named +"/a/b/c", then the GET request should start: + + GET /znodes/v1/a/b/c?view=children HTTP/1.1 + +If the requested view is "data", then the data of a znode is returned +as described in the previous section. If the requested view is +"children", then a list of children is returned in either an XML +document, or in a JSON object. (The default is JSON, but this can be +controlled changed by setting the Accept header.) + + +Creating a ZooKeeper session +---------------------------- + +In order to be able to create ephemeral nodes you first need to start +a new session. + + POST /sessions/v1?op=create&expire= HTTP/1.1 + +If the session creation is successful, then a 201 code will be returned. + +A session is just an UUID that you can pass around as a parameter and +the REST server will foward your request on the attached persistent +connection. + +Keeping a session alive +----------------------- + +To keep a session alive you must send hearbeat requests: + + PUT /sessions/v1/ HTTP/1.1 + +Closing a ZooKeeper session +--------------------------- + +You can close a connection by sending a DELETE request. + + DELETE /sessions/v1/ HTTP/1.1 + +If you don't close a session it will automatically expire after +the amount of time you specified on creation. + +Creating a ZooKeeper znode +-------------------------- + +We use the POST method to create a ZooKeeper znode. For example, to +create a znode named "c" under a parent named "/a/b", then the POST +request should start: + + POST /znodes/v1/a/b?op=create&name=c HTTP/1.1 + +If the creation is successful, then a 201 code will be returned. If +it fails, then a number of different codes might be returned +(documented in a later subsection). + +ZooKeeper's create operation has a flag that tells the server to +append a sequence-number to the client-supplied znode-name in order to +make the znode-name unique. If you set this flag and ask to create a +znode named "/a/b/c", and a znode named "/a/b" already exists, then +"create" will create a znode named "/a/b/c-#" instead, where "#" is and +integer required to generate a unique name in for format %10d. + +To obtain this behavior, an additional "sequence=true" parameter +should be added to the parameters of the POST. (Note that "sequence" +is an optional parameter, that defaults to "false"; this default may +be provided explicitly if desired.) + +On success the actual path of the created znode will be returned. + +If you want to create an ephemeral node you need to specify an +additional "ephemeral=true" parameter. (Note that "ephemeral" is an optional +parameter, that defaults to "false") + +(Note: ZooKeeper also allows the client to set ACLs for the +newly-created znode. This feature is not currently supported by the +HTTP gateway to ZooKeeper.) + + +Content types and negotiation +----------------------------- + +ZooKeeper REST gateway implementations may support three content-types +for request and response messages: + +* application/octet-stream + + HEAD - returns nothing (note below: status = 204) + GET - returns the znode data as an octet-stream + PUT - send binary data, returns nothing + POST - send binary data, returns the name of the znode + DELETE - returns nothing + + For PUT and HEAD some other content-type (i.e. JSON or XML) must be + used to access the Stat information of a znode. + +* application/json, application/javascript & application/xml + + HEAD - returns nothing + GET - returns a STAT or CHILD structure + PUT - send binary data, returns a STAT structure (sans data field) + POST - send binary data, returns a PATH structure + DELETE - returns nothing + + (structures defined below) + + Results returning DATA may include an optional "dataformat" + parameter which has two possible values; base64 (default) or + utf8. This allows the caller to control the format of returned data + and may simplify usage -- for example cat'ing results to the command + line with something like curl, or accessing a url through a browser. + Care should be exercised however, if utf8 is used on non character + data errors may result. + + "application/javascript" requests may include an optional "callback" + parameter. The response is wrapped in a callback method of your + choice. e.g. appending &callback=foo to your request will result in + a response body of: foo(...). Callbacks may only contain + alphanumeric characters and underscores. + +PATH + path : string + uri: string + + path is the full path to the znode as seen by ZooKeeper + + uri is the full URI of the znode as seen by the REST server, does not + include any query parameters (i.e. it's the path to the REST resource) + +SESSION + id : string UUID + uri : string + +CHILD + PATH + child_uri_template: string + children : [ string* ] + + The children list of strings contains only the name of the child + znodes, not the full path. + + child_uri_template is a template for URI of child znodes as seen by the + REST server. e.g. "http://localhost:9998/znodes/v1/foo/{child}", where + foo is the parent node, and {child} can be substituted with the name + of each child in the children array in order to access that resource. + This template is provided to simplify child access. + +STAT + PATH + encoding : value of "base64" or "utf8" + data : base64 or utf8 encoded string + stat : + czxid : number + mzxid : number + ctime : number + mtime : number + version : number + cversion : number + aversion : number + ephemeralOwner : number + datalength : number + numChildren : number + pzxid : number + + +Error Codes +----------- + +The ZooKeeper gateway uses HTTP response codes as follows: + + * 200 (Success) - ZOK for "get" "set" "delete", "yes" case of "exists" (json/xml) + * 201 (Created) - ZOK for "create" + * 204 (No Content) - ZOK for "yes" case of "exists" (octet) + * 400 (Bad Request) - ZINVALIDACL, ZBADARGUMENTS, version param not a number + * 401 (Unauthorized) - ZAUTHFAILED + * 404 (Not Found) - ZOK for "no" case of "exists;" ZNONODE for "get," "set," and "delete" + * 409 (Conflict) - ZNODEEXISTS, ZNONODE for "create," ZNOTEMPTY, + * 412 (Precondition Failed) - ZBADVERSION + * 415 (Unsupported Media Type) - if content-type of PUT or POST is not "application/octet-stream" + * 500 (Internal Server Error) - Failure in gateway code + * 501 (Not Implemented) - HTTP method other than GET, PUT, HEAD, DELETE + * 502 (Bad Gateway) - All other ZooKeeper error codes + * 503 (Service Unavailable) - ZSESSIONEXPIRED, ZCONNECTIONLOSS, (gateway will try to reestablish the connection, but will not hold the request waiting...) + * 504 (Gateway Timeout) - ZOPERATIONTIMEOUT, or ZooKeeper does not return in a timely manner + +Note that these are the codes used by the HTTP-to-Gateway software +itself. Depending on how this software is configured into a Web +server, the resulting Web Server might behave differently, e.g., it +might do redirection, check other headers, etc. + +Error Messages +-------------- + +Error messages are returned to the caller, format is dependent on the +format requested in the call. + +* application/octet-stream + + A string containing the error message. It should include the request + and information detailing the reason for the error. + +* application/json + + { "request":"GET /a/b/c", "message":"Node doesn't exist" } + +* application/xml + + + + GET /a/b/c + Node doesn't exist + + + +Binding ZooKeeper to an HTTP server +----------------------------------- + +It might be sage to assume that everyone is happy to run an Apache +server, and thus write a "mod_zookeeper" for Apache that works only +for the Apache Web Server. However, different operational +environments prefer different Web Servers, and it would be nice to +support more than one Web server. + +Issues: + + * Configuration. + + * Defining a root: Need to provide a URL alias and associate it + with a server. Need to be able to map different aliases to + different servers (implemented via multiple ZK connections). + + * Sharing connection across multiple processes. + + * Asynchronous. + + * Adaptors. + + * Code re-use. + + +Authentication -- TBD, not currently supported + +...the config file should contain authentication material for the gateway + +...the config file should contain an ACL list to be passed along to "create" + +...would we ever want to authenticate each request to ZooKeeper?... diff --git a/zookeeper-contrib/zookeeper-contrib-rest/build.xml b/zookeeper-contrib/zookeeper-contrib-rest/build.xml new file mode 100644 index 0000000..f9cade6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/build.xml @@ -0,0 +1,189 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/README b/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/README new file mode 100644 index 0000000..085810a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/README @@ -0,0 +1,8 @@ + +In order to generate .jks (java keystore files) you need to use keytool. + +The password for the existing .jks is "123456" (without quotes). + +Some tutorials: + - http://www.mobilefish.com/tutorials/java/java_quickguide_keytool.html + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/rest.cer b/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/rest.cer new file mode 100644 index 0000000..13e5aab Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/rest.cer differ diff --git a/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/rest.jks b/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/rest.jks new file mode 100644 index 0000000..539e8be Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-rest/conf/keys/rest.jks differ diff --git a/zookeeper-contrib/zookeeper-contrib-rest/conf/log4j.properties b/zookeeper-contrib/zookeeper-contrib-rest/conf/log4j.properties new file mode 100644 index 0000000..21ba7e4 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/conf/log4j.properties @@ -0,0 +1,68 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +# + +# +# ZooKeeper Logging Configuration +# + +# Format is " (, )+ + +# DEFAULT: console appender only +log4j.rootLogger=INFO, CONSOLE + +# Example with rolling log file +#log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE + +# Example with rolling log file and tracing +#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE + +# +# Log INFO level and above messages to the console +# +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.Threshold=INFO +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} - %-5p - [%t:%C{1}@%L] - %m%n + +# +# Add ROLLINGFILE to rootLogger to get log file output +# Log DEBUG level and above messages to a log file +log4j.appender.ROLLINGFILE=org.apache.log4j.ConsoleAppender +log4j.appender.ROLLINGFILE.Threshold=DEBUG +log4j.appender.ROLLINGFILE.File=bookkeeper.log +log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout +log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} - %-5p - [%t:%C{1}@%L] - %m%n + +# Max log file size of 10MB +log4j.appender.ROLLINGFILE.MaxFileSize=10MB +# uncomment the next line to limit number of backup files +#log4j.appender.ROLLINGFILE.MaxBackupIndex=10 + +# +# Add TRACEFILE to rootLogger to get log file output +# Log DEBUG level and above messages to a log file +log4j.appender.TRACEFILE=org.apache.log4j.FileAppender +log4j.appender.TRACEFILE.Threshold=TRACE +log4j.appender.TRACEFILE.File=bookkeeper_trace.log + +log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout +### Notice we are including log4j's NDC here (%x) +log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} - %-5p [%t:%C{1}@%L][%x] - %m%n diff --git a/zookeeper-contrib/zookeeper-contrib-rest/conf/rest.properties b/zookeeper-contrib/zookeeper-contrib-rest/conf/rest.properties new file mode 100644 index 0000000..f0abb45 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/conf/rest.properties @@ -0,0 +1,70 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +# + +# +# ZooKeeper REST Gateway Configuration file +# + +rest.port = 9998 + +# +# Endpoint definition +# + +# plain configuration ; +rest.endpoint.1 = /;localhost:2181,localhost:2182 + +# ... or chrooted to /zookeeper +# rest.endpoint.1 = /;localhost:2181,localhost:2182/zookeeper + +# HTTP Basic authentication for this endpoint +# rest.endpoint.1.http.auth = root:root1 + +# create -e /a data digest:'demo:ojnHEyje6F33LLzGVzg+yatf4Fc=':cdrwa +# any session on this endpoint will use authentication +# rest.endpoint.1.zk.digest = demo:test + +# you can easily generate the ACL using Python: +# import sha; sha.sha('demo:test').digest().encode('base64').strip() + +# +# ... you can define as many endpoints as you wish +# + +# rest.endpoint.2 = /restricted;localhost:2181 +# rest.endpoint.2.http.auth = admin:pass + +# rest.endpoint.3 = /cluster1;localhost:2181,localhost:2182 +# ** you should configure one end-point for each ZooKeeper cluster +# etc. + +# Global HTTP Basic Authentication +# You should also enable HTTPS-only access +# The authentication credentials are sent as plain text + +# rest.http.auth = guest:guest1 + +# Uncomment the lines bellow to allow https-only access + +# rest.ssl = true +# rest.ssl.jks = keys/rest.jks +# rest.ssl.jks.pass = 123456 + \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-rest/ivy.xml b/zookeeper-contrib/zookeeper-contrib-rest/ivy.xml new file mode 100644 index 0000000..5ed8a9e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/ivy.xml @@ -0,0 +1,48 @@ + + + + + + + + ZooKeeper REST + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/pom.xml b/zookeeper-contrib/zookeeper-contrib-rest/pom.xml new file mode 100644 index 0000000..d193b3d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/pom.xml @@ -0,0 +1,137 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-contrib + 3.6.3 + + + zookeeper-contrib-rest + jar + Apache ZooKeeper - Contrib - Rest + + ZooKeeper REST implementation using Jersey JAX-RS. + -------------------------------------------------- + + This is an implementation of version 2 of the ZooKeeper REST spec. + + Note: This interface is currently experimental, may change at any time, + etc... In general you should be using the Java/C client bindings to access + the ZooKeeper server. + + This REST ZooKeeper gateway is useful because most of the languages + have built-in support for working with HTTP based protocols. + + See SPEC.txt for details on the REST binding. + + + + 3.1 + 1.9.8 + 1.1.5.1 + + + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.apache.zookeeper + zookeeper + ${project.version} + test-jar + test + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + * + * + + + + + log4j + log4j + + + * + * + + + + + asm + asm + ${asm.version} + + + com.sun.grizzly + grizzly-servlet-webserver + ${grizzly.version} + + + com.sun.jersey + jersey-server + ${jersey.version} + + + com.sun.jersey + jersey-json + ${jersey.version} + + + com.sun.jersey + jersey-client + ${jersey.version} + test + + + junit + junit + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + true + + + + + + \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-rest/rest.sh b/zookeeper-contrib/zookeeper-contrib-rest/rest.sh new file mode 100644 index 0000000..daa8198 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/rest.sh @@ -0,0 +1,90 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +# +# If this scripted is run out of /usr/bin or some other system bin directory +# it should be linked to and not copied. Things like java jar files are found +# relative to the canonical path of this script. +# + +# Only follow symlinks if readlink supports it +if readlink -f "$0" > /dev/null 2>&1 +then + ZKREST=`readlink -f "$0"` +else + ZKREST="$0" +fi +ZKREST_HOME=`dirname "$ZKREST"` + +if $cygwin +then + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +if [ -z $ZKREST_PIDFILE ] + then ZKREST_PIDFILE=$ZKREST_HOME/server.pid +fi + +ZKREST_MAIN=org.apache.zookeeper.server.jersey.RestMain + +ZKREST_CONF=$ZKREST_HOME/conf +ZKREST_LOG=$ZKREST_HOME/zkrest.log + +CLASSPATH="$ZKREST_CONF:$CLASSPATH" + +for i in "$ZKREST_HOME"/lib/*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +for i in "$ZKREST_HOME"/zookeeper-*.jar +do + CLASSPATH="$i:$CLASSPATH" +done + +case $1 in +start) + echo "Starting ZooKeeper REST Gateway ... " + java -cp "$CLASSPATH" $JVMFLAGS $ZKREST_MAIN >$ZKREST_LOG 2>&1 & + /bin/echo -n $! > "$ZKREST_PIDFILE" + echo STARTED + ;; +stop) + echo "Stopping ZooKeeper REST Gateway ... " + if [ ! -f "$ZKREST_PIDFILE" ] + then + echo "error: could not find file $ZKREST_PIDFILE" + exit 1 + else + $KILL -9 $(cat "$ZKREST_PIDFILE") + rm "$ZKREST_PIDFILE" + echo STOPPED + fi + ;; +restart) + shift + "$0" stop ${@} + sleep 3 + "$0" start ${@} + ;; +*) + echo "Usage: $0 {start|stop|restart}" >&2 + +esac diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/RestMain.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/RestMain.java new file mode 100644 index 0000000..209207f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/RestMain.java @@ -0,0 +1,153 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.jersey.cfg.Credentials; +import org.apache.zookeeper.server.jersey.cfg.Endpoint; +import org.apache.zookeeper.server.jersey.cfg.RestCfg; +import org.apache.zookeeper.server.jersey.filters.HTTPBasicAuth; + +import com.sun.grizzly.SSLConfig; +import com.sun.grizzly.http.embed.GrizzlyWebServer; +import com.sun.grizzly.http.servlet.ServletAdapter; +import com.sun.jersey.spi.container.servlet.ServletContainer; + +/** + * Demonstration of how to run the REST service using Grizzly + */ +public class RestMain { + + private static Logger LOG = LoggerFactory.getLogger(RestMain.class); + + private GrizzlyWebServer gws; + private RestCfg cfg; + + public RestMain(RestCfg cfg) { + this.cfg = cfg; + } + + public void start() throws IOException { + System.out.println("Starting grizzly ..."); + + boolean useSSL = cfg.useSSL(); + String zkRestResourcesTempPath = Files.createTempDirectory("zkRestResourcesTempPath").toFile().getCanonicalPath(); + gws = new GrizzlyWebServer(cfg.getPort(), zkRestResourcesTempPath, useSSL); + // BUG: Grizzly needs a doc root if you are going to register multiple adapters + + for (Endpoint e : cfg.getEndpoints()) { + ZooKeeperService.mapContext(e.getContext(), e); + gws.addGrizzlyAdapter(createJerseyAdapter(e), new String[] { e + .getContext() }); + } + + if (useSSL) { + System.out.println("Starting SSL ..."); + String jks = cfg.getJKS("keys/rest.jks"); + String jksPassword = cfg.getJKSPassword(); + + SSLConfig sslConfig = new SSLConfig(); + URL resource = getClass().getClassLoader().getResource(jks); + if (resource == null) { + LOG.error("Unable to find the keystore file: " + jks); + System.exit(2); + } + try { + sslConfig.setKeyStoreFile(new File(resource.toURI()) + .getAbsolutePath()); + } catch (URISyntaxException e1) { + LOG.error("Unable to load keystore: " + jks, e1); + System.exit(2); + } + sslConfig.setKeyStorePass(jksPassword); + gws.setSSLConfig(sslConfig); + } + + gws.start(); + } + + public void stop() { + gws.stop(); + ZooKeeperService.closeAll(); + } + + private ServletAdapter createJerseyAdapter(Endpoint e) { + ServletAdapter jersey = new ServletAdapter(); + + jersey.setServletInstance(new ServletContainer()); + jersey.addInitParameter("com.sun.jersey.config.property.packages", + "org.apache.zookeeper.server.jersey.resources"); + jersey.setContextPath(e.getContext()); + + Credentials c = Credentials.join(e.getCredentials(), cfg + .getCredentials()); + if (!c.isEmpty()) { + jersey.addFilter(new HTTPBasicAuth(c), e.getContext() + + "-basic-auth", null); + } + + return jersey; + } + + /** + * The entry point for starting the server + * + */ + public static void main(String[] args) throws Exception { + RestCfg cfg = new RestCfg("rest.properties"); + + final RestMain main = new RestMain(cfg); + main.start(); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + main.stop(); + System.out.println("Got exit request. Bye."); + } + }); + + printEndpoints(cfg); + System.out.println("Server started."); + } + + private static void printEndpoints(RestCfg cfg) { + int port = cfg.getPort(); + + for (Endpoint e : cfg.getEndpoints()) { + + String context = e.getContext(); + if (context.charAt(context.length() - 1) != '/') { + context += "/"; + } + + System.out.println(String.format( + "Started %s - WADL: http://localhost:%d%sapplication.wadl", + context, port, context)); + } + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/ZooKeeperService.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/ZooKeeperService.java new file mode 100644 index 0000000..21d27a9 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/ZooKeeperService.java @@ -0,0 +1,242 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; +import java.util.TreeSet; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.server.jersey.cfg.Endpoint; + +/** + * Singleton which provides JAX-RS resources access to the ZooKeeper client. + * There's a single session for each base uri (so usually just one). + */ +public class ZooKeeperService { + + private static Logger LOG = LoggerFactory.getLogger(ZooKeeperService.class); + + /** Map base uri to ZooKeeper host:port parameters */ + private static Map contextMap = new HashMap(); + + /** Map base uri to ZooKeeper session */ + private static Map zkMap = new HashMap(); + + /** Session timers */ + private static Map zkSessionTimers = new HashMap(); + private static Timer timer = new Timer(); + + /** Track the status of the ZooKeeper session */ + private static class MyWatcher implements Watcher { + final String contextPath; + + /** Separate watcher for each base uri */ + public MyWatcher(String contextPath) { + this.contextPath = contextPath; + } + + /** + * Track state - in particular watch for expiration. if it happens for + * re-creation of the ZK client session + */ + synchronized public void process(WatchedEvent event) { + if (event.getState() == KeeperState.Expired) { + close(contextPath); + } + } + } + + /** ZooKeeper session timer */ + private static class SessionTimerTask extends TimerTask { + + private int delay; + private String contextPath, session; + private Timer timer; + + public SessionTimerTask(int delayInSeconds, String session, + String contextPath, Timer timer) { + delay = delayInSeconds * 1000; // convert to milliseconds + this.contextPath = contextPath; + this.session = session; + this.timer = timer; + reset(); + } + + public SessionTimerTask(SessionTimerTask t) { + this(t.delay / 1000, t.session, t.contextPath, t.timer); + } + + @Override + public void run() { + if (LOG.isInfoEnabled()) { + LOG.info(String.format("Session '%s' expired after " + + "'%d' milliseconds.", session, delay)); + } + ZooKeeperService.close(contextPath, session); + } + + public void reset() { + timer.schedule(this, delay); + } + + } + + /** + * Specify ZooKeeper host:port for a particular context path. The host:port + * string is passed to the ZK client, so this can be formatted with more + * than a single host:port pair. + */ + synchronized public static void mapContext(String contextPath, Endpoint e) { + contextMap.put(contextPath, e); + } + + /** + * Reset timer for a session + */ + synchronized public static void resetTimer(String contextPath, + String session) { + if (session != null) { + String uri = concat(contextPath, session); + + SessionTimerTask t = zkSessionTimers.remove(uri); + t.cancel(); + + zkSessionTimers.put(uri, new SessionTimerTask(t)); + } + } + + /** + * Close the ZooKeeper session and remove it from the internal maps + */ + public static void close(String contextPath) { + close(contextPath, null); + } + + /** + * Close the ZooKeeper session and remove it + */ + synchronized public static void close(String contextPath, String session) { + String uri = concat(contextPath, session); + + TimerTask t = zkSessionTimers.remove(uri); + if (t != null) { + t.cancel(); + } + + ZooKeeper zk = zkMap.remove(uri); + if (zk == null) { + return; + } + try { + zk.close(); + } catch (InterruptedException e) { + LOG.error("Interrupted while closing ZooKeeper connection.", e); + } + } + + /** + * Close all the ZooKeeper sessions and remove them from the internal maps + */ + synchronized public static void closeAll() { + Set sessions = new TreeSet(zkMap.keySet()); + for (String key : sessions) { + close(key); + } + } + + /** + * Is there an active connection for this session? + */ + synchronized public static boolean isConnected(String contextPath, + String session) { + return zkMap.containsKey(concat(contextPath, session)); + } + + /** + * Return a ZooKeeper client not tied to a specific session. + */ + public static ZooKeeper getClient(String contextPath) throws IOException { + return getClient(contextPath, null); + } + + /** + * Return a ZooKeeper client for a session with a default expire time + * + * @throws IOException + */ + public static ZooKeeper getClient(String contextPath, String session) + throws IOException { + return getClient(contextPath, session, 5); + } + + /** + * Return a ZooKeeper client which may or may not be connected, but it will + * not be expired. This method can be called multiple times, the same object + * will be returned except in the case where the session expires (at which + * point a new session will be returned) + */ + synchronized public static ZooKeeper getClient(String contextPath, + String session, int expireTime) throws IOException { + final String connectionId = concat(contextPath, session); + + ZooKeeper zk = zkMap.get(connectionId); + if (zk == null) { + + if (LOG.isInfoEnabled()) { + LOG.info(String.format("creating new " + + "connection for : '%s'", connectionId)); + } + Endpoint e = contextMap.get(contextPath); + zk = new ZooKeeper(e.getHostPort(), 30000, new MyWatcher( + connectionId)); + + for (Map.Entry p : e.getZooKeeperAuthInfo().entrySet()) { + zk.addAuthInfo("digest", String.format("%s:%s", p.getKey(), + p.getValue()).getBytes()); + } + + zkMap.put(connectionId, zk); + + // a session should automatically expire after an amount of time + if (session != null) { + zkSessionTimers.put(connectionId, new SessionTimerTask( + expireTime, session, contextPath, timer)); + } + } + return zk; + } + + private static String concat(String contextPath, String session) { + if (session != null) { + return String.format("%s@%s", contextPath, session); + } + return contextPath; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/Credentials.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/Credentials.java new file mode 100644 index 0000000..0730be5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/Credentials.java @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.cfg; + +import java.util.HashMap; + +public class Credentials extends HashMap { + + public static Credentials join(Credentials a, Credentials b) { + Credentials result = new Credentials(); + result.putAll(a); + result.putAll(b); + return result; + } + + public Credentials() { + super(); + } + + public Credentials(String credentials) { + super(); + + if (!credentials.trim().equals("")) { + String[] parts = credentials.split(","); + for(String p : parts) { + String[] userPass = p.split(":"); + put(userPass[0], userPass[1]); + } + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/Endpoint.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/Endpoint.java new file mode 100644 index 0000000..2a62782 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/Endpoint.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.cfg; + +public class Endpoint { + + private String context; + private HostPortSet hostPort; + private Credentials credentials; + private Credentials zookeeperAuth; + + public Endpoint(String context, String hostPortList) { + this.context = context; + this.hostPort = new HostPortSet(hostPortList); + } + + public String getContext() { + return context; + } + + public String getHostPort() { + return hostPort.toString(); + } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(String c) { + this.credentials = new Credentials(c); + } + + public void setZooKeeperAuthInfo(String digest) { + zookeeperAuth = new Credentials(digest); + } + + public final Credentials getZooKeeperAuthInfo() { + return zookeeperAuth; + } + + @Override + public boolean equals(Object o) { + Endpoint e = (Endpoint) o; + return context.equals(e.context); + } + + @Override + public int hashCode() { + return context.hashCode(); + } + + @Override + public String toString() { + return String.format("", context, hostPort.toString()); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/HostPort.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/HostPort.java new file mode 100644 index 0000000..51a1bdd --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/HostPort.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.cfg; + +public class HostPort { + + private String host; + private int port; + + public HostPort(String hostPort) { + String[] parts = hostPort.split(":"); + host = parts[0]; + port = Integer.parseInt(parts[1]); + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + @Override + public boolean equals(Object o) { + HostPort p = (HostPort) o; + return host.equals(p.host) && port == p.port; + } + + @Override + public int hashCode() { + return String.format("%s:%d", host, port).hashCode(); + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/HostPortSet.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/HostPortSet.java new file mode 100644 index 0000000..301a565 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/HostPortSet.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.cfg; + +import java.util.HashSet; +import java.util.Set; + +public class HostPortSet { + + private Set hostPortSet = new HashSet(); + private String original; + + public HostPortSet(String hostPortList) { + original = hostPortList; + + int chrootStart = hostPortList.indexOf('/'); + String hostPortPairs; + if (chrootStart != -1) { + hostPortPairs = hostPortList.substring(0, chrootStart); + } else { + hostPortPairs = hostPortList; + } + + String[] parts = hostPortPairs.split(","); + for(String p : parts) { + hostPortSet.add(new HostPort(p)); + } + } + + @Override + public String toString() { + return original; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/RestCfg.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/RestCfg.java new file mode 100644 index 0000000..93dd632 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/cfg/RestCfg.java @@ -0,0 +1,110 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.cfg; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashSet; +import java.util.Properties; +import java.util.Set; + +public class RestCfg { + + private Properties cfg = new Properties(); + + private Set endpoints = new HashSet(); + private Credentials credentials = new Credentials(); + + public RestCfg(String resource) throws IOException { + this(RestCfg.class.getClassLoader().getResourceAsStream(resource)); + } + + public RestCfg(InputStream io) throws IOException { + try { + cfg.load(io); + extractEndpoints(); + extractCredentials(); + } finally { + io.close(); + } + } + + private void extractCredentials() { + if (cfg.containsKey("rest.http.auth")) { + credentials = new Credentials(cfg.getProperty("rest.http.auth", "")); + } + } + + private void extractEndpoints() { + int count = 1; + while (true) { + String e = cfg.getProperty( + String.format("rest.endpoint.%d", count), null); + if (e == null) { + break; + } + + String[] parts = e.split(";"); + if (parts.length != 2) { + count++; + continue; + } + Endpoint point = new Endpoint(parts[0], parts[1]); + + String c = cfg.getProperty(String.format( + "rest.endpoint.%d.http.auth", count), ""); + point.setCredentials(c); + + String digest = cfg.getProperty(String.format( + "rest.endpoint.%d.zk.digest", count), ""); + point.setZooKeeperAuthInfo(digest); + + endpoints.add(point); + count++; + } + } + + public int getPort() { + return Integer.parseInt(cfg.getProperty("rest.port", "9998")); + } + + public boolean useSSL() { + return Boolean.valueOf(cfg.getProperty("rest.ssl", "false")); + } + + public final Set getEndpoints() { + return endpoints; + } + + public final Credentials getCredentials() { + return credentials; + } + + public String getJKS() { + return cfg.getProperty("rest.ssl.jks"); + } + + public String getJKS(String def) { + return cfg.getProperty("rest.ssl.jks", def); + } + + public String getJKSPassword() { + return cfg.getProperty("rest.ssl.jks.pass"); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/filters/HTTPBasicAuth.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/filters/HTTPBasicAuth.java new file mode 100644 index 0000000..49640b5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/filters/HTTPBasicAuth.java @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.filters; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.zookeeper.server.jersey.cfg.Credentials; + +import com.sun.jersey.core.util.Base64; + +public class HTTPBasicAuth implements Filter { + + private Credentials credentials; + + public HTTPBasicAuth(Credentials c) { + credentials = c; + } + + @Override + public void doFilter(ServletRequest req0, ServletResponse resp0, + FilterChain chain) throws IOException, ServletException { + + HttpServletRequest request = (HttpServletRequest) req0; + HttpServletResponse response = (HttpServletResponse) resp0; + + String authorization = request.getHeader("Authorization"); + if (authorization != null) { + String c[] = parseAuthorization(authorization); + if (c != null && credentials.containsKey(c[0]) + && credentials.get(c[0]).equals(c[1])) { + chain.doFilter(request, response); + return; + } + } + + response.setHeader("WWW-Authenticate", "Basic realm=\"Restricted\""); + response.sendError(401); + } + + private String[] parseAuthorization(String authorization) { + String parts[] = authorization.split(" "); + if (parts.length == 2 && parts[0].equalsIgnoreCase("Basic")) { + String userPass = Base64.base64Decode(parts[1]); + + int p = userPass.indexOf(":"); + if (p != -1) { + return new String[] { userPass.substring(0, p), + userPass.substring(p + 1) }; + } + } + return null; + } + + @Override + public void init(FilterConfig arg0) throws ServletException { + } + + @Override + public void destroy() { + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZChildren.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZChildren.java new file mode 100644 index 0000000..b3fad55 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZChildren.java @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.jaxb; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * Represents the CHILD using JAXB. + * Special JSON version is required to get proper formatting in both + * JSON and XML output. See details in ZNodeResource. + */ +@XmlRootElement(name="child") +public class ZChildren { + public String path; + public String uri; + + public String child_uri_template; + @XmlElementWrapper(name="children") + @XmlElement(name="child") + public List children; + + public ZChildren() { + // needed by jersey + children = new ArrayList(); + } + + public ZChildren(String path, String uri, String child_uri_template, + List children) + { + this.path = path; + this.uri = uri; + this.child_uri_template = child_uri_template; + if (children != null) { + this.children = children; + } else { + this.children = new ArrayList(); + } + } + + @Override + public int hashCode() { + return path.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ZChildren)) { + return false; + } + ZChildren o = (ZChildren) obj; + return path.equals(o.path) && children.equals(o.children); + } + + @Override + public String toString() { + return "ZChildren(" + path + "," + children + ")"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZChildrenJSON.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZChildrenJSON.java new file mode 100644 index 0000000..0dcece0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZChildrenJSON.java @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.jaxb; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * Represents the CHILD using JAXB. + * Special JSON version is required to get proper formatting in both + * JSON and XML output. See details in ZNodeResource. + */ +@XmlRootElement(name="child") +public class ZChildrenJSON { + public String path; + public String uri; + + public String child_uri_template; + public List children; + + public ZChildrenJSON() { + // needed by jersey + children = new ArrayList(); + } + + public ZChildrenJSON(String path, String uri, String child_uri_template, + List children) + { + this.path = path; + this.uri = uri; + this.child_uri_template = child_uri_template; + if (children != null) { + this.children = children; + } else { + this.children = new ArrayList(); + } + } + + @Override + public int hashCode() { + return path.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ZChildrenJSON)) { + return false; + } + ZChildrenJSON o = (ZChildrenJSON) obj; + return path.equals(o.path) && children.equals(o.children); + } + + @Override + public String toString() { + return "ZChildrenJSON(" + path + "," + children + ")"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZError.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZError.java new file mode 100644 index 0000000..e976ee0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZError.java @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.jaxb; + +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * Represents an ERROR using JAXB. + */ +@XmlRootElement(name="error") +public class ZError { + public String request; + public String message; + + public ZError(){ + // needed by jersey + } + + public ZError(String request, String message) { + this.request = request; + this.message = message; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZPath.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZPath.java new file mode 100644 index 0000000..4d83717 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZPath.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.jaxb; + +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * Represents a PATH using JAXB. + */ +@XmlRootElement(name="path") +public class ZPath { + public String path; + public String uri; + + public ZPath(){ + // needed by jersey + } + + public ZPath(String path) { + this(path, null); + } + + public ZPath(String path, String uri) { + this.path = path; + this.uri = uri; + } + + @Override + public int hashCode() { + return path.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ZPath)) { + return false; + } + ZPath o = (ZPath) obj; + return path.equals(o.path); + } + + @Override + public String toString() { + return "ZPath(" + path + ")"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZSession.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZSession.java new file mode 100644 index 0000000..06ca9e5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZSession.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.jaxb; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name="session") +public class ZSession { + public String id; + public String uri; + + public ZSession() { + // needed by jersey + } + + public ZSession(String id, String uri) { + this.id = id; + this.uri = uri; + } + + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof ZSession)) { + return false; + } + ZSession s = (ZSession) obj; + return id.equals(s.id); + } + + @Override + public String toString() { + return "ZSession(" + id +")"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZStat.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZStat.java new file mode 100644 index 0000000..af70d18 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/jaxb/ZStat.java @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.jaxb; + +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * Represents a STAT using JAXB. + */ +@XmlRootElement(name="stat") +public class ZStat { + public String path; + public String uri; + public byte[] data64; + public String dataUtf8; + + public long czxid; + public long mzxid; + public long ctime; + public long mtime; + public int version; + public int cversion; + public int aversion; + public long ephemeralOwner; + public int dataLength; + public int numChildren; + public long pzxid; + + + public ZStat(){ + // needed by jersey + } + + public ZStat(String path, byte[] data64, String dataUtf8) + { + this.path = path; + this.data64 = data64; + this.dataUtf8 = dataUtf8; + } + + public ZStat(String path, String uri, byte[] data64, String dataUtf8, + long czxid, long mzxid, long ctime, long mtime, int version, + int cversion, int aversion, long ephemeralOwner, int dataLength, + int numChildren, long pzxid) + { + this.path = path; + this.uri = uri; + this.data64 = data64; + this.dataUtf8 = dataUtf8; + + this.czxid = czxid; + this.mzxid = mzxid; + this.ctime = ctime; + this.mtime = mtime; + this.version = version; + this.cversion = cversion; + this.aversion = aversion; + this.ephemeralOwner = ephemeralOwner; + this.dataLength = dataLength; + this.numChildren = numChildren; + this.pzxid = pzxid; + } + + @Override + public int hashCode() { + return path.hashCode(); + } + + /** + * This method considers two ZStats equal if their path, encoding, and + * data match. It does not compare the ZooKeeper + * org.apache.zookeeper.data.Stat class fields. + */ + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ZStat)) { + return false; + } + ZStat o = (ZStat) obj; + return toString().equals(o.toString()); + } + + @Override + public String toString() { + return "ZStat(" + path + "," + "b64[" + + (data64 == null ? null : new String(data64)) + "]," + + dataUtf8 + ")"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/JAXBContextResolver.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/JAXBContextResolver.java new file mode 100644 index 0000000..0893586 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/JAXBContextResolver.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.resources; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Provider; +import javax.xml.bind.JAXBContext; + +import org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON; +import org.apache.zookeeper.server.jersey.jaxb.ZPath; +import org.apache.zookeeper.server.jersey.jaxb.ZStat; + +import com.sun.jersey.api.json.JSONConfiguration; +import com.sun.jersey.api.json.JSONJAXBContext; + +/** + * Tell Jersey how to resolve JSON formatting. Specifically detail the + * fields which are arrays and which are numbers (not strings). + */ +@Provider +@SuppressWarnings("unchecked") +public final class JAXBContextResolver implements ContextResolver { + private final JAXBContext context; + + private final Set typesSet; + + public JAXBContextResolver() throws Exception { + Class[] typesArr = + new Class[]{ZPath.class, ZStat.class, ZChildrenJSON.class}; + typesSet = new HashSet(Arrays.asList(typesArr)); + context = new JSONJAXBContext( + JSONConfiguration.mapped() + .arrays("children") + .nonStrings("czxid") + .nonStrings("mzxid") + .nonStrings("ctime") + .nonStrings("mtime") + .nonStrings("version") + .nonStrings("cversion") + .nonStrings("aversion") + .nonStrings("ephemeralOwner") + .nonStrings("dataLength") + .nonStrings("numChildren") + .nonStrings("pzxid") + .build(), + typesArr); + } + + public JAXBContext getContext(Class objectType) { + return (typesSet.contains(objectType)) ? context : null; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/KeeperExceptionMapper.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/KeeperExceptionMapper.java new file mode 100644 index 0000000..fdfc27b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/KeeperExceptionMapper.java @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.resources; + +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.jersey.jaxb.ZError; + + +/** + * Map KeeperException to HTTP status codes + */ +@Provider +public class KeeperExceptionMapper implements ExceptionMapper { + private UriInfo ui; + + public KeeperExceptionMapper(@Context UriInfo ui) { + this.ui = ui; + } + + public Response toResponse(KeeperException e) { + Response.Status status; + String message; + + String path = e.getPath(); + + switch(e.code()) { + case AUTHFAILED: + status = Response.Status.UNAUTHORIZED; + message = path + " not authorized"; + break; + case BADARGUMENTS: + status = Response.Status.BAD_REQUEST; + message = path + " bad arguments"; + break; + case BADVERSION: + status = Response.Status.PRECONDITION_FAILED; + message = path + " bad version"; + break; + case INVALIDACL: + status = Response.Status.BAD_REQUEST; + message = path + " invalid acl"; + break; + case NODEEXISTS: + status = Response.Status.CONFLICT; + message = path + " already exists"; + break; + case NONODE: + status = Response.Status.NOT_FOUND; + message = path + " not found"; + break; + case NOTEMPTY: + status = Response.Status.CONFLICT; + message = path + " not empty"; + break; + default: + status = Response.Status.fromStatusCode(502); // bad gateway + message = "Error processing request for " + path + + " : " + e.getMessage(); + } + + return Response.status(status).entity( + new ZError(ui.getRequestUri().toString(), message)).build(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/RuntimeExceptionMapper.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/RuntimeExceptionMapper.java new file mode 100644 index 0000000..46f33bb --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/RuntimeExceptionMapper.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.resources; + +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import org.apache.zookeeper.server.jersey.jaxb.ZError; + +/** + * Map RuntimeException to HTTP status codes + */ +@Provider +public class RuntimeExceptionMapper + implements ExceptionMapper +{ + private UriInfo ui; + + public RuntimeExceptionMapper(@Context UriInfo ui) { + this.ui = ui; + } + + public Response toResponse(RuntimeException e) { + // don't try to handle jersey exceptions ourselves + if (e instanceof WebApplicationException) { + WebApplicationException ie =(WebApplicationException) e; + return ie.getResponse(); + } + + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( + new ZError(ui.getRequestUri().toString(), + "Error processing request due to " + e + )).build(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/SessionsResource.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/SessionsResource.java new file mode 100644 index 0000000..0744604 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/SessionsResource.java @@ -0,0 +1,135 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.resources; + +import java.io.IOException; +import java.net.URI; +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.jersey.ZooKeeperService; +import org.apache.zookeeper.server.jersey.jaxb.ZError; +import org.apache.zookeeper.server.jersey.jaxb.ZSession; + +import com.sun.jersey.api.json.JSONWithPadding; + +@Path("sessions/v1/{session: .*}") +public class SessionsResource { + + private static Logger LOG = LoggerFactory.getLogger(SessionsResource.class); + + private String contextPath; + + public SessionsResource(@Context HttpServletRequest request) { + contextPath = request.getContextPath(); + if (contextPath.equals("")) { + contextPath = "/"; + } + } + + @PUT + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML }) + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + public Response keepAliveSession(@PathParam("session") String session, + @Context UriInfo ui, byte[] data) { + + if (!ZooKeeperService.isConnected(contextPath, session)) { + throwNotFound(session, ui); + } + + ZooKeeperService.resetTimer(contextPath, session); + return Response.status(Response.Status.OK).build(); + } + + @POST + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML }) + public Response createSession(@QueryParam("op") String op, + @DefaultValue("5") @QueryParam("expire") String expire, + @Context UriInfo ui) { + if (!op.equals("create")) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).entity( + new ZError(ui.getRequestUri().toString(), "")).build()); + } + + int expireInSeconds; + try { + expireInSeconds = Integer.parseInt(expire); + } catch (NumberFormatException e) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).build()); + } + + String uuid = UUID.randomUUID().toString(); + while (ZooKeeperService.isConnected(contextPath, uuid)) { + uuid = UUID.randomUUID().toString(); + } + + // establish the connection to the ZooKeeper cluster + try { + ZooKeeperService.getClient(contextPath, uuid, expireInSeconds); + } catch (IOException e) { + LOG.error("Failed while trying to create a new session", e); + + throw new WebApplicationException(Response.status( + Response.Status.INTERNAL_SERVER_ERROR).build()); + } + + URI uri = ui.getAbsolutePathBuilder().path(uuid).build(); + return Response.created(uri).entity( + new JSONWithPadding(new ZSession(uuid, uri.toString()))) + .build(); + } + + @DELETE + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM }) + public void deleteSession(@PathParam("session") String session, + @Context UriInfo ui) { + ZooKeeperService.close(contextPath, session); + } + + private static void throwNotFound(String session, UriInfo ui) + throws WebApplicationException { + throw new WebApplicationException(Response.status( + Response.Status.NOT_FOUND).entity( + new ZError(ui.getRequestUri().toString(), session + + " not found")).build()); + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/ZErrorWriter.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/ZErrorWriter.java new file mode 100644 index 0000000..706ab89 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/ZErrorWriter.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.resources; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; + +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import javax.ws.rs.ext.Provider; + +import org.apache.zookeeper.server.jersey.jaxb.ZError; + +/** + * Tell Jersey how to format an octet response error message. + */ +@Produces(MediaType.APPLICATION_OCTET_STREAM) +@Provider +public class ZErrorWriter implements MessageBodyWriter { + + public long getSize(ZError t, Class type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return -1; + } + + public boolean isWriteable(Class type, Type genericType, + Annotation[] annotations, MediaType mediaType) { + return ZError.class.isAssignableFrom(type); + } + + public void writeTo(ZError t, Class type, Type genericType, + Annotation[] annotations, MediaType mediaType, + MultivaluedMap httpHeaders, + OutputStream os) + throws IOException, WebApplicationException + { + PrintStream p = new PrintStream(os); + p.print("Request " + t.request + " failed due to " + t.message); + p.flush(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/ZNodeResource.java b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/ZNodeResource.java new file mode 100644 index 0000000..77371ea --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/main/java/org/apache/zookeeper/server/jersey/resources/ZNodeResource.java @@ -0,0 +1,412 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey.resources; + +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.HEAD; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.jersey.ZooKeeperService; +import org.apache.zookeeper.server.jersey.jaxb.ZChildren; +import org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON; +import org.apache.zookeeper.server.jersey.jaxb.ZError; +import org.apache.zookeeper.server.jersey.jaxb.ZPath; +import org.apache.zookeeper.server.jersey.jaxb.ZStat; + +import com.sun.jersey.api.json.JSONWithPadding; + +/** + * Version 1 implementation of the ZooKeeper REST specification. + */ +// TODO test octet fully +@Path("znodes/v1{path: /.*}") +public class ZNodeResource { + private final ZooKeeper zk; + + public ZNodeResource(@DefaultValue("") @QueryParam("session") String session, + @Context UriInfo ui, + @Context HttpServletRequest request + ) + throws IOException { + + String contextPath = request.getContextPath(); + if (contextPath.equals("")) { + contextPath = "/"; + } + if (session.equals("")) { + session = null; + } else if (!ZooKeeperService.isConnected(contextPath, session)) { + throw new WebApplicationException(Response.status( + Response.Status.UNAUTHORIZED).build()); + } + zk = ZooKeeperService.getClient(contextPath, session); + } + + private void ensurePathNotNull(String path) { + if (path == null) { + throw new IllegalArgumentException("Invalid path \"" + path + "\""); + } + } + + @HEAD + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML }) + public Response existsZNode(@PathParam("path") String path, + @Context UriInfo ui) throws InterruptedException, KeeperException { + Stat stat = zk.exists(path, false); + if (stat == null) { + throwNotFound(path, ui); + } + return Response.status(Response.Status.OK).build(); + } + + @HEAD + @Produces( { MediaType.APPLICATION_OCTET_STREAM }) + public Response existsZNodeAsOctet(@PathParam("path") String path, + @Context UriInfo ui) throws InterruptedException, KeeperException { + Stat stat = zk.exists(path, false); + if (stat == null) { + throwNotFound(path, ui); + } + return Response.status(Response.Status.NO_CONTENT).build(); + } + + /* + * getZNodeList and getZNodeListJSON are bogus - but necessary. + * Unfortunately Jersey 1.0.3 is unable to render both xml and json properly + * in the case where a object contains a list/array. It's impossible to get + * it to render properly for both. As a result we need to split into two + * jaxb classes. + */ + + @GET + @Produces( { MediaType.APPLICATION_JSON, "application/javascript" }) + public Response getZNodeListJSON( + @PathParam("path") String path, + @QueryParam("callback") String callback, + @DefaultValue("data") @QueryParam("view") String view, + @DefaultValue("base64") @QueryParam("dataformat") String dataformat, + @Context UriInfo ui) throws InterruptedException, KeeperException { + return getZNodeList(true, path, callback, view, dataformat, ui); + } + + @GET + @Produces(MediaType.APPLICATION_XML) + public Response getZNodeList( + @PathParam("path") String path, + @QueryParam("callback") String callback, + @DefaultValue("data") @QueryParam("view") String view, + @DefaultValue("base64") @QueryParam("dataformat") String dataformat, + @Context UriInfo ui) throws InterruptedException, KeeperException { + return getZNodeList(false, path, callback, view, dataformat, ui); + } + + private Response getZNodeList(boolean json, String path, String callback, + String view, String dataformat, UriInfo ui) + throws InterruptedException, KeeperException { + ensurePathNotNull(path); + + if (view.equals("children")) { + List children = new ArrayList(); + for (String child : zk.getChildren(path, false)) { + children.add(child); + } + + Object child; + String childTemplate = ui.getAbsolutePath().toString(); + if (!childTemplate.endsWith("/")) { + childTemplate += "/"; + } + childTemplate += "{child}"; + if (json) { + child = new ZChildrenJSON(path, + ui.getAbsolutePath().toString(), childTemplate, + children); + } else { + child = new ZChildren(path, ui.getAbsolutePath().toString(), + childTemplate, children); + } + return Response.status(Response.Status.OK).entity( + new JSONWithPadding(child, callback)).build(); + } else { + Stat stat = new Stat(); + byte[] data = zk.getData(path, false, stat); + + byte[] data64; + String dataUtf8; + if (data == null) { + data64 = null; + dataUtf8 = null; + } else if (!dataformat.equals("utf8")) { + data64 = data; + dataUtf8 = null; + } else { + data64 = null; + dataUtf8 = new String(data); + } + ZStat zstat = new ZStat(path, ui.getAbsolutePath().toString(), + data64, dataUtf8, stat.getCzxid(), stat.getMzxid(), stat + .getCtime(), stat.getMtime(), stat.getVersion(), + stat.getCversion(), stat.getAversion(), stat + .getEphemeralOwner(), stat.getDataLength(), stat + .getNumChildren(), stat.getPzxid()); + + return Response.status(Response.Status.OK).entity( + new JSONWithPadding(zstat, callback)).build(); + } + } + + @GET + @Produces(MediaType.APPLICATION_OCTET_STREAM) + public Response getZNodeListAsOctet(@PathParam("path") String path) + throws InterruptedException, KeeperException { + ensurePathNotNull(path); + + Stat stat = new Stat(); + byte[] data = zk.getData(path, false, stat); + + if (data == null) { + return Response.status(Response.Status.NO_CONTENT).build(); + } else { + return Response.status(Response.Status.OK).entity(data).build(); + } + } + + @PUT + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML }) + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + public Response setZNode( + @PathParam("path") String path, + @QueryParam("callback") String callback, + @DefaultValue("-1") @QueryParam("version") String versionParam, + @DefaultValue("base64") @QueryParam("dataformat") String dataformat, + @DefaultValue("false") @QueryParam("null") String setNull, + @Context UriInfo ui, byte[] data) throws InterruptedException, + KeeperException { + ensurePathNotNull(path); + + int version; + try { + version = Integer.parseInt(versionParam); + } catch (NumberFormatException e) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).entity( + new ZError(ui.getRequestUri().toString(), path + + " bad version " + versionParam)).build()); + } + + if (setNull.equals("true")) { + data = null; + } + + Stat stat = zk.setData(path, data, version); + + ZStat zstat = new ZStat(path, ui.getAbsolutePath().toString(), null, + null, stat.getCzxid(), stat.getMzxid(), stat.getCtime(), stat + .getMtime(), stat.getVersion(), stat.getCversion(), + stat.getAversion(), stat.getEphemeralOwner(), stat + .getDataLength(), stat.getNumChildren(), stat + .getPzxid()); + + return Response.status(Response.Status.OK).entity( + new JSONWithPadding(zstat, callback)).build(); + } + + @PUT + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + public void setZNodeAsOctet(@PathParam("path") String path, + @DefaultValue("-1") @QueryParam("version") String versionParam, + @DefaultValue("false") @QueryParam("null") String setNull, + @Context UriInfo ui, byte[] data) throws InterruptedException, + KeeperException { + ensurePathNotNull(path); + + int version; + try { + version = Integer.parseInt(versionParam); + } catch (NumberFormatException e) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).entity( + new ZError(ui.getRequestUri().toString(), path + + " bad version " + versionParam)).build()); + } + + if (setNull.equals("true")) { + data = null; + } + + zk.setData(path, data, version); + } + + @POST + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML }) + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + public Response createZNode( + @PathParam("path") String path, + @QueryParam("callback") String callback, + @DefaultValue("create") @QueryParam("op") String op, + @QueryParam("name") String name, + @DefaultValue("base64") @QueryParam("dataformat") String dataformat, + @DefaultValue("false") @QueryParam("null") String setNull, + @DefaultValue("false") @QueryParam("sequence") String sequence, + @DefaultValue("false") @QueryParam("ephemeral") String ephemeral, + @Context UriInfo ui, byte[] data) throws InterruptedException, + KeeperException { + ensurePathNotNull(path); + + if (path.equals("/")) { + path += name; + } else { + path += "/" + name; + } + + if (!op.equals("create")) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).entity( + new ZError(ui.getRequestUri().toString(), path + + " bad operaton " + op)).build()); + } + + if (setNull.equals("true")) { + data = null; + } + + CreateMode createMode; + if (sequence.equals("true")) { + if (ephemeral.equals("false")) { + createMode = CreateMode.PERSISTENT_SEQUENTIAL; + } else { + createMode = CreateMode.EPHEMERAL_SEQUENTIAL; + } + } else if (ephemeral.equals("false")) { + createMode = CreateMode.PERSISTENT; + } else { + createMode = CreateMode.EPHEMERAL; + } + + String newPath = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); + + URI uri = ui.getAbsolutePathBuilder().path(newPath).build(); + + return Response.created(uri).entity( + new JSONWithPadding(new ZPath(newPath, ui.getAbsolutePath() + .toString()))).build(); + } + + @POST + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Consumes(MediaType.APPLICATION_OCTET_STREAM) + public Response createZNodeAsOctet(@PathParam("path") String path, + @DefaultValue("create") @QueryParam("op") String op, + @QueryParam("name") String name, + @DefaultValue("false") @QueryParam("null") String setNull, + @DefaultValue("false") @QueryParam("sequence") String sequence, + @Context UriInfo ui, byte[] data) throws InterruptedException, + KeeperException { + ensurePathNotNull(path); + + if (path.equals("/")) { + path += name; + } else { + path += "/" + name; + } + + if (!op.equals("create")) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).entity( + new ZError(ui.getRequestUri().toString(), path + + " bad operaton " + op)).build()); + } + + if (setNull.equals("true")) { + data = null; + } + + CreateMode createMode; + if (sequence.equals("true")) { + createMode = CreateMode.PERSISTENT_SEQUENTIAL; + } else { + createMode = CreateMode.PERSISTENT; + } + + String newPath = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode); + + URI uri = ui.getAbsolutePathBuilder().path(newPath).build(); + + return Response.created(uri).entity( + new ZPath(newPath, ui.getAbsolutePath().toString())).build(); + } + + @DELETE + @Produces( { MediaType.APPLICATION_JSON, "application/javascript", + MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM }) + public void deleteZNode(@PathParam("path") String path, + @DefaultValue("-1") @QueryParam("version") String versionParam, + @Context UriInfo ui) throws InterruptedException, KeeperException { + ensurePathNotNull(path); + + int version; + try { + version = Integer.parseInt(versionParam); + } catch (NumberFormatException e) { + throw new WebApplicationException(Response.status( + Response.Status.BAD_REQUEST).entity( + new ZError(ui.getRequestUri().toString(), path + + " bad version " + versionParam)).build()); + } + + zk.delete(path, version); + } + + private static void throwNotFound(String path, UriInfo ui) + throws WebApplicationException { + throw new WebApplicationException(Response.status( + Response.Status.NOT_FOUND).entity( + new ZError(ui.getRequestUri().toString(), path + " not found")) + .build()); + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/python/README.txt b/zookeeper-contrib/zookeeper-contrib-rest/src/python/README.txt new file mode 100644 index 0000000..acc8ffb --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/python/README.txt @@ -0,0 +1,9 @@ +Some basic python scripts which use the REST interface: + +zkrest.py -- basic REST ZooKeeper client +demo_master_election.py -- shows how to implement master election +demo_queue.py -- basic queue +zk_dump_tree.py -- dumps the nodes & data of a znode hierarchy + +Generally these scripts require: + * simplejson diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/python/demo_master_election.py b/zookeeper-contrib/zookeeper-contrib-rest/src/python/demo_master_election.py new file mode 100644 index 0000000..c0317c7 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/python/demo_master_election.py @@ -0,0 +1,90 @@ +#! /usr/bin/env python + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import sys +import threading +import time + +from zkrest import ZooKeeper + +class Agent(threading.Thread): + """ A basic agent that wants to become a master and exit """ + + root = '/election' + + def __init__(self, id): + super(Agent, self).__init__() + self.zk = ZooKeeper() + self.id = id + + def run(self): + print 'Starting #%s' % self.id + with self.zk.session(expire=5): + + # signal agent presence + r = self.zk.create("%s/agent-" % self.root, + sequence=True, ephemeral=True) + self.me = r['path'] + + while True: + children = sorted([el['path'] \ + for el in self.zk.get_children(self.root)]) + master, previous = children[0], None + try: + index = children.index(self.me) + if index != 0: + previous = children[index-1] + except ValueError: + break + + if previous is None: + self.do_master_work() + # and don't forget to send heartbeat messages + break + else: + # do slave work in another thread + pass + + # wait for the previous agent or current master to exit / finish + while self.zk.exists(previous) or self.zk.exists(master): + time.sleep(0.5) + self.zk.heartbeat() + + # TODO signal the slave thread to exit and wait for it + # and rerun the election loop + + def do_master_work(self): + print "#%s: I'm the master: %s" % (self.id, self.me) + +def main(): + zk = ZooKeeper() + + # create the root node used for master election + if not zk.exists('/election'): + zk.create('/election') + + print 'Starting 10 agents ...' + agents = [Agent(id) for id in range(0,15)] + + map(Agent.start, agents) + map(Agent.join, agents) + + zk.delete('/election') + +if __name__ == '__main__': + sys.exit(main()) diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/python/demo_queue.py b/zookeeper-contrib/zookeeper-contrib-rest/src/python/demo_queue.py new file mode 100644 index 0000000..9ca4c64 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/python/demo_queue.py @@ -0,0 +1,99 @@ +#! /usr/bin/env python + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + + +# This is a simple message queue built on top of ZooKeeper. In order +# to be used in production it needs better error handling but it's +# still useful as a proof-of-concept. + +# Why use ZooKeeper as a queue? Highly available by design and has +# great performance. + +import sys +import threading +import time + +from zkrest import ZooKeeper + +class Queue(object): + def __init__(self, root, zk): + self.root = root + + self.zk = zk + + def put(self, data): + self.zk.create("%s/el-" % self.root, str(data), sequence=True, ephemeral=True) + + # creating ephemeral nodes for easy cleanup + # in a real world scenario you should create + # normal sequential znodes + + def fetch(self): + """ Pull an element from the queue + + This function is not blocking if the queue is empty, it will + just return None. + """ + children = sorted(self.zk.get_children(self.root), \ + lambda a, b: cmp(a['path'], b['path'])) + + if not children: + return None + + try: + first = children[0] + self.zk.delete(first['path'], version=first['version']) + if 'data64' not in first: + return '' + else: + return first['data64'].decode('base64') + + except (ZooKeeper.WrongVersion, ZooKeeper.NotFound): + # someone changed the znode between the get and delete + # this should not happen + # in practice you should retry the fetch + raise + + +def main(): + zk = ZooKeeper() + zk.start_session(expire=60) + + if not zk.exists('/queue'): + zk.create('/queue') + q = Queue('/queue', zk) + + print 'Pushing to queue 1 ... 5' + map(q.put, [1,2,3,4,5]) + + print 'Extracting ...' + while True: + el = q.fetch() + if el is None: + break + print el + + zk.close_session() + zk.delete('/queue') + + print 'Done.' + + +if __name__ == '__main__': + sys.exit(main()) + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/python/test.py b/zookeeper-contrib/zookeeper-contrib-rest/src/python/test.py new file mode 100644 index 0000000..363747a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/python/test.py @@ -0,0 +1,163 @@ +#! /usr/bin/env python + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import time +import unittest + +from zkrest import ZooKeeper + +class ZooKeeperREST_TestCase(unittest.TestCase): + + BASE_URI = 'http://localhost:9998' + + def setUp(self): + self.zk = ZooKeeper(self.BASE_URI) + + def tearDown(self): + try: + self.zk.delete('/test') + except ZooKeeper.NotFound: + pass + + def test_get_root_node(self): + assert self.zk.get('/') is not None + + def test_get_node_not_found(self): + self.assertRaises(ZooKeeper.NotFound, \ + self.zk.get, '/dummy-node') + + def test_exists_node(self): + assert self.zk.exists('/zookeeper') is True + + def test_get_children(self): + assert any([child['path'] == '/zookeeper/quota' \ + for child in self.zk.get_children('/zookeeper')]) + + def test_create_znode(self): + try: + self.zk.create('/test') + except ZooKeeper.ZNodeExists: + pass # it's ok if already exists + assert self.zk.exists('/test') is True + + def test_create_hierarchy(self): + try: + self.zk.delete(['/a/b', '/a']) + except ZooKeeper.NotFound: + pass + + self.zk.create('/a') + self.zk.create('/a/b') + + self.zk.delete(['/a/b', '/a']) + + def test_create_with_data(self): + self.zk.create('/test', 'some-data') + + zn = self.zk.get('/test') + self.assertEqual(zn.get('data64', None), \ + 'some-data'.encode('base64').strip()) + + def test_delete_znode(self): + self.zk.create('/test') + + self.zk.delete('/test') + assert not self.zk.exists('/test') + + def test_delete_older_version(self): + self.zk.create('/test') + + zn = self.zk.get('/test') + # do one more modification in order to increase the version number + self.zk.set('/test', 'dummy-data') + + self.assertRaises(ZooKeeper.WrongVersion, \ + self.zk.delete, '/test', version=zn['version']) + + def test_delete_raise_not_found(self): + self.zk.create('/test') + + zn = self.zk.get('/test') + self.zk.delete('/test') + + self.assertRaises(ZooKeeper.NotFound, \ + self.zk.delete, '/test', version=zn['version']) + + def test_set(self): + self.zk.create('/test') + + self.zk.set('/test', 'dummy') + + self.assertEqual(self.zk.get('/test')['data64'], \ + 'dummy'.encode('base64').strip()) + + def test_set_with_older_version(self): + if not self.zk.exists('/test'): + self.zk.create('/test', 'random-data') + + zn = self.zk.get('/test') + self.zk.set('/test', 'new-data') + self.assertRaises(ZooKeeper.WrongVersion, self.zk.set, \ + '/test', 'older-version', version=zn['version']) + + def test_set_null(self): + if not self.zk.exists('/test'): + self.zk.create('/test', 'random-data') + self.zk.set('/test', 'data') + assert 'data64' in self.zk.get('/test') + + self.zk.set('/test', null=True) + assert 'data64' not in self.zk.get('/test') + + def test_create_ephemeral_node(self): + with self.zk.session(): + if self.zk.exists('/ephemeral-test'): + self.zk.delete('/ephemeral-test') + + self.zk.create('/ephemeral-test', ephemeral=True) + zn = self.zk.get('/ephemeral-test') + + assert zn['ephemeralOwner'] != 0 + + def test_create_session(self): + with self.zk.session() as sid: + self.assertEqual(len(sid), 36) # UUID + + def test_session_invalidation(self): + self.zk.start_session(expire=1) + self.zk.create('/ephemeral-test', ephemeral=True) + + # keep the session alive by sending heartbeat requests + for _ in range(1,2): + self.zk.heartbeat() + time.sleep(0.9) + + time.sleep(2) # wait for the session to expire + self.assertRaises(ZooKeeper.InvalidSession, \ + self.zk.create, '/ephemeral-test', ephemeral=True) + + def test_presence_signaling(self): + with self.zk.session(expire=1): + self.zk.create('/i-am-online', ephemeral=True) + assert self.zk.exists('/i-am-online') + assert not self.zk.exists('/i-am-online') + + +if __name__ == '__main__': + unittest.main() + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/python/zk_dump_tree.py b/zookeeper-contrib/zookeeper-contrib-rest/src/python/zk_dump_tree.py new file mode 100644 index 0000000..517d23b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/python/zk_dump_tree.py @@ -0,0 +1,108 @@ +#!/usr/bin/python + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import getopt +import sys +import simplejson +import urllib2 +from base64 import b64decode + +printdata = False +fullpath = False + +def dump_node(url, depth): + """Dump the node, then dump children recursively + + Arguments: + - `url`: + - `depth`: + """ + req = urllib2.urlopen(url) + resp = simplejson.load(req) + if 'Error' in resp: + raise resp['Error'] + + if fullpath: + name = resp['path'] + else: + name = '/' + resp['path'].split('/')[-1] + + data64 = resp.get('data64') + dataUtf8 = resp.get('dataUtf8') + if data64 and printdata: + data = b64decode(data64) + print '%(indent)s%(name)s = b64(%(data64)s) str(%(data)s)' % \ + {'indent':' '*2*depth, 'name':name, 'data64':data64, 'data':data} + elif dataUtf8 and printdata: + print '%(indent)s%(name)s = %(data)s' % \ + {'indent':' '*2*depth, 'name':name, 'data':dataUtf8} + else: + print '%(indent)s%(name)s' % {'indent':' '*2*depth, 'name':name} + + req = urllib2.urlopen(resp['uri'] + '?view=children') + resp = simplejson.load(req) + + for child in resp.get('children', []): + dump_node(resp['child_uri_template'] + .replace("{child}", urllib2.quote(child)), + depth + 1) + +def zk_dump_tree(url, root): + """Dump the tree starting at the roota + + Arguments: + - `root`: + """ + dump_node(url + '/znodes/v1' + root, 0) + +def usage(): + """Usage + """ + print 'Usage: zk_dump_tree.py [-h|--help -u|--url=url -d|--data -f|--fullpath -r|--root=root]' + print ' where url is the url of the rest server, data is whether to' + print ' to include node data on output, root is the znode root' + print ' fullpath prints the full node path (useful for copy/paste)' + +if __name__ == '__main__': + try: + opts, args = getopt.getopt(sys.argv[1:], + "hu:dfr:", ["help", "url=", "data", "fullpath", "root="]) + except getopt.GetoptError, err: + # print help information and exit: + print str(err) # will print something like "option -a not recognized" + usage() + sys.exit(2) + url ='http://localhost:9998' + root = '/' + for o, a in opts: + if o in ("-d", "--data"): + printdata = True + elif o in ("-h", "--help"): + usage() + sys.exit() + elif o in ("-u", "--url"): + url = a + elif o in ("-r", "--root"): + root = a + elif o in ("-f", "--fullpath"): + fullpath = True + else: + assert False, "unhandled option" + + print 'Accessing REST server at ' + url + zk_dump_tree(url, root) diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/python/zkrest.py b/zookeeper-contrib/zookeeper-contrib-rest/src/python/zkrest.py new file mode 100644 index 0000000..c009d5d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/python/zkrest.py @@ -0,0 +1,218 @@ + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import urllib2 +import urllib +import simplejson + +from contextlib import contextmanager + +class RequestWithMethod(urllib2.Request): + """ Request class that know how to set the method name """ + def __init__(self, *args, **kwargs): + urllib2.Request.__init__(self, *args, **kwargs) + self._method = None + + def get_method(self): + return self._method or \ + urllib2.Request.get_method(self) + + def set_method(self, method): + self._method = method + +class ZooKeeper(object): + + class Error(Exception): pass + + class NotFound(Error): pass + + class ZNodeExists(Error): pass + + class InvalidSession(Error): pass + + class WrongVersion(Error): pass + + def __init__(self, uri = 'http://localhost:9998'): + self._base = uri + self._session = None + + def start_session(self, expire=5, id=None): + """ Create a session and return the ID """ + if id is None: + url = "%s/sessions/v1/?op=create&expire=%d" % (self._base, expire) + self._session = self._do_post(url)['id'] + else: + self._session = id + return self._session + + def close_session(self): + """ Close the session on the server """ + if self._session is not None: + url = '%s/sessions/v1/%s' % (self._base, self._session) + self._do_delete(url) + self._session = None + + def heartbeat(self): + """ Send a heartbeat request. This is needed in order to keep a session alive """ + if self._session is not None: + url = '%s/sessions/v1/%s' % (self._base, self._session) + self._do_put(url, '') + + @contextmanager + def session(self, *args, **kwargs): + """ Session handling using a context manager """ + yield self.start_session(*args, **kwargs) + self.close_session() + + def get(self, path): + """ Get a node """ + url = "%s/znodes/v1%s" % (self._base, path) + return self._do_get(url) + + def get_children(self, path): + """ Get all the children for a given path. This function creates a generator """ + url = "%s/znodes/v1%s?view=children" % (self._base, path) + resp = self._do_get(url) + for child in resp.get('children', []): + try: + yield self._do_get(resp['child_uri_template']\ + .replace('{child}', urllib2.quote(child))) + except ZooKeeper.NotFound: + continue + + def create(self, path, data=None, sequence=False, ephemeral=False): + """ Create a new node. By default this call creates a persistent znode. + + You can also create an ephemeral or a sequential znode. + """ + ri = path.rindex('/') + head, name = path[:ri+1], path[ri+1:] + if head != '/': head = head[:-1] + + flags = { + 'null': 'true' if data is None else 'false', + 'ephemeral': 'true' if ephemeral else 'false', + 'sequence': 'true' if sequence else 'false' + } + if ephemeral: + if self._session: + flags['session'] = self._session + else: + raise ZooKeeper.Error, 'You need a session '\ + 'to create an ephemeral node' + flags = urllib.urlencode(flags) + + url = "%s/znodes/v1%s?op=create&name=%s&%s" % \ + (self._base, head, name, flags) + + return self._do_post(url, data) + + def set(self, path, data=None, version=-1, null=False): + """ Set the value of node """ + url = "%s/znodes/v1%s?%s" % (self._base, path, \ + urllib.urlencode({ + 'version': version, + 'null': 'true' if null else 'false' + })) + return self._do_put(url, data) + + def delete(self, path, version=-1): + """ Delete a znode """ + if type(path) is list: + map(lambda el: self.delete(el, version), path) + return + + url = '%s/znodes/v1%s?%s' % (self._base, path, \ + urllib.urlencode({ + 'version':version + })) + try: + return self._do_delete(url) + except urllib2.HTTPError, e: + if e.code == 412: + raise ZooKeeper.WrongVersion(path) + elif e.code == 404: + raise ZooKeeper.NotFound(path) + raise + + def exists(self, path): + """ Do a znode exists """ + try: + self.get(path) + return True + except ZooKeeper.NotFound: + return False + + def _do_get(self, uri): + """ Send a GET request and convert errors to exceptions """ + try: + req = urllib2.urlopen(uri) + resp = simplejson.load(req) + + if 'Error' in resp: + raise ZooKeeper.Error(resp['Error']) + + return resp + except urllib2.HTTPError, e: + if e.code == 404: + raise ZooKeeper.NotFound(uri) + raise + + def _do_post(self, uri, data=None): + """ Send a POST request and convert errors to exceptions """ + try: + req = urllib2.Request(uri, {}) + req.add_header('Content-Type', 'application/octet-stream') + if data is not None: + req.add_data(data) + + resp = simplejson.load(urllib2.urlopen(req)) + if 'Error' in resp: + raise ZooKeeper.Error(resp['Error']) + return resp + + except urllib2.HTTPError, e: + if e.code == 201: + return True + elif e.code == 409: + raise ZooKeeper.ZNodeExists(uri) + elif e.code == 401: + raise ZooKeeper.InvalidSession(uri) + raise + + def _do_delete(self, uri): + """ Send a DELETE request """ + req = RequestWithMethod(uri) + req.set_method('DELETE') + req.add_header('Content-Type', 'application/octet-stream') + return urllib2.urlopen(req).read() + + def _do_put(self, uri, data): + """ Send a PUT request """ + try: + req = RequestWithMethod(uri) + req.set_method('PUT') + req.add_header('Content-Type', 'application/octet-stream') + if data is not None: + req.add_data(data) + + return urllib2.urlopen(req).read() + except urllib2.HTTPError, e: + if e.code == 412: # precondition failed + raise ZooKeeper.WrongVersion(uri) + raise + diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/Base.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/Base.java new file mode 100644 index 0000000..924f796 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/Base.java @@ -0,0 +1,91 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.io.ByteArrayInputStream; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.JUnit4ZKTestRunner; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.server.jersey.SetTest.MyWatcher; +import org.apache.zookeeper.server.jersey.cfg.RestCfg; +import org.junit.After; +import org.junit.Before; +import org.junit.runner.RunWith; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.WebResource; + +/** + * Test stand-alone server. + * + */ +@RunWith(JUnit4ZKTestRunner.class) +public class Base { + protected static final Logger LOG = LoggerFactory.getLogger(Base.class); + + protected static final String CONTEXT_PATH = "/zk"; + protected static final int GRIZZLY_PORT = 10104; + protected static final String BASEURI = String.format( + "http://localhost:%d%s", GRIZZLY_PORT, CONTEXT_PATH); + protected static final String ZKHOSTPORT = "localhost:22182"; + protected Client client; + protected WebResource znodesr, sessionsr; + + protected ZooKeeper zk; + + private RestMain rest; + + @Before + public void setUp() throws Exception { + RestCfg cfg = new RestCfg(new ByteArrayInputStream(String.format( + "rest.port=%s\n" + + "rest.endpoint.1=%s;%s\n", + GRIZZLY_PORT, CONTEXT_PATH, ZKHOSTPORT).getBytes())); + + rest = new RestMain(cfg); + rest.start(); + + zk = new ZooKeeper(ZKHOSTPORT, 30000, new MyWatcher()); + + client = Client.create(); + znodesr = client.resource(BASEURI).path("znodes/v1"); + sessionsr = client.resource(BASEURI).path("sessions/v1/"); + } + + @After + public void tearDown() throws Exception { + client.destroy(); + zk.close(); + rest.stop(); + } + + protected static String createBaseZNode() throws Exception { + ZooKeeper zk = new ZooKeeper(ZKHOSTPORT, 30000, new MyWatcher()); + + String baseZnode = zk.create("/test-", null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + zk.close(); + + return baseZnode; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/CreateTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/CreateTest.java new file mode 100644 index 0000000..018c54b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/CreateTest.java @@ -0,0 +1,162 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; +import java.util.Collection; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.jersey.jaxb.ZPath; +import org.junit.Test; +import org.junit.Assert; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.WebResource.Builder; + + +/** + * Test stand-alone server. + * + */ +@RunWith(Parameterized.class) +public class CreateTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(CreateTest.class); + + private String accept; + private String path; + private String name; + private String encoding; + private ClientResponse.Status expectedStatus; + private ZPath expectedPath; + private byte[] data; + private boolean sequence; + + public static class MyWatcher implements Watcher { + public void process(WatchedEvent event) { + // FIXME ignore for now + } + } + + @Parameters + public static Collection data() throws Exception { + String baseZnode = Base.createBaseZNode(); + + return Arrays.asList(new Object[][] { + {MediaType.APPLICATION_JSON, + baseZnode, "foo bar", "utf8", + ClientResponse.Status.CREATED, + new ZPath(baseZnode + "/foo bar"), null, + false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t1", "utf8", + ClientResponse.Status.CREATED, new ZPath(baseZnode + "/c-t1"), + null, false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t1", "utf8", + ClientResponse.Status.CONFLICT, null, null, false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t2", "utf8", + ClientResponse.Status.CREATED, new ZPath(baseZnode + "/c-t2"), + "".getBytes(), false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t2", "utf8", + ClientResponse.Status.CONFLICT, null, null, false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t3", "utf8", + ClientResponse.Status.CREATED, new ZPath(baseZnode + "/c-t3"), + "foo".getBytes(), false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t3", "utf8", + ClientResponse.Status.CONFLICT, null, null, false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-t4", "base64", + ClientResponse.Status.CREATED, new ZPath(baseZnode + "/c-t4"), + "foo".getBytes(), false }, + {MediaType.APPLICATION_JSON, baseZnode, "c-", "utf8", + ClientResponse.Status.CREATED, new ZPath(baseZnode + "/c-"), null, + true }, + {MediaType.APPLICATION_JSON, baseZnode, "c-", "utf8", + ClientResponse.Status.CREATED, new ZPath(baseZnode + "/c-"), null, + true } + }); + } + + public CreateTest(String accept, String path, String name, String encoding, + ClientResponse.Status status, ZPath expectedPath, byte[] data, + boolean sequence) + { + this.accept = accept; + this.path = path; + this.name = name; + this.encoding = encoding; + this.expectedStatus = status; + this.expectedPath = expectedPath; + this.data = data; + this.sequence = sequence; + } + + @Test + public void testCreate() throws Exception { + WebResource wr = znodesr.path(path).queryParam("dataformat", encoding) + .queryParam("name", name); + if (data == null) { + wr = wr.queryParam("null", "true"); + } + if (sequence) { + wr = wr.queryParam("sequence", "true"); + } + + Builder builder = wr.accept(accept); + + ClientResponse cr; + if (data == null) { + cr = builder.post(ClientResponse.class); + } else { + cr = builder.post(ClientResponse.class, data); + } + Assert.assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedPath == null) { + return; + } + + ZPath zpath = cr.getEntity(ZPath.class); + if (sequence) { + Assert.assertTrue(zpath.path.startsWith(expectedPath.path)); + Assert.assertTrue(zpath.uri.startsWith(znodesr.path(path).toString())); + } else { + Assert.assertEquals(expectedPath, zpath); + Assert.assertEquals(znodesr.path(path).toString(), zpath.uri); + } + + // use out-of-band method to verify + byte[] data = zk.getData(zpath.path, false, new Stat()); + if (data == null && this.data == null) { + return; + } else if (data == null || this.data == null) { + Assert.assertEquals(data, this.data); + } else { + Assert.assertTrue(new String(data) + " == " + new String(this.data), + Arrays.equals(data, this.data)); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/DeleteTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/DeleteTest.java new file mode 100644 index 0000000..495f93b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/DeleteTest.java @@ -0,0 +1,95 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; +import java.util.Collection; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.sun.jersey.api.client.ClientResponse; + + +/** + * Test stand-alone server. + * + */ +@RunWith(Parameterized.class) +public class DeleteTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(DeleteTest.class); + + private String zpath; + private ClientResponse.Status expectedStatus; + + public static class MyWatcher implements Watcher { + public void process(WatchedEvent event) { + // FIXME ignore for now + } + } + + @Parameters + public static Collection data() throws Exception { + String baseZnode = Base.createBaseZNode(); + + return Arrays.asList(new Object[][] { + {baseZnode, baseZnode, ClientResponse.Status.NO_CONTENT }, + {baseZnode, baseZnode, ClientResponse.Status.NO_CONTENT } + }); + } + + public DeleteTest(String path, String zpath, ClientResponse.Status status) { + this.zpath = zpath; + this.expectedStatus = status; + } + + public void verify(String type) throws Exception { + if (expectedStatus != ClientResponse.Status.NOT_FOUND) { + zpath = zk.create(zpath, null, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + } + + ClientResponse cr = znodesr.path(zpath).accept(type).type(type) + .delete(ClientResponse.class); + Assert.assertEquals(expectedStatus, cr.getClientResponseStatus()); + + // use out-of-band method to verify + Stat stat = zk.exists(zpath, false); + Assert.assertNull(stat); + } + + @Test + public void testDelete() throws Exception { + verify(MediaType.APPLICATION_OCTET_STREAM); + verify(MediaType.APPLICATION_JSON); + verify(MediaType.APPLICATION_XML); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/ExistsTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/ExistsTest.java new file mode 100644 index 0000000..68b40f0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/ExistsTest.java @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; +import java.util.Collection; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.sun.jersey.api.client.ClientResponse; + + +/** + * Test stand-alone server. + * + */ +@RunWith(Parameterized.class) +public class ExistsTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(ExistsTest.class); + + private String path; + private ClientResponse.Status expectedStatus; + + @Parameters + public static Collection data() throws Exception { + String baseZnode = Base.createBaseZNode(); + + return Arrays.asList(new Object[][] { + {baseZnode, ClientResponse.Status.OK }, + {baseZnode + "dkdk38383", ClientResponse.Status.NOT_FOUND } + }); + } + + public ExistsTest(String path, ClientResponse.Status status) { + this.path = path; + this.expectedStatus = status; + } + + private void verify(String type) { + ClientResponse cr = znodesr.path(path).accept(type).type(type).head(); + if (type.equals(MediaType.APPLICATION_OCTET_STREAM) + && expectedStatus == ClientResponse.Status.OK) { + Assert.assertEquals(ClientResponse.Status.NO_CONTENT, + cr.getClientResponseStatus()); + } else { + Assert.assertEquals(expectedStatus, cr.getClientResponseStatus()); + } + } + + @Test + public void testExists() throws Exception { + verify(MediaType.APPLICATION_OCTET_STREAM); + verify(MediaType.APPLICATION_JSON); + verify(MediaType.APPLICATION_XML); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/GetChildrenTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/GetChildrenTest.java new file mode 100644 index 0000000..8f7fc98 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/GetChildrenTest.java @@ -0,0 +1,138 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.server.jersey.jaxb.ZChildren; +import org.apache.zookeeper.server.jersey.jaxb.ZChildrenJSON; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.sun.jersey.api.client.ClientResponse; + + +/** + * Test stand-alone server. + * + */ +@RunWith(Parameterized.class) +public class GetChildrenTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(GetChildrenTest.class); + + private String accept; + private String path; + private ClientResponse.Status expectedStatus; + private String expectedPath; + private List expectedChildren; + + @Parameters + public static Collection data() throws Exception { + String baseZnode = Base.createBaseZNode(); + String baseZnode2 = Base.createBaseZNode(); + String baseZnode3 = Base.createBaseZNode(); + String baseZnode4 = Base.createBaseZNode(); + String baseZnode5 = Base.createBaseZNode(); + String baseZnode6 = Base.createBaseZNode(); + + return Arrays.asList(new Object[][] { + {MediaType.APPLICATION_JSON, baseZnode + "abddkdkd", + ClientResponse.Status.NOT_FOUND, null, null }, + {MediaType.APPLICATION_XML, baseZnode + "abddkdkd", + ClientResponse.Status.NOT_FOUND, null, null }, + {MediaType.APPLICATION_JSON, baseZnode, ClientResponse.Status.OK, + baseZnode, Arrays.asList(new String[] {}) }, + {MediaType.APPLICATION_XML, baseZnode, ClientResponse.Status.OK, + baseZnode, Arrays.asList(new String[] {}) }, + {MediaType.APPLICATION_JSON, baseZnode, ClientResponse.Status.OK, + baseZnode, Arrays.asList(new String[] {"c1"}) }, + {MediaType.APPLICATION_XML, baseZnode4, ClientResponse.Status.OK, + baseZnode4, Arrays.asList(new String[] {"c1"}) }, + {MediaType.APPLICATION_JSON, baseZnode2, ClientResponse.Status.OK, + baseZnode2, Arrays.asList(new String[] {"c1", "c2"}) }, + {MediaType.APPLICATION_XML, baseZnode5, ClientResponse.Status.OK, + baseZnode5, Arrays.asList(new String[] {"c1", "c2"}) }, + {MediaType.APPLICATION_JSON, baseZnode3, ClientResponse.Status.OK, + baseZnode3, Arrays.asList(new String[] {"c1", "c2", "c3", "c4"}) }, + {MediaType.APPLICATION_XML, baseZnode6, ClientResponse.Status.OK, + baseZnode6, Arrays.asList(new String[] {"c1", "c2", "c3", "c4"}) } + + }); + } + + public GetChildrenTest(String accept, String path, ClientResponse.Status status, + String expectedPath, List expectedChildren) + { + this.accept = accept; + this.path = path; + this.expectedStatus = status; + this.expectedPath = expectedPath; + this.expectedChildren = expectedChildren; + } + + @Test + public void testGetChildren() throws Exception { + if (expectedChildren != null) { + for(String child : expectedChildren) { + zk.create(expectedPath + "/" + child, null, + Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } + + ClientResponse cr = znodesr.path(path).queryParam("view", "children") + .accept(accept).get(ClientResponse.class); + Assert.assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedChildren == null) { + return; + } + + if (accept.equals(MediaType.APPLICATION_JSON)) { + ZChildrenJSON zchildren = cr.getEntity(ZChildrenJSON.class); + Collections.sort(expectedChildren); + Collections.sort(zchildren.children); + Assert.assertEquals(expectedChildren, zchildren.children); + Assert.assertEquals(znodesr.path(path).toString(), zchildren.uri); + Assert.assertEquals(znodesr.path(path).toString() + "/{child}", + zchildren.child_uri_template); + } else if (accept.equals(MediaType.APPLICATION_XML)) { + ZChildren zchildren = cr.getEntity(ZChildren.class); + Collections.sort(expectedChildren); + Collections.sort(zchildren.children); + Assert.assertEquals(expectedChildren, zchildren.children); + Assert.assertEquals(znodesr.path(path).toString(), zchildren.uri); + Assert.assertEquals(znodesr.path(path).toString() + "/{child}", + zchildren.child_uri_template); + } else { + Assert.fail("unknown accept type"); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/GetTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/GetTest.java new file mode 100644 index 0000000..8ee1dc6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/GetTest.java @@ -0,0 +1,122 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; +import java.util.Collection; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.server.jersey.jaxb.ZStat; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.sun.jersey.api.client.ClientResponse; + + +/** + * Test stand-alone server. + * + */ +@RunWith(Parameterized.class) +public class GetTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(GetTest.class); + + private String accept; + private String path; + private String encoding; + private ClientResponse.Status expectedStatus; + private ZStat expectedStat; + + @Parameters + public static Collection data() throws Exception { + String baseZnode = Base.createBaseZNode(); + + return Arrays.asList(new Object[][] { + {MediaType.APPLICATION_JSON, baseZnode, "utf8", + ClientResponse.Status.OK, new ZStat(baseZnode, null, null) }, + {MediaType.APPLICATION_JSON, baseZnode, "utf8", + ClientResponse.Status.OK, new ZStat(baseZnode, null, "") }, + {MediaType.APPLICATION_JSON, baseZnode, "utf8", + ClientResponse.Status.OK, new ZStat(baseZnode, null, "foo") }, + {MediaType.APPLICATION_JSON, baseZnode, "base64", + ClientResponse.Status.OK, new ZStat(baseZnode, null, null) }, + {MediaType.APPLICATION_JSON, baseZnode, "base64", + ClientResponse.Status.OK, new ZStat(baseZnode, "".getBytes(), null) }, + {MediaType.APPLICATION_JSON, baseZnode, "base64", + ClientResponse.Status.OK, new ZStat(baseZnode, "".getBytes(), null) }, + {MediaType.APPLICATION_JSON, baseZnode, "base64", + ClientResponse.Status.OK, new ZStat(baseZnode, "foo".getBytes(), null) }, + {MediaType.APPLICATION_JSON, baseZnode + "abaddkdk", "utf8", + ClientResponse.Status.NOT_FOUND, null }, + {MediaType.APPLICATION_JSON, baseZnode + "abaddkdk", "base64", + ClientResponse.Status.NOT_FOUND, null }, + + {MediaType.APPLICATION_XML, baseZnode, "utf8", + ClientResponse.Status.OK, new ZStat(baseZnode, null, "foo") }, + {MediaType.APPLICATION_XML, baseZnode, "base64", + ClientResponse.Status.OK, + new ZStat(baseZnode, "foo".getBytes(), null) }, + {MediaType.APPLICATION_XML, baseZnode + "abaddkdk", "utf8", + ClientResponse.Status.NOT_FOUND, null }, + {MediaType.APPLICATION_XML, baseZnode + "abaddkdk", "base64", + ClientResponse.Status.NOT_FOUND, null } + + }); + } + + public GetTest(String accept, String path, String encoding, + ClientResponse.Status status, ZStat stat) + { + this.accept = accept; + this.path = path; + this.encoding = encoding; + this.expectedStatus = status; + this.expectedStat = stat; + } + + @Test + public void testGet() throws Exception { + if (expectedStat != null) { + if (expectedStat.data64 != null || expectedStat.dataUtf8 == null) { + zk.setData(expectedStat.path, expectedStat.data64, -1); + } else { + zk.setData(expectedStat.path, + expectedStat.dataUtf8.getBytes(), -1); + } + } + + ClientResponse cr = znodesr.path(path).queryParam("dataformat", encoding) + .accept(accept).get(ClientResponse.class); + Assert.assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedStat == null) { + return; + } + + ZStat zstat = cr.getEntity(ZStat.class); + Assert.assertEquals(expectedStat, zstat); + Assert.assertEquals(znodesr.path(path).toString(), zstat.uri); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/RestTestSuite.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/RestTestSuite.java new file mode 100644 index 0000000..fc69caf --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/RestTestSuite.java @@ -0,0 +1,42 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({WadlTest.class, GetTest.class, GetChildrenTest.class, + CreateTest.class, SetTest.class, ExistsTest.class, DeleteTest.class }) +public class RestTestSuite { + + @BeforeClass + public static void setUp() { + // suite setup + } + + @AfterClass + public static void tearDown() { + // suite setup + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/RootTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/RootTest.java new file mode 100644 index 0000000..af8f9cf --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/RootTest.java @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.jersey.jaxb.ZPath; +import org.junit.Assert; +import org.junit.Test; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.WebResource.Builder; + +/** + * Test stand-alone server. + * + */ +public class RootTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(RootTest.class); + + @Test + public void testCreate() throws Exception { + String path = "/"; + String name = "roottest-create"; + byte[] data = "foo".getBytes(); + + WebResource wr = znodesr.path(path).queryParam("dataformat", "utf8") + .queryParam("name", name); + Builder builder = wr.accept(MediaType.APPLICATION_JSON); + + ClientResponse cr; + cr = builder.post(ClientResponse.class, data); + Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); + + ZPath zpath = cr.getEntity(ZPath.class); + Assert.assertEquals(new ZPath(path + name), zpath); + Assert.assertEquals(znodesr.path(path).toString(), zpath.uri); + + // use out-of-band method to verify + byte[] rdata = zk.getData(zpath.path, false, new Stat()); + Assert.assertTrue(new String(rdata) + " == " + new String(data), + Arrays.equals(rdata, data)); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/SessionTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/SessionTest.java new file mode 100644 index 0000000..b8fc927 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/SessionTest.java @@ -0,0 +1,135 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.io.IOException; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.jersey.jaxb.ZSession; +import org.codehaus.jettison.json.JSONException; +import org.junit.Assert; +import org.junit.Test; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.WebResource.Builder; + +public class SessionTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(SessionTest.class); + + private ZSession createSession() { + return createSession("30"); + } + + private ZSession createSession(String expire) { + WebResource wr = sessionsr.queryParam("op", "create") + .queryParam("expire", expire); + Builder b = wr.accept(MediaType.APPLICATION_JSON); + + ClientResponse cr = b.post(ClientResponse.class, null); + Assert.assertEquals(ClientResponse.Status.CREATED, cr + .getClientResponseStatus()); + + return cr.getEntity(ZSession.class); + } + + @Test + public void testCreateNewSession() throws JSONException { + ZSession session = createSession(); + Assert.assertEquals(session.id.length(), 36); + + // use out-of-band method to verify + Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + } + + @Test + public void testSessionExpires() throws InterruptedException { + ZSession session = createSession("1"); + + // use out-of-band method to verify + Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + + // wait for the session to be closed + Thread.sleep(1500); + Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + } + + @Test + public void testDeleteSession() { + ZSession session = createSession("30"); + + WebResource wr = sessionsr.path(session.id); + Builder b = wr.accept(MediaType.APPLICATION_JSON); + + Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + ClientResponse cr = b.delete(ClientResponse.class, null); + Assert.assertEquals(ClientResponse.Status.NO_CONTENT, + cr.getClientResponseStatus()); + + Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + } + + @Test + public void testSendHeartbeat() throws InterruptedException { + ZSession session = createSession("2"); + + Thread.sleep(1000); + WebResource wr = sessionsr.path(session.id); + Builder b = wr.accept(MediaType.APPLICATION_JSON); + + ClientResponse cr = b.put(ClientResponse.class, null); + Assert.assertEquals(ClientResponse.Status.OK, cr.getClientResponseStatus()); + + Thread.sleep(1500); + Assert.assertTrue(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + + Thread.sleep(1000); + Assert.assertFalse(ZooKeeperService.isConnected(CONTEXT_PATH, session.id)); + } + + @Test + public void testCreateEphemeralZNode() + throws KeeperException, InterruptedException, IOException { + ZSession session = createSession("30"); + + WebResource wr = znodesr.path("/") + .queryParam("op", "create") + .queryParam("name", "ephemeral-test") + .queryParam("ephemeral", "true") + .queryParam("session", session.id) + .queryParam("null", "true"); + + Builder b = wr.accept(MediaType.APPLICATION_JSON); + ClientResponse cr = b.post(ClientResponse.class); + Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus()); + + Stat stat = new Stat(); + zk.getData("/ephemeral-test", false, stat); + + ZooKeeper sessionZK = ZooKeeperService.getClient(CONTEXT_PATH, session.id); + Assert.assertEquals(stat.getEphemeralOwner(), sessionZK.getSessionId()); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/SetTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/SetTest.java new file mode 100644 index 0000000..a86ad46 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/SetTest.java @@ -0,0 +1,154 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import java.util.Arrays; +import java.util.Collection; + +import javax.ws.rs.core.MediaType; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.jersey.jaxb.ZStat; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.WebResource.Builder; + + +/** + * Test stand-alone server. + * + */ +@RunWith(Parameterized.class) +public class SetTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(SetTest.class); + + private String accept; + private String path; + private String encoding; + private ClientResponse.Status expectedStatus; + private ZStat expectedStat; + private byte[] data; + + public static class MyWatcher implements Watcher { + public void process(WatchedEvent event) { + // FIXME ignore for now + } + } + + @Parameters + public static Collection data() throws Exception { + String baseZnode = Base.createBaseZNode(); + + return Arrays.asList(new Object[][] { + {MediaType.APPLICATION_JSON, baseZnode + "/s-t1", "utf8", + ClientResponse.Status.OK, + new ZStat(baseZnode + "/s-t1", null, null), null }, + {MediaType.APPLICATION_JSON, baseZnode + "/s-t2", "utf8", + ClientResponse.Status.OK, + new ZStat(baseZnode + "/s-t2", null, null), new byte[0] }, + {MediaType.APPLICATION_JSON, baseZnode + "/s-t3", "utf8", + ClientResponse.Status.OK, + new ZStat(baseZnode + "/s-t3", null, null), "foobar".getBytes() }, + {MediaType.APPLICATION_JSON, baseZnode + "/s-t4", "base64", + ClientResponse.Status.OK, + new ZStat(baseZnode + "/s-t4", null, null), null }, + {MediaType.APPLICATION_JSON, baseZnode + "/s-t5", "base64", + ClientResponse.Status.OK, + new ZStat(baseZnode + "/s-t5", null, null), new byte[0] }, + {MediaType.APPLICATION_JSON, baseZnode + "/s-t6", "base64", + ClientResponse.Status.OK, + new ZStat(baseZnode + "/s-t6", null, null), + "foobar".getBytes() }, + {MediaType.APPLICATION_JSON, baseZnode + "/dkdkdkd", "utf8", + ClientResponse.Status.NOT_FOUND, null, null }, + {MediaType.APPLICATION_JSON, baseZnode + "/dkdkdkd", "base64", + ClientResponse.Status.NOT_FOUND, null, null }, + }); + } + + public SetTest(String accept, String path, String encoding, + ClientResponse.Status status, ZStat expectedStat, byte[] data) + { + this.accept = accept; + this.path = path; + this.encoding = encoding; + this.expectedStatus = status; + this.expectedStat = expectedStat; + this.data = data; + } + + @Test + public void testSet() throws Exception { + if (expectedStat != null) { + zk.create(expectedStat.path, "initial".getBytes(), Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + + WebResource wr = znodesr.path(path).queryParam("dataformat", encoding); + if (data == null) { + wr = wr.queryParam("null", "true"); + } + + Builder builder = wr.accept(accept) + .type(MediaType.APPLICATION_OCTET_STREAM); + + ClientResponse cr; + if (data == null) { + cr = builder.put(ClientResponse.class); + } else { + // this shouldn't be necessary (wrapping data with string) + // but without it there are problems on the server - ie it + // hangs for 30 seconds and doesn't get the data. + // TODO investigate + cr = builder.put(ClientResponse.class, new String(data)); + } + Assert.assertEquals(expectedStatus, cr.getClientResponseStatus()); + + if (expectedStat == null) { + return; + } + + ZStat zstat = cr.getEntity(ZStat.class); + Assert.assertEquals(expectedStat, zstat); + + // use out-of-band method to verify + byte[] data = zk.getData(zstat.path, false, new Stat()); + if (data == null && this.data == null) { + return; + } else if (data == null || this.data == null) { + Assert.fail((data == null ? null : new String(data)) + " == " + + (this.data == null ? null : new String(this.data))); + } else { + Assert.assertTrue(new String(data) + " == " + new String(this.data), + Arrays.equals(data, this.data)); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/WadlTest.java b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/WadlTest.java new file mode 100644 index 0000000..c3b10c0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/java/org/apache/zookeeper/server/jersey/WadlTest.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.jersey; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.junit.Assert; +import org.junit.Test; + +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.core.header.MediaTypes; + + +/** + * Test stand-alone server. + * + */ +public class WadlTest extends Base { + protected static final Logger LOG = LoggerFactory.getLogger(WadlTest.class); + + @Test + public void testApplicationWadl() { + WebResource r = client.resource(BASEURI); + String serviceWadl = r.path("application.wadl"). + accept(MediaTypes.WADL).get(String.class); + Assert.assertTrue("Something wrong. Returned wadl length not > 0.", + serviceWadl.length() > 0); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-rest/src/test/zkServer.sh b/zookeeper-contrib/zookeeper-contrib-rest/src/test/zkServer.sh new file mode 100644 index 0000000..4279d34 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-rest/src/test/zkServer.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +if [ "x$1" == "x" ] +then + echo "USAGE: $0 startClean|start|stop hostPorts" + exit 2 +fi + +if [ "x$1" == "xstartClean" ] +then + if [ "x${base_dir}" == "x" ] + then + rm -rf /tmp/zkdata + else + rm -rf ${base_dir}/build/tmp + fi +fi + +# Make sure nothing is left over from before +if [ -r "/tmp/zk.pid" ] +then +pid=`cat /tmp/zk.pid` +kill -9 $pid +rm -f /tmp/zk.pid +fi + +if [ -r "${base_dir}/build/tmp/zk.pid" ] +then +pid=`cat ${base_dir}/build/tmp/zk.pid` +kill -9 $pid +rm -f ${base_dir}/build/tmp/zk.pid +fi + +if [ "x${base_dir}" == "x" ] +then +zk_base="../../../" +else +zk_base="${base_dir}" +fi + +CLASSPATH="$CLASSPATH:${zk_base}/build/classes" +CLASSPATH="$CLASSPATH:${zk_base}/conf" + +for i in "${zk_base}"/build/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +for i in "${zk_base}"/zookeeper-server/src/main/resource/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +case $1 in +start|startClean) + if [ "x${base_dir}" == "x" ] + then + mkdir -p /tmp/zkdata + java -cp $CLASSPATH org.apache.zookeeper.server.ZooKeeperServerMain 22182 /tmp/zkdata &> /tmp/zk.log & + echo $! > /tmp/zk.pid + else + mkdir -p ${base_dir}/build/tmp/zkdata + java -cp $CLASSPATH org.apache.zookeeper.server.ZooKeeperServerMain 22182 ${base_dir}/build/tmp/zkdata &> ${base_dir}/build/tmp/zk.log & + echo $! > ${base_dir}/build/tmp/zk.pid + fi + sleep 5 + ;; +stop) + # Already killed above + ;; +*) + echo "Unknown command " + $1 + exit 2 +esac + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/Makefile.am b/zookeeper-contrib/zookeeper-contrib-zkfuse/Makefile.am new file mode 100644 index 0000000..36da1a5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/Makefile.am @@ -0,0 +1,4 @@ +## Process this file with automake to produce Makefile.in + +SUBDIRS = src + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/README.txt b/zookeeper-contrib/zookeeper-contrib-zkfuse/README.txt new file mode 100644 index 0000000..901d363 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/README.txt @@ -0,0 +1,63 @@ +Original authors of zkfuse are Swee Lim & Bartlomiej M Niechwiej of Yahoo. +' +ZooKeeper FUSE (File System in Userspace) +========================================= + +Pre-requisites +-------------- +1. Linux system with 2.6.X kernel. +2. Fuse (Filesystem in Userspace) must be installed on the build node. +3. Development build libraries: + a. fuse + b. log4cxx + c. pthread + d. boost + +Build instructions +------------------ +1. cd into this directory +2. autoreconf -if +3. ./configure +4. make +5. zkfuse binary is under the src directory + +Testing Zkfuse +-------------- +1. Depending on permission on /dev/fuse, you may need to sudo -u root. + * If /dev/fuse has permissions 0600, then you have to run Zkfuse as root. + * If /dev/fuse has permissions 0666, then you can run Zkfuse as any user. +2. Create or find a mount point that you have "rwx" permission. + * e.g. mkdir -p /tmp/zkfuse +3. Run Zkfuse as follows: + zkfuse -z -m /tmp/zkfuse -d + -z specifies ZooKeeper address(es) : + -m specifies the mount point + -d specifies the debug mode. + For additional command line options, try "zkfuse -h". + +FAQ +--- +Q. How to fix "warning: macro `AM_PATH_CPPUNIT' not found in library"? +A. * install cppunit (src or pkg) on build machine + +Q. Why can't Zkfuse cannot write to current directory? +A. * If Zkfuse is running as root on a NFS mounted file system, it will not + have root permissions because root user is mapped to another user by + NFS admin. + * If you run Zkfuse as root, it is a good idea to run Zkfuse from a + directory that you have write access to. This will allow core files + to be saved. + +Q. Why Zkfuse cannot mount? +A. * Check that the mount point exists and you have "rwx" permissions. + * Check that previous mounts have been umounted. If Zkfuse does not + exit cleanly, its mount point may have to be umounted manually. + If you cannot umount manually, make sure that there no files is open + within the mount point. + +Q. Why does Zkfuse complain about logging at startup? +A. * Zkfuse uses log4cxx for logging. It is looking for log4cxx.properties + file to obtain its logging configuration. + * There is an example log4cxx.properties file in the Zkfuse source + directory. + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/build.xml b/zookeeper-contrib/zookeeper-contrib-zkfuse/build.xml new file mode 100644 index 0000000..7104004 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/configure.ac b/zookeeper-contrib/zookeeper-contrib-zkfuse/configure.ac new file mode 100644 index 0000000..c86e7cd --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/configure.ac @@ -0,0 +1,72 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) + +AC_INIT([zkfuse], [2.2.0]) +AM_INIT_AUTOMAKE(foreign) + +AC_CONFIG_SRCDIR([src/zkadapter.h]) +AM_CONFIG_HEADER([config.h]) + +PACKAGE=zkfuse +VERSION=1.0 + +AC_SUBST(PACKAGE) +AC_SUBST(VERSION) + +BUILD_PATH="`pwd`" + +# Checks for programs. +AC_LANG_CPLUSPLUS +AC_PROG_CXX + +# Checks for libraries. +AC_CHECK_LIB([fuse], [main]) +AC_CHECK_LIB([log4cxx], [main], [], [AC_MSG_ERROR("We need log4cxx to build zkfuse")]) +AC_CHECK_LIB([thread], [thr_create]) +AC_CHECK_LIB([pthread], [pthread_create]) +AC_CHECK_LIB([rt], [clock_gettime]) +AC_CHECK_LIB([socket], [socket]) +AC_CHECK_LIB([nsl], [gethostbyname]) +AC_CHECK_LIB([ulockmgr], [ulockmgr_op]) + +ZOOKEEPER_PATH=${BUILD_PATH}/../../c +ZOOKEEPER_BUILD_PATH=${BUILD_PATH}/../../../build/c +AC_CHECK_LIB(zookeeper_mt, main, [ZOOKEEPER_LD="-L${ZOOKEEPER_BUILD_PATH}/.libs -lzookeeper_mt"],,["-L${ZOOKEEPER_BUILD_PATH}/.libs"]) + +AC_SUBST(ZOOKEEPER_PATH) +AC_SUBST(ZOOKEEPER_LD) + +# Checks for header files. +AC_HEADER_DIRENT +AC_HEADER_STDC +AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/time.h unistd.h]) +AC_CHECK_HEADERS([boost/shared_ptr.hpp boost/utility.hpp boost/weak_ptr.hpp],, AC_MSG_ERROR([boost library headers not found. Please install boost library.])) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_CONST +AC_TYPE_UID_T +AC_C_INLINE +AC_TYPE_INT32_T +AC_TYPE_INT64_T +AC_TYPE_MODE_T +AC_TYPE_OFF_T +AC_TYPE_SIZE_T +AC_CHECK_MEMBERS([struct stat.st_blksize]) +AC_STRUCT_ST_BLOCKS +AC_HEADER_TIME +AC_TYPE_UINT32_T +AC_TYPE_UINT64_T +AC_TYPE_UINT8_T +AC_C_VOLATILE + +# Checks for library functions. +AC_FUNC_UTIME_NULL +AC_CHECK_FUNCS([gettimeofday memset mkdir rmdir strdup strerror strstr strtol strtoul strtoull utime]) + +AC_CONFIG_FILES([Makefile]) +AC_CONFIG_FILES([src/Makefile]) +AC_OUTPUT +AC_C_VOLATILE diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/Makefile.am b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/Makefile.am new file mode 100644 index 0000000..c0d87e3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/Makefile.am @@ -0,0 +1,7 @@ +AM_CXXFLAGS = -I${ZOOKEEPER_PATH}/include -I${ZOOKEEPER_PATH}/generated \ + -I$(top_srcdir)/include -I/usr/include -D_FILE_OFFSET_BITS=64 -D_REENTRANT + +noinst_PROGRAMS = zkfuse + +zkfuse_SOURCES = zkfuse.cc zkadapter.cc thread.cc log.cc +zkfuse_LDADD = ${ZOOKEEPER_LD} \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/blockingqueue.h b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/blockingqueue.h new file mode 100644 index 0000000..4677290 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/blockingqueue.h @@ -0,0 +1,154 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __BLOCKINGQUEUE_H__ +#define __BLOCKINGQUEUE_H__ + +#include + +#include "mutex.h" + +using namespace std; +USING_ZKFUSE_NAMESPACE + +namespace zk { + +/** + * \brief An unbounded blocking queue of elements of type E. + * + *

+ * This class is thread safe. + */ +template +class BlockingQueue { + public: + + /** + * \brief Adds the specified element to this queue, waiting if necessary + * \brief for space to become available. + * + * @param e the element to be added + */ + void put(E e); + + /** + * \brief Retrieves and removes the head of this queue, waiting if + * \brief no elements are present in this queue. + * + * @param timeout how long to wait until an element becomes availabe, + * in milliseconds; if 0 then wait forever + * @param timedOut if not NULL then set to true whether this function timed out + * @return the element from the queue + */ + E take(int32_t timeout = 0, bool *timedOut = NULL); + + /** + * Returns the current size of this blocking queue. + * + * @return the number of elements in this queue + */ + int size() const; + + /** + * \brief Returns whether this queue is empty or not. + * + * @return true if this queue has no elements; false otherwise + */ + bool empty() const; + + private: + + /** + * The queue of elements. Deque is used to provide O(1) time + * for head elements removal. + */ + deque m_queue; + + /** + * The mutex used for queue synchronization. + */ + mutable zkfuse::Mutex m_mutex; + + /** + * The conditionial variable associated with the mutex above. + */ + mutable Cond m_cond; + +}; + +template +int BlockingQueue::size() const { + int size; + m_mutex.Acquire(); + size = m_queue.size(); + m_mutex.Release(); + return size; +} + +template +bool BlockingQueue::empty() const { + bool isEmpty; + m_mutex.Acquire(); + isEmpty = m_queue.empty(); + m_mutex.Release(); + return isEmpty; +} + +template +void BlockingQueue::put(E e) { + m_mutex.Acquire(); + m_queue.push_back( e ); + m_cond.Signal(); + m_mutex.Release(); +} + +template + E BlockingQueue::take(int32_t timeout, bool *timedOut) { + m_mutex.Acquire(); + bool hasResult = true; + while (m_queue.empty()) { + if (timeout <= 0) { + m_cond.Wait( m_mutex ); + } else { + if (!m_cond.Wait( m_mutex, timeout )) { + hasResult = false; + break; + } + } + } + if (hasResult) { + E e = m_queue.front(); + m_queue.pop_front(); + m_mutex.Release(); + if (timedOut) { + *timedOut = false; + } + return e; + } else { + m_mutex.Release(); + if (timedOut) { + *timedOut = true; + } + return E(); + } +} + +} + +#endif /* __BLOCKINGQUEUE_H__ */ + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/doxygen.cfg b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/doxygen.cfg new file mode 100644 index 0000000..308b094 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/doxygen.cfg @@ -0,0 +1,1242 @@ +# Doxyfile 1.4.3 + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = ZkFuse + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = doc + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Brazilian, Catalan, Chinese, Chinese-Traditional, Croatian, Czech, Danish, +# Dutch, Finnish, French, German, Greek, Hungarian, Italian, Japanese, +# Japanese-en (Japanese with English messages), Korean, Korean-en, Norwegian, +# Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovene, Spanish, +# Swedish, and Ukrainian. + +OUTPUT_LANGUAGE = English + +# This tag can be used to specify the encoding used in the generated output. +# The encoding is not always determined by the language that is chosen, +# but also whether or not the output is meant for Windows or non-Windows users. +# In case there is a difference, setting the USE_WINDOWS_ENCODING tag to YES +# forces the Windows encoding (this is the default for the Windows binary), +# whereas setting the tag to NO uses a Unix-style encoding (the default for +# all platforms other than Windows). + +USE_WINDOWS_ENCODING = NO + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like the Qt-style comments (thus requiring an +# explicit @brief command for a brief description. + +JAVADOC_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the DETAILS_AT_TOP tag is set to YES then Doxygen +# will output the detailed description near the top, like JavaDoc. +# If set to NO, the detailed description appears after the member +# documentation. + +DETAILS_AT_TOP = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = NO + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java sources +# only. Doxygen will then generate output that is more tailored for Java. +# For instance, namespaces will be presented as packages, qualified scopes +# will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = YES + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = YES + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = YES + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST= YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. + +SHOW_DIRECTORIES = YES + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from the +# version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the progam writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm + +FILE_PATTERNS = + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. + +EXCLUDE_PATTERNS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES (the default) +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES (the default) +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = YES + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compressed HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = NO + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# If the GENERATE_TREEVIEW tag is set to YES, a side panel will be +# generated containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (for instance Mozilla 1.0+, +# Netscape 6.0+, Internet explorer 5.0+, or Konqueror). Windows users are +# probably better off using the HTML help feature. + +GENERATE_TREEVIEW = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = YES + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = a4wide + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = NO + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = NO + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = NO + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = NO + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = NO + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. This is useful +# if you want to understand what is going on. On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_PREDEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = NO + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a call dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable call graphs for selected +# functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The MAX_DOT_GRAPH_WIDTH tag can be used to set the maximum allowed width +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_WIDTH = 1024 + +# The MAX_DOT_GRAPH_HEIGHT tag can be used to set the maximum allows height +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_HEIGHT = 1024 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that a graph may be further truncated if the graph's +# image dimensions are not sufficient to fit the graph (see MAX_DOT_GRAPH_WIDTH +# and MAX_DOT_GRAPH_HEIGHT). If 0 is used for the depth value (the default), +# the graph is not depth-constrained. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, which results in a white background. +# Warning: Depending on the platform used, enabling this option may lead to +# badly anti-aliased labels on the edges of a graph (i.e. they become hard to +# read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- + +# The SEARCHENGINE tag specifies whether or not a search engine should be +# used. If set to NO the values of all tags below this one will be ignored. + +SEARCHENGINE = NO diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/event.cc b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/event.cc new file mode 100644 index 0000000..541657e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/event.cc @@ -0,0 +1,29 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "event.h" + +#define LOG_LEVEL LOG_FATAL +#define MODULE_NAME "Event" + +using namespace std; + +namespace zkfuse { + +} /* end of 'namespace zkfuse' */ + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/event.h b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/event.h new file mode 100644 index 0000000..936ecc6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/event.h @@ -0,0 +1,553 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __EVENT_H__ +#define __EVENT_H__ + +#include +#include +#include +#include +#ifdef GCC4 +# include +using namespace std::tr1; +#else +# include +using namespace boost; +#endif + +#include "log.h" +#include "blockingqueue.h" +#include "mutex.h" +#include "thread.h" + +using namespace std; +using namespace zk; + +namespace zkfuse { + +//forward declaration of EventSource +template +class EventSource; + +/** + * \brief This interface is implemented by an observer + * \brief of a particular {@link EventSource}. + */ +template +class EventListener { + public: + + /** + * \brief This method is invoked whenever an event + * \brief has been received by the event source being observed. + * + * @param source the source the triggered the event + * @param e the actual event being triggered + */ + virtual void eventReceived(const EventSource &source, const E &e) = 0; +}; + +/** + * \brief This class represents a source of events. + * + *

+ * Each source can have many observers (listeners) attached to it + * and in case of an event, this source may propagate the event + * using {@link #fireEvent} method. + */ +template +class EventSource { + public: + + /** + * \brief The type corresponding to the list of registered event listeners. + */ + typedef set *> EventListeners; + + /** + * \brief Registers a new event listener. + * + * @param listener the listener to be added to the set of listeners + */ + void addListener(EventListener *listener) { + m_listeners.insert( listener ); + } + + /** + * \brief Removes an already registered listener. + * + * @param listener the listener to be removed + */ + void removeListener(EventListener *listener) { + m_listeners.erase( listener ); + } + + /** + * \brief Destructor. + */ + virtual ~EventSource() {} + + protected: + + /** + * \brief Fires the given event to all registered listeners. + * + *

+ * This method essentially iterates over all listeners + * and invokes {@link fireEvent(EventListener *listener, const E &event)} + * for each element. All derived classes are free to + * override the method to provide better error handling + * than the default implementation. + * + * @param event the event to be propagated to all listeners + */ + void fireEvent(const E &event); + + /** + * \brief Sends an event to the given listener. + * + * @param listener the listener to whom pass the event + * @param event the event to be handled + */ + virtual void fireEvent(EventListener *listener, const E &event); + + private: + + /** + * The set of registered event listeners. + */ + EventListeners m_listeners; + +}; + +/** + * \brief The interface of a generic event wrapper. + */ +class AbstractEventWrapper { + public: + + /** + * \brief Destructor. + */ + virtual ~AbstractEventWrapper() {} + + /** + * \brief Returns the underlying wrapee's data. + */ + virtual void *getWrapee() = 0; +}; + +/** + * \brief A template based implementation of {@link AbstractEventWrapper}. + */ +template +class EventWrapper : public AbstractEventWrapper { + public: + EventWrapper(const E &e) : m_e(e) { + } + void *getWrapee() { + return &m_e; + } + private: + E m_e; +}; + +/** + * \brief This class represents a generic event. + */ +class GenericEvent { + public: + + /** + * \brief Constructor. + */ + GenericEvent() : m_type(0) {} + + /** + * \brief Constructor. + * + * @param type the type of this event + * @param eventWarpper the wrapper around event's data + */ + GenericEvent(int type, AbstractEventWrapper *eventWrapper) : + m_type(type), m_eventWrapper(eventWrapper) { + } + + /** + * \brief Returns the type of this event. + * + * @return type of this event + */ + int getType() const { return m_type; } + + /** + * \brief Returns the event's data. + * + * @return the event's data + */ + void *getEvent() const { return m_eventWrapper->getWrapee(); } + + private: + + /** + * The event type. + */ + int m_type; + + /** + * The event represented as abstract wrapper. + */ + boost::shared_ptr m_eventWrapper; + +}; + +/** + * \brief This class adapts {@link EventListener} to a generic listener. + * Essentially this class listens on incoming events and fires them + * as {@link GenericEvent}s. + */ +template +class EventListenerAdapter : public virtual EventListener, + public virtual EventSource +{ + public: + + /** + * \brief Constructor. + * + * @param eventSource the source on which register this listener + */ + EventListenerAdapter(EventSource &eventSource) { + eventSource.addListener(this); + } + + void eventReceived(const EventSource &source, const E &e) { + AbstractEventWrapper *wrapper = new EventWrapper(e); + GenericEvent event(type, wrapper); + fireEvent( event ); + } + +}; + +/** + * \brief This class provides an adapter between an asynchronous and synchronous + * \brief event handling. + * + *

+ * This class queues up all received events and exposes them through + * {@link #getNextEvent()} method. + */ +template +class SynchronousEventAdapter : public EventListener { + public: + + void eventReceived(const EventSource &source, const E &e) { + m_queue.put( e ); + } + + /** + * \brief Returns the next available event from the underlying queue, + * \brief possibly blocking, if no data is available. + * + * @return the next available event + */ + E getNextEvent() { + return m_queue.take(); + } + + /** + * \brief Returns whether there are any events in the queue or not. + * + * @return true if there is at least one event and + * the next call to {@link #getNextEvent} won't block + */ + bool hasEvents() const { + return (m_queue.empty() ? false : true); + } + + /** + * \brief Destructor. + */ + virtual ~SynchronousEventAdapter() {} + + private: + + /** + * The blocking queue of all events received so far. + */ + BlockingQueue m_queue; + +}; + +/** + * This typedef defines the type of a timer Id. + */ +typedef int32_t TimerId; + +/** + * This class represents a timer event parametrized by the user's data type. + */ +template +class TimerEvent { + public: + + /** + * \brief Constructor. + * + * @param id the ID of this event + * @param alarmTime when this event is to be triggered + * @param userData the user data associated with this event + */ + TimerEvent(TimerId id, int64_t alarmTime, const T &userData) : + m_id(id), m_alarmTime(alarmTime), m_userData(userData) + {} + + /** + * \brief Constructor. + */ + TimerEvent() : m_id(-1), m_alarmTime(-1) {} + + /** + * \brief Returns the ID. + * + * @return the ID of this event + */ + TimerId getID() const { return m_id; } + + /** + * \brief Returns the alarm time. + * + * @return the alarm time + */ + int64_t getAlarmTime() const { return m_alarmTime; } + + /** + * \brief Returns the user's data. + * + * @return the user's data + */ + T const &getUserData() const { return m_userData; } + + /** + * \brief Returns whether the given alarm time is less than this event's + * \brief time. + */ + bool operator<(const int64_t alarmTime) const { + return m_alarmTime < alarmTime; + } + + private: + + /** + * The ID of ths event. + */ + TimerId m_id; + + /** + * The time at which this event triggers. + */ + int64_t m_alarmTime; + + /** + * The user specific data associated with this event. + */ + T m_userData; + +}; + +template +class Timer : public EventSource > { + public: + + /** + * \brief Constructor. + */ + Timer() : m_currentEventID(0), m_terminating(false) { + m_workerThread.Create( *this, &Timer::sendAlarms ); + } + + /** + * \brief Destructor. + */ + ~Timer() { + m_terminating = true; + m_lock.notify(); + m_workerThread.Join(); + } + + /** + * \brief Schedules the given event timeFromNow milliseconds. + * + * @param timeFromNow time from now, in milliseconds, when the event + * should be triggered + * @param userData the user data associated with the timer event + * + * @return the ID of the newly created timer event + */ + TimerId scheduleAfter(int64_t timeFromNow, const T &userData) { + return scheduleAt( getCurrentTimeMillis() + timeFromNow, userData ); + } + + /** + * \brief Schedules an event at the given time. + * + * @param absTime absolute time, in milliseconds, at which the event + * should be triggered; the time is measured + * from Jan 1st, 1970 + * @param userData the user data associated with the timer event + * + * @return the ID of the newly created timer event + */ + TimerId scheduleAt(int64_t absTime, const T &userData) { + m_lock.lock(); + typename QueueType::iterator pos = + lower_bound( m_queue.begin(), m_queue.end(), absTime ); + TimerId id = m_currentEventID++; + TimerEvent event(id, absTime, userData); + m_queue.insert( pos, event ); + m_lock.notify(); + m_lock.unlock(); + return id; + } + + /** + * \brief Returns the current time since Jan 1, 1970, in milliseconds. + * + * @return the current time in milliseconds + */ + static int64_t getCurrentTimeMillis() { + struct timeval now; + gettimeofday( &now, NULL ); + return now.tv_sec * 1000LL + now.tv_usec / 1000; + } + + /** + * \brief Cancels the given timer event. + * + * + * @param eventID the ID of the event to be canceled + * + * @return whether the event has been canceled + */ + bool cancelAlarm(TimerId eventID) { + bool canceled = false; + m_lock.lock(); + typename QueueType::iterator i; + for (i = m_queue.begin(); i != m_queue.end(); ++i) { + if (eventID == i->getID()) { + m_queue.erase( i ); + canceled = true; + break; + } + } + m_lock.unlock(); + return canceled; + } + + /** + * Executes the main loop of the worker thread. + */ + void sendAlarms() { + //iterate until terminating + while (!m_terminating) { + m_lock.lock(); + //1 step - wait until there is an event in the queue + if (m_queue.empty()) { + //wait up to 100ms to get next event + m_lock.wait( 100 ); + } + bool fire = false; + if (!m_queue.empty()) { + //retrieve the event from the queue and send it + TimerEvent event = m_queue.front(); + //check whether we can send it right away + int64_t timeToWait = + event.getAlarmTime() - getCurrentTimeMillis(); + if (timeToWait <= 0) { + m_queue.pop_front(); + //we fire only if it's still in the queue and alarm + //time has just elapsed (in case the top event + //is canceled) + fire = true; + } else { + m_lock.wait( timeToWait ); + } + m_lock.unlock(); + if (fire) { + fireEvent( event ); + } + } else { + m_lock.unlock(); + } + } + } + + private: + + /** + * The type of timer events queue. + */ + typedef deque > QueueType; + + /** + * The current event ID, auto-incremented each time a new event + * is created. + */ + TimerId m_currentEventID; + + /** + * The queue of timer events sorted by {@link TimerEvent#alarmTime}. + */ + QueueType m_queue; + + /** + * The lock used to guard {@link #m_queue}. + */ + Lock m_lock; + + /** + * The thread that triggers alarms. + */ + CXXThread > m_workerThread; + + /** + * Whether {@link #m_workerThread} is terminating. + */ + volatile bool m_terminating; + +}; + +template +void EventSource::fireEvent(const E &event) { + for (typename EventListeners::iterator i = m_listeners.begin(); + i != m_listeners.end(); + ++i) + { + fireEvent( *i, event ); + } +} + +template +void EventSource::fireEvent(EventListener *listener, const E &event) { + listener->eventReceived( *this, event ); +} + +} /* end of 'namespace zkfuse' */ + +#endif /* __EVENT_H__ */ diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log.cc b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log.cc new file mode 100644 index 0000000..e2bfb0d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log.cc @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include + +#include "log.h" + +using namespace std; + +/** + * \brief This class encapsulates a log4cxx configuration. + */ +class LogConfiguration { + public: + LogConfiguration(const string &file) { + PropertyConfigurator::configureAndWatch( file, 5000 ); + } +}; + +//enforces the configuration to be initialized +static LogConfiguration logConfig( "log4cxx.properties" ); diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log.h b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log.h new file mode 100644 index 0000000..aefce10 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log.h @@ -0,0 +1,116 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __LOG_H__ +#define __LOG_H__ + +#define ZKFUSE_NAMESPACE zkfuse +#define START_ZKFUSE_NAMESPACE namespace ZKFUSE_NAMESPACE { +#define END_ZKFUSE_NAMESPACE } +#define USING_ZKFUSE_NAMESPACE using namespace ZKFUSE_NAMESPACE; + +#include +#include +#include + +#include +#include +#include +using namespace log4cxx; +using namespace log4cxx::helpers; + +#define PRINTIP(x) ((uint8_t*)&x)[0], ((uint8_t*)&x)[1], \ + ((uint8_t*)&x)[2], ((uint8_t*)&x)[3] + +#define IPFMT "%u.%u.%u.%u" + +#define DECLARE_LOGGER(varName) \ +extern LoggerPtr varName; + +#define DEFINE_LOGGER(varName, logName) \ +static LoggerPtr varName = Logger::getLogger( logName ); + +#define MAX_BUFFER_SIZE 20000 + +#define SPRINTF_LOG_MSG(buffer, fmt, args...) \ + char buffer[MAX_BUFFER_SIZE]; \ + snprintf( buffer, MAX_BUFFER_SIZE, fmt, ##args ); + +// older versions of log4cxx don't support tracing +#ifdef LOG4CXX_TRACE +#define LOG_TRACE(logger, fmt, args...) \ + if (logger->isTraceEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_TRACE( logger, __tmp ); \ + } +#else +#define LOG_TRACE(logger, fmt, args...) \ + if (logger->isDebugEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_DEBUG( logger, __tmp ); \ + } +#endif + +#define LOG_DEBUG(logger, fmt, args...) \ + if (logger->isDebugEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_DEBUG( logger, __tmp ); \ + } + +#define LOG_INFO(logger, fmt, args...) \ + if (logger->isInfoEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_INFO( logger, __tmp ); \ + } + +#define LOG_WARN(logger, fmt, args...) \ + if (logger->isWarnEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_WARN( logger, __tmp ); \ + } + +#define LOG_ERROR(logger, fmt, args...) \ + if (logger->isErrorEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_ERROR( logger, __tmp ); \ + } + +#define LOG_FATAL(logger, fmt, args...) \ + if (logger->isFatalEnabled()) { \ + SPRINTF_LOG_MSG( __tmp, fmt, ##args ); \ + LOG4CXX_FATAL( logger, __tmp ); \ + } + +#ifdef DISABLE_TRACE +# define TRACE(logger, x) +#else +# define TRACE(logger, x) \ +class Trace { \ + public: \ + Trace(const void* p) : _p(p) { \ + LOG_TRACE(logger, "%s %p Enter", __PRETTY_FUNCTION__, p); \ + } \ + ~Trace() { \ + LOG_TRACE(logger, "%s %p Exit", __PRETTY_FUNCTION__, _p); \ + } \ + const void* _p; \ +} traceObj(x); +#endif /* DISABLE_TRACE */ + +#endif /* __LOG_H__ */ + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log4cxx.properties b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log4cxx.properties new file mode 100644 index 0000000..1e373e4 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/log4cxx.properties @@ -0,0 +1,28 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=TRACE, A1 + +# A1 is set to be a ConsoleAppender. +log4j.appender.A1=org.apache.log4cxx.ConsoleAppender + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4cxx.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n + +log4j.category.zkfuse=TRACE + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/mutex.h b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/mutex.h new file mode 100644 index 0000000..86c4604 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/mutex.h @@ -0,0 +1,169 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __MUTEX_H__ +#define __MUTEX_H__ + +#include +#include +#include + +#include "log.h" + +START_ZKFUSE_NAMESPACE + +class Cond; + +class Mutex { + friend class Cond; + public: + Mutex() { + pthread_mutexattr_init( &m_mutexAttr ); + pthread_mutexattr_settype( &m_mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP ); + pthread_mutex_init( &mutex, &m_mutexAttr ); + } + ~Mutex() { + pthread_mutex_destroy(&mutex); + pthread_mutexattr_destroy( &m_mutexAttr ); + } + void Acquire() { Lock(); } + void Release() { Unlock(); } + void Lock() { + pthread_mutex_lock(&mutex); + } + int TryLock() { + return pthread_mutex_trylock(&mutex); + } + void Unlock() { + pthread_mutex_unlock(&mutex); + } + private: + pthread_mutex_t mutex; + pthread_mutexattr_t m_mutexAttr; +}; + +class AutoLock { + public: + AutoLock(Mutex& mutex) : _mutex(mutex) { + mutex.Lock(); + } + ~AutoLock() { + _mutex.Unlock(); + } + private: + friend class AutoUnlockTemp; + Mutex& _mutex; +}; + +class AutoUnlockTemp { + public: + AutoUnlockTemp(AutoLock & autoLock) : _autoLock(autoLock) { + _autoLock._mutex.Unlock(); + } + ~AutoUnlockTemp() { + _autoLock._mutex.Lock(); + } + private: + AutoLock & _autoLock; +}; + +class Cond { + public: + Cond() { + static pthread_condattr_t attr; + static bool inited = false; + if(!inited) { + inited = true; + pthread_condattr_init(&attr); + } + pthread_cond_init(&_cond, &attr); + } + ~Cond() { + pthread_cond_destroy(&_cond); + } + + void Wait(Mutex& mutex) { + pthread_cond_wait(&_cond, &mutex.mutex); + } + + bool Wait(Mutex& mutex, long long int timeout) { + struct timeval now; + gettimeofday( &now, NULL ); + struct timespec abstime; + int64_t microSecs = now.tv_sec * 1000000LL + now.tv_usec; + microSecs += timeout * 1000; + abstime.tv_sec = microSecs / 1000000LL; + abstime.tv_nsec = (microSecs % 1000000LL) * 1000; + if (pthread_cond_timedwait(&_cond, &mutex.mutex, &abstime) == ETIMEDOUT) { + return false; + } else { + return true; + } + } + + void Signal() { + pthread_cond_signal(&_cond); + } + + private: + pthread_cond_t _cond; +}; + +/** + * A wrapper class for {@link Mutex} and {@link Cond}. + */ +class Lock { + public: + + void lock() { + m_mutex.Lock(); + } + + void unlock() { + m_mutex.Unlock(); + } + + void wait() { + m_cond.Wait( m_mutex ); + } + + bool wait(long long int timeout) { + return m_cond.Wait( m_mutex, timeout ); + } + + void notify() { + m_cond.Signal(); + } + + private: + + /** + * The mutex. + */ + Mutex m_mutex; + + /** + * The condition associated with this lock's mutex. + */ + Cond m_cond; +}; + +END_ZKFUSE_NAMESPACE + +#endif /* __MUTEX_H__ */ + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/thread.cc b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/thread.cc new file mode 100644 index 0000000..f1ed816 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/thread.cc @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include + +#include "thread.h" + +DEFINE_LOGGER( LOG, "Thread" ) + +START_ZKFUSE_NAMESPACE + +void Thread::Create(void* ctx, ThreadFunc func) +{ + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, _stackSize); + int ret = pthread_create(&mThread, &attr, func, ctx); + if(ret != 0) { + LOG_FATAL( LOG, "pthread_create failed: %s", strerror(errno) ); + } + // pthread_attr_destroy(&attr); + _ctx = ctx; + _func = func; +} + +END_ZKFUSE_NAMESPACE diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/thread.h b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/thread.h new file mode 100644 index 0000000..0ed12d7 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/thread.h @@ -0,0 +1,99 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __THREAD_H__ +#define __THREAD_H__ + +#include +#include +#include +#include + +#include "log.h" + +START_ZKFUSE_NAMESPACE + +class Thread { + public: + static const size_t defaultStackSize = 1024 * 1024; + typedef void* (*ThreadFunc) (void*); + Thread(size_t stackSize = defaultStackSize) + : _stackSize(stackSize), _ctx(NULL), _func(NULL) + { + memset( &mThread, 0, sizeof(mThread) ); + } + ~Thread() { } + + void Create(void* ctx, ThreadFunc func); + void Join() { + //avoid SEGFAULT because of unitialized mThread + //in case Create(...) was never called + if (_func != NULL) { + pthread_join(mThread, 0); + } + } + private: + pthread_t mThread; + void *_ctx; + ThreadFunc _func; + size_t _stackSize; +}; + + +template +struct ThreadContext { + typedef void (T::*FuncPtr) (void); + ThreadContext(T& ctx, FuncPtr func) : _ctx(ctx), _func(func) {} + void run(void) { + (_ctx.*_func)(); + } + T& _ctx; + FuncPtr _func; +}; + +template +void* ThreadExec(void *obj) { + ThreadContext* tc = (ThreadContext*)(obj); + assert(tc != 0); + tc->run(); + return 0; +} + +template +class CXXThread : public Thread { + public: + typedef void (T::*FuncPtr) (void); + CXXThread(size_t stackSize = Thread::defaultStackSize) + : Thread(stackSize), ctx(0) {} + ~CXXThread() { if (ctx) delete ctx; } + + void Create(T& obj, FuncPtr func) { + assert(ctx == 0); + ctx = new ThreadContext(obj, func); + Thread::Create(ctx, ThreadExec); + } + + private: + ThreadContext* ctx; +}; + + +END_ZKFUSE_NAMESPACE + +#endif /* __THREAD_H__ */ + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkadapter.cc b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkadapter.cc new file mode 100644 index 0000000..7f02fa3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkadapter.cc @@ -0,0 +1,884 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include + +#include "blockingqueue.h" +#include "thread.h" +#include "zkadapter.h" + +using namespace std; +using namespace zk; + +DEFINE_LOGGER( LOG, "zookeeper.adapter" ) +DEFINE_LOGGER( ZK_LOG, "zookeeper.core" ) + +/** + * \brief A helper class to initialize ZK logging. + */ +class InitZooKeeperLogging +{ + public: + InitZooKeeperLogging() { + if (ZK_LOG->isDebugEnabled() +#ifdef LOG4CXX_TRACE + || ZK_LOG->isTraceEnabled() +#endif + ) + { + zoo_set_debug_level( ZOO_LOG_LEVEL_DEBUG ); + } else if (ZK_LOG->isInfoEnabled()) { + zoo_set_debug_level( ZOO_LOG_LEVEL_INFO ); + } else if (ZK_LOG->isWarnEnabled()) { + zoo_set_debug_level( ZOO_LOG_LEVEL_WARN ); + } else { + zoo_set_debug_level( ZOO_LOG_LEVEL_ERROR ); + } + } +}; + +using namespace std; + +namespace zk +{ + +/** + * \brief This class provides logic for checking if a request can be retried. + */ +class RetryHandler +{ + public: + RetryHandler(const ZooKeeperConfig &zkConfig) + : m_zkConfig(zkConfig) + { + if (zkConfig.getAutoReconnect()) { + retries = 2; + } else { + retries = 0; + } + } + + /** + * \brief Attempts to fix a side effect of the given RC. + * + * @param rc the ZK error code + * @return whether the error code has been handled and the caller should + * retry an operation the caused this error + */ + bool handleRC(int rc) + { + TRACE( LOG, "handleRC" ); + + //check if the given error code is recoverable + if (!retryOnError(rc)) { + return false; + } + LOG_TRACE( LOG, "RC: %d, retries left: %d", rc, retries ); + if (retries-- > 0) { + return true; + } else { + return false; + } + } + + private: + /** + * The ZK config. + */ + const ZooKeeperConfig &m_zkConfig; + + /** + * The number of outstanding retries. + */ + int retries; + + /** + * Checks whether the given error entitles this adapter + * to retry the previous operation. + * + * @param zkErrorCode one of the ZK error code + */ + static bool retryOnError(int zkErrorCode) + { + return (zkErrorCode == ZCONNECTIONLOSS || + zkErrorCode == ZOPERATIONTIMEOUT); + } +}; + + +//the implementation of the global ZK event watcher +void zkWatcher(zhandle_t *zh, int type, int state, const char *path, + void *watcherCtx) +{ + TRACE( LOG, "zkWatcher" ); + + //a workaround for buggy ZK API + string sPath = + (path == NULL || + state == ZOO_SESSION_EVENT || + state == ZOO_NOTWATCHING_EVENT) + ? "" + : string(path); + LOG_INFO( LOG, + "Received a ZK event - type: %d, state: %d, path: '%s'", + type, state, sPath.c_str() ); + ZooKeeperAdapter *zka = (ZooKeeperAdapter *)zoo_get_context(zh); + if (zka != NULL) { + zka->enqueueEvent( type, state, sPath ); + } else { + LOG_ERROR( LOG, + "Skipping ZK event (type: %d, state: %d, path: '%s'), " + "because ZK passed no context", + type, state, sPath.c_str() ); + } +} + + + +// ======================================================================= + +ZooKeeperAdapter::ZooKeeperAdapter(ZooKeeperConfig config, + ZKEventListener *listener, + bool establishConnection) + throw(ZooKeeperException) + : m_zkConfig(config), + mp_zkHandle(NULL), + m_terminating(false), + m_connected(false), + m_state(AS_DISCONNECTED) +{ + TRACE( LOG, "ZooKeeperAdapter" ); + + resetRemainingConnectTimeout(); + + //enforce setting up appropriate ZK log level + static InitZooKeeperLogging INIT_ZK_LOGGING; + + if (listener != NULL) { + addListener(listener); + } + + //start the event dispatcher thread + m_eventDispatcher.Create( *this, &ZooKeeperAdapter::processEvents ); + + //start the user event dispatcher thread + m_userEventDispatcher.Create( *this, &ZooKeeperAdapter::processUserEvents ); + + //optionally establish the connection + if (establishConnection) { + reconnect(); + } +} + +ZooKeeperAdapter::~ZooKeeperAdapter() +{ + TRACE( LOG, "~ZooKeeperAdapter" ); + + try { + disconnect(); + } catch (std::exception &e) { + LOG_ERROR( LOG, + "An exception while disconnecting from ZK: %s", + e.what() ); + } + m_terminating = true; + m_userEventDispatcher.Join(); + m_eventDispatcher.Join(); +} + +void +ZooKeeperAdapter::validatePath(const string &path) throw(ZooKeeperException) +{ + TRACE( LOG, "validatePath" ); + + if (path.find( "/" ) != 0) { + throw ZooKeeperException( string("Node path must start with '/' but" + "it was '") + + path + + "'" ); + } + if (path.length() > 1) { + if (path.rfind( "/" ) == path.length() - 1) { + throw ZooKeeperException( string("Node path must not end with " + "'/' but it was '") + + path + + "'" ); + } + if (path.find( "//" ) != string::npos) { + throw ZooKeeperException( string("Node path must not contain " + "'//' but it was '") + + path + + "'" ); + } + } +} + +void +ZooKeeperAdapter::disconnect() +{ + TRACE( LOG, "disconnect" ); + LOG_TRACE( LOG, "mp_zkHandle: %p, state %d", mp_zkHandle, m_state ); + + m_stateLock.lock(); + if (mp_zkHandle != NULL) { + zookeeper_close( mp_zkHandle ); + mp_zkHandle = NULL; + setState( AS_DISCONNECTED ); + } + m_stateLock.unlock(); +} + +void +ZooKeeperAdapter::reconnect() throw(ZooKeeperException) +{ + TRACE( LOG, "reconnect" ); + + m_stateLock.lock(); + //clear the connection state + disconnect(); + + //establish a new connection to ZooKeeper + mp_zkHandle = zookeeper_init( m_zkConfig.getHosts().c_str(), + zkWatcher, + m_zkConfig.getLeaseTimeout(), + NULL, this, 0); + resetRemainingConnectTimeout(); + if (mp_zkHandle != NULL) { + setState( AS_CONNECTING ); + m_stateLock.unlock(); + } else { + m_stateLock.unlock(); + throw ZooKeeperException( + string("Unable to connect to ZK running at '") + + m_zkConfig.getHosts() + "'" ); + } + + LOG_DEBUG( LOG, "mp_zkHandle: %p, state %d", mp_zkHandle, m_state ); +} + +void +ZooKeeperAdapter::handleEvent(int type, int state, const string &path) +{ + TRACE( LOG, "handleEvent" ); + LOG_TRACE( LOG, + "type: %d, state %d, path: %s", + type, state, path.c_str() ); + Listener2Context context, context2; + //ignore internal ZK events + if (type != ZOO_SESSION_EVENT && type != ZOO_NOTWATCHING_EVENT) { + m_zkContextsMutex.Acquire(); + //check if the user context is available + if (type == ZOO_CHANGED_EVENT || type == ZOO_DELETED_EVENT) { + //we may have two types of interest here, + //in this case lets try to notify twice + context = findAndRemoveListenerContext( GET_NODE_DATA, path ); + context2 = findAndRemoveListenerContext( NODE_EXISTS, path ); + if (context.empty()) { + //make sure that the 2nd context is NULL and + // assign it to the 1st one + context = context2; + context2.clear(); + } + } else if (type == ZOO_CHILD_EVENT) { + context = findAndRemoveListenerContext( GET_NODE_CHILDREN, path ); + } else if (type == ZOO_CREATED_EVENT) { + context = findAndRemoveListenerContext( NODE_EXISTS, path ); + } + m_zkContextsMutex.Release(); + } + + handleEvent( type, state, path, context ); + if (!context2.empty()) { + handleEvent( type, state, path, context2 ); + } +} + +void +ZooKeeperAdapter::handleEvent(int type, + int state, + const string &path, + const Listener2Context &listeners) +{ + TRACE( LOG, "handleEvents" ); + + if (listeners.empty()) { + //propagate with empty context + ZKWatcherEvent event(type, state, path); + fireEvent( event ); + } else { + for (Listener2Context::const_iterator i = listeners.begin(); + i != listeners.end(); + ++i) { + ZKWatcherEvent event(type, state, path, i->second); + if (i->first != NULL) { + fireEvent( i->first, event ); + } else { + fireEvent( event ); + } + } + } +} + +void +ZooKeeperAdapter::enqueueEvent(int type, int state, const string &path) +{ + TRACE( LOG, "enqueueEvents" ); + + m_events.put( ZKWatcherEvent( type, state, path ) ); +} + +void +ZooKeeperAdapter::processEvents() +{ + TRACE( LOG, "processEvents" ); + + while (!m_terminating) { + bool timedOut = false; + ZKWatcherEvent source = m_events.take( 100, &timedOut ); + if (!timedOut) { + if (source.getType() == ZOO_SESSION_EVENT) { + LOG_INFO( LOG, + "Received SESSION event, state: %d. Adapter state: %d", + source.getState(), m_state ); + m_stateLock.lock(); + if (source.getState() == ZOO_CONNECTED_STATE) { + m_connected = true; + resetRemainingConnectTimeout(); + setState( AS_CONNECTED ); + } else if (source.getState() == ZOO_CONNECTING_STATE) { + m_connected = false; + setState( AS_CONNECTING ); + } else if (source.getState() == ZOO_EXPIRED_SESSION_STATE) { + LOG_INFO( LOG, "Received EXPIRED_SESSION event" ); + setState( AS_SESSION_EXPIRED ); + } + m_stateLock.unlock(); + } + m_userEvents.put( source ); + } + } +} + +void +ZooKeeperAdapter::processUserEvents() +{ + TRACE( LOG, "processUserEvents" ); + + while (!m_terminating) { + bool timedOut = false; + ZKWatcherEvent source = m_userEvents.take( 100, &timedOut ); + if (!timedOut) { + try { + handleEvent( source.getType(), + source.getState(), + source.getPath() ); + } catch (std::exception &e) { + LOG_ERROR( LOG, + "Unable to process event (type: %d, state: %d, " + "path: %s), because of exception: %s", + source.getType(), + source.getState(), + source.getPath().c_str(), + e.what() ); + } + } + } +} + +void +ZooKeeperAdapter::registerContext(WatchableMethod method, + const string &path, + ZKEventListener *listener, + ContextType context) +{ + TRACE( LOG, "registerContext" ); + + m_zkContexts[method][path][listener] = context; +} + +ZooKeeperAdapter::Listener2Context +ZooKeeperAdapter::findAndRemoveListenerContext(WatchableMethod method, + const string &path) +{ + TRACE( LOG, "findAndRemoveListenerContext" ); + + Listener2Context listeners; + Path2Listener2Context::iterator elem = m_zkContexts[method].find( path ); + if (elem != m_zkContexts[method].end()) { + listeners = elem->second; + m_zkContexts[method].erase( elem ); + } + return listeners; +} + +void +ZooKeeperAdapter::setState(AdapterState newState) +{ + TRACE( LOG, "setState" ); + if (newState != m_state) { + LOG_INFO( LOG, "Adapter state transition: %d -> %d", m_state, newState ); + m_state = newState; + m_stateLock.notify(); + } else { + LOG_TRACE( LOG, "New state same as the current: %d", newState ); + } +} + + +//TODO move this code to verifyConnection so reconnect() +//is called from one place only +void +ZooKeeperAdapter::waitUntilConnected() + throw(ZooKeeperException) +{ + TRACE( LOG, "waitUntilConnected" ); + long long int timeout = getRemainingConnectTimeout(); + LOG_INFO( LOG, + "Waiting up to %lld ms until a connection to ZK is established", + timeout ); + bool connected; + if (timeout > 0) { + long long int toWait = timeout; + while (m_state != AS_CONNECTED && toWait > 0) { + //check if session expired and reconnect if so + if (m_state == AS_SESSION_EXPIRED) { + LOG_INFO( LOG, + "Reconnecting because the current session has expired" ); + reconnect(); + } + struct timeval now; + gettimeofday( &now, NULL ); + int64_t milliSecs = -(now.tv_sec * 1000LL + now.tv_usec / 1000); + LOG_TRACE( LOG, "About to wait %lld ms", toWait ); + m_stateLock.wait( toWait ); + gettimeofday( &now, NULL ); + milliSecs += now.tv_sec * 1000LL + now.tv_usec / 1000; + toWait -= milliSecs; + } + waitedForConnect( timeout - toWait ); + LOG_INFO( LOG, "Waited %lld ms", timeout - toWait ); + } + connected = (m_state == AS_CONNECTED); + if (!connected) { + if (timeout > 0) { + LOG_WARN( LOG, "Timed out while waiting for connection to ZK" ); + throw ZooKeeperException("Timed out while waiting for " + "connection to ZK"); + } else { + LOG_ERROR( LOG, "Global timeout expired and still not connected to ZK" ); + throw ZooKeeperException("Global timeout expired and still not " + "connected to ZK"); + } + } + LOG_INFO( LOG, "Connected!" ); +} + +void +ZooKeeperAdapter::verifyConnection() throw(ZooKeeperException) +{ + TRACE( LOG, "verifyConnection" ); + + m_stateLock.lock(); + try { + if (m_state == AS_DISCONNECTED) { + throw ZooKeeperException("Disconnected from ZK. " \ + "Please use reconnect() before attempting to use any ZK API"); + } else if (m_state != AS_CONNECTED) { + LOG_TRACE( LOG, "Checking if need to reconnect..." ); + //we are not connected, so check if connection in progress... + if (m_state != AS_CONNECTING) { + LOG_TRACE( LOG, + "yes. Checking if allowed to auto-reconnect..." ); + //...not in progres, so check if we can reconnect + if (!m_zkConfig.getAutoReconnect()) { + //...too bad, disallowed :( + LOG_TRACE( LOG, "no. Sorry." ); + throw ZooKeeperException("ZK connection is down and " + "auto-reconnect is not allowed"); + } else { + LOG_TRACE( LOG, "...yes. About to reconnect" ); + } + //...we are good to retry the connection + reconnect(); + } else { + LOG_TRACE( LOG, "...no, already in CONNECTING state" ); + } + //wait until the connection is established + waitUntilConnected(); + } + } catch (ZooKeeperException &e) { + m_stateLock.unlock(); + throw; + } + m_stateLock.unlock(); +} + +bool +ZooKeeperAdapter::createNode(const string &path, + const string &value, + int flags, + bool createAncestors, + string &returnPath) + throw(ZooKeeperException) +{ + TRACE( LOG, "createNode (internal)" ); + validatePath( path ); + + const int MAX_PATH_LENGTH = 1024; + char realPath[MAX_PATH_LENGTH]; + realPath[0] = 0; + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + rc = zoo_create( mp_zkHandle, + path.c_str(), + value.c_str(), + value.length(), + &ZOO_OPEN_ACL_UNSAFE, + flags, + realPath, + MAX_PATH_LENGTH ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) { + if (rc == ZNODEEXISTS) { + //the node already exists + LOG_WARN( LOG, "Error %d for %s", rc, path.c_str() ); + return false; + } else if (rc == ZNONODE && createAncestors) { + LOG_WARN( LOG, "Error %d for %s", rc, path.c_str() ); + //one of the ancestors doesn't exist so lets start from the root + //and make sure the whole path exists, creating missing nodes if + //necessary + for (string::size_type pos = 1; pos != string::npos; ) { + pos = path.find( "/", pos ); + if (pos != string::npos) { + try { + createNode( path.substr( 0, pos ), "", 0, true ); + } catch (ZooKeeperException &e) { + throw ZooKeeperException( string("Unable to create " + "node ") + + path, + rc ); + } + pos++; + } else { + //no more path components + return createNode( path, value, flags, false, returnPath ); + } + } + } + LOG_ERROR( LOG,"Error %d for %s", rc, path.c_str() ); + throw ZooKeeperException( string("Unable to create node ") + + path, + rc ); + } else { + LOG_INFO( LOG, "%s has been created", realPath ); + returnPath = string( realPath ); + return true; + } +} + +bool +ZooKeeperAdapter::createNode(const string &path, + const string &value, + int flags, + bool createAncestors) + throw(ZooKeeperException) +{ + TRACE( LOG, "createNode" ); + + string createdPath; + return createNode( path, value, flags, createAncestors, createdPath ); +} + +int64_t +ZooKeeperAdapter::createSequence(const string &path, + const string &value, + int flags, + bool createAncestors) + throw(ZooKeeperException) +{ + TRACE( LOG, "createSequence" ); + + string createdPath; + bool result = createNode( path, + value, + flags | ZOO_SEQUENCE, + createAncestors, + createdPath ); + if (!result) { + return -1; + } else { + //extract sequence number from the returned path + if (createdPath.find( path ) != 0) { + throw ZooKeeperException( string("Expecting returned path '") + + createdPath + + "' to start with '" + + path + + "'" ); + } + string seqSuffix = + createdPath.substr( path.length(), + createdPath.length() - path.length() ); + char *ptr = NULL; + int64_t seq = strtol( seqSuffix.c_str(), &ptr, 10 ); + if (ptr != NULL && *ptr != '\0') { + throw ZooKeeperException( string("Expecting a number but got ") + + seqSuffix ); + } + return seq; + } +} + +bool +ZooKeeperAdapter::deleteNode(const string &path, + bool recursive, + int version) + throw(ZooKeeperException) +{ + TRACE( LOG, "deleteNode" ); + + validatePath( path ); + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + rc = zoo_delete( mp_zkHandle, path.c_str(), version ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) { + if (rc == ZNONODE) { + LOG_WARN( LOG, "Error %d for %s", rc, path.c_str() ); + return false; + } + if (rc == ZNOTEMPTY && recursive) { + LOG_WARN( LOG, "Error %d for %s", rc, path.c_str() ); + //get all children and delete them recursively... + vector nodeList; + getNodeChildren( nodeList, path, NULL ); + for (vector::const_iterator i = nodeList.begin(); + i != nodeList.end(); + ++i) { + deleteNode( *i, true ); + } + //...and finally attempt to delete the node again + return deleteNode( path, false ); + } + LOG_ERROR( LOG, "Error %d for %s", rc, path.c_str() ); + throw ZooKeeperException( string("Unable to delete node ") + path, + rc ); + } else { + LOG_INFO( LOG, "%s has been deleted", path.c_str() ); + return true; + } +} + +bool +ZooKeeperAdapter::nodeExists(const string &path, + ZKEventListener *listener, + void *context, Stat *stat) + throw(ZooKeeperException) +{ + TRACE( LOG, "nodeExists" ); + + validatePath( path ); + + struct Stat tmpStat; + if (stat == NULL) { + stat = &tmpStat; + } + memset( stat, 0, sizeof(Stat) ); + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + if (context != NULL) { + m_zkContextsMutex.Acquire(); + rc = zoo_exists( mp_zkHandle, + path.c_str(), + (listener != NULL ? 1 : 0), + stat ); + if (rc == ZOK || rc == ZNONODE) { + registerContext( NODE_EXISTS, path, listener, context ); + } + m_zkContextsMutex.Release(); + } else { + rc = zoo_exists( mp_zkHandle, + path.c_str(), + (listener != NULL ? 1 : 0), + stat ); + } + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) { + if (rc == ZNONODE) { + LOG_TRACE( LOG, "Node %s does not exist", path.c_str() ); + return false; + } + LOG_ERROR( LOG, "Error %d for %s", rc, path.c_str() ); + throw ZooKeeperException( + string("Unable to check existence of node ") + path, + rc ); + } else { + return true; + } +} + +void +ZooKeeperAdapter::getNodeChildren(vector &nodeList, + const string &path, + ZKEventListener *listener, + void *context) + throw (ZooKeeperException) +{ + TRACE( LOG, "getNodeChildren" ); + + validatePath( path ); + + String_vector children; + memset( &children, 0, sizeof(children) ); + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + if (context != NULL) { + m_zkContextsMutex.Acquire(); + rc = zoo_get_children( mp_zkHandle, + path.c_str(), + (listener != NULL ? 1 : 0), + &children ); + if (rc == ZOK) { + registerContext( GET_NODE_CHILDREN, path, listener, context ); + } + m_zkContextsMutex.Release(); + } else { + rc = zoo_get_children( mp_zkHandle, + path.c_str(), + (listener != NULL ? 1 : 0), + &children ); + } + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) { + LOG_ERROR( LOG, "Error %d for %s", rc, path.c_str() ); + throw ZooKeeperException( string("Unable to get children of node ") + + path, + rc ); + } else { + for (int i = 0; i < children.count; ++i) { + //convert each child's path from relative to absolute + string absPath(path); + if (path != "/") { + absPath.append( "/" ); + } + absPath.append( children.data[i] ); + nodeList.push_back( absPath ); + } + //make sure the order is always deterministic + sort( nodeList.begin(), nodeList.end() ); + } +} + +string +ZooKeeperAdapter::getNodeData(const string &path, + ZKEventListener *listener, + void *context, Stat *stat) + throw(ZooKeeperException) +{ + TRACE( LOG, "getNodeData" ); + + validatePath( path ); + + const int MAX_DATA_LENGTH = 128 * 1024; + char buffer[MAX_DATA_LENGTH]; + memset( buffer, 0, MAX_DATA_LENGTH ); + struct Stat tmpStat; + if (stat == NULL) { + stat = &tmpStat; + } + memset( stat, 0, sizeof(Stat) ); + + int rc; + int len; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + len = MAX_DATA_LENGTH - 1; + if (context != NULL) { + m_zkContextsMutex.Acquire(); + rc = zoo_get( mp_zkHandle, + path.c_str(), + (listener != NULL ? 1 : 0), + buffer, &len, stat ); + if (rc == ZOK) { + registerContext( GET_NODE_DATA, path, listener, context ); + } + m_zkContextsMutex.Release(); + } else { + rc = zoo_get( mp_zkHandle, + path.c_str(), + (listener != NULL ? 1 : 0), + buffer, &len, stat ); + } + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) { + LOG_ERROR( LOG, "Error %d for %s", rc, path.c_str() ); + throw ZooKeeperException( + string("Unable to get data of node ") + path, rc + ); + } else { + if (len == -1) { + len = 0; + } + return string( buffer, len ); + } +} + +void +ZooKeeperAdapter::setNodeData(const string &path, + const string &value, + int version) + throw(ZooKeeperException) +{ + TRACE( LOG, "setNodeData" ); + + validatePath( path ); + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + rc = zoo_set( mp_zkHandle, + path.c_str(), + value.c_str(), + value.length(), + version); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) { + LOG_ERROR( LOG, "Error %d for %s", rc, path.c_str() ); + throw ZooKeeperException( string("Unable to set data for node ") + + path, + rc ); + } +} + +} /* end of 'namespace zk' */ + diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkadapter.h b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkadapter.h new file mode 100644 index 0000000..8d4d1d5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkadapter.h @@ -0,0 +1,718 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __ZKADAPTER_H__ +#define __ZKADAPTER_H__ + +#include +#include +#include + +extern "C" { +#include "zookeeper.h" +} + +#include "log.h" +#include "mutex.h" +#include "thread.h" +#include "blockingqueue.h" +#include "event.h" + +using namespace std; +using namespace zkfuse; + +namespace zk { + +/** + * \brief A cluster related exception. + */ +class ZooKeeperException : + public std::exception +{ + public: + + /** + * \brief Constructor. + * + * @param msg the detailed message associated with this exception + */ + ZooKeeperException(const string &msg) : + m_message(msg), m_zkErrorCode(0) + {} + + /** + * \brief Constructor. + * + * @param msg the detailed message associated with this exception + * @param errorCode the ZK error code associated with this exception + */ + ZooKeeperException(const string &msg, int errorCode) : + m_zkErrorCode(errorCode) + { + char tmp[100]; + sprintf( tmp, " (ZK error code: %d)", errorCode ); + m_message = msg + tmp; + } + + /** + * \brief Destructor. + */ + ~ZooKeeperException() throw() {} + + /** + * \brief Returns detailed description of the exception. + */ + const char *what() const throw() { + return m_message.c_str(); + } + + /** + * \brief Returns the ZK error code. + */ + int getZKErrorCode() const { + return m_zkErrorCode; + } + + private: + + /** + * The detailed message associated with this exception. + */ + string m_message; + + /** + * The optional error code received from ZK. + */ + int m_zkErrorCode; + +}; + +/** + * \brief This class encapsulates configuration of a ZK client. + */ +class ZooKeeperConfig +{ + public: + + /** + * \brief Constructor. + * + * @param hosts the comma separated list of host and port pairs of ZK nodes + * @param leaseTimeout the lease timeout (heartbeat) + * @param autoReconnect whether to allow for auto-reconnect + * @param connectTimeout the connect timeout, in milliseconds; + */ + ZooKeeperConfig(const string &hosts, + int leaseTimeout, + bool autoReconnect = true, + long long int connectTimeout = 15000) : + m_hosts(hosts), m_leaseTimeout(leaseTimeout), + m_autoReconnect(autoReconnect), m_connectTimeout(connectTimeout) {} + + /** + * \brief Returns the list of ZK hosts to connect to. + */ + string getHosts() const { return m_hosts; } + + /** + * \brief Returns the lease timeout. + */ + int getLeaseTimeout() const { return m_leaseTimeout; } + + /** + * \brief Returns whether {@link ZooKeeperAdapter} should attempt + * \brief to automatically reconnect in case of a connection failure. + */ + bool getAutoReconnect() const { return m_autoReconnect; } + + /** + * \brief Gets the connect timeout. + * + * @return the connect timeout + */ + long long int getConnectTimeout() const { return m_connectTimeout; } + + private: + + /** + * The host addresses of ZK nodes. + */ + const string m_hosts; + + /** + * The ZK lease timeout. + */ + const int m_leaseTimeout; + + /** + * True if this adapater should attempt to autoreconnect in case + * the current session has been dropped. + */ + const bool m_autoReconnect; + + /** + * How long to wait, in milliseconds, before a connection + * is established to ZK. + */ + const long long int m_connectTimeout; + +}; + +/** + * \brief A data value object representing a watcher event received from the ZK. + */ +class ZKWatcherEvent +{ + public: + + /** + * \brief The type representing the user's context. + */ + typedef void *ContextType; + + /** + * \brief Constructor. + * + * @param type the type of this event + * @param state the state of this event + * @param path the corresponding path, may be empty for some event types + * @param context the user specified context; possibly NULL + */ + ZKWatcherEvent() : + m_type(-1), m_state(-1), m_path(""), mp_context(NULL) {} + + /** + * \brief Constructor. + * + * @param type the type of this event + * @param state the state of this event + * @param path the corresponding path, may be empty for some event types + * @param context the user specified context; possibly NULL + */ + ZKWatcherEvent(int type, int state, const string &path, + ContextType context = NULL) : + m_type(type), m_state(state), m_path(path), mp_context(context) {} + + int getType() const { return m_type; } + int getState() const { return m_state; } + string const &getPath() const { return m_path; } + ContextType getContext() const { return mp_context; } + + bool operator==(const ZKWatcherEvent &we) const { + return m_type == we.m_type && m_state == we.m_state + && m_path == we.m_path && mp_context == we.mp_context; + } + + private: + + /** + * The type of this event. It can be either ZOO_CREATED_EVENT, ZOO_DELETED_EVENT, + * ZOO_CHANGED_EVENT, ZOO_CHILD_EVENT, ZOO_SESSION_EVENT or ZOO_NOTWATCHING_EVENT. + * See zookeeper.h for more details. + */ + const int m_type; + + /** + * The state of ZK at the time of sending this event. + * It can be either ZOO_CONNECTING_STATE, ZOO_ASSOCIATING_STATE, + * ZOO_CONNECTED_STATE, ZOO_EXPIRED_SESSION_STATE or AUTH_FAILED_STATE. + * See {@file zookeeper.h} for more details. + */ + const int m_state; + + /** + * The corresponding path of the node in subject. It may be empty + * for some event types. + */ + const string m_path; + + /** + * The pointer to the user specified context, possibly NULL. + */ + ContextType mp_context; + +}; + +/** + * \brief The type definition of ZK event source. + */ +typedef EventSource ZKEventSource; + +/** + * \brief The type definition of ZK event listener. + */ +typedef EventListener ZKEventListener; + +/** + * \brief This is a wrapper around ZK C synchrounous API. + */ +class ZooKeeperAdapter + : public ZKEventSource +{ + public: + /** + * \brief The global function that handles all ZK asynchronous notifications. + */ + friend void zkWatcher(zhandle_t *, int, int, const char *, void *watcherCtx); + + /** + * \brief The type representing the user's context. + */ + typedef void *ContextType; + + /** + * \brief The map type of ZK event listener to user specified context mapping. + */ + typedef map Listener2Context; + + /** + * \brief The map type of ZK path's to listener's contexts. + */ + typedef map Path2Listener2Context; + + /** + * \brief All possible states of this client, in respect to + * \brief connection to the ZK server. + */ + enum AdapterState { + //mp_zkHandle is NULL + AS_DISCONNECTED = 0, + //mp_zkHandle is valid but this client is reconnecting + AS_CONNECTING, + //mp_zkHandle is valid and this client is connected + AS_CONNECTED, + //mp_zkHandle is valid, however no more calls can be made to ZK API + AS_SESSION_EXPIRED + }; + + /** + * \brief Constructor. + * Attempts to create a ZK adapter, optionally connecting + * to the ZK. Note, that if the connection is to be established + * and the given listener is NULL, some events may be lost, + * as they may arrive asynchronously before this method finishes. + * + * @param config the ZK configuration + * @param listener the event listener to be used for listening + * on incoming ZK events; + * if NULL not used + * @param establishConnection whether to establish connection to the ZK + * + * @throw ZooKeeperException if cannot establish connection to the given ZK + */ + ZooKeeperAdapter(ZooKeeperConfig config, + ZKEventListener *listener = NULL, + bool establishConnection = false) + throw(ZooKeeperException); + + /** + * \brief Destructor. + */ + ~ZooKeeperAdapter(); + + /** + * \brief Returns the current config. + */ + const ZooKeeperConfig &getZooKeeperConfig() const { + return m_zkConfig; + } + + /** + * \brief Restablishes connection to the ZK. + * If this adapter is already connected, the current connection + * will be dropped and a new connection will be established. + * + * @throw ZooKeeperException if cannot establish connection to the ZK + */ + void reconnect() throw(ZooKeeperException); + + /** + * \brief Disconnects from the ZK and unregisters {@link #mp_zkHandle}. + */ + void disconnect(); + + /** + * \brief Creates a new node identified by the given path. + * This method will optionally attempt to create all missing ancestors. + * + * @param path the absolute path name of the node to be created + * @param value the initial value to be associated with the node + * @param flags the ZK flags of the node to be created + * @param createAncestors if true and there are some missing ancestor nodes, + * this method will attempt to create them + * + * @return true if the node has been successfully created; false otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool createNode(const string &path, + const string &value = "", + int flags = 0, + bool createAncestors = true) + throw(ZooKeeperException); + + /** + * \brief Creates a new sequence node using the give path as the prefix. + * This method will optionally attempt to create all missing ancestors. + * + * @param path the absolute path name of the node to be created; + * @param value the initial value to be associated with the node + * @param flags the ZK flags of the sequence node to be created + * (in addition to SEQUENCE) + * @param createAncestors if true and there are some missing ancestor + * nodes, this method will attempt to create them + * + * @return the sequence number associate with newly created node, + * or -1 if it couldn't be created + * @throw ZooKeeperException if the operation has failed + */ + int64_t createSequence(const string &path, + const string &value = "", + int flags = 0, + bool createAncestors = true) + throw(ZooKeeperException); + + /** + * \brief Deletes a node identified by the given path. + * + * @param path the absolute path name of the node to be deleted + * @param recursive if true this method will attempt to remove + * all children of the given node if any exist + * @param version the expected version of the node. The function will + * fail if the actual version of the node does not match + * the expected version + * + * @return true if the node has been deleted; false otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool deleteNode(const string &path, bool recursive = false, int version = -1) + throw(ZooKeeperException); + + /** + * \brief Checks whether the given node exists or not. + * + * @param path the absolute path name of the node to be checked + * @param listener the listener for ZK watcher events; + * passing non NULL effectively establishes + * a ZK watch on the given node + * @param context the user specified context that is to be passed + * in a corresponding {@link ZKWatcherEvent} at later time; + * not used if listener is NULL + * @param stat the optional node statistics to be filled in by ZK + * + * @return true if the given node exists; false otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool nodeExists(const string &path, + ZKEventListener *listener = NULL, + void *context = NULL, + Stat *stat = NULL) + throw(ZooKeeperException); + + /** + * \brief Retrieves list of all children of the given node. + * + * @param path the absolute path name of the node for which to get children + * @param listener the listener for ZK watcher events; + * passing non NULL effectively establishes + * a ZK watch on the given node + * @param context the user specified context that is to be passed + * in a corresponding {@link ZKWatcherEvent} at later time; + * not used if listener is NULL + * + * @return the list of absolute paths of child nodes, possibly empty + * @throw ZooKeeperException if the operation has failed + */ + void getNodeChildren(vector &children, + const string &path, + ZKEventListener *listener = NULL, + void *context = NULL) + throw(ZooKeeperException); + + /** + * \brief Gets the given node's data. + * + * @param path the absolute path name of the node to get data from + * @param listener the listener for ZK watcher events; + * passing non NULL effectively establishes + * a ZK watch on the given node + * @param context the user specified context that is to be passed + * in a corresponding {@link ZKWatcherEvent} at later time; + * not used if listener is NULL + * @param stat the optional node statistics to be filled in by ZK + * + * @return the node's data + * @throw ZooKeeperException if the operation has failed + */ + string getNodeData(const string &path, + ZKEventListener *listener = NULL, + void *context = NULL, + Stat *stat = NULL) + throw(ZooKeeperException); + + /** + * \brief Sets the given node's data. + * + * @param path the absolute path name of the node to get data from + * @param value the node's data to be set + * @param version the expected version of the node. The function will + * fail if the actual version of the node does not match + * the expected version + * + * @throw ZooKeeperException if the operation has failed + */ + void setNodeData(const string &path, const string &value, int version = -1) + throw(ZooKeeperException); + + /** + * \brief Validates the given path to a node in ZK. + * + * @param the path to be validated + * + * @throw ZooKeeperException if the given path is not valid + * (for instance it doesn't start with "/") + */ + static void validatePath(const string &path) throw(ZooKeeperException); + + /** + * Returns the current state of this adapter. + * + * @return the current state of this adapter + * @see AdapterState + */ + AdapterState getState() const { + return m_state; + } + + private: + + /** + * This enum defines methods from this class than can trigger an event. + */ + enum WatchableMethod { + NODE_EXISTS = 0, + GET_NODE_CHILDREN, + GET_NODE_DATA + }; + + /** + * \brief Creates a new node identified by the given path. + * This method is used internally to implement {@link createNode(...)} + * and {@link createSequence(...)}. On success, this method will set + * createdPath. + * + * @param path the absolute path name of the node to be created + * @param value the initial value to be associated with the node + * @param flags the ZK flags of the node to be created + * @param createAncestors if true and there are some missing ancestor nodes, + * this method will attempt to create them + * @param createdPath the actual path of the node that has been created; + * useful for sequences + * + * @return true if the node has been successfully created; false otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool createNode(const string &path, + const string &value, + int flags, + bool createAncestors, + string &createdPath) + throw(ZooKeeperException); + + /** + * Handles an asynchronous event received from the ZK. + */ + void handleEvent(int type, int state, const string &path); + + /** + * Handles an asynchronous event received from the ZK. + * This method iterates over all listeners and passes the event + * to each of them. + */ + void handleEvent(int type, int state, const string &path, + const Listener2Context &listeners); + + /** + * \brief Enqueues the given event in {@link #m_events} queue. + */ + void enqueueEvent(int type, int state, const string &path); + + /** + * \brief Processes all ZK adapter events in a loop. + */ + void processEvents(); + + /** + * \brief Processes all user events in a loop. + */ + void processUserEvents(); + + /** + * \brief Registers the given context in the {@link #m_zkContexts} + * \brief contexts map. + * + * @param method the method where the given path is being used + * @param path the path of interest + * @param listener the event listener to call back later on + * @param context the user specified context to be passed back to user + */ + void registerContext(WatchableMethod method, const string &path, + ZKEventListener *listener, ContextType context); + + /** + * \brief Attempts to find a listener to context map in the contexts' + * \brief map, based on the specified criteria. + * If the context is found, it will be removed the udnerlying map. + * + * @param method the method type identify Listener2Context map + * @param path the path to be used to search in the Listener2Context map + * + * @return the context map associated with the given method and path, + * or empty map if not found + */ + Listener2Context findAndRemoveListenerContext(WatchableMethod method, + const string &path); + + /** + * Sets the new state in case it's different then the current one. + * This method assumes that {@link #m_stateLock} has been already locked. + * + * @param newState the new state to be set + */ + void setState(AdapterState newState); + + /** + * Waits until this client gets connected. The total wait time + * is given by {@link getRemainingConnectTimeout()}. + * If a timeout elapses, this method will throw an exception. + * + * @throw ZooKeeperException if unable to connect within the given timeout + */ + void waitUntilConnected() + throw(ZooKeeperException); + + /** + * Verifies whether the connection is established, + * optionally auto reconnecting. + * + * @throw ZooKeeperConnection if this client is disconnected + * and auto-reconnect failed or was not allowed + */ + void verifyConnection() throw(ZooKeeperException); + + /** + * Returns the remaining connect timeout. The timeout resets + * to {@link #m_connectTimeout} on a successfull connection to the ZK. + * + * @return the remaining connect timeout, in milliseconds + */ + long long int getRemainingConnectTimeout() { + return m_remainingConnectTimeout; + } + + /** + * Resets the remaining connect timeout to {@link #m_connectTimeout}. + */ + void resetRemainingConnectTimeout() { + m_remainingConnectTimeout = m_zkConfig.getConnectTimeout(); + } + + /** + * Updates the remaining connect timeout to reflect the given wait time. + * + * @param time the time for how long waited so far on connect to succeed + */ + void waitedForConnect(long long time) { + m_remainingConnectTimeout -= time; + } + + private: + + /** + * The mutex use to protect {@link #m_zkContexts}. + */ + zkfuse::Mutex m_zkContextsMutex; + + /** + * The map of registered ZK paths that are being watched. + * Each entry maps a function type to another map of registered contexts. + * + * @see WatchableMethod + */ + map m_zkContexts; + + /** + * The current ZK configuration. + */ + const ZooKeeperConfig m_zkConfig; + + /** + * The current ZK session. + */ + zhandle_t *mp_zkHandle; + + /** + * The blocking queue of all events waiting to be processed by ZK adapter. + */ + BlockingQueue m_events; + + /** + * The blocking queue of all events waiting to be processed by users + * of ZK adapter. + */ + BlockingQueue m_userEvents; + + /** + * The thread that dispatches all events from {@link #m_events} queue. + */ + CXXThread m_eventDispatcher; + + /** + * The thread that dispatches all events from {@link #m_userEvents} queue. + */ + CXXThread m_userEventDispatcher; + + /** + * Whether {@link #m_eventDispatcher} is terminating. + */ + volatile bool m_terminating; + + /** + * Whether this adapter is connected to the ZK. + */ + volatile bool m_connected; + + /** + * The state of this adapter. + */ + AdapterState m_state; + + /** + * The lock used to synchronize access to {@link #m_state}. + */ + Lock m_stateLock; + + /** + * How much time left for the connect to succeed, in milliseconds. + */ + long long int m_remainingConnectTimeout; + +}; + +} /* end of 'namespace zk' */ + +#endif /* __ZKADAPTER_H__ */ diff --git a/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkfuse.cc b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkfuse.cc new file mode 100644 index 0000000..6a82168 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkfuse/src/zkfuse.cc @@ -0,0 +1,4492 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#define FUSE_USE_VERSION 26 + +#ifdef HAVE_CONFIG_H +#include +#endif + +#undef _GNU_SOURCE +#define _GNU_SOURCE + +extern "C" { +#include +#include +} +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SETXATTR +#include +#endif + +#include + +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "mutex.h" +#include "zkadapter.h" + +#define ZOOKEEPER_ROOT_CHILDREN_WATCH_BUG + +/** + Typedef for ZooKeeperAdapter::Data. +*/ +typedef std::string Data; +/** + Typedef for ZooKeeperAdapter::NodeNames. +*/ +typedef vector NodeNames; + +#define MAX_DATA_SIZE 1024; + +DEFINE_LOGGER(LOG, "zkfuse"); + +inline +uint64_t millisecsToSecs(uint64_t millisecs) +{ + return millisecs / 1000; +} +inline +uint64_t secsToMillisecs(uint64_t secs) +{ + return secs * 1000; +} +inline +uint64_t nanosecsToMillisecs(uint64_t nanosecs) +{ + return nanosecs / 1000000; +} +inline +uint64_t timespecToMillisecs(const struct timespec & ts) +{ + return secsToMillisecs(ts.tv_sec) + nanosecsToMillisecs(ts.tv_nsec); +} + +typedef boost::shared_ptr ZooKeeperAdapterSharedPtr; + +/** + * ZkFuseCommon - holds immutable configuration objects. + * + * No locks are required to access these objects. + * A ZkFuseCommon instance is considered to be a data object and may be copied. + */ +class ZkFuseCommon +{ + private: + /** + References the ZooKeeperAdapter instance to be used. + */ + ZooKeeperAdapterSharedPtr _zkAdapter; + /** + Path to the ZooKeeper root node. + */ + std::string _rootPathName; + /** + Name used to access data "file" when the ZK node has + children. + */ + std::string _dataFileName; + /** + Suffix added to path components to force interpretation of + path components as directory. This is usually only required + for the last component. For example, ZkFuse may consider + a leaf node a regular file, e.g. /a/b/c/leaf. The suffix + can be used to create child under this node, e.g. + mkdir /a/b/c/leaf{forceDirSuffix}/new_leaf. + */ + std::string _forceDirSuffix; + /** + Prefix common to all metadata nodes created by ZkFuse. + */ + std::string _metadataNamePrefix; + /** + Path component name that identifies a directory metadata node. + A directory metadata node is currently empty. It is used by ZkFuse + to create a child when mkdir is used. This prevents ZkFuse + from interpreting the new child as a regular file. + */ + std::string _dirMetadataName; + /** + Path component name that identifies a regular file metadata node. + A regular metadata node holds metadata required to implement + Posix regular file semantics, such as setting mtime. + */ + std::string _regMetadataName; + /** + Number of not-in-use nodes to cache. + */ + unsigned _cacheSize; + /** + Assume this userid owns all nodes. + */ + const uid_t _uid; + /** + Assume this groupid owns all nodes. + */ + const gid_t _gid; + /** + Blocksize used to calculate number of blocks used for stat. + */ + const unsigned _blkSize; + + public: + /** + Constructor. + */ + ZkFuseCommon() + : _zkAdapter(), + _rootPathName("/"), + _dataFileName(), + _forceDirSuffix(), + _metadataNamePrefix(".zkfuse."), + _dirMetadataName(_metadataNamePrefix + "dir"), + _regMetadataName(_metadataNamePrefix + "file"), + _cacheSize(256), + _uid(geteuid()), + _gid(getegid()), + _blkSize(8192) + { + } + /** + Get root path name. Always "/". + \see _rootPathName + */ + const std::string & getRootPathName() const + { + return _rootPathName; + } + /** + Get dataFileName - the name for synthesized files to access + ZooKeeper node data. + \see _dataFileName + */ + const std::string & getDataFileName() const + { + return _dataFileName; + } + /** + Set dataFileName. + \see getDataFileName + \see _dataFileName + */ + void setDataFileName(const std::string & dataFileName) + { + _dataFileName = dataFileName; + } + /** + Get metadataNamePrefix - the common prefix for all ZkFuse created + metadata ZooKeeper nodes. + \see _metadataNamePrefix + */ + const std::string & getMetadataNamePrefix() const + { + return _metadataNamePrefix; + } + /** + Get forceDirSuffix - the suffix added to a path component to force + the path component to be treated like a directory. + \see _forceDirSuffix + */ + const std::string & getForceDirSuffix() const + { + return _forceDirSuffix; + } + /** + Set forceDirSuffix. + \see getForceDirSuffix + \see _forceDirSuffix + */ + void setForceDirSuffix(const std::string & forceDirSuffix) + { + _forceDirSuffix = forceDirSuffix; + } + /** + Get dirMetadataName - path component name of all directory + metadata ZooKeeper nodes. + \see _dirMetadataname + */ + const std::string & getDirMetadataName() const + { + return _dirMetadataName; + } + /** + Get regMetadataName - path component name of all regular file + metadata ZooKeeper nodes. + \see _regMetadataname + */ + const std::string & getRegMetadataName() const + { + return _regMetadataName; + } + /** + Get number of not-in-use ZkFuseFile instances to to cache. + \see _cacheSize + */ + unsigned getCacheSize() const + { + return _cacheSize; + } + /** + Set cache size. + \see getCacheSize + \see _cacheSize + */ + void setCacheSize(unsigned v) + { + _cacheSize = v; + } + /** + Get userid. + \see _uid + */ + uid_t getUid() const + { + return _uid; + } + /** + Get groupid. + \see _gid + */ + gid_t getGid() const + { + return _gid; + } + /** + Get block size. + \see _blkSize + */ + unsigned getBlkSize() const + { + return _blkSize; + } + /** + Get ZooKeeperAdapter. + \see _zkAdapter. + */ + const ZooKeeperAdapterSharedPtr & getZkAdapter() const + { + return _zkAdapter; + } + /** + Set ZooKeeperAdapter. + \see _zkAdaptor + */ + void setZkAdapter(const ZooKeeperAdapterSharedPtr & zkAdapter) + { + _zkAdapter = zkAdapter; + } +}; + +/** + ZkFuseNameType - identifies the type of the ZkFuse path. + */ +enum ZkFuseNameType { + /** + ZkFuse path is not syntheiszed. + ZkFuse should use its default rules to determine the Posix representation + of the path. + */ + ZkFuseNameDefaultType = 0, + /** + ZkFuse path is synthesized and identifies the data part of a + ZooKeeper node, i.e. Posix regular file semantics is expected. + */ + ZkFuseNameRegType = 1, + /** + ZkFuse path is synthesized and identifies the chidlren part of a + ZooKeeper node, i.e. Posix directory semantics is expected. + */ + ZkFuseNameDirType = 2 +}; + +class ZkFuseFile; + +typedef ZkFuseFile * ZkFuseFilePtr; + +class ZkFuseHandleManagerFactory; + +/** + ZkFuseHandleManager - keeps track of all the ZkFuseFile instances + allocated by a ZkFuseHandleManager instance and provides them + with a handle that can be used by FUSE. + + It maps a ZooKeeper path to a handle and a handle to a ZkFuse instance. + It also implements the methods that takes path names as arguments, such + as open, mknod, rmdir, and rename. + + Memory management + - References ZkFuseFile instances using regular pointers + Smart pointer is not used because reference counts are needed to + determine how many time a node is opened as a regular file or + directory. This also avoids circular smart pointer references. + - Each ZkFuseFile instance holds a reference to its ZkFuseHandleManager + using a boost::shared_ptr. This ensures that the ZkFuseHandleManager + instance that has the handle for the ZkFuseFile instance does not + get garbage collected while the ZkFuseFile instance exists. + + Concurrency control + - Except for the immutable ZkFuseCommon, all other member variables + are protected by _mutex. + - A method in this class can hold _mutex when it directly or + indirectly invokes ZkFuseFile methods. A ZkFuseFile method that holds + a ZkFuseFile instance _mutex cannot invoke a ZkFuseHandleManager + method that acquires the ZkFuseHandleManager instance's _mutex. + Otherwise, this may cause a dead lock. + - Methods that with names that begin with "_" do not acquire _mutex. + They are usually called by public methods that acquire and hold _mutex. + */ +class ZkFuseHandleManager : boost::noncopyable +{ + private: + /** + Typedef of handle, which is an int. + */ + typedef int Handle; + /** + Typedef of std::map used to map path to handle. + */ + typedef std::map Map; + /** + Typedef of std::vector used to map handle to ZkFuseFile instances. + */ + typedef std::vector Files; + /** + Typedef of std::vector used to hold unused handles. + */ + typedef std::vector FreeList; + /** + Typedef of boost::weak_ptr to the ZkFuseHandleManager instance. + */ + typedef boost::weak_ptr WeakPtr; + + /* Only ZkFuseHandleManagerFactory can create instances of this class */ + friend class ZkFuseHandleManagerFactory; + + /** + Contains common configuration. + Immutable so that it can be accessed without locks. + */ + const ZkFuseCommon _common; + /** + Maps a path name to a Handle. + */ + Map _map; + /** + Maps a handle to a ZkFuseFile instances. + Also holds pointers to all known ZkFuseFile instances. + An element may point to an allocated ZkFuseFile instance or be NULL. + + An allocated ZkFuseFile instance may be in one of the following states: + - in-use + Currently open, i.e. the ZkFuseFile instance's reference count + greater than 0. + - in-cache + Not currently open, i.e. the ZkFuseFile instances's + reference count is 0. + */ + Files _files; + /** + List of free'ed handles. + */ + FreeList _freeList; + /** + Mutex used to protect this instance. + */ + mutable zkfuse::Mutex _mutex; + /** + Count of number of in-use entries. + It used to calculate number of cached nodes. + Number cached nodes is (_files.size() - _numInUse). + */ + unsigned _numInUse; + /** + WeakPtr to myself. + */ + WeakPtr _thisWeakPtr; + + /** + Obtain a handle for the given path. + - If path is not known, then allocate a new handle and increment + _numInUse, and set newFile to true. The allocated + ZkFuseFile instance's reference count should be 1. + - If path is known, increase the corresponding + ZkFuseFile instance's reference count. + + \return the allocated handle. + \param path the path to lookup. + \param newFile indicates whether a new handle has been allocated. + */ + Handle allocate(const std::string & path, bool & newFile); + + /** + Constructor. + + \param common the immutable common configuration. + \param reserve number of elements to pre-allocate for + _files and _freeList. + */ + ZkFuseHandleManager( + const ZkFuseCommon & common, + const unsigned reserve) + : _common(common), + _files(), + _freeList(), + _mutex(), + _numInUse(0) + { + _files.reserve(reserve); + _files[0] = NULL; /* 0 never allocated */ + _files.resize(1); + _freeList.reserve(reserve); + } + + public: + /** + Typedef for boost::shared_ptr for this ZkFuseHandleManager class. + */ + typedef boost::shared_ptr SharedPtr; + + /** + Destructor. + */ + ~ZkFuseHandleManager() + { + } + /** + Get the ZkFuseFile instance for a handle. + + \return the ZkFuseFile instance identified by the handle. + \param handle get ZkFuseFile instance for this handle. + */ + ZkFuseFilePtr getFile(Handle handle) const + { + AutoLock lock(_mutex); + return _files[handle]; + } + /** + Get the immutable common configuration. + + \return the common configuration instance. + */ + const ZkFuseCommon & getCommon() const + { + return _common; + } + /** + Deallocate a previously allocated handle. + This decrements the reference count of the corresponding + ZkFuseFile instance. If the reference count becomes zero, + decrement _numInUse. It may also cause the ZkFuseFile instance + to be reclaimed if there are too many cached ZkFuseFile instances. + + The ZkFuseFile instance should be reclaimed if the number of + unused ZkFuseFile instances exceeds the configured cache size, i.e. + (_files.size() - _numInUse) > _common.getCacheSize() + and the ZkFuseFile instance has a reference count of zero. + + Reclaiming a ZkFuseFile instance involves removing the ZkFuseFile + instance's path to handle mapping from _map and the handle to the + ZkFuseFile instance mapping from _files, adding the handle to + the _freeList, and finally deleting the ZkFuseFile instance. + + \param handle the handle that should be deallocated. + */ + void deallocate(Handle handle); + /** + Handles ZooKeeper session events. + It invokes the known ZkFuseFile instances to let them know + that their watches will no longer be valid. + */ + void eventReceived(const ZKWatcherEvent & event); + /** + Get data from the specified the ZooKeeper path. + + \return 0 if successful, otherwise return negative errno. + \param path the path of the ZooKeeper node. + \param data return data read. + */ + int getData(const std::string & path, Data & data); + /** + Set data into the specified ZooKeeper path. + + \return 0 if successful, otherwise return negative errno. + \param path the path of the ZooKeeper node. + \param data the data to be written. + \param exists set to true if this path exists. + \param doFlush set to true if new data should be flushed to ZooKeeper. + */ + int setData(const std::string & path, + const Data & data, + bool exists, + bool doFlush); + /** + Create a ZooKeeper node to represent a ZkFuse file or directory. + + \return handle if successful, otherwise return negative errno. + \param path to create. + \param mode should be either S_IFDIR for directory or + S_IFREG for regular file. + \param mayExist if set and the ZooKeeper node already exist, return + valid handle instead of -EEXIST. + \param created returns whether a new ZooKeeper node had been created. + */ + int mknod(const std::string & path, + mode_t mode, + bool mayExist, + bool & created); + /** + Open a ZooKeeper node. + + The justCreated argument is used to differentiate if the _deleted flag + of the ZkFuseFile instance is to be trusted (i.e. the path + does not exist in ZooKeeper.) The _deleted flag is trusted + if the ZkFuseFile instance is known to exist in ZooKeeper after + invoking ZooKeeper with the path. + + If justCreated is true, then the ZkFuseFile instance was just created. + The ZkFuseFile constructor sets the _deleted flag to true because + path is not known to exist and hence should not be accessed. + The justCreated flag will force the ZkFuseFile instance to invoke + ZooKeeper to determine if the path exists. + + \return handle if successful, otherwise return negative errno. + \param path the path to open. + \param justCreated indicates if this is newly created ZkFuseFile instance. + */ + int open(const std::string & path, bool justCreated); + /** + Remove a ZkFuse directory. + + If force is not set, then the ZooKeeper node will be removed only + if it has no data and no child nodes except ZkFuse metadata nodes. + + \return 0 if successful, otherwise return negative errno. + \param path the path to remove. + \param force force removal, i.e. bypass checks. + */ + int rmdir(const char * path, bool force = false); + /** + Make a ZkFuse directory. + + ZkFuse represents a ZooKeeper node with no data and no children + as a regular file. In order to differentiate a newly created + directory from an empty regular file, mkdir will create a directory + metadata node as a child of the directory. + + \return 0 if successful, otherwise return negative errno. + \param path the path of the directory to create. + \param mode create directory with this mode + (mode currently not implemented). + */ + int mkdir(const char * path, mode_t mode); + /** + Remove a ZkFuse regular file. + + A file is the abstraction for the data part of a ZooKeeper node. + - If ZkFuse represents a ZooKeeper node as a directory, the data part + of the node is represented by synthesizing a name for this file. This + synthesized name is visible through readdir if the ZooKeeper node's + data is not empty. Removing such a file is done by truncating + the ZooKeeper node's data to 0 length. + - If ZkFuse represents a ZooKeeper node as a file, then removing the + is done by removing the ZooKeeper node (and its metadata). + + \return 0 if successful, otherwise return negative errno. + \param path the path of the file to remove. + */ + int unlink(const char * path); + /** + Get attributes of a ZkFuse regular file or directory. + + \return 0 if successful, otherwise return negative errno. + \param path get attributes for this path + \param stbuf store attributes here. + */ + int getattr(const char * path, struct stat & stbuf); + /** + Rename a ZkFuse regular file. + + It creates a new ZooKeeper node at toPath, copies data and file + metadata from the ZooKeeper node at fromPath to the new node, + and deletes the current ZooKeeper node. If the current ZooKeeper + node is not deleted if the new ZooKeeper node cannot be created + or the data copy fails. + + It cannot be used to rename a directory. + + \return 0 if successful, otherwise return negative errno. + \param fromPath the current path. + \param toPath rename to this path. + */ + int rename(const char * fromPath, const char * toPath); + /** + Add a child ZooKeeper path to the children information cache + of the ZkFuseFile instance that caches the parent ZooKeeper node. + + This is used to add a child path after a new ZooKeeper node has + been created to the children information cache of the parent + ZooKeeper node. This is needed because waiting for the children + changed event to update the cache may result in inconsistent local + views of the changes. + \see removeChildFromParent + + \parama childPath the path of the child ZooKeeper node. + */ + void addChildToParent(const std::string & childPath) const; + /** + Remove a child ZooKeeper path from the children information cache + of the ZkFuseFile instance that caches the parent ZooKeeper node. + + For example, this should happen whenever a path is deleted. + This child information cache of the parent will eventually be + invalidated by watches. However, the delivery of the children + change event may come after the next access and thus provide + the client with an inconsistent view. One example is that + client deletes the last file in a directory, but the children + changed event is not delivered before the client invokes rmdir. + to remove the parent. In this case, the rmdir fails because + the cached children information of the parent indicates the + "directory" is not empty. + + \param childPath the path of the child ZooKeeper node. + */ + void removeChildFromParent(const std::string & childPath) const; + /** + Return the path for the parent of the specified ZooKeeper path. + + \return the parent path. + \param childPath the child path. + */ + std::string getParentPath(const std::string & childPath) const; + /** + Return the ZooKeeper path from a ZkFuse path. + + The ZkFuse path may be a synthesized path. For example, a synthesized + path is required to access the data part of a ZooKeeper node's + data when ZkFuse represents the ZooKeeper node as directory. + A synthesized path is also required to create a child ZooKeeper node + under a ZooKeeper node that is represented by a regular file. + + \return the ZooKeeper path for path. + \param path the ZkFuse path, which may be a synthesized path. + \param nameType indicate whether the ZkFuse path is synthesized and + whether the synthesized ZkFuse path identifies a + directory or a regular file. + */ + std::string getZkPath(const char * path, ZkFuseNameType & nameType) const; +}; + +/** + ZkFuseHandleManagerFactory - factory for ZkFuseHandleManager. + + This is the only way to create a ZkFuseHandleManager instance. + to make sure that _thisWeakPtr of the instance is intialized + after the instance is created. + */ +class ZkFuseHandleManagerFactory +{ + public: + /** + Create an instance of ZkFuseHandleManager. + + \return the created ZkFuseHandleManager instance. + \param common the common configuration. + \param reserve initially reserve space for this number of handles. + */ + static ZkFuseHandleManager::SharedPtr create( + const ZkFuseCommon & common, + unsigned reserve = 1000) + { + ZkFuseHandleManager::SharedPtr manager + (new ZkFuseHandleManager(common, reserve)); + manager->_thisWeakPtr = manager; + return manager; + } +}; + +/** + ZkFuseAutoHandle - automatically closes handle. + + It holds an opened handle and automatically closes this handle + when it is destroyed. This enables code that open a handle + to be exception safe. + */ +class ZkFuseAutoHandle +{ + private: + /** + Typedef for Handle which is an int. + */ + typedef int Handle; + /** + Holds a reference to the ZkFuseHandlerManager instance that + allocated the handle. + */ + ZkFuseHandleManager::SharedPtr _manager; + /** + The handle that should be closed when this instance is destroyed. + A valid handle has value that is equal or greater than 0. + A negative value indicates an error condition, usually the value + is a negative errno. + */ + Handle _handle; + /** + Caches a reference to the ZkFuseFile instance with this handle. + This is a performance optimization so that _manager.getFile(_handle) + is only called once when the handle is initialized. + */ + ZkFuseFilePtr _file; + + /** + Initialize reference to the ZkFuseFile instance with this handle. + */ + void _initFile() + { + if (_handle >= 0) { + _file = _manager->getFile(_handle); + } else { + _file = NULL; + } + } + + public: + /** + Constructor - takes an previously opened handle. + + \param manager the ZkFuseHandleManager instance who allocated the handle. + \param handle the handle. + */ + ZkFuseAutoHandle( + const ZkFuseHandleManager::SharedPtr & manager, + int handle) + : _manager(manager), + _handle(handle), + _file() + { + _initFile(); + } + /** + Constructor - open path and remember handle. + + \param manager the ZkFuseHandleManager instance who allocated the handle. + \param path open this path and remember its handle in this instance. + */ + ZkFuseAutoHandle( + const ZkFuseHandleManager::SharedPtr & manager, + const std::string & path) + : _manager(manager), + _handle(_manager->open(path, false)), + _file() + { + _initFile(); + } + /** + Constructor - create path and remember handle. + + The creation mode indicates whether the path identifies a regular file + or a directory. + + \param manager the ZkFuseHandleManager instance who allocated the handle. + \param path create this path and remember its handle in this instance. + \param mode the creation mode for the path, should be either + S_IFDIR or S_IFDIR. + \param mayExist, if set and the path already exists, + then the ZkFuseAutoHandle will hold the handle + for the path instead of -EEXIST. + If not set and the path does not exist, then the handle + be -EEXIST. + */ + ZkFuseAutoHandle( + const ZkFuseHandleManager::SharedPtr & manager, + const std::string & path, + mode_t mode, + bool mayExist) + : _manager(manager), + _handle(-1), + _file() + { + bool created; + _handle = _manager->mknod(path, mode, mayExist, created); + _initFile(); + } + /** + Destructor - closes the handle. + */ + ~ZkFuseAutoHandle() + { + reset(); + } + /** + Get the handle. + \see _handle + */ + int get() const + { + return _handle; + } + /** + Get the ZkFuseFile instance of the handle. + \see _file + */ + ZkFuseFilePtr getFile() const + { + return _file; + } + /** + Forget the handle, don't close the handle. + */ + void release() + { + _handle = -1; + _file = NULL; + } + /** + Change the remembered handle. + + It will close the current handle (if valid). + */ + void reset(int handle = -1); +}; + +/** + ZkFuseStat - C++ wrapper for ZooKeeper Stat. + + This wrapper provides ZooKeeper Stat will constructors that + initializes the instance variables of Stat. + */ +class ZkFuseStat : public Stat +{ + public: + /** + Constructor - clear instance variables. + */ + ZkFuseStat() + { + clear(); + } + /** + Destructor - do nothing. + */ + ~ZkFuseStat() + { + } + /** + Clear instance variables. + */ + void clear() + { + czxid = 0; + mzxid = 0; + ctime = 0; + mtime = 0; + version = 0; + cversion = 0; + aversion = 0; + } +}; + +/** + ZkFuseFile - an instance encapsulates the runtime state of an allocated + ZooKeeper node. + + Memory management + - Referenced by the ZkFuseHandleManager that created this instance. + - Uses boost::shared_ptr to reference the ZkFuseHandleManager that + created this instance. This makes sure that this ZkFuseHandleManager + instance cannot be deleted when it has allocated ZkFuseFile instances. + - A ZkFuseHandleManager deletes itself if it can be reclaimed. + It can be reclaimed if it has no watches, its reference count is zero, + and the ZkFuseHandleManager instance would have more than the + configured number of cached ZkFuseFile instances. + - A ZkFuseFile instance cannot be deleted if it has active watches on + its ZooKeeper node. When one of its watches fires, the ZkFuseFile + instance must exist because one of its methods will be invoked + to process the event. If the ZkFuseFile instance has been deleted, + the method will access previously freed memory. + + Concurrency control + - _mutex protects the instance variables of an instance. + - Callers should assume that a public method will acquire _mutex. + - Methods of this class may not hold _mutex while invoking an + ZkFuseHandleManager instance. + - Methods that with names that begin with "_" do not acquire _mutex. + They are usually called by public methods that acquire and hold _mutex. +*/ +class ZkFuseFile : boost::noncopyable +{ + public: + /** + Maximum size for the data part of a ZooKeeper node. + */ + static const unsigned maxDataFileSize = MAX_DATA_SIZE; + + private: + /** + Mode returned by getattr for a ZkFuse directory. + */ + static const mode_t dirMode = (S_IFDIR | 0777); + /** + Mode returned by getattr for a ZkFuse regular file. + */ + static const mode_t regMode = (S_IFREG | 0777); + + /** + References the ZkFuseHandleManager that created this instance. + */ + ZkFuseHandleManager::SharedPtr _manager; + /** + Handle for this instance. + */ + const int _handle; + /** + Path of the ZooKeeper node represented by this instance. + */ + const std::string _path; + /** + Mutex that protects the instance variables of this instance. + */ + mutable zkfuse::Mutex _mutex; + /** + Reference count for this instance, i.e. the number of opens + minus the number of closes. + */ + int _refCount; + /** + Indicates whether the ZooKeeper node exist. + This flag allows caching of deleted ZooKeeper node to avoid + repeated ZooKeeper lookups for a non-existent path, and avoid + using cached information. + + Its value is true if + - it is verified to exist (by calling ZooKeeper), or + - it is existence is unknown because ZooKeeper has not been + invoked to verify its path's existence. + */ + bool _deleted; + /** + Count of current number directory opens minus directory closes. + */ + int _openDirCount; + /** + Indicates whether cached children information is valid. + + It is true if the cached children information is valid. + */ + bool _initializedChildren; + /** + Indicates whether there is an outstanding children watch. + + It is true if it has an outstanding children watch. + */ + bool _hasChildrenListener; + /** + Cached children information. + + The cache is valid if _initializedChildren is true. + */ + NodeNames _children; + + /** + Indicates whether the cached data is valid. + + It is true if the cached data and ZooKeeper Stat are valid. + */ + bool _initializedData; + /** + Indicates whether there is an outstanding data watch. + + It is true if it has an outstanding data watch. + */ + bool _hasDataListener; + /** + Indicates whether the cached data (_activeData) has been modified. + + It is true if the cached data has been modified. + */ + bool _dirtyData; + /** + Currently active data. + + To maintain atomicity of updates and emulate Posix semantics, + when a ZkFuse file remains open, the same data will be accessed + by the file's clients. The data will be flushed to ZooKeeper when + the flush method is called. The flush method may be called + explicitly by a client or implicitly when the ZkFuse file is no + longer currently open. + + _activeData and _activeStat stores the data and ZooKeeper Stat + that will be accessed by the file's clients. + + If there are changes when the ZkFuse file is open, new data is + cached as latest data (by _latestData and _latestStat). + */ + Data _activeData; + /** + Currently active ZooKeeper Stat. + \see _activeData + */ + ZkFuseStat _activeStat; + /** + Latest data. + This is either the same as _activeData or it is newer. It is newer + is it has been updated by event triggered by a data watch. + */ + Data _latestData; + /** + Latest ZooKeeper data. + This is either the same as _activeStat or it is newer. It is newer + is it has been updated by event triggered by a data watch. + */ + ZkFuseStat _latestStat; + + /** + Get userid. + + \return the userid. + */ + uid_t _getUid() const + { + return _manager->getCommon().getUid(); + } + /** + Get groupid. + + \return the groupid. + */ + gid_t _getGid() const + { + return _manager->getCommon().getGid(); + } + /** + Get block size. + + \return the block size. + */ + unsigned _getBlkSize() const + { + return _manager->getCommon().getBlkSize(); + } + /** + Get number of children, include metadata children in the count. + + \return the number of children including metadata children. + */ + unsigned _numChildrenIncludeMeta() const + { + unsigned count = _children.size(); + LOG_DEBUG(LOG, "numChildrenIncludeMeta() returns %u", count); + return count; + } + /** + Get number of children, exclude metadata children in the count. + + \return the number of children excluding metadata children. + */ + unsigned _numChildrenExcludeMeta() const + { + unsigned count = 0; + for (NodeNames::const_iterator it = _children.begin(); + it != _children.end(); + it++) { + if (!_isMeta(*it)) { + count++; + } + } + LOG_DEBUG(LOG, "numChildrenExcludeMeta() returns %u", count); + return count; + } + /** + Whether the ZooKeeper node has children, include metadata + children. + + \return true if it has children including metadata children. + */ + bool _hasChildrenIncludeMeta() const + { + return _numChildrenIncludeMeta() != 0; + } + /** + Return true if the ZooKeeper node has children, include metadata + children. + + \return true if it has children excluding metadata children. + */ + bool _hasChildrenExcludeMeta() const + { + return _numChildrenExcludeMeta() != 0; + } + /** + Whether the ZooKeeper node has data. + + \return true if _activeData is not empty. + */ + bool _hasData() const + { + return _activeData.empty() == false; + } + /** + Whether the ZooKeeper node has child with the specified path. + + \return true if the ZooKeeper node has a child with the specified path. + \param childPath the path of the child. + */ + bool _hasChildPath(const std::string & childPath) const + { + bool hasChild = + std::find(_children.begin(), _children.end(), childPath) + != _children.end(); + LOG_DEBUG(LOG, "hasChild(childPath %s) returns %d", + childPath.c_str(), hasChild); + return hasChild; + } + /** + Whether the given path component is a ZkFuse synthesized path + component. + + A ZkFuse synthesized path component will begin with + the metadataNamePrefix obtained from the common configuration. + \see _metadataNamePrefix + + \return true if the path component is a ZkFuse synthesized path + component. + \param childName the path component to check if it is synthesized by + ZkFuse. + */ + bool _isMeta(const std::string & childName) const + { + bool isMeta; + const std::string & prefix = + _manager->getCommon().getMetadataNamePrefix(); + unsigned offset = + (_path.length() > 1 ? + _path.length() + 1 : + 1 /* special case for root dir */ ); + unsigned minLength = offset + prefix.length(); + if (childName.length() < minLength || + childName.compare(offset, prefix.length(), prefix) != 0) { + isMeta = false; + } else { + isMeta = true; + } + LOG_DEBUG(LOG, "isMeta(childName %s) returns %d", + childName.c_str(), isMeta); + return isMeta; + } + /** + Build a path for a specific child of the ZooKeeper node. + + This is done by appending "/" (unless it is the ZooKeeper node + is the root node) and the name of the child. + + \return the path for the specified child of the ZooKeeper node. + \param name the name of the child. + */ + std::string _getChildPath(const std::string & name) const + { + return buildChildPath(_path, name); + } + /** + Whether the ZooKeeper node has a regular file metadata child node. + + \return true if the ZooKeeper node has a regular file metadata child + node. + */ + bool _hasRegMetadata() const + { + bool res = _hasChildPath( + _getChildPath(_manager->getCommon().getRegMetadataName())); + LOG_DEBUG(LOG, "hasRegMetadata() returns %d", res); + return res; + } + /** + Whether the ZooKeeper node has a directory metadata child node. + + \return true if the ZooKeeper node has a directory metadata child + node. + */ + bool _hasDirMetadata() const + { + bool res = _hasChildPath( + _getChildPath(_manager->getCommon().getDirMetadataName())); + LOG_DEBUG(LOG, "hasDirMetadata() returns %d", res); + return res; + } + /** + Whether ZkFuse should present the ZooKeeper node as a ZkFuse regular + file. + + It should be a ZkFuse regular file it has no children or its + only children is its regular file metadata child node. + + \return true if the Zookeeper node should be presented as a ZkFuse + regular file. + */ + bool _isReg() const + { + unsigned numChildrenIncludeMeta = _numChildrenIncludeMeta(); + bool res = + (numChildrenIncludeMeta == 0) || + (numChildrenIncludeMeta == 1 && _hasRegMetadata() == true); + LOG_DEBUG(LOG, "isReg() returns %d", res); + return res; + } + /** + Whether ZkFuse should present the ZooKeeper node as a ZkFuse directory. + + It should be a ZkFuse directory if it should not be presented as + a ZkFuse regular directory. + \see _isReg + + \return true if the Zookeeper node should be presented as a ZkFuse + directory. + */ + bool _isDir() const + { + return !_isReg(); + } + /** + Whether ZkFuse should present the ZooKeeper node as a ZkFuse regular + file by taking into account the specified ZkFuseNameType. + + The ZkFuseNameType may override the default ZkFuse presentation of + a ZooKeeper node. + + \return true if ZkFuse should present the ZooKeeper node as a ZkFuse + regular file. + \param nameType specifies the ZkFuseNameType. + \param doLock whether _mutex should be acquired, it should be true + if the caller did not acquire _mutex. + */ + bool _isRegNameType(ZkFuseNameType nameType, bool doLock = false) const + { + bool res; + switch (nameType) { + case ZkFuseNameRegType: + res = true; + break; + case ZkFuseNameDirType: + res = false; + break; + case ZkFuseNameDefaultType: + default: + if (doLock) { + AutoLock lock(_mutex); + res = _isReg(); + } else { + res = _isReg(); + } + break; + } + LOG_DEBUG(LOG, "isRegNameType(nameType %d) returns %d", + int(nameType), res); + return res; + } + /** + Whether ZkFuse should present the ZooKeeper node as a ZkFuse + directory by taking into account the specified ZkFuseNameType. + + The ZkFuseNameType may override the default ZkFuse presentation of + a ZooKeeper node. + + \return true if ZkFuse should present the ZooKeeper node as a ZkFuse + directory. + \param nameType specifies the ZkFuseNameType. + \param doLock whether _mutex should be acquired, it should be true + if the caller did not acquire _mutex. + */ + bool _isDirNameType(ZkFuseNameType nameType, bool doLock = false) const + { + bool res; + switch (nameType) { + case ZkFuseNameRegType: + res = false; + break; + case ZkFuseNameDirType: + res = true; + break; + case ZkFuseNameDefaultType: + default: + if (doLock) { + AutoLock lock(_mutex); + res = _isDir(); + } else { + res = _isDir(); + } + break; + } + LOG_DEBUG(LOG, "isDirNameType(nameType %d) returns %d", + int(nameType), res); + return res; + } + /** + ZkFuse regular file metadata. + */ + struct Metadata { + /** + Version of the ZooKeeper node data that this metadata is good for. + */ + uint32_t version; + /** + Acces time in milliseconds. + */ + uint64_t atime; + /** + Modified time in milliseconds. + */ + uint64_t mtime; + + /** + Constructor. + */ + Metadata() + : version(0), + atime(0), + mtime(0) + { + } + }; + /** + Encode Metadata into Data so that it can be stored in a metadata + ZooKeeper node. + + Each Metadata attribute is encoded as ": " on single line + terminated by newline. + + \param meta the input Metadata. + \param data the output Data after encoding. + */ + void _encodeMetadata(const Metadata & meta, Data & data) const + { + LOG_DEBUG(LOG, "encodeMetadata()"); + std::ostringstream oss; + oss << "version: " << meta.version << endl + << "atime: " << meta.atime << endl + << "mtime: " << meta.mtime << endl; + data = oss.str(); + } + /** + Decode Data from a metadata child ZooKeeper node into Metadata. + + Data is a stream of ": " records separated by newline. + + \param data the input Data. + \param meta the output Metadata after decoding. + */ + void _decodeMetadata(const Data & data, Metadata & meta) const + { + LOG_DEBUG(LOG, "decodeMetadata(data %s)", data.c_str()); + std::istringstream iss(data); + char key[128]; + char value[1024]; + while (!iss.eof()) { + key[0] = 0; + value[0] = 0; + iss.get(key, sizeof(key), ' '); + if (iss.eof()) { + break; + } + iss.ignore(32, ' '); + iss.getline(value, sizeof(value)); + LOG_DEBUG(LOG, "key %s value %s", key, value); + if (strcmp(key, "version:") == 0) { + unsigned long long v = strtoull(value, NULL, 0); + LOG_DEBUG(LOG, "version: %llu", v); + meta.version = v; + } + else if (strcmp(key, "atime:") == 0) { + unsigned long long v = strtoull(value, NULL, 0); + LOG_DEBUG(LOG, "atime: %llu", v); + meta.atime = v; + } + else if (strcmp(key, "mtime:") == 0) { + unsigned long long v = strtoull(value, NULL, 0); + LOG_DEBUG(LOG, "mtime: %llu", v); + meta.mtime = v; + } + else { + LOG_WARN(LOG, "decodeMetadata: path %s unknown key %s %s\n", + _path.c_str(), key, value); + } + } + LOG_DEBUG(LOG, "decodeMetadata done"); + } + /** + Flush data to the ZooKeeper node. + + If cached active data has been modified, flush it to the ZooKeeper node. + Returns -EIO if the data cannot be written because the cached active + data is not the expected version, i.e. ZooKeeper returns ZBADVERSION. + -EIO may also indicate a more general failure, such as unable to + communicate with ZooKeeper. + + \return 0 if successful, otherwise negative errno. + */ + int _flush() + { + LOG_DEBUG(LOG, "flush() path %s", _path.c_str()); + + int res = 0; + try { + if (_dirtyData) { + LOG_DEBUG(LOG, "is dirty, active version %d", + _activeStat.version); + _manager->getCommon().getZkAdapter()-> + setNodeData(_path, _activeData, _activeStat.version); + /* assumes version always increments by one if successful */ + _deleted = false; + _activeStat.version++; + _dirtyData = false; + res = 0; + } + else { + LOG_DEBUG(LOG, "not dirty"); + res = 0; + } + } catch (const ZooKeeperException & e) { + if (e.getZKErrorCode() == ZBADVERSION) { + LOG_ERROR(LOG, "flush %s bad version, was %d", + _path.c_str(), _activeStat.version); + res = -EIO; + } + else { + LOG_ERROR(LOG, "flush %s exception %s", + _path.c_str(), e.what()); + res = -EIO; + } + } + + LOG_DEBUG(LOG, "flush returns %d", res); + return res; + } + /** + Truncate or expand the size of the cached active data. + + This method only changes the size of the cached active data. + This change is committed to ZooKeeper when the cached data + is written to the ZooKeeper node by flush(). + + Return -EFBIG is the requested size exceeds the maximum. + + \return 0 if successful, otherwise negative errno. + \param size the requested size. + */ + int _truncate(off_t size) + { + LOG_DEBUG(LOG, "truncate(size %zu) path %s", size, _path.c_str()); + + int res = 0; + + if (!_isInitialized()) { + LOG_DEBUG(LOG, "not initialized"); + res = -EIO; + } + else if (size > _activeData.size()) { + if (size > maxDataFileSize) { + LOG_DEBUG(LOG, "size > maxDataFileSize"); + res = -EFBIG; + } else { + LOG_DEBUG(LOG, "increase to size"); + _activeData.insert(_activeData.begin() + + (size - _activeData.size()), 0); + _dirtyData = true; + res = 0; + } + } + else if (size < _activeData.size()) { + LOG_DEBUG(LOG, "decrease to size"); + _activeData.resize(size); + _dirtyData = true; + res = 0; + } + else { + LOG_DEBUG(LOG, "do nothing, same size"); + } + + LOG_DEBUG(LOG, "truncate returns %d", res); + return res; + } + /** + Remove a ZkFuse directory. + + If force is true, then the ZooKeeper node and its decendants + will be deleted. + + If force is false, then this method implements the semantics + of removing a ZkFuse directory. It will delete the ZooKeeper node + only if the ZooKeeper node have no data and no non-metadata + children. + - Return -ENOTDIR if the ZooKeeper node is not considered + to be a directory (after taking into consideration the specified + ZkFuseNameType). + - Return -ENOTEMPTY if the ZooKeeper node has data or it has + non-metadata children. + - Return -ENOENT if the ZooKeeper cannot be deleted, usually this + is because it does not exist. + + \return 0 if successful, otherwise negative errno. + \param nameType the ZkFuseNameType of the path used to specify the + directory to be removed. It influences whether ZkFuse + considers the ZooKeeper node to be a regular file or + directory. \see ZkFuseNameType + \param force set to true to bypass ZkFuse rmdir semantic check. + */ + int _rmdir(ZkFuseNameType nameType, bool force) + { + LOG_DEBUG(LOG, "rmdir(nameType %d, force %d) path %s", + int(nameType), force, _path.c_str()); + + int res = 0; + try { + if (!force && !_isDirNameType(nameType)) { + LOG_DEBUG(LOG, "failed because not directory"); + res = -ENOTDIR; + } + else if (!force && _hasData()) { + /* rmdir cannot occur if there non-empty "data file" */ + LOG_DEBUG(LOG, "failed because node has data"); + res = -ENOTEMPTY; + } + else if (!force && _hasChildrenExcludeMeta()) { + /* rmdir cannot occur if there are "subdirs" */ + LOG_DEBUG(LOG, "failed because node has children"); + res = -ENOTEMPTY; + } + else { + LOG_DEBUG(LOG, "delete node"); + bool deleted = _manager->getCommon().getZkAdapter()-> + deleteNode(_path, true); + if (deleted) { + _deleted = true; + _clearChildren(); + res = 0; + } else { + /* TODO: differentiate delete error conditions, + * e.g. access permission, not exists, ... ? + */ + LOG_DEBUG(LOG, "delete failed"); + res = -ENOENT; + } + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "rmdir %s exception %s", _path.c_str(), e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "rmdir returns %d", res); + return res; + } + /** + Remove a ZkFuse regular file. + + This method implements the semantics of removing a ZkFuse regular file. + - If the ZkFuse regular file represents the data part of the + ZooKeeper node which is presented as a ZkFuse directory, + the regular file is virtually deleted by truncating the + ZooKeeper node's data. Readdir will not synthesize a regular + file entry for the data part of a ZooKeeper node if + the ZooKeeper node has no data. + - If the ZkFuse regular file represents the data part of the + ZooKeeper node which is presented as a ZkFuse regular file, + the ZooKeeper node and its decendants are deleted. + + Returns -EISDIR if the ZkFuse regular file cannot be deleted + because ZkFuse consider it to be a directory. + + \return 0 if successful, otherwise negative errno. + \param nameType the ZkFuseNameType of the path used to specify the + directory to be removed. It influences whether ZkFuse + considers the ZooKeeper node to be a regular file or + directory. \see ZkFuseNameType + */ + int _unlink(ZkFuseNameType nameType) + { + LOG_DEBUG(LOG, "unlink(nameType %d) path %s", + int(nameType), _path.c_str()); + + int res = 0; + switch (nameType) { + case ZkFuseNameRegType: + if (_isDir()) { + res = _truncate(0); + } else { + res = _rmdir(nameType, true); + } + break; + case ZkFuseNameDirType: + res = -EISDIR; + break; + case ZkFuseNameDefaultType: + default: + if (_isReg()) { + res = _rmdir(nameType, true); + } else { + res = -EISDIR; + } + break; + } + + LOG_DEBUG(LOG, "unlink returns %d", res); + return res; + } + /** + Whether cached children and data are valid. + + \return true if cached children and data are valid. + */ + bool _isInitialized() const + { + return _initializedChildren && _initializedData; + } + /** + Clear and invalidate cached children information. + */ + void _clearChildren() + { + _initializedChildren = false; + _children.clear(); + } + /** + Clear and invalidate cached data. + */ + void _clearData() + { + _initializedData = false; + _dirtyData = false; + _activeData.clear(); + _activeStat.clear(); + _latestData.clear(); + _latestStat.clear(); + } + /** + Whether the ZkFuseFile instance is a zombie. + + It is a zombie if it is not currently open, i.e. its reference count + is 0. + */ + bool _isZombie() const + { + return (_refCount == 0); + } + /** + Whether the ZkFuseFile instance is currently opened as a regular file + only once. + + It is used to determine when the cached data can be replaced with + the latest data. \see _activeData. + + \return true if its currently opened as a regular file only once. + */ + bool _isOnlyRegOpen() const + { + return ((_refCount - _openDirCount) == 1); + } + /** + Get attributes without accessing metadata. + + The atime and mtime returned does not take into consideration + overrides present in a matadata file. + + \return 0 if successful, otherwise negative errno. + \param stbuf return attributes here. + \param nameType specifies the ZkFuseNameType of the ZkFuse path used + to get attributes. It influences whether the directory + or regular file attributes are returned. + */ + int _getattrNoMetaAccess(struct stat & stbuf, ZkFuseNameType nameType) const + { + int res = 0; + if (_deleted) { + LOG_DEBUG(LOG, "deleted"); + res = -ENOENT; + } + else if (!_isInitialized()) { + LOG_DEBUG(LOG, "not initialized"); + res = -EIO; + } + else { + assert(_isInitialized()); + bool isRegular = _isRegNameType(nameType); + if (isRegular) { + LOG_DEBUG(LOG, "regular"); + stbuf.st_mode = regMode; + stbuf.st_nlink = 1; + stbuf.st_size = _activeData.size(); + } else { + LOG_DEBUG(LOG, "directory"); + stbuf.st_mode = dirMode; + stbuf.st_nlink = + _children.size() + (_activeData.empty() ? 0 : 1); + stbuf.st_size = stbuf.st_nlink; + } + stbuf.st_uid = _getUid(); + stbuf.st_gid = _getGid(); + /* IMPORTANT: + * Conversion to secs from millisecs must occur before + * assigning to st_atime, st_mtime, and st_ctime. Otherwise + * truncating from 64-bit to 32-bit will cause lost of + * most significant 32-bits before converting to secs. + */ + stbuf.st_atime = millisecsToSecs(_activeStat.mtime); + stbuf.st_mtime = millisecsToSecs(_activeStat.mtime); + stbuf.st_ctime = millisecsToSecs(_activeStat.ctime); + stbuf.st_blksize = _getBlkSize(); + stbuf.st_blocks = + (stbuf.st_size + stbuf.st_blksize - 1) / stbuf.st_blksize; + res = 0; + } + return res; + } + /** + Get the context that should be registered with the data and + children watches. + + The returned context is a pointer to the ZkFuseFile instance + cast to the desired ContextType. + + \return the context. + */ + ZooKeeperAdapter::ContextType _getZkContext() const + { + return (ZooKeeperAdapter::ContextType) NULL; + } + + /** + DataListener - listener that listens for ZooKeeper data events + and calls dataEventReceived on the ZkFuseFile instance + identified by the event context. + \see dataEventReceived + */ + class DataListener : public ZKEventListener { + public: + /** + Received a data event and invoke ZkFuseFile instance obtained from + event context to handle the event. + */ + virtual void eventReceived(const ZKEventSource & source, + const ZKWatcherEvent & event) + { + assert(event.getContext() != 0); + ZkFuseFile * file = static_cast(event.getContext()); + file->dataEventReceived(event); + } + }; + + /** + DataListener - listener that listens for ZooKeeper children events + and calls childrenEventReceived on the ZkFuseFile instance + identified by the event context. + \see childrenEventReceived + */ + class ChildrenListener : public ZKEventListener { + public: + /** + Received a children event and invoke ZkFuseFile instance obtained from + event context to handle the event. + */ + virtual void eventReceived(const ZKEventSource & source, + const ZKWatcherEvent & event) + { + assert(event.getContext() != 0); + ZkFuseFile * file = static_cast(event.getContext()); + file->childrenEventReceived(event); + } + }; + + /** + Globally shared DataListener. + */ + static DataListener _dataListener; + /** + Globally shared ChildrenListener. + */ + static ChildrenListener _childrenListener; + + public: + /** + Constructor. + + Sets reference count to one, i.e. it has been constructed because + a client is trying to open the path. \see _refCount. + Sets deleted to true. \see _deleted. + Sets number of currently directory opens to zero. \see _openDirCount. + Invalidate cach for children information and data. + + \param manager the ZkFuseHandleManager instance who is creating this + ZkFuseFile instance. + \param handle the handle assigned by the ZkFuseHandleManager instance + for this ZkFuseFile instance. + \param path the ZooKeeper path represented by this ZkFuseFile instance. + */ + ZkFuseFile(const ZkFuseHandleManager::SharedPtr & manager, + const int handle, + const std::string & path) + : _manager(manager), + _handle(handle), + _path(path), + _mutex(), + _refCount(1), + _deleted(true), + /* children stuff */ + _openDirCount(0), + _initializedChildren(false), + _hasChildrenListener(false), + _children(), + /* data stuff */ + _initializedData(false), + _hasDataListener(false), + _dirtyData(false), + _activeData(), + _activeStat(), + _latestData(), + _latestStat() + { + LOG_DEBUG(LOG, "constructor() path %s", _path.c_str()); + } + /** + Destructor. + */ + ~ZkFuseFile() + { + LOG_DEBUG(LOG, "destructor() path %s", _path.c_str()); + + assert(_isZombie()); + _clearChildren(); + _clearData(); + } + /** + Whether the ZooKeeper node represented by this ZkFuseFile instance + has been deleted. + \see _deleted + + \return true if it is deleted. + */ + bool isDeleted() const + { + AutoLock lock(_mutex); + return _deleted; + } + /** + Return the path of the ZooKeeper node represented by this ZkFuseFile + instance. + \see _path. + + \return the ZooKeeper node's path. + */ + const string & getPath() const + { + return _path; + } + /** + Add a childPath to the children information cache. + + \return 0 if successful, otherwise return negative errno. + \param childPath the ZooKeeper path of the child. + */ + int addChild(const std::string & childPath) + { + LOG_DEBUG(LOG, "addChild(childPath %s) path %s", + childPath.c_str(), _path.c_str()); + + int res = 0; + { + AutoLock lock(_mutex); + if (_initializedChildren) { + NodeNames::iterator it = + std::find(_children.begin(), _children.end(), childPath); + if (it == _children.end()) { + LOG_DEBUG(LOG, "child not found, adding child path"); + _children.push_back(childPath); + res = 0; + } + else { + LOG_DEBUG(LOG, "child found"); + res = -EEXIST; + } + } + } + + LOG_DEBUG(LOG, "addChild returns %d", res); + return res; + } + /** + Remove a childPath from the children information cache. + + \return 0 if successful, otherwise return negative errno. + \param childPath the ZooKeeper path of the child. + */ + int removeChild(const std::string & childPath) + { + LOG_DEBUG(LOG, "removeChild(childPath %s) path %s", + childPath.c_str(), _path.c_str()); + + int res = 0; + { + AutoLock lock(_mutex); + if (_initializedChildren) { + NodeNames::iterator it = + std::find(_children.begin(), _children.end(), childPath); + if (it != _children.end()) { + LOG_DEBUG(LOG, "child found"); + _children.erase(it); + res = 0; + } + else { + LOG_DEBUG(LOG, "child not found"); + res = -ENOENT; + } + } + } + + LOG_DEBUG(LOG, "removeChild returns %d", res); + return res; + } + /** + Invalidate the cached children information and cached data. + \see _clearChildren + \see _clearData + + \param clearChildren set to true to invalidate children information cache. + \param clearData set to true to invalidate data cache. + */ + void clear(bool clearChildren = true, bool clearData = true) + { + LOG_DEBUG(LOG, "clear(clearChildren %d, clearData %d) path %s", + clearChildren, clearData, _path.c_str()); + + { + AutoLock lock(_mutex); + if (clearChildren) { + _clearChildren(); + } + if (clearData) { + _clearData(); + } + } + } + /** + Whether reference count is zero. + \see _refCount + + \return true if reference count is zero. + */ + bool isZombie() const + { + AutoLock lock(_mutex); + + return (_refCount == 0); + } + /** + Increment the reference count of the ZkFuseFile instance. + + This method may be called by a ZkFuseFileManager instance while + holding the ZkFuseFileManager's _mutex. To avoid deadlocks, + this methods must never invoke a ZkFuseFileManager instance + directly or indirectly while holding the ZkFuseFile instance's + _mutex. + \see _refCount + + \return the post-increment reference count. + \param count value to increment the reference count by. + */ + int incRefCount(int count = 1) + { + LOG_DEBUG(LOG, "incRefCount(count %d) path %s", count, _path.c_str()); + + int res = 0; + { + AutoLock lock(_mutex); + _refCount += count; + assert(_refCount >= 0); + res = _refCount; + } + + LOG_DEBUG(LOG, "incRefCount returns %d", res); + return res; + } + /** + Decrement the reference count of the ZkFuseFile instance. + + This method may be called by a ZkFuseFileManager instance while + holding the ZkFuseFileManager's _mutex. To avoid deadlocks, + this methods must never invoke a ZkFuseFileManager instance + directly or indirectly while holding the ZkFuseFile instance's + _mutex. + \see _refCount + + \return the post-decrement reference count. + \param count value to decrement the reference count by. + */ + int decRefCount(int count = 1) + { + return incRefCount(-count); + } + /** + Increment the count of number times the ZkFuseFile instance has + been opened as a directory. + + This count is incremented by opendir and decremented by releasedir. + \see _openDirCount. + + \return the post-increment count. + \param count the value to increment the count by. + */ + int incOpenDirCount(int count = 1) + { + LOG_DEBUG(LOG, "incOpenDirCount(count %d) path %s", + count, _path.c_str()); + + int res = 0; + { + AutoLock lock(_mutex); + _openDirCount += count; + assert(_openDirCount >= 0); + res = _openDirCount; + assert(_openDirCount <= _refCount); + } + + LOG_DEBUG(LOG, "incOpenDirCount returns %d", res); + return res; + + } + /** + Decrement the count of number times the ZkFuseFile instance has + been opened as a directory. + + This count is incremented by opendir and decremented by releasedir. + \see _openDirCount. + + \return the post-decrement count. + \param count the value to decrement the count by. + */ + int decOpenDirCount(int count = 1) + { + return incOpenDirCount(-count); + } + /** + Whether ZkFuse should present the ZooKeeper node as a ZkFuse + directory by taking into account the specified ZkFuseNameType. + + The ZkFuseNameType may override the default ZkFuse presentation of + a ZooKeeper node. + \see _isDirNameType + + \return true if ZkFuse should present the ZooKeeper node as a ZkFuse + directory. + \param nameType specifies the ZkFuseNameType. + */ + bool isDirNameType(ZkFuseNameType nameType) const + { + return _isDirNameType(nameType, true); + } + /** + Whether ZkFuse should present the ZooKeeper node as a ZkFuse + regular file by taking into account the specified ZkFuseNameType. + + The ZkFuseNameType may override the default ZkFuse presentation of + a ZooKeeper node. + \see _isRegNameType + + \return true if ZkFuse should present the ZooKeeper node as a ZkFuse + regular file. + \param nameType specifies the ZkFuseNameType. + */ + bool isRegNameType(ZkFuseNameType nameType) const + { + return _isRegNameType(nameType, true); + } + /** + Get the active data. + \see _activeData + + \param data return data here. + */ + void getData(Data & data) const + { + AutoLock lock(_mutex); + + data = _activeData; + } + /** + Set the active data. + \see _activeData + + Return -EFBIG is the data to be written is bigger than the maximum + permitted size (and no data is written). + + \return 0 if successful, otherwise return negative errno. + \param data set to this data. + \param doFlush whether to flush the data to the ZooKeeper node. + */ + int setData(const Data & data, bool doFlush) + { + LOG_DEBUG(LOG, "setData(doFlush %d) path %s", doFlush, _path.c_str()); + int res = 0; + + if (data.size() > maxDataFileSize) { + res = -EFBIG; + } + else { + AutoLock lock(_mutex); + _activeData = data; + _dirtyData = true; + if (doFlush) { + res = _flush(); + } + } + + LOG_DEBUG(LOG, "setData() returns %d", res); + return res; + } + /** + Update the children information and the data caches as needed. + + This method is invoked when a ZkFuse regular file or directory + implemented by this ZkFuseFile instance is opened, e.g. + using open or opendir. It attempts to: + - make sure that the cache has valid children information + - register for watches for changes if no previous watches have + been registered. + + The newFile flag indicates if the ZkFuseFile instance has just + been constructed and that ZooKeeper has not been contacted to + determine if the ZooKeeper path for this file really exist. + When a ZkFuseFile instance is created, the _deleted flag is set to + true because it is safer to assume that the ZooKeeper node does + not exist. The newFile flag causes the _deleted flag to be + ignored and ZooKeeper to be contacted to update the caches. + + If the newFile flag is false, then the ZkFuseFile instance is + currently open and have been opened before. Hence, these previous + opens should have contacted ZooKeeper and would like learned from + ZooKeeper whether the ZooKeeper path exists. Therefore, + the _deleted flag should be trustworthy, i.e. it has accurate + information on whether the ZooKeeper path actually exists. + + \return 0 if successful, otherwise return negative errno. + \param newFile set to true if the ZkFuseFile instance is newly created. + */ + int update(bool newFile) + { + LOG_DEBUG(LOG, "update(newFile %d) path %s", newFile, _path.c_str()); + + int res = 0; + { + AutoLock lock(_mutex); + + /* At this point, cannot be zombie. + */ + assert(!_isZombie()); + if (!newFile && _deleted) { + /* Deleted file, don't bother to update caches */ + LOG_DEBUG(LOG, "deleted, not new file"); + res = -ENOENT; + } + else { + try { + LOG_DEBUG(LOG, "initialized children %d, data %d", + _initializedChildren, _initializedData); + LOG_DEBUG(LOG, "has children watch %d, data watch %d", + _hasChildrenListener, _hasDataListener); + /* + * Children handling starts here. + * If don't have children listener, + * then must establish listener. + * If don't have cached children information, + * then must get children information. + * It just happens, that the same ZooKeeper API + * is used for both. + */ + if (_initializedChildren == false || + _hasChildrenListener == false +#ifdef ZOOKEEPER_ROOT_CHILDREN_WATCH_BUG + /* HACK for root node because changes to children + * on a root node does not cause children watches to + * fire. + */ + || _path.length() == 1 +#endif // ZOOKEEPER_ROOT_CHILDREN_WATCH_BUG + ) { + LOG_DEBUG(LOG, "update children"); + NodeNames children; + _manager->getCommon().getZkAdapter()-> + getNodeChildren( children, _path, + &_childrenListener, _getZkContext()); + _hasChildrenListener = true; + LOG_DEBUG(LOG, "update children done"); + _children.swap(children); + _initializedChildren = true; + /* Since getNodeChildren is successful, the + * path must exist */ + _deleted = false; + } + else { + /* Children information is fresh since + * it is initialized and and have been + * updated by listener. + */ + } + /* + * Data handling starts here. + */ + assert(newFile == false || _isOnlyRegOpen()); + if (!_isOnlyRegOpen()) { + /* If is already currently opened by someone, + * then don't update data with latest from ZooKeeper, + * use current active data (which may be initialized + * or not). + * \see _activeData + */ + LOG_DEBUG(LOG, "node currently in-use, no data update"); + } + else { + /* If not opened/reopened by someone else, + * then perform more comprehensive checks of + * to make data and listener is setup correctly. + * If don't have data listener, + * then must establish listener. + * If don't have cached data, + * then must get data. + * It just happens, that the same ZooKeeper API + * is used for both. + */ + LOG_DEBUG(LOG, "node first use or reuse"); + if (_initializedData == false || + _hasDataListener == false) { + /* Don't have any data for now or need to register + * for callback */ + LOG_DEBUG(LOG, "update data"); + _latestData = + _manager->getCommon().getZkAdapter()-> + getNodeData(_path, &_dataListener, + _getZkContext(), + &_latestStat); + _hasDataListener = true; + LOG_DEBUG(LOG, + "update data done, latest version %d", + _latestStat.version); + /* Since getNodeData is successful, the + * path must exist. */ + _deleted = false; + } + else { + /* Data is fresh since it is initialized and + * and have been updated by listener. + */ + } + /* Update active data to the same as the most + * recently acquire data. + */ + _activeData = _latestData; + _activeStat = _latestStat; + _initializedData = true; + _dirtyData = false; + LOG_DEBUG(LOG, "update set active version %d", + _activeStat.version); + } + res = 0; + } catch (const ZooKeeperException & e) { + /* May have ZNONODE exception if path does exist. */ + if (e.getZKErrorCode() == ZNONODE) { + LOG_DEBUG(LOG, "update %s exception %s", + _path.c_str(), e.what()); + /* Path does not exist, set _deleted, + * clear children information cache + */ + _deleted = true; + _clearChildren(); + res = -ENOENT; + } else { + LOG_ERROR(LOG, "update %s exception %s", + _path.c_str(), e.what()); + res = -EIO; + } + } + } + } + + LOG_DEBUG(LOG, "update returns %d", res); + return res; + } + /** + Process a data event. + + This method may: + - Invalidate the data cache. + - Invoke ZooKeeper to update the data cache and register a new + data watch so that the cache can be kept in-sync with the + ZooKeeper node's data. + + This method does not change the active data. Active data will be + changed to a later version by update() at the appropriate time. + \see update. + */ + void dataEventReceived(const ZKWatcherEvent & event) + { + bool reclaim = false; + int eventType = event.getType(); + int eventState = event.getState(); + + /* + IMPORTANT: + + Do not mark ZkFuseFile instance as deleted when a ZOO_DELETED_EVENT + is received without checking with ZooKeeper. An example of + problematic sequence would be: + + 1. Create node. + 2. Set data and watch. + 3. Delete node. + 4. Create node. + 5. Deleted event received. + + It is a bug to mark the ZkFuseFile instance as deleted after + step 5 because the node exists. + + Therefore, this method should always contact ZooKeeper to keep the + data cache (and deleted status) up-to-date if necessary. + */ + LOG_DEBUG(LOG, "dataEventReceived() path %s, type %d, state %d", + _path.c_str(), eventType, eventState); + { + AutoLock lock(_mutex); + + _hasDataListener = false; + /* If zombie, then invalidate cached data. + * This clears _initializedData and eliminate + * the need to get the latest data from ZooKeeper and + * re-register data watch. + */ + if (_isZombie() && _initializedData) { + LOG_DEBUG(LOG, "invalidate data"); + _clearData(); + } + else if ((_refCount - _openDirCount) > 0) { + /* Don't invalidate cached data because clients of currently + * open files don't expect the data to change from under them. + * If data acted upon by these clients have become stale, + * then the clients will get an error when ZkFuse attempts to + * flush dirty data. The clients will not get error + * notification if they don't modify the stale data. + * + * If data cache is cleared here, then the following code + * to update data cache and re-register data watch will not + * be executed and may result in the cached data being + * out-of-sync with ZooKeeper. + */ + LOG_WARN(LOG, + "%s data has changed while in-use, " + "type %d, state %d, refCount %d", + _path.c_str(), eventType, eventState, _refCount); + } + /* If cache was valid and still connected + * then get the latest data from ZooKeeper + * and re-register data watch. This is required to keep + * the data cache in-sync with ZooKeeper. + */ + if (_initializedData && + eventState == ZOO_CONNECTED_STATE + ) { + try { + LOG_DEBUG(LOG, "register data watcher"); + _latestData = + _manager->getCommon().getZkAdapter()-> + getNodeData(_path, &_dataListener, _getZkContext(), + &_latestStat); + _hasDataListener = true; + LOG_DEBUG(LOG, + "get data done, version %u, cversion %u done", + _latestStat.version, _latestStat.cversion); + _deleted = false; + } catch (const ZooKeeperException & e) { + if (e.getZKErrorCode() == ZNONODE) { + _deleted = true; + _clearChildren(); + } + LOG_ERROR(LOG, "dataEventReceived %s exception %s", + _path.c_str(), e.what()); + } + } + } + LOG_DEBUG(LOG, "dataEventReceived return %d", reclaim); + } + /** + Process a children event. + + This method may: + - Invalidate the children information cache. + - Invoke ZooKeeper to update the children cache and register a new + data watch so that the cache can be kept in-sync with the + ZooKeeper node's children information. + */ + void childrenEventReceived(const ZKWatcherEvent & event) + { + bool reclaim = false; + int eventType = event.getType(); + int eventState = event.getState(); + + LOG_DEBUG(LOG, "childrenEventReceived() path %s, type %d, state %d", + _path.c_str(), eventType, eventState); + { + AutoLock lock(_mutex); + + _hasChildrenListener = false; + /* If zombie or disconnected, then invalidate cached children + * information. This clears _initializedChildren and eliminate + * the need to get the latest children information and + * re-register children watch. + */ + if (_initializedChildren && + (_isZombie() || eventState != ZOO_CONNECTED_STATE)) { + LOG_DEBUG(LOG, "invalidate children"); + _clearChildren(); + } + else if (_initializedChildren) { + /* Keep cached children information so that we have some + * children information if get new children information + * fails. If there is failure, then on next open, + * update() will attempt again to get children information + * again because _hasChildrenListener will be false. + * + * If children information cache is cleared here, then + * the following code to update children information cache + * and re-register children watch will not be executed + * and may result in the cached children information being + * out-of-sync with ZooKeeper. + * + * The children cache will be cleared if unable to + * get children and re-establish watch. + */ + LOG_WARN(LOG, + "%s children has changed while in-use, " + "type %d, state %d, refCount %d", + _path.c_str(), eventType, eventState, _refCount); + } + /* If children cache was valid and still connected, + * then get the latest children information from ZooKeeper + * and re-register children watch. This is required to + * keep the children information cache in-sync with ZooKeeper. + */ + if (_initializedChildren && + eventState == ZOO_CONNECTED_STATE + ) { + /* Should try to keep the cache in-sync, register call + * callback again and get current children. + */ + try { + LOG_DEBUG(LOG, "update children"); + NodeNames children; + _manager->getCommon().getZkAdapter()-> + getNodeChildren(children, _path, + &_childrenListener, _getZkContext()); + _hasChildrenListener = true; + LOG_DEBUG(LOG, "update children done"); + _children.swap(children); + _deleted = false; + } catch (const ZooKeeperException & e) { + if (e.getZKErrorCode() == ZNONODE) { + _deleted = true; + _clearChildren(); + } + LOG_ERROR(LOG, "childrenEventReceived %s exception %s", + _path.c_str(), e.what()); + _children.clear(); + } + } + } + LOG_DEBUG(LOG, "childrenEventReceived returns %d", reclaim); + } + /** + Truncate or expand the size of the cached active data. + + This method only changes the size of the cached active data. + This change is committed to ZooKeeper when the cached data + is written to the ZooKeeper node by flush(). + + Return -EFBIG is the requested size exceeds the maximum. + + \return 0 if successful, otherwise negative errno. + \param size the requested size. + */ + int truncate(off_t size) + { + int res = 0; + + { + AutoLock lock(_mutex); + res = _truncate(size); + } + + return res; + } + /** + Copy range of active data into specified output buffer. + + \return if successful, return number of bytes copied, otherwise + return negative errno. + \param buf address of the output buffer. + \param size size of the output buffer and desired number of bytes to copy. + \param offset offset into active data to start copying from. + */ + int read(char *buf, size_t size, off_t offset) const + { + LOG_DEBUG(LOG, "read(size %zu, off_t %zu) path %s", + size, offset, _path.c_str()); + + int res = 0; + + { + AutoLock lock(_mutex); + if (!_initializedData) { + LOG_DEBUG(LOG, "not initialized"); + res = -EIO; + } + else { + off_t fileSize = _activeData.size(); + if (offset > fileSize) { + LOG_DEBUG(LOG, "offset > fileSize %zu", fileSize); + res = 0; + } + else { + if (offset + size > fileSize) { + size = fileSize - offset; + LOG_DEBUG(LOG, + "reducing read size to %zu for fileSize %zu", + size, fileSize); + } + copy(_activeData.begin() + offset, + _activeData.begin() + offset + size, + buf); + res = size; + } + } + } + + LOG_DEBUG(LOG, "read returns %d", res); + return res; + } + /** + Copy buffer content to active data. + + \return if successful, return number of bytes copied, otherwise + return negative errno. + \param buf address of the buffer. + \param size size of the input buffer and desired number of bytes to copy. + \param offset offset into active data to start copying to. + */ + int write(const char *buf, size_t size, off_t offset) + { + LOG_DEBUG(LOG, "write(size %zu, off_t %zu) path %s", + size, offset, _path.c_str()); + + int res = 0; + + { + AutoLock lock(_mutex); + if (!_initializedData) { + LOG_DEBUG(LOG, "not initialized"); + res = -EIO; + } + else if (offset >= maxDataFileSize) { + LOG_DEBUG(LOG, "offset > maxDataFileSize %u", maxDataFileSize); + res = -ENOSPC; + } + else { + if (offset + size > maxDataFileSize) { + LOG_DEBUG(LOG, + "reducing write size to %zu " + "for maxDataFileSize %u", + size, maxDataFileSize); + size = maxDataFileSize - offset; + } + off_t fileSize = _activeData.size(); + if (offset + size > fileSize) { + LOG_DEBUG(LOG, "resizing to %zu", offset + size); + _activeData.resize(offset + size); + } + copy(buf, buf + size, _activeData.begin() + offset); + memcpy(&_activeData[offset], buf, size); + _dirtyData = true; + res = size; + } + } + + LOG_DEBUG(LOG, "write returns %d", res); + return res; + } + /** + Flush data to the ZooKeeper node. + + If cached active data has been modified, flush it to the ZooKeeper node. + Returns -EIO if the data cannot be written because the cached active + data is not the expected version, i.e. ZooKeeper returns ZBADVERSION. + -EIO may also indicate a more general failure, such as unable to + communicate with ZooKeeper. + + \return 0 if successful, otherwise negative errno. + */ + int flush() + { + int res = 0; + { + AutoLock lock(_mutex); + res = _flush(); + } + return res; + } + /** + Close of the ZkFuse regular file represented by the ZkFuseFile instance. + + This may: + - Flush dirty data to the ZooKeeper node, and return the result of the + flush operation. + - Reclaim the ZkFuseFile instance. + \see ZkFuseHandleManaer::reclaimIfNecessary + + \return result of flush operation - 0 if successful, + otherwise negative errno. + */ + int close() + { + LOG_DEBUG(LOG, "close() path %s", _path.c_str()); + int res = 0; + + bool reclaim = false; + { + AutoLock lock(_mutex); + res = _flush(); + if (_deleted) { + _clearData(); + _clearChildren(); + } + } + _manager->deallocate(_handle); + + LOG_DEBUG(LOG, "close returns %d", res); + return res; + } + /** + Get ZkFuse regular file or directory attributes. + + \return 0 if successful, otherwise negative errno. + \param stbuf return attributes here. + \param nameType specifies the ZkFuseNameType of the ZkFuse path used + to get attributes. It influences whether the directory + or regular file attributes are returned. + */ + int getattr(struct stat & stbuf, ZkFuseNameType nameType) const + { + LOG_DEBUG(LOG, "getattr(nameType %d) path %s", + int(nameType), _path.c_str()); + + int res = 0; + int version = 0; + std::string metaPath; + { + AutoLock lock(_mutex); + + res = _getattrNoMetaAccess(stbuf, nameType); + if (res == 0) { + version = _activeStat.version; + metaPath = _getChildPath( + ((stbuf.st_mode & S_IFMT) == S_IFREG) ? + _manager->getCommon().getRegMetadataName() : + _manager->getCommon().getDirMetadataName()); + if (_hasChildPath(metaPath) == false) { + metaPath.clear(); + } + } + } + if (res == 0 && metaPath.empty() == false) { + Data data; + int metaRes = _manager->getData(metaPath, data); + LOG_DEBUG(LOG, "metaRes %d dataSize %zu", + metaRes, data.size()); + if (metaRes == 0 && data.empty() == false) { + Metadata metadata; + _decodeMetadata(data, metadata); + LOG_DEBUG(LOG, "metadata version %u active version %u", + metadata.version, version); + if (metadata.version == version) { + /* IMPORTANT: + * Must convert from millisecs to secs before setting + * st_atime and st_mtime to avoid truncation error + * due to 64-bit to 32-bit conversion. + */ + stbuf.st_atime = millisecsToSecs(metadata.atime); + stbuf.st_mtime = millisecsToSecs(metadata.mtime); + } + } + } + + LOG_DEBUG(LOG, "getattr returns %d", res); + return res; + } + /** + Read directory entries. + This interface is defined by FUSE. + + \return 0 if successful, otherwise negative errno. + \param buf output buffer to store output directory entries. + \param filler function used to fill the output buffer. + \param offset start filling from a specific offset. + */ + int readdir(void *buf, fuse_fill_dir_t filler, off_t offset) const + { + LOG_DEBUG(LOG, "readdir(offset %zu) path %s", offset, _path.c_str()); + int res = 0; + + int dataFileIndex = -1; + unsigned leftTrim = 0; + typedef std::pair DirEntry; + typedef std::vector DirEntries; + DirEntries dirEntries; + + /* Get directory entries in two phase to avoid invoking + * ZkFuseHandleManager while holding _mutex. + * In first phase, get all the names of child nodes starting + * at offset. Also remember their index for use in second phase. + * The first phase hold _mutex. + */ + { + AutoLock lock(_mutex); + if (!_isInitialized()) { + LOG_DEBUG(LOG, "not initialized"); + res = -EIO; + } + else { + leftTrim = (_path.length() == 1 ? 1 : _path.length() + 1); + unsigned start = offset; + unsigned i; + for (i = start; i < _children.size(); i++) { + const std::string & childName = _children[i]; + if (_isMeta(childName)) { + continue; + } + dirEntries.push_back(DirEntry(childName, i)); + } + if (i == _children.size() && !_activeData.empty()) { + dataFileIndex = i + 1; + } + res = 0; + } + } + + /* Second phase starts here. + * DONOT hold _mutex as this phase invokes ZkFuseHandleManager to + * get attributes for the directory entries. + */ + if (res == 0) { + bool full = false; + for (DirEntries::const_iterator it = dirEntries.begin(); + it != dirEntries.end(); + it++) { + + ZkFuseAutoHandle childAutoHandle(_manager, it->first); + int childRes = childAutoHandle.get(); + if (childRes >= 0) { + struct stat stbuf; + int attrRes = childAutoHandle.getFile()-> + getattr(stbuf, ZkFuseNameDefaultType); + if (attrRes == 0) { + if (filler(buf, it->first.c_str() + leftTrim, + &stbuf, it->second + 1)) { + LOG_DEBUG(LOG, "filler full"); + full = true; + break; + } + } + } + } + if (full == false && dataFileIndex != -1) { + LOG_DEBUG(LOG, "include data file name"); + struct stat stbuf; + int attrRes = getattr(stbuf, ZkFuseNameRegType); + if (attrRes == 0) { + filler(buf, + _manager->getCommon().getDataFileName().c_str(), + &stbuf, dataFileIndex + 1); + } + } + } + + LOG_DEBUG(LOG, "readdir returns %d", res); + return res; + } + /** + Set the access time and modified time. + + Set the access and modifieds times on the ZkFuse regular file + or directory represented by this ZkFuseFile instance. + + Since there is no interface to change these times on a + ZooKeeper node, ZkFuse simulates this by writing to a + metadata node which is a child node of the ZooKeeper node. + ZkFuse writes the current version, the specified access + and modified times to the metadata node. + + When get attributes is invoked, get attributes will check + for the presence of this metadata node and if the version + number matches the current data version, then get attributes + will return the access and modified times stored in the + metadata node. + + \return 0 if successful, otherwise negative errno. + \param atime access time in milliseconds. + \param mtime modified time in milliseconds. + \param nameType specifies the ZkFuseNameType of the ZkFuse path used + to set access and modified times. It influences + whether the directory or regular file access and + modified times are set. + */ + int utime(uint64_t atime, uint64_t mtime, ZkFuseNameType nameType) + { + LOG_DEBUG(LOG, + "utime(atime %llu, mtime %llu, nameType %d) path %s", + (unsigned long long) atime, + (unsigned long long) mtime, + (int) nameType, _path.c_str()); + + int res = 0; + std::string metaPath; + bool exists = false; + Data data; + { + AutoLock lock(_mutex); + + if (!_isInitialized()) { + LOG_DEBUG(LOG, "not initialized"); + res = -EIO; + } + else { + bool isRegular = _isRegNameType(nameType); + Metadata metadata; + metadata.version = _activeStat.version; + metadata.atime = atime; + metadata.mtime = mtime; + metaPath = _getChildPath( + isRegular ? + _manager->getCommon().getRegMetadataName() : + _manager->getCommon().getDirMetadataName()); + exists = _hasChildPath(metaPath); + _encodeMetadata(metadata, data); + res = 0; + } + } + if (res == 0 && metaPath.empty() == false) { + res = _manager->setData(metaPath, data, exists, true); + } + + LOG_DEBUG(LOG, "utime returns %d", res); + return res; + } + /** + Remove a ZkFuse directory. + + If force is true, then the ZooKeeper node and its decendants + will be deleted. + + If force is false, then this method implements the semantics + of removing a ZkFuse directory. It will delete the ZooKeeper node + only if the ZooKeeper node have no data and no non-metadata + children. + - Return -ENOTDIR if the ZooKeeper node is not considered + to be a directory (after taking into consideration the specified + ZkFuseNameType). + - Return -ENOTEMPTY if the ZooKeeper node has data or it has + non-metadata children. + - Return -ENOENT if the ZooKeeper cannot be deleted, usually this + is because it does not exist. + + \return 0 if successful, otherwise negative errno. + \param nameType the ZkFuseNameType of the path used to specify the + directory to be removed. It influences whether ZkFuse + considers the ZooKeeper node to be a regular file or + directory. \see ZkFuseNameType + \param force set to true to bypass ZkFuse rmdir semantic check. + */ + int rmdir(ZkFuseNameType nameType, bool force) + { + int res = 0; + + { + AutoLock lock(_mutex); + res = _rmdir(nameType, force); + } + if (res == 0) { + _manager->removeChildFromParent(_path); + } + return res; + } + /** + Remove a ZkFuse regular file. + + This method implements the semantics of removing a ZkFuse regular file. + - If the ZkFuse regular file represents the data part of the + ZooKeeper node which is presented as a ZkFuse directory, + the regular file is virtually deleted by truncating the + ZooKeeper node's data. Readdir will not synthesize a regular + file entry for the data part of a ZooKeeper node if + the ZooKeeper node has no data. + - If the ZkFuse regular file represents the data part of the + ZooKeeper node which is presented as a ZkFuse regular file, + the ZooKeeper node and its decendants are deleted. + + Returns -EISDIR if the ZkFuse regular file cannot be deleted + because ZkFuse consider it to be a directory. + + \return 0 if successful, otherwise negative errno. + \param nameType the ZkFuseNameType of the path used to specify the + directory to be removed. It influences whether ZkFuse + considers the ZooKeeper node to be a regular file or + directory. \see ZkFuseNameType + */ + int unlink(ZkFuseNameType nameType) + { + int res = 0; + { + AutoLock lock(_mutex); + res = _unlink(nameType); + } + if (res == 0) { + _manager->removeChildFromParent(_path); + } + return res; + } + /** + Utility function to construct a ZooKeeper path for a child + of a ZooKeeper node. + + \return the full path of the child. + \param parent the parent's full path. + \param child the child's parent component. + */ + static std::string buildChildPath(const std::string & parent, + const std::string & child) + { + std::string s; + s.reserve(parent.length() + child.length() + 32); + if (parent.length() > 1) { + // special case for root dir + s += parent; + } + s += "/"; + s += child; + return s; + } +}; + +ZkFuseFile::DataListener ZkFuseFile::_dataListener; +ZkFuseFile::ChildrenListener ZkFuseFile::_childrenListener; + +void ZkFuseAutoHandle::reset(int handle) +{ + int old = _handle; + ZkFuseFilePtr oldFile = _file; + _handle = handle; + _initFile(); + if (old >= 0) { + assert(oldFile != NULL); + oldFile->close(); + } +} + +ZkFuseHandleManager::Handle +ZkFuseHandleManager::allocate(const std::string & path, bool & newFile) +{ + LOG_DEBUG(LOG, "allocate(path %s)", path.c_str()); + + Handle handle; + { + AutoLock lock(_mutex); + Map::iterator it = _map.find(path); + if (it == _map.end()) { + LOG_DEBUG(LOG, "not found"); + if (_freeList.empty()) { + handle = _files.size(); + _files.resize(handle + 1); + LOG_DEBUG(LOG, "free list empty, resize handle %d", handle); + } else { + handle = _freeList.back(); + _freeList.pop_back(); + LOG_DEBUG(LOG, "get from free list, handle %d", handle); + } + assert(_files[handle] == NULL); + _files[handle] = + new ZkFuseFile(SharedPtr(_thisWeakPtr), handle, path); + /* Not really supposed to invoke the new ZkFuseFile instance + * because this method is not supposed to invoke ZkFuseFile + * methods that while holding _mutex. However, it is safe + * to do without casuing deadlock because these methods + * are known not to invoke other methods, especially one + * that invoke this ZkFuseHandleManager instance. + */ + assert(_files[handle]->incRefCount(0) == 1); + _map[path] = handle; + _numInUse++; + LOG_DEBUG(LOG, "numInUse %u", _numInUse); + newFile = true; + } else { + LOG_DEBUG(LOG, "found"); + handle = it->second; + assert(_files[handle] != NULL); + int refCount = _files[handle]->incRefCount(); + if (refCount == 1) { + _numInUse++; + LOG_DEBUG(LOG, "resurrecting zombie, numInUse %u", _numInUse); + } + newFile = false; + } + } + + LOG_DEBUG(LOG, "allocate returns %d, newFile %d", handle, newFile); + return handle; +} + +void ZkFuseHandleManager::deallocate(Handle handle) +{ + LOG_DEBUG(LOG, "deallocate(handle %d)", handle); + + if (handle >= 0) { + bool reclaim = false; + ZkFuseFilePtr file; + { + AutoLock lock(_mutex); + file = _files[handle]; + assert(file != NULL); + int refCount = file->decRefCount(); + const std::string & path = file->getPath(); + LOG_DEBUG(LOG, "path %s ref count %d", path.c_str(), refCount); + if (refCount == 0) { + _numInUse--; + unsigned numCached = _files.size() - _numInUse; + if (numCached > _common.getCacheSize()) { + LOG_TRACE(LOG, + "reclaim path %s, cacheSize %u, filesSize %zu, " + "numInUse %u", + path.c_str(), + _common.getCacheSize(), _files.size(), _numInUse); + _map.erase(path); + _files[handle] = NULL; + _freeList.push_back(handle); + reclaim = true; + } + } + } + if (reclaim) { + delete file; + } + } + else { + LOG_DEBUG(LOG, "handle invalid"); + } + + LOG_DEBUG(LOG, "deallocate done"); +} + +void ZkFuseHandleManager::eventReceived(const ZKWatcherEvent & event) +{ + int eventType = event.getType(); + int eventState = event.getState(); + const std::string & path = event.getPath(); + LOG_DEBUG(LOG, "eventReceived() eventType %d, eventState %d, path %s", + eventType, eventState, path.c_str()); + + if (eventType == ZOO_DELETED_EVENT || + eventType == ZOO_CHANGED_EVENT || + eventType == ZOO_CHILD_EVENT) { + { + AutoLock lock(_mutex); + Map::iterator it = _map.find(path); + if (it != _map.end()) { + LOG_DEBUG(LOG, "path found"); + Handle handle = it->second; + ZkFuseFilePtr file = _files[handle]; + assert(file != NULL); + /* Prevent the ZkFuseFile instance from being + * deleted while handling the event. + */ + int refCount = file->incRefCount(); + if (refCount == 1) { + _numInUse++; + } + /* Pretent to be dir open. + */ + int dirCount = file->incOpenDirCount(); + { + /* _mutex is unlocked in this scope */ + AutoUnlockTemp autoUnlockTemp(lock); + if (eventType == ZOO_CHILD_EVENT) { + file->childrenEventReceived(event); + } + else if (eventType == ZOO_CHANGED_EVENT) { + file->dataEventReceived(event); + } + else { + assert(eventType == ZOO_DELETED_EVENT); + file->dataEventReceived(event); + // file->childrenEventReceived(event); + } + file->decOpenDirCount(); + deallocate(handle); + } + } + else { + LOG_WARN(LOG, + "path %s not found for event type %d, event state %d", + path.c_str(), eventType, eventState); + } + } + } + else if (eventType == ZOO_SESSION_EVENT) { + if (eventState == ZOO_CONNECTING_STATE) { + LOG_TRACE(LOG, "*** CONNECTING ***"); + { + AutoLock lock(_mutex); + for (int handle = 0; handle < _files.size(); handle++) { + ZkFuseFilePtr file = _files[handle]; + if (file != NULL) { + /* prevent the ZkFuseFile instance from being + * deleted while handling the event. + */ + int refCount = file->incRefCount(); + if (refCount == 1) { + _numInUse++; + } + /* Pretent to be dir open. + */ + int dirCount = file->incOpenDirCount(); + { + /* _mutex is unlocked in this scope */ + AutoUnlockTemp autoUnlockTemp(lock); + file->dataEventReceived(event); + file->childrenEventReceived(event); + file->decOpenDirCount(); + deallocate(handle); + } + /* this will eventually call decrement ref count */ + } + } + } + } + else if (eventState == ZOO_CONNECTED_STATE) { + LOG_TRACE(LOG, "*** CONNECTED ***"); + } + } + else { + LOG_WARN(LOG, + "eventReceived ignoring event type %d, event state %d, " + "path %s", eventType, eventState, path.c_str()); + } +} + +int ZkFuseHandleManager::getData(const std::string & path, + Data & data) +{ + LOG_DEBUG(LOG, "getData(path %s)", path.c_str()); + + int res = 0; + data.clear(); + ZkFuseAutoHandle autoHandle(SharedPtr(_thisWeakPtr), path); + res = autoHandle.get(); + if (res >= 0) { + autoHandle.getFile()->getData(data); + res = 0; + } + + LOG_DEBUG(LOG, "getData returns %d", res); + return res; +} + +int ZkFuseHandleManager::setData(const std::string & path, + const Data & data, + bool exists, + bool doFlush) +{ + LOG_DEBUG(LOG, "setData(path %s, exists %d)\n%s", + path.c_str(), exists, data.c_str()); + + int res = 0; + if (exists) { + res = open(path, false); + } else { + bool created; + res = mknod(path, S_IFREG, true, created); + } + if (res >= 0) { + ZkFuseAutoHandle autoHandle(SharedPtr(_thisWeakPtr), res); + res = autoHandle.getFile()->setData(data, doFlush); + } + + LOG_DEBUG(LOG, "setData returns %d", res); + return res; +} + +int ZkFuseHandleManager::mknod(const std::string & path, + mode_t mode, + bool mayExist, + bool & created) +{ + LOG_DEBUG(LOG, "mknod(path %s, mode %o, mayExist %d)", + path.c_str(), mode, mayExist); + + int res = 0; + created = false; + try { + if (S_ISREG(mode) == false && S_ISDIR(mode) == false) { + LOG_DEBUG(LOG, "bad mode %o", mode); + res = -EINVAL; + } + else { + Data data; + LOG_DEBUG(LOG, "create %s", path.c_str()); + created = + _common.getZkAdapter()->createNode(path, data, 0, false); + if (created) { + LOG_DEBUG(LOG, "created"); + if (S_ISDIR(mode)) { + /* is mkdir - create directory marker */ + std::string dirMetaPath = ZkFuseFile::buildChildPath + (path, _common.getDirMetadataName()); + LOG_DEBUG(LOG, "create %s", dirMetaPath.c_str()); + bool created; + int metaRes = mknod(dirMetaPath, S_IFREG, true, created); + if (metaRes >= 0) { + getFile(metaRes)->close(); + } + } + addChildToParent(path); + LOG_DEBUG(LOG, "open after create"); + res = open(path, true); + } else { + LOG_DEBUG(LOG, "create failed"); + int openRes = open(path, false); + if (openRes >= 0) { + if (mayExist == false) { + LOG_DEBUG(LOG, "create failed because already exist"); + getFile(openRes)->close(); + res = -EEXIST; + } else { + res = openRes; + } + } else { + LOG_DEBUG(LOG, "create failed but does not exist"); + res = -ENOENT; + } + } + } + } catch (const ZooKeeperException & e) { + LOG_ERROR(LOG, "mknod %s exception %s", path.c_str(), e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "mknod returns %d created %d", res, created); + return res; +} + +int ZkFuseHandleManager::mkdir(const char * path, mode_t mode) +{ + LOG_DEBUG(LOG, "mkdir(path %s, mode %o)", path, mode); + + int res = 0; + try { + ZkFuseNameType nameType; + std::string zkPath = getZkPath(path, nameType); + mode = (mode & ~S_IFMT) | S_IFDIR; + ZkFuseAutoHandle autoHandle + (SharedPtr(_thisWeakPtr), zkPath, mode, false); + res = autoHandle.get(); + if (res >= 0) { + res = 0; + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "mkdir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "mkdir returns %d", res); + return res; +} + +int ZkFuseHandleManager::open(const std::string & path, bool justCreated) +{ + LOG_DEBUG(LOG, "open(path %s, justCreated %d)", + path.c_str(), justCreated); + + int res = 0; + try { + bool newFile; + Handle handle = allocate(path, newFile); + ZkFuseAutoHandle autoHandle(SharedPtr(_thisWeakPtr), handle); + res = getFile(handle)->update(newFile || justCreated); + if (res == 0) { + res = handle; + autoHandle.release(); + } + } catch (const ZooKeeperException & e) { + LOG_ERROR(LOG, "open %s exception %s", path.c_str(), e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "open returns %d", res); + return res; +} + +int ZkFuseHandleManager::rmdir(const char * path, bool force) +{ + LOG_DEBUG(LOG, "rmdir(path %s, force %d)", path, force); + + int res = 0; + + try { + ZkFuseNameType nameType; + std::string zkPath = getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(SharedPtr(_thisWeakPtr), zkPath); + res = autoHandle.get(); + if (res >= 0) { + res = autoHandle.getFile()->rmdir(nameType, force); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "rmdir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "rmdir returns %d", res); + return res; +} + + +int +ZkFuseHandleManager::unlink(const char * path) +{ + LOG_DEBUG(LOG, "unlink(path %s)", path); + + ZkFuseNameType nameType; + std::string zkPath = getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(SharedPtr(_thisWeakPtr), zkPath); + int res = autoHandle.get(); + if (res >= 0) { + res = autoHandle.getFile()->unlink(nameType); + } + + LOG_DEBUG(LOG, "unlink returns %d", res); + return res; +} + +int ZkFuseHandleManager::getattr(const char *path, struct stat &stbuf) +{ + LOG_DEBUG(LOG, "getattr(path %s)", path); + + int res = 0; + try { + ZkFuseNameType nameType; + std::string zkPath = getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(SharedPtr(_thisWeakPtr), zkPath); + res = autoHandle.get(); + if (res >= 0) { + res = autoHandle.getFile()->getattr(stbuf, nameType); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "getattr %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "getattr returns %d", res); + return res; +} + +int +ZkFuseHandleManager::rename(const char * fromPath, const char * toPath) +{ + LOG_DEBUG(LOG, "rename(fromPath %s, toPath %s)", fromPath, toPath); + + ZkFuseNameType fromNameType; + std::string fromZkPath = getZkPath(fromPath, fromNameType); + ZkFuseAutoHandle fromAutoHandle(SharedPtr(_thisWeakPtr), fromZkPath); + int res = fromAutoHandle.get(); + if (res >= 0) { + LOG_DEBUG(LOG, "good fromPath"); + if (fromAutoHandle.getFile()->isDirNameType(fromNameType)) { + LOG_DEBUG(LOG, "fromPath is directory"); + res = -EISDIR; + } + } + if (res >= 0) { + ZkFuseNameType toNameType; + std::string toZkPath = getZkPath(toPath, toNameType); + bool created; + res = mknod(toZkPath.c_str(), S_IFREG, true, created); + if (res >= 0) { + ZkFuseAutoHandle toAutoHandle(SharedPtr(_thisWeakPtr), res); + if (toAutoHandle.getFile()->isDirNameType(toNameType)) { + LOG_DEBUG(LOG, "toPath is directory"); + res = -EISDIR; + } + if (res >= 0) { + LOG_DEBUG(LOG, "copy data"); + Data data; + fromAutoHandle.getFile()->getData(data); + toAutoHandle.getFile()->setData(data, true); + LOG_DEBUG(LOG, "copy metadata"); + struct stat stbuf; + int metaRes = + fromAutoHandle.getFile()->getattr(stbuf, fromNameType); + if (metaRes < 0) { + LOG_DEBUG(LOG, "get metadata failed"); + } + else { + metaRes = toAutoHandle.getFile()-> + utime(secsToMillisecs(stbuf.st_atime), + secsToMillisecs(stbuf.st_mtime), + toNameType); + if (metaRes < 0) { + LOG_DEBUG(LOG, "set metadata failed"); + } + } + } + if (created && res < 0) { + LOG_DEBUG(LOG, "undo create because copy data failed"); + int rmRes = toAutoHandle.getFile()->rmdir(toNameType, true); + } + } + } + if (res >= 0) { + LOG_DEBUG(LOG, "copy successful, unlink fromPath"); + res = fromAutoHandle.getFile()->unlink(fromNameType); + } + + LOG_DEBUG(LOG, "rename returns %d", res); + return res; +} + +void +ZkFuseHandleManager::addChildToParent(const std::string & childPath) const +{ + LOG_DEBUG(LOG, "addChildToParent(childPath %s)", childPath.c_str()); + + std::string parentPath = getParentPath(childPath); + if (!parentPath.empty()) { + AutoLock lock(_mutex); + Map::const_iterator it = _map.find(parentPath); + if (it != _map.end()) { + Handle handle = it->second; + assert(_files[handle] != NULL); + _files[handle]->addChild(childPath); + } + } + + LOG_DEBUG(LOG, "addChildToParent done"); +} + +void +ZkFuseHandleManager::removeChildFromParent(const std::string & childPath) const +{ + LOG_DEBUG(LOG, "removeChildFromParent(childPath %s)", childPath.c_str()); + + std::string parentPath = getParentPath(childPath); + if (!parentPath.empty()) { + AutoLock lock(_mutex); + Map::const_iterator it = _map.find(parentPath); + if (it != _map.end()) { + Handle handle = it->second; + assert(_files[handle] != NULL); + _files[handle]->removeChild(childPath); + } + } + + LOG_DEBUG(LOG, "removeChildFromParent done"); +} + +std::string +ZkFuseHandleManager::getParentPath(const std::string & childPath) const +{ + std::string::size_type lastPos = childPath.rfind('/'); + if (lastPos > 0) { + return std::string(childPath, 0, lastPos); + } + else { + assert(childPath[0] == '/'); + return std::string(); + } +} + +std::string +ZkFuseHandleManager::getZkPath(const char * path, ZkFuseNameType & nameType) + const +{ + LOG_DEBUG(LOG, "getZkPath(path %s)", path); + + std::string res; + unsigned pathLen = strlen(path); + const std::string & dataFileName = _common.getDataFileName(); + unsigned dataSuffixLen = dataFileName.length(); + const char * dataSuffix = dataFileName.c_str(); + unsigned dataSuffixIncludeSlashLen = dataSuffixLen + 1; + const std::string & forceDirSuffix = _common.getForceDirSuffix(); + unsigned forceDirSuffixLen = _common.getForceDirSuffix().length(); + /* Check if path is "/". If so, it is always a directory. + */ + if (pathLen == 1) { + assert(path[0] == '/'); + res = _common.getRootPathName(); + nameType = ZkFuseNameDirType; + } + /* Check if path ends of /{dataSuffix}, e.g. /foo/bar/{dataSuffix}. + * If so remove dataSuffix and nameType is ZkFuseNameRegType. + */ + else if ( + (pathLen >= dataSuffixIncludeSlashLen) && + (path[pathLen - dataSuffixIncludeSlashLen] == '/') && + (strncmp(path + (pathLen - dataSuffixLen), + dataSuffix, dataSuffixLen) == 0) + ) { + if ((pathLen - dataSuffixIncludeSlashLen) == 0) { + res = _common.getRootPathName(); + } else { + res.assign(path, pathLen - dataSuffixIncludeSlashLen); + } + nameType = ZkFuseNameRegType; + } + /* If not ZkFuseNameRegType, then check if path ends of + * {forceDirSuffix}, e.g. /foo/bar{forceDirSuffix}. + * If so remove forceDirSuffix and nameType is ZkFuseNameDirType. + */ + else if (forceDirSuffixLen > 0 && + pathLen >= forceDirSuffixLen && + strncmp(path + (pathLen - forceDirSuffixLen), + forceDirSuffix.c_str(), forceDirSuffixLen) == 0) { + res.assign(path, pathLen - forceDirSuffixLen); + nameType = ZkFuseNameDirType; + } + /* If not ZkFuseNameRegType and not ZkFuseNameDirType, then + * it is ZkFuseNameDefaultType. ZkFuse will infer type from + * ZooKeeper node's content. + */ + else { + res = path; + nameType = ZkFuseNameDefaultType; + } + /* Intermediate components of the path name may have + * forceDirSuffix, e.g. /foo/bar{forceDirSuffix}/baz. + * If so, remove the intermediate {forceDirSuffix}es. + */ + if (forceDirSuffixLen > 0) { + /* pos is an optimization to avoid always scanning from + * beginning of path + */ + unsigned pos = 0; + while ((res.length() - pos) > forceDirSuffixLen + 1) { + const char * found = + strstr(res.c_str() + pos, forceDirSuffix.c_str()); + if (found == NULL) { + break; + } + if (found[forceDirSuffixLen] == '/' || + found[forceDirSuffixLen] == '\0') { + pos = found - res.c_str(); + res.erase(pos, forceDirSuffixLen); + } + else { + pos += forceDirSuffixLen; + } + } + } + + LOG_DEBUG(LOG, "getZkPath returns %s, nameType %d", + res.c_str(), int(nameType)); + return res; +} + +static ZkFuseHandleManager::SharedPtr singletonZkFuseHandleManager; + +inline const ZkFuseHandleManager::SharedPtr & zkFuseHandleManager() +{ + return singletonZkFuseHandleManager; +} + +static +int zkfuse_getattr(const char *path, struct stat *stbuf) +{ + LOG_DEBUG(LOG, "zkfuse_getattr(path %s)", path); + + int res = 0; + try { + res = zkFuseHandleManager()->getattr(path, *stbuf); + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_getattr %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_getattr returns %d", res); + return res; +} + +static +int zkfuse_fgetattr(const char *path, struct stat *stbuf, + struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_fgetattr(path %s)", path); + + int res = 0; + int handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + res = zkFuseHandleManager()->getFile(handle)-> + getattr(*stbuf, ZkFuseNameDefaultType); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_fgetattr %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_fgetattr returns %d", res); + return res; +} + +static +int zkfuse_access(const char *path, int mask) +{ + /* not implemented */ + return -1; +} + +static +int zkfuse_readlink(const char *path, char *buf, size_t size) +{ + /* not implemented */ + return -1; +} + +static +int zkfuse_opendir(const char *path, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_opendir(path %s)", path); + + int res = 0; + try { + ZkFuseNameType nameType; + std::string zkPath = zkFuseHandleManager()->getZkPath(path, nameType); + if (nameType == ZkFuseNameRegType) { + res = -ENOENT; + } + else { + ZkFuseAutoHandle autoHandle(zkFuseHandleManager(), zkPath); + res = autoHandle.get(); + if (res >= 0) { + autoHandle.getFile()->incOpenDirCount(); + autoHandle.release(); + fi->fh = res; + res = 0; + } + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_opendir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_opendir returns %d", res); + return res; +} + +static int +zkfuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_readdir(path %s, offset %zu)", path, offset); + + int res = 0; + int handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + res = zkFuseHandleManager()->getFile(handle)-> + readdir(buf, filler, offset); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_readdir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_readdir returns %d", res); + return res; +} + +static +int zkfuse_releasedir(const char *path, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_releasedir(path %s)", path); + + int res = 0; + unsigned handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + zkFuseHandleManager()->getFile(handle)->decOpenDirCount(); + zkFuseHandleManager()->getFile(handle)->close(); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_releasedir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_releasedir returns %d", res); + return res; +} + +static +int zkfuse_mknod(const char *path, mode_t mode, dev_t rdev) +{ + LOG_DEBUG(LOG, "zkfuse_mknod(path %s, mode %o)", path, mode); + + int res = 0; + try { + ZkFuseNameType nameType; + std::string zkPath = zkFuseHandleManager()->getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(zkFuseHandleManager(), zkPath, mode, false); + res = autoHandle.get(); + if (res >= 0) { + res = 0; + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_mknod %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_mknod returns %d", res); + return res; +} + +static int zkfuse_mkdir(const char *path, mode_t mode) +{ + LOG_DEBUG(LOG, "zkfuse_mkdir(path %s, mode %o", path, mode); + + int res = 0; + try { + res = zkFuseHandleManager()->mkdir(path, mode); + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_mkdir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_mkdir returns %d", res); + return res; +} + +static int zkfuse_unlink(const char *path) +{ + LOG_DEBUG(LOG, "zkfuse_unlink(path %s)", path); + + int res = 0; + try { + res = zkFuseHandleManager()->unlink(path); + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_unlink %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_unlink returns %d", res); + return res; +} + +static int zkfuse_rmdir(const char *path) +{ + LOG_DEBUG(LOG, "zkfuse_rmdir(path %s)", path); + + int res = 0; + try { + res = zkFuseHandleManager()->rmdir(path); + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_rmdir %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_rmdir returns %d", res); + + return res; +} + +static int zkfuse_symlink(const char *from, const char *to) +{ + /* not implemented */ + return -1; +} + +static int zkfuse_rename(const char *from, const char *to) +{ + LOG_DEBUG(LOG, "zkfuse_rename(from %s, to %s)", from, to); + + int res = 0; + try { + res = zkFuseHandleManager()->rename(from, to); + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_rename %s %s exception %s", from, to, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_rename returns %d", res); + + return res; +} + +static int zkfuse_link(const char *from, const char *to) +{ + /* not implemented */ + return -1; +} + +static int zkfuse_chmod(const char *path, mode_t mode) +{ + LOG_DEBUG(LOG, "zkfuse_chmod(path %s, mode %o)", path, mode); + int res = 0; + + LOG_DEBUG(LOG, "zkfuse_chmod returns %d", res); + return res; +} + +static int zkfuse_chown(const char *path, uid_t uid, gid_t gid) +{ + LOG_DEBUG(LOG, "zkfuse_chown(path %s, uid %d, gid %d)", path, uid, gid); + + int res = 0; + + if (zkFuseHandleManager()->getCommon().getUid() == uid && + zkFuseHandleManager()->getCommon().getGid() == gid) { + res = 0; + } + else { + res = -EPERM; + } + + LOG_DEBUG(LOG, "zkfuse_chown returns %d", res); + return 0; +} + +static int zkfuse_truncate(const char *path, off_t size) +{ + LOG_DEBUG(LOG, "zkfuse_truncate(path %s, size %zu)", path, size); + + int res = 0; + try { + ZkFuseNameType nameType; + std::string zkPath = zkFuseHandleManager()->getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(zkFuseHandleManager(), zkPath); + res = autoHandle.get(); + if (res >= 0) { + res = autoHandle.getFile()->truncate(size); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_truncate %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_truncate returns %d", res); + return res; +} + +static +int zkfuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_ftruncate(path %s, size %zu)", path, size); + + int res = 0; + unsigned handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + res = zkFuseHandleManager()->getFile(handle)->truncate(size); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_ftruncate %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_ftruncate returns %d", res); + return res; +} + +static +int zkfuse_utimens(const char *path, const struct timespec ts[2]) +{ + LOG_DEBUG(LOG, "zkfuse_utimens(path %s)", path); + + int res = 0; + try { + uint64_t atime = timespecToMillisecs(ts[0]); + uint64_t mtime = timespecToMillisecs(ts[1]); + ZkFuseNameType nameType; + std::string zkPath = zkFuseHandleManager()->getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(zkFuseHandleManager(), zkPath); + res = autoHandle.get(); + if (res >= 0) { + res = autoHandle.getFile()->utime(atime, mtime, nameType); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_utimens %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_utimens returns %d", res); + return res; +} + +static +int zkfuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) +{ + int fd; + + fd = open(path, fi->flags, mode); + if (fd == -1) + return -errno; + + fi->fh = fd; + return 0; +} + +static +int zkfuse_open(const char *path, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_open(path %s, flags %o)", path, fi->flags); + + int res = 0; + try { + ZkFuseNameType nameType; + std::string zkPath = zkFuseHandleManager()->getZkPath(path, nameType); + ZkFuseAutoHandle autoHandle(zkFuseHandleManager(), zkPath); + res = autoHandle.get(); + if (res >= 0) { + if (autoHandle.getFile()->isDirNameType(nameType)) { + res = -ENOENT; + } + } + if (res >= 0) { + autoHandle.release(); + fi->fh = res; + res = 0; + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_open %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_open returns %d", res); + return res; +} + +static +int zkfuse_read(const char *path, char *buf, size_t size, off_t offset, + struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_read(path %s, size %zu, offset %zu)", + path, size, offset); + + int res = 0; + unsigned handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + res = zkFuseHandleManager()->getFile(handle)-> + read(buf, size, offset); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_read %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_read returns %d", res); + return res; +} + +static +int zkfuse_write(const char *path, const char *buf, size_t size, + off_t offset, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_write(path %s, size %zu, offset %zu)", + path, size, offset); + + int res = 0; + unsigned handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + res = zkFuseHandleManager()->getFile(handle)-> + write(buf, size, offset); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_write %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_write returns %d", res); + return res; +} + +static int zkfuse_statfs(const char *path, struct statvfs *stbuf) +{ + /* not implemented */ + return -1; +} + +static +int zkfuse_flush(const char *path, struct fuse_file_info *fi) +{ + /* This is called from every close on an open file, so call the + close on the underlying filesystem. But since flush may be + called multiple times for an open file, this must not really + close the file. This is important if used on a network + filesystem like NFS which flush the data/metadata on close() */ + + LOG_DEBUG(LOG, "zkfuse_flush(path %s)", path); + + int res = 0; + unsigned handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + res = zkFuseHandleManager()->getFile(handle)->flush(); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_flush %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_flush returns %d", res); + return res; +} + +static +int zkfuse_release(const char *path, struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_release(path %s)", path); + + int res = 0; + unsigned handle = fi->fh; + try { + if (handle <= 0) { + res = -EINVAL; + } + else { + zkFuseHandleManager()->getFile(handle)->close(); + } + } catch (const std::exception & e) { + LOG_ERROR(LOG, "zkfuse_release %s exception %s", path, e.what()); + res = -EIO; + } + + LOG_DEBUG(LOG, "zkfuse_release returns %d", res); + return res; +} + +static +int zkfuse_fsync(const char *path, int isdatasync, + struct fuse_file_info *fi) +{ + LOG_DEBUG(LOG, "zkfuse_fsync(path %s, isdatasync %d)", path, isdatasync); + + (void) isdatasync; + int res = zkfuse_flush(path, fi); + + LOG_DEBUG(LOG, "zkfuse_fsync returns %d", res); + return res; +} + +#ifdef HAVE_SETXATTR +/* xattr operations are optional and can safely be left unimplemented */ +static int zkfuse_setxattr(const char *path, const char *name, const char *value, + size_t size, int flags) +{ + int res = lsetxattr(path, name, value, size, flags); + if (res == -1) + return -errno; + return 0; +} + +static int zkfuse_getxattr(const char *path, const char *name, char *value, + size_t size) +{ + int res = lgetxattr(path, name, value, size); + if (res == -1) + return -errno; + return res; +} + +static int zkfuse_listxattr(const char *path, char *list, size_t size) +{ + int res = llistxattr(path, list, size); + if (res == -1) + return -errno; + return res; +} + +static int zkfuse_removexattr(const char *path, const char *name) +{ + int res = lremovexattr(path, name); + if (res == -1) + return -errno; + return 0; +} +#endif /* HAVE_SETXATTR */ + +static +int zkfuse_lock(const char *path, struct fuse_file_info *fi, int cmd, + struct flock *lock) +{ + (void) path; + return ulockmgr_op(fi->fh, cmd, lock, &fi->lock_owner, + sizeof(fi->lock_owner)); +} + + +static +void init_zkfuse_oper(fuse_operations & fo) +{ + memset(&fo, 0, sizeof(fuse_operations)); + fo.getattr = zkfuse_getattr; + fo.fgetattr = zkfuse_fgetattr; + // fo.access = zkfuse_access; + // fo.readlink = zkfuse_readlink; + fo.opendir = zkfuse_opendir; + fo.readdir = zkfuse_readdir; + fo.releasedir = zkfuse_releasedir; + fo.mknod = zkfuse_mknod; + fo.mkdir = zkfuse_mkdir; + // fo.symlink = zkfuse_symlink; + fo.unlink = zkfuse_unlink; + fo.rmdir = zkfuse_rmdir; + fo.rename = zkfuse_rename; + // fo.link = zkfuse_link; + fo.chmod = zkfuse_chmod; + fo.chown = zkfuse_chown; + fo.truncate = zkfuse_truncate; + fo.ftruncate = zkfuse_ftruncate; + fo.utimens = zkfuse_utimens; + // fo.create = zkfuse_create; + fo.open = zkfuse_open; + fo.read = zkfuse_read; + fo.write = zkfuse_write; + fo.statfs = zkfuse_statfs; + fo.flush = zkfuse_flush; + fo.release = zkfuse_release; + fo.fsync = zkfuse_fsync; +#ifdef HAVE_SETXATTR + // fo.setxattr = zkfuse_setxattr; + // fo.getxattr = zkfuse_getxattr; + // fo.listxattr = zkfuse_listxattr; + // fo.removexattr = zkfuse_removexattr; +#endif + fo.lock = zkfuse_lock; +}; + + +/** + * The listener of ZK events. + */ +class SessionEventListener : public ZKEventListener +{ + private: + /** + References the ZkFuseHandleManager instance that should be + invoked to service events. + */ + ZkFuseHandleManager::SharedPtr _manager; + + public: + /** + Sets the ZkFuseHandleManager instance that should be invoked + to service events. + */ + void setManager(const ZkFuseHandleManager::SharedPtr & manager) + { + _manager = manager; + } + /** + Received an event and invoke ZkFuseHandleManager instance to handle + received event. + */ + virtual void eventReceived(const ZKEventSource & source, + const ZKWatcherEvent & event) + { + _manager->eventReceived(event); + } +}; + +void +usage(int argc, char *argv[]) +{ + cout + << argv[0] + << " usage: " + << argv[0] + << " [args-and-values]+" << endl + << "nodepath == a complete path to a ZooKeeper node" << endl + << "\t--cachesize= or -c :" << endl + << " number of ZooKeeper nodes to cache." << endl + << "\t--debug or -d: " << endl + << "\t enable fuse debug mode." << endl + << "\t--help or -h: " << endl + << "\t print this message." << endl + << "\t--mount= or -m : " << endl + << "\t specifies where to mount the zkfuse filesystem." << endl + << "\t--name or -n: " << endl + << "\t name of file for accessing node data." << endl + << "\t--zookeeper= or -z : " << endl + << "\t specifies information needed to connect to zeekeeper." << endl; +} + +int +main(int argc, char *argv[]) +{ + /** + * Initialize log4cxx + */ + const std::string file("log4cxx.properties"); + PropertyConfigurator::configureAndWatch( file, 5000 ); + LOG_INFO(LOG, "Starting zkfuse"); + + /** + * Supported operations. + */ + enum ZkOption { + ZkOptionCacheSize = 1000, + ZkOptionDebug = 1001, + ZkOptionForceDirSuffix = 1002, + ZkOptionHelp = 1003, + ZkOptionMount = 1004, + ZkOptionName = 1005, + ZkOptionZookeeper = 1006, + ZkOptionInvalid = -1 + }; + + static const char *shortOptions = "c:df:hm:n:z:"; + static struct option longOptions[] = { + { "cachesize", 1, 0, ZkOptionCacheSize }, + { "debug", 0, 0, ZkOptionDebug }, + { "forcedirsuffix", 1, 0, ZkOptionForceDirSuffix }, + { "help", 0, 0, ZkOptionHelp }, + { "mount", 1, 0, ZkOptionMount }, + { "name", 1, 0, ZkOptionName }, + { "zookeeper", 1, 0, ZkOptionZookeeper }, + { 0, 0, 0, 0 } + }; + + /** + * Parse arguments + */ + bool debugFlag = false; + std::string mountPoint = "/tmp/zkfuse"; + std::string nameOfFile = "_data_"; + std::string forceDirSuffix = "._dir_"; + std::string zkHost; + unsigned cacheSize = 256; + + while (true) { + int c; + + c = getopt_long(argc, argv, shortOptions, longOptions, 0); + if (c == -1) { + break; + } + + switch (c) { + case ZkOptionInvalid: + cerr + << argv[0] + << ": ERROR: Did not specify legal argument!" + << endl; + return 99; + case 'c': + case ZkOptionCacheSize: + cacheSize = strtoul(optarg, NULL, 0); + break; + case 'd': + case ZkOptionDebug: + debugFlag = true; + break; + case 'f': + case ZkOptionForceDirSuffix: + forceDirSuffix = optarg; + break; + case 'h': + case ZkOptionHelp: + usage(argc, argv); + return 0; + case 'm': + case ZkOptionMount: + mountPoint = optarg; + break; + case 'n': + case ZkOptionName: + nameOfFile = optarg; + break; + case 'z': + case ZkOptionZookeeper: + zkHost = optarg; + break; + } + } + + /** + * Check that zkHost has a value, otherwise abort. + */ + if (zkHost.empty()) { + cerr + << argv[0] + << ": ERROR: " + << "required argument \"--zookeeper \" was not given!" + << endl; + return 99; + } + /** + * Check that zkHost has a value, otherwise abort. + */ + if (forceDirSuffix.empty()) { + cerr + << argv[0] + << ": ERROR: " + << "required argument \"--forcedirsuffix \" " + "not cannot be empty!" + << endl; + return 99; + } + /** + * Check nameOfFile has no forward slash + */ + if (nameOfFile.find_first_of('/') != std::string::npos) { + cerr + << argv[0] + << ": ERROR: " + << "'/' present in name which is not allowed" + << endl; + return 99; + } + + if (debugFlag) { + cout + << "cacheSize = " + << cacheSize + << ", debug = " + << debugFlag + << ", forceDirSuffix = \"" + << forceDirSuffix + << "\", mount = \"" + << mountPoint + << "\", name = \"" + << nameOfFile + << "\", zookeeper = \"" + << zkHost + << "\", optind = " + << optind + << ", argc = " + << argc + << ", current arg = \"" + << (optind >= argc ? "NULL" : argv[optind]) + << "\"" + << endl; + } + + SessionEventListener listener; + SynchronousEventAdapter eventAdapter; + LOG_INFO(LOG, "Create ZK adapter"); + try { + /** + * Create an instance of ZK adapter. + */ + std::string h(zkHost); + ZooKeeperConfig config(h, 1000, true, 10000); + ZkFuseCommon zkFuseCommon; + ZooKeeperAdapterSharedPtr zkPtr( + new ZooKeeperAdapter( + config, + &listener, + false + ) + ); + zkFuseCommon.setZkAdapter(zkPtr); + zkFuseCommon.setDataFileName(nameOfFile); + zkFuseCommon.setForceDirSuffix(forceDirSuffix); + zkFuseCommon.setCacheSize(cacheSize); + singletonZkFuseHandleManager = + ZkFuseHandleManagerFactory::create(zkFuseCommon); + listener.setManager(singletonZkFuseHandleManager); + zkPtr->reconnect(); + + } catch (const ZooKeeperException & e) { + cerr + << argv[0] + << ": ERROR: ZookKeeperException caught: " + << e.what() + << endl; + } catch (std::exception & e) { + cerr + << argv[0] + << ": ERROR: std::exception caught: " + << e.what() + << endl; + } + +#ifdef ZOOKEEPER_ROOT_CHILDREN_WATCH_BUG + cerr << "ZOOKEEPER_ROOT_CHILDREN_WATCH_BUG enabled" << endl; +#endif + /** + * Initialize fuse + */ + LOG_INFO(LOG, "Initialize fuse"); + umask(0); + fuse_operations zkfuse_oper; + init_zkfuse_oper(zkfuse_oper); + int fakeArgc = debugFlag ? 3 : 2; + char * fakeArgv[] = { + argv[0], + strdup(mountPoint.c_str()), + debugFlag ? strdup("-d") : NULL, + NULL + }; + int res = fuse_main(fakeArgc, fakeArgv, &zkfuse_oper, NULL); + for (unsigned i = 1; i <= 2; i++) { + if (fakeArgv[i] != NULL) { + free(fakeArgv[i]); + } + } + + return res; +} diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/Changes b/zookeeper-contrib/zookeeper-contrib-zkperl/Changes new file mode 100644 index 0000000..c31420e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/Changes @@ -0,0 +1,65 @@ +Net::ZooKeeper - Perl extension for Apache ZooKeeper + +Revision history +================ + +0.01 Dec 5, 2008 + - initial version + +0.02 Dec 16, 2008 + - support connection to ZooKeeper and get() method + +0.03 Jan 9, 2009 + - implemented watch mechanism for get() + +0.04 Jan 15, 2009 + - all basic ZooKeeper methods supported + +0.05 Jan 21, 2009 + - converted from T_PTROBJ to T_ZK_HASH with PERL_MAGIC_ext, + allows DESTROY() to be called repeatedly + +0.06 Jan 27, 2009 + - converted from attribute accessor methods to inner and outer hashes + with PERL_MAGIC_tied + +0.07 Jan 29, 2009 + - all tied hash methods completed + +0.08 Jan 30, 2009 + - simple thread safety enforced with CLONE_SKIP + +0.09 Feb 12, 2009 + - ACL constants + +0.10 Feb 18, 2009 + - ACL support + +0.11 Feb 21, 2009 + - ZooKeeper version check + +0.20 Feb 25, 2009 + - refactored watches as subclass + +0.30 Feb 27, 2009 + - refactored stats as subclass + +0.31 Mar 6, 2009 + - test suite completed + +0.32 Mar 25, 2009 + - initial documentation completed, first public release + +0.33 Apr 20, 2009 + - copyright donated to ASF + +0.34 Jul 14, 2009 + - support ZooKeeper 3.2.0 release + +0.35 Jul 15, 2009 + - support multiple include and library locations + +0.36 Mar 27, 2011 + - Fix zookeeper version check, but only warn since we haven't been enforcing it in a while + - Look for zookeeper includes in some sane places by default + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/LICENSE b/zookeeper-contrib/zookeeper-contrib-zkperl/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/LICENSE @@ -0,0 +1,202 @@ + + 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/zookeeper-contrib/zookeeper-contrib-zkperl/MANIFEST b/zookeeper-contrib/zookeeper-contrib-zkperl/MANIFEST new file mode 100644 index 0000000..1b57436 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/MANIFEST @@ -0,0 +1,23 @@ +Changes +LICENSE +Makefile.PL +MANIFEST +NOTICE +README +typemap +ZooKeeper.pm +ZooKeeper.xs +build/check_zk_version.c +build/check_zk_version.h +t/10_invalid.t +t/15_thread.t +t/20_tie.t +t/22_stat_tie.t +t/24_watch_tie.t +t/30_connect.t +t/35_log.t +t/40_basic.t +t/45_class.t +t/50_access.t +t/60_watch.t +t/util.pl diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/Makefile.PL b/zookeeper-contrib/zookeeper-contrib-zkperl/Makefile.PL new file mode 100644 index 0000000..9a0996d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/Makefile.PL @@ -0,0 +1,73 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use 5.008_008; + +use Config; +use ExtUtils::MakeMaker; +use Getopt::Long; + +my $ZOO_MAJOR_VERSION = 3; +my $ZOO_REQUIRED_VERSION = qr{^$ZOO_MAJOR_VERSION\.\d+.\d+$}ismx; + +my @zk_inc_paths; +my @zk_lib_paths; + +GetOptions( + 'zookeeper-include=s' => \@zk_inc_paths, + 'zookeeper-lib=s' => \@zk_lib_paths +); + +my $zk_inc_paths = join(' ', map("-I$_", @zk_inc_paths)); +my $zk_lib_paths = join(' ', map("-L$_", @zk_lib_paths)); + +$zk_inc_paths .= ' ' unless ($zk_inc_paths eq ''); +$zk_lib_paths .= ' ' unless ($zk_lib_paths eq ''); + +my $cc = $Config{'cc'}; +my $check_file = 'build/check_zk_version'; + +my $check_out = qx($cc $zk_inc_paths $zk_lib_paths -I. -o $check_file $check_file.c 2>&1); + +if ($?) { + if ($check_out =~ /zookeeper_version\.h/) { + die("Could not determine ZooKeeper version:\n\n$check_out"); + } + else { + ## keep in sync with build/check_zk_version.h + die("Net::ZooKeeper requires at least ZooKeeper version 3.1.1\n"); + } +} + +chomp(my $zk_ver = qx($check_file)); + +if ($? >> 8 != 0) { + die "Couldn't check zookeeper version: $zk_ver: $r"; +} +elsif ($zk_ver !~ $ZOO_REQUIRED_VERSION) { + warn "Net::ZooKeeper requires ZooKeeper 3.x, found $zk_ver!"; +} + +WriteMakefile( + 'INC' => "$zk_inc_paths-I.", + 'LIBS' => [ "$zk_lib_paths-lzookeeper_mt" ], + 'NAME' => 'Net::ZooKeeper', + 'VERSION_FROM' => 'ZooKeeper.pm', + 'clean' => { 'FILES' => 'build/check_zk_version.o' } +); + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/NOTICE b/zookeeper-contrib/zookeeper-contrib-zkperl/NOTICE new file mode 100644 index 0000000..b68fdac --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/NOTICE @@ -0,0 +1,6 @@ +Net::ZooKeeper - Perl extension for Apache ZooKeeper +Copyright 2009 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/README b/zookeeper-contrib/zookeeper-contrib-zkperl/README new file mode 100644 index 0000000..bbe2a0d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/README @@ -0,0 +1,86 @@ +Net::ZooKeeper - Perl extension for Apache ZooKeeper +==================================================== + +Net::ZooKeeper provides a Perl interface to the synchronous C API +of Apache ZooKeeper. ZooKeeper is coordination service for +distributed applications. +For details see the ZooKeeper home page at: + +http://zookeeper.apache.org/ + +INSTALLATION + +To install this module type the following, first install the +zookeeper C client, then: + + perl Makefile.PL + make + ZK_TEST_HOSTS=host:port,... make test + make install + +If the C headers and library are installed in non-standard +locations, specify them as arguments to Makefile.PL: + + perl Makefile.PL \ + --zookeeper-include=/path/to/zookeeper/client/include \ + --zookeeper-lib=/path/to/zookeeper/client/lib + +The path supplied to the --zookeeper-include option should +identify the directory that contains the zookeeper.h and other +ZooKeeper C include files. + +The path supplied to the --zookeeper-lib option should identify +the directory that contains the libzookeeper_mt library. + +When running "make test", if no ZK_TEST_HOSTS environment +variable is set, many tests will be skipped because no connection +to a ZooKeeper server is available. To execute these tests, +the ZK_TEST_HOSTS variable may be assigned a list of one or more +ZooKeeper host:port pairs, e.g., "localhost:7100,otherhost:7200". + +The ZK_TEST_PATH environment variable, if defined, specifies +the ZooKeeper path under which all test nodes should be created. +The tests expect to have full read/write/create/delete/admin +ZooKeeper permissions under this path. If no ZK_TEST_PATH +variable is defined, the root ZooKeeper path ("/") is used. + +DEPENDENCIES + +Version 3.1.1 of ZooKeeper is required at a minimum. + +For version 3.1.1, you may also want to apply some of these +additional patches to the ZooKeeper C API code: + +https://issues.apache.org/jira/browse/ZOOKEEPER-262 +https://issues.apache.org/jira/browse/ZOOKEEPER-318 + +For version 3.1.1, you may also want to apply some of these +additional patches to the ZooKeeper C API code: + +https://issues.apache.org/jira/browse/ZOOKEEPER-262 +https://issues.apache.org/jira/browse/ZOOKEEPER-466 + +This module requires that the multi-threaded version of the +ZooKeeper C API client library be available on your system. + +This in turn implies that the POSIX pthread library is available +as well. + +COPYRIGHT AND LICENCE + +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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/zookeeper-contrib/zookeeper-contrib-zkperl/ZooKeeper.pm b/zookeeper-contrib/zookeeper-contrib-zkperl/ZooKeeper.pm new file mode 100644 index 0000000..507f029 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/ZooKeeper.pm @@ -0,0 +1,1257 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use 5.008_008; + +use strict; +use warnings; + +package Net::ZooKeeper; + +require Exporter; +require XSLoader; + +our $VERSION = '0.36'; + +our @ISA = qw(Exporter); + +our %EXPORT_TAGS = ( + 'errors' => [qw( + ZOK + ZSYSTEMERROR + ZRUNTIMEINCONSISTENCY + ZDATAINCONSISTENCY + ZCONNECTIONLOSS + ZMARSHALLINGERROR + ZUNIMPLEMENTED + ZOPERATIONTIMEOUT + ZBADARGUMENTS + ZINVALIDSTATE + ZAPIERROR + ZNONODE + ZNOAUTH + ZBADVERSION + ZNOCHILDRENFOREPHEMERALS + ZNODEEXISTS + ZNOTEMPTY + ZSESSIONEXPIRED + ZINVALIDCALLBACK + ZINVALIDACL + ZAUTHFAILED + ZCLOSING + ZNOTHING + )], + 'node_flags' => [qw( + ZOO_EPHEMERAL + ZOO_SEQUENCE + )], + 'acl_perms' => [qw( + ZOO_PERM_READ + ZOO_PERM_WRITE + ZOO_PERM_CREATE + ZOO_PERM_DELETE + ZOO_PERM_ADMIN + ZOO_PERM_ALL + )], + 'acls' => [qw( + ZOO_OPEN_ACL_UNSAFE + ZOO_READ_ACL_UNSAFE + ZOO_CREATOR_ALL_ACL + )], + 'events' => [qw( + ZOO_CREATED_EVENT + ZOO_DELETED_EVENT + ZOO_CHANGED_EVENT + ZOO_CHILD_EVENT + ZOO_SESSION_EVENT + ZOO_NOTWATCHING_EVENT + )], + 'states' => [qw( + ZOO_EXPIRED_SESSION_STATE + ZOO_AUTH_FAILED_STATE + ZOO_CONNECTING_STATE + ZOO_ASSOCIATING_STATE + ZOO_CONNECTED_STATE + )], + 'log_levels' => [qw( + ZOO_LOG_LEVEL_OFF + ZOO_LOG_LEVEL_ERROR + ZOO_LOG_LEVEL_WARN + ZOO_LOG_LEVEL_INFO + ZOO_LOG_LEVEL_DEBUG + )] +); + +{ + my %tags; + + push @{$EXPORT_TAGS{'all'}}, + grep {!$tags{$_}++} @{$EXPORT_TAGS{$_}} foreach (keys(%EXPORT_TAGS)); +} + +our @EXPORT_OK = ( @{$EXPORT_TAGS{'all'}} ); + +XSLoader::load('Net::ZooKeeper', $VERSION); + +1; + +__END__ + +=head1 NAME + +Net::ZooKeeper - Perl extension for Apache ZooKeeper + +=head1 SYNOPSIS + + use Net::ZooKeeper qw(:node_flags :acls); + + my $zkh = Net::ZooKeeper->new('localhost:7000'); + + $zkh->create('/foo', 'bar', + 'flags' => ZOO_EPHEMERAL, + 'acl' => ZOO_OPEN_ACL_UNSAFE) or + die("unable to create node /foo: " . $zkh->get_error() . "\n"); + + print "node /foo has value: " . $zkh->get('/foo') . "\n"; + + $zkh->set('/foo', 'baz'); + + print "node / has child nodes:\n"; + foreach my $path ($zkh->get_children('/')) { + print " /$path\n"; + } + + my $stat = $zkh->stat(); + if ($zkh->exists('/foo', 'stat' => $stat)) { + print "node /foo has stat info:\n"; + while (my($key,$value) = each(%{$stat})) { + print " $key: $value\n"; + } + } + + foreach my $acl_entry ($zkh->get_acl('/foo')) { + print "node /foo has ACL entry:\n"; + print " perms: $acl_entry->{perms}\n"; + print " scheme: $acl_entry->{scheme}\n"; + print " id: $acl_entry->{id}\n"; + } + + my $watch = $zkh->watch('timeout' => 10000); + $zkh->exists('/foo', 'watch' => $watch); + + if ($watch->wait()) { + print "watch triggered on node /foo:\n"; + print " event: $watch->{event}\n"; + print " state: $watch->{state}\n"; + } + else { + print "watch timed out after 10 seconds\n"; + } + + $zkh->delete('/foo'); + +=head1 DESCRIPTION + +Net::ZooKeeper provides a Perl interface to the synchronous C API +of Apache ZooKeeper. ZooKeeper is coordination service for +distributed applications. + +Each connection to ZooKeeper is represented as a handle object +of the class Net::ZooKeeper, similar to the manner in which database +connections are represented in the DBI module. + +To disconnect from ZooKeeper, simply destroy the Net::ZooKeeper +handle object by undefining it or by explicitly calling the +C method. + +The methods which may be invoked on Net::ZooKeeper handles +correspond to the functions of the synchronous ZooKeeper C API; +e.g., the Net::ZooKeeper method C calls the ZooKeeper +C function C, C calls C, +and so forth. + +The synchronous API functions wait for a response from the ZooKeeper +cluster before returning a result to the caller. Using these +functions permits Net::ZooKeeper to provide an interface similar +to that of a DBI driver module. + +=head2 Internal POSIX Threads + +The use of the synchronous ZooKeeper C API still requires that +the ZooKeeper C client code create several POSIX threads which run +concurrently with the main thread containing the Perl interpreter. + +The synchronous API functions are wrappers of the asynchronous +functions in the ZooKeeper C API. When a request is made by the +caller's thread (i.e., the one with the running Perl interpreter), +it is enqueued for delivery at a later time by the ZooKeeper C client +code's IO thread. The caller's thread then waits for notification +before returning from the synchronous API function. + +The IO thread dequeues the request and sends it to the ZooKeeper +cluster, while also ensuring that a regular "heartbeat" is maintained +with the cluster so that the current session does not time out. +When the IO thread receives a response from +the ZooKeeper cluster, it enqueues the response for delivery to the +client by the second thread of the ZooKeeper client code, the +completion thread. + +If the caller is using the asynchronous API, the completion thread +invokes the appropriate callback function provided by the caller +for the given request. In the case of Net::ZooKeeper, it is not +viable for the completion thread to invoke a Perl callback function +at arbitrary times; this could interfere with the state of the +Perl interpreter. + +For this reason Net::ZooKeeper uses the synchronous API only. After +enqueuing requests the synchronous API functions wait for notification +of the corresponding response. The completion thread delivers these +notifications, at which point the synchronous functions return to +their caller. + +Note that the IO and completion threads are POSIX threads, not +Perl ithreads. Net::ZooKeeper defined a C function so +that if Perl ithreads are spawned while a Net::ZooKeeper connection +is active, the Net::ZooKeeper handle objects inherited by the +spawned ithread contain undefined values so that they can not be used. +Thus each ithread will need to create its own private connections to a +ZooKeeper cluster. + +Note also that before invoking C to spawn a new process, +all Net::ZooKeeper handles should be destroyed so that all +connections to ZooKeeper are closed and all internal POSIX threads +have exited. If a child process needs to communicate with +ZooKeeper it should open its own private connections after it is +created by C. + +=head2 Signals + +The ZooKeeper C API uses TCP connections to communicate with +the ZooKeeper cluster. These connections may generate SIGPIPE +signals when they encounter errors, such as when a connection +is terminated by a ZooKeeper server. Therefore most applications +will want to trap or ignore SIGPIPE signals, e.g.: + + local $SIG{'PIPE'} = 'IGNORE'; + +Ignoring SIGPIPE signals (or providing a signal handler that returns +control to the interrupted program after receiving the signal) +will allow the ZooKeeper C client code to detect the connection error +and report it upon return from the next Net::ZooKeeper method. + +=head2 Error Handling + +Net::ZooKeeper methods return different values in the case of an +error depending on their purpose and context. For example, +C returns true if the node exists and false otherwise, +which may indicate either that the node does not exist or that +an error occurred. + +After any method returns a false, empty, or undefined value which +might indicate an error has occurred, the C method +may be called to examine the specific error code, if any. + +If C returns C, no error has occurred. If the +error code is less than C, it indicates a normal error +condition reported by the ZooKeeper server, such as C +(node does not exist) or C (node already exists). + +If the error code is greater than C, then a connection +error or server error has occurred and the client should probably +close the connection by undefining the Net::ZooKeeper handle object +and, if necessary, attempt to create a new connection to the +ZooKeeper cluster. + +=head2 Access Control + +If the ZooKeeper cluster is not configured with C then +it will respect the access controls set for each node in the +ZooKeeper hierarchy. These access controls are defined using ACLs +(Access Control Lists); see the ZooKeeper documentation for compete +details. + +In Net::ZooKeeper, ACLs are represented as arrays of hashes, where +each hash is an ACL entry that must contain three attributes, +C, C, and C. The C attribute's value +should be composed by combining ACL permission flags using the +bitwise OR operator. See C<:acl_perms> for a list of the +available ACL permission flags. + +The ACL for a node may be read using the C method. A +node's ACL may be set when the node is created by passing an ACL +array as the value of the C<'acl'> option to the C method, +and may be updated by passing an ACL array to the C method. + +When a client connects to a ZooKeeper cluster it is automatically +assigned authentication credentials based on its IP address. +Additional authentication credentials may be added using +the C method. Once a credential has been added for +the current session, there is no way to disable it. + +As an example, digest authentication may be enabled for a session +by calling C as follows: + + $zkh->add_auth('digest', "$username:$password"); + +Note that the username and password are transmitted in cleartext +to the ZooKeeper cluster. + +Such authentication credentials would enable access to a node +whose ACL contained an entry with a C attribute of +C<'digest'> and an C attribute containing a Base64-encoded +SHA1 digest of the string C<"$username:$password">. The +Perl modules Digest and MIME::Base64 may be used to create +such ACL ID values as follows: + + use Digest qw(); + use MIME::Base64 qw(); + + my $ctx = Digest->new('SHA-1')->add("$username:$password"); + my $digest = MIME::Base64::encode($ctx->digest()); + +Note that using the C method of the Digest module +will not result in digest strings with the "=" suffix characters +required by ZooKeeper. + +=head2 Logging + +As of ZooKeeper version 3.1.1, logging in the C client code is +implemented with a single, shared file handle to which all +of the internal POSIX threads write log messages; by default, +this file handle is attached to STDERR. + +Moreover, this file handle is shared by all active ZooKeeper +connections (each of which has its own private IO and completion +threads; see L above). + +Net::ZooKeeper therefore does not provide per-connection handle +attributes related to logging. The global function +C may be used to set the current +log level. See C<:log_levels> for a list of the available log +levels. The default log level is C. + +To capture ZooKeeper log messages to a file instead of STDERR, +redirect STDERR to a new file handle in the normal Perl manner: + + open(OLDERR, '>&', fileno(STDERR)) or + die("unable to dup STDERR: $!"); + open(STDERR, '>', $log_file) or + die("unable to redirect STDERR: $!"); + +=head2 Connection Order + +ZooKeeper clusters are typically made up of an odd number of +ZooKeeper servers. When connecting to such a cluster, the +C method should be passed a comma-separated list of +the hostnames and ports for each of the servers in the cluster, +e.g., C<'host1:7000,host2:7000,host2:7100'>. + +The default behaviour of the ZooKeeper client code is to +reorder this list randomly before making any connections. +A connection is then made to the first server in the reordered +list. If that connection fails, the IO thread will +automatically attempt to reconnect to the cluster, this time +to the next server in the list; when the last server in the list +is reached, the IO thread will continue again with the first +server. + +For certain purposes it may be necessary for ZooKeeper clients +to know the exact order in which the IO thread will attempt to +connect to the servers of a cluster. To do so, call +C. Note, +however, that this will affect all Net::ZooKeeper object +handles created by the current process. + +=head1 ATTRIBUTES + +=head2 Net::ZooKeeper + +The Net::ZooKeeper class provides the main interface to the +ZooKeeper client API. The following attributes are available +for each Net::ZooKeeper handle object and are specific to +that handle and the method calls invoked on it. As with DBI +handle objects, attributes may be read and written through +a hash interface, e.g.: + + print sprintf("Session timeout is %.2f seconds.\n", + $zkh->{session_timeout} / 1000); + + $zkh->{watch_timeout} = 10000; + +=over 4 + +=item hosts + +The comma-separated list of ZooKeeper server hostnames and ports +as passed to the C method. Note that by default the +ZooKeeper C client code will reorder this list before attempting +to connect for the first time; see L for details. + +This attribute is B and may not be modified. + +=item session_timeout + +The session timeout value, in milliseconds, as set by the +ZooKeeper server after connection. This value may not be +exactly the same as what was requested in the C<'session_timeout'> +option of the C method; the server will adjust the +requested timeout value so that it is within a certain range +of the server's C setting. See the ZooKeeper +documentation for details. + +Because the actual connection to the ZooKeeper server is +not made during the C method call but shortly +thereafter by the IO thread, note that this value may not +be initialized to its final value until at least one +other method which requires communication with the server +(such as C) has succeeded. + +This attribute is B and may not be modified. + +=item session_id + +The client's session ID value as set by the ZooKeeper server +after connection. This is a binary data string which may +be passed to subsequent C calls as the value of +the C<'session_id'> option, if the user wishes to attempt to +continue a session after a failure. Note that the server +may not honour such an attempt. + +Because the actual connection to the ZooKeeper server is +not made during the C method call but shortly +thereafter by the IO thread, note that this value may not +be initialized to its final value until at least one +other method which requires communication with the server +(such as C) has succeeded. + +This attribute is B and may not be modified. + +=item data_read_len + +The maximum length of node data that will be returned to +the caller by the C method. If a node's data exceeds +this length, the returned value will be shorter than the +actual node data as stored in the ZooKeeper cluster. + +The default maximum length of the node data returned by +C is 1023 bytes. This may be changed by setting +the C attribute to a different value. + +Passing a value for the C<'data_read_len'> option when calling +the C method will temporarily override the per-handle +maximum. + +=item path_read_len + +The maximum length of a newly created node's path that will +be returned to the caller by the C method. If the path +of the newly created node exceeds this length, the returned +value will be shorter than the actual path of the node as stored +in the ZooKeeper cluster. + +The default maximum length of the node path returned by +C is 1023 bytes. This may be changed by setting +the C attribute to a different value. + +Passing a value for the C<'path_read_len'> option when calling +the C method will temporarily override the current +value of this attribute. + +=item watch_timeout + +The C attribute value, in milliseconds, inherited by +all watch objects (of class Net::ZooKeeper::Watch) created by +calls to the C method. When a watch object's +C method is invoked without a C<'timeout'> option, +it waits for an event notification from the ZooKeeper cluster +for no longer than the timeout period specified by the value of +the watch object's C attribute. + +The default C attribute value for all watch objects +created by the C method is 1 minute (60000 +milliseconds). This may be changed for a particular handle +object by setting this attribute to a different value; afterwards, +the new value will be inherited by any watch objects created +by the handle object's C method. Previously +created watch objects will not be affected. + +Passing a value for the C<'timeout'> option when calling +the C method will temporarily override the current +value of this attribute and cause the newly created watch object +to inherit a different value. + +See also the C method, and the C attribute +and C method of the Net::ZooKeeper::Watch class. + +=item pending_watches + +The number of internal ZooKeeper watches created for this handle +object that are still awaiting an event notification from the +ZooKeeper cluster. + +Note that this number may be different than the number of +extant watch objects created by the handle object's C +method, not only because some event notifications may have +occurred, but also if any watch objects have been reassigned +by reusing them in more than one call to any of the C, +C, or C methods. + +This attribute is B and may not be modified. + +=back + +=head2 Net::ZooKeeper::Stat + +The Net::ZooKeeper::Stat class provides a hash interface to +the individual pieces of information which together compose the +state of a given ZooKeeper node. Net::ZooKeeper::Stat objects +are created by calling the C method on a Net::ZooKeeper +handle object, and may then be passed to any methods which accept +a C<'stat'> option value, such as C. + +Net::ZooKeeper::Stat objects may be reused multiple times. +If the Net::ZooKeeper method to which the stat object is +passed succeeds, then the stat object is updated with the newly +retrieved node state information, and any state information +previously stored in the stat object is overwritten. + +All of the attributes of stat objects are B. + +=over 4 + +=item ctime + +The creation time of the node in milliseconds since the epoch. + +=item mtime + +The time of the last modification of the node's data in +milliseconds since the epoch. + +=item data_len + +The length of the node's data in bytes. + +=item num_children + +The number of child nodes beneath of the current node. + +=item ephemeral_owner + +If the node was created with the C flag, +this attribute holds the session ID of the ZooKeeper client +which created the node. If the node was not created with +the C flag, this attribute is set to zero. + +=item version + +The number of revisions of the node's data. The ZooKeeper +cluster will increment this version number whenever the +node's data is changed. When the node is first created this +version number is initialized to zero. + +=item acl_version + +The number of revisions of the node's ACL. The ZooKeeper +cluster will increment this version number whenever the +node's ACL is changed. When the node is first created this +version number is initialized to zero. + +=item children_version + +The number of revisions of the node's list of child nodes. +The ZooKeeper cluster will increment this version number +whenever the list of child nodes is changed. When the node +is first created this version number is initialized to zero. + +=item czxid + +The ZooKeeper transaction ID (ZXID) of the transaction which +created the node. + +=item mzxid + +The ZooKeeper transaction ID (ZXID) of the transaction which +last modified the node's data. This is initially set to +the same transaction ID as the C attribute by the +C method. + +=item children_zxid + +The ZooKeeper transaction ID (ZXID) of the transaction which +last modified the node's list of child nodes. This is +initially set to the same transaction ID as the C +attribute by the C method. + +=back + +=head2 Net::ZooKeeper::Watch + +The Net::ZooKeeper::Watch class provides a hash interface +to the data returned by event notifications from the ZooKeeper +cluster. Net::ZooKeeper::Watch objects are created by calling +the C method on a Net::ZooKeeper handle object, and +may then be passed to any methods which accept a C<'watch'> +option value, such as C. + +Net::ZooKeeper::Watch objects may be reused multiple times. +Regardless of whether the Net::ZooKeeper method to which the +watch object is passed succeeds, the watch object will be +updated to receive an event notification exclusively for the +node referenced in that method call. In the case of an error, +however, the watch object may never receive any event +notification. + +=over 4 + +=item timeout + +The default timeout value, in milliseconds, for all +invocations of the C method made on the watch object. +When the C method is invoked without a +C<'timeout'> option value, it waits for an +event notification from the ZooKeeper cluster for no longer +than the timeout period specified by this attribute. +This default timeout period may be altered by setting this +attribute to a different value. + +Passing a value for the C<'timeout'> option when calling +the C method will temporarily override the current +value of this attribute and cause the C method to +use a different timeout period. + +When a Net::ZooKeeper handle object's C method is +invoked without a C<'timeout'> option, it returns a newly +created watch object whose C attribute value +is initialized to the current value of the handle object's +C attribute. When the C method is invoked +with a C<'timeout'> option, the new watch object's C +attribute value is initialized to the value specified by +the C<'timeout'> option. + +See also the C method, and the C attribute +and C method of the Net::ZooKeeper class. + +=item event + +The type of event which triggered the notification, such +as C if the node's data was changed. +See C<:events> for a list of the possible event types. +If zero, no event notification has occurred yet. + +Note that the events which will trigger a notification +will depend on the Net::ZooKeeper method to which +the watch object was passed. Watches set through the +C and C methods will report events relating +to the node's data, while watches set through the +C method will report events relating to the +creation or deletion of child nodes of the watched node. + +This attribute is B and may not be modified. + +=item state + +The state of the Net::ZooKeeper connection at the time of +the event notification. See C<:states> for a list of +the possible connection states. If zero, no event +notification has occurred yet. + +This attribute is B and may not be modified. + +=back + +=head1 METHODS + +=head2 Net::ZooKeeper + +The following methods are defined for the Net::ZooKeeper class. + +=over 4 + +=item new() + + $zkh = Net::ZooKeeper->new('host1:7000,host2:7000'); + $zkh = Net::ZooKeeper->new('host1:7000,host2:7000', + 'session_timeout' => $session_timeout, + 'session_id' => $session_id); + +Creates a new Net::ZooKeeper handle object and attempts to +connect to the one of the servers of the given ZooKeeper +cluster. As described in the L and +L sections, the ZooKeeper client code will +create an IO thread which maintains the connection with a +regular "heartbeat" request. In the event of a connection error +the IO thread will also attempt to reconnect to another one of +the servers using the same session ID. In general, these actions +should be invisible to the user, although Net::ZooKeeper methods +may return transient errors while the IO thread +reconnects with another server. + +To disconnect, undefine the Net::ZooKeeper handle object +or call the C method. (After calling C +the handle object can not be reused.) + +The ZooKeeper client code will send a "heartbeat" message +if a third of the session timeout period has elapsed without +any communication with the ZooKeeper server. A specific +session timeout period may be requested when creating a +Net::ZooKeeper handle object by supplying a value, in +milliseconds, for the C<'session_timeout'> option. The +ZooKeeper server adjust the requested timeout value so that +it is within a certain range of the server's C setting; +the actual session timeout value will be available as the +value of the handle's C attribute after at +least one method call has succeeded. See the C +attribute for more information. + +If no C<'session_timeout'> option is provided, the default +value of 10 seconds (10000 milliseconds) will be used in the +initial connection request; again, the actual timeout period to +which the server agrees will be available subsequently as the +value of the C attribute. + +Upon successful connection (i.e., after the success of a method +which requires communication with the server), the C +attribute will hold a short binary string which represents the +client's session ID as set by the server. All ephemeral nodes +created by the session are identified by this ID in the +C attribute of any Net::ZooKeeper::Stat objects +used to query their state. + +The ZooKeeper client code will use this session ID internally +whenever it tries to reconnect to another server in the ZooKeeper +cluster after detecting a failed connection. If it successfully +reconnects with the same session ID, the session will continue +and ephemeral nodes belonging to it will not be deleted. + +However, if the server determines that the session has timed +out (for example because no "heartbeat" requests have been +received within the agreed-upon session timeout period), the +session will be terminated by the cluster and all ephemeral nodes +owned by the current session automatically deleted. + +On occasion the ZooKeeper client code may not be able to quickly +reconnect to a live server and the caller may want to destroy +the existing Net::ZooKeeper handle object and attempt a +fresh connection using the same session ID as before with a +new Net::ZooKeeper object. To do so, save the C +attribute value before undefining the old handle object +and then pass that binary string as the value of the +C<'session_id'> option to the C method when creating the +next handle object. After the successful completion of a +method which requires communication with the server, if the +new handle object's C attribute value matches the +old session ID then the session has been successfully maintained; +otherwise, the old session was expired by the cluster. + +=item get_error() + + $code = $zkh->get_error(); + +Returns the ZooKeeper error code, if any, from the most +recent Net::ZooKeeper method invocation. The returned value +will be zero (equivalent to C) if no error occurred, +otherwise non-zero. Non-zero values may be compared to +the error code names exported by the C<:errors> tagset. + +See L for more details. + +=item add_auth() + + $zkh->add_auth('digest', "$username:$password"); + +The C method may be used to add authentication +credentials to a session. Once a credential has been added for +the current session, there is no way to disable it. + +When using the digest authentication scheme, note that the +username and password are transmitted in cleartext +to the ZooKeeper cluster. + +See L for additional details. + +=item create() + + $path = $zkh->create($req_path, $data); + $path = $zkh->create($req_path, $data, + 'flags' => (ZOO_EPHEMERAL | ZOO_SEQUENCE), + 'acl' => ZOO_OPEN_ACL_UNSAFE, + 'path_read_len' => 100); + +Requests that a node be created in the ZooKeeper cluster's +hierarchy with the given path and data. Upon success, +the returns the node's path, otherwise undef. + +The path returned by a successful C method call +may not be the new node's full path as it appears in the +ZooKeeper hierarchy, depending on the length of the actual +path and the value of the handle object's C +attribute. If the length of the actual path exceeds the +current value of the C attribute, the path +returned by the C method will be truncated; note +that the node's path in the ZooKeeper hierarchy is not +affected by this truncation. + +Specifying a value for the C<'path_read_len'> option will +temporarily override the value of the C +attribute for the duration of the C method. + +The flag values available for use with the C<'flags'> option +are C and C; both are +included in the C<:flags> tagset. The flags should be +combined with the bitwise OR operator if more than one +is required. + +The C flag causes the node to be marked as +ephemeral, meaning it will be automatically deleted if it +still exists when the client's session ends. The +C flag causes a unique integer to be appended +to the node's final path component. See the ZooKeeper +documentation for additional advice on how to use these flags. + +When creating a node it may be important to define an ACL +for it; to do this, pass a reference to an ACL array (as +described in L) using the C<'acl'> option. +See also the C<:acl_perms> and C<:acls> tagsets for lists +of the available ACL permission flags and pre-defined ACLs. + +=item delete() + + $ret = $zkh->delete($path); + $ret = $zkh->delete($path, 'version' => $version); + +Requests that a node be deleted from the ZooKeeper hierarchy. +Returns true upon success, false otherwise. + +If a value for the C<'version'> option is supplied, the node +will only be deleted if its version number matches the given +value. See the C attribute of the Net::ZooKeeper::Stat +class for details on node version numbering. + +=item exists() + + $ret = $zkh->exists($path); + $ret = $zkh->exists($path, 'stat' => $stat, 'watch' => $watch); + +Tests whether a given node exists. Returns true if the node +exists, otherwise false. When the C method is successful +but the node does not exist, it returns false, and C +will return C until another method is called on the +handle object. + +The C<'stat'> option may be used to request that a +Net::ZooKeeper::Stat object be updated with the node's +current state information. The stat object will only be +updated if the node exists and the C method +succeeds. The stat object must first have been created +using the C method. + +The C<'watch'> option may be used to request that a +Net::ZooKeeper::Watch object be assigned to receive +notification of an event which alters the node's data. +The watch object must first have been created using the +C method. If the watch object was previously +assigned to receive notifications for another node, it +will be reassigned even if the C method fails. + +=item get_children() + + @child_names = $zkh->get_children($path); + $num_children = $zkh->get_children($path, 'watch' => $watch); + +Queries the names or number of the child nodes stored beneath +a given node in the ZooKeeper hierarchy. In a list context, +returns a list of the child nodes' names upon success, otherwise +an empty list. When the C method is successful +but there are no child nodes, it returns an empty list, and +C will return C until another method is called +on the handle object. + +In a scalar context, C returns the number +of child nodes upon success, otherwise undef. + +The names of the child nodes are simply the final component +of the nodes' paths, i.e., the portion of their path which +follows the path of the given parent node, excluding the +"/" delimiter. + +The C<'watch'> option may be used to request that a +Net::ZooKeeper::Watch object be assigned to receive +notification of an event which alters the node's list of +child nodes. The watch object must first have been created +using the C method. If the watch object was +previously assigned to receive notifications for another node, +it will be reassigned even if the C method fails. + +=item get() + + $data = $zkh->get($path); + $data = $zkh->get($path, 'data_read_len' => 100, + 'stat' => $stat, 'watch' => $watch); + +Queries the data stored in a given node. Returns the +data as a string upon success, otherwise undef. Note +that the data may contain nulls if the node's data is +not a text string. + +If the length of the node's data exceeds the current value +of the handle object's C attribute, the +string returned by the C method will be truncated; +note that the node's data in the ZooKeeper cluster is not +affected by this truncation. + +Specifying a value for the C<'data_read_len'> option will +temporarily override the value of the C +attribute for the duration of the C method. + +The C<'stat'> option may be used to request that a +Net::ZooKeeper::Stat object be updated with the node's +current state information. The stat object will only be +updated if the C method succeeds. The stat object +must first have been created using the C method. + +The C<'watch'> option may be used to request that a +Net::ZooKeeper::Watch object be assigned to receive +notification of an event which alters the node's data. +The watch object must first have been created using the +C method. If the watch object was previously +assigned to receive notifications for another node, it +will be reassigned even if the C method fails. + +=item set() + + $ret = $zkh->set($path, $data); + $ret = $zkh->set($path, $data, 'version' => $version, + 'stat' => $stat); + +Requests that a node's data be updated in the ZooKeeper +hierarchy. Returns true upon success, false otherwise. + +If a value for the C<'version'> option is supplied, the node's +data will only be updated if its version number matches the +given value. See the C attribute of the +Net::ZooKeeper::Stat class for details on node version numbering. + +The C<'stat'> option may be used to request that a +Net::ZooKeeper::Stat object be updated with the node's +current state information. The stat object will only be +updated if the C method succeeds. The stat object +must first have been created using the C method. + +=item get_acl() + + @acl = $zkh->get_acl($path); + $num_acl_entries = $zkh->get_acl($path, 'stat' => $stat); + +Queries the ACL associated with a node in the ZooKeeper +hierarchy, if any. In a list context, returns an array with +the node's ACL entries upon success, otherwise +an empty list. When the C method is successful +but there are no ACL entries, it returns an empty list, and +C will return C until another method is called +on the handle object. + +The elements of the returned array are hashes, each of which +represents one ACL entry. Each hash contains C, +C, and C elements. See the L +section for additional details, and the +C<:acl_perms> and C<:acls> tagsets for lists of the +available ACL permission flags and pre-defined ACLs. + +In a scalar context, C returns the number +of ACL entries upon success, otherwise undef. + +The C<'stat'> option may be used to request that a +Net::ZooKeeper::Stat object be updated with the node's +current state information. The stat object will only be +updated if the C method succeeds. The stat object +must first have been created using the C method. + +=item set_acl() + + $acl = [{ + 'perms' => (ZOO_PERM_READ | ZOO_PERM_WRITE), + 'scheme' => 'digest', + 'id' => "$username:$digest" + }]; + $ret = $zkh->set_acl($path, $acl); + $ret = $zkh->set_acl($path, ZOO_OPEN_ACL_UNSAFE, + 'version' => $version); + +Requests that a node's ACL be updated in the ZooKeeper +hierarchy. Returns true upon success, false otherwise. + +The ACL should be passed as a reference to an array of +hashes, where each hash represents one ACL entry. Each +hash should contain C, C, and C elements +as described in the L section. +See also the C<:acl_perms> and C<:acls> tagsets for lists +of the available ACL permission flags and pre-defined ACLs. + +If a value for the C<'version'> option is supplied, the node's +ACL will only be updated if its version number matches the +given value. See the C attribute of the +Net::ZooKeeper::Stat class for details on node version numbering. + +=item stat() + + $stat = $zkh->stat(); + +Creates a new Net::ZooKeeper::Stat object which may be used +with the C<'stat'> option of the C, C, +C, and C methods. When the stat object +is passed to any of these methods, upon success its attribute +values are updated to reflect the current state of the +node specified in the method call. The stat object is not +updated if the method call does not succeed. + +=item watch() + + $watch = $zkh->watch(); + $watch = $zkh->watch('timeout' => $timeout); + +Creates a new Net::ZooKeeper::Watch object which may be +used to wait for event notifications from the ZooKeeper +cluster. Each time the watch object is passed to any +of the C, C, or C methods, +its attribute values are immediately reset to zero, and will +later be updated upon receipt of an appropriate event +notification for the node specified in the method call. + +The specific types of events which cause notifications to be +sent by the ZooKeeper cluster depend on the method call used. +After use with the C and C methods, the +watch object will be set to receive an event notification +caused by a modification of the node's data or the node itself +(e.g., deletion of the node). After use with the +C method, the watch object will be set to +receive an event notification caused by a modification +of the node's list of child nodes. + +Watch objects receive at most one event notification after +their assignment to a node by one of the C, +C, or C methods. Note that in the +case of an error, the watch object may never receive any +event notification. However, when the parent Net::ZooKeeper +handle object experiences a connection error, the ZooKeeper +client code will notify all pending watches with an event of +type C. See C for more information +regarding the watch object's attribute values after a +connection error. + +A watch object may be reused with another C, +C, or C method call at any time, +in which case the watch object's attribute values +are reset to zero and the watch object will no longer be updated +by any event notification relevant to the previous method call. + +When the C method is invoked without a C<'timeout'> +option, it returns a newly created watch object whose C +attribute value is initialized to the current value of the +Net::ZooKeeper handle object's C attribute. +Otherwise, when the C method is invoked with a +C<'timeout'> option, the new watch object's C attribute +value is initialized to the value specified by the +C<'timeout'> option. + +See also the C attribute, and the C +attribute and C method of the Net::ZooKeeper::Watch +class. + +=back + +=head2 Net::ZooKeeper::Stat + +No methods are defined for the Net::ZooKeeper::Stat class. + +=head2 Net::ZooKeeper::Watch + +Only one method is defined for the Net::ZooKeeper::Watch class. + +=over 4 + +=item wait() + + $ret = $watch->wait(); + $ret = $watch->wait('timeout' => $timeout); + +Waits for an event notification from the ZooKeeper cluster +for the node most recently associated with the watch object. +Nodes are associated with a watch object by passing the +watch object as the value of a C<'watch'> option to a +Net::ZooKeeper method; methods which accept a C<'watch'> option +are C, C, and C. + +When the C method is invoked with a C<'timeout'> +option, it waits for no more than the number of milliseconds +specified by the C<'timeout'> option. +Otherwise, when the C method is invoked without a +C<'timeout'> option, it waits for no more than the timeout +period specified by the value of the watch object's C +attribute. + +The C method returns true if an event notification +was received, otherwise false. When C returns true, +the C and C attributes of the watch object +will be updated with the event's type and the current +connection state. + +When the parent Net::ZooKeeper handle object experiences a +connection error, the ZooKeeper client code will notify all +pending watches with an event of type C. +In this case, the C attribute will report the current +state of the connection to the ZooKeeper cluster. + +See also the C attribute, and the C method +and C attribute of the Net::ZooKeeper class. + +=back + +=head1 FUNCTIONS + +The following functions have global scope and affect all +Net::ZooKeeper handle objects. + +=over 4 + +=item set_log_level() + + Net::ZooKeeper::set_log_level($level); + +The C function may be called to +alter the number and type of messages written to the current log +file handle (if any). The default value is C +which disables all logging. + +See the L section for more details and C<:log_levels> +for a list of the available log levels. + +=item set_deterministic_conn_order() + + Net::ZooKeeper::set_deterministic_conn_order(1); + +The C function +may be called to indicate whether or not the list of ZooKeeper +servers passed to the C method should be randomly permuted. +If set to a true value, the list of servers will not be altered. +The default false value indicates the list of servers will +be randomly reordered prior to connection. + +See the L section for more details. + +=back + +=head1 EXPORTS + +Nothing is exported by default. Various tagsets exist which +group the tags available for export into different categories: + +=over 4 + +=item :errors + +ZooKeeper error codes. These may be compared to the values +returned by the C method. + +=item :node_flags + +The ZooKeeper node flags C and C, +which may be passed in the C<'flags'> option to the C +method. When more than node flag is required they +should be combined using the bitwise OR operator. + +=item :acl_perms + +The ZooKeeper ACL permission flags which may be used in +the value of the C attribute of an ACL entry hash. +When more than one ACL permission flag is required they +should be combined using the bitwise OR operator. + +The available ACL permission flags are C, +C, C, C, +and C. For convenience, C is +defined as the bitwise OR of all of these flags. + +=item :acls + +Common ZooKeeper ACLs which may be useful. C +specifies a node which is entirely open to all users with no +restrictions at all. C specifies +a node which is readable by all users; permissions for other actions +are not defined in this ACL. C specifies a node +for which all actions require the same authentication credentials as +held by the session which created the node; this implies that a +session should authenticate with an appropriate scheme before +creating a node with this ACL. + +=item :events + +The ZooKeeper event types which are returned in value of +the C attribute a Net::ZooKeeper::Watch object after +an event occurs on a watched node. + +=item :states + +The ZooKeeper connection states which are returned in value of +the C attribute of a Net::ZooKeeper::Watch object after +an event occurs on a watched node. + +=item :log_levels + +The ZooKeeper log levels which may be passed to the +C function. The available +log levels are, from least to most verbose, C +(the default), C, C, +C, and C. + +=item :all + +Everything from all of the above tagsets. + +=back + +=head1 SEE ALSO + +The Apache ZooKeeper project's home page at +L provides a wealth of detail +on how to develop applications using ZooKeeper. + +=head1 AUTHOR + +Chris Darroch, Echrisd@apache.orgE + +=head1 COPYRIGHT AND LICENSE + +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. + +=cut + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/ZooKeeper.xs b/zookeeper-contrib/zookeeper-contrib-zkperl/ZooKeeper.xs new file mode 100644 index 0000000..4b6067b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/ZooKeeper.xs @@ -0,0 +1,2664 @@ +/* Net::ZooKeeper - Perl extension for Apache ZooKeeper + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#define PERL_NO_GET_CONTEXT + +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" + +#include /* pthread_mutex_lock(), etc. */ +#include /* memset(), etc. */ +#include /* CHAR_BIT */ +#include /* gettimeofday() */ + +#include + +#include "build/check_zk_version.h" + + +#define PACKAGE_NAME "Net::ZooKeeper" +#define PACKAGE_SIGNATURE 19631123 + +#define STAT_PACKAGE_NAME "Net::ZooKeeper::Stat" +#define STAT_PACKAGE_SIGNATURE 19960512 + +#define WATCH_PACKAGE_NAME "Net::ZooKeeper::Watch" +#define WATCH_PACKAGE_SIGNATURE 20050326 + +#define MAX_KEY_NAME_LEN 16 /* "children_version" */ + +#define NUM_ACL_ENTRY_KEYS 3 +#define NUM_KEYS 7 +#define NUM_STAT_KEYS 11 +#define NUM_WATCH_KEYS 3 + +#define DEFAULT_RECV_TIMEOUT_MSEC 10000 + +#define DEFAULT_DATA_BUF_LEN 1023 +#define DEFAULT_PATH_BUF_LEN 1023 +#define DEFAULT_WATCH_TIMEOUT 60000 + +#define ZOO_LOG_LEVEL_OFF 0 + +#ifndef strcaseEQ +#define strcaseEQ(a,b) (!strcasecmp((a),(b))) +#endif + + +typedef struct Stat zk_stat_t; + +typedef HV* Net__ZooKeeper__Stat; + +typedef struct zk_watch_t zk_watch_t; + +struct zk_watch_t { + pthread_mutex_t mutex; + pthread_cond_t cond; + int done; + int ret; + int event_type; + int event_state; + unsigned int timeout; + zk_watch_t *prev; + zk_watch_t *next; + int ref_count; +}; + +typedef HV* Net__ZooKeeper__Watch; + +typedef struct { + zhandle_t *handle; + zk_watch_t *first_watch; + int data_buf_len; + int path_buf_len; + unsigned int watch_timeout; + const char *hosts; + int hosts_len; + int last_ret; + int last_errno; +} zk_t; + +typedef HV* Net__ZooKeeper; + +typedef struct { + I32 signature; + union { + zk_t *zk; + zk_stat_t *stat; + zk_watch_t *watch; + } handle; +} zk_handle_t; + +typedef struct { + const char name[MAX_KEY_NAME_LEN + 1]; + U32 name_len; + size_t offset; + size_t size; + U32 hash; +} zk_key_t; + + +static zk_key_t zk_acl_entry_keys[NUM_ACL_ENTRY_KEYS] = { + {"perms", 0, 0, 0, 0}, + {"scheme", 0, 0, 0, 0}, + {"id", 0, 0, 0, 0} +}; + +static zk_key_t zk_keys[NUM_KEYS] = { + {"data_read_len", 0, 0, 0, 0}, + {"path_read_len", 0, 0, 0, 0}, + {"watch_timeout", 0, 0, 0, 0}, + {"hosts", 0, 0, 0, 0}, + {"session_timeout", 0, 0, 0, 0}, + {"session_id", 0, 0, 0, 0}, + {"pending_watches", 0, 0, 0, 0} +}; + +static zk_key_t zk_stat_keys[NUM_STAT_KEYS] = { + {"czxid", 0, offsetof(struct Stat, czxid), + sizeof(((struct Stat*) 0)->czxid), 0}, + {"mzxid", 0, offsetof(struct Stat, mzxid), + sizeof(((struct Stat*) 0)->mzxid), 0}, + {"ctime", 0, offsetof(struct Stat, ctime), + sizeof(((struct Stat*) 0)->ctime), 0}, + {"mtime", 0, offsetof(struct Stat, mtime), + sizeof(((struct Stat*) 0)->mtime), 0}, + {"version", 0, offsetof(struct Stat, version), + sizeof(((struct Stat*) 0)->version), 0}, + {"children_version", 0, offsetof(struct Stat, cversion), + sizeof(((struct Stat*) 0)->cversion), 0}, + {"acl_version", 0, offsetof(struct Stat, aversion), + sizeof(((struct Stat*) 0)->aversion), 0}, + {"ephemeral_owner", 0, offsetof(struct Stat, ephemeralOwner), + sizeof(((struct Stat*) 0)->ephemeralOwner), 0}, + {"data_len", 0, offsetof(struct Stat, dataLength), + sizeof(((struct Stat*) 0)->dataLength), 0}, + {"num_children", 0, offsetof(struct Stat, numChildren), + sizeof(((struct Stat*) 0)->numChildren), 0}, + {"children_zxid", 0, offsetof(struct Stat, pzxid), + sizeof(((struct Stat*) 0)->pzxid), 0} +}; + +static zk_key_t zk_watch_keys[NUM_WATCH_KEYS] = { + {"timeout", 0, 0, 0, 0}, + {"event", 0, 0, 0, 0}, + {"state", 0, 0, 0, 0} +}; + + +static void _zk_watcher(zhandle_t *handle, int type, int state, + const char *path, void *context) +{ + zk_watch_t *watch_ctx = context; + + pthread_mutex_lock(&watch_ctx->mutex); + + watch_ctx->event_type = type; + watch_ctx->event_state = state; + + watch_ctx->done = 1; + + pthread_cond_signal(&watch_ctx->cond); + pthread_mutex_unlock(&watch_ctx->mutex); + + return; +} + +static void _zk_auth_completion(int ret, const void *data) +{ + zk_watch_t *watch_ctx = (zk_watch_t*) data; + + pthread_mutex_lock(&watch_ctx->mutex); + + watch_ctx->ret = ret; + + watch_ctx->done = 1; + + pthread_cond_signal(&watch_ctx->cond); + pthread_mutex_unlock(&watch_ctx->mutex); + + return; +} + +static zk_watch_t *_zk_create_watch(pTHX) +{ + zk_watch_t *watch; + + Newxz(watch, 1, zk_watch_t); + + if (pthread_mutex_init(&watch->mutex, NULL)) { + int save_errno = errno; + + Safefree(watch); + + errno = save_errno; + return NULL; + } + + if (pthread_cond_init(&watch->cond, NULL)) { + int save_errno = errno; + + pthread_mutex_destroy(&watch->mutex); + Safefree(watch); + + errno = save_errno; + return NULL; + } + + return watch; +} + +static void _zk_destroy_watch(pTHX_ zk_watch_t *watch) +{ + pthread_cond_destroy(&watch->cond); + pthread_mutex_destroy(&watch->mutex); + + Safefree(watch); + + return; +} + +static zk_watch_t *_zk_acquire_watch(pTHX) +{ + zk_watch_t *watch = _zk_create_watch(aTHX); + + if (watch) { + watch->ref_count = 1; + } + + return watch; +} + +static void _zk_release_watch(pTHX_ zk_watch_t *watch, int list) +{ + if (list) { + if (watch->prev) { + watch->prev->next = watch->next; + } + if (watch->next) { + watch->next->prev = watch->prev; + } + watch->prev = NULL; + watch->next = NULL; + } + + if (--watch->ref_count == 0) { + _zk_destroy_watch(aTHX_ watch); + } + + return; +} + +static unsigned int _zk_release_watches(pTHX_ zk_watch_t *first_watch, + int final) +{ + zk_watch_t *watch = first_watch->next; + unsigned int pending_watches = 0; + + while (watch) { + zk_watch_t *next_watch = watch->next; + int done = final; + + if (!final) { + pthread_mutex_lock(&watch->mutex); + done = watch->done; + pthread_mutex_unlock(&watch->mutex); + } + + if (done) { + _zk_release_watch(aTHX_ watch, 1); + } + else { + ++pending_watches; + } + + watch = next_watch; + } + + return pending_watches; +} + +static void _zk_replace_watch(pTHX_ zk_handle_t *handle, + zk_watch_t *first_watch, + zk_watch_t *old_watch, zk_watch_t *new_watch) +{ + zk_watch_t *next_watch; + + new_watch->timeout = old_watch->timeout; + + _zk_release_watch(aTHX_ old_watch, 0); + + /* cleanup any completed watches not tied to a handle */ + _zk_release_watches(aTHX_ first_watch, 0); + + next_watch = first_watch->next; + + new_watch->prev = first_watch; + new_watch->next = next_watch; + + if (next_watch) { + next_watch->prev = new_watch; + } + + first_watch->next = new_watch; + + ++new_watch->ref_count; + + handle->handle.watch = new_watch; + + return; +} + +static void _zk_free_acl(pTHX_ struct ACL_vector *acl) +{ + if (acl->data) { + Safefree(acl->data); + } + + return; +} + +static const char *_zk_fill_acl(pTHX_ AV *acl_arr, struct ACL_vector *acl) +{ + I32 num_acl_entries = av_len(acl_arr) + 1; + int i; + + Zero(acl, 1, struct ACL_vector); + + if (num_acl_entries <= 0) { + return NULL; + } + else if (num_acl_entries > PERL_INT_MAX) { + num_acl_entries = PERL_INT_MAX; + } + + Newx(acl->data, num_acl_entries, struct ACL); + + for (i = 0; i < num_acl_entries; ++i) { + SV **acl_entry_ptr; + HV *acl_entry_hash; + zk_key_t *key; + SV **val_ptr; + struct ACL acl_entry; + + acl_entry_ptr = av_fetch(acl_arr, i, 0); + + if (!acl_entry_ptr) { + continue; + } + + if (!SvROK(*acl_entry_ptr) || + SvTYPE(SvRV(*acl_entry_ptr)) != SVt_PVHV) { + _zk_free_acl(aTHX_ acl); + + return "invalid ACL entry hash reference"; + } + + acl_entry_hash = (HV*) SvRV(*acl_entry_ptr); + + key = &zk_acl_entry_keys[0]; + val_ptr = hv_fetch(acl_entry_hash, key->name, key->name_len, 0); + + if (!val_ptr) { + _zk_free_acl(aTHX_ acl); + + return "no ACL entry perms element"; + } + + acl_entry.perms = SvIV(*val_ptr); + + if (!acl_entry.perms || (acl_entry.perms & ~ZOO_PERM_ALL)) { + _zk_free_acl(aTHX_ acl); + + return "invalid ACL entry perms"; + } + + key = &zk_acl_entry_keys[1]; + val_ptr = hv_fetch(acl_entry_hash, key->name, key->name_len, 0); + + if (!val_ptr) { + _zk_free_acl(aTHX_ acl); + + return "no ACL entry scheme element"; + } + + acl_entry.id.scheme = SvPV_nolen(*val_ptr); + + key = &zk_acl_entry_keys[2]; + val_ptr = hv_fetch(acl_entry_hash, key->name, key->name_len, 0); + + if (!val_ptr) { + _zk_free_acl(aTHX_ acl); + + return "no ACL entry id element"; + } + + acl_entry.id.id = SvPV_nolen(*val_ptr); + + ++acl->count; + acl->data[i] = acl_entry; + } + + return NULL; +} + +static void _zk_fill_acl_entry_hash(pTHX_ struct ACL *acl_entry, + HV *acl_entry_hash) +{ + zk_key_t *key; + SV *val; + + key = &zk_acl_entry_keys[0]; + val = newSViv(acl_entry->perms); + + if (!hv_store(acl_entry_hash, key->name, key->name_len, val, key->hash)) { + SvREFCNT_dec(val); + } + + key = &zk_acl_entry_keys[1]; + val = newSVpv(acl_entry->id.scheme, 0); + + if (!hv_store(acl_entry_hash, key->name, key->name_len, val, key->hash)) { + SvREFCNT_dec(val); + } + + key = &zk_acl_entry_keys[2]; + val = newSVpv(acl_entry->id.id, 0); + + if (!hv_store(acl_entry_hash, key->name, key->name_len, val, key->hash)) { + SvREFCNT_dec(val); + } + + return; +} + +static zk_handle_t *_zk_check_handle_inner(pTHX_ HV *attr_hash, + I32 package_signature) +{ + zk_handle_t *handle = NULL; + + if (SvRMAGICAL(attr_hash)) { + MAGIC *magic = mg_find((SV*) attr_hash, PERL_MAGIC_ext); + + if (magic) { + handle = (zk_handle_t*) magic->mg_ptr; + + if (handle->signature != package_signature) { + handle = NULL; + } + } + } + + return handle; +} + +static zk_handle_t *_zk_check_handle_outer(pTHX_ HV *hash, HV **attr_hash_ptr, + const char *package_name, + I32 package_signature) +{ + zk_handle_t *handle = NULL; + + if (attr_hash_ptr) { + *attr_hash_ptr = NULL; + } + + if (SvRMAGICAL((SV*) hash)) { + MAGIC *magic = mg_find((SV*) hash, PERL_MAGIC_tied); + + if (magic) { + SV *attr = magic->mg_obj; + + if (SvROK(attr) && SvTYPE(SvRV(attr)) == SVt_PVHV && + sv_derived_from(attr, package_name)) { + HV *attr_hash = (HV*) SvRV(attr); + + handle = _zk_check_handle_inner(aTHX_ attr_hash, + package_signature); + + if (handle && attr_hash_ptr) { + *attr_hash_ptr = attr_hash; + } + } + } + } + + return handle; +} + +static zk_t *_zk_get_handle_inner(pTHX_ Net__ZooKeeper attr_hash) +{ + zk_handle_t *handle; + + handle = _zk_check_handle_inner(aTHX_ attr_hash, PACKAGE_SIGNATURE); + + return handle ? handle->handle.zk : NULL; +} + +static zk_t *_zk_get_handle_outer(pTHX_ Net__ZooKeeper zkh) +{ + zk_handle_t *handle; + + handle = _zk_check_handle_outer(aTHX_ zkh, NULL, PACKAGE_NAME, + PACKAGE_SIGNATURE); + + return handle ? handle->handle.zk : NULL; +} + +static zk_stat_t *_zks_get_handle_inner(pTHX_ Net__ZooKeeper__Stat attr_hash) +{ + zk_handle_t *handle; + + handle = _zk_check_handle_inner(aTHX_ attr_hash, STAT_PACKAGE_SIGNATURE); + + return handle ? handle->handle.stat : NULL; +} + +static zk_stat_t *_zks_get_handle_outer(pTHX_ Net__ZooKeeper__Stat zksh) +{ + zk_handle_t *handle; + + handle = _zk_check_handle_outer(aTHX_ zksh, NULL, STAT_PACKAGE_NAME, + STAT_PACKAGE_SIGNATURE); + + return handle ? handle->handle.stat : NULL; +} + +static zk_watch_t *_zkw_get_handle_inner(pTHX_ Net__ZooKeeper__Watch attr_hash) +{ + zk_handle_t *handle; + + handle = _zk_check_handle_inner(aTHX_ attr_hash, WATCH_PACKAGE_SIGNATURE); + + return handle ? handle->handle.watch : NULL; +} + +static zk_watch_t *_zkw_get_handle_outer(pTHX_ Net__ZooKeeper__Watch zkwh, + zk_handle_t **handle_ptr) +{ + zk_handle_t *handle; + + handle = _zk_check_handle_outer(aTHX_ zkwh, NULL, WATCH_PACKAGE_NAME, + WATCH_PACKAGE_SIGNATURE); + + if (handle_ptr) { + *handle_ptr = handle; + } + + return handle ? handle->handle.watch : NULL; +} + + +MODULE = Net::ZooKeeper PACKAGE = Net::ZooKeeper PREFIX = zk_ + +REQUIRE: 1.9508 + +PROTOTYPES: ENABLE + +BOOT: +{ + int i; + + for (i = 0; i < NUM_ACL_ENTRY_KEYS; ++i) { + zk_key_t *key = &zk_acl_entry_keys[i]; + + key->name_len = strlen(key->name); + PERL_HASH(key->hash, key->name, key->name_len); + } + + for (i = 0; i < NUM_KEYS; ++i) { + zk_keys[i].name_len = strlen(zk_keys[i].name); + } + + for (i = 0; i < NUM_STAT_KEYS; ++i) { + zk_stat_keys[i].name_len = strlen(zk_stat_keys[i].name); + } + + for (i = 0; i < NUM_WATCH_KEYS; ++i) { + zk_watch_keys[i].name_len = strlen(zk_watch_keys[i].name); + } + + zoo_set_log_stream(NULL); + zoo_set_debug_level(0); +} + + +I32 +zk_constant(alias=Nullch) + char *alias + ALIAS: + ZOK = ZOK + ZSYSTEMERROR = ZSYSTEMERROR + ZRUNTIMEINCONSISTENCY = ZRUNTIMEINCONSISTENCY + ZDATAINCONSISTENCY = ZDATAINCONSISTENCY + ZCONNECTIONLOSS = ZCONNECTIONLOSS + ZMARSHALLINGERROR = ZMARSHALLINGERROR + ZUNIMPLEMENTED = ZUNIMPLEMENTED + ZOPERATIONTIMEOUT = ZOPERATIONTIMEOUT + ZBADARGUMENTS = ZBADARGUMENTS + ZINVALIDSTATE = ZINVALIDSTATE + ZAPIERROR = ZAPIERROR + ZNONODE = ZNONODE + ZNOAUTH = ZNOAUTH + ZBADVERSION = ZBADVERSION + ZNOCHILDRENFOREPHEMERALS = ZNOCHILDRENFOREPHEMERALS + ZNODEEXISTS = ZNODEEXISTS + ZNOTEMPTY = ZNOTEMPTY + ZSESSIONEXPIRED = ZSESSIONEXPIRED + ZINVALIDCALLBACK = ZINVALIDCALLBACK + ZINVALIDACL = ZINVALIDACL + ZAUTHFAILED = ZAUTHFAILED + ZCLOSING = ZCLOSING + ZNOTHING = ZNOTHING + + ZOO_EPHEMERAL = ZOO_EPHEMERAL + ZOO_SEQUENCE = ZOO_SEQUENCE + + ZOO_PERM_READ = ZOO_PERM_READ + ZOO_PERM_WRITE = ZOO_PERM_WRITE + ZOO_PERM_CREATE = ZOO_PERM_CREATE + ZOO_PERM_DELETE = ZOO_PERM_DELETE + ZOO_PERM_ADMIN = ZOO_PERM_ADMIN + ZOO_PERM_ALL = ZOO_PERM_ALL + + ZOO_CREATED_EVENT = ZOO_CREATED_EVENT + ZOO_DELETED_EVENT = ZOO_DELETED_EVENT + ZOO_CHANGED_EVENT = ZOO_CHANGED_EVENT + ZOO_CHILD_EVENT = ZOO_CHILD_EVENT + ZOO_SESSION_EVENT = ZOO_SESSION_EVENT + ZOO_NOTWATCHING_EVENT = ZOO_NOTWATCHING_EVENT + + ZOO_EXPIRED_SESSION_STATE = ZOO_EXPIRED_SESSION_STATE + ZOO_AUTH_FAILED_STATE = ZOO_AUTH_FAILED_STATE + ZOO_CONNECTING_STATE = ZOO_CONNECTING_STATE + ZOO_ASSOCIATING_STATE = ZOO_ASSOCIATING_STATE + ZOO_CONNECTED_STATE = ZOO_CONNECTED_STATE + + ZOO_LOG_LEVEL_OFF = ZOO_LOG_LEVEL_OFF + ZOO_LOG_LEVEL_ERROR = ZOO_LOG_LEVEL_ERROR + ZOO_LOG_LEVEL_WARN = ZOO_LOG_LEVEL_WARN + ZOO_LOG_LEVEL_INFO = ZOO_LOG_LEVEL_INFO + ZOO_LOG_LEVEL_DEBUG = ZOO_LOG_LEVEL_DEBUG + CODE: + if (!ix) { + if (!alias) { + alias = GvNAME(CvGV(cv)); + } + + if (strEQ(alias, "ZOK")) { + RETVAL = ZOK; + } + else if (strEQ(alias, "ZOO_LOG_LEVEL_OFF")) { + RETVAL = ZOO_LOG_LEVEL_OFF; + } + else { + Perl_croak(aTHX_ "unknown " PACKAGE_NAME " constant: %s", + alias); + } + } + else { + RETVAL = ix; + } + OUTPUT: + RETVAL + + +AV * +zk_acl_constant(alias=Nullch) + char *alias + ALIAS: + ZOO_OPEN_ACL_UNSAFE = 1 + ZOO_READ_ACL_UNSAFE = 2 + ZOO_CREATOR_ALL_ACL = 3 + PREINIT: + struct ACL_vector acl; + AV *acl_arr; + int i; + PPCODE: + if (!ix && !alias) { + alias = GvNAME(CvGV(cv)); + } + + if (ix == 1 || (alias != NULL && strEQ(alias, "ZOO_OPEN_ACL_UNSAFE"))) { + acl = ZOO_OPEN_ACL_UNSAFE; + } + else if (ix == 2 || (alias != NULL && strEQ(alias, "ZOO_READ_ACL_UNSAFE"))) { + acl = ZOO_READ_ACL_UNSAFE; + } + else if (ix == 3 || (alias != NULL && strEQ(alias, "ZOO_CREATOR_ALL_ACL"))) { + acl = ZOO_CREATOR_ALL_ACL; + } + else { + Perl_croak(aTHX_ "unknown " PACKAGE_NAME " constant: %s", alias); + } + + acl_arr = newAV(); + + av_extend(acl_arr, acl.count); + + for (i = 0; i < acl.count; ++i) { + HV *acl_entry_hash = newHV(); + SV *val; + + _zk_fill_acl_entry_hash(aTHX_ &acl.data[i], acl_entry_hash); + + val = newRV_noinc((SV*) acl_entry_hash); + + if (!av_store(acl_arr, i, val)) { + SvREFCNT_dec(val); + } + } + + ST(0) = sv_2mortal(newRV_noinc((SV*) acl_arr)); + + XSRETURN(1); + + +void +zk_set_log_level(level) + int level + PPCODE: + if (level < ZOO_LOG_LEVEL_OFF || level > ZOO_LOG_LEVEL_DEBUG) { + Perl_croak(aTHX_ "invalid log level: %d", level); + } + + zoo_set_debug_level(level); + + XSRETURN_EMPTY; + + +void +zk_set_deterministic_conn_order(flag) + bool flag + PPCODE: + zoo_deterministic_conn_order(!!flag); + + XSRETURN_EMPTY; + + +void +zk_new(package, hosts, ...) + char *package + char *hosts + PREINIT: + int recv_timeout = DEFAULT_RECV_TIMEOUT_MSEC; + const clientid_t *client_id = NULL; + zk_t *zk; + zk_handle_t *handle; + HV *stash, *zk_hash, *attr_hash; + SV *attr; + int i; + PPCODE: + if (items > 2 && items % 2) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + for (i = 2; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "session_timeout")) { + recv_timeout = SvIV(ST(i + 1)); + + /* NOTE: would be nice if requirement in zookeeper_interest() + * that recv_timeout*2 be non-negative was documented + */ + if (recv_timeout < 0 || recv_timeout > (PERL_INT_MAX >> 1)) { + Perl_croak(aTHX_ "invalid session timeout: %d", + recv_timeout); + } + } + else if (strcaseEQ(key, "session_id")) { + STRLEN client_id_len; + + client_id = (const clientid_t*) SvPV(ST(i + 1), client_id_len); + + if (client_id_len != sizeof(clientid_t)) { + Perl_croak(aTHX_ "invalid session ID"); + } + } + } + + Newxz(zk, 1, zk_t); + + zk->handle = zookeeper_init(hosts, NULL, recv_timeout, + client_id, NULL, 0); + + if (!zk->handle) { + Safefree(zk); + + XSRETURN_UNDEF; + } + + Newxz(zk->first_watch, 1, zk_watch_t); + + zk->data_buf_len = DEFAULT_DATA_BUF_LEN; + zk->path_buf_len = DEFAULT_PATH_BUF_LEN; + zk->watch_timeout = DEFAULT_WATCH_TIMEOUT; + + zk->hosts_len = strlen(hosts); + zk->hosts = savepvn(hosts, zk->hosts_len); + + Newx(handle, 1, zk_handle_t); + + handle->signature = PACKAGE_SIGNATURE; + handle->handle.zk = zk; + + /* We use several tricks from DBI here. The attr_hash is our + * empty inner hash; we attach extra magic to it in the form of + * our zk_handle_t structure. Then we tie attr_hash to zk_hash, + * our outer hash. This is what is passed around (by reference) by + * callers. + * + * Most methods use _zk_get_handle_outer() which finds our inner + * handle, then returns the zk_t structure from its extra magic + * pointer. + * + * However, the tied hash methods, FETCH(), STORE(), and so forth, + * receive an already-dereferenced inner handle hash. This is + * because we bless both inner and outer handles into this class, + * so when a caller's code references a hash element in our + * outer handle, Perl detects its tied magic, looks up the + * tied object (our inner handle) and invokes the tied hash methods + * in its class on it. Since we blessed it into the same class + * as the outer handle, these methods simply reside in our package. + */ + + stash = gv_stashpv(package, GV_ADDWARN); + + attr_hash = newHV(); + + sv_magic((SV*) attr_hash, Nullsv, PERL_MAGIC_ext, + (const char*) handle, 0); + + attr = sv_bless(newRV_noinc((SV*) attr_hash), stash); + + zk_hash = newHV(); + + sv_magic((SV*) zk_hash, attr, PERL_MAGIC_tied, Nullch, 0); + SvREFCNT_dec(attr); + + ST(0) = sv_bless(sv_2mortal(newRV_noinc((SV*) zk_hash)), stash); + + XSRETURN(1); + + +void +zk_DESTROY(zkh) + Net::ZooKeeper zkh + PREINIT: + zk_handle_t *handle; + HV *attr_hash; + int ret = ZBADARGUMENTS; + PPCODE: + handle = _zk_check_handle_outer(aTHX_ zkh, &attr_hash, + PACKAGE_NAME, PACKAGE_SIGNATURE); + + if (!handle) { + handle = _zk_check_handle_inner(aTHX_ zkh, PACKAGE_SIGNATURE); + + if (handle) { + attr_hash = zkh; + zkh = NULL; + } + } + + if (handle) { + zk_t *zk = handle->handle.zk; + + ret = zookeeper_close(zk->handle); + + /* detach all now-inactive watches still tied to handles */ + _zk_release_watches(aTHX_ zk->first_watch, 1); + + Safefree(zk->first_watch); + Safefree(zk->hosts); + Safefree(zk); + Safefree(handle); + + sv_unmagic((SV*) attr_hash, PERL_MAGIC_ext); + } + + if (zkh && attr_hash) { + sv_unmagic((SV*) zkh, PERL_MAGIC_tied); + } + + if (GIMME_V == G_VOID) { + XSRETURN_EMPTY; + } + else if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zk_CLONE(package) + char *package + PPCODE: + XSRETURN_EMPTY; + + +void +zk_CLONE_SKIP(package) + char *package + PPCODE: + XSRETURN_YES; + + +void +zk_TIEHASH(package, ...) + char *package + PPCODE: + Perl_croak(aTHX_ "tying hashes of class " + PACKAGE_NAME " not supported"); + + +void +zk_UNTIE(attr_hash, ref_count) + Net::ZooKeeper attr_hash + IV ref_count + PPCODE: + Perl_croak(aTHX_ "untying hashes of class " + PACKAGE_NAME " not supported"); + + +void +zk_FIRSTKEY(attr_hash) + Net::ZooKeeper attr_hash + PREINIT: + zk_t *zk; + PPCODE: + zk = _zk_get_handle_inner(aTHX_ attr_hash); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + ST(0) = sv_2mortal(newSVpvn(zk_keys[0].name, zk_keys[0].name_len)); + + XSRETURN(1); + + +void +zk_NEXTKEY(attr_hash, attr_key) + Net::ZooKeeper attr_hash + SV *attr_key + PREINIT: + zk_t *zk; + char *key; + int i; + PPCODE: + zk = _zk_get_handle_inner(aTHX_ attr_hash); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_KEYS; ++i) { + if (strcaseEQ(key, zk_keys[i].name)) { + ++i; + + break; + } + } + + if (i < NUM_KEYS) { + ST(0) = sv_2mortal(newSVpvn(zk_keys[i].name, zk_keys[i].name_len)); + + XSRETURN(1); + } + else { + XSRETURN_EMPTY; + } + + +void +zk_SCALAR(attr_hash) + Net::ZooKeeper attr_hash + PPCODE: + XSRETURN_YES; + + +void +zk_FETCH(attr_hash, attr_key) + Net::ZooKeeper attr_hash + SV *attr_key + PREINIT: + zk_t *zk; + char *key; + SV *val = NULL; + PPCODE: + zk = _zk_get_handle_inner(aTHX_ attr_hash); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + if (strcaseEQ(key, "data_read_len")) { + val = newSViv(zk->data_buf_len); + } + else if (strcaseEQ(key, "path_read_len")) { + val = newSViv(zk->path_buf_len); + } + else if (strcaseEQ(key, "watch_timeout")) { + val = newSVuv(zk->watch_timeout); + } + else if (strcaseEQ(key, "hosts")) { + val = newSVpvn(zk->hosts, zk->hosts_len); + } + else if (strcaseEQ(key, "session_timeout")) { + val = newSViv(zoo_recv_timeout(zk->handle)); + } + else if (strcaseEQ(key, "session_id")) { + const clientid_t *client_id; + clientid_t null_client_id; + + client_id = zoo_client_id(zk->handle); + + memset(&null_client_id, 0, sizeof(clientid_t)); + + if (!memcmp(client_id, &null_client_id, sizeof(clientid_t))) { + val = newSVpv("", 0); + } + else { + val = newSVpvn((const char*) client_id, sizeof(clientid_t)); + } + } + else if (strcaseEQ(key, "pending_watches")) { + /* cleanup any completed watches not tied to a handle */ + val = newSVuv(_zk_release_watches(aTHX_ zk->first_watch, 0)); + } + + if (val) { + ST(0) = sv_2mortal(val); + + XSRETURN(1); + } + + Perl_warn(aTHX_ "invalid element: %s", key); + + XSRETURN_UNDEF; + + +void +zk_STORE(attr_hash, attr_key, attr_val) + Net::ZooKeeper attr_hash + SV *attr_key + SV *attr_val + PREINIT: + zk_t *zk; + char *key; + PPCODE: + zk = _zk_get_handle_inner(aTHX_ attr_hash); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + if (strcaseEQ(key, "data_read_len")) { + int val = SvIV(attr_val); + + if (val < 0) { + Perl_croak(aTHX_ "invalid data read length: %d", val); + } + + zk->data_buf_len = val; + } + else if (strcaseEQ(key, "path_read_len")) { + int val = SvIV(attr_val); + + if (val < 0) { + Perl_croak(aTHX_ "invalid path read length: %d", val); + } + + zk->path_buf_len = val; + } + else if (strcaseEQ(key, "watch_timeout")) { + zk->watch_timeout = SvUV(attr_val); + } + else { + int i; + + for (i = 0; i < NUM_KEYS; ++i) { + if (strcaseEQ(key, zk_keys[i].name)) { + Perl_warn(aTHX_ "read-only element: %s", key); + + XSRETURN_EMPTY; + } + } + + Perl_warn(aTHX_ "invalid element: %s", key); + } + + XSRETURN_EMPTY; + + +void +zk_EXISTS(attr_hash, attr_key) + Net::ZooKeeper attr_hash + SV *attr_key + PREINIT: + zk_t *zk; + char *key; + int i; + PPCODE: + zk = _zk_get_handle_inner(aTHX_ attr_hash); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_KEYS; ++i) { + if (strcaseEQ(key, zk_keys[i].name)) { + XSRETURN_YES; + } + } + + XSRETURN_NO; + + +void +zk_DELETE(attr_hash, attr_key) + Net::ZooKeeper attr_hash + SV *attr_key + PPCODE: + Perl_warn(aTHX_ "deleting elements from hashes of class " + PACKAGE_NAME " not supported"); + + XSRETURN_EMPTY; + + +void +zk_CLEAR(attr_hash) + Net::ZooKeeper attr_hash + PPCODE: + Perl_warn(aTHX_ "clearing hashes of class " + PACKAGE_NAME " not supported"); + + XSRETURN_EMPTY; + + +SV * +zk_get_error(zkh) + Net::ZooKeeper zkh + PREINIT: + zk_t *zk; + CODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + RETVAL = newSViv(zk->last_ret); + errno = zk->last_errno; + OUTPUT: + RETVAL + + +void +zk_add_auth(zkh, scheme, cert) + Net::ZooKeeper zkh + char *scheme + char *cert; cert = (char *) SvPV($arg, cert_len); + PREINIT: + zk_t *zk; + STRLEN cert_len; + zk_watch_t *watch; + int ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (cert_len > PERL_INT_MAX) { + Perl_croak(aTHX_ "invalid certificate length: %u", cert_len); + } + + watch = _zk_create_watch(aTHX); + + if (!watch) { + /* errno will be set */ + zk->last_ret = ZSYSTEMERROR; + zk->last_errno = errno; + + XSRETURN_NO; + } + + errno = 0; + ret = zoo_add_auth(zk->handle, scheme, cert, cert_len, + _zk_auth_completion, watch); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (ret == ZOK) { + pthread_mutex_lock(&watch->mutex); + + while (!watch->done) { + pthread_cond_wait(&watch->cond, &watch->mutex); + } + + pthread_mutex_unlock(&watch->mutex); + + if (watch->done) { + ret = watch->ret; + } + else { + ret = ZINVALIDSTATE; + } + + /* errno may be set while we waited */ + zk->last_ret = ret; + zk->last_errno = errno; + } + + _zk_destroy_watch(aTHX_ watch); + + if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zk_create(zkh, path, buf, ...) + Net::ZooKeeper zkh + char *path + char *buf; buf = (char *) SvPV($arg, buf_len); + PREINIT: + zk_t *zk; + STRLEN buf_len; + int flags = 0; + char *path_buf; + int path_buf_len; + AV *acl_arr = NULL; + struct ACL_vector acl; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 3 && !(items % 2)) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + if (buf_len > PERL_INT_MAX) { + Perl_croak(aTHX_ "invalid data length: %u", buf_len); + } + + path_buf_len = zk->path_buf_len; + + for (i = 3; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "path_read_len")) { + path_buf_len = SvIV(ST(i + 1)); + + if (path_buf_len < 2) { + Perl_croak(aTHX_ "invalid path read length: %d", + path_buf_len); + } + } + else if (strcaseEQ(key, "flags")) { + flags = SvIV(ST(i + 1)); + + if (flags & ~(ZOO_SEQUENCE | ZOO_EPHEMERAL)) { + Perl_croak(aTHX_ "invalid create flags: %d", flags); + } + } + else if (strcaseEQ(key, "acl")) { + const char *err; + + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVAV) { + Perl_croak(aTHX_ "invalid ACL array reference"); + } + + acl_arr = (AV*) SvRV(ST(i + 1)); + + err = _zk_fill_acl(aTHX_ acl_arr, &acl); + + if (err) { + Perl_croak(aTHX_ err); + } + } + } + + /* NOTE: would be nice to be able to rely on null-terminated string */ + ++path_buf_len; + Newxz(path_buf, path_buf_len, char); + + errno = 0; + ret = zoo_create(zk->handle, path, buf, buf_len, + (acl_arr ? &acl : NULL), flags, + path_buf, path_buf_len); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (acl_arr) { + _zk_free_acl(aTHX_ &acl); + } + + if (ret == ZOK) { + ST(0) = sv_newmortal(); +#ifdef SV_HAS_TRAILING_NUL + sv_usepvn_flags(ST(0), path_buf, strlen(path_buf), + SV_HAS_TRAILING_NUL); +#else + sv_usepvn(ST(0), path_buf, strlen(path_buf)); +#endif + SvCUR_set(ST(0), strlen(path_buf)); + + XSRETURN(1); + } + + Safefree(path_buf); + + XSRETURN_UNDEF; + + +void +zk_delete(zkh, path, ...) + Net::ZooKeeper zkh + char *path + PREINIT: + zk_t *zk; + int version = -1; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 2 && items % 2) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + for (i = 2; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "version")) { + version = SvIV(ST(i + 1)); + + if (version < 0) { + Perl_croak(aTHX_ "invalid version requirement: %d", + version); + } + } + } + + errno = 0; + ret = zoo_delete(zk->handle, path, version); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zk_exists(zkh, path, ...) + Net::ZooKeeper zkh + char *path + PREINIT: + zk_t *zk; + zk_stat_t *stat = NULL; + zk_watch_t *old_watch = NULL; + zk_handle_t *watch_handle = NULL; + watcher_fn watcher = NULL; + zk_watch_t *new_watch = NULL; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 2 && items % 2) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + for (i = 2; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "stat")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), STAT_PACKAGE_NAME)) { + Perl_croak(aTHX_ "stat is not a hash reference of " + "type " STAT_PACKAGE_NAME); + } + + stat = _zks_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1))); + + if (!stat) { + Perl_croak(aTHX_ "invalid stat handle"); + } + } + else if (strcaseEQ(key, "watch")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), WATCH_PACKAGE_NAME)) { + Perl_croak(aTHX_ "watch is not a hash reference of " + "type " WATCH_PACKAGE_NAME); + } + + old_watch = _zkw_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1)), + &watch_handle); + + if (!old_watch) { + Perl_croak(aTHX_ "invalid watch handle"); + } + } + } + + if (watch_handle) { + new_watch = _zk_acquire_watch(aTHX); + + if (!new_watch) { + /* errno will be set */ + zk->last_ret = ZSYSTEMERROR; + zk->last_errno = errno; + + XSRETURN_NO; + } + + watcher = _zk_watcher; + } + + errno = 0; + ret = zoo_wexists(zk->handle, path, watcher, new_watch, stat); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (watch_handle) { + _zk_replace_watch(aTHX_ watch_handle, zk->first_watch, + old_watch, new_watch); + } + + if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zk_get_children(zkh, path, ...) + Net::ZooKeeper zkh + char *path + PREINIT: + zk_t *zk; + zk_watch_t *old_watch = NULL; + zk_handle_t *watch_handle = NULL; + watcher_fn watcher = NULL; + zk_watch_t *new_watch = NULL; + struct String_vector strings; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 2 && items % 2) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + for (i = 2; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "watch")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), WATCH_PACKAGE_NAME)) { + Perl_croak(aTHX_ "watch is not a hash reference of " + "type " WATCH_PACKAGE_NAME); + } + + old_watch = _zkw_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1)), + &watch_handle); + + if (!old_watch) { + Perl_croak(aTHX_ "invalid watch handle"); + } + } + } + + if (watch_handle) { + new_watch = _zk_acquire_watch(aTHX); + + if (!new_watch) { + /* errno will be set */ + zk->last_ret = ZSYSTEMERROR; + zk->last_errno = errno; + + if (GIMME_V == G_ARRAY) { + XSRETURN_EMPTY; + } + else { + XSRETURN_UNDEF; + } + } + + watcher = _zk_watcher; + } + + Zero(&strings, 1, struct String_vector); + + errno = 0; + ret = zoo_wget_children(zk->handle, path, watcher, new_watch, + &strings); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (watch_handle) { + _zk_replace_watch(aTHX_ watch_handle, zk->first_watch, + old_watch, new_watch); + } + + if (ret == ZOK) { + int num_children; + + num_children = + (strings.count > PERL_INT_MAX) ? PERL_INT_MAX : strings.count; + + if (GIMME_V == G_ARRAY && num_children > 0) { + EXTEND(SP, num_children); + + for (i = 0; i < num_children; ++i) { + ST(i) = sv_2mortal(newSVpv(strings.data[i], 0)); + } + } + + /* NOTE: would be nice if this were documented as required */ + deallocate_String_vector(&strings); + + if (GIMME_V == G_ARRAY) { + if (num_children == 0) { + XSRETURN_EMPTY; + } + + XSRETURN(num_children); + } + else { + ST(0) = sv_2mortal(newSViv(num_children)); + + XSRETURN(1); + } + } + else { + if (GIMME_V == G_ARRAY) { + XSRETURN_EMPTY; + } + else { + XSRETURN_UNDEF; + } + } + + +void +zk_get(zkh, path, ...) + Net::ZooKeeper zkh + char *path + PREINIT: + zk_t *zk; + int buf_len; + zk_stat_t *stat = NULL; + zk_watch_t *old_watch = NULL; + zk_handle_t *watch_handle = NULL; + char *buf; + watcher_fn watcher = NULL; + zk_watch_t *new_watch = NULL; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 2 && items % 2) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + buf_len = zk->data_buf_len; + + for (i = 2; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "data_read_len")) { + buf_len = SvIV(ST(i + 1)); + + if (buf_len < 0) { + Perl_croak(aTHX_ "invalid data read length: %d", + buf_len); + } + } + else if (strcaseEQ(key, "stat")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), STAT_PACKAGE_NAME)) { + Perl_croak(aTHX_ "stat is not a hash reference of " + "type " STAT_PACKAGE_NAME); + } + + stat = _zks_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1))); + + if (!stat) { + Perl_croak(aTHX_ "invalid stat handle"); + } + } + else if (strcaseEQ(key, "watch")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), WATCH_PACKAGE_NAME)) { + Perl_croak(aTHX_ "watch is not a hash reference of " + "type " WATCH_PACKAGE_NAME); + } + + old_watch = _zkw_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1)), + &watch_handle); + + if (!old_watch) { + Perl_croak(aTHX_ "invalid watch handle"); + } + } + } + + if (watch_handle) { + new_watch = _zk_acquire_watch(aTHX); + + if (!new_watch) { + /* errno will be set */ + zk->last_ret = ZSYSTEMERROR; + zk->last_errno = errno; + + XSRETURN_UNDEF; + } + + watcher = _zk_watcher; + } + + Newx(buf, buf_len + 1, char); + + errno = 0; + ret = zoo_wget(zk->handle, path, watcher, new_watch, + buf, &buf_len, stat); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (watch_handle) { + _zk_replace_watch(aTHX_ watch_handle, zk->first_watch, + old_watch, new_watch); + } + + if (ret == ZOK && buf_len != -1) { + ST(0) = sv_newmortal(); +#ifdef SV_HAS_TRAILING_NUL + buf[buf_len] = '\0'; + sv_usepvn_flags(ST(0), buf, buf_len, SV_HAS_TRAILING_NUL); +#else + sv_usepvn(ST(0), buf, buf_len); +#endif + + XSRETURN(1); + } + else { + Safefree(buf); + + XSRETURN_UNDEF; + } + + +void +zk_set(zkh, path, buf, ...) + Net::ZooKeeper zkh + char *path + char *buf; buf = (char *) SvPV($arg, buf_len); + PREINIT: + zk_t *zk; + int version = -1; + zk_stat_t *stat = NULL; + STRLEN buf_len; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 3 && !(items % 2)) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + if (buf_len > PERL_INT_MAX) { + Perl_croak(aTHX_ "invalid data length: %u", buf_len); + } + + for (i = 3; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "version")) { + version = SvIV(ST(i + 1)); + + if (version < 0) { + Perl_croak(aTHX_ "invalid version requirement: %d", + version); + } + } + else if (strcaseEQ(key, "stat")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), STAT_PACKAGE_NAME)) { + Perl_croak(aTHX_ "stat is not a hash reference of " + "type " STAT_PACKAGE_NAME); + } + + stat = _zks_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1))); + + if (!stat) { + Perl_croak(aTHX_ "invalid stat handle"); + } + } + } + + errno = 0; + ret = zoo_set2(zk->handle, path, buf, buf_len, version, stat); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zk_get_acl(zkh, path, ...) + Net::ZooKeeper zkh + char *path + PREINIT: + zk_t *zk; + zk_stat_t *stat = NULL; + struct ACL_vector acl; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 2 && items % 2) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + for (i = 2; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "stat")) { + if (!SvROK(ST(i + 1)) || SvTYPE(SvRV(ST(i + 1))) != SVt_PVHV || + !sv_derived_from(ST(i + 1), STAT_PACKAGE_NAME)) { + Perl_croak(aTHX_ "stat is not a hash reference of " + "type " STAT_PACKAGE_NAME); + } + + stat = _zks_get_handle_outer(aTHX_ (HV*) SvRV(ST(i + 1))); + + if (!stat) { + Perl_croak(aTHX_ "invalid stat handle"); + } + } + } + + errno = 0; + ret = zoo_get_acl(zk->handle, path, &acl, stat); + + zk->last_ret = ret; + zk->last_errno = errno; + + if (ret == ZOK) { + int num_acl_entries; + + num_acl_entries = + (acl.count > PERL_INT_MAX) ? PERL_INT_MAX : acl.count; + + if (GIMME_V == G_ARRAY && num_acl_entries > 0) { + EXTEND(SP, num_acl_entries); + + for (i = 0; i < num_acl_entries; ++i) { + HV *acl_entry_hash = newHV(); + + _zk_fill_acl_entry_hash(aTHX_ &acl.data[i], + acl_entry_hash); + + ST(i) = sv_2mortal(newRV_noinc((SV*) acl_entry_hash)); + } + } + + /* NOTE: would be nice if this were documented as required */ + deallocate_ACL_vector(&acl); + + if (GIMME_V == G_ARRAY) { + if (num_acl_entries == 0) { + XSRETURN_EMPTY; + } + + XSRETURN(num_acl_entries); + } + else { + ST(0) = sv_2mortal(newSViv(num_acl_entries)); + + XSRETURN(1); + } + } + else { + if (GIMME_V == G_ARRAY) { + XSRETURN_EMPTY; + } + else { + XSRETURN_UNDEF; + } + } + + +void +zk_set_acl(zkh, path, acl_arr, ...) + Net::ZooKeeper zkh + char *path + AV *acl_arr + PREINIT: + zk_t *zk; + const char *err; + int version = -1; + struct ACL_vector acl; + int i, ret; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 3 && !(items % 2)) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + err = _zk_fill_acl(aTHX_ acl_arr, &acl); + + if (err) { + Perl_croak(aTHX_ err); + } + + for (i = 3; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "version")) { + version = SvIV(ST(i + 1)); + + if (version < 0) { + Perl_croak(aTHX_ "invalid version requirement: %d", + version); + } + } + } + + errno = 0; + ret = zoo_set_acl(zk->handle, path, version, &acl); + + zk->last_ret = ret; + zk->last_errno = errno; + + _zk_free_acl(aTHX_ &acl); + + if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zk_stat(zkh) + Net::ZooKeeper zkh + PREINIT: + zk_t *zk; + zk_handle_t *handle; + HV *stash, *stat_hash, *attr_hash; + SV *attr; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + Newx(handle, 1, zk_handle_t); + + handle->signature = STAT_PACKAGE_SIGNATURE; + + Newxz(handle->handle.stat, 1, zk_stat_t); + + /* As in zk_new(), we use two levels of magic here. */ + + stash = gv_stashpv(STAT_PACKAGE_NAME, GV_ADDWARN); + + attr_hash = newHV(); + + sv_magic((SV*) attr_hash, Nullsv, PERL_MAGIC_ext, + (const char*) handle, 0); + + attr = sv_bless(newRV_noinc((SV*) attr_hash), stash); + + stat_hash = newHV(); + + sv_magic((SV*) stat_hash, attr, PERL_MAGIC_tied, Nullch, 0); + SvREFCNT_dec(attr); + + ST(0) = sv_bless(sv_2mortal(newRV_noinc((SV*) stat_hash)), stash); + + XSRETURN(1); + + +void +zk_watch(zkh, ...) + Net::ZooKeeper zkh + PREINIT: + zk_t *zk; + unsigned int timeout; + zk_watch_t *watch; + zk_handle_t *handle; + HV *stash, *watch_hash, *attr_hash; + SV *attr; + int i; + PPCODE: + zk = _zk_get_handle_outer(aTHX_ zkh); + + if (!zk) { + Perl_croak(aTHX_ "invalid handle"); + } + + zk->last_ret = ZOK; + zk->last_errno = 0; + + if (items > 1 && !(items % 2)) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + timeout = zk->watch_timeout; + + for (i = 1; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "timeout")) { + timeout = SvUV(ST(i + 1)); + } + } + + watch = _zk_acquire_watch(aTHX); + + if (!watch) { + /* errno will be set */ + zk->last_ret = ZSYSTEMERROR; + zk->last_errno = errno; + + XSRETURN_UNDEF; + } + + Newx(handle, 1, zk_handle_t); + + handle->signature = WATCH_PACKAGE_SIGNATURE; + handle->handle.watch = watch; + + /* As in zk_new(), we use two levels of magic here. */ + + stash = gv_stashpv(WATCH_PACKAGE_NAME, GV_ADDWARN); + + attr_hash = newHV(); + + watch->timeout = timeout; + + sv_magic((SV*) attr_hash, Nullsv, PERL_MAGIC_ext, + (const char*) handle, 0); + + attr = sv_bless(newRV_noinc((SV*) attr_hash), stash); + + watch_hash = newHV(); + + sv_magic((SV*) watch_hash, attr, PERL_MAGIC_tied, Nullch, 0); + SvREFCNT_dec(attr); + + ST(0) = sv_bless(sv_2mortal(newRV_noinc((SV*) watch_hash)), stash); + + XSRETURN(1); + + +MODULE = Net::ZooKeeper PACKAGE = Net::ZooKeeper::Stat PREFIX = zks_ + +void +zks_DESTROY(zksh) + Net::ZooKeeper::Stat zksh + PREINIT: + zk_handle_t *handle; + HV *attr_hash; + int ret = ZBADARGUMENTS; + PPCODE: + handle = _zk_check_handle_outer(aTHX_ zksh, &attr_hash, + STAT_PACKAGE_NAME, + STAT_PACKAGE_SIGNATURE); + + if (!handle) { + handle = _zk_check_handle_inner(aTHX_ zksh, + STAT_PACKAGE_SIGNATURE); + + if (handle) { + attr_hash = zksh; + zksh = NULL; + } + } + + if (handle) { + ret = ZOK; + + Safefree(handle->handle.stat); + Safefree(handle); + + sv_unmagic((SV*) attr_hash, PERL_MAGIC_ext); + } + + if (zksh && attr_hash) { + sv_unmagic((SV*) zksh, PERL_MAGIC_tied); + } + + if (GIMME_V == G_VOID) { + XSRETURN_EMPTY; + } + else if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zks_CLONE(package) + char *package + PPCODE: + XSRETURN_EMPTY; + + +void +zks_CLONE_SKIP(package) + char *package + PPCODE: + XSRETURN_YES; + + +void +zks_TIEHASH(package, ...) + char *package + PPCODE: + Perl_croak(aTHX_ "tying hashes of class " + STAT_PACKAGE_NAME " not supported"); + + +void +zks_UNTIE(attr_hash, ref_count) + Net::ZooKeeper::Stat attr_hash + IV ref_count + PPCODE: + Perl_croak(aTHX_ "untying hashes of class " + STAT_PACKAGE_NAME " not supported"); + + +void +zks_FIRSTKEY(attr_hash) + Net::ZooKeeper::Stat attr_hash + PREINIT: + zk_stat_t *stat; + PPCODE: + stat = _zks_get_handle_inner(aTHX_ attr_hash); + + if (!stat) { + Perl_croak(aTHX_ "invalid handle"); + } + + ST(0) = sv_2mortal(newSVpvn(zk_stat_keys[0].name, + zk_stat_keys[0].name_len)); + + XSRETURN(1); + + +void +zks_NEXTKEY(attr_hash, attr_key) + Net::ZooKeeper::Stat attr_hash + SV *attr_key + PREINIT: + zk_stat_t *stat; + char *key; + int i; + PPCODE: + stat = _zks_get_handle_inner(aTHX_ attr_hash); + + if (!stat) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_STAT_KEYS; ++i) { + if (strcaseEQ(key, zk_stat_keys[i].name)) { + ++i; + + break; + } + } + + if (i < NUM_STAT_KEYS) { + ST(0) = sv_2mortal(newSVpvn(zk_stat_keys[i].name, + zk_stat_keys[i].name_len)); + + XSRETURN(1); + } + else { + XSRETURN_EMPTY; + } + + +void +zks_SCALAR(attr_hash) + Net::ZooKeeper::Stat attr_hash + PPCODE: + XSRETURN_YES; + + +void +zks_FETCH(attr_hash, attr_key) + Net::ZooKeeper::Stat attr_hash + SV *attr_key + PREINIT: + zk_stat_t *stat; + char *key; + SV *val = NULL; + int i; + PPCODE: + stat = _zks_get_handle_inner(aTHX_ attr_hash); + + if (!stat) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_STAT_KEYS; ++i) { + if (strcaseEQ(key, zk_stat_keys[i].name)) { + if (zk_stat_keys[i].size * CHAR_BIT == 32) { + val = newSViv(*((int32_t*) (((char*) stat) + + zk_stat_keys[i].offset))); + } + else { + /* NOTE: %lld is inconsistent, so cast to a double */ + val = newSVpvf("%.0f", (double) + *((int64_t*) (((char*) stat) + + zk_stat_keys[i].offset))); + } + + break; + } + } + + if (val) { + ST(0) = sv_2mortal(val); + + XSRETURN(1); + } + + Perl_warn(aTHX_ "invalid element: %s", key); + + XSRETURN_UNDEF; + + +void +zks_STORE(attr_hash, attr_key, attr_val) + Net::ZooKeeper::Stat attr_hash + SV *attr_key + SV *attr_val + PREINIT: + zk_stat_t *stat; + char *key; + int i; + PPCODE: + stat = _zks_get_handle_inner(aTHX_ attr_hash); + + if (!stat) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_STAT_KEYS; ++i) { + if (strcaseEQ(key, zk_stat_keys[i].name)) { + Perl_warn(aTHX_ "read-only element: %s", key); + + XSRETURN_EMPTY; + } + } + + Perl_warn(aTHX_ "invalid element: %s", key); + + XSRETURN_EMPTY; + + +void +zks_EXISTS(attr_hash, attr_key) + Net::ZooKeeper::Stat attr_hash + SV *attr_key + PREINIT: + zk_stat_t *stat; + char *key; + int i; + PPCODE: + stat = _zks_get_handle_inner(aTHX_ attr_hash); + + if (!stat) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_STAT_KEYS; ++i) { + if (strcaseEQ(key, zk_stat_keys[i].name)) { + XSRETURN_YES; + } + } + + XSRETURN_NO; + + +void +zks_DELETE(attr_hash, attr_key) + Net::ZooKeeper::Stat attr_hash + SV *attr_key + PPCODE: + Perl_warn(aTHX_ "deleting elements from hashes of class " + STAT_PACKAGE_NAME " not supported"); + + XSRETURN_EMPTY; + + +void +zks_CLEAR(attr_hash) + Net::ZooKeeper::Stat attr_hash + PPCODE: + Perl_warn(aTHX_ "clearing hashes of class " + STAT_PACKAGE_NAME " not supported"); + + XSRETURN_EMPTY; + + +MODULE = Net::ZooKeeper PACKAGE = Net::ZooKeeper::Watch PREFIX = zkw_ + +void +zkw_DESTROY(zkwh) + Net::ZooKeeper::Watch zkwh + PREINIT: + zk_handle_t *handle; + HV *attr_hash; + int ret = ZBADARGUMENTS; + PPCODE: + handle = _zk_check_handle_outer(aTHX_ zkwh, &attr_hash, + WATCH_PACKAGE_NAME, + WATCH_PACKAGE_SIGNATURE); + + if (!handle) { + handle = _zk_check_handle_inner(aTHX_ zkwh, + WATCH_PACKAGE_SIGNATURE); + + if (handle) { + attr_hash = zkwh; + zkwh = NULL; + } + } + + if (handle) { + ret = ZOK; + + _zk_release_watch(aTHX_ handle->handle.watch, 0); + Safefree(handle); + + sv_unmagic((SV*) attr_hash, PERL_MAGIC_ext); + } + + if (zkwh && attr_hash) { + sv_unmagic((SV*) zkwh, PERL_MAGIC_tied); + } + + if (GIMME_V == G_VOID) { + XSRETURN_EMPTY; + } + else if (ret == ZOK) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + + +void +zkw_CLONE(package) + char *package + PPCODE: + XSRETURN_EMPTY; + + +void +zkw_CLONE_SKIP(package) + char *package + PPCODE: + XSRETURN_YES; + + +void +zkw_TIEHASH(package, ...) + char *package + PPCODE: + Perl_croak(aTHX_ "tying hashes of class " + WATCH_PACKAGE_NAME " not supported"); + + +void +zkw_UNTIE(attr_hash, ref_count) + Net::ZooKeeper::Watch attr_hash + IV ref_count + PPCODE: + Perl_croak(aTHX_ "untying hashes of class " + WATCH_PACKAGE_NAME " not supported"); + + +void +zkw_FIRSTKEY(attr_hash) + Net::ZooKeeper::Watch attr_hash + PREINIT: + zk_watch_t *watch; + PPCODE: + watch = _zkw_get_handle_inner(aTHX_ attr_hash); + + if (!watch) { + Perl_croak(aTHX_ "invalid handle"); + } + + ST(0) = sv_2mortal(newSVpvn(zk_watch_keys[0].name, + zk_watch_keys[0].name_len)); + + XSRETURN(1); + + +void +zkw_NEXTKEY(attr_hash, attr_key) + Net::ZooKeeper::Watch attr_hash + SV *attr_key + PREINIT: + zk_watch_t *watch; + char *key; + int i; + PPCODE: + watch = _zkw_get_handle_inner(aTHX_ attr_hash); + + if (!watch) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_WATCH_KEYS; ++i) { + if (strcaseEQ(key, zk_watch_keys[i].name)) { + ++i; + + break; + } + } + + if (i < NUM_WATCH_KEYS) { + ST(0) = sv_2mortal(newSVpvn(zk_watch_keys[i].name, + zk_watch_keys[i].name_len)); + + XSRETURN(1); + } + else { + XSRETURN_EMPTY; + } + + +void +zkw_SCALAR(attr_hash) + Net::ZooKeeper::Watch attr_hash + PPCODE: + XSRETURN_YES; + + +void +zkw_FETCH(attr_hash, attr_key) + Net::ZooKeeper::Watch attr_hash + SV *attr_key + PREINIT: + zk_watch_t *watch; + char *key; + SV *val = NULL; + PPCODE: + watch = _zkw_get_handle_inner(aTHX_ attr_hash); + + if (!watch) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + if (strcaseEQ(key, "timeout")) { + val = newSVuv(watch->timeout); + } + else if (strcaseEQ(key, "event")) { + val = newSViv(watch->event_type); + } + else if (strcaseEQ(key, "state")) { + val = newSViv(watch->event_state); + } + + if (val) { + ST(0) = sv_2mortal(val); + + XSRETURN(1); + } + + Perl_warn(aTHX_ "invalid element: %s", key); + + XSRETURN_UNDEF; + + +void +zkw_STORE(attr_hash, attr_key, attr_val) + Net::ZooKeeper::Watch attr_hash + SV *attr_key + SV *attr_val + PREINIT: + zk_watch_t *watch; + char *key; + PPCODE: + watch = _zkw_get_handle_inner(aTHX_ attr_hash); + + if (!watch) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + if (strcaseEQ(key, "timeout")) { + watch->timeout = SvUV(attr_val); + } + else { + int i; + + for (i = 0; i < NUM_WATCH_KEYS; ++i) { + if (strcaseEQ(key, zk_watch_keys[i].name)) { + Perl_warn(aTHX_ "read-only element: %s", key); + + XSRETURN_EMPTY; + } + } + + Perl_warn(aTHX_ "invalid element: %s", key); + } + + XSRETURN_EMPTY; + + +void +zkw_EXISTS(attr_hash, attr_key) + Net::ZooKeeper::Watch attr_hash + SV *attr_key + PREINIT: + zk_watch_t *watch; + char *key; + int i; + PPCODE: + watch = _zkw_get_handle_inner(aTHX_ attr_hash); + + if (!watch) { + Perl_croak(aTHX_ "invalid handle"); + } + + key = SvPV_nolen(attr_key); + + for (i = 0; i < NUM_WATCH_KEYS; ++i) { + if (strcaseEQ(key, zk_watch_keys[i].name)) { + XSRETURN_YES; + } + } + + XSRETURN_NO; + + +void +zkw_DELETE(attr_hash, attr_key) + Net::ZooKeeper::Watch attr_hash + SV *attr_key + PPCODE: + Perl_warn(aTHX_ "deleting elements from hashes of class " + WATCH_PACKAGE_NAME " not supported"); + + XSRETURN_EMPTY; + + +void +zkw_CLEAR(attr_hash) + Net::ZooKeeper::Watch attr_hash + PPCODE: + Perl_warn(aTHX_ "clearing hashes of class " + WATCH_PACKAGE_NAME " not supported"); + + XSRETURN_EMPTY; + + +void +zkw_wait(zkwh, ...) + Net::ZooKeeper::Watch zkwh + PREINIT: + zk_watch_t *watch; + unsigned int timeout; + struct timeval end_timeval; + int i, done; + struct timespec wait_timespec; + PPCODE: + watch = _zkw_get_handle_outer(aTHX_ zkwh, NULL); + + if (!watch) { + Perl_croak(aTHX_ "invalid handle"); + } + + if (items > 1 && !(items % 2)) { + Perl_croak(aTHX_ "invalid number of arguments"); + } + + timeout = watch->timeout; + + for (i = 1; i < items; i += 2) { + char *key = SvPV_nolen(ST(i)); + + if (strcaseEQ(key, "timeout")) { + timeout = SvUV(ST(i + 1)); + } + } + + gettimeofday(&end_timeval, NULL); + + end_timeval.tv_sec += timeout / 1000; + end_timeval.tv_usec += (timeout % 1000) * 1000; + + wait_timespec.tv_sec = end_timeval.tv_sec; + wait_timespec.tv_nsec = end_timeval.tv_usec * 1000; + + pthread_mutex_lock(&watch->mutex); + + while (!watch->done) { + struct timeval curr_timeval; + + gettimeofday(&curr_timeval, NULL); + + if (end_timeval.tv_sec < curr_timeval.tv_sec || + (end_timeval.tv_sec == curr_timeval.tv_sec && + end_timeval.tv_usec <= curr_timeval.tv_usec)) { + break; + } + + pthread_cond_timedwait(&watch->cond, &watch->mutex, + &wait_timespec); + } + + done = watch->done; + + pthread_mutex_unlock(&watch->mutex); + + if (done) { + XSRETURN_YES; + } + else { + XSRETURN_NO; + } + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/build.xml b/zookeeper-contrib/zookeeper-contrib-zkperl/build.xml new file mode 100644 index 0000000..84fa4c2 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/build/check_zk_version.c b/zookeeper-contrib/zookeeper-contrib-zkperl/build/check_zk_version.c new file mode 100644 index 0000000..e639cc1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/build/check_zk_version.c @@ -0,0 +1,29 @@ +/* Net::ZooKeeper - Perl extension for Apache ZooKeeper + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include + +#include "check_zk_version.h" + +int main() { + puts(ZOO_VERSION); + return 0; +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/build/check_zk_version.h b/zookeeper-contrib/zookeeper-contrib-zkperl/build/check_zk_version.h new file mode 100644 index 0000000..2078db2 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/build/check_zk_version.h @@ -0,0 +1,30 @@ +/* Net::ZooKeeper - Perl extension for Apache ZooKeeper + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* keep in sync with Makefile.PL */ +#if defined(ZOO_VERSION) +/* ZOO_VERSION was introduced by ZOOKEEPER-3635 (3.6.0-pre). */ +#else /* !defined(ZOO_VERSION) */ +#if !defined(ZOO_MAJOR_VERSION) || ZOO_MAJOR_VERSION != 3 || \ + !defined(ZOO_MINOR_VERSION) || ZOO_MINOR_VERSION < 1 || \ + !defined(ZOO_PATCH_VERSION) || \ + (ZOO_MINOR_VERSION == 1 && ZOO_PATCH_VERSION < 1) +#error "Net::ZooKeeper requires at least ZooKeeper version 3.1.1" +#endif +#endif /* !defined(ZOO_VERSION) */ diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/10_invalid.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/10_invalid.t new file mode 100644 index 0000000..5e080b6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/10_invalid.t @@ -0,0 +1,773 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 107; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(1); + + +## new() + +eval { + Net::ZooKeeper->new(); +}; +like($@, qr/Usage: Net::ZooKeeper::new\(package, hosts, \.\.\.\)/, + 'new(): no hostname specified'); + +eval { + Net::ZooKeeper->new($hosts, 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'new(): invalid number of arguments'); + +eval { + Net::ZooKeeper->new($hosts, 'session_timeout' => -3); +}; +like($@, qr/invalid session timeout/, + 'new(): invalid session timeout'); + +eval { + Net::ZooKeeper->new($hosts, 'session_timeout' => 0x4000_0000); +}; +like($@, qr/invalid session timeout/, + 'new(): invalid session timeout'); + +eval { + Net::ZooKeeper->new($hosts, 'session_id' => 'abcdef'); +}; +like($@, qr/invalid session ID/, + 'new(): invalid session ID'); + +my $zkh = Net::ZooKeeper->new($hosts); +isa_ok($zkh, 'Net::ZooKeeper', + 'new(): created handle'); + + +## DESTROY() + +eval { + $zkh->DESTROY('foo'); +}; +like($@, qr/Usage: Net::ZooKeeper::DESTROY\(zkh\)/, + 'DESTROY(): too many arguments'); + +my $bad_zkh = {}; +$bad_zkh = bless($bad_zkh, 'Net::ZooKeeper'); + +my $ret = $bad_zkh->DESTROY(); +ok(!$ret, + 'DESTROY(): no action on invalid handle'); + + +## add_auth() + +eval { + $zkh->add_auth(); +}; +like($@, qr/Usage: Net::ZooKeeper::add_auth\(zkh, scheme, cert\)/, + 'add_auth(): no scheme specified'); + +eval { + $zkh->add_auth('foo'); +}; +like($@, qr/Usage: Net::ZooKeeper::add_auth\(zkh, scheme, cert\)/, + 'add_auth(): no certificate specified'); + +eval { + $zkh->add_auth('foo', 'foo', 'bar'); +}; +like($@, qr/Usage: Net::ZooKeeper::add_auth\(zkh, scheme, cert\)/, + 'add_auth(): too many arguments'); + +eval { + $bad_zkh->add_auth('foo', 'foo'); +}; +like($@, qr/invalid handle/, + 'add_auth(): invalid handle'); + +eval { + Net::ZooKeeper::add_auth(1, 'foo', 'foo'); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'add_auth(): invalid hash reference'); + + +## create() + +eval { + $zkh->create(); +}; +like($@, qr/Usage: Net::ZooKeeper::create\(zkh, path, buf, \.\.\.\)/, + 'create(): no path specified'); + +eval { + $zkh->create($node_path); +}; +like($@, qr/Usage: Net::ZooKeeper::create\(zkh, path, buf, \.\.\.\)/, + 'create(): no data buffer specified'); + +eval { + $zkh->create($node_path, 'foo', 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'create(): invalid number of arguments'); + +eval { + $zkh->create($node_path, 'foo', 'path_read_len' => -3); +}; +like($@, qr/invalid path read length/, + 'create(): invalid path read length'); + +eval { + $zkh->create($node_path, 'foo', 'path_read_len' => 1); +}; +like($@, qr/invalid path read length/, + 'create(): invalid path read length'); + +eval { + $zkh->create($node_path, 'foo', 'flags' => 15); +}; +like($@, qr/invalid create flags/, + 'create(): invalid create flags'); + +eval { + $zkh->create($node_path, 'foo', 'flags' => ZOO_EPHEMERAL, 'acl', 'foo'); +}; +like($@, qr/invalid ACL array reference/, + 'create(): invalid ACL array reference'); + +eval { + $zkh->create($node_path, 'foo', 'acl', {}); +}; +like($@, qr/invalid ACL array reference/, + 'create(): invalid ACL array reference to hash'); + +eval { + my @acl = ('foo', 'bar'); + $zkh->create($node_path, 'foo', 'acl', \@acl); +}; +like($@, qr/invalid ACL entry hash reference/, + 'create(): invalid ACL entry hash reference'); + +eval { + my @acl = ({ 'foo' => 'bar' }); + $zkh->create($node_path, 'foo', 'acl', \@acl); +}; +like($@, qr/no ACL entry perms element/, + 'create(): no ACL entry perms element'); + +eval { + my @acl = ( + { + 'perms' => -1 + } + ); + $zkh->create($node_path, 'foo', 'acl', \@acl); +}; +like($@, qr/invalid ACL entry perms/, + 'create(): invalid ACL entry perms'); + +eval { + my @acl = ( + { + 'perms' => ZOO_PERM_ALL + } + ); + $zkh->create($node_path, 'foo', 'acl', \@acl); +}; +like($@, qr/no ACL entry scheme element/, + 'create(): no ACL entry scheme element'); + +eval { + my @acl = ( + { + 'perms' => ZOO_PERM_ALL, + 'scheme' => 'foo' + } + ); + $zkh->create($node_path, 'foo', 'acl', \@acl); +}; +like($@, qr/no ACL entry id element/, + 'create(): no ACL entry id element'); + +eval { + my @acl = ( + { + 'perms' => ZOO_PERM_ALL, + 'scheme' => 'foo', + 'id' => 'bar' + }, + 'bar' + ); + $zkh->create($node_path, 'foo', 'acl', \@acl); +}; +like($@, qr/invalid ACL entry hash reference/, + 'create(): invalid second ACL entry hash reference'); + +eval { + $bad_zkh->create($node_path, 'foo'); +}; +like($@, qr/invalid handle/, + 'create(): invalid handle'); + +eval { + Net::ZooKeeper::create(1, $node_path, 'foo'); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'create(): invalid hash reference'); + + +## delete() + +eval { + $zkh->delete(); +}; +like($@, qr/Usage: Net::ZooKeeper::delete\(zkh, path, \.\.\.\)/, + 'delete(): no path specified'); + +eval { + $zkh->delete($node_path, 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'delete(): invalid number of arguments'); + +eval { + $zkh->delete($node_path, 'version' => -3); +}; +like($@, qr/invalid version requirement/, + 'delete(): invalid version requirement'); + +eval { + $bad_zkh->delete($node_path); +}; +like($@, qr/invalid handle/, + 'delete(): invalid handle'); + +eval { + Net::ZooKeeper::delete(1, $node_path); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'delete(): invalid hash reference'); + + +## exists() + +eval { + $zkh->exists(); +}; +like($@, qr/Usage: Net::ZooKeeper::exists\(zkh, path, \.\.\.\)/, + 'exists(): no path specified'); + +eval { + $zkh->exists($node_path, 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'exists(): invalid number of arguments'); + +eval { + $zkh->exists($node_path, 'watch', 'bar'); +}; +like($@, qr/watch is not a hash reference of type Net::ZooKeeper::Watch/, + 'exists(): invalid watch hash reference'); + +eval { + $zkh->exists($node_path, 'watch', []); +}; +like($@, qr/watch is not a hash reference of type Net::ZooKeeper::Watch/, + 'exists(): invalid watch hash reference to array'); + +eval { + $zkh->exists($node_path, 'stat', 'bar'); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'exists(): invalid stat hash reference'); + +eval { + $zkh->exists($node_path, 'stat', []); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'exists(): invalid stat hash reference'); + +eval { + $bad_zkh->exists($node_path); +}; +like($@, qr/invalid handle/, + 'exists(): invalid handle'); + +eval { + Net::ZooKeeper::exists(1, $node_path); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'exists(): invalid hash reference'); + + +## get_children() + +eval { + $zkh->get_children(); +}; +like($@, qr/Usage: Net::ZooKeeper::get_children\(zkh, path, \.\.\.\)/, + 'get_children(): no path specified'); + +eval { + $zkh->get_children($node_path, 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'get_children(): invalid number of arguments'); + +eval { + $zkh->get_children($node_path, 'watch', 'bar'); +}; +like($@, qr/watch is not a hash reference of type Net::ZooKeeper::Watch/, + 'get_children(): invalid watch hash reference'); + +eval { + $zkh->get_children($node_path, 'watch', []); +}; +like($@, qr/watch is not a hash reference of type Net::ZooKeeper::Watch/, + 'get_children(): invalid watch ash reference to array'); + +eval { + $bad_zkh->get_children($node_path); +}; +like($@, qr/invalid handle/, + 'get_children(): invalid handle'); + +eval { + Net::ZooKeeper::get_children(1, $node_path); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'get_children(): invalid hash reference'); + + +## get() + +eval { + $zkh->get(); +}; +like($@, qr/Usage: Net::ZooKeeper::get\(zkh, path, \.\.\.\)/, + 'get(): no path specified'); + +eval { + $zkh->get($node_path, 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'get(): invalid number of arguments'); + +eval { + $zkh->get($node_path, 'data_read_len' => -3); +}; +like($@, qr/invalid data read length/, + 'get(): invalid data read length'); + +eval { + $zkh->get($node_path, 'data_read_len' => 10, 'watch', 'bar'); +}; +like($@, qr/watch is not a hash reference of type Net::ZooKeeper::Watch/, + 'get(): invalid watch hash reference'); + +eval { + $zkh->get($node_path, 'watch', []); +}; +like($@, qr/watch is not a hash reference of type Net::ZooKeeper::Watch/, + 'get(): invalid watch hash reference to array'); + +eval { + $zkh->get($node_path, 'stat', 'bar'); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'get(): invalid stat hash reference'); + +eval { + $zkh->get($node_path, 'stat', []); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'get(): invalid stat hash reference'); + +eval { + $bad_zkh->get($node_path); +}; +like($@, qr/invalid handle/, + 'get(): invalid handle'); + +eval { + Net::ZooKeeper::get(1, $node_path); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'get(): invalid hash reference'); + + +## set() + +eval { + $zkh->set(); +}; +like($@, qr/Usage: Net::ZooKeeper::set\(zkh, path, buf, \.\.\.\)/, + 'set(): no path specified'); + +eval { + $zkh->set($node_path); +}; +like($@, qr/Usage: Net::ZooKeeper::set\(zkh, path, buf, \.\.\.\)/, + 'set(): no data buffer specified'); + +eval { + $zkh->set($node_path, 'foo', 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'set(): invalid number of arguments'); + +eval { + $zkh->set($node_path, 'foo', 'version' => -3); +}; +like($@, qr/invalid version requirement/, + 'set(): invalid version requirement'); + +eval { + $zkh->set($node_path, 'foo', 'version', 0, 'stat', 'bar'); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'set(): invalid stat hash reference'); + +eval { + $zkh->set($node_path, 'foo', 'stat', []); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'set(): invalid stat hash reference'); + +eval { + $bad_zkh->set($node_path, 'foo'); +}; +like($@, qr/invalid handle/, + 'set(): invalid handle'); + +eval { + Net::ZooKeeper::set(1, $node_path, 'foo'); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'set(): invalid hash reference'); + + +## get_acl() + +eval { + $zkh->get_acl(); +}; +like($@, qr/Usage: Net::ZooKeeper::get_acl\(zkh, path, \.\.\.\)/, + 'get_acl(): no path specified'); + +eval { + $zkh->get_acl($node_path, 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'get_acl(): invalid number of arguments'); + +eval { + $zkh->get_acl($node_path, 'stat', 'bar'); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'get_acl(): invalid stat hash reference'); + +eval { + $zkh->get_acl($node_path, 'stat', []); +}; +like($@, qr/stat is not a hash reference of type Net::ZooKeeper::Stat/, + 'get_acl(): invalid stat hash reference'); + +eval { + $bad_zkh->get_acl($node_path); +}; +like($@, qr/invalid handle/, + 'get_acl(): invalid handle'); + +eval { + Net::ZooKeeper::get_acl(1, $node_path); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'get_acl(): invalid hash reference'); + + +## set_acl() + +eval { + $zkh->set_acl(); +}; +like($@, qr/Usage: Net::ZooKeeper::set_acl\(zkh, path, acl_arr, \.\.\.\)/, + 'set_acl(): no path specified'); + +eval { + $zkh->set_acl($node_path); +}; +like($@, qr/Usage: Net::ZooKeeper::set_acl\(zkh, path, acl_arr, \.\.\.\)/, + 'set_acl(): no data buffer specified'); + +eval { + $zkh->set_acl($node_path, 'foo'); +}; +like($@, qr/acl_arr is not an array reference/, + 'set_acl(): invalid ACL array reference'); + +eval { + $zkh->set_acl($node_path, {}); +}; +like($@, qr/acl_arr is not an array reference/, + 'set_acl(): invalid ACL array reference to hash'); + +eval { + my @acl = ('foo', 'bar'); + $zkh->set_acl($node_path, \@acl); +}; +like($@, qr/invalid ACL entry hash reference/, + 'set_acl(): invalid ACL entry hash reference'); + +eval { + my @acl = ({ 'foo' => 'bar' }); + $zkh->set_acl($node_path, \@acl); +}; +like($@, qr/no ACL entry perms element/, + 'set_acl(): no ACL entry perms element'); + +eval { + my @acl = ( + { + 'perms' => -1 + } + ); + $zkh->set_acl($node_path, \@acl); +}; +like($@, qr/invalid ACL entry perms/, + 'set_acl(): invalid ACL entry perms'); + +eval { + my @acl = ( + { + 'perms' => ZOO_PERM_ALL + } + ); + $zkh->set_acl($node_path, \@acl); +}; +like($@, qr/no ACL entry scheme element/, + 'set_acl(): no ACL entry scheme element'); + +eval { + my @acl = ( + { + 'perms' => ZOO_PERM_ALL, + 'scheme' => 'foo' + } + ); + $zkh->set_acl($node_path, \@acl); +}; +like($@, qr/no ACL entry id element/, + 'set_acl(): no ACL entry id element'); + +eval { + my @acl = ( + { + 'perms' => ZOO_PERM_ALL, + 'scheme' => 'foo', + 'id' => 'bar' + }, + 'bar' + ); + $zkh->set_acl($node_path, \@acl); +}; +like($@, qr/invalid ACL entry hash reference/, + 'set_acl(): invalid second ACL entry hash reference'); + +eval { + $zkh->set_acl($node_path, [], 'bar'); +}; +like($@, qr/invalid number of arguments/, + 'set_acl(): invalid number of arguments'); + +eval { + $zkh->set_acl($node_path, [], 'version' => -3); +}; +like($@, qr/invalid version requirement/, + 'set_acl(): invalid version requirement'); + +eval { + $bad_zkh->set_acl($node_path, []); +}; +like($@, qr/invalid handle/, + 'set_acl(): invalid handle'); + +eval { + Net::ZooKeeper::set_acl(1, $node_path, []); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'set_acl(): invalid hash reference'); + + +## stat() + +eval { + $zkh->stat('bar'); +}; +like($@, qr/Usage: Net::ZooKeeper::stat\(zkh\)/, + 'stat(): too many arguments'); + +eval { + $bad_zkh->stat(); +}; +like($@, qr/invalid handle/, + 'stat(): invalid handle'); + +eval { + Net::ZooKeeper::stat(1); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'stat(): invalid hash reference'); + +my $stat = $zkh->stat(); +isa_ok($stat, 'Net::ZooKeeper::Stat', + 'stat(): created stat handle'); + + +## stat DESTROY() + +eval { + $stat->DESTROY('foo'); +}; +like($@, qr/Usage: Net::ZooKeeper::Stat::DESTROY\(zksh\)/, + 'stat DESTROY(): too many arguments'); + +my $bad_stat = {}; +$bad_stat = bless($bad_stat, 'Net::ZooKeeper::Stat'); + +$ret = $bad_stat->DESTROY(); +ok(!$ret, + 'stat DESTROY(): no action on invalid handle'); + + +## watch() + +eval { + $zkh->watch('bar'); +}; +like($@, qr/invalid number of arguments/, + 'watch(): invalid number of arguments'); + +eval { + $bad_zkh->watch(); +}; +like($@, qr/invalid handle/, + 'watch(): invalid handle'); + +eval { + Net::ZooKeeper::watch(1); +}; +like($@, qr/zkh is not a hash reference of type Net::ZooKeeper/, + 'watch(): invalid hash reference'); + +my $watch = $zkh->watch(); +isa_ok($watch, 'Net::ZooKeeper::Watch', + 'watch(): created watch handle'); + + +## watch DESTROY() + +eval { + $watch->DESTROY('foo'); +}; +like($@, qr/Usage: Net::ZooKeeper::Watch::DESTROY\(zkwh\)/, + 'watch DESTROY(): too many arguments'); + +my $bad_watch = {}; +$bad_watch = bless($bad_watch, 'Net::ZooKeeper::Watch'); + +$ret = $bad_watch->DESTROY(); +ok(!$ret, + 'watch DESTROY(): no action on invalid handle'); + + +## wait() + +eval { + $watch->wait('bar'); +}; +like($@, qr/invalid number of arguments/, + 'wait(): invalid number of arguments'); + +eval { + $bad_watch->wait(); +}; +like($@, qr/invalid handle/, + 'wait(): invalid watch handle'); + +eval { + Net::ZooKeeper::Watch::wait(1); +}; +like($@, qr/zkwh is not a hash reference of type Net::ZooKeeper::Watch/, + 'wait(): invalid watch hash reference'); + + +## set_log_level() + +eval { + my $f = \&Net::ZooKeeper::set_log_level; + &$f(); +}; +like($@, qr/Usage: Net::ZooKeeper::set_log_level\(level\)/, + 'set_log_level(): no level specified'); + +eval { + my $f = \&Net::ZooKeeper::set_log_level; + &$f(ZOO_LOG_LEVEL_OFF, 'foo'); +}; +like($@, qr/Usage: Net::ZooKeeper::set_log_level\(level\)/, + 'set_log_level(): too many arguments'); + +eval { + Net::ZooKeeper::set_log_level((ZOO_LOG_LEVEL_OFF) - 1); +}; +like($@, qr/invalid log level/, + 'set_log_level(): invalid low log level'); + +eval { + Net::ZooKeeper::set_log_level((ZOO_LOG_LEVEL_DEBUG) + 1); +}; +like($@, qr/invalid log level/, + 'set_log_level(): invalid high log level'); + + +## set_deterministic_conn_order() + +eval { + my $f = \&Net::ZooKeeper::set_deterministic_conn_order; + &$f(); +}; +like($@, qr/Usage: Net::ZooKeeper::set_deterministic_conn_order\(flag\)/, + 'set_deterministic_conn_order(): no flag specified'); + +eval { + my $f = \&Net::ZooKeeper::set_deterministic_conn_order; + &$f(1, 'foo'); +}; +like($@, qr/Usage: Net::ZooKeeper::set_deterministic_conn_order\(flag\)/, + 'set_deterministic_conn_order(): too many arguments'); + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/15_thread.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/15_thread.t new file mode 100644 index 0000000..1ef56d0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/15_thread.t @@ -0,0 +1,121 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use Config; +use File::Spec; +use Test::More; + +BEGIN { + if ($Config{'useithreads'}) { + plan tests => 10; + } + else { + plan skip_all => 'no thread support'; + } +} + +use threads; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +my $zkh = Net::ZooKeeper->new($hosts); + +SKIP: { + skip 'no valid handle', 9 unless (defined($zkh)); + + my($thread) = threads->new(\&thread_test, $zkh); + + SKIP: { + skip 'no valid thread', 3 unless (defined($thread)); + + my(@ret) = $thread->join; + + ok((@ret == 3 and $ret[0]), + 'CLONE_SKIP(): handle reference after spawning thread'); + + ok((@ret == 3 and $ret[1]), + 'CLONE_SKIP(): scalar handle reference after spawning thread'); + + ok((@ret == 3 and $ret[2]), + 'CLONE_SKIP(): undef handle reference after spawning thread'); + } + + my $stat = $zkh->stat(); + + ($thread) = threads->new(\&thread_test, $stat); + + SKIP: { + skip 'no valid thread', 3 unless (defined($thread)); + + my(@ret) = $thread->join; + + ok((@ret == 3 and $ret[0]), + 'stat CLONE_SKIP(): stat handle reference after spawning thread'); + + ok((@ret == 3 and $ret[1]), + 'stat CLONE_SKIP(): scalar stat handle reference after ' . + 'spawning thread'); + + ok((@ret == 3 and $ret[2]), + 'stat CLONE_SKIP(): undef stat handle reference after ' . + 'spawning thread'); + } + + my $watch = $zkh->watch(); + + ($thread) = threads->new(\&thread_test, $watch); + + SKIP: { + skip 'no valid thread', 3 unless (defined($thread)); + + my(@ret) = $thread->join; + + ok((@ret == 3 and $ret[0]), + 'watch CLONE_SKIP(): watch handle reference after spawning thread'); + + ok((@ret == 3 and $ret[1]), + 'watch CLONE_SKIP(): scalar watch handle reference after ' . + 'spawning thread'); + + ok((@ret == 3 and $ret[2]), + 'watch CLONE_SKIP(): undef watch handle reference after ' . + 'spawning thread'); + } +} + +sub thread_test +{ + my $zkh = shift; + + my @ret; + + $ret[0] = ref($zkh) ? 1 : 0; + $ret[1] = ($ret[0] and ref($zkh) eq 'SCALAR') ? 1 : 0; + $ret[2] = ($ret[1] and !defined(${$zkh})) ? 1 : 0; + + return @ret; +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/20_tie.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/20_tie.t new file mode 100644 index 0000000..37e9a4f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/20_tie.t @@ -0,0 +1,353 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 54; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + skip 'no valid handle', 4 unless (defined($zkh)); + + + ## DESTROY() + + my $attr = tied(%{$zkh}); + + my $ret = $attr->DESTROY(); + ok($ret, + 'DESTROY(): destroyed inner hash'); + + $ret = $attr->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on destroyed inner hash'); + + $ret = $zkh->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on handle with destroyed inner hash'); + + undef $zkh; + ok(!defined($zkh), + 'undef: released handle with destroyed inner hash'); +} + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + skip 'no valid handle', 49 unless (defined($zkh)); + + + ## TIEHASH(), UNTIE() + + eval { + tie(%{$zkh}, 'Net::ZooKeeper'); + }; + like($@, qr/tying hashes of class Net::ZooKeeper not supported/, + 'tie(): tying hashes not supported'); + + eval { + Net::ZooKeeper::TIEHASH('Net::ZooKeeper'); + }; + like($@, qr/tying hashes of class Net::ZooKeeper not supported/, + 'TIEHASH(): tying hashes not supported'); + + eval { + untie(%{$zkh}); + }; + like($@, qr/untying hashes of class Net::ZooKeeper not supported/, + 'untie(): untying hashes not supported'); + + my $attr = tied(%{$zkh}); + + eval { + $attr->UNTIE(0); + }; + like($@, qr/untying hashes of class Net::ZooKeeper not supported/, + 'UNTIE(): untying hashes not supported'); + + + ## FIRSTKEY(), NEXTKEY(), SCALAR() + + my $copy_zkh; + { + my %copy_zkh = %{$zkh}; + $copy_zkh = \%copy_zkh; + } + bless($copy_zkh, 'Net::ZooKeeper'); + is(ref($copy_zkh), 'Net::ZooKeeper', + 'FIRSTKEY(), NEXTKEY(): copied dereferenced handle'); + + eval { + my $val = $copy_zkh->FIRSTKEY(); + }; + like($@, qr/invalid handle/, + 'FETCHKEY(): invalid handle'); + + eval { + my $val = $copy_zkh->NEXTKEY('data_read_len'); + }; + like($@, qr/invalid handle/, + 'NEXTKEY(): invalid handle'); + + my @keys = keys(%{$zkh}); + is(scalar(@keys), 7, + 'keys(): count of keys from handle'); + + @keys = keys(%{$copy_zkh}); + is(scalar(@keys), 7, + 'keys(): count of keys from copied dereferenced handle'); + + is($attr->FIRSTKEY(), 'data_read_len', + 'FIRSTKEY(): retrieved first key using inner hash'); + + is($attr->NEXTKEY('session_id'), 'pending_watches', + 'NEXTKEY(): retrieved last key using inner hash'); + + is($attr->NEXTKEY('pending_watches'), undef, + 'NEXTKEY(): undef returned after last key using inner hash'); + + ok(scalar(%{$zkh}), + 'scalar(): true value returned for dereferenced handle'); + + ok($zkh->SCALAR(), + 'SCALAR(): true value returned'); + + + ## FETCH() + + eval { + my $val = $copy_zkh->FETCH('data_read_len'); + }; + like($@, qr/invalid handle/, + 'FETCH(): invalid handle'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + my $val = $zkh->{'foo'}; + ok(!defined($val), + 'FETCH(): undef returned for invalid element'); + + like($msg, qr/invalid element/, + 'FETCH(): invalid element'); + } + + is($zkh->{'data_read_len'}, 1023, + 'FETCH(): default data read length'); + + is($zkh->{'path_read_len'}, 1023, + 'FETCH(): default path read length'); + + is($zkh->{'hosts'}, $hosts, + 'FETCH(): server hosts'); + + is($zkh->{'session_timeout'}, 10000, + 'FETCH(): default session timeout'); + + ok(defined($zkh->{'session_id'}), + 'FETCH(): session ID'); + + SKIP: { + my $zkh = Net::ZooKeeper->new('0.0.0.0:0'); + + skip 'no valid handle with invalid host', 1 unless (defined($zkh)); + + is($zkh->{'session_id'}, '', + 'FETCH(): empty session ID with invalid host'); + } + + is($zkh->{'pending_watches'}, 0, + 'FETCH(): default pending watch list length'); + + is($attr->FETCH('data_read_len'), 1023, + 'FETCH(): default data read length using inner hash'); + + + ## STORE() + + eval { + my $val = $copy_zkh->STORE('data_read_len', 'foo'); + }; + like($@, qr/invalid handle/, + 'STORE(): invalid handle'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->{'foo'} = 'foo'; + like($msg, qr/invalid element/, + 'STORE(): invalid element'); + } + + eval { + $zkh->{'data_read_len'} = -3; + }; + like($@, qr/invalid data read length/, + 'STORE(): invalid data read length'); + + eval { + $zkh->{'path_read_len'} = -3; + }; + like($@, qr/invalid path read length/, + 'STORE(): invalid path read length'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->{'hosts'} = 'foo'; + like($msg, qr/read-only element: hosts/, + 'STORE(): read-only server hosts element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->{'session_timeout'} = 0; + like($msg, qr/read-only element: session_timeout/, + 'STORE(): read-only session timeout element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->{'session_id'} = 'foo'; + like($msg, qr/read-only element: session_id/, + 'STORE(): read-only session ID element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->{'pending_watches'} = 0; + like($msg, qr/read-only element: pending_watches/, + 'STORE(): read-only pending watch list length element'); + } + + $zkh->{'data_read_len'} = 200; + is($zkh->{'data_read_len'}, 200, + 'STORE(): updated data read length'); + + $zkh->{'path_read_len'} = 100; + is($zkh->{'path_read_len'}, 100, + 'STORE(): updated path read length'); + + $attr->STORE('data_read_len', 100); + is($zkh->{'data_read_len'}, 100, + 'STORE(): updated data read length using inner hash'); + + + ## EXISTS() + + eval { + my $val = $copy_zkh->EXISTS('data_read_len'); + }; + like($@, qr/invalid handle/, + 'EXISTS(): invalid handle'); + + ok(!exists($zkh->{'foo'}), + 'exists(): invalid element of handle'); + + ok(exists($zkh->{'data_read_len'}), + 'exists(): data read length'); + + ok(exists($zkh->{'path_read_len'}), + 'exists(): path read length'); + + ok(exists($zkh->{'hosts'}), + 'exists(): server hosts'); + + ok(exists($zkh->{'session_timeout'}), + 'exists(): session timeout'); + + ok(exists($zkh->{'session_id'}), + 'exists(): session ID'); + + ok(exists($zkh->{'pending_watches'}), + 'exists(): pending watch list length'); + + ok($attr->EXISTS('data_read_len'), + 'EXISTS(): data read length using inner hash'); + + + ## DELETE(), CLEAR() + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + delete($zkh->{'data_read_len'}); + like($msg, + qr/deleting elements from hashes of class Net::ZooKeeper not supported/, + 'delete(): deleting hash elements not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->DELETE({'data_read_len'}); + like($msg, + qr/deleting elements from hashes of class Net::ZooKeeper not supported/, + 'DELETE(): deleting hash elements not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + %{$zkh} = (); + like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/, + 'assign: clearing hashes not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $zkh->CLEAR(); + like($msg, qr/clearing hashes of class Net::ZooKeeper not supported/, + 'CLEAR(): clearing hashes not supported'); + } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/22_stat_tie.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/22_stat_tie.t new file mode 100644 index 0000000..02e7913 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/22_stat_tie.t @@ -0,0 +1,438 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 66; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + my $stat = $zkh->stat() if (defined($zkh)); + + skip 'no valid stat handle', 4 unless (defined($stat)); + + + ## DESTROY() + + my $attr = tied(%{$stat}); + + my $ret = $attr->DESTROY(); + ok($ret, + 'stat DESTROY(): destroyed inner stat hash'); + + $ret = $attr->DESTROY(); + ok(!$ret, + 'stat DESTROY(): no action on destroyed inner stat hash'); + + $ret = $stat->DESTROY(); + ok(!$ret, + 'stat DESTROY(): no action on stat handle with destroyed inner hash'); + + undef $stat; + ok(!defined($stat), + 'undef: released stat handle with destroyed inner hash'); +} + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + my $stat = $zkh->stat() if (defined($zkh)); + + skip 'no valid stat handle', 61 unless (defined($stat)); + + + ## TIEHASH(), UNTIE() + + eval { + tie(%{$stat}, 'Net::ZooKeeper::Stat'); + }; + like($@, qr/tying hashes of class Net::ZooKeeper::Stat not supported/, + 'tie(): tying stat hashes not supported'); + + eval { + Net::ZooKeeper::Stat::TIEHASH('Net::ZooKeeper::Stat'); + }; + like($@, qr/tying hashes of class Net::ZooKeeper::Stat not supported/, + 'stat TIEHASH(): tying stat hashes not supported'); + + eval { + untie(%{$stat}); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/, + 'untie(): untying stat hashes not supported'); + + my $attr = tied(%{$stat}); + + eval { + $attr->UNTIE(0); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/, + 'stat UNTIE(): untying stat hashes not supported'); + + + ## FIRSTKEY(), NEXTKEY(), SCALAR() + + my $copy_stat; + { + my %copy_stat = %{$stat}; + $copy_stat = \%copy_stat; + } + bless($copy_stat, 'Net::ZooKeeper::Stat'); + is(ref($copy_stat), 'Net::ZooKeeper::Stat', + 'stat FIRSTKEY(), NEXTKEY(): copied dereferenced stat handle'); + + eval { + my $val = $copy_stat->FIRSTKEY(); + }; + like($@, qr/invalid handle/, + 'stat FETCHKEY(): invalid stat handle'); + + eval { + my $val = $copy_stat->NEXTKEY('czxid'); + }; + like($@, qr/invalid handle/, + 'stat NEXTKEY(): invalid stat handle'); + + my @keys = keys(%{$stat}); + is(scalar(@keys), 11, + 'keys(): count of keys from stat handle'); + + @keys = keys(%{$copy_stat}); + is(scalar(@keys), 11, + 'keys(): count of keys from copied dereferenced stat handle'); + + is($attr->FIRSTKEY(), 'czxid', + 'stat FIRSTKEY(): retrieved first key using inner stat hash'); + + is($attr->NEXTKEY('num_children'), 'children_zxid', + 'stat NEXTKEY(): retrieved last key using inner stat hash'); + + is($attr->NEXTKEY('children_zxid'), undef, + 'NEXTKEY(): undef returned after last key using inner stat hash'); + + ok(scalar(%{$stat}), + 'scalar(): true value returned for dereferenced stat handle'); + + ok($stat->SCALAR(), + 'stat SCALAR(): true value returned'); + + + ## FETCH() + + eval { + my $val = $copy_stat->FETCH('version'); + }; + like($@, qr/invalid handle/, + 'stat FETCH(): invalid stat handle'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + my $val = $stat->{'foo'}; + ok(!defined($val), + 'stat FETCH(): undef returned for invalid element'); + + like($msg, qr/invalid element/, + 'stat FETCH(): invalid element'); + } + + is($stat->{'czxid'}, 0, + 'stat FETCH(): default node creation ZooKeeper transaction ID'); + + is($stat->{'mzxid'}, 0, + 'stat FETCH(): default data last-modified ZooKeeper transaction ID'); + + is($stat->{'ctime'}, 0, + 'stat FETCH(): default node creation time'); + + is($stat->{'mtime'}, 0, + 'stat FETCH(): default data last-modified time'); + + is($stat->{'version'}, 0, + 'stat FETCH(): default data version'); + + is($stat->{'children_version'}, 0, + 'stat FETCH(): default child node list version'); + + is($stat->{'acl_version'}, 0, + 'stat FETCH(): default ACL version'); + + is($stat->{'ephemeral_owner'}, 0, + 'stat FETCH(): ephemeral node owner session ID'); + + is($stat->{'data_len'}, 0, + 'stat FETCH(): default data length'); + + is($stat->{'num_children'}, 0, + 'stat FETCH(): default child node list length'); + + is($stat->{'children_zxid'}, 0, + 'stat FETCH(): default child node list last-modified ' . + 'ZooKeeper transaction ID'); + + is($attr->FETCH('version'), 0, + 'stat FETCH(): default data version using inner stat hash'); + + + ## STORE() + + eval { + my $val = $copy_stat->STORE('version', 'foo'); + }; + like($@, qr/invalid handle/, + 'stat STORE(): invalid stat handle'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'foo'} = 'foo'; + like($msg, qr/invalid element/, + 'stat STORE(): invalid element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'czxid'} = 'foo'; + like($msg, qr/read-only element: czxid/, + 'stat STORE(): read-only node creation ' . + 'ZooKeeper transaction ID element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'mzxid'} = 'foo'; + like($msg, qr/read-only element: mzxid/, + 'stat STORE(): read-only data last-modified ' . + 'ZooKeeper transaction ID element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'ctime'} = 'foo'; + like($msg, qr/read-only element: ctime/, + 'stat STORE(): read-only node creation time element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'mtime'} = 'foo'; + like($msg, qr/read-only element: mtime/, + 'stat STORE(): read-only data last-modified time element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'version'} = 'foo'; + like($msg, qr/read-only element: version/, + 'stat STORE(): read-only data version element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'children_version'} = 'foo'; + like($msg, qr/read-only element: children_version/, + 'stat STORE(): read-only child node list version element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'acl_version'} = 'foo'; + like($msg, qr/read-only element: acl_version/, + 'stat STORE(): read-only ACL version element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'ephemeral_owner'} = 'foo'; + like($msg, qr/read-only element: ephemeral_owner/, + 'stat STORE(): read-only ephemeral node owner ' . + 'session ID element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'data_len'} = 'foo'; + like($msg, qr/read-only element: data_len/, + 'stat STORE(): read-only data length element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'num_children'} = 'foo'; + like($msg, qr/read-only element: num_children/, + 'stat STORE(): read-only child node list length element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->{'children_zxid'} = 'foo'; + like($msg, qr/read-only element: children_zxid/, + 'stat STORE(): read-only child node list last-modified ' . + 'ZooKeeper transaction ID element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $attr->STORE('version', 'foo'); + like($msg, qr/read-only element: version/, + 'stat STORE(): read-only data version element using ' . + 'inner stat hash'); + } + + + ## EXISTS() + + eval { + my $val = $copy_stat->EXISTS('version'); + }; + like($@, qr/invalid handle/, + 'stat EXISTS(): invalid stat handle'); + + ok(!exists($stat->{'foo'}), + 'exists(): invalid element of stat handle'); + + ok(exists($stat->{'czxid'}), + 'exists(): node creation ZooKeeper transaction ID'); + + ok(exists($stat->{'mzxid'}), + 'exists(): data last-modified ZooKeeper transaction ID'); + + ok(exists($stat->{'ctime'}), + 'exists(): node creation time'); + + ok(exists($stat->{'mtime'}), + 'exists(): data last-modified time'); + + ok(exists($stat->{'version'}), + 'exists(): data version'); + + ok(exists($stat->{'children_version'}), + 'exists(): child node list version'); + + ok(exists($stat->{'acl_version'}), + 'exists(): ACL version'); + + ok(exists($stat->{'ephemeral_owner'}), + 'exists(): ephemeral node owner session ID'); + + ok(exists($stat->{'data_len'}), + 'exists(): data length'); + + ok(exists($stat->{'num_children'}), + 'exists(): child node list length'); + + ok(exists($stat->{'children_zxid'}), + 'exists(): child node list last-modified ZooKeeper transaction ID'); + + ok($attr->EXISTS('version'), + 'stat EXISTS(): data version using inner stat hash'); + + + ## DELETE(), CLEAR() + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + delete($stat->{'version'}); + like($msg, + qr/deleting elements from hashes of class Net::ZooKeeper::Stat not supported/, + 'delete(): deleting stat hash elements not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->DELETE({'version'}); + like($msg, + qr/deleting elements from hashes of class Net::ZooKeeper::Stat not supported/, + 'stat DELETE(): deleting stat hash elements not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + %{$stat} = (); + like($msg, qr/clearing hashes of class Net::ZooKeeper::Stat not supported/, + 'assign: clearing stat hashes not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $stat->CLEAR(); + like($msg, qr/clearing hashes of class Net::ZooKeeper::Stat not supported/, + 'stat CLEAR(): clearing stat hashes not supported'); + } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/24_watch_tie.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/24_watch_tie.t new file mode 100644 index 0000000..e77879e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/24_watch_tie.t @@ -0,0 +1,292 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 42; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + my $watch = $zkh->watch() if (defined($zkh)); + + skip 'no valid watch handle', 4 unless (defined($watch)); + + + ## DESTROY() + + my $attr = tied(%{$watch}); + + my $ret = $attr->DESTROY(); + ok($ret, + 'watch DESTROY(): destroyed inner watch hash'); + + $ret = $attr->DESTROY(); + ok(!$ret, + 'watch DESTROY(): no action on destroyed inner watch hash'); + + $ret = $watch->DESTROY(); + ok(!$ret, + 'watch DESTROY(): no action on watch handle with destroyed inner hash'); + + undef $watch; + ok(!defined($watch), + 'undef: released watch handle with destroyed inner hash'); +} + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + my $watch = $zkh->watch() if (defined($zkh)); + + skip 'no valid watch handle', 37 unless (defined($watch)); + + + ## TIEHASH(), UNTIE() + + eval { + tie(%{$watch}, 'Net::ZooKeeper::Watch'); + }; + like($@, qr/tying hashes of class Net::ZooKeeper::Watch not supported/, + 'tie(): tying watch hashes not supported'); + + eval { + Net::ZooKeeper::Watch::TIEHASH('Net::ZooKeeper::Watch'); + }; + like($@, qr/tying hashes of class Net::ZooKeeper::Watch not supported/, + 'watch TIEHASH(): tying watch hashes not supported'); + + eval { + untie(%{$watch}); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/, + 'untie(): untying watch hashes not supported'); + + my $attr = tied(%{$watch}); + + eval { + $attr->UNTIE(0); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/, + 'watch UNTIE(): untying watch hashes not supported'); + + + ## FIRSTKEY(), NEXTKEY(), SCALAR() + + my $copy_watch; + { + my %copy_watch = %{$watch}; + $copy_watch = \%copy_watch; + } + bless($copy_watch, 'Net::ZooKeeper::Watch'); + is(ref($copy_watch), 'Net::ZooKeeper::Watch', + 'watch FIRSTKEY(), NEXTKEY(): copied dereferenced watch handle'); + + eval { + my $val = $copy_watch->FIRSTKEY(); + }; + like($@, qr/invalid handle/, + 'watch FETCHKEY(): invalid watch handle'); + + eval { + my $val = $copy_watch->NEXTKEY('czxid'); + }; + like($@, qr/invalid handle/, + 'watch NEXTKEY(): invalid watch handle'); + + my @keys = keys(%{$watch}); + is(scalar(@keys), 3, + 'keys(): count of keys from watch handle'); + + @keys = keys(%{$copy_watch}); + is(scalar(@keys), 3, + 'keys(): count of keys from copied dereferenced watch handle'); + + is($attr->FIRSTKEY(), 'timeout', + 'watch FIRSTKEY(): retrieved first key using inner watch hash'); + + is($attr->NEXTKEY('event'), 'state', + 'watch NEXTKEY(): retrieved last key using inner watch hash'); + + is($attr->NEXTKEY('state'), undef, + 'NEXTKEY(): undef returned after last key using inner watch hash'); + + ok(scalar(%{$watch}), + 'scalar(): true value returned for dereferenced watch handle'); + + ok($watch->SCALAR(), + 'watch SCALAR(): true value returned'); + + + ## FETCH() + + eval { + my $val = $copy_watch->FETCH('version'); + }; + like($@, qr/invalid handle/, + 'watch FETCH(): invalid watch handle'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + my $val = $watch->{'foo'}; + ok(!defined($val), + 'watch FETCH(): undef returned for invalid element'); + + like($msg, qr/invalid element/, + 'watch FETCH(): invalid element'); + } + + is($watch->{'timeout'}, 60000, + 'watch FETCH(): default timeout'); + + is($watch->{'event'}, 0, + 'watch FETCH(): default event'); + + is($watch->{'state'}, 0, + 'watch FETCH(): default state'); + + is($attr->FETCH('timeout'), 60000, + 'watch FETCH(): default timeout using inner watch hash'); + + + ## STORE() + + eval { + my $val = $copy_watch->STORE('version', 'foo'); + }; + like($@, qr/invalid handle/, + 'watch STORE(): invalid watch handle'); + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $watch->{'foo'} = 'foo'; + like($msg, qr/invalid element/, + 'watch STORE(): invalid element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $watch->{'event'} = 'foo'; + like($msg, qr/read-only element: event/, + 'watch STORE(): read-only event element'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $watch->{'state'} = 'foo'; + like($msg, qr/read-only element: state/, + 'watch STORE(): read-only state element'); + } + + $watch->{'timeout'} = 100; + is($watch->{'timeout'}, 100, + 'watch STORE(): updated timeout'); + + $attr->STORE('timeout', 200); + is($watch->{'timeout'}, 200, + 'watch STORE(): updated timeout using inner hash'); + + + ## EXISTS() + + eval { + my $val = $copy_watch->EXISTS('version'); + }; + like($@, qr/invalid handle/, + 'watch EXISTS(): invalid watch handle'); + + ok(!exists($watch->{'foo'}), + 'exists(): invalid element of watch handle'); + + ok(exists($watch->{'timeout'}), + 'exists(): timeout'); + + ok(exists($watch->{'event'}), + 'exists(): event'); + + ok(exists($watch->{'state'}), + 'exists(): state'); + + ok($attr->EXISTS('timeout'), + 'watch EXISTS(): timeout using inner watch hash'); + + + ## DELETE(), CLEAR() + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + delete($watch->{'version'}); + like($msg, + qr/deleting elements from hashes of class Net::ZooKeeper::Watch not supported/, + 'delete(): deleting watch hash elements not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $watch->DELETE({'version'}); + like($msg, + qr/deleting elements from hashes of class Net::ZooKeeper::Watch not supported/, + 'watch DELETE(): deleting watch hash elements not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + %{$watch} = (); + like($msg, qr/clearing hashes of class Net::ZooKeeper::Watch not supported/, + 'assign: clearing watch hashes not supported'); + } + + { + my $msg; + + $SIG{'__WARN__'} = sub { $msg = $_[0]; }; + + $watch->CLEAR(); + like($msg, qr/clearing hashes of class Net::ZooKeeper::Watch not supported/, + 'watch CLEAR(): clearing watch hashes not supported'); + } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/30_connect.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/30_connect.t new file mode 100644 index 0000000..c2b68bb --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/30_connect.t @@ -0,0 +1,202 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 29; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +## new(), DESTROY() + +Net::ZooKeeper::set_deterministic_conn_order(1); + +my $zkh = Net::ZooKeeper->new($hosts); +isa_ok($zkh, 'Net::ZooKeeper', + 'new(): created handle'); + +SKIP: { + skip 'no valid handle', 3 unless (defined($zkh)); + + my $ret = $zkh->DESTROY(); + ok($ret, + 'DESTROY(): destroyed handle'); + + $ret = $zkh->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on destroyed handle'); + + undef $zkh; + ok(!defined($zkh), + 'undef: released handle'); +} + +Net::ZooKeeper::set_deterministic_conn_order(0); + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + skip 'no valid handle', 10 unless (defined($zkh)); + + my $copy_zkh = $zkh; + isa_ok($copy_zkh, 'Net::ZooKeeper', + 'assign: copied handle'); + + my $ret = $zkh->exists($root_path); + ok(defined($ret), + 'exists(): no error from original handle'); + + undef $zkh; + ok(!defined($zkh), + 'undef: released original handle'); + + $ret = $copy_zkh->exists($root_path); + ok(defined($ret), + 'exists(): no error from first copy of handle'); + + $zkh = $copy_zkh; + isa_ok($zkh, 'Net::ZooKeeper', + 'assign: re-copied handle'); + + $ret = $copy_zkh->DESTROY(); + ok($ret, + 'DESTROY(): destroyed first copy of handle'); + + eval { + $zkh->exists($root_path); + }; + like($@, qr/invalid handle/, + 'exists(): invalid second copy of handle'); + + undef $copy_zkh; + ok(!defined($copy_zkh), + 'undef: released first copy of handle'); + + $ret = $zkh->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on second copy of destroyed handle'); + + undef $zkh; + ok(!defined($zkh), + 'undef: released second copy of handle'); +} + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + skip 'no valid handle', 6 unless (defined($zkh)); + + my $copy_zkh; + { + my %copy_zkh = %{$zkh}; + $copy_zkh = \%copy_zkh; + } + bless($copy_zkh, 'Net::ZooKeeper'); + isa_ok($copy_zkh, 'Net::ZooKeeper', + 'FIRSTKEY(), NEXTKEY(): copied dereferenced handle'); + + eval { + $copy_zkh->exists($root_path); + }; + like($@, qr/invalid handle/, + 'exists(): invalid copy of dereferenced handle'); + + $ret = $copy_zkh->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on copy of dereferenced handle'); + + undef $copy_zkh; + ok(!defined($copy_zkh), + 'undef: released copy of dereferenced handle'); + + my $ret = $zkh->exists($root_path); + ok(defined($ret), + 'exists(): no error from original handle'); + + undef $zkh; + ok(!defined($zkh), + 'undef: released original handle'); +} + +Net::ZooKeeper::set_deterministic_conn_order(1); + +my $zkh1 = Net::ZooKeeper->new($hosts, 'session_timeout' => 0x3FFF_FFFF); +isa_ok($zkh1, 'Net::ZooKeeper', + 'new(): created handle with maximum session timeout'); + +SKIP: { + my $ret = $zkh1->exists($root_path) if (defined($zkh1)); + + skip 'no connection to ZooKeeper', 7 unless + (defined($ret) and $ret); + + + ## FETCH() of read-only attributes + + ok(($zkh1->{'session_timeout'} > 0 and + $zkh1->{'session_timeout'} <= 0x3FFF_FFFF), + 'FETCH(): session timeout reset after connection'); + + my $session_id1 = $zkh1->{'session_id'}; + ok((length($session_id1) > 0), + 'FETCH(): non-empty session ID after connection'); + + SKIP: { + skip 'no session ID after connection', 1 unless + (length($session_id1) > 0); + + my @nonzero_bytes = grep($_ != 0, unpack('c' x length($session_id1), + $session_id1)); + ok((@nonzero_bytes > 0), + 'FETCH(): non-zero session ID after connection'); + } + + ## NOTE: to test re-connections with saved session IDs we create a second + ## connection with the same ID while the first is still active; + ## this is bad practice in normal usage + + my $zkh2 = Net::ZooKeeper->new($hosts, + 'session_id' => $session_id1, + 'session_timeout' => 20000); + isa_ok($zkh2, 'Net::ZooKeeper', + 'new(): created handle with session ID and valid session timeout'); + + $ret = $zkh2->exists($root_path); + ok($ret, + 'new(): reconnection with session ID'); + + SKIP: { + skip 'no connection to ZooKeeper', 2 unless ($ret); + + is($zkh2->{'session_timeout'}, 20000, + 'FETCH(): session timeout unchanged after connection'); + + my $session_id2 = $zkh2->{'session_id'}; + ok((length($session_id2) == length($session_id1) + and $session_id2 eq $session_id1), + 'FETCH(): reconnect with session ID'); + } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/35_log.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/35_log.t new file mode 100644 index 0000000..92821af --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/35_log.t @@ -0,0 +1,88 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 3; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +my $zkh = Net::ZooKeeper->new($hosts); + +Net::ZooKeeper::set_log_level(ZOO_LOG_LEVEL_INFO); + +SKIP: { + skip 'no valid handle', 2 unless (defined($zkh)); + + SKIP: { + my $dup = 0; + + if (open(OLDERR, '>&', fileno(STDERR))) { + if (close(STDERR) and open(STDERR, '+>', undef)) { + $dup = 1; + + my $old_select = select(STDERR); + $| = 1; + select($old_select); + } + else { + open(STDERR, '>&', fileno(OLDERR)); + close(OLDERR); + } + } + + skip 'no duplicated stderr', 2 unless ($dup); + + SKIP: { + $zkh->exists($root_path); + + sleep(1); + + skip 'no seek on stderr', 1 unless (seek(STDERR, 0, 0)); + + my $log = ; + like($log, qr/ZOO_/, + 'exists(): generated log message'); + } + + SKIP: { + $zkh->DESTROY(); + + sleep(1); + + skip 'no seek on stderr', 1 unless (seek(STDERR, 0, 0)); + + my $log = ; + like($log, qr/ZOO_/, + 'DESTROY(): generated log message'); + } + + open(STDERR, '>&', fileno(OLDERR)); + close(OLDERR); + } +} + +Net::ZooKeeper::set_log_level(ZOO_LOG_LEVEL_OFF); + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/40_basic.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/40_basic.t new file mode 100644 index 0000000..38a8a21 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/40_basic.t @@ -0,0 +1,277 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 35; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +my $zkh = Net::ZooKeeper->new($hosts); +my $path; + +SKIP: { + my $ret = $zkh->exists($root_path) if (defined($zkh)); + + skip 'no connection to ZooKeeper', 1 unless + (defined($ret) and $ret); + + $path = $zkh->create($node_path, 'foo', 'acl' => ZOO_OPEN_ACL_UNSAFE); + is($path, $node_path, + 'create(): created node'); +} + +SKIP: { + skip 'no connection to ZooKeeper', 21 unless + (defined($path) and $path eq $node_path); + + + ## exists() + + my $ret = $zkh->exists($node_path); + ok($ret, + 'exists(): checked node existence'); + + $ret = $zkh->exists($node_path . '/NONE'); + ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''), + 'exists(): checked node non-existence'); + + my $stat = $zkh->stat(); + + $ret = $zkh->exists($node_path, 'stat' => $stat); + ok(($ret and $stat->{'data_len'} == 3), + 'exists(): checked node existence with stat handle'); + + + ## get() + + my $node = $zkh->get($node_path); + is($node, 'foo', + 'get(): retrieved node value'); + + $node = $zkh->get($node_path . '/NONE'); + ok((!defined($node) and $zkh->get_error() == ZNONODE and $! eq ''), + 'get(): undef returned for non-extant node'); + + $node = $zkh->get($node_path, 'data_read_len', 2); + is($node, 'fo', + 'get(): retrieved truncated node value'); + + $node = $zkh->get($node_path, 'data_read_len' => 0); + is($node, '', + 'get(): retrieved zero-length node value'); + + $node = $zkh->get($node_path, 'stat' => $stat); + ok(($node eq 'foo' and $stat->{'data_len'} == 3), + 'get(): retrieved node value with stat handle'); + + + ## set() + + $ret = $zkh->set($node_path, 'foo'); + ok($ret, + 'set(): set node value'); + + SKIP: { + my $ret = $zkh->exists($node_path, 'stat' => $stat); + + skip 'invalid node data', 2 unless + ($ret and $stat->{'version'} == 1); + + $ret = $zkh->set($node_path, 'foo', 'version' => $stat->{'version'}); + ok($ret, + 'set(): set node value with matching version'); + + $ret = $zkh->set($node_path, 'foo', 'version' => $stat->{'version'}); + ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''), + 'set(): node value unchanged if non-matching version'); + } + + $ret = $zkh->set($node_path, 'foobaz', 'stat' => $stat); + ok(($ret and $stat->{'data_len'} == 6), + 'set(): retrieved node value with stat handle'); + + + ## create(), delete() + + $path = $zkh->create($node_path, 'foo', 'acl' => ZOO_OPEN_ACL_UNSAFE); + ok((!defined($path) and $zkh->get_error() == ZNODEEXISTS and $! eq ''), + 'create(): undef when attempting to create extant node'); + + $ret = $zkh->delete($node_path . '/NONE'); + ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''), + 'delete(): no deletion of non-extant node'); + + $ret = $zkh->delete($node_path); + ok($ret, + 'delete(): deleted node'); + + my $path_read_len = length($node_path) - 2; + + $path = $zkh->create($node_path, 'foo', + 'path_read_len' => $path_read_len, + 'acl' => ZOO_OPEN_ACL_UNSAFE); + is($path, substr($node_path, 0, -2), + 'create(): created node with small return path buffer'); + + $path = $zkh->create("$node_path/s", 'foo', + 'flags' => ZOO_SEQUENCE, + 'acl' => ZOO_OPEN_ACL_UNSAFE); + like($path, qr/^$node_path\/s[0-9]+$/, + 'create(): created sequential node'); + + SKIP: { + my $ret = $zkh->exists($path, 'stat' => $stat); + + unless ($ret and $stat->{'version'} == 0) { + my $ret = $zkh->delete($path); + diag(sprintf('unable to delete node %s: %d, %s', + $path, $zkh->get_error(), $!)) unless ($ret); + + skip 'invalid node data', 2; + } + + $ret = $zkh->delete($path, 'version' => ($stat->{'version'} + 1)); + ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''), + 'delete(): node not deleted if non-matching version'); + + $ret = $zkh->delete($path, 'version' => $stat->{'version'}); + ok($ret, + 'delete(): deleted sequential node with matching version'); + } + + $path = $zkh->create("$node_path/e", 'foo', + 'flags' => ZOO_EPHEMERAL, + 'acl' => ZOO_OPEN_ACL_UNSAFE); + is($path, "$node_path/e", + 'create(): created ephemeral node'); + + $path = $zkh->create("$node_path/es", 'foo', + 'flags' => (ZOO_SEQUENCE | ZOO_EPHEMERAL), + 'acl' => ZOO_OPEN_ACL_UNSAFE); + like($path, qr/^$node_path\/es[0-9]+$/, + 'create(): created ephemeral sequential node'); + + undef $zkh; +} + +$zkh = Net::ZooKeeper->new($hosts); + +SKIP: { + my $ret = $zkh->exists($node_path) if (defined($zkh)); + + skip 'no connection to ZooKeeper', 12 unless + (defined($ret) and $ret); + + $ret = $zkh->exists("$node_path/e"); + ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''), + 'exists(): checked ephemeral node non-extant after reconnection'); + + $ret = $zkh->exists($path); + ok((!$ret and $zkh->get_error() == ZNONODE and $! eq ''), + 'exists(): checked ephemeral sequential node non-extant ' . + 'after reconnection'); + + + ## get_children() + + my @child_paths = ('abc'); + @child_paths = $zkh->get_children($node_path); + ok((@child_paths == 0 and $zkh->get_error() == ZOK), + 'get_children(): retrieved empty list of child nodes'); + + my $num_children = $zkh->get_children($node_path); + ok((defined($num_children) and $num_children == 0), + 'get_children(): retrieved zero count of child nodes'); + + @child_paths = $zkh->get_children($node_path . '/NONE'); + ok((@child_paths == 0 and $zkh->get_error() == ZNONODE and $! eq ''), + 'get_children(): empty list returned for non-extant node'); + + $num_children = $zkh->get_children($node_path . '/NONE'); + ok((!defined($num_children) and $zkh->get_error() == ZNONODE and $! eq ''), + 'get_children(): undef returned for non-extant node'); + + SKIP: { + my $path = $zkh->create("$node_path/c1", 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE); + + skip 'no connection to ZooKeeper', 6 unless + (defined($path) and $path eq "$node_path/c1"); + + my @child_paths = ('abc'); + @child_paths = $zkh->get_children($node_path); + ok((@child_paths == 1 and $child_paths[0] eq 'c1'), + 'get_children(): retrieved list of single child node'); + + my $num_children = $zkh->get_children($node_path); + ok((defined($num_children) and $num_children == 1), + 'get_children(): retrieved count of single child node'); + + SKIP: { + my $path = $zkh->create("$node_path/c2", 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE); + + skip 'no connection to ZooKeeper', 2 unless + (defined($path) and $path eq "$node_path/c2"); + + my @child_paths = ('abc'); + @child_paths = $zkh->get_children($node_path); + ok((@child_paths == 2 and $child_paths[0] eq 'c1' and + $child_paths[1] eq 'c2'), + 'get_children(): retrieved list of two child nodes'); + + my $num_children = $zkh->get_children($node_path); + ok((defined($num_children) and $num_children == 2), + 'get_children(): retrieved count of two child nodes'); + + my $ret = $zkh->delete("$node_path/c2"); + diag(sprintf('unable to delete node %s: %d, %s', + "$node_path/c2", $zkh->get_error(), $!)) unless + ($ret); + } + + @child_paths = ('abc'); + @child_paths = $zkh->get_children($node_path); + ok((@child_paths == 1 and $child_paths[0] eq 'c1'), + 'get_children(): retrieved list of single child node'); + + $num_children = $zkh->get_children($node_path); + ok((defined($num_children) and $num_children == 1), + 'get_children(): retrieved count of single child node'); + + my $ret = $zkh->delete("$node_path/c1"); + diag(sprintf('unable to delete node %s: %d, %s', + "$node_path/c1", $zkh->get_error(), $!)) unless ($ret); + } + + + ## cleanup + + $ret = $zkh->delete($node_path); + diag(sprintf('unable to delete node %s: %d, %s', + $node_path, $zkh->get_error(), $!)) unless ($ret); +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/45_class.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/45_class.t new file mode 100644 index 0000000..4aa1a57 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/45_class.t @@ -0,0 +1,408 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 47; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + skip 'no valid handle', 15 unless (defined($zkh)); + + my $stat = $zkh->stat(); + my $watch = $zkh->watch(); + + + ## DESTROY() on reblessed handle + + bless($zkh, 'My::ZooKeeper'); + is(ref($zkh), 'My::ZooKeeper', + 'bless(): reblessed handle'); + + eval { + $zkh->EXISTS(); + }; + like($@, qr/Can't locate object method "EXISTS" via package "My::ZooKeeper"/, + 'EXISTS(): not defined on reblessed handle'); + + my $attr = tied(%{$zkh}); + + my $ret = $attr->DESTROY(); + ok($ret, + 'DESTROY(): destroyed inner hash of reblessed handle'); + + $ret = $attr->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on destroyed inner hash of reblessed handle'); + + undef $zkh; + ok(!defined($zkh), + 'undef: released reblessed handle'); + + + ## DESTROY() on reblessed stat handle + + bless($stat, 'My::ZooKeeper::Stat'); + is(ref($stat), 'My::ZooKeeper::Stat', + 'bless(): reblessed stat handle'); + + eval { + $stat->EXISTS(1); + }; + like($@, qr/Can't locate object method "EXISTS" via package "My::ZooKeeper::Stat"/, + 'stat EXISTS(): not defined on reblessed stat handle'); + + $attr = tied(%{$stat}); + + $ret = $attr->DESTROY(); + ok($ret, + 'stat DESTROY(): destroyed inner hash of reblessed stat handle'); + + $ret = $attr->DESTROY(); + ok(!$ret, + 'stat DESTROY(): no action on destroyed inner hash of ' . + 'reblessed stat handle'); + + undef $stat; + ok(!defined($stat), + 'undef: released reblessed stat handle'); + + + ## DESTROY() on reblessed watch handle + + bless($watch, 'My::ZooKeeper::Watch'); + is(ref($watch), 'My::ZooKeeper::Watch', + 'bless(): reblessed watch handle'); + + eval { + $watch->EXISTS(1); + }; + like($@, qr/Can't locate object method "EXISTS" via package "My::ZooKeeper::Watch"/, + 'watch EXISTS(): not defined on reblessed watch handle'); + + $attr = tied(%{$watch}); + + $ret = $attr->DESTROY(); + ok($ret, + 'watch DESTROY(): destroyed inner hash of reblessed watch handle'); + + $ret = $attr->DESTROY(); + ok(!$ret, + 'watch DESTROY(): no action on destroyed inner hash of ' . + 'reblessed watch handle'); + + undef $watch; + ok(!defined($watch), + 'undef: released reblessed watch handle'); +} + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + skip 'no valid handle', 9 unless (defined($zkh)); + + my $stat = $zkh->stat(); + my $watch = $zkh->watch(); + + + ## UNTIE() on reblessed handle + + bless($zkh, 'My::ZooKeeper'); + is(ref($zkh), 'My::ZooKeeper', + 'bless(): reblessed handle'); + + eval { + untie(%{$zkh}); + }; + like($@, qr/untying hashes of class Net::ZooKeeper not supported/, + 'untie(): untying hashes from reblessed handle not supported'); + + my $attr = tied(%{$zkh}); + + eval { + $attr->UNTIE(0); + }; + like($@, qr/untying hashes of class Net::ZooKeeper not supported/, + 'UNTIE(): untying hashes from reblessed handle not supported'); + + + ## UNTIE() on reblessed stat handle + + bless($stat, 'My::ZooKeeper::Stat'); + is(ref($stat), 'My::ZooKeeper::Stat', + 'bless(): reblessed stat handle'); + + eval { + untie(%{$stat}); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/, + 'untie(): untying hashes from reblessed stat handle not supported'); + + $attr = tied(%{$stat}); + + eval { + $attr->UNTIE(0); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Stat not supported/, + 'stat UNTIE(): untying hashes from reblessed stat handle ' . + 'not supported'); + + + ## UNTIE() on reblessed watch handle + + bless($watch, 'My::ZooKeeper::Watch'); + is(ref($watch), 'My::ZooKeeper::Watch', + 'bless(): reblessed watch handle'); + + eval { + untie(%{$watch}); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/, + 'untie(): untying hashes from reblessed watch handle not supported'); + + $attr = tied(%{$watch}); + + eval { + $attr->UNTIE(0); + }; + like($@, qr/untying hashes of class Net::ZooKeeper::Watch not supported/, + 'watch UNTIE(): untying hashes from reblessed watch handle ' . + 'not supported'); +} + + +package Net::ZooKeeper::Test; + +use Net::ZooKeeper qw(:acls); + +our @ISA = qw(Net::ZooKeeper); + +sub create +{ + my($self, $path, $buf) = @_; + + return $self->SUPER::create($path, $buf, + 'path_read_len' => length($path), + 'acl' => ZOO_OPEN_ACL_UNSAFE); +} + +sub get_first_child +{ + my($self, $path) = @_; + + my @child_paths = $self->get_children($path); + + if (@child_paths > 0) { + return $path . (($path =~ /\/$/) ? '' : '/') . $child_paths[0]; + } + + return undef; +} + +sub stat +{ + my $self = shift; + + my $stat = $self->SUPER::stat(); + + return bless($stat, 'Net::ZooKeeper::Test::Stat'); +} + + +sub watch +{ + my $self = shift; + + my $watch = $self->SUPER::watch(); + + return bless($watch, 'Net::ZooKeeper::Test::Watch'); +} + + +package Net::ZooKeeper::Test::Stat; + +our @ISA = qw(Net::ZooKeeper::Stat); + +sub get_ctime +{ + my $self = shift; + + return $self->{'ctime'}; +} + + +package Net::ZooKeeper::Test::Watch; + +our @ISA = qw(Net::ZooKeeper::Watch); + +sub get_timeout +{ + my $self = shift; + + return $self->{'timeout'}; +} + + +package main; + +my $sub_zkh = Net::ZooKeeper::Test->new($hosts); +isa_ok($sub_zkh, 'Net::ZooKeeper::Test', + 'new(): created subclassed handle'); + +SKIP: { + skip 'no valid subclassed handle', 21 unless (defined($sub_zkh)); + + is($sub_zkh->{'data_read_len'}, 1023, + 'FETCH(): default data read length using subclassed handle'); + + my $path; + + SKIP: { + my $ret = $sub_zkh->exists($root_path); + + skip 'no connection to ZooKeeper', 1 unless + (defined($ret) and $ret); + + $path = $sub_zkh->create($node_path, 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE); + is($path, $node_path, + 'create(): created node with subclassed handle'); + } + + SKIP: { + skip 'no connection to ZooKeeper', 1 unless + (defined($path) and $path eq $node_path); + + my $child_path = $sub_zkh->get_first_child($root_path); + is($child_path, $node_path, + 'get_first_child(): retrieved first child with subclassed handle'); + } + + my $sub_stat = $sub_zkh->stat(); + isa_ok($sub_stat, 'Net::ZooKeeper::Test::Stat', + 'stat(): created subclassed stat handle'); + + SKIP: { + skip 'no valid subclassed stat handle', 6 unless + (defined($sub_stat)); + + is($sub_stat->{'ctime'}, 0, + 'stat FETCH(): default ctime using subclassed stat handle'); + + SKIP: { + my $ret = $sub_zkh->exists($node_path, 'stat' => $sub_stat) if + (defined($path) and $path eq $node_path); + + skip 'no connection to ZooKeeper', 2 unless + (defined($ret) and $ret); + + my $ctime = $sub_stat->get_ctime(); + ok($ctime > 0, + 'get_ctime(): retrieved ctime with subclassed stat handle'); + + is($sub_stat->{'ctime'}, $ctime, + 'stat FETCH(): ctime using subclassed stat handle'); + } + + my $ret = $sub_stat->DESTROY(); + ok($ret, + 'stat DESTROY(): destroyed subclassed stat handle'); + + $ret = $sub_stat->DESTROY(); + ok(!$ret, + 'stat DESTROY(): no action on destroyed subclassed stat handle'); + + undef $sub_stat; + ok(!defined($sub_stat), + 'undef: released subclassed stat handle'); + } + + my $sub_watch = $sub_zkh->watch(); + isa_ok($sub_watch, 'Net::ZooKeeper::Test::Watch', + 'watch(): created subclassed watch handle'); + + SKIP: { + skip 'no valid subclassed watch handle', 6 unless + (defined($sub_watch)); + + SKIP: { + my $ret = $sub_zkh->exists($root_path, 'watch' => $sub_watch); + + skip 'no connection to ZooKeeper', 3 unless + (defined($ret) and $ret); + + $sub_watch->{'timeout'} = 50; + + is($sub_watch->get_timeout(), 50, + 'get_timeout(): retrieved timeout with subclassed ' . + 'watch handle'); + + is($sub_watch->{'timeout'}, 50, + 'watch FETCH(): timeout using subclassed stat handle'); + + $ret = $sub_watch->wait(); + ok(!$ret, + 'wait(): watch after checking node existence timed out with ' . + 'subclassed watch handle'); + } + + my $ret = $sub_watch->DESTROY(); + ok($ret, + 'watch DESTROY(): destroyed subclassed watch handle'); + + $ret = $sub_watch->DESTROY(); + ok(!$ret, + 'watch DESTROY(): no action on destroyed subclassed watch handle'); + + undef $sub_watch; + ok(!defined($sub_watch), + 'undef: released subclassed watch handle'); + } + + SKIP: { + skip 'no connection to ZooKeeper', 1 unless + (defined($path) and $path eq $node_path); + + my $ret = $sub_zkh->delete($node_path); + ok($ret, + 'delete(): deleted node with subclassed handle'); + } + + my $ret = $sub_zkh->DESTROY(); + ok($ret, + 'DESTROY(): destroyed subclassed handle'); + + $ret = $sub_zkh->DESTROY(); + ok(!$ret, + 'DESTROY(): no action on destroyed subclassed handle'); + + undef $sub_zkh; + ok(!defined($sub_zkh), + 'undef: released subclassed handle'); +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/50_access.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/50_access.t new file mode 100644 index 0000000..ef61ed6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/50_access.t @@ -0,0 +1,356 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 40; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + +my($username, $password, $digest) = zk_acl_test_setup(); + + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + my $path = $zkh->create($node_path, 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh)); + + skip 'no connection to ZooKeeper', 36 unless + (defined($path) and $path eq $node_path); + + + ## _zk_acl_constant() + + my $no_read_acl = ZOO_OPEN_ACL_UNSAFE; + ok((ref($no_read_acl) eq 'ARRAY' and + @{$no_read_acl} == 1 and + ref($no_read_acl->[0]) eq 'HASH' and + keys(%{$no_read_acl->[0]}) == 3 and + $no_read_acl->[0]->{'perms'} == ZOO_PERM_ALL), + '_zk_acl_constant(): returned default ACL'); + + my $zoo_read_acl_unsafe = ZOO_READ_ACL_UNSAFE; + ok((ref($zoo_read_acl_unsafe) eq 'ARRAY' and + @{$zoo_read_acl_unsafe} == 1 and + ref($zoo_read_acl_unsafe->[0]) eq 'HASH' and + keys(%{$zoo_read_acl_unsafe->[0]}) == 3 and + $zoo_read_acl_unsafe->[0]->{'perms'} == ZOO_PERM_READ), + '_zk_acl_constant(): returned good ACL'); + + my $zoo_creator_all_acl = ZOO_CREATOR_ALL_ACL; + ok((ref($zoo_creator_all_acl) eq 'ARRAY' and + @{$zoo_creator_all_acl} == 1 and + ref($zoo_creator_all_acl->[0]) eq 'HASH' and + keys(%{$zoo_creator_all_acl->[0]}) == 3 and + $zoo_creator_all_acl->[0]->{'perms'} == ZOO_PERM_ALL), + '_zk_acl_constant(): returned good ACL'); + + $no_read_acl->[0]->{'perms'} &= ~ZOO_PERM_READ; + is($no_read_acl->[0]->{'perms'}, ((ZOO_PERM_ALL) & ~ZOO_PERM_READ), + 'assign: altered default ACL'); + + is(ZOO_OPEN_ACL_UNSAFE->[0]->{'perms'}, ZOO_PERM_ALL, + '_zk_acl_constant(): returned unaltered default ACL'); + + my $copy_no_read_acl = $no_read_acl; + is_deeply($copy_no_read_acl, $no_read_acl, + 'assign: copied default ACL'); + + undef $no_read_acl; + ok(!defined($no_read_acl), + 'undef: released original default ACL'); + + is($copy_no_read_acl->[0]->{'perms'}, ((ZOO_PERM_ALL) & ~ZOO_PERM_READ), + 'undef: no change to copied default ACL'); + + $no_read_acl = $copy_no_read_acl; + is_deeply($no_read_acl, $copy_no_read_acl, + 'assign: re-copied default ACL'); + + + ## create() + + my $acl_node_path = "$node_path/a1"; + + $path = $zkh->create($acl_node_path, 'foo', 'acl' => $no_read_acl); + is($path, $acl_node_path, + 'create(): created node with no-read ACL'); + + my $node = $zkh->get($acl_node_path); + + my $skip_acl; + if (defined($node) and $node eq 'foo') { + $skip_acl = 1; + } + elsif(!defined($node) and $zkh->get_error() == ZNOAUTH) { + $skip_acl = 0; + } + else { + $skip_acl = -1; + diag(sprintf('unable to get node with no-read ACL %s: %d, %s', + $acl_node_path, $zkh->get_error(), $!)); + } + + my $ret = $zkh->delete($acl_node_path); + diag(sprintf('unable to delete node with no-read ACL %s: %d, %s', + $acl_node_path, $zkh->get_error(), $!)) unless ($ret); + + my $digest_acl = [ + { + 'perms' => ZOO_PERM_READ, + 'scheme' => 'world', + 'id' => 'anyone' + }, + { + 'perms' => (ZOO_PERM_WRITE | ZOO_PERM_ADMIN), + 'scheme' => 'digest', + 'id' => "$username:$digest" + } + ]; + + $path = $zkh->create($acl_node_path, 'foo', 'acl' => $digest_acl); + is($path, $acl_node_path, + 'create(): created node with digest auth ACL'); + + SKIP: { + skip 'ZooKeeper skipping ACLs', 1 unless (!$skip_acl); + + my $acl_node_path = "$node_path/a2"; + + my $path = $zkh->create($acl_node_path, 'foo', 'acl' => [ + { + 'perms' => ZOO_PERM_WRITE, + 'scheme' => 'foo', + 'id' => 'bar' + } + ]); + ok((!defined($path) and $zkh->get_error() == ZINVALIDACL and $! eq ''), + 'create(): undef when attempting to create node with invalid ACL'); + } + + + ## get_acl() + + my @acl = ('abc'); + @acl = $zkh->get_acl($node_path . '/NONE'); + ok((@acl == 0 and $zkh->get_error() == ZNONODE and $! eq ''), + 'get_acl(): empty list returned for non-extant node'); + + $num_acl_entries = $zkh->get_acl($node_path . '/NONE'); + ok((!defined($num_acl_entries) and $zkh->get_error() == ZNONODE and + $! eq ''), + 'get_acl(): undef returned for non-extant node'); + + @acl = ('abc'); + @acl = $zkh->get_acl($acl_node_path); + is_deeply(\@acl, $digest_acl, + 'get_acl(): retrieved digest ACL'); + + my $stat = $zkh->stat(); + + @acl = ('abc'); + @acl = $zkh->get_acl($node_path, 'stat' => $stat); + is_deeply(\@acl, ZOO_OPEN_ACL_UNSAFE, + 'get_acl(): retrieved ACL'); + + is($stat->{'data_len'}, 3, + 'get_acl(): retrieved ACL with stat handle'); + + SKIP: { + skip 'ZooKeeper not skipping ACLs', 3 unless ($skip_acl > 0); + + my $acl_node_path = "$node_path/a2"; + + my $path = $zkh->create($acl_node_path, 'foo', 'acl' => []); + is($path, $acl_node_path, + 'create(): created node with empty ACL'); + + my @acl = ('abc'); + @acl = $zkh->get_acl($acl_node_path); + ok((@acl == 0 and $zkh->get_error() == ZOK), + 'get_acl(): retrieved empty ACL'); + + my $num_acl_entries = $zkh->get_acl($acl_node_path); + ok((defined($num_acl_entries) and $num_acl_entries == 0), + 'get_acl(): retrieved zero count of ACL entries'); + + my $ret = $zkh->delete($acl_node_path); + diag(sprintf('unable to delete node with empty ACL %s: %d, %s', + $acl_node_path, $zkh->get_error(), $!)) unless ($ret); + } + + + ## set_acl() + + SKIP: { + skip 'ZooKeeper skipping ACLs', 2 unless (!$skip_acl); + + my $ret = $zkh->set_acl($acl_node_path, [ + { + 'perms' => ZOO_PERM_CREATE, + 'scheme' => 'foo', + 'id' => 'bar' + } + ]); + ok((!$ret and $zkh->get_error() == ZINVALIDACL and $! eq ''), + 'set_acl(): invalid ACL'); + + push @{$digest_acl}, { + 'perms' => (ZOO_PERM_CREATE | ZOO_PERM_DELETE), + 'scheme' => 'ip', + 'id' => '0.0.0.0' + }; + + $ret = $zkh->set_acl($acl_node_path, $digest_acl); + ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''), + 'set_acl(): ACL unchanged if no auth'); + } + + + ## add_auth(), set_acl() + + $ret = $zkh->add_auth('digest', ''); + ok($ret, + 'add_auth(): empty digest cert'); + + SKIP: { + skip 'ZooKeeper skipping ACLs', 1 unless (!$skip_acl); + + my $ret = $zkh->set($acl_node_path, 'foo'); + ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''), + 'set(): node value unchanged if no auth'); + } + + $ret = $zkh->add_auth('digest', "$username:$password"); + ok($ret, + 'add_auth(): valid digest cert'); + + SKIP: { + skip 'ZooKeeper skipping ACLs', 13 unless (!$skip_acl); + + my $ret = $zkh->set($acl_node_path, 'baz'); + ok($ret, + 'set(): set node value with auth'); + + my $node = $zkh->get($acl_node_path); + is($node, 'baz', + 'get(): retrieved node value with auth'); + + $ret = $zkh->set_acl($acl_node_path, $digest_acl); + ok($ret, + 'set_acl(): set digest ACL with auth'); + + my $stat = $zkh->stat(); + + my @acl = ('abc'); + @acl = $zkh->get_acl($acl_node_path, 'stat' => $stat); + is_deeply(\@acl, $digest_acl, + 'get_acl(): retrieved digest ACL with auth'); + + is($stat->{'data_len'}, 3, + 'get_acl(): retrieved digest ACL with stat handle and auth'); + + SKIP: { + skip 'invalid node data', 2 unless ($stat->{'version'} == 1); + + my $ret = $zkh->set_acl($acl_node_path, $digest_acl, + 'version' => $stat->{'version'}); + ok($ret, + 'set_acl(): set digest ACL with matching version with auth'); + + $ret = $zkh->set_acl($acl_node_path, $digest_acl, + 'version' => $stat->{'version'}); + ok((!$ret and $zkh->get_error() == ZBADVERSION and $! eq ''), + 'set_acl(): ACL unchanged if non-matching version'); + } + + my $child_node_path = "$acl_node_path/c1"; + + my $path = $zkh->create($child_node_path, 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE); + ok((!defined($path) and $zkh->get_error() == ZNOAUTH and $! eq ''), + 'create(): undef when attempting to create node if no auth'); + + $digest_acl->[1]->{'perms'} |= ZOO_PERM_CREATE; + $digest_acl->[2]->{'perms'} &= ~ZOO_PERM_CREATE; + + $ret = $zkh->set_acl($acl_node_path, $digest_acl); + ok($ret, + 'set_acl(): set changed digest ACL with auth'); + + $path = $zkh->create($child_node_path, 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE); + is($path, $child_node_path, + 'create(): created node with auth'); + + $ret = $zkh->delete($child_node_path); + ok((!$ret and $zkh->get_error() == ZNOAUTH and $! eq ''), + 'delete(): no deletion of node if no auth'); + + $digest_acl->[1]->{'perms'} |= ZOO_PERM_DELETE; + pop @{$digest_acl}; + + $ret = $zkh->set_acl($acl_node_path, $digest_acl); + ok($ret, + 'set_acl(): set reduced digest ACL with auth'); + + $ret = $zkh->delete($child_node_path); + ok($ret, + 'delete(): deleted node with auth'); + } + + + ## cleanup + + $ret = $zkh->delete($acl_node_path); + diag(sprintf('unable to delete node with digest auth ACL %s: %d, %s', + $acl_node_path, $zkh->get_error(), $!)) unless ($ret); + + $ret = $zkh->delete($node_path); + diag(sprintf('unable to delete node %s: %d, %s', + $node_path, $zkh->get_error(), $!)) unless ($ret); +} + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + my $ret = $zkh->exists($root_path) if (defined($zkh)); + + skip 'no connection to ZooKeeper', 1 unless + (defined($ret) and $ret); + + + ## add_auth() + + $ret = $zkh->add_auth('foo', 'bar'); + my $err = $zkh->get_error(); + ok((!$ret and + ($err == ZAUTHFAILED or + $err == ZCONNECTIONLOSS or + $err == ZSESSIONEXPIRED) + and $! eq ''), + 'set_acl(): invalid scheme'); +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/60_watch.t b/zookeeper-contrib/zookeeper-contrib-zkperl/t/60_watch.t new file mode 100644 index 0000000..7d30602 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/60_watch.t @@ -0,0 +1,304 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +use File::Spec; +use Test::More tests => 30; + +BEGIN { use_ok('Net::ZooKeeper', qw(:all)) }; + + +my $test_dir; +(undef, $test_dir, undef) = File::Spec->splitpath($0); +require File::Spec->catfile($test_dir, 'util.pl'); + +my($hosts, $root_path, $node_path) = zk_test_setup(0); + + +SKIP: { + my $zkh = Net::ZooKeeper->new($hosts); + + my $path = $zkh->create($node_path, 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE) if (defined($zkh)); + + skip 'no connection to ZooKeeper', 20 unless + (defined($path) and $path eq $node_path); + + + ## exists() + + $zkh->{'watch_timeout'} = 100; + + my $watch = $zkh->watch(); + + my $ret = $zkh->exists($node_path, 'watch' => $watch); + ok($ret, + 'exists(): checked node existence with watch handle'); + + $ret = $watch->wait(); + ok(!$ret, + 'wait(): watch after checking node existence timed out'); + + $ret = $zkh->exists($node_path, 'watch' => $watch); + ok($ret, + 'exists(): checked node existence with renewed watch handle'); + + $ret = $watch->wait(); + ok(!$ret, + 'wait(): watch after checking node existence timed out with ' . + 'renewed watch handle'); + + undef $watch; + ok(!defined($watch), + 'undef: released watch handle'); + + my $pending_watches = $zkh->{'pending_watches'}; + is($pending_watches, 2, + '_zk_release_watches(): report pending watches'); + + + ## get_children() + + $watch = $zkh->watch('timeout' => 50); + + my $num_children = $zkh->get_children($node_path, 'watch' => $watch); + ok((defined($num_children) and $num_children == 0), + 'get_children(): retrieved zero count of child nodes with ' . + 'watch handle'); + + $ret = $watch->wait(); + ok(!$ret, + 'wait(): watch after retrieving child nodes timed out with ' . + 'watch handle'); + + $watch->{'timeout'} = 100; + + my @child_paths = $zkh->get_children($node_path, 'watch' => $watch); + ok((@child_paths == 0), + 'get_children(): retrieved empty list of child nodes with ' . + 'renewed watch handle'); + + $ret = $watch->wait(); + ok(!$ret, + 'wait(): watch after retrieving child nodes timed out with ' . + 'renewed watch handle'); + + $pending_watches = $zkh->{'pending_watches'}; + is($pending_watches, 4, + '_zk_release_watches(): report pending watches'); + + + ## get() + + $watch = $zkh->watch(); + + my $node = $zkh->get($node_path, 'watch' => $watch); + is($node, 'foo', + 'get(): retrieved node value with watch handle'); + + $ret = $watch->wait('timeout' => 0); + ok(!$ret, + 'wait(): watch after retrieving node value timed out with ' . + 'watch handle'); + + $node = $zkh->get($node_path, 'watch' => $watch); + is($node, 'foo', + 'get(): retrieved node value with renewed watch handle'); + + $ret = $watch->wait(); + ok(!$ret, + 'wait(): watch after retrieving node value timed out with ' . + 'renewed watch handle'); + + $pending_watches = $zkh->{'pending_watches'}; + is($pending_watches, 6, + '_zk_release_watches(): all watches pending'); + + + ## _zk_release_watches() + + $ret = $zkh->DESTROY(); + ok($ret, + 'DESTROY(): destroyed handle with pending watches'); + + my $event = $watch->{'event'}; + is($event, 0, + '_zk_release_watches(): watch not destroyed when tied to watch handle'); + + $zkh = Net::ZooKeeper->new($hosts); + + SKIP: { + my $ret = $zkh->exists($node_path, 'watch' => $watch); + + skip 'no connection to ZooKeeper', 2 unless + (defined($ret) and $ret); + + ok($ret, + 'exists(): checked node existence with renewed watch handle ' . + 'from prior connection'); + + $ret = $watch->wait(); + ok(!$ret, + 'wait(): watch after checking node existence timed out with ' . + 'renewed watch handle from prior connection'); + + + } +} + +my $pid = fork(); + +SKIP: { + skip 'unable to fork', 4 unless (defined($pid)); + + my $zkh = Net::ZooKeeper->new($hosts); + + my $ret = $zkh->exists($node_path) if (defined($zkh)); + + if ($pid == 0) { + ## child process + + my $code = 0; + + if (defined($ret) and $ret) { + sleep(1); + + my $ret = $zkh->set($node_path, 'foo'); + + diag(sprintf('set(): failed in child process: %d, %s', + $zkh->get_error(), $!)) unless ($ret); + + $code = !$ret; + + sleep(1); + + my $path = $zkh->create("$node_path/c", 'foo', + 'acl' => ZOO_OPEN_ACL_UNSAFE); + + diag(sprintf('create(): failed in child process: %d, %s', + $zkh->get_error(), $!)) unless + (defined($path) and $path eq "$node_path/c"); + + $code &= !$ret; + + sleep(1); + + $ret = $zkh->delete("$node_path/c"); + + diag(sprintf('delete(): failed in child process: %d, %s', + $zkh->get_error(), $!)) unless ($ret); + + $code &= !$ret; + + sleep(1); + + $ret = $zkh->set($node_path, 'foo'); + + diag(sprintf('set(): failed in child process: %d, %s', + $zkh->get_error(), $!)) unless ($ret); + + $code &= !$ret; + } + + exit($code); + } + else { + ## parent process + + SKIP: { + skip 'no connection to ZooKeeper', 9 unless + (defined($ret) and $ret); + + my $watch = $zkh->watch('timeout' => 5000); + + + ## wait() + + my $ret = $zkh->exists($node_path, 'watch' => $watch); + ok($ret, + 'exists(): checked node existence with watch handle ' . + 'in parent'); + + $ret = $watch->wait(); + ok(($ret and $watch->{'event'} == ZOO_CHANGED_EVENT and + $watch->{'state'} == ZOO_CONNECTED_STATE), + 'wait(): waited for event after checking node existence'); + + my $num_children = $zkh->get_children($node_path, + 'watch' => $watch); + ok((defined($num_children) and $num_children == 0), + 'get_children(): retrieved zero count of child nodes with ' . + 'watch handle in parent'); + + $ret = $watch->wait(); + ok(($ret and $watch->{'event'} == ZOO_CHILD_EVENT and + $watch->{'state'} == ZOO_CONNECTED_STATE), + 'wait(): waited for create child event after ' . + 'retrieving child nodes'); + + my @child_paths = $zkh->get_children($node_path, + 'watch' => $watch); + ok((@child_paths == 1 and $child_paths[0] eq 'c'), + 'get_children(): retrieved list of child nodes with ' . + 'watch handle in parent'); + + $ret = $watch->wait(); + ok(($ret and $watch->{'event'} == ZOO_CHILD_EVENT and + $watch->{'state'} == ZOO_CONNECTED_STATE), + 'wait(): waited for delete child event after ' . + 'retrieving child nodes'); + + my $node = $zkh->get($node_path, 'watch' => $watch); + is($node, 'foo', + 'get(): retrieved node value with watch handle in parent'); + + $ret = $watch->wait(); + ok(($ret and $watch->{'event'} == ZOO_CHANGED_EVENT and + $watch->{'state'} == ZOO_CONNECTED_STATE), + 'wait(): waited for event after retrieving node value'); + + undef $watch; + + my $pending_watches = $zkh->{'pending_watches'}; + is($pending_watches, 0, + '_zk_release_watches(): no watches pending'); + } + + my $reap = waitpid($pid, 0); + + diag(sprintf('child process failed: exit %d, signal %d%s', + ($? >> 8), ($? & 127), + (($? & 128) ? ', core dump' : ''))) if + ($reap == $pid and $? != 0); + } +} + + +## cleanup + +{ + my $zkh = Net::ZooKeeper->new($hosts); + + my $ret = $zkh->exists($node_path) if (defined($zkh)); + + if (defined($ret) and $ret) { + $ret = $zkh->delete($node_path); + diag(sprintf('unable to delete node %s: %d, %s', + $node_path, $zkh->get_error(), $!)) unless ($ret); + } +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/t/util.pl b/zookeeper-contrib/zookeeper-contrib-zkperl/t/util.pl new file mode 100644 index 0000000..1ca738d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/t/util.pl @@ -0,0 +1,62 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +sub zk_test_setup +{ + my $verbose = shift; + + $SIG{'PIPE'} = 'IGNORE'; + + my $hosts = $ENV{'ZK_TEST_HOSTS'}; + unless (defined($hosts) and $hosts =~ /\S/) { + $hosts = 'localhost:0'; + diag('no ZooKeeper hostnames specified in ZK_TEST_HOSTS env var, ' . + "using $hosts") if ($verbose); + } + + my $root_path = $ENV{'ZK_TEST_PATH'}; + if (defined($root_path) and $root_path =~ /^\//) { + $root_path =~ s/\/+/\//g; + $root_path =~ s/\/$//; + } + else { + $root_path = '/'; + diag('no ZooKeeper path specified in ZK_TEST_PATH env var, ' . + 'using root path') if ($verbose); + } + + my $node_path = $root_path . (($root_path =~ /\/$/) ? '' : '/') . + '_net_zookeeper_test'; + + return ($hosts, $root_path, $node_path); +} + +sub zk_acl_test_setup +{ + my $username = '_net_zookeeper_test'; + + my $password = 'test'; + + ## digest is Base64-encoded SHA1 digest of username:password + my $digest = '2qi7Erp2cXYLGcQbXADiwUFaOGo='; + + return ($username, $password, $digest); +} + +1; + diff --git a/zookeeper-contrib/zookeeper-contrib-zkperl/typemap b/zookeeper-contrib/zookeeper-contrib-zkperl/typemap new file mode 100644 index 0000000..84636fd --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkperl/typemap @@ -0,0 +1,38 @@ +# Net::ZooKeeper - Perl extension for Apache ZooKeeper +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +TYPEMAP +Net::ZooKeeper T_ZK_HANDLE +Net::ZooKeeper::Stat T_ZK_HANDLE +Net::ZooKeeper::Watch T_ZK_HANDLE + +INPUT +T_ZK_HANDLE + if (SvROK($arg) && SvTYPE(SvRV($arg)) == SVt_PVHV && + sv_derived_from($arg, \"${ntype}\")) { + $var = (HV*) SvRV($arg); + } + else { + Perl_croak(aTHX_ + \"$var is not a hash reference of type ${ntype}\"); + } + +OUTPUT +T_ZK_HANDLE + NOT_IMPLEMENTED + diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/README b/zookeeper-contrib/zookeeper-contrib-zkpython/README new file mode 100644 index 0000000..ffad255 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/README @@ -0,0 +1,115 @@ +Early version of ZooKeeper bindings for Python. All functions are imported as methods into the zookeeper module. + +Please do not rely on APIs staying constant in the short term. The handling of exceptions and failure modes is one area that is subject to change. + +DEPENDENCIES: +------------- + +This has only been tested against SVN/Git (i.e. 3.2.0 in development) but should work against 3.1.1. + +You will need the Python development headers installed to build the module - on many package-management systems, these can be found in python-devel. (On ubuntu 18.4, install python2.7 and python2.7-dev.) + +Python >= 2.6 is required. We have tested against 2.6 and 3.5+. + +By default, the extension assumes that the C client library was compiled with OpenSSL enabled (--with-openssl). You can disable OpenSSL support in the Python binding by setting the ZKPYTHON_NO_SSL environment variable to a non-empty string before executing Ant or setup.py. + +E.g. setting up python and python devel on ubuntu 18.4: +sudo apt-get install python2.7 python2.7-dev +sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 + +BUILD AND INSTALL: +------------------- + +To install, make sure that the C client has been built (use `mvn clean install -DskipTests -Pfull-build` in the root folder of zookeeper) or that the zookeeper C libraries are installed in /usr/local/lib (or change this directory in setup.py). Then run: + +ant install + +from zookeeper-contrib/zookeeper-contrib-zkpython/. + +To test, run ant test from the same directory. + +You can compile the module without installing by running + +ant compile + +In order to use the module, zookeeper.so must be in your PYTHONPATH or in one of the directories referenced by sys.path. Running ant install should make sure that this is the case, but if you only run ant compile you probably need to add build/contrib/zkpython/* to PYTHONPATH to find the module. The C client libraries must be in a system library path, or LD_LIBRARY_PATH or DYLD_LIBRARY_PATH (Mac OS) for the module to work correctly, otherwise you will see a library not found error when trying to import the module. + +NAMING CONVENTIONS: +-------------------- + +All methods that in the C library are zoo_fn_name have been implemented as zookeeper.fn_name. The exception is any function that has a watch function argument is named without the 'w' prefix (for example, zoo_wexists becomes zookeeper.exists). The variants of these functions without the watch argument (i.e. zoo_exists) have not been implemented on the understanding that they are superseded by the zoo_w* API. + +Enums and integer constants that begin ZOO_int_name are named as zookeeper.int_name. + +PARAMETER CHANGES: +------------------ + +Zookeeper handles are represented as integers to avoid marshalling the entire structure for every call. Therefore they are opaque from Python. + +Any parameter that is used to provide arguments to callback methods is not exposed in the API. Python provides better mechanisms for providing a closure to be called in the future. + +Every callback gets passed the handle of the ZooKeeper instance used to register the callback. + +DATA TYPES: +----------- + +ACL_vectors are lists of dictionaries. Stat structures are dictionaries. String_vectors are lists of strings. + +EXCEPTIONS AND ERROR HANDLING: +------------------------------ + +Currently synchronous calls indicate failure by throwing an exception (note that this includes the synchronous calls to set up asynchronous completion callbacks!). Success is returned as an integer. + +Callbacks signify failure by having the integer response code passed in. + +WHAT'S NEW IN 0.4: +------------------ + +More test coverage. + +Better reference counting, fixing at least two serious bugs. + +Out-of-range zhandles are now checked, fixing a potential security hole. + +Docstrings! Editing and cleanup required, but most of the text is there. + +zookeeper.set_watcher is now implemented correctly. + +zookeeper.client_id is now implemented correctly. zookeeper.init now respects the client_id parameter. + +get_context and set_context have been removed from the API. The context mechanism is used by PyZK to store the callables that are dispatched by C-side watchers. Messing with this from Python-side causes bugs very quickly. You should wrap all desired context up in a callable and then use zookeeper.set_watcher to attach it to the global watcher. + +Many methods now have optional parameters (usually if you can specify a watch, it's optional). The only time where genuinely optional parameters are still mandatory is when a required parameters comes after it. Currently we still respect the ZK C client parameter ordering. For example, you can simply connect with zookeeper.init("host:port") and ignore the other three parameters. + + +WHAT'S NEW IN 0.3: +------------------ + +Some tests in zkpython/test. More to follow! + +A variety of bugfixes. + +Changed the way methods return results - all responses are integers now, for the client to convert to a string if it needs. + +WHAT'S NEW IN 0.2: +------------------ + +The asynchronous API is now implemented (see zookeeper.a*). + +Most enums defined in zookeeper.h are now added as constants to the module. + +_set2 and a few other edge API calls have been implemented. The module is now nearly 100% feature complete! + +A reference count error was tracked down and killed. More probably lurk in there! + +WHAT'S NOT DONE / KNOWN ISSUES / FUTURE WORK: +--------------------------------------------- + +1. There may well be more memory leaks / reference count issues; however I am more confident that common paths are relatively safe. +2. There probably needs to be a more Pythonic Python-side wrapper for these functions (e.g. a zookeeper object, the ability to iterate through a tree of zk nodes) +3. Docstrings need a cleanup. +4. The way exceptions and error codes are returned needs looking at. Currently synchronous calls throw exceptions on everything but ZOK return, but asynchronous completions are simply passed the error code. Async. functions should never throw an exception on the C-side as they are practically impossible to catch. For the sync. functions, exceptions seem more reasonable, but some cases are certainly not exceptional. + +Bug reports / comments very welcome! + +Henry Robinson henry@cloudera.com diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/build.xml b/zookeeper-contrib/zookeeper-contrib-zkpython/build.xml new file mode 100644 index 0000000..99f634a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/build.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/ivy.xml b/zookeeper-contrib/zookeeper-contrib-zkpython/ivy.xml new file mode 100644 index 0000000..f8bad40 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/ivy.xml @@ -0,0 +1,35 @@ + + + + + + + + ZKPython + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/c/pyzk_docstrings.h b/zookeeper-contrib/zookeeper-contrib-zkpython/src/c/pyzk_docstrings.h new file mode 100644 index 0000000..2e6fd1e --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/c/pyzk_docstrings.h @@ -0,0 +1,621 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef PYZK_DOCSTRINGS_H +#define PYZK_DOCSTRINGS_H + +const char pyzk_acreate_doc[] = +"Create a node asynchronously.\n" +"\n" +"This method will create a node in ZooKeeper. A node can only be created if\n" +"it does not already exists. The Create Flags affect the creation of nodes.\n" +"If EPHEMERAL flag is set, the node will automatically get removed if the\n" +"client session goes away. If the SEQUENCE flag is set, a unique\n" +"monotonically increasing sequence number is appended to the path name.\n" +"\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: The name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" value: The data to be stored in the node.\n" +" acl: The initial ACL of the node. If None, the ACL of the parent will be\n" +" used.\n" +"\n" +" (Subsequent parameters are optional)\n" +" flags: this parameter can be set to 0 for normal create or an OR\n" +" of the Create Flags\n" +" completion: the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the parent node does not exist.\n" +"NODEEXISTS the node already exists\n" +"NOAUTH the client does not have permission.\n" +"NOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes.\n" +"\n" +"RETURNS:\n" +"Returns OK on success or throws of the following errcodes on failure:\n" +"EXCEPTIONS:\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +"MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +static const char pyzk_client_id_doc[] = +"Return the client session id, only valid if the connections\n" +" is currently connected (ie. last watcher state is CONNECTED_STATE)"; + +static const char pyzk_state_doc[] = +"Get the state of the zookeeper connection.\n" + "The return value will be one of the State Consts."; + +static const char pyzk_adelete_doc[] = +" Delete a node in zookeeper.\n" +"\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" version: the expected version of the node. The function will fail if the\n" +" actual version of the node does not match the expected version.\n" +" If -1 is used the version check will not take place. \n" +" completion: the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADVERSION expected version does not match actual version.\n" +"NOTEMPTY children are present; node cannot be deleted.\n" +"Returns OK on success or one of the following errcodes on failure:\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +static const char pyzk_aexists_doc[] = +" checks the existence of a node in zookeeper.\n" +"\n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" watch: if not None, a watch will be set at the server to notify the \n" +"client if the node changes. The watch will be set even if the node does not \n" +"exist. This allows clients to watch for nodes to appear.\n" +"\n" +" completion: the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +" OK operation completed successfully\n" +" NONODE the node does not exist.\n" +" NOAUTH the client does not have permission.\n" +" data the data that will be passed to the completion routine when the \n" +"function completes.\n" +" OK on success or one of the following errcodes on failure:\n" +" BADARGUMENTS - invalid input parameters\n" +" INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +" MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +static const char pyzk_aget_doc[] = +"Gets the data associated with a node.\n" +"\n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" watcher if not None, a watch will be set at the server to notify \n" +"the client if the node changes.\n" +" completion: the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +" OK operation completed successfully\n" +" NONODE the node does not exist.\n" +" NOAUTH the client does not have permission.\n" +" data the data that will be passed to the completion routine when \n" +"the function completes.\n" +"Returns OK on success or one of the following errcodes on failure:\n" +" BADARGUMENTS - invalid input parameters\n" +" INVALIDSTATE - zhandle state is either in SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + " MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +static const char pyzk_aset_doc[] = +" Sets the data associated with a node.\n" +"\n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" buffer the buffer holding data to be written to the node.\n" +" buflen the number of bytes from buffer to write.\n" +"\n" +"(Subsequent parameters are optional)\n" +" version the expected version of the node. The function will fail if \n" +"the actual version of the node does not match the expected version. If -1 is \n" +"used the version check will not take place.\n" +"completion: If None, \n" +"the function will execute synchronously. Otherwise, the function will return \n" +"immediately and invoke the completion routine when the request completes.\n" +" completion the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADVERSION expected version does not match actual version.\n" +" data the data that will be passed to the completion routine when \n" +"the function completes.\n" +"Returns OK on success or one of the following errcodes on failure:\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +"MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +static const char pyzk_aget_children_doc[] = +" Lists the children of a node.\n" +"\n" +"This function is similar to zoo_aget_children except it allows one specify \n" +"a watcher object rather than a boolean watch flag.\n" +" \n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" watcher if non-null, a watch will be set at the server to notify \n" +"the client if the node changes.\n" +"\n" +" completion the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"\n" +"Returns OK on success or one of the following errcodes on failure:\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +"MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +static const char pyzk_async_doc[] = +" Flush leader channel.\n" +"\n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes\n" +"separating ancestors of the node.\n" +" completion the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"\n" +"Returns OK on success or one of the following errcodes on failure:\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +const static char pyzk_aget_acl_doc[] = +" Gets the acl associated with a node.\n" +"\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" completion: the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"\n" +"Returns:\n" +" OK on success or one of the following errcodes on failure:\n" +" BADARGUMENTS - invalid input parameters\n" +" INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +" MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +const char pyzk_aset_acl_doc[] = +" Sets the acl associated with a node.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" buffer: the buffer holding the acls to be written to the node.\n" +" completion: the routine to invoke when the request completes. The completion\n" +"will be triggered with one of the following codes passed in as the rc argument:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"INVALIDACL invalid ACL specified\n" +"BADVERSION expected version does not match actual version.\n" +"" +" Returns OK on success or one of the following errcodes on failure:\n" +" BADARGUMENTS - invalid input parameters\n" +" INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +" MARSHALLINGERROR - failed to marshall a request; possibly, out of memory"; + +const char pyzk_zerror_doc[] = +"Returns an error string corresponding to an integer error code.\n" +"\n" +"PARAMETERS:\n" +" err: Error code\n" +"RETURNS:\n" + " string corresponding to the return code\n"; + +const char pyzk_add_auth_doc[] = +" specify application credentials.\n" +"\n" +"The application calls this function to specify its credentials for purposes\n" +"of authentication. The server will use the security provider specified by \n" +"the scheme parameter to authenticate the client connection. If the \n" +"authentication request has failed:\n" +"- the server connection is dropped\n" +"- the watcher is called with the AUTH_FAILED_STATE value as the state \n" +"parameter.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" scheme the id of authentication scheme. Natively supported:\n" +"'digest' password-based authentication\n" +" cert: application credentials. The actual value depends on the scheme.\n" +" completion: the routine to invoke when the request completes. One of \n" +"the following result codes may be passed into the completion callback:\n" +"OK operation completed successfully\n" +"AUTHFAILED authentication failed \n" +"\n" +"RETURNS:\n" +"OK on success or one of the following errcodes on failure:\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" +"MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n" + "SYSTEMERROR - a system error occurred\n"; + +const char pyzk_is_unrecoverable_doc[] = +" checks if the current zookeeper connection state can't be recovered.\n" +"\n" +" The application must close the zhandle and try to reconnect.\n" +"\n" +"PARAMETERS:\n" +" zh the zookeeper handle (see zookeeper.init)\n" +"\n" +"RETURNS:\n" + "True if connection is unrecoverable, otherwise False\n"; + +const char pyzk_set_debug_level_doc[] = +"\brief sets the debugging level for the library \n" +"\n" +"PARAMETERS:\n" +" logLevel: One of LOG_LEVEL_ERROR, LOG_LEVEL_WARN, LOG_LEVEL_INFO or LOG_LEVEL_DEBUG\n" +"\n" +"RETURNS:\n" + " None\n"; + +static const char pyzk_set_log_stream_doc[] = +" sets the stream to be used by the library for logging \n" +"\n" +"The zookeeper library uses stderr as its default log stream. Applications\n" +"must make sure the stream is writable. Passing in NULL resets the stream \n" + "to its default value (stderr).\n" +"\n" +"PARAMETERS:\n" +" logStream: a writable file object\n" +"RETURNS:\n" +" None\n"; + +static const char pyzk_deterministic_conn_order_doc[] = +" enable/disable quorum endpoint order randomization\n" +"\n" +"If passed a non-zero value, will make the client connect to quorum peers\n" +"in the order as specified in the zookeeper.init() call.\n" +"A zero value causes zookeeper.init() to permute the peer endpoints\n" +"which is good for more even client connection distribution among the \n" +"quorum peers.\n" +"PARAMETERS:\n" +" yesOrNo\n" +"\n" +"RETURNS:\n" + " None\n"; + +static const char pyzk_create_doc[] = +" create a node synchronously.\n" +"\n" +"This method will create a node in ZooKeeper. A node can only be created if\n" +"it does not already exists. The Create Flags affect the creation of nodes.\n" +"If the EPHEMERAL flag is set, the node will automatically get removed if the\n" +"client session goes away. If the SEQUENCE flag is set, a unique\n" +"monotonically increasing sequence number is appended to the path name.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: The name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" value: The data to be stored in the node.\n" +" acl: The initial ACL of the node. If null, the ACL of the parent will be\n" +" used.\n" +" flags: this parameter can be set to 0 for normal create or an OR\n" +" of the Create Flags\n" +" realpath: the real path that is created (this might be different than the\n" +" path to create because of the SEQUENCE flag.\n" +" the maximum length of real path you would want.\n" +"\n" +"RETURNS:\n" +" The actual znode path that was created (may be different from path due to use of SEQUENTIAL\n" +" flag).\n" +"EXCEPTIONS:\n" +" NONODE the parent node does not exist.\n" +" NODEEXISTS the node already exists\n" +" NOAUTH the client does not have permission.\n" +" NOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes.\n" +" BADARGUMENTS - invalid input parameters\n" +" INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + " MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +static const char pyzk_delete_doc[] = +" delete a node in zookeeper synchronously.\n" +"\n" +"PARAMETERS:\n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" version: the expected version of the node. The function will fail if the\n" +" actual version of the node does not match the expected version.\n" +" If -1 (the default) is used the version check will not take place. \n" +"\n" +"RETURNS:\n" +"One of the following values is returned.\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADVERSION expected version does not match actual version.\n" +"NOTEMPTY children are present; node cannot be deleted.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +static const char pyzk_exists_doc[] = +" checks the existence of a node in zookeeper synchronously.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(Subsequent parameters are optional)\n" +" watch: if nonzero, a watch will be set at the server to notify the \n" +"client if the node changes. The watch will be set even if the node does not \n" +"exist. This allows clients to watch for nodes to appear.\n" +"\n" +"RETURNS:\n" +" the return stat value of the node.\n" +"EXCEPTIONS:\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + + +static const char pyzk_get_children_doc[] = +" lists the children of a node synchronously.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(subsequent parameters are optional)\n" +" watcher: if non-null, a watch will be set at the server to notify \n" +"the client if the node changes.\n" +"\n" +"RETURNS:\n" +" A list of znode names\n" +"EXCEPTIONS:\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +static const char pyzk_set_doc[] = +"\n" +" sets the data associated with a node. See set2 function if\n" +"you require access to the stat information associated with the znode.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" buffer: the buffer holding data to be written to the node.\n" +"\n" +"(subsequent parameters are optional)\n" +" version: the expected version of the node. The function will fail if \n" +"the actual version of the node does not match the expected version. If -1 is \n" +"used the version check will not take place. \n" +"\n" +"RETURNS:\n" +" the return code for the function call.\n" +"OK operation completed successfully\n" +"EXCEPTIONS:\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADVERSION expected version does not match actual version.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +static const char pyzk_get_acl_doc[] = +" gets the acl associated with a node synchronously.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" acl: the return value of acls on the path.\n" +"RETURNS:" +" returns the stat of the path specified.\n" +"EXCEPTIONS:" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + + +static const char pyzk_set_acl_doc[] = +" sets the acl associated with a node synchronously.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" version: the expected version of the path.\n" +" acl: the acl to be set on the path. \n" +"\n" +"RETURNS:\n" +"OK operation completed successfully\n" +"EXCEPTIONS:\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"INVALIDACL invalid ACL specified\n" +"BADVERSION expected version does not match actual version.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +static const char pyzk_close_doc[] = +" close the zookeeper handle and free up any resources.\n" +"\n" +"After this call, the client session will no longer be valid. The function\n" +"will flush any outstanding send requests before return. As a result it may \n" +"block.\n" +"\n" +"This method should only be called only once on a zookeeper handle. Calling\n" +"twice will cause undefined (and probably undesirable behavior).\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +"RETURNS:\n" +"Regardless of the error code returned, the zhandle \n" +"will be destroyed and all resources freed. \n" +"OK - success\n" +"EXCEPTIONS:\n" +"BADARGUMENTS - invalid input parameters\n" +"MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n" +"OPERATIONTIMEOUT - failed to flush the buffers within the specified timeout.\n" +"CONNECTIONLOSS - a network error occurred while attempting to send request to server\n" + "SYSTEMERROR -- a system (OS) error occurred; it's worth checking errno to get details\n"; + +static const char pyzk_set2_doc[] = +"\n" +" sets the data associated with a node, and returns the associated stat structure.\n" +"\n" +"PARAMETERS:\n" +" zh: the zookeeper handle obtained by a call to zookeeper.init\n" +" path: the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +" buffer: the buffer holding data to be written to the node.\n" +"\n" +"(subsequent parameters are optional)\n" +" version: the expected version of the node. The function will fail if \n" +"the actual version of the node does not match the expected version. If -1 is \n" +"used the version check will not take place. \n" +"\n" +"RETURNS:\n" +" the stat structure for the target znode\n" +"OK operation completed successfully\n" +"EXCEPTIONS:\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADVERSION expected version does not match actual version.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +static const char pyzk_init_doc[] = +"This method creates a new handle and a zookeeper session that corresponds\n" +"to that handle. Session establishment is asynchronous, meaning that the\n" +"session should not be considered established until (and unless) an\n" +"event of state CONNECTED_STATE is received.\n" +"PARAMETERS:\n" +" host: comma separated host:port pairs, each corresponding to a zk\n" +" server. e.g. '127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002'\n" +"\n" +"(subsequent parameters are optional)\n" +" fn: the global watcher callback function. When notifications are\n" +" triggered this function will be invoked.\n" +" recv_timeout: \n" +" (clientid, passwd)\n" +" clientid the id of a previously established session that this\n" +" client will be reconnecting to. Clients can access the session id of an established, valid,\n" +" connection by calling zoo_client_id. If\n" +" the specified clientid has expired, or if the clientid is invalid for \n" +" any reason, the returned zhandle_t will be invalid -- the zhandle_t \n" +" state will indicate the reason for failure (typically\n" +" EXPIRED_SESSION_STATE).\n" +"\n" +"RETURNS:\n" +" an integer handle. If it fails to create \n" +" a new zhandle the function throws an exception.\n"; + +static const char pyzk_init_ssl_doc[] = +"This method creates a new handle and a zookeeper SSL session that corresponds\n" +"to that handle. Session establishment is asynchronous, meaning that the\n" +"session should not be considered established until (and unless) an\n" +"event of state CONNECTED_STATE is received.\n" +"PARAMETERS:\n" +" host: comma separated host:port pairs, each corresponding to a zk\n" +" server. e.g. '127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002'\n" +" cert_str: SSL certificate string e.g. 'server.cert,client.cert,client-priv-key.pom,passwd'\n" +"\n" +"(subsequent parameters are optional)\n" +" fn: the global watcher callback function. When notifications are\n" +" triggered this function will be invoked.\n" +" recv_timeout: \n" +" (clientid, passwd)\n" +" clientid the id of a previously established session that this\n" +" client will be reconnecting to. Clients can access the session id of an established, valid,\n" +" connection by calling zoo_client_id. If\n" +" the specified clientid has expired, or if the clientid is invalid for \n" +" any reason, the returned zhandle_t will be invalid -- the zhandle_t \n" +" state will indicate the reason for failure (typically\n" +" EXPIRED_SESSION_STATE).\n" +"\n" +"RETURNS:\n" +" an integer handle. If it fails to create \n" +" a new zhandle the function throws an exception.\n"; + +static const char pyzk_get_doc[] = +" gets the data associated with a node synchronously.\n" +"\n" +"\n" +"PARAMETERS:\n" +" zh the zookeeper handle obtained by a call to zookeeper.init\n" +" path the name of the node. Expressed as a file name with slashes \n" +"separating ancestors of the node.\n" +"\n" +"(subsequent parameters are optional)\n" +" watcher if not None, a watch will be set at the server to notify \n" +" the client if the node changes.\n" +" bufferlen: This value defaults to 1024*1024 - 1Mb. This method returns \n" +" the minimum of bufferlen and the true length of the znode's data. \n" +"RETURNS:\n" +" the data associated with the node\n" +"OK operation completed successfully\n" +"NONODE the node does not exist.\n" +"NOAUTH the client does not have permission.\n" +"BADARGUMENTS - invalid input parameters\n" +"INVALIDSTATE - zhandle state is either in SESSION_EXPIRED_STATE or AUTH_FAILED_STATE\n" + "MARSHALLINGERROR - failed to marshall a request; possibly, out of memory\n"; + +#endif diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/c/zookeeper.c b/zookeeper-contrib/zookeeper-contrib-zkpython/src/c/zookeeper.c new file mode 100644 index 0000000..ee8a75a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/c/zookeeper.c @@ -0,0 +1,1702 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include + +////////////////////////////////////////////// +// EXCEPTIONS +PyObject *ZooKeeperException = NULL; +PyObject *SystemErrorException; +PyObject *RuntimeInconsistencyException; +PyObject *DataInconsistencyException; +PyObject *ConnectionLossException; +PyObject *MarshallingErrorException; +PyObject *UnimplementedException; +PyObject *OperationTimeoutException; +PyObject *BadArgumentsException; +PyObject *InvalidStateException; + +PyObject *ApiErrorException; +PyObject *NoNodeException; +PyObject *NoAuthException; +PyObject *NodeExistsException; +PyObject *BadVersionException; +PyObject *NoChildrenForEphemeralsException; +PyObject *NotEmptyException; +PyObject *SessionExpiredException; +PyObject *SessionMovedException; +PyObject *InvalidCallbackException; +PyObject *InvalidACLException; +PyObject *AuthFailedException; +PyObject *ClosingException; +PyObject *NothingException; + +PyObject *err_to_exception(int errcode) { + switch (errcode) { + case ZSYSTEMERROR: + return SystemErrorException; + case ZINVALIDSTATE: + return InvalidStateException; + case ZRUNTIMEINCONSISTENCY: + return RuntimeInconsistencyException; + case ZDATAINCONSISTENCY: + return DataInconsistencyException; + case ZCONNECTIONLOSS: + return ConnectionLossException; + case ZMARSHALLINGERROR: + return MarshallingErrorException; + case ZUNIMPLEMENTED: + return UnimplementedException; + case ZOPERATIONTIMEOUT: + return OperationTimeoutException; + case ZBADARGUMENTS: + return BadArgumentsException; + case ZAPIERROR: + return ApiErrorException; + case ZNONODE: + return NoNodeException; + case ZNOAUTH: + return NoAuthException; + case ZBADVERSION: + return BadVersionException; + case ZNOCHILDRENFOREPHEMERALS: + return NoChildrenForEphemeralsException; + case ZNODEEXISTS: + return NodeExistsException; + case ZINVALIDACL: + return InvalidACLException; + case ZAUTHFAILED: + return AuthFailedException; + case ZNOTEMPTY: + return NotEmptyException; + case ZSESSIONEXPIRED: + return SessionExpiredException; + case ZINVALIDCALLBACK: + return InvalidCallbackException; + case ZSESSIONMOVED: + return SessionMovedException; + case ZCLOSING: + return ClosingException; + case ZNOTHING: + return NothingException; + case ZOK: + default: + return NULL; + } +} + + +#define CHECK_ZHANDLE(z) if ( (z) < 0 || (z) >= num_zhandles) { \ + PyErr_SetString( ZooKeeperException, "zhandle out of range" ); \ +return NULL; \ +} else if ( zhandles[(z)] == NULL ) { \ + PyErr_SetString(ZooKeeperException, "zhandle already freed"); \ + return NULL; \ + } + +/* Contains all the state required for a watcher callback - these are + passed to the *dispatch functions as void*, cast to pywatcher_t and + then their callback member is invoked if not NULL */ +typedef struct { + int zhandle; + PyObject *callback; + int permanent; +}pywatcher_t; + +/* This array exists because we need to ref. count the global watchers + for each connection - but they're inaccessible without pulling in + zk_adaptor.h, which I'm trying to avoid. */ +static pywatcher_t **watchers; + +/* We keep an array of zhandles available for use. When a zhandle is + correctly closed, the C client frees the memory so we set the + zhandles[i] entry to NULL. This entry can then be re-used. */ +static zhandle_t** zhandles = NULL; +static int num_zhandles = 0; +static int max_zhandles = 0; +#define REAL_MAX_ZHANDLES 32768 + +/* -------------------------------------------------------------------------- */ +/* zhandles - unique connection ids - tracking */ +/* -------------------------------------------------------------------------- */ + + +/* Allocates an initial zhandle and watcher array */ +int init_zhandles(int num) { + zhandles = malloc(sizeof(zhandle_t*)*num); + watchers = malloc(sizeof(pywatcher_t*)*num); + if (zhandles == NULL || watchers == NULL) { + return 0; + } + max_zhandles = num; + num_zhandles = 0; + memset(zhandles, 0, sizeof(zhandle_t*)*max_zhandles); + return 1; +} + +/* Note that the following zhandle functions are not thread-safe. The + C-Python runtime does not seem to pre-empt a thread that is in a C + module, so there's no need for synchronisation. */ + +/* Doubles the size of the zhandle / watcher array Returns 0 if the + new array would be >= REAL_MAX_ZHANDLES in size. Called when zhandles + is full. Returns 0 if allocation failed or if max num zhandles + exceeded. */ +int resize_zhandles(void) { + zhandle_t **tmp = zhandles; + pywatcher_t ** wtmp = watchers; + if (max_zhandles >= REAL_MAX_ZHANDLES >> 1) { + return 0; + } + max_zhandles *= 2; + zhandles = malloc(sizeof(zhandle_t*)*max_zhandles); + if (zhandles == NULL) { + PyErr_SetString(PyExc_MemoryError, "malloc for new zhandles failed"); + return 0; + } + memset(zhandles, 0, sizeof(zhandle_t*)*max_zhandles); + memcpy(zhandles, tmp, sizeof(zhandle_t*)*max_zhandles/2); + + watchers = malloc(sizeof(pywatcher_t*)*max_zhandles); + if (watchers == NULL) { + PyErr_SetString(PyExc_MemoryError, "malloc for new watchers failed"); + return 0; + } + memset(watchers, 0, sizeof(pywatcher_t*)*max_zhandles); + memcpy(watchers, wtmp, sizeof(pywatcher_t*)*max_zhandles/2); + + free(wtmp); + free(tmp); + return 1; +} + +/* Find a free zhandle - this iterates through the list of open + zhandles, but we expect it to be infrequently called. There are + optimisations that can be made if this turns out to be problematic. + Returns -1 if no free handle is found - resize_handles() can be + called in that case. */ +unsigned int next_zhandle(void) { + int i = 0; + for (i=0;izhandle = zh; ret->callback = cb; ret->permanent = permanent; + return ret; +} + +/* Releases the reference taken in create_pywatcher to the callback, + then frees the allocated pywatcher_t* */ +void free_pywatcher(pywatcher_t *pw) +{ + if (pw == NULL) { + return; + } + Py_DECREF(pw->callback); + + free(pw); +} + +/* Constructs a new stat object. Returns Py_None if stat == NULL or a + dictionary containing all the stat information otherwise. In either + case, takes a reference to the returned object. */ +PyObject *build_stat( const struct Stat *stat ) +{ + if (stat == NULL) { + Py_INCREF(Py_None); + return Py_None; + } + return Py_BuildValue( "{s:K, s:K, s:K, s:K," + "s:i, s:i, s:i, s:K," + "s:i, s:i, s:K}", + "czxid", stat->czxid, + "mzxid", stat->mzxid, + "ctime", stat->ctime, + "mtime", stat->mtime, + "version", stat->version, + "cversion", stat->cversion, + "aversion", stat->aversion, + "ephemeralOwner", stat->ephemeralOwner, + "dataLength", stat->dataLength, + "numChildren", stat->numChildren, + "pzxid", stat->pzxid ); +} + +/* Creates a new list of strings from a String_vector. Returns the + empty list if the String_vector is NULL. Takes a reference to the + returned PyObject and gives that reference to the caller. */ +PyObject *build_string_vector(const struct String_vector *sv) +{ + PyObject *ret; + if (!sv) { + return PyList_New(0); + } + + ret = PyList_New(sv->count); + if (ret) { + int i; + for (i=0;icount;++i) { +#if PY_MAJOR_VERSION >= 3 + PyObject *s = PyUnicode_FromString(sv->data[i]); +#else + PyObject *s = PyString_FromString(sv->data[i]); +#endif + if (!s) { + if (ret != Py_None) { + Py_DECREF(ret); + } + ret = NULL; + break; + } + PyList_SetItem(ret, i, s); + } + } + return ret; +} + +/* Returns 1 if the PyObject is a valid representation of an ACL, and + 0 otherwise. */ +int check_is_acl(PyObject *o) { + int i; + PyObject *entry; + if (o == NULL) { + return 0; + } + if (!PyList_Check(o)) { + return 0; + } + for (i=0;icount); + int i; + for (i=0;icount;++i) { + PyObject *acl = Py_BuildValue( "{s:i, s:s, s:s}", + "perms", acls->data[i].perms, + "scheme", acls->data[i].id.scheme, + "id", acls->data[i].id.id ); + PyList_SetItem(ret, i, acl); + } + return ret; +} + +/* Parse the Python representation of an ACL list into an ACL_vector + (which needs subsequent freeing) */ +int parse_acls(struct ACL_vector *acls, PyObject *pyacls) +{ + PyObject *a; + int i; + if (acls == NULL || pyacls == NULL) { + PyErr_SetString(PyExc_ValueError, "acls or pyacls NULL in parse_acls"); + return 0; + } + + acls->count = PyList_Size( pyacls ); + + // Is this a list? If not, we can't do anything + if (PyList_Check(pyacls) == 0) { + PyErr_SetString(InvalidACLException, "List of ACLs required in parse_acls"); + return 0; + } + + acls->data = (struct ACL *)calloc(acls->count, sizeof(struct ACL)); + if (acls->data == NULL) { + PyErr_SetString(PyExc_MemoryError, "calloc failed in parse_acls"); + return 0; + } + + for (i=0;icount;++i) { + a = PyList_GetItem(pyacls, i); + // a is now a dictionary + PyObject *perms = PyDict_GetItemString( a, "perms" ); +#if PY_MAJOR_VERSION >= 3 + acls->data[i].perms = (int32_t)(PyLong_AsLong(perms)); + PyObject *tem_utfstring; + tem_utfstring = PyUnicode_AsEncodedString(PyDict_GetItemString( a, "id" ), "utf-8", NULL ); + acls->data[i].id.id = strdup( PyBytes_AS_STRING(tem_utfstring)); + Py_DECREF(tem_utfstring); + tem_utfstring = PyUnicode_AsEncodedString(PyDict_GetItemString( a, "scheme" ), "utf-8", NULL ); + acls->data[i].id.scheme = strdup( PyBytes_AS_STRING(tem_utfstring) ); + Py_DECREF(tem_utfstring); +#else + acls->data[i].perms = (int32_t)(PyInt_AsLong(perms)); + acls->data[i].id.id = strdup( PyString_AsString( PyDict_GetItemString( a, "id" ) ) ); + acls->data[i].id.scheme = strdup( PyString_AsString( PyDict_GetItemString( a, "scheme" ) ) ); +#endif + } + return 1; +} + +/* Deallocates the memory allocated inside an ACL_vector, but not the + ACL_vector itself */ +void free_acls( struct ACL_vector *acls ) +{ + if (acls == NULL) { + return; + } + int i; + for (i=0;icount;++i) { + free(acls->data[i].id.id); + free(acls->data[i].id.scheme); + } + free(acls->data); +} + +/* -------------------------------------------------------------------------- */ +/* Watcher and callback implementation */ +/* -------------------------------------------------------------------------- */ + +/* Every watcher invocation goes through this dispatch point, which + a) acquires the global interpreter lock + + b) unpacks the PyObject to call from the passed context pointer, + which handily includes the index of the relevant zookeeper handle + to pass back to Python. + + c) Makes the call into Python, checking for error conditions which + we are responsible for detecting and doing something about (we just + print the error and plough right on) + + d) releases the lock after freeing up the context object, which is + only used for one watch invocation (watches are one-shot, unless + 'permanent' != 0) +*/ +void watcher_dispatch(zhandle_t *zzh, int type, int state, + const char *path, void *context) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)context; + PyObject *callback = pyw->callback; + if (callback == NULL) { + // This is unexpected + char msg[256]; + sprintf(msg, "pywatcher: %d %p %d", pyw->zhandle, pyw->callback, pyw->permanent); + PyErr_SetString(PyExc_ValueError, msg); + return; + } + + gstate = PyGILState_Ensure(); + PyObject *arglist = Py_BuildValue("(i,i,i,s)", pyw->zhandle,type, state, path); + if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) { + PyErr_Print(); + } + Py_DECREF(arglist); + if (pyw->permanent == 0 && (type != ZOO_SESSION_EVENT || state < 0)) { + free_pywatcher(pyw); + } + PyGILState_Release(gstate); +} + +/* The completion callbacks (from asynchronous calls) are implemented similarly */ + +/* Called when an asynchronous call that returns void completes and + dispatches user provided callback */ +void void_completion_dispatch(int rc, const void *data) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)data; + if (pyw == NULL) + return; + PyObject *callback = pyw->callback; + gstate = PyGILState_Ensure(); + PyObject *arglist = Py_BuildValue("(i,i)", pyw->zhandle, rc); + if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) + PyErr_Print(); + Py_DECREF(arglist); + free_pywatcher(pyw); + PyGILState_Release(gstate); +} + +/* Called when an asynchronous call that returns a stat structure + completes and dispatches user provided callback */ +void stat_completion_dispatch(int rc, const struct Stat *stat, const void *data) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)data; + if (pyw == NULL) + return; + PyObject *callback = pyw->callback; + gstate = PyGILState_Ensure(); + PyObject *pystat = build_stat(stat); + PyObject *arglist = Py_BuildValue("(i,i,O)", pyw->zhandle,rc, pystat); + Py_DECREF(pystat); + if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) + PyErr_Print(); + Py_DECREF(arglist); + free_pywatcher(pyw); + PyGILState_Release(gstate); +} + +/* Called when an asynchronous call that returns a stat structure and + some untyped data completes and dispatches user provided + callback (used by aget) */ +void data_completion_dispatch(int rc, const char *value, int value_len, const struct Stat *stat, const void *data) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)data; + if (pyw == NULL) + return; + PyObject *callback = pyw->callback; + gstate = PyGILState_Ensure(); + PyObject *pystat = build_stat(stat); + PyObject *arglist = Py_BuildValue("(i,i,s#,O)", pyw->zhandle,rc, value,value_len, pystat); + Py_DECREF(pystat); + + if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) + PyErr_Print(); + Py_DECREF(arglist); + free_pywatcher(pyw); + PyGILState_Release(gstate); +} + +/* Called when an asynchronous call that returns a list of strings + completes and dispatches user provided callback */ +void strings_completion_dispatch(int rc, const struct String_vector *strings, const void *data) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)data; + if (pyw == NULL) + return; + PyObject *callback = pyw->callback; + gstate = PyGILState_Ensure(); + PyObject *pystrings = build_string_vector(strings); + if (pystrings) + { + PyObject *arglist = Py_BuildValue("(i,i,O)", pyw->zhandle, rc, pystrings); + if (arglist == NULL || PyObject_CallObject((PyObject*)callback, arglist) == NULL) + PyErr_Print(); + Py_DECREF(arglist); + } + else + PyErr_Print(); + Py_DECREF(pystrings); + free_pywatcher(pyw); + PyGILState_Release(gstate); +} + +/* Called when an asynchronous call that returns a single string + completes and dispatches user provided callback */ +void string_completion_dispatch(int rc, const char *value, const void *data) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)data; + if (pyw == NULL) { + return; + } + PyObject *callback = pyw->callback; + gstate = PyGILState_Ensure(); + PyObject *arglist = Py_BuildValue("(i,i,s)", pyw->zhandle,rc, value); + if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) + PyErr_Print(); + Py_DECREF(arglist); + free_pywatcher(pyw); + PyGILState_Release(gstate); +} + +/* Called when an asynchronous call that returns a list of ACLs + completes and dispatches user provided callback */ +void acl_completion_dispatch(int rc, struct ACL_vector *acl, struct Stat *stat, const void *data) +{ + PyGILState_STATE gstate; + pywatcher_t *pyw = (pywatcher_t*)data; + if (pyw == NULL) { + return; + } + PyObject *callback = pyw->callback; + gstate = PyGILState_Ensure(); + PyObject *pystat = build_stat(stat); + PyObject *pyacls = build_acls(acl); + PyObject *arglist = Py_BuildValue("(i,i,O,O)", pyw->zhandle,rc, pyacls, pystat); + + Py_DECREF(pystat); + Py_DECREF(pyacls); + + if (PyObject_CallObject((PyObject*)callback, arglist) == NULL) { + PyErr_Print(); + } + Py_DECREF(arglist); + free_pywatcher(pyw); + PyGILState_Release(gstate); +} + +/* -------------------------------------------------------------------------- */ +/* ZOOKEEPER API IMPLEMENTATION */ +/* -------------------------------------------------------------------------- */ + + +static PyObject *pyzookeeper_init_common(PyObject *self, PyObject *args, int ssl) { + const char *host; + const char *cert_str; + PyObject *watcherfn = Py_None; + zhandle_t *zh = NULL; + int recv_timeout = 10000; + clientid_t cid; + cid.client_id = -1; + const char *passwd; + int handle = next_zhandle(); + if (handle == -1) { + if (resize_zhandles() == 0) { + return NULL; + } + handle = next_zhandle(); + } + + if (handle == -1) { + PyErr_SetString(ZooKeeperException,"Couldn't find a free zhandle, something is very wrong"); + return NULL; + } + + if (ssl) { + if (!PyArg_ParseTuple(args, "ss|Oi(Ls)", &host, &cert_str, &watcherfn, &recv_timeout, &cid.client_id, &passwd)) + return NULL; + } else { + if (!PyArg_ParseTuple(args, "s|Oi(Ls)", &host, &watcherfn, &recv_timeout, &cid.client_id, &passwd)) + return NULL; + } + + if (cid.client_id != -1) { + strncpy(cid.passwd, passwd, 16*sizeof(char)); + } + pywatcher_t *pyw = NULL; + if (watcherfn != Py_None) { + pyw = create_pywatcher(handle, watcherfn,1); + if (pyw == NULL) { + return NULL; + } + } + watchers[handle] = pyw; + + if (ssl) { +#ifdef HAVE_OPENSSL_H + zh = zookeeper_init_ssl( host, cert_str, watcherfn != Py_None ? watcher_dispatch : NULL, + recv_timeout, cid.client_id == -1 ? 0 : &cid, pyw, 0 ); +#else + fprintf(stderr, "SSL support not compiled in (called with ssl=%d).\n", ssl); + abort(); +#endif + } else { + zh = zookeeper_init( host, watcherfn != Py_None ? watcher_dispatch : NULL, + recv_timeout, cid.client_id == -1 ? 0 : &cid, pyw, 0 ); + } + + if (zh == NULL) + { + PyErr_Format( ZooKeeperException, "Could not internally obtain%s zookeeper handle", ssl ? " SSL" : "" ); + return NULL; + } + + zhandles[handle] = zh; + return Py_BuildValue( "i", handle); +} + +static PyObject *pyzookeeper_init(PyObject *self, PyObject *args) +{ + return pyzookeeper_init_common(self, args, /*ssl*/0); +} + + +#ifdef HAVE_OPENSSL_H +static PyObject *pyzookeeper_init_ssl(PyObject *self, PyObject *args) +{ + return pyzookeeper_init_common(self, args, /*ssl*/1); +} +#endif + + +/* -------------------------------------------------------------------------- */ +/* Asynchronous API implementation */ +/* -------------------------------------------------------------------------- */ + +/* Asynchronous node creation, returns integer error code */ +PyObject *pyzoo_acreate(PyObject *self, PyObject *args) +{ + int zkhid; char *path; char *value; int valuelen; + struct ACL_vector acl; int flags = 0; + PyObject *completion_callback = Py_None; + PyObject *pyacls = Py_None; + if (!PyArg_ParseTuple(args, "iss#O|iO", &zkhid, &path, + &value, &valuelen, &pyacls, &flags, + &completion_callback)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + CHECK_ACLS(pyacls); + if (parse_acls(&acl, pyacls) == 0) { + return NULL; + } + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + int err = zoo_acreate( zhandles[zkhid], + path, + value, + valuelen, + pyacls == Py_None ? NULL : &acl, + flags, + string_completion_dispatch, + pyw); + free_acls(&acl); + if (err != ZOK) + { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err); +} + +/* Asynchronous node deletion, returns integer error code */ +PyObject *pyzoo_adelete(PyObject *self, PyObject *args) +{ + int zkhid; char *path; int version = -1; + PyObject *completion_callback = Py_None; + if (!PyArg_ParseTuple(args, "is|iO", &zkhid, &path, &version, &completion_callback)) + return NULL; + CHECK_ZHANDLE(zkhid); + + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + + int err = zoo_adelete( zhandles[zkhid], + path, + version, + void_completion_dispatch, + pyw); + + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err); +} + +/* Asynchronous node existence check, returns integer error code */ +PyObject *pyzoo_aexists(PyObject *self, PyObject *args) +{ + int zkhid; char *path; + PyObject *completion_callback = Py_None; + PyObject *exists_watch = Py_None; + if (!PyArg_ParseTuple(args, "is|OO", &zkhid, &path, + &exists_watch, &completion_callback)) + return NULL; + CHECK_ZHANDLE(zkhid); + void *comp_pyw = NULL; + if (completion_callback != Py_None) { + comp_pyw = create_pywatcher(zkhid, completion_callback, 0); + if (comp_pyw == NULL) { + return NULL; + } + } + void *exist_pyw = NULL; + if (exists_watch != Py_None) { + exist_pyw = create_pywatcher(zkhid, exists_watch, 0); + if (exist_pyw == NULL) { + return NULL; + } + } + + int err = zoo_awexists( zhandles[zkhid], + path, + exists_watch != Py_None ? watcher_dispatch : NULL, + exist_pyw, + stat_completion_dispatch, + comp_pyw); + + if (err != ZOK) + { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err);; +} + +/* Asynchronous node data retrieval, returns integer error code */ +PyObject *pyzoo_aget(PyObject *self, PyObject *args) +{ + int zkhid; char *path; + PyObject *completion_callback = Py_None; + PyObject *get_watch = Py_None; + void *comp_pw = NULL; + void *watch_pw = NULL; + + if (!PyArg_ParseTuple(args, "is|OO", &zkhid, &path, + &get_watch, &completion_callback)) { + return NULL; + } + + CHECK_ZHANDLE(zkhid); + + if (get_watch != Py_None) { + if ((watch_pw = create_pywatcher(zkhid, get_watch, 0)) == NULL) { + return NULL; + } + } + + if (completion_callback != Py_None) { + if ((comp_pw = create_pywatcher(zkhid, completion_callback, 0)) == NULL) { + return NULL; + } + } + + int err = zoo_awget( zhandles[zkhid], + path, + get_watch != Py_None ? watcher_dispatch : NULL, + watch_pw, + data_completion_dispatch, + comp_pw); + + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err); +} + +/* Asynchronous node contents update, returns integer error code */ +PyObject *pyzoo_aset(PyObject *self, PyObject *args) +{ + int zkhid; char *path; char *buffer; int buflen; int version=-1; + PyObject *completion_callback = Py_None; + if (!PyArg_ParseTuple(args, "iss#|iO", &zkhid, &path, &buffer, &buflen, &version, &completion_callback)) + return NULL; + CHECK_ZHANDLE(zkhid); + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + int err = zoo_aset( zhandles[zkhid], + path, + buffer, + buflen, + version, + stat_completion_dispatch, + pyw); + + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err); +} + +/* Asynchronous node child retrieval, returns integer error code */ +PyObject *pyzoo_aget_children(PyObject *self, PyObject *args) +{ + int zkhid; char *path; + PyObject *completion_callback = Py_None; + PyObject *get_watch; + if (!PyArg_ParseTuple(args, "is|OO", &zkhid, &path, + &get_watch, &completion_callback)) + return NULL; + CHECK_ZHANDLE(zkhid); + + void *get_pyw = NULL; + if (get_watch != Py_None) { + get_pyw = create_pywatcher(zkhid, get_watch, 0); + if (get_pyw == NULL) { + return NULL; + } + } + + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + + int err = zoo_awget_children( zhandles[zkhid], + path, + get_watch != Py_None ? watcher_dispatch : NULL, + get_pyw, + strings_completion_dispatch, + pyw); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err);; +} + +/* Asynchronous sync, returns integer error code */ +PyObject *pyzoo_async(PyObject *self, PyObject *args) +{ + int zkhid; char *path; + PyObject *completion_callback = Py_None; + if (!PyArg_ParseTuple(args, "is|O", &zkhid, &path, + &completion_callback)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + + int err = zoo_async( zhandles[zkhid], + path, + string_completion_dispatch, + pyw); + + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err);; +} + +/* Asynchronous node ACL retrieval, returns integer error code */ +PyObject *pyzoo_aget_acl(PyObject *self, PyObject *args) +{ + int zkhid; char *path; + PyObject *completion_callback = Py_None; + if (!PyArg_ParseTuple(args, "is|O", &zkhid, &path, + &completion_callback)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + + int err = zoo_aget_acl( zhandles[zkhid], + path, + acl_completion_dispatch, + pyw); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err);; +} + +/* Asynchronous node ACL update, returns integer error code */ +PyObject *pyzoo_aset_acl(PyObject *self, PyObject *args) +{ + int zkhid; char *path; int version; + PyObject *completion_callback = Py_None, *pyacl; + struct ACL_vector aclv; + if (!PyArg_ParseTuple(args, "isiO|O", &zkhid, &path, &version, + &pyacl, &completion_callback)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + CHECK_ACLS(pyacl); + if (parse_acls(&aclv, pyacl) == 0) { + return NULL; + } + + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + + int err = zoo_aset_acl( zhandles[zkhid], + path, + version, + &aclv, + void_completion_dispatch, + pyw); + free_acls(&aclv); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err);; +} + +/* Asynchronous authorization addition, returns integer error code */ +PyObject *pyzoo_add_auth(PyObject *self, PyObject *args) +{ + int zkhid; + char *scheme, *cert; + int certLen; + PyObject *completion_callback; + + if (!PyArg_ParseTuple(args, "iss#O", &zkhid, &scheme, &cert, &certLen, + &completion_callback)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + + void *pyw = NULL; + if (completion_callback != Py_None) { + pyw = create_pywatcher(zkhid, completion_callback, 0); + if (pyw == NULL) { + return NULL; + } + } + + int err = zoo_add_auth( zhandles[zkhid], + scheme, + cert, + certLen, + void_completion_dispatch, + pyw); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err); +} + +/* -------------------------------------------------------------------------- */ +/* Synchronous API implementation */ +/* -------------------------------------------------------------------------- */ + +/* Synchronous node creation, returns node path string */ +static PyObject *pyzoo_create(PyObject *self, PyObject *args) +{ + char *path; + int zkhid; + char* values; + int valuelen; + PyObject *acl = NULL; + int flags = 0; + char realbuf[256]; + const int maxbuf_len = 256; + if (!PyArg_ParseTuple(args, "iss#O|i",&zkhid, &path, &values, &valuelen,&acl,&flags)) + return NULL; + CHECK_ZHANDLE(zkhid); + struct ACL_vector aclv; + CHECK_ACLS(acl); + if (parse_acls(&aclv,acl) == 0) { + return NULL; + } + zhandle_t *zh = zhandles[zkhid]; + int err = zoo_create(zh, path, values, valuelen, &aclv, flags, realbuf, maxbuf_len); + free_acls(&aclv); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + + return Py_BuildValue("s", realbuf); +} + +/* Synchronous node deletion, returns integer error code */ +static PyObject *pyzoo_delete(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + int version = -1; + if (!PyArg_ParseTuple(args, "is|i",&zkhid,&path,&version)) + return NULL; + CHECK_ZHANDLE(zkhid); + zhandle_t *zh = zhandles[zkhid]; + int err = zoo_delete(zh, path, version); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err); +} + +/* Synchronous node existence check, returns stat if exists, None if + absent */ +static PyObject *pyzoo_exists(PyObject *self, PyObject *args) +{ + int zkhid; char *path; PyObject *watcherfn = Py_None; + struct Stat stat; + if (!PyArg_ParseTuple(args, "is|O", &zkhid, &path, &watcherfn)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + zhandle_t *zh = zhandles[zkhid]; + pywatcher_t *pw = NULL; + void *callback = NULL; + if (watcherfn != Py_None) { + pw = create_pywatcher(zkhid, watcherfn,0); + callback = watcher_dispatch; + if (pw == NULL) { + return NULL; + } + } + int err = zoo_wexists(zh, path, callback, pw, &stat); + if (err != ZOK && err != ZNONODE) { + PyErr_SetString(err_to_exception(err), zerror(err)); + free_pywatcher(pw); + return NULL; + } + if (err == ZNONODE) { + Py_INCREF(Py_None); + return Py_None; // This isn't exceptional + } + return build_stat(&stat); +} + +/* Synchronous node child retrieval, returns list of children's path + as strings */ +static PyObject *pyzoo_get_children(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + PyObject *watcherfn = Py_None; + struct String_vector strings; + if (!PyArg_ParseTuple(args, "is|O", &zkhid, &path, &watcherfn)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + pywatcher_t *pw = NULL; + void *callback = NULL; + if (watcherfn != Py_None) { + pw = create_pywatcher( zkhid, watcherfn, 0 ); + callback = watcher_dispatch; + if (pw == NULL) { + return NULL; + } + } + int err = zoo_wget_children(zhandles[zkhid], path, + callback, + pw, &strings ); + + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + free_pywatcher(pw); + return NULL; + } + + PyObject *ret = build_string_vector(&strings); + deallocate_String_vector(&strings); + return ret; +} + +/* Synchronous node data update, returns integer error code */ +static PyObject *pyzoo_set(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + char *buffer; + int buflen; + int version = -1; + if (!PyArg_ParseTuple(args, "iss#|i", &zkhid, &path, &buffer, &buflen, + &version)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + + int err = zoo_set(zhandles[zkhid], path, buffer, buflen, version); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + + return Py_BuildValue("i", err); +} + +/* Synchronous node data update, returns node's stat data structure */ +static PyObject *pyzoo_set2(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + char *buffer; + int buflen; + int version = -1; + if (!PyArg_ParseTuple(args, "iss#|i", &zkhid, &path, &buffer, &buflen, + &version)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + struct Stat stat; + int err = zoo_set2(zhandles[zkhid], path, buffer, buflen, version, &stat); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + + return build_stat(&stat); +} + +/* As per ZK documentation, datanodes are limited to 1Mb. Why not do a + stat followed by a get, to determine how big the buffer should be? + Because the znode may get updated between calls, so we can't + guarantee a complete get anyhow. */ +#define GET_BUFFER_SIZE 1024*1024 + +/* pyzoo_get has an extra parameter over the java/C equivalents. If + you set the fourth integer parameter buffer_len, we return + min(buffer_len, datalength) bytes. This is set by default to + GET_BUFFER_SIZE */ +static PyObject *pyzoo_get(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + char *buffer; + int buffer_len=GET_BUFFER_SIZE; + struct Stat stat; + PyObject *watcherfn = Py_None; + pywatcher_t *pw = NULL; + if (!PyArg_ParseTuple(args, "is|Oi", &zkhid, &path, &watcherfn, &buffer_len)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + if (watcherfn != Py_None) { + pw = create_pywatcher( zkhid, watcherfn,0 ); + if (pw == NULL) { + return NULL; + } + } + buffer = malloc(sizeof(char)*buffer_len); + if (buffer == NULL) { + free_pywatcher(pw); + PyErr_SetString(PyExc_MemoryError, "buffer could not be allocated in pyzoo_get"); + return NULL; + } + + int err = zoo_wget(zhandles[zkhid], path, + watcherfn != Py_None ? watcher_dispatch : NULL, + pw, buffer, + &buffer_len, &stat); + + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + free_pywatcher(pw); + free(buffer); + return NULL; + } + + PyObject *stat_dict = build_stat( &stat ); + PyObject *ret = Py_BuildValue( "(s#,N)", buffer,buffer_len < 0 ? 0 : buffer_len, stat_dict ); + free(buffer); + + return ret; +} + +/* Synchronous node ACL retrieval, returns list of ACLs */ +PyObject *pyzoo_get_acl(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + struct ACL_vector acl; + struct Stat stat; + if (!PyArg_ParseTuple(args, "is", &zkhid, &path)) + return NULL; + CHECK_ZHANDLE(zkhid); + int err = zoo_get_acl( zhandles[zkhid], path, &acl, &stat ); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + PyObject *pystat = build_stat( &stat ); + PyObject *acls = build_acls( &acl ); + PyObject *ret = Py_BuildValue( "(O,O)", pystat, acls ); + Py_DECREF(pystat); + Py_DECREF(acls); + return ret; +} + +/* Synchronous node ACL update, returns integer error code */ +PyObject *pyzoo_set_acl(PyObject *self, PyObject *args) +{ + int zkhid; + char *path; + int version; + PyObject *pyacls; + struct ACL_vector acl; + if (!PyArg_ParseTuple(args, "isiO", &zkhid, &path, &version, &pyacls)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + if (parse_acls(&acl, pyacls) == 0) { + return NULL; + } + int err = zoo_set_acl(zhandles[zkhid], path, version, &acl ); + free_acls(&acl); + if (err != ZOK) { + PyErr_SetString(err_to_exception(err), zerror(err)); + return NULL; + } + return Py_BuildValue("i", err);; +} + +/* -------------------------------------------------------------------------- */ +/* Session and context methods */ +/* -------------------------------------------------------------------------- */ + +/* Closes a connection, returns integer error code */ +PyObject *pyzoo_close(PyObject *self, PyObject *args) +{ + int zkhid, ret; + if (!PyArg_ParseTuple(args, "i", &zkhid)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + zhandle_t *handle = zhandles[zkhid]; + Py_BEGIN_ALLOW_THREADS + ret = zookeeper_close(handle); + Py_END_ALLOW_THREADS + zhandles[zkhid] = NULL; // The zk C client frees the zhandle + return Py_BuildValue("i", ret); +} + +/* Returns the ID of current client as a tuple (client_id, passwd) */ +PyObject *pyzoo_client_id(PyObject *self, PyObject *args) +{ + int zkhid; + if (!PyArg_ParseTuple(args, "i", &zkhid)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + const clientid_t *cid = zoo_client_id(zhandles[zkhid]); + return Py_BuildValue("(L,s)", cid->client_id, cid->passwd); +} + +/* DO NOT USE - context is used internally. This method is not exposed + in the Python module */ +PyObject *pyzoo_get_context(PyObject *self, PyObject *args) +{ + int zkhid; + if (!PyArg_ParseTuple(args, "i", &zkhid)) + return NULL; + CHECK_ZHANDLE(zkhid); + PyObject *context = NULL; + context = (PyObject*)zoo_get_context(zhandles[zkhid]); + if (context) return context; + Py_INCREF(Py_None); + return Py_None; +} + +/* DO NOT USE - context is used internally. This method is not exposed + in the Python module */ +PyObject *pyzoo_set_context(PyObject *self, PyObject *args) +{ + int zkhid; + PyObject *context; + if (!PyArg_ParseTuple(args, "iO", &zkhid, &context)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + PyObject *py_context = (PyObject*)zoo_get_context(zhandles[zkhid]); + if (py_context != NULL && py_context != Py_None) { + Py_DECREF(py_context); + } + Py_INCREF(context); + zoo_set_context(zhandles[zkhid], (void*)context); + Py_INCREF(Py_None); + return Py_None; +} + + +/* -------------------------------------------------------------------------- */ +/* Miscellaneous methods */ +/* -------------------------------------------------------------------------- */ + +/* Sets the global watcher. Returns None */ +PyObject *pyzoo_set_watcher(PyObject *self, PyObject *args) +{ + int zkhid; + PyObject *watcherfn; + if (!PyArg_ParseTuple(args, "iO", &zkhid, &watcherfn)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + pywatcher_t *pyw = watchers[zkhid]; + if (pyw != NULL) { + free_pywatcher( pyw ); + } + + // Create a *permanent* watcher object, not deallocated when called + pyw = create_pywatcher(zkhid, watcherfn,1); + if (pyw == NULL) { + return NULL; + } + watchers[zkhid] = pyw; + zoo_set_watcher(zhandles[zkhid], watcher_dispatch); + zoo_set_context(zhandles[zkhid], pyw); + Py_INCREF(Py_None); + return Py_None; +} + +/* Returns an integer code representing the current connection + state */ +PyObject *pyzoo_state(PyObject *self, PyObject *args) +{ + int zkhid; + if (!PyArg_ParseTuple(args,"i",&zkhid)) { + return NULL; + } + CHECK_ZHANDLE(zkhid); + int state = zoo_state(zhandles[zkhid]); + return Py_BuildValue("i",state); +} + + +/* Convert an integer error code into a string */ +PyObject *pyzerror(PyObject *self, PyObject *args) +{ + int rc; + if (!PyArg_ParseTuple(args,"i", &rc)) + return NULL; + return Py_BuildValue("s", zerror(rc)); +} + +/* Returns the integer receive timeout for a connection */ +PyObject *pyzoo_recv_timeout(PyObject *self, PyObject *args) +{ + int zkhid; + if (!PyArg_ParseTuple(args,"i",&zkhid)) + return NULL; + CHECK_ZHANDLE(zkhid); + int recv_timeout = zoo_recv_timeout(zhandles[zkhid]); + return Py_BuildValue("i",recv_timeout); +} + +/* Returns True if connection is unrecoverable, False otherwise */ +PyObject *pyis_unrecoverable(PyObject *self, PyObject *args) +{ + int zkhid; + if (!PyArg_ParseTuple(args,"i",&zkhid)) + return NULL; + CHECK_ZHANDLE(zkhid); + int ret = is_unrecoverable(zhandles[zkhid]); + if (ret == ZINVALIDSTATE) + Py_RETURN_TRUE; + Py_RETURN_FALSE; +} + +/* Set the debug level for logging, returns None */ +PyObject *pyzoo_set_debug_level(PyObject *self, PyObject *args) +{ + int loglevel; + if (!PyArg_ParseTuple(args, "i", &loglevel)) + return NULL; + zoo_set_debug_level((ZooLogLevel)loglevel); + Py_INCREF(Py_None); + return Py_None; +} + +static PyObject *log_stream = NULL; + +/* Set the output file-like object for logging output. Returns Py_None */ +PyObject *pyzoo_set_log_stream(PyObject *self, PyObject *args) +{ + PyObject *pystream = NULL; + if (!PyArg_ParseTuple(args,"O",&pystream)) { + PyErr_SetString(PyExc_ValueError, "Must supply a Python object to set_log_stream"); + return NULL; + } + +#if PY_MAJOR_VERSION >= 3 + extern PyTypeObject PyIOBase_Type; + if (!PyObject_IsInstance(pystream, (PyObject *)&PyIOBase_Type)) { +#else + if(!PyFile_Check(pystream)) { +#endif + + PyErr_SetString(PyExc_ValueError, "Must supply a file object to set_log_stream"); + return NULL; + } + /* Release the previous reference to log_stream that we took */ + if (log_stream != NULL) { + Py_DECREF(log_stream); + } + + log_stream = pystream; + Py_INCREF(log_stream); + +#if PY_MAJOR_VERSION >= 3 + int fd = PyObject_AsFileDescriptor(log_stream); + FILE *fp = fdopen(fd, "w"); +#else + FILE *fp = PyFile_AsFile(log_stream); +#endif + zoo_set_log_stream(fp); + + Py_INCREF(Py_None); + return Py_None; +} + +/* Set the connection order - randomized or in-order. Returns None. */ +PyObject *pyzoo_deterministic_conn_order(PyObject *self, PyObject *args) +{ + int yesOrNo; + if (!PyArg_ParseTuple(args, "i",&yesOrNo)) + return NULL; + zoo_deterministic_conn_order( yesOrNo ); + Py_INCREF(Py_None); + return Py_None; +} + +/* -------------------------------------------------------------------------- */ +/* Module setup */ +/* -------------------------------------------------------------------------- */ + +#include "pyzk_docstrings.h" + +static PyMethodDef ZooKeeperMethods[] = { + {"init", pyzookeeper_init, METH_VARARGS, pyzk_init_doc }, +#ifdef HAVE_OPENSSL_H + {"init_ssl", pyzookeeper_init_ssl, METH_VARARGS, pyzk_init_ssl_doc }, +#endif + {"create",pyzoo_create, METH_VARARGS, pyzk_create_doc }, + {"delete",pyzoo_delete, METH_VARARGS, pyzk_delete_doc }, + {"get_children", pyzoo_get_children, METH_VARARGS, pyzk_get_children_doc }, + {"set", pyzoo_set, METH_VARARGS, pyzk_set_doc }, + {"set2", pyzoo_set2, METH_VARARGS, pyzk_set2_doc }, + {"get",pyzoo_get, METH_VARARGS, pyzk_get_doc }, + {"exists",pyzoo_exists, METH_VARARGS, pyzk_exists_doc }, + {"get_acl", pyzoo_get_acl, METH_VARARGS, pyzk_get_acl_doc }, + {"set_acl", pyzoo_set_acl, METH_VARARGS, pyzk_set_acl_doc }, + {"close", pyzoo_close, METH_VARARGS, pyzk_close_doc }, + {"client_id", pyzoo_client_id, METH_VARARGS, pyzk_client_id_doc }, + {"set_watcher", pyzoo_set_watcher, METH_VARARGS }, + {"state", pyzoo_state, METH_VARARGS, pyzk_state_doc }, + {"recv_timeout",pyzoo_recv_timeout, METH_VARARGS }, + {"is_unrecoverable",pyis_unrecoverable, METH_VARARGS, pyzk_is_unrecoverable_doc }, + {"set_debug_level",pyzoo_set_debug_level, METH_VARARGS, pyzk_set_debug_level_doc }, + {"set_log_stream",pyzoo_set_log_stream, METH_VARARGS, pyzk_set_log_stream_doc }, + {"deterministic_conn_order",pyzoo_deterministic_conn_order, METH_VARARGS, pyzk_deterministic_conn_order_doc }, + {"acreate", pyzoo_acreate, METH_VARARGS, pyzk_acreate_doc }, + {"adelete", pyzoo_adelete, METH_VARARGS,pyzk_adelete_doc }, + {"aexists", pyzoo_aexists, METH_VARARGS,pyzk_aexists_doc }, + {"aget", pyzoo_aget, METH_VARARGS, pyzk_aget_doc }, + {"aset", pyzoo_aset, METH_VARARGS, pyzk_aset_doc }, + {"aget_children", pyzoo_aget_children, METH_VARARGS, pyzk_aget_children_doc }, + {"async", pyzoo_async, METH_VARARGS, pyzk_async_doc }, + {"aget_acl", pyzoo_aget_acl, METH_VARARGS, pyzk_aget_acl_doc }, + {"aset_acl", pyzoo_aset_acl, METH_VARARGS, pyzk_aset_acl_doc }, + {"zerror", pyzerror, METH_VARARGS, pyzk_zerror_doc }, + {"add_auth", pyzoo_add_auth, METH_VARARGS, pyzk_add_auth_doc }, + /* DO NOT USE get / set_context. Context is used internally to pass + the python watcher to a dispatch function. If you want context, set + it through set_watcher. */ + // {"get_context", pyzoo_get_context, METH_VARARGS, "" }, + // {"set_context", pyzoo_set_context, METH_VARARGS, "" }, + {NULL, NULL} +}; + +#if PY_MAJOR_VERSION >= 3 +static struct PyModuleDef zookeeper_moddef = { + PyModuleDef_HEAD_INIT, + "zookeeper", + NULL, + 0, + ZooKeeperMethods, + 0, + 0, + 0, + 0 +}; +#endif + +#define ADD_INTCONSTANT(x) PyModule_AddIntConstant(module, #x, ZOO_##x) +#define ADD_INTCONSTANTZ(x) PyModule_AddIntConstant(module, #x, Z##x) + +#define ADD_EXCEPTION(x) x = PyErr_NewException("zookeeper."#x, ZooKeeperException, NULL); \ + Py_INCREF(x); \ + PyModule_AddObject(module, #x, x); + +#if PY_MAJOR_VERSION >= 3 +PyMODINIT_FUNC PyInit_zookeeper(void) { +#else +PyMODINIT_FUNC initzookeeper(void) { +#endif + PyEval_InitThreads(); + +#if PY_MAJOR_VERSION >= 3 + PyObject *module = PyModule_Create(&zookeeper_moddef); +#else + PyObject *module = Py_InitModule("zookeeper", ZooKeeperMethods); +#endif + + if (init_zhandles(32) == 0) { +#if PY_MAJOR_VERSION >= 3 + Py_DECREF(module); + return PyErr_NoMemory(); +#else + return; +#endif + } + + ZooKeeperException = PyErr_NewException("zookeeper.ZooKeeperException", + PyExc_Exception, + NULL); + + PyModule_AddObject(module, "ZooKeeperException", ZooKeeperException); + Py_INCREF(ZooKeeperException); + + PyModule_AddStringConstant(module, "__version__", ZOO_VERSION); + + ADD_INTCONSTANT(PERM_READ); + ADD_INTCONSTANT(PERM_WRITE); + ADD_INTCONSTANT(PERM_CREATE); + ADD_INTCONSTANT(PERM_DELETE); + ADD_INTCONSTANT(PERM_ALL); + ADD_INTCONSTANT(PERM_ADMIN); + + ADD_INTCONSTANT(EPHEMERAL); + ADD_INTCONSTANT(SEQUENCE); + + ADD_INTCONSTANT(EXPIRED_SESSION_STATE); + ADD_INTCONSTANT(AUTH_FAILED_STATE); + ADD_INTCONSTANT(CONNECTING_STATE); + ADD_INTCONSTANT(ASSOCIATING_STATE); + ADD_INTCONSTANT(CONNECTED_STATE); + + ADD_INTCONSTANT(CREATED_EVENT); + ADD_INTCONSTANT(DELETED_EVENT); + ADD_INTCONSTANT(CHANGED_EVENT); + ADD_INTCONSTANT(CHILD_EVENT); + ADD_INTCONSTANT(SESSION_EVENT); + ADD_INTCONSTANT(NOTWATCHING_EVENT); + + ADD_INTCONSTANT(LOG_LEVEL_ERROR); + ADD_INTCONSTANT(LOG_LEVEL_WARN); + ADD_INTCONSTANT(LOG_LEVEL_INFO); + ADD_INTCONSTANT(LOG_LEVEL_DEBUG); + + ADD_INTCONSTANTZ(SYSTEMERROR); + ADD_INTCONSTANTZ(RUNTIMEINCONSISTENCY); + ADD_INTCONSTANTZ(DATAINCONSISTENCY); + ADD_INTCONSTANTZ(CONNECTIONLOSS); + ADD_INTCONSTANTZ(MARSHALLINGERROR); + ADD_INTCONSTANTZ(UNIMPLEMENTED); + ADD_INTCONSTANTZ(OPERATIONTIMEOUT); + ADD_INTCONSTANTZ(BADARGUMENTS); + ADD_INTCONSTANTZ(INVALIDSTATE); + + ADD_EXCEPTION(SystemErrorException); + ADD_EXCEPTION(RuntimeInconsistencyException); + ADD_EXCEPTION(DataInconsistencyException); + ADD_EXCEPTION(ConnectionLossException); + ADD_EXCEPTION(MarshallingErrorException); + ADD_EXCEPTION(UnimplementedException); + ADD_EXCEPTION(OperationTimeoutException); + ADD_EXCEPTION(BadArgumentsException); + ADD_EXCEPTION(InvalidStateException); + + ADD_INTCONSTANTZ(OK); + ADD_INTCONSTANTZ(APIERROR); + ADD_INTCONSTANTZ(NONODE); + ADD_INTCONSTANTZ(NOAUTH); + ADD_INTCONSTANTZ(BADVERSION); + ADD_INTCONSTANTZ(NOCHILDRENFOREPHEMERALS); + ADD_INTCONSTANTZ(NODEEXISTS); + ADD_INTCONSTANTZ(NOTEMPTY); + ADD_INTCONSTANTZ(SESSIONEXPIRED); + ADD_INTCONSTANTZ(INVALIDCALLBACK); + ADD_INTCONSTANTZ(INVALIDACL); + ADD_INTCONSTANTZ(AUTHFAILED); + ADD_INTCONSTANTZ(CLOSING); + ADD_INTCONSTANTZ(NOTHING); + ADD_INTCONSTANTZ(SESSIONMOVED); + + ADD_EXCEPTION(ApiErrorException); + ADD_EXCEPTION(NoNodeException); + ADD_EXCEPTION(NoAuthException); + ADD_EXCEPTION(BadVersionException); + ADD_EXCEPTION(NoChildrenForEphemeralsException); + ADD_EXCEPTION(NodeExistsException); + ADD_EXCEPTION(NotEmptyException); + ADD_EXCEPTION(SessionExpiredException); + ADD_EXCEPTION(InvalidCallbackException); + ADD_EXCEPTION(InvalidACLException); + ADD_EXCEPTION(AuthFailedException); + ADD_EXCEPTION(ClosingException); + ADD_EXCEPTION(NothingException); + ADD_EXCEPTION(SessionMovedException); + +#if PY_MAJOR_VERSION >= 3 + return module; +#endif +} diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/examples/README b/zookeeper-contrib/zookeeper-contrib-zkpython/src/examples/README new file mode 100644 index 0000000..3c53454 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/examples/README @@ -0,0 +1,8 @@ + +This folder contains sample showing how you can use ZooKeeper from Python. + +You should also check the following projects: + +* http://github.com/phunt/zk-smoketest +* http://github.com/henryr/pyzk-recipes + diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/examples/watch_znode_for_changes.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/examples/watch_znode_for_changes.py new file mode 100644 index 0000000..07100f0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/examples/watch_znode_for_changes.py @@ -0,0 +1,202 @@ +#!/usr/bin/env python2.6 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +""" ZNode Change Watcher Skeleton Script + +This script shows you how to write a python program that watches a specific +znode for changes and reacts to them. + +Steps to understand how this script works: + +1. start a standalone ZooKeeper server (by default it listens on localhost:2181) + +Did you know you can deploy "local clusters" by using zkconf[1]? +[1] http://github.com/phunt/zkconf + +2. enter the command line console + +3. create the test node: + [zk: (CONNECTED) 1] create /watch-test dummy-data + Created /watch-test + +4. in another shell start this script in verbose mode + $ python watch_znode_for_changes.py -v + + # you should see a lot of log messages. have a look over them because + # you can easily understand how zookeeper works + +5. update the node data: + + [zk: (CONNECTED) 2] set /watch-test new-data + cZxid = 0xa0000001a + ctime = Fri Jul 09 19:14:45 EEST 2010 + mZxid = 0xa0000001e + mtime = Fri Jul 09 19:18:18 EEST 2010 + pZxid = 0xa0000001a + cversion = 0 + dataVersion = 1 + aclVersion = 0 + ephemeralOwner = 0x0 + dataLength = 8 + numChildren = 0 + + ... and you should see similar log messages: + + 2010-07-09 19:18:18,537:11542(0xb6ea5b70):ZOO_DEBUG@process_completions@1765: Calling a watcher for node [/watch-test], type = -1 event=ZOO_CHANGED_EVENT + 2010-07-09 19:18:18,537 watch_znode_for_changes.py:83 - Running watcher: zh=0 event=3 state=3 path=/watch-test + 2010-07-09 19:18:18,537:11542(0xb6ea5b70):ZOO_DEBUG@zoo_awget@2400: Sending request xid=0x4c374b33 for path [/watch-test] to 127.0.0.1:2181 + 2010-07-09 19:18:18,545:11542(0xb76a6b70):ZOO_DEBUG@zookeeper_process@1980: Queueing asynchronous response + 2010-07-09 19:18:18,545:11542(0xb6ea5b70):ZOO_DEBUG@process_completions@1772: Calling COMPLETION_DATA for xid=0x4c374b33 rc=0 + 2010-07-09 19:18:18,545 watch_znode_for_changes.py:54 - This is where your application does work. + + You can repeat this step multiple times. + +6. that's all. in the end you can delete the node and you should see a ZOO_DELETED_EVENT + +""" + +import logging +import logging.handlers +import signal +import sys +import time +import threading +import zookeeper + +from optparse import OptionParser + +logger = logging.getLogger() + +class MyClass(threading.Thread): + znode = '/watch-test' + + def __init__(self, options, args): + threading.Thread.__init__(self) + + logger.debug('Initializing MyClass thread.') + if options.verbose: + zookeeper.set_debug_level(zookeeper.LOG_LEVEL_DEBUG) + + self.zh = zookeeper.init(options.servers) + if zookeeper.OK != zookeeper.aget(self.zh, self.znode, + self.watcher, self.handler): + logger.critical('Unable to get znode! Exiting.') + sys.exit(1) + + def __del__(self): + zookeeper.close(self.zh) + + def aget(self): + return zookeeper.aget(self.zh, self.znode, self.watcher, self.handler) + + def handler(self, zh, rc, data, stat): + """Handle zookeeper.aget() responses. + + This code handles the zookeeper.aget callback. It does not handle watches. + + Numeric arguments map to constants. See ``DATA`` in ``help(zookeeper)`` + for more information. + + Args: + zh Zookeeper handle that made this request. + rc Return code. + data Data stored in the znode. + + Does not provide a return value. + """ + if zookeeper.OK == rc: + logger.debug('This is where your application does work.') + else: + if zookeeper.NONODE == rc: + # avoid sending too many requests if the node does not yet exists + logger.info('Node not found. Trying again to set the watch.') + time.sleep(1) + + if zookeeper.OK != self.aget(): + logger.critical('Unable to get znode! Exiting.') + sys.exit(1) + + def watcher(self, zh, event, state, path): + """Handle zookeeper.aget() watches. + + This code is called when a znode changes and triggers a data watch. + It is not called to handle the zookeeper.aget call itself. + + Numeric arguments map to constants. See ``DATA`` in ``help(zookeeper)`` + for more information. + + Args: + zh Zookeeper handle that set this watch. + event Event that caused the watch (often called ``type`` elsewhere). + state Connection state. + path Znode that triggered this watch. + + Does not provide a return value. + """ + out = ['Running watcher:', + 'zh=%d' % zh, + 'event=%d' % event, + 'state=%d' % state, + 'path=%s' % path] + logger.debug(' '.join(out)) + if event == zookeeper.CHANGED_EVENT and \ + state == zookeeper.CONNECTED_STATE and \ + self.znode == path: + if zookeeper.OK != self.aget(): + logger.critical('Unable to get znode! Exiting.') + sys.exit(1) + + def run(self): + while True: + time.sleep(86400) + + +def main(argv=None): + # Allow Ctrl-C + signal.signal(signal.SIGINT, signal.SIG_DFL) + + parser = OptionParser() + parser.add_option('-v', '--verbose', + dest='verbose', + default=False, + action='store_true', + help='Verbose logging. (default: %default)') + parser.add_option('-s', '--servers', + dest='servers', + default='localhost:2181', + help='Comma-separated list of host:port pairs. (default: %default)') + + (options, args) = parser.parse_args() + + if options.verbose: + logger.setLevel(logging.DEBUG) + else: + logger.setLevel(logging.INFO) + + formatter = logging.Formatter("%(asctime)s %(filename)s:%(lineno)d - %(message)s") + stream_handler = logging.StreamHandler() + stream_handler.setFormatter(formatter) + logger.addHandler(stream_handler) + + logger.info('Starting Zookeeper python example: %s' % ' '.join(sys.argv)) + + mc = MyClass(options, args) + mc.start() + mc.join() + + +if __name__ == '__main__': + main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/python/setup.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/python/setup.py new file mode 100644 index 0000000..b225a31 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/python/setup.py @@ -0,0 +1,45 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +from distutils.core import setup, Extension +import os + +zookeeper_basedir = "../../" + +zookeeper_macros = [("THREADED", None)] + +# Assume the C extension includes OpenSSL support unless told +# otherwise. +if not os.environ.get("ZKPYTHON_NO_SSL"): + zookeeper_macros.append(("HAVE_OPENSSL_H", True)) + +zookeepermodule = Extension("zookeeper", + sources=["src/c/zookeeper.c"], + define_macros=zookeeper_macros, + include_dirs=[zookeeper_basedir + "/zookeeper-client/zookeeper-client-c/include", + zookeeper_basedir + "/zookeeper-client/zookeeper-client-c/target/c", + zookeeper_basedir + "/zookeeper-client/zookeeper-client-c/generated"], + libraries=["zookeeper_mt"], + library_dirs=[zookeeper_basedir + "/zookeeper-client/zookeeper-client-c/.libs/", + zookeeper_basedir + "/zookeeper-client/zookeeper-client-c/target/c/.libs/", + zookeeper_basedir + "/build/test/test-cppunit/.libs", + "/usr/local/lib" + ]) + +setup( name="ZooKeeper", + version = "0.4", + description = "ZooKeeper Python bindings", + ext_modules=[zookeepermodule] ) diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/python/zk.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/python/zk.py new file mode 100644 index 0000000..24986e3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/python/zk.py @@ -0,0 +1,76 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, time, threading + +f = open("out.log","w") +zookeeper.set_log_stream(f) + +connected = False +conn_cv = threading.Condition( ) + +def my_connection_watcher(handle,type,state,path): + global connected, conn_cv + print("Connected, handle is ", handle) + conn_cv.acquire() + connected = True + conn_cv.notifyAll() + conn_cv.release() + +conn_cv.acquire() +print("Connecting to localhost:2181 -- ") +handle = zookeeper.init("localhost:2181", my_connection_watcher, 10000, 0) +while not connected: + conn_cv.wait() +conn_cv.release() + +def my_getc_watch( handle, type, state, path ): + print("Watch fired -- ") + print(type, state, path) + +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"}; + +try: + zookeeper.create(handle, "/zk-python", "data", [ZOO_OPEN_ACL_UNSAFE], 0) + zookeeper.get_children(handle, "/zk-python", my_getc_watch) + for i in xrange(5): + print("Creating sequence node ", i, " ", zookeeper.create(handle, "/zk-python/sequencenode", "data", [ZOO_OPEN_ACL_UNSAFE], zookeeper.SEQUENCE )) +except: + pass + +def pp_zk(handle,root, indent = 0): + """Pretty print(a zookeeper tree, starting at root)""" + def make_path(child): + if root == "/": + return "/" + child + return root + "/" + child + children = zookeeper.get_children(handle, root, None) + out = "" + for i in xrange(indent): + out += "\t" + out += "|---"+root + " :: " + zookeeper.get(handle, root, None)[0] + print(out) + for child in children: + pp_zk(handle,make_path(child),indent+1) + +print("ZNode tree -- ") +pp_zk(handle,"/") + +print("Getting ACL / Stat for /zk-python --") +(stat, acl) = zookeeper.get_acl(handle, "/zk-python") +print("Stat:: ", stat) +print("Acl:: ", acl) + diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/acl_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/acl_test.py new file mode 100644 index 0000000..1289c8a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/acl_test.py @@ -0,0 +1,109 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading + +ZOO_OPEN_ACL_UNSAFE = {"perms":zookeeper.PERM_ALL, "scheme":"world", "id" :"anyone"} +ZOO_ACL_READ = {"perms":zookeeper.PERM_READ, "scheme": "world", + "id":"anyone"} +class ACLTest(zktestbase.TestBase): + """Test whether basic ACL setting and getting work correctly""" + # to do: startup and teardown via scripts? + def setUp(self): + zktestbase.TestBase.setUp(self) + try: + zookeeper.delete(self.handle, "/zk-python-acltest") + zookeeper.delete(self.handle, "/zk-python-aacltest") + except: + pass + + def test_sync_acl(self): + self.assertEqual(self.connected, True) + ret = zookeeper.create(self.handle, "/zk-python-acltest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + acls = zookeeper.get_acl(self.handle, "/zk-python-acltest") + self.assertEqual(acls[1], [ZOO_OPEN_ACL_UNSAFE]) + self.assertRaises(zookeeper.InvalidACLException,zookeeper.set_acl,self.handle, "/zk-python-acltest", -1, ZOO_ACL_READ) + zookeeper.set_acl(self.handle, "/zk-python-acltest", -1, [ZOO_ACL_READ]) + acls = zookeeper.get_acl(self.handle, "/zk-python-acltest") + self.assertEqual(acls[1], [ZOO_ACL_READ]) + + + def test_async_acl(self): + self.cv = threading.Condition() + self.cv = threading.Condition() + def aget_callback(handle, rc, acl, stat): + self.cv.acquire() + self.callback_flag = True + self.rc = rc + self.acl = acl + self.stat = stat + self.cv.notify() + self.cv.release() + + def aset_callback(handle, rc): + self.cv.acquire() + self.callback_flag = True + self.rc = rc + self.cv.notify() + self.cv.release() + + self.assertEqual(self.connected, True, "Not connected!") + ret = zookeeper.create(self.handle, "/zk-python-aacltest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + + self.cv.acquire() + zookeeper.aget_acl(self.handle, "/zk-python-aacltest", aget_callback) + self.cv.wait(15) + self.cv.release() + + self.assertEqual(self.callback_flag, True, "aget_acl timed out") + self.assertEqual(self.rc, zookeeper.OK, "aget failed") + self.assertEqual(self.acl, [ZOO_OPEN_ACL_UNSAFE], "Wrong ACL returned from aget") + + self.cv.acquire() + self.callback_flag = False + zookeeper.aset_acl(self.handle, "/zk-python-aacltest", -1, [ZOO_ACL_READ], aset_callback) + self.cv.wait(15) + self.cv.release() + + self.assertEqual(self.callback_flag, True, "aset_acl timed out") + self.assertEqual(self.rc, zookeeper.OK, "aset failed") + acls = zookeeper.get_acl(self.handle, "/zk-python-aacltest") + self.assertEqual(acls[1], [ZOO_ACL_READ], "Wrong ACL returned from get when aset") + + def test_invalid_acl(self): + self.assertRaises(zookeeper.InvalidACLException, + zookeeper.create, + self.handle, + "/zk-python-aclverifytest", + "", + None, + zookeeper.EPHEMERAL) + + def test_invalid_acl2(self): + """Verify all required keys are present in the ACL.""" + invalid_acl = [{"schema": "digest", "id": "zebra"}] + self.assertRaises(zookeeper.InvalidACLException, + zookeeper.create, + self.handle, + "/zk-python-aclverifytest", + "", + invalid_acl, + zookeeper.EPHEMERAL) + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/async_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/async_test.py new file mode 100644 index 0000000..61740ae --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/async_test.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading + +class AsyncTest(zktestbase.TestBase): + """Test whether async works""" + # to do: startup and teardown via scripts? + def setUp( self ): + zktestbase.TestBase.setUp(self) + + def test_async(self): + self.assertEqual(self.connected, True) + ret = getattr(zookeeper, 'async')(self.handle, "/") + self.assertEqual(ret, zookeeper.OK, "async failed") + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/callback_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/callback_test.py new file mode 100644 index 0000000..95e20b4 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/callback_test.py @@ -0,0 +1,155 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading, gc + +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + +class CallbackTest(zktestbase.TestBase): + """ + Test whether callbacks (watchers/completions) are correctly invoked + """ + # to do: startup and teardown via scripts? + def setUp(self): + zktestbase.TestBase.setUp(self) + self.cv = threading.Condition() + + def create_callback(self, callback): + """ + Returns a callable which signals cv and then calls callback + """ + def wrapper(*args, **kwargs): + self.cv.acquire() + callback(*args, **kwargs) + self.cv.notify() + self.cv.release() + return wrapper + + def test_none_callback(self): + """ + Test that no errors are raised when None is passed as a callback. + """ + self.ensureCreated("/zk-python-none-callback-test","test") + # To do this we need to issue two operations, waiting on the second + # to ensure that the first completes + zookeeper.get(self.handle, "/zk-python-none-callback-test", None) + (d,s) = zookeeper.get(self.handle, "/zk-python-none-callback-test") + self.assertEqual(d, "test") + + def callback_harness(self, trigger, test): + self.callback_flag = False + self.cv.acquire() + trigger() + self.cv.wait(15) + test() + + def test_dispatch_types(self): + """ + Test all the various dispatch mechanisms internal to the module. + """ + def dispatch_callback(*args, **kwargs): + self.callback_flag = True + self.ensureCreated("/zk-python-dispatch-test") + self.callback_harness( lambda: zookeeper.adelete(self.handle, + "/zk-python-dispatch-test", + -1, + self.create_callback(dispatch_callback)), + lambda: self.assertEqual(True, self.callback_flag, "Void dispatch not fired")) + + + self.ensureCreated("/zk-python-dispatch-test") + self.callback_harness( lambda: zookeeper.aexists(self.handle, + "/zk-python-dispatch-test", + None, + self.create_callback(dispatch_callback)), + lambda: self.assertEqual(True, self.callback_flag, "Stat dispatch not fired")) + + self.callback_harness( lambda: zookeeper.aget(self.handle, + "/zk-python-dispatch-test", + None, + self.create_callback(dispatch_callback)), + lambda: self.assertEqual(True, self.callback_flag, "Data dispatch not fired")) + + self.callback_harness( lambda: zookeeper.aget_children(self.handle, + "/", + None, + self.create_callback( dispatch_callback )), + lambda: self.assertEqual(True, self.callback_flag, "Strings dispatch not fired")) + + self.callback_harness( lambda: getattr(zookeeper, 'async')(self.handle, + "/", + self.create_callback( dispatch_callback )), + lambda: self.assertEqual(True, self.callback_flag, "String dispatch not fired")) + + self.callback_harness( lambda: zookeeper.aget_acl(self.handle, + "/", + self.create_callback( dispatch_callback )), + lambda: self.assertEqual(True, self.callback_flag, "ACL dispatch not fired")) + + def test_multiple_watchers(self): + """ + Test whether multiple watchers are correctly called + """ + cv1, cv2 = threading.Condition(), threading.Condition() + def watcher1(*args, **kwargs): + cv1.acquire() + self.watcher1 = True + cv1.notify() + cv1.release() + + def watcher2(*args, **kwargs): + cv2.acquire() + self.watcher2 = True + cv2.notify() + cv2.release() + + nodename = "/zk-python-multiple-watcher-test" + self.ensureCreated(nodename, "test") + cv1.acquire() + cv2.acquire() + zookeeper.get(self.handle, nodename, watcher1) + zookeeper.get(self.handle, nodename, watcher2) + zookeeper.set(self.handle, nodename, "test") + cv1.wait(15) + cv2.wait(15) + self.assertTrue(self.watcher1 and self.watcher2, "One or more watchers failed to fire") + + def test_lose_scope(self): + """ + The idea is to test that the reference counting doesn't + fail when we retain no references outside of the module + """ + self.ensureDeleted("/zk-python-lose-scope-test") + self.ensureCreated("/zk-python-lose-scope-test") + def set_watcher(): + def fn(): self.callback_flag = True + self.callback_flag = False + zookeeper.exists(self.handle, "/zk-python-lose-scope-test", + self.create_callback( lambda handle, type, state, path: fn() ) + ) + + set_watcher() + gc.collect() + self.cv.acquire() + zookeeper.set(self.handle, "/zk-python-lose-scope-test", "test") + self.cv.wait(15) + self.assertEqual(self.callback_flag, True) + + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/clientid_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/clientid_test.py new file mode 100644 index 0000000..90c8f0a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/clientid_test.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import unittest, threading + +import zookeeper, zktestbase + +class ClientidTest(zktestbase.TestBase): + """Test whether clientids work""" + def setUp(self): + pass + + def testclientid(self): + cv = threading.Condition() + self.connected = False + def connection_watcher(handle, type, state, path): + cv.acquire() + self.connected = True + cv.notify() + cv.release() + + cv.acquire() + self.handle = zookeeper.init(self.host, connection_watcher,10000,(123456,"mypassword")) + self.assertEqual(self.handle, zookeeper.OK) + cv.wait(15.0) + cv.release() + self.assertEqual(self.connected, True, "Connection timed out to " + self.host) + (cid,passwd) = zookeeper.client_id(self.handle) + self.assertEqual(cid,123456) + self.assertEqual(passwd,"mypassword") + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/close_deadlock_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/close_deadlock_test.py new file mode 100644 index 0000000..921d2cc --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/close_deadlock_test.py @@ -0,0 +1,50 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading +import time + + +class CloseDeadlockTest(zktestbase.TestBase): + """ + This tests for the issue found in + https://issues.apache.org/jira/browse/ZOOKEEPER-763 + + zookeeper.close blocks on waiting for all completions to + finish. Previously it was doing so while holding teh GIL, stopping + any completions from actually continuing. + + This test is a failure if it does not exit within a few seconds. + """ + def deadlock(): + cv = threading.Condition() + + def callback(*args): + cv.acquire() + cv.notifyAll() + cv.release() + time.sleep(1) + + cv.acquire() + zookeeper.aget(handle, "/", None, callback) + cv.wait() + zookeeper.close(handle) + + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/connection_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/connection_test.py new file mode 100644 index 0000000..3fbbd4b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/connection_test.py @@ -0,0 +1,163 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import unittest, threading, re, sys +if sys.version_info < (3,): + range = xrange + +import zookeeper, zktestbase +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + +class ConnectionTest(zktestbase.TestBase): + """Test whether we can make a connection""" + def setUp(self): + pass + + def testconnection(self): + cv = threading.Condition() + self.connected = False + def connection_watcher(handle, type, state, path): + cv.acquire() + self.connected = True + self.assertEqual(zookeeper.CONNECTED_STATE, state) + self.handle = handle + cv.notify() + cv.release() + + cv.acquire() + ret = zookeeper.init(self.host, connection_watcher) + cv.wait(15.0) + cv.release() + self.assertEqual(self.connected, True, "Connection timed out to " + self.host) + self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle)) + + self.assertEqual(zookeeper.close(self.handle), zookeeper.OK) + # Trying to close the same handle twice is an error, and the C library will segfault on it + # so make sure this is caught at the Python module layer + self.assertRaises(zookeeper.ZooKeeperException, + zookeeper.close, + self.handle) + + self.assertRaises(zookeeper.ZooKeeperException, + zookeeper.get, + self.handle, + "/") + + @unittest.skipUnless(hasattr(zookeeper, 'init_ssl'), + "SSL support not compiled in.") + def testsslconnection(self): + cv = threading.Condition() + self.connected = False + def connection_watcher(handle, type, state, path): + cv.acquire() + self.connected = True + self.assertEqual(zookeeper.CONNECTED_STATE, state) + self.handle = handle + cv.notify() + cv.release() + + cv.acquire() + ret = zookeeper.init_ssl(self.sslhost, self.sslcert, connection_watcher) + cv.wait(15.0) + cv.release() + self.assertEqual(self.connected, True, "SSL Connection timed out to " + self.host) + self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle)) + + self.assertEqual(zookeeper.close(self.handle), zookeeper.OK) + # Trying to close the same handle twice is an error, and the C library will segfault on it + # so make sure this is caught at the Python module layer + self.assertRaises(zookeeper.ZooKeeperException, + zookeeper.close, + self.handle) + + self.assertRaises(zookeeper.ZooKeeperException, + zookeeper.get, + self.handle, + "/") + + def testhandlereuse(self): + """ + Test a) multiple concurrent connections b) reuse of closed handles + """ + cv = threading.Condition() + self.connected = False + def connection_watcher(handle, type, state, path): + cv.acquire() + self.connected = True + self.assertEqual(zookeeper.CONNECTED_STATE, state) + self.handle = handle + cv.notify() + cv.release() + + cv.acquire() + handles = [ zookeeper.init(self.host) for i in range(10) ] + ret = zookeeper.init(self.host, connection_watcher) + cv.wait(15.0) + cv.release() + self.assertEqual(self.connected, True, "Connection timed out to " + self.host) + self.assertEqual(True, self.all( [ zookeeper.state(handle) == zookeeper.CONNECTED_STATE for handle in handles ] ), + "Not all connections succeeded") + oldhandle = handles[3] + zookeeper.close(oldhandle) + newhandle = zookeeper.init(self.host) + + # This assertion tests *internal* behaviour; i.e. that the module + # correctly reuses closed handles. This is therefore implementation + # dependent. + self.assertEqual(newhandle, oldhandle, "Didn't get reused handle") + + def testmanyhandles(self): + """ + Test the ability of the module to support many handles. + """ + # We'd like to do more, but currently the C client doesn't + # work with > 83 handles (fails to create a pipe) on MacOS 10.5.8 + handles = [ zookeeper.init(self.host) for i in range(9) ] + + cv = threading.Condition() + self.connected = False + def connection_watcher(handle, type, state, path): + cv.acquire() + self.connected = True + self.assertEqual(zookeeper.CONNECTED_STATE, state) + self.handle = handle + cv.notify() + cv.release() + + cv.acquire() + ret = zookeeper.init(self.host, connection_watcher) + cv.wait(15.0) + cv.release() + self.assertEqual(self.connected, True, "Connection timed out to " + self.host) + + for i,h in enumerate(handles): + path = "/zkpython-test-handles-%s" % str(i) + self.assertEqual(path, zookeeper.create(h, path, "", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL)) + + self.assertEqual(True, self.all( zookeeper.close(h) == zookeeper.OK for h in handles )) + + def testversionstringexists(self): + self.assertTrue(hasattr(zookeeper, '__version__')) + self.assertTrue(re.match("\d.\d.\d", zookeeper.__version__)) + + + def tearDown(self): + pass + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/create_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/create_test.py new file mode 100644 index 0000000..8ab80f9 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/create_test.py @@ -0,0 +1,104 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading + +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + +class CreationTest(zktestbase.TestBase): + """Test whether we can create znodes""" + # to do: startup and teardown via scripts? + def setUp(self): + zktestbase.TestBase.setUp(self) + try: + zookeeper.delete(self.handle, "/zk-python-createtest") + zookeeper.delete(self.handle, "/zk-python-acreatetest") + except: + pass + + def test_sync_create(self): + self.assertEqual(self.connected, True) + ret = zookeeper.create(self.handle, "/zk-python-createtest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + self.assertEqual(ret, "/zk-python-createtest") + self.assertRaises(zookeeper.NoChildrenForEphemeralsException, + zookeeper.create, + self.handle, + "/zk-python-createtest/invalid-child", + "", + [ZOO_OPEN_ACL_UNSAFE], + zookeeper.EPHEMERAL) + + def test_sync_create_existing(self): + self.assertEqual(self.connected, True) + ret = zookeeper.create(self.handle, "/zk-python-createtest-existing", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + self.assertEqual(ret, "/zk-python-createtest-existing") + + self.assertRaises(zookeeper.NodeExistsException, + zookeeper.create, + self.handle, + "/zk-python-createtest-existing", + "nodecontents", + [ZOO_OPEN_ACL_UNSAFE], + zookeeper.EPHEMERAL) + + + def test_exception_paths(self): + """ + Make sure common exceptions due to API misuse are correctly propogated + """ + self.assertRaises(zookeeper.BadArgumentsException, + zookeeper.create, + self.handle, + "/zk-python-badargs-test", + "", + [ZOO_OPEN_ACL_UNSAFE], + -1) + self.assertRaises(zookeeper.InvalidACLException, + zookeeper.create, + self.handle, + "/zk-python-invalidacl-test", + "", + ZOO_OPEN_ACL_UNSAFE) # Error - not a list + + + def test_async_create(self): + self.cv = threading.Condition() + def callback(handle, rc, value): + self.cv.acquire() + self.callback_flag = True + self.rc = rc + self.cv.notify() + self.cv.release() + + self.assertEqual(self.connected, True, "Not connected!") + self.cv.acquire() + + ret = zookeeper.acreate(self.handle, "/zk-python-acreatetest", "nodecontents", + [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL, + callback ) + self.assertEqual(ret, zookeeper.OK, "acreate failed") + while not self.callback_flag: + self.cv.wait(15) + self.cv.release() + + self.assertEqual(self.callback_flag, True, "acreate timed out") + self.assertEqual(self.rc, zookeeper.OK) + + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/delete_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/delete_test.py new file mode 100644 index 0000000..913b6a9 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/delete_test.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading + +class DeletionTest(zktestbase.TestBase): + """Test whether we can delete znodes""" + + def test_sync_delete(self): + ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + self.assertEqual(self.connected, True) + ret = zookeeper.create(self.handle, "/zk-python-deletetest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + self.assertEqual(ret, "/zk-python-deletetest") + ret = zookeeper.delete(self.handle,"/zk-python-deletetest") + self.assertEqual(ret, zookeeper.OK) + children = zookeeper.get_children(self.handle, "/") + self.assertEqual(False, "zk-python-deletetest" in children) + + # test exception + self.assertRaises(zookeeper.NoNodeException, + zookeeper.delete, + self.handle, + "/zk-python-deletetest") + + def test_async_delete(self): + ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + self.assertEqual(self.connected, True) + ret = zookeeper.create(self.handle, "/zk-python-adeletetest", "nodecontents", [ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + self.assertEqual(ret, "/zk-python-adeletetest") + + self.cv = threading.Condition() + self.callback_flag = False + self.rc = -1 + def callback(handle, rc): + self.cv.acquire() + self.callback_flag = True + self.cv.notify() + self.rc = rc # don't assert this here, as if the assertion fails, the test will block + self.cv.release() + + self.cv.acquire() + ret = zookeeper.adelete(self.handle,"/zk-python-adeletetest",-1,callback) + self.assertEqual(ret, zookeeper.OK, "adelete failed") + while not self.callback_flag: + self.cv.wait(15) + self.cv.release() + + self.assertEqual(self.callback_flag, True, "adelete timed out") + self.assertEqual(self.rc, zookeeper.OK) + + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/exists_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/exists_test.py new file mode 100644 index 0000000..ddc6ef3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/exists_test.py @@ -0,0 +1,64 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading + +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} +class ExistsTest(zktestbase.TestBase): + def setUp( self ): + zktestbase.TestBase.setUp(self) + try: + zookeeper.create(self.handle, "/zk-python-existstest","existstest", [ZOO_OPEN_ACL_UNSAFE],zookeeper.EPHEMERAL) + zookeeper.create(self.handle, "/zk-python-aexiststest","existstest",[ZOO_OPEN_ACL_UNSAFE],zookeeper.EPHEMERAL) + except: + pass + + def test_sync_exists(self): + self.assertEqual(self.connected, True) + ret = zookeeper.exists(self.handle, "/zk-python-existstest", None) + self.assertNotEqual(ret, None, "/zk-python-existstest does not exist (possibly means creation failure)") + + def test_sync_nexists(self): + self.assertEqual(None, zookeeper.exists(self.handle, "/i-dont-exist", None)) + + + def test_async_exists(self): + self.cv = threading.Condition() + def callback(handle, rc, stat): + self.cv.acquire() + self.callback_flag = True + self.cv.notify() + self.cv.release() + self.rc = rc + + self.assertEqual(self.connected, True) + + self.cv.acquire() + ret = zookeeper.aexists(self.handle, "/zk-python-aexiststest", None, + callback ) + self.assertEqual(ret, zookeeper.OK) + while not self.callback_flag: + self.cv.wait(15) + self.cv.release() + + self.assertEqual(self.callback_flag, True, "aexists timed out") + self.assertEqual(self.rc, zookeeper.OK, "Return code not ok:" + zookeeper.zerror(self.rc)) + + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/get_set_test.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/get_set_test.py new file mode 100644 index 0000000..b77b3b2 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/get_set_test.py @@ -0,0 +1,211 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import zookeeper, zktestbase, unittest, threading, sys +if sys.version_info < (3,): + range = xrange + +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + +class GetSetTest(zktestbase.TestBase): + def setUp( self ): + zktestbase.TestBase.setUp(self) + try: + zookeeper.create(self.handle, "/zk-python-getsettest", "on",[ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + zookeeper.create(self.handle, "/zk-python-agetsettest", + "on",[ZOO_OPEN_ACL_UNSAFE], zookeeper.EPHEMERAL) + except: + pass + + def test_empty_node(self): + """ + Test for a bug when instead of empty string we can get + random data from buffer malloc'ed to hold node contents. + See ZOOKEEPER-1906 for details + """ + NODE_PATH = "/zk-python-test-empty-node" + self.ensureDeleted(NODE_PATH) + zookeeper.create(self.handle, NODE_PATH, "", + [{"perms":0x1f, "scheme":"world", "id" :"anyone"}]) + (data,stat) = zookeeper.get(self.handle, NODE_PATH, None) + self.assertEqual(data, "", "Data is not empty as expected: " + data) + + def test_sync_getset(self): + self.assertEqual(self.connected, True, "Not connected!") + (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None) + self.assertEqual(data, "on", "Data is not 'on' as expected: " + data) + ret = zookeeper.set(self.handle, "/zk-python-getsettest", + "off", stat["version"]) + (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None) + self.assertEqual(data, "off", "Data is not 'off' as expected: " + data) + self.assertRaises(zookeeper.BadVersionException, + zookeeper.set, + self.handle, + "/zk-python-getsettest", + "test", + stat["version"]+1) + stat2 = zookeeper.set2(self.handle, "/zk-python-getsettest", + "set2", stat["version"]) + self.assertNotEqual(stat2, None, "set2 call failed, return should not be None") + self.assertEqual(stat2["numChildren"], 0, + "set2 call failed, numChildren not 0 in set2 call") + (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None) + self.assertEqual(data, "set2", "Data is not 'set2' as expected: " + data) + + def test_stat_deleted_node(self): + """ + Test for a bug that surfaced when trying to build a + stat object from a non-existant node. + + """ + self.ensureDeleted("/zk-python-test-deleteme") + self.assertRaises(zookeeper.NoNodeException, + zookeeper.get, + self.handle, + "/zk-python-test-deleteme") + self.cv = threading.Condition() + def callback(handle, rc, value, stat): + self.cv.acquire() + self.stat = stat + self.rc = rc + self.value = value + self.callback_flag = True + self.cv.notify() + self.cv.release() + self.cv.acquire() + zookeeper.aget(self.handle, "/zk-python-test-deleteme", None, callback) + self.cv.wait(15) + self.assertEqual(self.callback_flag, True, "aget timed out!") + self.assertEqual(self.stat, None, "Stat should be none!") + self.assertEqual(self.value, None, "Value should be none!") + + def test_sync_get_large_datanode(self): + """ + Test that we can retrieve datanode sizes up to + 1Mb with default parameters (depends on ZooKeeper server). + """ + + data = ''.join(["A" for x in range(1024*1023)]) + self.ensureDeleted("/zk-python-test-large-datanode") + zookeeper.create(self.handle, "/zk-python-test-large-datanode", data, + [{"perms":0x1f, "scheme":"world", "id" :"anyone"}]) + (ret,stat) = zookeeper.get(self.handle, "/zk-python-test-large-datanode") + self.assertEqual(len(ret), 1024*1023, + "Should have got 1Mb returned, instead got %s" % len(ret)) + (ret,stat) = zookeeper.get(self.handle, "/zk-python-test-large-datanode",None,500) + self.assertEqual(len(ret), 500, + "Should have got 500 bytes returned, instead got %s" % len(ret)) + + + + def test_async_getset(self): + self.cv = threading.Condition() + def get_callback(handle, rc, value, stat): + self.cv.acquire() + self.callback_flag = True + self.rc = rc + self.value = (value,stat) + self.cv.notify() + self.cv.release() + + def set_callback(handle, rc, stat): + self.cv.acquire() + self.callback_flag = True + self.rc = rc + self.value = stat + self.cv.notify() + self.cv.release() + + self.assertEqual(self.connected, True, "Not connected!") + + self.cv.acquire() + self.callback_flag = False + ret = zookeeper.aset(self.handle, "/zk-python-agetsettest", "off", -1, set_callback) + self.assertEqual(ret, zookeeper.OK, "aset failed") + while not self.callback_flag: + self.cv.wait(15) + self.cv.release() + self.assertEqual(self.callback_flag, True, "aset timed out") + + self.cv.acquire() + self.callback_flag = False + ret = zookeeper.aget(self.handle, "/zk-python-agetsettest", None, get_callback) + self.assertEqual(ret, zookeeper.OK, "aget failed") + self.cv.wait(15) + self.cv.release() + self.assertEqual(self.callback_flag, True, "aget timed out") + self.assertEqual(self.value[0], "off", "Data is not 'off' as expected: " + self.value[0]) + + def test_sync_getchildren(self): + self.ensureCreated("/zk-python-getchildrentest", flags=0) + self.ensureCreated("/zk-python-getchildrentest/child") + children = zookeeper.get_children(self.handle, "/zk-python-getchildrentest") + self.assertEqual(len(children), 1, "Expected to find 1 child, got " + str(len(children))) + + def test_async_getchildren(self): + self.ensureCreated("/zk-python-getchildrentest", flags=0) + self.ensureCreated("/zk-python-getchildrentest/child") + + def gc_callback(handle, rc, children): + self.cv.acquire() + self.rc = rc + self.children = children + self.callback_flag = True + self.cv.notify() + self.cv.release() + + self.cv.acquire() + self.callback_flag = False + zookeeper.aget_children(self.handle, "/zk-python-getchildrentest", None, gc_callback) + self.cv.wait(15) + self.assertEqual(self.callback_flag, True, "aget_children timed out") + self.assertEqual(self.rc, zookeeper.OK, "Return code for aget_children was not OK - %s" % zookeeper.zerror(self.rc)) + self.assertEqual(len(self.children), 1, "Expected to find 1 child, got " + str(len(self.children))) + + + def test_async_getchildren_with_watcher(self): + self.ensureCreated("/zk-python-getchildrentest", flags=0) + self.ensureCreated("/zk-python-getchildrentest/child") + + watched = [] + + def watcher(*args): + self.cv.acquire() + watched.append(args) + self.cv.notify() + self.cv.release() + + def children_callback(*args): + self.cv.acquire() + self.cv.notify() + self.cv.release() + + zookeeper.aget_children( + self.handle, "/zk-python-getchildrentest", watcher, children_callback) + + self.cv.acquire() + self.cv.wait() + self.cv.release() + + self.cv.acquire() + self.ensureCreated("/zk-python-getchildrentest/child2") + self.cv.wait(15) + self.assertTrue(watched) + +if __name__ == '__main__': + unittest.main() diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/run_tests.sh b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/run_tests.sh new file mode 100644 index 0000000..232359b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/run_tests.sh @@ -0,0 +1,43 @@ +#!/bin/sh +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Usage: run_tests.sh testdir [logdir] +# logdir is optional, defaults to cwd + +set -e + +# get the number of command-line arguments given +ARGC=$# + +# check to make sure enough arguments were given or exit +if [ $ARGC -lt 2 ]; then + export ZKPY_LOG_DIR="." +else + export ZKPY_LOG_DIR=$2 +fi + +# Find the build directory containing zookeeper.so +SO_PATH=`find ./target/ -name 'zookeeper*.so' | head -1` +PYTHONPATH=`dirname $SO_PATH` +LIB_PATH=../../zookeeper-client/zookeeper-client-c/target/c/.libs +for test in `ls $1/*_test.py`; +do + echo "Running $test" + echo "Running LD_LIBRARY_PATH=$LIB_PATH:$LD_LIBRARY_PATH DYLD_LIBRARY_PATH=$LIB_PATH:$DYLD_LIBRARY_PATH PYTHONPATH=$PYTHONPATH python $test" + LD_LIBRARY_PATH=$LIB_PATH:$LD_LIBRARY_PATH DYLD_LIBRARY_PATH=$LIB_PATH:$DYLD_LIBRARY_PATH PYTHONPATH=$PYTHONPATH python $test +done diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zkServer.sh b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zkServer.sh new file mode 100644 index 0000000..3b6ed37 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zkServer.sh @@ -0,0 +1,102 @@ +#!/bin/bash +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +if [ "x$1" == "x" ] +then + echo "USAGE: $0 startClean|start|stop" + exit 2 +fi + +if [ "x${base_dir}" == "x" ] +then + PROJECT_ROOT="../../" +else + PROJECT_ROOT=${base_dir} +fi +WORK_DIR=${PROJECT_ROOT}/zookeeper-contrib/zookeeper-contrib-zkpython/target/zkpython_tests +TEST_DIR=${PROJECT_ROOT}/zookeeper-contrib/zookeeper-contrib-zkpython/src/test + + +if [ -r "${WORK_DIR}/../zk.pid" ] +then + pid=`cat "${WORK_DIR}/../zk.pid"` + kill -9 $pid + rm -f "${WORK_DIR}/../zk.pid" +fi + +which lsof &> /dev/null +if [ $? -eq 0 ] +then + pid=`lsof -i :22182 | grep LISTEN | awk '{print $2}'` + if [ -n "$pid" ] + then + kill -9 $pid + fi +fi + + + + +if [ "x$1" == "xstartClean" ] +then + rm -rf ${WORK_DIR} +fi + + + +CLASSPATH="$CLASSPATH:${PROJECT_ROOT}/zookeeper-server/target/classes" +CLASSPATH="$CLASSPATH:${zk_base}/conf" + +for i in "${PROJECT_ROOT}"/zookeeper-server/target/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +for i in "${PROJECT_ROOT}"/zookeeper-server/src/main/resource/lib/*.jar +do + CLASSPATH="$CLASSPATH:$i" +done + +# Make sure nothing is left over from before +#fuser -skn tcp 22182/tcp + +case $1 in +start|startClean) + mkdir -p ${WORK_DIR}/zkdata + + rm -rf ${WORK_DIR}/ssl + mkdir -p ${WORK_DIR}/ssl + cp ${PROJECT_ROOT}/zookeeper-client/zookeeper-client-c/ssl/gencerts.sh ${WORK_DIR}/ssl/ + cd ${WORK_DIR}/ssl/ + ./gencerts.sh + cd - + + sed "s#WORKDIR#${WORK_DIR}#g" ${TEST_DIR}/zoo.cfg > "${WORK_DIR}/zoo.cfg" + java -Dzookeeper.extendedTypesEnabled=true -Dznode.container.checkIntervalMs=100 -cp $CLASSPATH org.apache.zookeeper.server.ZooKeeperServerMain "${WORK_DIR}/zoo.cfg" &> "${WORK_DIR}/zoo.log" & + pid=$! + echo -n $! > ${WORK_DIR}/../zk.pid + sleep 5 + ;; +stop) + # Already killed above + ;; +*) + echo "Unknown command " + $1 + exit 2 +esac + diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zktestbase.py b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zktestbase.py new file mode 100644 index 0000000..cafbec5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zktestbase.py @@ -0,0 +1,104 @@ +#!/usr/bin/python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import os +import unittest, threading, zookeeper +ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} + +class TestBase(unittest.TestCase): + SERVER_PORT = 22182 + SERVER_SSL_PORT = 22183 + + def __init__(self,methodName='runTest'): + unittest.TestCase.__init__(self,methodName) + self.host = "localhost:%d" % self.SERVER_PORT + self.sslhost = "localhost:%d" % self.SERVER_SSL_PORT + self.sslcert = "./target/zkpython_tests/ssl/server.crt,./target/zkpython_tests/ssl/client.crt,./target/zkpython_tests/ssl/clientkey.pem,password" + self.connected = False + self.handle = -1 + logdir = os.environ.get("ZKPY_LOG_DIR") + logfile = os.path.join(logdir, self.__class__.__name__ + ".log") + try: + f = open(logfile,"w") + zookeeper.set_log_stream(f) + except IOError: + print("Couldn't open " + logfile + " for writing") + + + def setUp(self): + self.callback_flag = False + self.cv = threading.Condition() + self.connected = False + def connection_watcher(handle, type, state, path): + self.cv.acquire() + self.connected = True + self.cv.notify() + self.cv.release() + + self.cv.acquire() + self.handle = zookeeper.init(self.host, connection_watcher) + self.cv.wait(15.0) + self.cv.release() + + if not self.connected: + raise Exception("Couldn't connect to host -", self.host) + + def newConnection(self): + cv = threading.Condition() + self.pending_connection = False + def connection_watcher(handle, type, state, path): + cv.acquire() + self.pending_connection = True + cv.notify() + cv.release() + + cv.acquire() + handle = zookeeper.init(self.host, connection_watcher) + cv.wait(15.0) + cv.release() + + if not self.pending_connection: + raise Exception("Couldn't connect to host -", self.host) + return handle + + def ensureDeleted(self,path): + self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle), "Not connected!") + try: + self.assertEqual(zookeeper.OK, zookeeper.delete(self.handle, path)) + except zookeeper.NoNodeException: + pass + + def ensureCreated(self,path,data="",flags=zookeeper.EPHEMERAL): + """ + It's possible not to get the flags you want here if the node already exists + """ + self.assertEqual(zookeeper.CONNECTED_STATE, zookeeper.state(self.handle), "Not connected!") + try: + self.assertEqual(path, zookeeper.create(self.handle, path, data, [ZOO_OPEN_ACL_UNSAFE], flags)) + except zookeeper.NodeExistsException: + pass + + def tearDown(self): + if self.connected: + zookeeper.close(self.handle) + + def all(self, iterable): + for element in iterable: + if not element: + return False + return True diff --git a/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zoo.cfg b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zoo.cfg new file mode 100644 index 0000000..2d4fc31 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zkpython/src/test/zoo.cfg @@ -0,0 +1,14 @@ +tickTime=500 +initLimit=10 +syncLimit=5 +dataDir=WORKDIR/zkdata +maxClientCnxns=200 + +clientPort=22182 +secureClientPort=22183 +serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory +ssl.keyStore.location=WORKDIR/ssl/server.jks +ssl.keyStore.password=password +ssl.trustStore.location=WORKDIR/ssl/servertrust.jks +ssl.trustStore.password=password + diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/Makefile.am b/zookeeper-contrib/zookeeper-contrib-zktreeutil/Makefile.am new file mode 100644 index 0000000..36da1a5 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/Makefile.am @@ -0,0 +1,4 @@ +## Process this file with automake to produce Makefile.in + +SUBDIRS = src + diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/README.txt b/zookeeper-contrib/zookeeper-contrib-zktreeutil/README.txt new file mode 100644 index 0000000..43b06fa --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/README.txt @@ -0,0 +1,74 @@ +========================================== +zktreeutil - Zookeeper Tree Data Utility +Author: Anirban Roy +Organization: Yahoo Inc. +========================================== + +zktreeutil program is intended to manage and manipulate zk-tree data quickly, effi- +ciently and with ease. The utility operates on free-form ZK-tree and hence can be used +for any cluster managed by Zookeeper. Here are the basic functionalities - + +EXPORT: The whole/partial ZK-tree is exported into a XML file. This helps in +capturing a current snapshot of the data for backup/analysis. For a subtree +export, one need to specify the path to the ZK-subtree with proper option. + +IMPORT: The ZK-tree can be imported from XML into ZK cluster. This helps in priming +the new ZK cluster with static configuration. The import can be non-intrusive by +making only the additions in the existing data. The import of subtree is also +possible by optionally providing the path to the ZK-subtree. + +DIFF: Creates a diff between live ZK data vs data saved in XML file. Diff can ignore +some ZK-tree branches (possibly dynamic data) on reading the optional ignore flag +from XML file. Diffing on a ZK-subtree achieved by providing path to ZK-subtree with +diff command. + +UPDATE: Make the incremental changes into the live ZK-tree from saved XML, essentia- +lly after running the diff. + +DUMP: Dumps the ZK-tree on the standard output device reading either from live ZK +server or XML file. Like export, ZK-subtree can be dumped with optionaly +providing the path to the ZK-subtree, and till a certain depth of the (sub)tree. + +The exported ZK data into XML file can be shortened by only keeping the static ZK +nodes which are required to prime a cluster. The dynamic zk nodes (created on-the- +fly) can be ignored by setting a 'ignore' attribute at the root node of the dynamic +subtree (see tests/zk_sample.xml), possibly deleting all inner ZK nodes under that. +Once ignored, the whole subtree is ignored during DIFF, UPDATE and WRITE. + +Pre-requisites +-------------- +1. Linux system with 2.6.X kernel. +2. Zookeeper C client library (locally built at ../../c/.libs) >= 3.X.X +3. Development build libraries (rpm packages): + a. boost-devel >= 1.32.0 + b. libxml2-devel >= 2.7.3 + c. log4cxx0100-devel >= 0.10.0 + +Build instructions +------------------ +1. cd into this directory +2. autoreconf -if +3. ./configure +4. make +5. 'zktreeutil' binary created under src directory + +Limitations +----------- +Current version works with text data only, binary data will be supported in future +versions. + +Testing and usage of zktreeutil +-------------------------------- +1. Run Zookeeper server locally on port 2181 +2. export LD_LIBRARY_PATH=../../c/.libs/:/usr/local/lib/ +3. ./src/zktreeutil --help # show help +4. ./src/zktreeutil --zookeeper=localhost:2181 --import --xmlfile=tests/zk_sample.xml 2>/dev/null # import sample ZK tree +5. ./src/zktreeutil --zookeeper=localhost:2181 --dump --path=/myapp/version-1.0 2>/dev/null # dump Zk subtree +5. ./src/zktreeutil --zookeeper=localhost:2181 --dump --depth=3 2>/dev/null # dump Zk tree till certain depth +6. ./src/zktreeutil --xmlfile=zk_sample.xml -D 2>/dev/null # dump the xml data +7. Change zk_sample.xml with adding/deleting/chaging some nodes +8. ./src/zktreeutil -z localhost:2181 -F -x zk_sample.xml -p /myapp/version-1.0/configuration 2>/dev/null # take a diff of changes +9. ./src/zktreeutil -z localhost:2181 -E 2>/dev/null > zk_sample2.xml # export the mofied ZK tree +10. ./src/zktreeutil -z localhost:2181 -U -x zk_sample.xml -p /myapp/version-1.0/distributions 2>/dev/null # update with incr. changes +11. ./src/zktreeutil --zookeeper=localhost:2181 --import --force --xmlfile=zk_sample2.xml 2>/dev/null # re-prime the ZK tree + diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/build.xml b/zookeeper-contrib/zookeeper-contrib-zktreeutil/build.xml new file mode 100644 index 0000000..7ff6749 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/configure.ac b/zookeeper-contrib/zookeeper-contrib-zktreeutil/configure.ac new file mode 100644 index 0000000..b4a82a7 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/configure.ac @@ -0,0 +1,66 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) + +AC_INIT([zktreeutil], [1.0.0]) +AM_INIT_AUTOMAKE(foreign) + +AC_CONFIG_SRCDIR([src]) +AM_CONFIG_HEADER([config.h]) + +PACKAGE=zktreeutil +VERSION=1.0.0 + +AC_SUBST(PACKAGE) +AC_SUBST(VERSION) +BUILD_PATH="`pwd`" + +# Checks for programs. +AC_LANG_CPLUSPLUS +AC_PROG_CXX + +# Checks for libxm2. +AM_PATH_XML2(2.7.3) +XML2_INCLUDE="/usr/include/libxml2" +AC_SUBST(XML2_INCLUDE) + +# Zookeeper C client +ZOOKEEPER_PATH=${BUILD_PATH}/../../c +AC_CHECK_LIB(zookeeper_mt, main, [ZOOKEEPER="-L${ZOOKEEPER_PATH}/.libs -lzookeeper_mt"],,["-L${ZOOKEEPER_PATH}/.libs"]) +if test -z "${ZOOKEEPER}"; then + AC_ERROR("... zookeeper C client not found!") +fi + +AC_SUBST(ZOOKEEPER) +AC_SUBST(ZOOKEEPER_PATH) + +### log4cxx ### + +LOG4CXX_VERSION="0.10.0" +LOG4CXX_INCLUDE="/usr/local/include" +LOG4CXX_LIB_PATH="/usr/local/lib" +AC_CHECK_LIB(log4cxx, main, [LOG4CXX="-L${LOG4CXX_LIB_PATH} -llog4cxx"],,["-L${LOG4CXX_LIB_PATH}"]) +if test -z "${LOG4CXX}"; then + AC_ERROR("... log4cxx not found!") +fi + +AC_SUBST(LOG4CXX) +AC_SUBST(LOG4CXX_VERSION) +AC_SUBST(LOG4CXX_INCLUDE) + +# Checks for header files. +AC_HEADER_DIRENT +AC_HEADER_STDC +AC_CHECK_HEADERS([stdlib.h string.h stdio.h unistd.h boost/shared_ptr.hpp boost/algorithm/string.hpp boost/algorithm/string/split.hpp]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_CONST +AC_C_INLINE +AC_TYPE_SIZE_T +AC_C_VOLATILE + +AC_CONFIG_FILES([Makefile]) +AC_CONFIG_FILES([src/Makefile]) +AC_OUTPUT diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/Makefile.am b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/Makefile.am new file mode 100644 index 0000000..641077a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/Makefile.am @@ -0,0 +1,24 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +AM_CXXFLAGS = -I${ZOOKEEPER_PATH}/include -I${ZOOKEEPER_PATH}/generated \ + -I$(top_srcdir)/include -I${LOG4CXX_INCLUDE} -I/usr/include \ + -I${XML2_INCLUDE} + +bin_PROGRAMS = zktreeutil + +zktreeutil_SOURCES = ZkAdaptor.cc ZkTreeUtil.cc ZkTreeUtilMain.cc +zktreeutil_LDADD = ${ZOOKEEPER} ${XML_LIBS} ${LOG4CXX} diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/SimpleTree.h b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/SimpleTree.h new file mode 100644 index 0000000..8226f05 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/SimpleTree.h @@ -0,0 +1,150 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __SIMPLE_TREE_H__ +#define __SIMPLE_TREE_H__ + +#include +#include + +namespace zktreeutil +{ + using std::vector; + + /** + * \brief A simple tree data-structure template. + */ + template < class KeyType, class DataType > class SimpleTreeNode + { + private: + /** + * \brief The type representing simple-tree node smart-pointer. + */ + typedef boost::shared_ptr< SimpleTreeNode< KeyType, DataType > > SimpleTreeNodeSptr; + + public: + /** + * \brief Constructor. + * + * @param isRoot the flag indicating whether the node is root. + */ + SimpleTreeNode (bool isRoot=false) : isRoot_(isRoot) + { + } + + /** + * \brief Constructor. + * + * @param key the key stored at the tree node + * @param isRoot the flag indicating whether the node is root + */ + SimpleTreeNode (const KeyType& key, bool isRoot=false) : + isRoot_(isRoot), key_(key) + { + } + + /** + * \brief Constructor. + * + * @param key the key stored at the tree node + * @param val the value stored at the tree node + * @param isRoot the flag indicating whether the node is root + */ + SimpleTreeNode (const KeyType& key, const DataType& val, bool isRoot=false) : + isRoot_(isRoot), key_(key), val_(val) + { + } + + /** + * \brief Destructor. + */ + ~SimpleTreeNode () throw() {} + + /** + * \brief Add a child node to this node. + * + * @param node the child node to be added + */ + void addChild (const SimpleTreeNodeSptr node) { children_.push_back (node); } + + /** + * \brief Sets the key of this node. + * + * @param key the key to be set + */ + void setKey (const KeyType& key) { key_ = key; } + + /** + * \brief Sets the data of this node. + * + * @param val the value to be set + */ + void setData (const DataType& val) { val_ = val; } + + /** + * \brief Gets the key of this node. + * + * @return the key of this node + */ + KeyType getKey () const { return key_; } + + /** + * \brief Gets the data of this node. + * + * @return the value of this node + */ + DataType getData () const { return val_; } + + /** + * \brief Gets the i'th of this node. + * + * @param idx the index of the child node + * @return the child node + */ + SimpleTreeNodeSptr getChild (unsigned idx) const { return children_[idx]; } + + /** + * \brief Gets the number of children of this node. + * + * @return the number of children + */ + unsigned numChildren () const { return children_.size(); } + + /** + * \brief Indicates whether this node is root. + * + * @return 'true' if this node is root, 'false' otherwise + */ + bool isRoot () const { return isRoot_; } + + /** + * \brief Indicates whether this node is leaf node. + * + * @return 'true' if this node is leaf node, 'false' otherwise + */ + bool isLeaf () const { return !numChildren(); } + + private: + bool isRoot_; // Flag indicates if the node is root + KeyType key_; // Key of this node + DataType val_; // Value of this node + vector< SimpleTreeNodeSptr > children_; // List of children of this node + }; +} + +#endif // __SIMPLE_TREE_H__ diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkAdaptor.cc b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkAdaptor.cc new file mode 100644 index 0000000..1df175a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkAdaptor.cc @@ -0,0 +1,513 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "ZkAdaptor.h" +#include +#include +#include +#include +#include + +// Logger +static log4cxx::LoggerPtr zkLoggerPtr = log4cxx::Logger::getLogger ("zookeeper.core"); + +namespace zktreeutil +{ + /** + * \brief This class provides logic for checking if a request can be retried. + */ + class RetryHandler + { + public: + RetryHandler(const ZooKeeperConfig &zkConfig) : m_zkConfig(zkConfig) + { + if (zkConfig.getAutoReconnect()) + retries = 2; + else + retries = 0; + } + + /** + * \brief Attempts to fix a side effect of the given RC. + * + * @param rc the ZK error code + * @return whether the error code has been handled and the caller should + * retry an operation the caused this error + */ + bool handleRC(int rc) + { + //check if the given error code is recoverable + if (!retryOnError(rc)) + return false; + + std::cerr << "[zktreeuti] Number of retries left: " << retries << std::endl; + if (retries-- > 0) + return true; + else + return false; + } + + private: + /** + * The ZK config. + */ + const ZooKeeperConfig &m_zkConfig; + + /** + * The number of outstanding retries. + */ + int retries; + + /** + * Checks whether the given error entitles this adapter + * to retry the previous operation. + * + * @param zkErrorCode one of the ZK error code + */ + static bool retryOnError(int zkErrorCode) + { + return (zkErrorCode == ZCONNECTIONLOSS || zkErrorCode == ZOPERATIONTIMEOUT); + } + }; + + + // ======================================================================= + + ZooKeeperAdapter::ZooKeeperAdapter(ZooKeeperConfig config) throw(ZooKeeperException) : + m_zkConfig(config), + mp_zkHandle(NULL) + { + // Enforce setting up appropriate ZK log level + if (zkLoggerPtr->isDebugEnabled() +#ifdef LOG4CXX_TRACE + || zkLoggerPtr->isTraceEnabled() +#endif + ) + { + zoo_set_debug_level( ZOO_LOG_LEVEL_DEBUG ); + } else if (zkLoggerPtr->isInfoEnabled()) { + zoo_set_debug_level( ZOO_LOG_LEVEL_INFO ); + } else if (zkLoggerPtr->isWarnEnabled()) { + zoo_set_debug_level( ZOO_LOG_LEVEL_WARN ); + } else { + zoo_set_debug_level( ZOO_LOG_LEVEL_ERROR ); + } + + // Establish the connection + reconnect(); + } + + ZooKeeperAdapter::~ZooKeeperAdapter() + { + try + { + disconnect(); + } + catch (std::exception &e) + { + std::cerr << "[zktreeutil] An exception while disconnecting from ZK: " + << e.what() + << std::endl; + } + } + + void ZooKeeperAdapter::validatePath(const string &path) throw(ZooKeeperException) + { + if (path.find ("/") != 0) + { + std::ostringstream oss; + oss << "Node path must start with '/' but" "it was '" + << path + << "'"; + throw ZooKeeperException (oss.str()); + } + if (path.length() > 1) + { + if (path.rfind ("/") == path.length() - 1) + { + std::ostringstream oss; + oss << "Node path must not end with '/' but it was '" + << path + << "'"; + throw ZooKeeperException (oss.str()); + } + if (path.find( "//" ) != string::npos) + { + std::ostringstream oss; + oss << "Node path must not contain '//' but it was '" + << path + << "'"; + throw ZooKeeperException (oss.str()); + } + } + } + + void ZooKeeperAdapter::disconnect() + { + if (mp_zkHandle != NULL) + { + zookeeper_close (mp_zkHandle); + mp_zkHandle = NULL; + } + } + + void ZooKeeperAdapter::reconnect() throw(ZooKeeperException) + { + // Clear the connection state + disconnect(); + + // Establish a new connection to ZooKeeper + mp_zkHandle = zookeeper_init( m_zkConfig.getHosts().c_str(), + NULL, + m_zkConfig.getLeaseTimeout(), + 0, + NULL, + 0); + if (mp_zkHandle == NULL) + { + // Invalid handle returned + std::ostringstream oss; + oss << "Unable to connect to ZK running at '" + << m_zkConfig.getHosts() + << "'"; + throw ZooKeeperException (oss.str()); + } + + // Enter into connect loop + int64_t connWaitTime = m_zkConfig.getConnectTimeout(); + while (1) + { + int state = zoo_state (mp_zkHandle); + if (state == ZOO_CONNECTED_STATE) + { + // connected + std::cerr << "[zktreeutil] Connected! mp_zkHandle: " + << mp_zkHandle + << std::endl; + return; + } + else if ( state && state != ZOO_CONNECTING_STATE) + { + // Not connecting any more... some other issue + std::ostringstream oss; + oss << "Unable to connect to ZK running at '" + << m_zkConfig.getHosts() + << "'; state=" + << state; + throw ZooKeeperException (oss.str()); + } + + // Still connecting, wait and come back + struct timeval now; + gettimeofday( &now, NULL ); + int64_t milliSecs = -(now.tv_sec * 1000LL + now.tv_usec / 1000); + std::cerr << "[zktreeutil] About to wait 1 sec" << std::endl; + sleep (1); + gettimeofday( &now, NULL ); + milliSecs += now.tv_sec * 1000LL + now.tv_usec / 1000; + connWaitTime -= milliSecs; + // Timed out !!! + if (connWaitTime <= 0) + break; + } + + // Timed out while connecting + std::ostringstream oss; + oss << "Timed out while connecting to ZK running at '" + << m_zkConfig.getHosts() + << "'"; + throw ZooKeeperException (oss.str()); + } + + void ZooKeeperAdapter::verifyConnection() throw(ZooKeeperException) + { + // Check connection state + int state = zoo_state (mp_zkHandle); + if (state != ZOO_CONNECTED_STATE) + { + if (m_zkConfig.getAutoReconnect()) + { + // Trying to reconnect + std::cerr << "[zktreeutil] Trying to reconnect..." << std::endl; + reconnect(); + } + else + { + std::ostringstream oss; + oss << "Disconnected from ZK running at '" + << m_zkConfig.getHosts() + << "'; state=" + << state; + throw ZooKeeperException (oss.str()); + } + } + } + + bool ZooKeeperAdapter::createNode(const string &path, + const string &value, + int flags, + bool createAncestors) throw(ZooKeeperException) + { + const int MAX_PATH_LENGTH = 1024; + char realPath[MAX_PATH_LENGTH]; + realPath[0] = 0; + + int rc; + RetryHandler rh(m_zkConfig); + do + { + verifyConnection(); + rc = zoo_create( mp_zkHandle, + path.c_str(), + value.c_str(), + value.length(), + &ZOO_OPEN_ACL_UNSAFE, + flags, + realPath, + MAX_PATH_LENGTH ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) // check return status + { + if (rc == ZNODEEXISTS) + { + //the node already exists + std::cerr << "[zktreeutil] ZK node " << path << " already exists" << std::endl; + return false; + } + else if (rc == ZNONODE && createAncestors) + { + std::cerr << "[zktreeutil] Intermediate ZK node missing in path " << path << std::endl; + //one of the ancestors doesn't exist so lets start from the root + //and make sure the whole path exists, creating missing nodes if + //necessary + for (string::size_type pos = 1; pos != string::npos; ) + { + pos = path.find( "/", pos ); + if (pos != string::npos) + { + try + { + createNode( path.substr( 0, pos ), "", 0, true ); + } + catch (ZooKeeperException &e) + { + throw ZooKeeperException( string("Unable to create " "node ") + path, rc ); + } + pos++; + } + else + { + // No more path components + return createNode( path, value, flags, false ); + } + } + } + + // Unexpected error during create + std::cerr << "[zktreeutil] Error in creating ZK node " << path << std::endl; + throw ZooKeeperException( string("Unable to create node ") + path, rc ); + } + + // Success + std::cerr << "[zktreeutil] " << realPath << " has been created" << std::endl; + return true; + } + + bool ZooKeeperAdapter::deleteNode(const string &path, + bool recursive, + int version) throw(ZooKeeperException) + { + // Validate the zk path + validatePath( path ); + + int rc; + RetryHandler rh(m_zkConfig); + do + { + verifyConnection(); + rc = zoo_delete( mp_zkHandle, path.c_str(), version ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) //check return status + { + if (rc == ZNONODE) + { + std::cerr << "[zktreeutil] ZK Node " + << path + << " does not exist" + << std::endl; + return false; + } + if (rc == ZNOTEMPTY && recursive) + { + std::cerr << "[zktreeutil] ZK Node " + << path + << " not empty; deleting..." + << std::endl; + //get all children and delete them recursively... + vector nodeList = getNodeChildren (path); + for (vector::const_iterator i = nodeList.begin(); + i != nodeList.end(); + ++i) { + deleteNode( *i, true ); + } + //...and finally attempt to delete the node again + return deleteNode( path, false ); + } + + // Unexpected return without success + std::cerr << "[zktreeutil] Unable to delete ZK node " << path << std::endl; + throw ZooKeeperException( string("Unable to delete node ") + path, rc ); + } + + // success + std::cerr << "[zktreeutil] " << path << " has been deleted" << std::endl; + return true; + } + + vector< string > ZooKeeperAdapter::getNodeChildren (const string &path) throw (ZooKeeperException) + { + // Validate the zk path + validatePath( path ); + + String_vector children; + memset( &children, 0, sizeof(children) ); + int rc; + RetryHandler rh(m_zkConfig); + do + { + verifyConnection(); + rc = zoo_get_children( mp_zkHandle, + path.c_str(), + 0, + &children ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) // check return code + { + std::cerr << "[zktreeutil] Error in fetching children of " << path << std::endl; + throw ZooKeeperException( string("Unable to get children of node ") + path, rc ); + } + else + { + vector< string > nodeList; + for (int i = 0; i < children.count; ++i) + { + //convert each child's path from relative to absolute + string absPath(path); + if (path != "/") + { + absPath.append( "/" ); + } + absPath.append( children.data[i] ); + nodeList.push_back( absPath ); + } + + //make sure the order is always deterministic + sort( nodeList.begin(), nodeList.end() ); + return nodeList; + } + } + + bool ZooKeeperAdapter::nodeExists(const string &path) throw(ZooKeeperException) + { + // Validate the zk path + validatePath( path ); + + struct Stat tmpStat; + struct Stat* stat = &tmpStat; + memset( stat, 0, sizeof(Stat) ); + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + rc = zoo_exists( mp_zkHandle, + path.c_str(), + 0, + stat ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) + { + if (rc == ZNONODE) + return false; + // Some error + std::cerr << "[zktreeutil] Error in checking existence of " << path << std::endl; + throw ZooKeeperException( string("Unable to check existence of node ") + path, rc ); + } else { + return true; + } + } + + string ZooKeeperAdapter::getNodeData(const string &path) throw(ZooKeeperException) + { + // Validate the zk path + validatePath( path ); + + const int MAX_DATA_LENGTH = 128 * 1024; + char buffer[MAX_DATA_LENGTH]; + memset( buffer, 0, MAX_DATA_LENGTH ); + struct Stat tmpStat; + struct Stat* stat = &tmpStat; + memset( stat, 0, sizeof(Stat) ); + + int rc; + int len; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + len = MAX_DATA_LENGTH - 1; + rc = zoo_get( mp_zkHandle, + path.c_str(), + 0, + buffer, &len, stat ); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) // checl return code + { + std::cerr << "[zktreeutil] Error in fetching value of " << path << std::endl; + throw ZooKeeperException( string("Unable to get data of node ") + path, rc ); + } + + // return data + return string( buffer, buffer + len ); + } + + void ZooKeeperAdapter::setNodeData(const string &path, + const string &value, + int version) throw(ZooKeeperException) + { + // Validate the zk path + validatePath( path ); + + int rc; + RetryHandler rh(m_zkConfig); + do { + verifyConnection(); + rc = zoo_set( mp_zkHandle, + path.c_str(), + value.c_str(), + value.length(), + version); + } while (rc != ZOK && rh.handleRC(rc)); + if (rc != ZOK) // check return code + { + std::cerr << "[zktreeutil] Error in setting value of " << path << std::endl; + throw ZooKeeperException( string("Unable to set data for node ") + path, rc ); + } + // success + } + +} /* end of 'namespace zktreeutil' */ diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkAdaptor.h b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkAdaptor.h new file mode 100644 index 0000000..4b68e28 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkAdaptor.h @@ -0,0 +1,327 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __ZK_ADAPTER_H__ +#define __ZK_ADAPTER_H__ + +#include +#include + +extern "C" { +#include "zookeeper.h" +} + +namespace zktreeutil +{ + using std::string; + using std::vector; + + /** + * \brief A cluster related exception. + */ + class ZooKeeperException : public std::exception + { + public: + + /** + * \brief Constructor. + * + * @param msg the detailed message associated with this exception + */ + ZooKeeperException(const string& msg) : + m_message(msg), + m_zkErrorCode(0) {} + + /** + * \brief Constructor. + * + * @param msg the detailed message associated with this exception + * @param errorCode the ZK error code associated with this exception + */ + ZooKeeperException(const string &msg, int errorCode) : + m_zkErrorCode(errorCode) + { + char tmp[100]; + sprintf( tmp, " (ZK error code: %d)", errorCode ); + m_message = msg + tmp; + } + + /** + * \brief Destructor. + */ + ~ZooKeeperException() throw() {} + + /** + * \brief Returns detailed description of the exception. + */ + const char *what() const throw() + { + return m_message.c_str(); + } + + /** + * \brief Returns the ZK error code. + */ + int getZKErrorCode() const + { + return m_zkErrorCode; + } + + private: + + /** + * The detailed message associated with this exception. + */ + string m_message; + + /** + * The optional error code received from ZK. + */ + int m_zkErrorCode; + + }; + + /** + * \brief This class encapsulates configuration of a ZK client. + */ + class ZooKeeperConfig + { + public: + + /** + * \brief Constructor. + * + * @param hosts the comma separated list of host and port pairs of ZK nodes + * @param leaseTimeout the lease timeout (heartbeat) + * @param autoReconnect whether to allow for auto-reconnect + * @param connectTimeout the connect timeout, in milliseconds; + */ + ZooKeeperConfig(const string &hosts, + int leaseTimeout, + bool autoReconnect = true, + long long int connectTimeout = 15000) + : m_hosts(hosts), + m_leaseTimeout(leaseTimeout), + m_autoReconnect(autoReconnect), + m_connectTimeout(connectTimeout) {} + + /** + * \brief Returns the list of ZK hosts to connect to. + */ + string getHosts() const { return m_hosts; } + + /** + * \brief Returns the lease timeout. + */ + int getLeaseTimeout() const { return m_leaseTimeout; } + + /** + * \brief Returns whether {@link ZooKeeperAdapter} should attempt + * \brief to automatically reconnect in case of a connection failure. + */ + bool getAutoReconnect() const { return m_autoReconnect; } + + /** + * \brief Gets the connect timeout. + * + * @return the connect timeout + */ + long long int getConnectTimeout() const { return m_connectTimeout; } + + private: + + /** + * The host addresses of ZK nodes. + */ + const string m_hosts; + + /** + * The ZK lease timeout. + */ + const int m_leaseTimeout; + + /** + * True if this adapater should attempt to autoreconnect in case + * the current session has been dropped. + */ + const bool m_autoReconnect; + + /** + * How long to wait, in milliseconds, before a connection + * is established to ZK. + */ + const long long int m_connectTimeout; + }; + + /** + * \brief This is a wrapper around ZK C synchrounous API. + */ + class ZooKeeperAdapter + { + public: + /** + * \brief Constructor. + * Attempts to create a ZK adapter, optionally connecting + * to the ZK. Note, that if the connection is to be established + * and the given listener is NULL, some events may be lost, + * as they may arrive asynchronously before this method finishes. + * + * @param config the ZK configuration + * @throw ZooKeeperException if cannot establish connection to the given ZK + */ + ZooKeeperAdapter(ZooKeeperConfig config) throw(ZooKeeperException); + + /** + * \brief Destructor. + */ + ~ZooKeeperAdapter(); + + /** + * \brief Returns the current config. + */ + const ZooKeeperConfig &getZooKeeperConfig() const { return m_zkConfig; } + + /** + * \brief Restablishes connection to the ZK. + * If this adapter is already connected, the current connection + * will be dropped and a new connection will be established. + * + * @throw ZooKeeperException if cannot establish connection to the ZK + */ + void reconnect() throw(ZooKeeperException); + + /** + * \brief Disconnects from the ZK and unregisters {@link #mp_zkHandle}. + */ + void disconnect(); + + /** + * \brief Creates a new node identified by the given path. + * This method will optionally attempt to create all missing ancestors. + * + * @param path the absolute path name of the node to be created + * @param value the initial value to be associated with the node + * @param flags the ZK flags of the node to be created + * @param createAncestors if true and there are some missing ancestor nodes, + * this method will attempt to create them + * + * @return true if the node has been successfully created; false otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool createNode(const string &path, + const string &value = "", + int flags = 0, + bool createAncestors = true) throw(ZooKeeperException); + + /** + * \brief Deletes a node identified by the given path. + * + * @param path the absolute path name of the node to be deleted + * @param recursive if true this method will attempt to remove + * all children of the given node if any exist + * @param version the expected version of the node. The function will + * fail if the actual version of the node does not match + * the expected version + * + * @return true if the node has been deleted; false otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool deleteNode(const string &path, + bool recursive = false, + int version = -1) throw(ZooKeeperException); + + /** + * \brief Retrieves list of all children of the given node. + * + * @param path the absolute path name of the node for which to get children + * @return the list of absolute paths of child nodes, possibly empty + * @throw ZooKeeperException if the operation has failed + */ + vector getNodeChildren( const string &path) throw(ZooKeeperException); + + /** + * \brief Check the existence of path to a znode. + * + * @param path the absolute path name of the znode + * @return TRUE if the znode exists; FALSE otherwise + * @throw ZooKeeperException if the operation has failed + */ + bool nodeExists(const string &path) throw(ZooKeeperException); + + /** + * \brief Gets the given node's data. + * + * @param path the absolute path name of the node to get data from + * + * @return the node's data + * @throw ZooKeeperException if the operation has failed + */ + string getNodeData(const string &path) throw(ZooKeeperException); + + /** + * \brief Sets the given node's data. + * + * @param path the absolute path name of the node to get data from + * @param value the node's data to be set + * @param version the expected version of the node. The function will + * fail if the actual version of the node does not match + * the expected version + * + * @throw ZooKeeperException if the operation has failed + */ + void setNodeData(const string &path, + const string &value, + int version = -1) throw(ZooKeeperException); + + /** + * \brief Validates the given path to a node in ZK. + * + * @param the path to be validated + * + * @throw ZooKeeperException if the given path is not valid + * (for instance it doesn't start with "/") + */ + static void validatePath(const string &path) throw(ZooKeeperException); + + private: + + /** + * Verifies whether the connection is established, + * optionally auto reconnecting. + * + * @throw ZooKeeperConnection if this client is disconnected + * and auto-reconnect failed or was not allowed + */ + void verifyConnection() throw(ZooKeeperException); + + private: + + /** + * The current ZK configuration. + */ + const ZooKeeperConfig m_zkConfig; + + /** + * The current ZK session. + */ + zhandle_t *mp_zkHandle; + }; + +} /* end of 'namespace zktreeutil' */ + +#endif /* __ZK_ADAPTER_H__ */ diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtil.cc b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtil.cc new file mode 100644 index 0000000..270bf31 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtil.cc @@ -0,0 +1,705 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include "ZkTreeUtil.h" + +#include +#include +#include +#include +#include + +namespace zktreeutil +{ + using std::map; + using std::pair; + + static ZkTreeNodeSptr loadZkTree_ (ZooKeeperAdapterSptr zkHandle, + const string& path) + { + // Extract the node value + string value = zkHandle->getNodeData(path); + + // Extract nodename from the path + string nodename = "/"; + if (path != "/") + { + vector< string > nodes; + boost::split(nodes, path, boost::is_any_of ("/") ); + nodename = nodes[nodes.size()-1]; + } + + // Create tree-node with name and value + ZkTreeNodeSptr nodeSptr = ZkTreeNodeSptr (new ZkTreeNode (nodename, value)); + std::cerr << "[zktreeutil] loaded nodename: " + << nodename + << " value: " + << value + << std::endl; + + // Load all the children + vector< string > cnodes = zkHandle->getNodeChildren (path); + for (unsigned i = 0; i < cnodes.size(); i++) + nodeSptr->addChild (loadZkTree_ (zkHandle, cnodes[i])); + + // Return the constructed node + return nodeSptr; + } + + static ZkTreeNodeSptr loadZkTreeXml_ (xmlNode* xmlNodePtr) + { + // Null check + if (xmlNodePtr == NULL) + { + std::cerr << "[zktreeutil] empty XML node encountered" << std::endl; + exit (-1); + } + + // Get the node name + xmlChar* name = xmlGetProp (xmlNodePtr, BAD_CAST "name"); + string nameStr = (const char*)name; + std::cerr << "[zktreeutil] node name: " << nameStr; + xmlFree (name); + // Get the node value + string valueStr; + xmlChar* value = xmlGetProp (xmlNodePtr, BAD_CAST "value"); + if (value) + { + valueStr = (const char*)value; + std::cerr << " value: " << valueStr; + } + xmlFree (value); + // Get the ignore flag + bool doIgnore = false; + xmlChar* ignore = xmlGetProp (xmlNodePtr, BAD_CAST "ignore"); + if (ignore) + { + string ignoreStr = (const char*) ignore; + if (ignoreStr == "true" || ignoreStr == "yes" || ignoreStr == "1") + { + doIgnore = true; + std::cerr << " "; + } + } + xmlFree (ignore); + std::cerr << std::endl; + + // Create the zk node + ZkTreeNodeSptr nodeSptr = + ZkTreeNodeSptr (new ZkTreeNode (nameStr, + ZkNodeData (valueStr, doIgnore))); + + // Load the children + for (xmlNode* chldNode = xmlNodePtr->children; + chldNode; + chldNode = chldNode->next) + if (chldNode->type == XML_ELEMENT_NODE) + nodeSptr->addChild (loadZkTreeXml_ (chldNode)); + + // Return the loaded node + return nodeSptr; + } + + static void writeZkTree_ (ZooKeeperAdapterSptr zkHandle, + const ZkTreeNodeSptr zkNodeSptr, + const string& path) + { + // Create the path in zk-tree + zkHandle->createNode(path.c_str(), "", 0, false); + std::cerr << "[zktreeutil] created key: " << path << std::endl; + // Set value for the path + string value = zkNodeSptr->getData().value; + if (value != "") + { + zkHandle->setNodeData (path.c_str(), value.c_str()); + std::cerr << "[zktreeutil] set value: " << std::endl; + } + + // Go deep to write the subtree rooted in the node, if not to be ignored + if (!(zkNodeSptr->getData().ignoreUpdate)) + { + for (unsigned i=0; i < zkNodeSptr->numChildren(); i++) + { + ZkTreeNodeSptr childNodeSptr = zkNodeSptr->getChild (i); + // Add the node name into the path and write in zk-tree + string cpath = ((path != "/")? path : "") + + string("/") + + childNodeSptr->getKey(); + writeZkTree_ (zkHandle, childNodeSptr, cpath); + } + } + + return; + } + + static void addTreeZkAction_ (const ZkTreeNodeSptr zkNodeSptr, + const string& path, + vector< ZkAction >& actions) + { + // Create the key + actions.push_back (ZkAction (ZkAction::CREATE, path)); + + // Set value for the new key + if (zkNodeSptr->getData().value != "") + actions.push_back (ZkAction (ZkAction::VALUE, + path, + zkNodeSptr->getData().value)); + + // Add all the children + for (unsigned i=0; i < zkNodeSptr->numChildren(); i++) + { + ZkTreeNodeSptr childSptr = zkNodeSptr->getChild (i); + string cpath = path + string("/") + childSptr->getKey(); + addTreeZkAction_ (childSptr, cpath, actions); + } + + return; + } + + static xmlNodePtr dumpZkTreeXml_ (const ZkTreeNodeSptr zkNodeSptr) + { + // Create xml node with zknode name and value + string nodename = zkNodeSptr->getKey (); + string value = zkNodeSptr->getData().value; + xmlNodePtr node = xmlNewNode(NULL, BAD_CAST "zknode"); + xmlNewProp (node, BAD_CAST "name", BAD_CAST nodename.c_str()); + if (value.length()) + xmlNewProp (node, BAD_CAST "value", BAD_CAST value.c_str()); + + // Add all the children rotted at this node + for (unsigned i=0; i < zkNodeSptr->numChildren(); i++) + xmlAddChild (node, dumpZkTreeXml_ (zkNodeSptr->getChild (i))); + + // Return xml node + return node; + } + + static void dumpZkTree_ (const ZkTreeNodeSptr zkNodeSptr, + int maxLevel, + int level, + vector< bool >& masks) + { + // Check the max. dlevel to be dumped + if (level > maxLevel) + return; + + + // Create branch + for (int i=0; i < level; i++) + { + if ( i== level-1) std::cout << "| "; + else if (masks[i]) std::cout << " "; + else std::cout << "| "; + } + std::cout << std::endl; + for (int i=0; i < level-1; i++) + { + if (masks[i]) std::cout << " "; + else std::cout << "| "; + } + + // Dump the node name and value + std::cout << "|--[" << zkNodeSptr->getKey(); + if (zkNodeSptr->getData().value != "") + std::cout << " => " << zkNodeSptr->getData().value; + std::cout << "]" << std::endl; + + // Dump all the children + for (unsigned i=0; i < zkNodeSptr->numChildren(); i++) + { + // Add mask for last child + if (i == zkNodeSptr->numChildren()-1) + masks.push_back(true); + else + masks.push_back(false); + dumpZkTree_ (zkNodeSptr->getChild (i), maxLevel, level+1, masks); + } + + masks.pop_back(); + return; + } + + static ZkTreeNodeSptr traverseBranch_ (const ZkTreeNodeSptr& zkRootSptr, + const string& path) + { + // Check if the tree is loaded into memory + if (zkRootSptr == NULL) + { + string errMsg = "[zktreeutil] null root passed for traversing"; + std::cout << errMsg << std::endl; + throw std::logic_error (errMsg); + } + + // Split the path and add intermediate znodes + vector< string > nodes; + boost::split(nodes, path, boost::is_any_of ("/") ); + + // Start traversing the tree + ZkTreeNodeSptr currNodeSptr = zkRootSptr; + for (unsigned znode_idx = 1; znode_idx < nodes.size(); znode_idx++) + { + bool found = false; + for (unsigned i=0; i < currNodeSptr->numChildren(); i++) + { + ZkTreeNodeSptr childNodeSptr = currNodeSptr->getChild(i); + if (childNodeSptr->getKey() == nodes[znode_idx]) + { + // Found! go to the znode + currNodeSptr = childNodeSptr; + found = true; + break; + } + } + if (!found) // No such znode found; return NULL node-ptr + { + string errMsg = string("[zktreeutil] unknown znode during traversal: ") + + nodes[znode_idx]; + std::cout << errMsg << std::endl; + throw std::logic_error (errMsg); + } + } + + return currNodeSptr; + } + + static ZkTreeNodeSptr createAncestors_ (const string& path) + { + // Create the root znode + ZkTreeNodeSptr zkRootSptr = ZkTreeNodeSptr (new ZkTreeNode ("/")); + ZkTreeNodeSptr currNodeSptr = zkRootSptr; + // Split the path and add intermediate znodes + vector< string > nodes; + boost::split(nodes, path, boost::is_any_of ("/") ); + for (unsigned i=1; i < nodes.size()-1; i++) + { + ZkTreeNodeSptr childNodeSptr = ZkTreeNodeSptr (new ZkTreeNode (nodes[i])); + currNodeSptr->addChild (childNodeSptr); + currNodeSptr = childNodeSptr; + } + + //Return the root of the branch + return zkRootSptr; + } + + ZooKeeperAdapterSptr ZkTreeUtil::get_zkHandle (const string& zkHosts) + { + try + { + // Create an instance of ZK adapter. + ZooKeeperConfig config (zkHosts, 10000); + ZooKeeperAdapterSptr zkHandleSptr = + ZooKeeperAdapterSptr (new ZooKeeperAdapter (config)); + return zkHandleSptr; + } + catch (const ZooKeeperException &e) + { + std::cerr << "[zktreeutil] zooKeeper exception caught: " + << e.what() + << std::endl; + throw; + } + catch (std::exception &stde) + { + std::cerr << "[zktreeutil] standard exception caught: " + << stde.what() + << std::endl; + throw; + } + catch (...) + { + std::cerr + << "[zktreeutil] unknown exception while connecting to zookeeper" + << std::endl; + throw; + } + } + + + void ZkTreeUtil::loadZkTree (const string& zkHosts, + const string& path, + bool force) + { + // Check if already loaded + if (loaded_ && !force) + { + std::cerr << "[zktreeutil] zk-tree already loaded into memory" + << std::endl; + return; + } + + // Connect to ZK server + ZooKeeperAdapterSptr zkHandle = get_zkHandle (zkHosts); + std::cerr << "[zktreeutil] connected to ZK serverfor reading" + << std::endl; + + // Check the existence of the path to znode + if (!zkHandle->nodeExists (path)) + { + string errMsg = string("[zktreeutil] path does not exists : ") + path; + std::cout << errMsg << std::endl; + throw std::logic_error (errMsg); + } + + // Load the rooted (sub)tree + ZkTreeNodeSptr zkSubrootSptr = loadZkTree_ (zkHandle, path); + + // Create the ancestors before loading the rooted subtree + if (path != "/") + { + zkRootSptr_ = createAncestors_(path); + string ppath = path.substr (0, path.rfind('/')); + ZkTreeNodeSptr parentSptr = traverseBranch_( zkRootSptr_, ppath); + parentSptr->addChild (zkSubrootSptr); + } + else // Loaded entire zk-tree + { + zkRootSptr_ = zkSubrootSptr; + } + + // Set load flag + loaded_ = true; + return; + } + + void ZkTreeUtil::loadZkTreeXml (const string& zkXmlConfig, + bool force) + { + // Check if already loaded + if (loaded_ && !force) + { + std::cerr << "[zktreeutil] zk-tree already loaded into memory" + << std::endl; + return; + } + + // Parse the file and get the DOM + xmlDocPtr docPtr = xmlReadFile(zkXmlConfig.c_str(), NULL, 0); + if (docPtr == NULL) { + std::cerr << "[zktreeutil] could not parse XML file " + << zkXmlConfig + << std::endl; + exit (-1); + } + std::cerr << "[zktreeutil] zk-tree XML parsing successful" + << std::endl; + + // Get the root element node + xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr); + // Create the root zk node + zkRootSptr_ = ZkTreeNodeSptr (new ZkTreeNode ("/")); + // Load the rooted XML tree + for (xmlNode* chldNode = rootPtr->children; + chldNode; + chldNode = chldNode->next) + { + if (chldNode->type == XML_ELEMENT_NODE) + zkRootSptr_->addChild (loadZkTreeXml_ (chldNode)); + } + + // set oad flag + loaded_ = true; + // Cleanup stuff + xmlFreeDoc(docPtr); + xmlCleanupParser(); + return; + } + + void ZkTreeUtil::writeZkTree (const string& zkHosts, + const string& path, + bool force) const + { + // Connect to ZK server + ZooKeeperAdapterSptr zkHandle = get_zkHandle (zkHosts); + std::cerr << "[zktreeutil] connected to ZK server for writing" + << std::endl; + + // Go to the rooted subtree + ZkTreeNodeSptr zkRootSptr = traverseBranch_ (zkRootSptr_, path); + + // Cleanup before write if forceful write enabled + if (force) + { + if (path != "/") // remove the subtree rooted at the znode + { + // Delete the subtree rooted at the znode before write + if (zkHandle->nodeExists (path)) + { + std::cerr << "[zktreeutil] deleting subtree rooted at " + << path + << "..." + << std::endl; + zkHandle->deleteNode (path, true); + } + } + else // remove the rooted znodes + { + std::cerr << "[zktreeutil] deleting rooted zk-tree" + << "..." + << std::endl; + // Get the root's children + vector< string > cnodes = zkHandle->getNodeChildren ("/"); + for (unsigned i=0; i < cnodes.size(); i++) + { + if ( cnodes[i] != "/zookeeper") // reserved for zookeeper use + zkHandle->deleteNode(cnodes[i], true); + } + } + } + + // Start tree construction + writeZkTree_ (zkHandle, zkRootSptr, path); + return; + } + + void ZkTreeUtil::dumpZkTree (bool xml, int depth) const + { + if (xml) + { + // Creates a new document, a node and set it as a root node + xmlDocPtr docPtr = xmlNewDoc(BAD_CAST "1.0"); + xmlNodePtr rootNode = xmlNewNode(NULL, BAD_CAST "root"); + xmlDocSetRootElement(docPtr, rootNode); + + // Add all the rooted children + for (unsigned i=0; i < zkRootSptr_->numChildren(); i++) + xmlAddChild (rootNode, dumpZkTreeXml_ (zkRootSptr_->getChild (i))); + + // Dumping document to stdio or file + xmlSaveFormatFileEnc("-", docPtr, "UTF-8", 1); + + // Cleanup stuff + xmlFreeDoc(docPtr); + xmlCleanupParser(); + return; + } + + // Dump text + std::cout << "/" << std::endl; + vector< bool > masks; + for (unsigned i=0; i < zkRootSptr_->numChildren(); i++) + { + if (i == zkRootSptr_->numChildren()-1) + masks.push_back(true); + else + masks.push_back(false); + dumpZkTree_ (zkRootSptr_->getChild (i), depth, 1, masks); + } + + return; + } + + vector< ZkAction > ZkTreeUtil::diffZkTree (const string& zkHosts, + const string& path) const + { + // Action container + vector< ZkAction > actions; + + if (!loaded_) + { + std::cout << "[zktreeutil] zk-tree not loaded for diff" + << std::endl; + exit (-1); + } + + // Load the rooted subtree from zookeeper + ZooKeeperAdapterSptr zkHandle = get_zkHandle (zkHosts); + std::cerr << "[zktreeutil] connected to ZK server for reading" + << std::endl; + ZkTreeNodeSptr zkLiveRootSptr = loadZkTree_ (zkHandle, path); + + // Go to the saved rooted subtree + ZkTreeNodeSptr zkLoadedRootSptr = + traverseBranch_ (zkRootSptr_, path); + + // Check the root value first + if (zkLoadedRootSptr->getData().value + != zkLiveRootSptr->getData().value) + { + actions.push_back (ZkAction (ZkAction::VALUE, + path, + zkLoadedRootSptr->getData().value, + zkLiveRootSptr->getData().value)); + } + + // Start traversal from root + vector< string > ppaths; + vector< pair< ZkTreeNodeSptr, ZkTreeNodeSptr > > commonNodes; + ppaths.push_back ((path != "/")? path : ""); + commonNodes.push_back (pair< ZkTreeNodeSptr, ZkTreeNodeSptr > + (zkLoadedRootSptr, zkLiveRootSptr)); + + for (unsigned j=0; j < commonNodes.size(); j++) + { + // Get children of loaded tree + map< string, ZkTreeNodeSptr > loadedChildren; + for (unsigned i=0; i < commonNodes[j].first->numChildren(); i++) + { + ZkTreeNodeSptr childSptr = commonNodes[j].first->getChild (i); + loadedChildren[childSptr->getKey()] = childSptr; + } + // Get children of live tree + map< string, ZkTreeNodeSptr > liveChildren; + for (unsigned i=0; i < commonNodes[j].second->numChildren(); i++) + { + ZkTreeNodeSptr childSptr = commonNodes[j].second->getChild (i); + liveChildren[childSptr->getKey()] = childSptr; + } + + // Start comparing the children + for (map< string, ZkTreeNodeSptr >::const_iterator it = + loadedChildren.begin(); + it != loadedChildren.end(); + it++) + { + bool ignoreKey = it->second->getData().ignoreUpdate; + string loadedVal = it->second->getData().value; + // Path to this node + string path = ppaths[j] + string("/") + it->first; + + map< string, ZkTreeNodeSptr >::const_iterator jt = + liveChildren.find (it->first); + if (jt != liveChildren.end()) + { + // Key is present in live zk-tree + string liveVal = jt->second->getData().value; + // Check value for the key, if not ignored + if (!ignoreKey) + { + if (loadedVal != liveVal) + { + // Value differs, set the new value for the key + actions.push_back (ZkAction (ZkAction::VALUE, + path, + loadedVal, + liveVal)); + } + + // Add node to common nodes + ppaths.push_back (path); + commonNodes.push_back (pair< ZkTreeNodeSptr, ZkTreeNodeSptr > + (it->second, jt->second)); + } + + // Remove the live zk node + liveChildren.erase (it->first); + } + else + { + // Add the subtree rooted to this node, if not ignored + if (!ignoreKey) + addTreeZkAction_ (it->second, path, actions); + } + } + + // Remaining live zk nodes to be deleted + for (map< string, ZkTreeNodeSptr >::const_iterator it = liveChildren.begin(); + it != liveChildren.end(); it++) + { + string path = ppaths[j] + string("/") + it->first; + actions.push_back (ZkAction (ZkAction::DELETE, path)); + } + } + // return the diff actions + return actions; + } + + void ZkTreeUtil::executeZkActions (const string& zkHosts, + const vector< ZkAction >& zkActions, + int execFlags) const + { + // Execute the diff zk actions + if (zkActions.size()) + { + // Connect to Zookeeper for writing + ZooKeeperAdapterSptr zkHandleSptr; + if ((execFlags & EXECUTE) + || (execFlags & INTERACTIVE)) + { + zkHandleSptr = get_zkHandle (zkHosts); + std::cerr << "[zktreeutil] connected to ZK server for writing" + << std::endl; + } + + for (unsigned i=0; i < zkActions.size(); i++) + { + if (zkActions[i].action == ZkAction::CREATE) + { + if (execFlags & PRINT) + std::cout << "CREAT- key:" << zkActions[i].key << std::endl; + if (execFlags & EXECUTE) + { + if (execFlags & INTERACTIVE) + { + string resp; + std::cout << "Execute this action?[yes/no]: "; + std::getline(std::cin, resp); + if (resp != "yes") + continue; + } + zkHandleSptr->createNode(zkActions[i].key.c_str(), "", 0, false); + } + } + else if (zkActions[i].action == ZkAction::DELETE) + { + if (execFlags & PRINT) + std::cout << "DELET- key:" << zkActions[i].key << std::endl; + if (execFlags & EXECUTE) + { + if (execFlags & INTERACTIVE) + { + string resp; + std::cout << "Execute this action?[yes/no]: "; + std::getline(std::cin, resp); + if (resp != "yes") + continue; + } + zkHandleSptr->deleteNode(zkActions[i].key.c_str(), true); + } + } + else if (zkActions[i].action == ZkAction::VALUE) + { + if (execFlags & PRINT) + { + std::cout << "VALUE- key:" + << zkActions[i].key + << " value:" << zkActions[i].newval; + if (zkActions[i].oldval != "") + std::cout << " old_value:" << zkActions[i].oldval; + std::cout << std::endl; + } + if (execFlags & EXECUTE) + { + if (execFlags & INTERACTIVE) + { + string resp; + std::cout << "Execute this action?[yes/no]: "; + std::getline(std::cin, resp); + if (resp != "yes") + continue; + } + zkHandleSptr->setNodeData (zkActions[i].key, zkActions[i].newval); + } + } + } + } + + return; + } + +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtil.h b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtil.h new file mode 100644 index 0000000..0a9be03 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtil.h @@ -0,0 +1,262 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef __ZK_TREE_UTIL_H__ +#define __ZK_TREE_UTIL_H__ + +#include +#include +#include "SimpleTree.h" +#include "ZkAdaptor.h" + +namespace zktreeutil +{ + +#define ZKTREEUTIL_INF 1000000000 + /** + * \brief A structure containing ZK node data. + */ + struct ZkNodeData + { + /** + * \brief The value string of the ZK node. + */ + string value; + + /** + * \brief The flag indicating whether children of the + * \brief node shduld be ignored during create/diff/update + */ + bool ignoreUpdate; + + /** + * \brief Constructor. + * + * @param val the value string + * @param ignore the flag indicating ignore any update/diff + */ + ZkNodeData (const string& val, bool ignore=false) + : value (val), ignoreUpdate (ignore) {} + + /** + * \brief Constructor. + * + * @param ignore the flag indicating ignore any update/diff + */ + ZkNodeData (bool ignore=false) + : ignoreUpdate (ignore) {} + }; + + /** + * \brief The type representing a ZK Treenode + */ + typedef SimpleTreeNode< string, ZkNodeData > ZkTreeNode; + + /** + * \brief The type representing a ZK Treenode smart-pointer + */ + typedef boost::shared_ptr< ZkTreeNode > ZkTreeNodeSptr; + + /** + * \brief The type representing a ZK Adapter smart-pointer + */ + typedef boost::shared_ptr< ZooKeeperAdapter > ZooKeeperAdapterSptr; + + /** + * \brief A structure defining a particular action on ZK node; + * \brief the action can be any of - + * \brief CREAT- : creates recussively + * \brief DELET- : deletes recursively + * \brief VALUE- : sets to + */ + struct ZkAction + { + /** + * \brief The action type; any of create/delete/setvalue. + */ + enum ZkActionType + { + NONE, + CREATE, + DELETE, + VALUE, + }; + + /** + * \brief action of this instance + */ + ZkActionType action; + + /** + * \brief ZK node key + */ + string key; + + /** + * \brief value to be set, if action is setvalue + */ + string newval; + + /** + * \brief existing value of the ZK node key + */ + string oldval; + + /** + * \brief Constructor. + */ + ZkAction () + : action (ZkAction::NONE) {} + + /** + * \brief Constructor. + * + * @param act the action to be taken + * @param k the key on which action to be taken + */ + ZkAction (ZkActionType act, const string& k) + : action(act), + key(k) {} + + /** + * \brief Constructor. + * + * @param act the action to be taken + * @param k the key on which action to be taken + * @param v the value of the ZK node key + */ + ZkAction (ZkActionType act, const string& k, const string& v) + : action(act), + key(k), + newval(v) {} + + /** + * \brief Constructor. + * + * @param act the action to be taken + * @param k the key on which action to be taken + * @param nv the new value of the ZK node key + * @param ov the old value of the ZK node key + */ + ZkAction (ZkActionType act, const string& k, const string& nv, const string& ov) + : action (act), + key(k), + newval(nv), + oldval(ov) {} + }; + + /** + * \brief The ZK tree utility class; supports loading ZK tree from ZK server OR + * \brief from saved XML file, saving ZK tree into XML file, dumping the ZK tree + * \brief on standard output, creting a diff between saved ZK tree and live ZK + * \brief tree and incremental update of the live ZK tree. + */ + class ZkTreeUtil + { + public: + /** + * \brief Execution flag on ZkAction + */ + enum ZkActionExecuteFlag + { + NONE = 0, + PRINT = 1, + EXECUTE = 2, + INTERACTIVE = 5, + }; + + public: + /** + * \brief Connects to zookeeper and returns a valid ZK handle + * + * @param zkHosts comma separated list of host:port forming ZK quorum + * @param a valid ZK handle + */ + static ZooKeeperAdapterSptr get_zkHandle (const string& zkHosts); + + + public: + /** + * \brief Constructor. + */ + ZkTreeUtil () : loaded_(false) {} + + /** + * \brief loads the ZK tree from ZK server into memory + * + * @param zkHosts comma separated list of host:port forming ZK quorum + * @param path path to the subtree to be loaded into memory + * @param force forces reloading in case tree already loaded into memory + */ + void loadZkTree (const string& zkHosts, const string& path="/", bool force=false); + + /** + * \brief loads the ZK tree from XML file into memory + * + * @param zkXmlConfig ZK tree XML file + * @param force forces reloading in case tree already loaded into memory + */ + void loadZkTreeXml (const string& zkXmlConfig, bool force=false); + + /** + * \brief writes the in-memory ZK tree on to ZK server + * + * @param zkHosts comma separated list of host:port forming ZK quorum + * @param path path to the subtree to be written to ZK tree + * @param force forces cleanup of the ZK tree on the ZK server before writing + */ + void writeZkTree (const string& zkHosts, const string& path="/", bool force=false) const; + + /** + * \brief dupms the in-memory ZK tree on the standard output device; + * + * @param xml flag indicates whether tree should be dumped in XML format + * @param depth the depth of the tree to be dumped for non-xml dump + */ + void dumpZkTree (bool xml=false, int depth=ZKTREEUTIL_INF) const; + + /** + * \brief returns a list of actions after taking a diff of in-memory + * \brief ZK tree and live ZK tree. + * + * @param zkHosts comma separated list of host:port forming ZK quorum + * @param path path to the subtree in consideration while taking diff with ZK tree + * @return a list of ZKAction instances to be performed on live ZK tree + */ + vector< ZkAction > diffZkTree (const string& zkHosts, const string& path="/") const; + + /** + * \brief performs create/delete/setvalue by executing a set of + * ZkActions on a live ZK tree. + * + * @param zkHosts comma separated list of host:port forming ZK quorum + * @param zkActions set of ZkActions + * @param execFlags flags indicating print/execute/interactive etc + */ + void executeZkActions (const string& zkHosts, + const vector< ZkAction >& zkActions, + int execFlags) const; + + private: + + ZkTreeNodeSptr zkRootSptr_; // ZK tree root node + bool loaded_; // Falg indicating whether ZK tree loaded into memory + }; +} + +#endif // __ZK_TREE_UTIL_H__ diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtilMain.cc b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtilMain.cc new file mode 100644 index 0000000..8afebf6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/src/ZkTreeUtilMain.cc @@ -0,0 +1,247 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include "ZkTreeUtil.h" + +using namespace zktreeutil; + +// The set of "long" options accepted by this program. +static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"import", no_argument, 0, 'I'}, + {"export", no_argument, 0, 'E'}, + {"update", no_argument, 0, 'U'}, + {"diff", no_argument, 0, 'F'}, + {"dump", no_argument, 0, 'D'}, + {"force", no_argument, 0, 'f'}, + {"xmlfile", required_argument, 0, 'x'}, + {"path", required_argument, 0, 'p'}, + {"depth", required_argument, 0, 'd'}, + {"zookeeper", required_argument, 0, 'z'}, + {0, 0, 0, 0} +}; +static char *short_options = "IEUFDfx:p:d:hz:"; + +static void usage(int argc, char *argv[]) +{ + std::cout << "ZK-tree utility for managing ZK-tree with XML import/export," << std::endl; + std::cout << "viewing diff between live and saved ZK-tree and performing" << std::endl; + std::cout << "incremental update of the same." << std::endl; + std::cout << "Usage: " << argv[0] << " [args-and-values]+" << std::endl; + std::cout + << "\t--import or -I: " + << std::endl + << "\t Imports the zookeeper tree from XML file. Must be specified with" + << std::endl + << "\t --zookeeper AND --xmlfile options. Optionally takes --path for" + << std::endl + << "\t importing subtree" + << std::endl; + std::cout + << "\t--export or -E: " + << std::endl + << "\t Exports the zookeeper tree to XML file. Must be specified with" + << std::endl + << "\t --zookeeper option. Optionally takes --path for exporting subtree" + << std::endl; + std::cout + << "\t--update or -U: " + << std::endl + << "\t Updates zookeeper tree with changes from XML file. Update operation" + << std::endl + << "\t is interactive unless specified with --force option. Must be speci-" + << std::endl + << "\t fied with --zookeeper AND --xmlfile options. Optionally takes --path" + << std::endl + << "\t for updating subtree." + << std::endl; + std::cout + << "\t--diff or -F: " + << std::endl + << "\t Creates a list of diff actions on ZK tree based on XML data. Must" + << std::endl + << "\t be specified with --zookeeper OR --xmlfile options. Optionally takes" + << std::endl + << "\t --path for subtree diff" + << std::endl; + std::cout + << "\t--dump or -D: " + << std::endl + << "\t Dumps the entire ZK (sub)tree to standard output. Must be specified" + << std::endl + << "\t with --zookeeper OR --xmlfile options. Optionally takes --path and" + << std::endl + << "\t --depth for dumping subtree." + << std::endl; + std::cout + << "\t--xmlfile= or -x : " + << std::endl + << "\t Zookeeper tree-data XML file." + << std::endl; + std::cout + << "\t--path= or -p : " + << std::endl + << "\t Path to the zookeeper subtree rootnode." + << std::endl; + std::cout + << "\t--depth= or -d : " + << std::endl + << "\t Depth of the ZK tree to be dumped (ignored for XML dump)." + << std::endl; + std::cout + << "\t--force or -f: Forces cleanup before import; also used for forceful" + << std::endl + << "\t update. Optionally be specified with --import and --update." + << std::endl; + std::cout + << "\t--help or -h: " + << std::endl + << "\t prints this message" + << std::endl; + std::cout + << "\t--zookeeper= or -z : " + << std::endl + << "\t specifies information to connect to zookeeper." + << std::endl; +} + +int main(int argc, char **argv) +{ + if (argc == 1) { + usage(argc, argv); + exit(0); + } + + // Parse the arguments. + int op = 0; + bool force = false; + string zkHosts; + string xmlFile; + string path = "/"; + int depth = 0; + while (1) + { + int c = getopt_long(argc, argv, short_options, long_options, 0); + if (c == -1) + break; + + switch (c) { + case 'I': op = c; + break; + case 'E': op = c; + break; + case 'U': op = c; + break; + case 'F': op = c; + break; + case 'D': op = c; + break; + case 'f': force = true; + break; + case 'x': xmlFile = optarg; + break; + case 'p': path = optarg; + break; + case 'd': depth = atoi (optarg); + break; + case 'z': zkHosts = optarg; + break; + case 'h': usage (argc, argv); + exit(0); + } + } + + ZkTreeUtil zkTreeUtil; + switch (op) + { + case 'I': { + if (zkHosts == "" || xmlFile == "") + { + std::cout << "[zktreeutil] missing params; please see usage" << std::endl; + exit (-1); + } + zkTreeUtil.loadZkTreeXml (xmlFile); + zkTreeUtil.writeZkTree (zkHosts, path, force); + std::cout << "[zktreeutil] import successful!" << std::endl; + break; + } + case 'E': { + if (zkHosts == "") + { + std::cout << "[zktreeutil] missing params; please see usage" << std::endl; + exit (-1); + } + zkTreeUtil.loadZkTree (zkHosts, path); + zkTreeUtil.dumpZkTree (true); + break; + } + case 'U': { + if (zkHosts == "" || xmlFile == "") + { + std::cout << "[zktreeutil] missing params; please see usage" << std::endl; + exit (-1); + } + zkTreeUtil.loadZkTreeXml (xmlFile); + vector< ZkAction > zkActions = zkTreeUtil.diffZkTree (zkHosts, path); + int flags = ZkTreeUtil::EXECUTE; + if (!force) flags |= ZkTreeUtil::INTERACTIVE; + zkTreeUtil.executeZkActions (zkHosts, zkActions, flags); + std::cout << "[zktreeutil] update successful!" << std::endl; + break; + } + case 'F': { + if (zkHosts == "" || xmlFile == "") + { + std::cout << "[zktreeutil] missing params; please see usage" << std::endl; + exit (-1); + } + zkTreeUtil.loadZkTreeXml (xmlFile); + vector< ZkAction > zkActions = zkTreeUtil.diffZkTree (zkHosts, path); + zkTreeUtil.executeZkActions (zkHosts, zkActions, ZkTreeUtil::PRINT); + break; + } + case 'D': { + if (zkHosts != "") + zkTreeUtil.loadZkTree (zkHosts, path); + else if (xmlFile != "") + zkTreeUtil.loadZkTreeXml (xmlFile); + else + { + std::cout << "[zktreeutil] missing params; please see usage" << std::endl; + exit (-1); + } + // Dump the ZK tree + if (depth) zkTreeUtil.dumpZkTree (false, depth); + else zkTreeUtil.dumpZkTree (false); + break; + } + } + + exit(0); +} + diff --git a/zookeeper-contrib/zookeeper-contrib-zktreeutil/tests/zk_sample.xml b/zookeeper-contrib/zookeeper-contrib-zktreeutil/tests/zk_sample.xml new file mode 100644 index 0000000..6e97daa --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zktreeutil/tests/zk_sample.xml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/NOTICE.txt b/zookeeper-contrib/zookeeper-contrib-zooinspector/NOTICE.txt new file mode 100644 index 0000000..5420ef0 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/NOTICE.txt @@ -0,0 +1,9 @@ +src/java/com/nitido/utils/toaster/Toaster.java: +This java file is copyright by Daniele Piras ("danielepiras80", no email known) released under the Apache Software License 2.0 +It has been downloaded in december 2009 from the CVS web interface of the sourceforge project http://sourceforge.net/projects/jtoaster/ . The web interface to CVS is not available anymore on sourceforge. + +The icons in src/main/resources/icons are taken from the Tango project +downloaded from http://tango.freedesktop.org/releases on 2011-09-06. +The Tango project is public domain. + +Distribution packagers should not include the icons in the package but rather depend on tango-icon-theme (Debian package name). ZooInspector will then try to get the icons from /usr/share/icons/Tango rather then from its jar file. diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/README.txt b/zookeeper-contrib/zookeeper-contrib-zooinspector/README.txt new file mode 100644 index 0000000..3c2a58f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/README.txt @@ -0,0 +1,94 @@ +========================================== +ZooInspector - Browser and Editor for ZooKeeper Instances +Author: Colin Goodheart-Smithe +Date: February 2010 +========================================== + +ZooInspector is a Java Swing based application for browsing and editing ZooKeeper instances. + +Contents +-------- + - Features + - Pre-requisites + - Build Instructions + - Using ZooInspector + - Creating and Using Plugins + +Features +-------- + Below is a list of features in the current release of ZooInspector. + - Load connection settings from a zookeeper properties file + - Plugable DataEncryptionManagers to specify how data should be encrypted and decrypted in the Zookeeper instance + - Browseable tree view of the ZooKeeper instance + - View the data in a node + - View the ACL's currently applied to a node + - View the metadata for a node (Version, Number of Children, Last modified Tiem, etc.) + - Plugable NodeViewers interface + - Ability to save/load and set default Node Viewers + +Pre-requisites +-------------- + - The main zookeeper build script must have been run before building this module + +Build Instructions +------------------ + 1. Open a command line. + 2. cd into this directory + 3. Run command: ant + 4. ZooInspector will be built to ../../../build/contrib/ZooInspector + 5. Copy zookeeper-3.x.x.jar into the lib sub-directory (if you are using zookeeper-3.3.0.jar it will have been + copied to this directory during the build + 6. By default the zookeeper.cmd and zookeeper.sh files expect zookeeper-3.3.0.jar. If you are using another version + you will need to change these files to point to the zookeeper-3.x.x.jar you copied to the lib directory + 7. To run ZooInspector run zooInspector.cmd (on Windows) or zooInspector.sh (on Linux). If you are using + zookeeper-3.3.0.jar and do not require any classpath changes you can run the zookeeper-dev-ZooInspector.jar + directly + +Using ZooInspector +------------------ + To start ZooInspector run zooInspector.cmd (on Windows) or zooInspector.sh (on Linux). If you are using + zookeeper-3.3.0.jar and do not require any classpath changes you can run the zookeeper-dev-ZooInspector.jar + directly. + + Click the play button on the toolbar to bring up the connection dialog. From here you can enter connection + information for your zookeeper instance. You can also load the connection properties from a file. This file can + have the format as a normal zookeeper properties file (i.e. hosts and timeout key-value pairs) and van optional have + an encryptionManager key-value pair to specify the DataEncryptionManager to use for this connection + (DataEncryptionManagers are explained in further detail in the 'Creating and Using Plugins' section below). You can + also set the entered information as the defaults so that when you first start ZooInspector these settings are + automatically loaded into this dialog. Pressing the OK button with connect to your ZooKeeper instance and show the + current node tree on the left of the main panel. + + Clicking a node in the node tree will load the data for that node into the node viewers. Three node viewers are + currently distributed with ZooInspector: + 1. Node Data - This enables you to see the data current stored on that node. This data can be modified and + saved. The data is decrypted and encrypted using the DataEncryptionManager specified on the connection + dialog. + 2. Node Metadata - This enables you to see the metadata associiated with this node. This is Essentially the data + obtained from the Stat object for this node. + 3. Node ACLs - This allows you to see the ACLs currently applied to this node. Currently there is no ability + to change the ACLs on a node, but it is a feature I would like to add. + Other custom Node Viewers can be added, this is explained in the 'Creating and Using Plugins' section below. + + +Creating and Using Plugins +-------------------------- + There are two types of plugin which can be used with ZooInspector: + 1. DataEncryptionManager - This specifies how data should be encrypted and decrypted when working with a + zookeeper instance. + 2. ZooInspectorNodeViewer - This is a GUI panel which provides a view of visualisation on a node. + More information on these interfaces can be found in the javadocs for this module. + + To use a plugin in ZooInspector, build the plugin to a jar and copy the jar to the lib sub-directory. Edit the + zooInspector.cmd and/or zooInspector.sh files to include your new jar on the classpath and run ZooInspector. + + For DataEncryptionManagers, click the play button to open the connection dialog and enter the full class name of + your DataEncryptionManager in the 'Data Encryption Manager' field. You can make this Data Encryption Manager the + default by clicking 'Set As Default'. Click the 'OK' button to instantiate and use your plugin. + + For ZooInspectorNodeViewers, Click the 'Change Node Viewers' button on the toolbar (looks like a tree with a pencil) + and enter the full classname for your Node Viewer in the field left of the 'Add' button, then click the 'Add' + button. The Node Viewer will be instantiated and should appear in the list. You can change the order of the Node + viewers by clicking the up and dpwn buttons and delete a Node Viewer by clicking the delete button. You can save + to configuration to a file or set it as the default if necessary. Then click the 'OK' button and your Node Viewer + should appear in the tabs on the right of the main panel. \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/TODO b/zookeeper-contrib/zookeeper-contrib-zooinspector/TODO new file mode 100644 index 0000000..404d5c9 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/TODO @@ -0,0 +1,19 @@ +- replace JToaster with standard notifications, see: + http://www.galago-project.org/specs/notification/ + http://stackoverflow.com/questions/857154/freedesktop-org-notifications-in-java + DBus and Java: + http://bolta-gecko.blogspot.com/2009/06/using-d-bus-in-java.html + http://dbus.freedesktop.org/doc/dbus-java/ (packaged in Debian) + +- properly respect + http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html + http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html + http://standards.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html + +- Rename classes to avoid redundand "ZooInspector" prefix. + +- Ant build file has hard coded log4j dependency. (ZK will move to maven anyways...) + +- make directory for config files configurable via commandline parameter + +- Clean up the code! :-) diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/build.xml b/zookeeper-contrib/zookeeper-contrib-zooinspector/build.xml new file mode 100644 index 0000000..7eb5e27 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/build.xml @@ -0,0 +1,148 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/ivy.xml b/zookeeper-contrib/zookeeper-contrib-zooinspector/ivy.xml new file mode 100644 index 0000000..96acdde --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/ivy.xml @@ -0,0 +1,46 @@ + + + + + + + + ZooInspector + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/licences/Apache Software Licence v2.0.txt b/zookeeper-contrib/zookeeper-contrib-zooinspector/licences/Apache Software Licence v2.0.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/licences/Apache Software Licence v2.0.txt @@ -0,0 +1,202 @@ + + 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/zookeeper-contrib/zookeeper-contrib-zooinspector/pom.xml b/zookeeper-contrib/zookeeper-contrib-zooinspector/pom.xml new file mode 100644 index 0000000..607611f --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/pom.xml @@ -0,0 +1,92 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-contrib + 3.6.3 + + + zookeeper-contrib-zooinspector + jar + Apache ZooKeeper - Contrib - ZooInspector + + ZooInspector is a Java Swing based application for browsing and editing ZooKeeper instances. + + + + 0.6 + 18.0 + + + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + * + * + + + + + log4j + log4j + + + * + * + + + + + junit + junit + test + + + com.google.guava + guava + ${guava.version} + + + org.apache.rat + apache-rat-tasks + ${rat.version} + + + commons-collections + commons-collections + + + + \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/com/nitido/utils/toaster/Toaster.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/com/nitido/utils/toaster/Toaster.java new file mode 100644 index 0000000..0a4fe94 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/com/nitido/utils/toaster/Toaster.java @@ -0,0 +1,527 @@ +/** + * This java file is copyright by Daniele Piras ("danielepiras80", no email known) released under the + * Apache Software License 2.0. It has been downloaded in december 2009 from the CVS web interface + * of the sourceforge project http://sourceforge.net/projects/jtoaster/ . The web interface to CVS + * is not available anymore on sourceforge. + * + */ + +/** + * Java Toaster is a java utility class for your swing applications + * that show an animate box coming from the bottom of your screen + * with a notification message and/or an associated image + * (like msn online/offline notifications). + * + * Toaster panel in windows system follow the taskbar; So if + * the taskbar is into the bottom the panel coming from the bottom + * and if the taskbar is on the top then the panel coming from the top. + * + * This is a simple example of utilization: + * + * import com.nitido.utils.toaster.*; + * import javax.swing.*; + * + * public class ToasterTest + * { + * + * public static void main(String[] args) + * { + * // Initialize toaster manager... + * Toaster toasterManager = new Toaster(); + * + * // Show a simple toaster + * toasterManager.showToaster( new ImageIcon( "mylogo.gif" ), "A simple toaster with an image" ); + * } + * } + */ +package com.nitido.utils.toaster; + +import java.awt.*; + +import javax.swing.*; +import javax.swing.border.*; + +/** + * Class to show tosters in multiplatform + * + */ +public class Toaster +{ + // Width of the toster + private int toasterWidth = 300; + + // Height of the toster + private int toasterHeight = 80; + + // Step for the toaster + private int step = 20; + + // Step time + private int stepTime = 20; + + // Show time + private int displayTime = 3000; + + // Current number of toaster... + private int currentNumberOfToaster = 0; + + // Last opened toaster + private int maxToaster = 0; + + // Max number of toasters for the sceen + private int maxToasterInSceen; + + // Font used to display message + private Font font; + + // Color for border + private Color borderColor; + + // Color for toaster + private Color toasterColor; + + // Set message color + private Color messageColor; + + // Set the margin + int margin; + + // Flag that indicate if use alwaysOnTop or not. + // method always on top start only SINCE JDK 5 ! + boolean useAlwaysOnTop = true; + + private static final long serialVersionUID = 1L; + + /** + * Constructor to initialized toaster component... + * + */ + public Toaster() + { + // Set default font... + font = new Font("Arial", Font.BOLD, 12); + // Border color + borderColor = new Color(245, 153, 15); + toasterColor = Color.WHITE; + messageColor = Color.BLACK; + useAlwaysOnTop = true; + // Verify AlwaysOnTop Flag... + try + { + JWindow.class.getMethod( "setAlwaysOnTop", new Class[] { Boolean.class } ); + } + catch( Exception e ) + { + useAlwaysOnTop = false; + } + + } + + /** + * Class that rappresent a single toaster + * + */ + class SingleToaster extends javax.swing.JWindow + { + private static final long serialVersionUID = 1L; + + // Label to store Icon + private JLabel iconLabel = new JLabel(); + + // Text area for the message + private JTextArea message = new JTextArea(); + + + + + /*** + * Simple costructor that initialized components... + */ + public SingleToaster() + { + initComponents(); + } + + /*** + * Function to initialized components + */ + private void initComponents() + { + + setSize(toasterWidth, toasterHeight); + message.setFont( getToasterMessageFont() ); + JPanel externalPanel = new JPanel(new BorderLayout(1, 1)); + externalPanel.setBackground( getBorderColor() ); + JPanel innerPanel = new JPanel(new BorderLayout( getMargin(), getMargin() )); + innerPanel.setBackground( getToasterColor() ); + message.setBackground( getToasterColor() ); + message.setMargin( new Insets( 2,2,2,2 ) ); + message.setLineWrap( true ); + message.setWrapStyleWord( true ); + + EtchedBorder etchedBorder = (EtchedBorder) BorderFactory + .createEtchedBorder(); + externalPanel.setBorder(etchedBorder); + + externalPanel.add(innerPanel); + message.setForeground( getMessageColor() ); + innerPanel.add(iconLabel, BorderLayout.WEST); + innerPanel.add(message, BorderLayout.CENTER); + getContentPane().add(externalPanel); + } + + + /*** + * Start toaster animation... + */ + public void animate() + { + ( new Animation( this ) ).start(); + } + + } + + /*** + * Class that manage the animation + */ + class Animation extends Thread + { + SingleToaster toaster; + + public Animation( SingleToaster toaster ) + { + this.toaster = toaster; + } + + + /** + * Animate vertically the toaster. The toaster could be moved from bottom + * to upper or to upper to bottom + * @param posx + * @param fromy + * @param toy + * @throws InterruptedException + */ + protected void animateVertically( int posx, int fromY, int toY ) throws InterruptedException + { + + toaster.setLocation( posx, fromY ); + if ( toY < fromY ) + { + for (int i = fromY; i > toY; i -= step) + { + toaster.setLocation(posx, i); + Thread.sleep(stepTime); + } + } + else + { + for (int i = fromY; i < toY; i += step) + { + toaster.setLocation(posx, i); + Thread.sleep(stepTime); + } + } + toaster.setLocation( posx, toY ); + } + + public void run() + { + try + { + boolean animateFromBottom = true; + GraphicsEnvironment ge = GraphicsEnvironment + .getLocalGraphicsEnvironment(); + Rectangle screenRect = ge.getMaximumWindowBounds(); + + int screenHeight = (int) screenRect.height; + + int startYPosition; + int stopYPosition; + + if ( screenRect.y > 0 ) + { + animateFromBottom = false; // Animate from top! + } + + maxToasterInSceen = screenHeight / toasterHeight; + + + int posx = (int) screenRect.width - toasterWidth - 1; + + toaster.setLocation(posx, screenHeight); + toaster.setVisible(true); + if ( useAlwaysOnTop ) + { + toaster.setAlwaysOnTop(true); + } + + if ( animateFromBottom ) + { + startYPosition = screenHeight; + stopYPosition = startYPosition - toasterHeight - 1; + if ( currentNumberOfToaster > 0 ) + { + stopYPosition = stopYPosition - ( maxToaster % maxToasterInSceen * toasterHeight ); + } + else + { + maxToaster = 0; + } + } + else + { + startYPosition = screenRect.y - toasterHeight; + stopYPosition = screenRect.y; + + if ( currentNumberOfToaster > 0 ) + { + stopYPosition = stopYPosition + ( maxToaster % maxToasterInSceen * toasterHeight ); + } + else + { + maxToaster = 0; + } + } + + currentNumberOfToaster++; + maxToaster++; + + + animateVertically( posx, startYPosition, stopYPosition ); + Thread.sleep(displayTime); + animateVertically( posx, stopYPosition, startYPosition ); + + currentNumberOfToaster--; + toaster.setVisible(false); + toaster.dispose(); + } catch (Exception e) + { + e.printStackTrace(); + } + } + } + + + + /** + * Show a toaster with the specified message and the associated icon. + */ + public void showToaster(Icon icon, String msg) + { + SingleToaster singleToaster = new SingleToaster(); + if ( icon != null ) + { + singleToaster.iconLabel.setIcon( icon ); + } + singleToaster.message.setText( msg ); + singleToaster.animate(); + } + + /** + * Show a toaster with the specified message. + */ + public void showToaster( String msg ) + { + showToaster( null, msg ); + } + + /** + * @return Returns the font + */ + public Font getToasterMessageFont() + { + // TODO Auto-generated method stub + return font; + } + + /** + * Set the font for the message + */ + public void setToasterMessageFont( Font f) + { + font = f; + } + + + /** + * @return Returns the borderColor. + */ + public Color getBorderColor() + { + return borderColor; + } + + + + /** + * @param borderColor The borderColor to set. + */ + public void setBorderColor(Color borderColor) + { + this.borderColor = borderColor; + } + + + + /** + * @return Returns the displayTime. + */ + public int getDisplayTime() + { + return displayTime; + } + + + + /** + * @param displayTime The displayTime to set. + */ + public void setDisplayTime(int displayTime) + { + this.displayTime = displayTime; + } + + + + /** + * @return Returns the margin. + */ + public int getMargin() + { + return margin; + } + + + + /** + * @param margin The margin to set. + */ + public void setMargin(int margin) + { + this.margin = margin; + } + + + + /** + * @return Returns the messageColor. + */ + public Color getMessageColor() + { + return messageColor; + } + + + + /** + * @param messageColor The messageColor to set. + */ + public void setMessageColor(Color messageColor) + { + this.messageColor = messageColor; + } + + + + /** + * @return Returns the step. + */ + public int getStep() + { + return step; + } + + + + /** + * @param step The step to set. + */ + public void setStep(int step) + { + this.step = step; + } + + + + /** + * @return Returns the stepTime. + */ + public int getStepTime() + { + return stepTime; + } + + + + /** + * @param stepTime The stepTime to set. + */ + public void setStepTime(int stepTime) + { + this.stepTime = stepTime; + } + + + + /** + * @return Returns the toasterColor. + */ + public Color getToasterColor() + { + return toasterColor; + } + + + + /** + * @param toasterColor The toasterColor to set. + */ + public void setToasterColor(Color toasterColor) + { + this.toasterColor = toasterColor; + } + + + + /** + * @return Returns the toasterHeight. + */ + public int getToasterHeight() + { + return toasterHeight; + } + + + + /** + * @param toasterHeight The toasterHeight to set. + */ + public void setToasterHeight(int toasterHeight) + { + this.toasterHeight = toasterHeight; + } + + + + /** + * @return Returns the toasterWidth. + */ + public int getToasterWidth() + { + return toasterWidth; + } + + + + /** + * @param toasterWidth The toasterWidth to set. + */ + public void setToasterWidth(int toasterWidth) + { + this.toasterWidth = toasterWidth; + } + + + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/ZooInspector.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/ZooInspector.java new file mode 100644 index 0000000..0322d98 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/ZooInspector.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector; + +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.UIManager; + +import org.apache.zookeeper.inspector.gui.IconResource; +import org.apache.zookeeper.inspector.gui.ZooInspectorPanel; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorManagerImpl; + +/** + * + */ +public class ZooInspector { + + public static IconResource iconResource; + + /** + * @param args + * - not used. The value of these parameters will have no effect + * on the application + */ + public static void main(String[] args) { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + JFrame frame = new JFrame("ZooInspector"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + iconResource = new IconResource(); + final ZooInspectorPanel zooInspectorPanel = new ZooInspectorPanel( + new ZooInspectorManagerImpl(), iconResource); + frame.addWindowListener(new WindowAdapter() { + @Override + public void windowClosed(WindowEvent e) { + super.windowClosed(e); + zooInspectorPanel.disconnect(true); + } + }); + + frame.setContentPane(zooInspectorPanel); + frame.setSize(1024, 768); + frame.setVisible(true); + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred loading ZooInspector", e); + JOptionPane.showMessageDialog(null, + "ZooInspector failed to start: " + e.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.java new file mode 100644 index 0000000..c8662db --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/encryption/BasicDataEncryptionManager.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.encryption; + +/** + * + */ +public class BasicDataEncryptionManager implements DataEncryptionManager { + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.encryption.DataEncryptionManager#decryptData + * (byte[]) + */ + public String decryptData(byte[] encrypted) throws Exception { + if(encrypted == null) { + return ""; + } + return new String(encrypted); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.encryption.DataEncryptionManager#encryptData + * (java.lang.String) + */ + public byte[] encryptData(String data) throws Exception { + if (data == null) { + return new byte[0]; + } + return data.getBytes(); + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/encryption/DataEncryptionManager.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/encryption/DataEncryptionManager.java new file mode 100644 index 0000000..15a9ee4 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/encryption/DataEncryptionManager.java @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.encryption; + +/** + * A class which describes how data should be encrypted and decrypted + */ +public interface DataEncryptionManager { + /** + * @param data + * - the data to be encrypted + * @return the encrypted data + * @throws Exception + */ + public byte[] encryptData(String data) throws Exception; + + /** + * @param encrypted + * - the data to be decrypted + * @return the decrypted data + * @throws Exception + */ + public String decryptData(byte[] encrypted) throws Exception; +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/IconResource.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/IconResource.java new file mode 100644 index 0000000..411900a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/IconResource.java @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.swing.ImageIcon; + +import org.apache.zookeeper.inspector.logger.LoggerFactory; + +/** + * @see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html + * I tried to take icons that are available in the Tango icon set + */ +public class IconResource { + + public static final String ICON_ChangeNodeViewers = ""; + public static final String ICON_TREE_LEAF = "mimetypes/text-x-generic"; + public static final String ICON_TREE_OPEN = "places/folder"; + public static final String ICON_TREE_CLOSE = "places/folder"; + public static final String ICON_INFORMATION = "status/info"; + public static final String ICON_SAVE = "actions/document-save"; + public static final String ICON_UP = "actions/up"; + public static final String ICON_DOWN = "actions/down"; + public static final String ICON_ADD = "actions/add"; + public static final String ICON_REMOVE = "actions/remove"; + public static final String ICON_START = "actions/media-playback-start"; + public static final String ICON_STOP = "actions/media-playback-stop"; + public static final String ICON_DOCUMENT_ADD = "actions/document-new"; + public static final String ICON_REFRESH = "actions/view-refresh"; + public static final String ICON_TRASH = "places/user-trash"; + // better: actions/help-about, but not in tango + public static final String ICON_HELP_ABOUT = "status/info"; + + private static final String DEFAULT_THEME = "Tango"; + private static final String DEFAULT_SIZE = "16x16"; + private static final String FALLBACK_ICON = "face-surprise"; + + // compare http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html + private static final String[] DEFAULT_XDG_DATA_DIRS = new String[]{ + "/usr/local/share", + "/usr/share" + }; + + private String theme = DEFAULT_THEME; + private String size = DEFAULT_SIZE; + + public URL find(String name) { + String iconPath = buildIconPath(name); + URL iconUrl = findInPaths(iconPath); + if(null != iconUrl) return iconUrl; + + iconUrl = getClass().getResource(iconPath); + if(null != iconUrl) return iconUrl; + + if(!name.equals(FALLBACK_ICON)) return find(FALLBACK_ICON); + return null; + } + + public ImageIcon get(String name, String description) { + URL iconUrl = find(name); + if(null==iconUrl) { + ImageIcon icon = new ImageIcon(); + icon.setDescription(description); + return icon; + } else { + return new ImageIcon(iconUrl, description); + } + } + + private URL findInPaths(String iconPath) { + for(String dataDir : DEFAULT_XDG_DATA_DIRS) { + File file = new File(dataDir + iconPath); + if(file.exists()) { + try { + return file.toURI().toURL(); + } catch (MalformedURLException e) { + LoggerFactory.getLogger().warn(e.toString()); + } + } + } + return null; + } + + private String buildIconPath(String name) { + return "/icons/" + theme + "/" + size + "/" + name + ".png"; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/NodeViewersChangeListener.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/NodeViewersChangeListener.java new file mode 100644 index 0000000..b0b1e98 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/NodeViewersChangeListener.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.util.List; + +import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; + +/** + * A Listener for changes to the configuration of which node viewers are shown + */ +public interface NodeViewersChangeListener { + /** + * Called when the node viewers configuration is changed (i.e node viewers + * are added, removed or the order of the node viewers is changed) + * + * @param newViewers + * - a {@link List} of {@link ZooInspectorNodeViewer}s which are + * to be shown + */ + public void nodeViewersChanged(List newViewers); +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/Toolbar.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/Toolbar.java new file mode 100644 index 0000000..06e80a8 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/Toolbar.java @@ -0,0 +1,92 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.event.ActionListener; +import java.util.HashMap; +import java.util.Map; + +import javax.swing.JButton; +import javax.swing.JToolBar; + +public class Toolbar { + + private final IconResource iconResource; + private final JToolBar toolbar = new JToolBar(); + private final Map buttons = new HashMap(); + + private static final Button[] buttonsToToggle = new Button[] { + Button.connect, Button.disconnect, Button.refresh, Button.addNode, Button.deleteNode + }; + + public Toolbar(IconResource iconResource) { + this.iconResource = iconResource; + init(); + } + + public void addActionListener(Button button, ActionListener actionListener) { + buttons.get(button).addActionListener(actionListener); + } + + public JToolBar getJToolBar() { + return toolbar; + } + + public void toggleButtons(boolean connected) { + for(Button button : buttonsToToggle) { + buttons.get(button).setEnabled(connected != button.enabled); + } + } + + private void init() { + toolbar.setFloatable(false); + for(Button button : Button.values()) { + JButton jbutton = button.createJButton(iconResource); + buttons.put(button, jbutton); + toolbar.add(jbutton); + } + } + + public static enum Button { + connect("Connect",IconResource.ICON_START,true), + disconnect("Disconnect",IconResource.ICON_STOP,false), + refresh("Refresh",IconResource.ICON_REFRESH,false), + addNode("Add Node",IconResource.ICON_DOCUMENT_ADD,false), + deleteNode("Delete Node",IconResource.ICON_TRASH,false), + nodeViewers("Change Node Viewers",IconResource.ICON_ChangeNodeViewers,true), + about("About ZooInspector",IconResource.ICON_HELP_ABOUT,true); + + private String toolTip; + private String icon; + private boolean enabled; + + Button(String toolTip, String icon, boolean enabled) { + this.toolTip = toolTip; + this.icon = icon; + this.enabled = enabled; + } + + public JButton createJButton(IconResource iconResource) { + JButton jbutton = new JButton(iconResource.get(icon, toolTip)); + jbutton.setEnabled(enabled); + jbutton.setToolTipText(toolTip); + return jbutton; + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.java new file mode 100644 index 0000000..58096b2 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorAboutDialog.java @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JEditorPane; +import javax.swing.JPanel; + +import org.apache.zookeeper.inspector.logger.LoggerFactory; + +/** + * The About Dialog for the application + */ +public class ZooInspectorAboutDialog extends JDialog { + /** + * @param frame + * - the Frame from which the dialog is displayed + */ + public ZooInspectorAboutDialog(Frame frame, IconResource iconResource) { + super(frame); + this.setLayout(new BorderLayout()); + this.setIconImage(iconResource.get(IconResource.ICON_INFORMATION, "About ZooInspector").getImage()); + this.setTitle("About ZooInspector"); + this.setModal(true); + this.setAlwaysOnTop(true); + this.setResizable(false); + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + JEditorPane aboutPane = new JEditorPane(); + aboutPane.setEditable(false); + aboutPane.setOpaque(false); + java.net.URL aboutURL = ZooInspectorAboutDialog.class + .getResource("about.html"); + try { + aboutPane.setPage(aboutURL); + } catch (IOException e) { + LoggerFactory.getLogger().error( + "Error loading about.html, file may be corrupt", e); + } + panel.add(aboutPane, BorderLayout.CENTER); + panel.setPreferredSize(new Dimension(600, 200)); + JPanel buttonsPanel = new JPanel(); + buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); + JButton okButton = new JButton("OK"); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorAboutDialog.this.dispose(); + } + }); + buttonsPanel.add(okButton); + this.add(panel, BorderLayout.CENTER); + this.add(buttonsPanel, BorderLayout.SOUTH); + this.pack(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorConnectionPropertiesDialog.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorConnectionPropertiesDialog.java new file mode 100644 index 0000000..1647021 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorConnectionPropertiesDialog.java @@ -0,0 +1,321 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.BorderLayout; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Map.Entry; + +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.Pair; + +/** + * The connection properties dialog. This is used to determine the settings for + * connecting to a zookeeper instance + */ +public class ZooInspectorConnectionPropertiesDialog extends JDialog { + + private final Map components; + + /** + * @param lastConnectionProps + * - the last connection properties used. if this is the first + * conneciton since starting the applications this will be the + * default settings + * @param connectionPropertiesTemplateAndLabels + * - the connection properties and labels to show in this dialog + * @param zooInspectorPanel + * - the {@link ZooInspectorPanel} linked to this dialog + */ + public ZooInspectorConnectionPropertiesDialog( + Properties lastConnectionProps, + Pair>, Map> connectionPropertiesTemplateAndLabels, + final ZooInspectorPanel zooInspectorPanel) { + final Map> connectionPropertiesTemplate = connectionPropertiesTemplateAndLabels + .getKey(); + final Map connectionPropertiesLabels = connectionPropertiesTemplateAndLabels + .getValue(); + this.setLayout(new BorderLayout()); + this.setTitle("Connection Settings"); + this.setModal(true); + this.setAlwaysOnTop(true); + this.setResizable(false); + final JPanel options = new JPanel(); + final JFileChooser fileChooser = new JFileChooser(); + options.setLayout(new GridBagLayout()); + int i = 0; + components = new HashMap(); + for (Entry> entry : connectionPropertiesTemplate + .entrySet()) { + int rowPos = 2 * i + 1; + JLabel label = new JLabel(connectionPropertiesLabels.get(entry + .getKey())); + GridBagConstraints c1 = new GridBagConstraints(); + c1.gridx = 0; + c1.gridy = rowPos; + c1.gridwidth = 1; + c1.gridheight = 1; + c1.weightx = 0; + c1.weighty = 0; + c1.anchor = GridBagConstraints.WEST; + c1.fill = GridBagConstraints.HORIZONTAL; + c1.insets = new Insets(5, 5, 5, 5); + c1.ipadx = 0; + c1.ipady = 0; + options.add(label, c1); + if (entry.getValue().size() == 0) { + JTextField text = new JTextField(); + GridBagConstraints c2 = new GridBagConstraints(); + c2.gridx = 2; + c2.gridy = rowPos; + c2.gridwidth = 1; + c2.gridheight = 1; + c2.weightx = 0; + c2.weighty = 0; + c2.anchor = GridBagConstraints.WEST; + c2.fill = GridBagConstraints.HORIZONTAL; + c2.insets = new Insets(5, 5, 5, 5); + c2.ipadx = 0; + c2.ipady = 0; + options.add(text, c2); + components.put(entry.getKey(), text); + } else if (entry.getValue().size() == 1) { + JTextField text = new JTextField(entry.getValue().get(0)); + GridBagConstraints c2 = new GridBagConstraints(); + c2.gridx = 2; + c2.gridy = rowPos; + c2.gridwidth = 1; + c2.gridheight = 1; + c2.weightx = 0; + c2.weighty = 0; + c2.anchor = GridBagConstraints.WEST; + c2.fill = GridBagConstraints.HORIZONTAL; + c2.insets = new Insets(5, 5, 5, 5); + c2.ipadx = 0; + c2.ipady = 0; + options.add(text, c2); + components.put(entry.getKey(), text); + } else { + List list = entry.getValue(); + JComboBox combo = new JComboBox(list.toArray(new String[list + .size()])); + combo.setSelectedItem(list.get(0)); + GridBagConstraints c2 = new GridBagConstraints(); + c2.gridx = 2; + c2.gridy = rowPos; + c2.gridwidth = 1; + c2.gridheight = 1; + c2.weightx = 0; + c2.weighty = 0; + c2.anchor = GridBagConstraints.WEST; + c2.fill = GridBagConstraints.HORIZONTAL; + c2.insets = new Insets(5, 5, 5, 5); + c2.ipadx = 0; + c2.ipady = 0; + options.add(combo, c2); + components.put(entry.getKey(), combo); + } + i++; + } + loadConnectionProps(lastConnectionProps); + JPanel buttonsPanel = new JPanel(); + buttonsPanel.setLayout(new GridBagLayout()); + JButton loadPropsFileButton = new JButton("Load from file"); + loadPropsFileButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int result = fileChooser + .showOpenDialog(ZooInspectorConnectionPropertiesDialog.this); + if (result == JFileChooser.APPROVE_OPTION) { + File propsFilePath = fileChooser.getSelectedFile(); + Properties props = new Properties(); + try { + FileReader reader = new FileReader(propsFilePath); + try { + props.load(reader); + loadConnectionProps(props); + } finally { + reader.close(); + } + } catch (IOException ex) { + LoggerFactory + .getLogger() + .error( + "An Error occurred loading connection properties from file", + ex); + JOptionPane + .showMessageDialog( + ZooInspectorConnectionPropertiesDialog.this, + "An Error occurred loading connection properties from file", + "Error", JOptionPane.ERROR_MESSAGE); + } + options.revalidate(); + options.repaint(); + } + + } + }); + GridBagConstraints c3 = new GridBagConstraints(); + c3.gridx = 0; + c3.gridy = 0; + c3.gridwidth = 1; + c3.gridheight = 1; + c3.weightx = 0; + c3.weighty = 1; + c3.anchor = GridBagConstraints.SOUTHWEST; + c3.fill = GridBagConstraints.NONE; + c3.insets = new Insets(5, 5, 5, 5); + c3.ipadx = 0; + c3.ipady = 0; + buttonsPanel.add(loadPropsFileButton, c3); + JButton saveDefaultPropsFileButton = new JButton("Set As Default"); + saveDefaultPropsFileButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + + Properties connectionProps = getConnectionProps(); + try { + zooInspectorPanel + .setdefaultConnectionProps(connectionProps); + } catch (IOException ex) { + LoggerFactory + .getLogger() + .error( + "An Error occurred saving the default connection properties file", + ex); + JOptionPane + .showMessageDialog( + ZooInspectorConnectionPropertiesDialog.this, + "An Error occurred saving the default connection properties file", + "Error", JOptionPane.ERROR_MESSAGE); + } + } + }); + GridBagConstraints c6 = new GridBagConstraints(); + c6.gridx = 1; + c6.gridy = 0; + c6.gridwidth = 1; + c6.gridheight = 1; + c6.weightx = 1; + c6.weighty = 1; + c6.anchor = GridBagConstraints.SOUTHWEST; + c6.fill = GridBagConstraints.NONE; + c6.insets = new Insets(5, 5, 5, 5); + c6.ipadx = 0; + c6.ipady = 0; + buttonsPanel.add(saveDefaultPropsFileButton, c6); + JButton okButton = new JButton("OK"); + okButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + ZooInspectorConnectionPropertiesDialog.this.dispose(); + Properties connectionProps = getConnectionProps(); + zooInspectorPanel.connect(connectionProps); + } + }); + GridBagConstraints c4 = new GridBagConstraints(); + c4.gridx = 2; + c4.gridy = 0; + c4.gridwidth = 1; + c4.gridheight = 1; + c4.weightx = 0; + c4.weighty = 1; + c4.anchor = GridBagConstraints.SOUTH; + c4.fill = GridBagConstraints.HORIZONTAL; + c4.insets = new Insets(5, 5, 5, 5); + c4.ipadx = 0; + c4.ipady = 0; + buttonsPanel.add(okButton, c4); + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + ZooInspectorConnectionPropertiesDialog.this.dispose(); + } + }); + GridBagConstraints c5 = new GridBagConstraints(); + c5.gridx = 3; + c5.gridy = 0; + c5.gridwidth = 1; + c5.gridheight = 1; + c5.weightx = 0; + c5.weighty = 1; + c5.anchor = GridBagConstraints.SOUTH; + c5.fill = GridBagConstraints.HORIZONTAL; + c5.insets = new Insets(5, 5, 5, 5); + c5.ipadx = 0; + c5.ipady = 0; + buttonsPanel.add(cancelButton, c5); + this.add(options, BorderLayout.CENTER); + this.add(buttonsPanel, BorderLayout.SOUTH); + this.pack(); + } + + private void loadConnectionProps(Properties props) { + if (props != null) { + for (Object key : props.keySet()) { + String propsKey = (String) key; + if (components.containsKey(propsKey)) { + JComponent component = components.get(propsKey); + String value = props.getProperty(propsKey); + if (component instanceof JTextField) { + ((JTextField) component).setText(value); + } else if (component instanceof JComboBox) { + ((JComboBox) component).setSelectedItem(value); + } + } + } + } + } + + private Properties getConnectionProps() { + Properties connectionProps = new Properties(); + for (Entry entry : components.entrySet()) { + String value = null; + JComponent component = entry.getValue(); + if (component instanceof JTextField) { + value = ((JTextField) component).getText(); + } else if (component instanceof JComboBox) { + value = ((JComboBox) component).getSelectedItem().toString(); + } + connectionProps.put(entry.getKey(), value); + } + return connectionProps; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java new file mode 100644 index 0000000..e3cc7b1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersDialog.java @@ -0,0 +1,631 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.datatransfer.Transferable; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.DefaultListModel; +import javax.swing.DropMode; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; +import javax.swing.TransferHandler; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; + +import org.apache.zookeeper.inspector.gui.Toolbar.Button; +import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; + +/** + * A {@link JDialog} for configuring which {@link ZooInspectorNodeViewer}s to + * show in the application + */ +public class ZooInspectorNodeViewersDialog extends JDialog implements + ListSelectionListener { + + private final JList viewersList; + private final JFileChooser fileChooser = new JFileChooser(new File(".")); + private final Map buttons = new HashMap(); + /** + * @param frame + * - the Frame from which the dialog is displayed + * @param currentViewers + * - the {@link ZooInspectorNodeViewer}s to show + * @param listeners + * - the {@link NodeViewersChangeListener}s which need to be + * notified of changes to the node viewers configuration + * @param manager + * - the {@link ZooInspectorManager} for the application + * + */ + public ZooInspectorNodeViewersDialog(Frame frame, + final List currentViewers, + final Collection listeners, + final ZooInspectorManager manager, + final IconResource iconResource) { + super(frame); + final List newViewers = new ArrayList( + currentViewers); + this.setLayout(new BorderLayout()); + this.setIconImage(iconResource.get(IconResource.ICON_ChangeNodeViewers,"") + .getImage()); + this.setTitle("About ZooInspector"); + this.setModal(true); + this.setAlwaysOnTop(true); + this.setResizable(true); + final JPanel panel = new JPanel(); + panel.setLayout(new GridBagLayout()); + viewersList = new JList(); + DefaultListModel model = new DefaultListModel(); + for (ZooInspectorNodeViewer viewer : newViewers) { + model.addElement(viewer); + } + viewersList.setModel(model); + viewersList.setCellRenderer(new DefaultListCellRenderer() { + @Override + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, + boolean cellHasFocus) { + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) value; + JLabel label = (JLabel) super.getListCellRendererComponent( + list, value, index, isSelected, cellHasFocus); + label.setText(viewer.getTitle()); + return label; + } + }); + viewersList.setDropMode(DropMode.INSERT); + viewersList.enableInputMethods(true); + viewersList.setDragEnabled(true); + viewersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + viewersList.getSelectionModel().addListSelectionListener(this); + viewersList.setTransferHandler(new TransferHandler() { + + @Override + public boolean canImport(TransferHandler.TransferSupport info) { + // we only import NodeViewers + if (!info + .isDataFlavorSupported(ZooInspectorNodeViewer.nodeViewerDataFlavor)) { + return false; + } + + JList.DropLocation dl = (JList.DropLocation) info + .getDropLocation(); + if (dl.getIndex() == -1) { + return false; + } + return true; + } + + @Override + public boolean importData(TransferHandler.TransferSupport info) { + JList.DropLocation dl = (JList.DropLocation) info + .getDropLocation(); + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + int index = dl.getIndex(); + boolean insert = dl.isInsert(); + // Get the string that is being dropped. + Transferable t = info.getTransferable(); + String data; + try { + data = (String) t + .getTransferData(ZooInspectorNodeViewer.nodeViewerDataFlavor); + } catch (Exception e) { + return false; + } + try { + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class + .forName(data).newInstance(); + if (listModel.contains(viewer)) { + listModel.removeElement(viewer); + } + if (insert) { + listModel.add(index, viewer); + } else { + listModel.set(index, viewer); + } + return true; + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error instantiating class: " + data, e); + return false; + } + + } + + @Override + public int getSourceActions(JComponent c) { + return MOVE; + } + + @Override + protected Transferable createTransferable(JComponent c) { + JList list = (JList) c; + ZooInspectorNodeViewer value = (ZooInspectorNodeViewer) list + .getSelectedValue(); + return value; + } + }); + JScrollPane scroller = new JScrollPane(viewersList); + GridBagConstraints c1 = new GridBagConstraints(); + c1.gridx = 0; + c1.gridy = 0; + c1.gridwidth = 3; + c1.gridheight = 3; + c1.weightx = 0; + c1.weighty = 1; + c1.anchor = GridBagConstraints.CENTER; + c1.fill = GridBagConstraints.BOTH; + c1.insets = new Insets(5, 5, 5, 5); + c1.ipadx = 0; + c1.ipady = 0; + panel.add(scroller, c1); + + final JTextField newViewerTextField = new JTextField(); + + for(Button button : Button.values()) { + JButton jbutton = button.createJButton(iconResource); + buttons.put(button, jbutton); + } + GridBagConstraints c2 = new GridBagConstraints(); + c2.gridx = 3; + c2.gridy = 0; + c2.gridwidth = 1; + c2.gridheight = 1; + c2.weightx = 0; + c2.weighty = 0; + c2.anchor = GridBagConstraints.NORTH; + c2.fill = GridBagConstraints.HORIZONTAL; + c2.insets = new Insets(5, 5, 5, 5); + c2.ipadx = 0; + c2.ipady = 0; + panel.add(buttons.get(Button.up), c2); + GridBagConstraints c3 = new GridBagConstraints(); + c3.gridx = 3; + c3.gridy = 2; + c3.gridwidth = 1; + c3.gridheight = 1; + c3.weightx = 0; + c3.weighty = 0; + c3.anchor = GridBagConstraints.NORTH; + c3.fill = GridBagConstraints.HORIZONTAL; + c3.insets = new Insets(5, 5, 5, 5); + c3.ipadx = 0; + c3.ipady = 0; + panel.add(buttons.get(Button.down), c3); + GridBagConstraints c4 = new GridBagConstraints(); + c4.gridx = 3; + c4.gridy = 1; + c4.gridwidth = 1; + c4.gridheight = 1; + c4.weightx = 0; + c4.weighty = 0; + c4.anchor = GridBagConstraints.NORTH; + c4.fill = GridBagConstraints.HORIZONTAL; + c4.insets = new Insets(5, 5, 5, 5); + c4.ipadx = 0; + c4.ipady = 0; + panel.add(buttons.get(Button.remove), c4); + GridBagConstraints c5 = new GridBagConstraints(); + c5.gridx = 0; + c5.gridy = 3; + c5.gridwidth = 3; + c5.gridheight = 1; + c5.weightx = 0; + c5.weighty = 0; + c5.anchor = GridBagConstraints.CENTER; + c5.fill = GridBagConstraints.BOTH; + c5.insets = new Insets(5, 5, 5, 5); + c5.ipadx = 0; + c5.ipady = 0; + panel.add(newViewerTextField, c5); + GridBagConstraints c6 = new GridBagConstraints(); + c6.gridx = 3; + c6.gridy = 3; + c6.gridwidth = 1; + c6.gridheight = 1; + c6.weightx = 0; + c6.weighty = 0; + c6.anchor = GridBagConstraints.CENTER; + c6.fill = GridBagConstraints.BOTH; + c6.insets = new Insets(5, 5, 5, 5); + c6.ipadx = 0; + c6.ipady = 0; + panel.add(buttons.get(Button.add), c6); + buttons.get(Button.up).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList + .getSelectedValue(); + int index = viewersList.getSelectedIndex(); + if (listModel.contains(viewer)) { + listModel.removeElementAt(index); + listModel.insertElementAt(viewer, index - 1); + viewersList.setSelectedValue(viewer, true); + } + } + }); + buttons.get(Button.down).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList + .getSelectedValue(); + int index = viewersList.getSelectedIndex(); + if (listModel.contains(viewer)) { + listModel.removeElementAt(index); + listModel.insertElementAt(viewer, index + 1); + viewersList.setSelectedValue(viewer, true); + } + } + }); + buttons.get(Button.remove).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) viewersList + .getSelectedValue(); + int index = viewersList.getSelectedIndex(); + if (listModel.contains(viewer)) { + listModel.removeElement(viewer); + viewersList + .setSelectedIndex(index == listModel.size() ? index - 1 + : index); + } + } + }); + buttons.get(Button.add).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + String className = newViewerTextField.getText(); + if (className == null || className.length() == 0) { + JOptionPane + .showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Please enter the full class name for a Node Viewer and click the add button", + "Input Error", JOptionPane.ERROR_MESSAGE); + } else { + try { + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class + .forName(className).newInstance(); + if (listModel.contains(viewer)) { + JOptionPane + .showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Node viewer already exists. Each node viewer can only be added once.", + "Input Error", + JOptionPane.ERROR_MESSAGE); + } else { + listModel.addElement(viewer); + } + } catch (Exception ex) { + LoggerFactory + .getLogger() + .error( + "An error occurred while instaniating the node viewer. ", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "An error occurred while instaniating the node viewer: " + + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + } + }); + + GridBagConstraints c7 = new GridBagConstraints(); + c7.gridx = 0; + c7.gridy = 4; + c7.gridwidth = 1; + c7.gridheight = 1; + c7.weightx = 1; + c7.weighty = 0; + c7.anchor = GridBagConstraints.WEST; + c7.fill = GridBagConstraints.VERTICAL; + c7.insets = new Insets(5, 5, 5, 5); + c7.ipadx = 0; + c7.ipady = 0; + panel.add(buttons.get(Button.save), c7); + GridBagConstraints c8 = new GridBagConstraints(); + c8.gridx = 1; + c8.gridy = 4; + c8.gridwidth = 1; + c8.gridheight = 1; + c8.weightx = 0; + c8.weighty = 0; + c8.anchor = GridBagConstraints.WEST; + c8.fill = GridBagConstraints.VERTICAL; + c8.insets = new Insets(5, 5, 5, 5); + c8.ipadx = 0; + c8.ipady = 0; + panel.add(buttons.get(Button.load), c8); + GridBagConstraints c9 = new GridBagConstraints(); + c9.gridx = 2; + c9.gridy = 4; + c9.gridwidth = 1; + c9.gridheight = 1; + c9.weightx = 0; + c9.weighty = 0; + c9.anchor = GridBagConstraints.WEST; + c9.fill = GridBagConstraints.VERTICAL; + c9.insets = new Insets(5, 5, 5, 5); + c9.ipadx = 0; + c9.ipady = 0; + panel.add(buttons.get(Button.setDefaults), c9); + buttons.get(Button.save).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int result = fileChooser + .showSaveDialog(ZooInspectorNodeViewersDialog.this); + if (result == JFileChooser.APPROVE_OPTION) { + File selectedFile = fileChooser.getSelectedFile(); + int answer = JOptionPane.YES_OPTION; + if (selectedFile.exists()) { + answer = JOptionPane + .showConfirmDialog( + ZooInspectorNodeViewersDialog.this, + "The specified file already exists. do you want to overwrite it?", + "Confirm Overwrite", + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE); + } + if (answer == JOptionPane.YES_OPTION) { + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + List nodeViewersClassNames = new ArrayList(); + Object[] modelContents = listModel.toArray(); + for (Object o : modelContents) { + nodeViewersClassNames + .add(((ZooInspectorNodeViewer) o) + .getClass().getCanonicalName()); + } + try { + manager.saveNodeViewersFile(selectedFile, + nodeViewersClassNames); + } catch (IOException ex) { + LoggerFactory + .getLogger() + .error( + "Error saving node viewer configuration from file.", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Error saving node viewer configuration from file: " + + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + } + } + }); + buttons.get(Button.load).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int result = fileChooser + .showOpenDialog(ZooInspectorNodeViewersDialog.this); + if (result == JFileChooser.APPROVE_OPTION) { + try { + List nodeViewersClassNames = manager + .loadNodeViewersFile(fileChooser + .getSelectedFile()); + List nodeViewers = new ArrayList(); + for (String nodeViewersClassName : nodeViewersClassNames) { + ZooInspectorNodeViewer viewer = (ZooInspectorNodeViewer) Class + .forName(nodeViewersClassName) + .newInstance(); + nodeViewers.add(viewer); + } + DefaultListModel model = new DefaultListModel(); + for (ZooInspectorNodeViewer viewer : nodeViewers) { + model.addElement(viewer); + } + viewersList.setModel(model); + panel.revalidate(); + panel.repaint(); + } catch (Exception ex) { + LoggerFactory + .getLogger() + .error( + "Error loading node viewer configuration from file.", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Error loading node viewer configuration from file: " + + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + } + }); + buttons.get(Button.setDefaults).addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + int answer = JOptionPane + .showConfirmDialog( + ZooInspectorNodeViewersDialog.this, + "Are you sure you want to save this configuration as the default?", + "Confirm Set Defaults", + JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE); + if (answer == JOptionPane.YES_OPTION) { + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + List nodeViewersClassNames = new ArrayList(); + Object[] modelContents = listModel.toArray(); + for (Object o : modelContents) { + nodeViewersClassNames.add(((ZooInspectorNodeViewer) o) + .getClass().getCanonicalName()); + } + try { + manager + .setDefaultNodeViewerConfiguration(nodeViewersClassNames); + } catch (IOException ex) { + LoggerFactory + .getLogger() + .error( + "Error setting default node viewer configuration.", + ex); + JOptionPane.showMessageDialog( + ZooInspectorNodeViewersDialog.this, + "Error setting default node viewer configuration: " + + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + } + } + } + }); + + JPanel buttonsPanel = new JPanel(); + buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); + JButton okButton = new JButton("OK"); + okButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorNodeViewersDialog.this.dispose(); + DefaultListModel listModel = (DefaultListModel) viewersList + .getModel(); + newViewers.clear(); + Object[] modelContents = listModel.toArray(); + for (Object o : modelContents) { + newViewers.add((ZooInspectorNodeViewer) o); + } + currentViewers.clear(); + currentViewers.addAll(newViewers); + for (NodeViewersChangeListener listener : listeners) { + listener.nodeViewersChanged(currentViewers); + } + } + }); + buttonsPanel.add(okButton); + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorNodeViewersDialog.this.dispose(); + } + }); + buttonsPanel.add(cancelButton); + this.add(panel, BorderLayout.CENTER); + this.add(buttonsPanel, BorderLayout.SOUTH); + this.pack(); + } + + /* + * (non-Javadoc) + * + * @see + * javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event + * .ListSelectionEvent) + */ + public void valueChanged(ListSelectionEvent e) { + JButton removeButton = buttons.get(Button.remove); + JButton upButton = buttons.get(Button.up); + JButton downButton = buttons.get(Button.down); + int index = viewersList.getSelectedIndex(); + + if (index == -1) { + removeButton.setEnabled(false); + upButton.setEnabled(false); + downButton.setEnabled(false); + } else { + removeButton.setEnabled(true); + if (index == 0) { + upButton.setEnabled(false); + } else { + upButton.setEnabled(true); + } + if (index == ((DefaultListModel) viewersList.getModel()).getSize()) { + downButton.setEnabled(false); + } else { + downButton.setEnabled(true); + } + } + } + + public static enum Button { + up("Move currently selected node viewer up",IconResource.ICON_UP,false), + down("Move currently selected node viewer down",IconResource.ICON_DOWN,false), + add("Add node viewer",IconResource.ICON_ADD,true), + remove("Remove currently selected node viewer",IconResource.ICON_REMOVE,false), + save("Save current node viewer configuration to file","Save"), + load("Load node viewer configuration from file","Load"), + setDefaults("Set current configuration asd defaults","Set as defaults"); + + private String toolTip; + private String icon; + private boolean enabled; + + Button(String toolTip, String icon, boolean enabled) { + this.toolTip = toolTip; + this.icon = icon; + this.enabled = enabled; + } + + Button(String toolTip, String icon) { + this(toolTip, icon, true); + } + + public JButton createJButton(IconResource iconResource) { + ImageIcon imageIcon = iconResource.get(icon, toolTip); + JButton jbutton; + + if(imageIcon == null) { + jbutton = new JButton(icon); + } else { + jbutton = new JButton(imageIcon); + } + + jbutton.setEnabled(enabled); + jbutton.setToolTipText(toolTip); + return jbutton; + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersPanel.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersPanel.java new file mode 100644 index 0000000..05c256b --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorNodeViewersPanel.java @@ -0,0 +1,140 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.BorderLayout; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JPanel; +import javax.swing.JTabbedPane; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.TreePath; + +import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; +import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; + +/** + * This is the {@link JPanel} which contains the {@link ZooInspectorNodeViewer}s + */ +public class ZooInspectorNodeViewersPanel extends JPanel implements + TreeSelectionListener, ChangeListener { + + private final List nodeVeiwers = new ArrayList(); + private final List needsReload = new ArrayList(); + private final JTabbedPane tabbedPane; + private final List selectedNodes = new ArrayList(); + private final ZooInspectorNodeManager zooInspectorManager; + + /** + * @param zooInspectorManager + * - the {@link ZooInspectorManager} for the application + * @param nodeVeiwers + * - the {@link ZooInspectorNodeViewer}s to show + */ + public ZooInspectorNodeViewersPanel( + ZooInspectorNodeManager zooInspectorManager, + List nodeVeiwers) { + this.zooInspectorManager = zooInspectorManager; + this.setLayout(new BorderLayout()); + tabbedPane = new JTabbedPane(JTabbedPane.TOP, + JTabbedPane.WRAP_TAB_LAYOUT); + setNodeViewers(nodeVeiwers); + tabbedPane.addChangeListener(this); + this.add(tabbedPane, BorderLayout.CENTER); + reloadSelectedViewer(); + } + + /** + * @param nodeViewers + * - the {@link ZooInspectorNodeViewer}s to show + */ + public void setNodeViewers(List nodeViewers) { + this.nodeVeiwers.clear(); + this.nodeVeiwers.addAll(nodeViewers); + needsReload.clear(); + tabbedPane.removeAll(); + for (ZooInspectorNodeViewer nodeViewer : nodeVeiwers) { + nodeViewer.setZooInspectorManager(zooInspectorManager); + needsReload.add(true); + tabbedPane.add(nodeViewer.getTitle(), nodeViewer); + } + this.revalidate(); + this.repaint(); + } + + private void reloadSelectedViewer() { + int index = this.tabbedPane.getSelectedIndex(); + if (index != -1 && this.needsReload.get(index)) { + ZooInspectorNodeViewer viewer = this.nodeVeiwers.get(index); + viewer.nodeSelectionChanged(selectedNodes); + this.needsReload.set(index, false); + } + } + + /* + * (non-Javadoc) + * + * @see + * javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event + * .TreeSelectionEvent) + */ + public void valueChanged(TreeSelectionEvent e) { + TreePath[] paths = e.getPaths(); + selectedNodes.clear(); + for (TreePath path : paths) { + boolean appended = false; + StringBuilder sb = new StringBuilder(); + Object[] pathArray = path.getPath(); + for (Object o : pathArray) { + if (o != null) { + String nodeName = o.toString(); + if (nodeName != null) { + if (nodeName.length() > 0) { + appended = true; + sb.append("/"); //$NON-NLS-1$ + sb.append(o.toString()); + } + } + } + } + if (appended) { + selectedNodes.add(sb.toString()); + } + } + for (int i = 0; i < needsReload.size(); i++) { + this.needsReload.set(i, true); + } + reloadSelectedViewer(); + } + + /* + * (non-Javadoc) + * + * @see + * javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent + * ) + */ + public void stateChanged(ChangeEvent e) { + reloadSelectedViewer(); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java new file mode 100644 index 0000000..e816ceb --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorPanel.java @@ -0,0 +1,258 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.ExecutionException; + +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JToolBar; +import javax.swing.SwingWorker; + +import org.apache.zookeeper.inspector.gui.actions.AddNodeAction; +import org.apache.zookeeper.inspector.gui.actions.DeleteNodeAction; +import org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; + +/** + * The parent {@link JPanel} for the whole application + */ +public class ZooInspectorPanel extends JPanel implements + NodeViewersChangeListener { + private final IconResource iconResource; + private final Toolbar toolbar; + private final ZooInspectorNodeViewersPanel nodeViewersPanel; + private final ZooInspectorTreeViewer treeViewer; + private final ZooInspectorManager zooInspectorManager; + + private final List listeners = new ArrayList(); + { + listeners.add(this); + } + + /** + * @param zooInspectorManager + * - the {@link ZooInspectorManager} for the application + */ + public ZooInspectorPanel(final ZooInspectorManager zooInspectorManager, final IconResource iconResource) { + this.zooInspectorManager = zooInspectorManager; + this.iconResource = iconResource; + toolbar = new Toolbar(iconResource); + final List nodeViewers = new ArrayList(); + try { + List defaultNodeViewersClassNames = this.zooInspectorManager + .getDefaultNodeViewerConfiguration(); + for (String className : defaultNodeViewersClassNames) { + nodeViewers.add((ZooInspectorNodeViewer) Class.forName( + className).newInstance()); + } + } catch (Exception ex) { + LoggerFactory.getLogger().error( + "Error loading default node viewers.", ex); + JOptionPane.showMessageDialog(ZooInspectorPanel.this, + "Error loading default node viewers: " + ex.getMessage(), + "Error", JOptionPane.ERROR_MESSAGE); + } + nodeViewersPanel = new ZooInspectorNodeViewersPanel( + zooInspectorManager, nodeViewers); + treeViewer = new ZooInspectorTreeViewer(zooInspectorManager, + nodeViewersPanel, iconResource); + this.setLayout(new BorderLayout()); + + toolbar.addActionListener(Toolbar.Button.connect, new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorConnectionPropertiesDialog zicpd = new ZooInspectorConnectionPropertiesDialog( + zooInspectorManager.getLastConnectionProps(), + zooInspectorManager.getConnectionPropertiesTemplate(), + ZooInspectorPanel.this); + zicpd.setVisible(true); + } + }); + toolbar.addActionListener(Toolbar.Button.disconnect, new ActionListener() { + public void actionPerformed(ActionEvent e) { + disconnect(); + } + }); + toolbar.addActionListener(Toolbar.Button.refresh, new ActionListener() { + public void actionPerformed(ActionEvent e) { + treeViewer.refreshView(); + } + }); + + toolbar.addActionListener(Toolbar.Button.addNode, + new AddNodeAction(this, treeViewer, zooInspectorManager)); + toolbar.addActionListener(Toolbar.Button.deleteNode, + new DeleteNodeAction(this, treeViewer, zooInspectorManager)); + + toolbar.addActionListener(Toolbar.Button.nodeViewers, new ActionListener() { + + public void actionPerformed(ActionEvent e) { + ZooInspectorNodeViewersDialog nvd = new ZooInspectorNodeViewersDialog( + JOptionPane.getRootFrame(), nodeViewers, listeners, + zooInspectorManager, iconResource); + nvd.setVisible(true); + } + }); + toolbar.addActionListener(Toolbar.Button.about, new ActionListener() { + public void actionPerformed(ActionEvent e) { + ZooInspectorAboutDialog zicpd = new ZooInspectorAboutDialog( + JOptionPane.getRootFrame(), iconResource); + zicpd.setVisible(true); + } + }); + JScrollPane treeScroller = new JScrollPane(treeViewer); + JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, + treeScroller, nodeViewersPanel); + splitPane.setResizeWeight(0.25); + this.add(splitPane, BorderLayout.CENTER); + this.add(toolbar.getJToolBar(), BorderLayout.NORTH); + } + + /** + * @param connectionProps + * the {@link Properties} for connecting to the zookeeper + * instance + */ + public void connect(final Properties connectionProps) { + SwingWorker worker = new SwingWorker() { + + @Override + protected Boolean doInBackground() throws Exception { + zooInspectorManager.setLastConnectionProps(connectionProps); + return zooInspectorManager.connect(connectionProps); + } + + @Override + protected void done() { + try { + if (get()) { + treeViewer.refreshView(); + toolbar.toggleButtons(true); + } else { + JOptionPane.showMessageDialog(ZooInspectorPanel.this, + "Unable to connect to zookeeper", "Error", + JOptionPane.ERROR_MESSAGE); + } + } catch (InterruptedException e) { + LoggerFactory + .getLogger() + .error( + "Error occurred while connecting to ZooKeeper server", + e); + } catch (ExecutionException e) { + LoggerFactory + .getLogger() + .error( + "Error occurred while connecting to ZooKeeper server", + e); + } + } + + }; + worker.execute(); + } + + /** + * + */ + public void disconnect() { + disconnect(false); + } + + /** + * @param wait + * - set this to true if the method should only return once the + * application has successfully disconnected + */ + public void disconnect(boolean wait) { + SwingWorker worker = new SwingWorker() { + + @Override + protected Boolean doInBackground() throws Exception { + return ZooInspectorPanel.this.zooInspectorManager.disconnect(); + } + + @Override + protected void done() { + try { + if (get()) { + treeViewer.clearView(); + toolbar.toggleButtons(false); + } + } catch (InterruptedException e) { + LoggerFactory + .getLogger() + .error( + "Error occurred while disconnecting from ZooKeeper server", + e); + } catch (ExecutionException e) { + LoggerFactory + .getLogger() + .error( + "Error occurred while disconnecting from ZooKeeper server", + e); + } + } + + }; + worker.execute(); + if (wait) { + while (!worker.isDone()) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + LoggerFactory + .getLogger() + .error( + "Error occurred while disconnecting from ZooKeeper server", + e); + } + } + } + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.gui.NodeViewersChangeListener# + * nodeViewersChanged(java.util.List) + */ + public void nodeViewersChanged(List newViewers) { + this.nodeViewersPanel.setNodeViewers(newViewers); + } + + /** + * @param connectionProps + * @throws IOException + */ + public void setdefaultConnectionProps(Properties connectionProps) + throws IOException { + this.zooInspectorManager.saveDefaultConnectionFile(connectionProps); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.java new file mode 100644 index 0000000..e08f2d3 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/ZooInspectorTreeViewer.java @@ -0,0 +1,384 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javax.swing.ImageIcon; +import javax.swing.JMenuItem; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.JComponent; +import javax.swing.JTree; +import javax.swing.SwingWorker; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreeNode; +import javax.swing.tree.TreePath; + +import org.apache.zookeeper.inspector.gui.actions.AddNodeAction; +import org.apache.zookeeper.inspector.gui.actions.DeleteNodeAction; +import org.apache.zookeeper.inspector.manager.NodeListener; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; + +import com.nitido.utils.toaster.Toaster; +import static javax.swing.KeyStroke.getKeyStroke; + +/** + * A {@link JPanel} for showing the tree view of all the nodes in the zookeeper + * instance + */ +public class ZooInspectorTreeViewer extends JPanel implements NodeListener { + private final ZooInspectorManager zooInspectorManager; + private final JTree tree; + private final Toaster toasterManager; + private final ImageIcon toasterIcon; + + /** + * @param zooInspectorManager + * - the {@link ZooInspectorManager} for the application + * @param listener + * - the {@link TreeSelectionListener} to listen for changes in + * the selected node on the node tree + */ + public ZooInspectorTreeViewer( + final ZooInspectorManager zooInspectorManager, + TreeSelectionListener listener, IconResource iconResource) { + + this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) + .put(getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK), "deleteNode"); + + this.getActionMap().put("deleteNode", + new DeleteNodeAction(this, this, zooInspectorManager)); + + this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) + .put(getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK), "addNode"); + + this.getActionMap().put("addNode", + new AddNodeAction(this, this, zooInspectorManager)); + + this.zooInspectorManager = zooInspectorManager; + this.setLayout(new BorderLayout()); + final JPopupMenu popupMenu = new JPopupMenu(); + + final JMenuItem addNode = new JMenuItem("Add Node"); + addNode.addActionListener(new AddNodeAction(this, this, zooInspectorManager)); + + final JMenuItem deleteNode = new JMenuItem("Delete Node"); + deleteNode.addActionListener(new DeleteNodeAction(this, this, zooInspectorManager)); + + final JMenuItem addNotify = new JMenuItem("Add Change Notification"); + this.toasterManager = new Toaster(); + this.toasterManager.setBorderColor(Color.BLACK); + this.toasterManager.setMessageColor(Color.BLACK); + this.toasterManager.setToasterColor(Color.WHITE); + toasterIcon = iconResource.get(IconResource.ICON_INFORMATION,""); + addNotify.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + List selectedNodes = getSelectedNodes(); + zooInspectorManager.addWatchers(selectedNodes, + ZooInspectorTreeViewer.this); + } + }); + final JMenuItem removeNotify = new JMenuItem( + "Remove Change Notification"); + removeNotify.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + List selectedNodes = getSelectedNodes(); + zooInspectorManager.removeWatchers(selectedNodes); + } + }); + tree = new JTree(new DefaultMutableTreeNode()); + tree.setCellRenderer(new ZooInspectorTreeCellRenderer(iconResource)); + tree.setEditable(false); + tree.getSelectionModel().addTreeSelectionListener(listener); + tree.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON3) { + // TODO only show add if a selected node isn't being + // watched, and only show remove if a selected node is being + // watched + popupMenu.removeAll(); + popupMenu.add(addNode); + popupMenu.add(deleteNode); + popupMenu.add(addNotify); + popupMenu.add(removeNotify); + popupMenu.show(ZooInspectorTreeViewer.this, e.getX(), e + .getY()); + } + } + }); + this.add(tree, BorderLayout.CENTER); + } + + /** + * Refresh the tree view + */ + public void refreshView() { + final Set expandedNodes = new LinkedHashSet(); + int rowCount = tree.getRowCount(); + for (int i = 0; i < rowCount; i++) { + TreePath path = tree.getPathForRow(i); + if (tree.isExpanded(path)) { + expandedNodes.add(path); + } + } + final TreePath[] selectedNodes = tree.getSelectionPaths(); + SwingWorker worker = new SwingWorker() { + + @Override + protected Boolean doInBackground() throws Exception { + tree.setModel(new DefaultTreeModel(new ZooInspectorTreeNode( + "/", null))); + return true; + } + + @Override + protected void done() { + for (TreePath path : expandedNodes) { + tree.expandPath(path); + } + tree.getSelectionModel().setSelectionPaths(selectedNodes); + } + }; + worker.execute(); + } + + /** + * clear the tree view of all nodes + */ + public void clearView() { + tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode())); + } + + private static class ZooInspectorTreeCellRenderer extends + DefaultTreeCellRenderer { + public ZooInspectorTreeCellRenderer(IconResource iconResource) { + setLeafIcon(iconResource.get(IconResource.ICON_TREE_LEAF,"")); + setOpenIcon(iconResource.get(IconResource.ICON_TREE_OPEN,"")); + setClosedIcon(iconResource.get(IconResource.ICON_TREE_CLOSE,"")); + } + } + + private class ZooInspectorTreeNode implements TreeNode { + private final String nodePath; + private final String nodeName; + private final ZooInspectorTreeNode parent; + + public ZooInspectorTreeNode(String nodePath, ZooInspectorTreeNode parent) { + this.parent = parent; + this.nodePath = nodePath; + int index = nodePath.lastIndexOf("/"); + if (index == -1) { + throw new IllegalArgumentException("Invalid node path" + + nodePath); + } + this.nodeName = nodePath.substring(index + 1); + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#children() + */ + public Enumeration children() { + List children = zooInspectorManager + .getChildren(this.nodePath); + Collections.sort(children); + List returnChildren = new ArrayList(); + for (String child : children) { + returnChildren.add(new ZooInspectorTreeNode((this.nodePath + .equals("/") ? "" : this.nodePath) + + "/" + child, this)); + } + return Collections.enumeration(returnChildren); + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#getAllowsChildren() + */ + public boolean getAllowsChildren() { + return zooInspectorManager.isAllowsChildren(this.nodePath); + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#getChildAt(int) + */ + public TreeNode getChildAt(int childIndex) { + String child = zooInspectorManager.getNodeChild(this.nodePath, + childIndex); + if (child != null) { + return new ZooInspectorTreeNode((this.nodePath.equals("/") ? "" + : this.nodePath) + + "/" + child, this); + } + return null; + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#getChildCount() + */ + public int getChildCount() { + return zooInspectorManager.getNumChildren(this.nodePath); + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode) + */ + public int getIndex(TreeNode node) { + return zooInspectorManager.getNodeIndex(this.nodePath); + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#getParent() + */ + public TreeNode getParent() { + return this.parent; + } + + /* + * (non-Javadoc) + * + * @see javax.swing.tree.TreeNode#isLeaf() + */ + public boolean isLeaf() { + return !zooInspectorManager.hasChildren(this.nodePath); + } + + @Override + public String toString() { + return this.nodeName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + getOuterType().hashCode(); + result = prime * result + + ((nodePath == null) ? 0 : nodePath.hashCode()); + result = prime * result + + ((parent == null) ? 0 : parent.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ZooInspectorTreeNode other = (ZooInspectorTreeNode) obj; + if (!getOuterType().equals(other.getOuterType())) + return false; + if (nodePath == null) { + if (other.nodePath != null) + return false; + } else if (!nodePath.equals(other.nodePath)) + return false; + if (parent == null) { + if (other.parent != null) + return false; + } else if (!parent.equals(other.parent)) + return false; + return true; + } + + private ZooInspectorTreeViewer getOuterType() { + return ZooInspectorTreeViewer.this; + } + + } + + /** + * @return {@link List} of the currently selected nodes + */ + public List getSelectedNodes() { + TreePath[] paths = tree.getSelectionPaths(); + List selectedNodes = new ArrayList(); + if (paths != null) { + for (TreePath path : paths) { + StringBuilder sb = new StringBuilder(); + Object[] pathArray = path.getPath(); + for (Object o : pathArray) { + String nodeName = o.toString(); + if (nodeName.length() > 0) { + sb.append("/"); + sb.append(o.toString()); + } + } + selectedNodes.add(sb.toString()); + } + } + return selectedNodes; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.NodeListener#processEvent(java + * .lang.String, java.lang.String, java.util.Map) + */ + public void processEvent(String nodePath, String eventType, + Map eventInfo) { + StringBuilder sb = new StringBuilder(); + sb.append("Node: "); + sb.append(nodePath); + sb.append("\nEvent: "); + sb.append(eventType); + if (eventInfo != null) { + for (Map.Entry entry : eventInfo.entrySet()) { + sb.append("\n"); + sb.append(entry.getKey()); + sb.append(": "); + sb.append(entry.getValue()); + } + } + this.toasterManager.showToaster(toasterIcon, sb.toString()); + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/about.html b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/about.html new file mode 100644 index 0000000..17fb3dc --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/about.html @@ -0,0 +1,21 @@ + + + + +ZooInspector v0.1 + + +

ZooInspector was developed by Colin Goodheart-Smithe and is +available under the Apache Software Licence v2.0.

+

The Icons used were sourced from the Eclipse project (http://www.eclipse.org) and licensed +under the Eclipse Public Licence v1.0. [http://www.eclipse.org/org/documents/epl-v10.php] +

+

ZooKeeper is available from http://zookeeper.apache.org/ +and is licensed under an Apache Software Licence v2.0

+

The ApacheSoftware Licence v2.0 can be found at http://www.apache.org/licenses/LICENSE-2.0

+ + diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/actions/AddNodeAction.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/actions/AddNodeAction.java new file mode 100644 index 0000000..3091611 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/actions/AddNodeAction.java @@ -0,0 +1,74 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui.actions; + +import org.apache.zookeeper.inspector.gui.ZooInspectorPanel; +import org.apache.zookeeper.inspector.gui.ZooInspectorTreeViewer; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; +import java.awt.event.KeyEvent; + +public class AddNodeAction extends AbstractAction { + + private JPanel panel; + private ZooInspectorTreeViewer treeViewer; + private ZooInspectorManager zooInspectorManager; + + public AddNodeAction(JPanel parentPanel, + ZooInspectorTreeViewer treeViewer, + ZooInspectorManager zooInspectorManager) { + this.panel = parentPanel; + this.treeViewer = treeViewer; + this.zooInspectorManager = zooInspectorManager; + } + + public void actionPerformed(ActionEvent e) { + final List selectedNodes = treeViewer + .getSelectedNodes(); + if (selectedNodes.size() == 1) { + final String nodeName = JOptionPane.showInputDialog( + panel, + "Please Enter a name for the new node", + "Create Node", JOptionPane.INFORMATION_MESSAGE); + if (nodeName != null && nodeName.length() > 0) { + SwingWorker worker = new SwingWorker() { + + @Override + protected Boolean doInBackground() throws Exception { + return zooInspectorManager + .createNode(selectedNodes.get(0), + nodeName); + } + + @Override + protected void done() { + treeViewer.refreshView(); + } + }; + worker.execute(); + } + } else { + JOptionPane.showMessageDialog(panel, + "Please select 1 parent node for the new node."); + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/actions/DeleteNodeAction.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/actions/DeleteNodeAction.java new file mode 100644 index 0000000..9001670 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/actions/DeleteNodeAction.java @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui.actions; + +import org.apache.zookeeper.inspector.gui.ZooInspectorTreeViewer; +import org.apache.zookeeper.inspector.manager.ZooInspectorManager; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; +import java.awt.event.KeyEvent; + +public class DeleteNodeAction extends AbstractAction { + + private JPanel parentPanel; + private ZooInspectorTreeViewer treeViewer; + private ZooInspectorManager zooInspectorManager; + + public DeleteNodeAction(JPanel parentPanel, + ZooInspectorTreeViewer treeViewer, + ZooInspectorManager zooInspectorManager) { + this.parentPanel = parentPanel; + this.treeViewer = treeViewer; + this.zooInspectorManager = zooInspectorManager; + } + + + public void actionPerformed(ActionEvent e) { + final List selectedNodes = treeViewer + .getSelectedNodes(); + if (selectedNodes.size() == 0) { + JOptionPane.showMessageDialog(parentPanel, + "Please select at least 1 node to be deleted"); + } else { + int answer = JOptionPane.showConfirmDialog( + parentPanel, + "Are you sure you want to delete the selected nodes?" + + "(This action cannot be reverted)", + "Confirm Delete", JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE + ); + if (answer == JOptionPane.YES_OPTION) { + SwingWorker worker = new SwingWorker() { + + @Override + protected Boolean doInBackground() throws Exception { + for (String nodePath : selectedNodes) { + zooInspectorManager + .deleteNode(nodePath); + } + return true; + } + + @Override + protected void done() { + treeViewer.refreshView(); + } + }; + worker.execute(); + } + } + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerACL.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerACL.java new file mode 100644 index 0000000..5ac203c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerACL.java @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui.nodeviewer; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import javax.swing.BorderFactory; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; +import javax.swing.SwingWorker; + +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; + +/** + * A node viewer for displaying the ACLs currently applied to the selected node + */ +public class NodeViewerACL extends ZooInspectorNodeViewer { + private ZooInspectorNodeManager zooInspectorManager; + private final JPanel aclDataPanel; + private String selectedNode; + + /** + * + */ + public NodeViewerACL() { + this.setLayout(new BorderLayout()); + this.aclDataPanel = new JPanel(); + this.aclDataPanel.setBackground(Color.WHITE); + JScrollPane scroller = new JScrollPane(this.aclDataPanel); + this.add(scroller, BorderLayout.CENTER); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * getTitle() + */ + @Override + public String getTitle() { + return "Node ACLs"; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * nodeSelectionChanged(java.util.Set) + */ + @Override + public void nodeSelectionChanged(List selectedNodes) { + this.aclDataPanel.removeAll(); + if (selectedNodes.size() > 0) { + this.selectedNode = selectedNodes.get(0); + SwingWorker>, Void> worker = new SwingWorker>, Void>() { + + @Override + protected List> doInBackground() + throws Exception { + return NodeViewerACL.this.zooInspectorManager + .getACLs(NodeViewerACL.this.selectedNode); + } + + @Override + protected void done() { + List> acls = null; + try { + acls = get(); + } catch (InterruptedException e) { + acls = new ArrayList>(); + LoggerFactory.getLogger().error( + "Error retrieving ACL Information for node: " + + NodeViewerACL.this.selectedNode, e); + } catch (ExecutionException e) { + acls = new ArrayList>(); + LoggerFactory.getLogger().error( + "Error retrieving ACL Information for node: " + + NodeViewerACL.this.selectedNode, e); + } + aclDataPanel.setLayout(new GridBagLayout()); + int j = 0; + for (Map data : acls) { + int rowPos = 2 * j + 1; + JPanel aclPanel = new JPanel(); + aclPanel.setBorder(BorderFactory + .createLineBorder(Color.BLACK)); + aclPanel.setBackground(Color.WHITE); + aclPanel.setLayout(new GridBagLayout()); + int i = 0; + for (Map.Entry entry : data.entrySet()) { + int rowPosACL = 2 * i + 1; + JLabel label = new JLabel(entry.getKey()); + JTextField text = new JTextField(entry.getValue()); + text.setEditable(false); + GridBagConstraints c1 = new GridBagConstraints(); + c1.gridx = 1; + c1.gridy = rowPosACL; + c1.gridwidth = 1; + c1.gridheight = 1; + c1.weightx = 0; + c1.weighty = 0; + c1.anchor = GridBagConstraints.NORTHWEST; + c1.fill = GridBagConstraints.BOTH; + c1.insets = new Insets(5, 5, 5, 5); + c1.ipadx = 0; + c1.ipady = 0; + aclPanel.add(label, c1); + GridBagConstraints c2 = new GridBagConstraints(); + c2.gridx = 3; + c2.gridy = rowPosACL; + c2.gridwidth = 1; + c2.gridheight = 1; + c2.weightx = 0; + c2.weighty = 0; + c2.anchor = GridBagConstraints.NORTHWEST; + c2.fill = GridBagConstraints.BOTH; + c2.insets = new Insets(5, 5, 5, 5); + c2.ipadx = 0; + c2.ipady = 0; + aclPanel.add(text, c2); + i++; + } + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 1; + c.gridy = rowPos; + c.gridwidth = 1; + c.gridheight = 1; + c.weightx = 1; + c.weighty = 1; + c.anchor = GridBagConstraints.NORTHWEST; + c.fill = GridBagConstraints.NONE; + c.insets = new Insets(5, 5, 5, 5); + c.ipadx = 0; + c.ipady = 0; + aclDataPanel.add(aclPanel, c); + } + NodeViewerACL.this.aclDataPanel.revalidate(); + NodeViewerACL.this.aclDataPanel.repaint(); + } + }; + worker.execute(); + } + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * setZooInspectorManager + * (org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager) + */ + @Override + public void setZooInspectorManager( + ZooInspectorNodeManager zooInspectorManager) { + this.zooInspectorManager = zooInspectorManager; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java new file mode 100644 index 0000000..6061e17 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerData.java @@ -0,0 +1,143 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui.nodeviewer; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; +import java.util.concurrent.ExecutionException; + +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JScrollPane; +import javax.swing.JTextPane; +import javax.swing.JToolBar; +import javax.swing.SwingWorker; + +import org.apache.zookeeper.inspector.ZooInspector; +import org.apache.zookeeper.inspector.gui.IconResource; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; + +/** + * A node viewer for displaying the data for the currently selected node + */ +public class NodeViewerData extends ZooInspectorNodeViewer { + private ZooInspectorNodeManager zooInspectorManager; + private final JTextPane dataArea; + private final JToolBar toolbar; + private String selectedNode; + + public NodeViewerData() { + this.setLayout(new BorderLayout()); + this.dataArea = new JTextPane(); + this.toolbar = new JToolBar(); + this.toolbar.setFloatable(false); + JScrollPane scroller = new JScrollPane(this.dataArea); + scroller + .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); + this.add(scroller, BorderLayout.CENTER); + this.add(this.toolbar, BorderLayout.NORTH); + JButton saveButton = new JButton(ZooInspector.iconResource.get(IconResource.ICON_SAVE,"")); + saveButton.addActionListener(new ActionListener() { + + public void actionPerformed(ActionEvent e) { + if (selectedNode != null) { + if (JOptionPane.showConfirmDialog(NodeViewerData.this, + "Are you sure you want to save this node?" + + " (this action cannot be reverted)", + "Confirm Save", JOptionPane.YES_NO_OPTION, + JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION) { + zooInspectorManager.setData(selectedNode, dataArea + .getText()); + } + } + } + }); + this.toolbar.add(saveButton); + + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * getTitle() + */ + @Override + public String getTitle() { + return "Node Data"; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * nodeSelectionChanged(java.util.Set) + */ + @Override + public void nodeSelectionChanged(List selectedNodes) { + if (selectedNodes.size() > 0) { + this.selectedNode = selectedNodes.get(0); + SwingWorker worker = new SwingWorker() { + + @Override + protected String doInBackground() throws Exception { + return NodeViewerData.this.zooInspectorManager + .getData(NodeViewerData.this.selectedNode); + } + + @Override + protected void done() { + String data = ""; + try { + data = get(); + } catch (InterruptedException e) { + LoggerFactory.getLogger().error( + "Error retrieving data for node: " + + NodeViewerData.this.selectedNode, e); + } catch (ExecutionException e) { + LoggerFactory.getLogger().error( + "Error retrieving data for node: " + + NodeViewerData.this.selectedNode, e); + } + NodeViewerData.this.dataArea.setText(data); + } + }; + worker.execute(); + } + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * setZooInspectorManager + * (org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager) + */ + @Override + public void setZooInspectorManager( + ZooInspectorNodeManager zooInspectorManager) { + this.zooInspectorManager = zooInspectorManager; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerMetaData.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerMetaData.java new file mode 100644 index 0000000..5c2df8d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/NodeViewerMetaData.java @@ -0,0 +1,186 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui.nodeviewer; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextField; +import javax.swing.SwingWorker; + +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; + +/** + * A node viewer for displaying the meta data for the currently selected node. + * The meta data is essentially the information from the {@link Stat} for the + * node + */ +public class NodeViewerMetaData extends ZooInspectorNodeViewer { + private ZooInspectorNodeManager zooInspectorManager; + private final JPanel metaDataPanel; + private String selectedNode; + + /** + * + */ + public NodeViewerMetaData() { + this.setLayout(new BorderLayout()); + this.metaDataPanel = new JPanel(); + this.metaDataPanel.setBackground(Color.WHITE); + JScrollPane scroller = new JScrollPane(this.metaDataPanel); + this.add(scroller, BorderLayout.CENTER); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * getTitle() + */ + @Override + public String getTitle() { + return "Node Metadata"; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * nodeSelectionChanged(java.util.Set) + */ + @Override + public void nodeSelectionChanged(List selectedNodes) { + this.metaDataPanel.removeAll(); + if (selectedNodes.size() > 0) { + this.selectedNode = selectedNodes.get(0); + SwingWorker, Void> worker = new SwingWorker, Void>() { + + @Override + protected Map doInBackground() throws Exception { + return NodeViewerMetaData.this.zooInspectorManager + .getNodeMeta(NodeViewerMetaData.this.selectedNode); + } + + @Override + protected void done() { + Map data = null; + try { + data = get(); + } catch (InterruptedException e) { + data = new HashMap(); + LoggerFactory.getLogger().error( + "Error retrieving meta data for node: " + + NodeViewerMetaData.this.selectedNode, + e); + } catch (ExecutionException e) { + data = new HashMap(); + LoggerFactory.getLogger().error( + "Error retrieving meta data for node: " + + NodeViewerMetaData.this.selectedNode, + e); + } + NodeViewerMetaData.this.metaDataPanel + .setLayout(new GridBagLayout()); + JPanel infoPanel = new JPanel(); + infoPanel.setBackground(Color.WHITE); + infoPanel.setLayout(new GridBagLayout()); + int i = 0; + int rowPos = 0; + for (Map.Entry entry : data.entrySet()) { + rowPos = 2 * i + 1; + JLabel label = new JLabel(entry.getKey()); + JTextField text = new JTextField(entry.getValue()); + text.setEditable(false); + GridBagConstraints c1 = new GridBagConstraints(); + c1.gridx = 0; + c1.gridy = rowPos; + c1.gridwidth = 1; + c1.gridheight = 1; + c1.weightx = 0; + c1.weighty = 0; + c1.anchor = GridBagConstraints.WEST; + c1.fill = GridBagConstraints.HORIZONTAL; + c1.insets = new Insets(5, 5, 5, 5); + c1.ipadx = 0; + c1.ipady = 0; + infoPanel.add(label, c1); + GridBagConstraints c2 = new GridBagConstraints(); + c2.gridx = 2; + c2.gridy = rowPos; + c2.gridwidth = 1; + c2.gridheight = 1; + c2.weightx = 0; + c2.weighty = 0; + c2.anchor = GridBagConstraints.WEST; + c2.fill = GridBagConstraints.HORIZONTAL; + c2.insets = new Insets(5, 5, 5, 5); + c2.ipadx = 0; + c2.ipady = 0; + infoPanel.add(text, c2); + i++; + } + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 1; + c.gridy = rowPos; + c.gridwidth = 1; + c.gridheight = 1; + c.weightx = 1; + c.weighty = 1; + c.anchor = GridBagConstraints.NORTHWEST; + c.fill = GridBagConstraints.NONE; + c.insets = new Insets(5, 5, 5, 5); + c.ipadx = 0; + c.ipady = 0; + NodeViewerMetaData.this.metaDataPanel.add(infoPanel, c); + NodeViewerMetaData.this.metaDataPanel.revalidate(); + NodeViewerMetaData.this.metaDataPanel.repaint(); + } + }; + worker.execute(); + } + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.gui.nodeviewer.ZooInspectorNodeViewer# + * setZooInspectorManager + * (org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager) + */ + @Override + public void setZooInspectorManager( + ZooInspectorNodeManager zooInspectorManager) { + this.zooInspectorManager = zooInspectorManager; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/ZooInspectorNodeViewer.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/ZooInspectorNodeViewer.java new file mode 100644 index 0000000..32119a8 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/gui/nodeviewer/ZooInspectorNodeViewer.java @@ -0,0 +1,138 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.gui.nodeviewer; + +import java.awt.datatransfer.DataFlavor; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.io.IOException; +import java.util.List; + +import javax.swing.JPanel; + +import org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager; + +/** + * A {@link JPanel} for displaying information about the currently selected + * node(s) + */ +public abstract class ZooInspectorNodeViewer extends JPanel implements + Transferable { + /** + * The {@link DataFlavor} used for DnD in the node viewer configuration + * dialog + */ + public static final DataFlavor nodeViewerDataFlavor = new DataFlavor( + ZooInspectorNodeViewer.class, "nodeviewer"); + + /** + * @param zooInspectorManager + */ + public abstract void setZooInspectorManager( + ZooInspectorNodeManager zooInspectorManager); + + /** + * Called whenever the selected nodes in the tree view changes. + * + * @param selectedNodes + * - the nodes currently selected in the tree view + * + */ + public abstract void nodeSelectionChanged(List selectedNodes); + + /** + * @return the title of the node viewer. this will be shown on the tab for + * this node viewer. + */ + public abstract String getTitle(); + + /* + * (non-Javadoc) + * + * @see + * java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer + * .DataFlavor) + */ + public Object getTransferData(DataFlavor flavor) + throws UnsupportedFlavorException, IOException { + if (flavor.equals(nodeViewerDataFlavor)) { + return this.getClass().getCanonicalName(); + } else { + return null; + } + } + + /* + * (non-Javadoc) + * + * @see java.awt.datatransfer.Transferable#getTransferDataFlavors() + */ + public DataFlavor[] getTransferDataFlavors() { + return new DataFlavor[] { nodeViewerDataFlavor }; + } + + /* + * (non-Javadoc) + * + * @seejava.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt. + * datatransfer.DataFlavor) + */ + public boolean isDataFlavorSupported(DataFlavor flavor) { + return flavor.equals(nodeViewerDataFlavor); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((getTitle() == null) ? 0 : getTitle().hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ZooInspectorNodeViewer other = (ZooInspectorNodeViewer) obj; + if (getClass().getCanonicalName() != other.getClass() + .getCanonicalName()) { + return false; + } + if (getTitle() == null) { + if (other.getTitle() != null) + return false; + } else if (!getTitle().equals(other.getTitle())) + return false; + return true; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/logger/LoggerFactory.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/logger/LoggerFactory.java new file mode 100644 index 0000000..e4fae41 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/logger/LoggerFactory.java @@ -0,0 +1,36 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.logger; + +/** + * Provides a {@link Logger} for use across the entire application + * + */ +public class LoggerFactory +{ + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("org.apache.zookeeper.inspector"); //$NON-NLS-1$ + + /** + * @return {@link Logger} for ZooInspector + */ + public static org.slf4j.Logger getLogger() + { + return logger; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/NodeListener.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/NodeListener.java new file mode 100644 index 0000000..fe55a45 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/NodeListener.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +import java.util.Map; + +/** + * A Listener for Events on zookeeper nodes + */ +public interface NodeListener { + /** + * @param nodePath + * - the path of the node + * @param eventType + * - the event type + * @param eventInfo + * - a {@link Map} containing any other information about this + * event + */ + public void processEvent(String nodePath, String eventType, + Map eventInfo); +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/NodesCache.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/NodesCache.java new file mode 100644 index 0000000..45c5a27 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/NodesCache.java @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.inspector.logger.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +public class NodesCache { + + public static final int CACHE_SIZE = 40000; + + public static final int EXPIRATION_TIME = 100; + + private final LoadingCache> nodes; + + private ZooKeeper zooKeeper; + + public NodesCache(ZooKeeper zooKeeper) { + this.zooKeeper = zooKeeper; + this.nodes = CacheBuilder.newBuilder() + .maximumSize(CACHE_SIZE) + .expireAfterWrite(EXPIRATION_TIME, TimeUnit.MILLISECONDS) + .build( + new CacheLoader>() { + @Override + public List load(String nodePath) throws Exception { + return getChildren(nodePath); + } + } + ); + } + + public List getChildren(String nodePath) { + try { + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + List children = this.zooKeeper.getChildren(nodePath, false); + Collections.sort(children); + return children; + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving child of node: " + nodePath, e + ); + } + return null; + } + + public String getNodeChild(String nodePath, int index) { + List childNodes = null; + try { + childNodes = nodes.get(nodePath); + return childNodes.get(index); + } catch (ExecutionException e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving child " + index + "of node: " + nodePath, e + ); + } + return null; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/Pair.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/Pair.java new file mode 100644 index 0000000..b72950c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/Pair.java @@ -0,0 +1,120 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +/** + * A utility class for storing a pair of objects + * + * @param + * @param + */ +public class Pair { + private K key; + private V value; + + /** + * @param key + * @param value + */ + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + /** + * + */ + public Pair() { + // Do Nothing + } + + /** + * @return key + */ + public K getKey() { + return key; + } + + /** + * @param key + */ + public void setKey(K key) { + this.key = key; + } + + /** + * @return value + */ + public V getValue() { + return value; + } + + /** + * @param value + */ + public void setValue(V value) { + this.value = value; + } + + @Override + public String toString() { + return "Pair [" + key + ", " + value + "]"; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((key == null) ? 0 : key.hashCode()); + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Pair other = (Pair) obj; + if (key == null) { + if (other.key != null) + return false; + } else if (!key.equals(other.key)) + return false; + if (value == null) { + if (other.value != null) + return false; + } else if (!value.equals(other.value)) + return false; + return true; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorManager.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorManager.java new file mode 100644 index 0000000..74c3cb2 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorManager.java @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.swing.JComboBox; +import javax.swing.JTextField; + +/** + * A Manager for all interactions between the application and the Zookeeper + * instance + */ +public interface ZooInspectorManager extends ZooInspectorNodeManager, + ZooInspectorNodeTreeManager { + + /** + * @param connectionProps + * @return true if successfully connected + */ + public boolean connect(Properties connectionProps); + + /** + * @return true if successfully disconnected + */ + public boolean disconnect(); + + /** + * @return a {@link Pair} containing the following: + *
    + *
  • a {@link Map} of property keys to list of possible values. If + * the list size is 1 the value is taken to be the default value for + * a {@link JTextField}. If the list size is greater than 1, the + * values are taken to be the possible options to show in a + * {@link JComboBox} with the first selected as default.
  • + *
  • a {@link Map} of property keys to the label to show on the UI + *
  • + *
      + * + */ + public Pair>, Map> getConnectionPropertiesTemplate(); + + /** + * @param selectedNodes + * - the nodes to add the watcher to + * @param nodeListener + * - the node listener for this watcher + */ + public void addWatchers(Collection selectedNodes, + NodeListener nodeListener); + + /** + * @param selectedNodes + * - the nodes to remove the watchers from + */ + public void removeWatchers(Collection selectedNodes); + + /** + * @param selectedFile + * - the file to load which contains the node viewers + * configuration + * @return nodeViewers - the class names of the node viewers from the + * configuration + * @throws IOException + * - if the configuration file cannot be loaded + */ + public List loadNodeViewersFile(File selectedFile) + throws IOException; + + /** + * @param selectedFile + * - the file to save the configuration to + * @param nodeViewersClassNames + * - the class names of the node viewers + * @throws IOException + * - if the configuration file cannot be saved + */ + public void saveNodeViewersFile(File selectedFile, + List nodeViewersClassNames) throws IOException; + + /** + * @param nodeViewersClassNames + * - the class names of the node viewers + * @throws IOException + * - if the default configuration file cannot be loaded + */ + public void setDefaultNodeViewerConfiguration( + List nodeViewersClassNames) throws IOException; + + /** + * @return nodeViewers - the class names of the node viewers from the + * configuration + * @throws IOException + * - if the default configuration file cannot be loaded + */ + List getDefaultNodeViewerConfiguration() throws IOException; + + /** + * @param connectionProps + * - the connection properties last used to connect to the + * zookeeeper instance + */ + public void setLastConnectionProps(Properties connectionProps); + + /** + * @return last connection Properties - the connection properties last used + * to connect to the zookeeeper instance + */ + public Properties getLastConnectionProps(); + + /** + * @param props + * - the properties to use as the default connection settings + * @throws IOException + * - if the default configuration file cannot be saved + */ + public void saveDefaultConnectionFile(Properties props) throws IOException; + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java new file mode 100644 index 0000000..2c57907 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorManagerImpl.java @@ -0,0 +1,875 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.Perms; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager; +import org.apache.zookeeper.inspector.encryption.DataEncryptionManager; +import org.apache.zookeeper.inspector.logger.LoggerFactory; +import org.apache.zookeeper.retry.ZooKeeperRetry; + +/** + * A default implementation of {@link ZooInspectorManager} for connecting to + * zookeeper instances + */ +public class ZooInspectorManagerImpl implements ZooInspectorManager { + private static final String A_VERSION = "ACL Version"; + private static final String C_TIME = "Creation Time"; + private static final String C_VERSION = "Children Version"; + private static final String CZXID = "Creation ID"; + private static final String DATA_LENGTH = "Data Length"; + private static final String EPHEMERAL_OWNER = "Ephemeral Owner"; + private static final String M_TIME = "Last Modified Time"; + private static final String MZXID = "Modified ID"; + private static final String NUM_CHILDREN = "Number of Children"; + private static final String PZXID = "Node ID"; + private static final String VERSION = "Data Version"; + private static final String ACL_PERMS = "Permissions"; + private static final String ACL_SCHEME = "Scheme"; + private static final String ACL_ID = "Id"; + private static final String SESSION_STATE = "Session State"; + private static final String SESSION_ID = "Session ID"; + /** + * The key used for the connect string in the connection properties file + */ + public static final String CONNECT_STRING = "hosts"; + /** + * The key used for the session timeout in the connection properties file + */ + public static final String SESSION_TIMEOUT = "timeout"; + /** + * The key used for the data encryption manager in the connection properties + * file + */ + public static final String DATA_ENCRYPTION_MANAGER = "encryptionManager"; + /** + * The key used for the authentication scheme in the connection properties file + */ + public static final String AUTH_SCHEME_KEY = "authScheme"; + /** + * The key used for the authentication data in the connection properties file + */ + public static final String AUTH_DATA_KEY = "authData"; + + + private static final File defaultNodeViewersFile = new File( + "./src/main/resources/defaultNodeViewers.cfg"); + private static final File defaultConnectionFile = new File( + "./src/main/resources/defaultConnectionSettings.cfg"); + + private DataEncryptionManager encryptionManager; + private String connectString; + private int sessionTimeout; + private ZooKeeper zooKeeper; + private final Map watchers = new HashMap(); + protected boolean connected = true; + private Properties lastConnectionProps; + private String defaultEncryptionManager; + private String defaultTimeout; + private String defaultHosts; + private String defaultAuthScheme; + private String defaultAuthValue; + private NodesCache nodesCache; + + /** + * @throws IOException + * - thrown if the default connection settings cannot be loaded + * + */ + public ZooInspectorManagerImpl() throws IOException { + loadDefaultConnectionFile(); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorManager#connect(java + * .util.Properties) + */ + public boolean connect(Properties connectionProps) { + try { + if (this.zooKeeper == null) { + String connectString = connectionProps + .getProperty(CONNECT_STRING); + String sessionTimeout = connectionProps + .getProperty(SESSION_TIMEOUT); + String encryptionManager = connectionProps + .getProperty(DATA_ENCRYPTION_MANAGER); + String authScheme = connectionProps + .getProperty(AUTH_SCHEME_KEY); + String authData = connectionProps + .getProperty(AUTH_DATA_KEY); + + if (connectString == null || sessionTimeout == null) { + throw new IllegalArgumentException( + "Both connect string and session timeout are required."); + } + if (encryptionManager == null) { + this.encryptionManager = new BasicDataEncryptionManager(); + } else { + Class clazz = Class.forName(encryptionManager); + + if (Arrays.asList(clazz.getInterfaces()).contains( + DataEncryptionManager.class)) { + this.encryptionManager = (DataEncryptionManager) Class + .forName(encryptionManager).newInstance(); + } else { + throw new IllegalArgumentException( + "Data encryption manager must implement DataEncryptionManager interface"); + } + } + this.connectString = connectString; + this.sessionTimeout = Integer.valueOf(sessionTimeout); + this.zooKeeper = new ZooKeeperRetry(connectString, Integer + .valueOf(sessionTimeout), new Watcher() { + + public void process(WatchedEvent event) { + if (event.getState() == KeeperState.Expired) { + connected = false; + } + } + }); + if (authData != null && authData.length() > 0){ + this.zooKeeper.addAuthInfo(authScheme, authData.getBytes()); + } + ((ZooKeeperRetry) this.zooKeeper).setRetryLimit(10); + connected = ((ZooKeeperRetry) this.zooKeeper).testConnection(); + } + } catch (Exception e) { + connected = false; + e.printStackTrace(); + } + if (!connected){ + disconnect(); + } else { + this.nodesCache = new NodesCache(zooKeeper); + } + return connected; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorManager#disconnect() + */ + public boolean disconnect() { + try { + if (this.zooKeeper != null) { + this.zooKeeper.close(); + this.zooKeeper = null; + connected = false; + removeWatchers(this.watchers.keySet()); + return true; + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred while disconnecting from ZooKeeper server", + e); + } + return false; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * getChildren(java.lang.String) + */ + public List getChildren(String nodePath) { + if (connected) { + return nodesCache.getChildren(nodePath); + } + return null; + + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager#getData + * (java.lang.String) + */ + public String getData(String nodePath) { + if (connected) { + try { + if (nodePath.length() == 0) { + nodePath = "/"; + } + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + return this.encryptionManager.decryptData(zooKeeper + .getData(nodePath, false, s)); + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred getting data for node: " + nodePath, e); + } + } + return null; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * getNodeChild(java.lang.String, int) + */ + public String getNodeChild(String nodePath, int childIndex) { + if (connected) { + return this.nodesCache.getNodeChild(nodePath, childIndex); + } + return null; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * getNodeIndex(java.lang.String) + */ + public int getNodeIndex(String nodePath) { + if (connected) { + int index = nodePath.lastIndexOf("/"); + if (index == -1 + || (!nodePath.equals("/") && nodePath.charAt(nodePath + .length() - 1) == '/')) { + throw new IllegalArgumentException("Invalid node path: " + + nodePath); + } + String parentPath = nodePath.substring(0, index); + String child = nodePath.substring(index + 1); + if (parentPath != null && parentPath.length() > 0) { + List children = this.nodesCache.getChildren(parentPath); + if (children != null) { + return children.indexOf(child); + } + } + } + return -1; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager#getACLs + * (java.lang.String) + */ + public List> getACLs(String nodePath) { + List> returnACLs = new ArrayList>(); + if (connected) { + try { + if (nodePath.length() == 0) { + nodePath = "/"; + } + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + List acls = zooKeeper.getACL(nodePath, s); + for (ACL acl : acls) { + Map aclMap = new LinkedHashMap(); + aclMap.put(ACL_SCHEME, acl.getId().getScheme()); + aclMap.put(ACL_ID, acl.getId().getId()); + StringBuilder sb = new StringBuilder(); + int perms = acl.getPerms(); + boolean addedPerm = false; + if ((perms & Perms.READ) == Perms.READ) { + sb.append("Read"); + addedPerm = true; + } + if (addedPerm) { + sb.append(", "); + } + if ((perms & Perms.WRITE) == Perms.WRITE) { + sb.append("Write"); + addedPerm = true; + } + if (addedPerm) { + sb.append(", "); + } + if ((perms & Perms.CREATE) == Perms.CREATE) { + sb.append("Create"); + addedPerm = true; + } + if (addedPerm) { + sb.append(", "); + } + if ((perms & Perms.DELETE) == Perms.DELETE) { + sb.append("Delete"); + addedPerm = true; + } + if (addedPerm) { + sb.append(", "); + } + if ((perms & Perms.ADMIN) == Perms.ADMIN) { + sb.append("Admin"); + addedPerm = true; + } + aclMap.put(ACL_PERMS, sb.toString()); + returnACLs.add(aclMap); + } + } + } catch (InterruptedException e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving ACLs of node: " + nodePath, + e); + } catch (KeeperException e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving ACLs of node: " + nodePath, + e); + } + } + return returnACLs; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * getNodeMeta(java.lang.String) + */ + public Map getNodeMeta(String nodePath) { + Map nodeMeta = new LinkedHashMap(); + if (connected) { + try { + if (nodePath.length() == 0) { + nodePath = "/"; + } + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + nodeMeta.put(A_VERSION, String.valueOf(s.getAversion())); + nodeMeta.put(C_TIME, String.valueOf(s.getCtime())); + nodeMeta.put(C_VERSION, String.valueOf(s.getCversion())); + nodeMeta.put(CZXID, String.valueOf(s.getCzxid())); + nodeMeta + .put(DATA_LENGTH, String.valueOf(s.getDataLength())); + nodeMeta.put(EPHEMERAL_OWNER, String.valueOf(s + .getEphemeralOwner())); + nodeMeta.put(M_TIME, String.valueOf(s.getMtime())); + nodeMeta.put(MZXID, String.valueOf(s.getMzxid())); + nodeMeta.put(NUM_CHILDREN, String.valueOf(s + .getNumChildren())); + nodeMeta.put(PZXID, String.valueOf(s.getPzxid())); + nodeMeta.put(VERSION, String.valueOf(s.getVersion())); + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving meta data for node: " + + nodePath, e); + } + } + return nodeMeta; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * getNumChildren(java.lang.String) + */ + public int getNumChildren(String nodePath) { + if (connected) { + try { + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + return s.getNumChildren(); + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred getting the number of children of node: " + + nodePath, e); + } + } + return -1; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * hasChildren(java.lang.String) + */ + public boolean hasChildren(String nodePath) { + return getNumChildren(nodePath) > 0; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * isAllowsChildren(java.lang.String) + */ + public boolean isAllowsChildren(String nodePath) { + if (connected) { + try { + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + return s.getEphemeralOwner() == 0; + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred determining whether node is allowed children: " + + nodePath, e); + } + } + return false; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorReadOnlyManager# + * getSessionMeta() + */ + public Map getSessionMeta() { + Map sessionMeta = new LinkedHashMap(); + try { + if (zooKeeper != null) { + + sessionMeta.put(SESSION_ID, String.valueOf(zooKeeper + .getSessionId())); + sessionMeta.put(SESSION_STATE, String.valueOf(zooKeeper + .getState().toString())); + sessionMeta.put(CONNECT_STRING, this.connectString); + sessionMeta.put(SESSION_TIMEOUT, String + .valueOf(this.sessionTimeout)); + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred retrieving session meta data.", e); + } + return sessionMeta; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorNodeTreeManager#createNode + * (java.lang.String, java.lang.String) + */ + public boolean createNode(String parent, String nodeName) { + if (connected) { + try { + String[] nodeElements = nodeName.split("/"); + for (String nodeElement : nodeElements) { + String node = parent + "/" + nodeElement; + Stat s = zooKeeper.exists(node, false); + if (s == null) { + zooKeeper.create(node, this.encryptionManager + .encryptData(null), Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + parent = node; + } + } + return true; + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred creating node: " + parent + "/" + + nodeName, e); + } + } + return false; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorNodeTreeManager#deleteNode + * (java.lang.String) + */ + public boolean deleteNode(String nodePath) { + if (connected) { + try { + Stat s = zooKeeper.exists(nodePath, false); + if (s != null) { + List children = zooKeeper.getChildren(nodePath, + false); + for (String child : children) { + String node = nodePath + "/" + child; + deleteNode(node); + } + zooKeeper.delete(nodePath, -1); + } + return true; + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred deleting node: " + nodePath, e); + } + } + return false; + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorNodeManager#setData + * (java.lang.String, java.lang.String) + */ + public boolean setData(String nodePath, String data) { + if (connected) { + try { + zooKeeper.setData(nodePath, this.encryptionManager + .encryptData(data), -1); + return true; + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred setting data for node: " + nodePath, e); + } + } + return false; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * getConnectionPropertiesTemplate() + */ + public Pair>, Map> getConnectionPropertiesTemplate() { + Map> template = new LinkedHashMap>(); + template.put(CONNECT_STRING, Arrays + .asList(new String[] { defaultHosts })); + template.put(SESSION_TIMEOUT, Arrays + .asList(new String[] { defaultTimeout })); + template.put(DATA_ENCRYPTION_MANAGER, Arrays + .asList(new String[] { defaultEncryptionManager })); + template.put(AUTH_SCHEME_KEY, Arrays + .asList(new String[] { defaultAuthScheme })); + template.put(AUTH_DATA_KEY, Arrays + .asList(new String[] { defaultAuthValue })); + Map labels = new LinkedHashMap(); + labels.put(CONNECT_STRING, "Connect String"); + labels.put(SESSION_TIMEOUT, "Session Timeout"); + labels.put(DATA_ENCRYPTION_MANAGER, "Data Encryption Manager"); + labels.put(AUTH_SCHEME_KEY, "Authentication Scheme"); + labels.put(AUTH_DATA_KEY, "Authentication Data"); + return new Pair>, Map>( + template, labels); + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorManager#addWatchers + * (java.util.Collection, + * org.apache.zookeeper.inspector.manager.NodeListener) + */ + public void addWatchers(Collection selectedNodes, + NodeListener nodeListener) { + // add watcher for each node and add node to collection of + // watched nodes + if (connected) { + for (String node : selectedNodes) { + if (!watchers.containsKey(node)) { + try { + watchers.put(node, new NodeWatcher(node, nodeListener, + zooKeeper)); + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred adding node watcher for node: " + + node, e); + } + } + } + } + } + + /* + * (non-Javadoc) + * + * @see + * org.apache.zookeeper.inspector.manager.ZooInspectorManager#removeWatchers + * (java.util.Collection) + */ + public void removeWatchers(Collection selectedNodes) { + // remove watcher for each node and remove node from + // collection of watched nodes + if (connected) { + for (String node : selectedNodes) { + if (watchers.containsKey(node)) { + NodeWatcher watcher = watchers.remove(node); + if (watcher != null) { + watcher.stop(); + } + } + } + } + } + + /** + * A Watcher which will re-add itself every time an event is fired + * + */ + public class NodeWatcher implements Watcher { + + private final String nodePath; + private final NodeListener nodeListener; + private final ZooKeeper zookeeper; + private boolean closed = false; + + /** + * @param nodePath + * - the path to the node to watch + * @param nodeListener + * the {@link NodeListener} for this node + * @param zookeeper + * - a {@link ZooKeeper} to use to access zookeeper + * @throws InterruptedException + * @throws KeeperException + */ + public NodeWatcher(String nodePath, NodeListener nodeListener, + ZooKeeper zookeeper) throws KeeperException, + InterruptedException { + this.nodePath = nodePath; + this.nodeListener = nodeListener; + this.zookeeper = zookeeper; + Stat s = zooKeeper.exists(nodePath, this); + if (s != null) { + zookeeper.getChildren(nodePath, this); + } + } + + public void process(WatchedEvent event) { + if (!closed) { + try { + if (event.getType() != EventType.NodeDeleted) { + + Stat s = zooKeeper.exists(nodePath, this); + if (s != null) { + zookeeper.getChildren(nodePath, this); + } + } + } catch (Exception e) { + LoggerFactory.getLogger().error( + "Error occurred re-adding node watcherfor node " + + nodePath, e); + } + nodeListener.processEvent(event.getPath(), event.getType() + .name(), null); + } + } + + /** + * + */ + public void stop() { + this.closed = true; + } + + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * loadNodeViewersFile(java.io.File) + */ + public List loadNodeViewersFile(File selectedFile) + throws IOException { + List result = new ArrayList(); + if (defaultNodeViewersFile.exists()) { + FileReader reader = new FileReader(selectedFile); + try { + BufferedReader buff = new BufferedReader(reader); + try { + while (buff.ready()) { + String line = buff.readLine(); + if (line != null && line.length() > 0 && !line.startsWith("#")) { + result.add(line); + } + } + } finally { + buff.close(); + } + } finally { + reader.close(); + } + } + return result; + } + + private void loadDefaultConnectionFile() throws IOException { + if (defaultConnectionFile.exists()) { + Properties props = new Properties(); + + FileReader reader = new FileReader(defaultConnectionFile); + try { + props.load(reader); + } finally { + reader.close(); + } + defaultEncryptionManager = props + .getProperty(DATA_ENCRYPTION_MANAGER) == null ? "org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager" + : props.getProperty(DATA_ENCRYPTION_MANAGER); + defaultTimeout = props.getProperty(SESSION_TIMEOUT) == null ? "5000" + : props.getProperty(SESSION_TIMEOUT); + defaultHosts = props.getProperty(CONNECT_STRING) == null ? "localhost:2181" + : props.getProperty(CONNECT_STRING); + defaultAuthScheme = props.getProperty(AUTH_SCHEME_KEY) == null ? "" + : props.getProperty(AUTH_SCHEME_KEY); + defaultAuthValue = props.getProperty(AUTH_DATA_KEY) == null ? "" + : props.getProperty(AUTH_DATA_KEY); + } else { + defaultEncryptionManager = "org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager"; + defaultTimeout = "5000"; + defaultHosts = "localhost:2181"; + defaultAuthScheme = ""; + defaultAuthValue = ""; + } + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * saveDefaultConnectionFile(java.util.Properties) + */ + public void saveDefaultConnectionFile(Properties props) throws IOException { + File defaultDir = defaultConnectionFile.getParentFile(); + if (!defaultDir.exists()) { + if (!defaultDir.mkdirs()) { + throw new IOException( + "Failed to create configuration directory: " + + defaultDir.getAbsolutePath()); + } + } + if (!defaultConnectionFile.exists()) { + if (!defaultConnectionFile.createNewFile()) { + throw new IOException( + "Failed to create default connection file: " + + defaultConnectionFile.getAbsolutePath()); + } + } + FileWriter writer = new FileWriter(defaultConnectionFile); + try { + props.store(writer, "Default connection for ZooInspector"); + } finally { + writer.close(); + } + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * saveNodeViewersFile(java.io.File, java.util.List) + */ + public void saveNodeViewersFile(File selectedFile, + List nodeViewersClassNames) throws IOException { + if (!selectedFile.exists()) { + if (!selectedFile.createNewFile()) { + throw new IOException( + "Failed to create node viewers configuration file: " + + selectedFile.getAbsolutePath()); + } + } + FileWriter writer = new FileWriter(selectedFile); + try { + BufferedWriter buff = new BufferedWriter(writer); + try { + for (String nodeViewersClassName : nodeViewersClassNames) { + buff.append(nodeViewersClassName); + buff.append("\n"); + } + } finally { + buff.flush(); + buff.close(); + } + } finally { + writer.close(); + } + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * setDefaultNodeViewerConfiguration(java.io.File, java.util.List) + */ + public void setDefaultNodeViewerConfiguration( + List nodeViewersClassNames) throws IOException { + File defaultDir = defaultNodeViewersFile.getParentFile(); + if (!defaultDir.exists()) { + if (!defaultDir.mkdirs()) { + throw new IOException( + "Failed to create configuration directory: " + + defaultDir.getAbsolutePath()); + } + } + saveNodeViewersFile(defaultNodeViewersFile, nodeViewersClassNames); + } + + public List getDefaultNodeViewerConfiguration() throws IOException { + List defaultNodeViewers = loadNodeViewersFile(defaultNodeViewersFile); + if (defaultNodeViewers.isEmpty()) { + LoggerFactory.getLogger().warn("List of default node viewers is empty"); + } + return defaultNodeViewers; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * getLastConnectionProps() + */ + public Properties getLastConnectionProps() { + return this.lastConnectionProps; + } + + /* + * (non-Javadoc) + * + * @seeorg.apache.zookeeper.inspector.manager.ZooInspectorManager# + * setLastConnectionProps(java.util.Properties) + */ + public void setLastConnectionProps(Properties connectionProps) { + this.lastConnectionProps = connectionProps; + } +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeManager.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeManager.java new file mode 100644 index 0000000..0c62547 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeManager.java @@ -0,0 +1,33 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +/** + * A Manager for all interactions between the application and the nodes in a + * Zookeeper instance + * */ +public interface ZooInspectorNodeManager extends ZooInspectorReadOnlyManager { + /** + * @param nodePath + * - the path to the node on which to set the data + * @param data + * - the data to set on the this node + * @return true if the data for the node was successfully updated + */ + public boolean setData(String nodePath, String data); +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeTreeManager.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeTreeManager.java new file mode 100644 index 0000000..9718c1d --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorNodeTreeManager.java @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +/** + * A Manager for all interactions between the application and the node tree in a + * Zookeeper instance + */ +public interface ZooInspectorNodeTreeManager extends + ZooInspectorReadOnlyManager { + + /** + * @param parent + * - the parent node path for the node to add + * @param nodeName + * - the name of the new node + * @return true if the node was successfully created + */ + public abstract boolean createNode(String parent, String nodeName); + + /** + * @param nodePath + * - the path to the node to delete + * @return true if the node was successfully deleted + */ + public abstract boolean deleteNode(String nodePath); + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorReadOnlyManager.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorReadOnlyManager.java new file mode 100644 index 0000000..d9fdf5c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/inspector/manager/ZooInspectorReadOnlyManager.java @@ -0,0 +1,99 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.inspector.manager; + +import java.util.List; +import java.util.Map; + +/** + * A Manager for all read only interactions between the application and a node + * in a Zookeeper instance + */ +public interface ZooInspectorReadOnlyManager { + + /** + * @param nodePath + * - the path to the node to delete + * @return the data for the node + */ + public abstract String getData(String nodePath); + + /** + * @param nodePath + * - the path to the node to delete + * @return the metaData for the node + */ + public abstract Map getNodeMeta(String nodePath); + + /** + * @param nodePath + * - the path to the node to delete + * @return the ACLs set on the node + */ + public abstract List> getACLs(String nodePath); + + /** + * @return the metaData for the current session + */ + public abstract Map getSessionMeta(); + + /** + * @param nodePath + * - the path to the node to delete + * @return true if the node has children + */ + public abstract boolean hasChildren(String nodePath); + + /** + * @param nodePath + * - the path to the node to delete + * @return the index of the node within its siblings + */ + public abstract int getNodeIndex(String nodePath); + + /** + * @param nodePath + * - the path to the node to delete + * @return the number of children of the node + */ + public abstract int getNumChildren(String nodePath); + + /** + * @param nodePath + * - the path to the node to delete + * @param childIndex + * - the index to the node in the list of node children + * @return the path to the node for the child of the nodePath at childIndex + */ + public abstract String getNodeChild(String nodePath, int childIndex); + + /** + * @param nodePath + * - the path to the node to delete + * @return true if the node allows children nodes + */ + public abstract boolean isAllowsChildren(String nodePath); + + /** + * @param nodePath + * - the path to the node to delete + * @return a {@link List} of the children of the node + */ + public abstract List getChildren(String nodePath); + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/retry/ZooKeeperRetry.java b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/retry/ZooKeeperRetry.java new file mode 100644 index 0000000..ce959a1 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/java/org/apache/zookeeper/retry/ZooKeeperRetry.java @@ -0,0 +1,288 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.retry; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +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.apache.zookeeper.data.Stat; +import org.apache.zookeeper.inspector.logger.LoggerFactory; + +/** + * A Class which extends {@link ZooKeeper} and will automatically retry calls to + * zookeeper if a {@link KeeperException.ConnectionLossException} occurs + */ +public class ZooKeeperRetry extends ZooKeeper { + + private boolean closed = false; + private final Watcher watcher; + private int limit = -1; + + /** + * @param connectString + * @param sessionTimeout + * @param watcher + * @throws IOException + */ + public ZooKeeperRetry(String connectString, int sessionTimeout, + Watcher watcher) throws IOException { + super(connectString, sessionTimeout, watcher); + this.watcher = watcher; + } + + /** + * @param connectString + * @param sessionTimeout + * @param watcher + * @param sessionId + * @param sessionPasswd + * @throws IOException + */ + public ZooKeeperRetry(String connectString, int sessionTimeout, + Watcher watcher, long sessionId, byte[] sessionPasswd) + throws IOException { + super(connectString, sessionTimeout, watcher, sessionId, sessionPasswd); + this.watcher = watcher; + } + + @Override + public synchronized void close() throws InterruptedException { + this.closed = true; + super.close(); + } + + @Override + public String create(String path, byte[] data, List acl, + CreateMode createMode) throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.create(path, data, acl, createMode); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + if (exists(path, false) != null) { + return path; + } + } catch (KeeperException.NodeExistsException e) { + return path; + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public void delete(String path, int version) throws InterruptedException, + KeeperException { + int count = 0; + do { + try { + super.delete(path, version); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + if (exists(path, false) == null) { + return; + } + } catch (KeeperException.NoNodeException e) { + break; + } + } while (!closed && (limit == -1 || count++ < limit)); + } + + @Override + public Stat exists(String path, boolean watch) throws KeeperException, + InterruptedException { + int count = 0; + do { + try { + return super.exists(path, watch ? watcher : null); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public Stat exists(String path, Watcher watcher) throws KeeperException, + InterruptedException { + int count = 0; + do { + try { + return super.exists(path, watcher); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public List getACL(String path, Stat stat) throws KeeperException, + InterruptedException { + int count = 0; + do { + try { + return super.getACL(path, stat); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public List getChildren(String path, boolean watch) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getChildren(path, watch ? watcher : null); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return new ArrayList(); + } + + @Override + public List getChildren(String path, Watcher watcher) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getChildren(path, watcher); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return new ArrayList(); + } + + @Override + public byte[] getData(String path, boolean watch, Stat stat) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getData(path, watch ? watcher : null, stat); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public byte[] getData(String path, Watcher watcher, Stat stat) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.getData(path, watcher, stat); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public Stat setACL(String path, List acl, int aclVersion) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.setACL(path, acl, aclVersion); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + Stat s = exists(path, false); + if (s != null) { + if (getACL(path, s).equals(acl)) { + return s; + } + } else { + return null; + } + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + @Override + public Stat setData(String path, byte[] data, int version) + throws KeeperException, InterruptedException { + int count = 0; + do { + try { + return super.setData(path, data, version); + } catch (KeeperException.ConnectionLossException e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + Stat s = exists(path, false); + if (s != null) { + if (getData(path, false, s) == data) { + return s; + } + } else { + return null; + } + } + } while (!closed && (limit == -1 || count++ < limit)); + return null; + } + + /** + * @param limit + */ + public void setRetryLimit(int limit) { + this.limit = limit; + } + + /** + * @return true if successfully connected to zookeeper + */ + public boolean testConnection() { + int count = 0; + do { + try { + return super.exists("/", null) != null; + } catch (Exception e) { + LoggerFactory.getLogger().warn( + "ZooKeeper connection lost. Trying to reconnect."); + } + } while (count++ < 5); + return false; + } + +} diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/defaultConnectionSettings.cfg b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/defaultConnectionSettings.cfg new file mode 100644 index 0000000..90f20ae --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/defaultConnectionSettings.cfg @@ -0,0 +1,21 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +# +#Default connection for ZooInspector +hosts=localhost\:2181 +encryptionManager=org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager +timeout=5000 +authScheme= +authData= \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/defaultNodeViewers.cfg b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/defaultNodeViewers.cfg new file mode 100644 index 0000000..38fe9a8 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/defaultNodeViewers.cfg @@ -0,0 +1,17 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. +org.apache.zookeeper.inspector.gui.nodeviewer.NodeViewerData +org.apache.zookeeper.inspector.gui.nodeviewer.NodeViewerMetaData +org.apache.zookeeper.inspector.gui.nodeviewer.NodeViewerACL diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/add.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/add.png new file mode 100644 index 0000000..1aa7f09 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/add.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/document-new.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/document-new.png new file mode 100644 index 0000000..4c3efdd Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/document-new.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/document-save.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/document-save.png new file mode 100644 index 0000000..22ff495 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/document-save.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/down.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/down.png new file mode 100644 index 0000000..3dd7fcc Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/down.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/media-playback-start.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/media-playback-start.png new file mode 100644 index 0000000..a7de0fe Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/media-playback-start.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/media-playback-stop.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/media-playback-stop.png new file mode 100644 index 0000000..ede2815 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/media-playback-stop.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/remove.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/remove.png new file mode 100644 index 0000000..00b654e Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/remove.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/up.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/up.png new file mode 100644 index 0000000..fa9a7d7 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/up.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/view-refresh.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/view-refresh.png new file mode 100644 index 0000000..3fd71d6 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/actions/view-refresh.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/mimetypes/text-x-generic.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/mimetypes/text-x-generic.png new file mode 100644 index 0000000..2d7f2d6 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/mimetypes/text-x-generic.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/places/folder.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/places/folder.png new file mode 100644 index 0000000..65bd0bb Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/places/folder.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/places/user-trash.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/places/user-trash.png new file mode 100644 index 0000000..0e0953c Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/places/user-trash.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/status/info.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/status/info.png new file mode 100644 index 0000000..8851b99 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/16x16/status/info.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/add.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/add.png new file mode 100644 index 0000000..306d3d8 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/add.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/document-new.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/document-new.png new file mode 100644 index 0000000..e3808a1 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/document-new.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/document-save.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/document-save.png new file mode 100644 index 0000000..a94e0ea Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/document-save.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/down.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/down.png new file mode 100644 index 0000000..af23788 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/down.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/media-playback-start.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/media-playback-start.png new file mode 100644 index 0000000..10102d8 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/media-playback-start.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/media-playback-stop.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/media-playback-stop.png new file mode 100644 index 0000000..d0e4733 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/media-playback-stop.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/remove.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/remove.png new file mode 100644 index 0000000..45e5c2a Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/remove.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/up.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/up.png new file mode 100644 index 0000000..b0a0cd7 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/up.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/view-refresh.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/view-refresh.png new file mode 100644 index 0000000..cab4d02 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/actions/view-refresh.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/mimetypes/text-x-generic.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/mimetypes/text-x-generic.png new file mode 100644 index 0000000..d68a56c Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/mimetypes/text-x-generic.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/places/folder.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/places/folder.png new file mode 100644 index 0000000..01f45b8 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/places/folder.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/places/user-trash.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/places/user-trash.png new file mode 100644 index 0000000..05ff036 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/places/user-trash.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/status/info.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/status/info.png new file mode 100644 index 0000000..07cf010 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/22x22/status/info.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/add.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/add.png new file mode 100644 index 0000000..681e964 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/add.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/document-new.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/document-new.png new file mode 100644 index 0000000..9361222 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/document-new.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/document-save.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/document-save.png new file mode 100644 index 0000000..abdb0c0 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/document-save.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/down.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/down.png new file mode 100644 index 0000000..0d29387 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/down.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/media-playback-start.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/media-playback-start.png new file mode 100644 index 0000000..810de6d Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/media-playback-start.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/media-playback-stop.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/media-playback-stop.png new file mode 100644 index 0000000..66b0e93 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/media-playback-stop.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/remove.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/remove.png new file mode 100644 index 0000000..6454032 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/remove.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/up.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/up.png new file mode 100644 index 0000000..c24a439 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/up.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/view-refresh.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/view-refresh.png new file mode 100644 index 0000000..664520c Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/actions/view-refresh.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/mimetypes/text-x-generic.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/mimetypes/text-x-generic.png new file mode 100644 index 0000000..f43b5f0 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/mimetypes/text-x-generic.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/places/folder.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/places/folder.png new file mode 100644 index 0000000..c129714 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/places/folder.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/places/user-trash.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/places/user-trash.png new file mode 100644 index 0000000..9bf1fc5 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/places/user-trash.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/status/info.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/status/info.png new file mode 100644 index 0000000..0feb2b0 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/24x24/status/info.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/add.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/add.png new file mode 100644 index 0000000..2acdd8f Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/add.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/document-new.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/document-new.png new file mode 100644 index 0000000..e6d64bb Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/document-new.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/document-save.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/document-save.png new file mode 100644 index 0000000..db5c52b Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/document-save.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/down.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/down.png new file mode 100644 index 0000000..dce3f15 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/down.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/media-playback-start.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/media-playback-start.png new file mode 100644 index 0000000..66f32d8 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/media-playback-start.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/media-playback-stop.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/media-playback-stop.png new file mode 100644 index 0000000..a094787 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/media-playback-stop.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/remove.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/remove.png new file mode 100644 index 0000000..c5524f7 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/remove.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/up.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/up.png new file mode 100644 index 0000000..afb307b Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/up.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/view-refresh.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/view-refresh.png new file mode 100644 index 0000000..606ea9e Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/actions/view-refresh.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/mimetypes/text-x-generic.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/mimetypes/text-x-generic.png new file mode 100644 index 0000000..928a679 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/mimetypes/text-x-generic.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/places/folder.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/places/folder.png new file mode 100644 index 0000000..472484f Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/places/folder.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/places/user-trash.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/places/user-trash.png new file mode 100644 index 0000000..9b7a462 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/places/user-trash.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/status/info.png b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/status/info.png new file mode 100644 index 0000000..2ac5747 Binary files /dev/null and b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/icons/Tango/32x32/status/info.png differ diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/log4j.properties b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/log4j.properties new file mode 100644 index 0000000..db05af4 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/src/main/resources/log4j.properties @@ -0,0 +1,9 @@ +# ***** Set root logger level to INFO and it appender to stdout. +log4j.rootLogger=INFO,stdout + +# ***** stdout is set to be a ConsoleAppender. +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +# ***** stdout uses PatternLayout. +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +# ***** Pattern to output the caller's file name and line number. +log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n \ No newline at end of file diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector-dev.sh b/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector-dev.sh new file mode 100644 index 0000000..9be0b0a --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector-dev.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +java -cp ../../build/contrib/ZooInspector/*:../../../build/*:../../../build/lib/*:lib org.apache.zookeeper.inspector.ZooInspector diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector.cmd b/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector.cmd new file mode 100644 index 0000000..67c889c --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector.cmd @@ -0,0 +1,21 @@ +@echo off +@rem Licensed to the Apache Software Foundation (ASF) under one or more +@rem contributor license agreements. See the NOTICE file distributed with +@rem this work for additional information regarding copyright ownership. +@rem The ASF licenses this file to You under the Apache License, Version 2.0 +@rem (the "License"); you may not use this file except in compliance with +@rem the License. You may obtain a copy of the License at +@rem +@rem http://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. + +set CLASSPATH=lib\*;lib +for /F %%f in ('dir /b "%~dp0%\*.jar" 2^>nul') do ( + set CLASSPATH=%%f;%CLASSPATH% +) +java -cp "%CLASSPATH%" org.apache.zookeeper.inspector.ZooInspector diff --git a/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector.sh b/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector.sh new file mode 100644 index 0000000..5e3ebb6 --- /dev/null +++ b/zookeeper-contrib/zookeeper-contrib-zooinspector/zooInspector.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +java -cp "zookeeper-dev-ZooInspector.jar:lib/*:lib" org.apache.zookeeper.inspector.ZooInspector diff --git a/zookeeper-docs/pom.xml b/zookeeper-docs/pom.xml new file mode 100644 index 0000000..5b41dba --- /dev/null +++ b/zookeeper-docs/pom.xml @@ -0,0 +1,70 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + .. + + + zookeeper-docs + Apache ZooKeeper - Documentation + Documentation + + + + + com.ruleoftech + markdown-page-generator-plugin + 2.1.0 + + + process-sources + + generate + + + + + ${project.basedir}/src/main/resources/markdown/html/header.html + ${project.basedir}/src/main/resources/markdown/html/footer.html + images,skin + + + + maven-deploy-plugin + + + true + + + + org.apache.maven.plugins + maven-javadoc-plugin + + true + + + + + diff --git a/zookeeper-docs/src/main/resources/markdown/html/footer.html b/zookeeper-docs/src/main/resources/markdown/html/footer.html new file mode 100644 index 0000000..71d0c80 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/html/footer.html @@ -0,0 +1,18 @@ + +
       
      + + + + \ No newline at end of file diff --git a/zookeeper-docs/src/main/resources/markdown/html/header.html b/zookeeper-docs/src/main/resources/markdown/html/header.html new file mode 100644 index 0000000..c529f88 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/html/header.html @@ -0,0 +1,128 @@ + + + + + + ZooKeeper: Because Coordinating Distributed Systems is a Zoo + + + + + + + + + + +
      + +
      + + + +
      +
      +
      +
      +
      + +
      +
      +   +
      + +
      diff --git a/zookeeper-docs/src/main/resources/markdown/images/2pc.jpg b/zookeeper-docs/src/main/resources/markdown/images/2pc.jpg new file mode 100644 index 0000000..fe4488f Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/2pc.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/bk-overview.jpg b/zookeeper-docs/src/main/resources/markdown/images/bk-overview.jpg new file mode 100644 index 0000000..6e12fb4 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/bk-overview.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/favicon.ico b/zookeeper-docs/src/main/resources/markdown/images/favicon.ico new file mode 100644 index 0000000..161bcf7 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/favicon.ico differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/state_dia.dia b/zookeeper-docs/src/main/resources/markdown/images/state_dia.dia new file mode 100644 index 0000000..4a58a00 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/state_dia.dia differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/state_dia.jpg b/zookeeper-docs/src/main/resources/markdown/images/state_dia.jpg new file mode 100644 index 0000000..b6f4a8b Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/state_dia.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkAuditLogs.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkAuditLogs.jpg new file mode 100644 index 0000000..fd6c376 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkAuditLogs.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkarch.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkarch.jpg new file mode 100644 index 0000000..a0e5fcc Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkarch.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkcomponents.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkcomponents.jpg new file mode 100644 index 0000000..7690578 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkcomponents.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zknamespace.jpg b/zookeeper-docs/src/main/resources/markdown/images/zknamespace.jpg new file mode 100644 index 0000000..05534bc Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zknamespace.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkperfRW-3.2.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkperfRW-3.2.jpg new file mode 100644 index 0000000..594b50b Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkperfRW-3.2.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkperfRW.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkperfRW.jpg new file mode 100644 index 0000000..ad3019f Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkperfRW.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkperfreliability.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkperfreliability.jpg new file mode 100644 index 0000000..232bba8 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkperfreliability.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zkservice.jpg b/zookeeper-docs/src/main/resources/markdown/images/zkservice.jpg new file mode 100644 index 0000000..1ec9154 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zkservice.jpg differ diff --git a/zookeeper-docs/src/main/resources/markdown/images/zookeeper_small.gif b/zookeeper-docs/src/main/resources/markdown/images/zookeeper_small.gif new file mode 100644 index 0000000..4e8014f Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/images/zookeeper_small.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/index.md b/zookeeper-docs/src/main/resources/markdown/index.md new file mode 100644 index 0000000..443ba3a --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/index.md @@ -0,0 +1,63 @@ + + +## ZooKeeper: Because Coordinating Distributed Systems is a Zoo + +ZooKeeper is a high-performance coordination service for +distributed applications. It exposes common services - such as +naming, configuration management, synchronization, and group +services - in a simple interface so you don't have to write them +from scratch. You can use it off-the-shelf to implement +consensus, group management, leader election, and presence +protocols. And you can build on it for your own, specific needs. + +The following documents describe concepts and procedures to get +you started using ZooKeeper. If you have more questions, please +ask the [mailing list](http://zookeeper.apache.org/mailing_lists.html) or browse the +archives. + ++ **ZooKeeper Overview** + Technical Overview Documents for Client Developers, Administrators, and Contributors + + [Overview](zookeeperOver.html) - a bird's eye view of ZooKeeper, including design concepts and architecture + + [Getting Started](zookeeperStarted.html) - a tutorial-style guide for developers to install, run, and program to ZooKeeper + + [Release Notes](releasenotes.html) - new developer and user facing features, improvements, and incompatibilities ++ **Developers** + Documents for Developers using the ZooKeeper Client API + + [API Docs](apidocs/zookeeper-server/index.html) - the technical reference to ZooKeeper Client APIs + + [Programmer's Guide](zookeeperProgrammers.html) - a client application developer's guide to ZooKeeper + + [ZooKeeper Use Cases](zookeeperUseCases.html) - a series of use cases using the ZooKeeper. + + [ZooKeeper Java Example](javaExample.html) - a simple Zookeeper client application, written in Java + + [Barrier and Queue Tutorial](zookeeperTutorial.html) - sample implementations of barriers and queues + + [ZooKeeper Recipes](recipes.html) - higher level solutions to common problems in distributed applications ++ **Administrators & Operators** + Documents for Administrators and Operations Engineers of ZooKeeper Deployments + + [Administrator's Guide](zookeeperAdmin.html) - a guide for system administrators and anyone else who might deploy ZooKeeper + + [Quota Guide](zookeeperQuotas.html) - a guide for system administrators on Quotas in ZooKeeper. + + [JMX](zookeeperJMX.html) - how to enable JMX in ZooKeeper + + [Hierarchical quorums](zookeeperHierarchicalQuorums.html) + + [Observers](zookeeperObservers.html) - non-voting ensemble members that easily improve ZooKeeper's scalability + + [Dynamic Reconfiguration](zookeeperReconfig.html) - a guide on how to use dynamic reconfiguration in ZooKeeper + + [ZooKeeper CLI](zookeeperCLI.html) - a guide on how to use the ZooKeeper command line interface + + [ZooKeeper Tools](zookeeperTools.html) - a guide on how to use a series of tools for ZooKeeper + + [ZooKeeper Monitor](zookeeperMonitor.html) - a guide on how to monitor the ZooKeeper + + [Audit Logging](zookeeperAuditLogs.html) - a guide on how to configure audit logs in ZooKeeper Server and what contents are logged. ++ **Contributors** + Documents for Developers Contributing to the ZooKeeper Open Source Project + + [ZooKeeper Internals](zookeeperInternals.html) - assorted topics on the inner workings of ZooKeeper ++ **Miscellaneous ZooKeeper Documentation** + + [Wiki](https://cwiki.apache.org/confluence/display/ZOOKEEPER) + + [FAQ](https://cwiki.apache.org/confluence/display/ZOOKEEPER/FAQ) + diff --git a/zookeeper-docs/src/main/resources/markdown/javaExample.md b/zookeeper-docs/src/main/resources/markdown/javaExample.md new file mode 100644 index 0000000..a94b083 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/javaExample.md @@ -0,0 +1,628 @@ + + +# ZooKeeper Java Example + +* [A Simple Watch Client](#ch_Introduction) + * [Requirements](#sc_requirements) + * [Program Design](#sc_design) +* [The Executor Class](#sc_executor) +* [The DataMonitor Class](#sc_DataMonitor) +* [Complete Source Listings](#sc_completeSourceCode) + + + +## A Simple Watch Client + +To introduce you to the ZooKeeper Java API, we develop here a very simple +watch client. This ZooKeeper client watches a znode for changes +and responds to by starting or stopping a program. + + + +### Requirements + +The client has four requirements: + +* It takes as parameters: + * the address of the ZooKeeper service + * the name of a znode - the one to be watched + * the name of a file to write the output to + * an executable with arguments. +* It fetches the data associated with the znode and starts the executable. +* If the znode changes, the client re-fetches the contents and restarts the executable. +* If the znode disappears, the client kills the executable. + + + +### Program Design + +Conventionally, ZooKeeper applications are broken into two units, one which maintains the connection, +and the other which monitors data. In this application, the class called the **Executor** +maintains the ZooKeeper connection, and the class called the **DataMonitor** monitors the data +in the ZooKeeper tree. Also, Executor contains the main thread and contains the execution logic. +It is responsible for what little user interaction there is, as well as interaction with the executable program you +pass in as an argument and which the sample (per the requirements) shuts down and restarts, according to the +state of the znode. + + + +## The Executor Class + +The Executor object is the primary container of the sample application. It contains +both the **ZooKeeper** object, **DataMonitor**, as described above in +[Program Design](#sc_design). + + + // from the Executor class... + + public static void main(String[] args) { + if (args.length < 4) { + System.err + .println("USAGE: Executor hostPort znode filename program [args ...]"); + System.exit(2); + } + String hostPort = args[0]; + String znode = args[1]; + String filename = args[2]; + String exec[] = new String[args.length - 3]; + System.arraycopy(args, 3, exec, 0, exec.length); + try { + new Executor(hostPort, znode, filename, exec).run(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public Executor(String hostPort, String znode, String filename, + String exec[]) throws KeeperException, IOException { + this.filename = filename; + this.exec = exec; + zk = new ZooKeeper(hostPort, 3000, this); + dm = new DataMonitor(zk, znode, null, this); + } + + public void run() { + try { + synchronized (this) { + while (!dm.dead) { + wait(); + } + } + } catch (InterruptedException e) { + } + } + + +Recall that the Executor's job is to start and stop the executable whose name you pass in on the command line. +It does this in response to events fired by the ZooKeeper object. As you can see in the code above, the Executor passes +a reference to itself as the Watcher argument in the ZooKeeper constructor. It also passes a reference to itself +as DataMonitorListener argument to the DataMonitor constructor. Per the Executor's definition, it implements both these +interfaces: + + public class Executor implements Watcher, Runnable, DataMonitor.DataMonitorListener { + ... + + +The **Watcher** interface is defined by the ZooKeeper Java API. +ZooKeeper uses it to communicate back to its container. It supports only one method, `process()`, +and ZooKeeper uses it to communicates generic events that the main thread would be interested in, +such as the state of the ZooKeeper connection or the ZooKeeper session. The Executor in this example simply +forwards those events down to the DataMonitor to decide what to do with them. It does this simply to illustrate +the point that, by convention, the Executor or some Executor-like object "owns" the ZooKeeper connection, but it is +free to delegate the events to other events to other objects. It also uses this as the default channel on which +to fire watch events. (More on this later.) + + + public void process(WatchedEvent event) { + dm.process(event); + } + + +The **DataMonitorListener** +interface, on the other hand, is not part of the ZooKeeper API. It is a completely custom interface, +designed for this sample application. The DataMonitor object uses it to communicate back to its container, which +is also the Executor object. The DataMonitorListener interface looks like this: + + + public interface DataMonitorListener { + /** + * The existence status of the node has changed. + */ + void exists(byte data[]); + + /** + * The ZooKeeper session is no longer valid. + * + * @param rc + * the ZooKeeper reason code + */ + void closing(int rc); + } + + +This interface is defined in the DataMonitor class and implemented in the Executor class. +When `Executor.exists()` is invoked, the Executor decides whether to start up or shut down per the requirements. +Recall that the requires say to kill the executable when the znode ceases to _exist_. + +When `Executor.closing()` is invoked, the Executor decides whether or not to shut itself down +in response to the ZooKeeper connection permanently disappearing. + +As you might have guessed, DataMonitor is the object that invokes +these methods, in response to changes in ZooKeeper's state. + +Here are Executor's implementation of +`DataMonitorListener.exists()` and `DataMonitorListener.closing`: + + + public void exists( byte[] data ) { + if (data == null) { + if (child != null) { + System.out.println("Killing process"); + child.destroy(); + try { + child.waitFor(); + } catch (InterruptedException e) { + } + } + child = null; + } else { + if (child != null) { + System.out.println("Stopping child"); + child.destroy(); + try { + child.waitFor(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + try { + FileOutputStream fos = new FileOutputStream(filename); + fos.write(data); + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + System.out.println("Starting child"); + child = Runtime.getRuntime().exec(exec); + new StreamWriter(child.getInputStream(), System.out); + new StreamWriter(child.getErrorStream(), System.err); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public void closing(int rc) { + synchronized (this) { + notifyAll(); + } + } + + + + +## The DataMonitor Class + +The DataMonitor class has the meat of the ZooKeeper logic. It is mostly +asynchronous and event driven. DataMonitor kicks things off in the constructor with: + + + public DataMonitor(ZooKeeper zk, String znode, Watcher chainedWatcher, + DataMonitorListener listener) { + this.zk = zk; + this.znode = znode; + this.chainedWatcher = chainedWatcher; + this.listener = listener; + + // Get things started by checking if the node exists. We are going + // to be completely event driven + + +The call to `ZooKeeper.exists()` checks for the existence of the znode, +sets a watch, and passes a reference to itself (`this`) +as the completion callback object. In this sense, it kicks things off, since the +real processing happens when the watch is triggered. + +###### Note + +>Don't confuse the completion callback with the watch callback. The `ZooKeeper.exists()` +completion callback, which happens to be the method `StatCallback.processResult()` implemented +in the DataMonitor object, is invoked when the asynchronous _setting of the watch_ operation +(by `ZooKeeper.exists()`) completes on the server. + +>The triggering of the watch, on the other hand, sends an event to the _Executor_ object, since +the Executor registered as the Watcher of the ZooKeeper object. + +>As an aside, you might note that the DataMonitor could also register itself as the Watcher +for this particular watch event. This is new to ZooKeeper 3.0.0 (the support of multiple Watchers). In this +example, however, DataMonitor does not register as the Watcher. + +When the `ZooKeeper.exists()` operation completes on the server, the ZooKeeper API invokes this completion callback on +the client: + + + public void processResult(int rc, String path, Object ctx, Stat stat) { + boolean exists; + switch (rc) { + case Code.Ok: + exists = true; + break; + case Code.NoNode: + exists = false; + break; + case Code.SessionExpired: + case Code.NoAuth: + dead = true; + listener.closing(rc); + return; + default: + // Retry errors + zk.exists(znode, true, this, null); + return; + } + + byte b[] = null; + if (exists) { + try { + b = zk.getData(znode, false, null); + } catch (KeeperException e) { + // We don't need to worry about recovering now. The watch + // callbacks will kick off any exception handling + e.printStackTrace(); + } catch (InterruptedException e) { + return; + } + } + if ((b == null && b != prevData) + || (b != null && !Arrays.equals(prevData, b))) { + listener.exists(b); + prevData = b; + } + } + + +The code first checks the error codes for znode existence, fatal errors, and +recoverable errors. If the file (or znode) exists, it gets the data from the znode, and +then invoke the exists() callback of Executor if the state has changed. Note, +it doesn't have to do any Exception processing for the getData call because it +has watches pending for anything that could cause an error: if the node is deleted +before it calls `ZooKeeper.getData()`, the watch event set by +the `ZooKeeper.exists()` triggers a callback; +if there is a communication error, a connection watch event fires when +the connection comes back up. + +Finally, notice how DataMonitor processes watch events: + + + public void process(WatchedEvent event) { + String path = event.getPath(); + if (event.getType() == Event.EventType.None) { + // We are are being told that the state of the + // connection has changed + switch (event.getState()) { + case SyncConnected: + // In this particular example we don't need to do anything + // here - watches are automatically re-registered with + // server and any watches triggered while the client was + // disconnected will be delivered (in order of course) + break; + case Expired: + // It's all over + dead = true; + listener.closing(KeeperException.Code.SessionExpired); + break; + } + } else { + if (path != null && path.equals(znode)) { + // Something has changed on the node, let's find out + zk.exists(znode, true, this, null); + } + } + if (chainedWatcher != null) { + chainedWatcher.process(event); + } + } + + +If the client-side ZooKeeper libraries can re-establish the +communication channel (SyncConnected event) to ZooKeeper before +session expiration (Expired event) all of the session's watches will +automatically be re-established with the server (auto-reset of watches +is new in ZooKeeper 3.0.0). See [ZooKeeper Watches](zookeeperProgrammers.html#ch_zkWatches) +in the programmer guide for more on this. A bit lower down in this +function, when DataMonitor gets an event for a znode, it calls`ZooKeeper.exists()` to find out what has changed. + + + +## Complete Source Listings + +### Executor.java + + + /** + * A simple example program to use DataMonitor to start and + * stop executables based on a znode. The program watches the + * specified znode and saves the data that corresponds to the + * znode in the filesystem. It also starts the specified program + * with the specified arguments when the znode exists and kills + * the program if the znode goes away. + */ + import java.io.FileOutputStream; + import java.io.IOException; + import java.io.InputStream; + import java.io.OutputStream; + + import org.apache.zookeeper.KeeperException; + import org.apache.zookeeper.WatchedEvent; + import org.apache.zookeeper.Watcher; + import org.apache.zookeeper.ZooKeeper; + + public class Executor + implements Watcher, Runnable, DataMonitor.DataMonitorListener + { + String znode; + DataMonitor dm; + ZooKeeper zk; + String filename; + String exec[]; + Process child; + + public Executor(String hostPort, String znode, String filename, + String exec[]) throws KeeperException, IOException { + this.filename = filename; + this.exec = exec; + zk = new ZooKeeper(hostPort, 3000, this); + dm = new DataMonitor(zk, znode, null, this); + } + + /** + * @param args + */ + public static void main(String[] args) { + if (args.length < 4) { + System.err + .println("USAGE: Executor hostPort znode filename program [args ...]"); + System.exit(2); + } + String hostPort = args[0]; + String znode = args[1]; + String filename = args[2]; + String exec[] = new String[args.length - 3]; + System.arraycopy(args, 3, exec, 0, exec.length); + try { + new Executor(hostPort, znode, filename, exec).run(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /*************************************************************************** + * We do process any events ourselves, we just need to forward them on. + * + * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.proto.WatcherEvent) + */ + public void process(WatchedEvent event) { + dm.process(event); + } + + public void run() { + try { + synchronized (this) { + while (!dm.dead) { + wait(); + } + } + } catch (InterruptedException e) { + } + } + + public void closing(int rc) { + synchronized (this) { + notifyAll(); + } + } + + static class StreamWriter extends Thread { + OutputStream os; + + InputStream is; + + StreamWriter(InputStream is, OutputStream os) { + this.is = is; + this.os = os; + start(); + } + + public void run() { + byte b[] = new byte[80]; + int rc; + try { + while ((rc = is.read(b)) > 0) { + os.write(b, 0, rc); + } + } catch (IOException e) { + } + + } + } + + public void exists(byte[] data) { + if (data == null) { + if (child != null) { + System.out.println("Killing process"); + child.destroy(); + try { + child.waitFor(); + } catch (InterruptedException e) { + } + } + child = null; + } else { + if (child != null) { + System.out.println("Stopping child"); + child.destroy(); + try { + child.waitFor(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + try { + FileOutputStream fos = new FileOutputStream(filename); + fos.write(data); + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + System.out.println("Starting child"); + child = Runtime.getRuntime().exec(exec); + new StreamWriter(child.getInputStream(), System.out); + new StreamWriter(child.getErrorStream(), System.err); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + +### DataMonitor.java + + + /** + * A simple class that monitors the data and existence of a ZooKeeper + * node. It uses asynchronous ZooKeeper APIs. + */ + import java.util.Arrays; + + import org.apache.zookeeper.KeeperException; + import org.apache.zookeeper.WatchedEvent; + import org.apache.zookeeper.Watcher; + import org.apache.zookeeper.ZooKeeper; + import org.apache.zookeeper.AsyncCallback.StatCallback; + import org.apache.zookeeper.KeeperException.Code; + import org.apache.zookeeper.data.Stat; + + public class DataMonitor implements Watcher, StatCallback { + + ZooKeeper zk; + String znode; + Watcher chainedWatcher; + boolean dead; + DataMonitorListener listener; + byte prevData[]; + + public DataMonitor(ZooKeeper zk, String znode, Watcher chainedWatcher, + DataMonitorListener listener) { + this.zk = zk; + this.znode = znode; + this.chainedWatcher = chainedWatcher; + this.listener = listener; + // Get things started by checking if the node exists. We are going + // to be completely event driven + zk.exists(znode, true, this, null); + } + + /** + * Other classes use the DataMonitor by implementing this method + */ + public interface DataMonitorListener { + /** + * The existence status of the node has changed. + */ + void exists(byte data[]); + + /** + * The ZooKeeper session is no longer valid. + * + * @param rc + * the ZooKeeper reason code + */ + void closing(int rc); + } + + public void process(WatchedEvent event) { + String path = event.getPath(); + if (event.getType() == Event.EventType.None) { + // We are are being told that the state of the + // connection has changed + switch (event.getState()) { + case SyncConnected: + // In this particular example we don't need to do anything + // here - watches are automatically re-registered with + // server and any watches triggered while the client was + // disconnected will be delivered (in order of course) + break; + case Expired: + // It's all over + dead = true; + listener.closing(KeeperException.Code.SessionExpired); + break; + } + } else { + if (path != null && path.equals(znode)) { + // Something has changed on the node, let's find out + zk.exists(znode, true, this, null); + } + } + if (chainedWatcher != null) { + chainedWatcher.process(event); + } + } + + public void processResult(int rc, String path, Object ctx, Stat stat) { + boolean exists; + switch (rc) { + case Code.Ok: + exists = true; + break; + case Code.NoNode: + exists = false; + break; + case Code.SessionExpired: + case Code.NoAuth: + dead = true; + listener.closing(rc); + return; + default: + // Retry errors + zk.exists(znode, true, this, null); + return; + } + + byte b[] = null; + if (exists) { + try { + b = zk.getData(znode, false, null); + } catch (KeeperException e) { + // We don't need to worry about recovering now. The watch + // callbacks will kick off any exception handling + e.printStackTrace(); + } catch (InterruptedException e) { + return; + } + } + if ((b == null && b != prevData) + || (b != null && !Arrays.equals(prevData, b))) { + listener.exists(b); + prevData = b; + } + } + } + diff --git a/zookeeper-docs/src/main/resources/markdown/recipes.md b/zookeeper-docs/src/main/resources/markdown/recipes.md new file mode 100644 index 0000000..ceb2e71 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/recipes.md @@ -0,0 +1,416 @@ + + +# ZooKeeper Recipes and Solutions + +* [A Guide to Creating Higher-level Constructs with ZooKeeper](#ch_recipes) + * [Important Note About Error Handling](#sc_recipes_errorHandlingNote) + * [Out of the Box Applications: Name Service, Configuration, Group Membership](#sc_outOfTheBox) + * [Barriers](#sc_recipes_eventHandles) + * [Double Barriers](#sc_doubleBarriers) + * [Queues](#sc_recipes_Queues) + * [Priority Queues](#sc_recipes_priorityQueues) + * [Locks](#sc_recipes_Locks) + * [Recoverable Errors and the GUID](#sc_recipes_GuidNote) + * [Shared Locks](#Shared+Locks) + * [Revocable Shared Locks](#sc_revocableSharedLocks) + * [Two-phased Commit](#sc_recipes_twoPhasedCommit) + * [Leader Election](#sc_leaderElection) + + + +## A Guide to Creating Higher-level Constructs with ZooKeeper + +In this article, you'll find guidelines for using +ZooKeeper to implement higher order functions. All of them are conventions +implemented at the client and do not require special support from +ZooKeeper. Hopefully the community will capture these conventions in client-side libraries +to ease their use and to encourage standardization. + +One of the most interesting things about ZooKeeper is that even +though ZooKeeper uses _asynchronous_ notifications, you +can use it to build _synchronous_ consistency +primitives, such as queues and locks. As you will see, this is possible +because ZooKeeper imposes an overall order on updates, and has mechanisms +to expose this ordering. + +Note that the recipes below attempt to employ best practices. In +particular, they avoid polling, timers or anything else that would result +in a "herd effect", causing bursts of traffic and limiting +scalability. + +There are many useful functions that can be imagined that aren't +included here - revocable read-write priority locks, as just one example. +And some of the constructs mentioned here - locks, in particular - +illustrate certain points, even though you may find other constructs, such +as event handles or queues, a more practical means of performing the same +function. In general, the examples in this section are designed to +stimulate thought. + + + +### Important Note About Error Handling + +When implementing the recipes you must handle recoverable exceptions +(see the [FAQ](https://cwiki.apache.org/confluence/display/ZOOKEEPER/FAQ)). In +particular, several of the recipes employ sequential ephemeral +nodes. When creating a sequential ephemeral node there is an error case in +which the create() succeeds on the server but the server crashes before +returning the name of the node to the client. When the client reconnects its +session is still valid and, thus, the node is not removed. The implication is +that it is difficult for the client to know if its node was created or not. The +recipes below include measures to handle this. + + + +### Out of the Box Applications: Name Service, Configuration, Group Membership + +Name service and configuration are two of the primary applications +of ZooKeeper. These two functions are provided directly by the ZooKeeper +API. + +Another function directly provided by ZooKeeper is _group +membership_. The group is represented by a node. Members of the +group create ephemeral nodes under the group node. Nodes of the members +that fail abnormally will be removed automatically when ZooKeeper detects +the failure. + + + +### Barriers + +Distributed systems use _barriers_ +to block processing of a set of nodes until a condition is met +at which time all the nodes are allowed to proceed. Barriers are +implemented in ZooKeeper by designating a barrier node. The +barrier is in place if the barrier node exists. Here's the +pseudo code: + +1. Client calls the ZooKeeper API's **exists()** function on the barrier node, with + _watch_ set to true. +1. If **exists()** returns false, the + barrier is gone and the client proceeds +1. Else, if **exists()** returns true, + the clients wait for a watch event from ZooKeeper for the barrier + node. +1. When the watch event is triggered, the client reissues the + **exists( )** call, again waiting until + the barrier node is removed. + + + +#### Double Barriers + +Double barriers enable clients to synchronize the beginning and +the end of a computation. When enough processes have joined the barrier, +processes start their computation and leave the barrier once they have +finished. This recipe shows how to use a ZooKeeper node as a +barrier. + +The pseudo code in this recipe represents the barrier node as +_b_. Every client process _p_ +registers with the barrier node on entry and unregisters when it is +ready to leave. A node registers with the barrier node via the **Enter** procedure below, it waits until +_x_ client process register before proceeding with +the computation. (The _x_ here is up to you to +determine for your system.) + +| **Enter** | **Leave** | +|-----------------------------------|-------------------------------| +| 1. Create a name __n_ = _b_+“/”+_p__ | 1. **L = getChildren(b, false)** | +| 2. Set watch: **exists(_b_ + ‘‘/ready’’, true)** | 2. if no children, exit | +| 3. Create child: **create(_n_, EPHEMERAL)** | 3. if _p_ is only process node in L, delete(n) and exit | +| 4. **L = getChildren(b, false)** | 4. if _p_ is the lowest process node in L, wait on highest process node in L | +| 5. if fewer children in L than_x_, wait for watch event | 5. else **delete(_n_)**if still exists and wait on lowest process node in L | +| 6. else **create(b + ‘‘/ready’’, REGULAR)** | 6. goto 1 | + +On entering, all processes watch on a ready node and +create an ephemeral node as a child of the barrier node. Each process +but the last enters the barrier and waits for the ready node to appear +at line 5. The process that creates the xth node, the last process, will +see x nodes in the list of children and create the ready node, waking up +the other processes. Note that waiting processes wake up only when it is +time to exit, so waiting is efficient. + +On exit, you can't use a flag such as _ready_ +because you are watching for process nodes to go away. By using +ephemeral nodes, processes that fail after the barrier has been entered +do not prevent correct processes from finishing. When processes are +ready to leave, they need to delete their process nodes and wait for all +other processes to do the same. + +Processes exit when there are no process nodes left as children of +_b_. However, as an efficiency, you can use the +lowest process node as the ready flag. All other processes that are +ready to exit watch for the lowest existing process node to go away, and +the owner of the lowest process watches for any other process node +(picking the highest for simplicity) to go away. This means that only a +single process wakes up on each node deletion except for the last node, +which wakes up everyone when it is removed. + + + +### Queues + +Distributed queues are a common data structure. To implement a +distributed queue in ZooKeeper, first designate a znode to hold the queue, +the queue node. The distributed clients put something into the queue by +calling create() with a pathname ending in "queue-", with the +_sequence_ and _ephemeral_ flags in +the create() call set to true. Because the _sequence_ +flag is set, the new pathnames will have the form +_path-to-queue-node_/queue-X, where X is a monotonic increasing number. A +client that wants to be removed from the queue calls ZooKeeper's **getChildren( )** function, with +_watch_ set to true on the queue node, and begins +processing nodes with the lowest number. The client does not need to issue +another **getChildren( )** until it exhausts +the list obtained from the first **getChildren( +)** call. If there are are no children in the queue node, the +reader waits for a watch notification to check the queue again. + +###### Note +>There now exists a Queue implementation in ZooKeeper +recipes directory. This is distributed with the release -- +zookeeper-recipes/zookeeper-recipes-queue directory of the release artifact. + + + +#### Priority Queues + +To implement a priority queue, you need only make two simple +changes to the generic [queue +recipe](#sc_recipes_Queues) . First, to add to a queue, the pathname ends with +"queue-YY" where YY is the priority of the element with lower numbers +representing higher priority (just like UNIX). Second, when removing +from the queue, a client uses an up-to-date children list meaning that +the client will invalidate previously obtained children lists if a watch +notification triggers for the queue node. + + + +### Locks + +Fully distributed locks that are globally synchronous, meaning at +any snapshot in time no two clients think they hold the same lock. These +can be implemented using ZooKeeeper. As with priority queues, first define +a lock node. + +###### Note +>There now exists a Lock implementation in ZooKeeper +recipes directory. This is distributed with the release -- +zookeeper-recipes/zookeeper-recipes-lock directory of the release artifact. + +Clients wishing to obtain a lock do the following: + +1. Call **create( )** with a pathname + of "_locknode_/guid-lock-" and the _sequence_ and + _ephemeral_ flags set. The _guid_ + is needed in case the create() result is missed. See the note below. +1. Call **getChildren( )** on the lock + node _without_ setting the watch flag (this is + important to avoid the herd effect). +1. If the pathname created in step **1** has the lowest sequence number suffix, the + client has the lock and the client exits the protocol. +1. The client calls **exists( )** with + the watch flag set on the path in the lock directory with the next + lowest sequence number. +1. if **exists( )** returns null, go + to step **2**. Otherwise, wait for a + notification for the pathname from the previous step before going to + step **2**. + +The unlock protocol is very simple: clients wishing to release a +lock simply delete the node they created in step 1. + +Here are a few things to notice: + +* The removal of a node will only cause one client to wake up + since each node is watched by exactly one client. In this way, you + avoid the herd effect. + +* There is no polling or timeouts. + +* Because of the way you implement locking, it is easy to see the + amount of lock contention, break locks, debug locking problems, + etc. + + + +#### Recoverable Errors and the GUID + +* If a recoverable error occurs calling **create()** the + client should call **getChildren()** and check for a node + containing the _guid_ used in the path name. + This handles the case (noted [above](#sc_recipes_errorHandlingNote)) of + the create() succeeding on the server but the server crashing before returning the name + of the new node. + + + +#### Shared Locks + +You can implement shared locks by with a few changes to the lock +protocol: + +| **Obtaining a read lock:** | **Obtaining a write lock:** | +|----------------------------|-----------------------------| +| 1. Call **create( )** to create a node with pathname "*guid-/read-*". This is the lock node use later in the protocol. Make sure to set both the _sequence_ and _ephemeral_ flags. | 1. Call **create( )** to create a node with pathname "*guid-/write-*". This is the lock node spoken of later in the protocol. Make sure to set both _sequence_ and _ephemeral_ flags. | +| 2. Call **getChildren( )** on the lock node _without_ setting the _watch_ flag - this is important, as it avoids the herd effect. | 2. Call **getChildren( )** on the lock node _without_ setting the _watch_ flag - this is important, as it avoids the herd effect. | +| 3. If there are no children with a pathname starting with "*write-*" and having a lower sequence number than the node created in step **1**, the client has the lock and can exit the protocol. | 3. If there are no children with a lower sequence number than the node created in step **1**, the client has the lock and the client exits the protocol. | +| 4. Otherwise, call **exists( )**, with _watch_ flag, set on the node in lock directory with pathname starting with "*write-*" having the next lowest sequence number. | 4. Call **exists( ),** with _watch_ flag set, on the node with the pathname that has the next lowest sequence number. | +| 5. If **exists( )** returns _false_, goto step **2**. | 5. If **exists( )** returns _false_, goto step **2**. Otherwise, wait for a notification for the pathname from the previous step before going to step **2**. | +| 6. Otherwise, wait for a notification for the pathname from the previous step before going to step **2** | | + +Notes: + +* It might appear that this recipe creates a herd effect: + when there is a large group of clients waiting for a read + lock, and all getting notified more or less simultaneously + when the "*write-*" node with the lowest + sequence number is deleted. In fact. that's valid behavior: + as all those waiting reader clients should be released since + they have the lock. The herd effect refers to releasing a + "herd" when in fact only a single or a small number of + machines can proceed. + +* See the [note for Locks](#sc_recipes_GuidNote) on how to use the guid in the node. + + + +#### Revocable Shared Locks + +With minor modifications to the Shared Lock protocol, you make +shared locks revocable by modifying the shared lock protocol: + +In step **1**, of both obtain reader +and writer lock protocols, call **getData( +)** with _watch_ set, immediately after the +call to **create( )**. If the client +subsequently receives notification for the node it created in step +**1**, it does another **getData( )** on that node, with +_watch_ set and looks for the string "unlock", which +signals to the client that it must release the lock. This is because, +according to this shared lock protocol, you can request the client with +the lock give up the lock by calling **setData()** on the lock node, writing "unlock" to that node. + +Note that this protocol requires the lock holder to consent to +releasing the lock. Such consent is important, especially if the lock +holder needs to do some processing before releasing the lock. Of course +you can always implement _Revocable Shared Locks with Freaking +Laser Beams_ by stipulating in your protocol that the revoker +is allowed to delete the lock node if after some length of time the lock +isn't deleted by the lock holder. + + + +### Two-phased Commit + +A two-phase commit protocol is an algorithm that lets all clients in +a distributed system agree either to commit a transaction or abort. + +In ZooKeeper, you can implement a two-phased commit by having a +coordinator create a transaction node, say "/app/Tx", and one child node +per participating site, say "/app/Tx/s_i". When coordinator creates the +child node, it leaves the content undefined. Once each site involved in +the transaction receives the transaction from the coordinator, the site +reads each child node and sets a watch. Each site then processes the query +and votes "commit" or "abort" by writing to its respective node. Once the +write completes, the other sites are notified, and as soon as all sites +have all votes, they can decide either "abort" or "commit". Note that a +node can decide "abort" earlier if some site votes for "abort". + +An interesting aspect of this implementation is that the only role +of the coordinator is to decide upon the group of sites, to create the +ZooKeeper nodes, and to propagate the transaction to the corresponding +sites. In fact, even propagating the transaction can be done through +ZooKeeper by writing it in the transaction node. + +There are two important drawbacks of the approach described above. +One is the message complexity, which is O(n²). The second is the +impossibility of detecting failures of sites through ephemeral nodes. To +detect the failure of a site using ephemeral nodes, it is necessary that +the site create the node. + +To solve the first problem, you can have only the coordinator +notified of changes to the transaction nodes, and then notify the sites +once coordinator reaches a decision. Note that this approach is scalable, +but it's is slower too, as it requires all communication to go through the +coordinator. + +To address the second problem, you can have the coordinator +propagate the transaction to the sites, and have each site creating its +own ephemeral node. + + + +### Leader Election + +A simple way of doing leader election with ZooKeeper is to use the +**SEQUENCE|EPHEMERAL** flags when creating +znodes that represent "proposals" of clients. The idea is to have a znode, +say "/election", such that each znode creates a child znode "/election/guid-n_" +with both flags SEQUENCE|EPHEMERAL. With the sequence flag, ZooKeeper +automatically appends a sequence number that is greater than any one +previously appended to a child of "/election". The process that created +the znode with the smallest appended sequence number is the leader. + +That's not all, though. It is important to watch for failures of the +leader, so that a new client arises as the new leader in the case the +current leader fails. A trivial solution is to have all application +processes watching upon the current smallest znode, and checking if they +are the new leader when the smallest znode goes away (note that the +smallest znode will go away if the leader fails because the node is +ephemeral). But this causes a herd effect: upon a failure of the current +leader, all other processes receive a notification, and execute +getChildren on "/election" to obtain the current list of children of +"/election". If the number of clients is large, it causes a spike on the +number of operations that ZooKeeper servers have to process. To avoid the +herd effect, it is sufficient to watch for the next znode down on the +sequence of znodes. If a client receives a notification that the znode it +is watching is gone, then it becomes the new leader in the case that there +is no smaller znode. Note that this avoids the herd effect by not having +all clients watching the same znode. + +Here's the pseudo code: + +Let ELECTION be a path of choice of the application. To volunteer to +be a leader: + +1. Create znode z with path "ELECTION/guid-n_" with both SEQUENCE and + EPHEMERAL flags; +1. Let C be the children of "ELECTION", and i be the sequence + number of z; +1. Watch for changes on "ELECTION/guid-n_j", where j is the largest + sequence number such that j < i and n_j is a znode in C; + +Upon receiving a notification of znode deletion: + +1. Let C be the new set of children of ELECTION; +1. If z is the smallest node in C, then execute leader + procedure; +1. Otherwise, watch for changes on "ELECTION/guid-n_j", where j is the + largest sequence number such that j < i and n_j is a znode in C; + +Notes: + +* Note that the znode having no preceding znode on the list of + children do not imply that the creator of this znode is aware that it is + the current leader. Applications may consider creating a separate znode + to acknowledge that the leader has executed the leader procedure. + +* See the [note for Locks](#sc_recipes_GuidNote) on how to use the guid in the node. + + diff --git a/zookeeper-docs/src/main/resources/markdown/releasenotes.md b/zookeeper-docs/src/main/resources/markdown/releasenotes.md new file mode 100644 index 0000000..8c8ead1 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/releasenotes.md @@ -0,0 +1,87 @@ + + + + +# Release Notes - ZooKeeper - Version 3.6.3 + +## Bug + +* [ZOOKEEPER-2307](https://issues.apache.org/jira/browse/ZOOKEEPER-2307) - ZooKeeper not starting because acceptedEpoch is less than the currentEpoch +* [ZOOKEEPER-3128](https://issues.apache.org/jira/browse/ZOOKEEPER-3128) - Get CLI Command displays Authentication error for Authorization error +* [ZOOKEEPER-3877](https://issues.apache.org/jira/browse/ZOOKEEPER-3877) - JMX Bean RemotePeerBean should enclose IPV6 host in square bracket same as LocalPeerBean +* [ZOOKEEPER-3887](https://issues.apache.org/jira/browse/ZOOKEEPER-3887) - In SSL-only server zkServer.sh status command should use secureClientPortAddress instead of clientPortAddress +* [ZOOKEEPER-3911](https://issues.apache.org/jira/browse/ZOOKEEPER-3911) - Data inconsistency caused by DIFF sync uncommitted log +* [ZOOKEEPER-3931](https://issues.apache.org/jira/browse/ZOOKEEPER-3931) - "zkServer.sh version" returns a trailing dash +* [ZOOKEEPER-3954](https://issues.apache.org/jira/browse/ZOOKEEPER-3954) - use of uninitialized data in zookeeper-client/zookeeper-client-c/src/zookeeper.c:free_auth_completion +* [ZOOKEEPER-3955](https://issues.apache.org/jira/browse/ZOOKEEPER-3955) - added a shebang or a 'shell' directive to lastRevision.sh +* [ZOOKEEPER-3983](https://issues.apache.org/jira/browse/ZOOKEEPER-3983) - C client test suite hangs forever 'sss' is configured in /etc/nsswitch.conf +* [ZOOKEEPER-3991](https://issues.apache.org/jira/browse/ZOOKEEPER-3991) - QuorumCnxManager Listener port bind retry does not retry DNS lookup +* [ZOOKEEPER-3992](https://issues.apache.org/jira/browse/ZOOKEEPER-3992) - addWatch api should check the null watch +* [ZOOKEEPER-4011](https://issues.apache.org/jira/browse/ZOOKEEPER-4011) - Maven build fails on branch-3.6 because of jUnit 5 usage in DIFFSyncConsistencyTest +* [ZOOKEEPER-4045](https://issues.apache.org/jira/browse/ZOOKEEPER-4045) - CVE-2020-25649 - Upgrade jackson databind to 2.10.5.1 +* [ZOOKEEPER-4055](https://issues.apache.org/jira/browse/ZOOKEEPER-4055) - Dockerfile can't build Zookeeper C client library +* [ZOOKEEPER-4194](https://issues.apache.org/jira/browse/ZOOKEEPER-4194) - ZooInspector throws NullPointerExceptions to console when node data is null +* [ZOOKEEPER-4205](https://issues.apache.org/jira/browse/ZOOKEEPER-4205) - Test fails when port 8080 is in use +* [ZOOKEEPER-4207](https://issues.apache.org/jira/browse/ZOOKEEPER-4207) - New CI pipeline checks out master in branch builds too +* [ZOOKEEPER-4220](https://issues.apache.org/jira/browse/ZOOKEEPER-4220) - Potential redundant connection attempts during leader election +* [ZOOKEEPER-4222](https://issues.apache.org/jira/browse/ZOOKEEPER-4222) - Backport ZOOKEEPER-2307 to branch-3.6 +* [ZOOKEEPER-4223](https://issues.apache.org/jira/browse/ZOOKEEPER-4223) - Backport ZOOKEEPER-3706 to branch-3.6 +* [ZOOKEEPER-4224](https://issues.apache.org/jira/browse/ZOOKEEPER-4224) - Backport ZOOKEEPER-3891 to branch-3.6 +* [ZOOKEEPER-4225](https://issues.apache.org/jira/browse/ZOOKEEPER-4225) - Backport ZOOKEEPER-3642 to branch-3.6 +* [ZOOKEEPER-4227](https://issues.apache.org/jira/browse/ZOOKEEPER-4227) - X509AuthFailureTest is failing consistently +* [ZOOKEEPER-4230](https://issues.apache.org/jira/browse/ZOOKEEPER-4230) - Use dynamic temp folder instead of static temp folder in RestMain +* [ZOOKEEPER-4232](https://issues.apache.org/jira/browse/ZOOKEEPER-4232) - InvalidSnapshotTest corrupts its own test data +* [ZOOKEEPER-4260](https://issues.apache.org/jira/browse/ZOOKEEPER-4260) - Backport ZOOKEEPER-3575 to branch-3.6 +* [ZOOKEEPER-4267](https://issues.apache.org/jira/browse/ZOOKEEPER-4267) - Fix check-style issues +* [ZOOKEEPER-4269](https://issues.apache.org/jira/browse/ZOOKEEPER-4269) - acceptedEpoch.tmp rename failure will cause server startup error +* [ZOOKEEPER-4272](https://issues.apache.org/jira/browse/ZOOKEEPER-4272) - Upgrade Netty library to > 4.1.60 due to security vulnerability CVE-2021-21295 +* [ZOOKEEPER-4277](https://issues.apache.org/jira/browse/ZOOKEEPER-4277) - dependency-check:check failing - jetty-server-9.4.38 CVE-2021-28165 +* [ZOOKEEPER-4278](https://issues.apache.org/jira/browse/ZOOKEEPER-4278) - dependency-check:check failing - netty-transport-4.1.60.Final CVE-2021-21409 + +## Improvement + +* [ZOOKEEPER-1871](https://issues.apache.org/jira/browse/ZOOKEEPER-1871) - Add an option to zkCli to wait for connection before executing commands +* [ZOOKEEPER-3671](https://issues.apache.org/jira/browse/ZOOKEEPER-3671) - Use ThreadLocalConcurrent to Replace Random and Math.random +* [ZOOKEEPER-3808](https://issues.apache.org/jira/browse/ZOOKEEPER-3808) - correct the documentation about digest.enabled +* [ZOOKEEPER-3858](https://issues.apache.org/jira/browse/ZOOKEEPER-3858) - Add metrics to track server unavailable time +* [ZOOKEEPER-3935](https://issues.apache.org/jira/browse/ZOOKEEPER-3935) - Handle float metrics in check_zookeeper +* [ZOOKEEPER-3950](https://issues.apache.org/jira/browse/ZOOKEEPER-3950) - Add support for BCFKS key/trust store format +* [ZOOKEEPER-3952](https://issues.apache.org/jira/browse/ZOOKEEPER-3952) - Remove commons-lang from ZooKeeper +* [ZOOKEEPER-3960](https://issues.apache.org/jira/browse/ZOOKEEPER-3960) - Update ZooKeeper client documentation about key file format parameters +* [ZOOKEEPER-3978](https://issues.apache.org/jira/browse/ZOOKEEPER-3978) - Adding additional security metrics to zookeeper +* [ZOOKEEPER-4209](https://issues.apache.org/jira/browse/ZOOKEEPER-4209) - Update Netty version to 4.1.53.Final on 3.5 branch +* [ZOOKEEPER-4231](https://issues.apache.org/jira/browse/ZOOKEEPER-4231) - Add document for snapshot compression config +* [ZOOKEEPER-4259](https://issues.apache.org/jira/browse/ZOOKEEPER-4259) - Allow AdminServer to force https + +## Task + +* [ZOOKEEPER-3957](https://issues.apache.org/jira/browse/ZOOKEEPER-3957) - Create Owasp check build on new Jenkins instance +* [ZOOKEEPER-3980](https://issues.apache.org/jira/browse/ZOOKEEPER-3980) - Fix Jenkinsfiles with new tool names +* [ZOOKEEPER-3981](https://issues.apache.org/jira/browse/ZOOKEEPER-3981) - Flaky test MultipleAddressTest::testGetValidAddressWithNotValid +* [ZOOKEEPER-4017](https://issues.apache.org/jira/browse/ZOOKEEPER-4017) - Owasp check failing - Jetty 9.4.32 - CVE-2020-27216 +* [ZOOKEEPER-4023](https://issues.apache.org/jira/browse/ZOOKEEPER-4023) - dependency-check:check failing - Jetty 9.4.34.v20201102 - CVE-2020-27218 +* [ZOOKEEPER-4056](https://issues.apache.org/jira/browse/ZOOKEEPER-4056) - Update copyright notices from 2020 to 2021 +* [ZOOKEEPER-4233](https://issues.apache.org/jira/browse/ZOOKEEPER-4233) - dependency-check:check failing - Jetty 9.4.35.v20201120 - CVE-2020-2722 + +## Sub-task + +* [ZOOKEEPER-4251](https://issues.apache.org/jira/browse/ZOOKEEPER-4251) - Flaky test: org.apache.zookeeper.test.WatcherTest +* [ZOOKEEPER-4270](https://issues.apache.org/jira/browse/ZOOKEEPER-4270) - Flaky test: QuorumPeerMainTest#testLeaderOutOfView + + + + diff --git a/zookeeper-docs/src/main/resources/markdown/skin/basic.css b/zookeeper-docs/src/main/resources/markdown/skin/basic.css new file mode 100644 index 0000000..01c383d --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/basic.css @@ -0,0 +1,167 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +/** + * General + */ + +img { border: 0; } + +#content table { + border: 0; + width: 100%; +} +/*Hack to get IE to render the table at 100%*/ +* html #content table { margin-left: -3px; } + +#content th, +#content td { + margin: 0; + padding: 0; + vertical-align: top; +} + +.clearboth { + clear: both; +} + +.note, .warning, .fixme { + clear:right; + border: solid black 1px; + margin: 1em 3em; +} + +.note .label { + background: #369; + color: white; + font-weight: bold; + padding: 5px 10px; +} +.note .content { + background: #F0F0FF; + color: black; + line-height: 120%; + font-size: 90%; + padding: 5px 10px; +} +.warning .label { + background: #C00; + color: white; + font-weight: bold; + padding: 5px 10px; +} +.warning .content { + background: #FFF0F0; + color: black; + line-height: 120%; + font-size: 90%; + padding: 5px 10px; +} +.fixme .label { + background: #C6C600; + color: black; + font-weight: bold; + padding: 5px 10px; +} +.fixme .content { + padding: 5px 10px; +} + +/** + * Typography + */ + +body { + font-family: verdana, "Trebuchet MS", arial, helvetica, sans-serif; + font-size: 100%; +} + +#content { + font-family: Georgia, Palatino, Times, serif; + font-size: 95%; +} +#tabs { + font-size: 70%; +} +#menu { + font-size: 80%; +} +#footer { + font-size: 70%; +} + +h1, h2, h3, h4, h5, h6 { + font-family: "Trebuchet MS", verdana, arial, helvetica, sans-serif; + font-weight: bold; + margin-top: 1em; + margin-bottom: .5em; +} + +h1 { + margin-top: 0; + margin-bottom: 1em; + font-size: 1.4em; +} +#content h1 { + font-size: 160%; + margin-bottom: .5em; +} +#menu h1 { + margin: 0; + padding: 10px; + background: #336699; + color: white; +} +h2 { font-size: 120%; } +h3 { font-size: 100%; } +h4 { font-size: 90%; } +h5 { font-size: 80%; } +h6 { font-size: 75%; } + +p { + line-height: 120%; + text-align: left; + margin-top: .5em; + margin-bottom: 1em; +} + +#content li, +#content th, +#content td, +#content li ul, +#content li ol{ + margin-top: .5em; + margin-bottom: .5em; +} + + +#content li li, +#minitoc-area li{ + margin-top: 0em; + margin-bottom: 0em; +} + +#content .attribution { + text-align: right; + font-style: italic; + font-size: 85%; + margin-top: 1em; +} + +.codefrag { + font-family: "Courier New", Courier, monospace; + font-size: 110%; +} \ No newline at end of file diff --git a/zookeeper-docs/src/main/resources/markdown/skin/chapter.gif b/zookeeper-docs/src/main/resources/markdown/skin/chapter.gif new file mode 100644 index 0000000..d3d8245 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/chapter.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/chapter_open.gif b/zookeeper-docs/src/main/resources/markdown/skin/chapter_open.gif new file mode 100644 index 0000000..eecce18 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/chapter_open.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/current.gif b/zookeeper-docs/src/main/resources/markdown/skin/current.gif new file mode 100644 index 0000000..fd82c08 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/current.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/getBlank.js b/zookeeper-docs/src/main/resources/markdown/skin/getBlank.js new file mode 100644 index 0000000..d9978c0 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/getBlank.js @@ -0,0 +1,40 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +/** + * getBlank script - when included in a html file and called from a form text field, will set the value of this field to "" + * if the text value is still the standard value. + * getPrompt script - when included in a html file and called from a form text field, will set the value of this field to the prompt + * if the text value is empty. + * + * Typical usage: + * + * + */ + diff --git a/zookeeper-docs/src/main/resources/markdown/skin/getMenu.js b/zookeeper-docs/src/main/resources/markdown/skin/getMenu.js new file mode 100644 index 0000000..6878b26 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/getMenu.js @@ -0,0 +1,45 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +/** + * This script, when included in a html file, can be used to make collapsible menus + * + * Typical usage: + * + */ + +if (document.getElementById){ + document.write('') +} + + +function SwitchMenu(obj, thePath) +{ +var open = 'url("'+thePath + 'chapter_open.gif")'; +var close = 'url("'+thePath + 'chapter.gif")'; + if(document.getElementById) { + var el = document.getElementById(obj); + var title = document.getElementById(obj+'Title'); + + if(el.style.display != "block"){ + title.style.backgroundImage = open; + el.style.display = "block"; + }else{ + title.style.backgroundImage = close; + el.style.display = "none"; + } + }// end - if(document.getElementById) +}//end - function SwitchMenu(obj) diff --git a/zookeeper-docs/src/main/resources/markdown/skin/header_white_line.gif b/zookeeper-docs/src/main/resources/markdown/skin/header_white_line.gif new file mode 100644 index 0000000..369cae8 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/header_white_line.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/init.js b/zookeeper-docs/src/main/resources/markdown/skin/init.js new file mode 100644 index 0000000..fa40beb --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/init.js @@ -0,0 +1,57 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +/** + * This script, when included in a html file, can be used to make collapsible menus + * + * Typical usage: + * + */ + +function getFileName(url){ + var fileName = url.substring(url.lastIndexOf('/')+1); + return fileName; +} + +function init(){ + var url = window .location.pathname; + var fileName = getFileName(url); + + var menuItemGroup = document.getElementById("menu").children; + + for (i = 0; i < menuItemGroup.length; i++) { + if("menutitle" === menuItemGroup[i].className){ + continue; + } + var menuItem = menuItemGroup[i].children; + if(menuItem.length>0){ + for (j = 0; j < menuItem.length; j++) { + if(menuItem[j].firstElementChild != null){ + var linkItem = menuItem[j].firstElementChild; + if('a' === linkItem.localName){ + var linkFile = getFileName(linkItem.href); + if(fileName === linkFile && linkItem.href.lastIndexOf("apidocs/zookeeper-server/index.html")<0){ + linkItem.className = "selected"; + linkItem.parentNode.parentNode.className = "selectedmenuitemgroup"; + var title = document.getElementById(linkItem.parentNode.parentNode.id+"Title"); + title.className="menutitle selected"; + } + } + } + } + } + } +} diff --git a/zookeeper-docs/src/main/resources/markdown/skin/instruction_arrow.png b/zookeeper-docs/src/main/resources/markdown/skin/instruction_arrow.png new file mode 100644 index 0000000..0fbc724 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/instruction_arrow.png differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/menu.js b/zookeeper-docs/src/main/resources/markdown/skin/menu.js new file mode 100644 index 0000000..06ea471 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/menu.js @@ -0,0 +1,48 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +/** + * This script, when included in a html file, can be used to make collapsible menus + * + * Typical usage: + * + */ + +if (document.getElementById){ + document.write('') +} + +function SwitchMenu(obj) +{ + if(document.getElementById) { + var el = document.getElementById(obj); + var title = document.getElementById(obj+'Title'); + + if(obj.indexOf("_selected_")==0&&el.style.display == ""){ + el.style.display = "block"; + title.className = "pagegroupselected"; + } + + if(el.style.display != "block"){ + el.style.display = "block"; + title.className = "pagegroupopen"; + } + else{ + el.style.display = "none"; + title.className = "pagegroup"; + } + }// end - if(document.getElementById) +}//end - function SwitchMenu(obj) diff --git a/zookeeper-docs/src/main/resources/markdown/skin/page.gif b/zookeeper-docs/src/main/resources/markdown/skin/page.gif new file mode 100644 index 0000000..a144d32 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/page.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/print.css b/zookeeper-docs/src/main/resources/markdown/skin/print.css new file mode 100644 index 0000000..aaa9931 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/print.css @@ -0,0 +1,54 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +body { + font-family: Georgia, Palatino, serif; + font-size: 12pt; + background: white; +} + +#tabs, +#menu, +#content .toc { + display: none; +} + +#content { + width: auto; + padding: 0; + float: none !important; + color: black; + background: inherit; +} + +a:link, a:visited { + color: #336699; + background: inherit; + text-decoration: underline; +} + +#top .logo { + padding: 0; + margin: 0 0 2em 0; +} + +#footer { + margin-top: 4em; +} + +acronym { + border: 0; +} \ No newline at end of file diff --git a/zookeeper-docs/src/main/resources/markdown/skin/printer.gif b/zookeeper-docs/src/main/resources/markdown/skin/printer.gif new file mode 100644 index 0000000..a8d0d41 Binary files /dev/null and b/zookeeper-docs/src/main/resources/markdown/skin/printer.gif differ diff --git a/zookeeper-docs/src/main/resources/markdown/skin/profile.css b/zookeeper-docs/src/main/resources/markdown/skin/profile.css new file mode 100644 index 0000000..190e74f --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/profile.css @@ -0,0 +1,159 @@ + + +/* ==================== aural ============================ */ + +@media aural { + h1, h2, h3, h4, h5, h6 { voice-family: paul, male; stress: 20; richness: 90 } + h1 { pitch: x-low; pitch-range: 90 } + h2 { pitch: x-low; pitch-range: 80 } + h3 { pitch: low; pitch-range: 70 } + h4 { pitch: medium; pitch-range: 60 } + h5 { pitch: medium; pitch-range: 50 } + h6 { pitch: medium; pitch-range: 40 } + li, dt, dd { pitch: medium; richness: 60 } + dt { stress: 80 } + pre, code, tt { pitch: medium; pitch-range: 0; stress: 0; richness: 80 } + em { pitch: medium; pitch-range: 60; stress: 60; richness: 50 } + strong { pitch: medium; pitch-range: 60; stress: 90; richness: 90 } + dfn { pitch: high; pitch-range: 60; stress: 60 } + s, strike { richness: 0 } + i { pitch: medium; pitch-range: 60; stress: 60; richness: 50 } + b { pitch: medium; pitch-range: 60; stress: 90; richness: 90 } + u { richness: 0 } + + :link { voice-family: harry, male } + :visited { voice-family: betty, female } + :active { voice-family: betty, female; pitch-range: 80; pitch: x-high } +} + +#top { background-color: #FFFFFF;} + +#top .header .current { background-color: #4C6C8F;} +#top .header .current a:link { color: #ffffff; } +#top .header .current a:visited { color: #ffffff; } +#top .header .current a:hover { color: #ffffff; } + +#tabs li { background-color: #E5E4D9 ;} +#tabs li a:link { color: #000000; } +#tabs li a:visited { color: #000000; } +#tabs li a:hover { color: #000000; } + +#level2tabs a.selected { background-color: #4C6C8F ;} +#level2tabs a:link { color: #ffffff; } +#level2tabs a:visited { color: #ffffff; } +#level2tabs a:hover { color: #ffffff; } + +#level2tabs { background-color: #E5E4D9;} +#level2tabs a.unselected:link { color: #000000; } +#level2tabs a.unselected:visited { color: #000000; } +#level2tabs a.unselected:hover { color: #000000; } + +.heading { background-color: #E5E4D9;} + +.boxed { background-color: #E5E4D9;} +.underlined_5 {border-bottom: solid 5px #E5E4D9;} +.underlined_10 {border-bottom: solid 10px #E5E4D9;} +table caption { +background-color: #E5E4D9; +color: #000000; +} + +#feedback { +color: #FFFFFF; +background: #4C6C8F; +text-align: center; +} +#feedback #feedbackto { +color: #FFFFFF; +} + +#publishedStrip { +color: #FFFFFF; +background: #4C6C8F; +} + +#publishedStrip { +color: #000000; +background: #E5E4D9; +} + +#menu a.selected { background-color: #CFDCED; + border-color: #999999; + color: #000000;} +#menu a.selected:visited { color: #000000;} + +#menu { border-color: #999999;} +#menu .menupageitemgroup { border-color: #999999;} + +#menu { background-color: #4C6C8F;} +#menu { color: #ffffff;} +#menu a:link { color: #ffffff;} +#menu a:visited { color: #ffffff;} +#menu a:hover { +background-color: #4C6C8F; +color: #ffffff;} + +#menu h1 { +color: #000000; +background-color: #cfdced; +} + +#top .searchbox { +background-color: #E5E4D9 ; +color: #000000; +} + +#menu .menupageitemgroup { +background-color: #E5E4D9; +} +#menu .menupageitem { +color: #000000; +} +#menu .menupageitem a:link { color: #000000;} +#menu .menupageitem a:visited { color: #000000;} +#menu .menupageitem a:hover { +background-color: #E5E4D9; +color: #000000; +} + +body{ +background-color: #ffffff; +color: #000000; +} +a:link { color:#0000ff} +a:visited { color:#009999} +a:hover { color:#6587ff} + + +.ForrestTable { background-color: #ccc;} + +.ForrestTable td { background-color: #ffffff;} + +.highlight { background-color: #ffff00;} + +.fixme { border-color: #c60;} + +.note { border-color: #069;} + +.warning { border-color: #900;} + +#footer { background-color: #E5E4D9;} +/* extra-css */ + + p.quote { + margin-left: 2em; + padding: .5em; + background-color: #f0f0f0; + font-family: monospace; + } + + pre { + margin-left: 0em; + padding: 0.5em; + background-color: #f0f0f0; + font-family: monospace; + } + + + + \ No newline at end of file diff --git a/zookeeper-docs/src/main/resources/markdown/skin/prototype.js b/zookeeper-docs/src/main/resources/markdown/skin/prototype.js new file mode 100644 index 0000000..cc89daf --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/prototype.js @@ -0,0 +1,7588 @@ +/* Prototype JavaScript framework, version 1.7.3 + * (c) 2005-2010 Sam Stephenson + * + * Prototype is freely distributable under the terms of an MIT-style license. + * For details, see the Prototype web site: http://www.prototypejs.org/ + * + *--------------------------------------------------------------------------*/ + +var Prototype = { + + Version: '1.7.3', + + Browser: (function(){ + var ua = navigator.userAgent; + var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'; + return { + IE: !!window.attachEvent && !isOpera, + Opera: isOpera, + WebKit: ua.indexOf('AppleWebKit/') > -1, + Gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, + MobileSafari: /Apple.*Mobile/.test(ua) + } + })(), + + BrowserFeatures: { + XPath: !!document.evaluate, + + SelectorsAPI: !!document.querySelector, + + ElementExtensions: (function() { + var constructor = window.Element || window.HTMLElement; + return !!(constructor && constructor.prototype); + })(), + SpecificElementExtensions: (function() { + if (typeof window.HTMLDivElement !== 'undefined') + return true; + + var div = document.createElement('div'), + form = document.createElement('form'), + isSupported = false; + + if (div['__proto__'] && (div['__proto__'] !== form['__proto__'])) { + isSupported = true; + } + + div = form = null; + + return isSupported; + })() + }, + + ScriptFragment: ']*>([\\S\\s]*?)<\/script\\s*>', + JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/, + + emptyFunction: function() { }, + + K: function(x) { return x } +}; + +if (Prototype.Browser.MobileSafari) + Prototype.BrowserFeatures.SpecificElementExtensions = false; +/* Based on Alex Arnell's inheritance implementation. */ + +var Class = (function() { + + var IS_DONTENUM_BUGGY = (function(){ + for (var p in { toString: 1 }) { + if (p === 'toString') return false; + } + return true; + })(); + + function subclass() {}; + function create() { + var parent = null, properties = $A(arguments); + if (Object.isFunction(properties[0])) + parent = properties.shift(); + + function klass() { + this.initialize.apply(this, arguments); + } + + Object.extend(klass, Class.Methods); + klass.superclass = parent; + klass.subclasses = []; + + if (parent) { + subclass.prototype = parent.prototype; + klass.prototype = new subclass; + parent.subclasses.push(klass); + } + + for (var i = 0, length = properties.length; i < length; i++) + klass.addMethods(properties[i]); + + if (!klass.prototype.initialize) + klass.prototype.initialize = Prototype.emptyFunction; + + klass.prototype.constructor = klass; + return klass; + } + + function addMethods(source) { + var ancestor = this.superclass && this.superclass.prototype, + properties = Object.keys(source); + + if (IS_DONTENUM_BUGGY) { + if (source.toString != Object.prototype.toString) + properties.push("toString"); + if (source.valueOf != Object.prototype.valueOf) + properties.push("valueOf"); + } + + for (var i = 0, length = properties.length; i < length; i++) { + var property = properties[i], value = source[property]; + if (ancestor && Object.isFunction(value) && + value.argumentNames()[0] == "$super") { + var method = value; + value = (function(m) { + return function() { return ancestor[m].apply(this, arguments); }; + })(property).wrap(method); + + value.valueOf = (function(method) { + return function() { return method.valueOf.call(method); }; + })(method); + + value.toString = (function(method) { + return function() { return method.toString.call(method); }; + })(method); + } + this.prototype[property] = value; + } + + return this; + } + + return { + create: create, + Methods: { + addMethods: addMethods + } + }; +})(); +(function() { + + var _toString = Object.prototype.toString, + _hasOwnProperty = Object.prototype.hasOwnProperty, + NULL_TYPE = 'Null', + UNDEFINED_TYPE = 'Undefined', + BOOLEAN_TYPE = 'Boolean', + NUMBER_TYPE = 'Number', + STRING_TYPE = 'String', + OBJECT_TYPE = 'Object', + FUNCTION_CLASS = '[object Function]', + BOOLEAN_CLASS = '[object Boolean]', + NUMBER_CLASS = '[object Number]', + STRING_CLASS = '[object String]', + ARRAY_CLASS = '[object Array]', + DATE_CLASS = '[object Date]', + NATIVE_JSON_STRINGIFY_SUPPORT = window.JSON && + typeof JSON.stringify === 'function' && + JSON.stringify(0) === '0' && + typeof JSON.stringify(Prototype.K) === 'undefined'; + + + + var DONT_ENUMS = ['toString', 'toLocaleString', 'valueOf', + 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'constructor']; + + var IS_DONTENUM_BUGGY = (function(){ + for (var p in { toString: 1 }) { + if (p === 'toString') return false; + } + return true; + })(); + + function Type(o) { + switch(o) { + case null: return NULL_TYPE; + case (void 0): return UNDEFINED_TYPE; + } + var type = typeof o; + switch(type) { + case 'boolean': return BOOLEAN_TYPE; + case 'number': return NUMBER_TYPE; + case 'string': return STRING_TYPE; + } + return OBJECT_TYPE; + } + + function extend(destination, source) { + for (var property in source) + destination[property] = source[property]; + return destination; + } + + function inspect(object) { + try { + if (isUndefined(object)) return 'undefined'; + if (object === null) return 'null'; + return object.inspect ? object.inspect() : String(object); + } catch (e) { + if (e instanceof RangeError) return '...'; + throw e; + } + } + + function toJSON(value) { + return Str('', { '': value }, []); + } + + function Str(key, holder, stack) { + var value = holder[key]; + if (Type(value) === OBJECT_TYPE && typeof value.toJSON === 'function') { + value = value.toJSON(key); + } + + var _class = _toString.call(value); + + switch (_class) { + case NUMBER_CLASS: + case BOOLEAN_CLASS: + case STRING_CLASS: + value = value.valueOf(); + } + + switch (value) { + case null: return 'null'; + case true: return 'true'; + case false: return 'false'; + } + + var type = typeof value; + switch (type) { + case 'string': + return value.inspect(true); + case 'number': + return isFinite(value) ? String(value) : 'null'; + case 'object': + + for (var i = 0, length = stack.length; i < length; i++) { + if (stack[i] === value) { + throw new TypeError("Cyclic reference to '" + value + "' in object"); + } + } + stack.push(value); + + var partial = []; + if (_class === ARRAY_CLASS) { + for (var i = 0, length = value.length; i < length; i++) { + var str = Str(i, value, stack); + partial.push(typeof str === 'undefined' ? 'null' : str); + } + partial = '[' + partial.join(',') + ']'; + } else { + var keys = Object.keys(value); + for (var i = 0, length = keys.length; i < length; i++) { + var key = keys[i], str = Str(key, value, stack); + if (typeof str !== "undefined") { + partial.push(key.inspect(true)+ ':' + str); + } + } + partial = '{' + partial.join(',') + '}'; + } + stack.pop(); + return partial; + } + } + + function stringify(object) { + return JSON.stringify(object); + } + + function toQueryString(object) { + return $H(object).toQueryString(); + } + + function toHTML(object) { + return object && object.toHTML ? object.toHTML() : String.interpret(object); + } + + function keys(object) { + if (Type(object) !== OBJECT_TYPE) { throw new TypeError(); } + var results = []; + for (var property in object) { + if (_hasOwnProperty.call(object, property)) + results.push(property); + } + + if (IS_DONTENUM_BUGGY) { + for (var i = 0; property = DONT_ENUMS[i]; i++) { + if (_hasOwnProperty.call(object, property)) + results.push(property); + } + } + + return results; + } + + function values(object) { + var results = []; + for (var property in object) + results.push(object[property]); + return results; + } + + function clone(object) { + return extend({ }, object); + } + + function isElement(object) { + return !!(object && object.nodeType == 1); + } + + function isArray(object) { + return _toString.call(object) === ARRAY_CLASS; + } + + var hasNativeIsArray = (typeof Array.isArray == 'function') + && Array.isArray([]) && !Array.isArray({}); + + if (hasNativeIsArray) { + isArray = Array.isArray; + } + + function isHash(object) { + return object instanceof Hash; + } + + function isFunction(object) { + return _toString.call(object) === FUNCTION_CLASS; + } + + function isString(object) { + return _toString.call(object) === STRING_CLASS; + } + + function isNumber(object) { + return _toString.call(object) === NUMBER_CLASS; + } + + function isDate(object) { + return _toString.call(object) === DATE_CLASS; + } + + function isUndefined(object) { + return typeof object === "undefined"; + } + + extend(Object, { + extend: extend, + inspect: inspect, + toJSON: NATIVE_JSON_STRINGIFY_SUPPORT ? stringify : toJSON, + toQueryString: toQueryString, + toHTML: toHTML, + keys: Object.keys || keys, + values: values, + clone: clone, + isElement: isElement, + isArray: isArray, + isHash: isHash, + isFunction: isFunction, + isString: isString, + isNumber: isNumber, + isDate: isDate, + isUndefined: isUndefined + }); +})(); +Object.extend(Function.prototype, (function() { + var slice = Array.prototype.slice; + + function update(array, args) { + var arrayLength = array.length, length = args.length; + while (length--) array[arrayLength + length] = args[length]; + return array; + } + + function merge(array, args) { + array = slice.call(array, 0); + return update(array, args); + } + + function argumentNames() { + var names = this.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1] + .replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '') + .replace(/\s+/g, '').split(','); + return names.length == 1 && !names[0] ? [] : names; + } + + + function bind(context) { + if (arguments.length < 2 && Object.isUndefined(arguments[0])) + return this; + + if (!Object.isFunction(this)) + throw new TypeError("The object is not callable."); + + var nop = function() {}; + var __method = this, args = slice.call(arguments, 1); + + var bound = function() { + var a = merge(args, arguments); + var c = this instanceof bound ? this : context; + return __method.apply(c, a); + }; + + nop.prototype = this.prototype; + bound.prototype = new nop(); + + return bound; + } + + function bindAsEventListener(context) { + var __method = this, args = slice.call(arguments, 1); + return function(event) { + var a = update([event || window.event], args); + return __method.apply(context, a); + } + } + + function curry() { + if (!arguments.length) return this; + var __method = this, args = slice.call(arguments, 0); + return function() { + var a = merge(args, arguments); + return __method.apply(this, a); + } + } + + function delay(timeout) { + var __method = this, args = slice.call(arguments, 1); + timeout = timeout * 1000; + return window.setTimeout(function() { + return __method.apply(__method, args); + }, timeout); + } + + function defer() { + var args = update([0.01], arguments); + return this.delay.apply(this, args); + } + + function wrap(wrapper) { + var __method = this; + return function() { + var a = update([__method.bind(this)], arguments); + return wrapper.apply(this, a); + } + } + + function methodize() { + if (this._methodized) return this._methodized; + var __method = this; + return this._methodized = function() { + var a = update([this], arguments); + return __method.apply(null, a); + }; + } + + var extensions = { + argumentNames: argumentNames, + bindAsEventListener: bindAsEventListener, + curry: curry, + delay: delay, + defer: defer, + wrap: wrap, + methodize: methodize + }; + + if (!Function.prototype.bind) + extensions.bind = bind; + + return extensions; +})()); + + + +(function(proto) { + + + function toISOString() { + return this.getUTCFullYear() + '-' + + (this.getUTCMonth() + 1).toPaddedString(2) + '-' + + this.getUTCDate().toPaddedString(2) + 'T' + + this.getUTCHours().toPaddedString(2) + ':' + + this.getUTCMinutes().toPaddedString(2) + ':' + + this.getUTCSeconds().toPaddedString(2) + 'Z'; + } + + + function toJSON() { + return this.toISOString(); + } + + if (!proto.toISOString) proto.toISOString = toISOString; + if (!proto.toJSON) proto.toJSON = toJSON; + +})(Date.prototype); + + +RegExp.prototype.match = RegExp.prototype.test; + +RegExp.escape = function(str) { + return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); +}; +var PeriodicalExecuter = Class.create({ + initialize: function(callback, frequency) { + this.callback = callback; + this.frequency = frequency; + this.currentlyExecuting = false; + + this.registerCallback(); + }, + + registerCallback: function() { + this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000); + }, + + execute: function() { + this.callback(this); + }, + + stop: function() { + if (!this.timer) return; + clearInterval(this.timer); + this.timer = null; + }, + + onTimerEvent: function() { + if (!this.currentlyExecuting) { + try { + this.currentlyExecuting = true; + this.execute(); + this.currentlyExecuting = false; + } catch(e) { + this.currentlyExecuting = false; + throw e; + } + } + } +}); +Object.extend(String, { + interpret: function(value) { + return value == null ? '' : String(value); + }, + specialChar: { + '\b': '\\b', + '\t': '\\t', + '\n': '\\n', + '\f': '\\f', + '\r': '\\r', + '\\': '\\\\' + } +}); + +Object.extend(String.prototype, (function() { + var NATIVE_JSON_PARSE_SUPPORT = window.JSON && + typeof JSON.parse === 'function' && + JSON.parse('{"test": true}').test; + + function prepareReplacement(replacement) { + if (Object.isFunction(replacement)) return replacement; + var template = new Template(replacement); + return function(match) { return template.evaluate(match) }; + } + + function isNonEmptyRegExp(regexp) { + return regexp.source && regexp.source !== '(?:)'; + } + + + function gsub(pattern, replacement) { + var result = '', source = this, match; + replacement = prepareReplacement(replacement); + + if (Object.isString(pattern)) + pattern = RegExp.escape(pattern); + + if (!(pattern.length || isNonEmptyRegExp(pattern))) { + replacement = replacement(''); + return replacement + source.split('').join(replacement) + replacement; + } + + while (source.length > 0) { + match = source.match(pattern) + if (match && match[0].length > 0) { + result += source.slice(0, match.index); + result += String.interpret(replacement(match)); + source = source.slice(match.index + match[0].length); + } else { + result += source, source = ''; + } + } + return result; + } + + function sub(pattern, replacement, count) { + replacement = prepareReplacement(replacement); + count = Object.isUndefined(count) ? 1 : count; + + return this.gsub(pattern, function(match) { + if (--count < 0) return match[0]; + return replacement(match); + }); + } + + function scan(pattern, iterator) { + this.gsub(pattern, iterator); + return String(this); + } + + function truncate(length, truncation) { + length = length || 30; + truncation = Object.isUndefined(truncation) ? '...' : truncation; + return this.length > length ? + this.slice(0, length - truncation.length) + truncation : String(this); + } + + function strip() { + return this.replace(/^\s+/, '').replace(/\s+$/, ''); + } + + function stripTags() { + return this.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?(\/)?>|<\/\w+>/gi, ''); + } + + function stripScripts() { + return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), ''); + } + + function extractScripts() { + var matchAll = new RegExp(Prototype.ScriptFragment, 'img'), + matchOne = new RegExp(Prototype.ScriptFragment, 'im'); + return (this.match(matchAll) || []).map(function(scriptTag) { + return (scriptTag.match(matchOne) || ['', ''])[1]; + }); + } + + function evalScripts() { + return this.extractScripts().map(function(script) { return eval(script); }); + } + + function escapeHTML() { + return this.replace(/&/g,'&').replace(//g,'>'); + } + + function unescapeHTML() { + return this.stripTags().replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&'); + } + + + function toQueryParams(separator) { + var match = this.strip().match(/([^?#]*)(#.*)?$/); + if (!match) return { }; + + return match[1].split(separator || '&').inject({ }, function(hash, pair) { + if ((pair = pair.split('='))[0]) { + var key = decodeURIComponent(pair.shift()), + value = pair.length > 1 ? pair.join('=') : pair[0]; + + if (value != undefined) { + value = value.gsub('+', ' '); + value = decodeURIComponent(value); + } + + if (key in hash) { + if (!Object.isArray(hash[key])) hash[key] = [hash[key]]; + hash[key].push(value); + } + else hash[key] = value; + } + return hash; + }); + } + + function toArray() { + return this.split(''); + } + + function succ() { + return this.slice(0, this.length - 1) + + String.fromCharCode(this.charCodeAt(this.length - 1) + 1); + } + + function times(count) { + return count < 1 ? '' : new Array(count + 1).join(this); + } + + function camelize() { + return this.replace(/-+(.)?/g, function(match, chr) { + return chr ? chr.toUpperCase() : ''; + }); + } + + function capitalize() { + return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); + } + + function underscore() { + return this.replace(/::/g, '/') + .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') + .replace(/([a-z\d])([A-Z])/g, '$1_$2') + .replace(/-/g, '_') + .toLowerCase(); + } + + function dasherize() { + return this.replace(/_/g, '-'); + } + + function inspect(useDoubleQuotes) { + var escapedString = this.replace(/[\x00-\x1f\\]/g, function(character) { + if (character in String.specialChar) { + return String.specialChar[character]; + } + return '\\u00' + character.charCodeAt().toPaddedString(2, 16); + }); + if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"'; + return "'" + escapedString.replace(/'/g, '\\\'') + "'"; + } + + function unfilterJSON(filter) { + return this.replace(filter || Prototype.JSONFilter, '$1'); + } + + function isJSON() { + var str = this; + if (str.blank()) return false; + str = str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@'); + str = str.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); + str = str.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); + return (/^[\],:{}\s]*$/).test(str); + } + + function evalJSON(sanitize) { + var json = this.unfilterJSON(), + cx = /[\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff\u0000]/g; + if (cx.test(json)) { + json = json.replace(cx, function (a) { + return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); + }); + } + try { + if (!sanitize || json.isJSON()) return eval('(' + json + ')'); + } catch (e) { } + throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); + } + + function parseJSON() { + var json = this.unfilterJSON(); + return JSON.parse(json); + } + + function include(pattern) { + return this.indexOf(pattern) > -1; + } + + function startsWith(pattern, position) { + position = Object.isNumber(position) ? position : 0; + return this.lastIndexOf(pattern, position) === position; + } + + function endsWith(pattern, position) { + pattern = String(pattern); + position = Object.isNumber(position) ? position : this.length; + if (position < 0) position = 0; + if (position > this.length) position = this.length; + var d = position - pattern.length; + return d >= 0 && this.indexOf(pattern, d) === d; + } + + function empty() { + return this == ''; + } + + function blank() { + return /^\s*$/.test(this); + } + + function interpolate(object, pattern) { + return new Template(this, pattern).evaluate(object); + } + + return { + gsub: gsub, + sub: sub, + scan: scan, + truncate: truncate, + strip: String.prototype.trim || strip, + stripTags: stripTags, + stripScripts: stripScripts, + extractScripts: extractScripts, + evalScripts: evalScripts, + escapeHTML: escapeHTML, + unescapeHTML: unescapeHTML, + toQueryParams: toQueryParams, + parseQuery: toQueryParams, + toArray: toArray, + succ: succ, + times: times, + camelize: camelize, + capitalize: capitalize, + underscore: underscore, + dasherize: dasherize, + inspect: inspect, + unfilterJSON: unfilterJSON, + isJSON: isJSON, + evalJSON: NATIVE_JSON_PARSE_SUPPORT ? parseJSON : evalJSON, + include: include, + startsWith: String.prototype.startsWith || startsWith, + endsWith: String.prototype.endsWith || endsWith, + empty: empty, + blank: blank, + interpolate: interpolate + }; +})()); + +var Template = Class.create({ + initialize: function(template, pattern) { + this.template = template.toString(); + this.pattern = pattern || Template.Pattern; + }, + + evaluate: function(object) { + if (object && Object.isFunction(object.toTemplateReplacements)) + object = object.toTemplateReplacements(); + + return this.template.gsub(this.pattern, function(match) { + if (object == null) return (match[1] + ''); + + var before = match[1] || ''; + if (before == '\\') return match[2]; + + var ctx = object, expr = match[3], + pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/; + + match = pattern.exec(expr); + if (match == null) return before; + + while (match != null) { + var comp = match[1].startsWith('[') ? match[2].replace(/\\\\]/g, ']') : match[1]; + ctx = ctx[comp]; + if (null == ctx || '' == match[3]) break; + expr = expr.substring('[' == match[3] ? match[1].length : match[0].length); + match = pattern.exec(expr); + } + + return before + String.interpret(ctx); + }); + } +}); +Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/; + +var $break = { }; + +var Enumerable = (function() { + function each(iterator, context) { + try { + this._each(iterator, context); + } catch (e) { + if (e != $break) throw e; + } + return this; + } + + function eachSlice(number, iterator, context) { + var index = -number, slices = [], array = this.toArray(); + if (number < 1) return array; + while ((index += number) < array.length) + slices.push(array.slice(index, index+number)); + return slices.collect(iterator, context); + } + + function all(iterator, context) { + iterator = iterator || Prototype.K; + var result = true; + this.each(function(value, index) { + result = result && !!iterator.call(context, value, index, this); + if (!result) throw $break; + }, this); + return result; + } + + function any(iterator, context) { + iterator = iterator || Prototype.K; + var result = false; + this.each(function(value, index) { + if (result = !!iterator.call(context, value, index, this)) + throw $break; + }, this); + return result; + } + + function collect(iterator, context) { + iterator = iterator || Prototype.K; + var results = []; + this.each(function(value, index) { + results.push(iterator.call(context, value, index, this)); + }, this); + return results; + } + + function detect(iterator, context) { + var result; + this.each(function(value, index) { + if (iterator.call(context, value, index, this)) { + result = value; + throw $break; + } + }, this); + return result; + } + + function findAll(iterator, context) { + var results = []; + this.each(function(value, index) { + if (iterator.call(context, value, index, this)) + results.push(value); + }, this); + return results; + } + + function grep(filter, iterator, context) { + iterator = iterator || Prototype.K; + var results = []; + + if (Object.isString(filter)) + filter = new RegExp(RegExp.escape(filter)); + + this.each(function(value, index) { + if (filter.match(value)) + results.push(iterator.call(context, value, index, this)); + }, this); + return results; + } + + function include(object) { + if (Object.isFunction(this.indexOf) && this.indexOf(object) != -1) + return true; + + var found = false; + this.each(function(value) { + if (value == object) { + found = true; + throw $break; + } + }); + return found; + } + + function inGroupsOf(number, fillWith) { + fillWith = Object.isUndefined(fillWith) ? null : fillWith; + return this.eachSlice(number, function(slice) { + while(slice.length < number) slice.push(fillWith); + return slice; + }); + } + + function inject(memo, iterator, context) { + this.each(function(value, index) { + memo = iterator.call(context, memo, value, index, this); + }, this); + return memo; + } + + function invoke(method) { + var args = $A(arguments).slice(1); + return this.map(function(value) { + return value[method].apply(value, args); + }); + } + + function max(iterator, context) { + iterator = iterator || Prototype.K; + var result; + this.each(function(value, index) { + value = iterator.call(context, value, index, this); + if (result == null || value >= result) + result = value; + }, this); + return result; + } + + function min(iterator, context) { + iterator = iterator || Prototype.K; + var result; + this.each(function(value, index) { + value = iterator.call(context, value, index, this); + if (result == null || value < result) + result = value; + }, this); + return result; + } + + function partition(iterator, context) { + iterator = iterator || Prototype.K; + var trues = [], falses = []; + this.each(function(value, index) { + (iterator.call(context, value, index, this) ? + trues : falses).push(value); + }, this); + return [trues, falses]; + } + + function pluck(property) { + var results = []; + this.each(function(value) { + results.push(value[property]); + }); + return results; + } + + function reject(iterator, context) { + var results = []; + this.each(function(value, index) { + if (!iterator.call(context, value, index, this)) + results.push(value); + }, this); + return results; + } + + function sortBy(iterator, context) { + return this.map(function(value, index) { + return { + value: value, + criteria: iterator.call(context, value, index, this) + }; + }, this).sort(function(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + }).pluck('value'); + } + + function toArray() { + return this.map(); + } + + function zip() { + var iterator = Prototype.K, args = $A(arguments); + if (Object.isFunction(args.last())) + iterator = args.pop(); + + var collections = [this].concat(args).map($A); + return this.map(function(value, index) { + return iterator(collections.pluck(index)); + }); + } + + function size() { + return this.toArray().length; + } + + function inspect() { + return '#'; + } + + + + + + + + + + return { + each: each, + eachSlice: eachSlice, + all: all, + every: all, + any: any, + some: any, + collect: collect, + map: collect, + detect: detect, + findAll: findAll, + select: findAll, + filter: findAll, + grep: grep, + include: include, + member: include, + inGroupsOf: inGroupsOf, + inject: inject, + invoke: invoke, + max: max, + min: min, + partition: partition, + pluck: pluck, + reject: reject, + sortBy: sortBy, + toArray: toArray, + entries: toArray, + zip: zip, + size: size, + inspect: inspect, + find: detect + }; +})(); + +function $A(iterable) { + if (!iterable) return []; + if ('toArray' in Object(iterable)) return iterable.toArray(); + var length = iterable.length || 0, results = new Array(length); + while (length--) results[length] = iterable[length]; + return results; +} + + +function $w(string) { + if (!Object.isString(string)) return []; + string = string.strip(); + return string ? string.split(/\s+/) : []; +} + +Array.from = $A; + + +(function() { + var arrayProto = Array.prototype, + slice = arrayProto.slice, + _each = arrayProto.forEach; // use native browser JS 1.6 implementation if available + + function each(iterator, context) { + for (var i = 0, length = this.length >>> 0; i < length; i++) { + if (i in this) iterator.call(context, this[i], i, this); + } + } + if (!_each) _each = each; + + function clear() { + this.length = 0; + return this; + } + + function first() { + return this[0]; + } + + function last() { + return this[this.length - 1]; + } + + function compact() { + return this.select(function(value) { + return value != null; + }); + } + + function flatten() { + return this.inject([], function(array, value) { + if (Object.isArray(value)) + return array.concat(value.flatten()); + array.push(value); + return array; + }); + } + + function without() { + var values = slice.call(arguments, 0); + return this.select(function(value) { + return !values.include(value); + }); + } + + function reverse(inline) { + return (inline === false ? this.toArray() : this)._reverse(); + } + + function uniq(sorted) { + return this.inject([], function(array, value, index) { + if (0 == index || (sorted ? array.last() != value : !array.include(value))) + array.push(value); + return array; + }); + } + + function intersect(array) { + return this.uniq().findAll(function(item) { + return array.indexOf(item) !== -1; + }); + } + + + function clone() { + return slice.call(this, 0); + } + + function size() { + return this.length; + } + + function inspect() { + return '[' + this.map(Object.inspect).join(', ') + ']'; + } + + function indexOf(item, i) { + if (this == null) throw new TypeError(); + + var array = Object(this), length = array.length >>> 0; + if (length === 0) return -1; + + i = Number(i); + if (isNaN(i)) { + i = 0; + } else if (i !== 0 && isFinite(i)) { + i = (i > 0 ? 1 : -1) * Math.floor(Math.abs(i)); + } + + if (i > length) return -1; + + var k = i >= 0 ? i : Math.max(length - Math.abs(i), 0); + for (; k < length; k++) + if (k in array && array[k] === item) return k; + return -1; + } + + + function lastIndexOf(item, i) { + if (this == null) throw new TypeError(); + + var array = Object(this), length = array.length >>> 0; + if (length === 0) return -1; + + if (!Object.isUndefined(i)) { + i = Number(i); + if (isNaN(i)) { + i = 0; + } else if (i !== 0 && isFinite(i)) { + i = (i > 0 ? 1 : -1) * Math.floor(Math.abs(i)); + } + } else { + i = length; + } + + var k = i >= 0 ? Math.min(i, length - 1) : + length - Math.abs(i); + + for (; k >= 0; k--) + if (k in array && array[k] === item) return k; + return -1; + } + + function concat(_) { + var array = [], items = slice.call(arguments, 0), item, n = 0; + items.unshift(this); + for (var i = 0, length = items.length; i < length; i++) { + item = items[i]; + if (Object.isArray(item) && !('callee' in item)) { + for (var j = 0, arrayLength = item.length; j < arrayLength; j++) { + if (j in item) array[n] = item[j]; + n++; + } + } else { + array[n++] = item; + } + } + array.length = n; + return array; + } + + + function wrapNative(method) { + return function() { + if (arguments.length === 0) { + return method.call(this, Prototype.K); + } else if (arguments[0] === undefined) { + var args = slice.call(arguments, 1); + args.unshift(Prototype.K); + return method.apply(this, args); + } else { + return method.apply(this, arguments); + } + }; + } + + + function map(iterator) { + if (this == null) throw new TypeError(); + iterator = iterator || Prototype.K; + + var object = Object(this); + var results = [], context = arguments[1], n = 0; + + for (var i = 0, length = object.length >>> 0; i < length; i++) { + if (i in object) { + results[n] = iterator.call(context, object[i], i, object); + } + n++; + } + results.length = n; + return results; + } + + if (arrayProto.map) { + map = wrapNative(Array.prototype.map); + } + + function filter(iterator) { + if (this == null || !Object.isFunction(iterator)) + throw new TypeError(); + + var object = Object(this); + var results = [], context = arguments[1], value; + + for (var i = 0, length = object.length >>> 0; i < length; i++) { + if (i in object) { + value = object[i]; + if (iterator.call(context, value, i, object)) { + results.push(value); + } + } + } + return results; + } + + if (arrayProto.filter) { + filter = Array.prototype.filter; + } + + function some(iterator) { + if (this == null) throw new TypeError(); + iterator = iterator || Prototype.K; + var context = arguments[1]; + + var object = Object(this); + for (var i = 0, length = object.length >>> 0; i < length; i++) { + if (i in object && iterator.call(context, object[i], i, object)) { + return true; + } + } + + return false; + } + + if (arrayProto.some) { + some = wrapNative(Array.prototype.some); + } + + function every(iterator) { + if (this == null) throw new TypeError(); + iterator = iterator || Prototype.K; + var context = arguments[1]; + + var object = Object(this); + for (var i = 0, length = object.length >>> 0; i < length; i++) { + if (i in object && !iterator.call(context, object[i], i, object)) { + return false; + } + } + + return true; + } + + if (arrayProto.every) { + every = wrapNative(Array.prototype.every); + } + + + Object.extend(arrayProto, Enumerable); + + if (arrayProto.entries === Enumerable.entries) { + delete arrayProto.entries; + } + + if (!arrayProto._reverse) + arrayProto._reverse = arrayProto.reverse; + + Object.extend(arrayProto, { + _each: _each, + + map: map, + collect: map, + select: filter, + filter: filter, + findAll: filter, + some: some, + any: some, + every: every, + all: every, + + clear: clear, + first: first, + last: last, + compact: compact, + flatten: flatten, + without: without, + reverse: reverse, + uniq: uniq, + intersect: intersect, + clone: clone, + toArray: clone, + size: size, + inspect: inspect + }); + + var CONCAT_ARGUMENTS_BUGGY = (function() { + return [].concat(arguments)[0][0] !== 1; + })(1,2); + + if (CONCAT_ARGUMENTS_BUGGY) arrayProto.concat = concat; + + if (!arrayProto.indexOf) arrayProto.indexOf = indexOf; + if (!arrayProto.lastIndexOf) arrayProto.lastIndexOf = lastIndexOf; +})(); +function $H(object) { + return new Hash(object); +}; + +var Hash = Class.create(Enumerable, (function() { + function initialize(object) { + this._object = Object.isHash(object) ? object.toObject() : Object.clone(object); + } + + + function _each(iterator, context) { + var i = 0; + for (var key in this._object) { + var value = this._object[key], pair = [key, value]; + pair.key = key; + pair.value = value; + iterator.call(context, pair, i); + i++; + } + } + + function set(key, value) { + return this._object[key] = value; + } + + function get(key) { + if (this._object[key] !== Object.prototype[key]) + return this._object[key]; + } + + function unset(key) { + var value = this._object[key]; + delete this._object[key]; + return value; + } + + function toObject() { + return Object.clone(this._object); + } + + + + function keys() { + return this.pluck('key'); + } + + function values() { + return this.pluck('value'); + } + + function index(value) { + var match = this.detect(function(pair) { + return pair.value === value; + }); + return match && match.key; + } + + function merge(object) { + return this.clone().update(object); + } + + function update(object) { + return new Hash(object).inject(this, function(result, pair) { + result.set(pair.key, pair.value); + return result; + }); + } + + function toQueryPair(key, value) { + if (Object.isUndefined(value)) return key; + + value = String.interpret(value); + + value = value.gsub(/(\r)?\n/, '\r\n'); + value = encodeURIComponent(value); + value = value.gsub(/%20/, '+'); + return key + '=' + value; + } + + function toQueryString() { + return this.inject([], function(results, pair) { + var key = encodeURIComponent(pair.key), values = pair.value; + + if (values && typeof values == 'object') { + if (Object.isArray(values)) { + var queryValues = []; + for (var i = 0, len = values.length, value; i < len; i++) { + value = values[i]; + queryValues.push(toQueryPair(key, value)); + } + return results.concat(queryValues); + } + } else results.push(toQueryPair(key, values)); + return results; + }).join('&'); + } + + function inspect() { + return '#'; + } + + function clone() { + return new Hash(this); + } + + return { + initialize: initialize, + _each: _each, + set: set, + get: get, + unset: unset, + toObject: toObject, + toTemplateReplacements: toObject, + keys: keys, + values: values, + index: index, + merge: merge, + update: update, + toQueryString: toQueryString, + inspect: inspect, + toJSON: toObject, + clone: clone + }; +})()); + +Hash.from = $H; +Object.extend(Number.prototype, (function() { + function toColorPart() { + return this.toPaddedString(2, 16); + } + + function succ() { + return this + 1; + } + + function times(iterator, context) { + $R(0, this, true).each(iterator, context); + return this; + } + + function toPaddedString(length, radix) { + var string = this.toString(radix || 10); + return '0'.times(length - string.length) + string; + } + + function abs() { + return Math.abs(this); + } + + function round() { + return Math.round(this); + } + + function ceil() { + return Math.ceil(this); + } + + function floor() { + return Math.floor(this); + } + + return { + toColorPart: toColorPart, + succ: succ, + times: times, + toPaddedString: toPaddedString, + abs: abs, + round: round, + ceil: ceil, + floor: floor + }; +})()); + +function $R(start, end, exclusive) { + return new ObjectRange(start, end, exclusive); +} + +var ObjectRange = Class.create(Enumerable, (function() { + function initialize(start, end, exclusive) { + this.start = start; + this.end = end; + this.exclusive = exclusive; + } + + function _each(iterator, context) { + var value = this.start, i; + for (i = 0; this.include(value); i++) { + iterator.call(context, value, i); + value = value.succ(); + } + } + + function include(value) { + if (value < this.start) + return false; + if (this.exclusive) + return value < this.end; + return value <= this.end; + } + + return { + initialize: initialize, + _each: _each, + include: include + }; +})()); + + + +var Abstract = { }; + + +var Try = { + these: function() { + var returnValue; + + for (var i = 0, length = arguments.length; i < length; i++) { + var lambda = arguments[i]; + try { + returnValue = lambda(); + break; + } catch (e) { } + } + + return returnValue; + } +}; + +var Ajax = { + getTransport: function() { + return Try.these( + function() {return new XMLHttpRequest()}, + function() {return new ActiveXObject('Msxml2.XMLHTTP')}, + function() {return new ActiveXObject('Microsoft.XMLHTTP')} + ) || false; + }, + + activeRequestCount: 0 +}; + +Ajax.Responders = { + responders: [], + + _each: function(iterator, context) { + this.responders._each(iterator, context); + }, + + register: function(responder) { + if (!this.include(responder)) + this.responders.push(responder); + }, + + unregister: function(responder) { + this.responders = this.responders.without(responder); + }, + + dispatch: function(callback, request, transport, json) { + this.each(function(responder) { + if (Object.isFunction(responder[callback])) { + try { + responder[callback].apply(responder, [request, transport, json]); + } catch (e) { } + } + }); + } +}; + +Object.extend(Ajax.Responders, Enumerable); + +Ajax.Responders.register({ + onCreate: function() { Ajax.activeRequestCount++ }, + onComplete: function() { Ajax.activeRequestCount-- } +}); +Ajax.Base = Class.create({ + initialize: function(options) { + this.options = { + method: 'post', + asynchronous: true, + contentType: 'application/x-www-form-urlencoded', + encoding: 'UTF-8', + parameters: '', + evalJSON: true, + evalJS: true + }; + Object.extend(this.options, options || { }); + + this.options.method = this.options.method.toLowerCase(); + + if (Object.isHash(this.options.parameters)) + this.options.parameters = this.options.parameters.toObject(); + } +}); +Ajax.Request = Class.create(Ajax.Base, { + _complete: false, + + initialize: function($super, url, options) { + $super(options); + this.transport = Ajax.getTransport(); + this.request(url); + }, + + request: function(url) { + this.url = url; + this.method = this.options.method; + var params = Object.isString(this.options.parameters) ? + this.options.parameters : + Object.toQueryString(this.options.parameters); + + if (!['get', 'post'].include(this.method)) { + params += (params ? '&' : '') + "_method=" + this.method; + this.method = 'post'; + } + + if (params && this.method === 'get') { + this.url += (this.url.include('?') ? '&' : '?') + params; + } + + this.parameters = params.toQueryParams(); + + try { + var response = new Ajax.Response(this); + if (this.options.onCreate) this.options.onCreate(response); + Ajax.Responders.dispatch('onCreate', this, response); + + this.transport.open(this.method.toUpperCase(), this.url, + this.options.asynchronous); + + if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1); + + this.transport.onreadystatechange = this.onStateChange.bind(this); + this.setRequestHeaders(); + + this.body = this.method == 'post' ? (this.options.postBody || params) : null; + this.transport.send(this.body); + + /* Force Firefox to handle ready state 4 for synchronous requests */ + if (!this.options.asynchronous && this.transport.overrideMimeType) + this.onStateChange(); + + } + catch (e) { + this.dispatchException(e); + } + }, + + onStateChange: function() { + var readyState = this.transport.readyState; + if (readyState > 1 && !((readyState == 4) && this._complete)) + this.respondToReadyState(this.transport.readyState); + }, + + setRequestHeaders: function() { + var headers = { + 'X-Requested-With': 'XMLHttpRequest', + 'X-Prototype-Version': Prototype.Version, + 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*' + }; + + if (this.method == 'post') { + headers['Content-type'] = this.options.contentType + + (this.options.encoding ? '; charset=' + this.options.encoding : ''); + + /* Force "Connection: close" for older Mozilla browsers to work + * around a bug where XMLHttpRequest sends an incorrect + * Content-length header. See Mozilla Bugzilla #246651. + */ + if (this.transport.overrideMimeType && + (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005) + headers['Connection'] = 'close'; + } + + if (typeof this.options.requestHeaders == 'object') { + var extras = this.options.requestHeaders; + + if (Object.isFunction(extras.push)) + for (var i = 0, length = extras.length; i < length; i += 2) + headers[extras[i]] = extras[i+1]; + else + $H(extras).each(function(pair) { headers[pair.key] = pair.value }); + } + + for (var name in headers) + if (headers[name] != null) + this.transport.setRequestHeader(name, headers[name]); + }, + + success: function() { + var status = this.getStatus(); + return !status || (status >= 200 && status < 300) || status == 304; + }, + + getStatus: function() { + try { + if (this.transport.status === 1223) return 204; + return this.transport.status || 0; + } catch (e) { return 0 } + }, + + respondToReadyState: function(readyState) { + var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); + + if (state == 'Complete') { + try { + this._complete = true; + (this.options['on' + response.status] + || this.options['on' + (this.success() ? 'Success' : 'Failure')] + || Prototype.emptyFunction)(response, response.headerJSON); + } catch (e) { + this.dispatchException(e); + } + + var contentType = response.getHeader('Content-type'); + if (this.options.evalJS == 'force' + || (this.options.evalJS && this.isSameOrigin() && contentType + && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) + this.evalResponse(); + } + + try { + (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON); + Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); + } catch (e) { + this.dispatchException(e); + } + + if (state == 'Complete') { + this.transport.onreadystatechange = Prototype.emptyFunction; + } + }, + + isSameOrigin: function() { + var m = this.url.match(/^\s*https?:\/\/[^\/]*/); + return !m || (m[0] == '#{protocol}//#{domain}#{port}'.interpolate({ + protocol: location.protocol, + domain: document.domain, + port: location.port ? ':' + location.port : '' + })); + }, + + getHeader: function(name) { + try { + return this.transport.getResponseHeader(name) || null; + } catch (e) { return null; } + }, + + evalResponse: function() { + try { + return eval((this.transport.responseText || '').unfilterJSON()); + } catch (e) { + this.dispatchException(e); + } + }, + + dispatchException: function(exception) { + (this.options.onException || Prototype.emptyFunction)(this, exception); + Ajax.Responders.dispatch('onException', this, exception); + } +}); + +Ajax.Request.Events = + ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete']; + + + + + + + + +Ajax.Response = Class.create({ + initialize: function(request){ + this.request = request; + var transport = this.transport = request.transport, + readyState = this.readyState = transport.readyState; + + if ((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) { + this.status = this.getStatus(); + this.statusText = this.getStatusText(); + this.responseText = String.interpret(transport.responseText); + this.headerJSON = this._getHeaderJSON(); + } + + if (readyState == 4) { + var xml = transport.responseXML; + this.responseXML = Object.isUndefined(xml) ? null : xml; + this.responseJSON = this._getResponseJSON(); + } + }, + + status: 0, + + statusText: '', + + getStatus: Ajax.Request.prototype.getStatus, + + getStatusText: function() { + try { + return this.transport.statusText || ''; + } catch (e) { return '' } + }, + + getHeader: Ajax.Request.prototype.getHeader, + + getAllHeaders: function() { + try { + return this.getAllResponseHeaders(); + } catch (e) { return null } + }, + + getResponseHeader: function(name) { + return this.transport.getResponseHeader(name); + }, + + getAllResponseHeaders: function() { + return this.transport.getAllResponseHeaders(); + }, + + _getHeaderJSON: function() { + var json = this.getHeader('X-JSON'); + if (!json) return null; + + try { + json = decodeURIComponent(escape(json)); + } catch(e) { + } + + try { + return json.evalJSON(this.request.options.sanitizeJSON || + !this.request.isSameOrigin()); + } catch (e) { + this.request.dispatchException(e); + } + }, + + _getResponseJSON: function() { + var options = this.request.options; + if (!options.evalJSON || (options.evalJSON != 'force' && + !(this.getHeader('Content-type') || '').include('application/json')) || + this.responseText.blank()) + return null; + try { + return this.responseText.evalJSON(options.sanitizeJSON || + !this.request.isSameOrigin()); + } catch (e) { + this.request.dispatchException(e); + } + } +}); + +Ajax.Updater = Class.create(Ajax.Request, { + initialize: function($super, container, url, options) { + this.container = { + success: (container.success || container), + failure: (container.failure || (container.success ? null : container)) + }; + + options = Object.clone(options); + var onComplete = options.onComplete; + options.onComplete = (function(response, json) { + this.updateContent(response.responseText); + if (Object.isFunction(onComplete)) onComplete(response, json); + }).bind(this); + + $super(url, options); + }, + + updateContent: function(responseText) { + var receiver = this.container[this.success() ? 'success' : 'failure'], + options = this.options; + + if (!options.evalScripts) responseText = responseText.stripScripts(); + + if (receiver = $(receiver)) { + if (options.insertion) { + if (Object.isString(options.insertion)) { + var insertion = { }; insertion[options.insertion] = responseText; + receiver.insert(insertion); + } + else options.insertion(receiver, responseText); + } + else receiver.update(responseText); + } + } +}); + +Ajax.PeriodicalUpdater = Class.create(Ajax.Base, { + initialize: function($super, container, url, options) { + $super(options); + this.onComplete = this.options.onComplete; + + this.frequency = (this.options.frequency || 2); + this.decay = (this.options.decay || 1); + + this.updater = { }; + this.container = container; + this.url = url; + + this.start(); + }, + + start: function() { + this.options.onComplete = this.updateComplete.bind(this); + this.onTimerEvent(); + }, + + stop: function() { + this.updater.options.onComplete = undefined; + clearTimeout(this.timer); + (this.onComplete || Prototype.emptyFunction).apply(this, arguments); + }, + + updateComplete: function(response) { + if (this.options.decay) { + this.decay = (response.responseText == this.lastText ? + this.decay * this.options.decay : 1); + + this.lastText = response.responseText; + } + this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency); + }, + + onTimerEvent: function() { + this.updater = new Ajax.Updater(this.container, this.url, this.options); + } +}); + +(function(GLOBAL) { + + var UNDEFINED; + var SLICE = Array.prototype.slice; + + var DIV = document.createElement('div'); + + + function $(element) { + if (arguments.length > 1) { + for (var i = 0, elements = [], length = arguments.length; i < length; i++) + elements.push($(arguments[i])); + return elements; + } + + if (Object.isString(element)) + element = document.getElementById(element); + return Element.extend(element); + } + + GLOBAL.$ = $; + + + if (!GLOBAL.Node) GLOBAL.Node = {}; + + if (!GLOBAL.Node.ELEMENT_NODE) { + Object.extend(GLOBAL.Node, { + ELEMENT_NODE: 1, + ATTRIBUTE_NODE: 2, + TEXT_NODE: 3, + CDATA_SECTION_NODE: 4, + ENTITY_REFERENCE_NODE: 5, + ENTITY_NODE: 6, + PROCESSING_INSTRUCTION_NODE: 7, + COMMENT_NODE: 8, + DOCUMENT_NODE: 9, + DOCUMENT_TYPE_NODE: 10, + DOCUMENT_FRAGMENT_NODE: 11, + NOTATION_NODE: 12 + }); + } + + var ELEMENT_CACHE = {}; + + function shouldUseCreationCache(tagName, attributes) { + if (tagName === 'select') return false; + if ('type' in attributes) return false; + return true; + } + + var HAS_EXTENDED_CREATE_ELEMENT_SYNTAX = (function(){ + try { + var el = document.createElement(''); + return el.tagName.toLowerCase() === 'input' && el.name === 'x'; + } + catch(err) { + return false; + } + })(); + + + var oldElement = GLOBAL.Element; + function Element(tagName, attributes) { + attributes = attributes || {}; + tagName = tagName.toLowerCase(); + + if (HAS_EXTENDED_CREATE_ELEMENT_SYNTAX && attributes.name) { + tagName = '<' + tagName + ' name="' + attributes.name + '">'; + delete attributes.name; + return Element.writeAttribute(document.createElement(tagName), attributes); + } + + if (!ELEMENT_CACHE[tagName]) + ELEMENT_CACHE[tagName] = Element.extend(document.createElement(tagName)); + + var node = shouldUseCreationCache(tagName, attributes) ? + ELEMENT_CACHE[tagName].cloneNode(false) : document.createElement(tagName); + + return Element.writeAttribute(node, attributes); + } + + GLOBAL.Element = Element; + + Object.extend(GLOBAL.Element, oldElement || {}); + if (oldElement) GLOBAL.Element.prototype = oldElement.prototype; + + Element.Methods = { ByTag: {}, Simulated: {} }; + + var methods = {}; + + var INSPECT_ATTRIBUTES = { id: 'id', className: 'class' }; + function inspect(element) { + element = $(element); + var result = '<' + element.tagName.toLowerCase(); + + var attribute, value; + for (var property in INSPECT_ATTRIBUTES) { + attribute = INSPECT_ATTRIBUTES[property]; + value = (element[property] || '').toString(); + if (value) result += ' ' + attribute + '=' + value.inspect(true); + } + + return result + '>'; + } + + methods.inspect = inspect; + + + function visible(element) { + return $(element).getStyle('display') !== 'none'; + } + + function toggle(element, bool) { + element = $(element); + if (typeof bool !== 'boolean') + bool = !Element.visible(element); + Element[bool ? 'show' : 'hide'](element); + + return element; + } + + function hide(element) { + element = $(element); + element.style.display = 'none'; + return element; + } + + function show(element) { + element = $(element); + element.style.display = ''; + return element; + } + + + Object.extend(methods, { + visible: visible, + toggle: toggle, + hide: hide, + show: show + }); + + + function remove(element) { + element = $(element); + element.parentNode.removeChild(element); + return element; + } + + var SELECT_ELEMENT_INNERHTML_BUGGY = (function(){ + var el = document.createElement("select"), + isBuggy = true; + el.innerHTML = ""; + if (el.options && el.options[0]) { + isBuggy = el.options[0].nodeName.toUpperCase() !== "OPTION"; + } + el = null; + return isBuggy; + })(); + + var TABLE_ELEMENT_INNERHTML_BUGGY = (function(){ + try { + var el = document.createElement("table"); + if (el && el.tBodies) { + el.innerHTML = "test"; + var isBuggy = typeof el.tBodies[0] == "undefined"; + el = null; + return isBuggy; + } + } catch (e) { + return true; + } + })(); + + var LINK_ELEMENT_INNERHTML_BUGGY = (function() { + try { + var el = document.createElement('div'); + el.innerHTML = ""; + var isBuggy = (el.childNodes.length === 0); + el = null; + return isBuggy; + } catch(e) { + return true; + } + })(); + + var ANY_INNERHTML_BUGGY = SELECT_ELEMENT_INNERHTML_BUGGY || + TABLE_ELEMENT_INNERHTML_BUGGY || LINK_ELEMENT_INNERHTML_BUGGY; + + var SCRIPT_ELEMENT_REJECTS_TEXTNODE_APPENDING = (function () { + var s = document.createElement("script"), + isBuggy = false; + try { + s.appendChild(document.createTextNode("")); + isBuggy = !s.firstChild || + s.firstChild && s.firstChild.nodeType !== 3; + } catch (e) { + isBuggy = true; + } + s = null; + return isBuggy; + })(); + + function update(element, content) { + element = $(element); + + var descendants = element.getElementsByTagName('*'), + i = descendants.length; + while (i--) purgeElement(descendants[i]); + + if (content && content.toElement) + content = content.toElement(); + + if (Object.isElement(content)) + return element.update().insert(content); + + + content = Object.toHTML(content); + var tagName = element.tagName.toUpperCase(); + + if (tagName === 'SCRIPT' && SCRIPT_ELEMENT_REJECTS_TEXTNODE_APPENDING) { + element.text = content; + return element; + } + + if (ANY_INNERHTML_BUGGY) { + if (tagName in INSERTION_TRANSLATIONS.tags) { + while (element.firstChild) + element.removeChild(element.firstChild); + + var nodes = getContentFromAnonymousElement(tagName, content.stripScripts()); + for (var i = 0, node; node = nodes[i]; i++) + element.appendChild(node); + + } else if (LINK_ELEMENT_INNERHTML_BUGGY && Object.isString(content) && content.indexOf(' -1) { + while (element.firstChild) + element.removeChild(element.firstChild); + + var nodes = getContentFromAnonymousElement(tagName, + content.stripScripts(), true); + + for (var i = 0, node; node = nodes[i]; i++) + element.appendChild(node); + } else { + element.innerHTML = content.stripScripts(); + } + } else { + element.innerHTML = content.stripScripts(); + } + + content.evalScripts.bind(content).defer(); + return element; + } + + function replace(element, content) { + element = $(element); + + if (content && content.toElement) { + content = content.toElement(); + } else if (!Object.isElement(content)) { + content = Object.toHTML(content); + var range = element.ownerDocument.createRange(); + range.selectNode(element); + content.evalScripts.bind(content).defer(); + content = range.createContextualFragment(content.stripScripts()); + } + + element.parentNode.replaceChild(content, element); + return element; + } + + var INSERTION_TRANSLATIONS = { + before: function(element, node) { + element.parentNode.insertBefore(node, element); + }, + top: function(element, node) { + element.insertBefore(node, element.firstChild); + }, + bottom: function(element, node) { + element.appendChild(node); + }, + after: function(element, node) { + element.parentNode.insertBefore(node, element.nextSibling); + }, + + tags: { + TABLE: ['', '
      ', 1], + TBODY: ['', '
      ', 2], + TR: ['', '
      ', 3], + TD: ['
      ', '
      ', 4], + SELECT: ['', 1] + } + }; + + var tags = INSERTION_TRANSLATIONS.tags; + + Object.extend(tags, { + THEAD: tags.TBODY, + TFOOT: tags.TBODY, + TH: tags.TD + }); + + function replace_IE(element, content) { + element = $(element); + if (content && content.toElement) + content = content.toElement(); + if (Object.isElement(content)) { + element.parentNode.replaceChild(content, element); + return element; + } + + content = Object.toHTML(content); + var parent = element.parentNode, tagName = parent.tagName.toUpperCase(); + + if (tagName in INSERTION_TRANSLATIONS.tags) { + var nextSibling = Element.next(element); + var fragments = getContentFromAnonymousElement( + tagName, content.stripScripts()); + + parent.removeChild(element); + + var iterator; + if (nextSibling) + iterator = function(node) { parent.insertBefore(node, nextSibling) }; + else + iterator = function(node) { parent.appendChild(node); } + + fragments.each(iterator); + } else { + element.outerHTML = content.stripScripts(); + } + + content.evalScripts.bind(content).defer(); + return element; + } + + if ('outerHTML' in document.documentElement) + replace = replace_IE; + + function isContent(content) { + if (Object.isUndefined(content) || content === null) return false; + + if (Object.isString(content) || Object.isNumber(content)) return true; + if (Object.isElement(content)) return true; + if (content.toElement || content.toHTML) return true; + + return false; + } + + function insertContentAt(element, content, position) { + position = position.toLowerCase(); + var method = INSERTION_TRANSLATIONS[position]; + + if (content && content.toElement) content = content.toElement(); + if (Object.isElement(content)) { + method(element, content); + return element; + } + + content = Object.toHTML(content); + var tagName = ((position === 'before' || position === 'after') ? + element.parentNode : element).tagName.toUpperCase(); + + var childNodes = getContentFromAnonymousElement(tagName, content.stripScripts()); + + if (position === 'top' || position === 'after') childNodes.reverse(); + + for (var i = 0, node; node = childNodes[i]; i++) + method(element, node); + + content.evalScripts.bind(content).defer(); + } + + function insert(element, insertions) { + element = $(element); + + if (isContent(insertions)) + insertions = { bottom: insertions }; + + for (var position in insertions) + insertContentAt(element, insertions[position], position); + + return element; + } + + function wrap(element, wrapper, attributes) { + element = $(element); + + if (Object.isElement(wrapper)) { + $(wrapper).writeAttribute(attributes || {}); + } else if (Object.isString(wrapper)) { + wrapper = new Element(wrapper, attributes); + } else { + wrapper = new Element('div', wrapper); + } + + if (element.parentNode) + element.parentNode.replaceChild(wrapper, element); + + wrapper.appendChild(element); + + return wrapper; + } + + function cleanWhitespace(element) { + element = $(element); + var node = element.firstChild; + + while (node) { + var nextNode = node.nextSibling; + if (node.nodeType === Node.TEXT_NODE && !/\S/.test(node.nodeValue)) + element.removeChild(node); + node = nextNode; + } + return element; + } + + function empty(element) { + return $(element).innerHTML.blank(); + } + + function getContentFromAnonymousElement(tagName, html, force) { + var t = INSERTION_TRANSLATIONS.tags[tagName], div = DIV; + + var workaround = !!t; + if (!workaround && force) { + workaround = true; + t = ['', '', 0]; + } + + if (workaround) { + div.innerHTML = ' ' + t[0] + html + t[1]; + div.removeChild(div.firstChild); + for (var i = t[2]; i--; ) + div = div.firstChild; + } else { + div.innerHTML = html; + } + + return $A(div.childNodes); + } + + function clone(element, deep) { + if (!(element = $(element))) return; + var clone = element.cloneNode(deep); + if (!HAS_UNIQUE_ID_PROPERTY) { + clone._prototypeUID = UNDEFINED; + if (deep) { + var descendants = Element.select(clone, '*'), + i = descendants.length; + while (i--) + descendants[i]._prototypeUID = UNDEFINED; + } + } + return Element.extend(clone); + } + + function purgeElement(element) { + var uid = getUniqueElementID(element); + if (uid) { + Element.stopObserving(element); + if (!HAS_UNIQUE_ID_PROPERTY) + element._prototypeUID = UNDEFINED; + delete Element.Storage[uid]; + } + } + + function purgeCollection(elements) { + var i = elements.length; + while (i--) + purgeElement(elements[i]); + } + + function purgeCollection_IE(elements) { + var i = elements.length, element, uid; + while (i--) { + element = elements[i]; + uid = getUniqueElementID(element); + delete Element.Storage[uid]; + delete Event.cache[uid]; + } + } + + if (HAS_UNIQUE_ID_PROPERTY) { + purgeCollection = purgeCollection_IE; + } + + + function purge(element) { + if (!(element = $(element))) return; + purgeElement(element); + + var descendants = element.getElementsByTagName('*'), + i = descendants.length; + + while (i--) purgeElement(descendants[i]); + + return null; + } + + Object.extend(methods, { + remove: remove, + update: update, + replace: replace, + insert: insert, + wrap: wrap, + cleanWhitespace: cleanWhitespace, + empty: empty, + clone: clone, + purge: purge + }); + + + + function recursivelyCollect(element, property, maximumLength) { + element = $(element); + maximumLength = maximumLength || -1; + var elements = []; + + while (element = element[property]) { + if (element.nodeType === Node.ELEMENT_NODE) + elements.push(Element.extend(element)); + + if (elements.length === maximumLength) break; + } + + return elements; + } + + + function ancestors(element) { + return recursivelyCollect(element, 'parentNode'); + } + + function descendants(element) { + return Element.select(element, '*'); + } + + function firstDescendant(element) { + element = $(element).firstChild; + while (element && element.nodeType !== Node.ELEMENT_NODE) + element = element.nextSibling; + + return $(element); + } + + function immediateDescendants(element) { + var results = [], child = $(element).firstChild; + + while (child) { + if (child.nodeType === Node.ELEMENT_NODE) + results.push(Element.extend(child)); + + child = child.nextSibling; + } + + return results; + } + + function previousSiblings(element) { + return recursivelyCollect(element, 'previousSibling'); + } + + function nextSiblings(element) { + return recursivelyCollect(element, 'nextSibling'); + } + + function siblings(element) { + element = $(element); + var previous = previousSiblings(element), + next = nextSiblings(element); + return previous.reverse().concat(next); + } + + function match(element, selector) { + element = $(element); + + if (Object.isString(selector)) + return Prototype.Selector.match(element, selector); + + return selector.match(element); + } + + + function _recursivelyFind(element, property, expression, index) { + element = $(element), expression = expression || 0, index = index || 0; + if (Object.isNumber(expression)) { + index = expression, expression = null; + } + + while (element = element[property]) { + if (element.nodeType !== 1) continue; + if (expression && !Prototype.Selector.match(element, expression)) + continue; + if (--index >= 0) continue; + + return Element.extend(element); + } + } + + + function up(element, expression, index) { + element = $(element); + + if (arguments.length === 1) return $(element.parentNode); + return _recursivelyFind(element, 'parentNode', expression, index); + } + + function down(element, expression, index) { + if (arguments.length === 1) return firstDescendant(element); + element = $(element), expression = expression || 0, index = index || 0; + + if (Object.isNumber(expression)) + index = expression, expression = '*'; + + var node = Prototype.Selector.select(expression, element)[index]; + return Element.extend(node); + } + + function previous(element, expression, index) { + return _recursivelyFind(element, 'previousSibling', expression, index); + } + + function next(element, expression, index) { + return _recursivelyFind(element, 'nextSibling', expression, index); + } + + function select(element) { + element = $(element); + var expressions = SLICE.call(arguments, 1).join(', '); + return Prototype.Selector.select(expressions, element); + } + + function adjacent(element) { + element = $(element); + var expressions = SLICE.call(arguments, 1).join(', '); + var siblings = Element.siblings(element), results = []; + for (var i = 0, sibling; sibling = siblings[i]; i++) { + if (Prototype.Selector.match(sibling, expressions)) + results.push(sibling); + } + + return results; + } + + function descendantOf_DOM(element, ancestor) { + element = $(element), ancestor = $(ancestor); + if (!element || !ancestor) return false; + while (element = element.parentNode) + if (element === ancestor) return true; + return false; + } + + function descendantOf_contains(element, ancestor) { + element = $(element), ancestor = $(ancestor); + if (!element || !ancestor) return false; + if (!ancestor.contains) return descendantOf_DOM(element, ancestor); + return ancestor.contains(element) && ancestor !== element; + } + + function descendantOf_compareDocumentPosition(element, ancestor) { + element = $(element), ancestor = $(ancestor); + if (!element || !ancestor) return false; + return (element.compareDocumentPosition(ancestor) & 8) === 8; + } + + var descendantOf; + if (DIV.compareDocumentPosition) { + descendantOf = descendantOf_compareDocumentPosition; + } else if (DIV.contains) { + descendantOf = descendantOf_contains; + } else { + descendantOf = descendantOf_DOM; + } + + + Object.extend(methods, { + recursivelyCollect: recursivelyCollect, + ancestors: ancestors, + descendants: descendants, + firstDescendant: firstDescendant, + immediateDescendants: immediateDescendants, + previousSiblings: previousSiblings, + nextSiblings: nextSiblings, + siblings: siblings, + match: match, + up: up, + down: down, + previous: previous, + next: next, + select: select, + adjacent: adjacent, + descendantOf: descendantOf, + + getElementsBySelector: select, + + childElements: immediateDescendants + }); + + + var idCounter = 1; + function identify(element) { + element = $(element); + var id = Element.readAttribute(element, 'id'); + if (id) return id; + + do { id = 'anonymous_element_' + idCounter++ } while ($(id)); + + Element.writeAttribute(element, 'id', id); + return id; + } + + + function readAttribute(element, name) { + return $(element).getAttribute(name); + } + + function readAttribute_IE(element, name) { + element = $(element); + + var table = ATTRIBUTE_TRANSLATIONS.read; + if (table.values[name]) + return table.values[name](element, name); + + if (table.names[name]) name = table.names[name]; + + if (name.include(':')) { + if (!element.attributes || !element.attributes[name]) return null; + return element.attributes[name].value; + } + + return element.getAttribute(name); + } + + function readAttribute_Opera(element, name) { + if (name === 'title') return element.title; + return element.getAttribute(name); + } + + var PROBLEMATIC_ATTRIBUTE_READING = (function() { + DIV.setAttribute('onclick', []); + var value = DIV.getAttribute('onclick'); + var isFunction = Object.isArray(value); + DIV.removeAttribute('onclick'); + return isFunction; + })(); + + if (PROBLEMATIC_ATTRIBUTE_READING) { + readAttribute = readAttribute_IE; + } else if (Prototype.Browser.Opera) { + readAttribute = readAttribute_Opera; + } + + + function writeAttribute(element, name, value) { + element = $(element); + var attributes = {}, table = ATTRIBUTE_TRANSLATIONS.write; + + if (typeof name === 'object') { + attributes = name; + } else { + attributes[name] = Object.isUndefined(value) ? true : value; + } + + for (var attr in attributes) { + name = table.names[attr] || attr; + value = attributes[attr]; + if (table.values[attr]) { + value = table.values[attr](element, value); + if (Object.isUndefined(value)) continue; + } + if (value === false || value === null) + element.removeAttribute(name); + else if (value === true) + element.setAttribute(name, name); + else element.setAttribute(name, value); + } + + return element; + } + + var PROBLEMATIC_HAS_ATTRIBUTE_WITH_CHECKBOXES = (function () { + if (!HAS_EXTENDED_CREATE_ELEMENT_SYNTAX) { + return false; + } + var checkbox = document.createElement(''); + checkbox.checked = true; + var node = checkbox.getAttributeNode('checked'); + return !node || !node.specified; + })(); + + function hasAttribute(element, attribute) { + attribute = ATTRIBUTE_TRANSLATIONS.has[attribute] || attribute; + var node = $(element).getAttributeNode(attribute); + return !!(node && node.specified); + } + + function hasAttribute_IE(element, attribute) { + if (attribute === 'checked') { + return element.checked; + } + return hasAttribute(element, attribute); + } + + GLOBAL.Element.Methods.Simulated.hasAttribute = + PROBLEMATIC_HAS_ATTRIBUTE_WITH_CHECKBOXES ? + hasAttribute_IE : hasAttribute; + + function classNames(element) { + return new Element.ClassNames(element); + } + + var regExpCache = {}; + function getRegExpForClassName(className) { + if (regExpCache[className]) return regExpCache[className]; + + var re = new RegExp("(^|\\s+)" + className + "(\\s+|$)"); + regExpCache[className] = re; + return re; + } + + function hasClassName(element, className) { + if (!(element = $(element))) return; + + var elementClassName = element.className; + + if (elementClassName.length === 0) return false; + if (elementClassName === className) return true; + + return getRegExpForClassName(className).test(elementClassName); + } + + function addClassName(element, className) { + if (!(element = $(element))) return; + + if (!hasClassName(element, className)) + element.className += (element.className ? ' ' : '') + className; + + return element; + } + + function removeClassName(element, className) { + if (!(element = $(element))) return; + + element.className = element.className.replace( + getRegExpForClassName(className), ' ').strip(); + + return element; + } + + function toggleClassName(element, className, bool) { + if (!(element = $(element))) return; + + if (Object.isUndefined(bool)) + bool = !hasClassName(element, className); + + var method = Element[bool ? 'addClassName' : 'removeClassName']; + return method(element, className); + } + + var ATTRIBUTE_TRANSLATIONS = {}; + + var classProp = 'className', forProp = 'for'; + + DIV.setAttribute(classProp, 'x'); + if (DIV.className !== 'x') { + DIV.setAttribute('class', 'x'); + if (DIV.className === 'x') + classProp = 'class'; + } + + var LABEL = document.createElement('label'); + LABEL.setAttribute(forProp, 'x'); + if (LABEL.htmlFor !== 'x') { + LABEL.setAttribute('htmlFor', 'x'); + if (LABEL.htmlFor === 'x') + forProp = 'htmlFor'; + } + LABEL = null; + + function _getAttr(element, attribute) { + return element.getAttribute(attribute); + } + + function _getAttr2(element, attribute) { + return element.getAttribute(attribute, 2); + } + + function _getAttrNode(element, attribute) { + var node = element.getAttributeNode(attribute); + return node ? node.value : ''; + } + + function _getFlag(element, attribute) { + return $(element).hasAttribute(attribute) ? attribute : null; + } + + DIV.onclick = Prototype.emptyFunction; + var onclickValue = DIV.getAttribute('onclick'); + + var _getEv; + + if (String(onclickValue).indexOf('{') > -1) { + _getEv = function(element, attribute) { + var value = element.getAttribute(attribute); + if (!value) return null; + value = value.toString(); + value = value.split('{')[1]; + value = value.split('}')[0]; + return value.strip(); + }; + } + else if (onclickValue === '') { + _getEv = function(element, attribute) { + var value = element.getAttribute(attribute); + if (!value) return null; + return value.strip(); + }; + } + + ATTRIBUTE_TRANSLATIONS.read = { + names: { + 'class': classProp, + 'className': classProp, + 'for': forProp, + 'htmlFor': forProp + }, + + values: { + style: function(element) { + return element.style.cssText.toLowerCase(); + }, + title: function(element) { + return element.title; + } + } + }; + + ATTRIBUTE_TRANSLATIONS.write = { + names: { + className: 'class', + htmlFor: 'for', + cellpadding: 'cellPadding', + cellspacing: 'cellSpacing' + }, + + values: { + checked: function(element, value) { + value = !!value; + element.checked = value; + return value ? 'checked' : null; + }, + + style: function(element, value) { + element.style.cssText = value ? value : ''; + } + } + }; + + ATTRIBUTE_TRANSLATIONS.has = { names: {} }; + + Object.extend(ATTRIBUTE_TRANSLATIONS.write.names, + ATTRIBUTE_TRANSLATIONS.read.names); + + var CAMEL_CASED_ATTRIBUTE_NAMES = $w('colSpan rowSpan vAlign dateTime ' + + 'accessKey tabIndex encType maxLength readOnly longDesc frameBorder'); + + for (var i = 0, attr; attr = CAMEL_CASED_ATTRIBUTE_NAMES[i]; i++) { + ATTRIBUTE_TRANSLATIONS.write.names[attr.toLowerCase()] = attr; + ATTRIBUTE_TRANSLATIONS.has.names[attr.toLowerCase()] = attr; + } + + Object.extend(ATTRIBUTE_TRANSLATIONS.read.values, { + href: _getAttr2, + src: _getAttr2, + type: _getAttr, + action: _getAttrNode, + disabled: _getFlag, + checked: _getFlag, + readonly: _getFlag, + multiple: _getFlag, + onload: _getEv, + onunload: _getEv, + onclick: _getEv, + ondblclick: _getEv, + onmousedown: _getEv, + onmouseup: _getEv, + onmouseover: _getEv, + onmousemove: _getEv, + onmouseout: _getEv, + onfocus: _getEv, + onblur: _getEv, + onkeypress: _getEv, + onkeydown: _getEv, + onkeyup: _getEv, + onsubmit: _getEv, + onreset: _getEv, + onselect: _getEv, + onchange: _getEv + }); + + + Object.extend(methods, { + identify: identify, + readAttribute: readAttribute, + writeAttribute: writeAttribute, + classNames: classNames, + hasClassName: hasClassName, + addClassName: addClassName, + removeClassName: removeClassName, + toggleClassName: toggleClassName + }); + + + function normalizeStyleName(style) { + if (style === 'float' || style === 'styleFloat') + return 'cssFloat'; + return style.camelize(); + } + + function normalizeStyleName_IE(style) { + if (style === 'float' || style === 'cssFloat') + return 'styleFloat'; + return style.camelize(); + } + + function setStyle(element, styles) { + element = $(element); + var elementStyle = element.style, match; + + if (Object.isString(styles)) { + elementStyle.cssText += ';' + styles; + if (styles.include('opacity')) { + var opacity = styles.match(/opacity:\s*(\d?\.?\d*)/)[1]; + Element.setOpacity(element, opacity); + } + return element; + } + + for (var property in styles) { + if (property === 'opacity') { + Element.setOpacity(element, styles[property]); + } else { + var value = styles[property]; + if (property === 'float' || property === 'cssFloat') { + property = Object.isUndefined(elementStyle.styleFloat) ? + 'cssFloat' : 'styleFloat'; + } + elementStyle[property] = value; + } + } + + return element; + } + + + function getStyle(element, style) { + element = $(element); + style = normalizeStyleName(style); + + var value = element.style[style]; + if (!value || value === 'auto') { + var css = document.defaultView.getComputedStyle(element, null); + value = css ? css[style] : null; + } + + if (style === 'opacity') return value ? parseFloat(value) : 1.0; + return value === 'auto' ? null : value; + } + + function getStyle_Opera(element, style) { + switch (style) { + case 'height': case 'width': + if (!Element.visible(element)) return null; + + var dim = parseInt(getStyle(element, style), 10); + + if (dim !== element['offset' + style.capitalize()]) + return dim + 'px'; + + return Element.measure(element, style); + + default: return getStyle(element, style); + } + } + + function getStyle_IE(element, style) { + element = $(element); + style = normalizeStyleName_IE(style); + + var value = element.style[style]; + if (!value && element.currentStyle) { + value = element.currentStyle[style]; + } + + if (style === 'opacity') { + if (!STANDARD_CSS_OPACITY_SUPPORTED) + return getOpacity_IE(element); + else return value ? parseFloat(value) : 1.0; + } + + if (value === 'auto') { + if ((style === 'width' || style === 'height') && Element.visible(element)) + return Element.measure(element, style) + 'px'; + return null; + } + + return value; + } + + function stripAlphaFromFilter_IE(filter) { + return (filter || '').replace(/alpha\([^\)]*\)/gi, ''); + } + + function hasLayout_IE(element) { + if (!element.currentStyle || !element.currentStyle.hasLayout) + element.style.zoom = 1; + return element; + } + + var STANDARD_CSS_OPACITY_SUPPORTED = (function() { + DIV.style.cssText = "opacity:.55"; + return /^0.55/.test(DIV.style.opacity); + })(); + + function setOpacity(element, value) { + element = $(element); + if (value == 1 || value === '') value = ''; + else if (value < 0.00001) value = 0; + element.style.opacity = value; + return element; + } + + function setOpacity_IE(element, value) { + if (STANDARD_CSS_OPACITY_SUPPORTED) + return setOpacity(element, value); + + element = hasLayout_IE($(element)); + var filter = Element.getStyle(element, 'filter'), + style = element.style; + + if (value == 1 || value === '') { + filter = stripAlphaFromFilter_IE(filter); + if (filter) style.filter = filter; + else style.removeAttribute('filter'); + return element; + } + + if (value < 0.00001) value = 0; + + style.filter = stripAlphaFromFilter_IE(filter) + + ' alpha(opacity=' + (value * 100) + ')'; + + return element; + } + + + function getOpacity(element) { + return Element.getStyle(element, 'opacity'); + } + + function getOpacity_IE(element) { + if (STANDARD_CSS_OPACITY_SUPPORTED) + return getOpacity(element); + + var filter = Element.getStyle(element, 'filter'); + if (filter.length === 0) return 1.0; + var match = (filter || '').match(/alpha\(opacity=(.*)\)/i); + if (match && match[1]) return parseFloat(match[1]) / 100; + return 1.0; + } + + + Object.extend(methods, { + setStyle: setStyle, + getStyle: getStyle, + setOpacity: setOpacity, + getOpacity: getOpacity + }); + + if ('styleFloat' in DIV.style) { + methods.getStyle = getStyle_IE; + methods.setOpacity = setOpacity_IE; + methods.getOpacity = getOpacity_IE; + } + + var UID = 0; + + GLOBAL.Element.Storage = { UID: 1 }; + + function getUniqueElementID(element) { + if (element === window) return 0; + + if (typeof element._prototypeUID === 'undefined') + element._prototypeUID = Element.Storage.UID++; + return element._prototypeUID; + } + + function getUniqueElementID_IE(element) { + if (element === window) return 0; + if (element == document) return 1; + return element.uniqueID; + } + + var HAS_UNIQUE_ID_PROPERTY = ('uniqueID' in DIV); + if (HAS_UNIQUE_ID_PROPERTY) + getUniqueElementID = getUniqueElementID_IE; + + function getStorage(element) { + if (!(element = $(element))) return; + + var uid = getUniqueElementID(element); + + if (!Element.Storage[uid]) + Element.Storage[uid] = $H(); + + return Element.Storage[uid]; + } + + function store(element, key, value) { + if (!(element = $(element))) return; + var storage = getStorage(element); + if (arguments.length === 2) { + storage.update(key); + } else { + storage.set(key, value); + } + return element; + } + + function retrieve(element, key, defaultValue) { + if (!(element = $(element))) return; + var storage = getStorage(element), value = storage.get(key); + + if (Object.isUndefined(value)) { + storage.set(key, defaultValue); + value = defaultValue; + } + + return value; + } + + + Object.extend(methods, { + getStorage: getStorage, + store: store, + retrieve: retrieve + }); + + + var Methods = {}, ByTag = Element.Methods.ByTag, + F = Prototype.BrowserFeatures; + + if (!F.ElementExtensions && ('__proto__' in DIV)) { + GLOBAL.HTMLElement = {}; + GLOBAL.HTMLElement.prototype = DIV['__proto__']; + F.ElementExtensions = true; + } + + function checkElementPrototypeDeficiency(tagName) { + if (typeof window.Element === 'undefined') return false; + if (!HAS_EXTENDED_CREATE_ELEMENT_SYNTAX) return false; + var proto = window.Element.prototype; + if (proto) { + var id = '_' + (Math.random() + '').slice(2), + el = document.createElement(tagName); + proto[id] = 'x'; + var isBuggy = (el[id] !== 'x'); + delete proto[id]; + el = null; + return isBuggy; + } + + return false; + } + + var HTMLOBJECTELEMENT_PROTOTYPE_BUGGY = + checkElementPrototypeDeficiency('object'); + + function extendElementWith(element, methods) { + for (var property in methods) { + var value = methods[property]; + if (Object.isFunction(value) && !(property in element)) + element[property] = value.methodize(); + } + } + + var EXTENDED = {}; + function elementIsExtended(element) { + var uid = getUniqueElementID(element); + return (uid in EXTENDED); + } + + function extend(element) { + if (!element || elementIsExtended(element)) return element; + if (element.nodeType !== Node.ELEMENT_NODE || element == window) + return element; + + var methods = Object.clone(Methods), + tagName = element.tagName.toUpperCase(); + + if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]); + + extendElementWith(element, methods); + EXTENDED[getUniqueElementID(element)] = true; + return element; + } + + function extend_IE8(element) { + if (!element || elementIsExtended(element)) return element; + + var t = element.tagName; + if (t && (/^(?:object|applet|embed)$/i.test(t))) { + extendElementWith(element, Element.Methods); + extendElementWith(element, Element.Methods.Simulated); + extendElementWith(element, Element.Methods.ByTag[t.toUpperCase()]); + } + + return element; + } + + if (F.SpecificElementExtensions) { + extend = HTMLOBJECTELEMENT_PROTOTYPE_BUGGY ? extend_IE8 : Prototype.K; + } + + function addMethodsToTagName(tagName, methods) { + tagName = tagName.toUpperCase(); + if (!ByTag[tagName]) ByTag[tagName] = {}; + Object.extend(ByTag[tagName], methods); + } + + function mergeMethods(destination, methods, onlyIfAbsent) { + if (Object.isUndefined(onlyIfAbsent)) onlyIfAbsent = false; + for (var property in methods) { + var value = methods[property]; + if (!Object.isFunction(value)) continue; + if (!onlyIfAbsent || !(property in destination)) + destination[property] = value.methodize(); + } + } + + function findDOMClass(tagName) { + var klass; + var trans = { + "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph", + "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList", + "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading", + "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote", + "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION": + "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD": + "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR": + "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET": + "FrameSet", "IFRAME": "IFrame" + }; + if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName + 'Element'; + if (window[klass]) return window[klass]; + klass = 'HTML' + tagName.capitalize() + 'Element'; + if (window[klass]) return window[klass]; + + var element = document.createElement(tagName), + proto = element['__proto__'] || element.constructor.prototype; + + element = null; + return proto; + } + + function addMethods(methods) { + if (arguments.length === 0) addFormMethods(); + + if (arguments.length === 2) { + var tagName = methods; + methods = arguments[1]; + } + + if (!tagName) { + Object.extend(Element.Methods, methods || {}); + } else { + if (Object.isArray(tagName)) { + for (var i = 0, tag; tag = tagName[i]; i++) + addMethodsToTagName(tag, methods); + } else { + addMethodsToTagName(tagName, methods); + } + } + + var ELEMENT_PROTOTYPE = window.HTMLElement ? HTMLElement.prototype : + Element.prototype; + + if (F.ElementExtensions) { + mergeMethods(ELEMENT_PROTOTYPE, Element.Methods); + mergeMethods(ELEMENT_PROTOTYPE, Element.Methods.Simulated, true); + } + + if (F.SpecificElementExtensions) { + for (var tag in Element.Methods.ByTag) { + var klass = findDOMClass(tag); + if (Object.isUndefined(klass)) continue; + mergeMethods(klass.prototype, ByTag[tag]); + } + } + + Object.extend(Element, Element.Methods); + Object.extend(Element, Element.Methods.Simulated); + delete Element.ByTag; + delete Element.Simulated; + + Element.extend.refresh(); + + ELEMENT_CACHE = {}; + } + + Object.extend(GLOBAL.Element, { + extend: extend, + addMethods: addMethods + }); + + if (extend === Prototype.K) { + GLOBAL.Element.extend.refresh = Prototype.emptyFunction; + } else { + GLOBAL.Element.extend.refresh = function() { + if (Prototype.BrowserFeatures.ElementExtensions) return; + Object.extend(Methods, Element.Methods); + Object.extend(Methods, Element.Methods.Simulated); + + EXTENDED = {}; + }; + } + + function addFormMethods() { + Object.extend(Form, Form.Methods); + Object.extend(Form.Element, Form.Element.Methods); + Object.extend(Element.Methods.ByTag, { + "FORM": Object.clone(Form.Methods), + "INPUT": Object.clone(Form.Element.Methods), + "SELECT": Object.clone(Form.Element.Methods), + "TEXTAREA": Object.clone(Form.Element.Methods), + "BUTTON": Object.clone(Form.Element.Methods) + }); + } + + Element.addMethods(methods); + + function destroyCache_IE() { + DIV = null; + ELEMENT_CACHE = null; + } + + if (window.attachEvent) + window.attachEvent('onunload', destroyCache_IE); + +})(this); +(function() { + + function toDecimal(pctString) { + var match = pctString.match(/^(\d+)%?$/i); + if (!match) return null; + return (Number(match[1]) / 100); + } + + function getRawStyle(element, style) { + element = $(element); + + var value = element.style[style]; + if (!value || value === 'auto') { + var css = document.defaultView.getComputedStyle(element, null); + value = css ? css[style] : null; + } + + if (style === 'opacity') return value ? parseFloat(value) : 1.0; + return value === 'auto' ? null : value; + } + + function getRawStyle_IE(element, style) { + var value = element.style[style]; + if (!value && element.currentStyle) { + value = element.currentStyle[style]; + } + return value; + } + + function getContentWidth(element, context) { + var boxWidth = element.offsetWidth; + + var bl = getPixelValue(element, 'borderLeftWidth', context) || 0; + var br = getPixelValue(element, 'borderRightWidth', context) || 0; + var pl = getPixelValue(element, 'paddingLeft', context) || 0; + var pr = getPixelValue(element, 'paddingRight', context) || 0; + + return boxWidth - bl - br - pl - pr; + } + + if (!Object.isUndefined(document.documentElement.currentStyle) && !Prototype.Browser.Opera) { + getRawStyle = getRawStyle_IE; + } + + + function getPixelValue(value, property, context) { + var element = null; + if (Object.isElement(value)) { + element = value; + value = getRawStyle(element, property); + } + + if (value === null || Object.isUndefined(value)) { + return null; + } + + if ((/^(?:-)?\d+(\.\d+)?(px)?$/i).test(value)) { + return window.parseFloat(value); + } + + var isPercentage = value.include('%'), isViewport = (context === document.viewport); + + if (/\d/.test(value) && element && element.runtimeStyle && !(isPercentage && isViewport)) { + var style = element.style.left, rStyle = element.runtimeStyle.left; + element.runtimeStyle.left = element.currentStyle.left; + element.style.left = value || 0; + value = element.style.pixelLeft; + element.style.left = style; + element.runtimeStyle.left = rStyle; + + return value; + } + + if (element && isPercentage) { + context = context || element.parentNode; + var decimal = toDecimal(value), whole = null; + + var isHorizontal = property.include('left') || property.include('right') || + property.include('width'); + + var isVertical = property.include('top') || property.include('bottom') || + property.include('height'); + + if (context === document.viewport) { + if (isHorizontal) { + whole = document.viewport.getWidth(); + } else if (isVertical) { + whole = document.viewport.getHeight(); + } + } else { + if (isHorizontal) { + whole = $(context).measure('width'); + } else if (isVertical) { + whole = $(context).measure('height'); + } + } + + return (whole === null) ? 0 : whole * decimal; + } + + return 0; + } + + function toCSSPixels(number) { + if (Object.isString(number) && number.endsWith('px')) + return number; + return number + 'px'; + } + + function isDisplayed(element) { + while (element && element.parentNode) { + var display = element.getStyle('display'); + if (display === 'none') { + return false; + } + element = $(element.parentNode); + } + return true; + } + + var hasLayout = Prototype.K; + if ('currentStyle' in document.documentElement) { + hasLayout = function(element) { + if (!element.currentStyle.hasLayout) { + element.style.zoom = 1; + } + return element; + }; + } + + function cssNameFor(key) { + if (key.include('border')) key = key + '-width'; + return key.camelize(); + } + + Element.Layout = Class.create(Hash, { + initialize: function($super, element, preCompute) { + $super(); + this.element = $(element); + + Element.Layout.PROPERTIES.each( function(property) { + this._set(property, null); + }, this); + + if (preCompute) { + this._preComputing = true; + this._begin(); + Element.Layout.PROPERTIES.each( this._compute, this ); + this._end(); + this._preComputing = false; + } + }, + + _set: function(property, value) { + return Hash.prototype.set.call(this, property, value); + }, + + set: function(property, value) { + throw "Properties of Element.Layout are read-only."; + }, + + get: function($super, property) { + var value = $super(property); + return value === null ? this._compute(property) : value; + }, + + _begin: function() { + if (this._isPrepared()) return; + + var element = this.element; + if (isDisplayed(element)) { + this._setPrepared(true); + return; + } + + + var originalStyles = { + position: element.style.position || '', + width: element.style.width || '', + visibility: element.style.visibility || '', + display: element.style.display || '' + }; + + element.store('prototype_original_styles', originalStyles); + + var position = getRawStyle(element, 'position'), width = element.offsetWidth; + + if (width === 0 || width === null) { + element.style.display = 'block'; + width = element.offsetWidth; + } + + var context = (position === 'fixed') ? document.viewport : + element.parentNode; + + var tempStyles = { + visibility: 'hidden', + display: 'block' + }; + + if (position !== 'fixed') tempStyles.position = 'absolute'; + + element.setStyle(tempStyles); + + var positionedWidth = element.offsetWidth, newWidth; + if (width && (positionedWidth === width)) { + newWidth = getContentWidth(element, context); + } else if (position === 'absolute' || position === 'fixed') { + newWidth = getContentWidth(element, context); + } else { + var parent = element.parentNode, pLayout = $(parent).getLayout(); + + newWidth = pLayout.get('width') - + this.get('margin-left') - + this.get('border-left') - + this.get('padding-left') - + this.get('padding-right') - + this.get('border-right') - + this.get('margin-right'); + } + + element.setStyle({ width: newWidth + 'px' }); + + this._setPrepared(true); + }, + + _end: function() { + var element = this.element; + var originalStyles = element.retrieve('prototype_original_styles'); + element.store('prototype_original_styles', null); + element.setStyle(originalStyles); + this._setPrepared(false); + }, + + _compute: function(property) { + var COMPUTATIONS = Element.Layout.COMPUTATIONS; + if (!(property in COMPUTATIONS)) { + throw "Property not found."; + } + + return this._set(property, COMPUTATIONS[property].call(this, this.element)); + }, + + _isPrepared: function() { + return this.element.retrieve('prototype_element_layout_prepared', false); + }, + + _setPrepared: function(bool) { + return this.element.store('prototype_element_layout_prepared', bool); + }, + + toObject: function() { + var args = $A(arguments); + var keys = (args.length === 0) ? Element.Layout.PROPERTIES : + args.join(' ').split(' '); + var obj = {}; + keys.each( function(key) { + if (!Element.Layout.PROPERTIES.include(key)) return; + var value = this.get(key); + if (value != null) obj[key] = value; + }, this); + return obj; + }, + + toHash: function() { + var obj = this.toObject.apply(this, arguments); + return new Hash(obj); + }, + + toCSS: function() { + var args = $A(arguments); + var keys = (args.length === 0) ? Element.Layout.PROPERTIES : + args.join(' ').split(' '); + var css = {}; + + keys.each( function(key) { + if (!Element.Layout.PROPERTIES.include(key)) return; + if (Element.Layout.COMPOSITE_PROPERTIES.include(key)) return; + + var value = this.get(key); + if (value != null) css[cssNameFor(key)] = value + 'px'; + }, this); + return css; + }, + + inspect: function() { + return "#"; + } + }); + + Object.extend(Element.Layout, { + PROPERTIES: $w('height width top left right bottom border-left border-right border-top border-bottom padding-left padding-right padding-top padding-bottom margin-top margin-bottom margin-left margin-right padding-box-width padding-box-height border-box-width border-box-height margin-box-width margin-box-height'), + + COMPOSITE_PROPERTIES: $w('padding-box-width padding-box-height margin-box-width margin-box-height border-box-width border-box-height'), + + COMPUTATIONS: { + 'height': function(element) { + if (!this._preComputing) this._begin(); + + var bHeight = this.get('border-box-height'); + if (bHeight <= 0) { + if (!this._preComputing) this._end(); + return 0; + } + + var bTop = this.get('border-top'), + bBottom = this.get('border-bottom'); + + var pTop = this.get('padding-top'), + pBottom = this.get('padding-bottom'); + + if (!this._preComputing) this._end(); + + return bHeight - bTop - bBottom - pTop - pBottom; + }, + + 'width': function(element) { + if (!this._preComputing) this._begin(); + + var bWidth = this.get('border-box-width'); + if (bWidth <= 0) { + if (!this._preComputing) this._end(); + return 0; + } + + var bLeft = this.get('border-left'), + bRight = this.get('border-right'); + + var pLeft = this.get('padding-left'), + pRight = this.get('padding-right'); + + if (!this._preComputing) this._end(); + return bWidth - bLeft - bRight - pLeft - pRight; + }, + + 'padding-box-height': function(element) { + var height = this.get('height'), + pTop = this.get('padding-top'), + pBottom = this.get('padding-bottom'); + + return height + pTop + pBottom; + }, + + 'padding-box-width': function(element) { + var width = this.get('width'), + pLeft = this.get('padding-left'), + pRight = this.get('padding-right'); + + return width + pLeft + pRight; + }, + + 'border-box-height': function(element) { + if (!this._preComputing) this._begin(); + var height = element.offsetHeight; + if (!this._preComputing) this._end(); + return height; + }, + + 'border-box-width': function(element) { + if (!this._preComputing) this._begin(); + var width = element.offsetWidth; + if (!this._preComputing) this._end(); + return width; + }, + + 'margin-box-height': function(element) { + var bHeight = this.get('border-box-height'), + mTop = this.get('margin-top'), + mBottom = this.get('margin-bottom'); + + if (bHeight <= 0) return 0; + + return bHeight + mTop + mBottom; + }, + + 'margin-box-width': function(element) { + var bWidth = this.get('border-box-width'), + mLeft = this.get('margin-left'), + mRight = this.get('margin-right'); + + if (bWidth <= 0) return 0; + + return bWidth + mLeft + mRight; + }, + + 'top': function(element) { + var offset = element.positionedOffset(); + return offset.top; + }, + + 'bottom': function(element) { + var offset = element.positionedOffset(), + parent = element.getOffsetParent(), + pHeight = parent.measure('height'); + + var mHeight = this.get('border-box-height'); + + return pHeight - mHeight - offset.top; + }, + + 'left': function(element) { + var offset = element.positionedOffset(); + return offset.left; + }, + + 'right': function(element) { + var offset = element.positionedOffset(), + parent = element.getOffsetParent(), + pWidth = parent.measure('width'); + + var mWidth = this.get('border-box-width'); + + return pWidth - mWidth - offset.left; + }, + + 'padding-top': function(element) { + return getPixelValue(element, 'paddingTop'); + }, + + 'padding-bottom': function(element) { + return getPixelValue(element, 'paddingBottom'); + }, + + 'padding-left': function(element) { + return getPixelValue(element, 'paddingLeft'); + }, + + 'padding-right': function(element) { + return getPixelValue(element, 'paddingRight'); + }, + + 'border-top': function(element) { + return getPixelValue(element, 'borderTopWidth'); + }, + + 'border-bottom': function(element) { + return getPixelValue(element, 'borderBottomWidth'); + }, + + 'border-left': function(element) { + return getPixelValue(element, 'borderLeftWidth'); + }, + + 'border-right': function(element) { + return getPixelValue(element, 'borderRightWidth'); + }, + + 'margin-top': function(element) { + return getPixelValue(element, 'marginTop'); + }, + + 'margin-bottom': function(element) { + return getPixelValue(element, 'marginBottom'); + }, + + 'margin-left': function(element) { + return getPixelValue(element, 'marginLeft'); + }, + + 'margin-right': function(element) { + return getPixelValue(element, 'marginRight'); + } + } + }); + + if ('getBoundingClientRect' in document.documentElement) { + Object.extend(Element.Layout.COMPUTATIONS, { + 'right': function(element) { + var parent = hasLayout(element.getOffsetParent()); + var rect = element.getBoundingClientRect(), + pRect = parent.getBoundingClientRect(); + + return (pRect.right - rect.right).round(); + }, + + 'bottom': function(element) { + var parent = hasLayout(element.getOffsetParent()); + var rect = element.getBoundingClientRect(), + pRect = parent.getBoundingClientRect(); + + return (pRect.bottom - rect.bottom).round(); + } + }); + } + + Element.Offset = Class.create({ + initialize: function(left, top) { + this.left = left.round(); + this.top = top.round(); + + this[0] = this.left; + this[1] = this.top; + }, + + relativeTo: function(offset) { + return new Element.Offset( + this.left - offset.left, + this.top - offset.top + ); + }, + + inspect: function() { + return "#".interpolate(this); + }, + + toString: function() { + return "[#{left}, #{top}]".interpolate(this); + }, + + toArray: function() { + return [this.left, this.top]; + } + }); + + function getLayout(element, preCompute) { + return new Element.Layout(element, preCompute); + } + + function measure(element, property) { + return $(element).getLayout().get(property); + } + + function getHeight(element) { + return Element.getDimensions(element).height; + } + + function getWidth(element) { + return Element.getDimensions(element).width; + } + + function getDimensions(element) { + element = $(element); + var display = Element.getStyle(element, 'display'); + + if (display && display !== 'none') { + return { width: element.offsetWidth, height: element.offsetHeight }; + } + + var style = element.style; + var originalStyles = { + visibility: style.visibility, + position: style.position, + display: style.display + }; + + var newStyles = { + visibility: 'hidden', + display: 'block' + }; + + if (originalStyles.position !== 'fixed') + newStyles.position = 'absolute'; + + Element.setStyle(element, newStyles); + + var dimensions = { + width: element.offsetWidth, + height: element.offsetHeight + }; + + Element.setStyle(element, originalStyles); + + return dimensions; + } + + function getOffsetParent(element) { + element = $(element); + + function selfOrBody(element) { + return isHtml(element) ? $(document.body) : $(element); + } + + if (isDocument(element) || isDetached(element) || isBody(element) || isHtml(element)) + return $(document.body); + + var isInline = (Element.getStyle(element, 'display') === 'inline'); + if (!isInline && element.offsetParent) return selfOrBody(element.offsetParent); + + while ((element = element.parentNode) && element !== document.body) { + if (Element.getStyle(element, 'position') !== 'static') { + return selfOrBody(element); + } + } + + return $(document.body); + } + + + function cumulativeOffset(element) { + element = $(element); + var valueT = 0, valueL = 0; + if (element.parentNode) { + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + } while (element); + } + return new Element.Offset(valueL, valueT); + } + + function positionedOffset(element) { + element = $(element); + + var layout = element.getLayout(); + + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + element = element.offsetParent; + if (element) { + if (isBody(element)) break; + var p = Element.getStyle(element, 'position'); + if (p !== 'static') break; + } + } while (element); + + valueL -= layout.get('margin-left'); + valueT -= layout.get('margin-top'); + + return new Element.Offset(valueL, valueT); + } + + function cumulativeScrollOffset(element) { + var valueT = 0, valueL = 0; + do { + if (element === document.body) { + var bodyScrollNode = document.documentElement || document.body.parentNode || document.body; + valueT += !Object.isUndefined(window.pageYOffset) ? window.pageYOffset : bodyScrollNode.scrollTop || 0; + valueL += !Object.isUndefined(window.pageXOffset) ? window.pageXOffset : bodyScrollNode.scrollLeft || 0; + break; + } else { + valueT += element.scrollTop || 0; + valueL += element.scrollLeft || 0; + element = element.parentNode; + } + } while (element); + return new Element.Offset(valueL, valueT); + } + + function viewportOffset(forElement) { + var valueT = 0, valueL = 0, docBody = document.body; + + forElement = $(forElement); + var element = forElement; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + if (element.offsetParent == docBody && + Element.getStyle(element, 'position') == 'absolute') break; + } while (element = element.offsetParent); + + element = forElement; + do { + if (element != docBody) { + valueT -= element.scrollTop || 0; + valueL -= element.scrollLeft || 0; + } + } while (element = element.parentNode); + return new Element.Offset(valueL, valueT); + } + + function absolutize(element) { + element = $(element); + + if (Element.getStyle(element, 'position') === 'absolute') { + return element; + } + + var offsetParent = getOffsetParent(element); + var eOffset = element.viewportOffset(), + pOffset = offsetParent.viewportOffset(); + + var offset = eOffset.relativeTo(pOffset); + var layout = element.getLayout(); + + element.store('prototype_absolutize_original_styles', { + position: element.getStyle('position'), + left: element.getStyle('left'), + top: element.getStyle('top'), + width: element.getStyle('width'), + height: element.getStyle('height') + }); + + element.setStyle({ + position: 'absolute', + top: offset.top + 'px', + left: offset.left + 'px', + width: layout.get('width') + 'px', + height: layout.get('height') + 'px' + }); + + return element; + } + + function relativize(element) { + element = $(element); + if (Element.getStyle(element, 'position') === 'relative') { + return element; + } + + var originalStyles = + element.retrieve('prototype_absolutize_original_styles'); + + if (originalStyles) element.setStyle(originalStyles); + return element; + } + + + function scrollTo(element) { + element = $(element); + var pos = Element.cumulativeOffset(element); + window.scrollTo(pos.left, pos.top); + return element; + } + + + function makePositioned(element) { + element = $(element); + var position = Element.getStyle(element, 'position'), styles = {}; + if (position === 'static' || !position) { + styles.position = 'relative'; + if (Prototype.Browser.Opera) { + styles.top = 0; + styles.left = 0; + } + Element.setStyle(element, styles); + Element.store(element, 'prototype_made_positioned', true); + } + return element; + } + + function undoPositioned(element) { + element = $(element); + var storage = Element.getStorage(element), + madePositioned = storage.get('prototype_made_positioned'); + + if (madePositioned) { + storage.unset('prototype_made_positioned'); + Element.setStyle(element, { + position: '', + top: '', + bottom: '', + left: '', + right: '' + }); + } + return element; + } + + function makeClipping(element) { + element = $(element); + + var storage = Element.getStorage(element), + madeClipping = storage.get('prototype_made_clipping'); + + if (Object.isUndefined(madeClipping)) { + var overflow = Element.getStyle(element, 'overflow'); + storage.set('prototype_made_clipping', overflow); + if (overflow !== 'hidden') + element.style.overflow = 'hidden'; + } + + return element; + } + + function undoClipping(element) { + element = $(element); + var storage = Element.getStorage(element), + overflow = storage.get('prototype_made_clipping'); + + if (!Object.isUndefined(overflow)) { + storage.unset('prototype_made_clipping'); + element.style.overflow = overflow || ''; + } + + return element; + } + + function clonePosition(element, source, options) { + options = Object.extend({ + setLeft: true, + setTop: true, + setWidth: true, + setHeight: true, + offsetTop: 0, + offsetLeft: 0 + }, options || {}); + + var docEl = document.documentElement; + + source = $(source); + element = $(element); + var p, delta, layout, styles = {}; + + if (options.setLeft || options.setTop) { + p = Element.viewportOffset(source); + delta = [0, 0]; + if (Element.getStyle(element, 'position') === 'absolute') { + var parent = Element.getOffsetParent(element); + if (parent !== document.body) delta = Element.viewportOffset(parent); + } + } + + function pageScrollXY() { + var x = 0, y = 0; + if (Object.isNumber(window.pageXOffset)) { + x = window.pageXOffset; + y = window.pageYOffset; + } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) { + x = document.body.scrollLeft; + y = document.body.scrollTop; + } else if (docEl && (docEl.scrollLeft || docEl.scrollTop)) { + x = docEl.scrollLeft; + y = docEl.scrollTop; + } + return { x: x, y: y }; + } + + var pageXY = pageScrollXY(); + + + if (options.setWidth || options.setHeight) { + layout = Element.getLayout(source); + } + + if (options.setLeft) + styles.left = (p[0] + pageXY.x - delta[0] + options.offsetLeft) + 'px'; + if (options.setTop) + styles.top = (p[1] + pageXY.y - delta[1] + options.offsetTop) + 'px'; + + var currentLayout = element.getLayout(); + + if (options.setWidth) { + styles.width = layout.get('width') + 'px'; + } + if (options.setHeight) { + styles.height = layout.get('height') + 'px'; + } + + return Element.setStyle(element, styles); + } + + + if (Prototype.Browser.IE) { + getOffsetParent = getOffsetParent.wrap( + function(proceed, element) { + element = $(element); + + if (isDocument(element) || isDetached(element) || isBody(element) || isHtml(element)) + return $(document.body); + + var position = element.getStyle('position'); + if (position !== 'static') return proceed(element); + + element.setStyle({ position: 'relative' }); + var value = proceed(element); + element.setStyle({ position: position }); + return value; + } + ); + + positionedOffset = positionedOffset.wrap(function(proceed, element) { + element = $(element); + if (!element.parentNode) return new Element.Offset(0, 0); + var position = element.getStyle('position'); + if (position !== 'static') return proceed(element); + + var offsetParent = element.getOffsetParent(); + if (offsetParent && offsetParent.getStyle('position') === 'fixed') + hasLayout(offsetParent); + + element.setStyle({ position: 'relative' }); + var value = proceed(element); + element.setStyle({ position: position }); + return value; + }); + } else if (Prototype.Browser.Webkit) { + cumulativeOffset = function(element) { + element = $(element); + var valueT = 0, valueL = 0; + do { + valueT += element.offsetTop || 0; + valueL += element.offsetLeft || 0; + if (element.offsetParent == document.body) { + if (Element.getStyle(element, 'position') == 'absolute') break; + } + + element = element.offsetParent; + } while (element); + + return new Element.Offset(valueL, valueT); + }; + } + + + Element.addMethods({ + getLayout: getLayout, + measure: measure, + getWidth: getWidth, + getHeight: getHeight, + getDimensions: getDimensions, + getOffsetParent: getOffsetParent, + cumulativeOffset: cumulativeOffset, + positionedOffset: positionedOffset, + cumulativeScrollOffset: cumulativeScrollOffset, + viewportOffset: viewportOffset, + absolutize: absolutize, + relativize: relativize, + scrollTo: scrollTo, + makePositioned: makePositioned, + undoPositioned: undoPositioned, + makeClipping: makeClipping, + undoClipping: undoClipping, + clonePosition: clonePosition + }); + + function isBody(element) { + return element.nodeName.toUpperCase() === 'BODY'; + } + + function isHtml(element) { + return element.nodeName.toUpperCase() === 'HTML'; + } + + function isDocument(element) { + return element.nodeType === Node.DOCUMENT_NODE; + } + + function isDetached(element) { + return element !== document.body && + !Element.descendantOf(element, document.body); + } + + if ('getBoundingClientRect' in document.documentElement) { + Element.addMethods({ + viewportOffset: function(element) { + element = $(element); + if (isDetached(element)) return new Element.Offset(0, 0); + + var rect = element.getBoundingClientRect(), + docEl = document.documentElement; + return new Element.Offset(rect.left - docEl.clientLeft, + rect.top - docEl.clientTop); + } + }); + } + + +})(); + +(function() { + + var IS_OLD_OPERA = Prototype.Browser.Opera && + (window.parseFloat(window.opera.version()) < 9.5); + var ROOT = null; + function getRootElement() { + if (ROOT) return ROOT; + ROOT = IS_OLD_OPERA ? document.body : document.documentElement; + return ROOT; + } + + function getDimensions() { + return { width: this.getWidth(), height: this.getHeight() }; + } + + function getWidth() { + return getRootElement().clientWidth; + } + + function getHeight() { + return getRootElement().clientHeight; + } + + function getScrollOffsets() { + var x = window.pageXOffset || document.documentElement.scrollLeft || + document.body.scrollLeft; + var y = window.pageYOffset || document.documentElement.scrollTop || + document.body.scrollTop; + + return new Element.Offset(x, y); + } + + document.viewport = { + getDimensions: getDimensions, + getWidth: getWidth, + getHeight: getHeight, + getScrollOffsets: getScrollOffsets + }; + +})(); +window.$$ = function() { + var expression = $A(arguments).join(', '); + return Prototype.Selector.select(expression, document); +}; + +Prototype.Selector = (function() { + + function select() { + throw new Error('Method "Prototype.Selector.select" must be defined.'); + } + + function match() { + throw new Error('Method "Prototype.Selector.match" must be defined.'); + } + + function find(elements, expression, index) { + index = index || 0; + var match = Prototype.Selector.match, length = elements.length, matchIndex = 0, i; + + for (i = 0; i < length; i++) { + if (match(elements[i], expression) && index == matchIndex++) { + return Element.extend(elements[i]); + } + } + } + + function extendElements(elements) { + for (var i = 0, length = elements.length; i < length; i++) { + Element.extend(elements[i]); + } + return elements; + } + + + var K = Prototype.K; + + return { + select: select, + match: match, + find: find, + extendElements: (Element.extend === K) ? K : extendElements, + extendElement: Element.extend + }; +})(); +Prototype._original_property = window.Sizzle; + +;(function () { + function fakeDefine(fn) { + Prototype._actual_sizzle = fn(); + } + fakeDefine.amd = true; + + if (typeof define !== 'undefined' && define.amd) { + Prototype._original_define = define; + Prototype._actual_sizzle = null; + window.define = fakeDefine; + } +})(); + +/*! + * Sizzle CSS Selector Engine v1.10.18 + * http://sizzlejs.com/ + * + * Copyright 2013 jQuery Foundation, Inc. and other contributors + * Released under the MIT license + * http://jquery.org/license + * + * Date: 2014-02-05 + */ +(function( window ) { + +var i, + support, + Expr, + getText, + isXML, + compile, + select, + outermostContext, + sortInput, + hasDuplicate, + + setDocument, + document, + docElem, + documentIsHTML, + rbuggyQSA, + rbuggyMatches, + matches, + contains, + + expando = "sizzle" + -(new Date()), + preferredDoc = window.document, + dirruns = 0, + done = 0, + classCache = createCache(), + tokenCache = createCache(), + compilerCache = createCache(), + sortOrder = function( a, b ) { + if ( a === b ) { + hasDuplicate = true; + } + return 0; + }, + + strundefined = typeof undefined, + MAX_NEGATIVE = 1 << 31, + + hasOwn = ({}).hasOwnProperty, + arr = [], + pop = arr.pop, + push_native = arr.push, + push = arr.push, + slice = arr.slice, + indexOf = arr.indexOf || function( elem ) { + var i = 0, + len = this.length; + for ( ; i < len; i++ ) { + if ( this[i] === elem ) { + return i; + } + } + return -1; + }, + + booleans = "checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped", + + + whitespace = "[\\x20\\t\\r\\n\\f]", + characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+", + + identifier = characterEncoding.replace( "w", "w#" ), + + attributes = "\\[" + whitespace + "*(" + characterEncoding + ")" + whitespace + + "*(?:([*^$|!~]?=)" + whitespace + "*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|(" + identifier + ")|)|)" + whitespace + "*\\]", + + pseudos = ":(" + characterEncoding + ")(?:\\(((['\"])((?:\\\\.|[^\\\\])*?)\\3|((?:\\\\.|[^\\\\()[\\]]|" + attributes.replace( 3, 8 ) + ")*)|.*)\\)|)", + + rtrim = new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" ), + + rcomma = new RegExp( "^" + whitespace + "*," + whitespace + "*" ), + rcombinators = new RegExp( "^" + whitespace + "*([>+~]|" + whitespace + ")" + whitespace + "*" ), + + rattributeQuotes = new RegExp( "=" + whitespace + "*([^\\]'\"]*?)" + whitespace + "*\\]", "g" ), + + rpseudo = new RegExp( pseudos ), + ridentifier = new RegExp( "^" + identifier + "$" ), + + matchExpr = { + "ID": new RegExp( "^#(" + characterEncoding + ")" ), + "CLASS": new RegExp( "^\\.(" + characterEncoding + ")" ), + "TAG": new RegExp( "^(" + characterEncoding.replace( "w", "w*" ) + ")" ), + "ATTR": new RegExp( "^" + attributes ), + "PSEUDO": new RegExp( "^" + pseudos ), + "CHILD": new RegExp( "^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\(" + whitespace + + "*(even|odd|(([+-]|)(\\d*)n|)" + whitespace + "*(?:([+-]|)" + whitespace + + "*(\\d+)|))" + whitespace + "*\\)|)", "i" ), + "bool": new RegExp( "^(?:" + booleans + ")$", "i" ), + "needsContext": new RegExp( "^" + whitespace + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" + + whitespace + "*((?:-\\d)?\\d*)" + whitespace + "*\\)|)(?=[^-]|$)", "i" ) + }, + + rinputs = /^(?:input|select|textarea|button)$/i, + rheader = /^h\d$/i, + + rnative = /^[^{]+\{\s*\[native \w/, + + rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/, + + rsibling = /[+~]/, + rescape = /'|\\/g, + + runescape = new RegExp( "\\\\([\\da-f]{1,6}" + whitespace + "?|(" + whitespace + ")|.)", "ig" ), + funescape = function( _, escaped, escapedWhitespace ) { + var high = "0x" + escaped - 0x10000; + return high !== high || escapedWhitespace ? + escaped : + high < 0 ? + String.fromCharCode( high + 0x10000 ) : + String.fromCharCode( high >> 10 | 0xD800, high & 0x3FF | 0xDC00 ); + }; + +try { + push.apply( + (arr = slice.call( preferredDoc.childNodes )), + preferredDoc.childNodes + ); + arr[ preferredDoc.childNodes.length ].nodeType; +} catch ( e ) { + push = { apply: arr.length ? + + function( target, els ) { + push_native.apply( target, slice.call(els) ); + } : + + function( target, els ) { + var j = target.length, + i = 0; + while ( (target[j++] = els[i++]) ) {} + target.length = j - 1; + } + }; +} + +function Sizzle( selector, context, results, seed ) { + var match, elem, m, nodeType, + i, groups, old, nid, newContext, newSelector; + + if ( ( context ? context.ownerDocument || context : preferredDoc ) !== document ) { + setDocument( context ); + } + + context = context || document; + results = results || []; + + if ( !selector || typeof selector !== "string" ) { + return results; + } + + if ( (nodeType = context.nodeType) !== 1 && nodeType !== 9 ) { + return []; + } + + if ( documentIsHTML && !seed ) { + + if ( (match = rquickExpr.exec( selector )) ) { + if ( (m = match[1]) ) { + if ( nodeType === 9 ) { + elem = context.getElementById( m ); + if ( elem && elem.parentNode ) { + if ( elem.id === m ) { + results.push( elem ); + return results; + } + } else { + return results; + } + } else { + if ( context.ownerDocument && (elem = context.ownerDocument.getElementById( m )) && + contains( context, elem ) && elem.id === m ) { + results.push( elem ); + return results; + } + } + + } else if ( match[2] ) { + push.apply( results, context.getElementsByTagName( selector ) ); + return results; + + } else if ( (m = match[3]) && support.getElementsByClassName && context.getElementsByClassName ) { + push.apply( results, context.getElementsByClassName( m ) ); + return results; + } + } + + if ( support.qsa && (!rbuggyQSA || !rbuggyQSA.test( selector )) ) { + nid = old = expando; + newContext = context; + newSelector = nodeType === 9 && selector; + + if ( nodeType === 1 && context.nodeName.toLowerCase() !== "object" ) { + groups = tokenize( selector ); + + if ( (old = context.getAttribute("id")) ) { + nid = old.replace( rescape, "\\$&" ); + } else { + context.setAttribute( "id", nid ); + } + nid = "[id='" + nid + "'] "; + + i = groups.length; + while ( i-- ) { + groups[i] = nid + toSelector( groups[i] ); + } + newContext = rsibling.test( selector ) && testContext( context.parentNode ) || context; + newSelector = groups.join(","); + } + + if ( newSelector ) { + try { + push.apply( results, + newContext.querySelectorAll( newSelector ) + ); + return results; + } catch(qsaError) { + } finally { + if ( !old ) { + context.removeAttribute("id"); + } + } + } + } + } + + return select( selector.replace( rtrim, "$1" ), context, results, seed ); +} + +/** + * Create key-value caches of limited size + * @returns {Function(string, Object)} Returns the Object data after storing it on itself with + * property name the (space-suffixed) string and (if the cache is larger than Expr.cacheLength) + * deleting the oldest entry + */ +function createCache() { + var keys = []; + + function cache( key, value ) { + if ( keys.push( key + " " ) > Expr.cacheLength ) { + delete cache[ keys.shift() ]; + } + return (cache[ key + " " ] = value); + } + return cache; +} + +/** + * Mark a function for special use by Sizzle + * @param {Function} fn The function to mark + */ +function markFunction( fn ) { + fn[ expando ] = true; + return fn; +} + +/** + * Support testing using an element + * @param {Function} fn Passed the created div and expects a boolean result + */ +function assert( fn ) { + var div = document.createElement("div"); + + try { + return !!fn( div ); + } catch (e) { + return false; + } finally { + if ( div.parentNode ) { + div.parentNode.removeChild( div ); + } + div = null; + } +} + +/** + * Adds the same handler for all of the specified attrs + * @param {String} attrs Pipe-separated list of attributes + * @param {Function} handler The method that will be applied + */ +function addHandle( attrs, handler ) { + var arr = attrs.split("|"), + i = attrs.length; + + while ( i-- ) { + Expr.attrHandle[ arr[i] ] = handler; + } +} + +/** + * Checks document order of two siblings + * @param {Element} a + * @param {Element} b + * @returns {Number} Returns less than 0 if a precedes b, greater than 0 if a follows b + */ +function siblingCheck( a, b ) { + var cur = b && a, + diff = cur && a.nodeType === 1 && b.nodeType === 1 && + ( ~b.sourceIndex || MAX_NEGATIVE ) - + ( ~a.sourceIndex || MAX_NEGATIVE ); + + if ( diff ) { + return diff; + } + + if ( cur ) { + while ( (cur = cur.nextSibling) ) { + if ( cur === b ) { + return -1; + } + } + } + + return a ? 1 : -1; +} + +/** + * Returns a function to use in pseudos for input types + * @param {String} type + */ +function createInputPseudo( type ) { + return function( elem ) { + var name = elem.nodeName.toLowerCase(); + return name === "input" && elem.type === type; + }; +} + +/** + * Returns a function to use in pseudos for buttons + * @param {String} type + */ +function createButtonPseudo( type ) { + return function( elem ) { + var name = elem.nodeName.toLowerCase(); + return (name === "input" || name === "button") && elem.type === type; + }; +} + +/** + * Returns a function to use in pseudos for positionals + * @param {Function} fn + */ +function createPositionalPseudo( fn ) { + return markFunction(function( argument ) { + argument = +argument; + return markFunction(function( seed, matches ) { + var j, + matchIndexes = fn( [], seed.length, argument ), + i = matchIndexes.length; + + while ( i-- ) { + if ( seed[ (j = matchIndexes[i]) ] ) { + seed[j] = !(matches[j] = seed[j]); + } + } + }); + }); +} + +/** + * Checks a node for validity as a Sizzle context + * @param {Element|Object=} context + * @returns {Element|Object|Boolean} The input node if acceptable, otherwise a falsy value + */ +function testContext( context ) { + return context && typeof context.getElementsByTagName !== strundefined && context; +} + +support = Sizzle.support = {}; + +/** + * Detects XML nodes + * @param {Element|Object} elem An element or a document + * @returns {Boolean} True iff elem is a non-HTML XML node + */ +isXML = Sizzle.isXML = function( elem ) { + var documentElement = elem && (elem.ownerDocument || elem).documentElement; + return documentElement ? documentElement.nodeName !== "HTML" : false; +}; + +/** + * Sets document-related variables once based on the current document + * @param {Element|Object} [doc] An element or document object to use to set the document + * @returns {Object} Returns the current document + */ +setDocument = Sizzle.setDocument = function( node ) { + var hasCompare, + doc = node ? node.ownerDocument || node : preferredDoc, + parent = doc.defaultView; + + if ( doc === document || doc.nodeType !== 9 || !doc.documentElement ) { + return document; + } + + document = doc; + docElem = doc.documentElement; + + documentIsHTML = !isXML( doc ); + + if ( parent && parent !== parent.top ) { + if ( parent.addEventListener ) { + parent.addEventListener( "unload", function() { + setDocument(); + }, false ); + } else if ( parent.attachEvent ) { + parent.attachEvent( "onunload", function() { + setDocument(); + }); + } + } + + /* Attributes + ---------------------------------------------------------------------- */ + + support.attributes = assert(function( div ) { + div.className = "i"; + return !div.getAttribute("className"); + }); + + /* getElement(s)By* + ---------------------------------------------------------------------- */ + + support.getElementsByTagName = assert(function( div ) { + div.appendChild( doc.createComment("") ); + return !div.getElementsByTagName("*").length; + }); + + support.getElementsByClassName = rnative.test( doc.getElementsByClassName ) && assert(function( div ) { + div.innerHTML = "
      "; + + div.firstChild.className = "i"; + return div.getElementsByClassName("i").length === 2; + }); + + support.getById = assert(function( div ) { + docElem.appendChild( div ).id = expando; + return !doc.getElementsByName || !doc.getElementsByName( expando ).length; + }); + + if ( support.getById ) { + Expr.find["ID"] = function( id, context ) { + if ( typeof context.getElementById !== strundefined && documentIsHTML ) { + var m = context.getElementById( id ); + return m && m.parentNode ? [m] : []; + } + }; + Expr.filter["ID"] = function( id ) { + var attrId = id.replace( runescape, funescape ); + return function( elem ) { + return elem.getAttribute("id") === attrId; + }; + }; + } else { + delete Expr.find["ID"]; + + Expr.filter["ID"] = function( id ) { + var attrId = id.replace( runescape, funescape ); + return function( elem ) { + var node = typeof elem.getAttributeNode !== strundefined && elem.getAttributeNode("id"); + return node && node.value === attrId; + }; + }; + } + + Expr.find["TAG"] = support.getElementsByTagName ? + function( tag, context ) { + if ( typeof context.getElementsByTagName !== strundefined ) { + return context.getElementsByTagName( tag ); + } + } : + function( tag, context ) { + var elem, + tmp = [], + i = 0, + results = context.getElementsByTagName( tag ); + + if ( tag === "*" ) { + while ( (elem = results[i++]) ) { + if ( elem.nodeType === 1 ) { + tmp.push( elem ); + } + } + + return tmp; + } + return results; + }; + + Expr.find["CLASS"] = support.getElementsByClassName && function( className, context ) { + if ( typeof context.getElementsByClassName !== strundefined && documentIsHTML ) { + return context.getElementsByClassName( className ); + } + }; + + /* QSA/matchesSelector + ---------------------------------------------------------------------- */ + + + rbuggyMatches = []; + + rbuggyQSA = []; + + if ( (support.qsa = rnative.test( doc.querySelectorAll )) ) { + assert(function( div ) { + div.innerHTML = ""; + + if ( div.querySelectorAll("[t^='']").length ) { + rbuggyQSA.push( "[*^$]=" + whitespace + "*(?:''|\"\")" ); + } + + if ( !div.querySelectorAll("[selected]").length ) { + rbuggyQSA.push( "\\[" + whitespace + "*(?:value|" + booleans + ")" ); + } + + if ( !div.querySelectorAll(":checked").length ) { + rbuggyQSA.push(":checked"); + } + }); + + assert(function( div ) { + var input = doc.createElement("input"); + input.setAttribute( "type", "hidden" ); + div.appendChild( input ).setAttribute( "name", "D" ); + + if ( div.querySelectorAll("[name=d]").length ) { + rbuggyQSA.push( "name" + whitespace + "*[*^$|!~]?=" ); + } + + if ( !div.querySelectorAll(":enabled").length ) { + rbuggyQSA.push( ":enabled", ":disabled" ); + } + + div.querySelectorAll("*,:x"); + rbuggyQSA.push(",.*:"); + }); + } + + if ( (support.matchesSelector = rnative.test( (matches = docElem.webkitMatchesSelector || + docElem.mozMatchesSelector || + docElem.oMatchesSelector || + docElem.msMatchesSelector) )) ) { + + assert(function( div ) { + support.disconnectedMatch = matches.call( div, "div" ); + + matches.call( div, "[s!='']:x" ); + rbuggyMatches.push( "!=", pseudos ); + }); + } + + rbuggyQSA = rbuggyQSA.length && new RegExp( rbuggyQSA.join("|") ); + rbuggyMatches = rbuggyMatches.length && new RegExp( rbuggyMatches.join("|") ); + + /* Contains + ---------------------------------------------------------------------- */ + hasCompare = rnative.test( docElem.compareDocumentPosition ); + + contains = hasCompare || rnative.test( docElem.contains ) ? + function( a, b ) { + var adown = a.nodeType === 9 ? a.documentElement : a, + bup = b && b.parentNode; + return a === bup || !!( bup && bup.nodeType === 1 && ( + adown.contains ? + adown.contains( bup ) : + a.compareDocumentPosition && a.compareDocumentPosition( bup ) & 16 + )); + } : + function( a, b ) { + if ( b ) { + while ( (b = b.parentNode) ) { + if ( b === a ) { + return true; + } + } + } + return false; + }; + + /* Sorting + ---------------------------------------------------------------------- */ + + sortOrder = hasCompare ? + function( a, b ) { + + if ( a === b ) { + hasDuplicate = true; + return 0; + } + + var compare = !a.compareDocumentPosition - !b.compareDocumentPosition; + if ( compare ) { + return compare; + } + + compare = ( a.ownerDocument || a ) === ( b.ownerDocument || b ) ? + a.compareDocumentPosition( b ) : + + 1; + + if ( compare & 1 || + (!support.sortDetached && b.compareDocumentPosition( a ) === compare) ) { + + if ( a === doc || a.ownerDocument === preferredDoc && contains(preferredDoc, a) ) { + return -1; + } + if ( b === doc || b.ownerDocument === preferredDoc && contains(preferredDoc, b) ) { + return 1; + } + + return sortInput ? + ( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) : + 0; + } + + return compare & 4 ? -1 : 1; + } : + function( a, b ) { + if ( a === b ) { + hasDuplicate = true; + return 0; + } + + var cur, + i = 0, + aup = a.parentNode, + bup = b.parentNode, + ap = [ a ], + bp = [ b ]; + + if ( !aup || !bup ) { + return a === doc ? -1 : + b === doc ? 1 : + aup ? -1 : + bup ? 1 : + sortInput ? + ( indexOf.call( sortInput, a ) - indexOf.call( sortInput, b ) ) : + 0; + + } else if ( aup === bup ) { + return siblingCheck( a, b ); + } + + cur = a; + while ( (cur = cur.parentNode) ) { + ap.unshift( cur ); + } + cur = b; + while ( (cur = cur.parentNode) ) { + bp.unshift( cur ); + } + + while ( ap[i] === bp[i] ) { + i++; + } + + return i ? + siblingCheck( ap[i], bp[i] ) : + + ap[i] === preferredDoc ? -1 : + bp[i] === preferredDoc ? 1 : + 0; + }; + + return doc; +}; + +Sizzle.matches = function( expr, elements ) { + return Sizzle( expr, null, null, elements ); +}; + +Sizzle.matchesSelector = function( elem, expr ) { + if ( ( elem.ownerDocument || elem ) !== document ) { + setDocument( elem ); + } + + expr = expr.replace( rattributeQuotes, "='$1']" ); + + if ( support.matchesSelector && documentIsHTML && + ( !rbuggyMatches || !rbuggyMatches.test( expr ) ) && + ( !rbuggyQSA || !rbuggyQSA.test( expr ) ) ) { + + try { + var ret = matches.call( elem, expr ); + + if ( ret || support.disconnectedMatch || + elem.document && elem.document.nodeType !== 11 ) { + return ret; + } + } catch(e) {} + } + + return Sizzle( expr, document, null, [elem] ).length > 0; +}; + +Sizzle.contains = function( context, elem ) { + if ( ( context.ownerDocument || context ) !== document ) { + setDocument( context ); + } + return contains( context, elem ); +}; + +Sizzle.attr = function( elem, name ) { + if ( ( elem.ownerDocument || elem ) !== document ) { + setDocument( elem ); + } + + var fn = Expr.attrHandle[ name.toLowerCase() ], + val = fn && hasOwn.call( Expr.attrHandle, name.toLowerCase() ) ? + fn( elem, name, !documentIsHTML ) : + undefined; + + return val !== undefined ? + val : + support.attributes || !documentIsHTML ? + elem.getAttribute( name ) : + (val = elem.getAttributeNode(name)) && val.specified ? + val.value : + null; +}; + +Sizzle.error = function( msg ) { + throw new Error( "Syntax error, unrecognized expression: " + msg ); +}; + +/** + * Document sorting and removing duplicates + * @param {ArrayLike} results + */ +Sizzle.uniqueSort = function( results ) { + var elem, + duplicates = [], + j = 0, + i = 0; + + hasDuplicate = !support.detectDuplicates; + sortInput = !support.sortStable && results.slice( 0 ); + results.sort( sortOrder ); + + if ( hasDuplicate ) { + while ( (elem = results[i++]) ) { + if ( elem === results[ i ] ) { + j = duplicates.push( i ); + } + } + while ( j-- ) { + results.splice( duplicates[ j ], 1 ); + } + } + + sortInput = null; + + return results; +}; + +/** + * Utility function for retrieving the text value of an array of DOM nodes + * @param {Array|Element} elem + */ +getText = Sizzle.getText = function( elem ) { + var node, + ret = "", + i = 0, + nodeType = elem.nodeType; + + if ( !nodeType ) { + while ( (node = elem[i++]) ) { + ret += getText( node ); + } + } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { + if ( typeof elem.textContent === "string" ) { + return elem.textContent; + } else { + for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { + ret += getText( elem ); + } + } + } else if ( nodeType === 3 || nodeType === 4 ) { + return elem.nodeValue; + } + + return ret; +}; + +Expr = Sizzle.selectors = { + + cacheLength: 50, + + createPseudo: markFunction, + + match: matchExpr, + + attrHandle: {}, + + find: {}, + + relative: { + ">": { dir: "parentNode", first: true }, + " ": { dir: "parentNode" }, + "+": { dir: "previousSibling", first: true }, + "~": { dir: "previousSibling" } + }, + + preFilter: { + "ATTR": function( match ) { + match[1] = match[1].replace( runescape, funescape ); + + match[3] = ( match[4] || match[5] || "" ).replace( runescape, funescape ); + + if ( match[2] === "~=" ) { + match[3] = " " + match[3] + " "; + } + + return match.slice( 0, 4 ); + }, + + "CHILD": function( match ) { + /* matches from matchExpr["CHILD"] + 1 type (only|nth|...) + 2 what (child|of-type) + 3 argument (even|odd|\d*|\d*n([+-]\d+)?|...) + 4 xn-component of xn+y argument ([+-]?\d*n|) + 5 sign of xn-component + 6 x of xn-component + 7 sign of y-component + 8 y of y-component + */ + match[1] = match[1].toLowerCase(); + + if ( match[1].slice( 0, 3 ) === "nth" ) { + if ( !match[3] ) { + Sizzle.error( match[0] ); + } + + match[4] = +( match[4] ? match[5] + (match[6] || 1) : 2 * ( match[3] === "even" || match[3] === "odd" ) ); + match[5] = +( ( match[7] + match[8] ) || match[3] === "odd" ); + + } else if ( match[3] ) { + Sizzle.error( match[0] ); + } + + return match; + }, + + "PSEUDO": function( match ) { + var excess, + unquoted = !match[5] && match[2]; + + if ( matchExpr["CHILD"].test( match[0] ) ) { + return null; + } + + if ( match[3] && match[4] !== undefined ) { + match[2] = match[4]; + + } else if ( unquoted && rpseudo.test( unquoted ) && + (excess = tokenize( unquoted, true )) && + (excess = unquoted.indexOf( ")", unquoted.length - excess ) - unquoted.length) ) { + + match[0] = match[0].slice( 0, excess ); + match[2] = unquoted.slice( 0, excess ); + } + + return match.slice( 0, 3 ); + } + }, + + filter: { + + "TAG": function( nodeNameSelector ) { + var nodeName = nodeNameSelector.replace( runescape, funescape ).toLowerCase(); + return nodeNameSelector === "*" ? + function() { return true; } : + function( elem ) { + return elem.nodeName && elem.nodeName.toLowerCase() === nodeName; + }; + }, + + "CLASS": function( className ) { + var pattern = classCache[ className + " " ]; + + return pattern || + (pattern = new RegExp( "(^|" + whitespace + ")" + className + "(" + whitespace + "|$)" )) && + classCache( className, function( elem ) { + return pattern.test( typeof elem.className === "string" && elem.className || typeof elem.getAttribute !== strundefined && elem.getAttribute("class") || "" ); + }); + }, + + "ATTR": function( name, operator, check ) { + return function( elem ) { + var result = Sizzle.attr( elem, name ); + + if ( result == null ) { + return operator === "!="; + } + if ( !operator ) { + return true; + } + + result += ""; + + return operator === "=" ? result === check : + operator === "!=" ? result !== check : + operator === "^=" ? check && result.indexOf( check ) === 0 : + operator === "*=" ? check && result.indexOf( check ) > -1 : + operator === "$=" ? check && result.slice( -check.length ) === check : + operator === "~=" ? ( " " + result + " " ).indexOf( check ) > -1 : + operator === "|=" ? result === check || result.slice( 0, check.length + 1 ) === check + "-" : + false; + }; + }, + + "CHILD": function( type, what, argument, first, last ) { + var simple = type.slice( 0, 3 ) !== "nth", + forward = type.slice( -4 ) !== "last", + ofType = what === "of-type"; + + return first === 1 && last === 0 ? + + function( elem ) { + return !!elem.parentNode; + } : + + function( elem, context, xml ) { + var cache, outerCache, node, diff, nodeIndex, start, + dir = simple !== forward ? "nextSibling" : "previousSibling", + parent = elem.parentNode, + name = ofType && elem.nodeName.toLowerCase(), + useCache = !xml && !ofType; + + if ( parent ) { + + if ( simple ) { + while ( dir ) { + node = elem; + while ( (node = node[ dir ]) ) { + if ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) { + return false; + } + } + start = dir = type === "only" && !start && "nextSibling"; + } + return true; + } + + start = [ forward ? parent.firstChild : parent.lastChild ]; + + if ( forward && useCache ) { + outerCache = parent[ expando ] || (parent[ expando ] = {}); + cache = outerCache[ type ] || []; + nodeIndex = cache[0] === dirruns && cache[1]; + diff = cache[0] === dirruns && cache[2]; + node = nodeIndex && parent.childNodes[ nodeIndex ]; + + while ( (node = ++nodeIndex && node && node[ dir ] || + + (diff = nodeIndex = 0) || start.pop()) ) { + + if ( node.nodeType === 1 && ++diff && node === elem ) { + outerCache[ type ] = [ dirruns, nodeIndex, diff ]; + break; + } + } + + } else if ( useCache && (cache = (elem[ expando ] || (elem[ expando ] = {}))[ type ]) && cache[0] === dirruns ) { + diff = cache[1]; + + } else { + while ( (node = ++nodeIndex && node && node[ dir ] || + (diff = nodeIndex = 0) || start.pop()) ) { + + if ( ( ofType ? node.nodeName.toLowerCase() === name : node.nodeType === 1 ) && ++diff ) { + if ( useCache ) { + (node[ expando ] || (node[ expando ] = {}))[ type ] = [ dirruns, diff ]; + } + + if ( node === elem ) { + break; + } + } + } + } + + diff -= last; + return diff === first || ( diff % first === 0 && diff / first >= 0 ); + } + }; + }, + + "PSEUDO": function( pseudo, argument ) { + var args, + fn = Expr.pseudos[ pseudo ] || Expr.setFilters[ pseudo.toLowerCase() ] || + Sizzle.error( "unsupported pseudo: " + pseudo ); + + if ( fn[ expando ] ) { + return fn( argument ); + } + + if ( fn.length > 1 ) { + args = [ pseudo, pseudo, "", argument ]; + return Expr.setFilters.hasOwnProperty( pseudo.toLowerCase() ) ? + markFunction(function( seed, matches ) { + var idx, + matched = fn( seed, argument ), + i = matched.length; + while ( i-- ) { + idx = indexOf.call( seed, matched[i] ); + seed[ idx ] = !( matches[ idx ] = matched[i] ); + } + }) : + function( elem ) { + return fn( elem, 0, args ); + }; + } + + return fn; + } + }, + + pseudos: { + "not": markFunction(function( selector ) { + var input = [], + results = [], + matcher = compile( selector.replace( rtrim, "$1" ) ); + + return matcher[ expando ] ? + markFunction(function( seed, matches, context, xml ) { + var elem, + unmatched = matcher( seed, null, xml, [] ), + i = seed.length; + + while ( i-- ) { + if ( (elem = unmatched[i]) ) { + seed[i] = !(matches[i] = elem); + } + } + }) : + function( elem, context, xml ) { + input[0] = elem; + matcher( input, null, xml, results ); + return !results.pop(); + }; + }), + + "has": markFunction(function( selector ) { + return function( elem ) { + return Sizzle( selector, elem ).length > 0; + }; + }), + + "contains": markFunction(function( text ) { + return function( elem ) { + return ( elem.textContent || elem.innerText || getText( elem ) ).indexOf( text ) > -1; + }; + }), + + "lang": markFunction( function( lang ) { + if ( !ridentifier.test(lang || "") ) { + Sizzle.error( "unsupported lang: " + lang ); + } + lang = lang.replace( runescape, funescape ).toLowerCase(); + return function( elem ) { + var elemLang; + do { + if ( (elemLang = documentIsHTML ? + elem.lang : + elem.getAttribute("xml:lang") || elem.getAttribute("lang")) ) { + + elemLang = elemLang.toLowerCase(); + return elemLang === lang || elemLang.indexOf( lang + "-" ) === 0; + } + } while ( (elem = elem.parentNode) && elem.nodeType === 1 ); + return false; + }; + }), + + "target": function( elem ) { + var hash = window.location && window.location.hash; + return hash && hash.slice( 1 ) === elem.id; + }, + + "root": function( elem ) { + return elem === docElem; + }, + + "focus": function( elem ) { + return elem === document.activeElement && (!document.hasFocus || document.hasFocus()) && !!(elem.type || elem.href || ~elem.tabIndex); + }, + + "enabled": function( elem ) { + return elem.disabled === false; + }, + + "disabled": function( elem ) { + return elem.disabled === true; + }, + + "checked": function( elem ) { + var nodeName = elem.nodeName.toLowerCase(); + return (nodeName === "input" && !!elem.checked) || (nodeName === "option" && !!elem.selected); + }, + + "selected": function( elem ) { + if ( elem.parentNode ) { + elem.parentNode.selectedIndex; + } + + return elem.selected === true; + }, + + "empty": function( elem ) { + for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { + if ( elem.nodeType < 6 ) { + return false; + } + } + return true; + }, + + "parent": function( elem ) { + return !Expr.pseudos["empty"]( elem ); + }, + + "header": function( elem ) { + return rheader.test( elem.nodeName ); + }, + + "input": function( elem ) { + return rinputs.test( elem.nodeName ); + }, + + "button": function( elem ) { + var name = elem.nodeName.toLowerCase(); + return name === "input" && elem.type === "button" || name === "button"; + }, + + "text": function( elem ) { + var attr; + return elem.nodeName.toLowerCase() === "input" && + elem.type === "text" && + + ( (attr = elem.getAttribute("type")) == null || attr.toLowerCase() === "text" ); + }, + + "first": createPositionalPseudo(function() { + return [ 0 ]; + }), + + "last": createPositionalPseudo(function( matchIndexes, length ) { + return [ length - 1 ]; + }), + + "eq": createPositionalPseudo(function( matchIndexes, length, argument ) { + return [ argument < 0 ? argument + length : argument ]; + }), + + "even": createPositionalPseudo(function( matchIndexes, length ) { + var i = 0; + for ( ; i < length; i += 2 ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "odd": createPositionalPseudo(function( matchIndexes, length ) { + var i = 1; + for ( ; i < length; i += 2 ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "lt": createPositionalPseudo(function( matchIndexes, length, argument ) { + var i = argument < 0 ? argument + length : argument; + for ( ; --i >= 0; ) { + matchIndexes.push( i ); + } + return matchIndexes; + }), + + "gt": createPositionalPseudo(function( matchIndexes, length, argument ) { + var i = argument < 0 ? argument + length : argument; + for ( ; ++i < length; ) { + matchIndexes.push( i ); + } + return matchIndexes; + }) + } +}; + +Expr.pseudos["nth"] = Expr.pseudos["eq"]; + +for ( i in { radio: true, checkbox: true, file: true, password: true, image: true } ) { + Expr.pseudos[ i ] = createInputPseudo( i ); +} +for ( i in { submit: true, reset: true } ) { + Expr.pseudos[ i ] = createButtonPseudo( i ); +} + +function setFilters() {} +setFilters.prototype = Expr.filters = Expr.pseudos; +Expr.setFilters = new setFilters(); + +function tokenize( selector, parseOnly ) { + var matched, match, tokens, type, + soFar, groups, preFilters, + cached = tokenCache[ selector + " " ]; + + if ( cached ) { + return parseOnly ? 0 : cached.slice( 0 ); + } + + soFar = selector; + groups = []; + preFilters = Expr.preFilter; + + while ( soFar ) { + + if ( !matched || (match = rcomma.exec( soFar )) ) { + if ( match ) { + soFar = soFar.slice( match[0].length ) || soFar; + } + groups.push( (tokens = []) ); + } + + matched = false; + + if ( (match = rcombinators.exec( soFar )) ) { + matched = match.shift(); + tokens.push({ + value: matched, + type: match[0].replace( rtrim, " " ) + }); + soFar = soFar.slice( matched.length ); + } + + for ( type in Expr.filter ) { + if ( (match = matchExpr[ type ].exec( soFar )) && (!preFilters[ type ] || + (match = preFilters[ type ]( match ))) ) { + matched = match.shift(); + tokens.push({ + value: matched, + type: type, + matches: match + }); + soFar = soFar.slice( matched.length ); + } + } + + if ( !matched ) { + break; + } + } + + return parseOnly ? + soFar.length : + soFar ? + Sizzle.error( selector ) : + tokenCache( selector, groups ).slice( 0 ); +} + +function toSelector( tokens ) { + var i = 0, + len = tokens.length, + selector = ""; + for ( ; i < len; i++ ) { + selector += tokens[i].value; + } + return selector; +} + +function addCombinator( matcher, combinator, base ) { + var dir = combinator.dir, + checkNonElements = base && dir === "parentNode", + doneName = done++; + + return combinator.first ? + function( elem, context, xml ) { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + return matcher( elem, context, xml ); + } + } + } : + + function( elem, context, xml ) { + var oldCache, outerCache, + newCache = [ dirruns, doneName ]; + + if ( xml ) { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + if ( matcher( elem, context, xml ) ) { + return true; + } + } + } + } else { + while ( (elem = elem[ dir ]) ) { + if ( elem.nodeType === 1 || checkNonElements ) { + outerCache = elem[ expando ] || (elem[ expando ] = {}); + if ( (oldCache = outerCache[ dir ]) && + oldCache[ 0 ] === dirruns && oldCache[ 1 ] === doneName ) { + + return (newCache[ 2 ] = oldCache[ 2 ]); + } else { + outerCache[ dir ] = newCache; + + if ( (newCache[ 2 ] = matcher( elem, context, xml )) ) { + return true; + } + } + } + } + } + }; +} + +function elementMatcher( matchers ) { + return matchers.length > 1 ? + function( elem, context, xml ) { + var i = matchers.length; + while ( i-- ) { + if ( !matchers[i]( elem, context, xml ) ) { + return false; + } + } + return true; + } : + matchers[0]; +} + +function multipleContexts( selector, contexts, results ) { + var i = 0, + len = contexts.length; + for ( ; i < len; i++ ) { + Sizzle( selector, contexts[i], results ); + } + return results; +} + +function condense( unmatched, map, filter, context, xml ) { + var elem, + newUnmatched = [], + i = 0, + len = unmatched.length, + mapped = map != null; + + for ( ; i < len; i++ ) { + if ( (elem = unmatched[i]) ) { + if ( !filter || filter( elem, context, xml ) ) { + newUnmatched.push( elem ); + if ( mapped ) { + map.push( i ); + } + } + } + } + + return newUnmatched; +} + +function setMatcher( preFilter, selector, matcher, postFilter, postFinder, postSelector ) { + if ( postFilter && !postFilter[ expando ] ) { + postFilter = setMatcher( postFilter ); + } + if ( postFinder && !postFinder[ expando ] ) { + postFinder = setMatcher( postFinder, postSelector ); + } + return markFunction(function( seed, results, context, xml ) { + var temp, i, elem, + preMap = [], + postMap = [], + preexisting = results.length, + + elems = seed || multipleContexts( selector || "*", context.nodeType ? [ context ] : context, [] ), + + matcherIn = preFilter && ( seed || !selector ) ? + condense( elems, preMap, preFilter, context, xml ) : + elems, + + matcherOut = matcher ? + postFinder || ( seed ? preFilter : preexisting || postFilter ) ? + + [] : + + results : + matcherIn; + + if ( matcher ) { + matcher( matcherIn, matcherOut, context, xml ); + } + + if ( postFilter ) { + temp = condense( matcherOut, postMap ); + postFilter( temp, [], context, xml ); + + i = temp.length; + while ( i-- ) { + if ( (elem = temp[i]) ) { + matcherOut[ postMap[i] ] = !(matcherIn[ postMap[i] ] = elem); + } + } + } + + if ( seed ) { + if ( postFinder || preFilter ) { + if ( postFinder ) { + temp = []; + i = matcherOut.length; + while ( i-- ) { + if ( (elem = matcherOut[i]) ) { + temp.push( (matcherIn[i] = elem) ); + } + } + postFinder( null, (matcherOut = []), temp, xml ); + } + + i = matcherOut.length; + while ( i-- ) { + if ( (elem = matcherOut[i]) && + (temp = postFinder ? indexOf.call( seed, elem ) : preMap[i]) > -1 ) { + + seed[temp] = !(results[temp] = elem); + } + } + } + + } else { + matcherOut = condense( + matcherOut === results ? + matcherOut.splice( preexisting, matcherOut.length ) : + matcherOut + ); + if ( postFinder ) { + postFinder( null, results, matcherOut, xml ); + } else { + push.apply( results, matcherOut ); + } + } + }); +} + +function matcherFromTokens( tokens ) { + var checkContext, matcher, j, + len = tokens.length, + leadingRelative = Expr.relative[ tokens[0].type ], + implicitRelative = leadingRelative || Expr.relative[" "], + i = leadingRelative ? 1 : 0, + + matchContext = addCombinator( function( elem ) { + return elem === checkContext; + }, implicitRelative, true ), + matchAnyContext = addCombinator( function( elem ) { + return indexOf.call( checkContext, elem ) > -1; + }, implicitRelative, true ), + matchers = [ function( elem, context, xml ) { + return ( !leadingRelative && ( xml || context !== outermostContext ) ) || ( + (checkContext = context).nodeType ? + matchContext( elem, context, xml ) : + matchAnyContext( elem, context, xml ) ); + } ]; + + for ( ; i < len; i++ ) { + if ( (matcher = Expr.relative[ tokens[i].type ]) ) { + matchers = [ addCombinator(elementMatcher( matchers ), matcher) ]; + } else { + matcher = Expr.filter[ tokens[i].type ].apply( null, tokens[i].matches ); + + if ( matcher[ expando ] ) { + j = ++i; + for ( ; j < len; j++ ) { + if ( Expr.relative[ tokens[j].type ] ) { + break; + } + } + return setMatcher( + i > 1 && elementMatcher( matchers ), + i > 1 && toSelector( + tokens.slice( 0, i - 1 ).concat({ value: tokens[ i - 2 ].type === " " ? "*" : "" }) + ).replace( rtrim, "$1" ), + matcher, + i < j && matcherFromTokens( tokens.slice( i, j ) ), + j < len && matcherFromTokens( (tokens = tokens.slice( j )) ), + j < len && toSelector( tokens ) + ); + } + matchers.push( matcher ); + } + } + + return elementMatcher( matchers ); +} + +function matcherFromGroupMatchers( elementMatchers, setMatchers ) { + var bySet = setMatchers.length > 0, + byElement = elementMatchers.length > 0, + superMatcher = function( seed, context, xml, results, outermost ) { + var elem, j, matcher, + matchedCount = 0, + i = "0", + unmatched = seed && [], + setMatched = [], + contextBackup = outermostContext, + elems = seed || byElement && Expr.find["TAG"]( "*", outermost ), + dirrunsUnique = (dirruns += contextBackup == null ? 1 : Math.random() || 0.1), + len = elems.length; + + if ( outermost ) { + outermostContext = context !== document && context; + } + + for ( ; i !== len && (elem = elems[i]) != null; i++ ) { + if ( byElement && elem ) { + j = 0; + while ( (matcher = elementMatchers[j++]) ) { + if ( matcher( elem, context, xml ) ) { + results.push( elem ); + break; + } + } + if ( outermost ) { + dirruns = dirrunsUnique; + } + } + + if ( bySet ) { + if ( (elem = !matcher && elem) ) { + matchedCount--; + } + + if ( seed ) { + unmatched.push( elem ); + } + } + } + + matchedCount += i; + if ( bySet && i !== matchedCount ) { + j = 0; + while ( (matcher = setMatchers[j++]) ) { + matcher( unmatched, setMatched, context, xml ); + } + + if ( seed ) { + if ( matchedCount > 0 ) { + while ( i-- ) { + if ( !(unmatched[i] || setMatched[i]) ) { + setMatched[i] = pop.call( results ); + } + } + } + + setMatched = condense( setMatched ); + } + + push.apply( results, setMatched ); + + if ( outermost && !seed && setMatched.length > 0 && + ( matchedCount + setMatchers.length ) > 1 ) { + + Sizzle.uniqueSort( results ); + } + } + + if ( outermost ) { + dirruns = dirrunsUnique; + outermostContext = contextBackup; + } + + return unmatched; + }; + + return bySet ? + markFunction( superMatcher ) : + superMatcher; +} + +compile = Sizzle.compile = function( selector, match /* Internal Use Only */ ) { + var i, + setMatchers = [], + elementMatchers = [], + cached = compilerCache[ selector + " " ]; + + if ( !cached ) { + if ( !match ) { + match = tokenize( selector ); + } + i = match.length; + while ( i-- ) { + cached = matcherFromTokens( match[i] ); + if ( cached[ expando ] ) { + setMatchers.push( cached ); + } else { + elementMatchers.push( cached ); + } + } + + cached = compilerCache( selector, matcherFromGroupMatchers( elementMatchers, setMatchers ) ); + + cached.selector = selector; + } + return cached; +}; + +/** + * A low-level selection function that works with Sizzle's compiled + * selector functions + * @param {String|Function} selector A selector or a pre-compiled + * selector function built with Sizzle.compile + * @param {Element} context + * @param {Array} [results] + * @param {Array} [seed] A set of elements to match against + */ +select = Sizzle.select = function( selector, context, results, seed ) { + var i, tokens, token, type, find, + compiled = typeof selector === "function" && selector, + match = !seed && tokenize( (selector = compiled.selector || selector) ); + + results = results || []; + + if ( match.length === 1 ) { + + tokens = match[0] = match[0].slice( 0 ); + if ( tokens.length > 2 && (token = tokens[0]).type === "ID" && + support.getById && context.nodeType === 9 && documentIsHTML && + Expr.relative[ tokens[1].type ] ) { + + context = ( Expr.find["ID"]( token.matches[0].replace(runescape, funescape), context ) || [] )[0]; + if ( !context ) { + return results; + + } else if ( compiled ) { + context = context.parentNode; + } + + selector = selector.slice( tokens.shift().value.length ); + } + + i = matchExpr["needsContext"].test( selector ) ? 0 : tokens.length; + while ( i-- ) { + token = tokens[i]; + + if ( Expr.relative[ (type = token.type) ] ) { + break; + } + if ( (find = Expr.find[ type ]) ) { + if ( (seed = find( + token.matches[0].replace( runescape, funescape ), + rsibling.test( tokens[0].type ) && testContext( context.parentNode ) || context + )) ) { + + tokens.splice( i, 1 ); + selector = seed.length && toSelector( tokens ); + if ( !selector ) { + push.apply( results, seed ); + return results; + } + + break; + } + } + } + } + + ( compiled || compile( selector, match ) )( + seed, + context, + !documentIsHTML, + results, + rsibling.test( selector ) && testContext( context.parentNode ) || context + ); + return results; +}; + + +support.sortStable = expando.split("").sort( sortOrder ).join("") === expando; + +support.detectDuplicates = !!hasDuplicate; + +setDocument(); + +support.sortDetached = assert(function( div1 ) { + return div1.compareDocumentPosition( document.createElement("div") ) & 1; +}); + +if ( !assert(function( div ) { + div.innerHTML = ""; + return div.firstChild.getAttribute("href") === "#" ; +}) ) { + addHandle( "type|href|height|width", function( elem, name, isXML ) { + if ( !isXML ) { + return elem.getAttribute( name, name.toLowerCase() === "type" ? 1 : 2 ); + } + }); +} + +if ( !support.attributes || !assert(function( div ) { + div.innerHTML = ""; + div.firstChild.setAttribute( "value", "" ); + return div.firstChild.getAttribute( "value" ) === ""; +}) ) { + addHandle( "value", function( elem, name, isXML ) { + if ( !isXML && elem.nodeName.toLowerCase() === "input" ) { + return elem.defaultValue; + } + }); +} + +if ( !assert(function( div ) { + return div.getAttribute("disabled") == null; +}) ) { + addHandle( booleans, function( elem, name, isXML ) { + var val; + if ( !isXML ) { + return elem[ name ] === true ? name.toLowerCase() : + (val = elem.getAttributeNode( name )) && val.specified ? + val.value : + null; + } + }); +} + +if ( typeof define === "function" && define.amd ) { + define(function() { return Sizzle; }); +} else if ( typeof module !== "undefined" && module.exports ) { + module.exports = Sizzle; +} else { + window.Sizzle = Sizzle; +} + +})( window ); + +;(function() { + if (typeof Sizzle !== 'undefined') { + return; + } + + if (typeof define !== 'undefined' && define.amd) { + window.Sizzle = Prototype._actual_sizzle; + window.define = Prototype._original_define; + delete Prototype._actual_sizzle; + delete Prototype._original_define; + } else if (typeof module !== 'undefined' && module.exports) { + window.Sizzle = module.exports; + module.exports = {}; + } +})(); + +;(function(engine) { + var extendElements = Prototype.Selector.extendElements; + + function select(selector, scope) { + return extendElements(engine(selector, scope || document)); + } + + function match(element, selector) { + return engine.matches(selector, [element]).length == 1; + } + + Prototype.Selector.engine = engine; + Prototype.Selector.select = select; + Prototype.Selector.match = match; +})(Sizzle); + +window.Sizzle = Prototype._original_property; +delete Prototype._original_property; + +var Form = { + reset: function(form) { + form = $(form); + form.reset(); + return form; + }, + + serializeElements: function(elements, options) { + if (typeof options != 'object') options = { hash: !!options }; + else if (Object.isUndefined(options.hash)) options.hash = true; + var key, value, submitted = false, submit = options.submit, accumulator, initial; + + if (options.hash) { + initial = {}; + accumulator = function(result, key, value) { + if (key in result) { + if (!Object.isArray(result[key])) result[key] = [result[key]]; + result[key] = result[key].concat(value); + } else result[key] = value; + return result; + }; + } else { + initial = ''; + accumulator = function(result, key, values) { + if (!Object.isArray(values)) {values = [values];} + if (!values.length) {return result;} + var encodedKey = encodeURIComponent(key).gsub(/%20/, '+'); + return result + (result ? "&" : "") + values.map(function (value) { + value = value.gsub(/(\r)?\n/, '\r\n'); + value = encodeURIComponent(value); + value = value.gsub(/%20/, '+'); + return encodedKey + "=" + value; + }).join("&"); + }; + } + + return elements.inject(initial, function(result, element) { + if (!element.disabled && element.name) { + key = element.name; value = $(element).getValue(); + if (value != null && element.type != 'file' && (element.type != 'submit' || (!submitted && + submit !== false && (!submit || key == submit) && (submitted = true)))) { + result = accumulator(result, key, value); + } + } + return result; + }); + } +}; + +Form.Methods = { + serialize: function(form, options) { + return Form.serializeElements(Form.getElements(form), options); + }, + + + getElements: function(form) { + var elements = $(form).getElementsByTagName('*'); + var element, results = [], serializers = Form.Element.Serializers; + + for (var i = 0; element = elements[i]; i++) { + if (serializers[element.tagName.toLowerCase()]) + results.push(Element.extend(element)); + } + return results; + }, + + getInputs: function(form, typeName, name) { + form = $(form); + var inputs = form.getElementsByTagName('input'); + + if (!typeName && !name) return $A(inputs).map(Element.extend); + + for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) { + var input = inputs[i]; + if ((typeName && input.type != typeName) || (name && input.name != name)) + continue; + matchingInputs.push(Element.extend(input)); + } + + return matchingInputs; + }, + + disable: function(form) { + form = $(form); + Form.getElements(form).invoke('disable'); + return form; + }, + + enable: function(form) { + form = $(form); + Form.getElements(form).invoke('enable'); + return form; + }, + + findFirstElement: function(form) { + var elements = $(form).getElements().findAll(function(element) { + return 'hidden' != element.type && !element.disabled; + }); + var firstByIndex = elements.findAll(function(element) { + return element.hasAttribute('tabIndex') && element.tabIndex >= 0; + }).sortBy(function(element) { return element.tabIndex }).first(); + + return firstByIndex ? firstByIndex : elements.find(function(element) { + return /^(?:input|select|textarea)$/i.test(element.tagName); + }); + }, + + focusFirstElement: function(form) { + form = $(form); + var element = form.findFirstElement(); + if (element) element.activate(); + return form; + }, + + request: function(form, options) { + form = $(form), options = Object.clone(options || { }); + + var params = options.parameters, action = form.readAttribute('action') || ''; + if (action.blank()) action = window.location.href; + options.parameters = form.serialize(true); + + if (params) { + if (Object.isString(params)) params = params.toQueryParams(); + Object.extend(options.parameters, params); + } + + if (form.hasAttribute('method') && !options.method) + options.method = form.method; + + return new Ajax.Request(action, options); + } +}; + +/*--------------------------------------------------------------------------*/ + + +Form.Element = { + focus: function(element) { + $(element).focus(); + return element; + }, + + select: function(element) { + $(element).select(); + return element; + } +}; + +Form.Element.Methods = { + + serialize: function(element) { + element = $(element); + if (!element.disabled && element.name) { + var value = element.getValue(); + if (value != undefined) { + var pair = { }; + pair[element.name] = value; + return Object.toQueryString(pair); + } + } + return ''; + }, + + getValue: function(element) { + element = $(element); + var method = element.tagName.toLowerCase(); + return Form.Element.Serializers[method](element); + }, + + setValue: function(element, value) { + element = $(element); + var method = element.tagName.toLowerCase(); + Form.Element.Serializers[method](element, value); + return element; + }, + + clear: function(element) { + $(element).value = ''; + return element; + }, + + present: function(element) { + return $(element).value != ''; + }, + + activate: function(element) { + element = $(element); + try { + element.focus(); + if (element.select && (element.tagName.toLowerCase() != 'input' || + !(/^(?:button|reset|submit)$/i.test(element.type)))) + element.select(); + } catch (e) { } + return element; + }, + + disable: function(element) { + element = $(element); + element.disabled = true; + return element; + }, + + enable: function(element) { + element = $(element); + element.disabled = false; + return element; + } +}; + +/*--------------------------------------------------------------------------*/ + +var Field = Form.Element; + +var $F = Form.Element.Methods.getValue; + +/*--------------------------------------------------------------------------*/ + +Form.Element.Serializers = (function() { + function input(element, value) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + return inputSelector(element, value); + default: + return valueSelector(element, value); + } + } + + function inputSelector(element, value) { + if (Object.isUndefined(value)) + return element.checked ? element.value : null; + else element.checked = !!value; + } + + function valueSelector(element, value) { + if (Object.isUndefined(value)) return element.value; + else element.value = value; + } + + function select(element, value) { + if (Object.isUndefined(value)) + return (element.type === 'select-one' ? selectOne : selectMany)(element); + + var opt, currentValue, single = !Object.isArray(value); + for (var i = 0, length = element.length; i < length; i++) { + opt = element.options[i]; + currentValue = this.optionValue(opt); + if (single) { + if (currentValue == value) { + opt.selected = true; + return; + } + } + else opt.selected = value.include(currentValue); + } + } + + function selectOne(element) { + var index = element.selectedIndex; + return index >= 0 ? optionValue(element.options[index]) : null; + } + + function selectMany(element) { + var values, length = element.length; + if (!length) return null; + + for (var i = 0, values = []; i < length; i++) { + var opt = element.options[i]; + if (opt.selected) values.push(optionValue(opt)); + } + return values; + } + + function optionValue(opt) { + return Element.hasAttribute(opt, 'value') ? opt.value : opt.text; + } + + return { + input: input, + inputSelector: inputSelector, + textarea: valueSelector, + select: select, + selectOne: selectOne, + selectMany: selectMany, + optionValue: optionValue, + button: valueSelector + }; +})(); + +/*--------------------------------------------------------------------------*/ + + +Abstract.TimedObserver = Class.create(PeriodicalExecuter, { + initialize: function($super, element, frequency, callback) { + $super(callback, frequency); + this.element = $(element); + this.lastValue = this.getValue(); + }, + + execute: function() { + var value = this.getValue(); + if (Object.isString(this.lastValue) && Object.isString(value) ? + this.lastValue != value : String(this.lastValue) != String(value)) { + this.callback(this.element, value); + this.lastValue = value; + } + } +}); + +Form.Element.Observer = Class.create(Abstract.TimedObserver, { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.Observer = Class.create(Abstract.TimedObserver, { + getValue: function() { + return Form.serialize(this.element); + } +}); + +/*--------------------------------------------------------------------------*/ + +Abstract.EventObserver = Class.create({ + initialize: function(element, callback) { + this.element = $(element); + this.callback = callback; + + this.lastValue = this.getValue(); + if (this.element.tagName.toLowerCase() == 'form') + this.registerFormCallbacks(); + else + this.registerCallback(this.element); + }, + + onElementEvent: function() { + var value = this.getValue(); + if (this.lastValue != value) { + this.callback(this.element, value); + this.lastValue = value; + } + }, + + registerFormCallbacks: function() { + Form.getElements(this.element).each(this.registerCallback, this); + }, + + registerCallback: function(element) { + if (element.type) { + switch (element.type.toLowerCase()) { + case 'checkbox': + case 'radio': + Event.observe(element, 'click', this.onElementEvent.bind(this)); + break; + default: + Event.observe(element, 'change', this.onElementEvent.bind(this)); + break; + } + } + } +}); + +Form.Element.EventObserver = Class.create(Abstract.EventObserver, { + getValue: function() { + return Form.Element.getValue(this.element); + } +}); + +Form.EventObserver = Class.create(Abstract.EventObserver, { + getValue: function() { + return Form.serialize(this.element); + } +}); +(function(GLOBAL) { + var DIV = document.createElement('div'); + var docEl = document.documentElement; + var MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED = 'onmouseenter' in docEl + && 'onmouseleave' in docEl; + + var Event = { + KEY_BACKSPACE: 8, + KEY_TAB: 9, + KEY_RETURN: 13, + KEY_ESC: 27, + KEY_LEFT: 37, + KEY_UP: 38, + KEY_RIGHT: 39, + KEY_DOWN: 40, + KEY_DELETE: 46, + KEY_HOME: 36, + KEY_END: 35, + KEY_PAGEUP: 33, + KEY_PAGEDOWN: 34, + KEY_INSERT: 45 + }; + + + var isIELegacyEvent = function(event) { return false; }; + + if (window.attachEvent) { + if (window.addEventListener) { + isIELegacyEvent = function(event) { + return !(event instanceof window.Event); + }; + } else { + isIELegacyEvent = function(event) { return true; }; + } + } + + var _isButton; + + function _isButtonForDOMEvents(event, code) { + return event.which ? (event.which === code + 1) : (event.button === code); + } + + var legacyButtonMap = { 0: 1, 1: 4, 2: 2 }; + function _isButtonForLegacyEvents(event, code) { + return event.button === legacyButtonMap[code]; + } + + function _isButtonForWebKit(event, code) { + switch (code) { + case 0: return event.which == 1 && !event.metaKey; + case 1: return event.which == 2 || (event.which == 1 && event.metaKey); + case 2: return event.which == 3; + default: return false; + } + } + + if (window.attachEvent) { + if (!window.addEventListener) { + _isButton = _isButtonForLegacyEvents; + } else { + _isButton = function(event, code) { + return isIELegacyEvent(event) ? _isButtonForLegacyEvents(event, code) : + _isButtonForDOMEvents(event, code); + } + } + } else if (Prototype.Browser.WebKit) { + _isButton = _isButtonForWebKit; + } else { + _isButton = _isButtonForDOMEvents; + } + + function isLeftClick(event) { return _isButton(event, 0) } + + function isMiddleClick(event) { return _isButton(event, 1) } + + function isRightClick(event) { return _isButton(event, 2) } + + function element(event) { + return Element.extend(_element(event)); + } + + function _element(event) { + event = Event.extend(event); + + var node = event.target, type = event.type, + currentTarget = event.currentTarget; + + if (currentTarget && currentTarget.tagName) { + if (type === 'load' || type === 'error' || + (type === 'click' && currentTarget.tagName.toLowerCase() === 'input' + && currentTarget.type === 'radio')) + node = currentTarget; + } + + return node.nodeType == Node.TEXT_NODE ? node.parentNode : node; + } + + function findElement(event, expression) { + var element = _element(event), selector = Prototype.Selector; + if (!expression) return Element.extend(element); + while (element) { + if (Object.isElement(element) && selector.match(element, expression)) + return Element.extend(element); + element = element.parentNode; + } + } + + function pointer(event) { + return { x: pointerX(event), y: pointerY(event) }; + } + + function pointerX(event) { + var docElement = document.documentElement, + body = document.body || { scrollLeft: 0 }; + + return event.pageX || (event.clientX + + (docElement.scrollLeft || body.scrollLeft) - + (docElement.clientLeft || 0)); + } + + function pointerY(event) { + var docElement = document.documentElement, + body = document.body || { scrollTop: 0 }; + + return event.pageY || (event.clientY + + (docElement.scrollTop || body.scrollTop) - + (docElement.clientTop || 0)); + } + + + function stop(event) { + Event.extend(event); + event.preventDefault(); + event.stopPropagation(); + + event.stopped = true; + } + + + Event.Methods = { + isLeftClick: isLeftClick, + isMiddleClick: isMiddleClick, + isRightClick: isRightClick, + + element: element, + findElement: findElement, + + pointer: pointer, + pointerX: pointerX, + pointerY: pointerY, + + stop: stop + }; + + var methods = Object.keys(Event.Methods).inject({ }, function(m, name) { + m[name] = Event.Methods[name].methodize(); + return m; + }); + + if (window.attachEvent) { + function _relatedTarget(event) { + var element; + switch (event.type) { + case 'mouseover': + case 'mouseenter': + element = event.fromElement; + break; + case 'mouseout': + case 'mouseleave': + element = event.toElement; + break; + default: + return null; + } + return Element.extend(element); + } + + var additionalMethods = { + stopPropagation: function() { this.cancelBubble = true }, + preventDefault: function() { this.returnValue = false }, + inspect: function() { return '[object Event]' } + }; + + Event.extend = function(event, element) { + if (!event) return false; + + if (!isIELegacyEvent(event)) return event; + + if (event._extendedByPrototype) return event; + event._extendedByPrototype = Prototype.emptyFunction; + + var pointer = Event.pointer(event); + + Object.extend(event, { + target: event.srcElement || element, + relatedTarget: _relatedTarget(event), + pageX: pointer.x, + pageY: pointer.y + }); + + Object.extend(event, methods); + Object.extend(event, additionalMethods); + + return event; + }; + } else { + Event.extend = Prototype.K; + } + + if (window.addEventListener) { + Event.prototype = window.Event.prototype || document.createEvent('HTMLEvents').__proto__; + Object.extend(Event.prototype, methods); + } + + var EVENT_TRANSLATIONS = { + mouseenter: 'mouseover', + mouseleave: 'mouseout' + }; + + function getDOMEventName(eventName) { + return EVENT_TRANSLATIONS[eventName] || eventName; + } + + if (MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED) + getDOMEventName = Prototype.K; + + function getUniqueElementID(element) { + if (element === window) return 0; + + if (typeof element._prototypeUID === 'undefined') + element._prototypeUID = Element.Storage.UID++; + return element._prototypeUID; + } + + function getUniqueElementID_IE(element) { + if (element === window) return 0; + if (element == document) return 1; + return element.uniqueID; + } + + if ('uniqueID' in DIV) + getUniqueElementID = getUniqueElementID_IE; + + function isCustomEvent(eventName) { + return eventName.include(':'); + } + + Event._isCustomEvent = isCustomEvent; + + function getOrCreateRegistryFor(element, uid) { + var CACHE = GLOBAL.Event.cache; + if (Object.isUndefined(uid)) + uid = getUniqueElementID(element); + if (!CACHE[uid]) CACHE[uid] = { element: element }; + return CACHE[uid]; + } + + function destroyRegistryForElement(element, uid) { + if (Object.isUndefined(uid)) + uid = getUniqueElementID(element); + delete GLOBAL.Event.cache[uid]; + } + + + function register(element, eventName, handler) { + var registry = getOrCreateRegistryFor(element); + if (!registry[eventName]) registry[eventName] = []; + var entries = registry[eventName]; + + var i = entries.length; + while (i--) + if (entries[i].handler === handler) return null; + + var uid = getUniqueElementID(element); + var responder = GLOBAL.Event._createResponder(uid, eventName, handler); + var entry = { + responder: responder, + handler: handler + }; + + entries.push(entry); + return entry; + } + + function unregister(element, eventName, handler) { + var registry = getOrCreateRegistryFor(element); + var entries = registry[eventName] || []; + + var i = entries.length, entry; + while (i--) { + if (entries[i].handler === handler) { + entry = entries[i]; + break; + } + } + + if (entry) { + var index = entries.indexOf(entry); + entries.splice(index, 1); + } + + if (entries.length === 0) { + delete registry[eventName]; + if (Object.keys(registry).length === 1 && ('element' in registry)) + destroyRegistryForElement(element); + } + + return entry; + } + + + function observe(element, eventName, handler) { + element = $(element); + var entry = register(element, eventName, handler); + + if (entry === null) return element; + + var responder = entry.responder; + if (isCustomEvent(eventName)) + observeCustomEvent(element, eventName, responder); + else + observeStandardEvent(element, eventName, responder); + + return element; + } + + function observeStandardEvent(element, eventName, responder) { + var actualEventName = getDOMEventName(eventName); + if (element.addEventListener) { + element.addEventListener(actualEventName, responder, false); + } else { + element.attachEvent('on' + actualEventName, responder); + } + } + + function observeCustomEvent(element, eventName, responder) { + if (element.addEventListener) { + element.addEventListener('dataavailable', responder, false); + } else { + element.attachEvent('ondataavailable', responder); + element.attachEvent('onlosecapture', responder); + } + } + + function stopObserving(element, eventName, handler) { + element = $(element); + var handlerGiven = !Object.isUndefined(handler), + eventNameGiven = !Object.isUndefined(eventName); + + if (!eventNameGiven && !handlerGiven) { + stopObservingElement(element); + return element; + } + + if (!handlerGiven) { + stopObservingEventName(element, eventName); + return element; + } + + var entry = unregister(element, eventName, handler); + + if (!entry) return element; + removeEvent(element, eventName, entry.responder); + return element; + } + + function stopObservingStandardEvent(element, eventName, responder) { + var actualEventName = getDOMEventName(eventName); + if (element.removeEventListener) { + element.removeEventListener(actualEventName, responder, false); + } else { + element.detachEvent('on' + actualEventName, responder); + } + } + + function stopObservingCustomEvent(element, eventName, responder) { + if (element.removeEventListener) { + element.removeEventListener('dataavailable', responder, false); + } else { + element.detachEvent('ondataavailable', responder); + element.detachEvent('onlosecapture', responder); + } + } + + + + function stopObservingElement(element) { + var uid = getUniqueElementID(element), registry = GLOBAL.Event.cache[uid]; + if (!registry) return; + + destroyRegistryForElement(element, uid); + + var entries, i; + for (var eventName in registry) { + if (eventName === 'element') continue; + + entries = registry[eventName]; + i = entries.length; + while (i--) + removeEvent(element, eventName, entries[i].responder); + } + } + + function stopObservingEventName(element, eventName) { + var registry = getOrCreateRegistryFor(element); + var entries = registry[eventName]; + if (entries) { + delete registry[eventName]; + } + + entries = entries || []; + + var i = entries.length; + while (i--) + removeEvent(element, eventName, entries[i].responder); + + for (var name in registry) { + if (name === 'element') continue; + return; // There is another registered event + } + + destroyRegistryForElement(element); + } + + + function removeEvent(element, eventName, handler) { + if (isCustomEvent(eventName)) + stopObservingCustomEvent(element, eventName, handler); + else + stopObservingStandardEvent(element, eventName, handler); + } + + + + function getFireTarget(element) { + if (element !== document) return element; + if (document.createEvent && !element.dispatchEvent) + return document.documentElement; + return element; + } + + function fire(element, eventName, memo, bubble) { + element = getFireTarget($(element)); + if (Object.isUndefined(bubble)) bubble = true; + memo = memo || {}; + + var event = fireEvent(element, eventName, memo, bubble); + return Event.extend(event); + } + + function fireEvent_DOM(element, eventName, memo, bubble) { + var event = document.createEvent('HTMLEvents'); + event.initEvent('dataavailable', bubble, true); + + event.eventName = eventName; + event.memo = memo; + + element.dispatchEvent(event); + return event; + } + + function fireEvent_IE(element, eventName, memo, bubble) { + var event = document.createEventObject(); + event.eventType = bubble ? 'ondataavailable' : 'onlosecapture'; + + event.eventName = eventName; + event.memo = memo; + + element.fireEvent(event.eventType, event); + return event; + } + + var fireEvent = document.createEvent ? fireEvent_DOM : fireEvent_IE; + + + + Event.Handler = Class.create({ + initialize: function(element, eventName, selector, callback) { + this.element = $(element); + this.eventName = eventName; + this.selector = selector; + this.callback = callback; + this.handler = this.handleEvent.bind(this); + }, + + + start: function() { + Event.observe(this.element, this.eventName, this.handler); + return this; + }, + + stop: function() { + Event.stopObserving(this.element, this.eventName, this.handler); + return this; + }, + + handleEvent: function(event) { + var element = Event.findElement(event, this.selector); + if (element) this.callback.call(this.element, event, element); + } + }); + + function on(element, eventName, selector, callback) { + element = $(element); + if (Object.isFunction(selector) && Object.isUndefined(callback)) { + callback = selector, selector = null; + } + + return new Event.Handler(element, eventName, selector, callback).start(); + } + + Object.extend(Event, Event.Methods); + + Object.extend(Event, { + fire: fire, + observe: observe, + stopObserving: stopObserving, + on: on + }); + + Element.addMethods({ + fire: fire, + + observe: observe, + + stopObserving: stopObserving, + + on: on + }); + + Object.extend(document, { + fire: fire.methodize(), + + observe: observe.methodize(), + + stopObserving: stopObserving.methodize(), + + on: on.methodize(), + + loaded: false + }); + + if (GLOBAL.Event) Object.extend(window.Event, Event); + else GLOBAL.Event = Event; + + GLOBAL.Event.cache = {}; + + function destroyCache_IE() { + GLOBAL.Event.cache = null; + } + + if (window.attachEvent) + window.attachEvent('onunload', destroyCache_IE); + + DIV = null; + docEl = null; +})(this); + +(function(GLOBAL) { + /* Code for creating leak-free event responders is based on work by + John-David Dalton. */ + + var docEl = document.documentElement; + var MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED = 'onmouseenter' in docEl + && 'onmouseleave' in docEl; + + function isSimulatedMouseEnterLeaveEvent(eventName) { + return !MOUSEENTER_MOUSELEAVE_EVENTS_SUPPORTED && + (eventName === 'mouseenter' || eventName === 'mouseleave'); + } + + function createResponder(uid, eventName, handler) { + if (Event._isCustomEvent(eventName)) + return createResponderForCustomEvent(uid, eventName, handler); + if (isSimulatedMouseEnterLeaveEvent(eventName)) + return createMouseEnterLeaveResponder(uid, eventName, handler); + + return function(event) { + if (!Event.cache) return; + + var element = Event.cache[uid].element; + Event.extend(event, element); + handler.call(element, event); + }; + } + + function createResponderForCustomEvent(uid, eventName, handler) { + return function(event) { + var cache = Event.cache[uid]; + var element = cache && cache.element; + + if (Object.isUndefined(event.eventName)) + return false; + + if (event.eventName !== eventName) + return false; + + Event.extend(event, element); + handler.call(element, event); + }; + } + + function createMouseEnterLeaveResponder(uid, eventName, handler) { + return function(event) { + var element = Event.cache[uid].element; + + Event.extend(event, element); + var parent = event.relatedTarget; + + while (parent && parent !== element) { + try { parent = parent.parentNode; } + catch(e) { parent = element; } + } + + if (parent === element) return; + handler.call(element, event); + } + } + + GLOBAL.Event._createResponder = createResponder; + docEl = null; +})(this); + +(function(GLOBAL) { + /* Support for the DOMContentLoaded event is based on work by Dan Webb, + Matthias Miller, Dean Edwards, John Resig, and Diego Perini. */ + + var TIMER; + + function fireContentLoadedEvent() { + if (document.loaded) return; + if (TIMER) window.clearTimeout(TIMER); + document.loaded = true; + document.fire('dom:loaded'); + } + + function checkReadyState() { + if (document.readyState === 'complete') { + document.detachEvent('onreadystatechange', checkReadyState); + fireContentLoadedEvent(); + } + } + + function pollDoScroll() { + try { + document.documentElement.doScroll('left'); + } catch (e) { + TIMER = pollDoScroll.defer(); + return; + } + + fireContentLoadedEvent(); + } + + + if (document.readyState === 'complete') { + fireContentLoadedEvent(); + return; + } + + if (document.addEventListener) { + document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false); + } else { + document.attachEvent('onreadystatechange', checkReadyState); + if (window == top) TIMER = pollDoScroll.defer(); + } + + Event.observe(window, 'load', fireContentLoadedEvent); +})(this); + + +Element.addMethods(); +/*------------------------------- DEPRECATED -------------------------------*/ + +Hash.toQueryString = Object.toQueryString; + +var Toggle = { display: Element.toggle }; + +Element.addMethods({ + childOf: Element.Methods.descendantOf +}); + +var Insertion = { + Before: function(element, content) { + return Element.insert(element, {before:content}); + }, + + Top: function(element, content) { + return Element.insert(element, {top:content}); + }, + + Bottom: function(element, content) { + return Element.insert(element, {bottom:content}); + }, + + After: function(element, content) { + return Element.insert(element, {after:content}); + } +}; + +var $continue = new Error('"throw $continue" is deprecated, use "return" instead'); + +var Position = { + includeScrollOffsets: false, + + prepare: function() { + this.deltaX = window.pageXOffset + || document.documentElement.scrollLeft + || document.body.scrollLeft + || 0; + this.deltaY = window.pageYOffset + || document.documentElement.scrollTop + || document.body.scrollTop + || 0; + }, + + within: function(element, x, y) { + if (this.includeScrollOffsets) + return this.withinIncludingScrolloffsets(element, x, y); + this.xcomp = x; + this.ycomp = y; + this.offset = Element.cumulativeOffset(element); + + return (y >= this.offset[1] && + y < this.offset[1] + element.offsetHeight && + x >= this.offset[0] && + x < this.offset[0] + element.offsetWidth); + }, + + withinIncludingScrolloffsets: function(element, x, y) { + var offsetcache = Element.cumulativeScrollOffset(element); + + this.xcomp = x + offsetcache[0] - this.deltaX; + this.ycomp = y + offsetcache[1] - this.deltaY; + this.offset = Element.cumulativeOffset(element); + + return (this.ycomp >= this.offset[1] && + this.ycomp < this.offset[1] + element.offsetHeight && + this.xcomp >= this.offset[0] && + this.xcomp < this.offset[0] + element.offsetWidth); + }, + + overlap: function(mode, element) { + if (!mode) return 0; + if (mode == 'vertical') + return ((this.offset[1] + element.offsetHeight) - this.ycomp) / + element.offsetHeight; + if (mode == 'horizontal') + return ((this.offset[0] + element.offsetWidth) - this.xcomp) / + element.offsetWidth; + }, + + + cumulativeOffset: Element.Methods.cumulativeOffset, + + positionedOffset: Element.Methods.positionedOffset, + + absolutize: function(element) { + Position.prepare(); + return Element.absolutize(element); + }, + + relativize: function(element) { + Position.prepare(); + return Element.relativize(element); + }, + + realOffset: Element.Methods.cumulativeScrollOffset, + + offsetParent: Element.Methods.getOffsetParent, + + page: Element.Methods.viewportOffset, + + clone: function(source, target, options) { + options = options || { }; + return Element.clonePosition(target, source, options); + } +}; + +/*--------------------------------------------------------------------------*/ + +if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ + function iter(name) { + return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]"; + } + + instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ? + function(element, className) { + className = className.toString().strip(); + var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className); + return cond ? document._getElementsByXPath('.//*' + cond, element) : []; + } : function(element, className) { + className = className.toString().strip(); + var elements = [], classNames = (/\s/.test(className) ? $w(className) : null); + if (!classNames && !className) return elements; + + var nodes = $(element).getElementsByTagName('*'); + className = ' ' + className + ' '; + + for (var i = 0, child, cn; child = nodes[i]; i++) { + if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) || + (classNames && classNames.all(function(name) { + return !name.toString().blank() && cn.include(' ' + name + ' '); + })))) + elements.push(Element.extend(child)); + } + return elements; + }; + + return function(className, parentElement) { + return $(parentElement || document.body).getElementsByClassName(className); + }; +}(Element.Methods); + +/*--------------------------------------------------------------------------*/ + +Element.ClassNames = Class.create(); +Element.ClassNames.prototype = { + initialize: function(element) { + this.element = $(element); + }, + + _each: function(iterator, context) { + this.element.className.split(/\s+/).select(function(name) { + return name.length > 0; + })._each(iterator, context); + }, + + set: function(className) { + this.element.className = className; + }, + + add: function(classNameToAdd) { + if (this.include(classNameToAdd)) return; + this.set($A(this).concat(classNameToAdd).join(' ')); + }, + + remove: function(classNameToRemove) { + if (!this.include(classNameToRemove)) return; + this.set($A(this).without(classNameToRemove).join(' ')); + }, + + toString: function() { + return $A(this).join(' '); + } +}; + +Object.extend(Element.ClassNames.prototype, Enumerable); + +/*--------------------------------------------------------------------------*/ + +(function() { + window.Selector = Class.create({ + initialize: function(expression) { + this.expression = expression.strip(); + }, + + findElements: function(rootElement) { + return Prototype.Selector.select(this.expression, rootElement); + }, + + match: function(element) { + return Prototype.Selector.match(element, this.expression); + }, + + toString: function() { + return this.expression; + }, + + inspect: function() { + return "#"; + } + }); + + Object.extend(Selector, { + matchElements: function(elements, expression) { + var match = Prototype.Selector.match, + results = []; + + for (var i = 0, length = elements.length; i < length; i++) { + var element = elements[i]; + if (match(element, expression)) { + results.push(Element.extend(element)); + } + } + return results; + }, + + findElement: function(elements, expression, index) { + index = index || 0; + var matchIndex = 0, element; + for (var i = 0, length = elements.length; i < length; i++) { + element = elements[i]; + if (Prototype.Selector.match(element, expression) && index === matchIndex++) { + return Element.extend(element); + } + } + }, + + findChildElements: function(element, expressions) { + var selector = expressions.toArray().join(', '); + return Prototype.Selector.select(selector, element || document); + } + }); +})(); diff --git a/zookeeper-docs/src/main/resources/markdown/skin/screen.css b/zookeeper-docs/src/main/resources/markdown/skin/screen.css new file mode 100644 index 0000000..9ce32c2 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/skin/screen.css @@ -0,0 +1,531 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one or more +* contributor license agreements. See the NOTICE file distributed with +* this work for additional information regarding copyright ownership. +* The ASF licenses this file to You 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. +*/ +body { margin: 0px 0px 0px 0px; font-family: Verdana, Helvetica, sans-serif; } + +h1 { font-size : 160%; margin: 0px 0px 0px 0px; padding: 0px; } +h2 { font-size : 140%; margin: 1em 0px 0.8em 0px; padding: 0px; font-weight : bold;} +h3 { font-size : 130%; margin: 0.8em 0px 0px 0px; padding: 0px; font-weight : bold; } +.h3 { margin: 22px 0px 3px 0px; } +h4 { font-size : 120%; margin: 0.7em 0px 0px 0px; padding: 0px; font-weight : normal; text-align: left; } +.h4 { margin: 18px 0px 0px 0px; } +h4.faq { font-size : 120%; margin: 18px 0px 0px 0px; padding: 0px; font-weight : bold; text-align: left; } +h5 { font-size : 100%; margin: 14px 0px 0px 0px; padding: 0px; font-weight : normal; text-align: left; } + +/** +* table +*/ +table .title { background-color: #000000; } +.ForrestTable { + color: #ffffff; + background-color: #7099C5; + width: 100%; + font-size : 100%; + empty-cells: show; +} +table caption { + padding-left: 5px; + color: white; + text-align: left; + font-weight: bold; + background-color: #000000; +} +.ForrestTable td { + color: black; + background-color: #f0f0ff; +} +.ForrestTable th { text-align: center; } +/** + * Page Header + */ + +#top { + position: relative; + float: left; + width: 100%; + background: #294563; /* if you want a background in the header, put it here */ +} + +#top .breadtrail { + background: #CFDCED; + color: black; + border-bottom: solid 1px white; + padding: 3px 10px; + font-size: 75%; +} +#top .breadtrail a { color: black; } + +#top .header { + float: left; + width: 100%; + background: url("header_white_line.gif") repeat-x bottom; +} + +#top .grouplogo { + padding: 7px 0 10px 10px; + float: left; + text-align: left; +} +#top .projectlogo { + padding: 7px 0 10px 10px; + float: left; + width: 33%; + text-align: right; +} +#top .projectlogoA1 { + padding: 7px 0 10px 10px; + float: right; +} +html>body #top .searchbox { + bottom: 0px; +} +#top .searchbox { + position: absolute; + right: 10px; + height: 42px; + font-size: 70%; + white-space: nowrap; + bottom: -1px; /* compensate for IE rendering issue */ + border-radius: 5px 5px 0px 0px; +} + +#top .searchbox form { + padding: 5px 10px; + margin: 0; +} +#top .searchbox p { + padding: 0 0 2px 0; + margin: 0; +} +#top .searchbox input { + font-size: 100%; +} + +#tabs { + clear: both; + padding-left: 10px; + margin: 0; + list-style: none; +} + +#tabs li { + float: left; + margin: 0 3px 0 0; + padding: 0; + border-radius: 5px 5px 0px 0px; +} + +/*background: url("tab-left.gif") no-repeat left top;*/ +#tabs li a { + float: left; + display: block; + font-family: verdana, arial, sans-serif; + text-decoration: none; + color: black; + white-space: nowrap; + padding: 5px 15px 4px; + width: .1em; /* IE/Win fix */ +} + +#tabs li a:hover { + + cursor: pointer; + text-decoration:underline; +} + +#tabs > li a { width: auto; } /* Rest of IE/Win fix */ + +/* Commented Backslash Hack hides rule from IE5-Mac \*/ +#tabs a { float: none; } +/* End IE5-Mac hack */ + +#top .header .current { + background-color: #4C6C8F; +} +#top .header .current a { + font-weight: bold; + padding-bottom: 5px; + color: white; +} +#publishedStrip { + padding-right: 10px; + padding-left: 20px; + padding-top: 3px; + padding-bottom:3px; + color: #ffffff; + font-size : 60%; + font-weight: bold; + background-color: #4C6C8F; + text-align:right; +} + +#level2tabs { +margin: 0; +float:left; +position:relative; + +} + + + +#level2tabs a:hover { + + cursor: pointer; + text-decoration:underline; + +} + +#level2tabs a{ + + cursor: pointer; + text-decoration:none; + background-image: url('chapter.gif'); + background-repeat: no-repeat; + background-position: center left; + padding-left: 6px; + margin-left: 6px; +} + +/* +* border-top: solid #4C6C8F 15px; +*/ +#main { + position: relative; + background: white; + clear:both; +} +#main .breadtrail { + clear:both; + position: relative; + background: #CFDCED; + color: black; + border-bottom: solid 1px black; + border-top: solid 1px black; + padding: 0px 180px; + font-size: 75%; + z-index:10; +} + +img.corner { + width: 15px; + height: 15px; + border: none; + display: block !important; +} + +img.cornersmall { + width: 5px; + height: 5px; + border: none; + display: block !important; +} +/** + * Side menu + */ +#menu a { font-weight: normal; text-decoration: none;} +#menu a:visited { font-weight: normal; } +#menu a:active { font-weight: normal; } +#menu a:hover { font-weight: normal; text-decoration:underline;} + +#menuarea { width:10em;} +#menu { + position: relative; + float: left; + width: 160px; + padding-top: 0px; + padding-bottom: 15px; + top:-18px; + left:10px; + z-index: 20; + background-color: #f90; + font-size : 70%; + border-radius: 0px 0px 15px 15px; +} + +.menutitle { + cursor:pointer; + padding: 3px 12px; + margin-left: 10px; + background-image: url('chapter.gif'); + background-repeat: no-repeat; + background-position: center left; + font-weight : bold; +} + +.menutitle.selected { + background-image: url('chapter_open.gif'); +} + +.menutitle:hover{text-decoration:underline;cursor: pointer;} + +#menu .menuitemgroup { + margin: 0px 0px 6px 8px; + padding: 0px; + font-weight : bold; } + +#menu .selectedmenuitemgroup{ + margin: 0px 0px 0px 8px; + padding: 0px; + font-weight : normal; + + } + +#menu .menuitem { + padding: 2px 0px 1px 13px; + background-image: url('page.gif'); + background-repeat: no-repeat; + background-position: center left; + font-weight : normal; + margin-left: 10px; +} + +#menu .selected { + font-style : normal; + margin-right: 10px; + +} +.menuitem .selected { + border-style: solid; + border-width: 1px; +} +#menu .menupageitemgroup { + padding: 3px 0px 4px 6px; + font-style : normal; + border-bottom: 1px solid ; + border-left: 1px solid ; + border-right: 1px solid ; + margin-right: 10px; +} +#menu .menupageitem { + font-style : normal; + font-weight : normal; + border-width: 0px; + font-size : 90%; +} +#menu .searchbox { + text-align: center; +} +#menu .searchbox form { + padding: 3px 3px; + margin: 0; +} +#menu .searchbox input { + font-size: 100%; +} + +#content { + padding: 20px 20px 20px 180px; + margin: 0; + font : small Verdana, Helvetica, sans-serif; + font-size : 80%; +} + +#content ul { + margin: 0; + padding: 0 25px; +} +#content li { + padding: 0 5px; +} +#feedback { + color: black; + background: #CFDCED; + text-align:center; + margin-top: 5px; +} +#feedback #feedbackto { + font-size: 90%; + color: black; +} +#footer { + clear: both; + position: relative; /* IE bugfix (http://www.dracos.co.uk/web/css/ie6floatbug/) */ + width: 100%; + background: #CFDCED; + border-top: solid 1px #4C6C8F; + color: black; +} +#footer .copyright { + position: relative; /* IE bugfix cont'd */ + padding: 5px; + margin: 0; + width: 60%; +} +#footer .lastmodified { + position: relative; /* IE bugfix cont'd */ + float: right; + width: 30%; + padding: 5px; + margin: 0; + text-align: right; +} +#footer a { color: white; } + +#footer #logos { + text-align: left; +} + + +/** + * Misc Styles + */ + +acronym { cursor: help; } +.boxed { background-color: #a5b6c6;} +.underlined_5 {border-bottom: solid 5px #4C6C8F;} +.underlined_10 {border-bottom: solid 10px #4C6C8F;} +/* ==================== snail trail ============================ */ + +.trail { + position: relative; /* IE bugfix cont'd */ + font-size: 70%; + text-align: right; + float: right; + margin: -10px 5px 0px 5px; + padding: 0; +} + +#motd-area { + position:relative; + float:right; + width: 35%; + background-color: #f0f0ff; + border: solid 1px #4C6C8F; + margin: 0px 0px 10px 10px; + padding: 5px; +} + +#minitoc-area { + border-top: solid 1px #4C6C8F; + border-bottom: solid 1px #4C6C8F; + margin: 15px 10% 5px 15px; + /* margin-bottom: 15px; + margin-left: 15px; + margin-right: 10%;*/ + padding-bottom: 7px; + padding-top: 5px; +} +.minitoc { + list-style-image: url('current.gif'); + font-weight: normal; +} + +.abstract{ + text-align:justify; + } + +li p { + margin: 0; + padding: 0; +} + +.pdflink { + position: relative; /* IE bugfix cont'd */ + float: right; + margin: 0px 5px; + padding: 0; +} +.pdflink br { + margin-top: -10px; + padding-left: 1px; +} +.pdflink a { + display: block; + font-size: 70%; + text-align: center; + margin: 0; + padding: 0; +} + +.pdflink img { + display: block; + height: 16px; + width: 16px; +} +.xmllink { + position: relative; /* IE bugfix cont'd */ + float: right; + margin: 0px 5px; + padding: 0; +} +.xmllink br { + margin-top: -10px; + padding-left: 1px; +} +.xmllink a { + display: block; + font-size: 70%; + text-align: center; + margin: 0; + padding: 0; +} + +.xmllink img { + display: block; + height: 16px; + width: 16px; +} +.podlink { + position: relative; /* IE bugfix cont'd */ + float: right; + margin: 0px 5px; + padding: 0; +} +.podlink br { + margin-top: -10px; + padding-left: 1px; +} +.podlink a { + display: block; + font-size: 70%; + text-align: center; + margin: 0; + padding: 0; +} + +.podlink img { + display: block; + height: 16px; + width: 16px; +} + +.printlink { + position: relative; /* IE bugfix cont'd */ + float: right; +} +.printlink br { + margin-top: -10px; + padding-left: 1px; +} +.printlink a { + display: block; + font-size: 70%; + text-align: center; + margin: 0; + padding: 0; +} +.printlink img { + display: block; + height: 16px; + width: 16px; +} + +p.instruction { + display: list-item; + list-style-image: url('../instruction_arrow.png'); + list-style-position: outside; + margin-left: 2em; +} \ No newline at end of file diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperAdmin.md b/zookeeper-docs/src/main/resources/markdown/zookeeperAdmin.md new file mode 100644 index 0000000..2b78564 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperAdmin.md @@ -0,0 +1,2546 @@ + + +# ZooKeeper Administrator's Guide + +### A Guide to Deployment and Administration + +* [Deployment](#ch_deployment) + * [System Requirements](#sc_systemReq) + * [Supported Platforms](#sc_supportedPlatforms) + * [Required Software](#sc_requiredSoftware) + * [Clustered (Multi-Server) Setup](#sc_zkMulitServerSetup) + * [Single Server and Developer Setup](#sc_singleAndDevSetup) +* [Administration](#ch_administration) + * [Designing a ZooKeeper Deployment](#sc_designing) + * [Cross Machine Requirements](#sc_CrossMachineRequirements) + * [Single Machine Requirements](#Single+Machine+Requirements) + * [Provisioning](#sc_provisioning) + * [Things to Consider: ZooKeeper Strengths and Limitations](#sc_strengthsAndLimitations) + * [Administering](#sc_administering) + * [Maintenance](#sc_maintenance) + * [Ongoing Data Directory Cleanup](#Ongoing+Data+Directory+Cleanup) + * [Debug Log Cleanup (log4j)](#Debug+Log+Cleanup+%28log4j%29) + * [Supervision](#sc_supervision) + * [Monitoring](#sc_monitoring) + * [Logging](#sc_logging) + * [Troubleshooting](#sc_troubleshooting) + * [Configuration Parameters](#sc_configuration) + * [Minimum Configuration](#sc_minimumConfiguration) + * [Advanced Configuration](#sc_advancedConfiguration) + * [Cluster Options](#sc_clusterOptions) + * [Encryption, Authentication, Authorization Options](#sc_authOptions) + * [Experimental Options/Features](#Experimental+Options%2FFeatures) + * [Unsafe Options](#Unsafe+Options) + * [Disabling data directory autocreation](#Disabling+data+directory+autocreation) + * [Enabling db existence validation](#sc_db_existence_validation) + * [Performance Tuning Options](#sc_performance_options) + * [AdminServer configuration](#sc_adminserver_config) + * [Communication using the Netty framework](#Communication+using+the+Netty+framework) + * [Quorum TLS](#Quorum+TLS) + * [Upgrading existing non-TLS cluster with no downtime](#Upgrading+existing+nonTLS+cluster) + * [ZooKeeper Commands](#sc_zkCommands) + * [The Four Letter Words](#sc_4lw) + * [The AdminServer](#sc_adminserver) + * [Data File Management](#sc_dataFileManagement) + * [The Data Directory](#The+Data+Directory) + * [The Log Directory](#The+Log+Directory) + * [File Management](#sc_filemanagement) + * [Recovery - TxnLogToolkit](#Recovery+-+TxnLogToolkit) + * [Things to Avoid](#sc_commonProblems) + * [Best Practices](#sc_bestPractices) + + + +## Deployment + +This section contains information about deploying Zookeeper and +covers these topics: + +* [System Requirements](#sc_systemReq) +* [Clustered (Multi-Server) Setup](#sc_zkMulitServerSetup) +* [Single Server and Developer Setup](#sc_singleAndDevSetup) + +The first two sections assume you are interested in installing +ZooKeeper in a production environment such as a datacenter. The final +section covers situations in which you are setting up ZooKeeper on a +limited basis - for evaluation, testing, or development - but not in a +production environment. + + + +### System Requirements + + + +#### Supported Platforms + +ZooKeeper consists of multiple components. Some components are +supported broadly, and other components are supported only on a smaller +set of platforms. + +* **Client** is the Java client + library, used by applications to connect to a ZooKeeper ensemble. +* **Server** is the Java server + that runs on the ZooKeeper ensemble nodes. +* **Native Client** is a client + implemented in C, similar to the Java client, used by applications + to connect to a ZooKeeper ensemble. +* **Contrib** refers to multiple + optional add-on components. + +The following matrix describes the level of support committed for +running each component on different operating system platforms. + +##### Support Matrix + +| Operating System | Client | Server | Native Client | Contrib | +|------------------|--------|--------|---------------|---------| +| GNU/Linux | Development and Production | Development and Production | Development and Production | Development and Production | +| Solaris | Development and Production | Development and Production | Not Supported | Not Supported | +| FreeBSD | Development and Production | Development and Production | Not Supported | Not Supported | +| Windows | Development and Production | Development and Production | Not Supported | Not Supported | +| Mac OS X | Development Only | Development Only | Not Supported | Not Supported | + +For any operating system not explicitly mentioned as supported in +the matrix, components may or may not work. The ZooKeeper community +will fix obvious bugs that are reported for other platforms, but there +is no full support. + + + +#### Required Software + +ZooKeeper runs in Java, release 1.8 or greater +(JDK 8 LTS, JDK 11 LTS, JDK 12 - Java 9 and 10 are not supported). +It runs as an _ensemble_ of ZooKeeper servers. Three +ZooKeeper servers is the minimum recommended size for an +ensemble, and we also recommend that they run on separate +machines. At Yahoo!, ZooKeeper is usually deployed on +dedicated RHEL boxes, with dual-core processors, 2GB of RAM, +and 80GB IDE hard drives. + + + +### Clustered (Multi-Server) Setup + +For reliable ZooKeeper service, you should deploy ZooKeeper in a +cluster known as an _ensemble_. As long as a majority +of the ensemble are up, the service will be available. Because Zookeeper +requires a majority, it is best to use an +odd number of machines. For example, with four machines ZooKeeper can +only handle the failure of a single machine; if two machines fail, the +remaining two machines do not constitute a majority. However, with five +machines ZooKeeper can handle the failure of two machines. + +###### Note +>As mentioned in the +[ZooKeeper Getting Started Guide](zookeeperStarted.html) +, a minimum of three servers are required for a fault tolerant +clustered setup, and it is strongly recommended that you have an +odd number of servers. + +>Usually three servers is more than enough for a production +install, but for maximum reliability during maintenance, you may +wish to install five servers. With three servers, if you perform +maintenance on one of them, you are vulnerable to a failure on one +of the other two servers during that maintenance. If you have five +of them running, you can take one down for maintenance, and know +that you're still OK if one of the other four suddenly fails. + +>Your redundancy considerations should include all aspects of +your environment. If you have three ZooKeeper servers, but their +network cables are all plugged into the same network switch, then +the failure of that switch will take down your entire ensemble. + +Here are the steps to set a server that will be part of an +ensemble. These steps should be performed on every host in the +ensemble: + +1. Install the Java JDK. You can use the native packaging system + for your system, or download the JDK from: + [http://java.sun.com/javase/downloads/index.jsp](http://java.sun.com/javase/downloads/index.jsp) + +2. Set the Java heap size. This is very important to avoid + swapping, which will seriously degrade ZooKeeper performance. To + determine the correct value, use load tests, and make sure you are + well below the usage limit that would cause you to swap. Be + conservative - use a maximum heap size of 3GB for a 4GB + machine. + +3. Install the ZooKeeper Server Package. It can be downloaded + from: + [http://zookeeper.apache.org/releases.html](http://zookeeper.apache.org/releases.html) + +4. Create a configuration file. This file can be called anything. + Use the following settings as a starting point: + + tickTime=2000 + dataDir=/var/lib/zookeeper/ + clientPort=2181 + initLimit=5 + syncLimit=2 + server.1=zoo1:2888:3888 + server.2=zoo2:2888:3888 + server.3=zoo3:2888:3888 + + You can find the meanings of these and other configuration + settings in the section [Configuration Parameters](#sc_configuration). A word + though about a few here: + Every machine that is part of the ZooKeeper ensemble should know + about every other machine in the ensemble. You accomplish this with + the series of lines of the form **server.id=host:port:port**. + (The parameters **host** and **port** are straightforward, for each server + you need to specify first a Quorum port then a dedicated port for ZooKeeper leader + election). Since ZooKeeper 3.6.0 you can also [specify multiple addresses](#id_multi_address) + for each ZooKeeper server instance (this can increase availability when multiple physical + network interfaces can be used parallel in the cluster). + You attribute the + server id to each machine by creating a file named + *myid*, one for each server, which resides in + that server's data directory, as specified by the configuration file + parameter **dataDir**. + +5. The myid file + consists of a single line containing only the text of that machine's + id. So *myid* of server 1 would contain the text + "1" and nothing else. The id must be unique within the + ensemble and should have a value between 1 and 255. + **IMPORTANT:** if you enable extended features such + as TTL Nodes (see below) the id must be between 1 + and 254 due to internal limitations. + +6. Create an initialization marker file *initialize* + in the same directory as *myid*. This file indicates + that an empty data directory is expected. When present, an empty database + is created and the marker file deleted. When not present, an empty data + directory will mean this peer will not have voting rights and it will not + populate the data directory until it communicates with an active leader. + Intended use is to only create this file when bringing up a new + ensemble. + +7. If your configuration file is set up, you can start a + ZooKeeper server: + + $ java -cp zookeeper.jar:lib/*:conf org.apache.zookeeper.server.quorum.QuorumPeerMain zoo.conf + + QuorumPeerMain starts a ZooKeeper server, + [JMX](http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/) + management beans are also registered which allows + management through a JMX management console. + The [ZooKeeper JMX + document](zookeeperJMX.html) contains details on managing ZooKeeper with JMX. + See the script _bin/zkServer.sh_, + which is included in the release, for an example + of starting server instances. +8. Test your deployment by connecting to the hosts: + In Java, you can run the following command to execute + simple operations: + + $ bin/zkCli.sh -server 127.0.0.1:2181 + + + +### Single Server and Developer Setup + +If you want to setup ZooKeeper for development purposes, you will +probably want to setup a single server instance of ZooKeeper, and then +install either the Java or C client-side libraries and bindings on your +development machine. + +The steps to setting up a single server instance are the similar +to the above, except the configuration file is simpler. You can find the +complete instructions in the [Installing and +Running ZooKeeper in Single Server Mode](zookeeperStarted.html#sc_InstallingSingleMode) section of the [ZooKeeper Getting Started +Guide](zookeeperStarted.html). + +For information on installing the client side libraries, refer to +the [Bindings](zookeeperProgrammers.html#ch_bindings) +section of the [ZooKeeper +Programmer's Guide](zookeeperProgrammers.html). + + + +## Administration + +This section contains information about running and maintaining +ZooKeeper and covers these topics: + +* [Designing a ZooKeeper Deployment](#sc_designing) +* [Provisioning](#sc_provisioning) +* [Things to Consider: ZooKeeper Strengths and Limitations](#sc_strengthsAndLimitations) +* [Administering](#sc_administering) +* [Maintenance](#sc_maintenance) +* [Supervision](#sc_supervision) +* [Monitoring](#sc_monitoring) +* [Logging](#sc_logging) +* [Troubleshooting](#sc_troubleshooting) +* [Configuration Parameters](#sc_configuration) +* [ZooKeeper Commands](#sc_zkCommands) +* [Data File Management](#sc_dataFileManagement) +* [Things to Avoid](#sc_commonProblems) +* [Best Practices](#sc_bestPractices) + + + +### Designing a ZooKeeper Deployment + +The reliability of ZooKeeper rests on two basic assumptions. + +1. Only a minority of servers in a deployment + will fail. _Failure_ in this context + means a machine crash, or some error in the network that + partitions a server off from the majority. +1. Deployed machines operate correctly. To + operate correctly means to execute code correctly, to have + clocks that work properly, and to have storage and network + components that perform consistently. + +The sections below contain considerations for ZooKeeper +administrators to maximize the probability for these assumptions +to hold true. Some of these are cross-machines considerations, +and others are things you should consider for each and every +machine in your deployment. + + + +#### Cross Machine Requirements + +For the ZooKeeper service to be active, there must be a +majority of non-failing machines that can communicate with +each other. To create a deployment that can tolerate the +failure of F machines, you should count on deploying 2xF+1 +machines. Thus, a deployment that consists of three machines +can handle one failure, and a deployment of five machines can +handle two failures. Note that a deployment of six machines +can only handle two failures since three machines is not a +majority. For this reason, ZooKeeper deployments are usually +made up of an odd number of machines. + +To achieve the highest probability of tolerating a failure +you should try to make machine failures independent. For +example, if most of the machines share the same switch, +failure of that switch could cause a correlated failure and +bring down the service. The same holds true of shared power +circuits, cooling systems, etc. + + + +#### Single Machine Requirements + +If ZooKeeper has to contend with other applications for +access to resources like storage media, CPU, network, or +memory, its performance will suffer markedly. ZooKeeper has +strong durability guarantees, which means it uses storage +media to log changes before the operation responsible for the +change is allowed to complete. You should be aware of this +dependency then, and take great care if you want to ensure +that ZooKeeper operations aren’t held up by your media. Here +are some things you can do to minimize that sort of +degradation: + +* ZooKeeper's transaction log must be on a dedicated + device. (A dedicated partition is not enough.) ZooKeeper + writes the log sequentially, without seeking Sharing your + log device with other processes can cause seeks and + contention, which in turn can cause multi-second + delays. +* Do not put ZooKeeper in a situation that can cause a + swap. In order for ZooKeeper to function with any sort of + timeliness, it simply cannot be allowed to swap. + Therefore, make certain that the maximum heap size given + to ZooKeeper is not bigger than the amount of real memory + available to ZooKeeper. For more on this, see + [Things to Avoid](#sc_commonProblems) + below. + + + +### Provisioning + + + +### Things to Consider: ZooKeeper Strengths and Limitations + + + +### Administering + + + +### Maintenance + +Little long term maintenance is required for a ZooKeeper +cluster however you must be aware of the following: + + + +#### Ongoing Data Directory Cleanup + +The ZooKeeper [Data +Directory](#var_datadir) contains files which are a persistent copy +of the znodes stored by a particular serving ensemble. These +are the snapshot and transactional log files. As changes are +made to the znodes these changes are appended to a +transaction log. Occasionally, when a log grows large, a +snapshot of the current state of all znodes will be written +to the filesystem and a new transaction log file is created +for future transactions. During snapshotting, ZooKeeper may +continue appending incoming transactions to the old log file. +Therefore, some transactions which are newer than a snapshot +may be found in the last transaction log preceding the +snapshot. + +A ZooKeeper server **will not remove +old snapshots and log files** when using the default +configuration (see autopurge below), this is the +responsibility of the operator. Every serving environment is +different and therefore the requirements of managing these +files may differ from install to install (backup for example). + +The PurgeTxnLog utility implements a simple retention +policy that administrators can use. The [API docs](index.html) contains details on +calling conventions (arguments, etc...). + +In the following example the last count snapshots and +their corresponding logs are retained and the others are +deleted. The value of should typically be +greater than 3 (although not required, this provides 3 backups +in the unlikely event a recent log has become corrupted). This +can be run as a cron job on the ZooKeeper server machines to +clean up the logs daily. + + java -cp zookeeper.jar:lib/slf4j-api-1.7.5.jar:lib/slf4j-log4j12-1.7.5.jar:lib/log4j-1.2.17.jar:conf org.apache.zookeeper.server.PurgeTxnLog -n + + +Automatic purging of the snapshots and corresponding +transaction logs was introduced in version 3.4.0 and can be +enabled via the following configuration parameters **autopurge.snapRetainCount** and **autopurge.purgeInterval**. For more on +this, see [Advanced Configuration](#sc_advancedConfiguration) +below. + + + +#### Debug Log Cleanup (log4j) + +See the section on [logging](#sc_logging) in this document. It is +expected that you will setup a rolling file appender using the +in-built log4j feature. The sample configuration file in the +release tar's conf/log4j.properties provides an example of +this. + + + +### Supervision + +You will want to have a supervisory process that manages +each of your ZooKeeper server processes (JVM). The ZK server is +designed to be "fail fast" meaning that it will shutdown +(process exit) if an error occurs that it cannot recover +from. As a ZooKeeper serving cluster is highly reliable, this +means that while the server may go down the cluster as a whole +is still active and serving requests. Additionally, as the +cluster is "self healing" the failed server once restarted will +automatically rejoin the ensemble w/o any manual +interaction. + +Having a supervisory process such as [daemontools](http://cr.yp.to/daemontools.html) or +[SMF](http://en.wikipedia.org/wiki/Service\_Management\_Facility) +(other options for supervisory process are also available, it's +up to you which one you would like to use, these are just two +examples) managing your ZooKeeper server ensures that if the +process does exit abnormally it will automatically be restarted +and will quickly rejoin the cluster. + +It is also recommended to configure the ZooKeeper server process to +terminate and dump its heap if an OutOfMemoryError** occurs. This is achieved +by launching the JVM with the following arguments on Linux and Windows +respectively. The *zkServer.sh* and +*zkServer.cmd* scripts that ship with ZooKeeper set +these options. + + -XX:+HeapDumpOnOutOfMemoryError -XX:OnOutOfMemoryError='kill -9 %p' + + "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" + + + +### Monitoring + +The ZooKeeper service can be monitored in one of three primary ways: + +* the command port through the use of [4 letter words](#sc_zkCommands) +* with [JMX](zookeeperJMX.html) +* using the [`zkServer.sh status` command](zookeeperTools.html#zkServer) + + + +### Logging + +ZooKeeper uses **[SLF4J](http://www.slf4j.org)** +version 1.7.5 as its logging infrastructure. For backward compatibility it is bound to +**LOG4J** but you can use +**[LOGBack](http://logback.qos.ch/)** +or any other supported logging framework of your choice. + +The ZooKeeper default *log4j.properties* +file resides in the *conf* directory. Log4j requires that +*log4j.properties* either be in the working directory +(the directory from which ZooKeeper is run) or be accessible from the classpath. + +For more information about SLF4J, see +[its manual](http://www.slf4j.org/manual.html). + +For more information about LOG4J, see +[Log4j Default Initialization Procedure](http://logging.apache.org/log4j/1.2/manual.html#defaultInit) +of the log4j manual. + + + +### Troubleshooting + +* *Server not coming up because of file corruption* : + A server might not be able to read its database and fail to come up because of + some file corruption in the transaction logs of the ZooKeeper server. You will + see some IOException on loading ZooKeeper database. In such a case, + make sure all the other servers in your ensemble are up and working. Use "stat" + command on the command port to see if they are in good health. After you have verified that + all the other servers of the ensemble are up, you can go ahead and clean the database + of the corrupt server. Delete all the files in datadir/version-2 and datalogdir/version-2/. + Restart the server. + + + +### Configuration Parameters + +ZooKeeper's behavior is governed by the ZooKeeper configuration +file. This file is designed so that the exact same file can be used by +all the servers that make up a ZooKeeper server assuming the disk +layouts are the same. If servers use different configuration files, care +must be taken to ensure that the list of servers in all of the different +configuration files match. + +###### Note +>In 3.5.0 and later, some of these parameters should be placed in +a dynamic configuration file. If they are placed in the static +configuration file, ZooKeeper will automatically move them over to the +dynamic configuration file. See [Dynamic Reconfiguration](zookeeperReconfig.html) for more information. + + + +#### Minimum Configuration + +Here are the minimum configuration keywords that must be defined +in the configuration file: + +* *clientPort* : + the port to listen for client connections; that is, the + port that clients attempt to connect to. + +* *secureClientPort* : + the port to listen on for secure client connections using SSL. + **clientPort** specifies + the port for plaintext connections while **secureClientPort** specifies the port for SSL + connections. Specifying both enables mixed-mode while omitting + either will disable that mode. + Note that SSL feature will be enabled when user plugs-in + zookeeper.serverCnxnFactory, zookeeper.clientCnxnSocket as Netty. + +* *observerMasterPort* : + the port to listen for observer connections; that is, the + port that observers attempt to connect to. + if the property is set then the server will host observer connections + when in follower mode in addition to when in leader mode and correspondingly + attempt to connect to any voting peer when in observer mode. + +* *dataDir* : + the location where ZooKeeper will store the in-memory + database snapshots and, unless specified otherwise, the + transaction log of updates to the database. + ###### Note + >Be careful where you put the transaction log. A + dedicated transaction log device is key to consistent good + performance. Putting the log on a busy device will adversely + affect performance. + +* *tickTime* : + the length of a single tick, which is the basic time unit + used by ZooKeeper, as measured in milliseconds. It is used to + regulate heartbeats, and timeouts. For example, the minimum + session timeout will be two ticks. + + + +#### Advanced Configuration + +The configuration settings in the section are optional. You can +use them to further fine tune the behaviour of your ZooKeeper servers. +Some can also be set using Java system properties, generally of the +form _zookeeper.keyword_. The exact system +property, when available, is noted below. + +* *dataLogDir* : + (No Java system property) + This option will direct the machine to write the + transaction log to the **dataLogDir** rather than the **dataDir**. This allows a dedicated log + device to be used, and helps avoid competition between logging + and snapshots. + ###### Note + >Having a dedicated log device has a large impact on + throughput and stable latencies. It is highly recommended to + dedicate a log device and set **dataLogDir** to point to a directory on + that device, and then make sure to point **dataDir** to a directory + _not_ residing on that device. + +* *globalOutstandingLimit* : + (Java system property: **zookeeper.globalOutstandingLimit.**) + Clients can submit requests faster than ZooKeeper can + process them, especially if there are a lot of clients. To + prevent ZooKeeper from running out of memory due to queued + requests, ZooKeeper will throttle clients so that there is no + more than globalOutstandingLimit outstanding requests in the + system. The default limit is 1,000. + +* *preAllocSize* : + (Java system property: **zookeeper.preAllocSize**) + To avoid seeks ZooKeeper allocates space in the + transaction log file in blocks of preAllocSize kilobytes. The + default block size is 64M. One reason for changing the size of + the blocks is to reduce the block size if snapshots are taken + more often. (Also, see **snapCount** and **snapSizeLimitInKb**). + +* *snapCount* : + (Java system property: **zookeeper.snapCount**) + ZooKeeper records its transactions using snapshots and + a transaction log (think write-ahead log).The number of + transactions recorded in the transaction log before a snapshot + can be taken (and the transaction log rolled) is determined + by snapCount. In order to prevent all of the machines in the quorum + from taking a snapshot at the same time, each ZooKeeper server + will take a snapshot when the number of transactions in the transaction log + reaches a runtime generated random value in the \[snapCount/2+1, snapCount] + range.The default snapCount is 100,000. + +* *commitLogCount* * : + (Java system property: **zookeeper.commitLogCount**) + Zookeeper maintains an in-memory list of last committed requests for fast synchronization with + followers when the followers are not too behind. This improves sync performance in case when your + snapshots are large (>100,000). + The default commitLogCount value is 500. + +* *snapSizeLimitInKb* : + (Java system property: **zookeeper.snapSizeLimitInKb**) + ZooKeeper records its transactions using snapshots and + a transaction log (think write-ahead log). The total size in bytes allowed + in the set of transactions recorded in the transaction log before a snapshot + can be taken (and the transaction log rolled) is determined + by snapSize. In order to prevent all of the machines in the quorum + from taking a snapshot at the same time, each ZooKeeper server + will take a snapshot when the size in bytes of the set of transactions in the + transaction log reaches a runtime generated random value in the \[snapSize/2+1, snapSize] + range. Each file system has a minimum standard file size and in order + to for valid functioning of this feature, the number chosen must be larger + than that value. The default snapSizeLimitInKb is 4,194,304 (4GB). + A non-positive value will disable the feature. + +* *txnLogSizeLimitInKb* : + (Java system property: **zookeeper.txnLogSizeLimitInKb**) + Zookeeper transaction log file can also be controlled more + directly using txnLogSizeLimitInKb. Larger txn logs can lead to + slower follower syncs when sync is done using transaction log. + This is because leader has to scan through the appropriate log + file on disk to find the transaction to start sync from. + This feature is turned off by default and snapCount and snapSizeLimitInKb are the + only values that limit transaction log size. When enabled + Zookeeper will roll the log when any of the limits is hit. + Please note that actual log size can exceed this value by the size + of the serialized transaction. On the other hand, if this value is + set too close to (or smaller than) **preAllocSize**, + it can cause Zookeeper to roll the log for every transaction. While + this is not a correctness issue, this may cause severely degraded + performance. To avoid this and to get most out of this feature, it is + recommended to set the value to N * **preAllocSize** + where N >= 2. + +* *maxCnxns* : + (Java system property: **zookeeper.maxCnxns**) + Limits the total number of concurrent connections that can be made to a + zookeeper server (per client Port of each server ). This is used to prevent certain + classes of DoS attacks. The default is 0 and setting it to 0 entirely removes + the limit on total number of concurrent connections. Accounting for the + number of connections for serverCnxnFactory and a secureServerCnxnFactory is done + separately, so a peer is allowed to host up to 2*maxCnxns provided they are of appropriate types. + +* *maxClientCnxns* : + (No Java system property) + Limits the number of concurrent connections (at the socket + level) that a single client, identified by IP address, may make + to a single member of the ZooKeeper ensemble. This is used to + prevent certain classes of DoS attacks, including file + descriptor exhaustion. The default is 60. Setting this to 0 + entirely removes the limit on concurrent connections. + +* *clientPortAddress* : + **New in 3.3.0:** the + address (ipv4, ipv6 or hostname) to listen for client + connections; that is, the address that clients attempt + to connect to. This is optional, by default we bind in + such a way that any connection to the **clientPort** for any + address/interface/nic on the server will be + accepted. + +* *minSessionTimeout* : + (No Java system property) + **New in 3.3.0:** the + minimum session timeout in milliseconds that the server + will allow the client to negotiate. Defaults to 2 times + the **tickTime**. + +* *maxSessionTimeout* : + (No Java system property) + **New in 3.3.0:** the + maximum session timeout in milliseconds that the server + will allow the client to negotiate. Defaults to 20 times + the **tickTime**. + +* *fsync.warningthresholdms* : + (Java system property: **zookeeper.fsync.warningthresholdms**) + **New in 3.3.4:** A + warning message will be output to the log whenever an + fsync in the Transactional Log (WAL) takes longer than + this value. The values is specified in milliseconds and + defaults to 1000. This value can only be set as a + system property. + +* *maxResponseCacheSize* : + (Java system property: **zookeeper.maxResponseCacheSize**) + When set to a positive integer, it determines the size + of the cache that stores the serialized form of recently + read records. Helps save the serialization cost on + popular znodes. The metrics **response_packet_cache_hits** + and **response_packet_cache_misses** can be used to tune + this value to a given workload. The feature is turned on + by default with a value of 400, set to 0 or a negative + integer to turn the feature off. + +* *maxGetChildrenResponseCacheSize* : + (Java system property: **zookeeper.maxGetChildrenResponseCacheSize**) + **New in 3.6.0:** + Similar to **maxResponseCacheSize**, but applies to get children + requests. The metrics **response_packet_get_children_cache_hits** + and **response_packet_get_children_cache_misses** can be used to tune + this value to a given workload. The feature is turned on + by default with a value of 400, set to 0 or a negative + integer to turn the feature off. + +* *autopurge.snapRetainCount* : + (No Java system property) + **New in 3.4.0:** + When enabled, ZooKeeper auto purge feature retains + the **autopurge.snapRetainCount** most + recent snapshots and the corresponding transaction logs in the + **dataDir** and **dataLogDir** respectively and deletes the rest. + Defaults to 3. Minimum value is 3. + +* *autopurge.purgeInterval* : + (No Java system property) + **New in 3.4.0:** The + time interval in hours for which the purge task has to + be triggered. Set to a positive integer (1 and above) + to enable the auto purging. Defaults to 0. + +* *syncEnabled* : + (Java system property: **zookeeper.observer.syncEnabled**) + **New in 3.4.6, 3.5.0:** + The observers now log transaction and write snapshot to disk + by default like the participants. This reduces the recovery time + of the observers on restart. Set to "false" to disable this + feature. Default is "true" + +* *fastleader.minNotificationInterval* : + (Java system property: **zookeeper.fastleader.minNotificationInterval**) + Lower bound for length of time between two consecutive notification + checks on the leader election. This interval determines how long a + peer waits to check the set of election votes and effects how + quickly an election can resolve. The interval follows a backoff + strategy from the configured minimum (this) and the configured maximum + (fastleader.maxNotificationInterval) for long elections. + +* *fastleader.maxNotificationInterval* : + (Java system property: **zookeeper.fastleader.maxNotificationInterval**) + Upper bound for length of time between two consecutive notification + checks on the leader election. This interval determines how long a + peer waits to check the set of election votes and effects how + quickly an election can resolve. The interval follows a backoff + strategy from the configured minimum (fastleader.minNotificationInterval) + and the configured maximum (this) for long elections. + +* *connectionMaxTokens* : + (Java system property: **zookeeper.connection_throttle_tokens**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. + This parameter defines the maximum number of tokens in the token-bucket. + When set to 0, throttling is disabled. Default is 0. + +* *connectionTokenFillTime* : + (Java system property: **zookeeper.connection_throttle_fill_time**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. + This parameter defines the interval in milliseconds when the token bucket is re-filled with + *connectionTokenFillCount* tokens. Default is 1. + +* *connectionTokenFillCount* : + (Java system property: **zookeeper.connection_throttle_fill_count**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. + This parameter defines the number of tokens to add to the token bucket every + *connectionTokenFillTime* milliseconds. Default is 1. + +* *connectionFreezeTime* : + (Java system property: **zookeeper.connection_throttle_freeze_time**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. + This parameter defines the interval in milliseconds when the dropping + probability is adjusted. When set to -1, probabilistic dropping is disabled. + Default is -1. + +* *connectionDropIncrease* : + (Java system property: **zookeeper.connection_throttle_drop_increase**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. + This parameter defines the dropping probability to increase. The throttler + checks every *connectionFreezeTime* milliseconds and if the token bucket is + empty, the dropping probability will be increased by *connectionDropIncrease*. + The default is 0.02. + +* *connectionDropDecrease* : + (Java system property: **zookeeper.connection_throttle_drop_decrease**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. + This parameter defines the dropping probability to decrease. The throttler + checks every *connectionFreezeTime* milliseconds and if the token bucket has + more tokens than a threshold, the dropping probability will be decreased by + *connectionDropDecrease*. The threshold is *connectionMaxTokens* \* + *connectionDecreaseRatio*. The default is 0.002. + +* *connectionDecreaseRatio* : + (Java system property: **zookeeper.connection_throttle_decrease_ratio**) + **New in 3.6.0:** + This is one of the parameters to tune the server-side connection throttler, + which is a token-based rate limiting mechanism with optional probabilistic + dropping. This parameter defines the threshold to decrease the dropping + probability. The default is 0. + +* *zookeeper.connection_throttle_weight_enabled* : + (Java system property only) + **New in 3.6.0:** + Whether to consider connection weights when throttling. Only useful when connection throttle is enabled, that is, connectionMaxTokens is larger than 0. The default is false. + +* *zookeeper.connection_throttle_global_session_weight* : + (Java system property only) + **New in 3.6.0:** + The weight of a global session. It is the number of tokens required for a global session request to get through the connection throttler. It has to be a positive integer no smaller than the weight of a local session. The default is 3. + +* *zookeeper.connection_throttle_local_session_weight* : + (Java system property only) + **New in 3.6.0:** + The weight of a local session. It is the number of tokens required for a local session request to get through the connection throttler. It has to be a positive integer no larger than the weight of a global session or a renew session. The default is 1. + +* *zookeeper.connection_throttle_renew_session_weight* : + (Java system property only) + **New in 3.6.0:** + The weight of renewing a session. It is also the number of tokens required for a reconnect request to get through the throttler. It has to be a positive integer no smaller than the weight of a local session. The default is 2. + + + * *clientPortListenBacklog* : + **New in 3.4.14, 3.5.5, 3.6.0:** + The socket backlog length for the ZooKeeper server socket. This controls + the number of requests that will be queued server-side to be processed + by the ZooKeeper server. Connections that exceed this length will receive + a network timeout (30s) which may cause ZooKeeper session expiry issues. + By default, this value is unset (`-1`) which, on Linux, uses a backlog of + `50`. This value must be a positive number. + +* *serverCnxnFactory* : + (Java system property: **zookeeper.serverCnxnFactory**) + Specifies ServerCnxnFactory implementation. + This should be set to `NettyServerCnxnFactory` in order to use TLS based server communication. + Default is `NIOServerCnxnFactory`. + +* *flushDelay* : + (Java system property: **zookeeper.flushDelay**) + Time in milliseconds to delay the flush of the commit log. + Does not affect the limit defined by *maxBatchSize*. + Disabled by default (with value 0). Ensembles with high write rates + may see throughput improved with a value of 10-20 ms. + +* *maxWriteQueuePollTime* : + (Java system property: **zookeeper.maxWriteQueuePollTime**) + If *flushDelay* is enabled, this determines the amount of time in milliseconds + to wait before flushing when no new requests are being queued. + Set to *flushDelay*/3 by default (implicitly disabled by default). + +* *maxBatchSize* : + (Java system property: **zookeeper.maxBatchSize**) + The number of transactions allowed in the server before a flush of the + commit log is triggered. + Does not affect the limit defined by *flushDelay*. + Default is 1000. + +* *requestThrottleLimit* : + (Java system property: **zookeeper.request_throttle_max_requests**) + **New in 3.6.0:** + The total number of outstanding requests allowed before the RequestThrottler starts stalling. When set to 0, throttling is disabled. The default is 0. + +* *requestThrottleStallTime* : + (Java system property: **zookeeper.request_throttle_stall_time**) + **New in 3.6.0:** + The maximum time (in milliseconds) for which a thread may wait to be notified that it may proceed processing a request. The default is 100. + +* *requestThrottleDropStale* : + (Java system property: **request_throttle_drop_stale**) + **New in 3.6.0:** + When enabled, the throttler will drop stale requests rather than issue them to the request pipeline. A stale request is a request sent by a connection that is now closed, and/or a request that will have a request latency higher than the sessionTimeout. The default is true. + +* *requestStaleLatencyCheck* : + (Java system property: **zookeeper.request_stale_latency_check**) + **New in 3.6.0:** + When enabled, a request is considered stale if the request latency is higher than its associated session timeout. Disabled by default. + +* *requestStaleConnectionCheck* : + (Java system property: **zookeeper.request_stale_connection_check**) + **New in 3.6.0:** + When enabled, a request is considered stale if the request's connection has closed. Enabled by default. + +* *zookeeper.request_throttler.shutdownTimeout* : + (Java system property only) + **New in 3.6.0:** + The time (in milliseconds) the RequestThrottler waits for the request queue to drain during shutdown before it shuts down forcefully. The default is 10000. + +* *advancedFlowControlEnabled* : + (Java system property: **zookeeper.netty.advancedFlowControl.enabled**) + Using accurate flow control in netty based on the status of ZooKeeper + pipeline to avoid direct buffer OOM. It will disable the AUTO_READ in + Netty. + +* *enableEagerACLCheck* : + (Java system property only: **zookeeper.enableEagerACLCheck**) + When set to "true", enables eager ACL check on write requests on each local + server before sending the requests to quorum. Default is "false". + +* *maxConcurrentSnapSyncs* : + (Java system property: **zookeeper.leader.maxConcurrentSnapSyncs**) + The maximum number of snap syncs a leader or a follower can serve at the same + time. The default is 10. + +* *maxConcurrentDiffSyncs* : + (Java system property: **zookeeper.leader.maxConcurrentDiffSyncs**) + The maximum number of diff syncs a leader or a follower can serve at the same + time. The default is 100. + +* *digest.enabled* : + (Java system property only: **zookeeper.digest.enabled**) + **New in 3.6.0:** + The digest feature is added to detect the data inconsistency inside + ZooKeeper when loading database from disk, catching up and following + leader, its doing incrementally hash check for the DataTree based on + the adHash paper mentioned in + + https://cseweb.ucsd.edu/~daniele/papers/IncHash.pdf + + The idea is simple, the hash value of DataTree will be updated incrementally + based on the changes to the set of data. When the leader is preparing the txn, + it will pre-calculate the hash of the tree based on the changes happened with + formula: + + current_hash = current_hash + hash(new node data) - hash(old node data) + + If it’s creating a new node, the hash(old node data) will be 0, and if it’s a + delete node op, the hash(new node data) will be 0. + + This hash will be associated with each txn to represent the expected hash value + after applying the txn to the data tree, it will be sent to followers with + original proposals. Learner will compare the actual hash value with the one in + the txn after applying the txn to the data tree, and report mismatch if it’s not + the same. + + These digest value will also be persisted with each txn and snapshot on the disk, + so when servers restarted and load data from disk, it will compare and see if + there is hash mismatch, which will help detect data loss issue on disk. + + For the actual hash function, we’re using CRC internally, it’s not a collisionless + hash function, but it’s more efficient compared to collisionless hash, and the + collision possibility is really really rare and can already meet our needs here. + + This feature is backward and forward compatible, so it can safely rolling upgrade, + downgrade, enabled and later disabled without any compatible issue. Here are the + scenarios have been covered and tested: + + 1. When leader runs with new code while follower runs with old one, the digest will + be append to the end of each txn, follower will only read header and txn data, + digest value in the txn will be ignored. It won't affect the follower reads and + processes the next txn. + 2. When leader runs with old code while follower runs with new one, the digest won't + be sent with txn, when follower tries to read the digest, it will throw EOF which + is caught and handled gracefully with digest value set to null. + 3. When loading old snapshot with new code, it will throw IOException when trying to + read the non-exist digest value, and the exception will be caught and digest will + be set to null, which means we won't compare digest when loading this snapshot, + which is expected to happen during rolling upgrade + 4. When loading new snapshot with old code, it will finish successfully after deserialzing + the data tree, the digest value at the end of snapshot file will be ignored + 5. The scenarios of rolling restart with flags change are similar to the 1st and 2nd + scenarios discussed above, if the leader enabled but follower not, digest value will + be ignored, and follower won't compare the digest during runtime; if leader disabled + but follower enabled, follower will get EOF exception which is handled gracefully. + + Note: the current digest calculation excluded nodes under /zookeeper + due to the potential inconsistency in the /zookeeper/quota stat node, + we can include that after that issue is fixed. + + By default, this feature is enabled, set "false" to disable it. + +* *snapshot.compression.method* : + (Java system property: **zookeeper.snapshot.compression.method**) + **New in 3.6.0:** + This property controls whether or not ZooKeeper should compress snapshots + before storing them on disk (see [ZOOKEEPER-3179](https://issues.apache.org/jira/browse/ZOOKEEPER-3179)). + Possible values are: + - "": Disabled (no snapshot compression). This is the default behavior. + - "gz": See [gzip compression](https://en.wikipedia.org/wiki/Gzip). + - "snappy": See [Snappy compression](https://en.wikipedia.org/wiki/Snappy_(compression)). + +* *snapshot.trust.empty* : + (Java system property: **zookeeper.snapshot.trust.empty**) + **New in 3.5.6:** + This property controls whether or not ZooKeeper should treat missing + snapshot files as a fatal state that can't be recovered from. + Set to true to allow ZooKeeper servers recover without snapshot + files. This should only be set during upgrading from old versions of + ZooKeeper (3.4.x, pre 3.5.3) where ZooKeeper might only have transaction + log files but without presence of snapshot files. If the value is set + during upgrade, we recommend to set the value back to false after upgrading + and restart ZooKeeper process so ZooKeeper can continue normal data + consistency check during recovery process. + Default value is false. + +* *audit.enable* : + (Java system property: **zookeeper.audit.enable**) + **New in 3.6.0:** + By default audit logs are disabled. Set to "true" to enable it. Default value is "false". + See the [ZooKeeper audit logs](zookeeperAuditLogs.html) for more information. + +* *audit.impl.class* : + (Java system property: **zookeeper.audit.impl.class**) + **New in 3.6.0:** + Class to implement the audit logger. By default log4j based audit logger org.apache.zookeeper.audit + .Log4jAuditLogger is used. + See the [ZooKeeper audit logs](zookeeperAuditLogs.html) for more information. + +* *largeRequestMaxBytes* : + (Java system property: **zookeeper.largeRequestMaxBytes**) + **New in 3.6.0:** + The maximum number of bytes of all inflight large request. The connection will be closed if a coming large request causes the limit exceeded. The default is 100 * 1024 * 1024. + +* *largeRequestThreshold* : + (Java system property: **zookeeper.largeRequestThreshold**) + **New in 3.6.0:** + The size threshold after which a request is considered a large request. If it is -1, then all requests are considered small, effectively turning off large request throttling. The default is -1. + +* *outstandingHandshake.limit* + (Jave system property only: **zookeeper.netty.server.outstandingHandshake.limit**) + The maximum in-flight TLS handshake connections could have in ZooKeeper, + the connections exceed this limit will be rejected before starting handshake. + This setting doesn't limit the max TLS concurrency, but helps avoid herd + effect due to TLS handshake timeout when there are too many in-flight TLS + handshakes. Set it to something like 250 is good enough to avoid herd effect. + +* *learner.asyncSending* + (Java system property: **zookeeper.learner.asyncSending**) + (Java system property: **learner.asyncSending**)(Added for backward compatibility) + **New in 3.7.0:** + The sending and receiving packets in Learner were done synchronously in a critical section. An untimely network issue could cause the followers to hang (see [ZOOKEEPER-3575](https://issues.apache.org/jira/browse/ZOOKEEPER-3575) and [ZOOKEEPER-4074](https://issues.apache.org/jira/browse/ZOOKEEPER-4074)). The new design moves sending packets in Learner to a separate thread and sends the packets asynchronously. The new design is enabled with this parameter (learner.asyncSending). + The default is false. + + +#### Cluster Options + +The options in this section are designed for use with an ensemble +of servers -- that is, when deploying clusters of servers. + +* *electionAlg* : + (No Java system property) + Election implementation to use. A value of "1" corresponds to the + non-authenticated UDP-based version of fast leader election, "2" + corresponds to the authenticated UDP-based version of fast + leader election, and "3" corresponds to TCP-based version of + fast leader election. Algorithm 3 was made default in 3.2.0 and + prior versions (3.0.0 and 3.1.0) were using algorithm 1 and 2 as well. + ###### Note + >The implementations of leader election 1, and 2 were + **deprecated** in 3.4.0. Since 3.6.0 only FastLeaderElection is available, + in case of upgrade you have to shutdown all of your servers and + restart them with electionAlg=3 (or by removing the line from the configuration file). > + +* *initLimit* : + (No Java system property) + Amount of time, in ticks (see [tickTime](#id_tickTime)), to allow followers to + connect and sync to a leader. Increased this value as needed, if + the amount of data managed by ZooKeeper is large. + +* *connectToLearnerMasterLimit* : + (Java system property: zookeeper.**connectToLearnerMasterLimit**) + Amount of time, in ticks (see [tickTime](#id_tickTime)), to allow followers to + connect to the leader after leader election. Defaults to the value of initLimit. + Use when initLimit is high so connecting to learner master doesn't result in higher timeout. + +* *leaderServes* : + (Java system property: zookeeper.**leaderServes**) + Leader accepts client connections. Default value is "yes". + The leader machine coordinates updates. For higher update + throughput at the slight expense of read throughput the leader + can be configured to not accept clients and focus on + coordination. The default to this option is yes, which means + that a leader will accept client connections. + ###### Note + >Turning on leader selection is highly recommended when + you have more than three ZooKeeper servers in an ensemble. + +* *server.x=[hostname]:nnnnn[:nnnnn] etc* : + (No Java system property) + servers making up the ZooKeeper ensemble. When the server + starts up, it determines which server it is by looking for the + file *myid* in the data directory. That file + contains the server number, in ASCII, and it should match + **x** in **server.x** in the left hand side of this + setting. + The list of servers that make up ZooKeeper servers that is + used by the clients must match the list of ZooKeeper servers + that each ZooKeeper server has. + There are two port numbers **nnnnn**. + The first followers use to connect to the leader, and the second is for + leader election. If you want to test multiple servers on a single machine, then + different ports can be used for each server. + + + + Since ZooKeeper 3.6.0 it is possible to specify **multiple addresses** for each + ZooKeeper server (see [ZOOKEEPER-3188](https://issues.apache.org/jira/projects/ZOOKEEPER/issues/ZOOKEEPER-3188)). + To enable this feature, you must set the *multiAddress.enabled* configuration property + to *true*. This helps to increase availability and adds network level + resiliency to ZooKeeper. When multiple physical network interfaces are used + for the servers, ZooKeeper is able to bind on all interfaces and runtime switching + to a working interface in case a network error. The different addresses can be specified + in the config using a pipe ('|') character. A valid configuration using multiple addresses looks like: + + server.1=zoo1-net1:2888:3888|zoo1-net2:2889:3889 + server.2=zoo2-net1:2888:3888|zoo2-net2:2889:3889 + server.3=zoo3-net1:2888:3888|zoo3-net2:2889:3889 + + + ###### Note + >By enabling this feature, the Quorum protocol (ZooKeeper Server-Server protocol) will change. + The users will not notice this and when anyone starts a ZooKeeper cluster with the new config, + everything will work normally. However, it's not possible to enable this feature and specify + multiple addresses during a rolling upgrade if the old ZooKeeper cluster didn't support the + *multiAddress* feature (and the new Quorum protocol). In case if you need this feature but you + also need to perform a rolling upgrade from a ZooKeeper cluster older than *3.6.0*, then you + first need to do the rolling upgrade without enabling the MultiAddress feature and later make + a separate rolling restart with the new configuration where **multiAddress.enabled** is set + to **true** and multiple addresses are provided. + +* *syncLimit* : + (No Java system property) + Amount of time, in ticks (see [tickTime](#id_tickTime)), to allow followers to sync + with ZooKeeper. If followers fall too far behind a leader, they + will be dropped. + +* *group.x=nnnnn[:nnnnn]* : + (No Java system property) + Enables a hierarchical quorum construction."x" is a group identifier + and the numbers following the "=" sign correspond to server identifiers. + The left-hand side of the assignment is a colon-separated list of server + identifiers. Note that groups must be disjoint and the union of all groups + must be the ZooKeeper ensemble. + You will find an example [here](zookeeperHierarchicalQuorums.html) + +* *weight.x=nnnnn* : + (No Java system property) + Used along with "group", it assigns a weight to a server when + forming quorums. Such a value corresponds to the weight of a server + when voting. There are a few parts of ZooKeeper that require voting + such as leader election and the atomic broadcast protocol. By default + the weight of server is 1. If the configuration defines groups, but not + weights, then a value of 1 will be assigned to all servers. + You will find an example [here](zookeeperHierarchicalQuorums.html) + +* *cnxTimeout* : + (Java system property: zookeeper.**cnxTimeout**) + Sets the timeout value for opening connections for leader election notifications. + Only applicable if you are using electionAlg 3. + ###### Note + >Default value is 5 seconds. + +* *quorumCnxnTimeoutMs* : + (Java system property: zookeeper.**quorumCnxnTimeoutMs**) + Sets the read timeout value for the connections for leader election notifications. + Only applicable if you are using electionAlg 3. + ######Note + >Default value is -1, which will then use the syncLimit * tickTime as the timeout. + +* *standaloneEnabled* : + (No Java system property) + **New in 3.5.0:** + When set to false, a single server can be started in replicated + mode, a lone participant can run with observers, and a cluster + can reconfigure down to one node, and up from one node. The + default is true for backwards compatibility. It can be set + using QuorumPeerConfig's setStandaloneEnabled method or by + adding "standaloneEnabled=false" or "standaloneEnabled=true" + to a server's config file. + +* *reconfigEnabled* : + (No Java system property) + **New in 3.5.3:** + This controls the enabling or disabling of + [Dynamic Reconfiguration](zookeeperReconfig.html) feature. When the feature + is enabled, users can perform reconfigure operations through + the ZooKeeper client API or through ZooKeeper command line tools + assuming users are authorized to perform such operations. + When the feature is disabled, no user, including the super user, + can perform a reconfiguration. Any attempt to reconfigure will return an error. + **"reconfigEnabled"** option can be set as + **"reconfigEnabled=false"** or + **"reconfigEnabled=true"** + to a server's config file, or using QuorumPeerConfig's + setReconfigEnabled method. The default value is false. + If present, the value should be consistent across every server in + the entire ensemble. Setting the value as true on some servers and false + on other servers will cause inconsistent behavior depending on which server + is elected as leader. If the leader has a setting of + **"reconfigEnabled=true"**, then the ensemble + will have reconfig feature enabled. If the leader has a setting of + **"reconfigEnabled=false"**, then the ensemble + will have reconfig feature disabled. It is thus recommended to have a consistent + value for **"reconfigEnabled"** across servers + in the ensemble. + +* *4lw.commands.whitelist* : + (Java system property: **zookeeper.4lw.commands.whitelist**) + **New in 3.5.3:** + A list of comma separated [Four Letter Words](#sc_4lw) + commands that user wants to use. A valid Four Letter Words + command must be put in this list else ZooKeeper server will + not enable the command. + By default the whitelist only contains "srvr" command + which zkServer.sh uses. The rest of four letter word commands are disabled + by default. + Here's an example of the configuration that enables stat, ruok, conf, and isro + command while disabling the rest of Four Letter Words command: + + 4lw.commands.whitelist=stat, ruok, conf, isro + + +If you really need enable all four letter word commands by default, you can use +the asterisk option so you don't have to include every command one by one in the list. +As an example, this will enable all four letter word commands: + + + 4lw.commands.whitelist=* + + +* *tcpKeepAlive* : + (Java system property: **zookeeper.tcpKeepAlive**) + **New in 3.5.4:** + Setting this to true sets the TCP keepAlive flag on the + sockets used by quorum members to perform elections. + This will allow for connections between quorum members to + remain up when there is network infrastructure that may + otherwise break them. Some NATs and firewalls may terminate + or lose state for long running or idle connections. + Enabling this option relies on OS level settings to work + properly, check your operating system's options regarding TCP + keepalive for more information. Defaults to + **false**. + +* *electionPortBindRetry* : + (Java system property only: **zookeeper.electionPortBindRetry**) + Property set max retry count when Zookeeper server fails to bind + leader election port. Such errors can be temporary and recoverable, + such as DNS issue described in [ZOOKEEPER-3320](https://issues.apache.org/jira/projects/ZOOKEEPER/issues/ZOOKEEPER-3320), + or non-retryable, such as port already in use. + In case of transient errors, this property can improve availability + of Zookeeper server and help it to self recover. + Default value 3. In container environment, especially in Kubernetes, + this value should be increased or set to 0(infinite retry) to overcome issues + related to DNS name resolving. + + +* *observer.reconnectDelayMs* : + (Java system property: **zookeeper.observer.reconnectDelayMs**) + When observer loses its connection with the leader, it waits for the + specified value before trying to reconnect with the leader so that + the entire observer fleet won't try to run leader election and reconnect + to the leader at once. + Defaults to 0 ms. + +* *observer.election.DelayMs* : + (Java system property: **zookeeper.observer.election.DelayMs**) + Delay the observer's participation in a leader election upon disconnect + so as to prevent unexpected additional load on the voting peers during + the process. Defaults to 200 ms. + +* *localSessionsEnabled* and *localSessionsUpgradingEnabled* : + **New in 3.5:** + Optional value is true or false. Their default values are false. + Turning on the local session feature by setting *localSessionsEnabled=true*. Turning on + *localSessionsUpgradingEnabled* can upgrade a local session to a global session automatically as required (e.g. creating ephemeral nodes), + which only matters when *localSessionsEnabled* is enabled. + + + +#### Encryption, Authentication, Authorization Options + +The options in this section allow control over +encryption/authentication/authorization performed by the service. + +Beside this page, you can also find useful information about client side configuration in the +[Programmers Guide](zookeeperProgrammers.html#sc_java_client_configuration). +The ZooKeeper Wiki also has useful pages about [ZooKeeper SSL support](https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeper+SSL+User+Guide), +and [SASL authentication for ZooKeeper](https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeper+and+SASL). + +* *DigestAuthenticationProvider.superDigest* : + (Java system property: **zookeeper.DigestAuthenticationProvider.superDigest**) + By default this feature is **disabled** + **New in 3.2:** + Enables a ZooKeeper ensemble administrator to access the + znode hierarchy as a "super" user. In particular no ACL + checking occurs for a user authenticated as + super. + org.apache.zookeeper.server.auth.DigestAuthenticationProvider + can be used to generate the superDigest, call it with + one parameter of "super:". Provide the + generated "super:" as the system property value + when starting each server of the ensemble. + When authenticating to a ZooKeeper server (from a + ZooKeeper client) pass a scheme of "digest" and authdata + of "super:". Note that digest auth passes + the authdata in plaintext to the server, it would be + prudent to use this authentication method only on + localhost (not over the network) or over an encrypted + connection. + +* *X509AuthenticationProvider.superUser* : + (Java system property: **zookeeper.X509AuthenticationProvider.superUser**) + The SSL-backed way to enable a ZooKeeper ensemble + administrator to access the znode hierarchy as a "super" user. + When this parameter is set to an X500 principal name, only an + authenticated client with that principal will be able to bypass + ACL checking and have full privileges to all znodes. + +* *zookeeper.superUser* : + (Java system property: **zookeeper.superUser**) + Similar to **zookeeper.X509AuthenticationProvider.superUser** + but is generic for SASL based logins. It stores the name of + a user that can access the znode hierarchy as a "super" user. + +* *ssl.authProvider* : + (Java system property: **zookeeper.ssl.authProvider**) + Specifies a subclass of **org.apache.zookeeper.auth.X509AuthenticationProvider** + to use for secure client authentication. This is useful in + certificate key infrastructures that do not use JKS. It may be + necessary to extend **javax.net.ssl.X509KeyManager** and **javax.net.ssl.X509TrustManager** + to get the desired behavior from the SSL stack. To configure the + ZooKeeper server to use the custom provider for authentication, + choose a scheme name for the custom AuthenticationProvider and + set the property **zookeeper.authProvider.[scheme]** to the fully-qualified class name of the custom + implementation. This will load the provider into the ProviderRegistry. + Then set this property **zookeeper.ssl.authProvider=[scheme]** and that provider + will be used for secure authentication. + +* *zookeeper.ensembleAuthName* : + (Java system property only: **zookeeper.ensembleAuthName**) + **New in 3.6.0:** + Specify a list of comma-separated valid names/aliases of an ensemble. A client + can provide the ensemble name it intends to connect as the credential for scheme "ensemble". The EnsembleAuthenticationProvider will check the credential against + the list of names/aliases of the ensemble that receives the connection request. + If the credential is not in the list, the connection request will be refused. + This prevents a client accidentally connecting to a wrong ensemble. + +* *zookeeper.sessionRequireClientSASLAuth* : + (Java system property only: **zookeeper.sessionRequireClientSASLAuth**) + **New in 3.6.0:** + When set to **true**, ZooKeeper server will only accept connections and requests from clients + that have authenticated with server via SASL. Clients that are not configured with SASL + authentication, or configured with SASL but failed authentication (i.e. with invalid credential) + will not be able to establish a session with server. A typed error code (-124) will be delivered + in such case, both Java and C client will close the session with server thereafter, + without further attempts on retrying to reconnect. + + By default, this feature is disabled. Users who would like to opt-in can enable the feature + by setting **zookeeper.sessionRequireClientSASLAuth** to **true**. + + This feature overrules the zookeeper.allowSaslFailedClients option, so even if server is + configured to allow clients that fail SASL authentication to login, client will not be able to + establish a session with server if this feature is enabled. + +* *sslQuorum* : + (Java system property: **zookeeper.sslQuorum**) + **New in 3.5.5:** + Enables encrypted quorum communication. Default is `false`. + +* *ssl.keyStore.location and ssl.keyStore.password* and *ssl.quorum.keyStore.location* and *ssl.quorum.keyStore.password* : + (Java system properties: **zookeeper.ssl.keyStore.location** and **zookeeper.ssl.keyStore.password** and **zookeeper.ssl.quorum.keyStore.location** and **zookeeper.ssl.quorum.keyStore.password**) + **New in 3.5.5:** + Specifies the file path to a Java keystore containing the local + credentials to be used for client and quorum TLS connections, and the + password to unlock the file. + +* *ssl.keyStore.type* and *ssl.quorum.keyStore.type* : + (Java system properties: **zookeeper.ssl.keyStore.type** and **zookeeper.ssl.quorum.keyStore.type**) + **New in 3.5.5:** + Specifies the file format of client and quorum keystores. Values: JKS, PEM, PKCS12 or null (detect by filename). + Default: null. + **New in 3.6.3, 3.7.0:** + The format BCFKS was added. + +* *ssl.trustStore.location* and *ssl.trustStore.password* and *ssl.quorum.trustStore.location* and *ssl.quorum.trustStore.password* : + (Java system properties: **zookeeper.ssl.trustStore.location** and **zookeeper.ssl.trustStore.password** and **zookeeper.ssl.quorum.trustStore.location** and **zookeeper.ssl.quorum.trustStore.password**) + **New in 3.5.5:** + Specifies the file path to a Java truststore containing the remote + credentials to be used for client and quorum TLS connections, and the + password to unlock the file. + +* *ssl.trustStore.type* and *ssl.quorum.trustStore.type* : + (Java system properties: **zookeeper.ssl.trustStore.type** and **zookeeper.ssl.quorum.trustStore.type**) + **New in 3.5.5:** + Specifies the file format of client and quorum trustStores. Values: JKS, PEM, PKCS12 or null (detect by filename). + Default: null. + **New in 3.6.3, 3.7.0:** + The format BCFKS was added. + +* *ssl.protocol* and *ssl.quorum.protocol* : + (Java system properties: **zookeeper.ssl.protocol** and **zookeeper.ssl.quorum.protocol**) + **New in 3.5.5:** + Specifies to protocol to be used in client and quorum TLS negotiation. + Default: TLSv1.2 + +* *ssl.enabledProtocols* and *ssl.quorum.enabledProtocols* : + (Java system properties: **zookeeper.ssl.enabledProtocols** and **zookeeper.ssl.quorum.enabledProtocols**) + **New in 3.5.5:** + Specifies the enabled protocols in client and quorum TLS negotiation. + Default: value of `protocol` property + +* *ssl.ciphersuites* and *ssl.quorum.ciphersuites* : + (Java system properties: **zookeeper.ssl.ciphersuites** and **zookeeper.ssl.quorum.ciphersuites**) + **New in 3.5.5:** + Specifies the enabled cipher suites to be used in client and quorum TLS negotiation. + Default: Enabled cipher suites depend on the Java runtime version being used. + +* *ssl.context.supplier.class* and *ssl.quorum.context.supplier.class* : + (Java system properties: **zookeeper.ssl.context.supplier.class** and **zookeeper.ssl.quorum.context.supplier.class**) + **New in 3.5.5:** + Specifies the class to be used for creating SSL context in client and quorum SSL communication. + This allows you to use custom SSL context and implement the following scenarios: + 1. Use hardware keystore, loaded in using PKCS11 or something similar. + 2. You don't have access to the software keystore, but can retrieve an already-constructed SSLContext from their container. + Default: null + +* *ssl.hostnameVerification* and *ssl.quorum.hostnameVerification* : + (Java system properties: **zookeeper.ssl.hostnameVerification** and **zookeeper.ssl.quorum.hostnameVerification**) + **New in 3.5.5:** + Specifies whether the hostname verification is enabled in client and quorum TLS negotiation process. + Disabling it only recommended for testing purposes. + Default: true + +* *ssl.crl* and *ssl.quorum.crl* : + (Java system properties: **zookeeper.ssl.crl** and **zookeeper.ssl.quorum.crl**) + **New in 3.5.5:** + Specifies whether Certificate Revocation List is enabled in client and quorum TLS protocols. + Default: false + +* *ssl.ocsp* and *ssl.quorum.ocsp* : + (Java system properties: **zookeeper.ssl.ocsp** and **zookeeper.ssl.quorum.ocsp**) + **New in 3.5.5:** + Specifies whether Online Certificate Status Protocol is enabled in client and quorum TLS protocols. + Default: false + +* *ssl.clientAuth* and *ssl.quorum.clientAuth* : + (Java system properties: **zookeeper.ssl.clientAuth** and **zookeeper.ssl.quorum.clientAuth**) + **Added in 3.5.5, but broken until 3.5.7:** + Specifies options to authenticate ssl connections from clients. Valid values are + + * "none": server will not request client authentication + * "want": server will "request" client authentication + * "need": server will "require" client authentication + + Default: "need" + +* *ssl.handshakeDetectionTimeoutMillis* and *ssl.quorum.handshakeDetectionTimeoutMillis* : + (Java system properties: **zookeeper.ssl.handshakeDetectionTimeoutMillis** and **zookeeper.ssl.quorum.handshakeDetectionTimeoutMillis**) + **New in 3.5.5:** + TBD + +* *client.portUnification*: + (Java system property: **zookeeper.client.portUnification**) + Specifies that the client port should accept SSL connections + (using the same configuration as the secure client port). + Default: false + +* *authProvider*: + (Java system property: **zookeeper.authProvider**) + You can specify multiple authentication provider classes for ZooKeeper. + Usually you use this parameter to specify the SASL authentication provider + like: `authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider` + +* *kerberos.removeHostFromPrincipal* + (Java system property: **zookeeper.kerberos.removeHostFromPrincipal**) + You can instruct ZooKeeper to remove the host from the client principal name during authentication. + (e.g. the zk/myhost@EXAMPLE.COM client principal will be authenticated in ZooKeeper as zk@EXAMPLE.COM) + Default: false + +* *kerberos.removeRealmFromPrincipal* + (Java system property: **zookeeper.kerberos.removeRealmFromPrincipal**) + You can instruct ZooKeeper to remove the realm from the client principal name during authentication. + (e.g. the zk/myhost@EXAMPLE.COM client principal will be authenticated in ZooKeeper as zk/myhost) + Default: false + +* *multiAddress.enabled* : + (Java system property: **zookeeper.multiAddress.enabled**) + **New in 3.6.0:** + Since ZooKeeper 3.6.0 you can also [specify multiple addresses](#id_multi_address) + for each ZooKeeper server instance (this can increase availability when multiple physical + network interfaces can be used parallel in the cluster). Setting this parameter to + **true** will enable this feature. Please note, that you can not enable this feature + during a rolling upgrade if the version of the old ZooKeeper cluster is prior to 3.6.0. + The default value is **false**. + +* *multiAddress.reachabilityCheckTimeoutMs* : + (Java system property: **zookeeper.multiAddress.reachabilityCheckTimeoutMs**) + **New in 3.6.0:** + Since ZooKeeper 3.6.0 you can also [specify multiple addresses](#id_multi_address) + for each ZooKeeper server instance (this can increase availability when multiple physical + network interfaces can be used parallel in the cluster). ZooKeeper will perform ICMP ECHO requests + or try to establish a TCP connection on port 7 (Echo) of the destination host in order to find + the reachable addresses. This happens only if you provide multiple addresses in the configuration. + In this property you can set the timeout in millisecs for the reachability check. The check happens + in parallel for the different addresses, so the timeout you set here is the maximum time will be taken + by checking the reachability of all addresses. + The default value is **1000**. + + This parameter has no effect, unless you enable the MultiAddress feature by setting *multiAddress.enabled=true*. + + + +#### Experimental Options/Features + +New features that are currently considered experimental. + +* *Read Only Mode Server* : + (Java system property: **readonlymode.enabled**) + **New in 3.4.0:** + Setting this value to true enables Read Only Mode server + support (disabled by default). ROM allows clients + sessions which requested ROM support to connect to the + server even when the server might be partitioned from + the quorum. In this mode ROM clients can still read + values from the ZK service, but will be unable to write + values and see changes from other clients. See + ZOOKEEPER-784 for more details. + + + +#### Unsafe Options + +The following options can be useful, but be careful when you use +them. The risk of each is explained along with the explanation of what +the variable does. + +* *forceSync* : + (Java system property: **zookeeper.forceSync**) + Requires updates to be synced to media of the transaction + log before finishing processing the update. If this option is + set to no, ZooKeeper will not require updates to be synced to + the media. + +* *jute.maxbuffer* : + (Java system property:**jute.maxbuffer**). + - This option can only be set as a Java system property. + There is no zookeeper prefix on it. It specifies the maximum + size of the data that can be stored in a znode. The unit is: byte. The default is + 0xfffff(1048575) bytes, or just under 1M. + - If this option is changed, the system property must be set on all servers and clients otherwise + problems will arise. + - When *jute.maxbuffer* in the client side is greater than the server side, the client wants to write the data + exceeds *jute.maxbuffer* in the server side, the server side will get **java.io.IOException: Len error** + - When *jute.maxbuffer* in the client side is less than the server side, the client wants to read the data + exceeds *jute.maxbuffer* in the client side, the client side will get **java.io.IOException: Unreasonable length** + or **Packet len is out of range!** + - This is really a sanity check. ZooKeeper is designed to store data on the order of kilobytes in size. + In the production environment, increasing this property to exceed the default value is not recommended for the following reasons: + - Large size znodes cause unwarranted latency spikes, worsen the throughput + - Large size znodes make the synchronization time between leader and followers unpredictable and non-convergent(sometimes timeout), cause the quorum unstable + +* *jute.maxbuffer.extrasize*: + (Java system property: **zookeeper.jute.maxbuffer.extrasize**) + **New in 3.5.7:** + While processing client requests ZooKeeper server adds some additional information into + the requests before persisting it as a transaction. Earlier this additional information size + was fixed to 1024 bytes. For many scenarios, specially scenarios where jute.maxbuffer value + is more than 1 MB and request type is multi, this fixed size was insufficient. + To handle all the scenarios additional information size is increased from 1024 byte + to same as jute.maxbuffer size and also it is made configurable through jute.maxbuffer.extrasize. + Generally this property is not required to be configured as default value is the most optimal value. + +* *skipACL* : + (Java system property: **zookeeper.skipACL**) + Skips ACL checks. This results in a boost in throughput, + but opens up full access to the data tree to everyone. + +* *quorumListenOnAllIPs* : + When set to true the ZooKeeper server will listen + for connections from its peers on all available IP addresses, + and not only the address configured in the server list of the + configuration file. It affects the connections handling the + ZAB protocol and the Fast Leader Election protocol. Default + value is **false**. + +* *multiAddress.reachabilityCheckEnabled* : + (Java system property: **zookeeper.multiAddress.reachabilityCheckEnabled**) + **New in 3.6.0:** + Since ZooKeeper 3.6.0 you can also [specify multiple addresses](#id_multi_address) + for each ZooKeeper server instance (this can increase availability when multiple physical + network interfaces can be used parallel in the cluster). ZooKeeper will perform ICMP ECHO requests + or try to establish a TCP connection on port 7 (Echo) of the destination host in order to find + the reachable addresses. This happens only if you provide multiple addresses in the configuration. + The reachable check can fail if you hit some ICMP rate-limitation, (e.g. on MacOS) when you try to + start a large (e.g. 11+) ensemble members cluster on a single machine for testing. + + Default value is **true**. By setting this parameter to 'false' you can disable the reachability checks. + Please note, disabling the reachability check will cause the cluster not to be able to reconfigure + itself properly during network problems, so the disabling is advised only during testing. + + This parameter has no effect, unless you enable the MultiAddress feature by setting *multiAddress.enabled=true*. + + + +#### Disabling data directory autocreation + +**New in 3.5:** The default +behavior of a ZooKeeper server is to automatically create the +data directory (specified in the configuration file) when +started if that directory does not already exist. This can be +inconvenient and even dangerous in some cases. Take the case +where a configuration change is made to a running server, +wherein the **dataDir** parameter +is accidentally changed. When the ZooKeeper server is +restarted it will create this non-existent directory and begin +serving - with an empty znode namespace. This scenario can +result in an effective "split brain" situation (i.e. data in +both the new invalid directory and the original valid data +store). As such is would be good to have an option to turn off +this autocreate behavior. In general for production +environments this should be done, unfortunately however the +default legacy behavior cannot be changed at this point and +therefore this must be done on a case by case basis. This is +left to users and to packagers of ZooKeeper distributions. + +When running **zkServer.sh** autocreate can be disabled +by setting the environment variable **ZOO_DATADIR_AUTOCREATE_DISABLE** to 1. +When running ZooKeeper servers directly from class files this +can be accomplished by setting **zookeeper.datadir.autocreate=false** on +the java command line, i.e. **-Dzookeeper.datadir.autocreate=false** + +When this feature is disabled, and the ZooKeeper server +determines that the required directories do not exist it will +generate an error and refuse to start. + +A new script **zkServer-initialize.sh** is provided to +support this new feature. If autocreate is disabled it is +necessary for the user to first install ZooKeeper, then create +the data directory (and potentially txnlog directory), and +then start the server. Otherwise as mentioned in the previous +paragraph the server will not start. Running **zkServer-initialize.sh** will create the +required directories, and optionally setup the myid file +(optional command line parameter). This script can be used +even if the autocreate feature itself is not used, and will +likely be of use to users as this (setup, including creation +of the myid file) has been an issue for users in the past. +Note that this script ensures the data directories exist only, +it does not create a config file, but rather requires a config +file to be available in order to execute. + + + +#### Enabling db existence validation + +**New in 3.6.0:** The default +behavior of a ZooKeeper server on startup when no data tree +is found is to set zxid to zero and join the quorum as a +voting member. This can be dangerous if some event (e.g. a +rogue 'rm -rf') has removed the data directory while the +server was down since this server may help elect a leader +that is missing transactions. Enabling db existence validation +will change the behavior on startup when no data tree is +found: the server joins the ensemble as a non-voting participant +until it is able to sync with the leader and acquire an up-to-date +version of the ensemble data. To indicate an empty data tree is +expected (ensemble creation), the user should place a file +'initialize' in the same directory as 'myid'. This file will +be detected and deleted by the server on startup. + +Initialization validation can be enabled when running +ZooKeeper servers directly from class files by setting +**zookeeper.db.autocreate=false** +on the java command line, i.e. +**-Dzookeeper.db.autocreate=false**. +Running **zkServer-initialize.sh** +will create the required initialization file. + + + +#### Performance Tuning Options + +**New in 3.5.0:** Several subsystems have been reworked +to improve read throughput. This includes multi-threading of the NIO communication subsystem and +request processing pipeline (Commit Processor). NIO is the default client/server communication +subsystem. Its threading model comprises 1 acceptor thread, 1-N selector threads and 0-M +socket I/O worker threads. In the request processing pipeline the system can be configured +to process multiple read request at once while maintaining the same consistency guarantee +(same-session read-after-write). The Commit Processor threading model comprises 1 main +thread and 0-N worker threads. + +The default values are aimed at maximizing read throughput on a dedicated ZooKeeper machine. +Both subsystems need to have sufficient amount of threads to achieve peak read throughput. + +* *zookeeper.nio.numSelectorThreads* : + (Java system property only: **zookeeper.nio.numSelectorThreads**) + **New in 3.5.0:** + Number of NIO selector threads. At least 1 selector thread required. + It is recommended to use more than one selector for large numbers + of client connections. The default value is sqrt( number of cpu cores / 2 ). + +* *zookeeper.nio.numWorkerThreads* : + (Java system property only: **zookeeper.nio.numWorkerThreads**) + **New in 3.5.0:** + Number of NIO worker threads. If configured with 0 worker threads, the selector threads + do the socket I/O directly. The default value is 2 times the number of cpu cores. + +* *zookeeper.commitProcessor.numWorkerThreads* : + (Java system property only: **zookeeper.commitProcessor.numWorkerThreads**) + **New in 3.5.0:** + Number of Commit Processor worker threads. If configured with 0 worker threads, the main thread + will process the request directly. The default value is the number of cpu cores. + +* *zookeeper.commitProcessor.maxReadBatchSize* : + (Java system property only: **zookeeper.commitProcessor.maxReadBatchSize**) + Max number of reads to process from queuedRequests before switching to processing commits. + If the value < 0 (default), we switch whenever we have a local write, and pending commits. + A high read batch size will delay commit processing, causing stale data to be served. + If reads are known to arrive in fixed size batches then matching that batch size with + the value of this property can smooth queue performance. Since reads are handled in parallel, + one recommendation is to set this property to match *zookeeper.commitProcessor.numWorkerThread* + (default is the number of cpu cores) or lower. + +* *zookeeper.commitProcessor.maxCommitBatchSize* : + (Java system property only: **zookeeper.commitProcessor.maxCommitBatchSize**) + Max number of commits to process before processing reads. We will try to process as many + remote/local commits as we can till we reach this count. A high commit batch size will delay + reads while processing more commits. A low commit batch size will favor reads. + It is recommended to only set this property when an ensemble is serving a workload with a high + commit rate. If writes are known to arrive in a set number of batches then matching that + batch size with the value of this property can smooth queue performance. A generic + approach would be to set this value to equal the ensemble size so that with the processing + of each batch the current server will probabilistically handle a write related to one of + its direct clients. + Default is "1". Negative and zero values are not supported. + +* *znode.container.checkIntervalMs* : + (Java system property only) + **New in 3.6.0:** The + time interval in milliseconds for each check of candidate container + and ttl nodes. Default is "60000". + +* *znode.container.maxPerMinute* : + (Java system property only) + **New in 3.6.0:** The + maximum number of container and ttl nodes that can be deleted per + minute. This prevents herding during container deletion. + Default is "10000". + +* *znode.container.maxNeverUsedIntervalMs* : + (Java system property only) + **New in 3.6.0:** The + maximum interval in milliseconds that a container that has never had + any children is retained. Should be long enough for your client to + create the container, do any needed work and then create children. + Default is "0" which is used to indicate that containers + that have never had any children are never deleted. + + + +#### Debug Observability Configurations + +**New in 3.6.0:** The following options are introduced to make zookeeper easier to debug. + +* *zookeeper.messageTracker.BufferSize* : + (Java system property only) + Controls the maximum number of messages stored in **MessageTracker**. Value should be positive + integers. The default value is 10. **MessageTracker** is introduced in **3.6.0** to record the + last set of messages between a server (follower or observer) and a leader, when a server + disconnects with leader. These set of messages will then be dumped to zookeeper's log file, + and will help reconstruct the state of the servers at the time of the disconnection and + will be useful for debugging purpose. + +* *zookeeper.messageTracker.Enabled* : + (Java system property only) + When set to "true", will enable **MessageTracker** to track and record messages. Default value + is "false". + + + +#### AdminServer configuration + +**New in 3.7.1:** The following +options are used to configure the [AdminServer](#sc_adminserver). + +* *admin.forceHttps* : + (Java system property: **zookeeper.admin.forceHttps**) + Force AdminServer to use SSL, thus allowing only HTTPS traffic. + Defaults to disabled. + Overwrites **admin.portUnification** settings. + +**New in 3.6.0:** The following +options are used to configure the [AdminServer](#sc_adminserver). + +* *admin.portUnification* : + (Java system property: **zookeeper.admin.portUnification**) + Enable the admin port to accept both HTTP and HTTPS traffic. + Defaults to disabled. + +**New in 3.5.0:** The following +options are used to configure the [AdminServer](#sc_adminserver). + +* *admin.enableServer* : + (Java system property: **zookeeper.admin.enableServer**) + Set to "false" to disable the AdminServer. By default the + AdminServer is enabled. + +* *admin.serverAddress* : + (Java system property: **zookeeper.admin.serverAddress**) + The address the embedded Jetty server listens on. Defaults to 0.0.0.0. + +* *admin.serverPort* : + (Java system property: **zookeeper.admin.serverPort**) + The port the embedded Jetty server listens on. Defaults to 8080. + +* *admin.idleTimeout* : + (Java system property: **zookeeper.admin.idleTimeout**) + Set the maximum idle time in milliseconds that a connection can wait + before sending or receiving data. Defaults to 30000 ms. + +* *admin.commandURL* : + (Java system property: **zookeeper.admin.commandURL**) + The URL for listing and issuing commands relative to the + root URL. Defaults to "/commands". + +### Metrics Providers + +**New in 3.6.0:** The following options are used to configure metrics. + + By default ZooKeeper server exposes useful metrics using the [AdminServer](#sc_adminserver). + and [Four Letter Words](#sc_4lw) interface. + + Since 3.6.0 you can configure a different Metrics Provider, that exports metrics + to your favourite system. + + Since 3.6.0 ZooKeeper binary package bundles an integration with [Prometheus.io](https://prometheus.io) + +* *metricsProvider.className* : + Set to "org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider" to + enable Prometheus.io exporter. + +* *metricsProvider.httpPort* : + Prometheus.io exporter will start a Jetty server and bind to this port, it default to 7000. + Prometheus end point will be http://hostname:httPort/metrics. + +* *metricsProvider.exportJvmInfo* : + If this property is set to **true** Prometheus.io will export useful metrics about the JVM. + The default is true. + + + +### Communication using the Netty framework + +[Netty](http://netty.io) +is an NIO based client/server communication framework, it +simplifies (over NIO being used directly) many of the +complexities of network level communication for java +applications. Additionally the Netty framework has built +in support for encryption (SSL) and authentication +(certificates). These are optional features and can be +turned on or off individually. + +In versions 3.5+, a ZooKeeper server can use Netty +instead of NIO (default option) by setting the environment +variable **zookeeper.serverCnxnFactory** +to **org.apache.zookeeper.server.NettyServerCnxnFactory**; +for the client, set **zookeeper.clientCnxnSocket** +to **org.apache.zookeeper.ClientCnxnSocketNetty**. + + + +#### Quorum TLS + +*New in 3.5.5* + +Based on the Netty Framework ZooKeeper ensembles can be set up +to use TLS encryption in their communication channels. This section +describes how to set up encryption on the quorum communication. + +Please note that Quorum TLS encapsulates securing both leader election +and quorum communication protocols. + +1. Create SSL keystore JKS to store local credentials + +One keystore should be created for each ZK instance. + +In this example we generate a self-signed certificate and store it +together with the private key in `keystore.jks`. This is suitable for +testing purposes, but you probably need an official certificate to sign +your keys in a production environment. + +Please note that the alias (`-alias`) and the distinguished name (`-dname`) +must match the hostname of the machine that is associated with, otherwise +hostname verification won't work. + +``` +keytool -genkeypair -alias $(hostname -f) -keyalg RSA -keysize 2048 -dname "cn=$(hostname -f)" -keypass password -keystore keystore.jks -storepass password +``` + +2. Extract the signed public key (certificate) from keystore + +*This step might only necessary for self-signed certificates.* + +``` +keytool -exportcert -alias $(hostname -f) -keystore keystore.jks -file $(hostname -f).cer -rfc +``` + +3. Create SSL truststore JKS containing certificates of all ZooKeeper instances + +The same truststore (storing all accepted certs) should be shared on +participants of the ensemble. You need to use different aliases to store +multiple certificates in the same truststore. Name of the aliases doesn't matter. + +``` +keytool -importcert -alias [host1..3] -file [host1..3].cer -keystore truststore.jks -storepass password +``` + +4. You need to use `NettyServerCnxnFactory` as serverCnxnFactory, because SSL is not supported by NIO. +Add the following configuration settings to your `zoo.cfg` config file: + +``` +sslQuorum=true +serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory +ssl.quorum.keyStore.location=/path/to/keystore.jks +ssl.quorum.keyStore.password=password +ssl.quorum.trustStore.location=/path/to/truststore.jks +ssl.quorum.trustStore.password=password +``` + +5. Verify in the logs that your ensemble is running on TLS: + +``` +INFO [main:QuorumPeer@1789] - Using TLS encrypted quorum communication +INFO [main:QuorumPeer@1797] - Port unification disabled +... +INFO [QuorumPeerListener:QuorumCnxManager$Listener@877] - Creating TLS-only quorum server socket +``` + + + +#### Upgrading existing non-TLS cluster with no downtime + +*New in 3.5.5* + +Here are the steps needed to upgrade an already running ZooKeeper ensemble +to TLS without downtime by taking advantage of port unification functionality. + +1. Create the necessary keystores and truststores for all ZK participants as described in the previous section + +2. Add the following config settings and restart the first node + +``` +sslQuorum=false +portUnification=true +serverCnxnFactory=org.apache.zookeeper.server.NettyServerCnxnFactory +ssl.quorum.keyStore.location=/path/to/keystore.jks +ssl.quorum.keyStore.password=password +ssl.quorum.trustStore.location=/path/to/truststore.jks +ssl.quorum.trustStore.password=password +``` + +Note that TLS is not yet enabled, but we turn on port unification. + +3. Repeat step #2 on the remaining nodes. Verify that you see the following entries in the logs: + +``` +INFO [main:QuorumPeer@1791] - Using insecure (non-TLS) quorum communication +INFO [main:QuorumPeer@1797] - Port unification enabled +... +INFO [QuorumPeerListener:QuorumCnxManager$Listener@874] - Creating TLS-enabled quorum server socket +``` + +You should also double check after each node restart that the quorum become healthy again. + +4. Enable Quorum TLS on each node and do rolling restart: + +``` +sslQuorum=true +portUnification=true +``` + +5. Once you verified that your entire ensemble is running on TLS, you could disable port unification +and do another rolling restart + +``` +sslQuorum=true +portUnification=false +``` + + + + +### ZooKeeper Commands + + + +#### The Four Letter Words + +ZooKeeper responds to a small set of commands. Each command is +composed of four letters. You issue the commands to ZooKeeper via telnet +or nc, at the client port. + +Three of the more interesting commands: "stat" gives some +general information about the server and connected clients, +while "srvr" and "cons" give extended details on server and +connections respectively. + +**New in 3.5.3:** +Four Letter Words need to be explicitly white listed before using. +Please refer **4lw.commands.whitelist** +described in [cluster configuration section](#sc_clusterOptions) for details. +Moving forward, Four Letter Words will be deprecated, please use +[AdminServer](#sc_adminserver) instead. + +* *conf* : + **New in 3.3.0:** Print + details about serving configuration. + +* *cons* : + **New in 3.3.0:** List + full connection/session details for all clients connected + to this server. Includes information on numbers of packets + received/sent, session id, operation latencies, last + operation performed, etc... + +* *crst* : + **New in 3.3.0:** Reset + connection/session statistics for all connections. + +* *dump* : + Lists the outstanding sessions and ephemeral nodes. + +* *envi* : + Print details about serving environment + +* *ruok* : + Tests if server is running in a non-error state. The server + will respond with imok if it is running. Otherwise it will not + respond at all. + A response of "imok" does not necessarily indicate that the + server has joined the quorum, just that the server process is active + and bound to the specified client port. Use "stat" for details on + state wrt quorum and client connection information. + +* *srst* : + Reset server statistics. + +* *srvr* : + **New in 3.3.0:** Lists + full details for the server. + +* *stat* : + Lists brief details for the server and connected + clients. + +* *wchs* : + **New in 3.3.0:** Lists + brief information on watches for the server. + +* *wchc* : + **New in 3.3.0:** Lists + detailed information on watches for the server, by + session. This outputs a list of sessions(connections) + with associated watches (paths). Note, depending on the + number of watches this operation may be expensive (ie + impact server performance), use it carefully. + +* *dirs* : + **New in 3.5.1:** + Shows the total size of snapshot and log files in bytes + +* *wchp* : + **New in 3.3.0:** Lists + detailed information on watches for the server, by path. + This outputs a list of paths (znodes) with associated + sessions. Note, depending on the number of watches this + operation may be expensive (ie impact server performance), + use it carefully. + +* *mntr* : + **New in 3.4.0:** Outputs a list + of variables that could be used for monitoring the health of the cluster. + + + $ echo mntr | nc localhost 2185 + zk_version 3.4.0 + zk_avg_latency 0.7561 - be account to four decimal places + zk_max_latency 0 + zk_min_latency 0 + zk_packets_received 70 + zk_packets_sent 69 + zk_outstanding_requests 0 + zk_server_state leader + zk_znode_count 4 + zk_watch_count 0 + zk_ephemerals_count 0 + zk_approximate_data_size 27 + zk_followers 4 - only exposed by the Leader + zk_synced_followers 4 - only exposed by the Leader + zk_pending_syncs 0 - only exposed by the Leader + zk_open_file_descriptor_count 23 - only available on Unix platforms + zk_max_file_descriptor_count 1024 - only available on Unix platforms + + +The output is compatible with java properties format and the content +may change over time (new keys added). Your scripts should expect changes. +ATTENTION: Some of the keys are platform specific and some of the keys are only exported by the Leader. +The output contains multiple lines with the following format: + + + key \t value + + +* *isro* : + **New in 3.4.0:** Tests if + server is running in read-only mode. The server will respond with + "ro" if in read-only mode or "rw" if not in read-only mode. + +* *hash* : + **New in 3.6.0:** + Return the latest history of the tree digest associated with zxid. + +* *gtmk* : + Gets the current trace mask as a 64-bit signed long value in + decimal format. See `stmk` for an explanation of + the possible values. + +* *stmk* : + Sets the current trace mask. The trace mask is 64 bits, + where each bit enables or disables a specific category of trace + logging on the server. Log4J must be configured to enable + `TRACE` level first in order to see trace logging + messages. The bits of the trace mask correspond to the following + trace logging categories. + + | Trace Mask Bit Values | | + |-----------------------|---------------------| + | 0b0000000000 | Unused, reserved for future use. | + | 0b0000000010 | Logs client requests, excluding ping requests. | + | 0b0000000100 | Unused, reserved for future use. | + | 0b0000001000 | Logs client ping requests. | + | 0b0000010000 | Logs packets received from the quorum peer that is the current leader, excluding ping requests. | + | 0b0000100000 | Logs addition, removal and validation of client sessions. | + | 0b0001000000 | Logs delivery of watch events to client sessions. | + | 0b0010000000 | Logs ping packets received from the quorum peer that is the current leader. | + | 0b0100000000 | Unused, reserved for future use. | + | 0b1000000000 | Unused, reserved for future use. | + + All remaining bits in the 64-bit value are unused and + reserved for future use. Multiple trace logging categories are + specified by calculating the bitwise OR of the documented values. + The default trace mask is 0b0100110010. Thus, by default, trace + logging includes client requests, packets received from the + leader and sessions. + To set a different trace mask, send a request containing the + `stmk` four-letter word followed by the trace + mask represented as a 64-bit signed long value. This example uses + the Perl `pack` function to construct a trace + mask that enables all trace logging categories described above and + convert it to a 64-bit signed long value with big-endian byte + order. The result is appended to `stmk` and sent + to the server using netcat. The server responds with the new + trace mask in decimal format. + + + $ perl -e "print 'stmk', pack('q>', 0b0011111010)" | nc localhost 2181 + 250 + + +Here's an example of the **ruok** +command: + + + $ echo ruok | nc 127.0.0.1 5111 + imok + + + + +#### The AdminServer + +**New in 3.5.0:** The AdminServer is +an embedded Jetty server that provides an HTTP interface to the four +letter word commands. By default, the server is started on port 8080, +and commands are issued by going to the URL "/commands/\[command name]", +e.g., http://localhost:8080/commands/stat. The command response is +returned as JSON. Unlike the original protocol, commands are not +restricted to four-letter names, and commands can have multiple names; +for instance, "stmk" can also be referred to as "set_trace_mask". To +view a list of all available commands, point a browser to the URL +/commands (e.g., http://localhost:8080/commands). See the [AdminServer configuration options](#sc_adminserver_config) +for how to change the port and URLs. + +The AdminServer is enabled by default, but can be disabled by either: + +* Setting the zookeeper.admin.enableServer system + property to false. +* Removing Jetty from the classpath. (This option is + useful if you would like to override ZooKeeper's jetty + dependency.) + +Note that the TCP four letter word interface is still available if +the AdminServer is disabled. + +Available commands include: + +* *connection_stat_reset/crst*: + Reset all client connection statistics. + No new fields returned. + +* *configuration/conf/config* : + Print basic details about serving configuration, e.g. + client port, absolute path to data directory. + +* *connections/cons* : + Information on client connections to server. + Note, depending on the number of client connections this operation may be expensive + (i.e. impact server performance). + Returns "connections", a list of connection info objects. + +* *hash*: + Txn digests in the historical digest list. + One is recorded every 128 transactions. + Returns "digests", a list to transaction digest objects. + +* *dirs* : + Information on logfile directory and snapshot directory + size in bytes. + Returns "datadir_size" and "logdir_size". + +* *dump* : + Information on session expirations and ephemerals. + Note, depending on the number of global sessions and ephemerals + this operation may be expensive (i.e. impact server performance). + Returns "expiry_time_to_session_ids" and "session_id_to_ephemeral_paths" as maps. + +* *environment/env/envi* : + All defined environment variables. + Returns each as its own field. + +* *get_trace_mask/gtmk* : + The current trace mask. Read-only version of *set_trace_mask*. + See the description of the four letter command *stmk* for + more details. + Returns "tracemask". + +* *initial_configuration/icfg* : + Print the text of the configuration file used to start the peer. + Returns "initial_configuration". + +* *is_read_only/isro* : + A true/false if this server is in read-only mode. + Returns "read_only". + +* *last_snapshot/lsnp* : + Information of the last snapshot that zookeeper server has finished saving to disk. + If called during the initial time period between the server starting up + and the server finishing saving its first snapshot, the command returns the + information of the snapshot read when starting up the server. + Returns "zxid" and "timestamp", the latter using a time unit of seconds. + +* *leader/lead* : + If the ensemble is configured in quorum mode then emits the current leader + status of the peer and the current leader location. + Returns "is_leader", "leader_id", and "leader_ip". + +* *monitor/mntr* : + Emits a wide variety of useful info for monitoring. + Includes performance stats, information about internal queues, and + summaries of the data tree (among other things). + Returns each as its own field. + +* *observer_connection_stat_reset/orst* : + Reset all observer connection statistics. Companion command to *observers*. + No new fields returned. + +* *ruok* : + No-op command, check if the server is running. + A response does not necessarily indicate that the + server has joined the quorum, just that the admin server + is active and bound to the specified port. + No new fields returned. + +* *set_trace_mask/stmk* : + Sets the trace mask (as such, it requires a parameter). + Write version of *get_trace_mask*. + See the description of the four letter command *stmk* for + more details. + Returns "tracemask". + +* *server_stats/srvr* : + Server information. + Returns multiple fields giving a brief overview of server state. + +* *stats/stat* : + Same as *server_stats* but also returns the "connections" field (see *connections* + for details). + Note, depending on the number of client connections this operation may be expensive + (i.e. impact server performance). + +* *stat_reset/srst* : + Resets server statistics. This is a subset of the information returned + by *server_stats* and *stats*. + No new fields returned. + +* *observers/obsr* : + Information on observer connections to server. + Always available on a Leader, available on a Follower if its + acting as a learner master. + Returns "synced_observers" (int) and "observers" (list of per-observer properties). + +* *system_properties/sysp* : + All defined system properties. + Returns each as its own field. + +* *voting_view* : + Provides the current voting members in the ensemble. + Returns "current_config" as a map. + +* *watches/wchc* : + Watch information aggregated by session. + Note, depending on the number of watches this operation may be expensive + (i.e. impact server performance). + Returns "session_id_to_watched_paths" as a map. + +* *watches_by_path/wchp* : + Watch information aggregated by path. + Note, depending on the number of watches this operation may be expensive + (i.e. impact server performance). + Returns "path_to_session_ids" as a map. + +* *watch_summary/wchs* : + Summarized watch information. + Returns "num_total_watches", "num_paths", and "num_connections". + +* *zabstate* : + The current phase of Zab protocol that peer is running and whether it is a + voting member. + Peers can be in one of these phases: ELECTION, DISCOVERY, SYNCHRONIZATION, BROADCAST. + Returns fields "voting" and "zabstate". + + + + +### Data File Management + +ZooKeeper stores its data in a data directory and its transaction +log in a transaction log directory. By default these two directories are +the same. The server can (and should) be configured to store the +transaction log files in a separate directory than the data files. +Throughput increases and latency decreases when transaction logs reside +on a dedicated log devices. + + + +#### The Data Directory + +This directory has two or three files in it: + +* *myid* - contains a single integer in + human readable ASCII text that represents the server id. +* *initialize* - presence indicates lack of + data tree is expected. Cleaned up once data tree is created. +* *snapshot.* - holds the fuzzy + snapshot of a data tree. + +Each ZooKeeper server has a unique id. This id is used in two +places: the *myid* file and the configuration file. +The *myid* file identifies the server that +corresponds to the given data directory. The configuration file lists +the contact information for each server identified by its server id. +When a ZooKeeper server instance starts, it reads its id from the +*myid* file and then, using that id, reads from the +configuration file, looking up the port on which it should +listen. + +The *snapshot* files stored in the data +directory are fuzzy snapshots in the sense that during the time the +ZooKeeper server is taking the snapshot, updates are occurring to the +data tree. The suffix of the *snapshot* file names +is the _zxid_, the ZooKeeper transaction id, of the +last committed transaction at the start of the snapshot. Thus, the +snapshot includes a subset of the updates to the data tree that +occurred while the snapshot was in process. The snapshot, then, may +not correspond to any data tree that actually existed, and for this +reason we refer to it as a fuzzy snapshot. Still, ZooKeeper can +recover using this snapshot because it takes advantage of the +idempotent nature of its updates. By replaying the transaction log +against fuzzy snapshots ZooKeeper gets the state of the system at the +end of the log. + + + +#### The Log Directory + +The Log Directory contains the ZooKeeper transaction logs. +Before any update takes place, ZooKeeper ensures that the transaction +that represents the update is written to non-volatile storage. A new +log file is started when the number of transactions written to the +current log file reaches a (variable) threshold. The threshold is +computed using the same parameter which influences the frequency of +snapshotting (see snapCount and snapSizeLimitInKb above). The log file's +suffix is the first zxid written to that log. + + + +#### File Management + +The format of snapshot and log files does not change between +standalone ZooKeeper servers and different configurations of +replicated ZooKeeper servers. Therefore, you can pull these files from +a running replicated ZooKeeper server to a development machine with a +stand-alone ZooKeeper server for troubleshooting. + +Using older log and snapshot files, you can look at the previous +state of ZooKeeper servers and even restore that state. The +LogFormatter class allows an administrator to look at the transactions +in a log. + +The ZooKeeper server creates snapshot and log files, but +never deletes them. The retention policy of the data and log +files is implemented outside of the ZooKeeper server. The +server itself only needs the latest complete fuzzy snapshot, all log +files following it, and the last log file preceding it. The latter +requirement is necessary to include updates which happened after this +snapshot was started but went into the existing log file at that time. +This is possible because snapshotting and rolling over of logs +proceed somewhat independently in ZooKeeper. See the +[maintenance](#sc_maintenance) section in +this document for more details on setting a retention policy +and maintenance of ZooKeeper storage. + +###### Note +>The data stored in these files is not encrypted. In the case of +storing sensitive data in ZooKeeper, necessary measures need to be +taken to prevent unauthorized access. Such measures are external to +ZooKeeper (e.g., control access to the files) and depend on the +individual settings in which it is being deployed. + + + +#### Recovery - TxnLogToolkit +More details can be found in [this](http://zookeeper.apache.org/doc/current/zookeeperTools.html#zkTxnLogToolkit) + + + +### Things to Avoid + +Here are some common problems you can avoid by configuring +ZooKeeper correctly: + +* *inconsistent lists of servers* : + The list of ZooKeeper servers used by the clients must match + the list of ZooKeeper servers that each ZooKeeper server has. + Things work okay if the client list is a subset of the real list, + but things will really act strange if clients have a list of + ZooKeeper servers that are in different ZooKeeper clusters. Also, + the server lists in each Zookeeper server configuration file + should be consistent with one another. + +* *incorrect placement of transaction log* : + The most performance critical part of ZooKeeper is the + transaction log. ZooKeeper syncs transactions to media before it + returns a response. A dedicated transaction log device is key to + consistent good performance. Putting the log on a busy device will + adversely affect performance. If you only have one storage device, + increase the snapCount so that snapshot files are generated less often; + it does not eliminate the problem, but it makes more resources available + for the transaction log. + +* *incorrect Java heap size* : + You should take special care to set your Java max heap size + correctly. In particular, you should not create a situation in + which ZooKeeper swaps to disk. The disk is death to ZooKeeper. + Everything is ordered, so if processing one request swaps the + disk, all other queued requests will probably do the same. the + disk. DON'T SWAP. + Be conservative in your estimates: if you have 4G of RAM, do + not set the Java max heap size to 6G or even 4G. For example, it + is more likely you would use a 3G heap for a 4G machine, as the + operating system and the cache also need memory. The best and only + recommend practice for estimating the heap size your system needs + is to run load tests, and then make sure you are well below the + usage limit that would cause the system to swap. + +* *Publicly accessible deployment* : + A ZooKeeper ensemble is expected to operate in a trusted computing environment. + It is thus recommended to deploy ZooKeeper behind a firewall. + + + +### Best Practices + +For best results, take note of the following list of good +Zookeeper practices: + +For multi-tenant installations see the [section](zookeeperProgrammers.html#ch_zkSessions) +detailing ZooKeeper "chroot" support, this can be very useful +when deploying many applications/services interfacing to a +single ZooKeeper cluster. diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperAuditLogs.md b/zookeeper-docs/src/main/resources/markdown/zookeeperAuditLogs.md new file mode 100644 index 0000000..957980d --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperAuditLogs.md @@ -0,0 +1,129 @@ + + +# ZooKeeper Audit Logging + +* [ZooKeeper Audit Logs](#ch_auditLogs) +* [ZooKeeper Audit Log Configuration](#ch_reconfig_format) +* [Who is taken as user in audit logs?](#ch_zkAuditUser) + + +## ZooKeeper Audit Logs + +Apache ZooKeeper supports audit logs from version 3.6.0. By default audit logs are disabled. To enable audit logs + configure audit.enable=true in conf/zoo.cfg. Audit logs are not logged on all the ZooKeeper servers, but logged only on the servers where client is connected as depicted in below figure. + +![Audit Logs](images/zkAuditLogs.jpg) + + +The audit log captures detailed information for the operations that are selected to be audited. The audit information is written as a set of key=value pairs for the following keys + +| Key | Value | +| ----- | ----- | +|session | client session id | +|user | comma separated list of users who are associate with a client session. For more on this, see [Who is taken as user in audit logs](#ch_zkAuditUser). +|ip | client IP address +|operation | any one of the selected operations for audit. Possible values are(serverStart, serverStop, create, delete, setData, setAcl, multiOperation, reconfig, ephemeralZNodeDeleteOnSessionClose) +|znode | path of the znode +|znode type | type of znode in case of creation operation +|acl | String representation of znode ACL like cdrwa(create, delete,read, write, admin). This is logged only for setAcl operation +|result | result of the operation. Possible values are (success/failure/invoked). Result "invoked" is used for serverStop operation because stop is logged before ensuring that server actually stopped. + +Below are sample audit logs for all operations, where client is connected from 192.168.1.2, client principal is zkcli@HADOOP.COM, server principal is zookeeper/192.168.1.3@HADOOP.COM + + user=zookeeper/192.168.1.3 operation=serverStart result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=create znode=/a znode_type=persistent result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=create znode=/a znode_type=persistent result=failure + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=setData znode=/a result=failure + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=setData znode=/a result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=setAcl znode=/a acl=world:anyone:cdrwa result=failure + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=setAcl znode=/a acl=world:anyone:cdrwa result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=create znode=/b znode_type=persistent result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=setData znode=/b result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=delete znode=/b result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=multiOperation result=failure + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=delete znode=/a result=failure + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=delete znode=/a result=success + session=0x19344730001 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=create znode=/ephemral znode_type=ephemral result=success + session=0x19344730001 user=zookeeper/192.168.1.3 operation=ephemeralZNodeDeletionOnSessionCloseOrExpire znode=/ephemral result=success + session=0x19344730000 user=192.168.1.2,zkcli@HADOOP.COM ip=192.168.1.2 operation=reconfig znode=/zookeeper/config result=success + user=zookeeper/192.168.1.3 operation=serverStop result=invoked + + + +## ZooKeeper Audit Log Configuration + +By default audit logs are disabled. To enable audit logs configure audit.enable=true in conf/zoo.cfg. Audit logging is done using log4j. Following is the default log4j configuration for audit logs in conf/log4j.properties + + # + # zk audit logging + # + zookeeper.auditlog.file=zookeeper_audit.log + zookeeper.auditlog.threshold=INFO + audit.logger=INFO, RFAAUDIT + log4j.logger.org.apache.zookeeper.audit.Log4jAuditLogger=${audit.logger} + log4j.additivity.org.apache.zookeeper.audit.Log4jAuditLogger=false + log4j.appender.RFAAUDIT=org.apache.log4j.RollingFileAppender + log4j.appender.RFAAUDIT.File=${zookeeper.log.dir}/${zookeeper.auditlog.file} + log4j.appender.RFAAUDIT.layout=org.apache.log4j.PatternLayout + log4j.appender.RFAAUDIT.layout.ConversionPattern=%d{ISO8601} %p %c{2}: %m%n + log4j.appender.RFAAUDIT.Threshold=${zookeeper.auditlog.threshold} + + # Max log file size of 10MB + log4j.appender.RFAAUDIT.MaxFileSize=10MB + log4j.appender.RFAAUDIT.MaxBackupIndex=10 + +Change above configuration to customize the auditlog file, number of backups, max file size, custom audit logger etc. + + + +## Who is taken as user in audit logs? + +By default there are only four authentication provider: + +* IPAuthenticationProvider +* SASLAuthenticationProvider +* X509AuthenticationProvider +* DigestAuthenticationProvider + +User is decided based on the configured authentication provider: + +* When IPAuthenticationProvider is configured then authenticated IP is taken as user +* When SASLAuthenticationProvider is configured then client principal is taken as user +* When X509AuthenticationProvider is configured then client certificate is taken as user +* When DigestAuthenticationProvider is configured then authenticated user is user + +Custom authentication provider can override org.apache.zookeeper.server.auth.AuthenticationProvider.getUserName(String id) + to provide user name. If authentication provider is not overriding this method then whatever is stored in + org.apache.zookeeper.data.Id.id is taken as user. + Generally only user name is stored in this field but it is up to the custom authentication provider what they store in it. + For audit logging value of org.apache.zookeeper.data.Id.id would be taken as user. + +In ZooKeeper Server not all the operations are done by clients but some operations are done by the server itself. For example when client closes the session, ephemeral znodes are deleted by the Server. These deletion are not done by clients directly but it is done the server itself these are called system operations. For these system operations the user associated with the ZooKeeper server are taken as user while audit logging these operations. For example if in ZooKeeper server principal is zookeeper/hadoop.hadoop.com@HADOOP.COM then this becomes the system user and all the system operations will be logged with this user name. + + user=zookeeper/hadoop.hadoop.com@HADOOP.COM operation=serverStart result=success + + +If there is no user associate with ZooKeeper server then the user who started the ZooKeeper server is taken as the user. For example if server started by root then root is taken as the system user + + user=root operation=serverStart result=success + + +Single client can attach multiple authentication schemes to a session, in this case all authenticated schemes will taken taken as user and will be presented as comma separated list. For example if a client is authenticate with principal zkcli@HADOOP.COM and ip 127.0.0.1 then create znode audit log will be as: + + session=0x10c0bcb0000 user=zkcli@HADOOP.COM,127.0.0.1 ip=127.0.0.1 operation=create znode=/a result=success + + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperCLI.md b/zookeeper-docs/src/main/resources/markdown/zookeeperCLI.md new file mode 100644 index 0000000..53da2c4 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperCLI.md @@ -0,0 +1,536 @@ + + +# ZooKeeper-cli: the ZooKeeper command line interface + +## Pre-requisites +Enter into the ZooKeeper-cli + +```bash +# connect to the localhost with the default port:2181 +bin/zkCli.sh +# connect to the remote host with timeout:3s +bin/zkCli.sh -timeout 3000 -server remoteIP:2181 +# connect to the remote host with -waitforconnection option to wait for connection success before executing commands +bin/zkCli.sh -waitforconnection -timeout 3000 -server remoteIP:2181 +# connect with a custom client configuration properties file +bin/zkCli.sh -client-configuration /path/to/client.properties +``` +## help +Showing helps about ZooKeeper commands + +```bash +[zkshell: 1] help +# a sample one +[zkshell: 2] h +ZooKeeper -server host:port cmd args + addauth scheme auth + close + config [-c] [-w] [-s] + connect host:port + create [-s] [-e] [-c] [-t ttl] path [data] [acl] + delete [-v version] path + deleteall path + delquota [-n|-b] path + get [-s] [-w] path + getAcl [-s] path + getAllChildrenNumber path + getEphemerals path + history + listquota path + ls [-s] [-w] [-R] path + printwatches on|off + quit + reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] + redo cmdno + removewatches path [-c|-d|-a] [-l] + set [-s] [-v version] path data + setAcl [-s] [-v version] [-R] path acl + setquota -n|-b val path + stat [-w] path + sync path + version +``` + +## addauth +Add a authorized user for ACL + +```bash +[zkshell: 9] getAcl /acl_digest_test + Insufficient permission : /acl_digest_test +[zkshell: 10] addauth digest user1:12345 +[zkshell: 11] getAcl /acl_digest_test + 'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE= + : cdrwa +# add a super user +# Notice:set zookeeper.DigestAuthenticationProvider +# e.g. zookeeper.DigestAuthenticationProvider.superDigest=zookeeper:qW/HnTfCSoQpB5G8LgkwT3IbiFc= +[zkshell: 12] addauth digest zookeeper:admin +``` + +## close +Close this client/session. + +```bash +[zkshell: 0] close + 2019-03-09 06:42:22,178 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@528] - EventThread shut down for session: 0x10007ab7c550006 + 2019-03-09 06:42:22,179 [myid:] - INFO [main:ZooKeeper@1346] - Session: 0x10007ab7c550006 closed +``` + +## config +Showing the config of quorum membership + +```bash +[zkshell: 17] config + server.1=[2001:db8:1:0:0:242:ac11:2]:2888:3888:participant + server.2=[2001:db8:1:0:0:242:ac11:2]:12888:13888:participant + server.3=[2001:db8:1:0:0:242:ac11:2]:22888:23888:participant + version=0 +``` +## connect +Connect a ZooKeeper server. + +```bash +[zkshell: 4] connect + 2019-03-09 06:43:33,179 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@986] - Socket connection established, initiating session, client: /127.0.0.1:35144, server: localhost/127.0.0.1:2181 + 2019-03-09 06:43:33,189 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1421] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x10007ab7c550007, negotiated timeout = 30000 + connect "localhost:2181,localhost:2182,localhost:2183" + +# connect a remote server +[zkshell: 5] connect remoteIP:2181 +``` +## create +Create a znode. + +```bash +# create a persistent_node +[zkshell: 7] create /persistent_node + Created /persistent_node + +# create a ephemeral node +[zkshell: 8] create -e /ephemeral_node mydata + Created /ephemeral_node + +# create the persistent-sequential node +[zkshell: 9] create -s /persistent_sequential_node mydata + Created /persistent_sequential_node0000000176 + +# create the ephemeral-sequential_node +[zkshell: 10] create -s -e /ephemeral_sequential_node mydata + Created /ephemeral_sequential_node0000000174 + +# create a node with the schema +[zkshell: 11] create /zk-node-create-schema mydata digest:user1:+owfoSBn/am19roBPzR1/MfCblE=:crwad + Created /zk-node-create-schema +[zkshell: 12] addauth digest user1:12345 +[zkshell: 13] getAcl /zk-node-create-schema + 'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE= + : cdrwa + +# create the container node.When the last child of a container is deleted,the container becomes to be deleted +[zkshell: 14] create -c /container_node mydata + Created /container_node +[zkshell: 15] create -c /container_node/child_1 mydata + Created /container_node/child_1 +[zkshell: 16] create -c /container_node/child_2 mydata + Created /container_node/child_2 +[zkshell: 17] delete /container_node/child_1 +[zkshell: 18] delete /container_node/child_2 +[zkshell: 19] get /container_node + org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /container_node + +# create the ttl node. +# set zookeeper.extendedTypesEnabled=true +# Otherwise:KeeperErrorCode = Unimplemented for /ttl_node +[zkshell: 20] create -t 3000 /ttl_node mydata + Created /ttl_node +# after 3s later +[zkshell: 21] get /ttl_node + org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /ttl_node +``` +## delete +Delete a node with a specific path + +```bash +[zkshell: 2] delete /config/topics/test +[zkshell: 3] ls /config/topics/test + Node does not exist: /config/topics/test +``` + +## deleteall +Delete all nodes under a specific path + +```bash +zkshell: 1] ls /config + [changes, clients, topics] +[zkshell: 2] deleteall /config +[zkshell: 3] ls /config + Node does not exist: /config +``` + +## delquota +Delete the quota under a path + +```bash +[zkshell: 1] delquota /quota_test +[zkshell: 2] listquota /quota_test + absolute path is /zookeeper/quota/quota_test/zookeeper_limits + quota for /quota_test does not exist. +``` +## get +Get the data of the specific path + +```bash +[zkshell: 10] get /latest_producer_id_block + {"version":1,"broker":0,"block_start":"0","block_end":"999"} + +# -s to show the stat +[zkshell: 11] get -s /latest_producer_id_block + {"version":1,"broker":0,"block_start":"0","block_end":"999"} + cZxid = 0x90000009a + ctime = Sat Jul 28 08:14:09 UTC 2018 + mZxid = 0x9000000a2 + mtime = Sat Jul 28 08:14:12 UTC 2018 + pZxid = 0x90000009a + cversion = 0 + dataVersion = 1 + aclVersion = 0 + ephemeralOwner = 0x0 + dataLength = 60 + numChildren = 0 + +# -w to set a watch on the data change, Notice: turn on the printwatches +[zkshell: 12] get -w /latest_producer_id_block + {"version":1,"broker":0,"block_start":"0","block_end":"999"} +[zkshell: 13] set /latest_producer_id_block mydata + WATCHER:: + WatchedEvent state:SyncConnected type:NodeDataChanged path:/latest_producer_id_block +``` + +## getAcl +Get the ACL permission of one path + +```bash +[zkshell: 4] create /acl_test mydata ip:127.0.0.1:crwda + Created /acl_test +[zkshell: 5] getAcl /acl_test + 'ip,'127.0.0.1 + : cdrwa + [zkshell: 6] getAcl /testwatch + 'world,'anyone + : cdrwa +``` +## getAllChildrenNumber +Get all numbers of children nodes under a specific path + +```bash +[zkshell: 1] getAllChildrenNumber / + 73779 +[zkshell: 2] getAllChildrenNumber /ZooKeeper + 2 +[zkshell: 3] getAllChildrenNumber /ZooKeeper/quota + 0 +``` +## getEphemerals +Get all the ephemeral nodes created by this session + +```bash +[zkshell: 1] create -e /test-get-ephemerals "ephemeral node" + Created /test-get-ephemerals +[zkshell: 2] getEphemerals + [/test-get-ephemerals] +[zkshell: 3] getEphemerals / + [/test-get-ephemerals] +[zkshell: 4] create -e /test-get-ephemerals-1 "ephemeral node" + Created /test-get-ephemerals-1 +[zkshell: 5] getEphemerals /test-get-ephemerals + test-get-ephemerals test-get-ephemerals-1 +[zkshell: 6] getEphemerals /test-get-ephemerals + [/test-get-ephemerals-1, /test-get-ephemerals] +[zkshell: 7] getEphemerals /test-get-ephemerals-1 + [/test-get-ephemerals-1] +``` + +## history +Showing the history about the recent 11 commands that you have executed + +```bash +[zkshell: 7] history + 0 - close + 1 - close + 2 - ls / + 3 - ls / + 4 - connect + 5 - ls / + 6 - ll + 7 - history +``` + +## listquota +Listing the quota of one path + +```bash +[zkshell: 1] listquota /quota_test + absolute path is /zookeeper/quota/quota_test/zookeeper_limits + Output quota for /quota_test count=2,bytes=-1 + Output stat for /quota_test count=4,bytes=0 +``` + +## ls +Listing the child nodes of one path + +```bash +[zkshell: 36] ls /quota_test + [child_1, child_2, child_3] + +# -s to show the stat +[zkshell: 37] ls -s /quota_test + [child_1, child_2, child_3] + cZxid = 0x110000002d + ctime = Thu Mar 07 11:19:07 UTC 2019 + mZxid = 0x110000002d + mtime = Thu Mar 07 11:19:07 UTC 2019 + pZxid = 0x1100000033 + cversion = 3 + dataVersion = 0 + aclVersion = 0 + ephemeralOwner = 0x0 + dataLength = 0 + numChildren = 3 + +# -R to show the child nodes recursely +[zkshell: 38] ls -R /quota_test + /quota_test + /quota_test/child_1 + /quota_test/child_2 + /quota_test/child_3 + +# -w to set a watch on the child change,Notice: turn on the printwatches +[zkshell: 39] ls -w /brokers + [ids, seqid, topics] +[zkshell: 40] delete /brokers/ids + WATCHER:: + WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/brokers +``` + +## printwatches +A switch to turn on/off whether printing watches or not. + +```bash +[zkshell: 0] printwatches + printwatches is on +[zkshell: 1] printwatches off +[zkshell: 2] printwatches + printwatches is off +[zkshell: 3] printwatches on +[zkshell: 4] printwatches + printwatches is on +``` + +## quit +Quit the CLI windows. + +```bash +[zkshell: 1] quit +``` + +## reconfig +Change the membership of the ensemble during the runtime. + +Before using this cli,read the details in the [Dynamic Reconfiguration](zookeeperReconfig.html) about the reconfig feature,especially the "Security" part. + +Pre-requisites: + +1. set reconfigEnabled=true in the zoo.cfg + +2. add a super user or skipAcl,otherwise will get “Insufficient permission”. e.g. addauth digest zookeeper:admin + +```bash +# Change follower 2 to an observer and change its port from 2182 to 12182 +# Add observer 5 to the ensemble +# Remove Observer 4 from the ensemble +[zkshell: 1] reconfig --add 2=localhost:2781:2786:observer;12182 --add 5=localhost:2781:2786:observer;2185 -remove 4 + Committed new configuration: + server.1=localhost:2780:2785:participant;0.0.0.0:2181 + server.2=localhost:2781:2786:observer;0.0.0.0:12182 + server.3=localhost:2782:2787:participant;0.0.0.0:2183 + server.5=localhost:2784:2789:observer;0.0.0.0:2185 + version=1c00000002 + +# -members to appoint the membership +[zkshell: 2] reconfig -members server.1=localhost:2780:2785:participant;0.0.0.0:2181,server.2=localhost:2781:2786:observer;0.0.0.0:12182,server.3=localhost:2782:2787:participant;0.0.0.0:12183 + Committed new configuration: + server.1=localhost:2780:2785:participant;0.0.0.0:2181 + server.2=localhost:2781:2786:observer;0.0.0.0:12182 + server.3=localhost:2782:2787:participant;0.0.0.0:12183 + version=f9fe0000000c + +# Change the current config to the one in the myNewConfig.txt +# But only if current config version is 2100000010 +[zkshell: 3] reconfig -file /data/software/zookeeper/zookeeper-test/conf/myNewConfig.txt -v 2100000010 + Committed new configuration: + server.1=localhost:2780:2785:participant;0.0.0.0:2181 + server.2=localhost:2781:2786:observer;0.0.0.0:12182 + server.3=localhost:2782:2787:participant;0.0.0.0:2183 + server.5=localhost:2784:2789:observer;0.0.0.0:2185 + version=220000000c +``` + +## redo +Redo the cmd with the index from history. + +```bash +[zkshell: 4] history + 0 - ls / + 1 - get /consumers + 2 - get /hbase + 3 - ls /hbase + 4 - history +[zkshell: 5] redo 3 + [backup-masters, draining, flush-table-proc, hbaseid, master-maintenance, meta-region-server, namespace, online-snapshot, replication, rs, running, splitWAL, switch, table, table-lock] +``` + +## removewatches +Remove the watches under a node. + +```bash +[zkshell: 1] get -w /brokers + null +[zkshell: 2] removewatches /brokers + WATCHER:: + WatchedEvent state:SyncConnected type:DataWatchRemoved path:/brokers + +``` + +## set +Set/update the data on a path. + +```bash +[zkshell: 50] set /brokers myNewData + +# -s to show the stat of this node. +[zkshell: 51] set -s /quota_test mydata_for_quota_test + cZxid = 0x110000002d + ctime = Thu Mar 07 11:19:07 UTC 2019 + mZxid = 0x1100000038 + mtime = Thu Mar 07 11:42:41 UTC 2019 + pZxid = 0x1100000033 + cversion = 3 + dataVersion = 2 + aclVersion = 0 + ephemeralOwner = 0x0 + dataLength = 21 + numChildren = 3 + +# -v to set the data with CAS,the version can be found from dataVersion using stat. +[zkshell: 52] set -v 0 /brokers myNewData +[zkshell: 53] set -v 0 /brokers myNewData + version No is not valid : /brokers +``` + +## setAcl +Set the Acl permission for one node. + +```bash +[zkshell: 28] addauth digest user1:12345 +[zkshell: 30] setAcl /acl_auth_test auth:user1:12345:crwad +[zkshell: 31] getAcl /acl_auth_test + 'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE= + : cdrwa + +# -R to set Acl recursely +[zkshell: 32] ls /acl_auth_test + [child_1, child_2] +[zkshell: 33] getAcl /acl_auth_test/child_2 + 'world,'anyone + : cdrwa +[zkshell: 34] setAcl -R /acl_auth_test auth:user1:12345:crwad +[zkshell: 35] getAcl /acl_auth_test/child_2 + 'digest,'user1:+owfoSBn/am19roBPzR1/MfCblE= + : cdrwa + +# -v set Acl with the acl version which can be found from the aclVersion using the stat +[zkshell: 36] stat /acl_auth_test + cZxid = 0xf9fc0000001c + ctime = Tue Mar 26 16:50:58 CST 2019 + mZxid = 0xf9fc0000001c + mtime = Tue Mar 26 16:50:58 CST 2019 + pZxid = 0xf9fc0000001f + cversion = 2 + dataVersion = 0 + aclVersion = 3 + ephemeralOwner = 0x0 + dataLength = 0 + numChildren = 2 +[zkshell: 37] setAcl -v 3 /acl_auth_test auth:user1:12345:crwad +``` + +## setquota +Set the quota in one path. + +```bash +# -n to limit the number of child nodes(included itself) +[zkshell: 18] setquota -n 2 /quota_test +[zkshell: 19] create /quota_test/child_1 + Created /quota_test/child_1 +[zkshell: 20] create /quota_test/child_2 + Created /quota_test/child_2 +[zkshell: 21] create /quota_test/child_3 + Created /quota_test/child_3 +# Notice:don't have a hard constraint,just log the warning info + 2019-03-07 11:22:36,680 [myid:1] - WARN [SyncThread:0:DataTree@374] - Quota exceeded: /quota_test count=3 limit=2 + 2019-03-07 11:22:41,861 [myid:1] - WARN [SyncThread:0:DataTree@374] - Quota exceeded: /quota_test count=4 limit=2 + +# -b to limit the bytes(data length) of one path +[zkshell: 22] setquota -b 5 /brokers +[zkshell: 23] set /brokers "I_love_zookeeper" +# Notice:don't have a hard constraint,just log the warning info + WARN [CommitProcWorkThread-7:DataTree@379] - Quota exceeded: /brokers bytes=4206 limit=5 +``` + +## stat +Showing the stat/metadata of one node. + +```bash +[zkshell: 1] stat /hbase + cZxid = 0x4000013d9 + ctime = Wed Jun 27 20:13:07 CST 2018 + mZxid = 0x4000013d9 + mtime = Wed Jun 27 20:13:07 CST 2018 + pZxid = 0x500000001 + cversion = 17 + dataVersion = 0 + aclVersion = 0 + ephemeralOwner = 0x0 + dataLength = 0 + numChildren = 15 +``` + +## sync +Sync the data of one node between leader and followers(Asynchronous sync) + +```bash +[zkshell: 14] sync / +[zkshell: 15] Sync is OK +``` + +## version +Show the version of the ZooKeeper client/CLI + +```bash +[zkshell: 1] version +ZooKeeper CLI version: 3.6.0-SNAPSHOT-29f9b2c1c0e832081f94d59a6b88709c5f1bb3ca, built on 05/30/2019 09:26 GMT +``` diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperHierarchicalQuorums.md b/zookeeper-docs/src/main/resources/markdown/zookeeperHierarchicalQuorums.md new file mode 100644 index 0000000..e11f34f --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperHierarchicalQuorums.md @@ -0,0 +1,47 @@ + + +# Introduction to hierarchical quorums + +This document gives an example of how to use hierarchical quorums. The basic idea is +very simple. First, we split servers into groups, and add a line for each group listing +the servers that form this group. Next we have to assign a weight to each server. + +The following example shows how to configure a system with three groups of three servers +each, and we assign a weight of 1 to each server: + + + group.1=1:2:3 + group.2=4:5:6 + group.3=7:8:9 + + weight.1=1 + weight.2=1 + weight.3=1 + weight.4=1 + weight.5=1 + weight.6=1 + weight.7=1 + weight.8=1 + weight.9=1 + + +When running the system, we are able to form a quorum once we have a majority of votes from +a majority of non-zero-weight groups. Groups that have zero weight are discarded and not +considered when forming quorums. Looking at the example, we are able to form a quorum once +we have votes from at least two servers from each of two different groups. + + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperInternals.md b/zookeeper-docs/src/main/resources/markdown/zookeeperInternals.md new file mode 100644 index 0000000..0505807 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperInternals.md @@ -0,0 +1,381 @@ + + +# ZooKeeper Internals + +* [Introduction](#ch_Introduction) +* [Atomic Broadcast](#sc_atomicBroadcast) + * [Guarantees, Properties, and Definitions](#sc_guaranteesPropertiesDefinitions) + * [Leader Activation](#sc_leaderElection) + * [Active Messaging](#sc_activeMessaging) + * [Summary](#sc_summary) + * [Comparisons](#sc_comparisons) +* [Consistency Guarantees](#sc_consistency) +* [Quorums](#sc_quorum) +* [Logging](#sc_logging) + * [Developer Guidelines](#sc_developerGuidelines) + * [Logging at the Right Level](#sc_rightLevel) + * [Use of Standard slf4j Idioms](#sc_slf4jIdioms) + + + +## Introduction + +This document contains information on the inner workings of ZooKeeper. +It discusses the following topics: + +* [Atomic Broadcast](#sc_atomicBroadcast) +* [Consistency Guarantees](#sc_consistency) +* [Quorums](#sc_quorum) +* [Logging](#sc_logging) + + + +## Atomic Broadcast + +At the heart of ZooKeeper is an atomic messaging system that keeps all of the servers in sync. + + + +### Guarantees, Properties, and Definitions + +The specific guarantees provided by the messaging system used by ZooKeeper are the following: + +* *_Reliable delivery_* : + If a message `m`, is delivered + by one server, message `m` will be eventually delivered by all servers. + +* *_Total order_* : + If a message `a` is + delivered before message `b` by one server, message `a` will be delivered before `b` by all + servers. + +* *_Causal order_* : + If a message `b` is sent after a message `a` has been delivered by the sender of `b`, + message `a` must be ordered before `b`. If a sender sends `c` after sending `b`, `c` must be ordered after `b`. + +The ZooKeeper messaging system also needs to be efficient, reliable, and easy to +implement and maintain. We make heavy use of messaging, so we need the system to +be able to handle thousands of requests per second. Although we can require at +least k+1 correct servers to send new messages, we must be able to recover from +correlated failures such as power outages. When we implemented the system we had +little time and few engineering resources, so we needed a protocol that is +accessible to engineers and is easy to implement. We found that our protocol +satisfied all of these goals. + +Our protocol assumes that we can construct point-to-point FIFO channels between +the servers. While similar services usually assume message delivery that can +lose or reorder messages, our assumption of FIFO channels is very practical +given that we use TCP for communication. Specifically we rely on the following property of TCP: + +* *_Ordered delivery_* : + Data is delivered in the same order it is sent and a message `m` is + delivered only after all messages sent before `m` have been delivered. + (The corollary to this is that if message `m` is lost all messages after `m` will be lost.) + +* *_No message after close_* : + Once a FIFO channel is closed, no messages will be received from it. + +FLP proved that consensus cannot be achieved in asynchronous distributed systems +if failures are possible. To ensure that we achieve consensus in the presence of failures +we use timeouts. However, we rely on time for liveness not for correctness. So, +if timeouts stop working (e.g., skewed clocks) the messaging system may +hang, but it will not violate its guarantees. + +When describing the ZooKeeper messaging protocol we will talk of packets, +proposals, and messages: + +* *_Packet_* : + a sequence of bytes sent through a FIFO channel. + +* *_Proposal_* : + a unit of agreement. Proposals are agreed upon by exchanging packets + with a quorum of ZooKeeper servers. Most proposals contain messages, however the + NEW_LEADER proposal is an example of a proposal that does not contain to a message. + +* *_Message_* : + a sequence of bytes to be atomically broadcast to all ZooKeeper + servers. A message put into a proposal and agreed upon before it is delivered. + +As stated above, ZooKeeper guarantees a total order of messages, and it also +guarantees a total order of proposals. ZooKeeper exposes the total ordering using +a ZooKeeper transaction id (_zxid_). All proposals will be stamped with a zxid when +it is proposed and exactly reflects the total ordering. Proposals are sent to all +ZooKeeper servers and committed when a quorum of them acknowledge the proposal. +If a proposal contains a message, the message will be delivered when the proposal +is committed. Acknowledgement means the server has recorded the proposal to persistent storage. +Our quorums have the requirement that any pair of quorum must have at least one server +in common. We ensure this by requiring that all quorums have size (_n/2+1_) where +n is the number of servers that make up a ZooKeeper service. + +The zxid has two parts: the epoch and a counter. In our implementation the zxid +is a 64-bit number. We use the high order 32-bits for the epoch and the low order +32-bits for the counter. Because zxid consists of two parts, zxid can be represented both as a +number and as a pair of integers, (_epoch, count_). The epoch number represents a +change in leadership. Each time a new leader comes into power it will have its +own epoch number. We have a simple algorithm to assign a unique zxid to a proposal: +the leader simply increments the zxid to obtain a unique zxid for each proposal. _Leadership activation will ensure that only one leader uses a given epoch, so our +simple algorithm guarantees that every proposal will have a unique id._ + +ZooKeeper messaging consists of two phases: + +* *_Leader activation_* : + In this phase a leader establishes the correct state of the system + and gets ready to start making proposals. + +* *_Active messaging_* : + In this phase a leader accepts messages to propose and coordinates message delivery. + +ZooKeeper is a holistic protocol. We do not focus on individual proposals, rather +look at the stream of proposals as a whole. Our strict ordering allows us to do this +efficiently and greatly simplifies our protocol. Leadership activation embodies +this holistic concept. A leader becomes active only when a quorum of followers +(The leader counts as a follower as well. You can always vote for yourself ) has synced +up with the leader, they have the same state. This state consists of all of the +proposals that the leader believes have been committed and the proposal to follow +the leader, the NEW_LEADER proposal. (Hopefully you are thinking to +yourself, _Does the set of proposals that the leader believes has been committed +include all the proposals that really have been committed?_ The answer is _yes_. +Below, we make clear why.) + + + +### Leader Activation + +Leader activation includes leader election (`FastLeaderElection`). +ZooKeeper messaging doesn't care about the exact method of electing a leader as long as the following holds: + +* The leader has seen the highest zxid of all the followers. +* A quorum of servers have committed to following the leader. + +Of these two requirements only the first, the highest zxid among the followers +needs to hold for correct operation. The second requirement, a quorum of followers, +just needs to hold with high probability. We are going to recheck the second requirement, +so if a failure happens during or after the leader election and quorum is lost, +we will recover by abandoning leader activation and running another election. + +After leader election a single server will be designated as a leader and start +waiting for followers to connect. The rest of the servers will try to connect to +the leader. The leader will sync up with the followers by sending any proposals they +are missing, or if a follower is missing too many proposals, it will send a full +snapshot of the state to the follower. + +There is a corner case in which a follower that has proposals, `U`, not seen +by a leader arrives. Proposals are seen in order, so the proposals of `U` will have a zxids +higher than zxids seen by the leader. The follower must have arrived after the +leader election, otherwise the follower would have been elected leader given that +it has seen a higher zxid. Since committed proposals must be seen by a quorum of +servers, and a quorum of servers that elected the leader did not see `U`, the proposals +of `U` have not been committed, so they can be discarded. When the follower connects +to the leader, the leader will tell the follower to discard `U`. + +A new leader establishes a zxid to start using for new proposals by getting the +epoch, e, of the highest zxid it has seen and setting the next zxid to use to be +(e+1, 0), after the leader syncs with a follower, it will propose a NEW_LEADER +proposal. Once the NEW_LEADER proposal has been committed, the leader will activate +and start receiving and issuing proposals. + +It all sounds complicated but here are the basic rules of operation during leader +activation: + +* A follower will ACK the NEW_LEADER proposal after it has synced with the leader. +* A follower will only ACK a NEW_LEADER proposal with a given zxid from a single server. +* A new leader will COMMIT the NEW_LEADER proposal when a quorum of followers has ACKed it. +* A follower will commit any state it received from the leader when the NEW_LEADER proposal is COMMIT. +* A new leader will not accept new proposals until the NEW_LEADER proposal has been COMMITTED. + +If leader election terminates erroneously, we don't have a problem since the +NEW_LEADER proposal will not be committed since the leader will not have quorum. +When this happens, the leader and any remaining followers will timeout and go back +to leader election. + + + +### Active Messaging + +Leader Activation does all the heavy lifting. Once the leader is coronated he can +start blasting out proposals. As long as he remains the leader no other leader can +emerge since no other leader will be able to get a quorum of followers. If a new +leader does emerge, +it means that the leader has lost quorum, and the new leader will clean up any +mess left over during her leadership activation. + +ZooKeeper messaging operates similar to a classic two-phase commit. + +![Two phase commit](images/2pc.jpg) + +All communication channels are FIFO, so everything is done in order. Specifically +the following operating constraints are observed: + +* The leader sends proposals to all followers using + the same order. Moreover, this order follows the order in which requests have been + received. Because we use FIFO channels this means that followers also receive proposals in order. +* Followers process messages in the order they are received. This + means that messages will be ACKed in order and the leader will receive ACKs from + followers in order, due to the FIFO channels. It also means that if message `m` + has been written to non-volatile storage, all messages that were proposed before + `m` have been written to non-volatile storage. +* The leader will issue a COMMIT to all followers as soon as a + quorum of followers have ACKed a message. Since messages are ACKed in order, + COMMITs will be sent by the leader as received by the followers in order. +* COMMITs are processed in order. Followers deliver a proposal + message when that proposal is committed. + + + +### Summary + +So there you go. Why does it work? Specifically, why does a set of proposals +believed by a new leader always contain any proposal that has actually been committed? +First, all proposals have a unique zxid, so unlike other protocols, we never have +to worry about two different values being proposed for the same zxid; followers +(a leader is also a follower) see and record proposals in order; proposals are +committed in order; there is only one active leader at a time since followers only +follow a single leader at a time; a new leader has seen all committed proposals +from the previous epoch since it has seen the highest zxid from a quorum of servers; +any uncommitted proposals from a previous epoch seen by a new leader will be committed +by that leader before it becomes active. + + + +### Comparisons + +Isn't this just Multi-Paxos? No, Multi-Paxos requires some way of assuring that +there is only a single coordinator. We do not count on such assurances. Instead +we use the leader activation to recover from leadership change or old leaders +believing they are still active. + +Isn't this just Paxos? Your active messaging phase looks just like phase 2 of Paxos? +Actually, to us active messaging looks just like 2 phase commit without the need to +handle aborts. Active messaging is different from both in the sense that it has +cross proposal ordering requirements. If we do not maintain strict FIFO ordering of +all packets, it all falls apart. Also, our leader activation phase is different from +both of them. In particular, our use of epochs allows us to skip blocks of uncommitted +proposals and to not worry about duplicate proposals for a given zxid. + + + + +## Consistency Guarantees + +The [consistency](https://jepsen.io/consistency) guarantees of ZooKeeper lie between sequential consistency and linearizability. In this section, we explain the exact consistency guarantees that ZooKeeper provides. + +Write operations in ZooKeeper are *linearizable*. In other words, each `write` will appear to take effect atomically at some point between when the client issues the request and receives the corresponding response. This means that the writes performed by all the clients in ZooKeeper can be totally ordered in such a way that respects the real-time ordering of these writes. However, merely stating that write operations are linearizable is meaningless unless we also talk about read operations. + +Read operations in ZooKeeper are *not linearizable* since they can return potentially stale data. This is because a `read` in ZooKeeper is not a quorum operation and a server will respond immediately to a client that is performing a `read`. ZooKeeper does this because it prioritizes performance over consistency for the read use case. However, reads in ZooKeeper are *sequentially consistent*, because `read` operations will appear to take effect in some sequential order that furthermore respects the order of each client's operations. A common pattern to work around this is to issue a `sync` before issuing a `read`. This too does **not** strictly guarantee up-to-date data because `sync` is [not currently a quorum operation](https://issues.apache.org/jira/browse/ZOOKEEPER-1675). To illustrate, consider a scenario where two servers simultaneously think they are the leader, something that could occur if the TCP connection timeout is smaller than `syncLimit * tickTime`. Note that this is [unlikely](https://www.amazon.com/ZooKeeper-Distributed-Coordination-Flavio-Junqueira/dp/1449361307) to occur in practice, but should be kept in mind nevertheless when discussing strict theoretical guarantees. Under this scenario, it is possible that the `sync` is served by the “leader” with stale data, thereby allowing the following `read` to be stale as well. The stronger guarantee of linearizability is provided if an actual quorum operation (e.g., a `write`) is performed before a `read`. + +Overall, the consistency guarantees of ZooKeeper are formally captured by the notion of [ordered sequential consistency](http://webee.technion.ac.il/people/idish/ftp/OSC-IPL17.pdf) or `OSC(U)` to be exact, which lies between sequential consistency and linearizability. + + + +## Quorums + +Atomic broadcast and leader election use the notion of quorum to guarantee a consistent +view of the system. By default, ZooKeeper uses majority quorums, which means that every +voting that happens in one of these protocols requires a majority to vote on. One example is +acknowledging a leader proposal: the leader can only commit once it receives an +acknowledgement from a quorum of servers. + +If we extract the properties that we really need from our use of majorities, we have that we only +need to guarantee that groups of processes used to validate an operation by voting (e.g., acknowledging +a leader proposal) pairwise intersect in at least one server. Using majorities guarantees such a property. +However, there are other ways of constructing quorums different from majorities. For example, we can assign +weights to the votes of servers, and say that the votes of some servers are more important. To obtain a quorum, +we get enough votes so that the sum of weights of all votes is larger than half of the total sum of all weights. + +A different construction that uses weights and is useful in wide-area deployments (co-locations) is a hierarchical +one. With this construction, we split the servers into disjoint groups and assign weights to processes. To form +a quorum, we have to get a hold of enough servers from a majority of groups G, such that for each group g in G, +the sum of votes from g is larger than half of the sum of weights in g. Interestingly, this construction enables +smaller quorums. If we have, for example, 9 servers, we split them into 3 groups, and assign a weight of 1 to each +server, then we are able to form quorums of size 4. Note that two subsets of processes composed each of a majority +of servers from each of a majority of groups necessarily have a non-empty intersection. It is reasonable to expect +that a majority of co-locations will have a majority of servers available with high probability. + +With ZooKeeper, we provide a user with the ability of configuring servers to use majority quorums, weights, or a +hierarchy of groups. + + + +## Logging + +Zookeeper uses [slf4j](http://www.slf4j.org/index.html) as an abstraction layer for logging. [log4j](http://logging.apache.org/log4j) in version 1.2 is chosen as the final logging implementation for now. +For better embedding support, it is planned in the future to leave the decision of choosing the final logging implementation to the end user. +Therefore, always use the slf4j api to write log statements in the code, but configure log4j for how to log at runtime. +Note that slf4j has no FATAL level, former messages at FATAL level have been moved to ERROR level. +For information on configuring log4j for +ZooKeeper, see the [Logging](zookeeperAdmin.html#sc_logging) section +of the [ZooKeeper Administrator's Guide.](zookeeperAdmin.html) + + + +### Developer Guidelines + +Please follow the [slf4j manual](http://www.slf4j.org/manual.html) when creating log statements within code. +Also read the [FAQ on performance](http://www.slf4j.org/faq.html#logging\_performance), when creating log statements. Patch reviewers will look for the following: + + + +#### Logging at the Right Level + +There are several levels of logging in slf4j. + +It's important to pick the right one. In order of higher to lower severity: + +1. ERROR level designates error events that might still allow the application to continue running. +1. WARN level designates potentially harmful situations. +1. INFO level designates informational messages that highlight the progress of the application at coarse-grained level. +1. DEBUG Level designates fine-grained informational events that are most useful to debug an application. +1. TRACE Level designates finer-grained informational events than the DEBUG. + +ZooKeeper is typically run in production such that log messages of INFO level +severity and higher (more severe) are output to the log. + + + +#### Use of Standard slf4j Idioms + +_Static Message Logging_ + + LOG.debug("process completed successfully!"); + +However when creating parameterized messages are required, use formatting anchors. + + LOG.debug("got {} messages in {} minutes",new Object[]{count,time}); + +_Naming_ + +Loggers should be named after the class in which they are used. + + public class Foo { + private static final Logger LOG = LoggerFactory.getLogger(Foo.class); + .... + public Foo() { + LOG.info("constructing Foo"); + +_Exception handling_ + + try { + // code + } catch (XYZException e) { + // do this + LOG.error("Something bad happened", e); + // don't do this (generally) + // LOG.error(e); + // why? because "don't do" case hides the stack trace + + // continue process here as you need... recover or (re)throw + } diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperJMX.md b/zookeeper-docs/src/main/resources/markdown/zookeeperJMX.md new file mode 100644 index 0000000..368540f --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperJMX.md @@ -0,0 +1,121 @@ + + +# ZooKeeper JMX + +* [JMX](#ch_jmx) +* [Starting ZooKeeper with JMX enabled](#ch_starting) +* [Run a JMX console](#ch_console) +* [ZooKeeper MBean Reference](#ch_reference) + + + +## JMX + +Apache ZooKeeper has extensive support for JMX, allowing you +to view and manage a ZooKeeper serving ensemble. + +This document assumes that you have basic knowledge of +JMX. See [Sun JMX Technology](http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/) page to get started with JMX. + +See the [JMX Management Guide](http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html) for details on setting up local and +remote management of VM instances. By default the included +_zkServer.sh_ supports only local management - +review the linked document to enable support for remote management +(beyond the scope of this document). + + + +## Starting ZooKeeper with JMX enabled + +The class +_org.apache.zookeeper.server.quorum.QuorumPeerMain_ +will start a JMX manageable ZooKeeper server. This class +registers the proper MBeans during initialization to support JMX +monitoring and management of the +instance. See _bin/zkServer.sh_ for one +example of starting ZooKeeper using QuorumPeerMain. + + + +## Run a JMX console + +There are a number of JMX consoles available which can connect +to the running server. For this example we will use Sun's +_jconsole_. + +The Java JDK ships with a simple JMX console +named [jconsole](http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html) +which can be used to connect to ZooKeeper and inspect a running +server. Once you've started ZooKeeper using QuorumPeerMain +start _jconsole_, which typically resides in +_JDK_HOME/bin/jconsole_ + +When the "new connection" window is displayed either connect +to local process (if jconsole started on the same host as Server) or +use the remote process connection. + +By default the "overview" tab for the VM is displayed (this +is a great way to get insight into the VM btw). Select +the "MBeans" tab. + +You should now see _org.apache.ZooKeeperService_ +on the left hand side. Expand this item and depending on how you've +started the server you will be able to monitor and manage various +service related features. + +Also note that ZooKeeper will register log4j MBeans as +well. In the same section along the left hand side you will see +"log4j". Expand that to manage log4j through JMX. Of particular +interest is the ability to dynamically change the logging levels +used by editing the appender and root thresholds. Log4j MBean +registration can be disabled by passing +_-Dzookeeper.jmx.log4j.disable=true_ to the JVM +when starting ZooKeeper. In addition, we can specify the name of +the MBean with the _-Dzookeeper.jmx.log4j.mbean=log4j:hierarchy=default_ +option, in case we need to upgrade an integrated system +using the old MBean name (`log4j:hierarchy = default`). + + + +## ZooKeeper MBean Reference + +This table details JMX for a server participating in a +replicated ZooKeeper ensemble (ie not standalone). This is the +typical case for a production environment. + +### MBeans, their names and description + +| MBean | MBean Object Name | Description | +|-----------|-------------------|-------------------------------------------------| +| Quorum | ReplicatedServer_id<#> | Represents the Quorum, or Ensemble - parent of all cluster members. Note that the object name includes the "myid" of the server (name suffix) that your JMX agent has connected to. | +| LocalPeer/RemotePeer | replica.<#> | Represents a local or remote peer (ie server participating in the ensemble). Note that the object name includes the "myid" of the server (name suffix). | +| LeaderElection | LeaderElection | Represents a ZooKeeper cluster leader election which is in progress. Provides information about the election, such as when it started. | +| Leader | Leader | Indicates that the parent replica is the leader and provides attributes/operations for that server. Note that Leader is a subclass of ZooKeeperServer, so it provides all of the information normally associated with a ZooKeeperServer node. | +| Follower | Follower | Indicates that the parent replica is a follower and provides attributes/operations for that server. Note that Follower is a subclass of ZooKeeperServer, so it provides all of the information normally associated with a ZooKeeperServer node. | +| DataTree | InMemoryDataTree | Statistics on the in memory znode database, also operations to access finer (and more computationally intensive) statistics on the data (such as ephemeral count). InMemoryDataTrees are children of ZooKeeperServer nodes. | +| ServerCnxn | | Statistics on each client connection, also operations on those connections (such as termination). Note the object name is the session id of the connection in hex form. | + +This table details JMX for a standalone server. Typically +standalone is only used in development situations. + +### MBeans, their names and description + +| MBean | MBean Object Name | Description | +|-------|-------------------|------------------------| +| ZooKeeperServer | StandaloneServer_port<#> | Statistics on the running server, also operations to reset these attributes. Note that the object name includes the client port of the server (name suffix). | +| DataTree | InMemoryDataTree | Statistics on the in memory znode database, also operations to access finer (and more computationally intensive) statistics on the data (such as ephemeral count). | +| ServerCnxn | < session_id > | Statistics on each client connection, also operations on those connections (such as termination). Note the object name is the session id of the connection in hex form. | diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperMonitor.md b/zookeeper-docs/src/main/resources/markdown/zookeeperMonitor.md new file mode 100644 index 0000000..3265aef --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperMonitor.md @@ -0,0 +1,95 @@ + + +# ZooKeeper Monitor Guide + +* [New Metrics System](#Metrics-System) + * [Metrics](#Metrics) + * [Prometheus](#Prometheus) + * [Grafana](#Grafana) + +* [JMX](#JMX) + +* [Four letter words](#four-letter-words) + + + +## New Metrics System +The feature:`New Metrics System` has been available since 3.6.0 which provides the abundant metrics +to help users monitor the ZooKeeper on the topic: znode, network, disk, quorum, leader election, +client, security, failures, watch/session, requestProcessor, and so forth. + + + +### Metrics +All the metrics are included in the `ServerMetrics.java`. + + + +### Prometheus +- Running a [Prometheus](https://prometheus.io/) monitoring service is the easiest way to ingest and record ZooKeeper's metrics. +- Pre-requisites: + - enable the `Prometheus MetricsProvider` by setting `metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider` in the zoo.cfg. + - the Port is also configurable by setting `metricsProvider.httpPort`(the default value:7000) +- Install Prometheus: + Go to the official website download [page](https://prometheus.io/download/), download the latest release. + +- Set Prometheus's scraper to target the ZooKeeper cluster endpoints: + + ```bash + cat > /tmp/test-zk.yaml <> /tmp/test-zk.log 2>&1 & + ``` + +- Now Prometheus will scrape zk metrics every 10 seconds. + + + +### Grafana +- Grafana has built-in Prometheus support; just add a Prometheus data source: + + ```bash + Name: test-zk + Type: Prometheus + Url: http://localhost:9090 + Access: proxy + ``` +- Then download and import the default ZooKeeper dashboard [template](https://grafana.com/dashboards/10465) and customize. +- Users can ask for Grafana dashboard account if having any good improvements by writing a email to **dev@zookeeper.apache.org**. + + +## JMX +More details can be found in [here](http://zookeeper.apache.org/doc/current/zookeeperJMX.html) + + +## Four letter words +More details can be found in [here](http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_zkCommands) diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperObservers.md b/zookeeper-docs/src/main/resources/markdown/zookeeperObservers.md new file mode 100644 index 0000000..a41e703 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperObservers.md @@ -0,0 +1,138 @@ + + +# ZooKeeper Observers + +* [Observers: Scaling ZooKeeper Without Hurting Write Performance](#ch_Introduction) +* [How to use Observers](#sc_UsingObservers) +* [Example use cases](#ch_UseCases) + + + +## Observers: Scaling ZooKeeper Without Hurting Write Performance + +Although ZooKeeper performs very well by having clients connect directly +to voting members of the ensemble, this architecture makes it hard to +scale out to huge numbers of clients. The problem is that as we add more +voting members, the write performance drops. This is due to the fact that +a write operation requires the agreement of (in general) at least half the +nodes in an ensemble and therefore the cost of a vote can increase +significantly as more voters are added. + +We have introduced a new type of ZooKeeper node called +an _Observer_ which helps address this problem and +further improves ZooKeeper's scalability. Observers are non-voting members +of an ensemble which only hear the results of votes, not the agreement +protocol that leads up to them. Other than this simple distinction, +Observers function exactly the same as Followers - clients may connect to +them and send read and write requests to them. Observers forward these +requests to the Leader like Followers do, but they then simply wait to +hear the result of the vote. Because of this, we can increase the number +of Observers as much as we like without harming the performance of votes. + +Observers have other advantages. Because they do not vote, they are not a +critical part of the ZooKeeper ensemble. Therefore they can fail, or be +disconnected from the cluster, without harming the availability of the +ZooKeeper service. The benefit to the user is that Observers may connect +over less reliable network links than Followers. In fact, Observers may be +used to talk to a ZooKeeper server from another data center. Clients of +the Observer will see fast reads, as all reads are served locally, and +writes result in minimal network traffic as the number of messages +required in the absence of the vote protocol is smaller. + + + +## How to use Observers + +Setting up a ZooKeeper ensemble that uses Observers is very simple, +and requires just two changes to your config files. Firstly, in the config +file of every node that is to be an Observer, you must place this line: + + peerType=observer + +This line tells ZooKeeper that the server is to be an Observer. Secondly, +in every server config file, you must add :observer to the server +definition line of each Observer. For example: + + server.1:localhost:2181:3181:observer + +This tells every other server that server.1 is an Observer, and that they +should not expect it to vote. This is all the configuration you need to do +to add an Observer to your ZooKeeper cluster. Now you can connect to it as +though it were an ordinary Follower. Try it out, by running: + + $ bin/zkCli.sh -server localhost:2181 + +where localhost:2181 is the hostname and port number of the Observer as +specified in every config file. You should see a command line prompt +through which you can issue commands like _ls_ to query +the ZooKeeper service. + + + +## How to use Observer Masters + +Observers function simple as non-voting members of the ensemble, sharing +the Learner interface with Followers and holding only a slightly different +internal pipeline. Both maintain connections along the quorum port with the +Leader by which they learn of all new proposals on the ensemble. + +By default, Observers connect to the Leader of the quorum along its +quorum port and this is how they learn of all new proposals on the +ensemble. There are benefits to allowing Observers to connect to the +Followers instead as a means of plugging into the commit stream in place +of connecting to the Leader. It shifts the burden of supporting Observers +off the Leader and allow it to focus on coordinating the commit of writes. +This means better performance when the Leader is under high load, +particularly high network load such as can happen after a leader election +when many Learners need to sync. It reduces the total network connections +maintained on the Leader when there are a high number of observers. +Activating Followers to support Observers allow the overall number of +Observers to scale into the hundreds. On the other end, Observer +availability is improved since it will take shorter time for a high +number of Observers to finish syncing and start serving client traffic. + +This feature can be activated by letting all members of the ensemble know +which port will be used by the Followers to listen for Observer +connections. The following entry, when added to the server config file, +will instruct Observers to connect to peers (Leaders and Followers) on +port 2191 and instruct Followers to create an ObserverMaster thread to +listen and serve on that port. + + observerMasterPort=2191 + + +## Example use cases + +Two example use cases for Observers are listed below. In fact, wherever +you wish to scale the number of clients of your ZooKeeper ensemble, or +where you wish to insulate the critical part of an ensemble from the load +of dealing with client requests, Observers are a good architectural +choice. + +* As a datacenter bridge: Forming a ZK ensemble between two + datacenters is a problematic endeavour as the high variance in latency + between the datacenters could lead to false positive failure detection + and partitioning. However if the ensemble runs entirely in one + datacenter, and the second datacenter runs only Observers, partitions + aren't problematic as the ensemble remains connected. Clients of the + Observers may still see and issue proposals. +* As a link to a message bus: Some companies have expressed an + interest in using ZK as a component of a persistent reliable message + bus. Observers would give a natural integration point for this work: a + plug-in mechanism could be used to attach the stream of proposals an + Observer sees to a publish-subscribe system, again without loading the + core ensemble. diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperOver.md b/zookeeper-docs/src/main/resources/markdown/zookeeperOver.md new file mode 100644 index 0000000..4c60a3d --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperOver.md @@ -0,0 +1,336 @@ + + +# ZooKeeper + +* [ZooKeeper: A Distributed Coordination Service for Distributed Applications](#ch_DesignOverview) + * [Design Goals](#sc_designGoals) + * [Data model and the hierarchical namespace](#sc_dataModelNameSpace) + * [Nodes and ephemeral nodes](#Nodes+and+ephemeral+nodes) + * [Conditional updates and watches](#Conditional+updates+and+watches) + * [Guarantees](#Guarantees) + * [Simple API](#Simple+API) + * [Implementation](#Implementation) + * [Uses](#Uses) + * [Performance](#Performance) + * [Reliability](#Reliability) + * [The ZooKeeper Project](#The+ZooKeeper+Project) + + + +## ZooKeeper: A Distributed Coordination Service for Distributed Applications + +ZooKeeper is a distributed, open-source coordination service for +distributed applications. It exposes a simple set of primitives that +distributed applications can build upon to implement higher level services +for synchronization, configuration maintenance, and groups and naming. It +is designed to be easy to program to, and uses a data model styled after +the familiar directory tree structure of file systems. It runs in Java and +has bindings for both Java and C. + +Coordination services are notoriously hard to get right. They are +especially prone to errors such as race conditions and deadlock. The +motivation behind ZooKeeper is to relieve distributed applications the +responsibility of implementing coordination services from scratch. + + + +### Design Goals + +**ZooKeeper is simple.** ZooKeeper +allows distributed processes to coordinate with each other through a +shared hierarchical namespace which is organized similarly to a standard +file system. The namespace consists of data registers - called znodes, +in ZooKeeper parlance - and these are similar to files and directories. +Unlike a typical file system, which is designed for storage, ZooKeeper +data is kept in-memory, which means ZooKeeper can achieve high +throughput and low latency numbers. + +The ZooKeeper implementation puts a premium on high performance, +highly available, strictly ordered access. The performance aspects of +ZooKeeper means it can be used in large, distributed systems. The +reliability aspects keep it from being a single point of failure. The +strict ordering means that sophisticated synchronization primitives can +be implemented at the client. + +**ZooKeeper is replicated.** Like the +distributed processes it coordinates, ZooKeeper itself is intended to be +replicated over a set of hosts called an ensemble. + +![ZooKeeper Service](images/zkservice.jpg) + +The servers that make up the ZooKeeper service must all know about +each other. They maintain an in-memory image of state, along with a +transaction logs and snapshots in a persistent store. As long as a +majority of the servers are available, the ZooKeeper service will be +available. + +Clients connect to a single ZooKeeper server. The client maintains +a TCP connection through which it sends requests, gets responses, gets +watch events, and sends heart beats. If the TCP connection to the server +breaks, the client will connect to a different server. + +**ZooKeeper is ordered.** ZooKeeper +stamps each update with a number that reflects the order of all +ZooKeeper transactions. Subsequent operations can use the order to +implement higher-level abstractions, such as synchronization +primitives. + +**ZooKeeper is fast.** It is +especially fast in "read-dominant" workloads. ZooKeeper applications run +on thousands of machines, and it performs best where reads are more +common than writes, at ratios of around 10:1. + + + +### Data model and the hierarchical namespace + +The namespace provided by ZooKeeper is much like that of a +standard file system. A name is a sequence of path elements separated by +a slash (/). Every node in ZooKeeper's namespace is identified by a +path. + +#### ZooKeeper's Hierarchical Namespace + +![ZooKeeper's Hierarchical Namespace](images/zknamespace.jpg) + + + +### Nodes and ephemeral nodes + +Unlike standard file systems, each node in a ZooKeeper +namespace can have data associated with it as well as children. It is +like having a file-system that allows a file to also be a directory. +(ZooKeeper was designed to store coordination data: status information, +configuration, location information, etc., so the data stored at each +node is usually small, in the byte to kilobyte range.) We use the term +_znode_ to make it clear that we are talking about +ZooKeeper data nodes. + +Znodes maintain a stat structure that includes version numbers for +data changes, ACL changes, and timestamps, to allow cache validations +and coordinated updates. Each time a znode's data changes, the version +number increases. For instance, whenever a client retrieves data it also +receives the version of the data. + +The data stored at each znode in a namespace is read and written +atomically. Reads get all the data bytes associated with a znode and a +write replaces all the data. Each node has an Access Control List (ACL) +that restricts who can do what. + +ZooKeeper also has the notion of ephemeral nodes. These znodes +exists as long as the session that created the znode is active. When the +session ends the znode is deleted. + + + +### Conditional updates and watches + +ZooKeeper supports the concept of _watches_. +Clients can set a watch on a znode. A watch will be triggered and +removed when the znode changes. When a watch is triggered, the client +receives a packet saying that the znode has changed. If the +connection between the client and one of the ZooKeeper servers is +broken, the client will receive a local notification. + +**New in 3.6.0:** Clients can also set +permanent, recursive watches on a znode that are not removed when triggered +and that trigger for changes on the registered znode as well as any children +znodes recursively. + + + +### Guarantees + +ZooKeeper is very fast and very simple. Since its goal, though, is +to be a basis for the construction of more complicated services, such as +synchronization, it provides a set of guarantees. These are: + +* Sequential Consistency - Updates from a client will be applied + in the order that they were sent. +* Atomicity - Updates either succeed or fail. No partial + results. +* Single System Image - A client will see the same view of the + service regardless of the server that it connects to. i.e., a + client will never see an older view of the system even if the + client fails over to a different server with the same session. +* Reliability - Once an update has been applied, it will persist + from that time forward until a client overwrites the update. +* Timeliness - The clients view of the system is guaranteed to + be up-to-date within a certain time bound. + + + +### Simple API + +One of the design goals of ZooKeeper is providing a very simple +programming interface. As a result, it supports only these +operations: + +* *create* : + creates a node at a location in the tree + +* *delete* : + deletes a node + +* *exists* : + tests if a node exists at a location + +* *get data* : + reads the data from a node + +* *set data* : + writes data to a node + +* *get children* : + retrieves a list of children of a node + +* *sync* : + waits for data to be propagated + + + +### Implementation + +[ZooKeeper Components](#zkComponents) shows the high-level components +of the ZooKeeper service. With the exception of the request processor, +each of +the servers that make up the ZooKeeper service replicates its own copy +of each of the components. + + + +![ZooKeeper Components](images/zkcomponents.jpg) + +The replicated database is an in-memory database containing the +entire data tree. Updates are logged to disk for recoverability, and +writes are serialized to disk before they are applied to the in-memory +database. + +Every ZooKeeper server services clients. Clients connect to +exactly one server to submit requests. Read requests are serviced from +the local replica of each server database. Requests that change the +state of the service, write requests, are processed by an agreement +protocol. + +As part of the agreement protocol all write requests from clients +are forwarded to a single server, called the +_leader_. The rest of the ZooKeeper servers, called +_followers_, receive message proposals from the +leader and agree upon message delivery. The messaging layer takes care +of replacing leaders on failures and syncing followers with +leaders. + +ZooKeeper uses a custom atomic messaging protocol. Since the +messaging layer is atomic, ZooKeeper can guarantee that the local +replicas never diverge. When the leader receives a write request, it +calculates what the state of the system is when the write is to be +applied and transforms this into a transaction that captures this new +state. + + + +### Uses + +The programming interface to ZooKeeper is deliberately simple. +With it, however, you can implement higher order operations, such as +synchronizations primitives, group membership, ownership, etc. + + + +### Performance + +ZooKeeper is designed to be highly performance. But is it? The +results of the ZooKeeper's development team at Yahoo! Research indicate +that it is. (See [ZooKeeper Throughput as the Read-Write Ratio Varies](#zkPerfRW).) It is especially high +performance in applications where reads outnumber writes, since writes +involve synchronizing the state of all servers. (Reads outnumbering +writes is typically the case for a coordination service.) + + + +![ZooKeeper Throughput as the Read-Write Ratio Varies](images/zkperfRW-3.2.jpg) + +The [ZooKeeper Throughput as the Read-Write Ratio Varies](#zkPerfRW) is a throughput +graph of ZooKeeper release 3.2 running on servers with dual 2Ghz +Xeon and two SATA 15K RPM drives. One drive was used as a +dedicated ZooKeeper log device. The snapshots were written to +the OS drive. Write requests were 1K writes and the reads were +1K reads. "Servers" indicate the size of the ZooKeeper +ensemble, the number of servers that make up the +service. Approximately 30 other servers were used to simulate +the clients. The ZooKeeper ensemble was configured such that +leaders do not allow connections from clients. + +######Note +>In version 3.2 r/w performance improved by ~2x compared to + the [previous 3.1 release](http://zookeeper.apache.org/docs/r3.1.1/zookeeperOver.html#Performance). + +Benchmarks also indicate that it is reliable, too. +[Reliability in the Presence of Errors](#zkPerfReliability) shows how a deployment responds to +various failures. The events marked in the figure are the following: + +1. Failure and recovery of a follower +1. Failure and recovery of a different follower +1. Failure of the leader +1. Failure and recovery of two followers +1. Failure of another leader + + + +### Reliability + +To show the behavior of the system over time as +failures are injected we ran a ZooKeeper service made up of +7 machines. We ran the same saturation benchmark as before, +but this time we kept the write percentage at a constant +30%, which is a conservative ratio of our expected +workloads. + + + +![Reliability in the Presence of Errors](images/zkperfreliability.jpg) + +There are a few important observations from this graph. First, if +followers fail and recover quickly, then ZooKeeper is able to sustain a +high throughput despite the failure. But maybe more importantly, the +leader election algorithm allows for the system to recover fast enough +to prevent throughput from dropping substantially. In our observations, +ZooKeeper takes less than 200ms to elect a new leader. Third, as +followers recover, ZooKeeper is able to raise throughput again once they +start processing requests. + + + +### The ZooKeeper Project + +ZooKeeper has been +[successfully used](https://cwiki.apache.org/confluence/display/ZOOKEEPER/PoweredBy) +in many industrial applications. It is used at Yahoo! as the +coordination and failure recovery service for Yahoo! Message +Broker, which is a highly scalable publish-subscribe system +managing thousands of topics for replication and data +delivery. It is used by the Fetching Service for Yahoo! +crawler, where it also manages failure recovery. A number of +Yahoo! advertising systems also use ZooKeeper to implement +reliable services. + +All users and developers are encouraged to join the +community and contribute their expertise. See the +[Zookeeper Project on Apache](http://zookeeper.apache.org/) +for more information. + + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperProgrammers.md b/zookeeper-docs/src/main/resources/markdown/zookeeperProgrammers.md new file mode 100644 index 0000000..7c0d4ec --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperProgrammers.md @@ -0,0 +1,1631 @@ + + +# ZooKeeper Programmer's Guide + +### Developing Distributed Applications that use ZooKeeper + +* [Introduction](#_introduction) +* [The ZooKeeper Data Model](#ch_zkDataModel) + * [ZNodes](#sc_zkDataModel_znodes) + * [Watches](#sc_zkDataMode_watches) + * [Data Access](#Data+Access) + * [Ephemeral Nodes](#Ephemeral+Nodes) + * [Sequence Nodes -- Unique Naming](#Sequence+Nodes+--+Unique+Naming) + * [Container Nodes](#Container+Nodes) + * [TTL Nodes](#TTL+Nodes) + * [Time in ZooKeeper](#sc_timeInZk) + * [ZooKeeper Stat Structure](#sc_zkStatStructure) +* [ZooKeeper Sessions](#ch_zkSessions) +* [ZooKeeper Watches](#ch_zkWatches) + * [Semantics of Watches](#sc_WatchSemantics) + * [Persistent, Recursive Watches](#sc_WatchPersistentRecursive) + * [Remove Watches](#sc_WatchRemoval) + * [What ZooKeeper Guarantees about Watches](#sc_WatchGuarantees) + * [Things to Remember about Watches](#sc_WatchRememberThese) +* [ZooKeeper access control using ACLs](#sc_ZooKeeperAccessControl) + * [ACL Permissions](#sc_ACLPermissions) + * [Builtin ACL Schemes](#sc_BuiltinACLSchemes) + * [ZooKeeper C client API](#ZooKeeper+C+client+API) +* [Pluggable ZooKeeper authentication](#sc_ZooKeeperPluggableAuthentication) +* [Consistency Guarantees](#ch_zkGuarantees) +* [Bindings](#ch_bindings) + * [Java Binding](#Java+Binding) + * [Client Configuration Parameters](#sc_java_client_configuration) + * [C Binding](#C+Binding) + * [Installation](#Installation) + * [Building Your Own C Client](#Building+Your+Own+C+Client) +* [Building Blocks: A Guide to ZooKeeper Operations](#ch_guideToZkOperations) + * [Handling Errors](#sc_errorsZk) + * [Connecting to ZooKeeper](#sc_connectingToZk) +* [Gotchas: Common Problems and Troubleshooting](#ch_gotchas) + + + +## Introduction + +This document is a guide for developers wishing to create +distributed applications that take advantage of ZooKeeper's coordination +services. It contains conceptual and practical information. + +The first four sections of this guide present a higher level +discussions of various ZooKeeper concepts. These are necessary both for an +understanding of how ZooKeeper works as well how to work with it. It does +not contain source code, but it does assume a familiarity with the +problems associated with distributed computing. The sections in this first +group are: + +* [The ZooKeeper Data Model](#ch_zkDataModel) +* [ZooKeeper Sessions](#ch_zkSessions) +* [ZooKeeper Watches](#ch_zkWatches) +* [Consistency Guarantees](#ch_zkGuarantees) + +The next four sections provide practical programming +information. These are: + +* [Building Blocks: A Guide to ZooKeeper Operations](#ch_guideToZkOperations) +* [Bindings](#ch_bindings) +* [Gotchas: Common Problems and Troubleshooting](#ch_gotchas) + +The book concludes with an [appendix](#apx_linksToOtherInfo) containing links to other +useful, ZooKeeper-related information. + +Most of the information in this document is written to be accessible as +stand-alone reference material. However, before starting your first +ZooKeeper application, you should probably at least read the chapters on +the [ZooKeeper Data Model](#ch_zkDataModel) and [ZooKeeper Basic Operations](#ch_guideToZkOperations). + + + +## The ZooKeeper Data Model + +ZooKeeper has a hierarchal namespace, much like a distributed file +system. The only difference is that each node in the namespace can have +data associated with it as well as children. It is like having a file +system that allows a file to also be a directory. Paths to nodes are +always expressed as canonical, absolute, slash-separated paths; there are +no relative reference. Any unicode character can be used in a path subject +to the following constraints: + +* The null character (\\u0000) cannot be part of a path name. (This + causes problems with the C binding.) +* The following characters can't be used because they don't + display well, or render in confusing ways: \\u0001 - \\u001F and \\u007F + - \\u009F. +* The following characters are not allowed: \\ud800 - uF8FF, + \\uFFF0 - uFFFF. +* The "." character can be used as part of another name, but "." + and ".." cannot alone be used to indicate a node along a path, + because ZooKeeper doesn't use relative paths. The following would be + invalid: "/a/b/./c" or "/a/b/../c". +* The token "zookeeper" is reserved. + + + +### ZNodes + +Every node in a ZooKeeper tree is referred to as a +_znode_. Znodes maintain a stat structure that +includes version numbers for data changes, acl changes. The stat +structure also has timestamps. The version number, together with the +timestamp, allows ZooKeeper to validate the cache and to coordinate +updates. Each time a znode's data changes, the version number increases. +For instance, whenever a client retrieves data, it also receives the +version of the data. And when a client performs an update or a delete, +it must supply the version of the data of the znode it is changing. If +the version it supplies doesn't match the actual version of the data, +the update will fail. (This behavior can be overridden. + +######Note + +>In distributed application engineering, the word +_node_ can refer to a generic host machine, a +server, a member of an ensemble, a client process, etc. In the ZooKeeper +documentation, _znodes_ refer to the data nodes. +_Servers_ refers to machines that make up the +ZooKeeper service; _quorum peers_ refer to the +servers that make up an ensemble; client refers to any host or process +which uses a ZooKeeper service. + +Znodes are the main entity that a programmer access. They have +several characteristics that are worth mentioning here. + + + +#### Watches + +Clients can set watches on znodes. Changes to that znode trigger +the watch and then clear the watch. When a watch triggers, ZooKeeper +sends the client a notification. More information about watches can be +found in the section +[ZooKeeper Watches](#ch_zkWatches). + + + +#### Data Access + +The data stored at each znode in a namespace is read and written +atomically. Reads get all the data bytes associated with a znode and a +write replaces all the data. Each node has an Access Control List +(ACL) that restricts who can do what. + +ZooKeeper was not designed to be a general database or large +object store. Instead, it manages coordination data. This data can +come in the form of configuration, status information, rendezvous, etc. +A common property of the various forms of coordination data is that +they are relatively small: measured in kilobytes. +The ZooKeeper client and the server implementations have sanity checks +to ensure that znodes have less than 1M of data, but the data should +be much less than that on average. Operating on relatively large data +sizes will cause some operations to take much more time than others and +will affect the latencies of some operations because of the extra time +needed to move more data over the network and onto storage media. If +large data storage is needed, the usual pattern of dealing with such +data is to store it on a bulk storage system, such as NFS or HDFS, and +store pointers to the storage locations in ZooKeeper. + + + +#### Ephemeral Nodes + +ZooKeeper also has the notion of ephemeral nodes. These znodes +exists as long as the session that created the znode is active. When +the session ends the znode is deleted. Because of this behavior +ephemeral znodes are not allowed to have children. The list of ephemerals +for the session can be retrieved using **getEphemerals()** api. + +##### getEphemerals() +Retrieves the list of ephemeral nodes created by the session for the +given path. If the path is empty, it will list all the ephemeral nodes +for the session. +**Use Case** - A sample use case might be, if the list of ephemeral +nodes for the session needs to be collected for duplicate data entry check +and the nodes are created in a sequential manner so you do not know the name +for duplicate check. In that case, getEphemerals() api could be used to +get the list of nodes for the session. This might be a typical use case +for service discovery. + + + +#### Sequence Nodes -- Unique Naming + +When creating a znode you can also request that +ZooKeeper append a monotonically increasing counter to the end +of path. This counter is unique to the parent znode. The +counter has a format of %010d -- that is 10 digits with 0 +(zero) padding (the counter is formatted in this way to +simplify sorting), i.e. "0000000001". See +[Queue +Recipe](recipes.html#sc_recipes_Queues) for an example use of this feature. Note: the +counter used to store the next sequence number is a signed int +(4bytes) maintained by the parent node, the counter will +overflow when incremented beyond 2147483647 (resulting in a +name "-2147483648"). + + + +#### Container Nodes + +**Added in 3.6.0** + +ZooKeeper has the notion of container znodes. Container znodes are +special purpose znodes useful for recipes such as leader, lock, etc. +When the last child of a container is deleted, the container becomes +a candidate to be deleted by the server at some point in the future. + +Given this property, you should be prepared to get +KeeperException.NoNodeException when creating children inside of +container znodes. i.e. when creating child znodes inside of container znodes +always check for KeeperException.NoNodeException and recreate the container +znode when it occurs. + + + +#### TTL Nodes + +**Added in 3.6.0** + +When creating PERSISTENT or PERSISTENT_SEQUENTIAL znodes, +you can optionally set a TTL in milliseconds for the znode. If the znode +is not modified within the TTL and has no children it will become a candidate +to be deleted by the server at some point in the future. + +Note: TTL Nodes must be enabled via System property as they +are disabled by default. See the [Administrator's Guide](zookeeperAdmin.html#sc_configuration) for +details. If you attempt to create TTL Nodes without the +proper System property set the server will throw +KeeperException.UnimplementedException. + + + +### Time in ZooKeeper + +ZooKeeper tracks time multiple ways: + +* **Zxid** + Every change to the ZooKeeper state receives a stamp in the + form of a _zxid_ (ZooKeeper Transaction Id). + This exposes the total ordering of all changes to ZooKeeper. Each + change will have a unique zxid and if zxid1 is smaller than zxid2 + then zxid1 happened before zxid2. +* **Version numbers** + Every change to a node will cause an increase to one of the + version numbers of that node. The three version numbers are version + (number of changes to the data of a znode), cversion (number of + changes to the children of a znode), and aversion (number of changes + to the ACL of a znode). +* **Ticks** + When using multi-server ZooKeeper, servers use ticks to define + timing of events such as status uploads, session timeouts, + connection timeouts between peers, etc. The tick time is only + indirectly exposed through the minimum session timeout (2 times the + tick time); if a client requests a session timeout less than the + minimum session timeout, the server will tell the client that the + session timeout is actually the minimum session timeout. +* **Real time** + ZooKeeper doesn't use real time, or clock time, at all except + to put timestamps into the stat structure on znode creation and + znode modification. + + + +### ZooKeeper Stat Structure + +The Stat structure for each znode in ZooKeeper is made up of the +following fields: + +* **czxid** + The zxid of the change that caused this znode to be + created. +* **mzxid** + The zxid of the change that last modified this znode. +* **pzxid** + The zxid of the change that last modified children of this znode. +* **ctime** + The time in milliseconds from epoch when this znode was + created. +* **mtime** + The time in milliseconds from epoch when this znode was last + modified. +* **version** + The number of changes to the data of this znode. +* **cversion** + The number of changes to the children of this znode. +* **aversion** + The number of changes to the ACL of this znode. +* **ephemeralOwner** + The session id of the owner of this znode if the znode is an + ephemeral node. If it is not an ephemeral node, it will be + zero. +* **dataLength** + The length of the data field of this znode. +* **numChildren** + The number of children of this znode. + + + +## ZooKeeper Sessions + +A ZooKeeper client establishes a session with the ZooKeeper +service by creating a handle to the service using a language +binding. Once created, the handle starts off in the CONNECTING state +and the client library tries to connect to one of the servers that +make up the ZooKeeper service at which point it switches to the +CONNECTED state. During normal operation the client handle will be in one of these +two states. If an unrecoverable error occurs, such as session +expiration or authentication failure, or if the application explicitly +closes the handle, the handle will move to the CLOSED state. +The following figure shows the possible state transitions of a +ZooKeeper client: + +![State transitions](images/state_dia.jpg) + +To create a client session the application code must provide +a connection string containing a comma separated list of host:port pairs, +each corresponding to a ZooKeeper server (e.g. "127.0.0.1:4545" or +"127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"). The ZooKeeper +client library will pick an arbitrary server and try to connect to +it. If this connection fails, or if the client becomes +disconnected from the server for any reason, the client will +automatically try the next server in the list, until a connection +is (re-)established. + +**Added in 3.2.0**: An +optional "chroot" suffix may also be appended to the connection +string. This will run the client commands while interpreting all +paths relative to this root (similar to the unix chroot +command). If used the example would look like: +"127.0.0.1:4545/app/a" or +"127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" where the +client would be rooted at "/app/a" and all paths would be relative +to this root - ie getting/setting/etc... "/foo/bar" would result +in operations being run on "/app/a/foo/bar" (from the server +perspective). This feature is particularly useful in multi-tenant +environments where each user of a particular ZooKeeper service +could be rooted differently. This makes re-use much simpler as +each user can code his/her application as if it were rooted at +"/", while actual location (say /app/a) could be determined at +deployment time. + +When a client gets a handle to the ZooKeeper service, +ZooKeeper creates a ZooKeeper session, represented as a 64-bit +number, that it assigns to the client. If the client connects to a +different ZooKeeper server, it will send the session id as a part +of the connection handshake. As a security measure, the server +creates a password for the session id that any ZooKeeper server +can validate.The password is sent to the client with the session +id when the client establishes the session. The client sends this +password with the session id whenever it reestablishes the session +with a new server. + +One of the parameters to the ZooKeeper client library call +to create a ZooKeeper session is the session timeout in +milliseconds. The client sends a requested timeout, the server +responds with the timeout that it can give the client. The current +implementation requires that the timeout be a minimum of 2 times +the tickTime (as set in the server configuration) and a maximum of +20 times the tickTime. The ZooKeeper client API allows access to +the negotiated timeout. + +When a client (session) becomes partitioned from the ZK +serving cluster it will begin searching the list of servers that +were specified during session creation. Eventually, when +connectivity between the client and at least one of the servers is +re-established, the session will either again transition to the +"connected" state (if reconnected within the session timeout +value) or it will transition to the "expired" state (if +reconnected after the session timeout). It is not advisable to +create a new session object (a new ZooKeeper.class or zookeeper +handle in the c binding) for disconnection. The ZK client library +will handle reconnect for you. In particular we have heuristics +built into the client library to handle things like "herd effect", +etc... Only create a new session when you are notified of session +expiration (mandatory). + +Session expiration is managed by the ZooKeeper cluster +itself, not by the client. When the ZK client establishes a +session with the cluster it provides a "timeout" value detailed +above. This value is used by the cluster to determine when the +client's session expires. Expirations happens when the cluster +does not hear from the client within the specified session timeout +period (i.e. no heartbeat). At session expiration the cluster will +delete any/all ephemeral nodes owned by that session and +immediately notify any/all connected clients of the change (anyone +watching those znodes). At this point the client of the expired +session is still disconnected from the cluster, it will not be +notified of the session expiration until/unless it is able to +re-establish a connection to the cluster. The client will stay in +disconnected state until the TCP connection is re-established with +the cluster, at which point the watcher of the expired session +will receive the "session expired" notification. + +Example state transitions for an expired session as seen by +the expired session's watcher: + +1. 'connected' : session is established and client + is communicating with cluster (client/server communication is + operating properly) +1. .... client is partitioned from the + cluster +1. 'disconnected' : client has lost connectivity + with the cluster +1. .... time elapses, after 'timeout' period the + cluster expires the session, nothing is seen by client as it is + disconnected from cluster +1. .... time elapses, the client regains network + level connectivity with the cluster +1. 'expired' : eventually the client reconnects to + the cluster, it is then notified of the + expiration + +Another parameter to the ZooKeeper session establishment +call is the default watcher. Watchers are notified when any state +change occurs in the client. For example if the client loses +connectivity to the server the client will be notified, or if the +client's session expires, etc... This watcher should consider the +initial state to be disconnected (i.e. before any state changes +events are sent to the watcher by the client lib). In the case of +a new connection, the first event sent to the watcher is typically +the session connection event. + +The session is kept alive by requests sent by the client. If +the session is idle for a period of time that would timeout the +session, the client will send a PING request to keep the session +alive. This PING request not only allows the ZooKeeper server to +know that the client is still active, but it also allows the +client to verify that its connection to the ZooKeeper server is +still active. The timing of the PING is conservative enough to +ensure reasonable time to detect a dead connection and reconnect +to a new server. + +Once a connection to the server is successfully established +(connected) there are basically two cases where the client lib generates +connectionloss (the result code in c binding, exception in Java -- see +the API documentation for binding specific details) when either a synchronous or +asynchronous operation is performed and one of the following holds: + +1. The application calls an operation on a session that is no + longer alive/valid +1. The ZooKeeper client disconnects from a server when there + are pending operations to that server, i.e., there is a pending asynchronous call. + +**Added in 3.2.0 -- SessionMovedException**. There is an internal +exception that is generally not seen by clients called the SessionMovedException. +This exception occurs because a request was received on a connection for a session +which has been reestablished on a different server. The normal cause of this error is +a client that sends a request to a server, but the network packet gets delayed, so +the client times out and connects to a new server. When the delayed packet arrives at +the first server, the old server detects that the session has moved, and closes the +client connection. Clients normally do not see this error since they do not read +from those old connections. (Old connections are usually closed.) One situation in which this +condition can be seen is when two clients try to reestablish the same connection using +a saved session id and password. One of the clients will reestablish the connection +and the second client will be disconnected (causing the pair to attempt to re-establish +its connection/session indefinitely). + +**Updating the list of servers**. We allow a client to +update the connection string by providing a new comma separated list of host:port pairs, +each corresponding to a ZooKeeper server. The function invokes a probabilistic load-balancing +algorithm which may cause the client to disconnect from its current host with the goal +to achieve expected uniform number of connections per server in the new list. +In case the current host to which the client is connected is not in the new list +this call will always cause the connection to be dropped. Otherwise, the decision +is based on whether the number of servers has increased or decreased and by how much. + +For example, if the previous connection string contained 3 hosts and now the list contains +these 3 hosts and 2 more hosts, 40% of clients connected to each of the 3 hosts will +move to one of the new hosts in order to balance the load. The algorithm will cause the client +to drop its connection to the current host to which it is connected with probability 0.4 and in this +case cause the client to connect to one of the 2 new hosts, chosen at random. + +Another example -- suppose we have 5 hosts and now update the list to remove 2 of the hosts, +the clients connected to the 3 remaining hosts will stay connected, whereas all clients connected +to the 2 removed hosts will need to move to one of the 3 hosts, chosen at random. If the connection +is dropped, the client moves to a special mode where he chooses a new server to connect to using the +probabilistic algorithm, and not just round robin. + +In the first example, each client decides to disconnect with probability 0.4 but once the decision is +made, it will try to connect to a random new server and only if it cannot connect to any of the new +servers will it try to connect to the old ones. After finding a server, or trying all servers in the +new list and failing to connect, the client moves back to the normal mode of operation where it picks +an arbitrary server from the connectString and attempts to connect to it. If that fails, it will continue +trying different random servers in round robin. (see above the algorithm used to initially choose a server) + +**Local session**. Added in 3.5.0, mainly implemented by [ZOOKEEPER-1147](https://issues.apache.org/jira/browse/ZOOKEEPER-1147). + +- Background: The creation and closing of sessions are costly in ZooKeeper because they need quorum confirmations, + they become the bottleneck of a ZooKeeper ensemble when it needs to handle thousands of client connections. +So after 3.5.0, we introduce a new type of session: local session which doesn't have a full functionality of a normal(global) session, this feature +will be available by turning on *localSessionsEnabled*. + +when *localSessionsUpgradingEnabled* is disable: + +- Local sessions cannot create ephemeral nodes + +- Once a local session is lost, users cannot re-establish it using the session-id/password, the session and its watches are gone for good. + Note: Losing the tcp connection does not necessarily imply that the session is lost. If the connection can be reestablished with the same zk server + before the session timeout then the client can continue (it simply cannot move to another server). + +- When a local session connects, the session info is only maintained on the zookeeper server that it is connected to. The leader is not aware of the creation of such a session and +there is no state written to disk. + +- The pings, expiration and other session state maintenance are handled by the server which current session is connected to. + +when *localSessionsUpgradingEnabled* is enable: + +- A local session can be upgraded to the global session automatically. + +- When a new session is created it is saved locally in a wrapped *LocalSessionTracker*. It can subsequently be upgraded +to a global session as required (e.g. create ephemeral nodes). If an upgrade is requested the session is removed from local + collections while keeping the same session ID. + +- Currently, Only the operation: *create ephemeral node* needs a session upgrade from local to global. +The reason is that the creation of ephemeral node depends heavily on a global session. If local session can create ephemeral +node without upgrading to global session, it will cause the data inconsistency between different nodes. +The leader also needs to know about the lifespan of a session in order to clean up ephemeral nodes on close/expiry. +This requires a global session as the local session is tied to its particular server. + +- A session can be both a local and global session during upgrade, but the operation of upgrade cannot be called concurrently by two thread. + +- *ZooKeeperServer*(Standalone) uses *SessionTrackerImpl*; *LeaderZookeeper* uses *LeaderSessionTracker* which holds + *SessionTrackerImpl*(global) and *LocalSessionTracker*(if enable); *FollowerZooKeeperServer* and *ObserverZooKeeperServer* + use *LearnerSessionTracker* which holds *LocalSessionTracker*. + The UML Graph of Classes about session: + + ``` + +----------------+ +--------------------+ +---------------------+ + | | --> | | ----> | LocalSessionTracker | + | SessionTracker | | SessionTrackerImpl | +---------------------+ + | | | | +-----------------------+ + | | | | +-------------------------> | LeaderSessionTracker | + +----------------+ +--------------------+ | +-----------------------+ + | | + | | + | | + | +---------------------------+ + +---------> | | + | UpgradeableSessionTracker | + | | + | | ------------------------+ + +---------------------------+ | + | + | + v + +-----------------------+ + | LearnerSessionTracker | + +-----------------------+ + ``` + +- Q&A + - *What's the reason for having the config option to disable local session upgrade?* + - In a large deployment which wants to handle a very large number of clients, we know that clients connecting via the observers + which is supposed to be local session only. So this is more like a safeguard against someone accidentally creates lots of ephemeral nodes and global sessions. + + - *When is the session created?* + - In the current implementation, it will try to create a local session when processing *ConnectRequest* and when + *createSession* request reaches *FinalRequestProcessor*. + + - *What happens if the create for session is sent at server A and the client disconnects to some other server B + which ends up sending it again and then disconnects and connects back to server A?* + - When a client reconnects to B, its sessionId won’t exist in B’s local session tracker. So B will send validation packet. + If CreateSession issued by A is committed before validation packet arrive the client will be able to connect. + Otherwise, the client will get session expired because the quorum hasn’t know about this session yet. + If the client also tries to connect back to A again, the session is already removed from local session tracker. + So A will need to send a validation packet to the leader. The outcome should be the same as B depending on the timing of the request. + + + +## ZooKeeper Watches + +All of the read operations in ZooKeeper - **getData()**, **getChildren()**, and **exists()** - have the option of setting a watch as a +side effect. Here is ZooKeeper's definition of a watch: a watch event is +one-time trigger, sent to the client that set the watch, which occurs when +the data for which the watch was set changes. There are three key points +to consider in this definition of a watch: + +* **One-time trigger** + One watch event will be sent to the client when the data has changed. + For example, if a client does a getData("/znode1", true) and later the + data for /znode1 is changed or deleted, the client will get a watch + event for /znode1. If /znode1 changes again, no watch event will be + sent unless the client has done another read that sets a new + watch. +* **Sent to the client** + This implies that an event is on the way to the client, but may + not reach the client before the successful return code to the change + operation reaches the client that initiated the change. Watches are + sent asynchronously to watchers. ZooKeeper provides an ordering + guarantee: a client will never see a change for which it has set a + watch until it first sees the watch event. Network delays or other + factors may cause different clients to see watches and return codes + from updates at different times. The key point is that everything seen + by the different clients will have a consistent order. +* **The data for which the watch was + set** + This refers to the different ways a node can change. It + helps to think of ZooKeeper as maintaining two lists of + watches: data watches and child watches. getData() and + exists() set data watches. getChildren() sets child + watches. Alternatively, it may help to think of watches being + set according to the kind of data returned. getData() and + exists() return information about the data of the node, + whereas getChildren() returns a list of children. Thus, + setData() will trigger data watches for the znode being set + (assuming the set is successful). A successful create() will + trigger a data watch for the znode being created and a child + watch for the parent znode. A successful delete() will trigger + both a data watch and a child watch (since there can be no + more children) for a znode being deleted as well as a child + watch for the parent znode. + +Watches are maintained locally at the ZooKeeper server to which the +client is connected. This allows watches to be lightweight to set, +maintain, and dispatch. When a client connects to a new server, the watch +will be triggered for any session events. Watches will not be received +while disconnected from a server. When a client reconnects, any previously +registered watches will be reregistered and triggered if needed. In +general this all occurs transparently. There is one case where a watch +may be missed: a watch for the existence of a znode not yet created will +be missed if the znode is created and deleted while disconnected. + +**New in 3.6.0:** Clients can also set +permanent, recursive watches on a znode that are not removed when triggered +and that trigger for changes on the registered znode as well as any children +znodes recursively. + + + +### Semantics of Watches + +We can set watches with the three calls that read the state of +ZooKeeper: exists, getData, and getChildren. The following list details +the events that a watch can trigger and the calls that enable them: + +* **Created event:** + Enabled with a call to exists. +* **Deleted event:** + Enabled with a call to exists, getData, and getChildren. +* **Changed event:** + Enabled with a call to exists and getData. +* **Child event:** + Enabled with a call to getChildren. + + + +### Persistent, Recursive Watches + +**New in 3.6.0:** There is now a variation on the standard +watch described above whereby you can set a watch that does not get removed when triggered. +Additionally, these watches trigger the event types *NodeCreated*, *NodeDeleted*, and *NodeDataChanged* +and, optionally, recursively for all znodes starting at the znode that the watch is registered for. Note +that *NodeChildrenChanged* events are not triggered for persistent recursive watches as it would be redundant. + +Persistent watches are set using the method *addWatch()*. The triggering semantics and guarantees +(other than one-time triggering) are the same as standard watches. The only exception regarding events is that +recursive persistent watchers never trigger child changed events as they are redundant. +Persistent watches are removed using *removeWatches()* with watcher type *WatcherType.Any*. + + + +### Remove Watches + +We can remove the watches registered on a znode with a call to +removeWatches. Also, a ZooKeeper client can remove watches locally even +if there is no server connection by setting the local flag to true. The +following list details the events which will be triggered after the +successful watch removal. + +* **Child Remove event:** + Watcher which was added with a call to getChildren. +* **Data Remove event:** + Watcher which was added with a call to exists or getData. +* **Persistent Remove event:** + Watcher which was added with a call to add a persistent watch. + + + +### What ZooKeeper Guarantees about Watches + +With regard to watches, ZooKeeper maintains these +guarantees: + +* Watches are ordered with respect to other events, other + watches, and asynchronous replies. The ZooKeeper client libraries + ensures that everything is dispatched in order. + +* A client will see a watch event for a znode it is watching + before seeing the new data that corresponds to that znode. + +* The order of watch events from ZooKeeper corresponds to the + order of the updates as seen by the ZooKeeper service. + + + +### Things to Remember about Watches + +* Standard watches are one time triggers; if you get a watch event and + you want to get notified of future changes, you must set another + watch. + +* Because standard watches are one time triggers and there is latency + between getting the event and sending a new request to get a watch + you cannot reliably see every change that happens to a node in + ZooKeeper. Be prepared to handle the case where the znode changes + multiple times between getting the event and setting the watch + again. (You may not care, but at least realize it may + happen.) + +* A watch object, or function/context pair, will only be + triggered once for a given notification. For example, if the same + watch object is registered for an exists and a getData call for the + same file and that file is then deleted, the watch object would + only be invoked once with the deletion notification for the file. + +* When you disconnect from a server (for example, when the + server fails), you will not get any watches until the connection + is reestablished. For this reason session events are sent to all + outstanding watch handlers. Use session events to go into a safe + mode: you will not be receiving events while disconnected, so your + process should act conservatively in that mode. + + + +## ZooKeeper access control using ACLs + +ZooKeeper uses ACLs to control access to its znodes (the +data nodes of a ZooKeeper data tree). The ACL implementation is +quite similar to UNIX file access permissions: it employs +permission bits to allow/disallow various operations against a +node and the scope to which the bits apply. Unlike standard UNIX +permissions, a ZooKeeper node is not limited by the three standard +scopes for user (owner of the file), group, and world +(other). ZooKeeper does not have a notion of an owner of a +znode. Instead, an ACL specifies sets of ids and permissions that +are associated with those ids. + +Note also that an ACL pertains only to a specific znode. In +particular it does not apply to children. For example, if +_/app_ is only readable by ip:172.16.16.1 and +_/app/status_ is world readable, anyone will +be able to read _/app/status_; ACLs are not +recursive. + +ZooKeeper supports pluggable authentication schemes. Ids are +specified using the form _scheme:expression_, +where _scheme_ is the authentication scheme +that the id corresponds to. The set of valid expressions are defined +by the scheme. For example, _ip:172.16.16.1_ is +an id for a host with the address _172.16.16.1_ +using the _ip_ scheme, whereas _digest:bob:password_ +is an id for the user with the name of _bob_ using +the _digest_ scheme. + +When a client connects to ZooKeeper and authenticates +itself, ZooKeeper associates all the ids that correspond to a +client with the clients connection. These ids are checked against +the ACLs of znodes when a client tries to access a node. ACLs are +made up of pairs of _(scheme:expression, +perms)_. The format of +the _expression_ is specific to the scheme. For +example, the pair _(ip:19.22.0.0/16, READ)_ +gives the _READ_ permission to any clients with +an IP address that starts with 19.22. + + + +### ACL Permissions + +ZooKeeper supports the following permissions: + +* **CREATE**: you can create a child node +* **READ**: you can get data from a node and list its children. +* **WRITE**: you can set data for a node +* **DELETE**: you can delete a child node +* **ADMIN**: you can set permissions + +The _CREATE_ +and _DELETE_ permissions have been broken out +of the _WRITE_ permission for finer grained +access controls. The cases for _CREATE_ +and _DELETE_ are the following: + +You want A to be able to do a set on a ZooKeeper node, but +not be able to _CREATE_ +or _DELETE_ children. + +_CREATE_ +without _DELETE_: clients create requests by +creating ZooKeeper nodes in a parent directory. You want all +clients to be able to add, but only request processor can +delete. (This is kind of like the APPEND permission for +files.) + +Also, the _ADMIN_ permission is there +since ZooKeeper doesn’t have a notion of file owner. In some +sense the _ADMIN_ permission designates the +entity as the owner. ZooKeeper doesn’t support the LOOKUP +permission (execute permission bit on directories to allow you +to LOOKUP even though you can't list the directory). Everyone +implicitly has LOOKUP permission. This allows you to stat a +node, but nothing more. (The problem is, if you want to call +zoo_exists() on a node that doesn't exist, there is no +permission to check.) + +_ADMIN_ permission also has a special role in terms of ACLs: +in order to retrieve ACLs of a znode user has to have _READ_ or _ADMIN_ + permission, but without _ADMIN_ permission, digest hash values will be +masked out. + + + +#### Builtin ACL Schemes + +ZooKeeeper has the following built in schemes: + +* **world** has a + single id, _anyone_, that represents + anyone. +* **auth** is a special + scheme which ignores any provided expression and instead uses the current user, + credentials, and scheme. Any expression (whether _user_ like with SASL + authentication or _user:password_ like with DIGEST authentication) provided is ignored + by the ZooKeeper server when persisting the ACL. However, the expression must still be + provided in the ACL because the ACL must match the form _scheme:expression:perms_. + This scheme is provided as a convenience as it is a common use-case for + a user to create a znode and then restrict access to that znode to only that user. + If there is no authenticated user, setting an ACL with the auth scheme will fail. +* **digest** uses + a _username:password_ string to generate + MD5 hash which is then used as an ACL ID + identity. Authentication is done by sending + the _username:password_ in clear text. When + used in the ACL the expression will be + the _username:base64_ + encoded _SHA1_ + password _digest_. +* **ip** uses the + client host IP as an ACL ID identity. The ACL expression is of + the form _addr/bits_ where the most + significant _bits_ + of _addr_ are matched against the most + significant _bits_ of the client host + IP. +* **x509** uses the client + X500 Principal as an ACL ID identity. The ACL expression is the exact + X500 Principal name of a client. When using the secure port, clients + are automatically authenticated and their auth info for the x509 scheme + is set. + + + +#### ZooKeeper C client API + +The following constants are provided by the ZooKeeper C +library: + +* _const_ _int_ ZOO_PERM_READ; //can read node’s value and list its children +* _const_ _int_ ZOO_PERM_WRITE;// can set the node’s value +* _const_ _int_ ZOO_PERM_CREATE; //can create children +* _const_ _int_ ZOO_PERM_DELETE;// can delete children +* _const_ _int_ ZOO_PERM_ADMIN; //can execute set_acl() +* _const_ _int_ ZOO_PERM_ALL;// all of the above flags OR’d together + +The following are the standard ACL IDs: + +* _struct_ Id ZOO_ANYONE_ID_UNSAFE; //(‘world’,’anyone’) +* _struct_ Id ZOO_AUTH_IDS;// (‘auth’,’’) + +ZOO_AUTH_IDS empty identity string should be interpreted as “the identity of the creator”. + +ZooKeeper client comes with three standard ACLs: + +* _struct_ ACL_vector ZOO_OPEN_ACL_UNSAFE; //(ZOO_PERM_ALL,ZOO_ANYONE_ID_UNSAFE) +* _struct_ ACL_vector ZOO_READ_ACL_UNSAFE;// (ZOO_PERM_READ, ZOO_ANYONE_ID_UNSAFE) +* _struct_ ACL_vector ZOO_CREATOR_ALL_ACL; //(ZOO_PERM_ALL,ZOO_AUTH_IDS) + +The ZOO_OPEN_ACL_UNSAFE is completely open free for all +ACL: any application can execute any operation on the node and +can create, list and delete its children. The +ZOO_READ_ACL_UNSAFE is read-only access for any +application. CREATE_ALL_ACL grants all permissions to the +creator of the node. The creator must have been authenticated by +the server (for example, using “_digest_” +scheme) before it can create nodes with this ACL. + +The following ZooKeeper operations deal with ACLs: + +* _int_ _zoo_add_auth_ + (zhandle_t \*zh,_const_ _char_* + scheme,_const_ _char_* + cert, _int_ certLen, void_completion_t + completion, _const_ _void_ + \*data); + +The application uses the zoo_add_auth function to +authenticate itself to the server. The function can be called +multiple times if the application wants to authenticate using +different schemes and/or identities. + +* _int_ _zoo_create_ + (zhandle_t \*zh, _const_ _char_ + \*path, _const_ _char_ + \*value,_int_ + valuelen, _const_ _struct_ + ACL_vector \*acl, _int_ + flags,_char_ + \*realpath, _int_ + max_realpath_len); + +zoo_create(...) operation creates a new node. The acl +parameter is a list of ACLs associated with the node. The parent +node must have the CREATE permission bit set. + +* _int_ _zoo_get_acl_ + (zhandle_t \*zh, _const_ _char_ + \*path,_struct_ ACL_vector + \*acl, _struct_ Stat \*stat); + +This operation returns a node’s ACL info. The node must have READ or ADMIN +permission set. Without ADMIN permission, the digest hash values will be masked out. + +* _int_ _zoo_set_acl_ + (zhandle_t \*zh, _const_ _char_ + \*path, _int_ + version,_const_ _struct_ + ACL_vector \*acl); + +This function replaces node’s ACL list with a new one. The +node must have the ADMIN permission set. + +Here is a sample code that makes use of the above APIs to +authenticate itself using the “_foo_” scheme +and create an ephemeral node “/xyz” with create-only +permissions. + +######Note +>This is a very simple example which is intended to show +how to interact with ZooKeeper ACLs +specifically. See *.../trunk/zookeeper-client/zookeeper-client-c/src/cli.c* +for an example of a C client implementation + + + + #include + #include + + #include "zookeeper.h" + + static zhandle_t *zh; + + /** + * In this example this method gets the cert for your + * environment -- you must provide + */ + char *foo_get_cert_once(char* id) { return 0; } + + /** Watcher function -- empty for this example, not something you should + * do in real code */ + void watcher(zhandle_t *zzh, int type, int state, const char *path, + void *watcherCtx) {} + + int main(int argc, char argv) { + char buffer[512]; + char p[2048]; + char *cert=0; + char appId[64]; + + strcpy(appId, "example.foo_test"); + cert = foo_get_cert_once(appId); + if(cert!=0) { + fprintf(stderr, + "Certificate for appid [%s] is [%s]\n",appId,cert); + strncpy(p,cert, sizeof(p)-1); + free(cert); + } else { + fprintf(stderr, "Certificate for appid [%s] not found\n",appId); + strcpy(p, "dummy"); + } + + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + + zh = zookeeper_init("localhost:3181", watcher, 10000, 0, 0, 0); + if (!zh) { + return errno; + } + if(zoo_add_auth(zh,"foo",p,strlen(p),0,0)!=ZOK) + return 2; + + struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}}; + struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL}; + int rc = zoo_create(zh,"/xyz","value", 5, &CREATE_ONLY, ZOO_EPHEMERAL, + buffer, sizeof(buffer)-1); + + /** this operation will fail with a ZNOAUTH error */ + int buflen= sizeof(buffer); + struct Stat stat; + rc = zoo_get(zh, "/xyz", 0, buffer, &buflen, &stat); + if (rc) { + fprintf(stderr, "Error %d for %s\n", rc, __LINE__); + } + + zookeeper_close(zh); + return 0; + } + + + + +## Pluggable ZooKeeper authentication + +ZooKeeper runs in a variety of different environments with +various different authentication schemes, so it has a completely +pluggable authentication framework. Even the builtin authentication +schemes use the pluggable authentication framework. + +To understand how the authentication framework works, first you must +understand the two main authentication operations. The framework +first must authenticate the client. This is usually done as soon as +the client connects to a server and consists of validating information +sent from or gathered about a client and associating it with the connection. +The second operation handled by the framework is finding the entries in an +ACL that correspond to client. ACL entries are <_idspec, +permissions_> pairs. The _idspec_ may be +a simple string match against the authentication information associated +with the connection or it may be a expression that is evaluated against that +information. It is up to the implementation of the authentication plugin +to do the match. Here is the interface that an authentication plugin must +implement: + + + public interface AuthenticationProvider { + String getScheme(); + KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte authData[]); + boolean isValid(String id); + boolean matches(String id, String aclExpr); + boolean isAuthenticated(); + } + + +The first method _getScheme_ returns the string +that identifies the plugin. Because we support multiple methods of authentication, +an authentication credential or an _idspec_ will always be +prefixed with _scheme:_. The ZooKeeper server uses the scheme +returned by the authentication plugin to determine which ids the scheme +applies to. + +_handleAuthentication_ is called when a client +sends authentication information to be associated with a connection. The +client specifies the scheme to which the information corresponds. The +ZooKeeper server passes the information to the authentication plugin whose +_getScheme_ matches the scheme passed by the client. The +implementor of _handleAuthentication_ will usually return +an error if it determines that the information is bad, or it will associate information +with the connection using _cnxn.getAuthInfo().add(new Id(getScheme(), data))_. + +The authentication plugin is involved in both setting and using ACLs. When an +ACL is set for a znode, the ZooKeeper server will pass the id part of the entry to +the _isValid(String id)_ method. It is up to the plugin to verify +that the id has a correct form. For example, _ip:172.16.0.0/16_ +is a valid id, but _ip:host.com_ is not. If the new ACL includes +an "auth" entry, _isAuthenticated_ is used to see if the +authentication information for this scheme that is associated with the connection +should be added to the ACL. Some schemes +should not be included in auth. For example, the IP address of the client is not +considered as an id that should be added to the ACL if auth is specified. + +ZooKeeper invokes _matches(String id, String aclExpr)_ when checking an ACL. It +needs to match authentication information of the client against the relevant ACL +entries. To find the entries which apply to the client, the ZooKeeper server will +find the scheme of each entry and if there is authentication information +from that client for that scheme, _matches(String id, String aclExpr)_ +will be called with _id_ set to the authentication information +that was previously added to the connection by _handleAuthentication_ and +_aclExpr_ set to the id of the ACL entry. The authentication plugin +uses its own logic and matching scheme to determine if _id_ is included +in _aclExpr_. + +There are two built in authentication plugins: _ip_ and +_digest_. Additional plugins can adding using system properties. At +startup the ZooKeeper server will look for system properties that start with +"zookeeper.authProvider." and interpret the value of those properties as the class name +of an authentication plugin. These properties can be set using the +_-Dzookeeeper.authProvider.X=com.f.MyAuth_ or adding entries such as +the following in the server configuration file: + + + authProvider.1=com.f.MyAuth + authProvider.2=com.f.MyAuth2 + + +Care should be taking to ensure that the suffix on the property is unique. If there are +duplicates such as _-Dzookeeeper.authProvider.X=com.f.MyAuth -Dzookeeper.authProvider.X=com.f.MyAuth2_, +only one will be used. Also all servers must have the same plugins defined, otherwise clients using +the authentication schemes provided by the plugins will have problems connecting to some servers. + +**Added in 3.6.0**: An alternate abstraction is available for pluggable +authentication. It provides additional arguments. + + + public abstract class ServerAuthenticationProvider implements AuthenticationProvider { + public abstract KeeperException.Code handleAuthentication(ServerObjs serverObjs, byte authData[]); + public abstract boolean matches(ServerObjs serverObjs, MatchValues matchValues); + } + + +Instead of implementing AuthenticationProvider you extend ServerAuthenticationProvider. Your handleAuthentication() +and matches() methods will then receive the additional parameters (via ServerObjs and MatchValues). + +* **ZooKeeperServer** + The ZooKeeperServer instance +* **ServerCnxn** + The current connection +* **path** + The ZNode path being operated on (or null if not used) +* **perm** + The operation value or 0 +* **setAcls** + When the setAcl() method is being operated on, the list of ACLs that are being set + + + +## Consistency Guarantees + +ZooKeeper is a high performance, scalable service. Both reads and +write operations are designed to be fast, though reads are faster than +writes. The reason for this is that in the case of reads, ZooKeeper can +serve older data, which in turn is due to ZooKeeper's consistency +guarantees: + +* *Sequential Consistency* : + Updates from a client will be applied in the order that they + were sent. + +* *Atomicity* : + Updates either succeed or fail -- there are no partial + results. + +* *Single System Image* : + A client will see the same view of the service regardless of + the server that it connects to. i.e., a client will never see an + older view of the system even if the client fails over to a + different server with the same session. + +* *Reliability* : + Once an update has been applied, it will persist from that + time forward until a client overwrites the update. This guarantee + has two corollaries: + 1. If a client gets a successful return code, the update will + have been applied. On some failures (communication errors, + timeouts, etc) the client will not know if the update has + applied or not. We take steps to minimize the failures, but the + guarantee is only present with successful return codes. + (This is called the _monotonicity condition_ in Paxos.) + 1. Any updates that are seen by the client, through a read + request or successful update, will never be rolled back when + recovering from server failures. + +* *Timeliness* : + The clients view of the system is guaranteed to be up-to-date + within a certain time bound (on the order of tens of seconds). + Either system changes will be seen by a client within this bound, or + the client will detect a service outage. + +Using these consistency guarantees it is easy to build higher level +functions such as leader election, barriers, queues, and read/write +revocable locks solely at the ZooKeeper client (no additions needed to +ZooKeeper). See [Recipes and Solutions](recipes.html) +for more details. + +######Note + +>Sometimes developers mistakenly assume one other guarantee that +ZooKeeper does _not_ in fact make. This is: +> * Simultaneously Consistent Cross-Client Views* : + ZooKeeper does not guarantee that at every instance in + time, two different clients will have identical views of + ZooKeeper data. Due to factors like network delays, one client + may perform an update before another client gets notified of the + change. Consider the scenario of two clients, A and B. If client + A sets the value of a znode /a from 0 to 1, then tells client B + to read /a, client B may read the old value of 0, depending on + which server it is connected to. If it + is important that Client A and Client B read the same value, + Client B should call the **sync()** method from the ZooKeeper API + method before it performs its read. + So, ZooKeeper by itself doesn't guarantee that changes occur + synchronously across all servers, but ZooKeeper + primitives can be used to construct higher level functions that + provide useful client synchronization. (For more information, + see the [ZooKeeper Recipes](recipes.html). + + + +## Bindings + +The ZooKeeper client libraries come in two languages: Java and C. +The following sections describe these. + + + +### Java Binding + +There are two packages that make up the ZooKeeper Java binding: +**org.apache.zookeeper** and **org.apache.zookeeper.data**. The rest of the +packages that make up ZooKeeper are used internally or are part of the +server implementation. The **org.apache.zookeeper.data** package is made up of +generated classes that are used simply as containers. + +The main class used by a ZooKeeper Java client is the **ZooKeeper** class. Its two constructors differ only +by an optional session id and password. ZooKeeper supports session +recovery across instances of a process. A Java program may save its +session id and password to stable storage, restart, and recover the +session that was used by the earlier instance of the program. + +When a ZooKeeper object is created, two threads are created as +well: an IO thread and an event thread. All IO happens on the IO thread +(using Java NIO). All event callbacks happen on the event thread. +Session maintenance such as reconnecting to ZooKeeper servers and +maintaining heartbeat is done on the IO thread. Responses for +synchronous methods are also processed in the IO thread. All responses +to asynchronous methods and watch events are processed on the event +thread. There are a few things to notice that result from this +design: + +* All completions for asynchronous calls and watcher callbacks + will be made in order, one at a time. The caller can do any + processing they wish, but no other callbacks will be processed + during that time. +* Callbacks do not block the processing of the IO thread or the + processing of the synchronous calls. +* Synchronous calls may not return in the correct order. For + example, assume a client does the following processing: issues an + asynchronous read of node **/a** with + _watch_ set to true, and then in the completion + callback of the read it does a synchronous read of **/a**. (Maybe not good practice, but not illegal + either, and it makes for a simple example.) + Note that if there is a change to **/a** between the asynchronous read and the + synchronous read, the client library will receive the watch event + saying **/a** changed before the + response for the synchronous read, but because of the completion + callback blocking the event queue, the synchronous read will + return with the new value of **/a** + before the watch event is processed. + +Finally, the rules associated with shutdown are straightforward: +once a ZooKeeper object is closed or receives a fatal event +(SESSION_EXPIRED and AUTH_FAILED), the ZooKeeper object becomes invalid. +On a close, the two threads shut down and any further access on zookeeper +handle is undefined behavior and should be avoided. + + + +#### Client Configuration Parameters + +The following list contains configuration properties for the Java client. You can set any +of these properties using Java system properties. For server properties, please check the +[Server configuration section of the Admin Guide](zookeeperAdmin.html#sc_configuration). +The ZooKeeper Wiki also has useful pages about +[ZooKeeper SSL support](https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeper+SSL+User+Guide), +and [SASL authentication for ZooKeeper](https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeper+and+SASL). + + +* *zookeeper.sasl.client* : + Set the value to **false** to disable + SASL authentication. Default is **true**. + +* *zookeeper.sasl.clientconfig* : + Specifies the context key in the JAAS login file. Default is "Client". + +* *zookeeper.server.principal* : + Specifies the server principal to be used by the client for authentication, while connecting to the zookeeper + server, when Kerberos authentication is enabled. If this configuration is provided, then + the ZooKeeper client will NOT USE any of the following parameters to determine the server principal: + zookeeper.sasl.client.username, zookeeper.sasl.client.canonicalize.hostname, zookeeper.server.realm + Note: this config parameter is working only for ZooKeeper 3.5.7+, 3.6.0+ + +* *zookeeper.sasl.client.username* : + Traditionally, a principal is divided into three parts: the primary, the instance, and the realm. + The format of a typical Kerberos V5 principal is primary/instance@REALM. + zookeeper.sasl.client.username specifies the primary part of the server principal. Default + is "zookeeper". Instance part is derived from the server IP. Finally server's principal is + username/IP@realm, where username is the value of zookeeper.sasl.client.username, IP is + the server IP, and realm is the value of zookeeper.server.realm. + +* *zookeeper.sasl.client.canonicalize.hostname* : + Expecting the zookeeper.server.principal parameter is not provided, the ZooKeeper client will try to + determine the 'instance' (host) part of the ZooKeeper server principal. First it takes the hostname provided + as the ZooKeeper server connection string. Then it tries to 'canonicalize' the address by getting + the fully qualified domain name belonging to the address. You can disable this 'canonicalization' + by setting: zookeeper.sasl.client.canonicalize.hostname=false + +* *zookeeper.server.realm* : + Realm part of the server principal. By default it is the client principal realm. + +* *zookeeper.disableAutoWatchReset* : + This switch controls whether automatic watch resetting is enabled. Clients automatically + reset watches during session reconnect by default, this option allows the client to turn off + this behavior by setting zookeeper.disableAutoWatchReset to **true**. + +* *zookeeper.client.secure* : + **New in 3.5.5:** + If you want to connect to the server secure client port, you need to set this property to + **true** + on the client. This will connect to server using SSL with specified credentials. Note that + it requires the Netty client. + +* *zookeeper.clientCnxnSocket* : + Specifies which ClientCnxnSocket to be used. Possible values are + **org.apache.zookeeper.ClientCnxnSocketNIO** + and + **org.apache.zookeeper.ClientCnxnSocketNetty** + . Default is + **org.apache.zookeeper.ClientCnxnSocketNIO** + . If you want to connect to server's secure client port, you need to set this property to + **org.apache.zookeeper.ClientCnxnSocketNetty** + on client. + +* *zookeeper.ssl.keyStore.location and zookeeper.ssl.keyStore.password* : + **New in 3.5.5:** + Specifies the file path to a JKS containing the local credentials to be used for SSL connections, + and the password to unlock the file. + +* *zookeeper.ssl.trustStore.location and zookeeper.ssl.trustStore.password* : + **New in 3.5.5:** + Specifies the file path to a JKS containing the remote credentials to be used for SSL connections, + and the password to unlock the file. + +* *zookeeper.ssl.keyStore.type* and *zookeeper.ssl.trustStore.type*: + **New in 3.5.5:** + Specifies the file format of keys/trust store files used to establish TLS connection to the ZooKeeper server. + Values: JKS, PEM, PKCS12 or null (detect by filename). Default: null. + **New in 3.6.3, 3.7.0:** + The format BCFKS was added. + +* *jute.maxbuffer* : + In the client side, it specifies the maximum size of the incoming data from the server. The default is 0xfffff(1048575) bytes, + or just under 1M. This is really a sanity check. The ZooKeeper server is designed to store and send + data on the order of kilobytes. If incoming data length is more than this value, an IOException + is raised. This value of client side should keep same with the server side(Setting **System.setProperty("jute.maxbuffer", "xxxx")** in the client side will work), + otherwise problems will arise. + +* *zookeeper.kinit* : + Specifies path to kinit binary. Default is "/usr/bin/kinit". + + + +### C Binding + +The C binding has a single-threaded and multi-threaded library. +The multi-threaded library is easiest to use and is most similar to the +Java API. This library will create an IO thread and an event dispatch +thread for handling connection maintenance and callbacks. The +single-threaded library allows ZooKeeper to be used in event driven +applications by exposing the event loop used in the multi-threaded +library. + +The package includes two shared libraries: zookeeper_st and +zookeeper_mt. The former only provides the asynchronous APIs and +callbacks for integrating into the application's event loop. The only +reason this library exists is to support the platforms were a +_pthread_ library is not available or is unstable +(i.e. FreeBSD 4.x). In all other cases, application developers should +link with zookeeper_mt, as it includes support for both Sync and Async +API. + + + +#### Installation + +If you're building the client from a check-out from the Apache +repository, follow the steps outlined below. If you're building from a +project source package downloaded from apache, skip to step **3**. + +1. Run `ant compile_jute` from the ZooKeeper + top level directory (*.../trunk*). + This will create a directory named "generated" under + *.../trunk/zookeeper-client/zookeeper-client-c*. +1. Change directory to the*.../trunk/zookeeper-client/zookeeper-client-c* + and run `autoreconf -if` to bootstrap **autoconf**, **automake** and **libtool**. Make sure you have **autoconf version 2.59** or greater installed. + Skip to step**4**. +1. If you are building from a project source package, + unzip/untar the source tarball and cd to the* + zookeeper-x.x.x/zookeeper-client/zookeeper-client-c* directory. +1. Run `./configure ` to + generate the makefile. Here are some of options the **configure** utility supports that can be + useful in this step: + * `--enable-debug` + Enables optimization and enables debug info compiler + options. (Disabled by default.) + * `--without-syncapi` + Disables Sync API support; zookeeper_mt library won't be + built. (Enabled by default.) + * `--disable-static` + Do not build static libraries. (Enabled by + default.) + * `--disable-shared` + Do not build shared libraries. (Enabled by + default.) +######Note +>See INSTALL for general information about running **configure**. +1. Run `make` or `make + install` to build the libraries and install them. +1. To generate doxygen documentation for the ZooKeeper API, run + `make doxygen-doc`. All documentation will be + placed in a new subfolder named docs. By default, this command + only generates HTML. For information on other document formats, + run `./configure --help` + + + +#### Building Your Own C Client + +In order to be able to use the ZooKeeper C API in your application +you have to remember to + +1. Include ZooKeeper header: `#include ` +1. If you are building a multithreaded client, compile with + `-DTHREADED` compiler flag to enable the multi-threaded version of + the library, and then link against the + _zookeeper_mt_ library. If you are building a + single-threaded client, do not compile with `-DTHREADED`, and be + sure to link against the_zookeeper_st_library. + +######Note +>See *.../trunk/zookeeper-client/zookeeper-client-c/src/cli.c* +for an example of a C client implementation + + + +## Building Blocks: A Guide to ZooKeeper Operations + +This section surveys all the operations a developer can perform +against a ZooKeeper server. It is lower level information than the earlier +concepts chapters in this manual, but higher level than the ZooKeeper API +Reference. It covers these topics: + +* [Connecting to ZooKeeper](#sc_connectingToZk) + + + +### Handling Errors + +Both the Java and C client bindings may report errors. The Java client binding does so by throwing KeeperException, calling code() on the exception will return the specific error code. The C client binding returns an error code as defined in the enum ZOO_ERRORS. API callbacks indicate result code for both language bindings. See the API documentation (javadoc for Java, doxygen for C) for full details on the possible errors and their meaning. + + + +### Connecting to ZooKeeper + +Before we begin, you will have to set up a running Zookeeper server so that we can start developing the client. For C client bindings, we will be using the multithreaded library(zookeeper_mt) with a simple example written in C. To establish a connection with Zookeeper server, we make use of C API - _zookeeper_init_ with the following signature: + + int zookeeper_init(const char *host, watcher_fn fn, int recv_timeout, const clientid_t *clientid, void *context, int flags); + +* **host* : + Connection string to zookeeper server in the format of host:port. If there are multiple servers, use comma as separator after specifying the host:port pairs. Eg: "127.0.0.1:2181,127.0.0.1:3001,127.0.0.1:3002" + +* *fn* : + Watcher function to process events when a notification is triggered. + +* *recv_timeout* : + Session expiration time in milliseconds. + +* **clientid* : + We can specify 0 for a new session. If a session has already establish previously, we could provide that client ID and it would reconnect to that previous session. + +* **context* : + Context object that can be associated with the zkhandle_t handler. If it is not used, we can set it to 0. + +* *flags* : + In an initiation, we can leave it for 0. + +We will demonstrate client that outputs "Connected to Zookeeper" after successful connection or an error message otherwise. Let's call the following code _zkClient.cc_ : + + + #include + #include + #include + using namespace std; + + // Keeping track of the connection state + static int connected = 0; + static int expired = 0; + + // *zkHandler handles the connection with Zookeeper + static zhandle_t *zkHandler; + + // watcher function would process events + void watcher(zhandle_t *zkH, int type, int state, const char *path, void *watcherCtx) + { + if (type == ZOO_SESSION_EVENT) { + + // state refers to states of zookeeper connection. + // To keep it simple, we would demonstrate these 3: ZOO_EXPIRED_SESSION_STATE, ZOO_CONNECTED_STATE, ZOO_NOTCONNECTED_STATE + // If you are using ACL, you should be aware of an authentication failure state - ZOO_AUTH_FAILED_STATE + if (state == ZOO_CONNECTED_STATE) { + connected = 1; + } else if (state == ZOO_NOTCONNECTED_STATE ) { + connected = 0; + } else if (state == ZOO_EXPIRED_SESSION_STATE) { + expired = 1; + connected = 0; + zookeeper_close(zkH); + } + } + } + + int main(){ + zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); + + // zookeeper_init returns the handler upon a successful connection, null otherwise + zkHandler = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0); + + if (!zkHandler) { + return errno; + }else{ + printf("Connection established with Zookeeper. \n"); + } + + // Close Zookeeper connection + zookeeper_close(zkHandler); + + return 0; + } + + +Compile the code with the multithreaded library mentioned before. + +`> g++ -Iinclude/ zkClient.cpp -lzookeeper_mt -o Client` + +Run the client. + +`> ./Client` + +From the output, you should see "Connected to Zookeeper" along with Zookeeper's DEBUG messages if the connection is successful. + + + +## Gotchas: Common Problems and Troubleshooting + +So now you know ZooKeeper. It's fast, simple, your application +works, but wait ... something's wrong. Here are some pitfalls that +ZooKeeper users fall into: + +1. If you are using watches, you must look for the connected watch + event. When a ZooKeeper client disconnects from a server, you will + not receive notification of changes until reconnected. If you are + watching for a znode to come into existence, you will miss the event + if the znode is created and deleted while you are disconnected. +1. You must test ZooKeeper server failures. The ZooKeeper service + can survive failures as long as a majority of servers are active. The + question to ask is: can your application handle it? In the real world + a client's connection to ZooKeeper can break. (ZooKeeper server + failures and network partitions are common reasons for connection + loss.) The ZooKeeper client library takes care of recovering your + connection and letting you know what happened, but you must make sure + that you recover your state and any outstanding requests that failed. + Find out if you got it right in the test lab, not in production - test + with a ZooKeeper service made up of a several of servers and subject + them to reboots. +1. The list of ZooKeeper servers used by the client must match the + list of ZooKeeper servers that each ZooKeeper server has. Things can + work, although not optimally, if the client list is a subset of the + real list of ZooKeeper servers, but not if the client lists ZooKeeper + servers not in the ZooKeeper cluster. +1. Be careful where you put that transaction log. The most + performance-critical part of ZooKeeper is the transaction log. + ZooKeeper must sync transactions to media before it returns a + response. A dedicated transaction log device is key to consistent good + performance. Putting the log on a busy device will adversely effect + performance. If you only have one storage device, put trace files on + NFS and increase the snapshotCount; it doesn't eliminate the problem, + but it can mitigate it. +1. Set your Java max heap size correctly. It is very important to + _avoid swapping._ Going to disk unnecessarily will + almost certainly degrade your performance unacceptably. Remember, in + ZooKeeper, everything is ordered, so if one request hits the disk, all + other queued requests hit the disk. + To avoid swapping, try to set the heapsize to the amount of + physical memory you have, minus the amount needed by the OS and cache. + The best way to determine an optimal heap size for your configurations + is to _run load tests_. If for some reason you + can't, be conservative in your estimates and choose a number well + below the limit that would cause your machine to swap. For example, on + a 4G machine, a 3G heap is a conservative estimate to start + with. + +## Links to Other Information + +Outside the formal documentation, there're several other sources of +information for ZooKeeper developers. + +* *[API Reference](https://zookeeper.apache.org/doc/current/apidocs/zookeeper-server/index.html)* : + The complete reference to the ZooKeeper API + +* *[ZooKeeper Talk at the Hadoop Summit 2008](https://www.youtube.com/watch?v=rXI9xiesUV8)* : + A video introduction to ZooKeeper, by Benjamin Reed of Yahoo! + Research + +* *[Barrier and Queue Tutorial](https://cwiki.apache.org/confluence/display/ZOOKEEPER/Tutorial)* : + The excellent Java tutorial by Flavio Junqueira, implementing + simple barriers and producer-consumer queues using ZooKeeper. + +* *[ZooKeeper - A Reliable, Scalable Distributed Coordination System](https://cwiki.apache.org/confluence/display/ZOOKEEPER/ZooKeeperArticles)* : + An article by Todd Hoff (07/15/2008) + +* *[ZooKeeper Recipes](recipes.html)* : + Pseudo-level discussion of the implementation of various + synchronization solutions with ZooKeeper: Event Handles, Queues, + Locks, and Two-phase Commits. + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperQuotas.md b/zookeeper-docs/src/main/resources/markdown/zookeeperQuotas.md new file mode 100644 index 0000000..11226c3 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperQuotas.md @@ -0,0 +1,82 @@ + + +# ZooKeeper Quota's Guide + +### A Guide to Deployment and Administration + +* [Quotas](#zookeeper_quotas) + * [Setting Quotas](#Setting+Quotas) + * [Listing Quotas](#Listing+Quotas) + * [Deleting Quotas](#Deleting+Quotas) + + + +## Quotas + +ZooKeeper has both namespace and bytes quotas. You can use the ZooKeeperMain class to setup quotas. +ZooKeeper prints _WARN_ messages if users exceed the quota assigned to them. The messages +are printed in the log of the ZooKeeper. + +Notice: What the `namespace` quota means is the count quota which limits the number of children +under the path(included itself). + + $ bin/zkCli.sh -server host:port** + +The above command gives you a command line option of using quotas. + + + +### Setting Quotas + +- You can use `setquota` to set a quota on a ZooKeeper node. It has an option of setting quota with +`-n` (for namespace/count) and `-b` (for bytes/data length). + +- The ZooKeeper quota is stored in ZooKeeper itself in **/zookeeper/quota**. To disable other people from +changing the quotas, users can set the ACL for **/zookeeper/quota** ,so that only admins are able to read and write to it. + +- If the quota doesn't exist in the specified path,create the quota, otherwise update the quota. + +- The Scope of the quota users set is all the nodes under the path specified (included itself). + +- In order to simplify the calculation of quota in the current directory/hierarchy structure, a complete tree path(from root to leaf node) +can be set only one quota. In the situation when setting a quota in a path which its parent or child node already has a quota. `setquota` will +reject and tell the specified parent or child path, users can adjust allocations of quotas(delete/move-up/move-down the quota) +according to specific circumstances. + +- Combined with the Chroot, the quota will have a better isolation effectiveness between different applications.For example: + + ```bash + # Chroot is: + 192.168.0.1:2181,192.168.0.2:2181,192.168.0.3:2181/apps/app1 + setquota -n 100000 /apps/app1 + ``` + +- Users cannot set the quota on the path under **/zookeeper/quota** + + + +### Listing Quotas + +You can use _listquota_ to list a quota on a ZooKeeper node. + + + +### Deleting Quotas + +You can use _delquota_ to delete quota on a ZooKeeper node. + + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md b/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md new file mode 100644 index 0000000..afb85df --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperReconfig.md @@ -0,0 +1,894 @@ + + +# ZooKeeper Dynamic Reconfiguration + +* [Overview](#ch_reconfig_intro) +* [Changes to Configuration Format](#ch_reconfig_format) + * [Specifying the client port](#sc_reconfig_clientport) + * [Specifying multiple server addresses](#sc_multiaddress) + * [The standaloneEnabled flag](#sc_reconfig_standaloneEnabled) + * [The reconfigEnabled flag](#sc_reconfig_reconfigEnabled) + * [Dynamic configuration file](#sc_reconfig_file) + * [Backward compatibility](#sc_reconfig_backward) +* [Upgrading to 3.5.0](#ch_reconfig_upgrade) +* [Dynamic Reconfiguration of the ZooKeeper Ensemble](#ch_reconfig_dyn) + * [API](#ch_reconfig_api) + * [Security](#sc_reconfig_access_control) + * [Retrieving the current dynamic configuration](#sc_reconfig_retrieving) + * [Modifying the current dynamic configuration](#sc_reconfig_modifying) + * [General](#sc_reconfig_general) + * [Incremental mode](#sc_reconfig_incremental) + * [Non-incremental mode](#sc_reconfig_nonincremental) + * [Conditional reconfig](#sc_reconfig_conditional) + * [Error conditions](#sc_reconfig_errors) + * [Additional comments](#sc_reconfig_additional) +* [Rebalancing Client Connections](#ch_reconfig_rebalancing) + + + +## Overview + +Prior to the 3.5.0 release, the membership and all other configuration +parameters of Zookeeper were static - loaded during boot and immutable at +runtime. Operators resorted to ''rolling restarts'' - a manually intensive +and error-prone method of changing the configuration that has caused data +loss and inconsistency in production. + +Starting with 3.5.0, “rolling restarts” are no longer needed! +ZooKeeper comes with full support for automated configuration changes: the +set of Zookeeper servers, their roles (participant / observer), all ports, +and even the quorum system can be changed dynamically, without service +interruption and while maintaining data consistency. Reconfigurations are +performed immediately, just like other operations in ZooKeeper. Multiple +changes can be done using a single reconfiguration command. The dynamic +reconfiguration functionality does not limit operation concurrency, does +not require client operations to be stopped during reconfigurations, has a +very simple interface for administrators and no added complexity to other +client operations. + +New client-side features allow clients to find out about configuration +changes and to update the connection string (list of servers and their +client ports) stored in their ZooKeeper handle. A probabilistic algorithm +is used to rebalance clients across the new configuration servers while +keeping the extent of client migrations proportional to the change in +ensemble membership. + +This document provides the administrator manual for reconfiguration. +For a detailed description of the reconfiguration algorithms, performance +measurements, and more, please see our paper: + +* *Shraer, A., Reed, B., Malkhi, D., Junqueira, F. Dynamic +Reconfiguration of Primary/Backup Clusters. In _USENIX Annual +Technical Conference (ATC)_(2012), 425-437* : + Links: [paper (pdf)](https://www.usenix.org/system/files/conference/atc12/atc12-final74.pdf), [slides (pdf)](https://www.usenix.org/sites/default/files/conference/protected-files/shraer\_atc12\_slides.pdf), [video](https://www.usenix.org/conference/atc12/technical-sessions/presentation/shraer), [hadoop summit slides](http://www.slideshare.net/Hadoop\_Summit/dynamic-reconfiguration-of-zookeeper) + +**Note:** Starting with 3.5.3, the dynamic reconfiguration +feature is disabled by default, and has to be explicitly turned on via +[reconfigEnabled](zookeeperAdmin.html#sc_advancedConfiguration) configuration option. + + + +## Changes to Configuration Format + + + +### Specifying the client port + +A client port of a server is the port on which the server accepts +client connection requests. Starting with 3.5.0 the +_clientPort_ and _clientPortAddress_ configuration parameters should no longer be used. Instead, +this information is now part of the server keyword specification, which +becomes as follows: + + server. = ::[:role];[:]** + +The client port specification is to the right of the semicolon. The +client port address is optional, and if not specified it defaults to +"0.0.0.0". As usual, role is also optional, it can be +_participant_ or _observer_ +(_participant_ by default). + +Examples of legal server statements: + + server.5 = 125.23.63.23:1234:1235;1236 + server.5 = 125.23.63.23:1234:1235:participant;1236 + server.5 = 125.23.63.23:1234:1235:observer;1236 + server.5 = 125.23.63.23:1234:1235;125.23.63.24:1236 + server.5 = 125.23.63.23:1234:1235:participant;125.23.63.23:1236 + + + + +### Specifying multiple server addresses + +Since ZooKeeper 3.6.0 it is possible to specify multiple addresses for each +ZooKeeper server (see [ZOOKEEPER-3188](https://issues.apache.org/jira/projects/ZOOKEEPER/issues/ZOOKEEPER-3188)). +This helps to increase availability and adds network level +resiliency to ZooKeeper. When multiple physical network interfaces are used +for the servers, ZooKeeper is able to bind on all interfaces and runtime switching +to a working interface in case a network error. The different addresses can be +specified in the config using a pipe ('|') character. + +Examples for a valid configurations using multiple addresses: + + server.2=zoo2-net1:2888:3888|zoo2-net2:2889:3889;2188 + server.2=zoo2-net1:2888:3888|zoo2-net2:2889:3889|zoo2-net3:2890:3890;2188 + server.2=zoo2-net1:2888:3888|zoo2-net2:2889:3889;zoo2-net1:2188 + server.2=zoo2-net1:2888:3888:observer|zoo2-net2:2889:3889:observer;2188 + + + +### The _standaloneEnabled_ flag + +Prior to 3.5.0, one could run ZooKeeper in Standalone mode or in a +Distributed mode. These are separate implementation stacks, and +switching between them during run time is not possible. By default (for +backward compatibility) _standaloneEnabled_ is set to +_true_. The consequence of using this default is that +if started with a single server the ensemble will not be allowed to +grow, and if started with more than one server it will not be allowed to +shrink to contain fewer than two participants. + +Setting the flag to _false_ instructs the system +to run the Distributed software stack even if there is only a single +participant in the ensemble. To achieve this the (static) configuration +file should contain: + + standaloneEnabled=false** + +With this setting it is possible to start a ZooKeeper ensemble +containing a single participant and to dynamically grow it by adding +more servers. Similarly, it is possible to shrink an ensemble so that +just a single participant remains, by removing servers. + +Since running the Distributed mode allows more flexibility, we +recommend setting the flag to _false_. We expect that +the legacy Standalone mode will be deprecated in the future. + + + +### The _reconfigEnabled_ flag + +Starting with 3.5.0 and prior to 3.5.3, there is no way to disable +dynamic reconfiguration feature. We would like to offer the option of +disabling reconfiguration feature because with reconfiguration enabled, +we have a security concern that a malicious actor can make arbitrary changes +to the configuration of a ZooKeeper ensemble, including adding a compromised +server to the ensemble. We prefer to leave to the discretion of the user to +decide whether to enable it or not and make sure that the appropriate security +measure are in place. So in 3.5.3 the [reconfigEnabled](zookeeperAdmin.html#sc_advancedConfiguration) configuration option is introduced +such that the reconfiguration feature can be completely disabled and any attempts +to reconfigure a cluster through reconfig API with or without authentication +will fail by default, unless **reconfigEnabled** is set to +**true**. + +To set the option to true, the configuration file (zoo.cfg) should contain: + + reconfigEnabled=true + + + +### Dynamic configuration file + +Starting with 3.5.0 we're distinguishing between dynamic +configuration parameters, which can be changed during runtime, and +static configuration parameters, which are read from a configuration +file when a server boots and don't change during its execution. For now, +the following configuration keywords are considered part of the dynamic +configuration: _server_, _group_ +and _weight_. + +Dynamic configuration parameters are stored in a separate file on +the server (which we call the dynamic configuration file). This file is +linked from the static config file using the new +_dynamicConfigFile_ keyword. + +**Example** + +#### zoo_replicated1.cfg + + + tickTime=2000 + dataDir=/zookeeper/data/zookeeper1 + initLimit=5 + syncLimit=2 + dynamicConfigFile=/zookeeper/conf/zoo_replicated1.cfg.dynamic + + +#### zoo_replicated1.cfg.dynamic + + + server.1=125.23.63.23:2780:2783:participant;2791 + server.2=125.23.63.24:2781:2784:participant;2792 + server.3=125.23.63.25:2782:2785:participant;2793 + + +When the ensemble configuration changes, the static configuration +parameters remain the same. The dynamic parameters are pushed by +ZooKeeper and overwrite the dynamic configuration files on all servers. +Thus, the dynamic configuration files on the different servers are +usually identical (they can only differ momentarily when a +reconfiguration is in progress, or if a new configuration hasn't +propagated yet to some of the servers). Once created, the dynamic +configuration file should not be manually altered. Changed are only made +through the new reconfiguration commands outlined below. Note that +changing the config of an offline cluster could result in an +inconsistency with respect to configuration information stored in the +ZooKeeper log (and the special configuration znode, populated from the +log) and is therefore highly discouraged. + +**Example 2** + +Users may prefer to initially specify a single configuration file. +The following is thus also legal: + +#### zoo_replicated1.cfg + + + tickTime=2000 + dataDir=/zookeeper/data/zookeeper1 + initLimit=5 + syncLimit=2 + clientPort= + + +The configuration files on each server will be automatically split +into dynamic and static files, if they are not already in this format. +So the configuration file above will be automatically transformed into +the two files in Example 1. Note that the clientPort and +clientPortAddress lines (if specified) will be automatically removed +during this process, if they are redundant (as in the example above). +The original static configuration file is backed up (in a .bak +file). + + + +### Backward compatibility + +We still support the old configuration format. For example, the +following configuration file is acceptable (but not recommended): + +#### zoo_replicated1.cfg + + tickTime=2000 + dataDir=/zookeeper/data/zookeeper1 + initLimit=5 + syncLimit=2 + clientPort=2791 + server.1=125.23.63.23:2780:2783:participant + server.2=125.23.63.24:2781:2784:participant + server.3=125.23.63.25:2782:2785:participant + + +During boot, a dynamic configuration file is created and contains +the dynamic part of the configuration as explained earlier. In this +case, however, the line "clientPort=2791" will remain in the static +configuration file of server 1 since it is not redundant -- it was not +specified as part of the "server.1=..." using the format explained in +the section [Changes to Configuration Format](#ch_reconfig_format). If a reconfiguration +is invoked that sets the client port of server 1, we remove +"clientPort=2791" from the static configuration file (the dynamic file +now contain this information as part of the specification of server +1). + + + +## Upgrading to 3.5.0 + +Upgrading a running ZooKeeper ensemble to 3.5.0 should be done only +after upgrading your ensemble to the 3.4.6 release. Note that this is only +necessary for rolling upgrades (if you're fine with shutting down the +system completely, you don't have to go through 3.4.6). If you attempt a +rolling upgrade without going through 3.4.6 (for example from 3.4.5), you +may get the following error: + + 2013-01-30 11:32:10,663 [myid:2] - INFO [localhost/127.0.0.1:2784:QuorumCnxManager$Listener@498] - Received connection request /127.0.0.1:60876 + 2013-01-30 11:32:10,663 [myid:2] - WARN [localhost/127.0.0.1:2784:QuorumCnxManager@349] - Invalid server id: -65536 + +During a rolling upgrade, each server is taken down in turn and +rebooted with the new 3.5.0 binaries. Before starting the server with +3.5.0 binaries, we highly recommend updating the configuration file so +that all server statements "server.x=..." contain client ports (see the +section [Specifying the client port](#sc_reconfig_clientport)). As explained earlier +you may leave the configuration in a single file, as well as leave the +clientPort/clientPortAddress statements (although if you specify client +ports in the new format, these statements are now redundant). + + + +## Dynamic Reconfiguration of the ZooKeeper Ensemble + +The ZooKeeper Java and C API were extended with getConfig and reconfig +commands that facilitate reconfiguration. Both commands have a synchronous +(blocking) variant and an asynchronous one. We demonstrate these commands +here using the Java CLI, but note that you can similarly use the C CLI or +invoke the commands directly from a program just like any other ZooKeeper +command. + + + +### API + +There are two sets of APIs for both Java and C client. + +* ***Reconfiguration API*** : + Reconfiguration API is used to reconfigure the ZooKeeper cluster. + Starting with 3.5.3, reconfiguration Java APIs are moved into ZooKeeperAdmin class + from ZooKeeper class, and use of this API requires ACL setup and user + authentication (see [Security](#sc_reconfig_access_control) for more information.). + +* ***Get Configuration API*** : + Get configuration APIs are used to retrieve ZooKeeper cluster configuration information + stored in /zookeeper/config znode. Use of this API does not require specific setup or authentication, + because /zookeeper/config is readable to any users. + + + +### Security + +Prior to **3.5.3**, there is no enforced security mechanism +over reconfig so any ZooKeeper clients that can connect to ZooKeeper server ensemble +will have the ability to change the state of a ZooKeeper cluster via reconfig. +It is thus possible for a malicious client to add compromised server to an ensemble, +e.g., add a compromised server, or remove legitimate servers. +Cases like these could be security vulnerabilities on a case by case basis. + +To address this security concern, we introduced access control over reconfig +starting from **3.5.3** such that only a specific set of users +can use reconfig commands or APIs, and these users need be configured explicitly. In addition, +the setup of ZooKeeper cluster must enable authentication so ZooKeeper clients can be authenticated. + +We also provide an escape hatch for users who operate and interact with a ZooKeeper ensemble in a secured +environment (i.e. behind company firewall). For those users who want to use reconfiguration feature but +don't want the overhead of configuring an explicit list of authorized user for reconfig access checks, +they can set ["skipACL"](zookeeperAdmin.html#sc_authOptions) to "yes" which will +skip ACL check and allow any user to reconfigure cluster. + +Overall, ZooKeeper provides flexible configuration options for the reconfigure feature +that allow a user to choose based on user's security requirement. +We leave to the discretion of the user to decide appropriate security measure are in place. + +* ***Access Control*** : + The dynamic configuration is stored in a special znode + ZooDefs.CONFIG_NODE = /zookeeper/config. This node by default is read only + for all users, except super user and users that's explicitly configured for write + access. + Clients that need to use reconfig commands or reconfig API should be configured as users + that have write access to CONFIG_NODE. By default, only the super user has full control including + write access to CONFIG_NODE. Additional users can be granted write access through superuser + by setting an ACL that has write permission associated with specified user. + A few examples of how to setup ACLs and use reconfiguration API with authentication can be found in + ReconfigExceptionTest.java and TestReconfigServer.cc. + +* ***Authentication*** : + Authentication of users is orthogonal to the access control and is delegated to + existing authentication mechanism supported by ZooKeeper's pluggable authentication schemes. + See [ZooKeeper and SASL](https://cwiki.apache.org/confluence/display/ZOOKEEPER/Zookeeper+and+SASL) for more details on this topic. + +* ***Disable ACL check*** : + ZooKeeper supports ["skipACL"](zookeeperAdmin.html#sc_authOptions) option such that ACL + check will be completely skipped, if skipACL is set to "yes". In such cases any unauthenticated + users can use reconfig API. + + + +### Retrieving the current dynamic configuration + +The dynamic configuration is stored in a special znode +ZooDefs.CONFIG_NODE = /zookeeper/config. The new +`config` CLI command reads this znode (currently it is +simply a wrapper to `get /zookeeper/config`). As with +normal reads, to retrieve the latest committed value you should do a +`sync` first. + + [zk: 127.0.0.1:2791(CONNECTED) 3] config + server.1=localhost:2780:2783:participant;localhost:2791 + server.2=localhost:2781:2784:participant;localhost:2792 + server.3=localhost:2782:2785:participant;localhost:2793 + +Notice the last line of the output. This is the configuration +version. The version equals to the zxid of the reconfiguration command +which created this configuration. The version of the first established +configuration equals to the zxid of the NEWLEADER message sent by the +first successfully established leader. When a configuration is written +to a dynamic configuration file, the version automatically becomes part +of the filename and the static configuration file is updated with the +path to the new dynamic configuration file. Configuration files +corresponding to earlier versions are retained for backup +purposes. + +During boot time the version (if it exists) is extracted from the +filename. The version should never be altered manually by users or the +system administrator. It is used by the system to know which +configuration is most up-to-date. Manipulating it manually can result in +data loss and inconsistency. + +Just like a `get` command, the +`config` CLI command accepts the _-w_ +flag for setting a watch on the znode, and _-s_ flag for +displaying the Stats of the znode. It additionally accepts a new flag +_-c_ which outputs only the version and the client +connection string corresponding to the current configuration. For +example, for the configuration above we would get: + + [zk: 127.0.0.1:2791(CONNECTED) 17] config -c + 400000003 localhost:2791,localhost:2793,localhost:2792 + +Note that when using the API directly, this command is called +`getConfig`. + +As any read command it returns the configuration known to the +follower to which your client is connected, which may be slightly +out-of-date. One can use the `sync` command for +stronger guarantees. For example using the Java API: + + zk.sync(ZooDefs.CONFIG_NODE, void_callback, context); + zk.getConfig(watcher, callback, context); + +Note: in 3.5.0 it doesn't really matter which path is passed to the +`sync()` command as all the server's state is brought +up to date with the leader (so one could use a different path instead of +ZooDefs.CONFIG_NODE). However, this may change in the future. + + + +### Modifying the current dynamic configuration + +Modifying the configuration is done through the +`reconfig` command. There are two modes of +reconfiguration: incremental and non-incremental (bulk). The +non-incremental simply specifies the new dynamic configuration of the +system. The incremental specifies changes to the current configuration. +The `reconfig` command returns the new +configuration. + +A few examples are in: *ReconfigTest.java*, +*ReconfigRecoveryTest.java* and +*TestReconfigServer.cc*. + + + +#### General + +**Removing servers:** Any server can +be removed, including the leader (although removing the leader will +result in a short unavailability, see Figures 6 and 8 in the [paper](https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters)). The server will not be shut-down automatically. +Instead, it becomes a "non-voting follower". This is somewhat similar +to an observer in that its votes don't count towards the Quorum of +votes necessary to commit operations. However, unlike a non-voting +follower, an observer doesn't actually see any operation proposals and +does not ACK them. Thus a non-voting follower has a more significant +negative effect on system throughput compared to an observer. +Non-voting follower mode should only be used as a temporary mode, +before shutting the server down, or adding it as a follower or as an +observer to the ensemble. We do not shut the server down automatically +for two main reasons. The first reason is that we do not want all the +clients connected to this server to be immediately disconnected, +causing a flood of connection requests to other servers. Instead, it +is better if each client decides when to migrate independently. The +second reason is that removing a server may sometimes (rarely) be +necessary in order to change it from "observer" to "participant" (this +is explained in the section [Additional comments](#sc_reconfig_additional)). + +Note that the new configuration should have some minimal number of +participants in order to be considered legal. If the proposed change +would leave the cluster with less than 2 participants and standalone +mode is enabled (standaloneEnabled=true, see the section [The _standaloneEnabled_ flag](#sc_reconfig_standaloneEnabled)), the reconfig will not be +processed (BadArgumentsException). If standalone mode is disabled +(standaloneEnabled=false) then it's legal to remain with 1 or more +participants. + +**Adding servers:** Before a +reconfiguration is invoked, the administrator must make sure that a +quorum (majority) of participants from the new configuration are +already connected and synced with the current leader. To achieve this +we need to connect a new joining server to the leader before it is +officially part of the ensemble. This is done by starting the joining +server using an initial list of servers which is technically not a +legal configuration of the system but (a) contains the joiner, and (b) +gives sufficient information to the joiner in order for it to find and +connect to the current leader. We list a few different options of +doing this safely. + +1. Initial configuration of joiners is comprised of servers in + the last committed configuration and one or more joiners, where + **joiners are listed as observers.** + For example, if servers D and E are added at the same time to (A, + B, C) and server C is being removed, the initial configuration of + D could be (A, B, C, D) or (A, B, C, D, E), where D and E are + listed as observers. Similarly, the configuration of E could be + (A, B, C, E) or (A, B, C, D, E), where D and E are listed as + observers. **Note that listing the joiners as + observers will not actually make them observers - it will only + prevent them from accidentally forming a quorum with other + joiners.** Instead, they will contact the servers in the + current configuration and adopt the last committed configuration + (A, B, C), where the joiners are absent. Configuration files of + joiners are backed up and replaced automatically as this happens. + After connecting to the current leader, joiners become non-voting + followers until the system is reconfigured and they are added to + the ensemble (as participant or observer, as appropriate). +1. Initial configuration of each joiner is comprised of servers + in the last committed configuration + **the + joiner itself, listed as a participant.** For example, to + add a new server D to a configuration consisting of servers (A, B, + C), the administrator can start D using an initial configuration + file consisting of servers (A, B, C, D). If both D and E are added + at the same time to (A, B, C), the initial configuration of D + could be (A, B, C, D) and the configuration of E could be (A, B, + C, E). Similarly, if D is added and C is removed at the same time, + the initial configuration of D could be (A, B, C, D). Never list + more than one joiner as participant in the initial configuration + (see warning below). +1. Whether listing the joiner as an observer or as participant, + it is also fine not to list all the current configuration servers, + as long as the current leader is in the list. For example, when + adding D we could start D with a configuration file consisting of + just (A, D) if A is the current leader. however this is more + fragile since if A fails before D officially joins the ensemble, D + doesn’t know anyone else and therefore the administrator will have + to intervene and restart D with another server list. + +######Note +>##### Warning + +>Never specify more than one joining server in the same initial +configuration as participants. Currently, the joining servers don’t +know that they are joining an existing ensemble; if multiple joiners +are listed as participants they may form an independent quorum +creating a split-brain situation such as processing operations +independently from your main ensemble. It is OK to list multiple +joiners as observers in an initial config. + +If the configuration of existing servers changes or they become unavailable +before the joiner succeeds to connect and learn about configuration changes, the +joiner may need to be restarted with an updated configuration file in order to be +able to connect. + +Finally, note that once connected to the leader, a joiner adopts +the last committed configuration, in which it is absent (the initial +config of the joiner is backed up before being rewritten). If the +joiner restarts in this state, it will not be able to boot since it is +absent from its configuration file. In order to start it you’ll once +again have to specify an initial configuration. + +**Modifying server parameters:** One +can modify any of the ports of a server, or its role +(participant/observer) by adding it to the ensemble with different +parameters. This works in both the incremental and the bulk +reconfiguration modes. It is not necessary to remove the server and +then add it back; just specify the new parameters as if the server is +not yet in the system. The server will detect the configuration change +and perform the necessary adjustments. See an example in the section +[Incremental mode](#sc_reconfig_incremental) and an exception to this +rule in the section [Additional comments](#sc_reconfig_additional). + +It is also possible to change the Quorum System used by the +ensemble (for example, change the Majority Quorum System to a +Hierarchical Quorum System on the fly). This, however, is only allowed +using the bulk (non-incremental) reconfiguration mode. In general, +incremental reconfiguration only works with the Majority Quorum +System. Bulk reconfiguration works with both Hierarchical and Majority +Quorum Systems. + +**Performance Impact:** There is +practically no performance impact when removing a follower, since it +is not being automatically shut down (the effect of removal is that +the server's votes are no longer being counted). When adding a server, +there is no leader change and no noticeable performance disruption. +For details and graphs please see Figures 6, 7 and 8 in the [paper](https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters). + +The most significant disruption will happen when a leader change +is caused, in one of the following cases: + +1. Leader is removed from the ensemble. +1. Leader's role is changed from participant to observer. +1. The port used by the leader to send transactions to others + (quorum port) is modified. + +In these cases we perform a leader hand-off where the old leader +nominates a new leader. The resulting unavailability is usually +shorter than when a leader crashes since detecting leader failure is +unnecessary and electing a new leader can usually be avoided during a +hand-off (see Figures 6 and 8 in the [paper](https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters)). + +When the client port of a server is modified, it does not drop +existing client connections. New connections to the server will have +to use the new client port. + +**Progress guarantees:** Up to the +invocation of the reconfig operation, a quorum of the old +configuration is required to be available and connected for ZooKeeper +to be able to make progress. Once reconfig is invoked, a quorum of +both the old and of the new configurations must be available. The +final transition happens once (a) the new configuration is activated, +and (b) all operations scheduled before the new configuration is +activated by the leader are committed. Once (a) and (b) happen, only a +quorum of the new configuration is required. Note, however, that +neither (a) nor (b) are visible to a client. Specifically, when a +reconfiguration operation commits, it only means that an activation +message was sent out by the leader. It does not necessarily mean that +a quorum of the new configuration got this message (which is required +in order to activate it) or that (b) has happened. If one wants to +make sure that both (a) and (b) has already occurred (for example, in +order to know that it is safe to shut down old servers that were +removed), one can simply invoke an update +(`set-data`, or some other quorum operation, but not +a `sync`) and wait for it to commit. An alternative +way to achieve this was to introduce another round to the +reconfiguration protocol (which, for simplicity and compatibility with +Zab, we decided to avoid). + + + +#### Incremental mode + +The incremental mode allows adding and removing servers to the +current configuration. Multiple changes are allowed. For +example: + + > reconfig -remove 3 -add + server.5=125.23.63.23:1234:1235;1236 + +Both the add and the remove options get a list of comma separated +arguments (no spaces): + + > reconfig -remove 3,4 -add + server.5=localhost:2111:2112;2113,6=localhost:2114:2115:observer;2116 + +The format of the server statement is exactly the same as +described in the section [Specifying the client port](#sc_reconfig_clientport) and +includes the client port. Notice that here instead of "server.5=" you +can just say "5=". In the example above, if server 5 is already in the +system, but has different ports or is not an observer, it is updated +and once the configuration commits becomes an observer and starts +using these new ports. This is an easy way to turn participants into +observers and vice versa or change any of their ports, without +rebooting the server. + +ZooKeeper supports two types of Quorum Systems – the simple +Majority system (where the leader commits operations after receiving +ACKs from a majority of voters) and a more complex Hierarchical +system, where votes of different servers have different weights and +servers are divided into voting groups. Currently, incremental +reconfiguration is allowed only if the last proposed configuration +known to the leader uses a Majority Quorum System +(BadArgumentsException is thrown otherwise). + +Incremental mode - examples using the Java API: + + List leavingServers = new ArrayList(); + leavingServers.add("1"); + leavingServers.add("2"); + byte[] config = zk.reconfig(null, leavingServers, null, -1, new Stat()); + + List leavingServers = new ArrayList(); + List joiningServers = new ArrayList(); + leavingServers.add("1"); + joiningServers.add("server.4=localhost:1234:1235;1236"); + byte[] config = zk.reconfig(joiningServers, leavingServers, null, -1, new Stat()); + + String configStr = new String(config); + System.out.println(configStr); + +There is also an asynchronous API, and an API accepting comma +separated Strings instead of List. See +src/java/main/org/apache/zookeeper/ZooKeeper.java. + + + +#### Non-incremental mode + +The second mode of reconfiguration is non-incremental, whereby a +client gives a complete specification of the new dynamic system +configuration. The new configuration can either be given in place or +read from a file: + + > reconfig -file newconfig.cfg + +//newconfig.cfg is a dynamic config file, see [Dynamic configuration file](#sc_reconfig_file) + + > reconfig -members + server.1=125.23.63.23:2780:2783:participant;2791,server.2=125.23.63.24:2781:2784:participant;2792,server.3=125.23.63.25:2782:2785:participant;2793}} + +The new configuration may use a different Quorum System. For +example, you may specify a Hierarchical Quorum System even if the +current ensemble uses a Majority Quorum System. + +Bulk mode - example using the Java API: + + List newMembers = new ArrayList(); + newMembers.add("server.1=1111:1234:1235;1236"); + newMembers.add("server.2=1112:1237:1238;1239"); + newMembers.add("server.3=1114:1240:1241:observer;1242"); + + byte[] config = zk.reconfig(null, null, newMembers, -1, new Stat()); + + String configStr = new String(config); + System.out.println(configStr); + +There is also an asynchronous API, and an API accepting comma +separated String containing the new members instead of +List. See +src/java/main/org/apache/zookeeper/ZooKeeper.java. + + + +#### Conditional reconfig + +Sometimes (especially in non-incremental mode) a new proposed +configuration depends on what the client "believes" to be the current +configuration, and should be applied only to that configuration. +Specifically, the `reconfig` succeeds only if the +last configuration at the leader has the specified version. + + > reconfig -file -v + +In the previously listed Java examples, instead of -1 one could +specify a configuration version to condition the +reconfiguration. + + + +#### Error conditions + +In addition to normal ZooKeeper error conditions, a +reconfiguration may fail for the following reasons: + +1. another reconfig is currently in progress + (ReconfigInProgress) +1. the proposed change would leave the cluster with less than 2 + participants, in case standalone mode is enabled, or, if + standalone mode is disabled then its legal to remain with 1 or + more participants (BadArgumentsException) +1. no quorum of the new configuration was connected and + up-to-date with the leader when the reconfiguration processing + began (NewConfigNoQuorum) +1. `-v x` was specified, but the version +`y` of the latest configuration is not +`x` (BadVersionException) +1. an incremental reconfiguration was requested but the last + configuration at the leader uses a Quorum System which is + different from the Majority system (BadArgumentsException) +1. syntax error (BadArgumentsException) +1. I/O exception when reading the configuration from a file + (BadArgumentsException) + +Most of these are illustrated by test-cases in +*ReconfigFailureCases.java*. + + + +#### Additional comments + +**Liveness:** To better understand +the difference between incremental and non-incremental +reconfiguration, suppose that client C1 adds server D to the system +while a different client C2 adds server E. With the non-incremental +mode, each client would first invoke `config` to find +out the current configuration, and then locally create a new list of +servers by adding its own suggested server. The new configuration can +then be submitted using the non-incremental +`reconfig` command. After both reconfigurations +complete, only one of E or D will be added (not both), depending on +which client's request arrives second to the leader, overwriting the +previous configuration. The other client can repeat the process until +its change takes effect. This method guarantees system-wide progress +(i.e., for one of the clients), but does not ensure that every client +succeeds. To have more control C2 may request to only execute the +reconfiguration in case the version of the current configuration +hasn't changed, as explained in the section [Conditional reconfig](#sc_reconfig_conditional). In this way it may avoid blindly +overwriting the configuration of C1 if C1's configuration reached the +leader first. + +With incremental reconfiguration, both changes will take effect as +they are simply applied by the leader one after the other to the +current configuration, whatever that is (assuming that the second +reconfig request reaches the leader after it sends a commit message +for the first reconfig request -- currently the leader will refuse to +propose a reconfiguration if another one is already pending). Since +both clients are guaranteed to make progress, this method guarantees +stronger liveness. In practice, multiple concurrent reconfigurations +are probably rare. Non-incremental reconfiguration is currently the +only way to dynamically change the Quorum System. Incremental +configuration is currently only allowed with the Majority Quorum +System. + +**Changing an observer into a +follower:** Clearly, changing a server that participates in +voting into an observer may fail if error (2) occurs, i.e., if fewer +than the minimal allowed number of participants would remain. However, +converting an observer into a participant may sometimes fail for a +more subtle reason: Suppose, for example, that the current +configuration is (A, B, C, D), where A is the leader, B and C are +followers and D is an observer. In addition, suppose that B has +crashed. If a reconfiguration is submitted where D is said to become a +follower, it will fail with error (3) since in this configuration, a +majority of voters in the new configuration (any 3 voters), must be +connected and up-to-date with the leader. An observer cannot +acknowledge the history prefix sent during reconfiguration, and +therefore it does not count towards these 3 required servers and the +reconfiguration will be aborted. In case this happens, a client can +achieve the same task by two reconfig commands: first invoke a +reconfig to remove D from the configuration and then invoke a second +command to add it back as a participant (follower). During the +intermediate state D is a non-voting follower and can ACK the state +transfer performed during the second reconfig command. + + + +## Rebalancing Client Connections + +When a ZooKeeper cluster is started, if each client is given the same +connection string (list of servers), the client will randomly choose a +server in the list to connect to, which makes the expected number of +client connections per server the same for each of the servers. We +implemented a method that preserves this property when the set of servers +changes through reconfiguration. See Sections 4 and 5.1 in the [paper](https://www.usenix.org/conference/usenixfederatedconferencesweek/dynamic-recon%EF%AC%81guration-primarybackup-clusters). + +In order for the method to work, all clients must subscribe to +configuration changes (by setting a watch on /zookeeper/config either +directly or through the `getConfig` API command). When +the watch is triggered, the client should read the new configuration by +invoking `sync` and `getConfig` and if +the configuration is indeed new invoke the +`updateServerList` API command. To avoid mass client +migration at the same time, it is better to have each client sleep a +random short period of time before invoking +`updateServerList`. + +A few examples can be found in: +*StaticHostProviderTest.java* and +*TestReconfig.cc* + +Example (this is not a recipe, but a simplified example just to +explain the general idea): + + public void process(WatchedEvent event) { + synchronized (this) { + if (event.getType() == EventType.None) { + connected = (event.getState() == KeeperState.SyncConnected); + notifyAll(); + } else if (event.getPath()!=null && event.getPath().equals(ZooDefs.CONFIG_NODE)) { + // in prod code never block the event thread! + zk.sync(ZooDefs.CONFIG_NODE, this, null); + zk.getConfig(this, this, null); + } + } + } + + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + if (path!=null && path.equals(ZooDefs.CONFIG_NODE)) { + String config[] = ConfigUtils.getClientConfigStr(new String(data)).split(" "); // similar to config -c + long version = Long.parseLong(config[0], 16); + if (this.configVersion == null){ + this.configVersion = version; + } else if (version > this.configVersion) { + hostList = config[1]; + try { + // the following command is not blocking but may cause the client to close the socket and + // migrate to a different server. In practice it's better to wait a short period of time, chosen + // randomly, so that different clients migrate at different times + zk.updateServerList(hostList); + } catch (IOException e) { + System.err.println("Error updating server list"); + e.printStackTrace(); + } + this.configVersion = version; + } + } + } diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperStarted.md b/zookeeper-docs/src/main/resources/markdown/zookeeperStarted.md new file mode 100644 index 0000000..bb1e2f0 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperStarted.md @@ -0,0 +1,376 @@ + + +# ZooKeeper Getting Started Guide + +* [Getting Started: Coordinating Distributed Applications with ZooKeeper](#getting-started-coordinating-distributed-applications-with-zooKeeper) + * [Pre-requisites](#sc_Prerequisites) + * [Download](#sc_Download) + * [Standalone Operation](#sc_InstallingSingleMode) + * [Managing ZooKeeper Storage](#sc_FileManagement) + * [Connecting to ZooKeeper](#sc_ConnectingToZooKeeper) + * [Programming to ZooKeeper](#sc_ProgrammingToZooKeeper) + * [Running Replicated ZooKeeper](#sc_RunningReplicatedZooKeeper) + * [Other Optimizations](#other-optimizations) + + + +## Getting Started: Coordinating Distributed Applications with ZooKeeper + +This document contains information to get you started quickly with +ZooKeeper. It is aimed primarily at developers hoping to try it out, and +contains simple installation instructions for a single ZooKeeper server, a +few commands to verify that it is running, and a simple programming +example. Finally, as a convenience, there are a few sections regarding +more complicated installations, for example running replicated +deployments, and optimizing the transaction log. However for the complete +instructions for commercial deployments, please refer to the [ZooKeeper +Administrator's Guide](zookeeperAdmin.html). + + + +### Pre-requisites + +See [System Requirements](zookeeperAdmin.html#sc_systemReq) in the Admin guide. + + + +### Download + +To get a ZooKeeper distribution, download a recent +[stable](http://zookeeper.apache.org/releases.html) release from one of the Apache Download +Mirrors. + + + +### Standalone Operation + +Setting up a ZooKeeper server in standalone mode is +straightforward. The server is contained in a single JAR file, +so installation consists of creating a configuration. + +Once you've downloaded a stable ZooKeeper release unpack +it and cd to the root + +To start ZooKeeper you need a configuration file. Here is a sample, +create it in **conf/zoo.cfg**: + + + tickTime=2000 + dataDir=/var/lib/zookeeper + clientPort=2181 + + +This file can be called anything, but for the sake of this +discussion call +it **conf/zoo.cfg**. Change the +value of **dataDir** to specify an +existing (empty to start with) directory. Here are the meanings +for each of the fields: + +* ***tickTime*** : + the basic time unit in milliseconds used by ZooKeeper. It is + used to do heartbeats and the minimum session timeout will be + twice the tickTime. + +* ***dataDir*** : + the location to store the in-memory database snapshots and, + unless specified otherwise, the transaction log of updates to the + database. + +* ***clientPort*** : + the port to listen for client connections + +Now that you created the configuration file, you can start +ZooKeeper: + + + bin/zkServer.sh start + + +ZooKeeper logs messages using log4j -- more detail +available in the +[Logging](zookeeperProgrammers.html#Logging) +section of the Programmer's Guide. You will see log messages +coming to the console (default) and/or a log file depending on +the log4j configuration. + +The steps outlined here run ZooKeeper in standalone mode. There is +no replication, so if ZooKeeper process fails, the service will go down. +This is fine for most development situations, but to run ZooKeeper in +replicated mode, please see [Running Replicated +ZooKeeper](#sc_RunningReplicatedZooKeeper). + + + +### Managing ZooKeeper Storage + +For long running production systems ZooKeeper storage must +be managed externally (dataDir and logs). See the section on +[maintenance](zookeeperAdmin.html#sc_maintenance) for +more details. + + + +### Connecting to ZooKeeper + + + $ bin/zkCli.sh -server 127.0.0.1:2181 + + +This lets you perform simple, file-like operations. + +Once you have connected, you should see something like: + + + Connecting to localhost:2181 + log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). + log4j:WARN Please initialize the log4j system properly. + Welcome to ZooKeeper! + JLine support is enabled + [zkshell: 0] + +From the shell, type `help` to get a listing of commands that can be executed from the client, as in: + + + [zkshell: 0] help + ZooKeeper -server host:port cmd args + addauth scheme auth + close + config [-c] [-w] [-s] + connect host:port + create [-s] [-e] [-c] [-t ttl] path [data] [acl] + delete [-v version] path + deleteall path + delquota [-n|-b] path + get [-s] [-w] path + getAcl [-s] path + getAllChildrenNumber path + getEphemerals path + history + listquota path + ls [-s] [-w] [-R] path + ls2 path [watch] + printwatches on|off + quit + reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*] + redo cmdno + removewatches path [-c|-d|-a] [-l] + rmr path + set [-s] [-v version] path data + setAcl [-s] [-v version] [-R] path acl + setquota -n|-b val path + stat [-w] path + sync path + + +From here, you can try a few simple commands to get a feel for this simple command line interface. First, start by issuing the list command, as +in `ls`, yielding: + + + [zkshell: 8] ls / + [zookeeper] + + +Next, create a new znode by running `create /zk_test my_data`. This creates a new znode and associates the string "my_data" with the node. +You should see: + + + [zkshell: 9] create /zk_test my_data + Created /zk_test + + +Issue another `ls /` command to see what the directory looks like: + + + [zkshell: 11] ls / + [zookeeper, zk_test] + + +Notice that the zk_test directory has now been created. + +Next, verify that the data was associated with the znode by running the `get` command, as in: + + + [zkshell: 12] get /zk_test + my_data + cZxid = 5 + ctime = Fri Jun 05 13:57:06 PDT 2009 + mZxid = 5 + mtime = Fri Jun 05 13:57:06 PDT 2009 + pZxid = 5 + cversion = 0 + dataVersion = 0 + aclVersion = 0 + ephemeralOwner = 0 + dataLength = 7 + numChildren = 0 + + +We can change the data associated with zk_test by issuing the `set` command, as in: + + + [zkshell: 14] set /zk_test junk + cZxid = 5 + ctime = Fri Jun 05 13:57:06 PDT 2009 + mZxid = 6 + mtime = Fri Jun 05 14:01:52 PDT 2009 + pZxid = 5 + cversion = 0 + dataVersion = 1 + aclVersion = 0 + ephemeralOwner = 0 + dataLength = 4 + numChildren = 0 + [zkshell: 15] get /zk_test + junk + cZxid = 5 + ctime = Fri Jun 05 13:57:06 PDT 2009 + mZxid = 6 + mtime = Fri Jun 05 14:01:52 PDT 2009 + pZxid = 5 + cversion = 0 + dataVersion = 1 + aclVersion = 0 + ephemeralOwner = 0 + dataLength = 4 + numChildren = 0 + + +(Notice we did a `get` after setting the data and it did, indeed, change. + +Finally, let's `delete` the node by issuing: + + + [zkshell: 16] delete /zk_test + [zkshell: 17] ls / + [zookeeper] + [zkshell: 18] + + +That's it for now. To explore more, see the [Zookeeper CLI](zookeeperCLI.html). + + + +### Programming to ZooKeeper + +ZooKeeper has a Java bindings and C bindings. They are +functionally equivalent. The C bindings exist in two variants: single +threaded and multi-threaded. These differ only in how the messaging loop +is done. For more information, see the [Programming +Examples in the ZooKeeper Programmer's Guide](zookeeperProgrammers.html#ch_programStructureWithExample) for +sample code using the different APIs. + + + +### Running Replicated ZooKeeper + +Running ZooKeeper in standalone mode is convenient for evaluation, +some development, and testing. But in production, you should run +ZooKeeper in replicated mode. A replicated group of servers in the same +application is called a _quorum_, and in replicated +mode, all servers in the quorum have copies of the same configuration +file. + +######Note +>For replicated mode, a minimum of three servers are required, +and it is strongly recommended that you have an odd number of +servers. If you only have two servers, then you are in a +situation where if one of them fails, there are not enough +machines to form a majority quorum. Two servers are inherently +**less** stable than a single server, because there are two single +points of failure. + +The required +**conf/zoo.cfg** +file for replicated mode is similar to the one used in standalone +mode, but with a few differences. Here is an example: + + tickTime=2000 + dataDir=/var/lib/zookeeper + clientPort=2181 + initLimit=5 + syncLimit=2 + server.1=zoo1:2888:3888 + server.2=zoo2:2888:3888 + server.3=zoo3:2888:3888 + +The new entry, **initLimit** is +timeouts ZooKeeper uses to limit the length of time the ZooKeeper +servers in quorum have to connect to a leader. The entry **syncLimit** limits how far out of date a server can +be from a leader. + +With both of these timeouts, you specify the unit of time using +**tickTime**. In this example, the timeout +for initLimit is 5 ticks at 2000 milliseconds a tick, or 10 +seconds. + +The entries of the form _server.X_ list the +servers that make up the ZooKeeper service. When the server starts up, +it knows which server it is by looking for the file +_myid_ in the data directory. That file has the +contains the server number, in ASCII. + +Finally, note the two port numbers after each server +name: " 2888" and "3888". Peers use the former port to connect +to other peers. Such a connection is necessary so that peers +can communicate, for example, to agree upon the order of +updates. More specifically, a ZooKeeper server uses this port +to connect followers to the leader. When a new leader arises, a +follower opens a TCP connection to the leader using this +port. Because the default leader election also uses TCP, we +currently require another port for leader election. This is the +second port in the server entry. + +######Note +>If you want to test multiple servers on a single +machine, specify the servername +as _localhost_ with unique quorum & +leader election ports (i.e. 2888:3888, 2889:3889, 2890:3890 in +the example above) for each server.X in that server's config +file. Of course separate _dataDir_s and +distinct _clientPort_s are also necessary +(in the above replicated example, running on a +single _localhost_, you would still have +three config files). + +>Please be aware that setting up multiple servers on a single +machine will not create any redundancy. If something were to +happen which caused the machine to die, all of the zookeeper +servers would be offline. Full redundancy requires that each +server have its own machine. It must be a completely separate +physical server. Multiple virtual machines on the same physical +host are still vulnerable to the complete failure of that host. + +>If you have multiple network interfaces in your ZooKeeper machines, +you can also instruct ZooKeeper to bind on all of your interfaces and +automatically switch to a healthy interface in case of a network failure. +For details, see the [Configuration Parameters](zookeeperAdmin.html#id_multi_address). + + + +### Other Optimizations + +There are a couple of other configuration parameters that can +greatly increase performance: + +* To get low latencies on updates it is important to + have a dedicated transaction log directory. By default + transaction logs are put in the same directory as the data + snapshots and _myid_ file. The dataLogDir + parameters indicates a different directory to use for the + transaction logs. + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperTools.md b/zookeeper-docs/src/main/resources/markdown/zookeeperTools.md new file mode 100644 index 0000000..ab72375 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperTools.md @@ -0,0 +1,269 @@ + + +# A series of tools for ZooKeeper + +* [Scripts](#Scripts) + * [zkServer.sh](#zkServer) + * [zkCli.sh](#zkCli) + * [zkEnv.sh](#zkEnv) + * [zkCleanup.sh](#zkCleanup) + * [zkTxnLogToolkit.sh](#zkTxnLogToolkit) + * [zkSnapShotToolkit.sh](#zkSnapShotToolkit) + +* [Testing](#Testing) + * [Jepsen Test](#jepsen-test) + + + +## Scripts + + + +### zkServer.sh +A command for the operations for the ZooKeeper server. + +```bash +Usage: ./zkServer.sh {start|start-foreground|stop|version|restart|status|upgrade|print-cmd} +# start the server +./zkServer.sh start + +# start the server in the foreground for debugging +./zkServer.sh start-foreground + +# stop the server +./zkServer.sh stop + +# restart the server +./zkServer.sh restart + +# show the status,mode,role of the server +./zkServer.sh status +JMX enabled by default +Using config: /data/software/zookeeper/conf/zoo.cfg +Mode: standalone + +# Deprecated +./zkServer.sh upgrade + +# print the parameters of the start-up +./zkServer.sh print-cmd + +# show the version of the ZooKeeper server +./zkServer.sh version +Apache ZooKeeper, version 3.6.0-SNAPSHOT 06/11/2019 05:39 GMT + +``` + +The `status` command establishes a client connection to the server to execute diagnostic commands. +When the ZooKeeper cluster is started in client SSL only mode (by omitting the clientPort +from the zoo.cfg), then additional SSL related configuration has to be provided before using +the `./zkServer.sh status` command to find out if the ZooKeeper server is running. An example: + + CLIENT_JVMFLAGS="-Dzookeeper.clientCnxnSocket=org.apache.zookeeper.ClientCnxnSocketNetty -Dzookeeper.ssl.trustStore.location=/tmp/clienttrust.jks -Dzookeeper.ssl.trustStore.password=password -Dzookeeper.ssl.keyStore.location=/tmp/client.jks -Dzookeeper.ssl.keyStore.password=password -Dzookeeper.client.secure=true" ./zkServer.sh status + + + + +### zkCli.sh +Look at the [ZooKeeperCLI](zookeeperCLI.html) + + + +### zkEnv.sh +The environment setting for the ZooKeeper server + +```bash +# the setting of log property +ZOO_LOG_DIR: the directory to store the logs +ZOO_LOG4J_PROP: the level of logs to print +``` + + + +### zkCleanup.sh +Clean up the old snapshots and transaction logs. + +```bash +Usage: + * args dataLogDir [snapDir] -n count + * dataLogDir -- path to the txn log directory + * snapDir -- path to the snapshot directory + * count -- the number of old snaps/logs you want to keep, value should be greater than or equal to 3 +# Keep the latest 5 logs and snapshots +./zkCleanup.sh -n 5 +``` + + + +### zkTxnLogToolkit.sh +TxnLogToolkit is a command line tool shipped with ZooKeeper which +is capable of recovering transaction log entries with broken CRC. + +Running it without any command line parameters or with the `-h,--help` argument, it outputs the following help page: + + $ bin/zkTxnLogToolkit.sh + usage: TxnLogToolkit [-dhrv] txn_log_file_name + -d,--dump Dump mode. Dump all entries of the log file. (this is the default) + -h,--help Print help message + -r,--recover Recovery mode. Re-calculate CRC for broken entries. + -v,--verbose Be verbose in recovery mode: print all entries, not just fixed ones. + -y,--yes Non-interactive mode: repair all CRC errors without asking + +The default behaviour is safe: it dumps the entries of the given +transaction log file to the screen: (same as using `-d,--dump` parameter) + + $ bin/zkTxnLogToolkit.sh log.100000001 + ZooKeeper Transactional Log File with dbid 0 txnlog format version 2 + 4/5/18 2:15:58 PM CEST session 0x16295bafcc40000 cxid 0x0 zxid 0x100000001 createSession 30000 + CRC ERROR - 4/5/18 2:16:05 PM CEST session 0x16295bafcc40000 cxid 0x1 zxid 0x100000002 closeSession null + 4/5/18 2:16:05 PM CEST session 0x16295bafcc40000 cxid 0x1 zxid 0x100000002 closeSession null + 4/5/18 2:16:12 PM CEST session 0x26295bafcc90000 cxid 0x0 zxid 0x100000003 createSession 30000 + 4/5/18 2:17:34 PM CEST session 0x26295bafcc90000 cxid 0x0 zxid 0x200000001 closeSession null + 4/5/18 2:17:34 PM CEST session 0x16295bd23720000 cxid 0x0 zxid 0x200000002 createSession 30000 + 4/5/18 2:18:02 PM CEST session 0x16295bd23720000 cxid 0x2 zxid 0x200000003 create '/andor,#626262,v{s{31,s{'world,'anyone}}},F,1 + EOF reached after 6 txns. + +There's a CRC error in the 2nd entry of the above transaction log file. In **dump** +mode, the toolkit only prints this information to the screen without touching the original file. In +**recovery** mode (`-r,--recover` flag) the original file still remains +untouched and all transactions will be copied over to a new txn log file with ".fixed" suffix. It recalculates +CRC values and copies the calculated value, if it doesn't match the original txn entry. +By default, the tool works interactively: it asks for confirmation whenever CRC error encountered. + + $ bin/zkTxnLogToolkit.sh -r log.100000001 + ZooKeeper Transactional Log File with dbid 0 txnlog format version 2 + CRC ERROR - 4/5/18 2:16:05 PM CEST session 0x16295bafcc40000 cxid 0x1 zxid 0x100000002 closeSession null + Would you like to fix it (Yes/No/Abort) ? + +Answering **Yes** means the newly calculated CRC value will be outputted +to the new file. **No** means that the original CRC value will be copied over. +**Abort** will abort the entire operation and exits. +(In this case the ".fixed" will not be deleted and left in a half-complete state: contains only entries which +have already been processed or only the header if the operation was aborted at the first entry.) + + $ bin/zkTxnLogToolkit.sh -r log.100000001 + ZooKeeper Transactional Log File with dbid 0 txnlog format version 2 + CRC ERROR - 4/5/18 2:16:05 PM CEST session 0x16295bafcc40000 cxid 0x1 zxid 0x100000002 closeSession null + Would you like to fix it (Yes/No/Abort) ? y + EOF reached after 6 txns. + Recovery file log.100000001.fixed has been written with 1 fixed CRC error(s) + +The default behaviour of recovery is to be silent: only entries with CRC error get printed to the screen. +One can turn on verbose mode with the `-v,--verbose` parameter to see all records. +Interactive mode can be turned off with the `-y,--yes` parameter. In this case all CRC errors will be fixed +in the new transaction file. + + + +### zkSnapShotToolkit.sh +Dump a snapshot file to stdout, showing the detailed information of the each zk-node. + +```bash +# help +./zkSnapShotToolkit.sh +/usr/bin/java +USAGE: SnapshotFormatter [-d|-json] snapshot_file + -d dump the data for each znode + -json dump znode info in json format + +# show the each zk-node info without data content +./zkSnapShotToolkit.sh /data/zkdata/version-2/snapshot.fa01000186d +/zk-latencies_4/session_946 + cZxid = 0x00000f0003110b + ctime = Wed Sep 19 21:58:22 CST 2018 + mZxid = 0x00000f0003110b + mtime = Wed Sep 19 21:58:22 CST 2018 + pZxid = 0x00000f0003110b + cversion = 0 + dataVersion = 0 + aclVersion = 0 + ephemeralOwner = 0x00000000000000 + dataLength = 100 + +# [-d] show the each zk-node info with data content +./zkSnapShotToolkit.sh -d /data/zkdata/version-2/snapshot.fa01000186d +/zk-latencies2/session_26229 + cZxid = 0x00000900007ba0 + ctime = Wed Aug 15 20:13:52 CST 2018 + mZxid = 0x00000900007ba0 + mtime = Wed Aug 15 20:13:52 CST 2018 + pZxid = 0x00000900007ba0 + cversion = 0 + dataVersion = 0 + aclVersion = 0 + ephemeralOwner = 0x00000000000000 + data = eHh4eHh4eHh4eHh4eA== + +# [-json] show the each zk-node info with json format +./zkSnapShotToolkit.sh -json /data/zkdata/version-2/snapshot.fa01000186d +[[1,0,{"progname":"SnapshotFormatter.java","progver":"0.01","timestamp":1559788148637},[{"name":"\/","asize":0,"dsize":0,"dev":0,"ino":1001},[{"name":"zookeeper","asize":0,"dsize":0,"dev":0,"ino":1002},{"name":"config","asize":0,"dsize":0,"dev":0,"ino":1003},[{"name":"quota","asize":0,"dsize":0,"dev":0,"ino":1004},[{"name":"test","asize":0,"dsize":0,"dev":0,"ino":1005},{"name":"zookeeper_limits","asize":52,"dsize":52,"dev":0,"ino":1006},{"name":"zookeeper_stats","asize":15,"dsize":15,"dev":0,"ino":1007}]]],{"name":"test","asize":0,"dsize":0,"dev":0,"ino":1008}]] +``` + + + +## Testing + + + +### Jepsen Test +A framework for distributed systems verification, with fault injection. +Jepsen has been used to verify everything from eventually-consistent commutative databases to linearizable coordination systems to distributed task schedulers. +more details can be found in [jepsen-io](https://github.com/jepsen-io/jepsen) + +Running the [Dockerized Jepsen](https://github.com/jepsen-io/jepsen/blob/master/docker/README.md) is the simplest way to use the Jepsen. + +Installation: + +```bash +git clone git@github.com:jepsen-io/jepsen.git +cd docker +# maybe a long time for the first init. +./up.sh +# docker ps to check one control node and five db nodes are up +docker ps + CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES + 8265f1d3f89c docker_control "/bin/sh -c /init.sh" 9 hours ago Up 4 hours 0.0.0.0:32769->8080/tcp jepsen-control + 8a646102da44 docker_n5 "/run.sh" 9 hours ago Up 3 hours 22/tcp jepsen-n5 + 385454d7e520 docker_n1 "/run.sh" 9 hours ago Up 9 hours 22/tcp jepsen-n1 + a62d6a9d5f8e docker_n2 "/run.sh" 9 hours ago Up 9 hours 22/tcp jepsen-n2 + 1485e89d0d9a docker_n3 "/run.sh" 9 hours ago Up 9 hours 22/tcp jepsen-n3 + 27ae01e1a0c5 docker_node "/run.sh" 9 hours ago Up 9 hours 22/tcp jepsen-node + 53c444b00ebd docker_n4 "/run.sh" 9 hours ago Up 9 hours 22/tcp jepsen-n4 +``` + +Running & Test + +```bash +# Enter into the container:jepsen-control +docker exec -it jepsen-control bash +# Test +cd zookeeper && lein run test --concurrency 10 +# See something like the following to assert that ZooKeeper has passed the Jepsen test +INFO [2019-04-01 11:25:23,719] jepsen worker 8 - jepsen.util 8 :ok :read 2 +INFO [2019-04-01 11:25:23,722] jepsen worker 3 - jepsen.util 3 :invoke :cas [0 4] +INFO [2019-04-01 11:25:23,760] jepsen worker 3 - jepsen.util 3 :fail :cas [0 4] +INFO [2019-04-01 11:25:23,791] jepsen worker 1 - jepsen.util 1 :invoke :read nil +INFO [2019-04-01 11:25:23,794] jepsen worker 1 - jepsen.util 1 :ok :read 2 +INFO [2019-04-01 11:25:24,038] jepsen worker 0 - jepsen.util 0 :invoke :write 4 +INFO [2019-04-01 11:25:24,073] jepsen worker 0 - jepsen.util 0 :ok :write 4 +............................................................................... +Everything looks good! ヽ(‘ー`)ノ + +``` + +Reference: +read [this blog](https://aphyr.com/posts/291-call-me-maybe-zookeeper) to learn more about the Jepsen test for the Zookeeper. diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperTutorial.md b/zookeeper-docs/src/main/resources/markdown/zookeeperTutorial.md new file mode 100644 index 0000000..4057a38 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperTutorial.md @@ -0,0 +1,666 @@ + + +# Programming with ZooKeeper - A basic tutorial + +* [Introduction](#ch_Introduction) +* [Barriers](#sc_barriers) +* [Producer-Consumer Queues](#sc_producerConsumerQueues) +* [Complete example](#Complete+example) + * [Queue test](#Queue+test) + * [Barrier test](#Barrier+test) + * [Source Listing](#sc_sourceListing) + + + +## Introduction + +In this tutorial, we show simple implementations of barriers and +producer-consumer queues using ZooKeeper. We call the respective classes Barrier and Queue. +These examples assume that you have at least one ZooKeeper server running. + +Both primitives use the following common excerpt of code: + + static ZooKeeper zk = null; + static Integer mutex; + + String root; + + SyncPrimitive(String address) { + if(zk == null){ + try { + System.out.println("Starting ZK:"); + zk = new ZooKeeper(address, 3000, this); + mutex = new Integer(-1); + System.out.println("Finished starting ZK: " + zk); + } catch (IOException e) { + System.out.println(e.toString()); + zk = null; + } + } + } + + synchronized public void process(WatchedEvent event) { + synchronized (mutex) { + mutex.notify(); + } + } + + + +Both classes extend SyncPrimitive. In this way, we execute steps that are +common to all primitives in the constructor of SyncPrimitive. To keep the examples +simple, we create a ZooKeeper object the first time we instantiate either a barrier +object or a queue object, and we declare a static variable that is a reference +to this object. The subsequent instances of Barrier and Queue check whether a +ZooKeeper object exists. Alternatively, we could have the application creating a +ZooKeeper object and passing it to the constructor of Barrier and Queue. + +We use the process() method to process notifications triggered due to watches. +In the following discussion, we present code that sets watches. A watch is internal +structure that enables ZooKeeper to notify a client of a change to a node. For example, +if a client is waiting for other clients to leave a barrier, then it can set a watch and +wait for modifications to a particular node, which can indicate that it is the end of the wait. +This point becomes clear once we go over the examples. + + + +## Barriers + +A barrier is a primitive that enables a group of processes to synchronize the +beginning and the end of a computation. The general idea of this implementation +is to have a barrier node that serves the purpose of being a parent for individual +process nodes. Suppose that we call the barrier node "/b1". Each process "p" then +creates a node "/b1/p". Once enough processes have created their corresponding +nodes, joined processes can start the computation. + +In this example, each process instantiates a Barrier object, and its constructor takes as parameters: + +* the address of a ZooKeeper server (e.g., "zoo1.foo.com:2181") +* the path of the barrier node on ZooKeeper (e.g., "/b1") +* the size of the group of processes + +The constructor of Barrier passes the address of the Zookeeper server to the +constructor of the parent class. The parent class creates a ZooKeeper instance if +one does not exist. The constructor of Barrier then creates a +barrier node on ZooKeeper, which is the parent node of all process nodes, and +we call root (**Note:** This is not the ZooKeeper root "/"). + + /** + * Barrier constructor + * + * @param address + * @param root + * @param size + */ + Barrier(String address, String root, int size) { + super(address); + this.root = root; + this.size = size; + // Create barrier node + if (zk != null) { + try { + Stat s = zk.exists(root, false); + if (s == null) { + zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + } catch (KeeperException e) { + System.out + .println("Keeper exception when instantiating queue: " + + e.toString()); + } catch (InterruptedException e) { + System.out.println("Interrupted exception"); + } + } + + // My node name + try { + name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString()); + } catch (UnknownHostException e) { + System.out.println(e.toString()); + } + } + + +To enter the barrier, a process calls enter(). The process creates a node under +the root to represent it, using its host name to form the node name. It then wait +until enough processes have entered the barrier. A process does it by checking +the number of children the root node has with "getChildren()", and waiting for +notifications in the case it does not have enough. To receive a notification when +there is a change to the root node, a process has to set a watch, and does it +through the call to "getChildren()". In the code, we have that "getChildren()" +has two parameters. The first one states the node to read from, and the second is +a boolean flag that enables the process to set a watch. In the code the flag is true. + + /** + * Join barrier + * + * @return + * @throws KeeperException + * @throws InterruptedException + */ + + boolean enter() throws KeeperException, InterruptedException{ + zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL_SEQUENTIAL); + while (true) { + synchronized (mutex) { + List list = zk.getChildren(root, true); + + if (list.size() < size) { + mutex.wait(); + } else { + return true; + } + } + } + } + + +Note that enter() throws both KeeperException and InterruptedException, so it is +the responsibility of the application to catch and handle such exceptions. + +Once the computation is finished, a process calls leave() to leave the barrier. +First it deletes its corresponding node, and then it gets the children of the root +node. If there is at least one child, then it waits for a notification (obs: note +that the second parameter of the call to getChildren() is true, meaning that +ZooKeeper has to set a watch on the root node). Upon reception of a notification, +it checks once more whether the root node has any children. + + /** + * Wait until all reach barrier + * + * @return + * @throws KeeperException + * @throws InterruptedException + */ + + boolean leave() throws KeeperException, InterruptedException { + zk.delete(root + "/" + name, 0); + while (true) { + synchronized (mutex) { + List list = zk.getChildren(root, true); + if (list.size() > 0) { + mutex.wait(); + } else { + return true; + } + } + } + } + + + + +## Producer-Consumer Queues + +A producer-consumer queue is a distributed data structure that groups of processes +use to generate and consume items. Producer processes create new elements and add +them to the queue. Consumer processes remove elements from the list, and process them. +In this implementation, the elements are simple integers. The queue is represented +by a root node, and to add an element to the queue, a producer process creates a new node, +a child of the root node. + +The following excerpt of code corresponds to the constructor of the object. As +with Barrier objects, it first calls the constructor of the parent class, SyncPrimitive, +that creates a ZooKeeper object if one doesn't exist. It then verifies if the root +node of the queue exists, and creates if it doesn't. + + /** + * Constructor of producer-consumer queue + * + * @param address + * @param name + */ + Queue(String address, String name) { + super(address); + this.root = name; + // Create ZK node name + if (zk != null) { + try { + Stat s = zk.exists(root, false); + if (s == null) { + zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + } catch (KeeperException e) { + System.out + .println("Keeper exception when instantiating queue: " + + e.toString()); + } catch (InterruptedException e) { + System.out.println("Interrupted exception"); + } + } + } + + +A producer process calls "produce()" to add an element to the queue, and passes +an integer as an argument. To add an element to the queue, the method creates a +new node using "create()", and uses the SEQUENCE flag to instruct ZooKeeper to +append the value of the sequencer counter associated to the root node. In this way, +we impose a total order on the elements of the queue, thus guaranteeing that the +oldest element of the queue is the next one consumed. + + /** + * Add element to the queue. + * + * @param i + * @return + */ + + boolean produce(int i) throws KeeperException, InterruptedException{ + ByteBuffer b = ByteBuffer.allocate(4); + byte[] value; + + // Add child with value i + b.putInt(i); + value = b.array(); + zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + + return true; + } + + +To consume an element, a consumer process obtains the children of the root node, +reads the node with smallest counter value, and returns the element. Note that +if there is a conflict, then one of the two contending processes won't be able to +delete the node and the delete operation will throw an exception. + +A call to getChildren() returns the list of children in lexicographic order. +As lexicographic order does not necessarily follow the numerical order of the counter +values, we need to decide which element is the smallest. To decide which one has +the smallest counter value, we traverse the list, and remove the prefix "element" +from each one. + + /** + * Remove first element from the queue. + * + * @return + * @throws KeeperException + * @throws InterruptedException + */ + int consume() throws KeeperException, InterruptedException{ + int retvalue = -1; + Stat stat = null; + + // Get the first element available + while (true) { + synchronized (mutex) { + List list = zk.getChildren(root, true); + if (list.size() == 0) { + System.out.println("Going to wait"); + mutex.wait(); + } else { + Integer min = new Integer(list.get(0).substring(7)); + for(String s : list){ + Integer tempValue = new Integer(s.substring(7)); + //System.out.println("Temporary value: " + tempValue); + if(tempValue < min) min = tempValue; + } + System.out.println("Temporary value: " + root + "/element" + min); + byte[] b = zk.getData(root + "/element" + min, + false, stat); + zk.delete(root + "/element" + min, 0); + ByteBuffer buffer = ByteBuffer.wrap(b); + retvalue = buffer.getInt(); + + return retvalue; + } + } + } + } + } + + + + +## Complete example + +In the following section you can find a complete command line application to demonstrate the above mentioned +recipes. Use the following command to run it. + + ZOOBINDIR="[path_to_distro]/bin" + . "$ZOOBINDIR"/zkEnv.sh + java SyncPrimitive [Test Type] [ZK server] [No of elements] [Client type] + + + +### Queue test + +Start a producer to create 100 elements + + java SyncPrimitive qTest localhost 100 p + + +Start a consumer to consume 100 elements + + java SyncPrimitive qTest localhost 100 c + + + +### Barrier test + +Start a barrier with 2 participants (start as many times as many participants you'd like to enter) + + java SyncPrimitive bTest localhost 2 + + + +### Source Listing + +#### SyncPrimitive.Java + + import java.io.IOException; + import java.net.InetAddress; + import java.net.UnknownHostException; + import java.nio.ByteBuffer; + import java.util.List; + import java.util.Random; + + import org.apache.zookeeper.CreateMode; + import org.apache.zookeeper.KeeperException; + import org.apache.zookeeper.WatchedEvent; + import org.apache.zookeeper.Watcher; + import org.apache.zookeeper.ZooKeeper; + import org.apache.zookeeper.ZooDefs.Ids; + import org.apache.zookeeper.data.Stat; + + public class SyncPrimitive implements Watcher { + + static ZooKeeper zk = null; + static Integer mutex; + String root; + + SyncPrimitive(String address) { + if(zk == null){ + try { + System.out.println("Starting ZK:"); + zk = new ZooKeeper(address, 3000, this); + mutex = new Integer(-1); + System.out.println("Finished starting ZK: " + zk); + } catch (IOException e) { + System.out.println(e.toString()); + zk = null; + } + } + //else mutex = new Integer(-1); + } + + synchronized public void process(WatchedEvent event) { + synchronized (mutex) { + //System.out.println("Process: " + event.getType()); + mutex.notify(); + } + } + + /** + * Barrier + */ + static public class Barrier extends SyncPrimitive { + int size; + String name; + + /** + * Barrier constructor + * + * @param address + * @param root + * @param size + */ + Barrier(String address, String root, int size) { + super(address); + this.root = root; + this.size = size; + + // Create barrier node + if (zk != null) { + try { + Stat s = zk.exists(root, false); + if (s == null) { + zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + } catch (KeeperException e) { + System.out + .println("Keeper exception when instantiating queue: " + + e.toString()); + } catch (InterruptedException e) { + System.out.println("Interrupted exception"); + } + } + + // My node name + try { + name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString()); + } catch (UnknownHostException e) { + System.out.println(e.toString()); + } + + } + + /** + * Join barrier + * + * @return + * @throws KeeperException + * @throws InterruptedException + */ + + boolean enter() throws KeeperException, InterruptedException{ + zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL_SEQUENTIAL); + while (true) { + synchronized (mutex) { + List list = zk.getChildren(root, true); + + if (list.size() < size) { + mutex.wait(); + } else { + return true; + } + } + } + } + + /** + * Wait until all reach barrier + * + * @return + * @throws KeeperException + * @throws InterruptedException + */ + boolean leave() throws KeeperException, InterruptedException{ + zk.delete(root + "/" + name, 0); + while (true) { + synchronized (mutex) { + List list = zk.getChildren(root, true); + if (list.size() > 0) { + mutex.wait(); + } else { + return true; + } + } + } + } + } + + /** + * Producer-Consumer queue + */ + static public class Queue extends SyncPrimitive { + + /** + * Constructor of producer-consumer queue + * + * @param address + * @param name + */ + Queue(String address, String name) { + super(address); + this.root = name; + // Create ZK node name + if (zk != null) { + try { + Stat s = zk.exists(root, false); + if (s == null) { + zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + } catch (KeeperException e) { + System.out + .println("Keeper exception when instantiating queue: " + + e.toString()); + } catch (InterruptedException e) { + System.out.println("Interrupted exception"); + } + } + } + + /** + * Add element to the queue. + * + * @param i + * @return + */ + + boolean produce(int i) throws KeeperException, InterruptedException{ + ByteBuffer b = ByteBuffer.allocate(4); + byte[] value; + + // Add child with value i + b.putInt(i); + value = b.array(); + zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL); + + return true; + } + + /** + * Remove first element from the queue. + * + * @return + * @throws KeeperException + * @throws InterruptedException + */ + int consume() throws KeeperException, InterruptedException{ + int retvalue = -1; + Stat stat = null; + + // Get the first element available + while (true) { + synchronized (mutex) { + List list = zk.getChildren(root, true); + if (list.size() == 0) { + System.out.println("Going to wait"); + mutex.wait(); + } else { + Integer min = new Integer(list.get(0).substring(7)); + String minNode = list.get(0); + for(String s : list){ + Integer tempValue = new Integer(s.substring(7)); + //System.out.println("Temporary value: " + tempValue); + if(tempValue < min) { + min = tempValue; + minNode = s; + } + } + System.out.println("Temporary value: " + root + "/" + minNode); + byte[] b = zk.getData(root + "/" + minNode, + false, stat); + zk.delete(root + "/" + minNode, 0); + ByteBuffer buffer = ByteBuffer.wrap(b); + retvalue = buffer.getInt(); + + return retvalue; + } + } + } + } + } + + public static void main(String args[]) { + if (args[0].equals("qTest")) + queueTest(args); + else + barrierTest(args); + } + + public static void queueTest(String args[]) { + Queue q = new Queue(args[1], "/app1"); + + System.out.println("Input: " + args[1]); + int i; + Integer max = new Integer(args[2]); + + if (args[3].equals("p")) { + System.out.println("Producer"); + for (i = 0; i < max; i++) + try{ + q.produce(10 + i); + } catch (KeeperException e){ + + } catch (InterruptedException e){ + + } + } else { + System.out.println("Consumer"); + + for (i = 0; i < max; i++) { + try{ + int r = q.consume(); + System.out.println("Item: " + r); + } catch (KeeperException e){ + i--; + } catch (InterruptedException e){ + } + } + } + } + + public static void barrierTest(String args[]) { + Barrier b = new Barrier(args[1], "/b1", new Integer(args[2])); + try{ + boolean flag = b.enter(); + System.out.println("Entered barrier: " + args[2]); + if(!flag) System.out.println("Error when entering the barrier"); + } catch (KeeperException e){ + } catch (InterruptedException e){ + } + + // Generate random integer + Random rand = new Random(); + int r = rand.nextInt(100); + // Loop for rand iterations + for (int i = 0; i < r; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + try{ + b.leave(); + } catch (KeeperException e){ + + } catch (InterruptedException e){ + + } + System.out.println("Left barrier"); + } + } + diff --git a/zookeeper-docs/src/main/resources/markdown/zookeeperUseCases.md b/zookeeper-docs/src/main/resources/markdown/zookeeperUseCases.md new file mode 100644 index 0000000..bd29ee6 --- /dev/null +++ b/zookeeper-docs/src/main/resources/markdown/zookeeperUseCases.md @@ -0,0 +1,385 @@ + + +# ZooKeeper Use Cases + +- Applications and organizations using ZooKeeper include (alphabetically) [1]. +- If your use case wants to be listed here. Please do not hesitate, submit a pull request or write an email to **dev@zookeeper.apache.org**, + and then, your use case will be included. +- If this documentation has violated your intellectual property rights or you and your company's privacy, write an email to **dev@zookeeper.apache.org**, + we will handle them in a timely manner. + + +## Free Software Projects + +### [AdroitLogic UltraESB](http://adroitlogic.org/) + - Uses ZooKeeper to implement node coordination, in clustering support. This allows the management of the complete cluster, + or any specific node - from any other node connected via JMX. A Cluster wide command framework developed on top of the + ZooKeeper coordination allows commands that fail on some nodes to be retried etc. We also support the automated graceful + round-robin-restart of a complete cluster of nodes using the same framework [1]. + +### [Akka](http://akka.io/) + - Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM. + Or: Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM [1]. + +### [Eclipse Communication Framework](http://www.eclipse.org/ecf) + - The Eclipse ECF project provides an implementation of its Abstract Discovery services using Zookeeper. ECF itself + is used in many projects providing base functionallity for communication, all based on OSGi [1]. + +### [Eclipse Gyrex](http://www.eclipse.org/gyrex) + - The Eclipse Gyrex project provides a platform for building your own Java OSGi based clouds. + - ZooKeeper is used as the core cloud component for node membership and management, coordination of jobs executing among workers, + a lock service and a simple queue service and a lot more [1]. + +### [GoldenOrb](http://www.goldenorbos.org/) + - massive-scale Graph analysis [1]. + +### [Juju](https://juju.ubuntu.com/) + - Service deployment and orchestration framework, formerly called Ensemble [1]. + +### [Katta](http://katta.sourceforge.net/) + - Katta serves distributed Lucene indexes in a grid environment. + - Zookeeper is used for node, master and index management in the grid [1]. + +### [KeptCollections](https://github.com/anthonyu/KeptCollections) + - KeptCollections is a library of drop-in replacements for the data structures in the Java Collections framework. + - KeptCollections uses Apache ZooKeeper as a backing store, thus making its data structures distributed and scalable [1]. + +### [Neo4j](https://neo4j.com/) + - Neo4j is a Graph Database. It's a disk based, ACID compliant transactional storage engine for big graphs and fast graph traversals, + using external indicies like Lucene/Solr for global searches. + - We use ZooKeeper in the Neo4j High Availability components for write-master election, + read slave coordination and other cool stuff. ZooKeeper is a great and focused project - we like! [1]. + +### [Norbert](http://sna-projects.com/norbert) + - Partitioned routing and cluster management [1]. + +### [spring-cloud-zookeeper](https://spring.io/projects/spring-cloud-zookeeper) + - Spring Cloud Zookeeper provides Apache Zookeeper integrations for Spring Boot apps through autoconfiguration + and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations + you can quickly enable and configure the common patterns inside your application and build large distributed systems with Zookeeper. + The patterns provided include Service Discovery and Distributed Configuration [38]. + +### [spring-statemachine](https://projects.spring.io/spring-statemachine/) + - Spring Statemachine is a framework for application developers to use state machine concepts with Spring applications. + - Spring Statemachine can provide this feature:Distributed state machine based on a Zookeeper [31,32]. + +### [spring-xd](https://projects.spring.io/spring-xd/) + - Spring XD is a unified, distributed, and extensible system for data ingestion, real time analytics, batch processing, and data export. + The project’s goal is to simplify the development of big data applications. + - ZooKeeper - Provides all runtime information for the XD cluster. Tracks running containers, in which containers modules + and jobs are deployed, stream definitions, deployment manifests, and the like [30,31]. + +### [Talend ESB](http://www.talend.com/products-application-integration/application-integration-esb-se.php) + - Talend ESB is a versatile and flexible, enterprise service bus. + - It uses ZooKeeper as endpoint repository of both REST and SOAP Web services. + By using ZooKeeper Talend ESB is able to provide failover and load balancing capabilities in a very light-weight manner [1]. + +### [redis_failover](https://github.com/ryanlecompte/redis_failover) + - Redis Failover is a ZooKeeper-based automatic master/slave failover solution for Ruby [1]. + + +## Apache Projects + +### [Apache Accumulo](https://accumulo.apache.org/) + - Accumulo is a distributed key/value store that provides expressive, cell-level access labels. + - Apache ZooKeeper plays a central role within the Accumulo architecture. Its quorum consistency model supports an overall + Accumulo architecture with no single points of failure. Beyond that, Accumulo leverages ZooKeeper to store and communication + configuration information for users and tables, as well as operational states of processes and tablets [2]. + +### [Apache Atlas](http://atlas.apache.org) + - Atlas is a scalable and extensible set of core foundational governance services – enabling enterprises to effectively and efficiently meet + their compliance requirements within Hadoop and allows integration with the whole enterprise data ecosystem. + - Atlas uses Zookeeper for coordination to provide redundancy and high availability of HBase,Kafka [31,35]. + +### [Apache BookKeeper](https://bookkeeper.apache.org/) + - A scalable, fault-tolerant, and low-latency storage service optimized for real-time workloads. + - BookKeeper requires a metadata storage service to store information related to ledgers and available bookies. BookKeeper currently uses + ZooKeeper for this and other tasks [3]. + +### [Apache CXF DOSGi](http://cxf.apache.org/distributed-osgi.html) + - Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming + APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, + or CORBA and work over a variety of transports such as HTTP, JMS or JBI. + - The Distributed OSGi implementation at Apache CXF uses ZooKeeper for its Discovery functionality [4]. + +### [Apache Drill](http://drill.apache.org/) + - Schema-free SQL Query Engine for Hadoop, NoSQL and Cloud Storage + - ZooKeeper maintains ephemeral cluster membership information. The Drillbits use ZooKeeper to find other Drillbits in the cluster, + and the client uses ZooKeeper to find Drillbits to submit a query [28]. + +### [Apache Druid(Incubating)](https://druid.apache.org/) + - Apache Druid (incubating) is a high performance real-time analytics database. + - Apache Druid (incubating) uses Apache ZooKeeper (ZK) for management of current cluster state. The operations that happen over ZK are [27]: + - Coordinator leader election + - Segment "publishing" protocol from Historical and Realtime + - Segment load/drop protocol between Coordinator and Historical + - Overlord leader election + - Overlord and MiddleManager task management + +### [Apache Dubbo](http://dubbo.apache.org) + - Apache Dubbo is a high-performance, java based open source RPC framework. + - Zookeeper is used for service registration discovery and configuration management in Dubbo [6]. + +### [Apache Flink](https://flink.apache.org/) + - Apache Flink is a framework and distributed processing engine for stateful computations over unbounded and bounded data streams. + Flink has been designed to run in all common cluster environments, perform computations at in-memory speed and at any scale. + - To enable JobManager High Availability you have to set the high-availability mode to zookeeper, configure a ZooKeeper quorum and set up a masters file with all JobManagers hosts and their web UI ports. + Flink leverages ZooKeeper for distributed coordination between all running JobManager instances. ZooKeeper is a separate service from Flink, + which provides highly reliable distributed coordination via leader election and light-weight consistent state storage [23]. + +### [Apache Flume](https://flume.apache.org/) + - Flume is a distributed, reliable, and available service for efficiently collecting, aggregating, and moving large amounts + of log data. It has a simple and flexible architecture based on streaming data flows. It is robust and fault tolerant + with tunable reliability mechanisms and many failover and recovery mechanisms. It uses a simple extensible data model + that allows for online analytic application. + - Flume supports Agent configurations via Zookeeper. This is an experimental feature [5]. + +### [Apache Fluo](https://fluo.apache.org/) + - Apache Fluo is a distributed processing system that lets users make incremental updates to large data sets. + - Apache Fluo is built on Apache Accumulo which uses Apache Zookeeper for consensus [31,37]. + +### [Apache Griffin](https://griffin.apache.org/) + - Big Data Quality Solution For Batch and Streaming. + - Griffin uses Zookeeper for coordination to provide redundancy and high availability of Kafka [31,36]. + +### [Apache Hadoop](http://hadoop.apache.org/) + - The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across + clusters of computers using simple programming models. It is designed to scale up from single servers to thousands of machines, + each offering local computation and storage. Rather than rely on hardware to deliver high-availability, + the library itself is designed to detect and handle failures at the application layer, so delivering a highly-available service on top of a cluster of computers, each of which may be prone to failures. + - The implementation of automatic HDFS failover relies on ZooKeeper for the following things: + - **Failure detection** - each of the NameNode machines in the cluster maintains a persistent session in ZooKeeper. + If the machine crashes, the ZooKeeper session will expire, notifying the other NameNode that a failover should be triggered. + - **Active NameNode election** - ZooKeeper provides a simple mechanism to exclusively elect a node as active. If the current active NameNode crashes, + another node may take a special exclusive lock in ZooKeeper indicating that it should become the next active. + - The ZKFailoverController (ZKFC) is a new component which is a ZooKeeper client which also monitors and manages the state of the NameNode. + Each of the machines which runs a NameNode also runs a ZKFC, and that ZKFC is responsible for: + - **Health monitoring** - the ZKFC pings its local NameNode on a periodic basis with a health-check command. + So long as the NameNode responds in a timely fashion with a healthy status, the ZKFC considers the node healthy. + If the node has crashed, frozen, or otherwise entered an unhealthy state, the health monitor will mark it as unhealthy. + - **ZooKeeper session management** - when the local NameNode is healthy, the ZKFC holds a session open in ZooKeeper. + If the local NameNode is active, it also holds a special “lock” znode. This lock uses ZooKeeper’s support for “ephemeral” nodes; + if the session expires, the lock node will be automatically deleted. + - **ZooKeeper-based election** - if the local NameNode is healthy, and the ZKFC sees that no other node currently holds the lock znode, + it will itself try to acquire the lock. If it succeeds, then it has “won the election”, and is responsible for running a failover to make its local NameNode active. + The failover process is similar to the manual failover described above: first, the previous active is fenced if necessary, + and then the local NameNode transitions to active state [7]. + +### [Apache HBase](https://hbase.apache.org/) + - HBase is the Hadoop database. It's an open-source, distributed, column-oriented store model. + - HBase uses ZooKeeper for master election, server lease management, bootstrapping, and coordination between servers. + A distributed Apache HBase installation depends on a running ZooKeeper cluster. All participating nodes and clients + need to be able to access the running ZooKeeper ensemble [8]. + - As you can see, ZooKeeper is a fundamental part of HBase. All operations that require coordination, such as Regions + assignment, Master-Failover, replication, and snapshots, are built on ZooKeeper [20]. + +### [Apache Helix](http://helix.apache.org/) + - A cluster management framework for partitioned and replicated distributed resources. + - We need a distributed store to maintain the state of the cluster and a notification system to notify if there is any change in the cluster state. + Helix uses Apache ZooKeeper to achieve this functionality [21]. + Zookeeper provides: + - A way to represent PERSISTENT state which remains until its deleted + - A way to represent TRANSIENT/EPHEMERAL state which vanishes when the process that created the state dies + - A notification mechanism when there is a change in PERSISTENT and EPHEMERAL state + +### [Apache Hive](https://hive.apache.org) + - The Apache Hive data warehouse software facilitates reading, writing, and managing large datasets residing in distributed + storage using SQL. Structure can be projected onto data already in storage. A command line tool and JDBC driver are provided to connect users to Hive. + - Hive has been using ZooKeeper as distributed lock manager to support concurrency in HiveServer2 [25,26]. + +### [Apache Ignite](https://ignite.apache.org/) + - Ignite is a memory-centric distributed database, caching, and processing platform for + transactional, analytical, and streaming workloads delivering in-memory speeds at petabyte scale + - Apache Ignite discovery mechanism goes with a ZooKeeper implementations which allows scaling Ignite clusters to 100s and 1000s of nodes + preserving linear scalability and performance [31,34].​ + +### [Apache James Mailbox](http://james.apache.org/mailbox/) + - The Apache James Mailbox is a library providing a flexible Mailbox storage accessible by mail protocols + (IMAP4, POP3, SMTP,...) and other protocols. + - Uses Zookeeper and Curator Framework for generating distributed unique ID's [31]. + +### [Apache Kafka](https://kafka.apache.org/) + - Kafka is a distributed publish/subscribe messaging system + - Apache Kafka relies on ZooKeeper for the following things: + - **Controller election** + The controller is one of the most important broking entity in a Kafka ecosystem, and it also has the responsibility + to maintain the leader-follower relationship across all the partitions. If a node by some reason is shutting down, + it’s the controller’s responsibility to tell all the replicas to act as partition leaders in order to fulfill the + duties of the partition leaders on the node that is about to fail. So, whenever a node shuts down, a new controller + can be elected and it can also be made sure that at any given time, there is only one controller and all the follower nodes have agreed on that. + - **Configuration Of Topics** + The configuration regarding all the topics including the list of existing topics, the number of partitions for each topic, + the location of all the replicas, list of configuration overrides for all topics and which node is the preferred leader, etc. + - **Access control lists** + Access control lists or ACLs for all the topics are also maintained within Zookeeper. + - **Membership of the cluster** + Zookeeper also maintains a list of all the brokers that are functioning at any given moment and are a part of the cluster [9]. + +### [Apache Kylin](http://kylin.apache.org/) + - Apache Kylin is an open source Distributed Analytics Engine designed to provide SQL interface and multi-dimensional analysis (OLAP) on Hadoop/Spark supporting extremely large datasets, + original contributed from eBay Inc. + - Apache Kylin leverages Zookeeper for job coordination [31,33]. + +### [Apache Mesos](http://mesos.apache.org/) + - Apache Mesos abstracts CPU, memory, storage, and other compute resources away from machines (physical or virtual), + enabling fault-tolerant and elastic distributed systems to easily be built and run effectively. + - Mesos has a high-availability mode that uses multiple Mesos masters: one active master (called the leader or leading master) + and several backups in case it fails. The masters elect the leader, with Apache ZooKeeper both coordinating the election + and handling leader detection by masters, agents, and scheduler drivers [10]. + +### [Apache Oozie](https://oozie.apache.org) + - Oozie is a workflow scheduler system to manage Apache Hadoop jobs. + - the Oozie servers use it for coordinating access to the database and communicating with each other. In order to have full HA, + there should be at least 3 ZooKeeper servers [29]. + +### [Apache Pulsar](https://pulsar.apache.org) + - Apache Pulsar is an open-source distributed pub-sub messaging system originally created at Yahoo and now part of the Apache Software Foundation + - Pulsar uses Apache Zookeeper for metadata storage, cluster configuration, and coordination. In a Pulsar instance: + - A configuration store quorum stores configuration for tenants, namespaces, and other entities that need to be globally consistent. + - Each cluster has its own local ZooKeeper ensemble that stores cluster-specific configuration and coordination such as ownership metadata, + broker load reports, BookKeeper ledger metadata, and more [24]. + +### [Apache Solr](https://lucene.apache.org/solr/) + - Solr is the popular, blazing-fast, open source enterprise search platform built on Apache Lucene. + - In the "Cloud" edition (v4.x and up) of enterprise search engine Apache Solr, ZooKeeper is used for configuration, + leader election and more [12,13]. + +### [Apache Spark](https://spark.apache.org/) + - Apache Spark is a unified analytics engine for large-scale data processing. + - Utilizing ZooKeeper to provide leader election and some state storage, you can launch multiple Masters in your cluster connected to the same ZooKeeper instance. + One will be elected “leader” and the others will remain in standby mode. If the current leader dies, another Master will be elected, + recover the old Master’s state, and then resume scheduling [14]. + +### [Apache Storm](http://storm.apache.org) + - Apache Storm is a free and open source distributed realtime computation system. Apache Storm makes it easy to reliably + process unbounded streams of data, doing for realtime processing what Hadoop did for batch processing. + Apache Storm is simple, can be used with any programming language, and is a lot of fun to use! + - Storm uses Zookeeper for coordinating the cluster [22]. + + +## Companies + +### [AGETO](http://www.ageto.de/) + - The AGETO RnD team uses ZooKeeper in a variety of internal as well as external consulting projects [1]. + +### [Benipal Technologies](http://www.benipaltechnologies.com/) + - ZooKeeper is used for internal application development with Solr and Hadoop with Hbase [1]. + +### [Box](http://box.net/) + - Box uses ZooKeeper for service discovery, service coordination, Solr and Hadoop support, etc [1]. + +### [Deepdyve](http://www.deepdyve.com/) + - We do search for research and provide access to high quality content using advanced search technologies Zookeeper is used to + manage server state, control index deployment and a myriad other tasks [1]. + +### [Facebook](https://www.facebook.com/) + - Facebook uses the Zeus ([17,18]) for configuration management which is a forked version of ZooKeeper, with many scalability + and performance en- hancements in order to work at the Facebook scale. + It runs a consensus protocol among servers distributed across mul- tiple regions for resilience. If the leader fails, + a follower is converted into a new leader. + +### [Idium Portal](http://www.idium.no/no/idium_portal/) + - Idium Portal is a hosted web-publishing system delivered by Norwegian company, Idium AS. + - ZooKeeper is used for cluster messaging, service bootstrapping, and service coordination [1]. + +### [Makara](http://www.makara.com/) + - Using ZooKeeper on 2-node cluster on VMware workstation, Amazon EC2, Zen + - Using zkpython + - Looking into expanding into 100 node cluster [1]. + +### [Midokura](http://www.midokura.com/) + - We do virtualized networking for the cloud computing era. We use ZooKeeper for various aspects of our distributed control plane [1]. + +### [Pinterest](https://www.pinterest.com/) + - Pinterest uses the ZooKeeper for Service discovery and dynamic configuration.Like many large scale web sites, Pinterest’s infrastructure consists of servers that communicate with + backend services composed of a number of individual servers for managing load and fault tolerance. Ideally, we’d like the configuration to reflect only the active hosts, + so clients don’t need to deal with bad hosts as often. ZooKeeper provides a well known pattern to solve this problem [19]. + +### [Rackspace](http://www.rackspace.com/email_hosting) + - The Email & Apps team uses ZooKeeper to coordinate sharding and responsibility changes in a distributed e-mail client + that pulls and indexes data for search. ZooKeeper also provides distributed locking for connections to prevent a cluster from overwhelming servers [1]. + +### [Sematext](http://sematext.com/) + - Uses ZooKeeper in SPM (which includes ZooKeeper monitoring component, too!), Search Analytics, and Logsene [1]. + +### [Tubemogul](http://tubemogul.com/) + - Uses ZooKeeper for leader election, configuration management, locking, group membership [1]. + +### [Twitter](https://twitter.com/) + - ZooKeeper is used at Twitter as the source of truth for storing critical metadata. It serves as a coordination kernel to + provide distributed coordination services, such as leader election and distributed locking. + Some concrete examples of ZooKeeper in action include [15,16]: + - ZooKeeper is used to store service registry, which is used by Twitter’s naming service for service discovery. + - Manhattan (Twitter’s in-house key-value database), Nighthawk (sharded Redis), and Blobstore (in-house photo and video storage), + stores its cluster topology information in ZooKeeper. + - EventBus, Twitter’s pub-sub messaging system, stores critical metadata in ZooKeeper and uses ZooKeeper for leader election. + - Mesos, Twitter’s compute platform, uses ZooKeeper for leader election. + +### [Vast.com](http://www.vast.com/) + - Used internally as a part of sharding services, distributed synchronization of data/index updates, configuration management and failover support [1]. + +### [Wealthfront](http://wealthfront.com/) + - Wealthfront uses ZooKeeper for service discovery, leader election and distributed locking among its many backend services. + ZK is an essential part of Wealthfront's continuous [deployment infrastructure](http://eng.wealthfront.com/2010/05/02/deployment-infrastructure-for-continuous-deployment/) [1]. + +### [Yahoo!](http://www.yahoo.com/) + - ZooKeeper is used for a myriad of services inside Yahoo! for doing leader election, configuration management, sharding, locking, group membership etc [1]. + +### [Zynga](http://www.zynga.com/) + - ZooKeeper at Zynga is used for a variety of services including configuration management, leader election, sharding and more [1]. + + +#### References +- [1] https://cwiki.apache.org/confluence/display/ZOOKEEPER/PoweredBy +- [2] https://www.youtube.com/watch?v=Ew53T6h9oRw +- [3] https://bookkeeper.apache.org/docs/4.7.3/getting-started/concepts/#ledgers +- [4] http://cxf.apache.org/dosgi-discovery-demo-page.html +- [5] https://flume.apache.org/FlumeUserGuide.html +- [6] http://dubbo.apache.org/en-us/blog/dubbo-zk.html +- [7] https://hadoop.apache.org/docs/r2.7.1/hadoop-project-dist/hadoop-hdfs/HDFSHighAvailabilityWithQJM.html +- [8] https://hbase.apache.org/book.html#zookeeper +- [9] https://www.cloudkarafka.com/blog/2018-07-04-cloudkarafka_what_is_zookeeper.html +- [10] http://mesos.apache.org/documentation/latest/high-availability/ +- [11] http://incubator.apache.org/projects/s4.html +- [12] https://lucene.apache.org/solr/guide/6_6/using-zookeeper-to-manage-configuration-files.html#UsingZooKeepertoManageConfigurationFiles-StartupBootstrap +- [13] https://lucene.apache.org/solr/guide/6_6/setting-up-an-external-zookeeper-ensemble.html +- [14] https://spark.apache.org/docs/latest/spark-standalone.html#standby-masters-with-zookeeper +- [15] https://blog.twitter.com/engineering/en_us/topics/infrastructure/2018/zookeeper-at-twitter.html +- [16] https://blog.twitter.com/engineering/en_us/topics/infrastructure/2018/dynamic-configuration-at-twitter.html +- [17] TANG, C., KOOBURAT, T., VENKATACHALAM, P.,CHANDER, A., WEN, Z., NARAYANAN, A., DOWELL,P., AND KARL, R. Holistic Configuration Management + at Facebook. In Proceedings of the 25th Symposium on Operating System Principles (SOSP’15) (Monterey, CA,USA, Oct. 2015). +- [18] https://www.youtube.com/watch?v=SeZV373gUZc +- [19] https://medium.com/@Pinterest_Engineering/zookeeper-resilience-at-pinterest-adfd8acf2a6b +- [20] https://blog.cloudera.com/what-are-hbase-znodes/ +- [21] https://helix.apache.org/Architecture.html +- [22] http://storm.apache.org/releases/current/Setting-up-a-Storm-cluster.html +- [23] https://ci.apache.org/projects/flink/flink-docs-release-1.9/ops/jobmanager_high_availability.html +- [24] https://pulsar.apache.org/docs/en/concepts-architecture-overview/#metadata-store +- [25] https://cwiki.apache.org/confluence/display/Hive/Locking +- [26] *ZooKeeperHiveLockManager* implementation in the [hive](https://github.com/apache/hive/) code base +- [27] https://druid.apache.org/docs/latest/dependencies/zookeeper.html +- [28] https://mapr.com/blog/apache-drill-architecture-ultimate-guide/ +- [29] https://oozie.apache.org/docs/4.1.0/AG_Install.html +- [30] https://docs.spring.io/spring-xd/docs/current/reference/html/ +- [31] https://cwiki.apache.org/confluence/display/CURATOR/Powered+By +- [32] https://projects.spring.io/spring-statemachine/ +- [33] https://www.tigeranalytics.com/blog/apache-kylin-architecture/ +- [34] https://apacheignite.readme.io/docs/cluster-discovery +- [35] http://atlas.apache.org/HighAvailability.html +- [36] http://griffin.apache.org/docs/usecases.html +- [37] https://fluo.apache.org/ +- [38] https://spring.io/projects/spring-cloud-zookeeper diff --git a/zookeeper-it/README.txt b/zookeeper-it/README.txt new file mode 100644 index 0000000..2c4b1cc --- /dev/null +++ b/zookeeper-it/README.txt @@ -0,0 +1,63 @@ +To run the system test we need to create processing containers that we can +spawn tasks, called Instances, in. (how is that for a dangling preposition!?!) +Start up InstanceContainers first. Then run the system test. The system test +finds the InstanceContainers and directs them through ZooKeeper, so you are +going to need an instance of ZooKeeper running that they can all access. +The easiest way to do all of this is to use the zookeeper fat jar. + +Steps to run system test +------------------------ +1) build and transfer the fatjar from the `zookeeper-contrib/zookeeper-contrib-fatjar/target` + directory to all systems participating in the test + + Command to build fatjar without executing the tests: + + `mvn clean install -P fatjar -DskipTests` + +2) run a zookeeper standalone instance (cluster is ok too) + +e.g. java -jar zookeeper--fatjar.jar server + +Note: you must provide zoo.cfg, see sample in conf directory + +3) on each host start the system test container + +e.g. java -jar zookeeper--fatjar.jar ic /sysTest + +name : name of the test container - must be unique + typically it's a good idea to name after the host to aid debugging + +zkHostPort : the host:port of the server from step 2) + +4) initiate the system test using the fatjar: + +java -jar build/contrib/fatjar/zookeeper--fatjar.jar systest org.apache.zookeeper.test.system.SimpleSysTest + +by default it will access the zk server started in 2) on localhost:2181 + +or you can specify a remote host:port using + -DsysTest.zkHostPort=:,:,... + +java -DsysTest.zkHostPort=hostA:2181 -jar build/contrib/fatjar/zookeeper--fatjar.jar systest org.apache.zookeeper.test.system.SimpleSysTest + +where hostA is running the zk server started in step 2) above + +InstanceContainers can also be used to run a the saturation benchmark. The +first two steps are the same as the system test. Step 3 is almost the same: + +3) start the InstanceContainer on each host: + +e.g. java -jar zookeeper--fatjar.jar ic + +note prefix can be /sysTest or any other path. If you do use /sysTest, make +sure the system test isn't running when you run the benchmark. + +4) run GenerateLoad using the following + +java -jar build/contrib/fatjar/zookeeper--fatjar.jar generateLoad #servers #clients + +Once GenerateLoad is started, it will read commands from stdin. Usually +the only command you need to know is "percentage" which sets the percentage +of writes to use in the requests. Once a percentage is set, the benchmark +will start. "percentage 0" will cause only reads to be issued and +"percentage 100" will cause only writes to be issued. diff --git a/zookeeper-it/pom.xml b/zookeeper-it/pom.xml new file mode 100644 index 0000000..6af8c06 --- /dev/null +++ b/zookeeper-it/pom.xml @@ -0,0 +1,83 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + + + zookeeper-it + jar + Apache ZooKeeper - Tests + + ZooKeeper system tests + + + + true + true + true + + + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.apache.zookeeper + zookeeper + tests + test-jar + ${project.version} + + + junit + junit + + + org.openjdk.jmh + jmh-core + 1.23 + + + org.openjdk.jmh + jmh-generator-annprocess + 1.23 + + + + + src + + + org.apache.maven.plugins + maven-javadoc-plugin + + src + + + + + diff --git a/zookeeper-it/src/test/bench/org/apache/zookeeper/BenchMain.java b/zookeeper-it/src/test/bench/org/apache/zookeeper/BenchMain.java new file mode 100644 index 0000000..8e370c0 --- /dev/null +++ b/zookeeper-it/src/test/bench/org/apache/zookeeper/BenchMain.java @@ -0,0 +1,30 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.File; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; + +public class BenchMain { + public static void main(String args[]) throws Exception { + org.openjdk.jmh.Main.main(args); + } +} diff --git a/zookeeper-it/src/test/bench/org/apache/zookeeper/server/watch/WatchBench.java b/zookeeper-it/src/test/bench/org/apache/zookeeper/server/watch/WatchBench.java new file mode 100644 index 0000000..aee5b2f --- /dev/null +++ b/zookeeper-it/src/test/bench/org/apache/zookeeper/server/watch/WatchBench.java @@ -0,0 +1,300 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.server.watch.IWatchManager; +import org.apache.zookeeper.server.DumbWatcher; + +import org.openjdk.jmh.annotations.*; + +import java.util.concurrent.TimeUnit; + +@Fork(3) +public class WatchBench { + + static final String pathPrefix = "/reasonably/long/path/"; + static final EventType event = EventType.NodeDataChanged; + + static IWatchManager createWatchManager(String className) throws Exception { + Class clazz = Class.forName( + "org.apache.zookeeper.server.watch." + className); + return (IWatchManager) clazz.getConstructor().newInstance(); + } + + static void forceGC() { + int gcTimes = 3; + for (int i = 0; i < gcTimes; i++) { + try { + System.gc(); + Thread.currentThread().sleep(1000); + + System.runFinalization(); + Thread.currentThread().sleep(1000); + } catch (InterruptedException ex) { /* ignore */ } + } + } + + static long getMemoryUse() { + forceGC(); + long totalMem = Runtime.getRuntime().totalMemory(); + + forceGC(); + long freeMem = Runtime.getRuntime().freeMemory(); + return totalMem - freeMem; + } + + @State(Scope.Benchmark) + public static class IterationState { + + @Param({"WatchManager", "WatchManagerOptimized"}) + public String watchManagerClass; + + @Param({"10000"}) + public int pathCount; + + String[] paths; + + long watchesAdded = 0; + IWatchManager watchManager; + + long memWhenSetup = 0; + + @Setup(Level.Iteration) + public void setup() throws Exception { + paths = new String[pathCount]; + for (int i = 0; i < paths.length; i++) { + paths[i] = pathPrefix + i; + } + + watchesAdded = 0; + watchManager = createWatchManager(watchManagerClass); + + memWhenSetup = getMemoryUse(); + } + + @TearDown(Level.Iteration) + public void tearDown() { + long memUsed = getMemoryUse() - memWhenSetup; + System.out.println("Memory used: " + watchesAdded + " " + memUsed); + + double memPerMillionWatchesMB = memUsed * 1.0 / watchesAdded ; + System.out.println( + "Memory used per million watches " + + String.format("%.2f", memPerMillionWatchesMB) + "MB"); + } + } + + /** + * Test concenrate watch case where the watcher watches all paths. + * + * The output of this test will be the average time used to add the + * watch to all paths. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + @Warmup(iterations = 1, time = 10, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS) + public void testAddConcentrateWatch(IterationState state) throws Exception { + Watcher watcher = new DumbWatcher(); + + // watch all paths + for (String path : state.paths) { + if (state.watchManager.addWatch(path, watcher)) { + state.watchesAdded++; + } + } + } + + @State(Scope.Benchmark) + public static class InvocationState { + + @Param({"WatchManager", "WatchManagerOptimized"}) + public String watchManagerClass; + + @Param({"1", "1000"}) + public int pathCount; + + @Param({"1", "1000"}) + public int watcherCount; + + String[] paths; + Watcher[] watchers; + + IWatchManager watchManager; + + @Setup(Level.Invocation) + public void setup() throws Exception { + initialize(); + prepare(); + } + + void initialize() throws Exception { + if (paths == null || paths.length != pathCount) { + paths = new String[pathCount]; + for (int i = 0; i < pathCount; i++) { + paths[i] = pathPrefix + i; + } + } + + if (watchers == null || watchers.length != watcherCount) { + watchers = new Watcher[watcherCount]; + for (int i = 0; i < watcherCount; i++) { + watchers[i] = new DumbWatcher(); + } + } + if (watchManager == null || + !watchManager.getClass().getSimpleName().contains( + watchManagerClass)) { + watchManager = createWatchManager(watchManagerClass); + } + } + + void prepare() { + for (String path : paths) { + for (Watcher watcher : watchers) { + watchManager.addWatch(path, watcher); + } + } + } + } + + /** + * Test trigger watches in concenrate case. + * + * The output of this test is the time used to trigger those watches on + * all paths. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + @Warmup(iterations = 1, time = 10, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS) + public void testTriggerConcentrateWatch(InvocationState state) throws Exception { + for (String path : state.paths) { + state.watchManager.triggerWatch(path, event); + } + } + + @State(Scope.Benchmark) + public static class AddSparseWatchState extends InvocationState { + + @Param({"10000"}) + public int pathCount; + + @Param({"10000"}) + public int watcherCount; + + long watchesAdded = 0; + long memWhenSetup = 0; + + @Override + public void prepare() { + watchesAdded = 0; + memWhenSetup = getMemoryUse(); + } + + @TearDown(Level.Invocation) + public void tearDown() { + long memUsed = getMemoryUse() - memWhenSetup; + System.out.println("Memory used: " + watchesAdded + " " + memUsed); + + double memPerMillionWatchesMB = memUsed * 1.0 / watchesAdded ; + System.out.println( + "Memory used per million sparse watches " + + String.format("%.2f", memPerMillionWatchesMB) + "MB"); + + // clear all the watches + for (String path : paths) { + watchManager.triggerWatch(path, event); + } + } + } + + /** + * Test sparse watch case where only one watcher watches all paths, and + * only one path being watched by all watchers. + * + * The output of this test will be the average time used to add those + * sparse watches. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + @Warmup(iterations = 1, time = 10, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS) + public void testAddSparseWatch(AddSparseWatchState state) throws Exception { + // All watchers are watching the 1st path + for (Watcher watcher : state.watchers) { + if (state.watchManager.addWatch(state.paths[0], watcher)) { + state.watchesAdded++; + } + } + // The 1st watcher is watching all paths + for (String path : state.paths) { + if (state.watchManager.addWatch(path, state.watchers[0])) { + state.watchesAdded++; + } + } + } + + @State(Scope.Benchmark) + public static class TriggerSparseWatchState extends InvocationState { + + @Param({"10000"}) + public int pathCount; + + @Param({"10000"}) + public int watcherCount; + + @Override + public void prepare() { + // All watchers are watching the 1st path + for (Watcher watcher : watchers) { + watchManager.addWatch(paths[0], watcher); + } + + // The 1st watcher is watching all paths + for (String path : paths) { + watchManager.addWatch(path, watchers[0]); + } + } + } + + + /** + * Test trigger watches in sparse case. + * + * The output of this test is the time used to trigger those watches on + * all paths. + */ + @Benchmark + @BenchmarkMode(Mode.AverageTime) + @OutputTimeUnit(TimeUnit.MILLISECONDS) + @Warmup(iterations = 1, time = 10, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.SECONDS) + public void testTriggerSparseWatch(TriggerSparseWatchState state) throws Exception { + for (String path : state.paths) { + state.watchManager.triggerWatch(path, event); + } + } +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/BaseSysTest.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/BaseSysTest.java new file mode 100644 index 0000000..6816f2d --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/BaseSysTest.java @@ -0,0 +1,280 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.runner.JUnitCore; + +@Ignore("No tests in this class.") +public class BaseSysTest { + private static final File testData = new File( + System.getProperty("test.data.dir", "src/test/resources/data")); + private static int fakeBasePort = 33222; + private static String zkHostPort; + protected String prefix = "/sysTest"; + ZooKeeper zk; + static { + try { + zkHostPort = System.getProperty("sysTest.zkHostPort", InetAddress.getLocalHost().getCanonicalHostName() + ":2181"); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + InstanceManager im; + @Before + public void setUp() throws Exception { + if (!fakeMachines) { + zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {public void process(WatchedEvent e){}}); + im = new InstanceManager(zk, prefix); + } + } + @After + public void tearDown() throws Exception { + if (null != im) { + im.close(); + } + } + + int serverCount = defaultServerCount; + int clientCount = defaultClientCount; + static int defaultServerCount = 5; + static int defaultClientCount = 7; + static { + defaultServerCount = Integer.parseInt(System.getProperty("simpleSysTest.defaultServerCount", Integer.toString(defaultServerCount))); + defaultClientCount = Integer.parseInt(System.getProperty("simpleSysTest.defaultClientCount", Integer.toString(defaultClientCount))); + } + + String serverHostPort; + String quorumHostPort; + public String getHostPort() { + return serverHostPort; + } + public int getServerCount() { + return serverCount; + } + public int getClientCount() { + return clientCount; + } + + public void startServers() throws IOException { + for(int i = 0; i < serverCount; i++) { + startServer(i); + } + } + public void stopServers() throws IOException { + for(int i = 0; i < serverCount; i++) { + stopServer(i); + } + } + public void startClients() throws IOException { + for(int i = 0; i < clientCount; i++) { + startClient(i); + } + } + public void stopClients() throws IOException { + for(int i = 0; i < clientCount; i++) { + stopClient(i); + } + } + + private static boolean fakeMachines = System.getProperty("baseSysTest.fakeMachines", "no").equals("yes"); + + public void configureServers(int count) throws Exception { + serverCount = count; + if (fakeMachines) { + fakeConfigureServers(count); + } else { + distributedConfigureServers(count); + } + } + + private void distributedConfigureServers(int count) throws IOException { + StringBuilder sbClient = new StringBuilder(); + StringBuilder sbServer = new StringBuilder(); + try { + for(int i = 0; i < count; i++) { + String r[] = QuorumPeerInstance.createServer(im, i); + if (i > 0) { + sbClient.append(','); + sbServer.append(','); + } + sbClient.append(r[0]); // r[0] == "host:clientPort" + sbServer.append(r[1]); // r[1] == "host:leaderPort:leaderElectionPort" + sbServer.append(";"+(r[0].split(":"))[1]); // Appending ";clientPort" + } + serverHostPort = sbClient.toString(); + quorumHostPort = sbServer.toString(); + } catch(Exception e) { + IOException ioe = new IOException(e.getMessage()); + ioe.setStackTrace(e.getStackTrace()); + throw ioe; + } + } + + private QuorumPeer qps[]; + private File qpsDirs[]; + Map peers; + private void fakeConfigureServers(int count) throws IOException { + peers = new HashMap(); + qps = new QuorumPeer[count]; + qpsDirs = new File[count]; + for(int i = 1; i <= count; i++) { + InetSocketAddress peerAddress = new InetSocketAddress("127.0.0.1", + fakeBasePort + i); + InetSocketAddress electionAddr = new InetSocketAddress("127.0.0.1", + serverCount + fakeBasePort + i); + peers.put(Long.valueOf(i), new QuorumServer(i, peerAddress, + electionAddr)); + } + StringBuilder sb = new StringBuilder(); + for(int i = 0; i < count; i++) { + //make that testData exists otherwise it fails on windows + testData.mkdirs(); + qpsDirs[i] = File.createTempFile("sysTest", ".tmp", testData); + qpsDirs[i].delete(); + qpsDirs[i].mkdir(); + int port = fakeBasePort+10+i; + if (sb.length() > 0) { + sb.append(','); + } + sb.append("localhost:"); + sb.append(port); + } + serverHostPort = sb.toString(); + } + final static int tickTime = 2000; + final static int initLimit = 3; + final static int syncLimit = 3; + final static int connectToLearnerMasterLimit = 3; + + public void startServer(int index) throws IOException { + int port = fakeBasePort+10+index; + if (fakeMachines) { + qps[index] = new QuorumPeer(peers, qpsDirs[index], qpsDirs[index], port, 3, index+1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + qps[index].start(); + } else { + try { + QuorumPeerInstance.startInstance(im, quorumHostPort, index); + } catch(Exception e) { + IOException ioe = new IOException(e.getClass().getName() + ": " + e.getMessage()); + ioe.setStackTrace(e.getStackTrace()); + throw ioe; + } + } + } + public void stopServer(int index) throws IOException { + if (fakeMachines) { + qps[index].shutdown(); + } else { + try { + QuorumPeerInstance.stopInstance(im, index); + } catch(Exception e) { + IOException ioe = new IOException(e.getMessage()); + ioe.setStackTrace(e.getStackTrace()); + throw ioe; + } + } + } + + public void configureClients(int count, Class clazz, String params) throws Exception { + clientCount = count; + if (fakeMachines) { + fakeConfigureClients(count, clazz, params); + } else { + distributedConfigureClients(count, clazz, params); + } + } + private Class clazz; + String params; + private void distributedConfigureClients(int count, Class clazz, String params) throws IOException { + this.clazz = clazz; + this.params = params; + + } + private Instance fakeBaseClients[]; + private void fakeConfigureClients(int count, Class clazz, String params) { + fakeBaseClients = new Instance[count]; + for(int i = 0; i < count; i++) { + try { + fakeBaseClients[i] = clazz.getConstructor().newInstance(); + } catch (InstantiationException + | IllegalAccessException + | NoSuchMethodException + | InvocationTargetException e) { + e.printStackTrace(); + return; + } + fakeBaseClients[i].configure(i + " " + params); + } + } + public void startClient(int index) throws IOException { + if (fakeMachines) { + fakeStartClient(index); + } else { + distributedStartClient(index); + } + } + private void distributedStartClient(int index) throws IOException { + try { + im.assignInstance("client" + index, clazz, index + " " + params, 1); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + private void fakeStartClient(int index) { + fakeBaseClients[index].start(); + } + public void stopClient(int index) throws IOException { + if (fakeMachines) { + fakeStopClient(index); + } else { + distributedStopClient(index); + } + } + private void distributedStopClient(int index) throws IOException { + try { + im.removeInstance("client"+index); + } catch (Exception e) { + throw new IOException(e.getMessage()); + } + } + private void fakeStopClient(int index) { + fakeBaseClients[index].stop(); + } + + static public void main(String args[]) { + JUnitCore.main(args); + } +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/DuplicateNameException.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/DuplicateNameException.java new file mode 100644 index 0000000..8a96c57 --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/DuplicateNameException.java @@ -0,0 +1,26 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +public class DuplicateNameException extends Exception { + private static final long serialVersionUID = 1L; + public DuplicateNameException(String mess) { + super(mess); + } +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/GenerateLoad.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/GenerateLoad.java new file mode 100644 index 0000000..afcffc8 --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/GenerateLoad.java @@ -0,0 +1,720 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.PrintStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; +import java.util.Set; + +import org.apache.zookeeper.server.ExitCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.common.Time; + + +public class GenerateLoad { + protected static final Logger LOG = LoggerFactory.getLogger(GenerateLoad.class); + + static ServerSocket ss; + + static Set slaves = Collections + .synchronizedSet(new HashSet()); + + static Map totalByTime = new HashMap(); + + volatile static long currentInterval; + + static long lastChange; + + static PrintStream sf; + static PrintStream tf; + static { + try { + tf = new PrintStream(new FileOutputStream("trace")); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + static final int INTERVAL = 6000; + + synchronized static void add(long time, int count, Socket s) { + long interval = time / INTERVAL; + if (currentInterval == 0 || currentInterval > interval) { + System.out.println( + "Dropping " + count + " for " + new Date(time) + + " " + currentInterval + ">" + interval); + return; + } + // We track totals by seconds + Long total = totalByTime.get(interval); + if (total == null) { + totalByTime.put(interval, (long) count); + } else { + totalByTime.put(interval, total.longValue() + count); + } + tf.println(interval + " " + count + " " + s); + } + + synchronized static long remove(long interval) { + Long total = totalByTime.remove(interval); + return total == null ? -1 : total; + } + + static class SlaveThread extends Thread { + Socket s; + + SlaveThread(Socket s) { + setDaemon(true); + this.s = s; + start(); + } + + public void run() { + try { + System.out.println("Connected to " + s); + BufferedReader is = new BufferedReader(new InputStreamReader(s + .getInputStream())); + String result; + while ((result = is.readLine()) != null) { + String timePercentCount[] = result.split(" "); + if (timePercentCount.length != 5) { + System.err.println("Got " + result + " from " + s + + " exitng."); + throw new IOException(result); + } + long time = Long.parseLong(timePercentCount[0]); + // int percent = Integer.parseInt(timePercentCount[1]); + int count = Integer.parseInt(timePercentCount[2]); + int errs = Integer.parseInt(timePercentCount[3]); + if (errs > 0) { + System.out.println(s + " Got an error! " + errs); + } + add(time, count, s); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + close(); + } + } + + void send(int percentage) { + try { + s.getOutputStream().write((percentage + "\n").getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + void close() { + try { + System.err.println("Closing " + s); + slaves.remove(this); + s.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + static class AcceptorThread extends Thread { + AcceptorThread() { + setDaemon(true); + start(); + } + + public void run() { + try { + while (true) { + Socket s = ss.accept(); + System.err.println("Accepted connection from " + s); + slaves.add(new SlaveThread(s)); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + for (Iterator it = slaves.iterator(); it.hasNext();) { + SlaveThread st = it.next(); + it.remove(); + st.close(); + } + } + } + } + + static class ReporterThread extends Thread { + static int percentage; + + ReporterThread() { + setDaemon(true); + start(); + } + + public void run() { + try { + currentInterval = Time.currentElapsedTime() / INTERVAL; + // Give things time to report; + Thread.sleep(INTERVAL * 2); + long min = 99999; + long max = 0; + long total = 0; + int number = 0; + while (true) { + long now = Time.currentElapsedTime(); + long lastInterval = currentInterval; + currentInterval += 1; + long count = remove(lastInterval); + count = count * 1000 / INTERVAL; // Multiply by 1000 to get + // reqs/sec + if (lastChange != 0 + && (lastChange + INTERVAL * 3) < now) { + // We only want to print anything if things have had a + // chance to change + + if (count < min) { + min = count; + } + if (count > max) { + max = count; + } + total += count; + number++; + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(lastInterval * INTERVAL); + String report = lastInterval + " " + + calendar.get(Calendar.HOUR_OF_DAY) + ":" + + calendar.get(Calendar.MINUTE) + ":" + + calendar.get(Calendar.SECOND) + " " + + percentage + "% " + count + " " + min + " " + + ((double) total / (double) number) + " " + + max; + System.err.println(report); + if (sf != null) { + sf.println(report); + } + } else { + max = total = 0; + min = 999999999; + number = 0; + } + Thread.sleep(INTERVAL); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + synchronized static void sendChange(int percentage) { + long now = Time.currentElapsedTime(); + long start = now; + ReporterThread.percentage = percentage; + for (SlaveThread st : slaves.toArray(new SlaveThread[0])) { + st.send(percentage); + } + now = Time.currentElapsedTime(); + long delay = now - start; + if (delay > 1000) { + System.out.println("Delay of " + delay + " to send new percentage"); + } + lastChange = now; + } + + static public class GeneratorInstance implements Instance { + + byte bytes[]; + + int percentage = -1; + + int errors; + + final Object statSync = new Object(); + + int finished; + + int reads; + + int writes; + + int rlatency; + + int wlatency; + + int outstanding; + + volatile boolean alive; + + class ZooKeeperThread extends Thread implements Watcher, DataCallback, + StatCallback { + String host; + + ZooKeeperThread(String host) { + setDaemon(true); + alive = true; + this.host = host; + start(); + } + + static final int outstandingLimit = 100; + + synchronized void incOutstanding() throws InterruptedException { + outstanding++; + while (outstanding > outstandingLimit) { + wait(); + } + } + + synchronized void decOutstanding() { + outstanding--; + notifyAll(); + } + + Random r = new Random(); + + String path; + + ZooKeeper zk; + + boolean connected; + + public void run() { + try { + zk = new ZooKeeper(host, 60000, this); + synchronized (this) { + if (!connected) { + wait(20000); + } + } + for (int i = 0; i < 300; i++) { + try { + Thread.sleep(100); + path = zk.create("/client", new byte[16], + Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL_SEQUENTIAL); + break; + } catch (KeeperException e) { + LOG.error("keeper exception thrown", e); + } + } + if (path == null) { + System.err.println("Couldn't create a node in /!"); + return; + } + while (alive) { + if (r.nextInt(100) < percentage) { + zk.setData(path, bytes, -1, this, System + .currentTimeMillis()); + } else { + zk.getData(path, false, this, System + .currentTimeMillis()); + } + incOutstanding(); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + alive = false; + try { + zk.close(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + public void process(WatchedEvent event) { + System.err.println(event); + synchronized (this) { + if (event.getType() == EventType.None) { + connected = (event.getState() == KeeperState.SyncConnected); + notifyAll(); + } + } + } + + public void processResult(int rc, String path, Object ctx, byte[] data, + Stat stat) { + decOutstanding(); + synchronized (statSync) { + if (!alive) { + return; + } + if (rc != 0) { + System.err.println("Got rc = " + rc); + errors++; + } else { + finished++; + rlatency += Time.currentElapsedTime() - (Long) ctx; + reads++; + } + } + } + + public void processResult(int rc, String path, Object ctx, Stat stat) { + decOutstanding(); + synchronized (statSync) { + if (rc != 0) { + System.err.println("Got rc = " + rc); + errors++; + } else { + finished++; + wlatency += Time.currentElapsedTime() - (Long) ctx; + writes++; + } + } + } + } + + class SenderThread extends Thread { + Socket s; + + SenderThread(Socket s) { + this.s = s; + setDaemon(true); + start(); + } + + public void run() { + try { + OutputStream os = s.getOutputStream(); + finished = 0; + errors = 0; + while (alive) { + Thread.sleep(300); + if (percentage == -1 || (finished == 0 && errors == 0)) { + continue; + } + String report = Time.currentElapsedTime() + " " + + percentage + " " + finished + " " + errors + " " + + outstanding + "\n"; + /* String subreport = reads + " " + + (((double) rlatency) / reads) + " " + writes + + " " + (((double) wlatency / writes)); */ + synchronized (statSync) { + finished = 0; + errors = 0; + reads = 0; + writes = 0; + rlatency = 0; + wlatency = 0; + } + os.write(report.getBytes()); + //System.out.println("Reporting " + report + "+" + subreport); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + Socket s; + ZooKeeperThread zkThread; + SenderThread sendThread; + Reporter r; + + public void configure(final String params) { + System.err.println("Got " + params); + new Thread() { + public void run() { + try { + String parts[] = params.split(" "); + String hostPort[] = parts[1].split(":"); + int bytesSize = 1024; + if (parts.length == 3) { + try { + bytesSize = Integer.parseInt(parts[2]); + } catch(Exception e) { + System.err.println("Not an integer: " + parts[2]); + } + } + bytes = new byte[bytesSize]; + s = new Socket(hostPort[0], Integer.parseInt(hostPort[1])); + zkThread = new ZooKeeperThread(parts[0]); + sendThread = new SenderThread(s); + BufferedReader is = new BufferedReader(new InputStreamReader(s + .getInputStream())); + String line; + while ((line = is.readLine()) != null) { + percentage = Integer.parseInt(line); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + }.start(); + + } + + public void setReporter(Reporter r) { + this.r = r; + } + + public void start() { + try { + r.report("started"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void stop() { + alive = false; + zkThread.interrupt(); + sendThread.interrupt(); + try { + zkThread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + try { + sendThread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + try { + r.report("stopped"); + } catch (Exception e) { + e.printStackTrace(); + } + try { + s.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + private static class StatusWatcher implements Watcher { + volatile boolean connected; + + public void process(WatchedEvent event) { + if (event.getType() == Watcher.Event.EventType.None) { + synchronized (this) { + connected = event.getState() == Watcher.Event.KeeperState.SyncConnected; + notifyAll(); + } + } + } + + synchronized public boolean waitConnected(long timeout) + throws InterruptedException { + long endTime = Time.currentElapsedTime() + timeout; + while (!connected && Time.currentElapsedTime() < endTime) { + wait(endTime - Time.currentElapsedTime()); + } + return connected; + } + } + + private static boolean leaderOnly; + private static boolean leaderServes; + + private static String []processOptions(String args[]) { + ArrayList newArgs = new ArrayList(); + for(String a: args) { + if (a.equals("--leaderOnly")) { + leaderOnly = true; + leaderServes = true; + } else if (a.equals("--leaderServes")) { + leaderServes = true; + } else { + newArgs.add(a); + } + } + return newArgs.toArray(new String[0]); + } + + /** + * @param args + * @throws InterruptedException + * @throws KeeperException + * @throws DuplicateNameException + * @throws NoAvailableContainers + * @throws NoAssignmentException + */ + public static void main(String[] args) throws InterruptedException, + KeeperException, NoAvailableContainers, DuplicateNameException, + NoAssignmentException { + + args = processOptions(args); + if (args.length == 5) { + try { + StatusWatcher statusWatcher = new StatusWatcher(); + ZooKeeper zk = new ZooKeeper(args[0], 15000, statusWatcher); + if (!statusWatcher.waitConnected(5000)) { + System.err.println("Could not connect to " + args[0]); + return; + } + InstanceManager im = new InstanceManager(zk, args[1]); + ss = new ServerSocket(0); + int port = ss.getLocalPort(); + int serverCount = Integer.parseInt(args[2]); + int clientCount = Integer.parseInt(args[3]); + StringBuilder quorumHostPort = new StringBuilder(); + StringBuilder zkHostPort = new StringBuilder(); + for (int i = 0; i < serverCount; i++) { + String r[] = QuorumPeerInstance.createServer(im, i, leaderServes); + if (i > 0) { + quorumHostPort.append(','); + zkHostPort.append(','); + } + zkHostPort.append(r[0]); // r[0] == "host:clientPort" + quorumHostPort.append(r[1]); // r[1] == "host:leaderPort:leaderElectionPort" + quorumHostPort.append(";"+(r[0].split(":"))[1]); // Appending ";clientPort" + } + for (int i = 0; i < serverCount; i++) { + QuorumPeerInstance.startInstance(im, quorumHostPort + .toString(), i); + } + if (leaderOnly) { + int tries = 0; + outer: + while(true) { + Thread.sleep(1000); + IOException lastException = null; + String parts[] = zkHostPort.toString().split(","); + for(int i = 0; i < parts.length; i++) { + try { + String mode = getMode(parts[i]); + if (mode.equals("leader")) { + zkHostPort = new StringBuilder(parts[i]); + System.out.println("Connecting exclusively to " + zkHostPort.toString()); + break outer; + } + } catch(IOException e) { + lastException = e; + } + } + if (tries++ > 3) { + throw lastException; + } + } + } + for (int i = 0; i < clientCount; i++) { + im.assignInstance("client" + i, GeneratorInstance.class, + zkHostPort.toString() + + ' ' + + InetAddress.getLocalHost() + .getCanonicalHostName() + ':' + + port, 1); + } + new AcceptorThread(); + new ReporterThread(); + BufferedReader is = new BufferedReader(new InputStreamReader( + System.in)); + String line; + while ((line = is.readLine()) != null) { + try { + String cmdNumber[] = line.split(" "); + if (cmdNumber[0].equals("percentage") + && cmdNumber.length > 1) { + int number = Integer.parseInt(cmdNumber[1]); + if (number < 0 || number > 100) { + throw new NumberFormatException( + "must be between 0 and 100"); + } + sendChange(number); + } else if (cmdNumber[0].equals("sleep") + && cmdNumber.length > 1) { + int number = Integer.parseInt(cmdNumber[1]); + Thread.sleep(number * 1000); + } else if (cmdNumber[0].equals("save") + && cmdNumber.length > 1) { + sf = new PrintStream(cmdNumber[1]); + } else { + System.err.println("Commands must be:"); + System.err + .println("\tpercentage new_write_percentage"); + System.err.println("\tsleep seconds_to_sleep"); + System.err.println("\tsave file_to_save_output"); + } + } catch (NumberFormatException e) { + System.out.println("Not a valid number: " + + e.getMessage()); + } + } + } catch (NumberFormatException e) { + doUsage(); + } catch (IOException e) { + e.printStackTrace(); + System.exit(ExitCode.INVALID_INVOCATION.getValue()); + } + } else { + doUsage(); + } + + } + + private static String getMode(String hostPort) throws NumberFormatException, UnknownHostException, IOException { + String parts[] = hostPort.split(":"); + Socket s = new Socket(parts[0], Integer.parseInt(parts[1])); + s.getOutputStream().write("stat".getBytes()); + BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); + String line; + try { + while((line = br.readLine()) != null) { + if (line.startsWith("Mode: ")) { + return line.substring(6); + } + } + return "unknown"; + } finally { + s.close(); + } + } + + private static void doUsage() { + System.err.println("USAGE: " + GenerateLoad.class.getName() + + " [--leaderOnly] [--leaderServes] zookeeper_host:port containerPrefix #ofServers #ofClients requestSize"); + System.exit(ExitCode.INVALID_INVOCATION.getValue()); + } +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/Instance.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/Instance.java new file mode 100644 index 0000000..9b980de --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/Instance.java @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import org.apache.zookeeper.KeeperException; + +/** + * This interface is implemented by a class that can be run in an + * instance container. + * + */ +public interface Instance { + /** + * This object is used to report back changes in status. + */ + interface Reporter { + void report(String report) throws KeeperException, InterruptedException; + } + /** + * This will be the first method invoked by the InstanceContainer after + * an instance of this interface has been constructed. It will only be + * invoked once. + * + * @param r a handle to use to report on status changes. + */ + void setReporter(Reporter r); + /** + * This will be the second method invoked by the InstanceContainer. It + * may be invoked again if the configuration changes. + * + * @param params parameters that were passed to the InstanceManager when + * this instance was scheduled. + */ + void configure(String params); + /** + * Starts this instance. + */ + void start(); + /** + * Stops this instance. + */ + void stop(); +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/InstanceContainer.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/InstanceContainer.java new file mode 100644 index 0000000..44f6531 --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/InstanceContainer.java @@ -0,0 +1,312 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.zookeeper.server.ExitCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.KeeperException.ConnectionLossException; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.KeeperException.NodeExistsException; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.system.Instance.Reporter; + +/** + * This class starts up, + */ +public class InstanceContainer implements Watcher, AsyncCallback.ChildrenCallback { + private final class MyWatcher implements Watcher { + String myNode; + DataCallback dc; + MyWatcher(String myNode, DataCallback dc) { + this.myNode = myNode; + this.dc = dc; + } + public void process(WatchedEvent event) { + if (event.getPath() != null && event.getPath().equals(myNode)) { + zk.getData(myNode, this, dc, this); + } + } + } + private final class MyDataCallback implements DataCallback { + int lastVer; + String myNode; + Instance myInstance; + + MyDataCallback(String myNode, Instance myInstance, int ver) { + this.myNode = myNode; + this.myInstance = myInstance; + lastVer = ver; + } + public void processResult(int rc, String path, + Object ctx, byte[] data, Stat stat) { + if (rc == KeeperException.Code.NONODE.intValue()) { + // we can just ignore because the child watcher takes care of this + return; + } + if (rc != KeeperException.Code.OK.intValue()) { + zk.getData(myNode, (Watcher)ctx, this, ctx); + } + int currVer = stat.getVersion(); + if (currVer != lastVer) { + String parts[] = new String(data).split(" ", 2); + myInstance.configure(parts[1]); + lastVer = currVer; + } + } + } + private final class MyReporter implements Reporter { + String myReportNode; + + public MyReporter(String child) { + myReportNode = reportsNode + '/' + child; + } + + public void report(String report) throws KeeperException, InterruptedException { + for(int j = 0; j < maxTries; j++) { + try { + try { + zk.setData(myReportNode, report.getBytes(), -1); + } catch(NoNodeException e) { + zk.create(myReportNode, report.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + } + break; + } catch(ConnectionLossException e) {} + } + } + } + private static final Logger LOG = LoggerFactory.getLogger(InstanceContainer.class); + String name; + String zkHostPort; + // We only run if the readyNode exists + String prefixNode; + String statusNode = "available"; + String reportsNode = "reports"; + String assignmentsNode = "assignments"; + ZooKeeper zk; + static final int sessTimeout = 5000; + static final int maxTries = 3; + public InstanceContainer(String name, String zkHostPort, String prefix) throws UnknownHostException { + if (name.length() == 0 || name.equals("hostname")) { + name = InetAddress.getLocalHost().getCanonicalHostName(); + } + this.name = name; + this.zkHostPort = zkHostPort; + this.prefixNode = prefix; + this.statusNode = prefix + '/' + this.statusNode + '/' + name; + this.reportsNode = prefix + '/' + this.reportsNode; + this.assignmentsNode = prefix + '/' + this.assignmentsNode + '/' + name; + } + + private void rmnod(String path) throws InterruptedException, KeeperException { + KeeperException lastException = null; + for(int i = 0; i < maxTries; i++) { + try { + zk.delete(path, -1); + lastException = null; + break; + } catch (KeeperException.NoNodeException e) { + // cool this is what we want + break; + } catch (KeeperException e) { + lastException = e; + } + } + if (lastException != null) { + throw lastException; + } + } + private void mknod_inner(String path, CreateMode mode) throws KeeperException, InterruptedException { + for(int i = 0; i < maxTries; i++) { + try { + zk.create(path, null, Ids.OPEN_ACL_UNSAFE, mode); + break; + } catch (NodeExistsException e) { + if (mode != CreateMode.EPHEMERAL) { + return; + } + Stat stat = zk.exists(path, false); + if (stat == null) { + continue; + } + if (stat.getEphemeralOwner() != zk.getSessionId()) { + throw e; + } + break; + } catch (ConnectionLossException e) { + e.printStackTrace(); + } + } + } + + private void mknod(String path, CreateMode mode) throws KeeperException, InterruptedException { + String subpath[] = path.split("/"); + StringBuilder sb = new StringBuilder(); + // We start at 1 because / will create an empty part first + for(int i = 1; i < subpath.length; i++) { + sb.append("/"); + sb.append(subpath[i]); + CreateMode m = CreateMode.PERSISTENT; + if (i == subpath.length-1) { + m = mode; + } + mknod_inner(sb.toString(), m); + } + } + + public void run() throws IOException, InterruptedException, KeeperException { + zk = new ZooKeeper(zkHostPort, sessTimeout, this); + mknod(assignmentsNode, CreateMode.PERSISTENT); + mknod(statusNode, CreateMode.EPHEMERAL); + mknod(reportsNode, CreateMode.PERSISTENT); + // Now we just start watching the assignments directory + zk.getChildren(assignmentsNode, true, this, null); + } + + /** + * @param args the first parameter is the instance name, the second + * is the ZooKeeper spec. if the instance name is the empty string + * or "hostname", the hostname will be used. + * @throws InterruptedException + * @throws IOException + * @throws UnknownHostException + * @throws KeeperException + */ + public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException, KeeperException { + if (args.length != 3) { + System.err.println("USAGE: " + InstanceContainer.class.getName() + " name zkHostPort znodePrefix"); + System.exit(ExitCode.INVALID_INVOCATION.getValue()); + } + new InstanceContainer(args[0], args[1], args[2]).run(); + while(true) { + Thread.sleep(1000); + } + } + + public void process(WatchedEvent event) { + if (KeeperState.Expired == event.getState()) { + // It's all over + LOG.error("Lost session"); + System.exit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue()); + } + if (event.getPath() != null && event.getPath().equals(assignmentsNode)) { + // children have changed, so read in the new list + zk.getChildren(assignmentsNode, true, this, null); + } + } + + Map instances = new HashMap(); + + @Override + public void processResult(int rc, String path, Object ctx, List children) { + if (rc != KeeperException.Code.OK.intValue()) { + // try it again + zk.getChildren(assignmentsNode, true, this, null); + return; + } + Map newList = new HashMap(); + // check for differences + Stat stat = new Stat(); + for(String child: children) { + Instance i = instances.remove(child); + if (i == null) { + // Start up a new instance + byte[] data = null; + String myNode = assignmentsNode + '/' + child; + while(true) { + try { + data = zk.getData(myNode, true, stat); + break; + } catch (NoNodeException e) { + // The node doesn't exist anymore, so skip it + break; + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + return; + } + } + if (data != null) { + String instanceSpec = new String(data); + int spaceIndex = instanceSpec.indexOf(' '); + String clazz; + String conf; + if (spaceIndex == -1) { + clazz = instanceSpec; + conf = null; + } else { + clazz = instanceSpec.substring(0, spaceIndex); + conf = instanceSpec.substring(spaceIndex+1); + } + try { + Class c = Class.forName(clazz); + i = (Instance) c.getConstructor().newInstance(); + Reporter reporter = new MyReporter(child); + i.setReporter(reporter); + i.configure(conf); + i.start(); + newList.put(child, i); + int ver = stat.getVersion(); + Instance myInstance = i; + DataCallback dc = new MyDataCallback(myNode, myInstance, ver); + Watcher watcher = new MyWatcher(myNode, dc); + zk.getData(myNode, watcher, dc, watcher); + } catch (Exception e) { + LOG.warn("Skipping " + child, e); + if (e.getCause() != null) { + LOG.warn("Caused by", e.getCause()); + } + } + + } + } else { + // just move it to the new list + newList.put(child, i); + } + } + // kill anything that was removed for the children + for(Map.Entry i: instances.entrySet()) { + i.getValue().stop(); + try { + rmnod(reportsNode + '/' + i.getKey()); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (KeeperException e) { + e.printStackTrace(); + } + } + instances = newList; + } + +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/InstanceManager.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/InstanceManager.java new file mode 100644 index 0000000..12c94e9 --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/InstanceManager.java @@ -0,0 +1,347 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.KeeperException.ConnectionLossException; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.KeeperException.NodeExistsException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.common.Time; + +/** + * This class doles out assignments to InstanceContainers that are registered to + * a ZooKeeper znode. The znode will have four child nodes: + * * ready: this znode indicates that the InstanceManager is running + * * available: the children of this znode are ephemeral nodes representing + * running InstanceContainers + * * assignments: there will be a child under this znode for each available + * InstanceContainer. those znodes will have a child for each + * assigned instance + * * reports: there will be a child under this znode for each instance that is + * running. it will have the report string from the instance. + */ +public class InstanceManager implements AsyncCallback.ChildrenCallback, Watcher { + final private static Logger LOG = LoggerFactory.getLogger(InstanceManager.class); + private ZooKeeper zk; + private String prefixNode; + private String reportsNode = "reports"; + private String readyNode = "ready"; + private String assignmentsNode = "assignments"; + private String statusNode = "available"; + private static final int maxTries = 3; + private static final class Assigned { + String container; + int weight; + Assigned(String container, int weight) { + this.container = container; + this.weight = weight; + } + } + private static List preferredList = new ArrayList(); + static { + String list = System.getProperty("ic.preferredList"); + if (list != null) { + preferredList = Arrays.asList(list.split(",")); + System.err.println("Preferred List: " + preferredList); + } else { + System.err.println("Preferred List is empty"); + } + } + private Map> assignments = new HashMap>(); + private Map instanceToAssignment = new HashMap(); + public InstanceManager(ZooKeeper zk, String prefix) throws KeeperException, InterruptedException { + this.zk = zk; + this.prefixNode = prefix; + this.readyNode = prefix + '/' + this.readyNode; + this.assignmentsNode = prefix + '/' + this.assignmentsNode; + this.reportsNode = prefix + '/' + this.reportsNode; + this.statusNode = prefix + '/' + this.statusNode; + for(int i = 0; i < maxTries; i++) { + try { + setupNodes(zk); + break; + } catch(ConnectionLossException e) {} + } + ConnectionLossException lastException = null; + for(int i = 0; i < maxTries; i++) { + try { + List children = zk.getChildren(statusNode, this); + processResult(0, statusNode, null, children); + lastException = null; + break; + } catch(ConnectionLossException e) { + lastException = e; + } + } + if (lastException != null) { + throw lastException; + } + } + private void setupNodes(ZooKeeper zk) throws KeeperException, + InterruptedException { + try { + zk.create(prefixNode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch(NodeExistsException e) { /* this is ok */ } + try { + zk.create(assignmentsNode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch(NodeExistsException e) { /* this is ok */ } + try { + zk.create(statusNode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch(NodeExistsException e) { /* this is ok */ } + try { + zk.create(reportsNode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch(NodeExistsException e) { /* this is ok */ } + try { + zk.create(readyNode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch(NodeExistsException e) { /* this is ok */ } + } + + synchronized public void processResult(int rc, String path, Object ctx, + List children) { + if (rc != KeeperException.Code.OK.intValue()) { + zk.getChildren(statusNode, this, this, null); + return; + } + if (LOG.isDebugEnabled()) { + LOG.debug("Got " + children + " children from " + path); + } + Map> newAssignments = new HashMap>(); + for(String c: children) { + HashSet a = assignments.remove(c); + if (a != null) { + newAssignments.put(c, a); + } else { + newAssignments.put(c, new HashSet()); + } + } + // Clean up the dead machines + for(String dead: assignments.keySet()) { + try { + removeInstance(dead); + } catch (KeeperException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + assignments = newAssignments; + } + + public void process(WatchedEvent event) { + if (event.getPath().equals(statusNode)) { + zk.getChildren(statusNode, this, this, null); + } + } + synchronized public String assignInstance(String name, Class clazz, String params, int weight) throws NoAvailableContainers, DuplicateNameException, InterruptedException, KeeperException { + if (weight < 1) { + // if the weights are not above zero, things will get messed up + weight = 1; + } + String instanceSpec = clazz.getName() + ' ' + params; + if (instanceToAssignment.get(name) != null) { + throw new DuplicateNameException(name + " already exists"); + } + // find most idle node + String mostIdle = null; + int mostIdleWeight = Integer.MAX_VALUE; + for(String preferred: preferredList) { + HashSet assignmentList = assignments.get(preferred); + int w = 0; + if (assignmentList != null) { + for(Assigned a: assignmentList) { + w += a.weight; + } + if (w < mostIdleWeight) { + mostIdleWeight = w; + mostIdle = preferred; + } + } + } + for(Entry> e: assignments.entrySet()) { + int w = 0; + for(Assigned a: e.getValue()) { + w += a.weight; + } + if (w < mostIdleWeight) { + mostIdleWeight = w; + mostIdle = e.getKey(); + } + } + if (mostIdle == null) { + throw new NoAvailableContainers("No available containers"); + } + Assigned a = new Assigned(mostIdle, weight); + instanceToAssignment.put(name, a); + HashSet as = assignments.get(mostIdle); + if (as == null) { + as = new HashSet(); + assignments.put(mostIdle, as); + } + as.add(a); + KeeperException lastException = null; + for(int i = 0; i < maxTries; i++) { + try { + zk.create(assignmentsNode + '/' + mostIdle + '/' + name, instanceSpec.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + return mostIdle; + } catch(NodeExistsException e) { + return mostIdle; + } catch (KeeperException e) { + lastException = e; + } + } + throw lastException; + } + + public void reconfigureInstance(String name, String params) throws NoAssignmentException, InterruptedException, KeeperException { + if (LOG.isDebugEnabled()) { + LOG.debug("Reconfiguring " + name + " with " + params); + } + Assigned assigned = instanceToAssignment.get(name); + if (assigned == null) { + throw new NoAssignmentException(); + } + KeeperException lastException = null; + for(int i = 0; i < maxTries; i++) { + try { + zk.setData(assignmentsNode + '/' + assigned.container + '/' + name, ("update " + params).getBytes(), -1); + break; + } catch (ConnectionLossException e) { + lastException = e; + } + } + if (lastException != null) { + throw lastException; + } + } + + private void doDelete(String path) throws InterruptedException, KeeperException { + KeeperException lastException = null; + for(int i = 0; i < maxTries; i++) { + try { + zk.delete(path, -1); + return; + } catch(NoNodeException e) { + return; + } catch (KeeperException e) { + lastException = e; + } + } + throw lastException; + } + synchronized public void removeInstance(String name) throws InterruptedException, KeeperException { + Assigned assigned = instanceToAssignment.remove(name); + if (assigned == null) { + return; + } + assignments.get(assigned.container).remove(name); + doDelete(assignmentsNode + '/' + assigned.container + '/' + name); + doDelete(reportsNode + '/' + name); + } + + synchronized boolean isAlive(String name) { + return instanceToAssignment.get(name) != null; + } + + public void resetStatus(String name) throws InterruptedException, KeeperException { + KeeperException lastException = null; + for(int i = 0; i < maxTries; i++) { + try { + zk.delete(reportsNode + '/' + name, -1); + lastException = null; + break; + } catch(ConnectionLossException e) { + lastException = e; + } catch(NoNodeException e) { + // great this is what we want! + } + } + if (lastException != null) { + throw lastException; + } + } + + public String getStatus(String name, long timeout) throws KeeperException, InterruptedException { + Stat stat = new Stat(); + byte[] data = null; + long endTime = Time.currentElapsedTime() + timeout; + KeeperException lastException = null; + for(int i = 0; i < maxTries && endTime > Time.currentElapsedTime(); i++) { + try { + data = zk.getData(reportsNode + '/' + name, false, stat); + if (LOG.isDebugEnabled()) { + LOG.debug("Got Data: " + ((data == null) ? "null" : new String(data))); + } + lastException = null; + break; + } catch(ConnectionLossException e) { + lastException = e; + } catch(NoNodeException e) { + final Object eventObj = new Object(); + synchronized(eventObj) { + // wait for the node to appear + Stat eStat = zk.exists(reportsNode + '/' + name, new Watcher() { + public void process(WatchedEvent event) { + synchronized(eventObj) { + eventObj.notifyAll(); + } + }}); + if (eStat == null) { + eventObj.wait(endTime - Time.currentElapsedTime()); + } + } + lastException = e; + } + } + if (lastException != null) { + throw lastException; + } + return new String(data); + } + synchronized public void close() throws InterruptedException { + for(String name: instanceToAssignment.keySet().toArray(new String[0])) { + try { + removeInstance(name); + } catch(KeeperException e) { + e.printStackTrace(); + } + } + try { + doDelete(readyNode); + } catch (KeeperException e) { + e.printStackTrace(); + } + } +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/NoAssignmentException.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/NoAssignmentException.java new file mode 100644 index 0000000..1ac1390 --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/NoAssignmentException.java @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +public class NoAssignmentException extends Exception { + private static final long serialVersionUID = 1L; + +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/NoAvailableContainers.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/NoAvailableContainers.java new file mode 100644 index 0000000..b8aabcc --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/NoAvailableContainers.java @@ -0,0 +1,28 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +public class NoAvailableContainers extends Exception { + public NoAvailableContainers(String string) { + super(string); + } + + private static final long serialVersionUID = 1L; + +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/QuorumPeerInstance.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/QuorumPeerInstance.java new file mode 100644 index 0000000..782deaf --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/QuorumPeerInstance.java @@ -0,0 +1,291 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.test.TestUtils; + +class QuorumPeerInstance implements Instance { + final private static Logger LOG = LoggerFactory.getLogger(QuorumPeerInstance.class); + private static final File testData = new File( + System.getProperty("test.data.dir", "src/test/resources/data")); + + private static final int syncLimit = 3; + private static final int initLimit = 3; + private static final int connectToLearnerMasterLimit = 3; + private static final int tickTime = 2000; + String serverHostPort; + int serverId; + Reporter r; + QuorumPeer peer; + + public void setReporter(Reporter r) { + this.r = r; + } + + InetSocketAddress clientAddr; + InetSocketAddress quorumLeaderAddr; + InetSocketAddress quorumLeaderElectionAddr; + Map peers; + File snapDir, logDir; + + public QuorumPeerInstance() { + try { + File tmpFile = File.createTempFile("test", ".dir", testData); + File tmpDir = tmpFile.getParentFile(); + tmpFile.delete(); + File zkDirs = new File(tmpDir, "zktmp.cfg"); + logDir = tmpDir; + snapDir = tmpDir; + Properties p; + if (zkDirs.exists()) { + p = new Properties(); + FileInputStream input = new FileInputStream(zkDirs); + try { + p.load(input); + } finally { + input.close(); + } + } else { + p = System.getProperties(); + } + logDir = new File(p.getProperty("logDir", tmpDir.getAbsolutePath())); + snapDir = new File(p.getProperty("snapDir", tmpDir.getAbsolutePath())); + logDir = File.createTempFile("zktst", ".dir", logDir); + logDir.delete(); + logDir.mkdirs(); + snapDir = File.createTempFile("zktst", ".dir", snapDir); + snapDir.delete(); + snapDir.mkdirs(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void configure(String params) { + if (clientAddr == null) { + String parts[] = params.split(" "); + // The first time we are configured, it is just to tell + // us which machine we are + serverId = Integer.parseInt(parts[0]); + if (LOG.isDebugEnabled()) { + LOG.debug("Setting up server " + serverId); + } + if (parts.length > 1 && parts[1].equals("false")) { + System.setProperty("zookeeper.leaderServes", "no"); + } else { + System.setProperty("zookeeper.leaderServes", "yes"); + } + // Let's grab two ports + try { + ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost()); + clientAddr = (InetSocketAddress) ss.getLocalSocketAddress(); + ss.close(); + } catch(IOException e) { + e.printStackTrace(); + } + try { + ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost()); + quorumLeaderAddr = (InetSocketAddress) ss.getLocalSocketAddress(); + ss.close(); + } catch(IOException e) { + e.printStackTrace(); + } + try { + ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost()); + quorumLeaderElectionAddr = (InetSocketAddress) ss.getLocalSocketAddress(); + ss.close(); + } catch(IOException e) { + e.printStackTrace(); + } + String report = clientAddr.getHostString() + ':' + clientAddr.getPort() + + ',' + quorumLeaderAddr.getHostString() + ':' + quorumLeaderAddr.getPort() + ':' + quorumLeaderElectionAddr.getPort(); + try { + if (LOG.isDebugEnabled()) { + LOG.debug("Reporting " + report); + } + r.report(report); + } catch (Exception e) { + e.printStackTrace(); + } + return; + } else { + int spaceIndex = params.indexOf(' '); + if (spaceIndex == -1) { + LOG.warn("looking for host:port,... start|stop, but found " + params); + return; + } + String quorumSpecs = params.substring(0, spaceIndex); + String cmd = params.substring(spaceIndex+1); + if (LOG.isDebugEnabled()) { + LOG.debug("Running command: " + cmd); + } + if (!cmd.equals("start")) { + if (peer != null) { + peer.shutdown(); + } + peer = null; + try { + for(int i = 0; i < 5; i++) { + Thread.sleep(500); + try { + // Wait until we can't connect + new Socket("127.0.0.1", clientAddr.getPort()).close(); + } catch(IOException e) { break; } + } + r.report("stopped"); + } catch (Exception e) { + LOG.error("Unhandled error", e); + } + return; + } + String parts[] = quorumSpecs.split(","); + peers = new HashMap(); + for(int i = 0; i < parts.length; i++) { + // parts[i] == "host:leaderPort:leaderElectionPort;clientPort" + String subparts[] = ((parts[i].split(";"))[0]).split(":"); + String clientPort = (parts[i].split(";"))[1]; + peers.put(Long.valueOf(i), + new QuorumServer( + i, + new InetSocketAddress(subparts[0], Integer.parseInt(subparts[1])), + new InetSocketAddress(subparts[0], Integer.parseInt(subparts[2])), + new InetSocketAddress(subparts[0], Integer.parseInt(clientPort)))); + } + try { + if (LOG.isDebugEnabled()) { + LOG.debug("Starting quorumPeer " + serverId + " on port " + clientAddr.getPort()); + } + if (peer != null) { + LOG.warn("Peer " + serverId + " already started"); + return; + } + System.err.println("SnapDir = " + snapDir + " LogDir = " + logDir); + peer = new QuorumPeer(peers, snapDir, logDir, clientAddr.getPort(), 3, serverId, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + peer.start(); + for(int i = 0; i < 5; i++) { + Thread.sleep(500); + try { + // Wait until we can connect + new Socket("127.0.0.1", clientAddr.getPort()).close(); + break; + } catch(IOException e) {} + } + r.report("started"); + } catch (Exception e) { + LOG.error("Unhandled exception", e); + } + } + } + + public void start() { + } + + public void stop() { + if (LOG.isDebugEnabled()) { + LOG.debug("Stopping peer " + serverId); + } + if (peer != null) { + peer.shutdown(); + } + if (logDir != null) { + TestUtils.deleteFileRecursively(logDir); + } + if (snapDir != null) { + TestUtils.deleteFileRecursively(snapDir); + } + } + + /** + * This method is used to configure a QuorumPeerInstance + * + * @param im the InstanceManager that will be managing the new instance + * @param i the server number to configure (should be zero based) + * @throws NoAvailableContainers + * @throws DuplicateNameException + * @throws InterruptedException + * @throws KeeperException + */ + public static String[] createServer(InstanceManager im, int i) throws NoAvailableContainers, DuplicateNameException, InterruptedException, KeeperException { + return createServer(im, i, true); + } + + /** + * This method is used to configure a QuorumPeerInstance + * + * @param im the InstanceManager that will be managing the new instance + * @param i the server number to configure (should be zero based) + * @param leaderServes if false, the leader will not accept client connections + * @throws NoAvailableContainers + * @throws DuplicateNameException + * @throws InterruptedException + * @throws KeeperException + */ + public static String[] createServer(InstanceManager im, int i, boolean leaderServes) throws NoAvailableContainers, DuplicateNameException, InterruptedException, KeeperException { + im.assignInstance("server"+i, QuorumPeerInstance.class, Integer.toString(i) + " " + leaderServes, 50); + return im.getStatus("server"+i, 3000).split(","); + + } + + /** + * Start an instance of the quorumPeer. + * @param im the manager of the instance + * @param quorumHostPort the comma-separated list of host:port pairs of quorum peers + * @param index the zero based index of the server to start. + * @throws InterruptedException + * @throws KeeperException + * @throws NoAssignmentException + */ + public static void startInstance(InstanceManager im, String quorumHostPort, int index) throws InterruptedException, KeeperException, NoAssignmentException { + im.resetStatus("server" + index); + im.reconfigureInstance("server"+index, quorumHostPort + " start"); + im.getStatus("server" + index, 5000); + } + + /** + * Stop an instance of the quorumPeer + * @param im the manager of the instance + * @param index the zero based index of the server to stop + * @throws InterruptedException + * @throws KeeperException + * @throws NoAssignmentException + */ + public static void stopInstance(InstanceManager im, int index) throws InterruptedException, KeeperException, NoAssignmentException { + im.resetStatus("server" + index); + im.reconfigureInstance("server"+index, Integer.toString(index) + " stop"); + im.getStatus("server" + index, 3000); + + } + +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/SimpleClient.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/SimpleClient.java new file mode 100644 index 0000000..2dc80ba --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/SimpleClient.java @@ -0,0 +1,116 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; + +/** + * The client that gets spawned for the SimpleSysTest + * + */ +public class SimpleClient implements Instance, Watcher, AsyncCallback.DataCallback, StringCallback, StatCallback { + private static final long serialVersionUID = 1L; + String hostPort; + ZooKeeper zk; + transient int index; + transient String myPath; + byte[] data; + boolean createdEphemeral; + public void configure(String params) { + String parts[] = params.split(" "); + hostPort = parts[1]; + this.index = Integer.parseInt(parts[0]); + myPath = "/simpleCase/" + index; + } + + public void start() { + try { + zk = new ZooKeeper(hostPort, 15000, this); + zk.getData("/simpleCase", true, this, null); + if (null != r) { + r.report("Client " + index + " connecting to " + hostPort); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void stop() { + try { + if (zk != null) { + zk.close(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + public void process(WatchedEvent event) { + if (event.getPath() != null && event.getPath().equals("/simpleCase")) { + zk.getData("/simpleCase", true, this, null); + } + } + + public void processResult(int rc, String path, Object ctx, byte[] data, + Stat stat) { + if (rc != 0) { + zk.getData("/simpleCase", true, this, null); + } else { + this.data = data; + String content = new String(data); + if (content.equals("die")) { + this.stop(); + return; + } + if (!createdEphemeral) { + zk.create(myPath, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, this, null); + createdEphemeral = true; + } else { + zk.setData(myPath, data, -1, this, null); + } + } + } + + public void processResult(int rc, String path, Object ctx, String name) { + if (rc != 0) { + zk.create(myPath, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, this, null); + } + } + public void processResult(int rc, String path, Object ctx, Stat stat) { + if (rc != 0) { + zk.setData(myPath, data, -1, this, null); + } + } + @Override + public String toString() { + return SimpleClient.class.getName() + "[" + index + "] using " + hostPort; + } + + Reporter r; + public void setReporter(Reporter r) { + this.r = r; + } +} diff --git a/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/SimpleSysTest.java b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/SimpleSysTest.java new file mode 100644 index 0000000..d35521b --- /dev/null +++ b/zookeeper-it/src/test/java/org/apache/zookeeper/test/system/SimpleSysTest.java @@ -0,0 +1,173 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test.system; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.KeeperException.ConnectionLossException; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.data.Stat; +import org.junit.Assert; +import org.junit.Test; +import org.apache.zookeeper.common.Time; + +/** + * This does a basic system test. It starts up an ensemble of servers and a set of clients. + * It makes sure that all the clients come up. It kills off servers while making a change and + * then ensures that all clients see the change. And then signals the clients to die and + * watches them disappear. + * + */ +public class SimpleSysTest extends BaseSysTest implements Watcher { + int maxTries = 10; + boolean connected; + final private static Logger LOG = LoggerFactory.getLogger(SimpleSysTest.class); + + synchronized private boolean waitForConnect(ZooKeeper zk, long timeout) throws InterruptedException { + connected = (zk.getState() == States.CONNECTED); + long end = Time.currentElapsedTime() + timeout; + while(!connected && end > Time.currentElapsedTime()) { + wait(timeout); + connected = (zk.getState() == States.CONNECTED); + } + return connected; + } + + /** + * This test checks the following: + * 1) All clients connect successfully + * 2) Half of the servers die (assuming odd number) and a write succeeds + * 3) All servers are restarted and cluster stays alive + * 4) Clients see a change by the server + * 5) Clients' ephemeral nodes are cleaned up + * + * @throws Exception + */ + @Test + public void testSimpleCase() throws Exception { + configureServers(serverCount); + configureClients(clientCount, SimpleClient.class, getHostPort()); + Stat stat = new Stat(); + startServers(); + LOG.debug("Connecting to " + getHostPort()); + ZooKeeper zk = new ZooKeeper(getHostPort(), 15000, this); + waitForConnect(zk, 10000); + zk.create("/simpleCase", "orig".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + startClients(); + + // Check that all clients connect properly + for(int i = 0; i < getClientCount(); i++) { + for(int j = 0; j < maxTries; j++) { + try { + byte b[] = zk.getData("/simpleCase/" + i, false, stat); + Assert.assertEquals("orig", new String(b)); + } catch(NoNodeException e) { + if (j+1 == maxTries) { + Assert.fail("Max tries exceeded on client " + i); + } + Thread.sleep(1000); + } + } + } + + // Kill half the servers, make a change, restart the dead + // servers, and then bounce the other servers one by one + for(int i = 0; i < getServerCount(); i++) { + stopServer(i); + if (i+1 > getServerCount()/2) { + startServer(i); + } else if (i+1 == getServerCount()/2) { + Assert.assertTrue("Connection didn't recover", waitForConnect(zk, 10000)); + try { + zk.setData("/simpleCase", "new".getBytes(), -1); + } catch(ConnectionLossException e) { + Assert.assertTrue("Connection didn't recover", waitForConnect(zk, 10000)); + zk.setData("/simpleCase", "new".getBytes(), -1); + } + for(int j = 0; j < i; j++) { + LOG.info("Starting server " + j); + startServer(i); + } + } + } + Thread.sleep(100); // wait for things to stabilize + Assert.assertTrue("Servers didn't bounce", waitForConnect(zk, 15000)); + try { + zk.getData("/simpleCase", false, stat); + } catch(ConnectionLossException e) { + Assert.assertTrue("Servers didn't bounce", waitForConnect(zk, 15000)); + } + + // check that the change has propagated to everyone + for(int i = 0; i < getClientCount(); i++) { + for(int j = 0; j < maxTries; j++) { + byte[] data = zk.getData("/simpleCase/" + i, false, stat); + if (new String(data).equals("new")) { + break; + } + if (j+1 == maxTries) { + Assert.fail("max tries exceeded for " + i); + } + Thread.sleep(1000); + } + } + + // send out the kill signal + zk.setData("/simpleCase", "die".getBytes(), -1); + + // watch for everyone to die + for(int i = 0; i < getClientCount(); i++) { + try { + for(int j = 0; j < maxTries; j++) { + zk.getData("/simpleCase/" + i, false, stat); + if (j+1 == maxTries) { + Assert.fail("max tries exceeded waiting for child " + i + " to die"); + } + Thread.sleep(200); + } + } catch(NoNodeException e) { + // Great this is what we were hoping for! + } + } + + stopClients(); + stopServers(); + } + + public void process(WatchedEvent event) { + if (event.getState() == KeeperState.SyncConnected) { + synchronized(this) { + connected = true; + notifyAll(); + } + } else if (event.getState() == KeeperState.Disconnected) { + synchronized(this) { + connected = false; + notifyAll(); + } + } + } +} diff --git a/zookeeper-jute/pom.xml b/zookeeper-jute/pom.xml new file mode 100644 index 0000000..409ea4d --- /dev/null +++ b/zookeeper-jute/pom.xml @@ -0,0 +1,157 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + .. + + + zookeeper-jute + jar + Apache ZooKeeper - Jute + ZooKeeper jute + + + + org.apache.yetus + audience-annotations + + + junit + junit + test + + + + + + + org.codehaus.mojo + javacc-maven-plugin + 2.6 + + + generate-sources + javacc + + javacc + + + ${project.basedir}/src/main/java/org/apache/jute/compiler/generated/ + + rcc.jj + + 2 + false + ${project.build.directory}/generated-sources/java + + + + + + maven-compiler-plugin + + + pre-compile-jute + generate-sources + + compile + + + + + + org.codehaus.mojo + exec-maven-plugin + + + generate-Java-Jute + generate-sources + + exec + + + ${project.build.directory}/generated-sources/java + java + + -classpath + + org.apache.jute.compiler.generated.Rcc + -l + java + ${project.basedir}/src/main/resources/zookeeper.jute + + + + + generate-C-Jute + generate-sources + + exec + + + ${project.basedir}/../zookeeper-client/zookeeper-client-c/generated/ + java + + -classpath + + org.apache.jute.compiler.generated.Rcc + -l + c + ${project.basedir}/src/main/resources/zookeeper.jute + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + jute-as-dependency + generate-sources + + add-source + + + + ${basedir}/target/generated-sources/java + + + + + + + + com.github.spotbugs + spotbugs-maven-plugin + + true + + + + + + diff --git a/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java b/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java new file mode 100644 index 0000000..791bcb5 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/BinaryInputArchive.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +/** + * + */ +public class BinaryInputArchive implements InputArchive { + + public static final String UNREASONBLE_LENGTH = "Unreasonable length = "; + + // CHECKSTYLE.OFF: ConstantName - for backward compatibility + public static final int maxBuffer = Integer.getInteger("jute.maxbuffer", 0xfffff); + // CHECKSTYLE.ON: + private static final int extraMaxBuffer; + + static { + final Integer configuredExtraMaxBuffer = Integer.getInteger("zookeeper.jute.maxbuffer.extrasize", maxBuffer); + if(configuredExtraMaxBuffer < 1024) { + // Earlier hard coded value was 1024, So the value should not be less than that value + extraMaxBuffer = 1024; + } else { + extraMaxBuffer = configuredExtraMaxBuffer; + } + } + + private DataInput in; + private int maxBufferSize; + private int extraMaxBufferSize; + + public static BinaryInputArchive getArchive(InputStream strm) { + return new BinaryInputArchive(new DataInputStream(strm)); + } + + private static class BinaryIndex implements Index { + private int nelems; + + BinaryIndex(int nelems) { + this.nelems = nelems; + } + + public boolean done() { + return (nelems <= 0); + } + + public void incr() { + nelems--; + } + } + + /** + * Creates a new instance of BinaryInputArchive. + */ + public BinaryInputArchive(DataInput in) { + this(in, maxBuffer, extraMaxBuffer); + } + + public BinaryInputArchive(DataInput in, int maxBufferSize, int extraMaxBufferSize) { + this.in = in; + this.maxBufferSize = maxBufferSize; + this.extraMaxBufferSize = extraMaxBufferSize; + } + + public byte readByte(String tag) throws IOException { + return in.readByte(); + } + + public boolean readBool(String tag) throws IOException { + return in.readBoolean(); + } + + public int readInt(String tag) throws IOException { + return in.readInt(); + } + + public long readLong(String tag) throws IOException { + return in.readLong(); + } + + public float readFloat(String tag) throws IOException { + return in.readFloat(); + } + + public double readDouble(String tag) throws IOException { + return in.readDouble(); + } + + public String readString(String tag) throws IOException { + int len = in.readInt(); + if(len == -1) { + return null; + } + checkLength(len); + byte[] b = new byte[len]; + in.readFully(b); + return new String(b, StandardCharsets.UTF_8); + } + + public byte[] readBuffer(String tag) throws IOException { + int len = readInt(tag); + if(len == -1) { + return null; + } + checkLength(len); + byte[] arr = new byte[len]; + in.readFully(arr); + return arr; + } + + public void readRecord(Record r, String tag) throws IOException { + r.deserialize(this, tag); + } + + public void startRecord(String tag) throws IOException { + } + + public void endRecord(String tag) throws IOException { + } + + public Index startVector(String tag) throws IOException { + int len = readInt(tag); + if(len == -1) { + return null; + } + return new BinaryIndex(len); + } + + public void endVector(String tag) throws IOException { + } + + public Index startMap(String tag) throws IOException { + return new BinaryIndex(readInt(tag)); + } + + public void endMap(String tag) throws IOException { + } + + // Since this is a rough sanity check, add some padding to maxBuffer to + // make up for extra fields, etc. (otherwise e.g. clients may be able to + // write buffers larger than we can read from disk!) + private void checkLength(int len) throws IOException { + if(len < 0 || len > maxBufferSize + extraMaxBufferSize) { + throw new IOException(UNREASONBLE_LENGTH + len); + } + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/BinaryOutputArchive.java b/zookeeper-jute/src/main/java/org/apache/jute/BinaryOutputArchive.java new file mode 100644 index 0000000..4752d84 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/BinaryOutputArchive.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.DataOutput; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.TreeMap; + +/** + * + */ +public class BinaryOutputArchive implements OutputArchive { + private ByteBuffer bb = ByteBuffer.allocate(1024); + + private DataOutput out; + + public static BinaryOutputArchive getArchive(OutputStream strm) { + return new BinaryOutputArchive(new DataOutputStream(strm)); + } + + /** + * Creates a new instance of BinaryOutputArchive. + */ + public BinaryOutputArchive(DataOutput out) { + this.out = out; + } + + public void writeByte(byte b, String tag) throws IOException { + out.writeByte(b); + } + + public void writeBool(boolean b, String tag) throws IOException { + out.writeBoolean(b); + } + + public void writeInt(int i, String tag) throws IOException { + out.writeInt(i); + } + + public void writeLong(long l, String tag) throws IOException { + out.writeLong(l); + } + + public void writeFloat(float f, String tag) throws IOException { + out.writeFloat(f); + } + + public void writeDouble(double d, String tag) throws IOException { + out.writeDouble(d); + } + + /** + * create our own char encoder to utf8. This is faster + * then string.getbytes(UTF8). + * + * @param s the string to encode into utf8 + * @return utf8 byte sequence. + */ + private ByteBuffer stringToByteBuffer(CharSequence s) { + bb.clear(); + final int len = s.length(); + for (int i = 0; i < len; i++) { + if (bb.remaining() < 3) { + ByteBuffer n = ByteBuffer.allocate(bb.capacity() << 1); + bb.flip(); + n.put(bb); + bb = n; + } + char c = s.charAt(i); + if (c < 0x80) { + bb.put((byte) c); + } else if (c < 0x800) { + bb.put((byte) (0xc0 | (c >> 6))); + bb.put((byte) (0x80 | (c & 0x3f))); + } else { + bb.put((byte) (0xe0 | (c >> 12))); + bb.put((byte) (0x80 | ((c >> 6) & 0x3f))); + bb.put((byte) (0x80 | (c & 0x3f))); + } + } + bb.flip(); + return bb; + } + + public void writeString(String s, String tag) throws IOException { + if (s == null) { + writeInt(-1, "len"); + return; + } + ByteBuffer bb = stringToByteBuffer(s); + writeInt(bb.remaining(), "len"); + out.write(bb.array(), bb.position(), bb.limit()); + } + + public void writeBuffer(byte[] barr, String tag) + throws IOException { + if (barr == null) { + out.writeInt(-1); + return; + } + out.writeInt(barr.length); + out.write(barr); + } + + public void writeRecord(Record r, String tag) throws IOException { + r.serialize(this, tag); + } + + public void startRecord(Record r, String tag) throws IOException { + } + + public void endRecord(Record r, String tag) throws IOException { + } + + public void startVector(List v, String tag) throws IOException { + if (v == null) { + writeInt(-1, tag); + return; + } + writeInt(v.size(), tag); + } + + public void endVector(List v, String tag) throws IOException { + } + + public void startMap(TreeMap v, String tag) throws IOException { + writeInt(v.size(), tag); + } + + public void endMap(TreeMap v, String tag) throws IOException { + } + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/Index.java b/zookeeper-jute/src/main/java/org/apache/jute/Index.java new file mode 100644 index 0000000..b0f7e46 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/Index.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +/** + * Interface that acts as an iterator for deserializing maps. + * The deserializer returns an instance that the record uses to + * read vectors and maps. An example of usage is as follows: + * + * + * Index idx = startVector(...); + * while (!idx.done()) { + * .... // read element of a vector + * idx.incr(); + * } + * + * + */ +/************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 你把它当做 Iterator 理解就可以了 + */ +public interface Index { + boolean done(); // TODO_MA 注释: 等同于 hasNext() + void incr(); // TODO_MA 注释: 等同于 next() +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/InputArchive.java b/zookeeper-jute/src/main/java/org/apache/jute/InputArchive.java new file mode 100644 index 0000000..5d721e4 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/InputArchive.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; + +/** + * Interface that all the Deserializers have to implement. + * // TODO_MA 注释: 关于 InputArchive, 新版本只保留了一种实现: BinaryInputArchive + * // TODO_MA 注释: 这个保留的,就是 zookeeper-3.4.x版本中的默认实现 + * // TODO_MA 注释: 另外两种: XML,CSV + */ +public interface InputArchive { + + byte readByte(String tag) throws IOException; + + boolean readBool(String tag) throws IOException; + + int readInt(String tag) throws IOException; + + long readLong(String tag) throws IOException; + + float readFloat(String tag) throws IOException; + + double readDouble(String tag) throws IOException; + + String readString(String tag) throws IOException; + + byte[] readBuffer(String tag) throws IOException; + + void readRecord(Record r, String tag) throws IOException; + + void startRecord(String tag) throws IOException; + + void endRecord(String tag) throws IOException; + + Index startVector(String tag) throws IOException; + + void endVector(String tag) throws IOException; + + Index startMap(String tag) throws IOException; + + void endMap(String tag) throws IOException; + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/OutputArchive.java b/zookeeper-jute/src/main/java/org/apache/jute/OutputArchive.java new file mode 100644 index 0000000..b076ca1 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/OutputArchive.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; +import java.util.List; +import java.util.TreeMap; + +/** + * Interface that all the serializers have to implement. + * + */ +public interface OutputArchive { + + void writeByte(byte b, String tag) throws IOException; + + void writeBool(boolean b, String tag) throws IOException; + + void writeInt(int i, String tag) throws IOException; + + void writeLong(long l, String tag) throws IOException; + + void writeFloat(float f, String tag) throws IOException; + + void writeDouble(double d, String tag) throws IOException; + + void writeString(String s, String tag) throws IOException; + + void writeBuffer(byte[] buf, String tag) + throws IOException; + + void writeRecord(Record r, String tag) throws IOException; + + void startRecord(Record r, String tag) throws IOException; + + void endRecord(Record r, String tag) throws IOException; + + void startVector(List v, String tag) throws IOException; + + void endVector(List v, String tag) throws IOException; + + void startMap(TreeMap v, String tag) throws IOException; + + void endMap(TreeMap v, String tag) throws IOException; + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/Record.java b/zookeeper-jute/src/main/java/org/apache/jute/Record.java new file mode 100644 index 0000000..a55f896 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/Record.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * Interface that is implemented by generated classes. + */ +@InterfaceAudience.Public +public interface Record { + void serialize(OutputArchive archive, String tag) throws IOException; + void deserialize(InputArchive archive, String tag) throws IOException; +} + + +/************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: tag 是一个标记 + * 持久化对象/实例到磁盘的时候: 一个 实例 会变成一个抽象的 map + * "id" => value + * "name" => value + * - + * 将来在阅读 ZKDatabase 冷启动数据恢复的时候,会看到这个工作机制 + */ +class Student implements Record{ + + private int id; + private String name; + private Student student; + + @Override + public void serialize(OutputArchive archive, String tag) throws IOException { + archive.writeInt(id, ""); + archive.writeString(name, ""); + } + + @Override + public void deserialize(InputArchive archive, String tag) throws IOException { + this.id = archive.readInt(""); + this.name = archive.readString(""); + } +} + + +// TODO_MA 马中华 注释: ZK 内部有一个数据库:ZKDataBase +// TODO_MA 马中华 注释: 冷启动的时候,ZKDataBase 需要从 磁盘读取 DataNode 对象恢复到内存中 涉及到反序列化 +// TODO_MA 马中华 注释: ZKDataBase DataTree, 保存一些其他的信息,通过这个标记! +// TODO_MA 马中华 注释: Hadoop SequenceFile (序列化格式的 : key=value) \ No newline at end of file diff --git a/zookeeper-jute/src/main/java/org/apache/jute/RecordReader.java b/zookeeper-jute/src/main/java/org/apache/jute/RecordReader.java new file mode 100644 index 0000000..2c91d2e --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/RecordReader.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; + +/** + * Front-end interface to deserializers. Also acts as a factory + * for deserializers. + */ +public class RecordReader { + + private static HashMap archiveFactory; + + private InputArchive archive; + + static { + archiveFactory = new HashMap<>(); + + try { + archiveFactory.put( + "binary", + BinaryInputArchive.class.getDeclaredMethod("getArchive", InputStream.class)); + } catch (SecurityException | NoSuchMethodException ex) { + ex.printStackTrace(); + } + } + + private static InputArchive createArchive(InputStream in, String format) { + Method factory = archiveFactory.get(format); + + if (factory != null) { + Object[] params = {in}; + try { + return (InputArchive) factory.invoke(null, params); + } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException ex) { + ex.printStackTrace(); + } + } + + return null; + } + + /** + * Creates a new instance of RecordReader. + * + * @param in Stream from which to deserialize a record + * @param format Deserialization format ("binary", "xml", or "csv") + */ + public RecordReader(InputStream in, String format) { + archive = createArchive(in, format); + } + + /** + * Deserialize a record. + * + * @param r Record to be deserialized + */ + public void read(Record r) throws IOException { + r.deserialize(archive, ""); + } + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/RecordWriter.java b/zookeeper-jute/src/main/java/org/apache/jute/RecordWriter.java new file mode 100644 index 0000000..259a683 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/RecordWriter.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.HashMap; + +/** + * Front-end for serializers. Also serves as a factory for serializers. + */ +public class RecordWriter { + + private OutputArchive archive; + + static HashMap constructFactory() { + HashMap factory = new HashMap(); + + try { + factory.put( + "binary", + BinaryOutputArchive.class.getDeclaredMethod("getArchive", OutputStream.class)); + } catch (SecurityException | NoSuchMethodException ex) { + ex.printStackTrace(); + } + + return factory; + } + + private static HashMap archiveFactory = constructFactory(); + + private static OutputArchive createArchive(OutputStream out, String format) { + Method factory = archiveFactory.get(format); + if (factory != null) { + Object[] params = {out}; + try { + return (OutputArchive) factory.invoke(null, params); + } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException ex) { + ex.printStackTrace(); + } + } + return null; + } + + /** + * Creates a new instance of RecordWriter. + * + * @param out Output stream where the records will be serialized + * @param format Serialization format ("binary", "xml", or "csv") + */ + public RecordWriter(OutputStream out, String format) { + archive = createArchive(out, format); + } + + /** + * Serialize a record. + * + * @param r record to be serialized + */ + public void write(Record r) throws IOException { + r.serialize(archive, ""); + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/ToStringOutputArchive.java b/zookeeper-jute/src/main/java/org/apache/jute/ToStringOutputArchive.java new file mode 100644 index 0000000..a144011 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/ToStringOutputArchive.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import java.util.List; +import java.util.TreeMap; + +/** + * + */ +public class ToStringOutputArchive implements OutputArchive { + + private PrintStream stream; + private boolean isFirst = true; + + private void throwExceptionOnError(String tag) throws IOException { + if (stream.checkError()) { + throw new IOException("Error serializing " + tag); + } + } + + private void printCommaUnlessFirst() { + if (!isFirst) { + stream.print(","); + } + isFirst = false; + } + + /** + * Creates a new instance of ToStringOutputArchive. + */ + public ToStringOutputArchive(OutputStream out) throws UnsupportedEncodingException { + stream = new PrintStream(out, true, "UTF-8"); + } + + public void writeByte(byte b, String tag) throws IOException { + writeLong((long) b, tag); + } + + public void writeBool(boolean b, String tag) throws IOException { + printCommaUnlessFirst(); + String val = b ? "T" : "F"; + stream.print(val); + throwExceptionOnError(tag); + } + + public void writeInt(int i, String tag) throws IOException { + writeLong((long) i, tag); + } + + public void writeLong(long l, String tag) throws IOException { + printCommaUnlessFirst(); + stream.print(l); + throwExceptionOnError(tag); + } + + public void writeFloat(float f, String tag) throws IOException { + writeDouble((double) f, tag); + } + + public void writeDouble(double d, String tag) throws IOException { + printCommaUnlessFirst(); + stream.print(d); + throwExceptionOnError(tag); + } + + public void writeString(String s, String tag) throws IOException { + printCommaUnlessFirst(); + stream.print(escapeString(s)); + throwExceptionOnError(tag); + } + + public void writeBuffer(byte[] buf, String tag) + throws IOException { + printCommaUnlessFirst(); + stream.print(escapeBuffer(buf)); + throwExceptionOnError(tag); + } + + public void writeRecord(Record r, String tag) throws IOException { + if (r == null) { + return; + } + r.serialize(this, tag); + } + + public void startRecord(Record r, String tag) throws IOException { + if (tag != null && !"".equals(tag)) { + printCommaUnlessFirst(); + stream.print("s{"); + isFirst = true; + } + } + + public void endRecord(Record r, String tag) throws IOException { + if (tag == null || "".equals(tag)) { + stream.print("\n"); + isFirst = true; + } else { + stream.print("}"); + isFirst = false; + } + } + + public void startVector(List v, String tag) throws IOException { + printCommaUnlessFirst(); + stream.print("v{"); + isFirst = true; + } + + public void endVector(List v, String tag) throws IOException { + stream.print("}"); + isFirst = false; + } + + public void startMap(TreeMap v, String tag) throws IOException { + printCommaUnlessFirst(); + stream.print("m{"); + isFirst = true; + } + + public void endMap(TreeMap v, String tag) throws IOException { + stream.print("}"); + isFirst = false; + } + + private static String escapeString(String s) { + if (s == null) { + return ""; + } + + StringBuilder sb = new StringBuilder(s.length() + 1); + sb.append('\''); + int len = s.length(); + for (int i = 0; i < len; i++) { + char c = s.charAt(i); + switch (c) { + case '\0': + sb.append("%00"); + break; + case '\n': + sb.append("%0A"); + break; + case '\r': + sb.append("%0D"); + break; + case ',': + sb.append("%2C"); + break; + case '}': + sb.append("%7D"); + break; + case '%': + sb.append("%25"); + break; + default: + sb.append(c); + } + } + + return sb.toString(); + } + + private static String escapeBuffer(byte[] barr) { + if (barr == null || barr.length == 0) { + return ""; + } + + StringBuilder sb = new StringBuilder(barr.length + 1); + sb.append('#'); + + for (byte b : barr) { + sb.append(Integer.toHexString(b)); + } + + return sb.toString(); + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/Utils.java b/zookeeper-jute/src/main/java/org/apache/jute/Utils.java new file mode 100644 index 0000000..cf14f67 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/Utils.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +/** + * Various utility functions for Hadoop record I/O runtime. + */ +public class Utils { + + /** + * Cannot create a new instance of Utils. + */ + private Utils() { + super(); + } + + /** + * equals function that actually compares two buffers. + * + * @param onearray First buffer + * @param twoarray Second buffer + * @return true if one and two contain exactly the same content, else false. + */ + public static boolean bufEquals(byte[] onearray, byte[] twoarray) { + if (onearray == twoarray) { + return true; + } + + boolean ret = (onearray.length == twoarray.length); + + if (!ret) { + return ret; + } + + for (int idx = 0; idx < onearray.length; idx++) { + if (onearray[idx] != twoarray[idx]) { + return false; + } + } + return true; + } + + public static int compareBytes(byte[] b1, int off1, int len1, byte[] b2, int off2, int len2) { + int i; + for (i = 0; i < len1 && i < len2; i++) { + if (b1[off1 + i] != b2[off2 + i]) { + return b1[off1 + i] < b2[off2 + i] ? -1 : 1; + } + } + if (len1 != len2) { + return len1 < len2 ? -1 : 1; + } + return 0; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/CGenerator.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/CGenerator.java new file mode 100644 index 0000000..5dddcfb --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/CGenerator.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * C++ Code generator front-end for Hadoop record I/O. + */ +class CGenerator { + private String mName; + private List mInclFiles; + private List mRecList; + private final File outputDirectory; + + /** + * Creates a new instance of CppGenerator. + * + * @param name possibly full pathname to the file + * @param ilist included files (as JFile) + * @param rlist List of records defined within this file + * @param outputDirectory + */ + CGenerator(String name, List ilist, List rlist, + File outputDirectory) { + this.outputDirectory = outputDirectory; + mName = (new File(name)).getName(); + mInclFiles = ilist; + mRecList = rlist; + } + + /** + * Generate C++ code. This method only creates the requested file(s) + * and spits-out file-level elements (such as include statements etc.) + * record-level code is generated by JRecord. + */ + void genCode() throws IOException { + if (!outputDirectory.exists()) { + if (!outputDirectory.mkdirs()) { + throw new IOException("unable to create output directory " + + outputDirectory); + } + } + + try (FileWriter c = new FileWriter(new File(outputDirectory, mName + ".c")); + FileWriter h = new FileWriter(new File(outputDirectory, mName + ".h")); + ) { + h.write("/**\n"); + h.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + h.write("* or more contributor license agreements. See the NOTICE file\n"); + h.write("* distributed with this work for additional information\n"); + h.write("* regarding copyright ownership. The ASF licenses this file\n"); + h.write("* to you under the Apache License, Version 2.0 (the\n"); + h.write("* \"License\"); you may not use this file except in compliance\n"); + h.write("* with the License. You may obtain a copy of the License at\n"); + h.write("*\n"); + h.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + h.write("*\n"); + h.write("* Unless required by applicable law or agreed to in writing, software\n"); + h.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + h.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + h.write("* See the License for the specific language governing permissions and\n"); + h.write("* limitations under the License.\n"); + h.write("*/\n"); + h.write("\n"); + + c.write("/**\n"); + c.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + c.write("* or more contributor license agreements. See the NOTICE file\n"); + c.write("* distributed with this work for additional information\n"); + c.write("* regarding copyright ownership. The ASF licenses this file\n"); + c.write("* to you under the Apache License, Version 2.0 (the\n"); + c.write("* \"License\"); you may not use this file except in compliance\n"); + c.write("* with the License. You may obtain a copy of the License at\n"); + c.write("*\n"); + c.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + c.write("*\n"); + c.write("* Unless required by applicable law or agreed to in writing, software\n"); + c.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + c.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + c.write("* See the License for the specific language governing permissions and\n"); + c.write("* limitations under the License.\n"); + c.write("*/\n"); + c.write("\n"); + + h.write("#ifndef __" + mName.toUpperCase().replace('.', '_') + "__\n"); + h.write("#define __" + mName.toUpperCase().replace('.', '_') + "__\n"); + + h.write("#include \"recordio.h\"\n"); + for (Iterator i = mInclFiles.iterator(); i.hasNext(); ) { + JFile f = i.next(); + h.write("#include \"" + f.getName() + ".h\"\n"); + } + // required for compilation from C++ + h.write("\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); + + c.write("#include \n"); // need it for calloc() & free() + c.write("#include \"" + mName + ".h\"\n\n"); + + for (Iterator i = mRecList.iterator(); i.hasNext(); ) { + JRecord jr = i.next(); + jr.genCCode(h, c); + } + + h.write("\n#ifdef __cplusplus\n}\n#endif\n\n"); + h.write("#endif //" + mName.toUpperCase().replace('.', '_') + "__\n"); + } + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/CSharpGenerator.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/CSharpGenerator.java new file mode 100644 index 0000000..f698ff3 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/CSharpGenerator.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * + */ +public class CSharpGenerator { + private List mRecList; + private final File outputDirectory; + + /** + * Creates a new instance of CSharpGenerator. + * + * @param name possibly full pathname to the file + * @param ilist included files (as JFile) + * @param rlist List of records defined within this file + * @param outputDirectory + */ + CSharpGenerator(String name, List ilist, List rlist, + File outputDirectory) { + this.outputDirectory = outputDirectory; + mRecList = rlist; + } + + /** + * Generate C# code. This method only creates the requested file(s) + * and spits-out file-level elements (such as include statements etc.) + * record-level code is generated by JRecord. + */ + void genCode() throws IOException { + for (JRecord rec : mRecList) { + rec.genCsharpCode(outputDirectory); + } + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/CppGenerator.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/CppGenerator.java new file mode 100644 index 0000000..8376654 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/CppGenerator.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * C++ Code generator front-end for Hadoop record I/O. + */ +class CppGenerator { + private String mName; + private List mInclFiles; + private List mRecList; + private final File outputDirectory; + + /** + * Creates a new instance of CppGenerator. + * + * @param name possibly full pathname to the file + * @param ilist included files (as JFile) + * @param rlist List of records defined within this file + * @param outputDirectory + */ + CppGenerator(String name, List ilist, List rlist, + File outputDirectory) { + this.outputDirectory = outputDirectory; + mName = (new File(name)).getName(); + mInclFiles = ilist; + mRecList = rlist; + } + + /** + * Generate C++ code. This method only creates the requested file(s) + * and spits-out file-level elements (such as include statements etc.) + * record-level code is generated by JRecord. + */ + void genCode() throws IOException { + if (!outputDirectory.exists()) { + if (!outputDirectory.mkdirs()) { + throw new IOException("unable to create output directory " + + outputDirectory); + } + } + + try (FileWriter cc = new FileWriter(new File(outputDirectory, mName + ".cc")); + FileWriter hh = new FileWriter(new File(outputDirectory, mName + ".hh")); + ) { + hh.write("/**\n"); + hh.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + hh.write("* or more contributor license agreements. See the NOTICE file\n"); + hh.write("* distributed with this work for additional information\n"); + hh.write("* regarding copyright ownership. The ASF licenses this file\n"); + hh.write("* to you under the Apache License, Version 2.0 (the\n"); + hh.write("* \"License\"); you may not use this file except in compliance\n"); + hh.write("* with the License. You may obtain a copy of the License at\n"); + hh.write("*\n"); + hh.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + hh.write("*\n"); + hh.write("* Unless required by applicable law or agreed to in writing, software\n"); + hh.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + hh.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + hh.write("* See the License for the specific language governing permissions and\n"); + hh.write("* limitations under the License.\n"); + hh.write("*/\n"); + hh.write("\n"); + + cc.write("/**\n"); + cc.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + cc.write("* or more contributor license agreements. See the NOTICE file\n"); + cc.write("* distributed with this work for additional information\n"); + cc.write("* regarding copyright ownership. The ASF licenses this file\n"); + cc.write("* to you under the Apache License, Version 2.0 (the\n"); + cc.write("* \"License\"); you may not use this file except in compliance\n"); + cc.write("* with the License. You may obtain a copy of the License at\n"); + cc.write("*\n"); + cc.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + cc.write("*\n"); + cc.write("* Unless required by applicable law or agreed to in writing, software\n"); + cc.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + cc.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + cc.write("* See the License for the specific language governing permissions and\n"); + cc.write("* limitations under the License.\n"); + cc.write("*/\n"); + cc.write("\n"); + + hh.write("#ifndef __" + mName.toUpperCase().replace('.', '_') + "__\n"); + hh.write("#define __" + mName.toUpperCase().replace('.', '_') + "__\n"); + + hh.write("#include \"recordio.hh\"\n"); + for (Iterator i = mInclFiles.iterator(); i.hasNext(); ) { + JFile f = i.next(); + hh.write("#include \"" + f.getName() + ".hh\"\n"); + } + cc.write("#include \"" + mName + ".hh\"\n"); + + for (Iterator i = mRecList.iterator(); i.hasNext(); ) { + JRecord jr = i.next(); + jr.genCppCode(hh, cc); + } + + hh.write("#endif //" + mName.toUpperCase().replace('.', '_') + "__\n"); + } + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JBoolean.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JBoolean.java new file mode 100644 index 0000000..2d10ec4 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JBoolean.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JBoolean extends JType { + + /** + * Creates a new instance of JBoolean. + */ + public JBoolean() { + super("int32_t", "bool", "bool", "boolean", "Bool", "Boolean", "bool", "toBoolean"); + } + + public String getSignature() { + return "z"; + } + + public String genJavaCompareTo(String fname) { + return " ret = (" + fname + " == peer." + fname + ")? 0 : (" + fname + "?1:-1);\n"; + } + + public String genJavaHashCode(String fname) { + return " ret = (" + fname + ")?0:1;\n"; + } + + String genCsharpHashCode(String fname) { + return " ret = (" + capitalize(fname) + ")?0:1;\n"; + } + + String genCsharpCompareTo(String name) { + return " ret = (" + capitalize(name) + " == peer." + capitalize(name) + ")? 0 : (" + capitalize(name) + "?1:-1);\n"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JBuffer.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JBuffer.java new file mode 100644 index 0000000..92274ed --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JBuffer.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JBuffer extends JCompType { + + /** + * Creates a new instance of JBuffer. + */ + public JBuffer() { + super("struct buffer", " ::std::string", "byte[]", "byte[]", "Buffer", "byte[]", "byte[]"); + } + + public String genCppGetSet(String fname, int fIdx) { + String cgetFunc = " virtual const " + getCppType() + "& get" + fname + "() const {\n"; + cgetFunc += " return m" + fname + ";\n"; + cgetFunc += " }\n"; + String getFunc = " virtual " + getCppType() + "& get" + fname + "() {\n"; + getFunc += " bs_.set(" + fIdx + ");return m" + fname + ";\n"; + getFunc += " }\n"; + return cgetFunc + getFunc; + } + + public String getSignature() { + return "B"; + } + + public String genJavaReadWrapper(String fname, String tag, boolean decl) { + String ret = ""; + if (decl) { + ret = " byte[] " + fname + ";\n"; + } + return ret + " " + fname + "=a_.readBuffer(\"" + tag + "\");\n"; + } + + public String genJavaWriteWrapper(String fname, String tag) { + return " a_.writeBuffer(" + fname + ",\"" + tag + "\");\n"; + } + + public String genJavaCompareTo(String fname, String other) { + StringBuilder sb = new StringBuilder(); + sb.append(" {\n"); + sb.append(" byte[] my = " + fname + ";\n"); + sb.append(" byte[] ur = " + other + ";\n"); + sb.append(" ret = org.apache.jute.Utils.compareBytes(my,0,my.length,ur,0,ur.length);\n"); + sb.append(" }\n"); + return sb.toString(); + } + + public String genJavaCompareTo(String fname) { + return genJavaCompareTo(fname, "peer." + fname); + } + + public String genJavaCompareToWrapper(String fname, String other) { + return " " + genJavaCompareTo(fname, other); + } + + public String genJavaEquals(String fname, String peer) { + return " ret = org.apache.jute.Utils.bufEquals(" + fname + "," + peer + ");\n"; + } + + public String genJavaHashCode(String fname) { + return " ret = java.util.Arrays.toString(" + fname + ").hashCode();\n"; + } + + public String genJavaSlurpBytes(String b, String s, String l) { + StringBuilder sb = new StringBuilder(); + sb.append(" {\n"); + sb.append(" int i = org.apache.jute.Utils.readVInt(" + b + ", " + s + ");\n"); + sb.append(" int z = WritableUtils.getVIntSize(i);\n"); + sb.append(" " + s + " += z+i; " + l + " -= (z+i);\n"); + sb.append(" }\n"); + return sb.toString(); + } + + public String genJavaCompareBytes() { + StringBuilder sb = new StringBuilder(); + sb.append(" {\n"); + sb.append(" int i1 = org.apache.jute.Utils.readVInt(b1, s1);\n"); + sb.append(" int i2 = org.apache.jute.Utils.readVInt(b2, s2);\n"); + sb.append(" int z1 = WritableUtils.getVIntSize(i1);\n"); + sb.append(" int z2 = WritableUtils.getVIntSize(i2);\n"); + sb.append(" s1+=z1; s2+=z2; l1-=z1; l2-=z2;\n"); + sb.append(" int r1 = org.apache.jute.Utils.compareBytes(b1,s1,l1,b2,s2,l2);\n"); + sb.append(" if (r1 != 0) { return (r1<0)?-1:0; }\n"); + sb.append(" s1+=i1; s2+=i2; l1-=i1; l1-=i2;\n"); + sb.append(" }\n"); + return sb.toString(); + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JByte.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JByte.java new file mode 100644 index 0000000..cc8a737 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JByte.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JByte extends JType { + + /** + * Creates a new instance of JByte. + */ + public JByte() { + super("char", "int8_t", "byte", "byte", "Byte", "Byte", "byte", "toByte"); + } + + public String getSignature() { + return "b"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JCompType.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JCompType.java new file mode 100644 index 0000000..ebe95a2 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JCompType.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * Abstract base class for all the "compound" types such as ustring, + * buffer, vector, map, and record. + */ +abstract class JCompType extends JType { + + /** + * Creates a new instance of JCompType. + */ + JCompType( + String cType, + String cppType, + String csharpType, + String javaType, + String suffix, + String wrapper, + String csharpWrapper + ) { + super(cType, cppType, csharpType, javaType, suffix, wrapper, csharpWrapper, null); + } + + String genCppGetSet(String fname, int fIdx) { + String cgetFunc = " virtual const " + getCppType() + "& get" + fname + "() const {\n"; + cgetFunc += " return m" + fname + ";\n"; + cgetFunc += " }\n"; + String getFunc = " virtual " + getCppType() + "& get" + fname + "() {\n"; + getFunc += " bs_.set(" + fIdx + ");return m" + fname + ";\n"; + getFunc += " }\n"; + return cgetFunc + getFunc; + } + + String genJavaCompareTo(String fname) { + return " ret = " + fname + ".compareTo(peer." + fname + ");\n"; + } + + String genJavaEquals(String fname, String peer) { + return " ret = " + fname + ".equals(" + peer + ");\n"; + } + + String genJavaHashCode(String fname) { + return " ret = " + fname + ".hashCode();\n"; + } + + String genCsharpHashCode(String fname) { + return " ret = " + capitalize(fname) + ".GetHashCode();\n"; + } + + String genCsharpEquals(String name, String peer) { + String[] peerSplit = peer.split("\\."); + return " ret = " + capitalize(name) + ".Equals(" + peerSplit[0] + "." + capitalize(peerSplit[1]) + ");\n"; + } + + String genCsharpCompareTo(String name) { + return " ret = " + capitalize(name) + ".CompareTo(peer." + capitalize(name) + ");\n"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JDouble.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JDouble.java new file mode 100644 index 0000000..4e22ff3 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JDouble.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JDouble extends JType { + + /** + * Creates a new instance of JDouble. + */ + public JDouble() { + super("double", "double", "double", "double", "Double", "Double", "double", "toDouble"); + } + + public String getSignature() { + return "d"; + } + + public String genJavaHashCode(String fname) { + String tmp = "Double.doubleToLongBits(" + fname + ")"; + return " ret = (int)(" + tmp + "^(" + tmp + ">>>32));\n"; + } + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JField.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JField.java new file mode 100644 index 0000000..395c0b6 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JField.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JField { + private JType mType; + private String mName; + + /** + * Creates a new instance of JField. + */ + public JField(JType type, String name) { + mType = type; + mName = name; + } + + public String getSignature() { + return mType.getSignature(); + } + + public String genCppDecl() { + return mType.genCppDecl(mName); + } + + public String genCDecl() { + return mType.genCDecl(mName); + } + + public String genCsharpDecl() { + return mType.genCsharpDecl(mName); + } + + public String genCsharpConstructorParam(String fname) { + return mType.genCsharpConstructorParam(fname); + } + + public String genJavaDecl() { + return mType.genJavaDecl(mName); + } + + public String genJavaConstructorParam(String fname) { + return mType.genJavaConstructorParam(fname); + } + + public String getName() { + return mName; + } + + public String getCsharpName() { + return "Id".equals(mName) ? "ZKId" : mName; + } + + public String getTag() { + return mName; + } + + public JType getType() { + return mType; + } + + public String genCppGetSet(int fIdx) { + return mType.genCppGetSet(mName, fIdx); + } + + public String genCsharpConstructorSet(String fname) { + return mType.genCsharpConstructorSet(mName, fname); + } + + public String genCsharpGetSet(int fIdx) { + return mType.genCsharpGetSet(getCsharpName(), fIdx); + } + + public String genCsharpWriteMethodName() { + return mType.genCsharpWriteMethod(getCsharpName(), getTag()); + } + + public String genCsharpReadMethodName() { + return mType.genCsharpReadMethod(getCsharpName(), getTag()); + } + + public String genCsharpCompareTo() { + return mType.genCsharpCompareTo(getCsharpName()); + } + + public String genCsharpEquals() { + return mType.genCsharpEquals(getCsharpName(), "peer." + getCsharpName()); + } + + public String genCsharpHashCode() { + return mType.genCsharpHashCode(getCsharpName()); + } + + + public String genJavaGetSet(int fIdx) { + return mType.genJavaGetSet(mName, fIdx); + } + + public String genJavaWriteMethodName() { + return mType.genJavaWriteMethod(getName(), getTag()); + } + + public String genJavaReadMethodName() { + return mType.genJavaReadMethod(getName(), getTag()); + } + + public String genJavaCompareTo() { + return mType.genJavaCompareTo(getName()); + } + + public String genJavaEquals() { + return mType.genJavaEquals(getName(), "peer." + getName()); + } + + public String genJavaHashCode() { + return mType.genJavaHashCode(getName()); + } + + public String genJavaConstructorSet(String fname) { + return mType.genJavaConstructorSet(mName, fname); + } + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JFile.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JFile.java new file mode 100644 index 0000000..1d5ed41 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JFile.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Container for the Hadoop Record DDL. + * The main components of the file are filename, list of included files, + * and records defined in that file. + */ +public class JFile { + + private String mName; + private List mInclFiles; + private List mRecords; + + /** + * Creates a new instance of JFile. + * + * @param name possibly full pathname to the file + * @param inclFiles included files (as JFile) + * @param recList List of records defined within this file + */ + public JFile(String name, ArrayList inclFiles, + ArrayList recList) { + mName = name; + mInclFiles = inclFiles; + mRecords = recList; + } + + /** + * Strip the other pathname components and return the basename. + */ + String getName() { + int idx = mName.lastIndexOf('/'); + return (idx > 0) ? mName.substring(idx) : mName; + } + + /** + * Generate record code in given language. Language should be all + * lowercase. + * + * @param outputDirectory + */ + public void genCode(String language, File outputDirectory) + throws IOException { + if ("c++".equals(language)) { + CppGenerator gen = new CppGenerator(mName, mInclFiles, mRecords, + outputDirectory); + gen.genCode(); + } else if ("java".equals(language)) { + JavaGenerator gen = new JavaGenerator(mName, mInclFiles, mRecords, + outputDirectory); + gen.genCode(); + } else if ("c".equals(language)) { + CGenerator gen = new CGenerator(mName, mInclFiles, mRecords, + outputDirectory); + gen.genCode(); + } else if ("csharp".equals(language)) { + CSharpGenerator gen = new CSharpGenerator(mName, mInclFiles, mRecords, + outputDirectory); + gen.genCode(); + } else { + throw new IOException("Cannnot recognize language:" + language); + } + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JFloat.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JFloat.java new file mode 100644 index 0000000..c4452f1 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JFloat.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JFloat extends JType { + + /** + * Creates a new instance of JFloat. + */ + public JFloat() { + super("float", "float", "float", "float", "Float", "Float", "float", "toFloat"); + } + + public String getSignature() { + return "f"; + } + + public String genJavaHashCode(String fname) { + return " ret = Float.floatToIntBits(" + fname + ");\n"; + } + +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JInt.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JInt.java new file mode 100644 index 0000000..5632911 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JInt.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JInt extends JType { + + /** + * Creates a new instance of JInt. + */ + public JInt() { + super("int32_t", "int32_t", "int", "int", "Int", "Integer", "int", "toInt"); + } + + public String getSignature() { + return "i"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JLong.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JLong.java new file mode 100644 index 0000000..4563789 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JLong.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JLong extends JType { + + /** + * Creates a new instance of JLong. + */ + public JLong() { + super("int64_t", "int64_t", "long", "long", "Long", "Long", "long", "toLong"); + } + + public String getSignature() { + return "l"; + } + + public String genJavaHashCode(String fname) { + return " ret = (int) (" + fname + "^(" + fname + ">>>32));\n"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JMap.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JMap.java new file mode 100644 index 0000000..71c9642 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JMap.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JMap extends JCompType { + + private static int level = 0; + + private static String getLevel() { + return Integer.toString(level); + } + + private static void incrLevel() { + level++; + } + + private static void decrLevel() { + level--; + } + + private static String getId(String id) { + return id + getLevel(); + } + + private JType mKey; + private JType mValue; + + /** + * Creates a new instance of JMap. + */ + public JMap(JType t1, JType t2) { + super("#error", " ::std::map<" + t1.getCppType() + "," + t2.getCppType() + ">", + "System.Collections.Generic.SortedDictionary", "java.util.TreeMap", "Map", "System.Collections.Generic.SortedDictionary", "java.util.TreeMap"); + mKey = t1; + mValue = t2; + } + + public String getSignature() { + return "{" + mKey.getSignature() + mValue.getSignature() + "}"; + } + + public String genJavaCompareTo(String fname) { + return " throw new UnsupportedOperationException(\"comparing " + + fname + " is unimplemented\");\n"; + } + + public String genJavaReadWrapper(String fname, String tag, boolean decl) { + StringBuilder ret = new StringBuilder(""); + if (decl) { + ret.append(" java.util.TreeMap " + fname + ";\n"); + } + ret.append(" {\n"); + incrLevel(); + ret.append(" org.apache.jute.Index " + getId("midx") + " = a_.startMap(\"" + tag + "\");\n"); + ret.append(" " + fname + "=new java.util.TreeMap();\n"); + ret.append(" for (; !" + getId("midx") + ".done(); " + getId("midx") + ".incr()) {\n"); + ret.append(mKey.genJavaReadWrapper(getId("k"), getId("k"), true)); + ret.append(mValue.genJavaReadWrapper(getId("v"), getId("v"), true)); + ret.append(" " + fname + ".put(" + getId("k") + "," + getId("v") + ");\n"); + ret.append(" }\n"); + ret.append(" a_.endMap(\"" + tag + "\");\n"); + decrLevel(); + ret.append(" }\n"); + return ret.toString(); + } + + public String genJavaReadMethod(String fname, String tag) { + return genJavaReadWrapper(fname, tag, false); + } + + public String genJavaWriteWrapper(String fname, String tag) { + StringBuilder ret = new StringBuilder(" {\n"); + incrLevel(); + ret.append(" a_.startMap(" + fname + ",\"" + tag + "\");\n"); + ret.append(" java.util.Set " + getId("es") + " = " + fname + ".entrySet();\n"); + ret.append(" for(java.util.Iterator " + getId("midx") + " = " + getId("es") + ".iterator(); " + getId("midx") + ".hasNext(); ) {\n"); + ret.append(" java.util.Map.Entry " + getId("me") + " = (java.util.Map.Entry) " + getId("midx") + ".next();\n"); + ret.append(" " + mKey.getJavaWrapperType() + " " + getId("k") + " = (" + mKey.getJavaWrapperType() + ") " + getId("me") + ".getKey();\n"); + ret.append(" " + mValue.getJavaWrapperType() + " " + getId("v") + " = (" + mValue.getJavaWrapperType() + ") " + getId("me") + ".getValue();\n"); + ret.append(mKey.genJavaWriteWrapper(getId("k"), getId("k"))); + ret.append(mValue.genJavaWriteWrapper(getId("v"), getId("v"))); + ret.append(" }\n"); + ret.append(" a_.endMap(" + fname + ",\"" + tag + "\");\n"); + ret.append(" }\n"); + decrLevel(); + return ret.toString(); + } + + public String genJavaWriteMethod(String fname, String tag) { + return genJavaWriteWrapper(fname, tag); + } + + public String genCsharpWriteWrapper(String fname, int tag) { + StringBuilder ret = new StringBuilder(" {\n"); + incrLevel(); + ret.append(" a_.StartMap(" + fname + ",\"" + tag + "\");\n"); + ret.append(" java.util.Set " + getId("es") + " = " + fname + ".entrySet();\n"); + ret.append(" for(java.util.Iterator " + getId("midx") + " = " + getId("es") + ".iterator(); " + getId("midx") + ".hasNext(); ) {\n"); + ret.append(" java.util.Map.Entry " + getId("me") + " = (java.util.Map.Entry) " + getId("midx") + ".next();\n"); + ret.append(" " + mKey.getCsharpWrapperType() + " " + getId("k") + " = (" + mKey.getCsharpWrapperType() + ") " + getId("me") + ".getKey();\n"); + ret.append(" " + mValue.getCsharpWrapperType() + " " + getId("v") + " = (" + mValue.getCsharpWrapperType() + ") " + getId("me") + ".getValue();\n"); + ret.append(mKey.genCsharpWriteWrapper(getId("k"), getId("k"))); + ret.append(mValue.genCsharpWriteWrapper(getId("v"), getId("v"))); + ret.append(" }\n"); + ret.append(" a_.EndMap(" + fname + ",\"" + tag + "\");\n"); + ret.append(" }\n"); + decrLevel(); + return ret.toString(); + } + + String genCsharpWriteMethod(String fname, int tag) { + return genCsharpWriteWrapper(fname, tag); + } + + public String genCsharpReadWrapper(String fname, int tag, boolean decl) { + StringBuilder ret = new StringBuilder(""); + if (decl) { + ret.append(" System.Collections.SortedDictionary " + capitalize(fname) + ";\n"); + } + ret.append(" {\n"); + incrLevel(); + ret.append(" Org.Apache.Jute.IIndex " + getId("midx") + " = a_.StartMap(\"" + tag + "\");\n"); + ret.append(" " + fname + "= new System.Collections.SortedDictionary();\n"); + ret.append(" for (; !" + getId("midx") + ".done(); " + getId("midx") + ".incr()) {\n"); + ret.append(mKey.genCsharpReadWrapper(getId("k"), getId("k"), true)); + ret.append(mValue.genCsharpReadWrapper(getId("v"), getId("v"), true)); + ret.append(" " + fname + ".Add(" + getId("k") + "," + getId("v") + ");\n"); + ret.append(" }\n"); + ret.append(" a_.EndMap(\"" + tag + "\");\n"); + decrLevel(); + ret.append(" }\n"); + return ret.toString(); + } + + + String genCsharpReadMethod(String fname, int tag) { + return genCsharpReadWrapper(fname, tag, false); + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java new file mode 100644 index 0000000..7c73ded --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JRecord.java @@ -0,0 +1,770 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** + * + */ +public class JRecord extends JCompType { + + private String mFQName; + private String mName; + private String mModule; + private List mFields; + + /** + * Creates a new instance of JRecord. + */ + public JRecord(String name, ArrayList flist) { + super("struct " + name.substring(name.lastIndexOf('.') + 1), + name.replaceAll("\\.", "::"), getCsharpFQName(name), name, "Record", name, getCsharpFQName("IRecord")); + mFQName = name; + int idx = name.lastIndexOf('.'); + mName = name.substring(idx + 1); + mModule = name.substring(0, idx); + mFields = flist; + } + + public String getName() { + return mName; + } + + public String getCsharpName() { + return "Id".equals(mName) ? "ZKId" : mName; + } + + public String getJavaFQName() { + return mFQName; + } + + public String getCppFQName() { + return mFQName.replaceAll("\\.", "::"); + } + + public String getJavaPackage() { + return mModule; + } + + public String getCppNameSpace() { + return mModule.replaceAll("\\.", "::"); + } + + public String getCsharpNameSpace() { + String[] parts = mModule.split("\\."); + StringBuffer namespace = new StringBuffer(); + for (int i = 0; i < parts.length; i++) { + String capitalized = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase(); + namespace.append(capitalized); + if (i != parts.length - 1) { + namespace.append("."); + } + } + return namespace.toString(); + } + + public List getFields() { + return mFields; + } + + public String getSignature() { + StringBuilder sb = new StringBuilder(); + sb.append("L").append(mName).append("("); + for (Iterator i = mFields.iterator(); i.hasNext(); ) { + String s = i.next().getSignature(); + sb.append(s); + } + sb.append(")"); + return sb.toString(); + } + + public String genCppDecl(String fname) { + return " " + getCppNameSpace() + "::" + mName + " m" + fname + ";\n"; + } + + public String genJavaReadMethod(String fname, String tag) { + return genJavaReadWrapper(fname, tag, false); + } + + public String genJavaReadWrapper(String fname, String tag, boolean decl) { + StringBuilder ret = new StringBuilder(""); + if (decl) { + ret.append(" " + getJavaFQName() + " " + fname + ";\n"); + } + ret.append(" " + fname + "= new " + getJavaFQName() + "();\n"); + ret.append(" a_.readRecord(" + fname + ",\"" + tag + "\");\n"); + return ret.toString(); + } + + public String genJavaWriteWrapper(String fname, String tag) { + return " a_.writeRecord(" + fname + ",\"" + tag + "\");\n"; + } + + String genCsharpReadMethod(String fname, String tag) { + //return " "+capitalize(fname)+"=a_.Read"+mMethodSuffix+"(" + capitalize(fname) + ",\""+tag+"\");\n"; + return genCsharpReadWrapper(capitalize(fname), tag, false); + } + + public String genCsharpReadWrapper(String fname, String tag, boolean decl) { + StringBuilder ret = new StringBuilder(""); + if (decl) { + ret.append(" " + getCsharpFQName(mFQName) + " " + fname + ";\n"); + } + ret.append(" " + fname + "= new " + getCsharpFQName(mFQName) + "();\n"); + ret.append(" a_.ReadRecord(" + fname + ",\"" + tag + "\");\n"); + return ret.toString(); + } + + public String genCsharpWriteWrapper(String fname, String tag) { + return " a_.WriteRecord(" + fname + ",\"" + tag + "\");\n"; + } + + static Map vectorStructs = new HashMap(); + + public void genCCode(FileWriter h, FileWriter c) throws IOException { + for (JField f : mFields) { + if (f.getType() instanceof JVector) { + JVector jv = (JVector) f.getType(); + JType jvType = jv.getElementType(); + String structName = JVector.extractVectorName(jvType); + if (vectorStructs.get(structName) == null) { + vectorStructs.put(structName, structName); + h.write("struct " + structName + " {\n int32_t count;\n" + jv.getElementType().genCDecl("*data") + "\n};\n"); + h.write("int serialize_" + structName + "(struct oarchive *out, const char *tag, struct " + structName + " *v);\n"); + h.write("int deserialize_" + structName + "(struct iarchive *in, const char *tag, struct " + structName + " *v);\n"); + h.write("int allocate_" + structName + "(struct " + structName + " *v, int32_t len);\n"); + h.write("int deallocate_" + structName + "(struct " + structName + " *v);\n"); + c.write("int allocate_" + structName + "(struct " + structName + " *v, int32_t len) {\n"); + c.write(" if (!len) {\n"); + c.write(" v->count = 0;\n"); + c.write(" v->data = 0;\n"); + c.write(" } else {\n"); + c.write(" v->count = len;\n"); + c.write(" v->data = calloc(sizeof(*v->data), len);\n"); + c.write(" }\n"); + c.write(" return 0;\n"); + c.write("}\n"); + c.write("int deallocate_" + structName + "(struct " + structName + " *v) {\n"); + c.write(" if (v->data) {\n"); + c.write(" int32_t i;\n"); + c.write(" for(i=0;icount; i++) {\n"); + c.write(" deallocate_" + JRecord.extractMethodSuffix(jvType) + "(&v->data[i]);\n"); + c.write(" }\n"); + c.write(" free(v->data);\n"); + c.write(" v->data = 0;\n"); + c.write(" }\n"); + c.write(" return 0;\n"); + c.write("}\n"); + c.write("int serialize_" + structName + "(struct oarchive *out, const char *tag, struct " + structName + " *v)\n"); + c.write("{\n"); + c.write(" int32_t count = v->count;\n"); + c.write(" int rc = 0;\n"); + c.write(" int32_t i;\n"); + c.write(" rc = out->start_vector(out, tag, &count);\n"); + c.write(" for(i=0;icount;i++) {\n"); + genSerialize(c, jvType, "data", "data[i]"); + c.write(" }\n"); + c.write(" rc = rc ? rc : out->end_vector(out, tag);\n"); + c.write(" return rc;\n"); + c.write("}\n"); + c.write("int deserialize_" + structName + "(struct iarchive *in, const char *tag, struct " + structName + " *v)\n"); + c.write("{\n"); + c.write(" int rc = 0;\n"); + c.write(" int32_t i;\n"); + c.write(" rc = in->start_vector(in, tag, &v->count);\n"); + c.write(" v->data = calloc(v->count, sizeof(*v->data));\n"); + c.write(" for(i=0;icount;i++) {\n"); + genDeserialize(c, jvType, "value", "data[i]"); + c.write(" }\n"); + c.write(" rc = in->end_vector(in, tag);\n"); + c.write(" return rc;\n"); + c.write("}\n"); + + } + } + } + String recName = getName(); + h.write("struct " + recName + " {\n"); + for (JField f : mFields) { + h.write(f.genCDecl()); + } + h.write("};\n"); + h.write("int serialize_" + recName + "(struct oarchive *out, const char *tag, struct " + recName + " *v);\n"); + h.write("int deserialize_" + recName + "(struct iarchive *in, const char *tag, struct " + recName + "*v);\n"); + h.write("void deallocate_" + recName + "(struct " + recName + "*);\n"); + c.write("int serialize_" + recName + "(struct oarchive *out, const char *tag, struct " + recName + " *v)"); + c.write("{\n"); + c.write(" int rc;\n"); + c.write(" rc = out->start_record(out, tag);\n"); + for (JField f : mFields) { + genSerialize(c, f.getType(), f.getTag(), f.getName()); + } + c.write(" rc = rc ? rc : out->end_record(out, tag);\n"); + c.write(" return rc;\n"); + c.write("}\n"); + c.write("int deserialize_" + recName + "(struct iarchive *in, const char *tag, struct " + recName + "*v)"); + c.write("{\n"); + c.write(" int rc;\n"); + c.write(" rc = in->start_record(in, tag);\n"); + for (JField f : mFields) { + genDeserialize(c, f.getType(), f.getTag(), f.getName()); + } + c.write(" rc = rc ? rc : in->end_record(in, tag);\n"); + c.write(" return rc;\n"); + c.write("}\n"); + c.write("void deallocate_" + recName + "(struct " + recName + "*v)"); + c.write("{\n"); + for (JField f : mFields) { + if (f.getType() instanceof JRecord) { + c.write(" deallocate_" + extractStructName(f.getType()) + "(&v->" + f.getName() + ");\n"); + } else if (f.getType() instanceof JVector) { + JVector vt = (JVector) f.getType(); + c.write(" deallocate_" + JVector.extractVectorName(vt.getElementType()) + "(&v->" + f.getName() + ");\n"); + } else if (f.getType() instanceof JCompType) { + c.write(" deallocate_" + extractMethodSuffix(f.getType()) + "(&v->" + f.getName() + ");\n"); + } + } + c.write("}\n"); + } + + private void genSerialize(FileWriter c, JType type, String tag, String name) throws IOException { + if (type instanceof JRecord) { + c.write(" rc = rc ? rc : serialize_" + extractStructName(type) + "(out, \"" + tag + "\", &v->" + name + ");\n"); + } else if (type instanceof JVector) { + c.write(" rc = rc ? rc : serialize_" + JVector.extractVectorName(((JVector) type).getElementType()) + "(out, \"" + tag + "\", &v->" + name + ");\n"); + } else { + c.write(" rc = rc ? rc : out->serialize_" + extractMethodSuffix(type) + "(out, \"" + tag + "\", &v->" + name + ");\n"); + } + } + + private void genDeserialize(FileWriter c, JType type, String tag, String name) throws IOException { + if (type instanceof JRecord) { + c.write(" rc = rc ? rc : deserialize_" + extractStructName(type) + "(in, \"" + tag + "\", &v->" + name + ");\n"); + } else if (type instanceof JVector) { + c.write(" rc = rc ? rc : deserialize_" + JVector.extractVectorName(((JVector) type).getElementType()) + "(in, \"" + tag + "\", &v->" + name + ");\n"); + } else { + c.write(" rc = rc ? rc : in->deserialize_" + extractMethodSuffix(type) + "(in, \"" + tag + "\", &v->" + name + ");\n"); + } + } + + static String extractMethodSuffix(JType t) { + if (t instanceof JRecord) { + return extractStructName(t); + } + return t.getMethodSuffix(); + } + + private static String extractStructName(JType t) { + String type = t.getCType(); + + if (!type.startsWith("struct ")) { + return type; + } + + return type.substring("struct ".length()); + } + + public void genCppCode(FileWriter hh, FileWriter cc) + throws IOException { + String[] ns = getCppNameSpace().split("::"); + for (int i = 0; i < ns.length; i++) { + hh.write("namespace " + ns[i] + " {\n"); + } + + hh.write("class " + getName() + " : public ::hadoop::Record {\n"); + hh.write("private:\n"); + + for (Iterator i = mFields.iterator(); i.hasNext(); ) { + JField jf = i.next(); + hh.write(jf.genCppDecl()); + } + hh.write(" mutable std::bitset<" + mFields.size() + "> bs_;\n"); + hh.write("public:\n"); + hh.write(" virtual void serialize(::hadoop::OArchive& a_, const char* tag) const;\n"); + hh.write(" virtual void deserialize(::hadoop::IArchive& a_, const char* tag);\n"); + hh.write(" virtual const ::std::string& type() const;\n"); + hh.write(" virtual const ::std::string& signature() const;\n"); + hh.write(" virtual bool validate() const;\n"); + hh.write(" virtual bool operator<(const " + getName() + "& peer_) const;\n"); + hh.write(" virtual bool operator==(const " + getName() + "& peer_) const;\n"); + hh.write(" virtual ~" + getName() + "() {};\n"); + int fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + hh.write(jf.genCppGetSet(fIdx)); + } + hh.write("}; // end record " + getName() + "\n"); + for (int i = ns.length - 1; i >= 0; i--) { + hh.write("} // end namespace " + ns[i] + "\n"); + } + cc.write("void " + getCppFQName() + "::serialize(::hadoop::OArchive& a_, const char* tag) const {\n"); + cc.write(" if (!validate()) throw new ::hadoop::IOException(\"All fields not set.\");\n"); + cc.write(" a_.startRecord(*this,tag);\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + String name = jf.getName(); + if (jf.getType() instanceof JBuffer) { + cc.write(" a_.serialize(m" + name + ",m" + name + ".length(),\"" + jf.getTag() + "\");\n"); + } else { + cc.write(" a_.serialize(m" + name + ",\"" + jf.getTag() + "\");\n"); + } + cc.write(" bs_.reset(" + fIdx + ");\n"); + } + cc.write(" a_.endRecord(*this,tag);\n"); + cc.write(" return;\n"); + cc.write("}\n"); + + cc.write("void " + getCppFQName() + "::deserialize(::hadoop::IArchive& a_, const char* tag) {\n"); + cc.write(" a_.startRecord(*this,tag);\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + String name = jf.getName(); + if (jf.getType() instanceof JBuffer) { + cc.write(" { size_t len=0; a_.deserialize(m" + name + ",len,\"" + jf.getTag() + "\");}\n"); + } else { + cc.write(" a_.deserialize(m" + name + ",\"" + jf.getTag() + "\");\n"); + } + cc.write(" bs_.set(" + fIdx + ");\n"); + } + cc.write(" a_.endRecord(*this,tag);\n"); + cc.write(" return;\n"); + cc.write("}\n"); + + cc.write("bool " + getCppFQName() + "::validate() const {\n"); + cc.write(" if (bs_.size() != bs_.count()) return false;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = (JField) i.next(); + JType type = jf.getType(); + if (type instanceof JRecord) { + cc.write(" if (!m" + jf.getName() + ".validate()) return false;\n"); + } + } + cc.write(" return true;\n"); + cc.write("}\n"); + + cc.write("bool " + getCppFQName() + "::operator< (const " + getCppFQName() + "& peer_) const {\n"); + cc.write(" return (1\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); ) { + JField jf = i.next(); + String name = jf.getName(); + cc.write(" && (m" + name + " < peer_.m" + name + ")\n"); + } + cc.write(" );\n"); + cc.write("}\n"); + + cc.write("bool " + getCppFQName() + "::operator== (const " + getCppFQName() + "& peer_) const {\n"); + cc.write(" return (1\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); ) { + JField jf = i.next(); + String name = jf.getName(); + cc.write(" && (m" + name + " == peer_.m" + name + ")\n"); + } + cc.write(" );\n"); + cc.write("}\n"); + + cc.write("const ::std::string&" + getCppFQName() + "::type() const {\n"); + cc.write(" static const ::std::string type_(\"" + mName + "\");\n"); + cc.write(" return type_;\n"); + cc.write("}\n"); + + cc.write("const ::std::string&" + getCppFQName() + "::signature() const {\n"); + cc.write(" static const ::std::string sig_(\"" + getSignature() + "\");\n"); + cc.write(" return sig_;\n"); + cc.write("}\n"); + + } + + public void genJavaCode(File outputDirectory) throws IOException { + String pkg = getJavaPackage(); + String pkgpath = pkg.replaceAll("\\.", "/"); + File pkgdir = new File(outputDirectory, pkgpath); + if (!pkgdir.exists()) { + // create the pkg directory + if (!pkgdir.mkdirs()) { + throw new IOException("Cannnot create directory: " + pkgpath); + } + } else if (!pkgdir.isDirectory()) { + throw new IOException(pkgpath + " is not a directory."); + } + try (FileWriter jj = new FileWriter(new File(pkgdir, getName() + ".java"))) { + jj.write("// File generated by hadoop record compiler. Do not edit.\n"); + jj.write("/**\n"); + jj.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + jj.write("* or more contributor license agreements. See the NOTICE file\n"); + jj.write("* distributed with this work for additional information\n"); + jj.write("* regarding copyright ownership. The ASF licenses this file\n"); + jj.write("* to you under the Apache License, Version 2.0 (the\n"); + jj.write("* \"License\"); you may not use this file except in compliance\n"); + jj.write("* with the License. You may obtain a copy of the License at\n"); + jj.write("*\n"); + jj.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + jj.write("*\n"); + jj.write("* Unless required by applicable law or agreed to in writing, software\n"); + jj.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + jj.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + jj.write("* See the License for the specific language governing permissions and\n"); + jj.write("* limitations under the License.\n"); + jj.write("*/\n"); + jj.write("\n"); + jj.write("package " + getJavaPackage() + ";\n\n"); + jj.write("import org.apache.jute.*;\n"); + jj.write("import org.apache.jute.Record; // JDK14 needs explicit import due to clash with java.lang.Record\n"); + jj.write("import org.apache.yetus.audience.InterfaceAudience;\n"); + jj.write("@InterfaceAudience.Public\n"); + jj.write("public class " + getName() + " implements Record {\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); ) { + JField jf = i.next(); + jj.write(jf.genJavaDecl()); + } + jj.write(" public " + getName() + "() {\n"); + jj.write(" }\n"); + + jj.write(" public " + getName() + "(\n"); + int fIdx = 0; + int fLen = mFields.size(); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaConstructorParam(jf.getName())); + jj.write((fLen - 1 == fIdx) ? "" : ",\n"); + } + jj.write(") {\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaConstructorSet(jf.getName())); + } + jj.write(" }\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaGetSet(fIdx)); + } + jj.write(" public void serialize(OutputArchive a_, String tag) throws java.io.IOException {\n"); + jj.write(" a_.startRecord(this,tag);\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaWriteMethodName()); + } + jj.write(" a_.endRecord(this,tag);\n"); + jj.write(" }\n"); + + jj.write(" public void deserialize(InputArchive a_, String tag) throws java.io.IOException {\n"); + jj.write(" a_.startRecord(tag);\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaReadMethodName()); + } + jj.write(" a_.endRecord(tag);\n"); + jj.write("}\n"); + + jj.write(" public String toString() {\n"); + jj.write(" try {\n"); + jj.write(" java.io.ByteArrayOutputStream s =\n"); + jj.write(" new java.io.ByteArrayOutputStream();\n"); + jj.write(" ToStringOutputArchive a_ = \n"); + jj.write(" new ToStringOutputArchive(s);\n"); + jj.write(" a_.startRecord(this,\"\");\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaWriteMethodName()); + } + jj.write(" a_.endRecord(this,\"\");\n"); + jj.write(" return new String(s.toByteArray(), \"UTF-8\");\n"); + jj.write(" } catch (Throwable ex) {\n"); + jj.write(" ex.printStackTrace();\n"); + jj.write(" }\n"); + jj.write(" return \"ERROR\";\n"); + jj.write(" }\n"); + + jj.write(" public void write(java.io.DataOutput out) throws java.io.IOException {\n"); + jj.write(" BinaryOutputArchive archive = new BinaryOutputArchive(out);\n"); + jj.write(" serialize(archive, \"\");\n"); + jj.write(" }\n"); + + jj.write(" public void readFields(java.io.DataInput in) throws java.io.IOException {\n"); + jj.write(" BinaryInputArchive archive = new BinaryInputArchive(in);\n"); + jj.write(" deserialize(archive, \"\");\n"); + jj.write(" }\n"); + + jj.write(" public int compareTo (Object peer_) throws ClassCastException {\n"); + boolean unimplemented = false; + for (JField f : mFields) { + if ((f.getType() instanceof JMap) + || (f.getType() instanceof JVector)) { + unimplemented = true; + } + } + if (unimplemented) { + jj.write(" throw new UnsupportedOperationException(\"comparing " + + getName() + " is unimplemented\");\n"); + } else { + jj.write(" if (!(peer_ instanceof " + getName() + ")) {\n"); + jj.write(" throw new ClassCastException(\"Comparing different types of records.\");\n"); + jj.write(" }\n"); + jj.write(" " + getName() + " peer = (" + getName() + ") peer_;\n"); + jj.write(" int ret = 0;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaCompareTo()); + jj.write(" if (ret != 0) return ret;\n"); + } + jj.write(" return ret;\n"); + } + jj.write(" }\n"); + + jj.write(" public boolean equals(Object peer_) {\n"); + jj.write(" if (!(peer_ instanceof " + getName() + ")) {\n"); + jj.write(" return false;\n"); + jj.write(" }\n"); + jj.write(" if (peer_ == this) {\n"); + jj.write(" return true;\n"); + jj.write(" }\n"); + jj.write(" " + getName() + " peer = (" + getName() + ") peer_;\n"); + jj.write(" boolean ret = false;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaEquals()); + jj.write(" if (!ret) return ret;\n"); + } + jj.write(" return ret;\n"); + jj.write(" }\n"); + + jj.write(" public int hashCode() {\n"); + jj.write(" int result = 17;\n"); + jj.write(" int ret;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + jj.write(jf.genJavaHashCode()); + jj.write(" result = 37*result + ret;\n"); + } + jj.write(" return result;\n"); + jj.write(" }\n"); + jj.write(" public static String signature() {\n"); + jj.write(" return \"" + getSignature() + "\";\n"); + jj.write(" }\n"); + + jj.write("}\n"); + } + } + + public void genCsharpCode(File outputDirectory) throws IOException { + if (!outputDirectory.exists()) { + // create the pkg directory + if (!outputDirectory.mkdirs()) { + throw new IOException("Cannnot create directory: " + outputDirectory); + } + } else if (!outputDirectory.isDirectory()) { + throw new IOException(outputDirectory + " is not a directory."); + } + + try (FileWriter cs = new FileWriter(new File(outputDirectory, getName() + ".cs"));) { + cs.write("// File generated by hadoop record compiler. Do not edit.\n"); + cs.write("/**\n"); + cs.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + cs.write("* or more contributor license agreements. See the NOTICE file\n"); + cs.write("* distributed with this work for additional information\n"); + cs.write("* regarding copyright ownership. The ASF licenses this file\n"); + cs.write("* to you under the Apache License, Version 2.0 (the\n"); + cs.write("* \"License\"); you may not use this file except in compliance\n"); + cs.write("* with the License. You may obtain a copy of the License at\n"); + cs.write("*\n"); + cs.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + cs.write("*\n"); + cs.write("* Unless required by applicable law or agreed to in writing, software\n"); + cs.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + cs.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + cs.write("* See the License for the specific language governing permissions and\n"); + cs.write("* limitations under the License.\n"); + cs.write("*/\n"); + cs.write("\n"); + cs.write("using System;\n"); + cs.write("using Org.Apache.Jute;\n"); + cs.write("\n"); + cs.write("namespace " + getCsharpNameSpace() + "\n"); + cs.write("{\n"); + + String className = getCsharpName(); + cs.write("public class " + className + " : IRecord, IComparable \n"); + cs.write("{\n"); + cs.write(" public " + className + "() {\n"); + cs.write(" }\n"); + + cs.write(" public " + className + "(\n"); + int fIdx = 0; + int fLen = mFields.size(); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpConstructorParam(jf.getCsharpName())); + cs.write((fLen - 1 == fIdx) ? "" : ",\n"); + } + cs.write(") {\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpConstructorSet(jf.getCsharpName())); + } + cs.write(" }\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpGetSet(fIdx)); + cs.write("\n"); + } + cs.write(" public void Serialize(IOutputArchive a_, String tag) {\n"); + cs.write(" a_.StartRecord(this,tag);\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpWriteMethodName()); + } + cs.write(" a_.EndRecord(this,tag);\n"); + cs.write(" }\n"); + + cs.write(" public void Deserialize(IInputArchive a_, String tag) {\n"); + cs.write(" a_.StartRecord(tag);\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpReadMethodName()); + } + cs.write(" a_.EndRecord(tag);\n"); + cs.write("}\n"); + + cs.write(" public override String ToString() {\n"); + cs.write(" try {\n"); + cs.write(" System.IO.MemoryStream ms = new System.IO.MemoryStream();\n"); + cs.write(" MiscUtil.IO.EndianBinaryWriter writer =\n"); + cs.write(" new MiscUtil.IO.EndianBinaryWriter(MiscUtil.Conversion.EndianBitConverter.Big, ms, System.Text.Encoding.UTF8);\n"); + cs.write(" BinaryOutputArchive a_ = \n"); + cs.write(" new BinaryOutputArchive(writer);\n"); + cs.write(" a_.StartRecord(this,\"\");\n"); + fIdx = 0; + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpWriteMethodName()); + } + cs.write(" a_.EndRecord(this,\"\");\n"); + cs.write(" ms.Position = 0;\n"); + cs.write(" return System.Text.Encoding.UTF8.GetString(ms.ToArray());\n"); + cs.write(" } catch (Exception ex) {\n"); + cs.write(" Console.WriteLine(ex.StackTrace);\n"); + cs.write(" }\n"); + cs.write(" return \"ERROR\";\n"); + cs.write(" }\n"); + + cs.write(" public void Write(MiscUtil.IO.EndianBinaryWriter writer) {\n"); + cs.write(" BinaryOutputArchive archive = new BinaryOutputArchive(writer);\n"); + cs.write(" Serialize(archive, \"\");\n"); + cs.write(" }\n"); + + cs.write(" public void ReadFields(MiscUtil.IO.EndianBinaryReader reader) {\n"); + cs.write(" BinaryInputArchive archive = new BinaryInputArchive(reader);\n"); + cs.write(" Deserialize(archive, \"\");\n"); + cs.write(" }\n"); + + cs.write(" public int CompareTo (object peer_) {\n"); + boolean unimplemented = false; + for (JField f : mFields) { + if ((f.getType() instanceof JMap) + || (f.getType() instanceof JVector)) { + unimplemented = true; + } + } + if (unimplemented) { + cs.write(" throw new InvalidOperationException(\"comparing " + + getCsharpName() + " is unimplemented\");\n"); + } else { + cs.write(" if (!(peer_ is " + getCsharpName() + ")) {\n"); + cs.write(" throw new InvalidOperationException(\"Comparing different types of records.\");\n"); + cs.write(" }\n"); + cs.write(" " + getCsharpName() + " peer = (" + getCsharpName() + ") peer_;\n"); + cs.write(" int ret = 0;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpCompareTo()); + cs.write(" if (ret != 0) return ret;\n"); + } + cs.write(" return ret;\n"); + } + cs.write(" }\n"); + + cs.write(" public override bool Equals(object peer_) {\n"); + cs.write(" if (!(peer_ is " + getCsharpName() + ")) {\n"); + cs.write(" return false;\n"); + cs.write(" }\n"); + cs.write(" if (peer_ == this) {\n"); + cs.write(" return true;\n"); + cs.write(" }\n"); + cs.write(" bool ret = false;\n"); + cs.write(" " + getCsharpName() + " peer = (" + getCsharpName() + ")peer_;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpEquals()); + cs.write(" if (!ret) return ret;\n"); + } + cs.write(" return ret;\n"); + cs.write(" }\n"); + + cs.write(" public override int GetHashCode() {\n"); + cs.write(" int result = 17;\n"); + cs.write(" int ret;\n"); + for (Iterator i = mFields.iterator(); i.hasNext(); fIdx++) { + JField jf = i.next(); + cs.write(jf.genCsharpHashCode()); + cs.write(" result = 37*result + ret;\n"); + } + cs.write(" return result;\n"); + cs.write(" }\n"); + cs.write(" public static string Signature() {\n"); + cs.write(" return \"" + getSignature() + "\";\n"); + cs.write(" }\n"); + + cs.write("}\n"); + cs.write("}\n"); + } + } + + public static String getCsharpFQName(String name) { + String[] packages = name.split("\\."); + StringBuffer fQName = new StringBuffer(); + for (int i = 0; i < packages.length; i++) { + String pack = packages[i]; + pack = capitalize(pack); + pack = "Id".equals(pack) ? "ZKId" : pack; + fQName.append(capitalize(pack)); + if (i != packages.length - 1) { + fQName.append("."); + } + } + return fQName.toString(); + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JString.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JString.java new file mode 100644 index 0000000..60546cb --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JString.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * + */ +public class JString extends JCompType { + + /** + * Creates a new instance of JString. + */ + public JString() { + super("char *", " ::std::string", "string", "String", "String", "String", "string"); + } + + public String getSignature() { + return "s"; + } + + public String genJavaReadWrapper(String fname, String tag, boolean decl) { + String ret = ""; + if (decl) { + ret = " String " + fname + ";\n"; + } + return ret + " " + fname + "=a_.readString(\"" + tag + "\");\n"; + } + + public String genJavaWriteWrapper(String fname, String tag) { + return " a_.writeString(" + fname + ",\"" + tag + "\");\n"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JType.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JType.java new file mode 100644 index 0000000..d50ac01 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JType.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +/** + * Abstract Base class for all types supported by Hadoop Record I/O. + */ +public abstract class JType { + + private String mCName; + private String mCppName; + private String mCsharpName; + private String mJavaName; + protected String mMethodSuffix; + private String mWrapper; + private String mSharpWrapper; + private String mUnwrapMethod; + + /** + * Creates a new instance of JType. + */ + JType(String cname, String cppname, String csharpName, String javaname, String suffix, String wrapper, String csharpWrapper, String unwrap) { + mCName = cname; + mCppName = cppname; + mCsharpName = "Id".equals(csharpName) ? "ZKId" : csharpName; + mJavaName = javaname; + mMethodSuffix = suffix; + mWrapper = wrapper; + mSharpWrapper = csharpWrapper; + mUnwrapMethod = unwrap; + } + + abstract String getSignature(); + + String genCppDecl(String fname) { + return " " + mCppName + " m" + fname + ";\n"; + } + + String genCDecl(String name) { + return " " + mCName + " " + name + ";\n"; + } + + public String genCsharpDecl(String name) { + return " private " + mCsharpName + " " + name + ";\n"; + } + + String genJavaDecl(String fname) { + return " private " + mJavaName + " " + fname + ";\n"; + } + + String genJavaConstructorParam(String fname) { + return " " + mJavaName + " " + fname; + } + + String genCppGetSet(String fname, int fIdx) { + String getFunc = " virtual " + mCppName + " get" + fname + "() const {\n"; + getFunc += " return m" + fname + ";\n"; + getFunc += " }\n"; + String setFunc = " virtual void set" + fname + "(" + mCppName + " m_) {\n"; + setFunc += " m" + fname + "=m_; bs_.set(" + fIdx + ");\n"; + setFunc += " }\n"; + return getFunc + setFunc; + } + + String genCsharpGetSet(String fname, int fIdx) { + String getFunc = " public " + getCsharpType() + " " + capitalize(fname) + " { get; set; } "; + return getFunc; + } + + static String capitalize(String s) { + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + + String genJavaGetSet(String fname, int fIdx) { + String getFunc = " public " + mJavaName + " get" + capitalize(fname) + "() {\n"; + getFunc += " return " + fname + ";\n"; + getFunc += " }\n"; + String setFunc = " public void set" + capitalize(fname) + "(" + mJavaName + " m_) {\n"; + setFunc += " " + fname + "=m_;\n"; + setFunc += " }\n"; + return getFunc + setFunc; + } + + String getCType() { + return mCName; + } + + String getCppType() { + return mCppName; + } + + String getCsharpType() { + return mCsharpName; + } + + String getJavaType() { + return mJavaName; + } + + String getJavaWrapperType() { + return mWrapper; + } + + String getCsharpWrapperType() { + return mSharpWrapper; + } + + String getMethodSuffix() { + return mMethodSuffix; + } + + String genJavaWriteMethod(String fname, String tag) { + return " a_.write" + mMethodSuffix + "(" + fname + ",\"" + tag + "\");\n"; + } + + String genJavaReadMethod(String fname, String tag) { + return " " + fname + "=a_.read" + mMethodSuffix + "(\"" + tag + "\");\n"; + } + + String genJavaReadWrapper(String fname, String tag, boolean decl) { + String ret = ""; + if (decl) { + ret = " " + mWrapper + " " + fname + ";\n"; + } + return ret + " " + fname + "=new " + mWrapper + "(a_.read" + mMethodSuffix + "(\"" + tag + "\"));\n"; + } + + String genJavaWriteWrapper(String fname, String tag) { + return " a_.write" + mMethodSuffix + "(" + fname + "." + mUnwrapMethod + "(),\"" + tag + "\");\n"; + } + + String genJavaCompareTo(String fname) { + return " ret = (" + fname + " == peer." + fname + ")? 0 :((" + fname + "", "System.Collections.Generic.List<" + t.getCsharpType() + ">", "java.util.List<" + t.getJavaType() + ">", "Vector", + "System.Collections.Generic.List<" + t.getCsharpType() + ">", "java.util.ArrayList<" + t.getJavaType() + ">"); + mElement = t; + } + + public String getSignature() { + return "[" + mElement.getSignature() + "]"; + } + + public String genJavaCompareTo(String fname) { + return " throw new UnsupportedOperationException(\"comparing " + + fname + " is unimplemented\");\n"; + } + + public String genJavaReadWrapper(String fname, String tag, boolean decl) { + StringBuilder ret = new StringBuilder(""); + if (decl) { + ret.append(" java.util.List " + fname + ";\n"); + } + ret.append(" {\n"); + incrLevel(); + ret.append(" Index " + getId("vidx") + " = a_.startVector(\"" + tag + "\");\n"); + ret.append(" if (" + getId("vidx") + "!= null) {"); + ret.append(" " + fname + "=new java.util.ArrayList<" + mElement.getJavaType() + ">();\n"); + ret.append(" for (; !" + getId("vidx") + ".done(); " + getId("vidx") + ".incr()) {\n"); + ret.append(mElement.genJavaReadWrapper(getId("e"), getId("e"), true)); + ret.append(" " + fname + ".add(" + getId("e") + ");\n"); + ret.append(" }\n"); + ret.append(" }\n"); + ret.append(" a_.endVector(\"" + tag + "\");\n"); + decrLevel(); + ret.append(" }\n"); + return ret.toString(); + } + + public String genJavaReadMethod(String fname, String tag) { + return genJavaReadWrapper(fname, tag, false); + } + + public String genJavaWriteWrapper(String fname, String tag) { + StringBuilder ret = new StringBuilder(" {\n"); + incrLevel(); + ret.append(" a_.startVector(" + fname + ",\"" + tag + "\");\n"); + ret.append(" if (" + fname + "!= null) {"); + ret.append(" int " + getId("len") + " = " + fname + ".size();\n"); + ret.append(" for(int " + getId("vidx") + " = 0; " + getId("vidx") + "<" + getId("len") + "; " + getId("vidx") + "++) {\n"); + ret.append(" " + mElement.getJavaWrapperType() + " " + getId("e") + " = (" + mElement.getJavaWrapperType() + ") " + fname + ".get(" + getId("vidx") + ");\n"); + ret.append(mElement.genJavaWriteWrapper(getId("e"), getId("e"))); + ret.append(" }\n"); + ret.append(" }\n"); + ret.append(" a_.endVector(" + fname + ",\"" + tag + "\");\n"); + ret.append(" }\n"); + decrLevel(); + return ret.toString(); + } + + public String genJavaWriteMethod(String fname, String tag) { + return genJavaWriteWrapper(fname, tag); + } + + public JType getElementType() { + return mElement; + } + + public String genCsharpWriteWrapper(String fname, String tag) { + StringBuilder ret = new StringBuilder(" {\n"); + incrLevel(); + ret.append(" a_.StartVector(" + capitalize(fname) + ",\"" + tag + "\");\n"); + ret.append(" if (" + capitalize(fname) + "!= null) {"); + ret.append(" int " + getId("len") + " = " + capitalize(fname) + ".Count;\n"); + ret.append(" for(int " + getId("vidx") + " = 0; " + getId("vidx") + "<" + getId("len") + "; " + getId("vidx") + "++) {\n"); + ret.append(" " + mElement.getCsharpWrapperType() + " " + getId("e") + " = (" + mElement.getCsharpWrapperType() + ") " + capitalize(fname) + "[" + getId("vidx") + "];\n"); + ret.append(mElement.genCsharpWriteWrapper(getId("e"), getId("e"))); + ret.append(" }\n"); + ret.append(" }\n"); + ret.append(" a_.EndVector(" + capitalize(fname) + ",\"" + tag + "\");\n"); + ret.append(" }\n"); + decrLevel(); + return ret.toString(); + } + + String genCsharpWriteMethod(String fname, String tag) { + return genCsharpWriteWrapper(fname, tag); + } + + public String genCsharpReadWrapper(String fname, String tag, boolean decl) { + StringBuilder ret = new StringBuilder(); + if (decl) { + ret.append(" System.Collections.Generic.List<" + mElement.getCsharpType() + "> " + capitalize(fname) + ";\n"); + } + ret.append(" {\n"); + incrLevel(); + ret.append(" IIndex " + getId("vidx") + " = a_.StartVector(\"" + tag + "\");\n"); + ret.append(" if (" + getId("vidx") + "!= null) {"); + ret.append(" " + capitalize(fname) + "=new System.Collections.Generic.List<" + mElement.getCsharpType() + ">();\n"); + ret.append(" for (; !" + getId("vidx") + ".Done(); " + getId("vidx") + ".Incr()) {\n"); + ret.append(mElement.genCsharpReadWrapper(getId("e"), getId("e"), true)); + ret.append(" " + capitalize(fname) + ".Add(" + getId("e") + ");\n"); + ret.append(" }\n"); + ret.append(" }\n"); + ret.append(" a_.EndVector(\"" + tag + "\");\n"); + decrLevel(); + ret.append(" }\n"); + return ret.toString(); + } + + String genCsharpReadMethod(String fname, String tag) { + return genCsharpReadWrapper(fname, tag, false); + } + + static String extractVectorName(JType jvType) { + return JRecord.extractMethodSuffix(jvType) + "_vector"; + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/JavaGenerator.java b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JavaGenerator.java new file mode 100644 index 0000000..75e316d --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/JavaGenerator.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * Java Code generator front-end for Hadoop record I/O. + */ +class JavaGenerator { + private List mRecList; + private final File outputDirectory; + + /** + * Creates a new instance of JavaGenerator. + * + * @param name possibly full pathname to the file + * @param incl included files (as JFile) + * @param records List of records defined within this file + * @param outputDirectory + */ + JavaGenerator(String name, List incl, + List records, File outputDirectory) { + mRecList = records; + this.outputDirectory = outputDirectory; + } + + /** + * Generate Java code for records. This method is only a front-end to + * JRecord, since one file is generated for each record. + */ + void genCode() throws IOException { + for (Iterator i = mRecList.iterator(); i.hasNext(); ) { + JRecord rec = i.next(); + rec.genJavaCode(outputDirectory); + } + } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/package.html b/zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/package.html new file mode 100644 index 0000000..8ef8a8c --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/package.html @@ -0,0 +1,28 @@ + + + + + + Hadoop Record Compiler: Parser + + + This package contains code generated by JavaCC from the + Hadoop record syntax file rcc.jj. For details about the + record file syntax please @see org.apache.hadoop.record. + + diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj b/zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj new file mode 100644 index 0000000..94d4f42 --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/generated/rcc.jj @@ -0,0 +1,374 @@ +options { +STATIC=false; +} + +PARSER_BEGIN(Rcc) +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute.compiler.generated; + +import org.apache.jute.compiler.*; +import java.util.ArrayList; +import java.util.Hashtable; +import java.io.File; +import java.io.FileReader; +import java.io.FileNotFoundException; +import java.io.IOException; + +@SuppressWarnings("unused") +public class Rcc { + private static Hashtable recTab = new Hashtable(); + private static String curDir = System.getProperty("user.dir"); + private static String curFileName; + private static String curModuleName; + + public static void main(String args[]) { + String language = "java"; + ArrayList recFiles = new ArrayList(); + JFile curFile=null; + + for (int i=0; i(); + return parser.Input(); + } finally { + try { + reader.close(); + } catch (IOException e) { + } + } + } +} + +PARSER_END(Rcc) + +SKIP : +{ + " " +| "\t" +| "\n" +| "\r" +} + +SPECIAL_TOKEN : +{ + "//" : WithinOneLineComment +} + + SPECIAL_TOKEN : +{ + <("\n" | "\r" | "\r\n" )> : DEFAULT +} + + MORE : +{ + <~[]> +} + +SPECIAL_TOKEN : +{ + "/*" : WithinMultiLineComment +} + + SPECIAL_TOKEN : +{ + "*/" : DEFAULT +} + + MORE : +{ + <~[]> +} + +TOKEN : +{ + +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| +| "> +| +| +| +| +| +} + +JFile Input() : +{ + ArrayList ilist = new ArrayList(); + ArrayList rlist = new ArrayList(); + JFile i; + ArrayList l; +} +{ + ( + i = Include() + { ilist.add(i); } + | l = Module() + { rlist.addAll(l); } + )+ + + { return new JFile(curFileName, ilist, rlist); } +} + +JFile Include() : +{ + String fname; + Token t; +} +{ + + t = + { + JFile ret = null; + fname = t.image.replaceAll("^\"", "").replaceAll("\"$",""); + File file = new File(curDir, fname); + String tmpDir = curDir; + String tmpFile = curFileName; + curDir = file.getParent(); + curFileName = file.getName(); + try { + FileReader reader = new FileReader(file); + Rcc parser = new Rcc(reader); + try { + ret = parser.Input(); + System.out.println(fname + " Parsed Successfully"); + } catch (ParseException e) { + System.out.println(e.toString()); + System.exit(1); + } + try { + reader.close(); + } catch (IOException e) { + } + } catch (FileNotFoundException e) { + System.out.println("File " + fname + + " Not found."); + System.exit(1); + } + curDir = tmpDir; + curFileName = tmpFile; + return ret; + } +} + +ArrayList Module() : +{ + String mName; + ArrayList rlist; +} +{ + + mName = ModuleName() + { curModuleName = mName; } + + rlist = RecordList() + + { return rlist; } +} + +String ModuleName() : +{ + String name = ""; + Token t; +} +{ + t = + { name += t.image; } + ( + + t = + { name += "." + t.image; } + )* + { return name; } +} + +ArrayList RecordList() : +{ + ArrayList rlist = new ArrayList(); + JRecord r; +} +{ + ( + r = Record() + { rlist.add(r); } + )+ + { return rlist; } +} + +JRecord Record() : +{ + String rname; + ArrayList flist = new ArrayList(); + Token t; + JField f; +} +{ + + t = + { rname = t.image; } + + ( + f = Field() + { flist.add(f); } + + )+ + + { + String fqn = curModuleName + "." + rname; + JRecord r = new JRecord(fqn, flist); + recTab.put(fqn, r); + return r; + } +} + +JField Field() : +{ + JType jt; + Token t; +} +{ + jt = Type() + t = + { return new JField(jt, t.image); } +} + +JType Type() : +{ + JType jt; + Token t; + String rname; +} +{ + jt = Map() + { return jt; } +| jt = Vector() + { return jt; } +| + { return new JByte(); } +| + { return new JBoolean(); } +| + { return new JInt(); } +| + { return new JLong(); } +| + { return new JFloat(); } +| + { return new JDouble(); } +| + { return new JString(); } +| + { return new JBuffer(); } +| rname = ModuleName() + { + if (rname.indexOf('.', 0) < 0) { + rname = curModuleName + "." + rname; + } + JRecord r = recTab.get(rname); + if (r == null) { + System.out.println("Type " + rname + " not known. Exiting."); + System.exit(1); + } + return r; + } +} + +JMap Map() : +{ + JType jt1; + JType jt2; +} +{ + + + jt1 = Type() + + jt2 = Type() + + { return new JMap(jt1, jt2); } +} + +JVector Vector() : +{ + JType jt; +} +{ + + + jt = Type() + + { return new JVector(jt); } +} diff --git a/zookeeper-jute/src/main/java/org/apache/jute/compiler/package.html b/zookeeper-jute/src/main/java/org/apache/jute/compiler/package.html new file mode 100644 index 0000000..03bdb1b --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/compiler/package.html @@ -0,0 +1,30 @@ + + + + + + Hadoop Record Compiler + + + This package contains classes needed for code generation + from the hadoop record compiler. CppGenerator and JavaGenerator + are the main entry points from the parser. There are classes + corrsponding to every primitive type and compound type + included in Hadoop record I/O syntax. + + diff --git a/zookeeper-jute/src/main/java/org/apache/jute/package.html b/zookeeper-jute/src/main/java/org/apache/jute/package.html new file mode 100644 index 0000000..64a030a --- /dev/null +++ b/zookeeper-jute/src/main/java/org/apache/jute/package.html @@ -0,0 +1,665 @@ + + + + + + Hadoop Record I/O + + + Hadoop record I/O contains classes and a record description language + translator for simplifying serialization and deserialization of records in a + language-neutral manner. + +

      Introduction

      + + Software systems of any significant complexity require mechanisms for data +interchange with the outside world. These interchanges typically involve the +marshaling and unmarshaling of logical units of data to and from data streams +(files, network connections, memory buffers etc.). Applications usually have +some code for serializing and deserializing the data types that they manipulate +embedded in them. The work of serialization has several features that make +automatic code generation for it worthwhile. Given a particular output encoding +(binary, XML, etc.), serialization of primitive types and simple compositions +of primitives (structs, vectors etc.) is a very mechanical task. Manually +written serialization code can be susceptible to bugs especially when records +have a large number of fields or a record definition changes between software +versions. Lastly, it can be very useful for applications written in different +programming languages to be able to share and interchange data. This can be +made a lot easier by describing the data records manipulated by these +applications in a language agnostic manner and using the descriptions to derive +implementations of serialization in multiple target languages. + +This document describes Hadoop Record I/O, a mechanism that is aimed +at +
        +
      • enabling the specification of simple serializable data types (records) +
      • enabling the generation of code in multiple target languages for +marshaling and unmarshaling such types +
      • providing target language specific support that will enable application +programmers to incorporate generated code into their applications +
      + +The goals of Hadoop Record I/O are similar to those of mechanisms such as XDR, +ASN.1, PADS and ICE. While these systems all include a DDL that enables +the specification of most record types, they differ widely in what else they +focus on. The focus in Hadoop Record I/O is on data marshaling and +multi-lingual support. We take a translator-based approach to serialization. +Hadoop users have to describe their data in a simple data description +language. The Hadoop DDL translator rcc generates code that users +can invoke in order to read/write their data from/to simple stream +abstractions. Next we list explicitly some of the goals and non-goals of +Hadoop Record I/O. + + +

      Goals

      + +
        +
      • Support for commonly used primitive types. Hadoop should include as +primitives commonly used builtin types from programming languages we intend to +support. + +
      • Support for common data compositions (including recursive compositions). +Hadoop should support widely used composite types such as structs and +vectors. + +
      • Code generation in multiple target languages. Hadoop should be capable of +generating serialization code in multiple target languages and should be +easily extensible to new target languages. The initial target languages are +C++ and Java. + +
      • Support for generated target languages. Hadooop should include support +in the form of headers, libraries, packages for supported target languages +that enable easy inclusion and use of generated code in applications. + +
      • Support for multiple output encodings. Candidates include +packed binary, comma-separated text, XML etc. + +
      • Support for specifying record types in a backwards/forwards compatible +manner. This will probably be in the form of support for optional fields in +records. This version of the document does not include a description of the +planned mechanism, we intend to include it in the next iteration. + +
      + +

      Non-Goals

      + +
        +
      • Serializing existing arbitrary C++ classes. +
      • Serializing complex data structures such as trees, linked lists etc. +
      • Built-in indexing schemes, compression, or check-sums. +
      • Dynamic construction of objects from an XML schema. +
      + +The remainder of this document describes the features of Hadoop record I/O +in more detail. Section 2 describes the data types supported by the system. +Section 3 lays out the DDL syntax with some examples of simple records. +Section 4 describes the process of code generation with rcc. Section 5 +describes target language mappings and support for Hadoop types. We include a +fairly complete description of C++ mappings with intent to include Java and +others in upcoming iterations of this document. The last section talks about +supported output encodings. + + +

      Data Types and Streams

      + +This section describes the primitive and composite types supported by Hadoop. +We aim to support a set of types that can be used to simply and efficiently +express a wide range of record types in different programming languages. + +

      Primitive Types

      + +For the most part, the primitive types of Hadoop map directly to primitive +types in high level programming languages. Special cases are the +ustring (a Unicode string) and buffer types, which we believe +find wide use and which are usually implemented in library code and not +available as language built-ins. Hadoop also supplies these via library code +when a target language built-in is not present and there is no widely +adopted "standard" implementation. The complete list of primitive types is: + +
        +
      • byte: An 8-bit unsigned integer. +
      • boolean: A boolean value. +
      • int: A 32-bit signed integer. +
      • long: A 64-bit signed integer. +
      • float: A single precision floating point number as described by + IEEE-754. +
      • double: A double precision floating point number as described by + IEEE-754. +
      • ustring: A string consisting of Unicode characters. +
      • buffer: An arbitrary sequence of bytes. +
      + + +

      Composite Types

      +Hadoop supports a small set of composite types that enable the description +of simple aggregate types and containers. A composite type is serialized +by sequentially serializing it constituent elements. The supported +composite types are: + +
        + +
      • record: An aggregate type like a C-struct. This is a list of +typed fields that are together considered a single unit of data. A record +is serialized by sequentially serializing its constituent fields. In addition +to serialization a record has comparison operations (equality and less-than) +implemented for it, these are defined as memberwise comparisons. + +
      • vector: A sequence of entries of the same data type, primitive +or composite. + +
      • map: An associative container mapping instances of a key type to +instances of a value type. The key and value types may themselves be primitive +or composite types. + +
      + +

      Streams

      + +Hadoop generates code for serializing and deserializing record types to +abstract streams. For each target language Hadoop defines very simple input +and output stream interfaces. Application writers can usually develop +concrete implementations of these by putting a one method wrapper around +an existing stream implementation. + + +

      DDL Syntax and Examples

      + +We now describe the syntax of the Hadoop data description language. This is +followed by a few examples of DDL usage. + +

      Hadoop DDL Syntax

      + +
      
      +recfile = *include module *record
      +include = "include" path
      +path = (relative-path / absolute-path)
      +module = "module" module-name
      +module-name = name *("." name)
      +record := "class" name "{" 1*(field) "}"
      +field := type name ";"
      +name :=  ALPHA (ALPHA / DIGIT / "_" )*
      +type := (ptype / ctype)
      +ptype := ("byte" / "boolean" / "int" |
      +          "long" / "float" / "double"
      +          "ustring" / "buffer")
      +ctype := (("vector" "<" type ">") /
      +          ("map" "<" type "," type ">" ) ) / name)
      +
      + +A DDL file describes one or more record types. It begins with zero or +more include declarations, a single mandatory module declaration +followed by zero or more class declarations. The semantics of each of +these declarations are described below: + +
        + +
      • include: An include declaration specifies a DDL file to be +referenced when generating code for types in the current DDL file. Record types +in the current compilation unit may refer to types in all included files. +File inclusion is recursive. An include does not trigger code +generation for the referenced file. + +
      • module: Every Hadoop DDL file must have a single module +declaration that follows the list of includes and precedes all record +declarations. A module declaration identifies a scope within which +the names of all types in the current file are visible. Module names are +mapped to C++ namespaces, Java packages etc. in generated code. + +
      • class: Records types are specified through class +declarations. A class declaration is like a Java class declaration. +It specifies a named record type and a list of fields that constitute records +of the type. Usage is illustrated in the following examples. + +
      + +

      Examples

      + +
        +
      • A simple DDL file links.jr with just one record declaration. +
        
        +module links {
        +    class Link {
        +        ustring URL;
        +        boolean isRelative;
        +        ustring anchorText;
        +    };
        +}
        +
        + +
      • A DDL file outlinks.jr which includes another +
        
        +include "links.jr"
        +
        +module outlinks {
        +    class OutLinks {
        +        ustring baseURL;
        +        vector outLinks;
        +    };
        +}
        +
        +
      + +

      Code Generation

      + +The Hadoop translator is written in Java. Invocation is done by executing a +wrapper shell script named named rcc. It takes a list of +record description files as a mandatory argument and an +optional language argument (the default is Java) --language or +-l. Thus a typical invocation would look like: +
      
      +$ rcc -l C++  ...
      +
      + + +

      Target Language Mappings and Support

      + +For all target languages, the unit of code generation is a record type. +For each record type, Hadoop generates code for serialization and +deserialization, record comparison and access to record members. + +

      C++

      + +Support for including Hadoop generated C++ code in applications comes in the +form of a header file recordio.hh which needs to be included in source +that uses Hadoop types and a library librecordio.a which applications need +to be linked with. The header declares the Hadoop C++ namespace which defines +appropriate types for the various primitives, the basic interfaces for +records and streams and enumerates the supported serialization encodings. +Declarations of these interfaces and a description of their semantics follow: + +
      
      +namespace hadoop {
      +
      +  enum RecFormat { kBinary };
      +
      +  class InStream {
      +  public:
      +    virtual ssize_t read(void *buf, size_t n) = 0;
      +  };
      +
      +  class OutStream {
      +  public:
      +    virtual ssize_t write(const void *buf, size_t n) = 0;
      +  };
      +
      +  class IOError : public runtime_error {
      +  public:
      +    explicit IOError(const std::string& msg);
      +  };
      +
      +  class IArchive;
      +  class OArchive;
      +
      +  class RecordReader {
      +  public:
      +    RecordReader(InStream& in, RecFormat fmt);
      +    virtual ~RecordReader(void);
      +
      +    virtual void read(Record& rec);
      +  };
      +
      +  class RecordWriter {
      +  public:
      +    RecordWriter(OutStream& out, RecFormat fmt);
      +    virtual ~RecordWriter(void);
      +
      +    virtual void write(Record& rec);
      +  };
      +
      +
      +  class Record {
      +  public:
      +    virtual std::string type(void) const = 0;
      +    virtual std::string signature(void) const = 0;
      +  protected:
      +    virtual bool validate(void) const = 0;
      +
      +    virtual void
      +    serialize(OArchive& oa, const std::string& tag) const = 0;
      +
      +    virtual void
      +    deserialize(IArchive& ia, const std::string& tag) = 0;
      +  };
      +}
      +
      + +
        + +
      • RecFormat: An enumeration of the serialization encodings supported +by this implementation of Hadoop. + +
      • InStream: A simple abstraction for an input stream. This has a +single public read method that reads n bytes from the stream into +the buffer buf. Has the same semantics as a blocking read system +call. Returns the number of bytes read or -1 if an error occurs. + +
      • OutStream: A simple abstraction for an output stream. This has a +single write method that writes n bytes to the stream from the +buffer buf. Has the same semantics as a blocking write system +call. Returns the number of bytes written or -1 if an error occurs. + +
      • RecordReader: A RecordReader reads records one at a time from +an underlying stream in a specified record format. The reader is instantiated +with a stream and a serialization format. It has a read method that +takes an instance of a record and deserializes the record from the stream. + +
      • RecordWriter: A RecordWriter writes records one at a +time to an underlying stream in a specified record format. The writer is +instantiated with a stream and a serialization format. It has a +write method that takes an instance of a record and serializes the +record to the stream. + +
      • Record: The base class for all generated record types. This has two +public methods type and signature that return the typename and the +type signature of the record. + +
      + +Two files are generated for each record file (note: not for each record). If a +record file is named "name.jr", the generated files are +"name.jr.cc" and "name.jr.hh" containing serialization +implementations and record type declarations respectively. + +For each record in the DDL file, the generated header file will contain a +class definition corresponding to the record type, method definitions for the +generated type will be present in the '.cc' file. The generated class will +inherit from the abstract class hadoop::Record. The DDL files +module declaration determines the namespace the record belongs to. +Each '.' delimited token in the module declaration results in the +creation of a namespace. For instance, the declaration module docs.links +results in the creation of a docs namespace and a nested +docs::links namespace. In the preceding examples, the Link class +is placed in the links namespace. The header file corresponding to +the links.jr file will contain: + +
      
      +namespace links {
      +  class Link : public hadoop::Record {
      +    // ....
      +  };
      +};
      +
      + +Each field within the record will cause the generation of a private member +declaration of the appropriate type in the class declaration, and one or more +acccessor methods. The generated class will implement the serialize and +deserialize methods defined in hadoop::Record+. It will also +implement the inspection methods type and signature from +hadoop::Record. A default constructor and virtual destructor will also +be generated. Serialization code will read/write records into streams that +implement the hadoop::InStream and the hadoop::OutStream interfaces. + +For each member of a record an accessor method is generated that returns +either the member or a reference to the member. For members that are returned +by value, a setter method is also generated. This is true for primitive +data members of the types byte, int, long, boolean, float and +double. For example, for a int field called MyField the folowing +code is generated. + +
      
      +...
      +private:
      +  int32_t mMyField;
      +  ...
      +public:
      +  int32_t getMyField(void) const {
      +    return mMyField;
      +  };
      +
      +  void setMyField(int32_t m) {
      +    mMyField = m;
      +  };
      +  ...
      +
      + +For a ustring or buffer or composite field. The generated code +only contains accessors that return a reference to the field. A const +and a non-const accessor are generated. For example: + +
      
      +...
      +private:
      +  std::string mMyBuf;
      +  ...
      +public:
      +
      +  std::string& getMyBuf() {
      +    return mMyBuf;
      +  };
      +
      +  const std::string& getMyBuf() const {
      +    return mMyBuf;
      +  };
      +  ...
      +
      + +

      Examples

      + +Suppose the inclrec.jr file contains: +
      
      +module inclrec {
      +    class RI {
      +        int      I32;
      +        double   D;
      +        ustring  S;
      +    };
      +}
      +
      + +and the testrec.jr file contains: + +
      
      +include "inclrec.jr"
      +module testrec {
      +    class R {
      +        vector VF;
      +        RI            Rec;
      +        buffer        Buf;
      +    };
      +}
      +
      + +Then the invocation of rcc such as: +
      
      +$ rcc -l c++ inclrec.jr testrec.jr
      +
      +will result in generation of four files: +inclrec.jr.{cc,hh} and testrec.jr.{cc,hh}. + +The inclrec.jr.hh will contain: + +
      
      +#ifndef _INCLREC_JR_HH_
      +#define _INCLREC_JR_HH_
      +
      +#include "recordio.hh"
      +
      +namespace inclrec {
      +  
      +  class RI : public hadoop::Record {
      +
      +  private:
      +
      +    int32_t      mI32;
      +    double       mD;
      +    std::string  mS;
      +
      +  public:
      +
      +    RI(void);
      +    virtual ~RI(void);
      +
      +    virtual bool operator==(const RI& peer) const;
      +    virtual bool operator<(const RI& peer) const;
      +
      +    virtual int32_t getI32(void) const { return mI32; }
      +    virtual void setI32(int32_t v) { mI32 = v; }
      +
      +    virtual double getD(void) const { return mD; }
      +    virtual void setD(double v) { mD = v; }
      +
      +    virtual std::string& getS(void) const { return mS; }
      +    virtual const std::string& getS(void) const { return mS; }
      +
      +    virtual std::string type(void) const;
      +    virtual std::string signature(void) const;
      +
      +  protected:
      +
      +    virtual void serialize(hadoop::OArchive& a) const;
      +    virtual void deserialize(hadoop::IArchive& a);
      +
      +    virtual bool validate(void);
      +  };
      +} // end namespace inclrec
      +
      +#endif /* _INCLREC_JR_HH_ */
      +
      +
      + +The testrec.jr.hh file will contain: + + +
      
      +
      +#ifndef _TESTREC_JR_HH_
      +#define _TESTREC_JR_HH_
      +
      +#include "inclrec.jr.hh"
      +
      +namespace testrec {
      +  class R : public hadoop::Record {
      +
      +  private:
      +
      +    std::vector mVF;
      +    inclrec::RI        mRec;
      +    std::string        mBuf;
      +
      +  public:
      +
      +    R(void);
      +    virtual ~R(void);
      +
      +    virtual bool operator==(const R& peer) const;
      +    virtual bool operator<(const R& peer) const;
      +
      +    virtual std::vector& getVF(void) const;
      +    virtual const std::vector& getVF(void) const;
      +
      +    virtual std::string& getBuf(void) const ;
      +    virtual const std::string& getBuf(void) const;
      +
      +    virtual inclrec::RI& getRec(void) const;
      +    virtual const inclrec::RI& getRec(void) const;
      +    
      +    virtual bool serialize(hadoop::OutArchive& a) const;
      +    virtual bool deserialize(hadoop::InArchive& a);
      +    
      +    virtual std::string type(void) const;
      +    virtual std::string signature(void) const;
      +  };
      +}; // end namespace testrec
      +#endif /* _TESTREC_JR_HH_ */
      +
      +
      + +

      Java

      + +Code generation for Java is similar to that for C++. A Java class is generated +for each record type with private members corresponding to the fields. Getters +and setters for fields are also generated. Some differences arise in the +way comparison is expressed and in the mapping of modules to packages and +classes to files. For equality testing, an equals method is generated +for each record type. As per Java requirements a hashCode method is also +generated. For comparison a compareTo method is generated for each +record type. This has the semantics as defined by the Java Comparable +interface, that is, the method returns a negative integer, zero, or a positive +integer as the invoked object is less than, equal to, or greater than the +comparison parameter. + +A .java file is generated per record type as opposed to per DDL +file as in C++. The module declaration translates to a Java +package declaration. The module name maps to an identical Java package +name. In addition to this mapping, the DDL compiler creates the appropriate +directory hierarchy for the package and places the generated .java +files in the correct directories. + +

      Mapping Summary

      + +
      
      +DDL Type        C++ Type            Java Type 
      +
      +boolean         bool                boolean
      +byte            int8_t              byte
      +int             int32_t             int
      +long            int64_t             long
      +float           float               float
      +double          double              double
      +ustring         std::string         Text
      +buffer          std::string         java.io.ByteArrayOutputStream
      +class type      class type          class type
      +vector    std::vector   java.util.ArrayList
      +map  std::map java.util.TreeMap
      +
      + +

      Data encodings

      + +This section describes the format of the data encodings supported by Hadoop. +Currently, one data encoding is supported, namely binary. + +

      Binary Serialization Format

      + +The binary data encoding format is fairly dense. Serialization of composite +types is simply defined as a concatenation of serializations of the constituent +elements (lengths are included in vectors and maps). + +Composite types are serialized as follows: +
        +
      • class: Sequence of serialized members. +
      • vector: The number of elements serialized as an int. Followed by a +sequence of serialized elements. +
      • map: The number of key value pairs serialized as an int. Followed +by a sequence of serialized (key,value) pairs. +
      + +Serialization of primitives is more interesting, with a zero compression +optimization for integral types and normalization to UTF-8 for strings. +Primitive types are serialized as follows: + +
        +
      • byte: Represented by 1 byte, as is. +
      • boolean: Represented by 1-byte (0 or 1) +
      • int/long: Integers and longs are serialized zero compressed. +Represented as 1-byte if -120 <= value < 128. Otherwise, serialized as a +sequence of 2-5 bytes for ints, 2-9 bytes for longs. The first byte represents +the number of trailing bytes, N, as the negative number (-120-N). For example, +the number 1024 (0x400) is represented by the byte sequence 'x86 x04 x00'. +This doesn't help much for 4-byte integers but does a reasonably good job with +longs without bit twiddling. +
      • float/double: Serialized in IEEE 754 single and double precision +format in network byte order. This is the format used by Java. +
      • ustring: Serialized as 4-byte zero compressed length followed by +data encoded as UTF-8. Strings are normalized to UTF-8 regardless of native +language representation. +
      • buffer: Serialized as a 4-byte zero compressed length followed by the +raw bytes in the buffer. +
      + + + diff --git a/zookeeper-jute/src/main/resources/zookeeper.jute b/zookeeper-jute/src/main/resources/zookeeper.jute new file mode 100644 index 0000000..898838f --- /dev/null +++ b/zookeeper-jute/src/main/resources/zookeeper.jute @@ -0,0 +1,355 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +module org.apache.zookeeper.data { + class Id { + ustring scheme; + ustring id; + } + class ACL { + int perms; + Id id; + } + // information shared with the client + class Stat { + long czxid; // created zxid + long mzxid; // last modified zxid + long ctime; // created + long mtime; // last modified + int version; // version + int cversion; // child version + int aversion; // acl version + long ephemeralOwner; // owner id if ephemeral, 0 otw + int dataLength; //length of the data in the node + int numChildren; //number of children of this node + long pzxid; // last modified children + } + // information explicitly stored by the server persistently + class StatPersisted { + long czxid; // created zxid + long mzxid; // last modified zxid + long ctime; // created + long mtime; // last modified + int version; // version + int cversion; // child version + int aversion; // acl version + long ephemeralOwner; // owner id if ephemeral, 0 otw + long pzxid; // last modified children + } +} + +module org.apache.zookeeper.proto { + class ConnectRequest { + int protocolVersion; + long lastZxidSeen; + int timeOut; + long sessionId; + buffer passwd; + } + class ConnectResponse { + int protocolVersion; + int timeOut; + long sessionId; + buffer passwd; + } + class SetWatches { + long relativeZxid; + vectordataWatches; + vectorexistWatches; + vectorchildWatches; + } + class SetWatches2 { + long relativeZxid; + vectordataWatches; + vectorexistWatches; + vectorchildWatches; + vectorpersistentWatches; + vectorpersistentRecursiveWatches; + } + class RequestHeader { + int xid; + int type; + } + class MultiHeader { + int type; + boolean done; + int err; + } + class AuthPacket { + int type; + ustring scheme; + buffer auth; + } + class ReplyHeader { + int xid; + long zxid; + int err; + } + + class GetDataRequest { + ustring path; + boolean watch; + } + + class SetDataRequest { + ustring path; + buffer data; + int version; + } + class ReconfigRequest { + ustring joiningServers; + ustring leavingServers; + ustring newMembers; + long curConfigId; + } + class SetDataResponse { + org.apache.zookeeper.data.Stat stat; + } + class GetSASLRequest { + buffer token; + } + class SetSASLRequest { + buffer token; + } + class SetSASLResponse { + buffer token; + } + class CreateRequest { + ustring path; + buffer data; + vector acl; + int flags; + } + class CreateTTLRequest { + ustring path; + buffer data; + vector acl; + int flags; + long ttl; + } + class DeleteRequest { + ustring path; + int version; + } + class GetChildrenRequest { + ustring path; + boolean watch; + } + class GetAllChildrenNumberRequest { + ustring path; + } + class GetChildren2Request { + ustring path; + boolean watch; + } + class CheckVersionRequest { + ustring path; + int version; + } + class GetMaxChildrenRequest { + ustring path; + } + class GetMaxChildrenResponse { + int max; + } + class SetMaxChildrenRequest { + ustring path; + int max; + } + class SyncRequest { + ustring path; + } + class SyncResponse { + ustring path; + } + class GetACLRequest { + ustring path; + } + class SetACLRequest { + ustring path; + vector acl; + int version; + } + class SetACLResponse { + org.apache.zookeeper.data.Stat stat; + } + class AddWatchRequest { + ustring path; + int mode; + } + class WatcherEvent { + int type; // event type + int state; // state of the Keeper client runtime + ustring path; + } + class ErrorResponse { + int err; + } + class CreateResponse { + ustring path; + } + class Create2Response { + ustring path; + org.apache.zookeeper.data.Stat stat; + } + class ExistsRequest { + ustring path; + boolean watch; + } + class ExistsResponse { + org.apache.zookeeper.data.Stat stat; + } + class GetDataResponse { + buffer data; + org.apache.zookeeper.data.Stat stat; + } + class GetChildrenResponse { + vector children; + } + class GetAllChildrenNumberResponse { + int totalNumber; + } + class GetChildren2Response { + vector children; + org.apache.zookeeper.data.Stat stat; + } + class GetACLResponse { + vector acl; + org.apache.zookeeper.data.Stat stat; + } + class CheckWatchesRequest { + ustring path; + int type; + } + class RemoveWatchesRequest { + ustring path; + int type; + } + + class GetEphemeralsRequest { + ustring prefixPath; + } + + class GetEphemeralsResponse { + vector ephemerals; + } +} + +module org.apache.zookeeper.server.quorum { + class LearnerInfo { + long serverid; + int protocolVersion; + long configVersion; + } + class QuorumPacket { + int type; // Request, Ack, Commit, Ping + long zxid; + buffer data; // Only significant when type is request + vector authinfo; + } + class QuorumAuthPacket { + long magic; + int status; + buffer token; + } +} + +module org.apache.zookeeper.server.persistence { + class FileHeader { + int magic; + int version; + long dbid; + } +} + +module org.apache.zookeeper.txn { + class TxnDigest { + int version; + long treeDigest; + } + class TxnHeader { + long clientId; + int cxid; + long zxid; + long time; + int type; + } + class CreateTxnV0 { + ustring path; + buffer data; + vector acl; + boolean ephemeral; + } + class CreateTxn { + ustring path; + buffer data; + vector acl; + boolean ephemeral; + int parentCVersion; + } + class CreateTTLTxn { + ustring path; + buffer data; + vector acl; + int parentCVersion; + long ttl; + } + class CreateContainerTxn { + ustring path; + buffer data; + vector acl; + int parentCVersion; + } + class DeleteTxn { + ustring path; + } + class SetDataTxn { + ustring path; + buffer data; + int version; + } + class CheckVersionTxn { + ustring path; + int version; + } + class SetACLTxn { + ustring path; + vector acl; + int version; + } + class SetMaxChildrenTxn { + ustring path; + int max; + } + class CreateSessionTxn { + int timeOut; + } + class CloseSessionTxn { + vector paths2Delete; + } + class ErrorTxn { + int err; + } + class Txn { + int type; + buffer data; + } + class MultiTxn { + vector txns; + } +} diff --git a/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java b/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java new file mode 100644 index 0000000..fe9124c --- /dev/null +++ b/zookeeper-jute/src/test/java/org/apache/jute/BinaryInputArchiveTest.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import org.junit.Test; + +// TODO: introduce JuteTestCase as in ZKTestCase + +/** + * + */ +public class BinaryInputArchiveTest { + + @Test + public void testReadStringCheckLength() { + byte[] buf = new byte[]{ + Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE, Byte.MAX_VALUE}; + ByteArrayInputStream is = new ByteArrayInputStream(buf); + BinaryInputArchive ia = BinaryInputArchive.getArchive(is); + try { + ia.readString(""); + fail("Should have thrown an IOException"); + } catch (IOException e) { + assertTrue("Not 'Unreasonable length' exception: " + e, + e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH)); + } + } + + private void checkWriterAndReader(TestWriter writer, TestReader reader) { + TestCheckWriterReader.checkWriterAndReader( + BinaryOutputArchive::getArchive, + BinaryInputArchive::getArchive, + writer, + reader + ); + } + + @Test + public void testInt() { + final int expected = 4; + final String tag = "tag1"; + checkWriterAndReader( + (oa) -> oa.writeInt(expected, tag), + (ia) -> { + int actual = ia.readInt(tag); + assertEquals(expected, actual); + } + ); + } + + @Test + public void testBool() { + final boolean expected = false; + final String tag = "tag1"; + checkWriterAndReader( + (oa) -> oa.writeBool(expected, tag), + (ia) -> { + boolean actual = ia.readBool(tag); + assertEquals(expected, actual); + } + ); + } + + @Test + public void testString() { + final String expected = "hello"; + final String tag = "tag1"; + checkWriterAndReader( + (oa) -> oa.writeString(expected, tag), + (ia) -> { + String actual = ia.readString(tag); + assertEquals(expected, actual); + } + ); + } + + @Test + public void testFloat() { + final float expected = 3.14159f; + final String tag = "tag1"; + final float delta = 1e-10f; + checkWriterAndReader( + (oa) -> oa.writeFloat(expected, tag), + (ia) -> { + float actual = ia.readFloat(tag); + assertEquals(expected, actual, delta); + } + ); + } + + @Test + public void testDouble() { + final double expected = 3.14159f; + final String tag = "tag1"; + final float delta = 1e-20f; + checkWriterAndReader( + (oa) -> oa.writeDouble(expected, tag), + (ia) -> { + double actual = ia.readDouble(tag); + assertEquals(expected, actual, delta); + } + ); + } + + @Test + public void testBuffer() { + final byte[] expected = "hello-world".getBytes(StandardCharsets.UTF_8); + final String tag = "tag1"; + checkWriterAndReader( + (oa) -> oa.writeBuffer(expected, tag), + (ia) -> { + byte[] actual = ia.readBuffer(tag); + assertArrayEquals(expected, actual); + } + ); + } + /** + * Record length is more than the maxbuffer + extrasize length. + */ + @Test + public void testReadStringForRecordsHavingLengthMoreThanMaxAllowedSize() { + int maxBufferSize = 2000; + int extraMaxBufferSize = 1025; + //this record size is more than the max allowed size + int recordSize = maxBufferSize + extraMaxBufferSize + 100; + BinaryInputArchive ia = + getBinaryInputArchive(recordSize, maxBufferSize, extraMaxBufferSize); + try { + ia.readString(""); + fail("Should have thrown an IOException"); + } catch (IOException e) { + assertTrue("Not 'Unreasonable length' exception: " + e, + e.getMessage().startsWith(BinaryInputArchive.UNREASONBLE_LENGTH)); + } + } + + /** + * Record length is less than then maxbuffer + extrasize length. + */ + @Test + public void testReadStringForRecordsHavingLengthLessThanMaxAllowedSize() + throws IOException { + int maxBufferSize = 2000; + int extraMaxBufferSize = 1025; + int recordSize = maxBufferSize + extraMaxBufferSize - 100; + //Exception is not expected as record size is less than the allowed size + BinaryInputArchive ia = + getBinaryInputArchive(recordSize, maxBufferSize, extraMaxBufferSize); + String s = ia.readString(""); + assertNotNull(s); + assertEquals(recordSize, s.getBytes().length); + } + + private BinaryInputArchive getBinaryInputArchive(int recordSize, int maxBufferSize, + int extraMaxBufferSize) { + byte[] data = getData(recordSize); + DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); + return new BinaryInputArchive(dis, maxBufferSize, extraMaxBufferSize); + } + + private byte[] getData(int recordSize) { + ByteBuffer buf = ByteBuffer.allocate(recordSize + 4); + buf.putInt(recordSize); + byte[] bytes = new byte[recordSize]; + for (int i = 0; i < recordSize; i++) { + bytes[i] = (byte) 'a'; + } + buf.put(bytes); + return buf.array(); + } + +} diff --git a/zookeeper-jute/src/test/java/org/apache/jute/TestCheckWriterReader.java b/zookeeper-jute/src/test/java/org/apache/jute/TestCheckWriterReader.java new file mode 100644 index 0000000..81067ce --- /dev/null +++ b/zookeeper-jute/src/test/java/org/apache/jute/TestCheckWriterReader.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * TestOutputArchive creates an output archive from a given outputstream. + */ +interface TestOutputArchive { + OutputArchive getArchive(OutputStream os) throws IOException; +} + +interface TestInputArchive { + InputArchive getArchive(InputStream is) throws IOException; +} + +class TestCheckWriterReader { + + static void checkWriterAndReader( + TestOutputArchive output, TestInputArchive input, + TestWriter writer, TestReader reader) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + OutputArchive oa = output.getArchive(baos); + writer.write(oa); + } catch (IOException e) { + fail("Should not throw IOException while writing"); + } + InputStream is = new ByteArrayInputStream(baos.toByteArray()); + try { + InputArchive ia = input.getArchive(is); + reader.read(ia); + } catch (IOException e) { + fail("Should not throw IOException while reading back"); + } + } + +} \ No newline at end of file diff --git a/zookeeper-jute/src/test/java/org/apache/jute/TestReader.java b/zookeeper-jute/src/test/java/org/apache/jute/TestReader.java new file mode 100644 index 0000000..f74a729 --- /dev/null +++ b/zookeeper-jute/src/test/java/org/apache/jute/TestReader.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; + +/** + * + */ +public interface TestReader { + void read(InputArchive ia) throws IOException; +} diff --git a/zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java b/zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java new file mode 100644 index 0000000..7cd5fde --- /dev/null +++ b/zookeeper-jute/src/test/java/org/apache/jute/TestWriter.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jute; + +import java.io.IOException; + +/** + * + */ +public interface TestWriter { + + /** + * Write to the given output archive. + */ + void write(OutputArchive oa) throws IOException; +} \ No newline at end of file diff --git a/zookeeper-metrics-providers/pom.xml b/zookeeper-metrics-providers/pom.xml new file mode 100644 index 0000000..5fe7f08 --- /dev/null +++ b/zookeeper-metrics-providers/pom.xml @@ -0,0 +1,39 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + .. + + + zookeeper-metrics-providers + pom + Apache ZooKeeper - Metrics Providers + ZooKeeper Metrics Providers + + + zookeeper-prometheus-metrics + + + diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml new file mode 100644 index 0000000..1e23c15 --- /dev/null +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml @@ -0,0 +1,85 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-metrics-providers + 3.6.3 + .. + + + zookeeper-prometheus-metrics + jar + Apache ZooKeeper - Prometheus.io Metrics Provider + ZooKeeper Prometheus.io Metrics Provider implementation + + + 0.6.0 + + + + org.apache.zookeeper + + zookeeper + ${project.version} + + + io.prometheus + simpleclient + ${prometheus.version} + + + io.prometheus + simpleclient_hotspot + ${prometheus.version} + + + io.prometheus + simpleclient_servlet + ${prometheus.version} + + + org.eclipse.jetty + jetty-server + provided + + + org.eclipse.jetty + jetty-servlet + provided + + + org.mockito + mockito-core + test + + + junit + junit + test + + + + diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java new file mode 100644 index 0000000..d3628a7 --- /dev/null +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java @@ -0,0 +1,409 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.prometheus; + +import io.prometheus.client.Collector; +import io.prometheus.client.CollectorRegistry; +import io.prometheus.client.exporter.MetricsServlet; +import io.prometheus.client.hotspot.DefaultExports; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Objects; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.BiConsumer; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.zookeeper.metrics.Counter; +import org.apache.zookeeper.metrics.Gauge; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; +import org.apache.zookeeper.metrics.Summary; +import org.apache.zookeeper.metrics.SummarySet; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A Metrics Provider implementation based on https://prometheus.io. + * + * @since 3.6.0 + */ +public class PrometheusMetricsProvider implements MetricsProvider { + + private static final Logger LOG = LoggerFactory.getLogger(PrometheusMetricsProvider.class); + private static final String LABEL = "key"; + private static final String[] LABELS = {LABEL}; + /** + * We are using the 'defaultRegistry'. + *

      + * When you are running ZooKeeper (server or client) together with other + * libraries every metrics will be expected as a single view. + *

      + */ + private final CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry; + private int port = 7000; + private boolean exportJvmInfo = true; + private Server server; + private final MetricsServletImpl servlet = new MetricsServletImpl(); + private final Context rootContext = new Context(); + + @Override + public void configure(Properties configuration) throws MetricsProviderLifeCycleException { + LOG.info("Initializing metrics, configuration: {}", configuration); + this.port = Integer.parseInt(configuration.getProperty("httpPort", "7000")); + this.exportJvmInfo = Boolean.parseBoolean(configuration.getProperty("exportJvmInfo", "true")); + } + + @Override + public void start() throws MetricsProviderLifeCycleException { + try { + LOG.info("Starting /metrics HTTP endpoint at port {} exportJvmInfo: {}", port, exportJvmInfo); + if (exportJvmInfo) { + DefaultExports.initialize(); + } + server = new Server(port); + ServletContextHandler context = new ServletContextHandler(); + context.setContextPath("/"); + server.setHandler(context); + context.addServlet(new ServletHolder(servlet), "/metrics"); + server.start(); + } catch (Exception err) { + LOG.error("Cannot start /metrics server", err); + if (server != null) { + try { + server.stop(); + } catch (Exception suppressed) { + err.addSuppressed(suppressed); + } finally { + server = null; + } + } + throw new MetricsProviderLifeCycleException(err); + } + } + + // for tests + MetricsServletImpl getServlet() { + return servlet; + } + + @Override + public MetricsContext getRootContext() { + return rootContext; + } + + @Override + public void stop() { + if (server != null) { + try { + server.stop(); + } catch (Exception err) { + LOG.error("Cannot safely stop Jetty server", err); + } finally { + server = null; + } + } + } + + /** + * Dump all values to the 4lw interface and to the Admin server. + *

      + * This method is not expected to be used to serve metrics to Prometheus. We + * are using the MetricsServlet provided by Prometheus for that, leaving the + * real representation to the Prometheus Java client. + *

      + * + * @param sink the receiver of data (4lw interface, Admin server or tests) + */ + @Override + public void dump(BiConsumer sink) { + sampleGauges(); + Enumeration samplesFamilies = collectorRegistry.metricFamilySamples(); + while (samplesFamilies.hasMoreElements()) { + Collector.MetricFamilySamples samples = samplesFamilies.nextElement(); + samples.samples.forEach(sample -> { + String key = buildKeyForDump(sample); + sink.accept(key, sample.value); + }); + } + } + + private static String buildKeyForDump(Collector.MetricFamilySamples.Sample sample) { + StringBuilder keyBuilder = new StringBuilder(); + keyBuilder.append(sample.name); + if (sample.labelNames.size() > 0) { + keyBuilder.append('{'); + for (int i = 0; i < sample.labelNames.size(); ++i) { + if (i > 0) { + keyBuilder.append(','); + } + keyBuilder.append(sample.labelNames.get(i)); + keyBuilder.append("=\""); + keyBuilder.append(sample.labelValues.get(i)); + keyBuilder.append('"'); + } + keyBuilder.append('}'); + } + return keyBuilder.toString(); + } + + /** + * Update Gauges. In ZooKeeper Metrics API Gauges are callbacks served by + * internal components and the value is not held by Prometheus structures. + */ + private void sampleGauges() { + rootContext.gauges.values() + .forEach(PrometheusGaugeWrapper::sample); + } + + @Override + public void resetAllValues() { + // not supported on Prometheus + } + + private class Context implements MetricsContext { + + private final ConcurrentMap gauges = new ConcurrentHashMap<>(); + private final ConcurrentMap counters = new ConcurrentHashMap<>(); + private final ConcurrentMap basicSummaries = new ConcurrentHashMap<>(); + private final ConcurrentMap summaries = new ConcurrentHashMap<>(); + private final ConcurrentMap basicSummarySets = new ConcurrentHashMap<>(); + private final ConcurrentMap summarySets = new ConcurrentHashMap<>(); + + @Override + public MetricsContext getContext(String name) { + // no hierarchy yet + return this; + } + + @Override + public Counter getCounter(String name) { + return counters.computeIfAbsent(name, PrometheusCounter::new); + } + + /** + * Gauges may go up and down, in ZooKeeper they are a way to export + * internal values with a callback. + * + * @param name the name of the gauge + * @param gauge the callback + */ + @Override + public void registerGauge(String name, Gauge gauge) { + Objects.requireNonNull(name); + gauges.compute(name, (id, prev) -> + new PrometheusGaugeWrapper(id, gauge, prev != null ? prev.inner : null)); + } + + @Override + public void unregisterGauge(String name) { + PrometheusGaugeWrapper existing = gauges.remove(name); + if (existing != null) { + existing.unregister(); + } + } + + @Override + public Summary getSummary(String name, DetailLevel detailLevel) { + if (detailLevel == DetailLevel.BASIC) { + return basicSummaries.computeIfAbsent(name, (n) -> { + if (summaries.containsKey(n)) { + throw new IllegalArgumentException("Already registered a non basic summary as " + n); + } + return new PrometheusSummary(name, detailLevel); + }); + } else { + return summaries.computeIfAbsent(name, (n) -> { + if (basicSummaries.containsKey(n)) { + throw new IllegalArgumentException("Already registered a basic summary as " + n); + } + return new PrometheusSummary(name, detailLevel); + }); + } + } + + @Override + public SummarySet getSummarySet(String name, DetailLevel detailLevel) { + if (detailLevel == DetailLevel.BASIC) { + return basicSummarySets.computeIfAbsent(name, (n) -> { + if (summarySets.containsKey(n)) { + throw new IllegalArgumentException("Already registered a non basic summary set as " + n); + } + return new PrometheusLabelledSummary(name, detailLevel); + }); + } else { + return summarySets.computeIfAbsent(name, (n) -> { + if (basicSummarySets.containsKey(n)) { + throw new IllegalArgumentException("Already registered a basic summary set as " + n); + } + return new PrometheusLabelledSummary(name, detailLevel); + }); + } + } + + } + + private class PrometheusCounter implements Counter { + + private final io.prometheus.client.Counter inner; + private final String name; + + public PrometheusCounter(String name) { + this.name = name; + this.inner = io.prometheus.client.Counter + .build(name, name) + .register(collectorRegistry); + } + + @Override + public void add(long delta) { + try { + inner.inc(delta); + } catch (IllegalArgumentException err) { + LOG.error("invalid delta {} for metric {}", delta, name, err); + } + } + + @Override + public long get() { + // this method is used only for tests + // Prometheus returns a "double" + // it is safe to fine to a long + // we are never setting non-integer values + return (long) inner.get(); + } + + } + + private class PrometheusGaugeWrapper { + + private final io.prometheus.client.Gauge inner; + private final Gauge gauge; + private final String name; + + public PrometheusGaugeWrapper(String name, Gauge gauge, io.prometheus.client.Gauge prev) { + this.name = name; + this.gauge = gauge; + this.inner = prev != null ? prev + : io.prometheus.client.Gauge + .build(name, name) + .register(collectorRegistry); + } + + /** + * Call the callack and update Prometheus Gauge. This method is called + * when the server is polling for a value. + */ + private void sample() { + Number value = gauge.get(); + this.inner.set(value != null ? value.doubleValue() : 0); + } + + private void unregister() { + collectorRegistry.unregister(inner); + } + + } + + private class PrometheusSummary implements Summary { + + private final io.prometheus.client.Summary inner; + private final String name; + + public PrometheusSummary(String name, MetricsContext.DetailLevel level) { + this.name = name; + if (level == MetricsContext.DetailLevel.ADVANCED) { + this.inner = io.prometheus.client.Summary + .build(name, name) + .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error + .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error + .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error + .register(collectorRegistry); + } else { + this.inner = io.prometheus.client.Summary + .build(name, name) + .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error + .register(collectorRegistry); + } + } + + @Override + public void add(long delta) { + try { + inner.observe(delta); + } catch (IllegalArgumentException err) { + LOG.error("invalid delta {} for metric {}", delta, name, err); + } + } + + } + + private class PrometheusLabelledSummary implements SummarySet { + + private final io.prometheus.client.Summary inner; + private final String name; + + public PrometheusLabelledSummary(String name, MetricsContext.DetailLevel level) { + this.name = name; + if (level == MetricsContext.DetailLevel.ADVANCED) { + this.inner = io.prometheus.client.Summary + .build(name, name) + .labelNames(LABELS) + .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error + .quantile(0.9, 0.01) // Add 90th percentile with 1% tolerated error + .quantile(0.99, 0.001) // Add 99th percentile with 0.1% tolerated error + .register(collectorRegistry); + } else { + this.inner = io.prometheus.client.Summary + .build(name, name) + .labelNames(LABELS) + .quantile(0.5, 0.05) // Add 50th percentile (= median) with 5% tolerated error + .register(collectorRegistry); + } + } + + @Override + public void add(String key, long value) { + try { + inner.labels(key).observe(value); + } catch (IllegalArgumentException err) { + LOG.error("invalid value {} for metric {} with key {}", value, name, key, err); + } + } + + } + + class MetricsServletImpl extends MetricsServlet { + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + // little trick: update the Gauges before serving data + // from Prometheus CollectorRegistry + sampleGauges(); + // serve data using Prometheus built in client. + super.doGet(req, resp); + } + } +} diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/ExportJvmInfoTest.java b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/ExportJvmInfoTest.java new file mode 100644 index 0000000..fb5353d --- /dev/null +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/ExportJvmInfoTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.prometheus; + +import static org.junit.Assert.assertEquals; +import io.prometheus.client.CollectorRegistry; +import java.util.Properties; +import org.junit.Test; + +/** + * Tests about Prometheus Metrics Provider. Please note that we are not testing + * Prometheus but our integration. + */ +public class ExportJvmInfoTest { + + @Test + public void exportInfo() throws Exception { + runTest(true); + } + + @Test + public void doNotExportInfo() throws Exception { + runTest(false); + } + + private void runTest(boolean exportJvmInfo) throws Exception { + CollectorRegistry.defaultRegistry.clear(); + PrometheusMetricsProvider provider = new PrometheusMetricsProvider(); + try { + Properties configuration = new Properties(); + configuration.setProperty("httpPort", "0"); // ephemeral port + configuration.setProperty("exportJvmInfo", exportJvmInfo + ""); + provider.configure(configuration); + provider.start(); + boolean[] found = {false}; + provider.dump((k, v) -> { + found[0] = found[0] || k.contains("heap"); + }); + assertEquals(exportJvmInfo, found[0]); + } finally { + provider.stop(); + } + } + +} diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProviderTest.java b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProviderTest.java new file mode 100644 index 0000000..2d691db --- /dev/null +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProviderTest.java @@ -0,0 +1,296 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.prometheus; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import io.prometheus.client.CollectorRegistry; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Properties; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.zookeeper.metrics.Counter; +import org.apache.zookeeper.metrics.Gauge; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.metrics.Summary; +import org.hamcrest.CoreMatchers; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests about Prometheus Metrics Provider. Please note that we are not testing + * Prometheus but only our integration. + */ +public class PrometheusMetricsProviderTest { + + private PrometheusMetricsProvider provider; + + @Before + public void setup() throws Exception { + CollectorRegistry.defaultRegistry.clear(); + provider = new PrometheusMetricsProvider(); + Properties configuration = new Properties(); + configuration.setProperty("httpPort", "0"); // ephemeral port + configuration.setProperty("exportJvmInfo", "false"); + provider.configure(configuration); + provider.start(); + } + + @After + public void tearDown() { + if (provider != null) { + provider.stop(); + } + CollectorRegistry.defaultRegistry.clear(); + } + + @Test + public void testCounters() throws Exception { + Counter counter = provider.getRootContext().getCounter("cc"); + counter.add(10); + int[] count = {0}; + provider.dump((k, v) -> { + assertEquals("cc", k); + assertEquals(10, ((Number) v).intValue()); + count[0]++; + } + ); + assertEquals(1, count[0]); + count[0] = 0; + + // this is not allowed but it must not throw errors + counter.add(-1); + + provider.dump((k, v) -> { + assertEquals("cc", k); + assertEquals(10, ((Number) v).intValue()); + count[0]++; + } + ); + assertEquals(1, count[0]); + + // we always must get the same object + assertSame(counter, provider.getRootContext().getCounter("cc")); + + String res = callServlet(); + assertThat(res, CoreMatchers.containsString("# TYPE cc counter")); + assertThat(res, CoreMatchers.containsString("cc 10.0")); + } + + @Test + public void testGauge() throws Exception { + int[] values = {78, -89}; + int[] callCounts = {0, 0}; + Gauge gauge0 = () -> { + callCounts[0]++; + return values[0]; + }; + Gauge gauge1 = () -> { + callCounts[1]++; + return values[1]; + }; + provider.getRootContext().registerGauge("gg", gauge0); + + int[] count = {0}; + provider.dump((k, v) -> { + assertEquals("gg", k); + assertEquals(values[0], ((Number) v).intValue()); + count[0]++; + } + ); + assertEquals(1, callCounts[0]); + assertEquals(0, callCounts[1]); + assertEquals(1, count[0]); + count[0] = 0; + String res2 = callServlet(); + assertThat(res2, CoreMatchers.containsString("# TYPE gg gauge")); + assertThat(res2, CoreMatchers.containsString("gg 78.0")); + + provider.getRootContext().unregisterGauge("gg"); + provider.dump((k, v) -> { + count[0]++; + } + ); + assertEquals(2, callCounts[0]); + assertEquals(0, callCounts[1]); + assertEquals(0, count[0]); + String res3 = callServlet(); + assertTrue(res3.isEmpty()); + + provider.getRootContext().registerGauge("gg", gauge1); + + provider.dump((k, v) -> { + assertEquals("gg", k); + assertEquals(values[1], ((Number) v).intValue()); + count[0]++; + } + ); + assertEquals(2, callCounts[0]); + assertEquals(1, callCounts[1]); + assertEquals(1, count[0]); + count[0] = 0; + + String res4 = callServlet(); + assertThat(res4, CoreMatchers.containsString("# TYPE gg gauge")); + assertThat(res4, CoreMatchers.containsString("gg -89.0")); + assertEquals(2, callCounts[0]); + // the servlet must sample the value again (from gauge1) + assertEquals(2, callCounts[1]); + + // override gauge, without unregister + provider.getRootContext().registerGauge("gg", gauge0); + + provider.dump((k, v) -> { + count[0]++; + } + ); + assertEquals(1, count[0]); + assertEquals(3, callCounts[0]); + assertEquals(2, callCounts[1]); + } + + @Test + public void testBasicSummary() throws Exception { + Summary summary = provider.getRootContext() + .getSummary("cc", MetricsContext.DetailLevel.BASIC); + summary.add(10); + summary.add(10); + int[] count = {0}; + provider.dump((k, v) -> { + count[0]++; + int value = ((Number) v).intValue(); + + switch (k) { + case "cc{quantile=\"0.5\"}": + assertEquals(10, value); + break; + case "cc_count": + assertEquals(2, value); + break; + case "cc_sum": + assertEquals(20, value); + break; + default: + fail("unespected key " + k); + break; + } + } + ); + assertEquals(3, count[0]); + count[0] = 0; + + // we always must get the same object + assertSame(summary, provider.getRootContext() + .getSummary("cc", MetricsContext.DetailLevel.BASIC)); + + try { + provider.getRootContext() + .getSummary("cc", MetricsContext.DetailLevel.ADVANCED); + fail("Can't get the same summary with a different DetailLevel"); + } catch (IllegalArgumentException err) { + assertThat(err.getMessage(), containsString("Already registered")); + } + + String res = callServlet(); + assertThat(res, containsString("# TYPE cc summary")); + assertThat(res, CoreMatchers.containsString("cc_sum 20.0")); + assertThat(res, CoreMatchers.containsString("cc_count 2.0")); + assertThat(res, CoreMatchers.containsString("cc{quantile=\"0.5\",} 10.0")); + } + + @Test + public void testAdvancedSummary() throws Exception { + Summary summary = provider.getRootContext() + .getSummary("cc", MetricsContext.DetailLevel.ADVANCED); + summary.add(10); + summary.add(10); + int[] count = {0}; + provider.dump((k, v) -> { + count[0]++; + int value = ((Number) v).intValue(); + + switch (k) { + case "cc{quantile=\"0.5\"}": + assertEquals(10, value); + break; + case "cc{quantile=\"0.9\"}": + assertEquals(10, value); + break; + case "cc{quantile=\"0.99\"}": + assertEquals(10, value); + break; + case "cc_count": + assertEquals(2, value); + break; + case "cc_sum": + assertEquals(20, value); + break; + default: + fail("unespected key " + k); + break; + } + } + ); + assertEquals(5, count[0]); + count[0] = 0; + + // we always must get the same object + assertSame(summary, provider.getRootContext() + .getSummary("cc", MetricsContext.DetailLevel.ADVANCED)); + + try { + provider.getRootContext() + .getSummary("cc", MetricsContext.DetailLevel.BASIC); + fail("Can't get the same summary with a different DetailLevel"); + } catch (IllegalArgumentException err) { + assertThat(err.getMessage(), containsString("Already registered")); + } + + String res = callServlet(); + assertThat(res, containsString("# TYPE cc summary")); + assertThat(res, CoreMatchers.containsString("cc_sum 20.0")); + assertThat(res, CoreMatchers.containsString("cc_count 2.0")); + assertThat(res, CoreMatchers.containsString("cc{quantile=\"0.5\",} 10.0")); + assertThat(res, CoreMatchers.containsString("cc{quantile=\"0.9\",} 10.0")); + assertThat(res, CoreMatchers.containsString("cc{quantile=\"0.99\",} 10.0")); + } + + private String callServlet() throws ServletException, IOException { + // we are not performing an HTTP request + // but we are calling directly the servlet + StringWriter writer = new StringWriter(); + HttpServletResponse response = mock(HttpServletResponse.class); + when(response.getWriter()).thenReturn(new PrintWriter(writer)); + HttpServletRequest req = mock(HttpServletRequest.class); + provider.getServlet().doGet(req, response); + String res = writer.toString(); + return res; + } + +} diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/resources/log4j.properties b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/resources/log4j.properties new file mode 100644 index 0000000..d7f930d --- /dev/null +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/resources/log4j.properties @@ -0,0 +1,65 @@ +# Copyright 2012 The Apache Software Foundation +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Define some default values that can be overridden by system properties +zookeeper.root.logger=INFO, CONSOLE + +zookeeper.console.threshold=INFO + +zookeeper.log.dir=. +zookeeper.log.file=zookeeper.log +zookeeper.log.threshold=INFO +zookeeper.log.maxfilesize=256MB +zookeeper.log.maxbackupindex=20 + +zookeeper.tracelog.dir=${zookeeper.log.dir} +zookeeper.tracelog.file=zookeeper_trace.log + +log4j.rootLogger=${zookeeper.root.logger} + +# +# console +# Add "console" to rootlogger above if you want to use this +# +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold} +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n + +# +# Add ROLLINGFILE to rootLogger to get log file output +# +log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender +log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold} +log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file} +log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize} +log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex} +log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout +log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n + +# +# Add TRACEFILE to rootLogger to get log file output +# Log TRACE level and above messages to a log file +# +log4j.appender.TRACEFILE=org.apache.log4j.FileAppender +log4j.appender.TRACEFILE.Threshold=TRACE +log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file} + +log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout +### Notice we are including log4j's NDC here (%x) +log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n diff --git a/zookeeper-recipes/README.txt b/zookeeper-recipes/README.txt new file mode 100644 index 0000000..856dae0 --- /dev/null +++ b/zookeeper-recipes/README.txt @@ -0,0 +1,26 @@ +1) This source directory contains various Zookeeper recipe implementations. + +2) The recipe directory name should specify the name of the recipe you are implementing - eg. zookeeper-recipes-lock/. + +3) It would be great if you can provide both the java and c recipes for the zookeeper recipes. + C recipes go in to zookeeper-recipes/zookeeper-recipes-[recipe-name]/src/c + Java implementation goes into zookeeper-recipes/zookeeper-recipes-[recipe-name]/src/java. + +4) The recipes hold high standards like our zookeeper c/java libraries, so make sure that you include +some unit testing with both the c and java recipe code. + +5) Also, please name your c client public methods as +zkr_recipe-name_methodname +(eg. zkr_lock_lock in zookeeper-recipes-lock/src/c) + +6) The various recipes are in ../docs/recipes.html or +../../docs/reciped.pdf. Also, this is not an exhaustive list by any chance. +Zookeeper is used (and can be used) for more than what we have listed in the docs. + +7) To run the c tests in all the recipes, +- make sure the main zookeeper c libraries in +{top}/src/c/ are compiled. Run autoreconf -if;./configure; make. The libaries +will be installed in {top}/src/c/.libs. +- run autoreconf if;./configure;make run-check + in zookeeper-recipes/$recipename/src/c + diff --git a/zookeeper-recipes/build-recipes.xml b/zookeeper-recipes/build-recipes.xml new file mode 100644 index 0000000..d5bf87e --- /dev/null +++ b/zookeeper-recipes/build-recipes.xml @@ -0,0 +1,161 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-recipes/build.xml b/zookeeper-recipes/build.xml new file mode 100644 index 0000000..559d5a5 --- /dev/null +++ b/zookeeper-recipes/build.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-recipes/pom.xml b/zookeeper-recipes/pom.xml new file mode 100644 index 0000000..671d0dc --- /dev/null +++ b/zookeeper-recipes/pom.xml @@ -0,0 +1,112 @@ + + + + 4.0.0 + + + org.apache.zookeeper + parent + 3.6.3 + + + zookeeper-recipes + pom + Apache ZooKeeper - Recipes + + 1) This module contains various Zookeeper recipe implementations. + + 2) The recipe directory name should specify the name of the recipe you are implementing - eg. zookeeper-recipes-lock. + + 3) It would be great if you can provide both the java and c recipes for the zookeeper recipes. + C recipes go in to zookeeper-recipes/zookeeper-recipes-[recipe-name]/src/c + Java implementation goes into zookeeper-recipes/zookeeper-recipes-[recipe-name]/src/java. + + 4) The recipes hold high standards like our zookeeper c/java libraries, so make sure that you include + some unit testing with both the c and java recipe code. + + 5) Also, please name your c client public methods as + zkr_recipe-name_methodname + (eg. zkr_lock_lock in zookeeper-recipes-lock/src/c) + + 6) To run the c tests in all the recipes, + - make sure the main zookeeper c libraries in + zookeeper-client-c are compiled. Run autoreconf -if;./configure; make. The libraries + will be installed in {top}/src/c/.libs. + - run autoreconf if;./configure;make run-check + in zookeeper-recipes/$recipename/src/c + + + + + zookeeper-recipes-election + zookeeper-recipes-lock + zookeeper-recipes-queue + + + + + org.apache.zookeeper + zookeeper + ${project.version} + + + org.apache.zookeeper + zookeeper + ${project.version} + test-jar + test + + + org.xerial.snappy + snappy-java + test + + + junit + junit + test + + + io.dropwizard.metrics + metrics-core + test + + + com.github.spotbugs + spotbugs-annotations + provided + true + + + + + + + maven-deploy-plugin + + + true + + + + + + \ No newline at end of file diff --git a/zookeeper-recipes/zookeeper-recipes-election/README.txt b/zookeeper-recipes/zookeeper-recipes-election/README.txt new file mode 100644 index 0000000..f854b27 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/README.txt @@ -0,0 +1,27 @@ + + +1) This election interface recipe implements the leader election recipe +mentioned in ../../docs/recipes.[html,pdf]. + +2) To compile the leader election java recipe you can just run ant jar from +this directory. +Please report any bugs on the jira + +http://issues.apache.org/jira/browse/ZOOKEEPER + + diff --git a/zookeeper-recipes/zookeeper-recipes-election/build.xml b/zookeeper-recipes/zookeeper-recipes-election/build.xml new file mode 100644 index 0000000..05c467a --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/build.xml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-recipes/zookeeper-recipes-election/pom.xml b/zookeeper-recipes/zookeeper-recipes-election/pom.xml new file mode 100644 index 0000000..8bd20ff --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/pom.xml @@ -0,0 +1,55 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-recipes + 3.6.3 + + + zookeeper-recipes-election + jar + Apache ZooKeeper - Recipes - Election + + This election interface recipe implements the leader election recipe + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${surefire-forkcount} + false + -Xmx512m + ${project.basedir} + true + + ${project.build.directory}/surefire + + + + + + + \ No newline at end of file diff --git a/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderElectionAware.java b/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderElectionAware.java new file mode 100644 index 0000000..06804a8 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderElectionAware.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.leader; + +import org.apache.zookeeper.recipes.leader.LeaderElectionSupport.EventType; + +/** + * An interface to be implemented by clients that want to receive election + * events. + */ +public interface LeaderElectionAware { + + /** + * Called during each state transition. Current, low level events are provided + * at the beginning and end of each state. For instance, START may be followed + * by OFFER_START, OFFER_COMPLETE, DETERMINE_START, DETERMINE_COMPLETE, and so + * on. + * + * @param eventType + */ + void onElectionEvent(EventType eventType); + +} diff --git a/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupport.java b/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupport.java new file mode 100644 index 0000000..17419a8 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupport.java @@ -0,0 +1,471 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.leader; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +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.Stat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A leader election support library implementing the ZooKeeper election recipe. + * + *

      This support library is meant to simplify the construction of an exclusive + * leader system on top of Apache ZooKeeper. Any application that can become the + * leader (usually a process that provides a service, exclusively) would + * configure an instance of this class with their hostname, at least one + * listener (an implementation of {@link LeaderElectionAware}), and either an + * instance of {@link ZooKeeper} or the proper connection information. Once + * configured, invoking {@link #start()} will cause the client to connect to + * ZooKeeper and create a leader offer. The library then determines if it has + * been elected the leader using the algorithm described below. The client + * application can follow all state transitions via the listener callback. + * + *

      Leader election algorithm + * + *

      The library starts in a START state. Through each state transition, a state + * start and a state complete event are sent to all listeners. When + * {@link #start()} is called, a leader offer is created in ZooKeeper. A leader + * offer is an ephemeral sequential node that indicates a process that can act + * as a leader for this service. A read of all leader offers is then performed. + * The offer with the lowest sequence number is said to be the leader. The + * process elected leader will transition to the leader state. All other + * processes will transition to a ready state. Internally, the library creates a + * ZooKeeper watch on the leader offer with the sequence ID of N - 1 (where N is + * the process's sequence ID). If that offer disappears due to a process + * failure, the watching process will run through the election determination + * process again to see if it should become the leader. Note that sequence ID + * may not be contiguous due to failed processes. A process may revoke its offer + * to be the leader at any time by calling {@link #stop()}. + * + *

      Guarantees (not) Made and Caveats + * + *

        + *
      • It is possible for a (poorly implemented) process to create a leader + * offer, get the lowest sequence ID, but have something terrible occur where it + * maintains its connection to ZK (and thus its ephemeral leader offer node) but + * doesn't actually provide the service in question. It is up to the user to + * ensure any failure to become the leader - and whatever that means in the + * context of the user's application - results in a revocation of its leader + * offer (i.e. that {@link #stop()} is called).
      • + *
      • It is possible for ZK timeouts and retries to play a role in service + * liveliness. In other words, if process A has the lowest sequence ID but + * requires a few attempts to read the other leader offers' sequence IDs, + * election can seem slow. Users should apply timeouts during the determination + * process if they need to hit a specific SLA.
      • + *
      • The library makes a "best effort" to detect catastrophic failures of the + * process. It is possible that an unforeseen event results in (for instance) an + * unchecked exception that propagates passed normal error handling code. This + * normally doesn't matter as the same exception would almost certain destroy + * the entire process and thus the connection to ZK and the leader offer + * resulting in another round of leader determination.
      • + *
      + */ +public class LeaderElectionSupport implements Watcher { + + private static final Logger LOG = LoggerFactory.getLogger(LeaderElectionSupport.class); + + private ZooKeeper zooKeeper; + + private State state; + private Set listeners; + + private String rootNodeName; + private LeaderOffer leaderOffer; + private String hostName; + + public LeaderElectionSupport() { + state = State.STOP; + listeners = Collections.synchronizedSet(new HashSet<>()); + } + + /** + *

      + * Start the election process. This method will create a leader offer, + * determine its status, and either become the leader or become ready. If an + * instance of {@link ZooKeeper} has not yet been configured by the user, a + * new instance is created using the connectString and sessionTime specified. + *

      + *

      + * Any (anticipated) failures result in a failed event being sent to all + * listeners. + *

      + */ + public synchronized void start() { + state = State.START; + dispatchEvent(EventType.START); + + LOG.info("Starting leader election support"); + + if (zooKeeper == null) { + throw new IllegalStateException( + "No instance of zookeeper provided. Hint: use setZooKeeper()"); + } + + if (hostName == null) { + throw new IllegalStateException( + "No hostname provided. Hint: use setHostName()"); + } + + try { + makeOffer(); + determineElectionStatus(); + } catch (KeeperException | InterruptedException e) { + becomeFailed(e); + } + } + + /** + * Stops all election services, revokes any outstanding leader offers, and + * disconnects from ZooKeeper. + */ + public synchronized void stop() { + state = State.STOP; + dispatchEvent(EventType.STOP_START); + + LOG.info("Stopping leader election support"); + + if (leaderOffer != null) { + try { + zooKeeper.delete(leaderOffer.getNodePath(), -1); + LOG.info("Removed leader offer {}", leaderOffer.getNodePath()); + } catch (InterruptedException | KeeperException e) { + becomeFailed(e); + } + } + + dispatchEvent(EventType.STOP_COMPLETE); + } + + private void makeOffer() throws KeeperException, InterruptedException { + state = State.OFFER; + dispatchEvent(EventType.OFFER_START); + + LeaderOffer newLeaderOffer = new LeaderOffer(); + byte[] hostnameBytes; + synchronized (this) { + newLeaderOffer.setHostName(hostName); + hostnameBytes = hostName.getBytes(); + newLeaderOffer.setNodePath(zooKeeper.create(rootNodeName + "/" + "n_", + hostnameBytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL_SEQUENTIAL)); + leaderOffer = newLeaderOffer; + } + LOG.debug("Created leader offer {}", leaderOffer); + + dispatchEvent(EventType.OFFER_COMPLETE); + } + + private synchronized LeaderOffer getLeaderOffer() { + return leaderOffer; + } + + private void determineElectionStatus() throws KeeperException, InterruptedException { + + state = State.DETERMINE; + dispatchEvent(EventType.DETERMINE_START); + + LeaderOffer currentLeaderOffer = getLeaderOffer(); + + String[] components = currentLeaderOffer.getNodePath().split("/"); + + currentLeaderOffer.setId(Integer.valueOf(components[components.length - 1].substring("n_".length()))); + + List leaderOffers = toLeaderOffers(zooKeeper.getChildren(rootNodeName, false)); + + /* + * For each leader offer, find out where we fit in. If we're first, we + * become the leader. If we're not elected the leader, attempt to stat the + * offer just less than us. If they exist, watch for their failure, but if + * they don't, become the leader. + */ + for (int i = 0; i < leaderOffers.size(); i++) { + LeaderOffer leaderOffer = leaderOffers.get(i); + + if (leaderOffer.getId().equals(currentLeaderOffer.getId())) { + LOG.debug("There are {} leader offers. I am {} in line.", leaderOffers.size(), i); + + dispatchEvent(EventType.DETERMINE_COMPLETE); + + if (i == 0) { + becomeLeader(); + } else { + becomeReady(leaderOffers.get(i - 1)); + } + + /* Once we've figured out where we are, we're done. */ + break; + } + } + } + + private void becomeReady(LeaderOffer neighborLeaderOffer) + throws KeeperException, InterruptedException { + + LOG.info( + "{} not elected leader. Watching node: {}", + getLeaderOffer().getNodePath(), + neighborLeaderOffer.getNodePath()); + + /* + * Make sure to pass an explicit Watcher because we could be sharing this + * zooKeeper instance with someone else. + */ + Stat stat = zooKeeper.exists(neighborLeaderOffer.getNodePath(), this); + + if (stat != null) { + dispatchEvent(EventType.READY_START); + LOG.debug( + "We're behind {} in line and they're alive. Keeping an eye on them.", + neighborLeaderOffer.getNodePath()); + state = State.READY; + dispatchEvent(EventType.READY_COMPLETE); + } else { + /* + * If the stat fails, the node has gone missing between the call to + * getChildren() and exists(). We need to try and become the leader. + */ + LOG.info( + "We were behind {} but it looks like they died. Back to determination.", + neighborLeaderOffer.getNodePath()); + determineElectionStatus(); + } + + } + + private void becomeLeader() { + state = State.ELECTED; + dispatchEvent(EventType.ELECTED_START); + + LOG.info("Becoming leader with node: {}", getLeaderOffer().getNodePath()); + + dispatchEvent(EventType.ELECTED_COMPLETE); + } + + private void becomeFailed(Exception e) { + LOG.error("Failed in state {}", state, e); + + state = State.FAILED; + dispatchEvent(EventType.FAILED); + } + + /** + * Fetch the (user supplied) hostname of the current leader. Note that by the + * time this method returns, state could have changed so do not depend on this + * to be strongly consistent. This method has to read all leader offers from + * ZooKeeper to deterime who the leader is (i.e. there is no caching) so + * consider the performance implications of frequent invocation. If there are + * no leader offers this method returns null. + * + * @return hostname of the current leader + * @throws KeeperException + * @throws InterruptedException + */ + public String getLeaderHostName() throws KeeperException, InterruptedException { + + List leaderOffers = toLeaderOffers(zooKeeper.getChildren(rootNodeName, false)); + + if (leaderOffers.size() > 0) { + return leaderOffers.get(0).getHostName(); + } + + return null; + } + + private List toLeaderOffers(List strings) + throws KeeperException, InterruptedException { + + List leaderOffers = new ArrayList<>(strings.size()); + + /* + * Turn each child of rootNodeName into a leader offer. This is a tuple of + * the sequence number and the node name. + */ + for (String offer : strings) { + String hostName = new String(zooKeeper.getData(rootNodeName + "/" + offer, false, null)); + + leaderOffers.add(new LeaderOffer( + Integer.valueOf(offer.substring("n_".length())), + rootNodeName + "/" + offer, hostName)); + } + + /* + * We sort leader offers by sequence number (which may not be zero-based or + * contiguous) and keep their paths handy for setting watches. + */ + Collections.sort(leaderOffers, new LeaderOffer.IdComparator()); + + return leaderOffers; + } + + @Override + public void process(WatchedEvent event) { + if (event.getType().equals(Watcher.Event.EventType.NodeDeleted)) { + if (!event.getPath().equals(getLeaderOffer().getNodePath()) + && state != State.STOP) { + LOG.debug( + "Node {} deleted. Need to run through the election process.", + event.getPath()); + try { + determineElectionStatus(); + } catch (KeeperException | InterruptedException e) { + becomeFailed(e); + } + } + } + } + + private void dispatchEvent(EventType eventType) { + LOG.debug("Dispatching event: {}", eventType); + + synchronized (listeners) { + if (listeners.size() > 0) { + for (LeaderElectionAware observer : listeners) { + observer.onElectionEvent(eventType); + } + } + } + } + + /** + * Adds {@code listener} to the list of listeners who will receive events. + * + * @param listener + */ + public void addListener(LeaderElectionAware listener) { + listeners.add(listener); + } + + /** + * Remove {@code listener} from the list of listeners who receive events. + * + * @param listener + */ + public void removeListener(LeaderElectionAware listener) { + listeners.remove(listener); + } + + @Override + public String toString() { + return "{" + + " state:" + state + + " leaderOffer:" + getLeaderOffer() + + " zooKeeper:" + zooKeeper + + " hostName:" + getHostName() + + " listeners:" + listeners + + " }"; + } + + /** + *

      + * Gets the ZooKeeper root node to use for this service. + *

      + *

      + * For instance, a root node of {@code /mycompany/myservice} would be the + * parent of all leader offers for this service. Obviously all processes that + * wish to contend for leader status need to use the same root node. Note: We + * assume this node already exists. + *

      + * + * @return a znode path + */ + public String getRootNodeName() { + return rootNodeName; + } + + /** + *

      + * Sets the ZooKeeper root node to use for this service. + *

      + *

      + * For instance, a root node of {@code /mycompany/myservice} would be the + * parent of all leader offers for this service. Obviously all processes that + * wish to contend for leader status need to use the same root node. Note: We + * assume this node already exists. + *

      + */ + public void setRootNodeName(String rootNodeName) { + this.rootNodeName = rootNodeName; + } + + /** + * The {@link ZooKeeper} instance to use for all operations. Provided this + * overrides any connectString or sessionTimeout set. + */ + public ZooKeeper getZooKeeper() { + return zooKeeper; + } + + public void setZooKeeper(ZooKeeper zooKeeper) { + this.zooKeeper = zooKeeper; + } + + /** + * The hostname of this process. Mostly used as a convenience for logging and + * to respond to {@link #getLeaderHostName()} requests. + */ + public synchronized String getHostName() { + return hostName; + } + + public synchronized void setHostName(String hostName) { + this.hostName = hostName; + } + + /** + * The type of event. + */ + public enum EventType { + START, + OFFER_START, + OFFER_COMPLETE, + DETERMINE_START, + DETERMINE_COMPLETE, + ELECTED_START, + ELECTED_COMPLETE, + READY_START, + READY_COMPLETE, + FAILED, + STOP_START, + STOP_COMPLETE, + } + + /** + * The internal state of the election support service. + */ + public enum State { + START, + OFFER, + DETERMINE, + ELECTED, + READY, + FAILED, + STOP + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderOffer.java b/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderOffer.java new file mode 100644 index 0000000..d276f4c --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/src/main/java/org/apache/zookeeper/recipes/leader/LeaderOffer.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.leader; + +import java.io.Serializable; +import java.util.Comparator; + +/** + * A leader offer is a numeric id / path pair. The id is the sequential node id + * assigned by ZooKeeper where as the path is the absolute path to the ZNode. + */ +public class LeaderOffer { + + private Integer id; + private String nodePath; + private String hostName; + + public LeaderOffer() { + // Default constructor + } + + public LeaderOffer(Integer id, String nodePath, String hostName) { + this.id = id; + this.nodePath = nodePath; + this.hostName = hostName; + } + + @Override + public String toString() { + return "{" + + " id:" + id + + " nodePath:" + nodePath + + " hostName:" + hostName + + " }"; + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getNodePath() { + return nodePath; + } + + public void setNodePath(String nodePath) { + this.nodePath = nodePath; + } + + public String getHostName() { + return hostName; + } + + public void setHostName(String hostName) { + this.hostName = hostName; + } + + /** + * Compare two instances of {@link LeaderOffer} using only the {code}id{code} + * member. + */ + public static class IdComparator + implements Comparator, Serializable { + + @Override + public int compare(LeaderOffer o1, LeaderOffer o2) { + return o1.getId().compareTo(o2.getId()); + } + + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-election/src/test/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupportTest.java b/zookeeper-recipes/zookeeper-recipes-election/src/test/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupportTest.java new file mode 100644 index 0000000..2ea721e --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-election/src/test/java/org/apache/zookeeper/recipes/leader/LeaderElectionSupportTest.java @@ -0,0 +1,268 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.leader; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.recipes.leader.LeaderElectionSupport.EventType; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test for {@link LeaderElectionSupport}. + */ +public class LeaderElectionSupportTest extends ClientBase { + + private static final Logger LOGGER = LoggerFactory.getLogger(LeaderElectionSupportTest.class); + private static final String TEST_ROOT_NODE = "/" + System.currentTimeMillis() + "_"; + + private ZooKeeper zooKeeper; + + @Before + public void setUp() throws Exception { + super.setUp(); + + zooKeeper = createClient(); + + zooKeeper.create( + TEST_ROOT_NODE + Thread.currentThread().getId(), + new byte[0], + ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } + + @After + public void tearDown() throws Exception { + if (zooKeeper != null) { + zooKeeper.delete(TEST_ROOT_NODE + Thread.currentThread().getId(), -1); + } + + super.tearDown(); + } + + @Test + public void testNode() throws Exception { + LeaderElectionSupport electionSupport = createLeaderElectionSupport(); + + electionSupport.start(); + Thread.sleep(3000); + electionSupport.stop(); + } + + @Test + public void testNodes3() throws Exception { + int testIterations = 3; + final CountDownLatch latch = new CountDownLatch(testIterations); + final AtomicInteger failureCounter = new AtomicInteger(); + + for (int i = 0; i < testIterations; i++) { + runElectionSupportThread(latch, failureCounter); + } + + Assert.assertEquals(0, failureCounter.get()); + + if (!latch.await(10, TimeUnit.SECONDS)) { + LOGGER.info("Waited for all threads to start, but timed out. We had {} failures.", failureCounter); + } + } + + @Test + public void testNodes9() throws Exception { + int testIterations = 9; + final CountDownLatch latch = new CountDownLatch(testIterations); + final AtomicInteger failureCounter = new AtomicInteger(); + + for (int i = 0; i < testIterations; i++) { + runElectionSupportThread(latch, failureCounter); + } + + Assert.assertEquals(0, failureCounter.get()); + + if (!latch.await(10, TimeUnit.SECONDS)) { + LOGGER.info("Waited for all threads to start, but timed out. We had {} failures.", failureCounter); + } + } + + @Test + public void testNodes20() throws Exception { + int testIterations = 20; + final CountDownLatch latch = new CountDownLatch(testIterations); + final AtomicInteger failureCounter = new AtomicInteger(); + + for (int i = 0; i < testIterations; i++) { + runElectionSupportThread(latch, failureCounter); + } + + Assert.assertEquals(0, failureCounter.get()); + + if (!latch.await(10, TimeUnit.SECONDS)) { + LOGGER.info("Waited for all threads to start, but timed out. We had {} failures.", failureCounter); + } + } + + @Test + public void testNodes100() throws Exception { + int testIterations = 100; + final CountDownLatch latch = new CountDownLatch(testIterations); + final AtomicInteger failureCounter = new AtomicInteger(); + + for (int i = 0; i < testIterations; i++) { + runElectionSupportThread(latch, failureCounter); + } + + Assert.assertEquals(0, failureCounter.get()); + + if (!latch.await(20, TimeUnit.SECONDS)) { + LOGGER.info("Waited for all threads to start, but timed out. We had {} failures.", failureCounter); + } + } + + @Test + public void testOfferShuffle() throws InterruptedException { + int testIterations = 10; + final CountDownLatch latch = new CountDownLatch(testIterations); + final AtomicInteger failureCounter = new AtomicInteger(); + List threads = new ArrayList<>(testIterations); + + for (int i = 1; i <= testIterations; i++) { + threads.add(runElectionSupportThread(latch, failureCounter, Math.min(i * 1200, 10000))); + } + + if (!latch.await(60, TimeUnit.SECONDS)) { + LOGGER.info("Waited for all threads to start, but timed out. We had {} failures.", failureCounter); + } + } + + @Test + public void testGetLeaderHostName() throws Exception { + LeaderElectionSupport electionSupport = createLeaderElectionSupport(); + + electionSupport.start(); + + // Sketchy: We assume there will be a leader (probably us) in 3 seconds. + Thread.sleep(3000); + + String leaderHostName = electionSupport.getLeaderHostName(); + + Assert.assertNotNull(leaderHostName); + Assert.assertEquals("foohost", leaderHostName); + + electionSupport.stop(); + } + + @Test + public void testReadyOffer() throws Exception { + final ArrayList events = new ArrayList<>(); + final CountDownLatch electedComplete = new CountDownLatch(1); + + final LeaderElectionSupport electionSupport1 = createLeaderElectionSupport(); + electionSupport1.start(); + LeaderElectionSupport electionSupport2 = createLeaderElectionSupport(); + LeaderElectionAware listener = new LeaderElectionAware() { + boolean stoppedElectedNode = false; + @Override + public void onElectionEvent(EventType eventType) { + events.add(eventType); + if (!stoppedElectedNode + && eventType == EventType.DETERMINE_COMPLETE) { + stoppedElectedNode = true; + try { + // stopping the ELECTED node, so re-election will happen. + electionSupport1.stop(); + } catch (Exception e) { + LOGGER.error("Unexpected exception", e); + } + } + if (eventType == EventType.ELECTED_COMPLETE) { + electedComplete.countDown(); + } + } + }; + electionSupport2.addListener(listener); + electionSupport2.start(); + // waiting for re-election. + electedComplete.await(CONNECTION_TIMEOUT / 3, TimeUnit.MILLISECONDS); + + final ArrayList expectedevents = new ArrayList<>(); + expectedevents.add(EventType.START); + expectedevents.add(EventType.OFFER_START); + expectedevents.add(EventType.OFFER_COMPLETE); + expectedevents.add(EventType.DETERMINE_START); + expectedevents.add(EventType.DETERMINE_COMPLETE); + expectedevents.add(EventType.DETERMINE_START); + expectedevents.add(EventType.DETERMINE_COMPLETE); + expectedevents.add(EventType.ELECTED_START); + expectedevents.add(EventType.ELECTED_COMPLETE); + + Assert.assertEquals("Events has failed to executed in the order", expectedevents, events); + + electionSupport2.stop(); + } + + private LeaderElectionSupport createLeaderElectionSupport() { + LeaderElectionSupport electionSupport = new LeaderElectionSupport(); + + electionSupport.setZooKeeper(zooKeeper); + electionSupport.setRootNodeName(TEST_ROOT_NODE + Thread.currentThread().getId()); + electionSupport.setHostName("foohost"); + + return electionSupport; + } + + private Thread runElectionSupportThread( + final CountDownLatch latch, + final AtomicInteger failureCounter) { + return runElectionSupportThread(latch, failureCounter, 3000); + } + + private Thread runElectionSupportThread( + final CountDownLatch latch, + final AtomicInteger failureCounter, + final long sleepDuration) { + final LeaderElectionSupport electionSupport = createLeaderElectionSupport(); + + Thread t = new Thread(() -> { + try { + electionSupport.start(); + Thread.sleep(sleepDuration); + electionSupport.stop(); + + latch.countDown(); + } catch (Exception e) { + LOGGER.warn("Failed to run leader election.", e); + failureCounter.incrementAndGet(); + } + }); + + t.start(); + + return t; + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/README.txt b/zookeeper-recipes/zookeeper-recipes-lock/README.txt new file mode 100644 index 0000000..4ce629e --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/README.txt @@ -0,0 +1,28 @@ + + +1) This lock interface recipe implements the lock recipe +mentioned in ../../docs/recipes.[html,pdf]. + +2) To compile the lock java recipe you can just run ant jar from +this directory. For compiling the c libarary go to zookeeper-client/zookeeper-client-c and read +the INSTALLATION instructions. +Please report any bugs on the jira + +http://issues.apache.org/jira/browse/ZOOKEEPER + + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/build.xml b/zookeeper-recipes/zookeeper-recipes-lock/build.xml new file mode 100644 index 0000000..896ebbd --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/build.xml @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/pom.xml b/zookeeper-recipes/zookeeper-recipes-lock/pom.xml new file mode 100644 index 0000000..0293210 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/pom.xml @@ -0,0 +1,55 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-recipes + 3.6.3 + + + zookeeper-recipes-lock + jar + Apache ZooKeeper - Recipes - Lock + + This lock interface recipe implements the lock recipe + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${surefire-forkcount} + false + -Xmx512m + ${project.basedir} + true + + ${project.build.directory}/surefire + + + + + + + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/INSTALL b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/INSTALL new file mode 100644 index 0000000..5458714 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/INSTALL @@ -0,0 +1,234 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, +2006 Free Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf bug. Until the bug is fixed you can use this workaround: + + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/LICENSE b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/LICENSE @@ -0,0 +1,202 @@ + + 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/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/Makefile.am b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/Makefile.am new file mode 100644 index 0000000..9b36b43 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/Makefile.am @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +include $(top_srcdir)/aminclude.am + +AM_CFLAGS = -Wall -fPIC -I${ZOOKEEPER_PATH}/include -I${ZOOKEEPER_PATH}/generated \ + -I$(top_srcdir)/include -I/usr/include +AM_CPPFLAGS = -Wall -I${ZOOKEEPER_PATH}/include -I${ZOOKEEPER_PATH}/generated\ + -I${top_srcdir}/include -I/usr/include +EXTRA_DIST = LICENSE +lib_LTLIBRARIES = libzoolock.la +libzoolock_la_SOURCES = src/zoo_lock.c include/zoo_lock.h +libzoolock_la_CPPFLAGS = -DDLOPEN_MODULE +libzoolock_la_LDFLAGS = -version-info 0:1:0 + +#run the tests now + +TEST_SOURCES = tests/TestDriver.cc tests/TestClient.cc tests/Util.cc + + +check_PROGRAMS = zklocktest +nodist_zklocktest_SOURCES = ${TEST_SOURCES} +zklocktest_LDADD = ${ZOOKEEPER_LD} libzoolock.la -lpthread ${CPPUNIT_LIBS} +zklocktest_CXXFLAGS = -DUSE_STATIC_LIB ${CPPUNIT_CFLAGS} + +run-check: check + ./zklocktest ${TEST_OPTIONS} + +clean-local: clean-check + ${RM} ${DX_CLEANFILES} + +clean-check: + ${RM} ${nodist_zklocktest_OBJECTS} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/README.txt b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/README.txt new file mode 100644 index 0000000..9fd20da --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/README.txt @@ -0,0 +1,28 @@ + Zookeeper C lock client library + + +INSTALLATION + +If you're building the client from a source checkout you need to +follow the steps outlined below. If you're building from a release +tar downloaded from Apache please skip to step 2. + +1) make sure that you compile the main zookeeper c client library. + +2) change directory to zookeeper-recipes/zookeeper-recipes-lock/src/main/c + and do a "autoreconf -if" to bootstrap + autoconf, automake and libtool. Please make sure you have autoconf + version 2.59 or greater installed. +3) do a "./configure [OPTIONS]" to generate the makefile. See INSTALL + for general information about running configure. + +4) do a "make" or "make install" to build the libraries and install them. + Alternatively, you can also build and run a unit test suite (and + you probably should). Please make sure you have cppunit-1.10.x or + higher installed before you execute step 4. Once ./configure has + finished, do a "make run-check". It will build the libraries, build + the tests and run them. +5) to generate doxygen documentation do a "make doxygen-doc". All + documentations will be placed to a new subfolder named docs. By + default only HTML documentation is generated. For information on + other document formats please use "./configure --help" diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/acinclude.m4 b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/acinclude.m4 new file mode 100644 index 0000000..a4d405a --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/acinclude.m4 @@ -0,0 +1,312 @@ +# This file is part of Autoconf. -*- Autoconf -*- + +# Copyright (C) 2004 Oren Ben-Kiki +# This file is distributed under the same terms as the Autoconf macro files. + +# Generate automatic documentation using Doxygen. Works in concert with the +# aminclude.m4 file and a compatible doxygen configuration file. Defines the +# following public macros: +# +# DX_???_FEATURE(ON|OFF) - control the default setting of a Doxygen feature. +# Supported features are 'DOXYGEN' itself, 'DOT' for generating graphics, +# 'HTML' for plain HTML, 'CHM' for compressed HTML help (for MS users), 'CHI' +# for generating a seperate .chi file by the .chm file, and 'MAN', 'RTF', +# 'XML', 'PDF' and 'PS' for the appropriate output formats. The environment +# variable DOXYGEN_PAPER_SIZE may be specified to override the default 'a4wide' +# paper size. +# +# By default, HTML, PDF and PS documentation is generated as this seems to be +# the most popular and portable combination. MAN pages created by Doxygen are +# usually problematic, though by picking an appropriate subset and doing some +# massaging they might be better than nothing. CHM and RTF are specific for MS +# (note that you can't generate both HTML and CHM at the same time). The XML is +# rather useless unless you apply specialized post-processing to it. +# +# The macro mainly controls the default state of the feature. The use can +# override the default by specifying --enable or --disable. The macros ensure +# that contradictory flags are not given (e.g., --enable-doxygen-html and +# --enable-doxygen-chm, --enable-doxygen-anything with --disable-doxygen, etc.) +# Finally, each feature will be automatically disabled (with a warning) if the +# required programs are missing. +# +# Once all the feature defaults have been specified, call DX_INIT_DOXYGEN with +# the following parameters: a one-word name for the project for use as a +# filename base etc., an optional configuration file name (the default is +# 'Doxyfile', the same as Doxygen's default), and an optional output directory +# name (the default is 'doxygen-doc'). + +## ----------## +## Defaults. ## +## ----------## + +DX_ENV="" +AC_DEFUN([DX_FEATURE_doc], ON) +AC_DEFUN([DX_FEATURE_dot], ON) +AC_DEFUN([DX_FEATURE_man], OFF) +AC_DEFUN([DX_FEATURE_html], ON) +AC_DEFUN([DX_FEATURE_chm], OFF) +AC_DEFUN([DX_FEATURE_chi], OFF) +AC_DEFUN([DX_FEATURE_rtf], OFF) +AC_DEFUN([DX_FEATURE_xml], OFF) +AC_DEFUN([DX_FEATURE_pdf], ON) +AC_DEFUN([DX_FEATURE_ps], ON) + +## --------------- ## +## Private macros. ## +## --------------- ## + +# DX_ENV_APPEND(VARIABLE, VALUE) +# ------------------------------ +# Append VARIABLE="VALUE" to DX_ENV for invoking doxygen. +AC_DEFUN([DX_ENV_APPEND], [AC_SUBST([DX_ENV], ["$DX_ENV $1='$2'"])]) + +# DX_DIRNAME_EXPR +# --------------- +# Expand into a shell expression prints the directory part of a path. +AC_DEFUN([DX_DIRNAME_EXPR], + [[expr ".$1" : '\(\.\)[^/]*$' \| "x$1" : 'x\(.*\)/[^/]*$']]) + +# DX_IF_FEATURE(FEATURE, IF-ON, IF-OFF) +# ------------------------------------- +# Expands according to the M4 (static) status of the feature. +AC_DEFUN([DX_IF_FEATURE], [ifelse(DX_FEATURE_$1, ON, [$2], [$3])]) + +# DX_REQUIRE_PROG(VARIABLE, PROGRAM) +# ---------------------------------- +# Require the specified program to be found for the DX_CURRENT_FEATURE to work. +AC_DEFUN([DX_REQUIRE_PROG], [ +AC_PATH_TOOL([$1], [$2]) +if test "$DX_FLAG_$[DX_CURRENT_FEATURE$$1]" = 1; then + AC_MSG_WARN([$2 not found - will not DX_CURRENT_DESCRIPTION]) + AC_SUBST([DX_FLAG_]DX_CURRENT_FEATURE, 0) +fi +]) + +# DX_TEST_FEATURE(FEATURE) +# ------------------------ +# Expand to a shell expression testing whether the feature is active. +AC_DEFUN([DX_TEST_FEATURE], [test "$DX_FLAG_$1" = 1]) + +# DX_CHECK_DEPEND(REQUIRED_FEATURE, REQUIRED_STATE) +# ------------------------------------------------- +# Verify that a required features has the right state before trying to turn on +# the DX_CURRENT_FEATURE. +AC_DEFUN([DX_CHECK_DEPEND], [ +test "$DX_FLAG_$1" = "$2" \ +|| AC_MSG_ERROR([doxygen-DX_CURRENT_FEATURE ifelse([$2], 1, + requires, contradicts) doxygen-DX_CURRENT_FEATURE]) +]) + +# DX_CLEAR_DEPEND(FEATURE, REQUIRED_FEATURE, REQUIRED_STATE) +# ---------------------------------------------------------- +# Turn off the DX_CURRENT_FEATURE if the required feature is off. +AC_DEFUN([DX_CLEAR_DEPEND], [ +test "$DX_FLAG_$1" = "$2" || AC_SUBST([DX_FLAG_]DX_CURRENT_FEATURE, 0) +]) + +# DX_FEATURE_ARG(FEATURE, DESCRIPTION, +# CHECK_DEPEND, CLEAR_DEPEND, +# REQUIRE, DO-IF-ON, DO-IF-OFF) +# -------------------------------------------- +# Parse the command-line option controlling a feature. CHECK_DEPEND is called +# if the user explicitly turns the feature on (and invokes DX_CHECK_DEPEND), +# otherwise CLEAR_DEPEND is called to turn off the default state if a required +# feature is disabled (using DX_CLEAR_DEPEND). REQUIRE performs additional +# requirement tests (DX_REQUIRE_PROG). Finally, an automake flag is set and +# DO-IF-ON or DO-IF-OFF are called according to the final state of the feature. +AC_DEFUN([DX_ARG_ABLE], [ + AC_DEFUN([DX_CURRENT_FEATURE], [$1]) + AC_DEFUN([DX_CURRENT_DESCRIPTION], [$2]) + AC_ARG_ENABLE(doxygen-$1, + [AS_HELP_STRING(DX_IF_FEATURE([$1], [--disable-doxygen-$1], + [--enable-doxygen-$1]), + DX_IF_FEATURE([$1], [don't $2], [$2]))], + [ +case "$enableval" in +#( +y|Y|yes|Yes|YES) + AC_SUBST([DX_FLAG_$1], 1) + $3 +;; #( +n|N|no|No|NO) + AC_SUBST([DX_FLAG_$1], 0) +;; #( +*) + AC_MSG_ERROR([invalid value '$enableval' given to doxygen-$1]) +;; +esac +], [ +AC_SUBST([DX_FLAG_$1], [DX_IF_FEATURE([$1], 1, 0)]) +$4 +]) +if DX_TEST_FEATURE([$1]); then + $5 + : +fi +if DX_TEST_FEATURE([$1]); then + AM_CONDITIONAL(DX_COND_$1, :) + $6 + : +else + AM_CONDITIONAL(DX_COND_$1, false) + $7 + : +fi +]) + +## -------------- ## +## Public macros. ## +## -------------- ## + +# DX_XXX_FEATURE(DEFAULT_STATE) +# ----------------------------- +AC_DEFUN([DX_DOXYGEN_FEATURE], [AC_DEFUN([DX_FEATURE_doc], [$1])]) +AC_DEFUN([DX_MAN_FEATURE], [AC_DEFUN([DX_FEATURE_man], [$1])]) +AC_DEFUN([DX_HTML_FEATURE], [AC_DEFUN([DX_FEATURE_html], [$1])]) +AC_DEFUN([DX_CHM_FEATURE], [AC_DEFUN([DX_FEATURE_chm], [$1])]) +AC_DEFUN([DX_CHI_FEATURE], [AC_DEFUN([DX_FEATURE_chi], [$1])]) +AC_DEFUN([DX_RTF_FEATURE], [AC_DEFUN([DX_FEATURE_rtf], [$1])]) +AC_DEFUN([DX_XML_FEATURE], [AC_DEFUN([DX_FEATURE_xml], [$1])]) +AC_DEFUN([DX_XML_FEATURE], [AC_DEFUN([DX_FEATURE_xml], [$1])]) +AC_DEFUN([DX_PDF_FEATURE], [AC_DEFUN([DX_FEATURE_pdf], [$1])]) +AC_DEFUN([DX_PS_FEATURE], [AC_DEFUN([DX_FEATURE_ps], [$1])]) + +# DX_INIT_DOXYGEN(PROJECT, [CONFIG-FILE], [OUTPUT-DOC-DIR]) +# --------------------------------------------------------- +# PROJECT also serves as the base name for the documentation files. +# The default CONFIG-FILE is "Doxyfile" and OUTPUT-DOC-DIR is "doxygen-doc". +AC_DEFUN([DX_INIT_DOXYGEN], [ + +# Files: +AC_SUBST([DX_PROJECT], [$1]) +AC_SUBST([DX_CONFIG], [ifelse([$2], [], Doxyfile, [$2])]) +AC_SUBST([DX_DOCDIR], [ifelse([$3], [], doxygen-doc, [$3])]) + +# Environment variables used inside doxygen.cfg: +DX_ENV_APPEND(SRCDIR, $srcdir) +DX_ENV_APPEND(PROJECT, $DX_PROJECT) +DX_ENV_APPEND(DOCDIR, $DX_DOCDIR) +DX_ENV_APPEND(VERSION, $PACKAGE_VERSION) + +# Doxygen itself: +DX_ARG_ABLE(doc, [generate any doxygen documentation], + [], + [], + [DX_REQUIRE_PROG([DX_DOXYGEN], doxygen) + DX_REQUIRE_PROG([DX_PERL], perl)], + [DX_ENV_APPEND(PERL_PATH, $DX_PERL)]) + +# Dot for graphics: +DX_ARG_ABLE(dot, [generate graphics for doxygen documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_DOT], dot)], + [DX_ENV_APPEND(HAVE_DOT, YES) + DX_ENV_APPEND(DOT_PATH, [`DX_DIRNAME_EXPR($DX_DOT)`])], + [DX_ENV_APPEND(HAVE_DOT, NO)]) + +# Man pages generation: +DX_ARG_ABLE(man, [generate doxygen manual pages], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_MAN, YES)], + [DX_ENV_APPEND(GENERATE_MAN, NO)]) + +# RTF file generation: +DX_ARG_ABLE(rtf, [generate doxygen RTF documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_RTF, YES)], + [DX_ENV_APPEND(GENERATE_RTF, NO)]) + +# XML file generation: +DX_ARG_ABLE(xml, [generate doxygen XML documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_XML, YES)], + [DX_ENV_APPEND(GENERATE_XML, NO)]) + +# (Compressed) HTML help generation: +DX_ARG_ABLE(chm, [generate doxygen compressed HTML help documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_HHC], hhc)], + [DX_ENV_APPEND(HHC_PATH, $DX_HHC) + DX_ENV_APPEND(GENERATE_HTML, YES) + DX_ENV_APPEND(GENERATE_HTMLHELP, YES)], + [DX_ENV_APPEND(GENERATE_HTMLHELP, NO)]) + +# Seperate CHI file generation. +DX_ARG_ABLE(chi, [generate doxygen seperate compressed HTML help index file], + [DX_CHECK_DEPEND(chm, 1)], + [DX_CLEAR_DEPEND(chm, 1)], + [], + [DX_ENV_APPEND(GENERATE_CHI, YES)], + [DX_ENV_APPEND(GENERATE_CHI, NO)]) + +# Plain HTML pages generation: +DX_ARG_ABLE(html, [generate doxygen plain HTML documentation], + [DX_CHECK_DEPEND(doc, 1) DX_CHECK_DEPEND(chm, 0)], + [DX_CLEAR_DEPEND(doc, 1) DX_CLEAR_DEPEND(chm, 0)], + [], + [DX_ENV_APPEND(GENERATE_HTML, YES)], + [DX_TEST_FEATURE(chm) || DX_ENV_APPEND(GENERATE_HTML, NO)]) + +# PostScript file generation: +DX_ARG_ABLE(ps, [generate doxygen PostScript documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_LATEX], latex) + DX_REQUIRE_PROG([DX_MAKEINDEX], makeindex) + DX_REQUIRE_PROG([DX_DVIPS], dvips) + DX_REQUIRE_PROG([DX_EGREP], egrep)]) + +# PDF file generation: +DX_ARG_ABLE(pdf, [generate doxygen PDF documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_PDFLATEX], pdflatex) + DX_REQUIRE_PROG([DX_MAKEINDEX], makeindex) + DX_REQUIRE_PROG([DX_EGREP], egrep)]) + +# LaTeX generation for PS and/or PDF: +if DX_TEST_FEATURE(ps) || DX_TEST_FEATURE(pdf); then + AM_CONDITIONAL(DX_COND_latex, :) + DX_ENV_APPEND(GENERATE_LATEX, YES) +else + AM_CONDITIONAL(DX_COND_latex, false) + DX_ENV_APPEND(GENERATE_LATEX, NO) +fi + +# Paper size for PS and/or PDF: +AC_ARG_VAR(DOXYGEN_PAPER_SIZE, + [a4wide (default), a4, letter, legal or executive]) +case "$DOXYGEN_PAPER_SIZE" in +#( +"") + AC_SUBST(DOXYGEN_PAPER_SIZE, "") +;; #( +a4wide|a4|letter|legal|executive) + DX_ENV_APPEND(PAPER_SIZE, $DOXYGEN_PAPER_SIZE) +;; #( +*) + AC_MSG_ERROR([unknown DOXYGEN_PAPER_SIZE='$DOXYGEN_PAPER_SIZE']) +;; +esac + +#For debugging: +#echo DX_FLAG_doc=$DX_FLAG_doc +#echo DX_FLAG_dot=$DX_FLAG_dot +#echo DX_FLAG_man=$DX_FLAG_man +#echo DX_FLAG_html=$DX_FLAG_html +#echo DX_FLAG_chm=$DX_FLAG_chm +#echo DX_FLAG_chi=$DX_FLAG_chi +#echo DX_FLAG_rtf=$DX_FLAG_rtf +#echo DX_FLAG_xml=$DX_FLAG_xml +#echo DX_FLAG_pdf=$DX_FLAG_pdf +#echo DX_FLAG_ps=$DX_FLAG_ps +#echo DX_ENV=$DX_ENV +]) diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/aminclude.am b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/aminclude.am new file mode 100644 index 0000000..420049e --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/aminclude.am @@ -0,0 +1,186 @@ +# Copyright (C) 2004 Oren Ben-Kiki +# This file is distributed under the same terms as the Automake macro files. + +# Generate automatic documentation using Doxygen. Goals and variables values +# are controlled by the various DX_COND_??? conditionals set by autoconf. +# +# The provided goals are: +# doxygen-doc: Generate all doxygen documentation. +# doxygen-run: Run doxygen, which will generate some of the documentation +# (HTML, CHM, CHI, MAN, RTF, XML) but will not do the post +# processing required for the rest of it (PS, PDF, and some MAN). +# doxygen-man: Rename some doxygen generated man pages. +# doxygen-ps: Generate doxygen PostScript documentation. +# doxygen-pdf: Generate doxygen PDF documentation. +# +# Note that by default these are not integrated into the automake goals. If +# doxygen is used to generate man pages, you can achieve this integration by +# setting man3_MANS to the list of man pages generated and then adding the +# dependency: +# +# $(man3_MANS): doxygen-doc +# +# This will cause make to run doxygen and generate all the documentation. +# +# The following variable is intended for use in Makefile.am: +# +# DX_CLEANFILES = everything to clean. +# +# This is usually added to MOSTLYCLEANFILES. + +## --------------------------------- ## +## Format-independent Doxygen rules. ## +## --------------------------------- ## + +if DX_COND_doc + +## ------------------------------- ## +## Rules specific for HTML output. ## +## ------------------------------- ## + +if DX_COND_html + +DX_CLEAN_HTML = @DX_DOCDIR@/html + +endif DX_COND_html + +## ------------------------------ ## +## Rules specific for CHM output. ## +## ------------------------------ ## + +if DX_COND_chm + +DX_CLEAN_CHM = @DX_DOCDIR@/chm + +if DX_COND_chi + +DX_CLEAN_CHI = @DX_DOCDIR@/@PACKAGE@.chi + +endif DX_COND_chi + +endif DX_COND_chm + +## ------------------------------ ## +## Rules specific for MAN output. ## +## ------------------------------ ## + +if DX_COND_man + +DX_CLEAN_MAN = @DX_DOCDIR@/man + +endif DX_COND_man + +## ------------------------------ ## +## Rules specific for RTF output. ## +## ------------------------------ ## + +if DX_COND_rtf + +DX_CLEAN_RTF = @DX_DOCDIR@/rtf + +endif DX_COND_rtf + +## ------------------------------ ## +## Rules specific for XML output. ## +## ------------------------------ ## + +if DX_COND_xml + +DX_CLEAN_XML = @DX_DOCDIR@/xml + +endif DX_COND_xml + +## ----------------------------- ## +## Rules specific for PS output. ## +## ----------------------------- ## + +if DX_COND_ps + +DX_CLEAN_PS = @DX_DOCDIR@/@PACKAGE@.ps + +DX_PS_GOAL = doxygen-ps + +doxygen-ps: @DX_DOCDIR@/@PACKAGE@.ps + +@DX_DOCDIR@/@PACKAGE@.ps: @DX_DOCDIR@/@PACKAGE@.tag + cd @DX_DOCDIR@/latex; \ + rm -f *.aux *.toc *.idx *.ind *.ilg *.log *.out; \ + $(DX_LATEX) refman.tex; \ + $(MAKEINDEX_PATH) refman.idx; \ + $(DX_LATEX) refman.tex; \ + countdown=5; \ + while $(DX_EGREP) 'Rerun (LaTeX|to get cross-references right)' \ + refman.log > /dev/null 2>&1 \ + && test $$countdown -gt 0; do \ + $(DX_LATEX) refman.tex; \ + countdown=`expr $$countdown - 1`; \ + done; \ + $(DX_DVIPS) -o ../@PACKAGE@.ps refman.dvi + +endif DX_COND_ps + +## ------------------------------ ## +## Rules specific for PDF output. ## +## ------------------------------ ## + +if DX_COND_pdf + +DX_CLEAN_PDF = @DX_DOCDIR@/@PACKAGE@.pdf + +DX_PDF_GOAL = doxygen-pdf + +doxygen-pdf: @DX_DOCDIR@/@PACKAGE@.pdf + +@DX_DOCDIR@/@PACKAGE@.pdf: @DX_DOCDIR@/@PACKAGE@.tag + cd @DX_DOCDIR@/latex; \ + rm -f *.aux *.toc *.idx *.ind *.ilg *.log *.out; \ + $(DX_PDFLATEX) refman.tex; \ + $(DX_MAKEINDEX) refman.idx; \ + $(DX_PDFLATEX) refman.tex; \ + countdown=5; \ + while $(DX_EGREP) 'Rerun (LaTeX|to get cross-references right)' \ + refman.log > /dev/null 2>&1 \ + && test $$countdown -gt 0; do \ + $(DX_PDFLATEX) refman.tex; \ + countdown=`expr $$countdown - 1`; \ + done; \ + mv refman.pdf ../@PACKAGE@.pdf + +endif DX_COND_pdf + +## ------------------------------------------------- ## +## Rules specific for LaTeX (shared for PS and PDF). ## +## ------------------------------------------------- ## + +if DX_COND_latex + +DX_CLEAN_LATEX = @DX_DOCDIR@/latex + +endif DX_COND_latex + +.PHONY: doxygen-run doxygen-doc $(DX_PS_GOAL) $(DX_PDF_GOAL) + +.INTERMEDIATE: doxygen-run $(DX_PS_GOAL) $(DX_PDF_GOAL) + +doxygen-run: @DX_DOCDIR@/@PACKAGE@.tag + +doxygen-doc: doxygen-run $(DX_PS_GOAL) $(DX_PDF_GOAL) + +@DX_DOCDIR@/@PACKAGE@.tag: $(DX_CONFIG) $(pkginclude_HEADERS) + rm -rf @DX_DOCDIR@ + $(DX_ENV) $(DX_DOXYGEN) $(srcdir)/$(DX_CONFIG) + +DX_CLEANFILES = \ + @DX_DOCDIR@/@PACKAGE@.tag \ + -r \ + $(DX_CLEAN_HTML) \ + $(DX_CLEAN_CHM) \ + $(DX_CLEAN_CHI) \ + $(DX_CLEAN_MAN) \ + $(DX_CLEAN_RTF) \ + $(DX_CLEAN_XML) \ + $(DX_CLEAN_PS) \ + $(DX_CLEAN_PDF) \ + $(DX_CLEAN_LATEX) + +endif DX_COND_doc diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/c-doc.Doxyfile b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/c-doc.Doxyfile new file mode 100644 index 0000000..c56e86d --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/c-doc.Doxyfile @@ -0,0 +1,1252 @@ +# Doxyfile 1.4.7 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = $(PROJECT)-$(VERSION) + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = $(DOCDIR) + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Brazilian, Catalan, Chinese, Chinese-Traditional, Croatian, Czech, Danish, +# Dutch, Finnish, French, German, Greek, Hungarian, Italian, Japanese, +# Japanese-en (Japanese with English messages), Korean, Korean-en, Norwegian, +# Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovene, Spanish, +# Swedish, and Ukrainian. + +OUTPUT_LANGUAGE = English + +# This tag can be used to specify the encoding used in the generated output. +# The encoding is not always determined by the language that is chosen, +# but also whether or not the output is meant for Windows or non-Windows users. +# In case there is a difference, setting the USE_WINDOWS_ENCODING tag to YES +# forces the Windows encoding (this is the default for the Windows binary), +# whereas setting the tag to NO uses a Unix-style encoding (the default for +# all platforms other than Windows). + +USE_WINDOWS_ENCODING = NO + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like the Qt-style comments (thus requiring an +# explicit @brief command for a brief description. + +JAVADOC_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the DETAILS_AT_TOP tag is set to YES then Doxygen +# will output the detailed description near the top, like JavaDoc. +# If set to NO, the detailed description appears after the member +# documentation. + +DETAILS_AT_TOP = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = YES + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for Java. +# For instance, namespaces will be presented as packages, qualified scopes +# will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want to +# include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = NO + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = YES + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = NO + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST = YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. The default is NO. + +SHOW_DIRECTORIES = NO + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from the +# version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = include/zoo_lock.h + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py + +FILE_PATTERNS = + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES (the default) +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES (the default) +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. Otherwise they will link to the documentstion. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = $(GENERATE_HTML) + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compressed HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = $(GENERATE_HTMLHELP) + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = ../$(PROJECT).chm + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = $(HHC_PATH) + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = $(GENERATE_CHI) + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# If the GENERATE_TREEVIEW tag is set to YES, a side panel will be +# generated containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (for instance Mozilla 1.0+, +# Netscape 6.0+, Internet explorer 5.0+, or Konqueror). Windows users are +# probably better off using the HTML help feature. + +GENERATE_TREEVIEW = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = $(GENERATE_LATEX) + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = $(PAPER_SIZE) + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = NO + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = $(GENERATE_PDF) + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = $(GENERATE_RTF) + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = $(GENERATE_MAN) + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = $(GENERATE_XML) + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. This is useful +# if you want to understand what is going on. On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = $(DOCDIR)/$(PROJECT).tag + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = $(HAVE_DOT) + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a call dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable call graphs for selected +# functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a caller dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable caller graphs for selected +# functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = $(DOT_PATH) + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The MAX_DOT_GRAPH_WIDTH tag can be used to set the maximum allowed width +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_WIDTH = 1024 + +# The MAX_DOT_GRAPH_HEIGHT tag can be used to set the maximum allows height +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_HEIGHT = 1024 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that a graph may be further truncated if the graph's +# image dimensions are not sufficient to fit the graph (see MAX_DOT_GRAPH_WIDTH +# and MAX_DOT_GRAPH_HEIGHT). If 0 is used for the depth value (the default), +# the graph is not depth-constrained. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, which results in a white background. +# Warning: Depending on the platform used, enabling this option may lead to +# badly anti-aliased labels on the edges of a graph (i.e. they become hard to +# read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- + +# The SEARCHENGINE tag specifies whether or not a search engine should be +# used. If set to NO the values of all tags below this one will be ignored. + +SEARCHENGINE = NO diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/configure.ac b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/configure.ac new file mode 100644 index 0000000..53b2ea5 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/configure.ac @@ -0,0 +1,82 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) + +AC_INIT([zoolock], [3.2.0]) + +AC_CONFIG_SRCDIR([include/zoo_lock.h]) + +PACKAGE=zoolock +VERSION=1.0 + +AC_SUBST(PACKAGE) +AC_SUBST(VERSION) + +BUILD_PATH="`pwd`" + +# Checks for programs. +AC_LANG_CPLUSPLUS + +AM_INIT_AUTOMAKE([-Wall foreign]) +# Checks for libraries. + +#initialize Doxygen support +DX_HTML_FEATURE(ON) +DX_CHM_FEATURE(OFF) +DX_CHI_FEATURE(OFF) +DX_MAN_FEATURE(OFF) +DX_RTF_FEATURE(OFF) +DX_XML_FEATURE(OFF) +DX_PDF_FEATURE(OFF) +DX_PS_FEATURE(OFF) +DX_INIT_DOXYGEN([zookeeper-locks],[c-doc.Doxyfile],[docs]) + + +ZOOKEEPER_PATH=${BUILD_PATH}/../../../../../zookeeper-client/zookeeper-client-c +ZOOKEEPER_LD=-L${BUILD_PATH}/../../../../../zookeeper-client/zookeeper-client-c\ -lzookeeper_mt + +AC_SUBST(ZOOKEEPER_PATH) +AC_SUBST(ZOOKEEPER_LD) + +# Checks for header files. +AC_HEADER_DIRENT +AC_HEADER_STDC +AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/time.h unistd.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_CONST +AC_TYPE_UID_T +AC_C_INLINE +AC_TYPE_OFF_T +AC_TYPE_SIZE_T +AC_STRUCT_ST_BLOCKS +AC_HEADER_TIME +AC_C_VOLATILE +AC_PROG_CC +AC_PROG_LIBTOOL +#check for cppunit +AM_PATH_CPPUNIT(1.10.2) +# Checks for library functions. +AC_FUNC_UTIME_NULL +AC_CHECK_FUNCS([gettimeofday memset mkdir rmdir strdup strerror strstr strtol strtoul strtoull utime]) + +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT +AC_C_VOLATILE diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/include/zoo_lock.h b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/include/zoo_lock.h new file mode 100644 index 0000000..5c06bbf --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/include/zoo_lock.h @@ -0,0 +1,168 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#ifndef ZOOKEEPER_LOCK_H_ +#define ZOOKEEPER_LOCK_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif +/** + * \brief the call back function called on status change of lock + * + * the call back funtion is called with a rc of 0 if lock is acquired and + * with an rc of 1 if the lock is released + * \param rc the value to let us know if its locked or unlocked + * \param cbdata the callback data that we passed when initializing + * the zookeeper lock. + */ + +typedef void (* zkr_lock_completion) (int rc, void* cbdata); + +/** + * \file zoo_lock.h + * \brief zookeeper recipe for locking and leader election. + * this api implements a writelock on a given path in zookeeper. + * this api can also be used for leader election. + */ + +struct zkr_lock_mutex { + zhandle_t *zh; + char *path; + struct ACL_vector *acl; + char *id; + void *cbdata; + zkr_lock_completion completion; + pthread_mutex_t pmutex; + int isOwner; + char* ownerid; +}; + +typedef struct zkr_lock_mutex zkr_lock_mutex_t; + + +/** + * \brief initializing a zookeeper lock. + * + * this method instantiates the zookeeper mutex lock. + * \param mutex the mutex to initialize + * \param zh the zookeeper handle to use + * \param path the path in zookeeper to use for locking + * \param acl the acls to use in zookeeper. + * \return return 0 if successful. + */ +ZOOAPI int zkr_lock_init(zkr_lock_mutex_t *mutex, zhandle_t* zh, + char* path, struct ACL_vector *acl); + +/** + * \brief initializing a zookeeper lock. + * + * + * this method instantiates the zookeeper mutex lock with + * a completion function. + * + * \param mutex the mutex to initialize + * \param zh the zookeeper handle to use + * \param path the path in zookeeper to use for locking + * \param acl the acls to use in zookeeper. + * \param completion the callback thats called when lock + * is acquired and released. + * \param cbdata the callback method is called with data + * \return return 0 if successful. + */ +ZOOAPI int zkr_lock_init_cb(zkr_lock_mutex_t *mutex, zhandle_t* zh, + char* path, struct ACL_vector *acl, + zkr_lock_completion completion, void* cbdata); + +/** + * \brief lock the zookeeper mutex + * + * this method tries locking the mutex + * \param mutex the zookeeper mutex + * \return return 0 if there is no error. check + * with zkr_lock_isowner() if you have the lock + */ +ZOOAPI int zkr_lock_lock(zkr_lock_mutex_t *mutex); + +/** + * \brief unlock the zookeeper mutex + * + * this method unlocks the zookeeper mutex + * \param mutex the zookeeper mutex + * \return return 0 if there is not error in executing unlock. + * else returns non zero + */ +ZOOAPI int zkr_lock_unlock(zkr_lock_mutex_t *mutex); + +/** + * \brief set the callback function for zookeeper mutex + * + * this method sets the callback for zookeeper mutex + * \param mutex the zookeeper mutex + * \param callback the call back completion function + */ +ZOOAPI void zkr_lock_setcallback(zkr_lock_mutex_t *mutex, + zkr_lock_completion completion); + +/** + * \brief get the callback function for zookeeper mutex + * + * this method gets the callback funtion for zookeeper mutex + * \param mutex the zookeeper mutex + * \return the lock completion function + */ +ZOOAPI zkr_lock_completion zkr_lock_getcallback(zkr_lock_mutex_t *mutex); + +/** + * \brief destroy the mutex + * this method free the mutex + * \param mutex destroy the zookepeer lock. + * \return return 0 if destroyed. + */ +ZOOAPI int zkr_lock_destroy(zkr_lock_mutex_t* mutex); + +/** + * \brief return the parent path this mutex is using + * this method returns the parent path + * \param mutex the mutex + * \return return the parent path + */ +ZOOAPI char* zkr_lock_getpath(zkr_lock_mutex_t *mutex); + +/** + * \brief return if this mutex is owner of the lock + * this method returns if its owner or not + * \param mutex the mutex + * \return return true if is owner and false if not + */ +ZOOAPI int zkr_lock_isowner(zkr_lock_mutex_t *mutex); + +/** + * \brief return the id for this mutex + * this mutex retunrns the id string + * \param mutex the mutex + * \return the id for this mutex + */ +ZOOAPI char* zkr_lock_getid(zkr_lock_mutex_t *mutex); + +#ifdef __cplusplus +} +#endif +#endif //ZOOKEEPER_LOCK_H_ diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/src/zoo_lock.c b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/src/zoo_lock.c new file mode 100644 index 0000000..5721d4e --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/src/zoo_lock.c @@ -0,0 +1,416 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifdef DLL_EXPORT +#define USE_STATIC_LIB +#endif + +#if defined(__CYGWIN__) +#define USE_IPV6 +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_UTSNAME_H +#include +#endif + +#ifdef HAVE_GETPWUID_R +#include +#endif + +#define IF_DEBUG(x) if (logLevel==ZOO_LOG_LEVEL_DEBUG) {x;} + + +ZOOAPI int zkr_lock_init(zkr_lock_mutex_t* mutex, zhandle_t* zh, + char* path, struct ACL_vector *acl) { + mutex->zh = zh; + mutex->path = path; + mutex->acl = acl; + mutex->completion = NULL; + mutex->cbdata = NULL; + mutex->id = NULL; + mutex->ownerid = NULL; + mutex->isOwner = 0; + pthread_mutex_init(&(mutex->pmutex), NULL); + return 0; +} + +ZOOAPI int zkr_lock_init_cb(zkr_lock_mutex_t *mutex, zhandle_t* zh, + char *path, struct ACL_vector *acl, + zkr_lock_completion completion, void* cbdata) { + mutex->zh = zh; + mutex->path = path; + mutex->acl = acl; + mutex->completion = completion; + mutex->cbdata = cbdata; + mutex->isOwner = 0; + mutex->ownerid = NULL; + mutex->id = NULL; + pthread_mutex_init(&(mutex->pmutex), NULL); + return 0; +} + +static int _zkr_lock_unlock_nolock(zkr_lock_mutex_t *mutex) { + zhandle_t *zh = mutex->zh; + if (mutex->id != NULL) { + int len = strlen(mutex->path) + strlen(mutex->id) + 2; + char buf[len]; + sprintf(buf, "%s/%s", mutex->path, mutex->id); + int ret = 0; + int count = 0; + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = (.5)*1000000; + ret = ZCONNECTIONLOSS; + while (ret == ZCONNECTIONLOSS && (count < 3)) { + ret = zoo_delete(zh, buf, -1); + if (ret == ZCONNECTIONLOSS) { + LOG_DEBUG(LOGCALLBACK(zh), ("connectionloss while deleting the node")); + nanosleep(&ts, 0); + count++; + } + } + if (ret == ZOK || ret == ZNONODE) { + zkr_lock_completion completion = mutex->completion; + if (completion != NULL) { + completion(1, mutex->cbdata); + } + + free(mutex->id); + mutex->id = NULL; + return 0; + } + LOG_WARN(LOGCALLBACK(zh), ("not able to connect to server - giving up")); + return ZCONNECTIONLOSS; + } + + return ZSYSTEMERROR; +} +/** + * unlock the mutex + */ +ZOOAPI int zkr_lock_unlock(zkr_lock_mutex_t *mutex) { + int ret = 0; + pthread_mutex_lock(&(mutex->pmutex)); + ret = _zkr_lock_unlock_nolock(mutex); + pthread_mutex_unlock(&(mutex->pmutex)); + return ret; +} + +static void free_String_vector(struct String_vector *v) { + if (v->data) { + int32_t i; + for (i=0; icount; i++) { + free(v->data[i]); + } + free(v->data); + v->data = 0; + } +} + +static int strcmp_suffix(const char *str1, const char *str2) { + return strcmp(strrchr(str1, '-')+1, strrchr(str2, '-')+1); +} + +static int vstrcmp(const void* str1, const void* str2) { + const char **a = (const char**)str1; + const char **b = (const char**) str2; + return strcmp_suffix(*a, *b); +} + +static void sort_children(struct String_vector *vector) { + qsort( vector->data, vector->count, sizeof(char*), &vstrcmp); +} + +static char* child_floor(char **sorted_data, int len, char *element) { + char* ret = NULL; + int targetpos = -1, s = 0, e = len -1; + + while ( targetpos < 0 && s <= e ) { + int const i = s + (e - s) / 2; + int const cmp = strcmp_suffix(sorted_data[i], element); + if (cmp < 0) { + s = i + 1; + } else if (cmp == 0) { + targetpos = i; + } else { + e = i - 1; + } + } + + if (targetpos > 0) { + ret = sorted_data[targetpos - 1]; + } + + return ret; +} + +static void lock_watcher_fn(zhandle_t* zh, int type, int state, + const char* path, void *watcherCtx) { + //callback that we registered + //should be called + zkr_lock_lock((zkr_lock_mutex_t*) watcherCtx); +} + +/** + * get the last name of the path + */ +static char* getName(char* str) { + char* name = strrchr(str, '/'); + if (name == NULL) + return NULL; + return strdup(name + 1); +} + +/** + * just a method to retry get children + */ +static int retry_getchildren(zhandle_t *zh, char* path, struct String_vector *vector, + struct timespec *ts, int retry) { + int ret = ZCONNECTIONLOSS; + int count = 0; + while (ret == ZCONNECTIONLOSS && count < retry) { + ret = zoo_get_children(zh, path, 0, vector); + if (ret == ZCONNECTIONLOSS) { + LOG_DEBUG(LOGCALLBACK(zh), ("connection loss to the server")); + nanosleep(ts, 0); + count++; + } + } + return ret; +} + +/** see if our node already exists + * if it does then we dup the name and + * return it + */ +static char* lookupnode(struct String_vector *vector, char *prefix) { + char *ret = NULL; + if (vector->data) { + int i = 0; + for (i = 0; i < vector->count; i++) { + char* child = vector->data[i]; + if (strncmp(prefix, child, strlen(prefix)) == 0) { + ret = strdup(child); + break; + } + } + } + return ret; +} + +/** retry zoo_wexists + */ +static int retry_zoowexists(zhandle_t *zh, char* path, watcher_fn watcher, void* ctx, + struct Stat *stat, struct timespec *ts, int retry) { + int ret = ZCONNECTIONLOSS; + int count = 0; + while (ret == ZCONNECTIONLOSS && count < retry) { + ret = zoo_wexists(zh, path, watcher, ctx, stat); + if (ret == ZCONNECTIONLOSS) { + LOG_DEBUG(LOGCALLBACK(zh), ("connectionloss while setting watch on my predecessor")); + nanosleep(ts, 0); + count++; + } + } + return ret; +} + +/** + * the main code that does the zookeeper leader + * election. this code creates its own ephemeral + * node on the given path and sees if its the first + * one on the list and claims to be a leader if and only + * if its the first one of children in the paretn path + */ +static int zkr_lock_operation(zkr_lock_mutex_t *mutex, struct timespec *ts) { + zhandle_t *zh = mutex->zh; + char *path = mutex->path; + char *id = mutex->id; + struct Stat stat; + char* owner_id = NULL; + int retry = 3; + do { + const clientid_t *cid = zoo_client_id(zh); + // get the session id + int64_t session = cid->client_id; + char prefix[30]; + int ret = 0; +#if defined(__x86_64__) + snprintf(prefix, 30, "x-%016lx-", session); +#else + snprintf(prefix, 30, "x-%016llx-", session); +#endif + struct String_vector vectorst; + vectorst.data = NULL; + vectorst.count = 0; + ret = ZCONNECTIONLOSS; + ret = retry_getchildren(zh, path, &vectorst, ts, retry); + if (ret != ZOK) + return ret; + struct String_vector *vector = &vectorst; + mutex->id = lookupnode(vector, prefix); + free_String_vector(vector); + if (mutex->id == NULL) { + int len = strlen(path) + strlen(prefix) + 2; + char buf[len]; + char retbuf[len+20]; + snprintf(buf, len, "%s/%s", path, prefix); + ret = ZCONNECTIONLOSS; + ret = zoo_create(zh, buf, NULL, 0, mutex->acl, + ZOO_EPHEMERAL|ZOO_SEQUENCE, retbuf, (len+20)); + + // do not want to retry the create since + // we would end up creating more than one child + if (ret != ZOK) { + LOG_WARN(LOGCALLBACK(zh), "could not create zoo node %s", buf); + return ret; + } + mutex->id = getName(retbuf); + } + + if (mutex->id != NULL) { + ret = ZCONNECTIONLOSS; + ret = retry_getchildren(zh, path, vector, ts, retry); + if (ret != ZOK) { + LOG_WARN(LOGCALLBACK(zh), ("could not connect to server")); + return ret; + } + //sort this list + sort_children(vector); + owner_id = vector->data[0]; + mutex->ownerid = strdup(owner_id); + id = mutex->id; + char* lessthanme = child_floor(vector->data, vector->count, id); + if (lessthanme != NULL) { + int flen = strlen(mutex->path) + strlen(lessthanme) + 2; + char last_child[flen]; + sprintf(last_child, "%s/%s",mutex->path, lessthanme); + ret = ZCONNECTIONLOSS; + ret = retry_zoowexists(zh, last_child, &lock_watcher_fn, mutex, + &stat, ts, retry); + // cannot watch my predecessor i am giving up + // we need to be able to watch the predecessor + // since if we do not become a leader the others + // will keep waiting + if (ret != ZOK) { + free_String_vector(vector); + LOG_WARN(LOGCALLBACK(zh), ("unable to watch my predecessor")); + ret = _zkr_lock_unlock_nolock(mutex); + while (ret == 0) { + //we have to give up our leadership + // since we cannot watch out predecessor + ret = _zkr_lock_unlock_nolock(mutex); + } + return ret; + } + // we are not the owner of the lock + mutex->isOwner = 0; + } + else { + // this is the case when we are the owner + // of the lock + if (strcmp(mutex->id, owner_id) == 0) { + LOG_DEBUG(LOGCALLBACK(zh), "got the zoo lock owner - %s", mutex->id); + mutex->isOwner = 1; + if (mutex->completion != NULL) { + mutex->completion(0, mutex->cbdata); + } + return ZOK; + } + } + free_String_vector(vector); + return ZOK; + } + } while (mutex->id == NULL); + return ZOK; +} + +ZOOAPI int zkr_lock_lock(zkr_lock_mutex_t *mutex) { + pthread_mutex_lock(&(mutex->pmutex)); + zhandle_t *zh = mutex->zh; + char *path = mutex->path; + struct Stat stat; + int exists = zoo_exists(zh, path, 0, &stat); + int count = 0; + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = (.5)*1000000; + // retry to see if the path exists and + // and create if the path does not exist + while ((exists == ZCONNECTIONLOSS || exists == ZNONODE) && (count <4)) { + count++; + // retry the operation + if (exists == ZCONNECTIONLOSS) + exists = zoo_exists(zh, path, 0, &stat); + else if (exists == ZNONODE) + exists = zoo_create(zh, path, NULL, 0, mutex->acl, 0, NULL, 0); + nanosleep(&ts, 0); + + } + + // need to check if we cannot still access the server + int check_retry = ZCONNECTIONLOSS; + count = 0; + while (check_retry != ZOK && count <4) { + check_retry = zkr_lock_operation(mutex, &ts); + if (check_retry != ZOK) { + nanosleep(&ts, 0); + count++; + } + } + pthread_mutex_unlock(&(mutex->pmutex)); + return 0; +} + + +ZOOAPI char* zkr_lock_getpath(zkr_lock_mutex_t *mutex) { + return mutex->path; +} + +ZOOAPI int zkr_lock_isowner(zkr_lock_mutex_t *mutex) { + return (mutex->id != NULL && mutex->ownerid != NULL + && (strcmp(mutex->id, mutex->ownerid) == 0)); +} + +ZOOAPI char* zkr_lock_getid(zkr_lock_mutex_t *mutex) { + return mutex->ownerid; +} + +ZOOAPI int zkr_lock_destroy(zkr_lock_mutex_t* mutex) { + if (mutex->id) + free(mutex->id); + mutex->path = NULL; + mutex->acl = NULL; + mutex->completion = NULL; + pthread_mutex_destroy(&(mutex->pmutex)); + mutex->isOwner = 0; + if (mutex->ownerid) + free(mutex->ownerid); + return 0; +} + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/tests/TestClient.cc b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/tests/TestClient.cc new file mode 100644 index 0000000..7a7675a --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/c/tests/TestClient.cc @@ -0,0 +1,202 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include + +#include +#include +#include + + +using namespace std; + +#include +#include + +#include +#include +#include + +static void yield(zhandle_t *zh, int i) +{ + sleep(i); +} + +typedef struct evt { + string path; + int type; +} evt_t; + +typedef struct watchCtx { +private: + list events; +public: + bool connected; + zhandle_t *zh; + + watchCtx() { + connected = false; + zh = 0; + } + ~watchCtx() { + if (zh) { + zookeeper_close(zh); + zh = 0; + } + } + + evt_t getEvent() { + evt_t evt; + evt = events.front(); + events.pop_front(); + return evt; + } + + int countEvents() { + int count; + count = events.size(); + return count; + } + + void putEvent(evt_t evt) { + events.push_back(evt); + } + + bool waitForConnected(zhandle_t *zh) { + time_t expires = time(0) + 10; + while(!connected && time(0) < expires) { + yield(zh, 1); + } + return connected; + } + bool waitForDisconnected(zhandle_t *zh) { + time_t expires = time(0) + 15; + while(connected && time(0) < expires) { + yield(zh, 1); + } + return !connected; + } +} watchctx_t; + +class Zookeeper_locktest : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_locktest); + CPPUNIT_TEST(testlock); + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ + watchctx_t *ctx = (watchctx_t*)v; + + if (state == ZOO_CONNECTED_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + + static const char hostPorts[]; + + const char *getHostPorts() { + return hostPorts; + } + + zhandle_t *createClient(watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, + ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } + +public: + +#define ZKSERVER_CMD "./tests/zkServer.sh" + + void setUp() + { + char cmd[1024]; + sprintf(cmd, "%s startClean %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + + void startServer() { + char cmd[1024]; + sprintf(cmd, "%s start %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void stopServer() { + tearDown(); + } + + void tearDown() + { + char cmd[1024]; + sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + + void testlock() + { + watchctx_t ctx; + int rc; + struct Stat stat; + char buf[1024]; + int blen; + struct String_vector strings; + const char *testName; + zkr_lock_mutex_t mutexes[3]; + int count = 3; + int i = 0; + char* path = "/test-lock"; + for (i=0; i< 3; i++) { + zhandle_t *zh = createClient(&ctx); + zkr_lock_init(&mutexes[i], zh, path, &ZOO_OPEN_ACL_UNSAFE); + zkr_lock_lock(&mutexes[i]); + } + sleep(30); + zkr_lock_mutex leader = mutexes[0]; + zkr_lock_mutex mutex; + int ret = strcmp(leader.id, leader.ownerid); + CPPUNIT_ASSERT(ret == 0); + for(i=1; i < count; i++) { + mutex = mutexes[i]; + CPPUNIT_ASSERT(strcmp(mutex.id, mutex.ownerid) != 0); + } + zkr_lock_unlock(&leader); + sleep(30); + zkr_lock_mutex secondleader = mutexes[1]; + CPPUNIT_ASSERT(strcmp(secondleader.id , secondleader.ownerid) == 0); + for (i=2; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Util.h" + +using namespace std; + +CPPUNIT_NS_BEGIN + +class EclipseOutputter: public CompilerOutputter +{ +public: + EclipseOutputter(TestResultCollector *result,ostream &stream): + CompilerOutputter(result,stream,"%p:%l: "),stream_(stream) + { + } + virtual void printFailedTestName( TestFailure *failure ){} + virtual void printFailureMessage( TestFailure *failure ) + { + stream_<<": "; + Message msg = failure->thrownException()->message(); + stream_<< msg.shortDescription(); + + string text; + for(int i=0; i the output must be in the compiler error format. + //bool selfTest = (argc > 1) && (std::string("-ide") == argv[1]); + globalTestConfig.addConfigFromCmdLine(argc,argv); + + // Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + // Add a listener that colllects test result + CPPUNIT_NS::TestResultCollector result; + controller.addListener( &result ); + + // Add a listener that print dots as tests run. + // CPPUNIT_NS::TextTestProgressListener progress; + CPPUNIT_NS::BriefTestProgressListener progress; + controller.addListener( &progress ); + + CPPUNIT_NS::TestRunner runner; + runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); + + try + { + cout << "Running " << globalTestConfig.getTestName(); + runner.run( controller, globalTestConfig.getTestName()); + cout< +#include +#include + +// number of elements in array +#define COUNTOF(array) sizeof(array)/sizeof(array[0]) + +#define DECLARE_WRAPPER(ret,sym,sig) \ + extern "C" ret __real_##sym sig; \ + extern "C" ret __wrap_##sym sig + +#define CALL_REAL(sym,params) \ + __real_##sym params + +// must include "src/zookeeper_log.h" to be able to use this macro +#define TEST_TRACE(x) \ + log_message(3,__LINE__,__func__,format_log_message x) + +extern const std::string EMPTY_STRING; + +// ***************************************************************************** +// A bit of wizardry to get to the bare type from a reference or a pointer +// to the type +template +struct TypeOp { + typedef T BareT; + typedef T ArgT; +}; + +// partial specialization for reference types +template +struct TypeOp{ + typedef T& ArgT; + typedef typename TypeOp::BareT BareT; +}; + +// partial specialization for pointers +template +struct TypeOp{ + typedef T* ArgT; + typedef typename TypeOp::BareT BareT; +}; + +// ***************************************************************************** +// Container utilities + +template +void putValue(std::map& map,const K& k, const V& v){ + typedef std::map Map; + typename Map::const_iterator it=map.find(k); + if(it==map.end()) + map.insert(typename Map::value_type(k,v)); + else + map[k]=v; +} + +template +bool getValue(const std::map& map,const K& k,V& v){ + typedef std::map Map; + typename Map::const_iterator it=map.find(k); + if(it==map.end()) + return false; + v=it->second; + return true; +} + +// ***************************************************************************** +// misc utils + +// millisecond sleep +void millisleep(int ms); +// evaluate given predicate until it returns true or the timeout +// (in millis) has expired +template +int ensureCondition(const Predicate& p,int timeout){ + int elapsed=0; + while(!p() && elapsed CmdLineOptList; +public: + typedef CmdLineOptList::const_iterator const_iterator; + TestConfig(){} + ~TestConfig(){} + void addConfigFromCmdLine(int argc, char* argv[]){ + if(argc>=2) + testName_=argv[1]; + for(int i=2; i /tmp/zk.log & + echo $! > /tmp/zk.pid + sleep 5 + ;; +stop) + # Already killed above + ;; +*) + echo "Unknown command " + $1 + exit 2 +esac + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/LockListener.java b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/LockListener.java new file mode 100644 index 0000000..c2c7851 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/LockListener.java @@ -0,0 +1,41 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +/** + * This class has two methods which are call + * back methods when a lock is acquired and + * when the lock is released. + * + */ +public interface LockListener { + + /** + * call back called when the lock + * is acquired. + */ + void lockAcquired(); + + /** + * call back called when the lock is + * released. + */ + void lockReleased(); + +} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ProtocolSupport.java b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ProtocolSupport.java new file mode 100644 index 0000000..178e67f --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ProtocolSupport.java @@ -0,0 +1,202 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A base class for protocol implementations which provides a number of higher + * level helper methods for working with ZooKeeper along with retrying synchronous + * operations if the connection to ZooKeeper closes such as + * {@link #retryOperation(ZooKeeperOperation)}. + */ +class ProtocolSupport { + + private static final Logger LOG = LoggerFactory.getLogger(ProtocolSupport.class); + private static final int RETRY_COUNT = 10; + + protected final ZooKeeper zookeeper; + private AtomicBoolean closed = new AtomicBoolean(false); + private long retryDelay = 500L; + private List acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; + + public ProtocolSupport(ZooKeeper zookeeper) { + this.zookeeper = zookeeper; + } + + /** + * Closes this strategy and releases any ZooKeeper resources; but keeps the + * ZooKeeper instance open. + */ + public void close() { + if (closed.compareAndSet(false, true)) { + doClose(); + } + } + + /** + * return zookeeper client instance. + * + * @return zookeeper client instance + */ + public ZooKeeper getZookeeper() { + return zookeeper; + } + + /** + * return the acl its using. + * + * @return the acl. + */ + public List getAcl() { + return acl; + } + + /** + * set the acl. + * + * @param acl the acl to set to + */ + public void setAcl(List acl) { + this.acl = acl; + } + + /** + * get the retry delay in milliseconds. + * + * @return the retry delay + */ + public long getRetryDelay() { + return retryDelay; + } + + /** + * Sets the time waited between retry delays. + * + * @param retryDelay the retry delay + */ + public void setRetryDelay(long retryDelay) { + this.retryDelay = retryDelay; + } + + /** + * Allow derived classes to perform + * some custom closing operations to release resources. + */ + protected void doClose() { + + } + + /** + * Perform the given operation, retrying if the connection fails. + * + * @return object. it needs to be cast to the callee's expected + * return type. + */ + protected Object retryOperation(ZooKeeperOperation operation) + throws KeeperException, InterruptedException { + KeeperException exception = null; + for (int i = 0; i < RETRY_COUNT; i++) { + try { + return operation.execute(); + } catch (KeeperException.SessionExpiredException e) { + LOG.warn("Session expired {}. Reconnecting...", zookeeper, e); + throw e; + } catch (KeeperException.ConnectionLossException e) { + if (exception == null) { + exception = e; + } + LOG.debug("Attempt {} failed with connection loss. Reconnecting...", i); + retryDelay(i); + } + } + + throw exception; + } + + /** + * Ensures that the given path exists with no data, the current + * ACL and no flags. + * + * @param path + */ + protected void ensurePathExists(String path) { + ensureExists(path, null, acl, CreateMode.PERSISTENT); + } + + /** + * Ensures that the given path exists with the given data, ACL and flags. + * + * @param path + * @param acl + * @param flags + */ + protected void ensureExists( + final String path, + final byte[] data, + final List acl, + final CreateMode flags) { + try { + retryOperation(() -> { + Stat stat = zookeeper.exists(path, false); + if (stat != null) { + return true; + } + zookeeper.create(path, data, acl, flags); + return true; + }); + } catch (KeeperException | InterruptedException e) { + LOG.warn("Unexpected exception", e); + } + } + + /** + * Returns true if this protocol has been closed. + * + * @return true if this protocol is closed + */ + protected boolean isClosed() { + return closed.get(); + } + + /** + * Performs a retry delay if this is not the first attempt. + * + * @param attemptCount the number of the attempts performed so far + */ + protected void retryDelay(int attemptCount) { + if (attemptCount > 0) { + try { + Thread.sleep(attemptCount * retryDelay); + } catch (InterruptedException e) { + LOG.warn("Failed to sleep.", e); + } + } + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/WriteLock.java b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/WriteLock.java new file mode 100644 index 0000000..7338a97 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/WriteLock.java @@ -0,0 +1,303 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +import static org.apache.zookeeper.CreateMode.EPHEMERAL_SEQUENTIAL; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.util.List; +import java.util.SortedSet; +import java.util.TreeSet; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A protocol to implement an exclusive + * write lock or to elect a leader. + * + *

      You invoke {@link #lock()} to start the process of grabbing the lock; + * you may get the lock then or it may be some time later. + * + *

      You can register a listener so that you are invoked when you get the lock; + * otherwise you can ask if you have the lock by calling {@link #isOwner()}. + * + */ +public class WriteLock extends ProtocolSupport { + + private static final Logger LOG = LoggerFactory.getLogger(WriteLock.class); + + private final String dir; + private String id; + private ZNodeName idName; + private String ownerId; + private String lastChildId; + private byte[] data = {0x12, 0x34}; + private LockListener callback; + private LockZooKeeperOperation zop; + + /** + * zookeeper contructor for writelock. + * + * @param zookeeper zookeeper client instance + * @param dir the parent path you want to use for locking + * @param acl the acls that you want to use for all the paths, if null world read/write is used. + */ + public WriteLock(ZooKeeper zookeeper, String dir, List acl) { + super(zookeeper); + this.dir = dir; + if (acl != null) { + setAcl(acl); + } + this.zop = new LockZooKeeperOperation(); + } + + /** + * zookeeper contructor for writelock with callback. + * + * @param zookeeper the zookeeper client instance + * @param dir the parent path you want to use for locking + * @param acl the acls that you want to use for all the paths + * @param callback the call back instance + */ + public WriteLock( + ZooKeeper zookeeper, + String dir, + List acl, + LockListener callback) { + this(zookeeper, dir, acl); + this.callback = callback; + } + + /** + * return the current locklistener. + * + * @return the locklistener + */ + public synchronized LockListener getLockListener() { + return this.callback; + } + + /** + * register a different call back listener. + * + * @param callback the call back instance + */ + public synchronized void setLockListener(LockListener callback) { + this.callback = callback; + } + + /** + * Removes the lock or associated znode if + * you no longer require the lock. this also + * removes your request in the queue for locking + * in case you do not already hold the lock. + * + * @throws RuntimeException throws a runtime exception + * if it cannot connect to zookeeper. + */ + public synchronized void unlock() throws RuntimeException { + + if (!isClosed() && id != null) { + // we don't need to retry this operation in the case of failure + // as ZK will remove ephemeral files and we don't wanna hang + // this process when closing if we cannot reconnect to ZK + try { + + ZooKeeperOperation zopdel = () -> { + zookeeper.delete(id, -1); + return Boolean.TRUE; + }; + zopdel.execute(); + } catch (InterruptedException e) { + LOG.warn("Unexpected exception", e); + // set that we have been interrupted. + Thread.currentThread().interrupt(); + } catch (KeeperException.NoNodeException e) { + // do nothing + } catch (KeeperException e) { + LOG.warn("Unexpected exception", e); + throw new RuntimeException(e.getMessage(), e); + } finally { + LockListener lockListener = getLockListener(); + if (lockListener != null) { + lockListener.lockReleased(); + } + id = null; + } + } + } + + /** + * the watcher called on + * getting watch while watching + * my predecessor. + */ + private class LockWatcher implements Watcher { + + public void process(WatchedEvent event) { + // lets either become the leader or watch the new/updated node + LOG.debug("Watcher fired: {}", event); + try { + lock(); + } catch (Exception e) { + LOG.warn("Failed to acquire lock", e); + } + } + + } + + /** + * a zoookeeper operation that is mainly responsible + * for all the magic required for locking. + */ + private class LockZooKeeperOperation implements ZooKeeperOperation { + + /** + * find if we have been created earler if not create our node. + * + * @param prefix the prefix node + * @param zookeeper teh zookeeper client + * @param dir the dir paretn + * @throws KeeperException + * @throws InterruptedException + */ + private void findPrefixInChildren(String prefix, ZooKeeper zookeeper, String dir) + throws KeeperException, InterruptedException { + List names = zookeeper.getChildren(dir, false); + for (String name : names) { + if (name.startsWith(prefix)) { + id = name; + LOG.debug("Found id created last time: {}", id); + break; + } + } + if (id == null) { + id = zookeeper.create(dir + "/" + prefix, data, getAcl(), EPHEMERAL_SEQUENTIAL); + + LOG.debug("Created id: {}", id); + } + + } + + /** + * the command that is run and retried for actually + * obtaining the lock. + * + * @return if the command was successful or not + */ + @SuppressFBWarnings( + value = "NP_NULL_PARAM_DEREF_NONVIRTUAL", + justification = "findPrefixInChildren will assign a value to this.id") + public boolean execute() throws KeeperException, InterruptedException { + do { + if (id == null) { + long sessionId = zookeeper.getSessionId(); + String prefix = "x-" + sessionId + "-"; + // lets try look up the current ID if we failed + // in the middle of creating the znode + findPrefixInChildren(prefix, zookeeper, dir); + idName = new ZNodeName(id); + } + List names = zookeeper.getChildren(dir, false); + if (names.isEmpty()) { + LOG.warn("No children in: {} when we've just created one! Lets recreate it...", dir); + // lets force the recreation of the id + id = null; + } else { + // lets sort them explicitly (though they do seem to come back in order ususally :) + SortedSet sortedNames = new TreeSet<>(); + for (String name : names) { + sortedNames.add(new ZNodeName(dir + "/" + name)); + } + ownerId = sortedNames.first().getName(); + SortedSet lessThanMe = sortedNames.headSet(idName); + if (!lessThanMe.isEmpty()) { + ZNodeName lastChildName = lessThanMe.last(); + lastChildId = lastChildName.getName(); + LOG.debug("Watching less than me node: {}", lastChildId); + Stat stat = zookeeper.exists(lastChildId, new LockWatcher()); + if (stat != null) { + return Boolean.FALSE; + } else { + LOG.warn("Could not find the stats for less than me: {}", lastChildName.getName()); + } + } else { + if (isOwner()) { + LockListener lockListener = getLockListener(); + if (lockListener != null) { + lockListener.lockAcquired(); + } + return Boolean.TRUE; + } + } + } + } + while (id == null); + return Boolean.FALSE; + } + + } + + /** + * Attempts to acquire the exclusive write lock returning whether or not it was + * acquired. Note that the exclusive lock may be acquired some time later after + * this method has been invoked due to the current lock owner going away. + */ + public synchronized boolean lock() throws KeeperException, InterruptedException { + if (isClosed()) { + return false; + } + ensurePathExists(dir); + + return (Boolean) retryOperation(zop); + } + + /** + * return the parent dir for lock. + * + * @return the parent dir used for locks. + */ + public String getDir() { + return dir; + } + + /** + * Returns true if this node is the owner of the + * lock (or the leader). + */ + public boolean isOwner() { + return id != null && id.equals(ownerId); + } + + /** + * return the id for this lock. + * + * @return the id for this lock + */ + public String getId() { + return this.id; + } + +} + diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java new file mode 100644 index 0000000..76b1759 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java @@ -0,0 +1,118 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Represents an ephemeral znode name which has an ordered sequence number + * and can be sorted in order. + * + */ +class ZNodeName implements Comparable { + + private final String name; + private String prefix; + private int sequence = -1; + private static final Logger LOG = LoggerFactory.getLogger(ZNodeName.class); + + public ZNodeName(String name) { + if (name == null) { + throw new NullPointerException("id cannot be null"); + } + this.name = name; + this.prefix = name; + int idx = name.lastIndexOf('-'); + if (idx >= 0) { + this.prefix = name.substring(0, idx); + try { + this.sequence = Integer.parseInt(name.substring(idx + 1)); + // If an exception occurred we mis-detected a sequence suffix, + // so return -1. + } catch (NumberFormatException e) { + LOG.warn("Number format exception for {}.", idx, e); + } catch (ArrayIndexOutOfBoundsException e) { + LOG.warn("Array out of bounds for {}.", idx, e); + } + } + } + + @Override + public String toString() { + return name.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + ZNodeName sequence = (ZNodeName) o; + + return name.equals(sequence.name); + } + + @Override + public int hashCode() { + return name.hashCode() + 37; + } + + /** + * Compare znodes based on their sequence number. + * + * @param that other znode to compare to + * @return the difference between their sequence numbers: a positive value if this + * znode has a larger sequence number, 0 if they have the same sequence number + * or a negative number if this znode has a lower sequence number + */ + public int compareTo(ZNodeName that) { + int answer = this.sequence - that.sequence; + if (answer == 0) { + return this.prefix.compareTo(that.prefix); + } + return answer; + } + + /** + * Returns the name of the znode. + */ + public String getName() { + return name; + } + + /** + * Returns the sequence number. + */ + public int getZNodeName() { + return sequence; + } + + /** + * Returns the text prefix before the sequence number. + */ + public String getPrefix() { + return prefix; + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZooKeeperOperation.java b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZooKeeperOperation.java new file mode 100644 index 0000000..cb8bf1e --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZooKeeperOperation.java @@ -0,0 +1,40 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +import org.apache.zookeeper.KeeperException; + +/** + * A callback object which can be used for implementing retry-able operations in the + * {@link org.apache.zookeeper.recipes.lock.ProtocolSupport} class. + * + */ +public interface ZooKeeperOperation { + + /** + * Performs the operation - which may be involved multiple times if the connection + * to ZooKeeper closes during this operation. + * + * @return the result of the operation or null + * @throws KeeperException + * @throws InterruptedException + */ + boolean execute() throws KeeperException, InterruptedException; + +} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/WriteLockTest.java b/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/WriteLockTest.java new file mode 100644 index 0000000..6809fad --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/WriteLockTest.java @@ -0,0 +1,156 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +/** + * test for writelock. + */ +public class WriteLockTest extends ClientBase { + + protected int sessionTimeout = 10 * 1000; + protected String dir = "/" + getClass().getName(); + protected WriteLock[] nodes; + protected CountDownLatch latch = new CountDownLatch(1); + private boolean restartServer = true; + private boolean workAroundClosingLastZNodeFails = true; + private boolean killLeader = true; + + @Test + public void testRun() throws Exception { + runTest(3); + } + + class LockCallback implements LockListener { + + public void lockAcquired() { + latch.countDown(); + } + + public void lockReleased() { + + } + + } + protected void runTest(int count) throws Exception { + nodes = new WriteLock[count]; + for (int i = 0; i < count; i++) { + ZooKeeper keeper = createClient(); + WriteLock leader = new WriteLock(keeper, dir, null); + leader.setLockListener(new LockCallback()); + nodes[i] = leader; + + leader.lock(); + } + + // lets wait for any previous leaders to die and one of our new + // nodes to become the new leader + latch.await(30, TimeUnit.SECONDS); + + WriteLock first = nodes[0]; + dumpNodes(count); + + // lets assert that the first election is the leader + Assert.assertTrue("The first znode should be the leader " + first.getId(), first.isOwner()); + + for (int i = 1; i < count; i++) { + WriteLock node = nodes[i]; + Assert.assertFalse("Node should not be the leader " + node.getId(), node.isOwner()); + } + + if (count > 1) { + if (killLeader) { + System.out.println("Now killing the leader"); + // now lets kill the leader + latch = new CountDownLatch(1); + first.unlock(); + latch.await(30, TimeUnit.SECONDS); + //Thread.sleep(10000); + WriteLock second = nodes[1]; + dumpNodes(count); + // lets assert that the first election is the leader + Assert.assertTrue("The second znode should be the leader " + second.getId(), second.isOwner()); + + for (int i = 2; i < count; i++) { + WriteLock node = nodes[i]; + Assert.assertFalse("Node should not be the leader " + node.getId(), node.isOwner()); + } + } + + if (restartServer) { + // now lets stop the server + System.out.println("Now stopping the server"); + stopServer(); + Thread.sleep(10000); + + // TODO lets assert that we are no longer the leader + dumpNodes(count); + + System.out.println("Starting the server"); + startServer(); + Thread.sleep(10000); + + for (int i = 0; i < count - 1; i++) { + System.out.println("Calling acquire for node: " + i); + nodes[i].lock(); + } + dumpNodes(count); + System.out.println("Now closing down..."); + } + } + } + + protected void dumpNodes(int count) { + for (int i = 0; i < count; i++) { + WriteLock node = nodes[i]; + System.out.println("node: " + i + " id: " + node.getId() + " is leader: " + node.isOwner()); + } + } + + @After + public void tearDown() throws Exception { + if (nodes != null) { + for (int i = 0; i < nodes.length; i++) { + WriteLock node = nodes[i]; + if (node != null) { + System.out.println("Closing node: " + i); + node.close(); + if (workAroundClosingLastZNodeFails && i == nodes.length - 1) { + System.out.println("Not closing zookeeper: " + i + " due to bug!"); + } else { + System.out.println("Closing zookeeper: " + i); + node.getZookeeper().close(); + System.out.println("Closed zookeeper: " + i); + } + } + } + } + System.out.println("Now lets stop the server"); + super.tearDown(); + + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java b/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java new file mode 100644 index 0000000..40872fc --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java @@ -0,0 +1,82 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.lock; + +import java.util.SortedSet; +import java.util.TreeSet; +import org.junit.Assert; +import org.junit.Test; + +/** + * test for znodenames. + */ +public class ZNodeNameTest { + + @Test + public void testOrderWithSamePrefix() throws Exception { + String[] names = {"x-3", "x-5", "x-11", "x-1"}; + String[] expected = {"x-1", "x-3", "x-5", "x-11"}; + assertOrderedNodeNames(names, expected); + } + @Test + public void testOrderWithDifferentPrefixes() throws Exception { + String[] names = {"r-3", "r-2", "r-1", "w-2", "w-1"}; + String[] expected = {"r-1", "w-1", "r-2", "w-2", "r-3"}; + assertOrderedNodeNames(names, expected); + } + @Test + public void testOrderWithDifferentPrefixIncludingSessionId() throws Exception { + String[] names = { + "x-242681582799028564-0000000002", + "x-170623981976748329-0000000003", + "x-98566387950223723-0000000001" + }; + String[] expected = { + "x-98566387950223723-0000000001", + "x-242681582799028564-0000000002", + "x-170623981976748329-0000000003" + }; + assertOrderedNodeNames(names, expected); + } + @Test + public void testOrderWithExtraPrefixes() throws Exception { + String[] names = {"r-1-3-2", "r-2-2-1", "r-3-1-3"}; + String[] expected = {"r-2-2-1", "r-1-3-2", "r-3-1-3"}; + assertOrderedNodeNames(names, expected); + } + + protected void assertOrderedNodeNames(String[] names, String[] expected) { + int size = names.length; + SortedSet nodeNames = new TreeSet<>(); + for (String name : names) { + nodeNames.add(new ZNodeName(name)); + } + Assert.assertEquals( + "The SortedSet does not have the expected size!", + nodeNames.size(), + expected.length); + + int index = 0; + for (ZNodeName nodeName : nodeNames) { + String name = nodeName.getName(); + Assert.assertEquals("Node " + index, expected[index++], name); + } + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-queue/README.txt b/zookeeper-recipes/zookeeper-recipes-queue/README.txt new file mode 100644 index 0000000..d59a3c3 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/README.txt @@ -0,0 +1,30 @@ + + +1) This queue interface recipe implements the queue recipe +mentioned in ../../../docs/recipes.[html,pdf]. +A more detailed explanation is at http://www.cloudera.com/blog/2009/05/28/building-a-distributed-concurrent-queue-with-apache-zookeeper/ + +2) This recipe does not handle KeeperException.ConnectionLossException or ZCONNECTIONLOSS. It will only work correctly once ZOOKEEPER-22 https://issues.apache.org/jira/browse/ZOOKEEPER-22 is resolved. + +3) To compile the queue java recipe you can just run ant jar from +this directory. +Please report any bugs on the jira + +http://issues.apache.org/jira/browse/ZOOKEEPER + + diff --git a/zookeeper-recipes/zookeeper-recipes-queue/build.xml b/zookeeper-recipes/zookeeper-recipes-queue/build.xml new file mode 100644 index 0000000..4346d96 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/build.xml @@ -0,0 +1,132 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Tests failed! + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-recipes/zookeeper-recipes-queue/pom.xml b/zookeeper-recipes/zookeeper-recipes-queue/pom.xml new file mode 100644 index 0000000..9b5ea60 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/pom.xml @@ -0,0 +1,60 @@ + + + + 4.0.0 + + org.apache.zookeeper + zookeeper-recipes + 3.6.3 + + + zookeeper-recipes-queue + jar + Apache ZooKeeper - Recipes - Queue + + This queue interface recipe implements the queue recipe + A more detailed explanation is at: + http://www.cloudera.com/blog/2009/05/28/building-a-distributed-concurrent-queue-with-apache-zookeeper/ + + This recipe does not handle KeeperException.ConnectionLossException or ZCONNECTIONLOSS. + It will only work correctly once ZOOKEEPER-22 https://issues.apache.org/jira/browse/ZOOKEEPER-22 is resolved. + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${surefire-forkcount} + false + -Xmx512m + ${project.basedir} + true + + ${project.build.directory}/surefire + + + + + + + \ No newline at end of file diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/INSTALL b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/INSTALL new file mode 100644 index 0000000..5458714 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/INSTALL @@ -0,0 +1,234 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, +2006 Free Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf bug. Until the bug is fixed you can use this workaround: + + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/LICENSE b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/LICENSE @@ -0,0 +1,202 @@ + + 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/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/Makefile.am b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/Makefile.am new file mode 100644 index 0000000..ecef3cc --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/Makefile.am @@ -0,0 +1,46 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +include $(top_srcdir)/aminclude.am + +AM_CFLAGS = -Wall -fPIC -I${ZOOKEEPER_PATH}/include -I${ZOOKEEPER_PATH}/generated \ + -I$(top_srcdir)/include -I/usr/include +AM_CPPFLAGS = -Wall -I${ZOOKEEPER_PATH}/include -I${ZOOKEEPER_PATH}/generated\ + -I${top_srcdir}/include -I/usr/include +EXTRA_DIST = LICENSE +lib_LTLIBRARIES = libzooqueue.la +libzooqueue_la_SOURCES = src/zoo_queue.c include/zoo_queue.h +libzooqueue_la_CPPFLAGS = -DDLOPEN_MODULE +libzooqueue_la_LDFLAGS = -version-info 0:1:0 + +#run the tests now + +TEST_SOURCES = tests/TestDriver.cc tests/TestClient.cc tests/Util.cc + + +check_PROGRAMS = zkqueuetest +nodist_zkqueuetest_SOURCES = ${TEST_SOURCES} +zkqueuetest_LDADD = ${ZOOKEEPER_LD} libzooqueue.la -lpthread ${CPPUNIT_LIBS} +zkqueuetest_CXXFLAGS = -DUSE_STATIC_LIB ${CPPUNIT_CFLAGS} + +run-check: check + ./zkqueuetest ${TEST_OPTIONS} + +clean-local: clean-check + ${RM} ${DX_CLEANFILES} + +clean-check: + ${RM} ${nodist_zkqueuetest_OBJECTS} diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/README.txt b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/README.txt new file mode 100644 index 0000000..10a215c --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/README.txt @@ -0,0 +1,30 @@ + Zookeeper C queue client library + + +INSTALLATION + +If you're building the client from a source checkout you need to +follow the steps outlined below. If you're building from a release +tar downloaded from Apache please skip to step 2. + +This recipe does not handle ZCONNECTIONLOSS. It will only work correctly once ZOOKEEPER-22 https://issues.apache.org/jira/browse/ZOOKEEPER-22 is resolved. + +1) make sure that you compile the main zookeeper c client library. + +2) change directory to zookeeper-recipes/zookeeper-recipes-queue/src/main/c + and do a "autoreconf -if" to bootstrap + autoconf, automake and libtool. Please make sure you have autoconf + version 2.59 or greater installed. +3) do a "./configure [OPTIONS]" to generate the makefile. See INSTALL + for general information about running configure. + +4) do a "make" or "make install" to build the libraries and install them. + Alternatively, you can also build and run a unit test suite (and + you probably should). Please make sure you have cppunit-1.10.x or + higher installed before you execute step 4. Once ./configure has + finished, do a "make run-check". It will build the libraries, build + the tests and run them. +5) to generate doxygen documentation do a "make doxygen-doc". All + documentations will be placed to a new subfolder named docs. By + default only HTML documentation is generated. For information on + other document formats please use "./configure --help" diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/acinclude.m4 b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/acinclude.m4 new file mode 100644 index 0000000..a4d405a --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/acinclude.m4 @@ -0,0 +1,312 @@ +# This file is part of Autoconf. -*- Autoconf -*- + +# Copyright (C) 2004 Oren Ben-Kiki +# This file is distributed under the same terms as the Autoconf macro files. + +# Generate automatic documentation using Doxygen. Works in concert with the +# aminclude.m4 file and a compatible doxygen configuration file. Defines the +# following public macros: +# +# DX_???_FEATURE(ON|OFF) - control the default setting of a Doxygen feature. +# Supported features are 'DOXYGEN' itself, 'DOT' for generating graphics, +# 'HTML' for plain HTML, 'CHM' for compressed HTML help (for MS users), 'CHI' +# for generating a seperate .chi file by the .chm file, and 'MAN', 'RTF', +# 'XML', 'PDF' and 'PS' for the appropriate output formats. The environment +# variable DOXYGEN_PAPER_SIZE may be specified to override the default 'a4wide' +# paper size. +# +# By default, HTML, PDF and PS documentation is generated as this seems to be +# the most popular and portable combination. MAN pages created by Doxygen are +# usually problematic, though by picking an appropriate subset and doing some +# massaging they might be better than nothing. CHM and RTF are specific for MS +# (note that you can't generate both HTML and CHM at the same time). The XML is +# rather useless unless you apply specialized post-processing to it. +# +# The macro mainly controls the default state of the feature. The use can +# override the default by specifying --enable or --disable. The macros ensure +# that contradictory flags are not given (e.g., --enable-doxygen-html and +# --enable-doxygen-chm, --enable-doxygen-anything with --disable-doxygen, etc.) +# Finally, each feature will be automatically disabled (with a warning) if the +# required programs are missing. +# +# Once all the feature defaults have been specified, call DX_INIT_DOXYGEN with +# the following parameters: a one-word name for the project for use as a +# filename base etc., an optional configuration file name (the default is +# 'Doxyfile', the same as Doxygen's default), and an optional output directory +# name (the default is 'doxygen-doc'). + +## ----------## +## Defaults. ## +## ----------## + +DX_ENV="" +AC_DEFUN([DX_FEATURE_doc], ON) +AC_DEFUN([DX_FEATURE_dot], ON) +AC_DEFUN([DX_FEATURE_man], OFF) +AC_DEFUN([DX_FEATURE_html], ON) +AC_DEFUN([DX_FEATURE_chm], OFF) +AC_DEFUN([DX_FEATURE_chi], OFF) +AC_DEFUN([DX_FEATURE_rtf], OFF) +AC_DEFUN([DX_FEATURE_xml], OFF) +AC_DEFUN([DX_FEATURE_pdf], ON) +AC_DEFUN([DX_FEATURE_ps], ON) + +## --------------- ## +## Private macros. ## +## --------------- ## + +# DX_ENV_APPEND(VARIABLE, VALUE) +# ------------------------------ +# Append VARIABLE="VALUE" to DX_ENV for invoking doxygen. +AC_DEFUN([DX_ENV_APPEND], [AC_SUBST([DX_ENV], ["$DX_ENV $1='$2'"])]) + +# DX_DIRNAME_EXPR +# --------------- +# Expand into a shell expression prints the directory part of a path. +AC_DEFUN([DX_DIRNAME_EXPR], + [[expr ".$1" : '\(\.\)[^/]*$' \| "x$1" : 'x\(.*\)/[^/]*$']]) + +# DX_IF_FEATURE(FEATURE, IF-ON, IF-OFF) +# ------------------------------------- +# Expands according to the M4 (static) status of the feature. +AC_DEFUN([DX_IF_FEATURE], [ifelse(DX_FEATURE_$1, ON, [$2], [$3])]) + +# DX_REQUIRE_PROG(VARIABLE, PROGRAM) +# ---------------------------------- +# Require the specified program to be found for the DX_CURRENT_FEATURE to work. +AC_DEFUN([DX_REQUIRE_PROG], [ +AC_PATH_TOOL([$1], [$2]) +if test "$DX_FLAG_$[DX_CURRENT_FEATURE$$1]" = 1; then + AC_MSG_WARN([$2 not found - will not DX_CURRENT_DESCRIPTION]) + AC_SUBST([DX_FLAG_]DX_CURRENT_FEATURE, 0) +fi +]) + +# DX_TEST_FEATURE(FEATURE) +# ------------------------ +# Expand to a shell expression testing whether the feature is active. +AC_DEFUN([DX_TEST_FEATURE], [test "$DX_FLAG_$1" = 1]) + +# DX_CHECK_DEPEND(REQUIRED_FEATURE, REQUIRED_STATE) +# ------------------------------------------------- +# Verify that a required features has the right state before trying to turn on +# the DX_CURRENT_FEATURE. +AC_DEFUN([DX_CHECK_DEPEND], [ +test "$DX_FLAG_$1" = "$2" \ +|| AC_MSG_ERROR([doxygen-DX_CURRENT_FEATURE ifelse([$2], 1, + requires, contradicts) doxygen-DX_CURRENT_FEATURE]) +]) + +# DX_CLEAR_DEPEND(FEATURE, REQUIRED_FEATURE, REQUIRED_STATE) +# ---------------------------------------------------------- +# Turn off the DX_CURRENT_FEATURE if the required feature is off. +AC_DEFUN([DX_CLEAR_DEPEND], [ +test "$DX_FLAG_$1" = "$2" || AC_SUBST([DX_FLAG_]DX_CURRENT_FEATURE, 0) +]) + +# DX_FEATURE_ARG(FEATURE, DESCRIPTION, +# CHECK_DEPEND, CLEAR_DEPEND, +# REQUIRE, DO-IF-ON, DO-IF-OFF) +# -------------------------------------------- +# Parse the command-line option controlling a feature. CHECK_DEPEND is called +# if the user explicitly turns the feature on (and invokes DX_CHECK_DEPEND), +# otherwise CLEAR_DEPEND is called to turn off the default state if a required +# feature is disabled (using DX_CLEAR_DEPEND). REQUIRE performs additional +# requirement tests (DX_REQUIRE_PROG). Finally, an automake flag is set and +# DO-IF-ON or DO-IF-OFF are called according to the final state of the feature. +AC_DEFUN([DX_ARG_ABLE], [ + AC_DEFUN([DX_CURRENT_FEATURE], [$1]) + AC_DEFUN([DX_CURRENT_DESCRIPTION], [$2]) + AC_ARG_ENABLE(doxygen-$1, + [AS_HELP_STRING(DX_IF_FEATURE([$1], [--disable-doxygen-$1], + [--enable-doxygen-$1]), + DX_IF_FEATURE([$1], [don't $2], [$2]))], + [ +case "$enableval" in +#( +y|Y|yes|Yes|YES) + AC_SUBST([DX_FLAG_$1], 1) + $3 +;; #( +n|N|no|No|NO) + AC_SUBST([DX_FLAG_$1], 0) +;; #( +*) + AC_MSG_ERROR([invalid value '$enableval' given to doxygen-$1]) +;; +esac +], [ +AC_SUBST([DX_FLAG_$1], [DX_IF_FEATURE([$1], 1, 0)]) +$4 +]) +if DX_TEST_FEATURE([$1]); then + $5 + : +fi +if DX_TEST_FEATURE([$1]); then + AM_CONDITIONAL(DX_COND_$1, :) + $6 + : +else + AM_CONDITIONAL(DX_COND_$1, false) + $7 + : +fi +]) + +## -------------- ## +## Public macros. ## +## -------------- ## + +# DX_XXX_FEATURE(DEFAULT_STATE) +# ----------------------------- +AC_DEFUN([DX_DOXYGEN_FEATURE], [AC_DEFUN([DX_FEATURE_doc], [$1])]) +AC_DEFUN([DX_MAN_FEATURE], [AC_DEFUN([DX_FEATURE_man], [$1])]) +AC_DEFUN([DX_HTML_FEATURE], [AC_DEFUN([DX_FEATURE_html], [$1])]) +AC_DEFUN([DX_CHM_FEATURE], [AC_DEFUN([DX_FEATURE_chm], [$1])]) +AC_DEFUN([DX_CHI_FEATURE], [AC_DEFUN([DX_FEATURE_chi], [$1])]) +AC_DEFUN([DX_RTF_FEATURE], [AC_DEFUN([DX_FEATURE_rtf], [$1])]) +AC_DEFUN([DX_XML_FEATURE], [AC_DEFUN([DX_FEATURE_xml], [$1])]) +AC_DEFUN([DX_XML_FEATURE], [AC_DEFUN([DX_FEATURE_xml], [$1])]) +AC_DEFUN([DX_PDF_FEATURE], [AC_DEFUN([DX_FEATURE_pdf], [$1])]) +AC_DEFUN([DX_PS_FEATURE], [AC_DEFUN([DX_FEATURE_ps], [$1])]) + +# DX_INIT_DOXYGEN(PROJECT, [CONFIG-FILE], [OUTPUT-DOC-DIR]) +# --------------------------------------------------------- +# PROJECT also serves as the base name for the documentation files. +# The default CONFIG-FILE is "Doxyfile" and OUTPUT-DOC-DIR is "doxygen-doc". +AC_DEFUN([DX_INIT_DOXYGEN], [ + +# Files: +AC_SUBST([DX_PROJECT], [$1]) +AC_SUBST([DX_CONFIG], [ifelse([$2], [], Doxyfile, [$2])]) +AC_SUBST([DX_DOCDIR], [ifelse([$3], [], doxygen-doc, [$3])]) + +# Environment variables used inside doxygen.cfg: +DX_ENV_APPEND(SRCDIR, $srcdir) +DX_ENV_APPEND(PROJECT, $DX_PROJECT) +DX_ENV_APPEND(DOCDIR, $DX_DOCDIR) +DX_ENV_APPEND(VERSION, $PACKAGE_VERSION) + +# Doxygen itself: +DX_ARG_ABLE(doc, [generate any doxygen documentation], + [], + [], + [DX_REQUIRE_PROG([DX_DOXYGEN], doxygen) + DX_REQUIRE_PROG([DX_PERL], perl)], + [DX_ENV_APPEND(PERL_PATH, $DX_PERL)]) + +# Dot for graphics: +DX_ARG_ABLE(dot, [generate graphics for doxygen documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_DOT], dot)], + [DX_ENV_APPEND(HAVE_DOT, YES) + DX_ENV_APPEND(DOT_PATH, [`DX_DIRNAME_EXPR($DX_DOT)`])], + [DX_ENV_APPEND(HAVE_DOT, NO)]) + +# Man pages generation: +DX_ARG_ABLE(man, [generate doxygen manual pages], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_MAN, YES)], + [DX_ENV_APPEND(GENERATE_MAN, NO)]) + +# RTF file generation: +DX_ARG_ABLE(rtf, [generate doxygen RTF documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_RTF, YES)], + [DX_ENV_APPEND(GENERATE_RTF, NO)]) + +# XML file generation: +DX_ARG_ABLE(xml, [generate doxygen XML documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [], + [DX_ENV_APPEND(GENERATE_XML, YES)], + [DX_ENV_APPEND(GENERATE_XML, NO)]) + +# (Compressed) HTML help generation: +DX_ARG_ABLE(chm, [generate doxygen compressed HTML help documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_HHC], hhc)], + [DX_ENV_APPEND(HHC_PATH, $DX_HHC) + DX_ENV_APPEND(GENERATE_HTML, YES) + DX_ENV_APPEND(GENERATE_HTMLHELP, YES)], + [DX_ENV_APPEND(GENERATE_HTMLHELP, NO)]) + +# Seperate CHI file generation. +DX_ARG_ABLE(chi, [generate doxygen seperate compressed HTML help index file], + [DX_CHECK_DEPEND(chm, 1)], + [DX_CLEAR_DEPEND(chm, 1)], + [], + [DX_ENV_APPEND(GENERATE_CHI, YES)], + [DX_ENV_APPEND(GENERATE_CHI, NO)]) + +# Plain HTML pages generation: +DX_ARG_ABLE(html, [generate doxygen plain HTML documentation], + [DX_CHECK_DEPEND(doc, 1) DX_CHECK_DEPEND(chm, 0)], + [DX_CLEAR_DEPEND(doc, 1) DX_CLEAR_DEPEND(chm, 0)], + [], + [DX_ENV_APPEND(GENERATE_HTML, YES)], + [DX_TEST_FEATURE(chm) || DX_ENV_APPEND(GENERATE_HTML, NO)]) + +# PostScript file generation: +DX_ARG_ABLE(ps, [generate doxygen PostScript documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_LATEX], latex) + DX_REQUIRE_PROG([DX_MAKEINDEX], makeindex) + DX_REQUIRE_PROG([DX_DVIPS], dvips) + DX_REQUIRE_PROG([DX_EGREP], egrep)]) + +# PDF file generation: +DX_ARG_ABLE(pdf, [generate doxygen PDF documentation], + [DX_CHECK_DEPEND(doc, 1)], + [DX_CLEAR_DEPEND(doc, 1)], + [DX_REQUIRE_PROG([DX_PDFLATEX], pdflatex) + DX_REQUIRE_PROG([DX_MAKEINDEX], makeindex) + DX_REQUIRE_PROG([DX_EGREP], egrep)]) + +# LaTeX generation for PS and/or PDF: +if DX_TEST_FEATURE(ps) || DX_TEST_FEATURE(pdf); then + AM_CONDITIONAL(DX_COND_latex, :) + DX_ENV_APPEND(GENERATE_LATEX, YES) +else + AM_CONDITIONAL(DX_COND_latex, false) + DX_ENV_APPEND(GENERATE_LATEX, NO) +fi + +# Paper size for PS and/or PDF: +AC_ARG_VAR(DOXYGEN_PAPER_SIZE, + [a4wide (default), a4, letter, legal or executive]) +case "$DOXYGEN_PAPER_SIZE" in +#( +"") + AC_SUBST(DOXYGEN_PAPER_SIZE, "") +;; #( +a4wide|a4|letter|legal|executive) + DX_ENV_APPEND(PAPER_SIZE, $DOXYGEN_PAPER_SIZE) +;; #( +*) + AC_MSG_ERROR([unknown DOXYGEN_PAPER_SIZE='$DOXYGEN_PAPER_SIZE']) +;; +esac + +#For debugging: +#echo DX_FLAG_doc=$DX_FLAG_doc +#echo DX_FLAG_dot=$DX_FLAG_dot +#echo DX_FLAG_man=$DX_FLAG_man +#echo DX_FLAG_html=$DX_FLAG_html +#echo DX_FLAG_chm=$DX_FLAG_chm +#echo DX_FLAG_chi=$DX_FLAG_chi +#echo DX_FLAG_rtf=$DX_FLAG_rtf +#echo DX_FLAG_xml=$DX_FLAG_xml +#echo DX_FLAG_pdf=$DX_FLAG_pdf +#echo DX_FLAG_ps=$DX_FLAG_ps +#echo DX_ENV=$DX_ENV +]) diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/aminclude.am b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/aminclude.am new file mode 100644 index 0000000..420049e --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/aminclude.am @@ -0,0 +1,186 @@ +# Copyright (C) 2004 Oren Ben-Kiki +# This file is distributed under the same terms as the Automake macro files. + +# Generate automatic documentation using Doxygen. Goals and variables values +# are controlled by the various DX_COND_??? conditionals set by autoconf. +# +# The provided goals are: +# doxygen-doc: Generate all doxygen documentation. +# doxygen-run: Run doxygen, which will generate some of the documentation +# (HTML, CHM, CHI, MAN, RTF, XML) but will not do the post +# processing required for the rest of it (PS, PDF, and some MAN). +# doxygen-man: Rename some doxygen generated man pages. +# doxygen-ps: Generate doxygen PostScript documentation. +# doxygen-pdf: Generate doxygen PDF documentation. +# +# Note that by default these are not integrated into the automake goals. If +# doxygen is used to generate man pages, you can achieve this integration by +# setting man3_MANS to the list of man pages generated and then adding the +# dependency: +# +# $(man3_MANS): doxygen-doc +# +# This will cause make to run doxygen and generate all the documentation. +# +# The following variable is intended for use in Makefile.am: +# +# DX_CLEANFILES = everything to clean. +# +# This is usually added to MOSTLYCLEANFILES. + +## --------------------------------- ## +## Format-independent Doxygen rules. ## +## --------------------------------- ## + +if DX_COND_doc + +## ------------------------------- ## +## Rules specific for HTML output. ## +## ------------------------------- ## + +if DX_COND_html + +DX_CLEAN_HTML = @DX_DOCDIR@/html + +endif DX_COND_html + +## ------------------------------ ## +## Rules specific for CHM output. ## +## ------------------------------ ## + +if DX_COND_chm + +DX_CLEAN_CHM = @DX_DOCDIR@/chm + +if DX_COND_chi + +DX_CLEAN_CHI = @DX_DOCDIR@/@PACKAGE@.chi + +endif DX_COND_chi + +endif DX_COND_chm + +## ------------------------------ ## +## Rules specific for MAN output. ## +## ------------------------------ ## + +if DX_COND_man + +DX_CLEAN_MAN = @DX_DOCDIR@/man + +endif DX_COND_man + +## ------------------------------ ## +## Rules specific for RTF output. ## +## ------------------------------ ## + +if DX_COND_rtf + +DX_CLEAN_RTF = @DX_DOCDIR@/rtf + +endif DX_COND_rtf + +## ------------------------------ ## +## Rules specific for XML output. ## +## ------------------------------ ## + +if DX_COND_xml + +DX_CLEAN_XML = @DX_DOCDIR@/xml + +endif DX_COND_xml + +## ----------------------------- ## +## Rules specific for PS output. ## +## ----------------------------- ## + +if DX_COND_ps + +DX_CLEAN_PS = @DX_DOCDIR@/@PACKAGE@.ps + +DX_PS_GOAL = doxygen-ps + +doxygen-ps: @DX_DOCDIR@/@PACKAGE@.ps + +@DX_DOCDIR@/@PACKAGE@.ps: @DX_DOCDIR@/@PACKAGE@.tag + cd @DX_DOCDIR@/latex; \ + rm -f *.aux *.toc *.idx *.ind *.ilg *.log *.out; \ + $(DX_LATEX) refman.tex; \ + $(MAKEINDEX_PATH) refman.idx; \ + $(DX_LATEX) refman.tex; \ + countdown=5; \ + while $(DX_EGREP) 'Rerun (LaTeX|to get cross-references right)' \ + refman.log > /dev/null 2>&1 \ + && test $$countdown -gt 0; do \ + $(DX_LATEX) refman.tex; \ + countdown=`expr $$countdown - 1`; \ + done; \ + $(DX_DVIPS) -o ../@PACKAGE@.ps refman.dvi + +endif DX_COND_ps + +## ------------------------------ ## +## Rules specific for PDF output. ## +## ------------------------------ ## + +if DX_COND_pdf + +DX_CLEAN_PDF = @DX_DOCDIR@/@PACKAGE@.pdf + +DX_PDF_GOAL = doxygen-pdf + +doxygen-pdf: @DX_DOCDIR@/@PACKAGE@.pdf + +@DX_DOCDIR@/@PACKAGE@.pdf: @DX_DOCDIR@/@PACKAGE@.tag + cd @DX_DOCDIR@/latex; \ + rm -f *.aux *.toc *.idx *.ind *.ilg *.log *.out; \ + $(DX_PDFLATEX) refman.tex; \ + $(DX_MAKEINDEX) refman.idx; \ + $(DX_PDFLATEX) refman.tex; \ + countdown=5; \ + while $(DX_EGREP) 'Rerun (LaTeX|to get cross-references right)' \ + refman.log > /dev/null 2>&1 \ + && test $$countdown -gt 0; do \ + $(DX_PDFLATEX) refman.tex; \ + countdown=`expr $$countdown - 1`; \ + done; \ + mv refman.pdf ../@PACKAGE@.pdf + +endif DX_COND_pdf + +## ------------------------------------------------- ## +## Rules specific for LaTeX (shared for PS and PDF). ## +## ------------------------------------------------- ## + +if DX_COND_latex + +DX_CLEAN_LATEX = @DX_DOCDIR@/latex + +endif DX_COND_latex + +.PHONY: doxygen-run doxygen-doc $(DX_PS_GOAL) $(DX_PDF_GOAL) + +.INTERMEDIATE: doxygen-run $(DX_PS_GOAL) $(DX_PDF_GOAL) + +doxygen-run: @DX_DOCDIR@/@PACKAGE@.tag + +doxygen-doc: doxygen-run $(DX_PS_GOAL) $(DX_PDF_GOAL) + +@DX_DOCDIR@/@PACKAGE@.tag: $(DX_CONFIG) $(pkginclude_HEADERS) + rm -rf @DX_DOCDIR@ + $(DX_ENV) $(DX_DOXYGEN) $(srcdir)/$(DX_CONFIG) + +DX_CLEANFILES = \ + @DX_DOCDIR@/@PACKAGE@.tag \ + -r \ + $(DX_CLEAN_HTML) \ + $(DX_CLEAN_CHM) \ + $(DX_CLEAN_CHI) \ + $(DX_CLEAN_MAN) \ + $(DX_CLEAN_RTF) \ + $(DX_CLEAN_XML) \ + $(DX_CLEAN_PS) \ + $(DX_CLEAN_PDF) \ + $(DX_CLEAN_LATEX) + +endif DX_COND_doc diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/c-doc.Doxyfile b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/c-doc.Doxyfile new file mode 100644 index 0000000..32e09a1 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/c-doc.Doxyfile @@ -0,0 +1,1252 @@ +# Doxyfile 1.4.7 + +# This file describes the settings to be used by the documentation system +# doxygen (www.doxygen.org) for a project +# +# All text after a hash (#) is considered a comment and will be ignored +# The format is: +# TAG = value [value, ...] +# For lists items can also be appended using: +# TAG += value [value, ...] +# Values that contain spaces should be placed between quotes (" ") + +#--------------------------------------------------------------------------- +# Project related configuration options +#--------------------------------------------------------------------------- + +# The PROJECT_NAME tag is a single word (or a sequence of words surrounded +# by quotes) that should identify the project. + +PROJECT_NAME = $(PROJECT)-$(VERSION) + +# The PROJECT_NUMBER tag can be used to enter a project or revision number. +# This could be handy for archiving the generated documentation or +# if some version control system is used. + +PROJECT_NUMBER = + +# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) +# base path where the generated documentation will be put. +# If a relative path is entered, it will be relative to the location +# where doxygen was started. If left blank the current directory will be used. + +OUTPUT_DIRECTORY = $(DOCDIR) + +# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create +# 4096 sub-directories (in 2 levels) under the output directory of each output +# format and will distribute the generated files over these directories. +# Enabling this option can be useful when feeding doxygen a huge amount of +# source files, where putting all generated files in the same directory would +# otherwise cause performance problems for the file system. + +CREATE_SUBDIRS = NO + +# The OUTPUT_LANGUAGE tag is used to specify the language in which all +# documentation generated by doxygen is written. Doxygen will use this +# information to generate all constant output in the proper language. +# The default language is English, other supported languages are: +# Brazilian, Catalan, Chinese, Chinese-Traditional, Croatian, Czech, Danish, +# Dutch, Finnish, French, German, Greek, Hungarian, Italian, Japanese, +# Japanese-en (Japanese with English messages), Korean, Korean-en, Norwegian, +# Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovene, Spanish, +# Swedish, and Ukrainian. + +OUTPUT_LANGUAGE = English + +# This tag can be used to specify the encoding used in the generated output. +# The encoding is not always determined by the language that is chosen, +# but also whether or not the output is meant for Windows or non-Windows users. +# In case there is a difference, setting the USE_WINDOWS_ENCODING tag to YES +# forces the Windows encoding (this is the default for the Windows binary), +# whereas setting the tag to NO uses a Unix-style encoding (the default for +# all platforms other than Windows). + +USE_WINDOWS_ENCODING = NO + +# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will +# include brief member descriptions after the members that are listed in +# the file and class documentation (similar to JavaDoc). +# Set to NO to disable this. + +BRIEF_MEMBER_DESC = YES + +# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend +# the brief description of a member or function before the detailed description. +# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the +# brief descriptions will be completely suppressed. + +REPEAT_BRIEF = YES + +# This tag implements a quasi-intelligent brief description abbreviator +# that is used to form the text in various listings. Each string +# in this list, if found as the leading text of the brief description, will be +# stripped from the text and the result after processing the whole list, is +# used as the annotated text. Otherwise, the brief description is used as-is. +# If left blank, the following values are used ("$name" is automatically +# replaced with the name of the entity): "The $name class" "The $name widget" +# "The $name file" "is" "provides" "specifies" "contains" +# "represents" "a" "an" "the" + +ABBREVIATE_BRIEF = + +# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then +# Doxygen will generate a detailed section even if there is only a brief +# description. + +ALWAYS_DETAILED_SEC = NO + +# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all +# inherited members of a class in the documentation of that class as if those +# members were ordinary class members. Constructors, destructors and assignment +# operators of the base classes will not be shown. + +INLINE_INHERITED_MEMB = NO + +# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full +# path before files name in the file list and in the header files. If set +# to NO the shortest path that makes the file name unique will be used. + +FULL_PATH_NAMES = YES + +# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag +# can be used to strip a user-defined part of the path. Stripping is +# only done if one of the specified strings matches the left-hand part of +# the path. The tag can be used to show relative paths in the file list. +# If left blank the directory from which doxygen is run is used as the +# path to strip. + +STRIP_FROM_PATH = + +# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of +# the path mentioned in the documentation of a class, which tells +# the reader which header file to include in order to use a class. +# If left blank only the name of the header file containing the class +# definition is used. Otherwise one should specify the include paths that +# are normally passed to the compiler using the -I flag. + +STRIP_FROM_INC_PATH = + +# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter +# (but less readable) file names. This can be useful is your file systems +# doesn't support long names like on DOS, Mac, or CD-ROM. + +SHORT_NAMES = NO + +# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen +# will interpret the first line (until the first dot) of a JavaDoc-style +# comment as the brief description. If set to NO, the JavaDoc +# comments will behave just like the Qt-style comments (thus requiring an +# explicit @brief command for a brief description. + +JAVADOC_AUTOBRIEF = NO + +# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen +# treat a multi-line C++ special comment block (i.e. a block of //! or /// +# comments) as a brief description. This used to be the default behaviour. +# The new default is to treat a multi-line C++ comment block as a detailed +# description. Set this tag to YES if you prefer the old behaviour instead. + +MULTILINE_CPP_IS_BRIEF = NO + +# If the DETAILS_AT_TOP tag is set to YES then Doxygen +# will output the detailed description near the top, like JavaDoc. +# If set to NO, the detailed description appears after the member +# documentation. + +DETAILS_AT_TOP = NO + +# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented +# member inherits the documentation from any documented member that it +# re-implements. + +INHERIT_DOCS = YES + +# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce +# a new page for each member. If set to NO, the documentation of a member will +# be part of the file/class/namespace that contains it. + +SEPARATE_MEMBER_PAGES = NO + +# The TAB_SIZE tag can be used to set the number of spaces in a tab. +# Doxygen uses this value to replace tabs by spaces in code fragments. + +TAB_SIZE = 8 + +# This tag can be used to specify a number of aliases that acts +# as commands in the documentation. An alias has the form "name=value". +# For example adding "sideeffect=\par Side Effects:\n" will allow you to +# put the command \sideeffect (or @sideeffect) in the documentation, which +# will result in a user-defined paragraph with heading "Side Effects:". +# You can put \n's in the value part of an alias to insert newlines. + +ALIASES = + +# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C +# sources only. Doxygen will then generate output that is more tailored for C. +# For instance, some of the names that are used will be different. The list +# of all members will be omitted, etc. + +OPTIMIZE_OUTPUT_FOR_C = YES + +# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java +# sources only. Doxygen will then generate output that is more tailored for Java. +# For instance, namespaces will be presented as packages, qualified scopes +# will look different, etc. + +OPTIMIZE_OUTPUT_JAVA = NO + +# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want to +# include (a tag file for) the STL sources as input, then you should +# set this tag to YES in order to let doxygen match functions declarations and +# definitions whose arguments contain STL classes (e.g. func(std::string); v.s. +# func(std::string) {}). This also make the inheritance and collaboration +# diagrams that involve STL classes more complete and accurate. + +BUILTIN_STL_SUPPORT = NO + +# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC +# tag is set to YES, then doxygen will reuse the documentation of the first +# member in the group (if any) for the other members of the group. By default +# all members of a group must be documented explicitly. + +DISTRIBUTE_GROUP_DOC = NO + +# Set the SUBGROUPING tag to YES (the default) to allow class member groups of +# the same type (for instance a group of public functions) to be put as a +# subgroup of that type (e.g. under the Public Functions section). Set it to +# NO to prevent subgrouping. Alternatively, this can be done per class using +# the \nosubgrouping command. + +SUBGROUPING = YES + +#--------------------------------------------------------------------------- +# Build related configuration options +#--------------------------------------------------------------------------- + +# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in +# documentation are documented, even if no documentation was available. +# Private class members and static file members will be hidden unless +# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES + +EXTRACT_ALL = NO + +# If the EXTRACT_PRIVATE tag is set to YES all private members of a class +# will be included in the documentation. + +EXTRACT_PRIVATE = NO + +# If the EXTRACT_STATIC tag is set to YES all static members of a file +# will be included in the documentation. + +EXTRACT_STATIC = YES + +# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) +# defined locally in source files will be included in the documentation. +# If set to NO only classes defined in header files are included. + +EXTRACT_LOCAL_CLASSES = YES + +# This flag is only useful for Objective-C code. When set to YES local +# methods, which are defined in the implementation section but not in +# the interface are included in the documentation. +# If set to NO (the default) only methods in the interface are included. + +EXTRACT_LOCAL_METHODS = NO + +# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all +# undocumented members of documented classes, files or namespaces. +# If set to NO (the default) these members will be included in the +# various overviews, but no documentation section is generated. +# This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_MEMBERS = NO + +# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all +# undocumented classes that are normally visible in the class hierarchy. +# If set to NO (the default) these classes will be included in the various +# overviews. This option has no effect if EXTRACT_ALL is enabled. + +HIDE_UNDOC_CLASSES = NO + +# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all +# friend (class|struct|union) declarations. +# If set to NO (the default) these declarations will be included in the +# documentation. + +HIDE_FRIEND_COMPOUNDS = NO + +# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any +# documentation blocks found inside the body of a function. +# If set to NO (the default) these blocks will be appended to the +# function's detailed documentation block. + +HIDE_IN_BODY_DOCS = NO + +# The INTERNAL_DOCS tag determines if documentation +# that is typed after a \internal command is included. If the tag is set +# to NO (the default) then the documentation will be excluded. +# Set it to YES to include the internal documentation. + +INTERNAL_DOCS = NO + +# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate +# file names in lower-case letters. If set to YES upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# and Mac users are advised to set this option to NO. + +CASE_SENSE_NAMES = YES + +# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen +# will show members with their full class and namespace scopes in the +# documentation. If set to YES the scope will be hidden. + +HIDE_SCOPE_NAMES = NO + +# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen +# will put a list of the files that are included by a file in the documentation +# of that file. + +SHOW_INCLUDE_FILES = NO + +# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] +# is inserted in the documentation for inline members. + +INLINE_INFO = YES + +# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen +# will sort the (detailed) documentation of file and class members +# alphabetically by member name. If set to NO the members will appear in +# declaration order. + +SORT_MEMBER_DOCS = YES + +# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the +# brief documentation of file, namespace and class members alphabetically +# by member name. If set to NO (the default) the members will appear in +# declaration order. + +SORT_BRIEF_DOCS = NO + +# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be +# sorted by fully-qualified names, including namespaces. If set to +# NO (the default), the class list will be sorted only by class name, +# not including the namespace part. +# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES. +# Note: This option applies only to the class list, not to the +# alphabetical list. + +SORT_BY_SCOPE_NAME = NO + +# The GENERATE_TODOLIST tag can be used to enable (YES) or +# disable (NO) the todo list. This list is created by putting \todo +# commands in the documentation. + +GENERATE_TODOLIST = YES + +# The GENERATE_TESTLIST tag can be used to enable (YES) or +# disable (NO) the test list. This list is created by putting \test +# commands in the documentation. + +GENERATE_TESTLIST = YES + +# The GENERATE_BUGLIST tag can be used to enable (YES) or +# disable (NO) the bug list. This list is created by putting \bug +# commands in the documentation. + +GENERATE_BUGLIST = YES + +# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or +# disable (NO) the deprecated list. This list is created by putting +# \deprecated commands in the documentation. + +GENERATE_DEPRECATEDLIST = YES + +# The ENABLED_SECTIONS tag can be used to enable conditional +# documentation sections, marked by \if sectionname ... \endif. + +ENABLED_SECTIONS = + +# The MAX_INITIALIZER_LINES tag determines the maximum number of lines +# the initial value of a variable or define consists of for it to appear in +# the documentation. If the initializer consists of more lines than specified +# here it will be hidden. Use a value of 0 to hide initializers completely. +# The appearance of the initializer of individual variables and defines in the +# documentation can be controlled using \showinitializer or \hideinitializer +# command in the documentation regardless of this setting. + +MAX_INITIALIZER_LINES = 30 + +# Set the SHOW_USED_FILES tag to NO to disable the list of files generated +# at the bottom of the documentation of classes and structs. If set to YES the +# list will mention the files that were used to generate the documentation. + +SHOW_USED_FILES = YES + +# If the sources in your project are distributed over multiple directories +# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy +# in the documentation. The default is NO. + +SHOW_DIRECTORIES = NO + +# The FILE_VERSION_FILTER tag can be used to specify a program or script that +# doxygen should invoke to get the current version for each file (typically from the +# version control system). Doxygen will invoke the program by executing (via +# popen()) the command , where is the value of +# the FILE_VERSION_FILTER tag, and is the name of an input file +# provided by doxygen. Whatever the program writes to standard output +# is used as the file version. See the manual for examples. + +FILE_VERSION_FILTER = + +#--------------------------------------------------------------------------- +# configuration options related to warning and progress messages +#--------------------------------------------------------------------------- + +# The QUIET tag can be used to turn on/off the messages that are generated +# by doxygen. Possible values are YES and NO. If left blank NO is used. + +QUIET = NO + +# The WARNINGS tag can be used to turn on/off the warning messages that are +# generated by doxygen. Possible values are YES and NO. If left blank +# NO is used. + +WARNINGS = YES + +# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings +# for undocumented members. If EXTRACT_ALL is set to YES then this flag will +# automatically be disabled. + +WARN_IF_UNDOCUMENTED = YES + +# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for +# potential errors in the documentation, such as not documenting some +# parameters in a documented function, or documenting parameters that +# don't exist or using markup commands wrongly. + +WARN_IF_DOC_ERROR = YES + +# This WARN_NO_PARAMDOC option can be abled to get warnings for +# functions that are documented, but have no documentation for their parameters +# or return value. If set to NO (the default) doxygen will only warn about +# wrong or incomplete parameter documentation, but not about the absence of +# documentation. + +WARN_NO_PARAMDOC = NO + +# The WARN_FORMAT tag determines the format of the warning messages that +# doxygen can produce. The string should contain the $file, $line, and $text +# tags, which will be replaced by the file and line number from which the +# warning originated and the warning text. Optionally the format may contain +# $version, which will be replaced by the version of the file (if it could +# be obtained via FILE_VERSION_FILTER) + +WARN_FORMAT = "$file:$line: $text" + +# The WARN_LOGFILE tag can be used to specify a file to which warning +# and error messages should be written. If left blank the output is written +# to stderr. + +WARN_LOGFILE = + +#--------------------------------------------------------------------------- +# configuration options related to the input files +#--------------------------------------------------------------------------- + +# The INPUT tag can be used to specify the files and/or directories that contain +# documented source files. You may enter file names like "myfile.cpp" or +# directories like "/usr/src/myproject". Separate the files or directories +# with spaces. + +INPUT = include/zoo_queue.h + +# If the value of the INPUT tag contains directories, you can use the +# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank the following patterns are tested: +# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx +# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py + +FILE_PATTERNS = + +# The RECURSIVE tag can be used to turn specify whether or not subdirectories +# should be searched for input files as well. Possible values are YES and NO. +# If left blank NO is used. + +RECURSIVE = NO + +# The EXCLUDE tag can be used to specify files and/or directories that should +# excluded from the INPUT source files. This way you can easily exclude a +# subdirectory from a directory tree whose root is specified with the INPUT tag. + +EXCLUDE = + +# The EXCLUDE_SYMLINKS tag can be used select whether or not files or +# directories that are symbolic links (a Unix filesystem feature) are excluded +# from the input. + +EXCLUDE_SYMLINKS = NO + +# If the value of the INPUT tag contains directories, you can use the +# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude +# certain files from those directories. Note that the wildcards are matched +# against the file with absolute path, so to exclude all test directories +# for example use the pattern */test/* + +EXCLUDE_PATTERNS = + +# The EXAMPLE_PATH tag can be used to specify one or more files or +# directories that contain example code fragments that are included (see +# the \include command). + +EXAMPLE_PATH = + +# If the value of the EXAMPLE_PATH tag contains directories, you can use the +# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp +# and *.h) to filter out the source-files in the directories. If left +# blank all files are included. + +EXAMPLE_PATTERNS = + +# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be +# searched for input files to be used with the \include or \dontinclude +# commands irrespective of the value of the RECURSIVE tag. +# Possible values are YES and NO. If left blank NO is used. + +EXAMPLE_RECURSIVE = NO + +# The IMAGE_PATH tag can be used to specify one or more files or +# directories that contain image that are included in the documentation (see +# the \image command). + +IMAGE_PATH = + +# The INPUT_FILTER tag can be used to specify a program that doxygen should +# invoke to filter for each input file. Doxygen will invoke the filter program +# by executing (via popen()) the command , where +# is the value of the INPUT_FILTER tag, and is the name of an +# input file. Doxygen will then use the output that the filter program writes +# to standard output. If FILTER_PATTERNS is specified, this tag will be +# ignored. + +INPUT_FILTER = + +# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern +# basis. Doxygen will compare the file name with each pattern and apply the +# filter if there is a match. The filters are a list of the form: +# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further +# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER +# is applied to all files. + +FILTER_PATTERNS = + +# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using +# INPUT_FILTER) will be used to filter the input files when producing source +# files to browse (i.e. when SOURCE_BROWSER is set to YES). + +FILTER_SOURCE_FILES = NO + +#--------------------------------------------------------------------------- +# configuration options related to source browsing +#--------------------------------------------------------------------------- + +# If the SOURCE_BROWSER tag is set to YES then a list of source files will +# be generated. Documented entities will be cross-referenced with these sources. +# Note: To get rid of all source code in the generated output, make sure also +# VERBATIM_HEADERS is set to NO. + +SOURCE_BROWSER = NO + +# Setting the INLINE_SOURCES tag to YES will include the body +# of functions and classes directly in the documentation. + +INLINE_SOURCES = NO + +# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct +# doxygen to hide any special comment blocks from generated source code +# fragments. Normal C and C++ comments will always remain visible. + +STRIP_CODE_COMMENTS = YES + +# If the REFERENCED_BY_RELATION tag is set to YES (the default) +# then for each documented function all documented +# functions referencing it will be listed. + +REFERENCED_BY_RELATION = YES + +# If the REFERENCES_RELATION tag is set to YES (the default) +# then for each documented function all documented entities +# called/used by that function will be listed. + +REFERENCES_RELATION = YES + +# If the REFERENCES_LINK_SOURCE tag is set to YES (the default) +# and SOURCE_BROWSER tag is set to YES, then the hyperlinks from +# functions in REFERENCES_RELATION and REFERENCED_BY_RELATION lists will +# link to the source code. Otherwise they will link to the documentstion. + +REFERENCES_LINK_SOURCE = YES + +# If the USE_HTAGS tag is set to YES then the references to source code +# will point to the HTML generated by the htags(1) tool instead of doxygen +# built-in source browser. The htags tool is part of GNU's global source +# tagging system (see http://www.gnu.org/software/global/global.html). You +# will need version 4.8.6 or higher. + +USE_HTAGS = NO + +# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen +# will generate a verbatim copy of the header file for each class for +# which an include is specified. Set to NO to disable this. + +VERBATIM_HEADERS = YES + +#--------------------------------------------------------------------------- +# configuration options related to the alphabetical class index +#--------------------------------------------------------------------------- + +# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index +# of all compounds will be generated. Enable this if the project +# contains a lot of classes, structs, unions or interfaces. + +ALPHABETICAL_INDEX = NO + +# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then +# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns +# in which this list will be split (can be a number in the range [1..20]) + +COLS_IN_ALPHA_INDEX = 5 + +# In case all classes in a project start with a common prefix, all +# classes will be put under the same header in the alphabetical index. +# The IGNORE_PREFIX tag can be used to specify one or more prefixes that +# should be ignored while generating the index headers. + +IGNORE_PREFIX = + +#--------------------------------------------------------------------------- +# configuration options related to the HTML output +#--------------------------------------------------------------------------- + +# If the GENERATE_HTML tag is set to YES (the default) Doxygen will +# generate HTML output. + +GENERATE_HTML = $(GENERATE_HTML) + +# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `html' will be used as the default path. + +HTML_OUTPUT = html + +# The HTML_FILE_EXTENSION tag can be used to specify the file extension for +# each generated HTML page (for example: .htm,.php,.asp). If it is left blank +# doxygen will generate files with .html extension. + +HTML_FILE_EXTENSION = .html + +# The HTML_HEADER tag can be used to specify a personal HTML header for +# each generated HTML page. If it is left blank doxygen will generate a +# standard header. + +HTML_HEADER = + +# The HTML_FOOTER tag can be used to specify a personal HTML footer for +# each generated HTML page. If it is left blank doxygen will generate a +# standard footer. + +HTML_FOOTER = + +# The HTML_STYLESHEET tag can be used to specify a user-defined cascading +# style sheet that is used by each HTML page. It can be used to +# fine-tune the look of the HTML output. If the tag is left blank doxygen +# will generate a default style sheet. Note that doxygen will try to copy +# the style sheet file to the HTML output directory, so don't put your own +# stylesheet in the HTML output directory as well, or it will be erased! + +HTML_STYLESHEET = + +# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, +# files or namespaces will be aligned in HTML using tables. If set to +# NO a bullet list will be used. + +HTML_ALIGN_MEMBERS = YES + +# If the GENERATE_HTMLHELP tag is set to YES, additional index files +# will be generated that can be used as input for tools like the +# Microsoft HTML help workshop to generate a compressed HTML help file (.chm) +# of the generated HTML documentation. + +GENERATE_HTMLHELP = $(GENERATE_HTMLHELP) + +# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can +# be used to specify the file name of the resulting .chm file. You +# can add a path in front of the file if the result should not be +# written to the html output directory. + +CHM_FILE = ../$(PROJECT).chm + +# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can +# be used to specify the location (absolute path including file name) of +# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run +# the HTML help compiler on the generated index.hhp. + +HHC_LOCATION = $(HHC_PATH) + +# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag +# controls if a separate .chi index file is generated (YES) or that +# it should be included in the master .chm file (NO). + +GENERATE_CHI = $(GENERATE_CHI) + +# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag +# controls whether a binary table of contents is generated (YES) or a +# normal table of contents (NO) in the .chm file. + +BINARY_TOC = NO + +# The TOC_EXPAND flag can be set to YES to add extra items for group members +# to the contents of the HTML help documentation and to the tree view. + +TOC_EXPAND = NO + +# The DISABLE_INDEX tag can be used to turn on/off the condensed index at +# top of each HTML page. The value NO (the default) enables the index and +# the value YES disables it. + +DISABLE_INDEX = NO + +# This tag can be used to set the number of enum values (range [1..20]) +# that doxygen will group on one line in the generated HTML documentation. + +ENUM_VALUES_PER_LINE = 4 + +# If the GENERATE_TREEVIEW tag is set to YES, a side panel will be +# generated containing a tree-like index structure (just like the one that +# is generated for HTML Help). For this to work a browser that supports +# JavaScript, DHTML, CSS and frames is required (for instance Mozilla 1.0+, +# Netscape 6.0+, Internet explorer 5.0+, or Konqueror). Windows users are +# probably better off using the HTML help feature. + +GENERATE_TREEVIEW = NO + +# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be +# used to set the initial width (in pixels) of the frame in which the tree +# is shown. + +TREEVIEW_WIDTH = 250 + +#--------------------------------------------------------------------------- +# configuration options related to the LaTeX output +#--------------------------------------------------------------------------- + +# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will +# generate Latex output. + +GENERATE_LATEX = $(GENERATE_LATEX) + +# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `latex' will be used as the default path. + +LATEX_OUTPUT = latex + +# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be +# invoked. If left blank `latex' will be used as the default command name. + +LATEX_CMD_NAME = latex + +# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to +# generate index for LaTeX. If left blank `makeindex' will be used as the +# default command name. + +MAKEINDEX_CMD_NAME = makeindex + +# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact +# LaTeX documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_LATEX = NO + +# The PAPER_TYPE tag can be used to set the paper type that is used +# by the printer. Possible values are: a4, a4wide, letter, legal and +# executive. If left blank a4wide will be used. + +PAPER_TYPE = $(PAPER_SIZE) + +# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX +# packages that should be included in the LaTeX output. + +EXTRA_PACKAGES = + +# The LATEX_HEADER tag can be used to specify a personal LaTeX header for +# the generated latex document. The header should contain everything until +# the first chapter. If it is left blank doxygen will generate a +# standard header. Notice: only use this tag if you know what you are doing! + +LATEX_HEADER = + +# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated +# is prepared for conversion to pdf (using ps2pdf). The pdf file will +# contain links (just like the HTML output) instead of page references +# This makes the output suitable for online browsing using a pdf viewer. + +PDF_HYPERLINKS = NO + +# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of +# plain latex in the generated Makefile. Set this option to YES to get a +# higher quality PDF documentation. + +USE_PDFLATEX = $(GENERATE_PDF) + +# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. +# command to the generated LaTeX files. This will instruct LaTeX to keep +# running if errors occur, instead of asking the user for help. +# This option is also used when generating formulas in HTML. + +LATEX_BATCHMODE = NO + +# If LATEX_HIDE_INDICES is set to YES then doxygen will not +# include the index chapters (such as File Index, Compound Index, etc.) +# in the output. + +LATEX_HIDE_INDICES = NO + +#--------------------------------------------------------------------------- +# configuration options related to the RTF output +#--------------------------------------------------------------------------- + +# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output +# The RTF output is optimized for Word 97 and may not look very pretty with +# other RTF readers or editors. + +GENERATE_RTF = $(GENERATE_RTF) + +# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `rtf' will be used as the default path. + +RTF_OUTPUT = rtf + +# If the COMPACT_RTF tag is set to YES Doxygen generates more compact +# RTF documents. This may be useful for small projects and may help to +# save some trees in general. + +COMPACT_RTF = NO + +# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated +# will contain hyperlink fields. The RTF file will +# contain links (just like the HTML output) instead of page references. +# This makes the output suitable for online browsing using WORD or other +# programs which support those fields. +# Note: wordpad (write) and others do not support links. + +RTF_HYPERLINKS = NO + +# Load stylesheet definitions from file. Syntax is similar to doxygen's +# config file, i.e. a series of assignments. You only have to provide +# replacements, missing definitions are set to their default value. + +RTF_STYLESHEET_FILE = + +# Set optional variables used in the generation of an rtf document. +# Syntax is similar to doxygen's config file. + +RTF_EXTENSIONS_FILE = + +#--------------------------------------------------------------------------- +# configuration options related to the man page output +#--------------------------------------------------------------------------- + +# If the GENERATE_MAN tag is set to YES (the default) Doxygen will +# generate man pages + +GENERATE_MAN = $(GENERATE_MAN) + +# The MAN_OUTPUT tag is used to specify where the man pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `man' will be used as the default path. + +MAN_OUTPUT = man + +# The MAN_EXTENSION tag determines the extension that is added to +# the generated man pages (default is the subroutine's section .3) + +MAN_EXTENSION = .3 + +# If the MAN_LINKS tag is set to YES and Doxygen generates man output, +# then it will generate one additional man file for each entity +# documented in the real man page(s). These additional files +# only source the real man page, but without them the man command +# would be unable to find the correct page. The default is NO. + +MAN_LINKS = NO + +#--------------------------------------------------------------------------- +# configuration options related to the XML output +#--------------------------------------------------------------------------- + +# If the GENERATE_XML tag is set to YES Doxygen will +# generate an XML file that captures the structure of +# the code including all documentation. + +GENERATE_XML = $(GENERATE_XML) + +# The XML_OUTPUT tag is used to specify where the XML pages will be put. +# If a relative path is entered the value of OUTPUT_DIRECTORY will be +# put in front of it. If left blank `xml' will be used as the default path. + +XML_OUTPUT = xml + +# The XML_SCHEMA tag can be used to specify an XML schema, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_SCHEMA = + +# The XML_DTD tag can be used to specify an XML DTD, +# which can be used by a validating XML parser to check the +# syntax of the XML files. + +XML_DTD = + +# If the XML_PROGRAMLISTING tag is set to YES Doxygen will +# dump the program listings (including syntax highlighting +# and cross-referencing information) to the XML output. Note that +# enabling this will significantly increase the size of the XML output. + +XML_PROGRAMLISTING = YES + +#--------------------------------------------------------------------------- +# configuration options for the AutoGen Definitions output +#--------------------------------------------------------------------------- + +# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will +# generate an AutoGen Definitions (see autogen.sf.net) file +# that captures the structure of the code including all +# documentation. Note that this feature is still experimental +# and incomplete at the moment. + +GENERATE_AUTOGEN_DEF = NO + +#--------------------------------------------------------------------------- +# configuration options related to the Perl module output +#--------------------------------------------------------------------------- + +# If the GENERATE_PERLMOD tag is set to YES Doxygen will +# generate a Perl module file that captures the structure of +# the code including all documentation. Note that this +# feature is still experimental and incomplete at the +# moment. + +GENERATE_PERLMOD = NO + +# If the PERLMOD_LATEX tag is set to YES Doxygen will generate +# the necessary Makefile rules, Perl scripts and LaTeX code to be able +# to generate PDF and DVI output from the Perl module output. + +PERLMOD_LATEX = NO + +# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be +# nicely formatted so it can be parsed by a human reader. This is useful +# if you want to understand what is going on. On the other hand, if this +# tag is set to NO the size of the Perl module output will be much smaller +# and Perl will parse it just the same. + +PERLMOD_PRETTY = YES + +# The names of the make variables in the generated doxyrules.make file +# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. +# This is useful so different doxyrules.make files included by the same +# Makefile don't overwrite each other's variables. + +PERLMOD_MAKEVAR_PREFIX = + +#--------------------------------------------------------------------------- +# Configuration options related to the preprocessor +#--------------------------------------------------------------------------- + +# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will +# evaluate all C-preprocessor directives found in the sources and include +# files. + +ENABLE_PREPROCESSING = YES + +# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro +# names in the source code. If set to NO (the default) only conditional +# compilation will be performed. Macro expansion can be done in a controlled +# way by setting EXPAND_ONLY_PREDEF to YES. + +MACRO_EXPANSION = NO + +# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES +# then the macro expansion is limited to the macros specified with the +# PREDEFINED and EXPAND_AS_DEFINED tags. + +EXPAND_ONLY_PREDEF = NO + +# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files +# in the INCLUDE_PATH (see below) will be search if a #include is found. + +SEARCH_INCLUDES = YES + +# The INCLUDE_PATH tag can be used to specify one or more directories that +# contain include files that are not input files but should be processed by +# the preprocessor. + +INCLUDE_PATH = + +# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard +# patterns (like *.h and *.hpp) to filter out the header-files in the +# directories. If left blank, the patterns specified with FILE_PATTERNS will +# be used. + +INCLUDE_FILE_PATTERNS = + +# The PREDEFINED tag can be used to specify one or more macro names that +# are defined before the preprocessor is started (similar to the -D option of +# gcc). The argument of the tag is a list of macros of the form: name +# or name=definition (no spaces). If the definition and the = are +# omitted =1 is assumed. To prevent a macro definition from being +# undefined via #undef or recursively expanded use the := operator +# instead of the = operator. + +PREDEFINED = + +# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then +# this tag can be used to specify a list of macro names that should be expanded. +# The macro definition that is found in the sources will be used. +# Use the PREDEFINED tag if you want to use a different macro definition. + +EXPAND_AS_DEFINED = + +# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then +# doxygen's preprocessor will remove all function-like macros that are alone +# on a line, have an all uppercase name, and do not end with a semicolon. Such +# function macros are typically used for boiler-plate code, and will confuse +# the parser if not removed. + +SKIP_FUNCTION_MACROS = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to external references +#--------------------------------------------------------------------------- + +# The TAGFILES option can be used to specify one or more tagfiles. +# Optionally an initial location of the external documentation +# can be added for each tagfile. The format of a tag file without +# this location is as follows: +# TAGFILES = file1 file2 ... +# Adding location for the tag files is done as follows: +# TAGFILES = file1=loc1 "file2 = loc2" ... +# where "loc1" and "loc2" can be relative or absolute paths or +# URLs. If a location is present for each tag, the installdox tool +# does not have to be run to correct the links. +# Note that each tag file must have a unique name +# (where the name does NOT include the path) +# If a tag file is not located in the directory in which doxygen +# is run, you must also specify the path to the tagfile here. + +TAGFILES = + +# When a file name is specified after GENERATE_TAGFILE, doxygen will create +# a tag file that is based on the input files it reads. + +GENERATE_TAGFILE = $(DOCDIR)/$(PROJECT).tag + +# If the ALLEXTERNALS tag is set to YES all external classes will be listed +# in the class index. If set to NO only the inherited external classes +# will be listed. + +ALLEXTERNALS = NO + +# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed +# in the modules index. If set to NO, only the current project's groups will +# be listed. + +EXTERNAL_GROUPS = YES + +# The PERL_PATH should be the absolute path and name of the perl script +# interpreter (i.e. the result of `which perl'). + +PERL_PATH = /usr/bin/perl + +#--------------------------------------------------------------------------- +# Configuration options related to the dot tool +#--------------------------------------------------------------------------- + +# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will +# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base +# or super classes. Setting the tag to NO turns the diagrams off. Note that +# this option is superseded by the HAVE_DOT option below. This is only a +# fallback. It is recommended to install and use dot, since it yields more +# powerful graphs. + +CLASS_DIAGRAMS = YES + +# If set to YES, the inheritance and collaboration graphs will hide +# inheritance and usage relations if the target is undocumented +# or is not a class. + +HIDE_UNDOC_RELATIONS = YES + +# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is +# available from the path. This tool is part of Graphviz, a graph visualization +# toolkit from AT&T and Lucent Bell Labs. The other options in this section +# have no effect if this option is set to NO (the default) + +HAVE_DOT = $(HAVE_DOT) + +# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect inheritance relations. Setting this tag to YES will force the +# the CLASS_DIAGRAMS tag to NO. + +CLASS_GRAPH = YES + +# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for each documented class showing the direct and +# indirect implementation dependencies (inheritance, containment, and +# class references variables) of the class with other documented classes. + +COLLABORATION_GRAPH = YES + +# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen +# will generate a graph for groups, showing the direct groups dependencies + +GROUP_GRAPHS = YES + +# If the UML_LOOK tag is set to YES doxygen will generate inheritance and +# collaboration diagrams in a style similar to the OMG's Unified Modeling +# Language. + +UML_LOOK = NO + +# If set to YES, the inheritance and collaboration graphs will show the +# relations between templates and their instances. + +TEMPLATE_RELATIONS = NO + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT +# tags are set to YES then doxygen will generate a graph for each documented +# file showing the direct and indirect include dependencies of the file with +# other documented files. + +INCLUDE_GRAPH = YES + +# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and +# HAVE_DOT tags are set to YES then doxygen will generate a graph for each +# documented header file showing the documented files that directly or +# indirectly include this file. + +INCLUDED_BY_GRAPH = YES + +# If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a call dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable call graphs for selected +# functions only using the \callgraph command. + +CALL_GRAPH = NO + +# If the CALLER_GRAPH and HAVE_DOT tags are set to YES then doxygen will +# generate a caller dependency graph for every global function or class method. +# Note that enabling this option will significantly increase the time of a run. +# So in most cases it will be better to enable caller graphs for selected +# functions only using the \callergraph command. + +CALLER_GRAPH = NO + +# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen +# will graphical hierarchy of all classes instead of a textual one. + +GRAPHICAL_HIERARCHY = YES + +# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES +# then doxygen will show the dependencies a directory has on other directories +# in a graphical way. The dependency relations are determined by the #include +# relations between the files in the directories. + +DIRECTORY_GRAPH = YES + +# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images +# generated by dot. Possible values are png, jpg, or gif +# If left blank png will be used. + +DOT_IMAGE_FORMAT = png + +# The tag DOT_PATH can be used to specify the path where the dot tool can be +# found. If left blank, it is assumed the dot tool can be found in the path. + +DOT_PATH = $(DOT_PATH) + +# The DOTFILE_DIRS tag can be used to specify one or more directories that +# contain dot files that are included in the documentation (see the +# \dotfile command). + +DOTFILE_DIRS = + +# The MAX_DOT_GRAPH_WIDTH tag can be used to set the maximum allowed width +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_WIDTH = 1024 + +# The MAX_DOT_GRAPH_HEIGHT tag can be used to set the maximum allows height +# (in pixels) of the graphs generated by dot. If a graph becomes larger than +# this value, doxygen will try to truncate the graph, so that it fits within +# the specified constraint. Beware that most browsers cannot cope with very +# large images. + +MAX_DOT_GRAPH_HEIGHT = 1024 + +# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the +# graphs generated by dot. A depth value of 3 means that only nodes reachable +# from the root by following a path via at most 3 edges will be shown. Nodes +# that lay further from the root node will be omitted. Note that setting this +# option to 1 or 2 may greatly reduce the computation time needed for large +# code bases. Also note that a graph may be further truncated if the graph's +# image dimensions are not sufficient to fit the graph (see MAX_DOT_GRAPH_WIDTH +# and MAX_DOT_GRAPH_HEIGHT). If 0 is used for the depth value (the default), +# the graph is not depth-constrained. + +MAX_DOT_GRAPH_DEPTH = 0 + +# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent +# background. This is disabled by default, which results in a white background. +# Warning: Depending on the platform used, enabling this option may lead to +# badly anti-aliased labels on the edges of a graph (i.e. they become hard to +# read). + +DOT_TRANSPARENT = NO + +# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output +# files in one run (i.e. multiple -o and -T options on the command line). This +# makes dot run faster, but since only newer versions of dot (>1.8.10) +# support this, this feature is disabled by default. + +DOT_MULTI_TARGETS = NO + +# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will +# generate a legend page explaining the meaning of the various boxes and +# arrows in the dot generated graphs. + +GENERATE_LEGEND = YES + +# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will +# remove the intermediate dot files that are used to generate +# the various graphs. + +DOT_CLEANUP = YES + +#--------------------------------------------------------------------------- +# Configuration::additions related to the search engine +#--------------------------------------------------------------------------- + +# The SEARCHENGINE tag specifies whether or not a search engine should be +# used. If set to NO the values of all tags below this one will be ignored. + +SEARCHENGINE = NO diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/configure.ac b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/configure.ac new file mode 100644 index 0000000..ede2480 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/configure.ac @@ -0,0 +1,82 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_PREREQ(2.59) + +AC_INIT([zooqueue], [3.2.0]) + +AC_CONFIG_SRCDIR([include/zoo_queue.h]) + +PACKAGE=zooqueue +VERSION=1.0 + +AC_SUBST(PACKAGE) +AC_SUBST(VERSION) + +BUILD_PATH="`pwd`" + +# Checks for programs. +AC_LANG_CPLUSPLUS + +AM_INIT_AUTOMAKE([-Wall foreign]) +# Checks for libraries. + +#initialize Doxygen support +DX_HTML_FEATURE(ON) +DX_CHM_FEATURE(OFF) +DX_CHI_FEATURE(OFF) +DX_MAN_FEATURE(OFF) +DX_RTF_FEATURE(OFF) +DX_XML_FEATURE(OFF) +DX_PDF_FEATURE(OFF) +DX_PS_FEATURE(OFF) +DX_INIT_DOXYGEN([zookeeper-queues],[c-doc.Doxyfile],[docs]) + + +ZOOKEEPER_PATH=${BUILD_PATH}/../../../../../zookeeper-client/zookeeper-client-c +ZOOKEEPER_LD=-L${BUILD_PATH}/../../../../../zookeeper-client/zookeeper-client-c\ -lzookeeper_mt + +AC_SUBST(ZOOKEEPER_PATH) +AC_SUBST(ZOOKEEPER_LD) + +# Checks for header files. +AC_HEADER_DIRENT +AC_HEADER_STDC +AC_CHECK_HEADERS([fcntl.h stdlib.h string.h sys/time.h unistd.h]) + +# Checks for typedefs, structures, and compiler characteristics. +AC_HEADER_STDBOOL +AC_C_CONST +AC_TYPE_UID_T +AC_C_INLINE +AC_TYPE_OFF_T +AC_TYPE_SIZE_T +AC_STRUCT_ST_BLOCKS +AC_HEADER_TIME +AC_C_VOLATILE +AC_PROG_CC +AC_PROG_LIBTOOL +#check for cppunit +AM_PATH_CPPUNIT(1.10.2) +# Checks for library functions. +AC_FUNC_UTIME_NULL +AC_CHECK_FUNCS([gettimeofday memset mkdir rmdir strdup strerror strstr strtol strtoul strtoull utime]) + +AC_CONFIG_FILES([Makefile]) +AC_OUTPUT +AC_C_VOLATILE diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/include/zoo_queue.h b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/include/zoo_queue.h new file mode 100644 index 0000000..dccc763 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/include/zoo_queue.h @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifndef ZOOKEEPER_QUEUE_H_ +#define ZOOKEEPER_QUEUE_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * \file zoo_queue.h + * \brief zookeeper recipe for queues. + */ + + +struct zkr_queue { + zhandle_t *zh; + char *path; + struct ACL_vector *acl; + pthread_mutex_t pmutex; + char *node_name; + int node_name_length; + char *cached_create_path; +}; + +typedef struct zkr_queue zkr_queue_t; + + +/** + * \brief initializes a zookeeper queue + * + * this method instantiates a zookeeper queue + * \param queue the zookeeper queue to initialize + * \param zh the zookeeper handle to use + * \param path the path in zookeeper to use for the queue + * \param acl the acl to use in zookeeper. + * \return return 0 if successful. + */ +ZOOAPI int zkr_queue_init(zkr_queue_t *queue, zhandle_t* zh, char* path, struct ACL_vector *acl); + +/** + * \brief adds an element to a zookeeper queue + * + * this method adds an element to the back of a zookeeper queue. + * \param queue the zookeeper queue to add the element to + * \param data a pointer to a data buffer + * \param buffer_len the length of the buffer + * \return returns 0 (ZOK) if successful, otherwise returns a zookeeper error code. + */ +ZOOAPI int zkr_queue_offer(zkr_queue_t *queue, const char *data, int buffer_len); + +/** + * \brief returns the head of a zookeeper queue + * + * this method returns the head of a zookeeper queue without removing it. + * \param queue the zookeeper queue to add the element to + * \param buffer a pointer to a data buffer + * \param buffer_len a pointer to the length of the buffer + * \return returns 0 (ZOK) and sets *buffer_len to the length of data written if successful (-1 if the queue is empty). Otherwise it will set *buffer_len to -1 and return a zookeeper error code. + */ +ZOOAPI int zkr_queue_element(zkr_queue_t *queue, char *buffer, int *buffer_len); + +/** + * \brief returns the head of a zookeeper queue + * + * this method returns the head of a zookeeper queue without removing it. + * \param queue the zookeeper queue to get the head of + * \param buffer a pointer to a data buffer + * \param buffer_len a pointer to the length of the buffer + * \return returns 0 (ZOK) and sets *buffer_len to the length of data written if successful (-1 if the queue is empty). Otherwise it will set *buffer_len to -1 and return a zookeeper error code. + */ +ZOOAPI int zkr_queue_remove(zkr_queue_t *queue, char *buffer, int *buffer_len); + +/** + * \brief removes and returns the head of a zookeeper queue, blocks if necessary + * + * this method returns the head of a zookeeper queue without removing it. + * \param queue the zookeeper queue to remove and return the head of + * \param buffer a pointer to a data buffer + * \param buffer_len a pointer to the length of the buffer + * \return returns 0 (ZOK) and sets *buffer_len to the length of data written if successful. Otherwise it will set *buffer_len to -1 and return a zookeeper error code. + */ +ZOOAPI int zkr_queue_take(zhandle_t *zh, zkr_queue_t *queue, char *buffer, int *buffer_len); + +/** + * \brief destroys a zookeeper queue structure + * + * this destroys a zookeeper queue structure, this is only a local operation and will not affect + * the state of the queue on the zookeeper server. + * \param queue the zookeeper queue to destroy + */ +void zkr_queue_destroy(zkr_queue_t *queue); + + +#ifdef __cplusplus +} +#endif +#endif //ZOOKEEPER_QUEUE_H_ diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/src/zoo_queue.c b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/src/zoo_queue.c new file mode 100644 index 0000000..d7cc570 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/src/zoo_queue.c @@ -0,0 +1,442 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#ifdef DLL_EXPORT +#define USE_STATIC_LIB +#endif + +#if defined(__CYGWIN__) +#define USE_IPV6 +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SYS_UTSNAME_H +#include +#endif + +#ifdef HAVE_GETPWUID_R +#include +#endif + +#define IF_DEBUG(x) if (logLevel==ZOO_LOG_LEVEL_DEBUG) {x;} + + +static void free_String_vector(struct String_vector *v) { + if (v->data) { + int32_t i; + for (i=0; icount; i++) { + free(v->data[i]); + } + free(v->data); + v->data = 0; + } +} + + +static int vstrcmp(const void* str1, const void* str2) { + const char **a = (const char**)str1; + const char **b = (const char**) str2; + return strcmp(*a, *b); +} + +static void sort_children(struct String_vector *vector) { + qsort( vector->data, vector->count, sizeof(char*), &vstrcmp); +} + + +static void concat_path_nodename_n(char *buffer, int len, const char *path, const char *node_name){ + snprintf(buffer, len, "%s/%s", path, node_name); +} + +static char *concat_path_nodename(const char *path, const char *node_name){ + int node_path_length = strlen(path) + 1+ strlen(node_name) +1; + char *node_path = (char *) malloc(node_path_length * sizeof(char)); + concat_path_nodename_n(node_path, node_path_length, path, node_name); + return node_path; +} + + +static void zkr_queue_cache_create_path(zkr_queue_t *queue){ + if(queue->cached_create_path != NULL){ + free(queue->cached_create_path); + } + queue->cached_create_path = concat_path_nodename(queue->path, queue->node_name); +} + +ZOOAPI int zkr_queue_init(zkr_queue_t *queue, zhandle_t* zh, char* path, struct ACL_vector *acl){ + queue->zh = zh; + queue->path = path; + queue->node_name = "qn-"; + queue->node_name_length = strlen(queue->node_name); + queue->cached_create_path = NULL; + queue->acl = acl; + pthread_mutex_init(&(queue->pmutex), NULL); + zkr_queue_cache_create_path(queue); + return 0; +} + +static ZOOAPI int create_queue_root(zkr_queue_t *queue){ + return zoo_create(queue->zh, queue->path, NULL, 0, queue->acl, 0, NULL, 0 ); +} + +static int valid_child_name(zkr_queue_t *queue, const char *child_name){ + return strncmp(queue->node_name, child_name, queue->node_name_length); +} + +ZOOAPI int zkr_queue_offer(zkr_queue_t *queue, const char *data, int buffer_len){ + for(;;){ + int rc = zoo_create(queue->zh, queue->cached_create_path, data, buffer_len, queue->acl, ZOO_SEQUENCE, NULL, 0 ); + switch(rc){ + int create_root_rc; + case ZNONODE: + create_root_rc = create_queue_root(queue); + switch(create_root_rc){ + case ZNODEEXISTS: + case ZOK: + break; + default: + return create_root_rc; + } + break; + default: + return rc; + } + } +} + + +ZOOAPI int zkr_queue_element(zkr_queue_t *queue, char *buffer, int *buffer_len){ + int path_length = strlen(queue->path); + for(;;){ + struct String_vector stvector; + struct String_vector *vector = &stvector; + /*Get sorted children*/ + int get_children_rc = zoo_get_children(queue->zh, queue->path, 0, vector); + switch(get_children_rc){ + case ZOK: + break; + case ZNONODE: + *buffer_len = -1; + return ZOK; + default: + return get_children_rc; + } + if(stvector.count == 0){ + *buffer_len = -1; + return ZOK; + } + + sort_children(vector); + /*try all*/ + int i; + for(i=0; i < stvector.count; i++){ + char *child_name = stvector.data[i]; + int child_path_length = path_length + 1 + strlen(child_name) +1; + char child_path[child_path_length]; + concat_path_nodename_n(child_path, child_path_length, queue->path, child_name); + int get_rc = zoo_get(queue->zh, child_path, 0, buffer, buffer_len, NULL); + switch(get_rc){ + case ZOK: + free_String_vector(vector); + return ZOK; + case ZNONODE: + break; + default: + free_String_vector(vector); + return get_rc; + } + } + + free_String_vector(vector); + } +} + +ZOOAPI int zkr_queue_remove(zkr_queue_t *queue, char *buffer, int *buffer_len){ + int path_length = strlen(queue->path); + for(;;){ + struct String_vector stvector; + struct String_vector *vector = &stvector; + /*Get sorted children*/ + int get_children_rc = zoo_get_children(queue->zh, queue->path, 0, &stvector); + switch(get_children_rc){ + case ZOK: + break; + case ZNONODE: + *buffer_len = -1; + return ZOK; + + default: + *buffer_len = -1; + return get_children_rc; + } + if(stvector.count == 0){ + *buffer_len = -1; + return ZOK; + } + + sort_children(vector); + /*try all*/ + int i; + for( i=0; i < stvector.count; i++){ + char *child_name = stvector.data[i]; + int child_path_length = path_length + 1 + strlen(child_name) +1; + char child_path[child_path_length]; + concat_path_nodename_n(child_path, child_path_length, queue->path, child_name); + int get_rc = zoo_get(queue->zh, child_path, 0, buffer, buffer_len, NULL); + switch(get_rc){ + int delete_rc; + case ZOK: + delete_rc = zoo_delete(queue->zh, child_path, -1); + switch(delete_rc){ + case ZOK: + free_String_vector(vector); + return delete_rc; + case ZNONODE: + break; + default: + free_String_vector(vector); + *buffer_len = -1; + return delete_rc; + } + break; + case ZNONODE: + break; + default: + free_String_vector(vector); + *buffer_len = -1; + return get_rc; + } + } + free_String_vector(vector); + } +} + +/** + * The take_latch structure roughly emulates a Java CountdownLatch with 1 as the initial value. + * It is meant to be used by a setter thread and a waiter thread. + * + * This latch is specialized to be used with the queue, all latches created for the same queue structure will use the same mutex. + * + * The setter thread at some point will call take_latch_setter_trigger_latch() on the thread. + * + * The waiter thread creates the latch and at some point either calls take_latch_waiter_await()s or take_latch_waiter_mark_unneeded()s it. + * The await function will return after the setter thread has triggered the latch. + * The mark unneeded function will return immediately and avoid some unneeded initialization. + * + * Whichever thread is last to call their required function disposes of the latch. + * + * The latch may disposed if no threads will call the waiting, marking, or triggering functions using take_latch_destroy_syncrhonized(). + */ + +struct take_latch { + enum take_state {take_init, take_waiting, take_triggered, take_not_needed} state; + pthread_cond_t latch_condition; + zkr_queue_t *queue; +}; + + +typedef struct take_latch take_latch_t; + + +static void take_latch_init( take_latch_t *latch, zkr_queue_t *queue){ + pthread_mutex_t *mutex = &(queue->pmutex); + pthread_mutex_lock(mutex); + latch->state = take_init; + latch->queue = queue; + pthread_mutex_unlock(mutex); +} + +static take_latch_t *create_take_latch(zkr_queue_t *queue){ + take_latch_t *new_take_latch = (take_latch_t *) malloc(sizeof(take_latch_t)); + take_latch_init(new_take_latch, queue); + return new_take_latch; +} + + +//Only call this when you own the mutex +static void take_latch_destroy_unsafe(take_latch_t *latch){ + if(latch->state == take_waiting){ + pthread_cond_destroy(&(latch->latch_condition)); + } + free(latch); +} + +static void take_latch_destroy_synchronized(take_latch_t *latch){ + pthread_mutex_t *mutex = &(latch->queue->pmutex); + pthread_mutex_lock(mutex); + take_latch_destroy_unsafe(latch); + pthread_mutex_unlock(mutex); +} + +static void take_latch_setter_trigger_latch(zhandle_t *zh, take_latch_t *latch){ + pthread_mutex_t *mutex = &(latch->queue->pmutex); + pthread_mutex_lock(mutex); + switch(latch->state){ + case take_init: + latch->state = take_triggered; + break; + case take_not_needed: + take_latch_destroy_unsafe(latch); + break; + case take_triggered: + LOG_DEBUG(LOGCALLBACK(zh), ("Error! Latch was triggered twice.")); + break; + case take_waiting: + pthread_cond_signal(&(latch->latch_condition)); + break; + } + pthread_mutex_unlock(mutex); +} + +static void take_latch_waiter_await(zhandle_t *zh, take_latch_t *latch){ + pthread_mutex_t *mutex = &(latch->queue->pmutex); + pthread_mutex_lock(mutex); + switch(latch->state){ + case take_init: + pthread_cond_init(&(latch->latch_condition),NULL); + latch->state = take_waiting; + pthread_cond_wait(&(latch->latch_condition),mutex); + take_latch_destroy_unsafe(latch); + break; + case take_waiting: + LOG_DEBUG(LOGCALLBACK(zh), ("Error! Called await twice.")); + break; + case take_not_needed: + LOG_DEBUG(LOGCALLBACK(zh), ("Error! Waiting after marking not needed.")); + break; + case take_triggered: + take_latch_destroy_unsafe(latch); + break; + } + pthread_mutex_unlock(mutex); +} + +static void take_latch_waiter_mark_unneeded(zhandle_t *zh, take_latch_t *latch){ + pthread_mutex_t *mutex = &(latch->queue->pmutex); + pthread_mutex_lock(mutex); + switch(latch->state){ + case take_init: + latch->state = take_not_needed; + break; + case take_waiting: + LOG_DEBUG(LOGCALLBACK(zh), ("Error! Can't mark unneeded after waiting.")); + break; + case take_not_needed: + LOG_DEBUG(LOGCALLBACK(zh), ("Marked unneeded twice.")); + break; + case take_triggered: + take_latch_destroy_unsafe(latch); + break; + } + pthread_mutex_unlock(mutex); +} + +static void take_watcher(zhandle_t *zh, int type, int state, const char *path, void *watcherCtx){ + take_latch_t *latch = (take_latch_t *) watcherCtx; + take_latch_setter_trigger_latch(zh, latch); +} + + + +ZOOAPI int zkr_queue_take(zhandle_t *zh, zkr_queue_t *queue, char *buffer, int *buffer_len){ + int path_length = strlen(queue->path); +take_attempt: + for(;;){ + struct String_vector stvector; + struct String_vector *vector = &stvector; + /*Get sorted children*/ + take_latch_t *take_latch = create_take_latch(queue); + int get_children_rc = zoo_wget_children(queue->zh, queue->path, take_watcher, take_latch, &stvector); + switch(get_children_rc){ + case ZOK: + break; + int create_queue_rc; + case ZNONODE: + take_latch_destroy_synchronized(take_latch); + create_queue_rc = create_queue_root(queue); + switch(create_queue_rc){ + case ZNODEEXISTS: + case ZOK: + goto take_attempt; + default: + *buffer_len = -1; + return create_queue_rc; + } + default: + take_latch_destroy_synchronized(take_latch); + *buffer_len = -1; + return get_children_rc; + } + if(stvector.count == 0){ + take_latch_waiter_await(zh, take_latch); + }else{ + take_latch_waiter_mark_unneeded(zh, take_latch); + } + + sort_children(vector); + /*try all*/ + int i; + for( i=0; i < stvector.count; i++){ + char *child_name = stvector.data[i]; + int child_path_length = path_length + 1 + strlen(child_name) +1; + char child_path[child_path_length]; + concat_path_nodename_n(child_path, child_path_length, queue->path, child_name); + int get_rc = zoo_get(queue->zh, child_path, 0, buffer, buffer_len, NULL); + switch(get_rc){ + int delete_rc; + case ZOK: + delete_rc = zoo_delete(queue->zh, child_path, -1); + switch(delete_rc){ + case ZOK: + free_String_vector(vector); + return delete_rc; + case ZNONODE: + break; + default: + free_String_vector(vector); + *buffer_len = -1; + return delete_rc; + } + break; + case ZNONODE: + break; + default: + free_String_vector(vector); + *buffer_len = -1; + return get_rc; + } + } + free_String_vector(vector); + } +} + +ZOOAPI void zkr_queue_destroy(zkr_queue_t *queue){ + pthread_mutex_destroy(&(queue->pmutex)); + if(queue->cached_create_path != NULL){ + free(queue->cached_create_path); + } +} diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/tests/TestClient.cc b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/tests/TestClient.cc new file mode 100644 index 0000000..5446d9b --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/tests/TestClient.cc @@ -0,0 +1,452 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include + +#include +#include +#include +#include +#include + + +using namespace std; + +#include +#include + +#include +#include + +static void yield(zhandle_t *zh, int i) +{ + sleep(i); +} + +typedef struct evt { + string path; + int type; +} evt_t; + +typedef struct watchCtx { +private: + list events; +public: + bool connected; + zhandle_t *zh; + + watchCtx() { + connected = false; + zh = 0; + } + ~watchCtx() { + if (zh) { + zookeeper_close(zh); + zh = 0; + } + } + + evt_t getEvent() { + evt_t evt; + evt = events.front(); + events.pop_front(); + return evt; + } + + int countEvents() { + int count; + count = events.size(); + return count; + } + + void putEvent(evt_t evt) { + events.push_back(evt); + } + + bool waitForConnected(zhandle_t *zh) { + time_t expires = time(0) + 10; + while(!connected && time(0) < expires) { + yield(zh, 1); + } + return connected; + } + bool waitForDisconnected(zhandle_t *zh) { + time_t expires = time(0) + 15; + while(connected && time(0) < expires) { + yield(zh, 1); + } + return !connected; + } +} watchctx_t; + +extern "C" { + + const char *thread_test_string="Hello World!"; + + void *offer_thread_shared_queue(void *queue_handle){ + zkr_queue_t *queue = (zkr_queue_t *) queue_handle; + + int test_string_buffer_length = strlen(thread_test_string) + 1; + int offer_rc = zkr_queue_offer(queue, thread_test_string, test_string_buffer_length); + pthread_exit(NULL); + } + + void *take_thread_shared_queue(void *queue_handle){ + zkr_queue_t *queue = (zkr_queue_t *) queue_handle; + + int test_string_buffer_length = strlen(thread_test_string) + 1; + int receive_buffer_capacity = test_string_buffer_length; + int receive_buffer_length = receive_buffer_capacity; + char *receive_buffer = (char *) malloc(sizeof(char) * receive_buffer_capacity); + + int remove_rc = zkr_queue_take(queue, receive_buffer, &receive_buffer_length); + switch(remove_rc){ + case ZOK: + pthread_exit(receive_buffer); + default: + free(receive_buffer); + pthread_exit(NULL); + } + } + + int valid_test_string(void *result){ + char *result_string = (char *) result; + return !strncmp(result_string, thread_test_string, strlen(thread_test_string)); + } +} + +class Zookeeper_queuetest : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(Zookeeper_queuetest); + CPPUNIT_TEST(testInitDestroy); + CPPUNIT_TEST(testOffer1); + CPPUNIT_TEST(testOfferRemove1); + CPPUNIT_TEST(testOfferRemove2); + CPPUNIT_TEST(testOfferRemove3); + CPPUNIT_TEST(testOfferRemove4); + CPPUNIT_TEST(testOfferRemove5); + CPPUNIT_TEST(testOfferRemove6); + CPPUNIT_TEST(testOfferTake1); + CPPUNIT_TEST(testOfferTake2); + CPPUNIT_TEST(testOfferTake3); + CPPUNIT_TEST(testOfferTake4); + CPPUNIT_TEST(testOfferTake5); + CPPUNIT_TEST(testOfferTake6); + CPPUNIT_TEST_SUITE_END(); + + static void watcher(zhandle_t *, int type, int state, const char *path,void*v){ + watchctx_t *ctx = (watchctx_t*)v; + + if (state == ZOO_CONNECTED_STATE) { + ctx->connected = true; + } else { + ctx->connected = false; + } + if (type != ZOO_SESSION_EVENT) { + evt_t evt; + evt.path = path; + evt.type = type; + ctx->putEvent(evt); + } + } + + static const char hostPorts[]; + + const char *getHostPorts() { + return hostPorts; + } + + zhandle_t *createClient(watchctx_t *ctx) { + zhandle_t *zk = zookeeper_init(hostPorts, watcher, 10000, 0, + ctx, 0); + ctx->zh = zk; + sleep(1); + return zk; + } + +public: + +#define ZKSERVER_CMD "./tests/zkServer.sh" + + void setUp() + { + char cmd[1024]; + sprintf(cmd, "%s startClean %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + + void startServer() { + char cmd[1024]; + sprintf(cmd, "%s start %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void stopServer() { + tearDown(); + } + + void tearDown() + { + char cmd[1024]; + sprintf(cmd, "%s stop %s", ZKSERVER_CMD, getHostPorts()); + CPPUNIT_ASSERT(system(cmd) == 0); + } + + void initializeQueuesAndHandles(int num_clients, zhandle_t *zoohandles[], + watchctx_t ctxs[], zkr_queue_t queues[], char *path){ + int i; + for(i=0; i< num_clients; i++){ + zoohandles[i] = createClient(&ctxs[i]); + zkr_queue_init(&queues[i], zoohandles[i], path, &ZOO_OPEN_ACL_UNSAFE); + } + } + + void cleanUpQueues(int num_clients, zkr_queue_t queues[]){ + int i; + for(i=0; i < num_clients; i++){ + zkr_queue_destroy(&queues[i]); + } + } + + void testInitDestroy(){ + int num_clients = 1; + watchctx_t ctxs[num_clients]; + zhandle_t *zoohandles[num_clients]; + zkr_queue_t queues[num_clients]; + char *path= (char *)"/testInitDestroy"; + + int i; + for(i=0; i< num_clients; i++){ + zoohandles[i] = createClient(&ctxs[i]); + zkr_queue_init(&queues[i], zoohandles[i], path, &ZOO_OPEN_ACL_UNSAFE); + } + + for(i=0; i< num_clients; i++){ + zkr_queue_destroy(&queues[i]); + } + + } + + void testOffer1(){ + int num_clients = 1; + watchctx_t ctxs[num_clients]; + zhandle_t *zoohandles[num_clients]; + zkr_queue_t queues[num_clients]; + char *path= (char *)"/testOffer1"; + + initializeQueuesAndHandles(num_clients, zoohandles, ctxs, queues, path); + + const char *test_string="Hello World!"; + int test_string_length = strlen(test_string); + int test_string_buffer_length = test_string_length + 1; + char buffer[test_string_buffer_length]; + + int offer_rc = zkr_queue_offer(&queues[0], test_string, test_string_buffer_length); + CPPUNIT_ASSERT(offer_rc == ZOK); + + int removed_element_buffer_length = test_string_buffer_length; + int remove_rc = zkr_queue_remove(&queues[0], buffer, &removed_element_buffer_length); + CPPUNIT_ASSERT(remove_rc == ZOK); + CPPUNIT_ASSERT(removed_element_buffer_length == test_string_buffer_length); + CPPUNIT_ASSERT(strncmp(test_string,buffer,test_string_length)==0); + + cleanUpQueues(num_clients,queues); + } + + void create_n_remove_m(char *path, int n, int m){ + int num_clients = 2; + watchctx_t ctxs[num_clients]; + zhandle_t *zoohandles[num_clients]; + zkr_queue_t queues[num_clients]; + + initializeQueuesAndHandles(num_clients, zoohandles, ctxs, queues, path); + + int i; + int max_digits = sizeof(int)*3; + const char *test_string = "Hello World!"; + int buffer_length = strlen(test_string) + max_digits + 1; + char correct_buffer[buffer_length]; + char receive_buffer[buffer_length]; + + for(i = 0; i < n; i++){ + snprintf(correct_buffer, buffer_length, "%s%d", test_string,i); + int offer_rc = zkr_queue_offer(&queues[0], correct_buffer, buffer_length); + CPPUNIT_ASSERT(offer_rc == ZOK); + } + printf("Offers\n"); + for(i=0; i=n){ + CPPUNIT_ASSERT(receive_buffer_length == -1); + }else{ + CPPUNIT_ASSERT(strncmp(correct_buffer,receive_buffer, buffer_length)==0); + } + } + + cleanUpQueues(num_clients,queues); + } + + void testOfferRemove1(){ + create_n_remove_m((char *)"/testOfferRemove1", 0,1); + } + + void testOfferRemove2(){ + create_n_remove_m((char *)"/testOfferRemove2", 1,1); + } + + void testOfferRemove3(){ + create_n_remove_m((char *)"/testOfferRemove3", 10,1); + } + + void testOfferRemove4(){ + create_n_remove_m((char *)"/testOfferRemove4", 10,10); + } + + void testOfferRemove5(){ + create_n_remove_m((char *)"/testOfferRemove5", 10,5); + } + + void testOfferRemove6(){ + create_n_remove_m((char *)"/testOfferRemove6", 10,11); + } + + void create_n_take_m(char *path, int n, int m){ + CPPUNIT_ASSERT(m<=n); + int num_clients = 2; + watchctx_t ctxs[num_clients]; + zhandle_t *zoohandles[num_clients]; + zkr_queue_t queues[num_clients]; + + initializeQueuesAndHandles(num_clients, zoohandles, ctxs, queues, path); + + int i; + int max_digits = sizeof(int)*3; + const char *test_string = "Hello World!"; + int buffer_length = strlen(test_string) + max_digits + 1; + char correct_buffer[buffer_length]; + char receive_buffer[buffer_length]; + + for(i = 0; i < n; i++){ + snprintf(correct_buffer, buffer_length, "%s%d", test_string,i); + int offer_rc = zkr_queue_offer(&queues[0], correct_buffer, buffer_length); + CPPUNIT_ASSERT(offer_rc == ZOK); + } + printf("Offers\n"); + for(i=0; i=n){ + CPPUNIT_ASSERT(receive_buffer_length == -1); + }else{ + CPPUNIT_ASSERT(strncmp(correct_buffer,receive_buffer, buffer_length)==0); + } + } + + cleanUpQueues(num_clients,queues); + } + + void testOfferTake1(){ + create_n_take_m((char *)"/testOfferTake1", 2,1); + } + + void testOfferTake2(){ + create_n_take_m((char *)"/testOfferTake2", 1,1); + } + + void testOfferTake3(){ + create_n_take_m((char *)"/testOfferTake3", 10,1); + } + + void testOfferTake4(){ + create_n_take_m((char *)"/testOfferTake4", 10,10); + } + + void testOfferTake5(){ + create_n_take_m((char *)"/testOfferTake5", 10,5); + } + + void testOfferTake6(){ + create_n_take_m((char *)"/testOfferTake6", 12,11); + } + + void testTakeThreaded(){ + int num_clients = 1; + watchctx_t ctxs[num_clients]; + zhandle_t *zoohandles[num_clients]; + zkr_queue_t queues[num_clients]; + char *path=(char *)"/testTakeThreaded"; + + initializeQueuesAndHandles(num_clients, zoohandles, ctxs, queues, path); + pthread_t take_thread; + + pthread_create(&take_thread, NULL, take_thread_shared_queue, (void *) &queues[0]); + + usleep(1000); + + pthread_t offer_thread; + pthread_create(&offer_thread, NULL, offer_thread_shared_queue, (void *) &queues[0]); + pthread_join(offer_thread, NULL); + + void *take_thread_result; + pthread_join(take_thread, &take_thread_result); + CPPUNIT_ASSERT(take_thread_result != NULL); + CPPUNIT_ASSERT(valid_test_string(take_thread_result)); + + cleanUpQueues(num_clients,queues); + } + + void testTakeThreaded2(){ + int num_clients = 1; + watchctx_t ctxs[num_clients]; + zhandle_t *zoohandles[num_clients]; + zkr_queue_t queues[num_clients]; + char *path=(char *)"/testTakeThreaded2"; + + initializeQueuesAndHandles(num_clients, zoohandles, ctxs, queues, path); + + int take_attempts; + int num_take_attempts = 2; + for(take_attempts=0; take_attempts < num_take_attempts; take_attempts++){ + pthread_t take_thread; + + pthread_create(&take_thread, NULL, take_thread_shared_queue, (void *) &queues[0]); + + usleep(1000); + + pthread_t offer_thread; + pthread_create(&offer_thread, NULL, offer_thread_shared_queue, (void *) &queues[0]); + pthread_join(offer_thread, NULL); + + void *take_thread_result; + pthread_join(take_thread, &take_thread_result); + CPPUNIT_ASSERT(take_thread_result != NULL); + CPPUNIT_ASSERT(valid_test_string(take_thread_result)); + + } + cleanUpQueues(num_clients,queues); + } +}; + +const char Zookeeper_queuetest::hostPorts[] = "127.0.0.1:22181"; +CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_queuetest); diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/tests/TestDriver.cc b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/tests/TestDriver.cc new file mode 100644 index 0000000..2b818f4 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/c/tests/TestDriver.cc @@ -0,0 +1,114 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Util.h" + +using namespace std; + +CPPUNIT_NS_BEGIN + +class EclipseOutputter: public CompilerOutputter +{ +public: + EclipseOutputter(TestResultCollector *result,ostream &stream): + CompilerOutputter(result,stream,"%p:%l: "),stream_(stream) + { + } + virtual void printFailedTestName( TestFailure *failure ){} + virtual void printFailureMessage( TestFailure *failure ) + { + stream_<<": "; + Message msg = failure->thrownException()->message(); + stream_<< msg.shortDescription(); + + string text; + for(int i=0; i the output must be in the compiler error format. + //bool selfTest = (argc > 1) && (std::string("-ide") == argv[1]); + globalTestConfig.addConfigFromCmdLine(argc,argv); + + // Create the event manager and test controller + CPPUNIT_NS::TestResult controller; + // Add a listener that colllects test result + CPPUNIT_NS::TestResultCollector result; + controller.addListener( &result ); + + // Add a listener that print dots as tests run. + // CPPUNIT_NS::TextTestProgressListener progress; + CPPUNIT_NS::BriefTestProgressListener progress; + controller.addListener( &progress ); + + CPPUNIT_NS::TestRunner runner; + runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() ); + + try + { + cout << "Running " << globalTestConfig.getTestName(); + runner.run( controller, globalTestConfig.getTestName()); + cout< +#include +#include + +// number of elements in array +#define COUNTOF(array) sizeof(array)/sizeof(array[0]) + +#define DECLARE_WRAPPER(ret,sym,sig) \ + extern "C" ret __real_##sym sig; \ + extern "C" ret __wrap_##sym sig + +#define CALL_REAL(sym,params) \ + __real_##sym params + +// must include "src/zookeeper_log.h" to be able to use this macro +#define TEST_TRACE(x) \ + log_message(3,__LINE__,__func__,format_log_message x) + +extern const std::string EMPTY_STRING; + +// ***************************************************************************** +// A bit of wizardry to get to the bare type from a reference or a pointer +// to the type +template +struct TypeOp { + typedef T BareT; + typedef T ArgT; +}; + +// partial specialization for reference types +template +struct TypeOp{ + typedef T& ArgT; + typedef typename TypeOp::BareT BareT; +}; + +// partial specialization for pointers +template +struct TypeOp{ + typedef T* ArgT; + typedef typename TypeOp::BareT BareT; +}; + +// ***************************************************************************** +// Container utilities + +template +void putValue(std::map& map,const K& k, const V& v){ + typedef std::map Map; + typename Map::const_iterator it=map.find(k); + if(it==map.end()) + map.insert(typename Map::value_type(k,v)); + else + map[k]=v; +} + +template +bool getValue(const std::map& map,const K& k,V& v){ + typedef std::map Map; + typename Map::const_iterator it=map.find(k); + if(it==map.end()) + return false; + v=it->second; + return true; +} + +// ***************************************************************************** +// misc utils + +// millisecond sleep +void millisleep(int ms); +// evaluate given predicate until it returns true or the timeout +// (in millis) has expired +template +int ensureCondition(const Predicate& p,int timeout){ + int elapsed=0; + while(!p() && elapsed CmdLineOptList; +public: + typedef CmdLineOptList::const_iterator const_iterator; + TestConfig(){} + ~TestConfig(){} + void addConfigFromCmdLine(int argc, char* argv[]){ + if(argc>=2) + testName_=argv[1]; + for(int i=2; i /tmp/zk.log & + echo $! > /tmp/zk.pid + sleep 5 + ;; +stop) + # Already killed above + ;; +*) + echo "Unknown command " + $1 + exit 2 +esac + diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/main/java/org/apache/zookeeper/recipes/queue/DistributedQueue.java b/zookeeper-recipes/zookeeper-recipes-queue/src/main/java/org/apache/zookeeper/recipes/queue/DistributedQueue.java new file mode 100644 index 0000000..0839cf5 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/main/java/org/apache/zookeeper/recipes/queue/DistributedQueue.java @@ -0,0 +1,302 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.queue; + +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.TreeMap; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A protocol to implement a distributed queue. + */ +public class DistributedQueue { + + private static final Logger LOG = LoggerFactory.getLogger(DistributedQueue.class); + + private final String dir; + + private ZooKeeper zookeeper; + private List acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; + + private final String prefix = "qn-"; + + public DistributedQueue(ZooKeeper zookeeper, String dir, List acl) { + this.dir = dir; + + if (acl != null) { + this.acl = acl; + } + this.zookeeper = zookeeper; + + } + + /** + * Returns a Map of the children, ordered by id. + * @param watcher optional watcher on getChildren() operation. + * @return map from id to child name for all children + */ + private Map orderedChildren(Watcher watcher) throws KeeperException, InterruptedException { + Map orderedChildren = new TreeMap<>(); + + List childNames; + childNames = zookeeper.getChildren(dir, watcher); + + for (String childName : childNames) { + try { + //Check format + if (!childName.regionMatches(0, prefix, 0, prefix.length())) { + LOG.warn("Found child node with improper name: {}", childName); + continue; + } + String suffix = childName.substring(prefix.length()); + Long childId = Long.parseLong(suffix); + orderedChildren.put(childId, childName); + } catch (NumberFormatException e) { + LOG.warn("Found child node with improper format : {}", childName, e); + } + } + + return orderedChildren; + } + + /** + * Find the smallest child node. + * @return The name of the smallest child node. + */ + private String smallestChildName() throws KeeperException, InterruptedException { + long minId = Long.MAX_VALUE; + String minName = ""; + + List childNames; + + try { + childNames = zookeeper.getChildren(dir, false); + } catch (KeeperException.NoNodeException e) { + LOG.warn("Unexpected exception", e); + return null; + } + + for (String childName : childNames) { + try { + //Check format + if (!childName.regionMatches(0, prefix, 0, prefix.length())) { + LOG.warn("Found child node with improper name: {}", childName); + continue; + } + String suffix = childName.substring(prefix.length()); + long childId = Long.parseLong(suffix); + if (childId < minId) { + minId = childId; + minName = childName; + } + } catch (NumberFormatException e) { + LOG.warn("Found child node with improper format : {}", childName, e); + } + } + + if (minId < Long.MAX_VALUE) { + return minName; + } else { + return null; + } + } + + /** + * Return the head of the queue without modifying the queue. + * @return the data at the head of the queue. + * @throws NoSuchElementException + * @throws KeeperException + * @throws InterruptedException + */ + public byte[] element() throws NoSuchElementException, KeeperException, InterruptedException { + Map orderedChildren; + + // element, take, and remove follow the same pattern. + // We want to return the child node with the smallest sequence number. + // Since other clients are remove()ing and take()ing nodes concurrently, + // the child with the smallest sequence number in orderedChildren might be gone by the time we check. + // We don't call getChildren again until we have tried the rest of the nodes in sequence order. + while (true) { + try { + orderedChildren = orderedChildren(null); + } catch (KeeperException.NoNodeException e) { + throw new NoSuchElementException(); + } + if (orderedChildren.size() == 0) { + throw new NoSuchElementException(); + } + + for (String headNode : orderedChildren.values()) { + if (headNode != null) { + try { + return zookeeper.getData(dir + "/" + headNode, false, null); + } catch (KeeperException.NoNodeException e) { + //Another client removed the node first, try next + } + } + } + + } + } + + /** + * Attempts to remove the head of the queue and return it. + * @return The former head of the queue + * @throws NoSuchElementException + * @throws KeeperException + * @throws InterruptedException + */ + public byte[] remove() throws NoSuchElementException, KeeperException, InterruptedException { + Map orderedChildren; + // Same as for element. Should refactor this. + while (true) { + try { + orderedChildren = orderedChildren(null); + } catch (KeeperException.NoNodeException e) { + throw new NoSuchElementException(); + } + if (orderedChildren.size() == 0) { + throw new NoSuchElementException(); + } + + for (String headNode : orderedChildren.values()) { + String path = dir + "/" + headNode; + try { + byte[] data = zookeeper.getData(path, false, null); + zookeeper.delete(path, -1); + return data; + } catch (KeeperException.NoNodeException e) { + // Another client deleted the node first. + } + } + + } + } + + private static class LatchChildWatcher implements Watcher { + + CountDownLatch latch; + + public LatchChildWatcher() { + latch = new CountDownLatch(1); + } + + public void process(WatchedEvent event) { + LOG.debug("Watcher fired: {}", event); + latch.countDown(); + } + public void await() throws InterruptedException { + latch.await(); + } + + } + + /** + * Removes the head of the queue and returns it, blocks until it succeeds. + * @return The former head of the queue + * @throws NoSuchElementException + * @throws KeeperException + * @throws InterruptedException + */ + public byte[] take() throws KeeperException, InterruptedException { + Map orderedChildren; + // Same as for element. Should refactor this. + while (true) { + LatchChildWatcher childWatcher = new LatchChildWatcher(); + try { + orderedChildren = orderedChildren(childWatcher); + } catch (KeeperException.NoNodeException e) { + zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT); + continue; + } + if (orderedChildren.size() == 0) { + childWatcher.await(); + continue; + } + + for (String headNode : orderedChildren.values()) { + String path = dir + "/" + headNode; + try { + byte[] data = zookeeper.getData(path, false, null); + zookeeper.delete(path, -1); + return data; + } catch (KeeperException.NoNodeException e) { + // Another client deleted the node first. + } + } + } + } + + /** + * Inserts data into queue. + * @param data + * @return true if data was successfully added + */ + public boolean offer(byte[] data) throws KeeperException, InterruptedException { + for (; ; ) { + try { + zookeeper.create(dir + "/" + prefix, data, acl, CreateMode.PERSISTENT_SEQUENTIAL); + return true; + } catch (KeeperException.NoNodeException e) { + zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT); + } + } + + } + + /** + * Returns the data at the first element of the queue, or null if the queue is empty. + * @return data at the first element of the queue, or null. + * @throws KeeperException + * @throws InterruptedException + */ + public byte[] peek() throws KeeperException, InterruptedException { + try { + return element(); + } catch (NoSuchElementException e) { + return null; + } + } + + /** + * Attempts to remove the head of the queue and return it. Returns null if the queue is empty. + * @return Head of the queue or null. + * @throws KeeperException + * @throws InterruptedException + */ + public byte[] poll() throws KeeperException, InterruptedException { + try { + return remove(); + } catch (NoSuchElementException e) { + return null; + } + } + +} diff --git a/zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java b/zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java new file mode 100644 index 0000000..7382920 --- /dev/null +++ b/zookeeper-recipes/zookeeper-recipes-queue/src/test/java/org/apache/zookeeper/recipes/queue/DistributedQueueTest.java @@ -0,0 +1,269 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.recipes.queue; + +import java.util.NoSuchElementException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link DistributedQueue}. + */ +public class DistributedQueueTest extends ClientBase { + + @After + public void tearDown() throws Exception { + super.tearDown(); + } + + @Test + public void testOffer1() throws Exception { + String dir = "/testOffer1"; + String testString = "Hello World"; + final int numClients = 1; + ZooKeeper[] clients = new ZooKeeper[numClients]; + DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + queueHandles[0].offer(testString.getBytes()); + + byte[] dequeuedBytes = queueHandles[0].remove(); + Assert.assertEquals(new String(dequeuedBytes), testString); + } + + @Test + public void testOffer2() throws Exception { + String dir = "/testOffer2"; + String testString = "Hello World"; + final int numClients = 2; + ZooKeeper[] clients = new ZooKeeper[numClients]; + DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + queueHandles[0].offer(testString.getBytes()); + + byte[] dequeuedBytes = queueHandles[1].remove(); + Assert.assertEquals(new String(dequeuedBytes), testString); + } + + @Test + public void testTake1() throws Exception { + String dir = "/testTake1"; + String testString = "Hello World"; + final int numClients = 1; + ZooKeeper[] clients = new ZooKeeper[numClients]; + DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + queueHandles[0].offer(testString.getBytes()); + + byte[] dequeuedBytes = queueHandles[0].take(); + Assert.assertEquals(new String(dequeuedBytes), testString); + } + + @Test + public void testRemove1() throws Exception { + String dir = "/testRemove1"; + final int numClients = 1; + ZooKeeper[] clients = new ZooKeeper[numClients]; + DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + try { + queueHandles[0].remove(); + } catch (NoSuchElementException e) { + return; + } + + Assert.fail(); + } + + public void createNremoveMtest(String dir, int n, int m) throws Exception { + String testString = "Hello World"; + final int numClients = 2; + ZooKeeper[] clients = new ZooKeeper[numClients]; + DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + for (int i = 0; i < n; i++) { + String offerString = testString + i; + queueHandles[0].offer(offerString.getBytes()); + } + + byte[] data = null; + for (int i = 0; i < m; i++) { + data = queueHandles[1].remove(); + } + + Assert.assertNotNull(data); + Assert.assertEquals(new String(data), testString + (m - 1)); + } + + @Test + public void testRemove2() throws Exception { + createNremoveMtest("/testRemove2", 10, 2); + } + @Test + public void testRemove3() throws Exception { + createNremoveMtest("/testRemove3", 1000, 1000); + } + + public void createNremoveMelementTest(String dir, int n, int m) throws Exception { + String testString = "Hello World"; + final int numClients = 2; + ZooKeeper[] clients = new ZooKeeper[numClients]; + DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + for (int i = 0; i < n; i++) { + String offerString = testString + i; + queueHandles[0].offer(offerString.getBytes()); + } + + for (int i = 0; i < m; i++) { + queueHandles[1].remove(); + } + Assert.assertEquals(new String(queueHandles[1].element()), testString + m); + } + + @Test + public void testElement1() throws Exception { + createNremoveMelementTest("/testElement1", 1, 0); + } + + @Test + public void testElement2() throws Exception { + createNremoveMelementTest("/testElement2", 10, 2); + } + + @Test + public void testElement3() throws Exception { + createNremoveMelementTest("/testElement3", 1000, 500); + } + + @Test + public void testElement4() throws Exception { + createNremoveMelementTest("/testElement4", 1000, 1000 - 1); + } + + @Test + public void testTakeWait1() throws Exception { + String dir = "/testTakeWait1"; + final String testString = "Hello World"; + final int numClients = 1; + final ZooKeeper[] clients = new ZooKeeper[numClients]; + final DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + + final byte[][] takeResult = new byte[1][]; + Thread takeThread = new Thread(() -> { + try { + takeResult[0] = queueHandles[0].take(); + } catch (KeeperException | InterruptedException ignore) { + // no op + } + }); + takeThread.start(); + + Thread.sleep(1000); + Thread offerThread = new Thread(() -> { + try { + queueHandles[0].offer(testString.getBytes()); + } catch (KeeperException | InterruptedException ignore) { + // no op + } + }); + offerThread.start(); + offerThread.join(); + + takeThread.join(); + + Assert.assertNotNull(takeResult[0]); + Assert.assertEquals(new String(takeResult[0]), testString); + } + + @Test + public void testTakeWait2() throws Exception { + String dir = "/testTakeWait2"; + final String testString = "Hello World"; + final int numClients = 1; + final ZooKeeper[] clients = new ZooKeeper[numClients]; + final DistributedQueue[] queueHandles = new DistributedQueue[numClients]; + for (int i = 0; i < clients.length; i++) { + clients[i] = createClient(); + queueHandles[i] = new DistributedQueue(clients[i], dir, null); + } + int numAttempts = 2; + for (int i = 0; i < numAttempts; i++) { + final byte[][] takeResult = new byte[1][]; + final String threadTestString = testString + i; + Thread takeThread = new Thread(() -> { + try { + takeResult[0] = queueHandles[0].take(); + } catch (KeeperException | InterruptedException ignore) { + // no op + } + }); + takeThread.start(); + + Thread.sleep(1000); + Thread offerThread = new Thread(() -> { + try { + queueHandles[0].offer(threadTestString.getBytes()); + } catch (KeeperException | InterruptedException ignore) { + // no op + } + }); + offerThread.start(); + offerThread.join(); + + takeThread.join(); + + Assert.assertNotNull(takeResult[0]); + Assert.assertEquals(new String(takeResult[0]), threadTestString); + } + } + +} + diff --git a/zookeeper-server/pom.xml b/zookeeper-server/pom.xml new file mode 100644 index 0000000..a58d23d --- /dev/null +++ b/zookeeper-server/pom.xml @@ -0,0 +1,292 @@ + + + + 4.0.0 + + org.apache.zookeeper + parent + 3.6.3 + .. + + + zookeeper + jar + Apache ZooKeeper - Server + ZooKeeper server + + + + com.github.spotbugs + spotbugs-annotations + provided + true + + + org.hamcrest + hamcrest-all + test + + + commons-collections + commons-collections + test + + + org.apache.zookeeper + zookeeper-jute + ${project.version} + + + commons-cli + commons-cli + provided + + + org.apache.yetus + audience-annotations + + + io.netty + netty-handler + + + io.netty + netty-transport-native-epoll + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-log4j12 + + + org.eclipse.jetty + jetty-server + provided + + + org.eclipse.jetty + jetty-servlet + provided + + + com.fasterxml.jackson.core + jackson-databind + provided + + + com.googlecode.json-simple + json-simple + provided + + + org.bouncycastle + bcprov-jdk15on + test + + + org.bouncycastle + bcpkix-jdk15on + test + + + jline + jline + provided + + + io.dropwizard.metrics + metrics-core + provided + + + log4j + log4j + + + org.apache.kerby + kerb-core + test + + + org.apache.kerby + kerb-simplekdc + test + + + org.apache.kerby + kerby-config + test + + + org.mockito + mockito-core + test + + + org.jmockit + jmockit + test + + + junit + junit + test + + + org.xerial.snappy + snappy-java + provided + + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + tbuild-time + + timestamp-property + + + build.time + MM/dd/yyyy HH:mm zz + en_US + GMT + + + + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources/java + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + pre-compile-vergen + generate-sources + + + org/apache/zookeeper/version/**/*.java + + + + compile + + + + + + org.codehaus.mojo + exec-maven-plugin + + + generate-version-info + generate-sources + + exec + + + ${project.basedir}/src/main/java/ + java + + -classpath + + org.apache.zookeeper.version.util.VerGen + ${project.version} + ${mvngit.commit.id} + ${build.time} + ${project.basedir}/target/generated-sources/java + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/lib + false + true + false + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*Test.java + + ${surefire-forkcount} + false + -Xmx512m -Dtest.junit.threads=${surefire-forkcount} -Dzookeeper.junit.threadid=${surefire.forkNumber} -javaagent:${org.jmockit:jmockit:jar} + ${project.basedir} + true + + ${project.build.directory}/surefire + super:D/InIHSb7yEEbrWz8b9l71RjZJU= + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + publish-test-jar + + test-jar + + + + + + + + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/AddWatchMode.java b/zookeeper-server/src/main/java/org/apache/zookeeper/AddWatchMode.java new file mode 100644 index 0000000..0f339c1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/AddWatchMode.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * Modes available to {@link ZooKeeper#addWatch(String, Watcher, AddWatchMode)} + */ +public enum AddWatchMode { + /** + *

      + * Set a watcher on the given path that does not get removed when triggered (i.e. it stays active + * until it is removed). This watcher + * is triggered for both data and child events. To remove the watcher, use + * removeWatches() with WatcherType.Any. The watcher behaves as if you placed an exists() watch and + * a getData() watch on the ZNode at the given path. + *

      + */ + PERSISTENT(ZooDefs.AddWatchModes.persistent), + + /** + *

      + * Set a watcher on the given path that: a) does not get removed when triggered (i.e. it stays active + * until it is removed); b) applies not only to the registered path but all child paths recursively. This watcher + * is triggered for both data and child events. To remove the watcher, use + * removeWatches() with WatcherType.Any + *

      + * + *

      + * The watcher behaves as if you placed an exists() watch and + * a getData() watch on the ZNode at the given path and any ZNodes that are children + * of the given path including children added later. + *

      + * + *

      + * NOTE: when there are active recursive watches there is a small performance decrease as all segments + * of ZNode paths must be checked for watch triggering. + *

      + */ + PERSISTENT_RECURSIVE(ZooDefs.AddWatchModes.persistentRecursive) + ; + + public int getMode() { + return mode; + } + + private final int mode; + + AddWatchMode(int mode) { + this.mode = mode; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/AsyncCallback.java b/zookeeper-server/src/main/java/org/apache/zookeeper/AsyncCallback.java new file mode 100644 index 0000000..918d118 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/AsyncCallback.java @@ -0,0 +1,351 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.List; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; + +/** + * Interface definitions of asynchronous callbacks. + * + *

      ZooKeeper provides asynchronous version as equivalent to synchronous APIs. + * + *

      An asynchronous callback is deferred to invoke after a function returns. + * Asynchronous calls usually improve system efficiency on IO-related APIs. + * + *

      It is highly recommended NOT to perform any blocking operation inside + * the callbacks. If you block the thread the ZooKeeper client won't process + * other events. + */ +@InterfaceAudience.Public +public interface AsyncCallback { + + /** + * This callback is used to retrieve the stat of the node. + */ + @InterfaceAudience.Public + interface StatCallback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + *

      On success, rc is {@link KeeperException.Code#OK}. + * + *

      On failure, rc is set to the corresponding failure code in {@link KeeperException}. + *

        + *
      • {@link KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls.
      • + *
      • {@link KeeperException.Code#BADVERSION} + * - The given version doesn't match the node's version for some API calls.
      • + *
      + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param stat {@link Stat} object of the node on given path. + * + * @see ZooKeeper#exists(String, boolean, StatCallback, Object) + * @see ZooKeeper#exists(String, Watcher, StatCallback, Object) + * @see ZooKeeper#setData(String, byte[], int, StatCallback, Object) + * @see ZooKeeper#setACL(String, List, int, StatCallback, Object) + */ + void processResult(int rc, String path, Object ctx, Stat stat); + + } + + /** + * This callback is used to get all children node number of the node. + * + * @since 3.6.0 + */ + @InterfaceAudience.Public + interface AllChildrenNumberCallback extends AsyncCallback { + + /** + * @param rc The return code or the result of the call. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param number The number of children nodes under a specific path. + * + * @see ZooKeeper#getAllChildrenNumber(String, AllChildrenNumberCallback, Object) + */ + void processResult(int rc, String path, Object ctx, int number); + + } + + /** + * This callback is used to retrieve the data and stat of the node. + */ + @InterfaceAudience.Public + interface DataCallback extends AsyncCallback { + + /** + * Process the result of asynchronous calls. + * + *

      On success, rc is {@link KeeperException.Code#OK}. + * + *

      On failure, rc is set to the corresponding failure code in {@link KeeperException}. + *

        + *
      • {@link KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls.
      • + *
      + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param data The data of the node. + * @param stat {@link Stat} object of the node on given path. + * + * @see ZooKeeper#getData(String, boolean, DataCallback, Object) + * @see ZooKeeper#getData(String, Watcher, DataCallback, Object) + * @see ZooKeeper#getConfig(boolean, DataCallback, Object) + * @see ZooKeeper#getConfig(Watcher, DataCallback, Object) + */ + void processResult(int rc, String path, Object ctx, byte[] data, Stat stat); + + } + + /** + * This callback is used to retrieve the ACL and stat of the node. + */ + @InterfaceAudience.Public + interface ACLCallback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + *

      On success, rc is {@link KeeperException.Code#OK}. + * + *

      On failure, rc is set to the corresponding failure code in {@link KeeperException}. + *

        + *
      • {@link KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls.
      • + *
      + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param acl ACL Id in {@link ZooDefs.Ids}. + * @param stat {@link Stat} object of the node on given path. + * + * @see ZooKeeper#getACL(String, Stat, ACLCallback, Object) + */ + void processResult(int rc, String path, Object ctx, List acl, Stat stat); + + } + + /** + * This callback is used to retrieve the children of the node. + */ + @InterfaceAudience.Public + interface ChildrenCallback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + *

      On success, rc is {@link KeeperException.Code#OK}. + * + *

      On failure, rc is set to the corresponding failure code in {@link KeeperException}. + *

        + *
      • {@link KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls.
      • + *
      + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param children An unordered array of children of the node on given path. + * + * @see ZooKeeper#getChildren(String, boolean, ChildrenCallback, Object) + * @see ZooKeeper#getChildren(String, Watcher, ChildrenCallback, Object) + */ + void processResult(int rc, String path, Object ctx, List children); + + } + + /** + * This callback is used to retrieve the children and stat of the node. + */ + @InterfaceAudience.Public + interface Children2Callback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param children An unordered array of children of the node on given path. + * @param stat {@link Stat} object of the node on given path. + * + * @see ChildrenCallback + * @see ZooKeeper#getChildren(String, boolean, Children2Callback, Object) + * @see ZooKeeper#getChildren(String, Watcher, Children2Callback, Object) + */ + void processResult(int rc, String path, Object ctx, List children, Stat stat); + + } + + /** + * This callback is used to retrieve the name and stat of the node. + */ + @InterfaceAudience.Public + interface Create2Callback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param name The name of the Znode that was created. On success, name + * and path are usually equal, unless a sequential node has + * been created. + * @param stat {@link Stat} object of the node on given path. + * + * @see StringCallback + * @see ZooKeeper#create(String, byte[], List, CreateMode, Create2Callback, Object) + * @see ZooKeeper#create(String, byte[], List, CreateMode, Create2Callback, Object, long) + */ + void processResult(int rc, String path, Object ctx, String name, Stat stat); + + } + + /** + * This callback is used to retrieve the name of the node. + */ + @InterfaceAudience.Public + interface StringCallback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + *

      On success, rc is {@link KeeperException.Code#OK}. + * + *

      On failure, rc is set to the corresponding failure code in {@link KeeperException}. + *

        + *
      • {@link KeeperException.Code#NODEEXISTS} + * - The node on give path already exists for some API calls.
      • + *
      • {@link KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls.
      • + *
      • {@link KeeperException.Code#NOCHILDRENFOREPHEMERALS} + * - An ephemeral node cannot have children. There is discussion in + * community. It might be changed in the future.
      • + *
      + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param name The name of the znode that was created. On success, name + * and path are usually equal, unless a sequential node has + * been created. + * + * @see ZooKeeper#create(String, byte[], List, CreateMode, StringCallback, Object) + */ + void processResult(int rc, String path, Object ctx, String name); + + } + + /** + * This callback doesn't retrieve anything from the node. It is useful for some APIs + * that doesn't want anything sent back, e.g. {@link ZooKeeper#sync(String, VoidCallback, Object)}. + */ + @InterfaceAudience.Public + interface VoidCallback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + *

      On success, rc is {@link KeeperException.Code#OK}. + * + *

      On failure, rc is set to the corresponding failure code in {@link KeeperException}. + *

        + *
      • {@link KeeperException.Code#NONODE} + * - The node on given path doesn't exist for some API calls.
      • + *
      • {@link KeeperException.Code#BADVERSION} + * - The given version doesn't match the node's version for some API calls.
      • + *
      • {@link KeeperException.Code#NOTEMPTY} + * - the node has children and some API calls cannot succeed, e.g. + * {@link ZooKeeper#delete(String, int, VoidCallback, Object)}.
      • + *
      + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * + * @see ZooKeeper#delete(String, int, VoidCallback, Object) + * @see ZooKeeper#removeAllWatches(String, Watcher.WatcherType, boolean, VoidCallback, Object) + * @see ZooKeeper#removeWatches(String, Watcher, Watcher.WatcherType, boolean, VoidCallback, Object) + * @see ZooKeeper#sync(String, VoidCallback, Object) + * + */ + void processResult(int rc, String path, Object ctx); + + } + + /** + * This callback is used to process the multiple results from a single multi call. + */ + @InterfaceAudience.Public + interface MultiCallback extends AsyncCallback { + + /** + * Process the result of the asynchronous call. + * + *

      On success, rc is {@link KeeperException.Code#OK}. All {@code opResults} are + * non-{@link OpResult.ErrorResult}. + * + *

      On failure, rc is a failure code in {@link KeeperException.Code}. Either + * {@code opResults} is null, or all {@code opResults} are {@link OpResult.ErrorResult}. + * All operations will be rolled back even if operations before the failing one were + * successful. + * + * @param rc The return code or the result of the call. + * @param path The path that we passed to asynchronous calls. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param opResults The list of results. One result for each operation, and the order + * matches that of input. + * + * @see ZooKeeper#multi(Iterable, MultiCallback, Object) + */ + void processResult(int rc, String path, Object ctx, List opResults); + + } + + /** + * This callback is used to process the getEphemerals results from a single getEphemerals call. + * + * @see ZooKeeper#getEphemerals(EphemeralsCallback, Object) + * @see ZooKeeper#getEphemerals(String, EphemeralsCallback, Object) + * + * @since 3.6.0 + */ + interface EphemeralsCallback extends AsyncCallback { + + /** + * @param rc The return code or the result of the call. + * @param ctx Whatever context object that we passed to asynchronous calls. + * @param paths The path that we passed to asynchronous calls. + */ + void processResult(int rc, Object ctx, List paths); + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxn.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxn.java new file mode 100644 index 0000000..78a286d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxn.java @@ -0,0 +1,1782 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ConnectException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadLocalRandom; +import javax.security.auth.login.LoginException; +import javax.security.sasl.SaslException; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.AsyncCallback.ACLCallback; +import org.apache.zookeeper.AsyncCallback.AllChildrenNumberCallback; +import org.apache.zookeeper.AsyncCallback.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +import org.apache.zookeeper.AsyncCallback.Create2Callback; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.EphemeralsCallback; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.OpResult.ErrorResult; +import org.apache.zookeeper.Watcher.Event; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.ZooKeeper.WatchRegistration; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.proto.AuthPacket; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.proto.Create2Response; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.ExistsResponse; +import org.apache.zookeeper.proto.GetACLResponse; +import org.apache.zookeeper.proto.GetAllChildrenNumberResponse; +import org.apache.zookeeper.proto.GetChildren2Response; +import org.apache.zookeeper.proto.GetChildrenResponse; +import org.apache.zookeeper.proto.GetDataResponse; +import org.apache.zookeeper.proto.GetEphemeralsResponse; +import org.apache.zookeeper.proto.GetSASLRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.SetACLResponse; +import org.apache.zookeeper.proto.SetDataResponse; +import org.apache.zookeeper.proto.SetWatches; +import org.apache.zookeeper.proto.SetWatches2; +import org.apache.zookeeper.proto.WatcherEvent; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.ZooTrace; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +/** + * This class manages the socket i/o for the client. ClientCnxn maintains a list + * of available servers to connect to and "transparently" switches servers it is + * connected to as needed. + */ +@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) +public class ClientCnxn { + + private static final Logger LOG = LoggerFactory.getLogger(ClientCnxn.class); + + /* ZOOKEEPER-706: If a session has a large number of watches set then + * attempting to re-establish those watches after a connection loss may + * fail due to the SetWatches request exceeding the server's configured + * jute.maxBuffer value. To avoid this we instead split the watch + * re-establishement across multiple SetWatches calls. This constant + * controls the size of each call. It is set to 128kB to be conservative + * with respect to the server's 1MB default for jute.maxBuffer. + */ + private static final int SET_WATCHES_MAX_LENGTH = 128 * 1024; + + /* predefined xid's values recognized as special by the server */ + // -1 means notification(WATCHER_EVENT) + public static final int NOTIFICATION_XID = -1; + // -2 is the xid for pings + public static final int PING_XID = -2; + // -4 is the xid for AuthPacket + public static final int AUTHPACKET_XID = -4; + // -8 is the xid for setWatch + public static final int SET_WATCHES_XID = -8; + + static class AuthData { + + AuthData(String scheme, byte[] data) { + this.scheme = scheme; + this.data = data; + } + + String scheme; + + byte[] data; + + } + + private final CopyOnWriteArraySet authInfo = new CopyOnWriteArraySet(); + + /** + * // TODO_MA 注释: 这些是已发送并正在等待响应的数据包。 + * These are the packets that have been sent and are waiting for a response. + */ + private final Queue pendingQueue = new ArrayDeque<>(); + + /** + * // TODO_MA 注释: 这些是需要发送的数据包。 + * These are the packets that need to be sent. + */ + private final LinkedBlockingDeque outgoingQueue = new LinkedBlockingDeque(); + + private int connectTimeout; + + /** + * The timeout in ms the client negotiated with the server. This is the + * "real" timeout, not the timeout request by the client (which may have + * been increased/decreased by the server which applies bounds to this + * value. + */ + private volatile int negotiatedSessionTimeout; + + private int readTimeout; + + private final int sessionTimeout; + + private final ZooKeeper zooKeeper; + + private final ClientWatchManager watcher; + + private long sessionId; + + private byte[] sessionPasswd = new byte[16]; + + /** + * If true, the connection is allowed to go to r-o mode. This field's value + * is sent, besides other data, during session creation handshake. If the + * server on the other side of the wire is partitioned it'll accept + * read-only clients only. + */ + private boolean readOnly; + + final String chrootPath; + + final SendThread sendThread; + + final EventThread eventThread; + + /** + * Set to true when close is called. Latches the connection such that we + * don't attempt to re-connect to the server if in the middle of closing the + * connection (client sends session disconnect to server as part of close + * operation) + */ + private volatile boolean closing = false; + + /** + * A set of ZooKeeper hosts this client could connect to. + */ + private final HostProvider hostProvider; + + /** + * Is set to true when a connection to a r/w server is established for the + * first time; never changed afterwards. + *

      + * Is used to handle situations when client without sessionId connects to a + * read-only server. Such client receives "fake" sessionId from read-only + * server, but this sessionId is invalid for other servers. So when such + * client finds a r/w server, it sends 0 instead of fake sessionId during + * connection handshake and establishes new, valid session. + *

      + * If this field is false (which implies we haven't seen r/w server before) + * then non-zero sessionId is fake, otherwise it is valid. + */ + volatile boolean seenRwServerBefore = false; + + public ZooKeeperSaslClient zooKeeperSaslClient; + + private final ZKClientConfig clientConfig; + /** + * If any request's response in not received in configured requestTimeout + * then it is assumed that the response packet is lost. + */ + private long requestTimeout; + + public long getSessionId() { + return sessionId; + } + + public byte[] getSessionPasswd() { + return sessionPasswd; + } + + public int getSessionTimeout() { + return negotiatedSessionTimeout; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + SocketAddress local = sendThread.getClientCnxnSocket().getLocalSocketAddress(); + SocketAddress remote = sendThread.getClientCnxnSocket().getRemoteSocketAddress(); + sb.append("sessionid:0x").append(Long.toHexString(getSessionId())).append(" local:").append(local) + .append(" remoteserver:").append(remote).append(" lastZxid:").append(lastZxid).append(" xid:").append(xid) + .append(" sent:").append(sendThread.getClientCnxnSocket().getSentCount()).append(" recv:") + .append(sendThread.getClientCnxnSocket().getRecvCount()).append(" queuedpkts:") + .append(outgoingQueue.size()).append(" pendingresp:").append(pendingQueue.size()).append(" queuedevents:") + .append(eventThread.waitingEvents.size()); + + return sb.toString(); + } + + /** + * This class allows us to pass the headers and the relevant records around. + */ + static class Packet { + + RequestHeader requestHeader; + + ReplyHeader replyHeader; + + Record request; + + Record response; + + ByteBuffer bb; + + /** + * Client's view of the path (may differ due to chroot) + **/ + String clientPath; + /** + * Servers's view of the path (may differ due to chroot) + **/ + String serverPath; + + boolean finished; + + AsyncCallback cb; + + Object ctx; + + WatchRegistration watchRegistration; + + public boolean readOnly; + + WatchDeregistration watchDeregistration; + + /** + * Convenience ctor + */ + Packet(RequestHeader requestHeader, ReplyHeader replyHeader, Record request, Record response, + WatchRegistration watchRegistration) { + this(requestHeader, replyHeader, request, response, watchRegistration, false); + } + + Packet(RequestHeader requestHeader, ReplyHeader replyHeader, Record request, Record response, + WatchRegistration watchRegistration, boolean readOnly) { + + this.requestHeader = requestHeader; + this.replyHeader = replyHeader; + this.request = request; + this.response = response; + this.readOnly = readOnly; + this.watchRegistration = watchRegistration; + } + + public void createBB() { + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeInt(-1, "len"); // We'll fill this in later + if(requestHeader != null) { + requestHeader.serialize(boa, "header"); + } + if(request instanceof ConnectRequest) { + request.serialize(boa, "connect"); + // append "am-I-allowed-to-be-readonly" flag + boa.writeBool(readOnly, "readOnly"); + } else if(request != null) { + request.serialize(boa, "request"); + } + baos.close(); + this.bb = ByteBuffer.wrap(baos.toByteArray()); + this.bb.putInt(this.bb.capacity() - 4); + this.bb.rewind(); + } catch(IOException e) { + LOG.warn("Unexpected exception", e); + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("clientPath:" + clientPath); + sb.append(" serverPath:" + serverPath); + sb.append(" finished:" + finished); + + sb.append(" header:: " + requestHeader); + sb.append(" replyHeader:: " + replyHeader); + sb.append(" request:: " + request); + sb.append(" response:: " + response); + + // jute toString is horrible, remove unnecessary newlines + return sb.toString().replaceAll("\r*\n+", " "); + } + + } + + /** + * Creates a connection object. The actual network connect doesn't get + * established until needed. The start() instance method must be called + * subsequent to construction. + * + * @param chrootPath - the chroot of this client. Should be removed from this Class in ZOOKEEPER-838 + * @param hostProvider the list of ZooKeeper servers to connect to + * @param sessionTimeout the timeout for connections. + * @param zooKeeper the zookeeper object that this connection is related to. + * @param watcher watcher for this connection + * @param clientCnxnSocket the socket implementation used (e.g. NIO/Netty) + * @param canBeReadOnly whether the connection is allowed to go to read-only + * mode in case of partitioning + * @throws IOException + */ + public ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZooKeeper zooKeeper, + ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket, boolean canBeReadOnly) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this(chrootPath, hostProvider, sessionTimeout, zooKeeper, watcher, clientCnxnSocket, 0, new byte[16], + canBeReadOnly); + } + + /** + * Creates a connection object. The actual network connect doesn't get + * established until needed. The start() instance method must be called + * subsequent to construction. + * + * @param chrootPath - the chroot of this client. Should be removed from this Class in ZOOKEEPER-838 + * @param hostProvider the list of ZooKeeper servers to connect to + * @param sessionTimeout the timeout for connections. + * @param zooKeeper the zookeeper object that this connection is related to. + * @param watcher watcher for this connection + * @param clientCnxnSocket the socket implementation used (e.g. NIO/Netty) + * @param sessionId session id if re-establishing session + * @param sessionPasswd session passwd if re-establishing session + * @param canBeReadOnly whether the connection is allowed to go to read-only + * mode in case of partitioning + * @throws IOException in cases of broken network + */ + public ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZooKeeper zooKeeper, + ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket, long sessionId, byte[] sessionPasswd, + boolean canBeReadOnly) throws IOException { + this.zooKeeper = zooKeeper; + this.watcher = watcher; + this.sessionId = sessionId; + this.sessionPasswd = sessionPasswd; + this.sessionTimeout = sessionTimeout; + this.hostProvider = hostProvider; + this.chrootPath = chrootPath; + + connectTimeout = sessionTimeout / hostProvider.size(); + readTimeout = sessionTimeout * 2 / 3; + readOnly = canBeReadOnly; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发起请求的线程(链接请求,写数据请求,读数据请求) + */ + sendThread = new SendThread(clientCnxnSocket); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 接收响应,执行处理的(watcher,callback,response) + */ + eventThread = new EventThread(); + + this.clientConfig = zooKeeper.getClientConfig(); + initRequestTimeout(); + } + + public void start() { + + // TODO_MA 注释: 先发起链接请求,建立链接之后,再发起正常的读写数据请求 + sendThread.start(); + + // TODO_MA 注释: 接收到请求的响应,执行处理的线程 + eventThread.start(); + } + + private Object eventOfDeath = new Object(); + + private static class WatcherSetEventPair { + + private final Set watchers; + private final WatchedEvent event; + + public WatcherSetEventPair(Set watchers, WatchedEvent event) { + this.watchers = watchers; + this.event = event; + } + + } + + /** + * Guard against creating "-EventThread-EventThread-EventThread-..." thread + * names when ZooKeeper object is being created from within a watcher. + * See ZOOKEEPER-795 for details. + */ + private static String makeThreadName(String suffix) { + String name = Thread.currentThread().getName().replaceAll("-EventThread", ""); + return name + suffix; + } + + /** + * Tests that current thread is the main event loop. + * This method is useful only for tests inside ZooKeeper project + * it is not a public API intended for use by external applications. + * + * @return true if Thread.currentThread() is an EventThread. + */ + public static boolean isInEventThread() { + return Thread.currentThread() instanceof EventThread; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + class EventThread extends ZooKeeperThread { + + // TODO_MA 马中华 注释: 存储 事件 的队列 + private final LinkedBlockingQueue waitingEvents = new LinkedBlockingQueue(); + + /** + * This is really the queued session state until the event + * thread actually processes the event and hands it to the watcher. + * But for all intents and purposes this is the state. + */ + private volatile KeeperState sessionState = KeeperState.Disconnected; + + private volatile boolean wasKilled = false; + private volatile boolean isRunning = false; + + EventThread() { + super(makeThreadName("-EventThread")); + setDaemon(true); + } + + public void queueEvent(WatchedEvent event) { + queueEvent(event, null); + } + + private void queueEvent(WatchedEvent event, Set materializedWatchers) { + if(event.getType() == EventType.None && sessionState == event.getState()) { + return; + } + sessionState = event.getState(); + final Set watchers; + if(materializedWatchers == null) { + // materialize the watchers based on the event + watchers = watcher.materialize(event.getState(), event.getType(), event.getPath()); + } else { + watchers = new HashSet(); + watchers.addAll(materializedWatchers); + } + WatcherSetEventPair pair = new WatcherSetEventPair(watchers, event); + // queue the pair (watch set & event) for later processing + waitingEvents.add(pair); + } + + public void queueCallback(AsyncCallback cb, int rc, String path, Object ctx) { + waitingEvents.add(new LocalCallback(cb, rc, path, ctx)); + } + + @SuppressFBWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER") + public void queuePacket(Packet packet) { + if(wasKilled) { + synchronized(waitingEvents) { + if(isRunning) { + waitingEvents.add(packet); + } else { + processEvent(packet); + } + } + } else { + waitingEvents.add(packet); + } + } + + public void queueEventOfDeath() { + waitingEvents.add(eventOfDeath); + } + + @Override + @SuppressFBWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER") + public void run() { + try { + isRunning = true; + while(true) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: EventThread 负责消费 waitingEvents 队列 + */ + Object event = waitingEvents.take(); + if(event == eventOfDeath) { + wasKilled = true; + } else { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理事件 + */ + processEvent(event); + } + if(wasKilled) { + synchronized(waitingEvents) { + if(waitingEvents.isEmpty()) { + isRunning = false; + break; + } + } + } + } + } catch(InterruptedException e) { + LOG.error("Event thread exiting due to interruption", e); + } + + LOG.info("EventThread shut down for session: 0x{}", Long.toHexString(getSessionId())); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理事件 + * 1、可能是 WatchedEvent + * 2、可能是 LocalCallback + * 3、其他的常规回调 + */ + private void processEvent(Object event) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 客户端接收到服务端返回回来的 response 经过解析之后得到: event 或者 watcherEvent + */ + if(event instanceof WatcherSetEventPair) { + // each watcher will process the event + WatcherSetEventPair pair = (WatcherSetEventPair) event; + for(Watcher watcher : pair.watchers) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 监听回调 + */ + watcher.process(pair.event); + + } catch(Throwable t) { + LOG.error("Error while calling watcher ", t); + } + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 回调处理 + */ + else if(event instanceof LocalCallback) { + LocalCallback lcb = (LocalCallback) event; + if(lcb.cb instanceof StatCallback) { + ((StatCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, null); + } else if(lcb.cb instanceof DataCallback) { + ((DataCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, null, null); + } else if(lcb.cb instanceof ACLCallback) { + ((ACLCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, null, null); + } else if(lcb.cb instanceof ChildrenCallback) { + ((ChildrenCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, null); + } else if(lcb.cb instanceof Children2Callback) { + ((Children2Callback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, null, null); + } else if(lcb.cb instanceof StringCallback) { + ((StringCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, null); + } else if(lcb.cb instanceof AsyncCallback.EphemeralsCallback) { + ((AsyncCallback.EphemeralsCallback) lcb.cb).processResult(lcb.rc, lcb.ctx, null); + } else if(lcb.cb instanceof AsyncCallback.AllChildrenNumberCallback) { + ((AsyncCallback.AllChildrenNumberCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx, -1); + } else if(lcb.cb instanceof AsyncCallback.MultiCallback) { + ((AsyncCallback.MultiCallback) lcb.cb) + .processResult(lcb.rc, lcb.path, lcb.ctx, Collections.emptyList()); + } else { + ((VoidCallback) lcb.cb).processResult(lcb.rc, lcb.path, lcb.ctx); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 其他的回调:普通的响应处理 + */ + else { + Packet p = (Packet) event; + int rc = 0; + String clientPath = p.clientPath; + if(p.replyHeader.getErr() != 0) { + rc = p.replyHeader.getErr(); + } + if(p.cb == null) { + LOG.warn("Somehow a null cb got to EventThread!"); + } else if(p.response instanceof ExistsResponse || p.response instanceof SetDataResponse || p.response instanceof SetACLResponse) { + StatCallback cb = (StatCallback) p.cb; + if(rc == 0) { + if(p.response instanceof ExistsResponse) { + cb.processResult(rc, clientPath, p.ctx, ((ExistsResponse) p.response).getStat()); + } else if(p.response instanceof SetDataResponse) { + cb.processResult(rc, clientPath, p.ctx, ((SetDataResponse) p.response).getStat()); + } else if(p.response instanceof SetACLResponse) { + cb.processResult(rc, clientPath, p.ctx, ((SetACLResponse) p.response).getStat()); + } + } else { + cb.processResult(rc, clientPath, p.ctx, null); + } + } + // TODO_MA 马中华 注释: GetDataResponse + else if(p.response instanceof GetDataResponse) { + DataCallback cb = (DataCallback) p.cb; + GetDataResponse rsp = (GetDataResponse) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, rsp.getData(), rsp.getStat()); + } else { + cb.processResult(rc, clientPath, p.ctx, null, null); + } + } else if(p.response instanceof GetACLResponse) { + ACLCallback cb = (ACLCallback) p.cb; + GetACLResponse rsp = (GetACLResponse) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, rsp.getAcl(), rsp.getStat()); + } else { + cb.processResult(rc, clientPath, p.ctx, null, null); + } + } + // TODO_MA 马中华 注释: GetChildrenResponse + else if(p.response instanceof GetChildrenResponse) { + ChildrenCallback cb = (ChildrenCallback) p.cb; + GetChildrenResponse rsp = (GetChildrenResponse) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, rsp.getChildren()); + } else { + cb.processResult(rc, clientPath, p.ctx, null); + } + } else if(p.response instanceof GetAllChildrenNumberResponse) { + AllChildrenNumberCallback cb = (AllChildrenNumberCallback) p.cb; + GetAllChildrenNumberResponse rsp = (GetAllChildrenNumberResponse) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, rsp.getTotalNumber()); + } else { + cb.processResult(rc, clientPath, p.ctx, -1); + } + } else if(p.response instanceof GetChildren2Response) { + Children2Callback cb = (Children2Callback) p.cb; + GetChildren2Response rsp = (GetChildren2Response) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, rsp.getChildren(), rsp.getStat()); + } else { + cb.processResult(rc, clientPath, p.ctx, null, null); + } + } + // TODO_MA 马中华 注释: CreateResponse + else if(p.response instanceof CreateResponse) { + StringCallback cb = (StringCallback) p.cb; + CreateResponse rsp = (CreateResponse) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, + (chrootPath == null ? rsp.getPath() : rsp.getPath().substring(chrootPath.length()))); + } else { + cb.processResult(rc, clientPath, p.ctx, null); + } + } else if(p.response instanceof Create2Response) { + Create2Callback cb = (Create2Callback) p.cb; + Create2Response rsp = (Create2Response) p.response; + if(rc == 0) { + cb.processResult(rc, clientPath, p.ctx, + (chrootPath == null ? rsp.getPath() : rsp.getPath().substring(chrootPath.length())), + rsp.getStat()); + } else { + cb.processResult(rc, clientPath, p.ctx, null, null); + } + } else if(p.response instanceof MultiResponse) { + MultiCallback cb = (MultiCallback) p.cb; + MultiResponse rsp = (MultiResponse) p.response; + if(rc == 0) { + List results = rsp.getResultList(); + int newRc = rc; + for(OpResult result : results) { + if(result instanceof ErrorResult && KeeperException.Code.OK + .intValue() != (newRc = ((ErrorResult) result).getErr())) { + break; + } + } + cb.processResult(newRc, clientPath, p.ctx, results); + } else { + cb.processResult(rc, clientPath, p.ctx, null); + } + } else if(p.response instanceof GetEphemeralsResponse) { + EphemeralsCallback cb = (EphemeralsCallback) p.cb; + GetEphemeralsResponse rsp = (GetEphemeralsResponse) p.response; + if(rc == 0) { + cb.processResult(rc, p.ctx, rsp.getEphemerals()); + } else { + cb.processResult(rc, p.ctx, null); + } + } else if(p.cb instanceof VoidCallback) { + VoidCallback cb = (VoidCallback) p.cb; + cb.processResult(rc, clientPath, p.ctx); + } + } + } catch(Throwable t) { + LOG.error("Unexpected throwable", t); + } + } + + } + + // @VisibleForTesting + protected void finishPacket(Packet p) { + int err = p.replyHeader.getErr(); + if(p.watchRegistration != null) { + p.watchRegistration.register(err); + } + // Add all the removed watch events to the event queue, so that the + // clients will be notified with 'Data/Child WatchRemoved' event type. + if(p.watchDeregistration != null) { + Map> materializedWatchers = null; + try { + materializedWatchers = p.watchDeregistration.unregister(err); + for(Entry> entry : materializedWatchers.entrySet()) { + Set watchers = entry.getValue(); + if(watchers.size() > 0) { + queueEvent(p.watchDeregistration.getClientPath(), err, watchers, entry.getKey()); + // ignore connectionloss when removing from local + // session + p.replyHeader.setErr(Code.OK.intValue()); + } + } + } catch(KeeperException.NoWatcherException nwe) { + p.replyHeader.setErr(nwe.code().intValue()); + } catch(KeeperException ke) { + p.replyHeader.setErr(ke.code().intValue()); + } + } + + if(p.cb == null) { + synchronized(p) { + p.finished = true; + p.notifyAll(); + } + } else { + p.finished = true; + eventThread.queuePacket(p); + } + } + + void queueEvent(String clientPath, int err, Set materializedWatchers, EventType eventType) { + KeeperState sessionState = KeeperState.SyncConnected; + if(KeeperException.Code.SESSIONEXPIRED.intValue() == err || KeeperException.Code.CONNECTIONLOSS + .intValue() == err) { + sessionState = Event.KeeperState.Disconnected; + } + WatchedEvent event = new WatchedEvent(eventType, sessionState, clientPath); + eventThread.queueEvent(event, materializedWatchers); + } + + void queueCallback(AsyncCallback cb, int rc, String path, Object ctx) { + eventThread.queueCallback(cb, rc, path, ctx); + } + + // for test only + protected void onConnecting(InetSocketAddress addr) { + + } + + private void conLossPacket(Packet p) { + if(p.replyHeader == null) { + return; + } + switch(state) { + case AUTH_FAILED: + p.replyHeader.setErr(KeeperException.Code.AUTHFAILED.intValue()); + break; + case CLOSED: + p.replyHeader.setErr(KeeperException.Code.SESSIONEXPIRED.intValue()); + break; + default: + p.replyHeader.setErr(KeeperException.Code.CONNECTIONLOSS.intValue()); + } + finishPacket(p); + } + + private volatile long lastZxid; + + public long getLastZxid() { + return lastZxid; + } + + static class EndOfStreamException extends IOException { + + private static final long serialVersionUID = -5438877188796231422L; + + public EndOfStreamException(String msg) { + super(msg); + } + + @Override + public String toString() { + return "EndOfStreamException: " + getMessage(); + } + + } + + private static class SessionTimeoutException extends IOException { + + private static final long serialVersionUID = 824482094072071178L; + + public SessionTimeoutException(String msg) { + super(msg); + } + + } + + private static class SessionExpiredException extends IOException { + + private static final long serialVersionUID = -1388816932076193249L; + + public SessionExpiredException(String msg) { + super(msg); + } + + } + + private static class RWServerFoundException extends IOException { + + private static final long serialVersionUID = 90431199887158758L; + + public RWServerFoundException(String msg) { + super(msg); + } + + } + + /** + * This class services the outgoing request queue and generates the heart + * beats. It also spawns the ReadThread. + */ + class SendThread extends ZooKeeperThread { + + private long lastPingSentNs; + private final ClientCnxnSocket clientCnxnSocket; + private boolean isFirstConnect = true; + + void readResponse(ByteBuffer incomingBuffer) throws IOException { + ByteBufferInputStream bbis = new ByteBufferInputStream(incomingBuffer); + BinaryInputArchive bbia = BinaryInputArchive.getArchive(bbis); + ReplyHeader replyHdr = new ReplyHeader(); + + replyHdr.deserialize(bbia, "header"); + switch(replyHdr.getXid()) { + case PING_XID: + LOG.debug("Got ping response for session id: 0x{} after {}ms.", Long.toHexString(sessionId), + ((System.nanoTime() - lastPingSentNs) / 1000000)); + return; + case AUTHPACKET_XID: + LOG.debug("Got auth session id: 0x{}", Long.toHexString(sessionId)); + if(replyHdr.getErr() == KeeperException.Code.AUTHFAILED.intValue()) { + changeZkState(States.AUTH_FAILED); + eventThread.queueEvent( + new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.AuthFailed, + null)); + eventThread.queueEventOfDeath(); + } + return; + case NOTIFICATION_XID: + LOG.debug("Got notification session id: 0x{}", Long.toHexString(sessionId)); + WatcherEvent event = new WatcherEvent(); + event.deserialize(bbia, "response"); + + // convert from a server path to a client path + if(chrootPath != null) { + String serverPath = event.getPath(); + if(serverPath.compareTo(chrootPath) == 0) { + event.setPath("/"); + } else if(serverPath.length() > chrootPath.length()) { + event.setPath(serverPath.substring(chrootPath.length())); + } else { + LOG.warn("Got server path {} which is too short for chroot path {}.", event.getPath(), + chrootPath); + } + } + + WatchedEvent we = new WatchedEvent(event); + LOG.debug("Got {} for session id 0x{}", we, Long.toHexString(sessionId)); + eventThread.queueEvent(we); + return; + default: + break; + } + + // If SASL authentication is currently in progress, construct and + // send a response packet immediately, rather than queuing a + // response as with other packets. + if(tunnelAuthInProgress()) { + GetSASLRequest request = new GetSASLRequest(); + request.deserialize(bbia, "token"); + zooKeeperSaslClient.respondToServer(request.getToken(), ClientCnxn.this); + return; + } + + Packet packet; + synchronized(pendingQueue) { + if(pendingQueue.size() == 0) { + throw new IOException("Nothing in the queue, but got " + replyHdr.getXid()); + } + packet = pendingQueue.remove(); + } + /* + * Since requests are processed in order, we better get a response + * to the first request! + */ + try { + if(packet.requestHeader.getXid() != replyHdr.getXid()) { + packet.replyHeader.setErr(KeeperException.Code.CONNECTIONLOSS.intValue()); + throw new IOException("Xid out of order. Got Xid " + replyHdr.getXid() + " with err " + replyHdr + .getErr() + " expected Xid " + packet.requestHeader + .getXid() + " for a packet with details: " + packet); + } + + packet.replyHeader.setXid(replyHdr.getXid()); + packet.replyHeader.setErr(replyHdr.getErr()); + packet.replyHeader.setZxid(replyHdr.getZxid()); + if(replyHdr.getZxid() > 0) { + lastZxid = replyHdr.getZxid(); + } + if(packet.response != null && replyHdr.getErr() == 0) { + packet.response.deserialize(bbia, "response"); + } + + LOG.debug("Reading reply session id: 0x{}, packet:: {}", Long.toHexString(sessionId), packet); + } finally { + finishPacket(packet); + } + } + + SendThread(ClientCnxnSocket clientCnxnSocket) throws IOException { + super(makeThreadName("-SendThread()")); + changeZkState(States.CONNECTING); + this.clientCnxnSocket = clientCnxnSocket; + setDaemon(true); + } + + // TODO: can not name this method getState since Thread.getState() + // already exists + // It would be cleaner to make class SendThread an implementation of + // Runnable + + /** + * Used by ClientCnxnSocket + * + * @return + */ + synchronized ZooKeeper.States getZkState() { + return state; + } + + synchronized void changeZkState(ZooKeeper.States newState) throws IOException { + if(!state.isAlive() && newState == States.CONNECTING) { + throw new IOException("Connection has already been closed and reconnection is not allowed"); + } + // It's safer to place state modification at the end. + state = newState; + } + + ClientCnxnSocket getClientCnxnSocket() { + return clientCnxnSocket; + } + + /** + * Setup session, previous watches, authentication. + */ + void primeConnection() throws IOException { + LOG.info("Socket connection established, initiating session, client: {}, server: {}", + clientCnxnSocket.getLocalSocketAddress(), clientCnxnSocket.getRemoteSocketAddress()); + isFirstConnect = false; + long sessId = (seenRwServerBefore) ? sessionId : 0; + + // TODO_MA 注释: 生成一个链接请求 + // TODO_MA 注释: 其实在这之前,已经完成了,只是再发送一个 连接请求给 ZK Server,建立 Session + ConnectRequest conReq = new ConnectRequest(0, lastZxid, sessionTimeout, sessId, sessionPasswd); + + // TODO_MA 注释: 处理注册监听的事情 + // We add backwards since we are pushing into the front + // Only send if there's a pending watch + // TODO: here we have the only remaining use of zooKeeper in + // this class. It's to be eliminated! + if(!clientConfig.getBoolean(ZKClientConfig.DISABLE_AUTO_WATCH_RESET)) { + List dataWatches = zooKeeper.getDataWatches(); + List existWatches = zooKeeper.getExistWatches(); + List childWatches = zooKeeper.getChildWatches(); + List persistentWatches = zooKeeper.getPersistentWatches(); + List persistentRecursiveWatches = zooKeeper.getPersistentRecursiveWatches(); + if(!dataWatches.isEmpty() || !existWatches.isEmpty() || !childWatches.isEmpty() || !persistentWatches + .isEmpty() || !persistentRecursiveWatches.isEmpty()) { + Iterator dataWatchesIter = prependChroot(dataWatches).iterator(); + Iterator existWatchesIter = prependChroot(existWatches).iterator(); + Iterator childWatchesIter = prependChroot(childWatches).iterator(); + Iterator persistentWatchesIter = prependChroot(persistentWatches).iterator(); + Iterator persistentRecursiveWatchesIter = prependChroot(persistentRecursiveWatches) + .iterator(); + long setWatchesLastZxid = lastZxid; + + while(dataWatchesIter.hasNext() || existWatchesIter.hasNext() || childWatchesIter + .hasNext() || persistentWatchesIter.hasNext() || persistentRecursiveWatchesIter.hasNext()) { + List dataWatchesBatch = new ArrayList(); + List existWatchesBatch = new ArrayList(); + List childWatchesBatch = new ArrayList(); + List persistentWatchesBatch = new ArrayList(); + List persistentRecursiveWatchesBatch = new ArrayList(); + int batchLength = 0; + + // Note, we may exceed our max length by a bit when we add the last + // watch in the batch. This isn't ideal, but it makes the code simpler. + while(batchLength < SET_WATCHES_MAX_LENGTH) { + final String watch; + if(dataWatchesIter.hasNext()) { + watch = dataWatchesIter.next(); + dataWatchesBatch.add(watch); + } else if(existWatchesIter.hasNext()) { + watch = existWatchesIter.next(); + existWatchesBatch.add(watch); + } else if(childWatchesIter.hasNext()) { + watch = childWatchesIter.next(); + childWatchesBatch.add(watch); + } else if(persistentWatchesIter.hasNext()) { + watch = persistentWatchesIter.next(); + persistentWatchesBatch.add(watch); + } else if(persistentRecursiveWatchesIter.hasNext()) { + watch = persistentRecursiveWatchesIter.next(); + persistentRecursiveWatchesBatch.add(watch); + } else { + break; + } + batchLength += watch.length(); + } + + Record record; + int opcode; + if(persistentWatchesBatch.isEmpty() && persistentRecursiveWatchesBatch.isEmpty()) { + // maintain compatibility with older servers - if no persistent/recursive watchers + // are used, use the old version of SetWatches + record = new SetWatches(setWatchesLastZxid, dataWatchesBatch, existWatchesBatch, + childWatchesBatch); + opcode = OpCode.setWatches; + } else { + record = new SetWatches2(setWatchesLastZxid, dataWatchesBatch, existWatchesBatch, + childWatchesBatch, persistentWatchesBatch, persistentRecursiveWatchesBatch); + opcode = OpCode.setWatches2; + } + + // TODO_MA 注释: 注册监听的数据包,加入队列 + RequestHeader header = new RequestHeader(ClientCnxn.SET_WATCHES_XID, opcode); + Packet packet = new Packet(header, new ReplyHeader(), record, null, null); + outgoingQueue.addFirst(packet); + } + } + } + + // TODO_MA 注释: 认证请求 + for(AuthData id : authInfo) { + outgoingQueue.addFirst(new Packet(new RequestHeader(ClientCnxn.AUTHPACKET_XID, OpCode.auth), null, + new AuthPacket(0, id.scheme, id.data), null, null)); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 将链接请求放到 outgoingQueue 队列中,等待发送 + * SendThread 消费该队列,执行队列中的请求对象的发送! + */ + outgoingQueue.addFirst(new Packet(null, null, conReq, null, null, readOnly)); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 注册 OP_READ 和 OP_WRITE 事件 + */ + clientCnxnSocket.connectionPrimed(); + LOG.debug("Session establishment request sent on {}", clientCnxnSocket.getRemoteSocketAddress()); + } + + private List prependChroot(List paths) { + if(chrootPath != null && !paths.isEmpty()) { + for(int i = 0; i < paths.size(); ++i) { + String clientPath = paths.get(i); + String serverPath; + // handle clientPath = "/" + if(clientPath.length() == 1) { + serverPath = chrootPath; + } else { + serverPath = chrootPath + clientPath; + } + paths.set(i, serverPath); + } + } + return paths; + } + + private void sendPing() { + lastPingSentNs = System.nanoTime(); + RequestHeader h = new RequestHeader(ClientCnxn.PING_XID, OpCode.ping); + queuePacket(h, null, null, null, null, null, null, null, null); + } + + private InetSocketAddress rwServerAddress = null; + + private static final int minPingRwTimeout = 100; + + private static final int maxPingRwTimeout = 60000; + + private int pingRwTimeout = minPingRwTimeout; + + // Set to true if and only if constructor of ZooKeeperSaslClient + // throws a LoginException: see startConnect() below. + private boolean saslLoginFailed = false; + + private void startConnect(InetSocketAddress addr) throws IOException { + // initializing it for new connection + saslLoginFailed = false; + if(!isFirstConnect) { + try { + Thread.sleep(ThreadLocalRandom.current().nextLong(1000)); + } catch(InterruptedException e) { + LOG.warn("Unexpected exception", e); + } + } + changeZkState(States.CONNECTING); + + String hostPort = addr.getHostString() + ":" + addr.getPort(); + MDC.put("myid", hostPort); + setName(getName().replaceAll("\\(.*\\)", "(" + hostPort + ")")); + if(clientConfig.isSaslClientEnabled()) { + try { + if(zooKeeperSaslClient != null) { + zooKeeperSaslClient.shutdown(); + } + zooKeeperSaslClient = new ZooKeeperSaslClient( + SaslServerPrincipal.getServerPrincipal(addr, clientConfig), clientConfig); + } catch(LoginException e) { + // An authentication error occurred when the SASL client tried to initialize: + // for Kerberos this means that the client failed to authenticate with the KDC. + // This is different from an authentication error that occurs during communication + // with the Zookeeper server, which is handled below. + LOG.warn( + "SASL configuration failed. " + "Will continue connection to Zookeeper server without " + "SASL authentication, if Zookeeper server allows it.", + e); + eventThread.queueEvent( + new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.AuthFailed, null)); + saslLoginFailed = true; + } + } + logStartConnect(addr); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 链接 ZK Server + */ + clientCnxnSocket.connect(addr); + } + + private void logStartConnect(InetSocketAddress addr) { + LOG.info("Opening socket connection to server {}.", addr); + if(zooKeeperSaslClient != null) { + LOG.info("SASL config status: {}", zooKeeperSaslClient.getConfigStatus()); + } + } + + @Override + public void run() { + + // TODO_MA 马中华 注释: outgoingQueue 传递 + clientCnxnSocket.introduce(this, sessionId, outgoingQueue); + clientCnxnSocket.updateNow(); + clientCnxnSocket.updateLastSendAndHeard(); + int to; + long lastPingRwServer = Time.currentElapsedTime(); + final int MAX_SEND_PING_INTERVAL = 10000; //10 seconds + InetSocketAddress serverAddress = null; + + // TODO_MA 注释: 循环 + while(state.isAlive()) { + try { + + // TODO_MA 注释: 如果不是已经链接,则发起链接 + if(!clientCnxnSocket.isConnected()) { + // don't re-establish connection if we are closing + if(closing) { + break; + } + + // TODO_MA 注释: 获取服务端地址的方式 + if(rwServerAddress != null) { + serverAddress = rwServerAddress; + rwServerAddress = null; + } else { + // TODO_MA 注释: 轮询! round robin = 事实上是随机 + // TODO_MA 注释: 每次获取之前,都打乱一下,然后获取第一个 + serverAddress = hostProvider.next(1000); + } + onConnecting(serverAddress); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 完成链接: NIO 客户端 链接 NIO 服务端 + */ + startConnect(serverAddress); + + clientCnxnSocket.updateLastSendAndHeard(); + } + + // TODO_MA 注释: 如果已经链接 + if(state.isConnected()) { + // determine whether we need to send an AuthFailed event. + if(zooKeeperSaslClient != null) { + boolean sendAuthEvent = false; + if(zooKeeperSaslClient.getSaslState() == ZooKeeperSaslClient.SaslState.INITIAL) { + try { + zooKeeperSaslClient.initialize(ClientCnxn.this); + } catch(SaslException e) { + LOG.error("SASL authentication with Zookeeper Quorum member failed.", e); + changeZkState(States.AUTH_FAILED); + sendAuthEvent = true; + } + } + KeeperState authState = zooKeeperSaslClient.getKeeperState(); + if(authState != null) { + if(authState == KeeperState.AuthFailed) { + // An authentication error occurred during authentication with the Zookeeper Server. + changeZkState(States.AUTH_FAILED); + sendAuthEvent = true; + } else { + if(authState == KeeperState.SaslAuthenticated) { + sendAuthEvent = true; + } + } + } + + if(sendAuthEvent) { + eventThread.queueEvent(new WatchedEvent(Watcher.Event.EventType.None, authState, null)); + if(state == States.AUTH_FAILED) { + eventThread.queueEventOfDeath(); + } + } + } + to = readTimeout - clientCnxnSocket.getIdleRecv(); + } else { + to = connectTimeout - clientCnxnSocket.getIdleRecv(); + } + + if(to <= 0) { + String warnInfo = String + .format("Client session timed out, have not heard from server in %dms for session id 0x%s", + clientCnxnSocket.getIdleRecv(), Long.toHexString(sessionId)); + LOG.warn(warnInfo); + throw new SessionTimeoutException(warnInfo); + } + + // TODO_MA 注释: 如果已经链接,发送心跳 + // TODO_MA 注释: client 需要和 server 维持心跳 + // TODO_MA 注释: 如果 链接在一个时间范围内没有任何操作,则需要进行过期处理 + // TODO_MA 注释: 服务端还必须要知道在这个没有任何操作的时间范围内没有动作,但是是否还活着? + // TODO_MA 注释: 如果心跳丢失,则意味着客户端失去了当前服务器的链接,重试跟其他服务器建立链接 + // TODO_MA 注释: 如果建立链接成功,则session未超时,则恢复回来 + if(state.isConnected()) { + //1000(1 second) is to prevent race condition missing to send the second ping + //also make sure not to send too many pings when readTimeout is small + int timeToNextPing = readTimeout / 2 - clientCnxnSocket.getIdleSend() - ((clientCnxnSocket + .getIdleSend() > 1000) ? 1000 : 0); + //send a ping request either time is due or no packet sent out within MAX_SEND_PING_INTERVAL + if(timeToNextPing <= 0 || clientCnxnSocket.getIdleSend() > MAX_SEND_PING_INTERVAL) { + sendPing(); + clientCnxnSocket.updateLastSend(); + } else { + if(timeToNextPing < to) { + to = timeToNextPing; + } + } + } + + // TODO_MA 注释: 如果是只读模式 + // If we are in read-only mode, seek for read/write server + if(state == States.CONNECTEDREADONLY) { + long now = Time.currentElapsedTime(); + int idlePingRwServer = (int) (now - lastPingRwServer); + if(idlePingRwServer >= pingRwTimeout) { + lastPingRwServer = now; + idlePingRwServer = 0; + pingRwTimeout = Math.min(2 * pingRwTimeout, maxPingRwTimeout); + pingRwServer(); + } + to = Math.min(to, pingRwTimeout - idlePingRwServer); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 执行读写处理 IO 处理 + * 1、要么是 OP_ACCEPT 动作 + * 2、要么是 OP_READ 或者 OP_WRITE 动作 + */ + clientCnxnSocket.doTransport(to, pendingQueue, ClientCnxn.this); + + } catch(Throwable e) { + if(closing) { + // closing so this is expected + LOG.warn("An exception was thrown while closing send thread for session 0x{}.", + Long.toHexString(getSessionId()), e); + break; + } else { + LOG.warn( + "Session 0x{} for sever {}, Closing socket connection. " + "Attempting reconnect except it is a SessionExpiredException.", + Long.toHexString(getSessionId()), serverAddress, e); + + // At this point, there might still be new packets appended to outgoingQueue. + // they will be handled in next connection or cleared up if closed. + cleanAndNotifyState(); + } + } + } + + synchronized(state) { + // When it comes to this point, it guarantees that later queued + // packet to outgoingQueue will be notified of death. + cleanup(); + } + clientCnxnSocket.close(); + if(state.isAlive()) { + eventThread.queueEvent(new WatchedEvent(Event.EventType.None, Event.KeeperState.Disconnected, null)); + } + eventThread.queueEvent(new WatchedEvent(Event.EventType.None, Event.KeeperState.Closed, null)); + ZooTrace.logTraceMessage(LOG, ZooTrace.getTextTraceLevel(), + "SendThread exited loop for session: 0x" + Long.toHexString(getSessionId())); + } + + private void cleanAndNotifyState() { + cleanup(); + if(state.isAlive()) { + eventThread.queueEvent(new WatchedEvent(Event.EventType.None, Event.KeeperState.Disconnected, null)); + } + clientCnxnSocket.updateNow(); + clientCnxnSocket.updateLastSendAndHeard(); + } + + private void pingRwServer() throws RWServerFoundException { + String result = null; + InetSocketAddress addr = hostProvider.next(0); + + LOG.info("Checking server {} for being r/w. Timeout {}", addr, pingRwTimeout); + + Socket sock = null; + BufferedReader br = null; + try { + sock = new Socket(addr.getHostString(), addr.getPort()); + sock.setSoLinger(false, -1); + sock.setSoTimeout(1000); + sock.setTcpNoDelay(true); + sock.getOutputStream().write("isro".getBytes()); + sock.getOutputStream().flush(); + sock.shutdownOutput(); + br = new BufferedReader(new InputStreamReader(sock.getInputStream())); + result = br.readLine(); + } catch(ConnectException e) { + // ignore, this just means server is not up + } catch(IOException e) { + // some unexpected error, warn about it + LOG.warn("Exception while seeking for r/w server.", e); + } finally { + if(sock != null) { + try { + sock.close(); + } catch(IOException e) { + LOG.warn("Unexpected exception", e); + } + } + if(br != null) { + try { + br.close(); + } catch(IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + + if("rw".equals(result)) { + pingRwTimeout = minPingRwTimeout; + // save the found address so that it's used during the next + // connection attempt + rwServerAddress = addr; + throw new RWServerFoundException( + "Majority server found at " + addr.getHostString() + ":" + addr.getPort()); + } + } + + private void cleanup() { + clientCnxnSocket.cleanup(); + synchronized(pendingQueue) { + for(Packet p : pendingQueue) { + conLossPacket(p); + } + pendingQueue.clear(); + } + // We can't call outgoingQueue.clear() here because + // between iterating and clear up there might be new + // packets added in queuePacket(). + Iterator iter = outgoingQueue.iterator(); + while(iter.hasNext()) { + Packet p = iter.next(); + conLossPacket(p); + iter.remove(); + } + } + + /** + * Callback invoked by the ClientCnxnSocket once a connection has been + * established. + * + * @param _negotiatedSessionTimeout + * @param _sessionId + * @param _sessionPasswd + * @param isRO + * @throws IOException + */ + void onConnected(int _negotiatedSessionTimeout, long _sessionId, byte[] _sessionPasswd, + boolean isRO) throws IOException { + negotiatedSessionTimeout = _negotiatedSessionTimeout; + if(negotiatedSessionTimeout <= 0) { + changeZkState(States.CLOSED); + + eventThread.queueEvent( + new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Expired, null)); + eventThread.queueEventOfDeath(); + + String warnInfo = String.format("Unable to reconnect to ZooKeeper service, session 0x%s has expired", + Long.toHexString(sessionId)); + LOG.warn(warnInfo); + throw new SessionExpiredException(warnInfo); + } + + if(!readOnly && isRO) { + LOG.error("Read/write client got connected to read-only server"); + } + + readTimeout = negotiatedSessionTimeout * 2 / 3; + connectTimeout = negotiatedSessionTimeout / hostProvider.size(); + hostProvider.onConnected(); + sessionId = _sessionId; + sessionPasswd = _sessionPasswd; + changeZkState((isRO) ? States.CONNECTEDREADONLY : States.CONNECTED); + seenRwServerBefore |= !isRO; + LOG.info("Session establishment complete on server {}, session id = 0x{}, negotiated timeout = {}{}", + clientCnxnSocket.getRemoteSocketAddress(), Long.toHexString(sessionId), negotiatedSessionTimeout, + (isRO ? " (READ-ONLY mode)" : "")); + KeeperState eventState = (isRO) ? KeeperState.ConnectedReadOnly : KeeperState.SyncConnected; + eventThread.queueEvent(new WatchedEvent(Watcher.Event.EventType.None, eventState, null)); + } + + void close() { + try { + changeZkState(States.CLOSED); + } catch(IOException e) { + LOG.warn("Connection close fails when migrates state from {} to CLOSED", getZkState()); + } + clientCnxnSocket.onClosing(); + } + + void testableCloseSocket() throws IOException { + clientCnxnSocket.testableCloseSocket(); + } + + public boolean tunnelAuthInProgress() { + // 1. SASL client is disabled. + if(!clientConfig.isSaslClientEnabled()) { + return false; + } + + // 2. SASL login failed. + if(saslLoginFailed) { + return false; + } + + // 3. SendThread has not created the authenticating object yet, + // therefore authentication is (at the earliest stage of being) in progress. + if(zooKeeperSaslClient == null) { + return true; + } + + // 4. authenticating object exists, so ask it for its progress. + return zooKeeperSaslClient.clientTunneledAuthenticationInProgress(); + } + + public void sendPacket(Packet p) throws IOException { + clientCnxnSocket.sendPacket(p); + } + + } + + /** + * Shutdown the send/event threads. This method should not be called + * directly - rather it should be called as part of close operation. This + * method is primarily here to allow the tests to verify disconnection + * behavior. + */ + public void disconnect() { + LOG.debug("Disconnecting client for session: 0x{}", Long.toHexString(getSessionId())); + + sendThread.close(); + try { + sendThread.join(); + } catch(InterruptedException ex) { + LOG.warn("Got interrupted while waiting for the sender thread to close", ex); + } + eventThread.queueEventOfDeath(); + if(zooKeeperSaslClient != null) { + zooKeeperSaslClient.shutdown(); + } + } + + /** + * Close the connection, which includes; send session disconnect to the + * server, shutdown the send/event threads. + * + * @throws IOException + */ + public void close() throws IOException { + LOG.debug("Closing client for session: 0x{}", Long.toHexString(getSessionId())); + + try { + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.closeSession); + + submitRequest(h, null, null, null); + } catch(InterruptedException e) { + // ignore, close the send/event threads + } finally { + disconnect(); + } + } + + // @VisibleForTesting + protected int xid = 1; + + // @VisibleForTesting + volatile States state = States.NOT_CONNECTED; + + /* + * getXid() is called externally by ClientCnxnNIO::doIO() when packets are sent from the outgoingQueue to + * the server. Thus, getXid() must be public. + */ + public synchronized int getXid() { + // Avoid negative cxid values. In particular, cxid values of -4, -2, and -1 are special and + // must not be used for requests -- see SendThread.readResponse. + // Skip from MAX to 1. + if(xid == Integer.MAX_VALUE) { + xid = 1; + } + return xid++; + } + + public ReplyHeader submitRequest(RequestHeader h, Record request, Record response, + WatchRegistration watchRegistration) throws InterruptedException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return submitRequest(h, request, response, watchRegistration, null); + } + + public ReplyHeader submitRequest(RequestHeader h, Record request, Record response, + WatchRegistration watchRegistration, WatchDeregistration watchDeregistration) throws InterruptedException { + ReplyHeader r = new ReplyHeader(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + Packet packet = queuePacket(h, r, request, response, null, null, null, null, watchRegistration, + watchDeregistration); + + synchronized(packet) { + if(requestTimeout > 0) { + // Wait for request completion with timeout + waitForPacketFinish(r, packet); + } else { + // Wait for request completion infinitely + while(!packet.finished) { + packet.wait(); + } + } + } + if(r.getErr() == Code.REQUESTTIMEOUT.intValue()) { + sendThread.cleanAndNotifyState(); + } + return r; + } + + /** + * Wait for request completion with timeout. + */ + private void waitForPacketFinish(ReplyHeader r, Packet packet) throws InterruptedException { + long waitStartTime = Time.currentElapsedTime(); + while(!packet.finished) { + packet.wait(requestTimeout); + if(!packet.finished && ((Time.currentElapsedTime() - waitStartTime) >= requestTimeout)) { + LOG.error("Timeout error occurred for the packet '{}'.", packet); + r.setErr(Code.REQUESTTIMEOUT.intValue()); + break; + } + } + } + + public void saslCompleted() { + sendThread.getClientCnxnSocket().saslCompleted(); + } + + public void sendPacket(Record request, Record response, AsyncCallback cb, int opCode) throws IOException { + // Generate Xid now because it will be sent immediately, + // by call to sendThread.sendPacket() below. + int xid = getXid(); + RequestHeader h = new RequestHeader(); + h.setXid(xid); + h.setType(opCode); + + ReplyHeader r = new ReplyHeader(); + r.setXid(xid); + + Packet p = new Packet(h, r, request, response, null, false); + p.cb = cb; + sendThread.sendPacket(p); + } + + public Packet queuePacket(RequestHeader h, ReplyHeader r, Record request, Record response, AsyncCallback cb, + String clientPath, String serverPath, Object ctx, WatchRegistration watchRegistration) { + return queuePacket(h, r, request, response, cb, clientPath, serverPath, ctx, watchRegistration, null); + } + + public Packet queuePacket(RequestHeader h, ReplyHeader r, Record request, Record response, AsyncCallback cb, + String clientPath, String serverPath, Object ctx, WatchRegistration watchRegistration, + WatchDeregistration watchDeregistration) { + Packet packet = null; + + // Note that we do not generate the Xid for the packet yet. It is + // generated later at send-time, by an implementation of ClientCnxnSocket::doIO(), + // where the packet is actually sent. + packet = new Packet(h, r, request, response, watchRegistration); + packet.cb = cb; + packet.ctx = ctx; + packet.clientPath = clientPath; + packet.serverPath = serverPath; + packet.watchDeregistration = watchDeregistration; + // The synchronized block here is for two purpose: + // 1. synchronize with the final cleanup() in SendThread.run() to avoid race + // 2. synchronized against each packet. So if a closeSession packet is added, + // later packet will be notified. + synchronized(state) { + if(!state.isAlive() || closing) { + conLossPacket(packet); + } else { + // If the client is asking to close the session then + // mark as closing + if(h.getType() == OpCode.closeSession) { + closing = true; + } + + // TODO_MA 注释: 请求数据包加入队列等待发送 + outgoingQueue.add(packet); + + // TODO_MA 注释:在看 ZooKeeper 这个类的构造方法的时候,知道它的内部,创建了 ClinetCxn 的一个客户端组件 + // TODO_MA 注释: 它的内部创建了两个线程: SendThread EventThread + // TODO_MA 注释: SendThread 负责消费 outgoingQueue + } + } + + // TODO_MA 注释: 唤醒 ClientCnxnSocket + sendThread.getClientCnxnSocket().packetAdded(); + return packet; + } + + public void addAuthInfo(String scheme, byte[] auth) { + if(!state.isAlive()) { + return; + } + authInfo.add(new AuthData(scheme, auth)); + queuePacket(new RequestHeader(ClientCnxn.AUTHPACKET_XID, OpCode.auth), null, new AuthPacket(0, scheme, auth), + null, null, null, null, null, null); + } + + States getState() { + return state; + } + + private static class LocalCallback { + + private final AsyncCallback cb; + private final int rc; + private final String path; + private final Object ctx; + + public LocalCallback(AsyncCallback cb, int rc, String path, Object ctx) { + this.cb = cb; + this.rc = rc; + this.path = path; + this.ctx = ctx; + } + + } + + private void initRequestTimeout() { + try { + requestTimeout = clientConfig + .getLong(ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT, ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT_DEFAULT); + LOG.info("{} value is {}. feature enabled={}", ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT, requestTimeout, + requestTimeout > 0); + } catch(NumberFormatException e) { + LOG.error("Configured value {} for property {} can not be parsed to long.", + clientConfig.getProperty(ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT), + ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT); + throw e; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java new file mode 100644 index 0000000..094961e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocket.java @@ -0,0 +1,252 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.text.MessageFormat; +import java.util.Queue; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.jute.BinaryInputArchive; +import org.apache.zookeeper.ClientCnxn.Packet; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.proto.ConnectResponse; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A ClientCnxnSocket does the lower level communication with a socket + * implementation. + * + * This code has been moved out of ClientCnxn so that a Netty implementation can + * be provided as an alternative to the NIO socket code. + * + */ +abstract class ClientCnxnSocket { + + private static final Logger LOG = LoggerFactory.getLogger(ClientCnxnSocket.class); + + protected boolean initialized; + + /** + * This buffer is only used to read the length of the incoming message. + */ + protected final ByteBuffer lenBuffer = ByteBuffer.allocateDirect(4); + + /** + * After the length is read, a new incomingBuffer is allocated in + * readLength() to receive the full message. + */ + protected ByteBuffer incomingBuffer = lenBuffer; + protected final AtomicLong sentCount = new AtomicLong(0L); + protected final AtomicLong recvCount = new AtomicLong(0L); + protected long lastHeard; + protected long lastSend; + protected long now; + protected ClientCnxn.SendThread sendThread; + protected LinkedBlockingDeque outgoingQueue; + protected ZKClientConfig clientConfig; + private int packetLen = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT; + + /** + * The sessionId is only available here for Log and Exception messages. + * Otherwise the socket doesn't need to know it. + */ + protected long sessionId; + + void introduce(ClientCnxn.SendThread sendThread, long sessionId, LinkedBlockingDeque outgoingQueue) { + this.sendThread = sendThread; + this.sessionId = sessionId; + + // TODO_MA 马中华 注释: outgoingQueue 传递 + this.outgoingQueue = outgoingQueue; + } + + void updateNow() { + now = Time.currentElapsedTime(); + } + + int getIdleRecv() { + return (int) (now - lastHeard); + } + + int getIdleSend() { + return (int) (now - lastSend); + } + + long getSentCount() { + return sentCount.get(); + } + + long getRecvCount() { + return recvCount.get(); + } + + void updateLastHeard() { + this.lastHeard = now; + } + + void updateLastSend() { + this.lastSend = now; + } + + void updateLastSendAndHeard() { + this.lastSend = now; + this.lastHeard = now; + } + + void readLength() throws IOException { + int len = incomingBuffer.getInt(); + if (len < 0 || len >= packetLen) { + throw new IOException("Packet len " + len + " is out of range!"); + } + incomingBuffer = ByteBuffer.allocate(len); + } + + void readConnectResult() throws IOException { + if (LOG.isTraceEnabled()) { + StringBuilder buf = new StringBuilder("0x["); + for (byte b : incomingBuffer.array()) { + buf.append(Integer.toHexString(b)).append(","); + } + buf.append("]"); + if (LOG.isTraceEnabled()) { + LOG.trace("readConnectResult {} {}", incomingBuffer.remaining(), buf.toString()); + } + } + + ByteBufferInputStream bbis = new ByteBufferInputStream(incomingBuffer); + BinaryInputArchive bbia = BinaryInputArchive.getArchive(bbis); + ConnectResponse conRsp = new ConnectResponse(); + conRsp.deserialize(bbia, "connect"); + + // read "is read-only" flag + boolean isRO = false; + try { + isRO = bbia.readBool("readOnly"); + } catch (IOException e) { + // this is ok -- just a packet from an old server which + // doesn't contain readOnly field + LOG.warn("Connected to an old server; r-o mode will be unavailable"); + } + + this.sessionId = conRsp.getSessionId(); + sendThread.onConnected(conRsp.getTimeOut(), this.sessionId, conRsp.getPasswd(), isRO); + } + + abstract boolean isConnected(); + + abstract void connect(InetSocketAddress addr) throws IOException; + + /** + * Returns the address to which the socket is connected. + */ + abstract SocketAddress getRemoteSocketAddress(); + + /** + * Returns the address to which the socket is bound. + */ + abstract SocketAddress getLocalSocketAddress(); + + /** + * Clean up resources for a fresh new socket. + * It's called before reconnect or close. + */ + abstract void cleanup(); + + /** + * new packets are added to outgoingQueue. + */ + abstract void packetAdded(); + + /** + * connState is marked CLOSED and notify ClientCnxnSocket to react. + */ + abstract void onClosing(); + + /** + * Sasl completes. Allows non-priming packgets to be sent. + * Note that this method will only be called if Sasl starts and completes. + */ + abstract void saslCompleted(); + + /** + * being called after ClientCnxn finish PrimeConnection + */ + abstract void connectionPrimed(); + + /** + * Do transportation work: + * - read packets into incomingBuffer. + * - write outgoing queue packets. + * - update relevant timestamp. + * + * @param waitTimeOut timeout in blocking wait. Unit in MilliSecond. + * @param pendingQueue These are the packets that have been sent and + * are waiting for a response. + * @param cnxn + * @throws IOException + * @throws InterruptedException + */ + abstract void doTransport( + int waitTimeOut, + Queue pendingQueue, + ClientCnxn cnxn) throws IOException, InterruptedException; + + /** + * Close the socket. + */ + abstract void testableCloseSocket() throws IOException; + + /** + * Close this client. + */ + abstract void close(); + + /** + * Send Sasl packets directly. + * The Sasl process will send the first (requestHeader == null) packet, + * and then block the doTransport write, + * finally unblock it when finished. + */ + abstract void sendPacket(Packet p) throws IOException; + + protected void initProperties() throws IOException { + try { + packetLen = clientConfig.getInt( + ZKConfig.JUTE_MAXBUFFER, + ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT); + LOG.info("{} value is {} Bytes", ZKConfig.JUTE_MAXBUFFER, packetLen); + } catch (NumberFormatException e) { + String msg = MessageFormat.format( + "Configured value {0} for property {1} can not be parsed to int", + clientConfig.getProperty(ZKConfig.JUTE_MAXBUFFER), + ZKConfig.JUTE_MAXBUFFER); + LOG.error(msg); + throw new IOException(msg); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNIO.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNIO.java new file mode 100644 index 0000000..0c564a5 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNIO.java @@ -0,0 +1,525 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; +import java.nio.channels.UnresolvedAddressException; +import java.nio.channels.UnsupportedAddressTypeException; +import java.util.Iterator; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.LinkedBlockingDeque; + +import org.apache.zookeeper.ClientCnxn.EndOfStreamException; +import org.apache.zookeeper.ClientCnxn.Packet; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.client.ZKClientConfig; +import org.omg.IOP.TAG_MULTIPLE_COMPONENTS; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientCnxnSocketNIO extends ClientCnxnSocket { + + private static final Logger LOG = LoggerFactory.getLogger(ClientCnxnSocketNIO.class); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * NIO 的知识: + * 1、Buffer + * 2、Selector 多路复用器 + * 3、Channel + */ + private final Selector selector = Selector.open(); + + private SelectionKey sockKey; + + private SocketAddress localSocketAddress; + + private SocketAddress remoteSocketAddress; + + ClientCnxnSocketNIO(ZKClientConfig clientConfig) throws IOException { + this.clientConfig = clientConfig; + initProperties(); + } + + @Override + boolean isConnected() { + return sockKey != null; + } + + /** + * @throws InterruptedException + * @throws IOException + */ + void doIO(Queue pendingQueue, ClientCnxn cnxn) throws InterruptedException, IOException { + + // TODO_MA 注释: 获取链接的 SocketChannel + SocketChannel sock = (SocketChannel) sockKey.channel(); + if(sock == null) { + throw new IOException("Socket is null!"); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果可以读 + */ + if(sockKey.isReadable()) { + int rc = sock.read(incomingBuffer); + if(rc < 0) { + throw new EndOfStreamException("Unable to read additional data from server sessionid 0x" + Long + .toHexString(sessionId) + ", likely server has closed socket"); + } + if(!incomingBuffer.hasRemaining()) { + incomingBuffer.flip(); + if(incomingBuffer == lenBuffer) { + recvCount.getAndIncrement(); + readLength(); + } + + // TODO_MA 注释: 如果未链接,读取链接响应 + else if(!initialized) { + readConnectResult(); + enableRead(); + if(findSendablePacket(outgoingQueue, sendThread.tunnelAuthInProgress()) != null) { + // Since SASL authentication has completed (if client is configured to do so), + // outgoing packets waiting in the outgoingQueue can now be sent. + enableWrite(); + } + lenBuffer.clear(); + incomingBuffer = lenBuffer; + updateLastHeard(); + initialized = true; + } + + // TODO_MA 注释: 读取服务端返回来的响应 + else { + sendThread.readResponse(incomingBuffer); + lenBuffer.clear(); + incomingBuffer = lenBuffer; + updateLastHeard(); + } + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果可以写 + */ + if(sockKey.isWritable()) { + + // TODO_MA 注释: outgoingQueue(ConnectRequest, GetDataRequest, CreateRequest) + Packet p = findSendablePacket(outgoingQueue, sendThread.tunnelAuthInProgress()); + + if(p != null) { + updateLastSend(); + // If we already started writing p, p.bb will already exist + if(p.bb == null) { + if((p.requestHeader != null) && (p.requestHeader.getType() != OpCode.ping) && (p.requestHeader + .getType() != OpCode.auth)) { + p.requestHeader.setXid(cnxn.getXid()); + } + + // TODO_MA 注释: 创建发送报文对象 ByteBuffer + // TODO_MA 注释: 把 Packet 对象变成 ByteBuffer + p.createBB(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 写数据包出去 + */ + sock.write(p.bb); + + // TODO_MA 注释: 到此为止, ZooKeeper 内部的 ClientCxn 内部的 SendThread 终于把 outgoingQueue + // TODO_MA 注释: 中的请求对象转换得到的 Packet 创建成 ByteBuffer 报文之后,发送给服务端了。 + + if(!p.bb.hasRemaining()) { + sentCount.getAndIncrement(); + outgoingQueue.removeFirstOccurrence(p); + if(p.requestHeader != null && p.requestHeader.getType() != OpCode.ping && p.requestHeader + .getType() != OpCode.auth) { + synchronized(pendingQueue) { + pendingQueue.add(p); + } + } + } + } + + // TODO_MA 注释: 读写事件的注册处理 + if(outgoingQueue.isEmpty()) { + // No more packets to send: turn off write interest flag. + // Will be turned on later by a later call to enableWrite(), + // from within ZooKeeperSaslClient (if client is configured + // to attempt SASL authentication), or in either doIO() or + // in doTransport() if not. + disableWrite(); + } else if(!initialized && p != null && !p.bb.hasRemaining()) { + // On initial connection, write the complete connect request + // packet, but then disable further writes until after + // receiving a successful connection response. If the + // session is expired, then the server sends the expiration + // response and immediately closes its end of the socket. If + // the client is simultaneously writing on its end, then the + // TCP stack may choose to abort with RST, in which case the + // client would never receive the session expired event. See + // http://docs.oracle.com/javase/6/docs/technotes/guides/net/articles/connection_release.html + disableWrite(); + } else { + // Just in case + enableWrite(); + } + } + } + + private Packet findSendablePacket(LinkedBlockingDeque outgoingQueue, boolean tunneledAuthInProgres) { + if(outgoingQueue.isEmpty()) { + return null; + } + + // TODO_MA 注释: 获取待发送的数据包 + // If we've already starting sending the first packet, we better finish + if(outgoingQueue.getFirst().bb != null || !tunneledAuthInProgres) { + return outgoingQueue.getFirst(); + } + // Since client's authentication with server is in progress, + // send only the null-header packet queued by primeConnection(). + // This packet must be sent so that the SASL authentication process + // can proceed, but all other packets should wait until + // SASL authentication completes. + Iterator iter = outgoingQueue.iterator(); + while(iter.hasNext()) { + Packet p = iter.next(); + if(p.requestHeader == null) { + // We've found the priming-packet. Move it to the beginning of the queue. + iter.remove(); + outgoingQueue.addFirst(p); + return p; + } else { + // Non-priming packet: defer it until later, leaving it in the queue + // until authentication completes. + LOG.debug("Deferring non-priming packet {} until SASL authentication completes.", p); + } + } + return null; + } + + @Override + void cleanup() { + if(sockKey != null) { + SocketChannel sock = (SocketChannel) sockKey.channel(); + sockKey.cancel(); + try { + sock.socket().shutdownInput(); + } catch(IOException e) { + LOG.debug("Ignoring exception during shutdown input", e); + } + try { + sock.socket().shutdownOutput(); + } catch(IOException e) { + LOG.debug("Ignoring exception during shutdown output", e); + } + try { + sock.socket().close(); + } catch(IOException e) { + LOG.debug("Ignoring exception during socket close", e); + } + try { + sock.close(); + } catch(IOException e) { + LOG.debug("Ignoring exception during channel close", e); + } + } + try { + Thread.sleep(100); + } catch(InterruptedException e) { + LOG.debug("SendThread interrupted during sleep, ignoring"); + } + sockKey = null; + } + + @Override + void close() { + try { + if(LOG.isTraceEnabled()) { + LOG.trace("Doing client selector close"); + } + + selector.close(); + + if(LOG.isTraceEnabled()) { + LOG.trace("Closed client selector"); + } + } catch(IOException e) { + LOG.warn("Ignoring exception during selector close", e); + } + } + + /** + * create a socket channel. + * + * @return the created socket channel + * @throws IOException + */ + SocketChannel createSock() throws IOException { + SocketChannel sock; + sock = SocketChannel.open(); + sock.configureBlocking(false); + sock.socket().setSoLinger(false, -1); + sock.socket().setTcpNoDelay(true); + return sock; + } + + /** + * register with the selection and connect + * // TODO_MA 注释: 有两种链接请求: + * // TODO_MA 注释: 1、真正的 TCP 网络链接请求 + * // TODO_MA 注释: 2、发送一个 ConnectRequest 请求对象,给服务端 + * // TODO_MA 注释: 服务端在接收到客户端发送过来的 ConnectRequest 之后执行处理,创建 Session + * + * @param sock the {@link SocketChannel} + * @param addr the address of remote host + * @throws IOException + */ + void registerAndConnect(SocketChannel sock, InetSocketAddress addr) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 注册 OP_CONNECT 事件 + * 1、客户端注册: OP_CONNECT + * 2、服务端注册: OP_ACCEPT + */ + sockKey = sock.register(selector, SelectionKey.OP_CONNECT); + + // TODO_MA 注释: 链接 ZK Server + // TODO_MA 注释: 建立物理链接 + boolean immediateConnect = sock.connect(addr); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 建立逻辑链接 + * 如果马上就完成了,因为异步,不一定马上完成链接 + * 完成了链接之后,发送链接请求给 zk server,建立 Session + */ + if(immediateConnect) { + sendThread.primeConnection(); + } + } + + @Override + void connect(InetSocketAddress addr) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建 NIO 客户端 + */ + SocketChannel sock = createSock(); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 链接 ZK Server + */ + registerAndConnect(sock, addr); + } catch(UnresolvedAddressException | UnsupportedAddressTypeException | SecurityException | IOException e) { + LOG.error("Unable to open socket to {}", addr); + sock.close(); + throw e; + } + initialized = false; + + /* + * Reset incomingBuffer + */ + lenBuffer.clear(); + incomingBuffer = lenBuffer; + } + + /** + * Returns the address to which the socket is connected. + * + * @return ip address of the remote side of the connection or null if not + * connected + */ + @Override + SocketAddress getRemoteSocketAddress() { + return remoteSocketAddress; + } + + /** + * Returns the local address to which the socket is bound. + * + * @return ip address of the remote side of the connection or null if not + * connected + */ + @Override + SocketAddress getLocalSocketAddress() { + return localSocketAddress; + } + + private void updateSocketAddresses() { + Socket socket = ((SocketChannel) sockKey.channel()).socket(); + localSocketAddress = socket.getLocalSocketAddress(); + remoteSocketAddress = socket.getRemoteSocketAddress(); + } + + @Override + void packetAdded() { + + // TODO_MA 注释: + wakeupCnxn(); + } + + @Override + void onClosing() { + wakeupCnxn(); + } + + private synchronized void wakeupCnxn() { + + // TODO_MA 注释: 唤醒 + selector.wakeup(); + } + + @Override + void doTransport(int waitTimeOut, Queue pendingQueue, + ClientCnxn cnxn) throws IOException, InterruptedException { + + // TODO_MA 注释: 获取准备就绪的 事件 + selector.select(waitTimeOut); + Set selected; + synchronized(this) { + selected = selector.selectedKeys(); + } + // Everything below and until we get back to the select is + // non blocking, so time is effectively a constant. That is + // Why we just have to do this once, here + updateNow(); + + // TODO_MA 注释: 遍历准备就绪的事件,执行处理 + for(SelectionKey k : selected) { + SocketChannel sc = ((SocketChannel) k.channel()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 完成链接 + */ + if((k.readyOps() & SelectionKey.OP_CONNECT) != 0) { + + // TODO_MA 注释: 如果当时没有立即建立链接,则在这里完成链接建立 + if(sc.finishConnect()) { + + // TODO_MA 注释: 记录时间 + updateLastSendAndHeard(); + + // TODO_MA 注释: 记录地址 + updateSocketAddresses(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + sendThread.primeConnection(); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 执行 IO 动作 + */ + else if((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) { + + // TODO_MA 注释: IO 处理 + doIO(pendingQueue, cnxn); + } + } + if(sendThread.getZkState().isConnected()) { + if(findSendablePacket(outgoingQueue, sendThread.tunnelAuthInProgress()) != null) { + enableWrite(); + } + } + selected.clear(); + } + + //TODO should this be synchronized? + @Override + void testableCloseSocket() throws IOException { + LOG.info("testableCloseSocket() called"); + // sockKey may be concurrently accessed by multiple + // threads. We use tmp here to avoid a race condition + SelectionKey tmp = sockKey; + if(tmp != null) { + ((SocketChannel) tmp.channel()).socket().close(); + } + } + + @Override + void saslCompleted() { + enableWrite(); + } + + synchronized void enableWrite() { + int i = sockKey.interestOps(); + if((i & SelectionKey.OP_WRITE) == 0) { + sockKey.interestOps(i | SelectionKey.OP_WRITE); + } + } + + private synchronized void disableWrite() { + int i = sockKey.interestOps(); + if((i & SelectionKey.OP_WRITE) != 0) { + sockKey.interestOps(i & (~SelectionKey.OP_WRITE)); + } + } + + private synchronized void enableRead() { + int i = sockKey.interestOps(); + if((i & SelectionKey.OP_READ) == 0) { + sockKey.interestOps(i | SelectionKey.OP_READ); + } + } + + @Override + void connectionPrimed() { + sockKey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE); + } + + Selector getSelector() { + return selector; + } + + @Override + void sendPacket(Packet p) throws IOException { + SocketChannel sock = (SocketChannel) sockKey.channel(); + if(sock == null) { + throw new IOException("Socket is null!"); + } + p.createBB(); + ByteBuffer pbb = p.bb; + sock.write(pbb); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNetty.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNetty.java new file mode 100644 index 0000000..5e61853 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxnSocketNetty.java @@ -0,0 +1,549 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.apache.zookeeper.common.X509Exception.SSLContextException; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.ssl.SslHandler; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.Iterator; +import java.util.Queue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; + +import org.apache.zookeeper.ClientCnxn.EndOfStreamException; +import org.apache.zookeeper.ClientCnxn.Packet; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.NettyUtils; +import org.apache.zookeeper.common.X509Util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * ClientCnxnSocketNetty implements ClientCnxnSocket abstract methods. + * It's responsible for connecting to server, reading/writing network traffic and + * being a layer between network data and higher level packets. + */ +public class ClientCnxnSocketNetty extends ClientCnxnSocket { + + private static final Logger LOG = LoggerFactory.getLogger(ClientCnxnSocketNetty.class); + + private final EventLoopGroup eventLoopGroup; + private Channel channel; + private CountDownLatch firstConnect; + private ChannelFuture connectFuture; + private final Lock connectLock = new ReentrantLock(); + private final AtomicBoolean disconnected = new AtomicBoolean(); + private final AtomicBoolean needSasl = new AtomicBoolean(); + private final Semaphore waitSasl = new Semaphore(0); + + private static final AtomicReference TEST_ALLOCATOR = new AtomicReference<>(null); + + ClientCnxnSocketNetty(ZKClientConfig clientConfig) throws IOException { + this.clientConfig = clientConfig; + // Client only has 1 outgoing socket, so the event loop group only needs + // a single thread. + eventLoopGroup = NettyUtils.newNioOrEpollEventLoopGroup(1 /* nThreads */); + initProperties(); + } + + /** + * lifecycles diagram: + *

      + * loop: + * - try: + * - - !isConnected() + * - - - connect() + * - - doTransport() + * - catch: + * - - cleanup() + * close() + *

      + * Other non-lifecycle methods are in jeopardy getting a null channel + * when calling in concurrency. We must handle it. + */ + + @Override + boolean isConnected() { + // Assuming that isConnected() is only used to initiate connection, + // not used by some other connection status judgement. + connectLock.lock(); + try { + return channel != null || connectFuture != null; + } finally { + connectLock.unlock(); + } + } + + private Bootstrap configureBootstrapAllocator(Bootstrap bootstrap) { + ByteBufAllocator testAllocator = TEST_ALLOCATOR.get(); + if(testAllocator != null) { + return bootstrap.option(ChannelOption.ALLOCATOR, testAllocator); + } else { + return bootstrap; + } + } + + @Override + void connect(InetSocketAddress addr) throws IOException { + firstConnect = new CountDownLatch(1); + + Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NettyUtils.nioOrEpollSocketChannel()) + .option(ChannelOption.SO_LINGER, -1).option(ChannelOption.TCP_NODELAY, true) + .handler(new ZKClientPipelineFactory(addr.getHostString(), addr.getPort())); + bootstrap = configureBootstrapAllocator(bootstrap); + bootstrap.validate(); + + connectLock.lock(); + try { + connectFuture = bootstrap.connect(addr); + connectFuture.addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture channelFuture) throws Exception { + // this lock guarantees that channel won't be assigned after cleanup(). + boolean connected = false; + connectLock.lock(); + try { + if(!channelFuture.isSuccess()) { + LOG.warn("future isn't success.", channelFuture.cause()); + return; + } else if(connectFuture == null) { + LOG.info("connect attempt cancelled"); + // If the connect attempt was cancelled but succeeded + // anyway, make sure to close the channel, otherwise + // we may leak a file descriptor. + channelFuture.channel().close(); + return; + } + // setup channel, variables, connection, etc. + channel = channelFuture.channel(); + + disconnected.set(false); + initialized = false; + lenBuffer.clear(); + incomingBuffer = lenBuffer; + + sendThread.primeConnection(); + updateNow(); + updateLastSendAndHeard(); + + if(sendThread.tunnelAuthInProgress()) { + waitSasl.drainPermits(); + needSasl.set(true); + sendPrimePacket(); + } else { + needSasl.set(false); + } + connected = true; + } finally { + connectFuture = null; + connectLock.unlock(); + if(connected) { + LOG.info("channel is connected: {}", channelFuture.channel()); + } + // need to wake on connect success or failure to avoid + // timing out ClientCnxn.SendThread which may be + // blocked waiting for first connect in doTransport(). + wakeupCnxn(); + firstConnect.countDown(); + } + } + }); + } finally { + connectLock.unlock(); + } + } + + @Override + void cleanup() { + connectLock.lock(); + try { + if(connectFuture != null) { + connectFuture.cancel(false); + connectFuture = null; + } + if(channel != null) { + channel.close().syncUninterruptibly(); + channel = null; + } + } finally { + connectLock.unlock(); + } + Iterator iter = outgoingQueue.iterator(); + while(iter.hasNext()) { + Packet p = iter.next(); + if(p == WakeupPacket.getInstance()) { + iter.remove(); + } + } + } + + @Override + void close() { + eventLoopGroup.shutdownGracefully(); + } + + @Override + void saslCompleted() { + needSasl.set(false); + waitSasl.release(); + } + + @Override + void connectionPrimed() { + } + + @Override + void packetAdded() { + // NO-OP. Adding a packet will already wake up a netty connection + // so we don't need to add a dummy packet to the queue to trigger + // a wake-up. + } + + @Override + void onClosing() { + firstConnect.countDown(); + wakeupCnxn(); + LOG.info("channel is told closing"); + } + + private void wakeupCnxn() { + if(needSasl.get()) { + waitSasl.release(); + } + outgoingQueue.add(WakeupPacket.getInstance()); + } + + @Override + void doTransport(int waitTimeOut, Queue pendingQueue, + ClientCnxn cnxn) throws IOException, InterruptedException { + try { + if(!firstConnect.await(waitTimeOut, TimeUnit.MILLISECONDS)) { + return; + } + Packet head = null; + if(needSasl.get()) { + if(!waitSasl.tryAcquire(waitTimeOut, TimeUnit.MILLISECONDS)) { + return; + } + } else { + head = outgoingQueue.poll(waitTimeOut, TimeUnit.MILLISECONDS); + } + // check if being waken up on closing. + if(!sendThread.getZkState().isAlive()) { + // adding back the packet to notify of failure in conLossPacket(). + addBack(head); + return; + } + // channel disconnection happened + if(disconnected.get()) { + addBack(head); + throw new EndOfStreamException("channel for sessionid 0x" + Long.toHexString(sessionId) + " is lost"); + } + if(head != null) { + doWrite(pendingQueue, head, cnxn); + } + } finally { + updateNow(); + } + } + + private void addBack(Packet head) { + if(head != null && head != WakeupPacket.getInstance()) { + outgoingQueue.addFirst(head); + } + } + + /** + * Sends a packet to the remote peer and flushes the channel. + * + * @param p packet to send. + * @return a ChannelFuture that will complete when the write operation + * succeeds or fails. + */ + private ChannelFuture sendPktAndFlush(Packet p) { + return sendPkt(p, true); + } + + /** + * Sends a packet to the remote peer but does not flush() the channel. + * + * @param p packet to send. + * @return a ChannelFuture that will complete when the write operation + * succeeds or fails. + */ + private ChannelFuture sendPktOnly(Packet p) { + return sendPkt(p, false); + } + + // Use a single listener instance to reduce GC + private final GenericFutureListener> onSendPktDoneListener = f -> { + if(f.isSuccess()) { + sentCount.getAndIncrement(); + } + }; + + private ChannelFuture sendPkt(Packet p, boolean doFlush) { + // Assuming the packet will be sent out successfully. Because if it fails, + // the channel will close and clean up queues. + p.createBB(); + updateLastSend(); + final ByteBuf writeBuffer = Unpooled.wrappedBuffer(p.bb); + final ChannelFuture result = doFlush ? channel.writeAndFlush(writeBuffer) : channel.write(writeBuffer); + result.addListener(onSendPktDoneListener); + return result; + } + + private void sendPrimePacket() { + // assuming the first packet is the priming packet. + sendPktAndFlush(outgoingQueue.remove()); + } + + /** + * doWrite handles writing the packets from outgoingQueue via network to server. + */ + private void doWrite(Queue pendingQueue, Packet p, ClientCnxn cnxn) { + updateNow(); + boolean anyPacketsSent = false; + while(true) { + if(p != WakeupPacket.getInstance()) { + if((p.requestHeader != null) && (p.requestHeader.getType() != ZooDefs.OpCode.ping) && (p.requestHeader + .getType() != ZooDefs.OpCode.auth)) { + p.requestHeader.setXid(cnxn.getXid()); + synchronized(pendingQueue) { + pendingQueue.add(p); + } + } + sendPktOnly(p); + anyPacketsSent = true; + } + if(outgoingQueue.isEmpty()) { + break; + } + p = outgoingQueue.remove(); + } + // TODO: maybe we should flush in the loop above every N packets/bytes? + // But, how do we determine the right value for N ... + if(anyPacketsSent) { + channel.flush(); + } + } + + @Override + void sendPacket(ClientCnxn.Packet p) throws IOException { + if(channel == null) { + throw new IOException("channel has been closed"); + } + sendPktAndFlush(p); + } + + @Override + SocketAddress getRemoteSocketAddress() { + Channel copiedChanRef = channel; + return (copiedChanRef == null) ? null : copiedChanRef.remoteAddress(); + } + + @Override + SocketAddress getLocalSocketAddress() { + Channel copiedChanRef = channel; + return (copiedChanRef == null) ? null : copiedChanRef.localAddress(); + } + + @Override + void testableCloseSocket() throws IOException { + Channel copiedChanRef = channel; + if(copiedChanRef != null) { + copiedChanRef.disconnect().awaitUninterruptibly(); + } + } + + // *************** CientCnxnSocketNetty ****************** + private static class WakeupPacket { + + private static final Packet instance = new Packet(null, null, null, null, null); + + protected WakeupPacket() { + // Exists only to defeat instantiation. + } + + public static Packet getInstance() { + return instance; + } + + } + + /** + * ZKClientPipelineFactory is the netty pipeline factory for this netty + * connection implementation. + */ + private class ZKClientPipelineFactory extends ChannelInitializer { + + private SSLContext sslContext = null; + private SSLEngine sslEngine = null; + private String host; + private int port; + + public ZKClientPipelineFactory(String host, int port) { + this.host = host; + this.port = port; + } + + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + if(clientConfig.getBoolean(ZKClientConfig.SECURE_CLIENT)) { + initSSL(pipeline); + } + pipeline.addLast("handler", new ZKClientHandler()); + } + + // The synchronized is to prevent the race on shared variable "sslEngine". + // Basically we only need to create it once. + private synchronized void initSSL(ChannelPipeline pipeline) throws SSLContextException { + if(sslContext == null || sslEngine == null) { + try(X509Util x509Util = new ClientX509Util()) { + sslContext = x509Util.createSSLContext(clientConfig); + sslEngine = sslContext.createSSLEngine(host, port); + sslEngine.setUseClientMode(true); + } + } + pipeline.addLast("ssl", new SslHandler(sslEngine)); + LOG.info("SSL handler added for channel: {}", pipeline.channel()); + } + + } + + /** + * ZKClientHandler is the netty handler that sits in netty upstream last + * place. It mainly handles read traffic and helps synchronize connection state. + */ + private class ZKClientHandler extends SimpleChannelInboundHandler { + + AtomicBoolean channelClosed = new AtomicBoolean(false); + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + LOG.info("channel is disconnected: {}", ctx.channel()); + cleanup(); + } + + /** + * netty handler has encountered problems. We are cleaning it up and tell outside to close + * the channel/connection. + */ + private void cleanup() { + if(!channelClosed.compareAndSet(false, true)) { + return; + } + disconnected.set(true); + onClosing(); + } + + @Override + protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception { + updateNow(); + while(buf.isReadable()) { + if(incomingBuffer.remaining() > buf.readableBytes()) { + int newLimit = incomingBuffer.position() + buf.readableBytes(); + incomingBuffer.limit(newLimit); + } + buf.readBytes(incomingBuffer); + incomingBuffer.limit(incomingBuffer.capacity()); + + if(!incomingBuffer.hasRemaining()) { + incomingBuffer.flip(); + if(incomingBuffer == lenBuffer) { + recvCount.getAndIncrement(); + readLength(); + } else if(!initialized) { + readConnectResult(); + lenBuffer.clear(); + incomingBuffer = lenBuffer; + initialized = true; + updateLastHeard(); + } else { + sendThread.readResponse(incomingBuffer); + lenBuffer.clear(); + incomingBuffer = lenBuffer; + updateLastHeard(); + } + } + } + wakeupCnxn(); + // Note: SimpleChannelInboundHandler releases the ByteBuf for us + // so we don't need to do it. + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + LOG.error("Unexpected throwable", cause); + cleanup(); + } + + } + + /** + * Sets the test ByteBufAllocator. This allocator will be used by all + * future instances of this class. + * It is not recommended to use this method outside of testing. + * + * @param allocator the ByteBufAllocator to use for all netty buffer + * allocations. + */ + static void setTestAllocator(ByteBufAllocator allocator) { + TEST_ALLOCATOR.set(allocator); + } + + /** + * Clears the test ByteBufAllocator. The default allocator will be used + * by all future instances of this class. + * It is not recommended to use this method outside of testing. + */ + static void clearTestAllocator() { + TEST_ALLOCATOR.set(null); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ClientWatchManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientWatchManager.java new file mode 100644 index 0000000..3458748 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ClientWatchManager.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.Set; + +/** + * 存在于服务端中的一个 Watcher 管理器 + */ +public interface ClientWatchManager { + + /** + * Return a set of watchers that should be notified of the event. The + * manager must not notify the watcher(s), however it will update it's + * internal structure as if the watches had triggered. The intent being + * that the callee is now responsible for notifying the watchers of the + * event, possibly at some later time. + * + * @param state event state + * @param type event type + * @param path event path + * @return may be empty set but must not be null + */ + Set materialize(Watcher.Event.KeeperState state, Watcher.Event.EventType type, String path); + + // TODO_MA 马中华 注释: 该方法的作用,是解析得到需要返回的 Watcher 集合 + // TODO_MA 马中华 注释: 该方法表示事件发生时,返回需要被通知的 Watcher 集合,可能为空集合。 +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/CreateMode.java b/zookeeper-server/src/main/java/org/apache/zookeeper/CreateMode.java new file mode 100644 index 0000000..139722a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/CreateMode.java @@ -0,0 +1,172 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/*** + * CreateMode value determines how the znode is created on ZooKeeper. + */ +@InterfaceAudience.Public +public enum CreateMode { + + /** + * The znode will not be automatically deleted upon client's disconnect. + */ + PERSISTENT(0, false, false, false, false), + /** + * The znode will not be automatically deleted upon client's disconnect, + * and its name will be appended with a monotonically increasing number. + */ + PERSISTENT_SEQUENTIAL(2, false, true, false, false), + /** + * The znode will be deleted upon the client's disconnect. + */ + EPHEMERAL(1, true, false, false, false), + /** + * The znode will be deleted upon the client's disconnect, and its name + * will be appended with a monotonically increasing number. + */ + EPHEMERAL_SEQUENTIAL(3, true, true, false, false), + /** + * The znode will be a container node. Container + * nodes are special purpose nodes useful for recipes such as leader, lock, + * etc. When the last child of a container is deleted, the container becomes + * a candidate to be deleted by the server at some point in the future. + * Given this property, you should be prepared to get + * {@link org.apache.zookeeper.KeeperException.NoNodeException} + * when creating children inside of this container node. + */ + CONTAINER(4, false, false, true, false), + /** + * The znode will not be automatically deleted upon client's disconnect. + * However if the znode has not been modified within the given TTL, it + * will be deleted once it has no children. + */ + PERSISTENT_WITH_TTL(5, false, false, false, true), + /** + * The znode will not be automatically deleted upon client's disconnect, + * and its name will be appended with a monotonically increasing number. + * However if the znode has not been modified within the given TTL, it + * will be deleted once it has no children. + */ + PERSISTENT_SEQUENTIAL_WITH_TTL(6, false, true, false, true); + + private static final Logger LOG = LoggerFactory.getLogger(CreateMode.class); + + private boolean ephemeral; + private boolean sequential; + private final boolean isContainer; + private int flag; + private boolean isTTL; + + CreateMode(int flag, boolean ephemeral, boolean sequential, boolean isContainer, boolean isTTL) { + this.flag = flag; + this.ephemeral = ephemeral; + this.sequential = sequential; + this.isContainer = isContainer; + this.isTTL = isTTL; + } + + public boolean isEphemeral() { + return ephemeral; + } + + public boolean isSequential() { + return sequential; + } + + public boolean isContainer() { + return isContainer; + } + + public boolean isTTL() { + return isTTL; + } + + public int toFlag() { + return flag; + } + + /** + * Map an integer value to a CreateMode value + */ + public static CreateMode fromFlag(int flag) throws KeeperException { + switch (flag) { + case 0: + return CreateMode.PERSISTENT; + + case 1: + return CreateMode.EPHEMERAL; + + case 2: + return CreateMode.PERSISTENT_SEQUENTIAL; + + case 3: + return CreateMode.EPHEMERAL_SEQUENTIAL; + + case 4: + return CreateMode.CONTAINER; + + case 5: + return CreateMode.PERSISTENT_WITH_TTL; + + case 6: + return CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL; + + default: + String errMsg = "Received an invalid flag value: " + flag + " to convert to a CreateMode"; + LOG.error(errMsg); + throw new KeeperException.BadArgumentsException(errMsg); + } + } + + /** + * Map an integer value to a CreateMode value + */ + public static CreateMode fromFlag(int flag, CreateMode defaultMode) { + switch (flag) { + case 0: + return CreateMode.PERSISTENT; + + case 1: + return CreateMode.EPHEMERAL; + + case 2: + return CreateMode.PERSISTENT_SEQUENTIAL; + + case 3: + return CreateMode.EPHEMERAL_SEQUENTIAL; + + case 4: + return CreateMode.CONTAINER; + + case 5: + return CreateMode.PERSISTENT_WITH_TTL; + + case 6: + return CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL; + + default: + return defaultMode; + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/DigestWatcher.java b/zookeeper-server/src/main/java/org/apache/zookeeper/DigestWatcher.java new file mode 100644 index 0000000..e8662df --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/DigestWatcher.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * This interface is used to notify the digest mismatch event. + */ +public interface DigestWatcher { + + /** + * Called when the digest mismatch is found on a given zxid. + * + * @param mismatchZxid the zxid when the digest mismatch happened. + */ + void process(long mismatchZxid); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Environment.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Environment.java new file mode 100644 index 0000000..9ec38a1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Environment.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; +import org.slf4j.Logger; + +/** + * Provide insight into the runtime environment. + * + */ +public class Environment { + + public static final String JAAS_CONF_KEY = "java.security.auth.login.config"; + + public static class Entry { + + private String k; + private String v; + public Entry(String k, String v) { + this.k = k; + this.v = v; + } + public String getKey() { + return k; + } + public String getValue() { + return v; + } + + @Override + public String toString() { + return k + "=" + v; + } + + } + + private static void put(List l, String k, String v) { + l.add(new Entry(k, v)); + } + + public static List list() { + List l = new ArrayList(); + put(l, "zookeeper.version", Version.getFullVersion()); + + try { + put(l, "host.name", InetAddress.getLocalHost().getCanonicalHostName()); + } catch (UnknownHostException e) { + put(l, "host.name", ""); + } + + put(l, "java.version", System.getProperty("java.version", "")); + put(l, "java.vendor", System.getProperty("java.vendor", "")); + put(l, "java.home", System.getProperty("java.home", "")); + put(l, "java.class.path", System.getProperty("java.class.path", "")); + put(l, "java.library.path", System.getProperty("java.library.path", "")); + put(l, "java.io.tmpdir", System.getProperty("java.io.tmpdir", "")); + put(l, "java.compiler", System.getProperty("java.compiler", "")); + put(l, "os.name", System.getProperty("os.name", "")); + put(l, "os.arch", System.getProperty("os.arch", "")); + put(l, "os.version", System.getProperty("os.version", "")); + put(l, "user.name", System.getProperty("user.name", "")); + put(l, "user.home", System.getProperty("user.home", "")); + put(l, "user.dir", System.getProperty("user.dir", "")); + + // Get memory information. + Runtime runtime = Runtime.getRuntime(); + int mb = 1024 * 1024; + put(l, "os.memory.free", runtime.freeMemory() / mb + "MB"); + put(l, "os.memory.max", runtime.maxMemory() / mb + "MB"); + put(l, "os.memory.total", runtime.totalMemory() / mb + "MB"); + + return l; + } + + public static void logEnv(String msg, Logger log) { + List env = Environment.list(); + for (Entry e : env) { + log.info(msg + e.toString()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/JLineZNodeCompleter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/JLineZNodeCompleter.java new file mode 100644 index 0000000..e555214 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/JLineZNodeCompleter.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.Collections; +import java.util.List; +import jline.console.completer.Completer; + +class JLineZNodeCompleter implements Completer { + + private ZooKeeper zk; + + public JLineZNodeCompleter(ZooKeeper zk) { + this.zk = zk; + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + public int complete(String buffer, int cursor, List candidates) { + // Guarantee that the final token is the one we're expanding + buffer = buffer.substring(0, cursor); + String token = ""; + if (!buffer.endsWith(" ")) { + String[] tokens = buffer.split(" "); + if (tokens.length != 0) { + token = tokens[tokens.length - 1]; + } + } + + if (token.startsWith("/")) { + return completeZNode(buffer, token, candidates); + } + return completeCommand(buffer, token, candidates); + } + + private int completeCommand(String buffer, String token, List candidates) { + for (String cmd : ZooKeeperMain.getCommands()) { + if (cmd.startsWith(token)) { + candidates.add(cmd); + } + } + return buffer.lastIndexOf(" ") + 1; + } + + private int completeZNode(String buffer, String token, List candidates) { + String path = token; + int idx = path.lastIndexOf("/") + 1; + String prefix = path.substring(idx); + try { + // Only the root path can end in a /, so strip it off every other prefix + String dir = idx == 1 ? "/" : path.substring(0, idx - 1); + List children = zk.getChildren(dir, false); + for (String child : children) { + if (child.startsWith(prefix)) { + candidates.add(child); + } + } + } catch (InterruptedException e) { + return 0; + } catch (KeeperException e) { + return 0; + } + Collections.sort(candidates); + return candidates.size() == 0 ? buffer.length() : buffer.lastIndexOf("/") + 1; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/KeeperException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/KeeperException.java new file mode 100644 index 0000000..5cff6f3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/KeeperException.java @@ -0,0 +1,943 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.yetus.audience.InterfaceAudience; + +@SuppressWarnings("serial") +@InterfaceAudience.Public +public abstract class KeeperException extends Exception { + + /** + * All multi-requests that result in an exception retain the results + * here so that it is possible to examine the problems in the catch + * scope. Non-multi requests will get a null if they try to access + * these results. + */ + private List results; + + /** + * All non-specific keeper exceptions should be constructed via + * this factory method in order to guarantee consistency in error + * codes and such. If you know the error code, then you should + * construct the special purpose exception directly. That will + * allow you to have the most specific possible declarations of + * what exceptions might actually be thrown. + * + * @param code The error code. + * @param path The ZooKeeper path being operated on. + * @return The specialized exception, presumably to be thrown by + * the caller. + */ + public static KeeperException create(Code code, String path) { + KeeperException r = create(code); + r.path = path; + return r; + } + + /** + * @deprecated deprecated in 3.1.0, use {@link #create(Code, String)} + * instead + */ + @Deprecated + public static KeeperException create(int code, String path) { + KeeperException r = create(Code.get(code)); + r.path = path; + return r; + } + + /** + * @deprecated deprecated in 3.1.0, use {@link #create(Code)} + * instead + */ + @Deprecated + public static KeeperException create(int code) { + return create(Code.get(code)); + } + + /** + * All non-specific keeper exceptions should be constructed via + * this factory method in order to guarantee consistency in error + * codes and such. If you know the error code, then you should + * construct the special purpose exception directly. That will + * allow you to have the most specific possible declarations of + * what exceptions might actually be thrown. + * + * @param code The error code of your new exception. This will + * also determine the specific type of the exception that is + * returned. + * @return The specialized exception, presumably to be thrown by + * the caller. + */ + public static KeeperException create(Code code) { + switch (code) { + case SYSTEMERROR: + return new SystemErrorException(); + case RUNTIMEINCONSISTENCY: + return new RuntimeInconsistencyException(); + case DATAINCONSISTENCY: + return new DataInconsistencyException(); + case CONNECTIONLOSS: + return new ConnectionLossException(); + case MARSHALLINGERROR: + return new MarshallingErrorException(); + case UNIMPLEMENTED: + return new UnimplementedException(); + case OPERATIONTIMEOUT: + return new OperationTimeoutException(); + case NEWCONFIGNOQUORUM: + return new NewConfigNoQuorum(); + case RECONFIGINPROGRESS: + return new ReconfigInProgress(); + case BADARGUMENTS: + return new BadArgumentsException(); + case APIERROR: + return new APIErrorException(); + case NONODE: + return new NoNodeException(); + case NOAUTH: + return new NoAuthException(); + case BADVERSION: + return new BadVersionException(); + case NOCHILDRENFOREPHEMERALS: + return new NoChildrenForEphemeralsException(); + case NODEEXISTS: + return new NodeExistsException(); + case INVALIDACL: + return new InvalidACLException(); + case AUTHFAILED: + return new AuthFailedException(); + case NOTEMPTY: + return new NotEmptyException(); + case SESSIONEXPIRED: + return new SessionExpiredException(); + case INVALIDCALLBACK: + return new InvalidCallbackException(); + case SESSIONMOVED: + return new SessionMovedException(); + case NOTREADONLY: + return new NotReadOnlyException(); + case EPHEMERALONLOCALSESSION: + return new EphemeralOnLocalSessionException(); + case NOWATCHER: + return new NoWatcherException(); + case RECONFIGDISABLED: + return new ReconfigDisabledException(); + case SESSIONCLOSEDREQUIRESASLAUTH: + return new SessionClosedRequireAuthException(); + case REQUESTTIMEOUT: + return new RequestTimeoutException(); + case OK: + default: + throw new IllegalArgumentException("Invalid exception code"); + } + } + + /** + * Set the code for this exception + * @param code error code + * @deprecated deprecated in 3.1.0, exceptions should be immutable, this + * method should not be used + */ + @Deprecated + public void setCode(int code) { + this.code = Code.get(code); + } + + /** This interface contains the original static final int constants + * which have now been replaced with an enumeration in Code. Do not + * reference this class directly, if necessary (legacy code) continue + * to access the constants through Code. + * Note: an interface is used here due to the fact that enums cannot + * reference constants defined within the same enum as said constants + * are considered initialized _after_ the enum itself. By using an + * interface as a super type this allows the deprecated constants to + * be initialized first and referenced when constructing the enums. I + * didn't want to have constants declared twice. This + * interface should be private, but it's declared public to enable + * javadoc to include in the user API spec. + */ + @Deprecated + @InterfaceAudience.Public + public interface CodeDeprecated { + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#OK} instead + */ + @Deprecated + int Ok = 0; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#SYSTEMERROR} instead + */ + @Deprecated + int SystemError = -1; + /** + * @deprecated deprecated in 3.1.0, use + * {@link Code#RUNTIMEINCONSISTENCY} instead + */ + @Deprecated + int RuntimeInconsistency = -2; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#DATAINCONSISTENCY} + * instead + */ + @Deprecated + int DataInconsistency = -3; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#CONNECTIONLOSS} + * instead + */ + @Deprecated + int ConnectionLoss = -4; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#MARSHALLINGERROR} + * instead + */ + @Deprecated + int MarshallingError = -5; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#UNIMPLEMENTED} + * instead + */ + @Deprecated + int Unimplemented = -6; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#OPERATIONTIMEOUT} + * instead + */ + @Deprecated + int OperationTimeout = -7; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#BADARGUMENTS} + * instead + */ + @Deprecated + int BadArguments = -8; + + @Deprecated + int UnknownSession = -12; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NEWCONFIGNOQUORUM} + * instead + */ + @Deprecated + int NewConfigNoQuorum = -13; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#RECONFIGINPROGRESS} + * instead + */ + @Deprecated + int ReconfigInProgress = -14; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#APIERROR} instead + */ + @Deprecated + int APIError = -100; + + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NONODE} instead + */ + @Deprecated + int NoNode = -101; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NOAUTH} instead + */ + @Deprecated + int NoAuth = -102; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#BADVERSION} instead + */ + @Deprecated + int BadVersion = -103; + /** + * @deprecated deprecated in 3.1.0, use + * {@link Code#NOCHILDRENFOREPHEMERALS} + * instead + */ + @Deprecated + int NoChildrenForEphemerals = -108; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NODEEXISTS} instead + */ + @Deprecated + int NodeExists = -110; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#NOTEMPTY} instead + */ + @Deprecated + int NotEmpty = -111; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#SESSIONEXPIRED} instead + */ + @Deprecated + int SessionExpired = -112; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#INVALIDCALLBACK} + * instead + */ + @Deprecated + int InvalidCallback = -113; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#INVALIDACL} instead + */ + @Deprecated + int InvalidACL = -114; + /** + * @deprecated deprecated in 3.1.0, use {@link Code#AUTHFAILED} instead + */ + @Deprecated + int AuthFailed = -115; + + // This value will be used directly in {@link CODE#SESSIONMOVED} + // public static final int SessionMoved = -118; + + @Deprecated + int EphemeralOnLocalSession = -120; + + } + + /** Codes which represent the various KeeperException + * types. This enum replaces the deprecated earlier static final int + * constants. The old, deprecated, values are in "camel case" while the new + * enum values are in all CAPS. + */ + @InterfaceAudience.Public + public enum Code implements CodeDeprecated { + /** Everything is OK */ + OK(Ok), + + /** System and server-side errors. + * This is never thrown by the server, it shouldn't be used other than + * to indicate a range. Specifically error codes greater than this + * value, but lesser than {@link #APIERROR}, are system errors. + */ + SYSTEMERROR(SystemError), + + /** A runtime inconsistency was found */ + RUNTIMEINCONSISTENCY(RuntimeInconsistency), + /** A data inconsistency was found */ + DATAINCONSISTENCY(DataInconsistency), + /** Connection to the server has been lost */ + CONNECTIONLOSS(ConnectionLoss), + /** Error while marshalling or unmarshalling data */ + MARSHALLINGERROR(MarshallingError), + /** Operation is unimplemented */ + UNIMPLEMENTED(Unimplemented), + /** Operation timeout */ + OPERATIONTIMEOUT(OperationTimeout), + /** Invalid arguments */ + BADARGUMENTS(BadArguments), + /** No quorum of new config is connected and up-to-date with the leader of last commmitted config - try + * invoking reconfiguration after new servers are connected and synced */ + NEWCONFIGNOQUORUM(NewConfigNoQuorum), + /** Another reconfiguration is in progress -- concurrent reconfigs not supported (yet) */ + RECONFIGINPROGRESS(ReconfigInProgress), + /** Unknown session (internal server use only) */ + UNKNOWNSESSION(UnknownSession), + + /** API errors. + * This is never thrown by the server, it shouldn't be used other than + * to indicate a range. Specifically error codes greater than this + * value are API errors (while values less than this indicate a + * {@link #SYSTEMERROR}). + */ + APIERROR(APIError), + + /** Node does not exist */ + NONODE(NoNode), + /** Not authenticated */ + NOAUTH(NoAuth), + /** Version conflict + In case of reconfiguration: reconfig requested from config version X but last seen config has a different version Y */ + BADVERSION(BadVersion), + /** Ephemeral nodes may not have children */ + NOCHILDRENFOREPHEMERALS(NoChildrenForEphemerals), + /** The node already exists */ + NODEEXISTS(NodeExists), + /** The node has children */ + NOTEMPTY(NotEmpty), + /** The session has been expired by the server */ + SESSIONEXPIRED(SessionExpired), + /** Invalid callback specified */ + INVALIDCALLBACK(InvalidCallback), + /** Invalid ACL specified */ + INVALIDACL(InvalidACL), + /** Client authentication failed */ + AUTHFAILED(AuthFailed), + /** Session moved to another server, so operation is ignored */ + SESSIONMOVED(-118), + /** State-changing request is passed to read-only server */ + NOTREADONLY(-119), + /** Attempt to create ephemeral node on a local session */ + EPHEMERALONLOCALSESSION(EphemeralOnLocalSession), + /** Attempts to remove a non-existing watcher */ + NOWATCHER(-121), + /** Request not completed within max allowed time.*/ + REQUESTTIMEOUT(-122), + /** Attempts to perform a reconfiguration operation when reconfiguration feature is disabled. */ + RECONFIGDISABLED(-123), + /** The session has been closed by server because server requires client to do SASL authentication, + * but client is not configured with SASL authentication or configuted with SASL but failed + * (i.e. wrong credential used.). */ + SESSIONCLOSEDREQUIRESASLAUTH(-124); + + private static final Map lookup = new HashMap(); + + static { + for (Code c : EnumSet.allOf(Code.class)) { + lookup.put(c.code, c); + } + } + + private final int code; + Code(int code) { + this.code = code; + } + + /** + * Get the int value for a particular Code. + * @return error code as integer + */ + public int intValue() { + return code; + } + + /** + * Get the Code value for a particular integer error code + * @param code int error code + * @return Code value corresponding to specified int code, or null + */ + public static Code get(int code) { + return lookup.get(code); + } + } + + static String getCodeMessage(Code code) { + switch (code) { + case OK: + return "ok"; + case SYSTEMERROR: + return "SystemError"; + case RUNTIMEINCONSISTENCY: + return "RuntimeInconsistency"; + case DATAINCONSISTENCY: + return "DataInconsistency"; + case CONNECTIONLOSS: + return "ConnectionLoss"; + case MARSHALLINGERROR: + return "MarshallingError"; + case NEWCONFIGNOQUORUM: + return "NewConfigNoQuorum"; + case RECONFIGINPROGRESS: + return "ReconfigInProgress"; + case UNIMPLEMENTED: + return "Unimplemented"; + case OPERATIONTIMEOUT: + return "OperationTimeout"; + case BADARGUMENTS: + return "BadArguments"; + case APIERROR: + return "APIError"; + case NONODE: + return "NoNode"; + case NOAUTH: + return "NoAuth"; + case BADVERSION: + return "BadVersion"; + case NOCHILDRENFOREPHEMERALS: + return "NoChildrenForEphemerals"; + case NODEEXISTS: + return "NodeExists"; + case INVALIDACL: + return "InvalidACL"; + case AUTHFAILED: + return "AuthFailed"; + case NOTEMPTY: + return "Directory not empty"; + case SESSIONEXPIRED: + return "Session expired"; + case INVALIDCALLBACK: + return "Invalid callback"; + case SESSIONMOVED: + return "Session moved"; + case NOTREADONLY: + return "Not a read-only call"; + case EPHEMERALONLOCALSESSION: + return "Ephemeral node on local session"; + case NOWATCHER: + return "No such watcher"; + case RECONFIGDISABLED: + return "Reconfig is disabled"; + case SESSIONCLOSEDREQUIRESASLAUTH: + return "Session closed because client failed to authenticate"; + default: + return "Unknown error " + code; + } + } + + private Code code; + + private String path; + + public KeeperException(Code code) { + this.code = code; + } + + KeeperException(Code code, String path) { + this.code = code; + this.path = path; + } + + /** + * Read the error code for this exception + * @return the error code for this exception + * @deprecated deprecated in 3.1.0, use {@link #code()} instead + */ + @Deprecated + public int getCode() { + return code.code; + } + + /** + * Read the error Code for this exception + * @return the error Code for this exception + */ + public Code code() { + return code; + } + + /** + * Read the path for this exception + * @return the path associated with this error, null if none + */ + public String getPath() { + return path; + } + + @Override + public String getMessage() { + if (path == null || path.isEmpty()) { + return "KeeperErrorCode = " + getCodeMessage(code); + } + return "KeeperErrorCode = " + getCodeMessage(code) + " for " + path; + } + + void setMultiResults(List results) { + this.results = results; + } + + /** + * If this exception was thrown by a multi-request then the (partial) results + * and error codes can be retrieved using this getter. + * @return A copy of the list of results from the operations in the multi-request. + * + * @since 3.4.0 + * + */ + public List getResults() { + return results != null ? new ArrayList(results) : null; + } + + /** + * @see Code#APIERROR + */ + @InterfaceAudience.Public + public static class APIErrorException extends KeeperException { + + public APIErrorException() { + super(Code.APIERROR); + } + + } + + /** + * @see Code#AUTHFAILED + */ + @InterfaceAudience.Public + public static class AuthFailedException extends KeeperException { + + public AuthFailedException() { + super(Code.AUTHFAILED); + } + + } + + /** + * @see Code#BADARGUMENTS + */ + @InterfaceAudience.Public + public static class BadArgumentsException extends KeeperException { + + public BadArgumentsException() { + super(Code.BADARGUMENTS); + } + public BadArgumentsException(String path) { + super(Code.BADARGUMENTS, path); + } + + } + + /** + * @see Code#BADVERSION + */ + @InterfaceAudience.Public + public static class BadVersionException extends KeeperException { + + public BadVersionException() { + super(Code.BADVERSION); + } + public BadVersionException(String path) { + super(Code.BADVERSION, path); + } + + } + + /** + * @see Code#CONNECTIONLOSS + */ + @InterfaceAudience.Public + public static class ConnectionLossException extends KeeperException { + + public ConnectionLossException() { + super(Code.CONNECTIONLOSS); + } + + } + + /** + * @see Code#DATAINCONSISTENCY + */ + @InterfaceAudience.Public + public static class DataInconsistencyException extends KeeperException { + + public DataInconsistencyException() { + super(Code.DATAINCONSISTENCY); + } + + } + + /** + * @see Code#INVALIDACL + */ + @InterfaceAudience.Public + public static class InvalidACLException extends KeeperException { + + public InvalidACLException() { + super(Code.INVALIDACL); + } + public InvalidACLException(String path) { + super(Code.INVALIDACL, path); + } + + } + + /** + * @see Code#INVALIDCALLBACK + */ + @InterfaceAudience.Public + public static class InvalidCallbackException extends KeeperException { + + public InvalidCallbackException() { + super(Code.INVALIDCALLBACK); + } + + } + + /** + * @see Code#MARSHALLINGERROR + */ + @InterfaceAudience.Public + public static class MarshallingErrorException extends KeeperException { + + public MarshallingErrorException() { + super(Code.MARSHALLINGERROR); + } + + } + + /** + * @see Code#NOAUTH + */ + @InterfaceAudience.Public + public static class NoAuthException extends KeeperException { + + public NoAuthException() { + super(Code.NOAUTH); + } + + } + + /** + * @see Code#NEWCONFIGNOQUORUM + */ + @InterfaceAudience.Public + public static class NewConfigNoQuorum extends KeeperException { + + public NewConfigNoQuorum() { + super(Code.NEWCONFIGNOQUORUM); + } + + } + + /** + * @see Code#RECONFIGINPROGRESS + */ + @InterfaceAudience.Public + public static class ReconfigInProgress extends KeeperException { + + public ReconfigInProgress() { + super(Code.RECONFIGINPROGRESS); + } + + } + + /** + * @see Code#NOCHILDRENFOREPHEMERALS + */ + @InterfaceAudience.Public + public static class NoChildrenForEphemeralsException extends KeeperException { + + public NoChildrenForEphemeralsException() { + super(Code.NOCHILDRENFOREPHEMERALS); + } + public NoChildrenForEphemeralsException(String path) { + super(Code.NOCHILDRENFOREPHEMERALS, path); + } + + } + + /** + * @see Code#NODEEXISTS + */ + @InterfaceAudience.Public + public static class NodeExistsException extends KeeperException { + + public NodeExistsException() { + super(Code.NODEEXISTS); + } + public NodeExistsException(String path) { + super(Code.NODEEXISTS, path); + } + + } + + /** + * @see Code#NONODE + */ + @InterfaceAudience.Public + public static class NoNodeException extends KeeperException { + + public NoNodeException() { + super(Code.NONODE); + } + public NoNodeException(String path) { + super(Code.NONODE, path); + } + + } + + /** + * @see Code#NOTEMPTY + */ + @InterfaceAudience.Public + public static class NotEmptyException extends KeeperException { + + public NotEmptyException() { + super(Code.NOTEMPTY); + } + public NotEmptyException(String path) { + super(Code.NOTEMPTY, path); + } + + } + + /** + * @see Code#OPERATIONTIMEOUT + */ + @InterfaceAudience.Public + public static class OperationTimeoutException extends KeeperException { + + public OperationTimeoutException() { + super(Code.OPERATIONTIMEOUT); + } + + } + + /** + * @see Code#RUNTIMEINCONSISTENCY + */ + @InterfaceAudience.Public + public static class RuntimeInconsistencyException extends KeeperException { + + public RuntimeInconsistencyException() { + super(Code.RUNTIMEINCONSISTENCY); + } + + } + + /** + * @see Code#SESSIONEXPIRED + */ + @InterfaceAudience.Public + public static class SessionExpiredException extends KeeperException { + + public SessionExpiredException() { + super(Code.SESSIONEXPIRED); + } + + } + + /** + * @see Code#UNKNOWNSESSION + */ + @InterfaceAudience.Public + public static class UnknownSessionException extends KeeperException { + + public UnknownSessionException() { + super(Code.UNKNOWNSESSION); + } + + } + + /** + * @see Code#SESSIONMOVED + */ + @InterfaceAudience.Public + public static class SessionMovedException extends KeeperException { + + public SessionMovedException() { + super(Code.SESSIONMOVED); + } + + } + + /** + * @see Code#NOTREADONLY + */ + @InterfaceAudience.Public + public static class NotReadOnlyException extends KeeperException { + + public NotReadOnlyException() { + super(Code.NOTREADONLY); + } + + } + + /** + * @see Code#EPHEMERALONLOCALSESSION + */ + @InterfaceAudience.Public + public static class EphemeralOnLocalSessionException extends KeeperException { + + public EphemeralOnLocalSessionException() { + super(Code.EPHEMERALONLOCALSESSION); + } + + } + + /** + * @see Code#SYSTEMERROR + */ + @InterfaceAudience.Public + public static class SystemErrorException extends KeeperException { + + public SystemErrorException() { + super(Code.SYSTEMERROR); + } + + } + + /** + * @see Code#UNIMPLEMENTED + */ + @InterfaceAudience.Public + public static class UnimplementedException extends KeeperException { + + public UnimplementedException() { + super(Code.UNIMPLEMENTED); + } + + } + + /** + * @see Code#NOWATCHER + */ + @InterfaceAudience.Public + public static class NoWatcherException extends KeeperException { + + public NoWatcherException() { + super(Code.NOWATCHER); + } + + public NoWatcherException(String path) { + super(Code.NOWATCHER, path); + } + + } + + /** + * @see Code#RECONFIGDISABLED + */ + @InterfaceAudience.Public + public static class ReconfigDisabledException extends KeeperException { + + public ReconfigDisabledException() { + super(Code.RECONFIGDISABLED); + } + public ReconfigDisabledException(String path) { + super(Code.RECONFIGDISABLED, path); + } + + } + + /** + * @see Code#SESSIONCLOSEDREQUIRESASLAUTH + */ + public static class SessionClosedRequireAuthException extends KeeperException { + + public SessionClosedRequireAuthException() { + super(Code.SESSIONCLOSEDREQUIRESASLAUTH); + } + public SessionClosedRequireAuthException(String path) { + super(Code.SESSIONCLOSEDREQUIRESASLAUTH, path); + } + + } + + /** + * @see Code#REQUESTTIMEOUT + */ + public static class RequestTimeoutException extends KeeperException { + + public RequestTimeoutException() { + super(Code.REQUESTTIMEOUT); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Login.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Login.java new file mode 100644 index 0000000..e74cddb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Login.java @@ -0,0 +1,441 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * This class is responsible for refreshing Kerberos credentials for + * logins for both Zookeeper client and server. + * See ZooKeeperSaslServer for server-side usage. + * See ZooKeeperSaslClient for client-side usage. + */ + +import java.util.Date; +import java.util.Set; +import java.util.concurrent.ThreadLocalRandom; +import javax.security.auth.Subject; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.kerberos.KerberosPrincipal; +import javax.security.auth.kerberos.KerberosTicket; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginContext; +import javax.security.auth.login.LoginException; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.server.ZooKeeperSaslServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Login { + + private static final String KINIT_COMMAND_DEFAULT = "/usr/bin/kinit"; + private static final Logger LOG = LoggerFactory.getLogger(Login.class); + public static final String SYSTEM_USER = System.getProperty("user.name", ""); + public CallbackHandler callbackHandler; + + // LoginThread will sleep until 80% of time from last refresh to + // ticket's expiry has been reached, at which time it will wake + // and try to renew the ticket. + private static final float TICKET_RENEW_WINDOW = 0.80f; + + /** + * Percentage of random jitter added to the renewal time + */ + private static final float TICKET_RENEW_JITTER = 0.05f; + + // Regardless of TICKET_RENEW_WINDOW setting above and the ticket expiry time, + // thread will not sleep between refresh attempts any less than 1 minute (60*1000 milliseconds = 1 minute). + // Change the '1' to e.g. 5, to change this to 5 minutes. + private static final long MIN_TIME_BEFORE_RELOGIN = 1 * 60 * 1000L; + + private Subject subject = null; + private Thread t = null; + private boolean isKrbTicket = false; + private boolean isUsingTicketCache = false; + + private LoginContext login = null; + private String loginContextName = null; + private String principal = null; + + // Initialize 'lastLogin' to do a login at first time + private long lastLogin = Time.currentElapsedTime() - MIN_TIME_BEFORE_RELOGIN; + private final ZKConfig zkConfig; + + /** + * LoginThread constructor. The constructor starts the thread used to + * periodically re-login to the Kerberos Ticket Granting Server. + * + * @param loginContextName + * name of section in JAAS file that will be use to login. Passed + * as first param to javax.security.auth.login.LoginContext(). + * + * @param callbackHandler + * Passed as second param to + * javax.security.auth.login.LoginContext(). + * @param zkConfig + * client or server configurations + * @throws javax.security.auth.login.LoginException + * Thrown if authentication fails. + */ + public Login(final String loginContextName, CallbackHandler callbackHandler, final ZKConfig zkConfig) throws LoginException { + this.zkConfig = zkConfig; + this.callbackHandler = callbackHandler; + login = login(loginContextName); + this.loginContextName = loginContextName; + subject = login.getSubject(); + isKrbTicket = !subject.getPrivateCredentials(KerberosTicket.class).isEmpty(); + AppConfigurationEntry[] entries = Configuration.getConfiguration().getAppConfigurationEntry(loginContextName); + for (AppConfigurationEntry entry : entries) { + // there will only be a single entry, so this for() loop will only be iterated through once. + if (entry.getOptions().get("useTicketCache") != null) { + String val = (String) entry.getOptions().get("useTicketCache"); + if (val.equals("true")) { + isUsingTicketCache = true; + } + } + if (entry.getOptions().get("principal") != null) { + principal = (String) entry.getOptions().get("principal"); + } + break; + } + + if (!isKrbTicket) { + // if no TGT, do not bother with ticket management. + return; + } + + // Refresh the Ticket Granting Ticket (TGT) periodically. How often to refresh is determined by the + // TGT's existing expiry date and the configured MIN_TIME_BEFORE_RELOGIN. For testing and development, + // you can decrease the interval of expiration of tickets (for example, to 3 minutes) by running : + // "modprinc -maxlife 3mins " in kadmin. + t = new Thread(new Runnable() { + public void run() { + LOG.info("TGT refresh thread started."); + while (true) { // renewal thread's main loop. if it exits from here, thread will exit. + KerberosTicket tgt = getTGT(); + long now = Time.currentWallTime(); + long nextRefresh; + Date nextRefreshDate; + if (tgt == null) { + nextRefresh = now + MIN_TIME_BEFORE_RELOGIN; + nextRefreshDate = new Date(nextRefresh); + LOG.warn("No TGT found: will try again at {}", nextRefreshDate); + } else { + nextRefresh = getRefreshTime(tgt); + long expiry = tgt.getEndTime().getTime(); + Date expiryDate = new Date(expiry); + if ((isUsingTicketCache) && (tgt.getEndTime().equals(tgt.getRenewTill()))) { + LOG.error( + "The TGT cannot be renewed beyond the next expiry date: {}." + + "This process will not be able to authenticate new SASL connections after that " + + "time (for example, it will not be authenticate a new connection with a Zookeeper " + + "Quorum member). Ask your system administrator to either increase the " + + "'renew until' time by doing : 'modprinc -maxrenewlife {}' within " + + "kadmin, or instead, to generate a keytab for {}. Because the TGT's " + + "expiry cannot be further extended by refreshing, exiting refresh thread now.", + expiryDate, + principal, + principal); + return; + } + // determine how long to sleep from looking at ticket's expiry. + // We should not allow the ticket to expire, but we should take into consideration + // MIN_TIME_BEFORE_RELOGIN. Will not sleep less than MIN_TIME_BEFORE_RELOGIN, unless doing so + // would cause ticket expiration. + if ((nextRefresh > expiry) || ((now + MIN_TIME_BEFORE_RELOGIN) > expiry)) { + // expiry is before next scheduled refresh). + nextRefresh = now; + } else { + if (nextRefresh < (now + MIN_TIME_BEFORE_RELOGIN)) { + // next scheduled refresh is sooner than (now + MIN_TIME_BEFORE_LOGIN). + Date until = new Date(nextRefresh); + Date newuntil = new Date(now + MIN_TIME_BEFORE_RELOGIN); + LOG.warn( + "TGT refresh thread time adjusted from : {} to : {} since " + + "the former is sooner than the minimum refresh interval (" + + "{} seconds) from now.", + until, + newuntil, + (MIN_TIME_BEFORE_RELOGIN / 1000)); + } + nextRefresh = Math.max(nextRefresh, now + MIN_TIME_BEFORE_RELOGIN); + } + nextRefreshDate = new Date(nextRefresh); + if (nextRefresh > expiry) { + LOG.error( + "next refresh: {} is later than expiry {}." + + " This may indicate a clock skew problem." + + " Check that this host and the KDC's " + + "hosts' clocks are in sync. Exiting refresh thread.", + nextRefreshDate, + expiryDate); + return; + } + } + if (now == nextRefresh) { + LOG.info("refreshing now because expiry is before next scheduled refresh time."); + } else if (now < nextRefresh) { + Date until = new Date(nextRefresh); + LOG.info("TGT refresh sleeping until: {}", until.toString()); + try { + Thread.sleep(nextRefresh - now); + } catch (InterruptedException ie) { + LOG.warn("TGT renewal thread has been interrupted and will exit."); + break; + } + } else { + LOG.error( + "nextRefresh:{} is in the past: exiting refresh thread. Check" + + " clock sync between this host and KDC - (KDC's clock is likely ahead of this host)." + + " Manual intervention will be required for this client to successfully authenticate." + + " Exiting refresh thread.", + nextRefreshDate); + break; + } + if (isUsingTicketCache) { + String cmd = zkConfig.getProperty(ZKConfig.KINIT_COMMAND, KINIT_COMMAND_DEFAULT); + String kinitArgs = "-R"; + int retry = 1; + while (retry >= 0) { + try { + LOG.debug("running ticket cache refresh command: {} {}", cmd, kinitArgs); + Shell.execCommand(cmd, kinitArgs); + break; + } catch (Exception e) { + if (retry > 0) { + --retry; + // sleep for 10 seconds + try { + Thread.sleep(10 * 1000); + } catch (InterruptedException ie) { + LOG.error("Interrupted while renewing TGT, exiting Login thread"); + return; + } + } else { + LOG.warn( + "Could not renew TGT due to problem running shell command: '{} {}'." + + " Exiting refresh thread.", + cmd, + kinitArgs, + e); + return; + } + } + } + } + try { + int retry = 1; + while (retry >= 0) { + try { + reLogin(); + break; + } catch (LoginException le) { + if (retry > 0) { + --retry; + // sleep for 10 seconds. + try { + Thread.sleep(10 * 1000); + } catch (InterruptedException e) { + LOG.error("Interrupted during login retry after LoginException:", le); + throw le; + } + } else { + LOG.error("Could not refresh TGT for principal: {}.", principal, le); + } + } + } + } catch (LoginException le) { + LOG.error("Failed to refresh TGT: refresh thread exiting now.", le); + break; + } + } + } + }); + t.setDaemon(true); + } + + public void startThreadIfNeeded() { + // thread object 't' will be null if a refresh thread is not needed. + if (t != null) { + t.start(); + } + } + + public void shutdown() { + if ((t != null) && (t.isAlive())) { + t.interrupt(); + try { + t.join(); + } catch (InterruptedException e) { + LOG.warn("error while waiting for Login thread to shutdown.", e); + } + } + } + + public Subject getSubject() { + return subject; + } + + public String getUserName() { + if (principal == null || principal.isEmpty()) { + return SYSTEM_USER; + } + return principal; + } + + public String getLoginContextName() { + return loginContextName; + } + + private synchronized LoginContext login(final String loginContextName) throws LoginException { + if (loginContextName == null) { + throw new LoginException("loginContext name (JAAS file section header) was null. " + + "Please check your java.security.login.auth.config (=" + + System.getProperty("java.security.login.auth.config") + + ") and your " + + getLoginContextMessage()); + } + LoginContext loginContext = new LoginContext(loginContextName, callbackHandler); + loginContext.login(); + LOG.info("{} successfully logged in.", loginContextName); + return loginContext; + } + + private String getLoginContextMessage() { + if (zkConfig instanceof ZKClientConfig) { + return ZKClientConfig.LOGIN_CONTEXT_NAME_KEY + + "(=" + + zkConfig.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT) + + ")"; + } else { + return ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY + + "(=" + + System.getProperty(ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY, ZooKeeperSaslServer.DEFAULT_LOGIN_CONTEXT_NAME) + + ")"; + } + } + + // c.f. org.apache.hadoop.security.UserGroupInformation. + private long getRefreshTime(KerberosTicket tgt) { + long start = tgt.getStartTime().getTime(); + long expires = tgt.getEndTime().getTime(); + LOG.info("TGT valid starting at: {}", tgt.getStartTime().toString()); + LOG.info("TGT expires: {}", tgt.getEndTime().toString()); + long proposedRefresh = start + (long) ((expires - start) + * (TICKET_RENEW_WINDOW + (TICKET_RENEW_JITTER + * ThreadLocalRandom.current().nextDouble()))); + if (proposedRefresh > expires) { + // proposedRefresh is too far in the future: it's after ticket expires: simply return now. + return Time.currentWallTime(); + } else { + return proposedRefresh; + } + } + + private synchronized KerberosTicket getTGT() { + Set tickets = subject.getPrivateCredentials(KerberosTicket.class); + for (KerberosTicket ticket : tickets) { + KerberosPrincipal server = ticket.getServer(); + if (server.getName().equals("krbtgt/" + server.getRealm() + "@" + server.getRealm())) { + LOG.debug("Client principal is \"{}\".", ticket.getClient().getName()); + LOG.debug("Server principal is \"{}\".", ticket.getServer().getName()); + return ticket; + } + } + return null; + } + + private boolean hasSufficientTimeElapsed() { + long now = Time.currentElapsedTime(); + if (now - getLastLogin() < MIN_TIME_BEFORE_RELOGIN) { + LOG.warn("Not attempting to re-login since the last re-login was " + + "attempted less than {} seconds before.", + (MIN_TIME_BEFORE_RELOGIN / 1000)); + return false; + } + // register most recent relogin attempt + setLastLogin(now); + return true; + } + + /** + * Returns login object + * @return login + */ + private LoginContext getLogin() { + return login; + } + + /** + * Set the login object + * @param login + */ + private void setLogin(LoginContext login) { + this.login = login; + } + + /** + * Set the last login time. + * @param time the number of milliseconds since the beginning of time + */ + private void setLastLogin(long time) { + lastLogin = time; + } + + /** + * Get the time of the last login. + * @return the number of milliseconds since the beginning of time. + */ + private long getLastLogin() { + return lastLogin; + } + + /** + * Re-login a principal. This method assumes that {@link #login(String)} has happened already. + * @throws javax.security.auth.login.LoginException on a failure + */ + // c.f. HADOOP-6559 + private synchronized void reLogin() throws LoginException { + if (!isKrbTicket) { + return; + } + LoginContext login = getLogin(); + if (login == null) { + throw new LoginException("login must be done first"); + } + if (!hasSufficientTimeElapsed()) { + return; + } + LOG.info("Initiating logout for {}", principal); + synchronized (Login.class) { + //clear up the kerberos state. But the tokens are not cleared! As per + //the Java kerberos login module code, only the kerberos credentials + //are cleared + login.logout(); + //login and also update the subject field of this instance to + //have the new credentials (pass it to the LoginContext constructor) + login = new LoginContext(loginContextName, getSubject()); + LOG.info("Initiating re-login for {}", principal); + login.login(); + setLogin(login); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/MultiOperationRecord.java b/zookeeper-server/src/main/java/org/apache/zookeeper/MultiOperationRecord.java new file mode 100644 index 0000000..d43e728 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/MultiOperationRecord.java @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.proto.CheckVersionRequest; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.CreateTTLRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.GetChildrenRequest; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.proto.MultiHeader; +import org.apache.zookeeper.proto.SetDataRequest; + +/** + * Encodes a composite operation. In the wire format, each operation + * consists of a single MultiHeader followed by the appropriate request. + * Each of these MultiHeaders has a type which indicates + * the type of the following operation or a negative number if no more operations + * are included. + * All of the operations must be from the same OpKind. + */ +public class MultiOperationRecord implements Record, Iterable { + + private List ops = new ArrayList(); + private Op.OpKind opKind = null; + + public MultiOperationRecord() { + } + + public MultiOperationRecord(Iterable ops) throws IllegalArgumentException { + for (Op op : ops) { + setOrCheckOpKind(op.getKind()); + add(op); + } + } + + @Override + public Iterator iterator() { + return ops.iterator(); + } + + public void add(Op op) throws IllegalArgumentException { + setOrCheckOpKind(op.getKind()); + ops.add(op); + } + + public int size() { + return ops.size(); + } + + /** + * Returns the kind of the operations contained by the record. + * @return The OpKind value of all the elements in the record. + */ + public Op.OpKind getOpKind() { + return opKind; + } + + private void setOrCheckOpKind(Op.OpKind ok) throws IllegalArgumentException { + if (opKind == null) { + opKind = ok; + } else if (ok != opKind) { + throw new IllegalArgumentException("Mixing read and write operations (transactions)" + + " is not allowed in a multi request."); + } + } + + @Override + public void serialize(OutputArchive archive, String tag) throws IOException { + archive.startRecord(this, tag); + for (Op op : ops) { + MultiHeader h = new MultiHeader(op.getType(), false, -1); + h.serialize(archive, tag); + switch (op.getType()) { + case ZooDefs.OpCode.create: + case ZooDefs.OpCode.create2: + case ZooDefs.OpCode.createTTL: + case ZooDefs.OpCode.createContainer: + case ZooDefs.OpCode.delete: + case ZooDefs.OpCode.setData: + case ZooDefs.OpCode.check: + case ZooDefs.OpCode.getChildren: + case ZooDefs.OpCode.getData: + op.toRequestRecord().serialize(archive, tag); + break; + default: + throw new IOException("Invalid type of op"); + } + } + new MultiHeader(-1, true, -1).serialize(archive, tag); + archive.endRecord(this, tag); + } + + @Override + public void deserialize(InputArchive archive, String tag) throws IOException { + archive.startRecord(tag); + MultiHeader h = new MultiHeader(); + h.deserialize(archive, tag); + try { + while (!h.getDone()) { + switch (h.getType()) { + case ZooDefs.OpCode.create: + case ZooDefs.OpCode.create2: + case ZooDefs.OpCode.createContainer: + CreateRequest cr = new CreateRequest(); + cr.deserialize(archive, tag); + add(Op.create(cr.getPath(), cr.getData(), cr.getAcl(), cr.getFlags())); + break; + case ZooDefs.OpCode.createTTL: + CreateTTLRequest crTtl = new CreateTTLRequest(); + crTtl.deserialize(archive, tag); + add(Op.create(crTtl.getPath(), crTtl.getData(), crTtl.getAcl(), crTtl.getFlags(), crTtl.getTtl())); + break; + case ZooDefs.OpCode.delete: + DeleteRequest dr = new DeleteRequest(); + dr.deserialize(archive, tag); + add(Op.delete(dr.getPath(), dr.getVersion())); + break; + case ZooDefs.OpCode.setData: + SetDataRequest sdr = new SetDataRequest(); + sdr.deserialize(archive, tag); + add(Op.setData(sdr.getPath(), sdr.getData(), sdr.getVersion())); + break; + case ZooDefs.OpCode.check: + CheckVersionRequest cvr = new CheckVersionRequest(); + cvr.deserialize(archive, tag); + add(Op.check(cvr.getPath(), cvr.getVersion())); + break; + case ZooDefs.OpCode.getChildren: + GetChildrenRequest gcr = new GetChildrenRequest(); + gcr.deserialize(archive, tag); + add(Op.getChildren(gcr.getPath())); + break; + case ZooDefs.OpCode.getData: + GetDataRequest gdr = new GetDataRequest(); + gdr.deserialize(archive, tag); + add(Op.getData(gdr.getPath())); + break; + default: + throw new IOException("Invalid type of op"); + } + h.deserialize(archive, tag); + } + } catch (IllegalArgumentException e) { + throw new IOException("Mixing different kind of ops"); + } + archive.endRecord(tag); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof MultiOperationRecord)) { + return false; + } + + MultiOperationRecord that = (MultiOperationRecord) o; + + if (ops != null) { + Iterator other = that.ops.iterator(); + for (Op op : ops) { + boolean hasMoreData = other.hasNext(); + if (!hasMoreData) { + return false; + } + Op otherOp = other.next(); + if (!op.equals(otherOp)) { + return false; + } + } + return !other.hasNext(); + } else { + return that.ops == null; + } + + } + + @Override + public int hashCode() { + int h = 1023; + for (Op op : ops) { + h = h * 25 + op.hashCode(); + } + return h; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/MultiResponse.java b/zookeeper-server/src/main/java/org/apache/zookeeper/MultiResponse.java new file mode 100644 index 0000000..16d2fb2 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/MultiResponse.java @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.proto.Create2Response; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.ErrorResponse; +import org.apache.zookeeper.proto.GetChildrenResponse; +import org.apache.zookeeper.proto.GetDataResponse; +import org.apache.zookeeper.proto.MultiHeader; +import org.apache.zookeeper.proto.SetDataResponse; + +/** + * Handles the response from a multi request. Such a response consists of + * a sequence of responses each prefixed by a MultiResponse that indicates + * the type of the response. The end of the list is indicated by a MultiHeader + * with a negative type. Each individual response is in the same format as + * with the corresponding operation in the original request list. + */ +public class MultiResponse implements Record, Iterable { + + private List results = new ArrayList(); + + public void add(OpResult x) { + results.add(x); + } + + @Override + public Iterator iterator() { + return results.iterator(); + } + + public int size() { + return results.size(); + } + + @Override + public void serialize(OutputArchive archive, String tag) throws IOException { + archive.startRecord(this, tag); + + for (OpResult result : results) { + int err = result.getType() == ZooDefs.OpCode.error ? ((OpResult.ErrorResult) result).getErr() : 0; + + new MultiHeader(result.getType(), false, err).serialize(archive, tag); + + switch (result.getType()) { + case ZooDefs.OpCode.create: + new CreateResponse(((OpResult.CreateResult) result).getPath()).serialize(archive, tag); + break; + case ZooDefs.OpCode.create2: + OpResult.CreateResult createResult = (OpResult.CreateResult) result; + new Create2Response(createResult.getPath(), createResult.getStat()).serialize(archive, tag); + break; + case ZooDefs.OpCode.delete: + case ZooDefs.OpCode.check: + break; + case ZooDefs.OpCode.setData: + new SetDataResponse(((OpResult.SetDataResult) result).getStat()).serialize(archive, tag); + break; + case ZooDefs.OpCode.getChildren: + new GetChildrenResponse(((OpResult.GetChildrenResult) result).getChildren()).serialize(archive, tag); + break; + case ZooDefs.OpCode.getData: + new GetDataResponse( + ((OpResult.GetDataResult) result).getData(), + ((OpResult.GetDataResult) result).getStat()) + .serialize(archive, tag); + break; + case ZooDefs.OpCode.error: + new ErrorResponse(((OpResult.ErrorResult) result).getErr()).serialize(archive, tag); + break; + default: + throw new IOException("Invalid type " + result.getType() + " in MultiResponse"); + } + } + new MultiHeader(-1, true, -1).serialize(archive, tag); + archive.endRecord(this, tag); + } + + @Override + public void deserialize(InputArchive archive, String tag) throws IOException { + results = new ArrayList(); + + archive.startRecord(tag); + MultiHeader h = new MultiHeader(); + h.deserialize(archive, tag); + while (!h.getDone()) { + switch (h.getType()) { + case ZooDefs.OpCode.create: + CreateResponse cr = new CreateResponse(); + cr.deserialize(archive, tag); + results.add(new OpResult.CreateResult(cr.getPath())); + break; + + case ZooDefs.OpCode.create2: + Create2Response cr2 = new Create2Response(); + cr2.deserialize(archive, tag); + results.add(new OpResult.CreateResult(cr2.getPath(), cr2.getStat())); + break; + + case ZooDefs.OpCode.delete: + results.add(new OpResult.DeleteResult()); + break; + + case ZooDefs.OpCode.setData: + SetDataResponse sdr = new SetDataResponse(); + sdr.deserialize(archive, tag); + results.add(new OpResult.SetDataResult(sdr.getStat())); + break; + + case ZooDefs.OpCode.check: + results.add(new OpResult.CheckResult()); + break; + + case ZooDefs.OpCode.getChildren: + GetChildrenResponse gcr = new GetChildrenResponse(); + gcr.deserialize(archive, tag); + results.add(new OpResult.GetChildrenResult(gcr.getChildren())); + break; + + case ZooDefs.OpCode.getData: + GetDataResponse gdr = new GetDataResponse(); + gdr.deserialize(archive, tag); + results.add(new OpResult.GetDataResult(gdr.getData(), gdr.getStat())); + break; + + case ZooDefs.OpCode.error: + // TODO: need way to more cleanly serialize/deserialize exceptions + ErrorResponse er = new ErrorResponse(); + er.deserialize(archive, tag); + results.add(new OpResult.ErrorResult(er.getErr())); + break; + + default: + throw new IOException("Invalid type " + h.getType() + " in MultiResponse"); + } + h.deserialize(archive, tag); + } + archive.endRecord(tag); + } + + public List getResultList() { + return results; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof MultiResponse)) { + return false; + } + + MultiResponse other = (MultiResponse) o; + + if (results != null) { + Iterator i = other.results.iterator(); + for (OpResult result : results) { + if (i.hasNext()) { + if (!result.equals(i.next())) { + return false; + } + } else { + return false; + } + } + return !i.hasNext(); + } else { + return other.results == null; + } + } + + @Override + public int hashCode() { + int hash = results.size(); + for (OpResult result : results) { + hash = (hash * 35) + result.hashCode(); + } + return hash; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Op.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Op.java new file mode 100644 index 0000000..8597ea8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Op.java @@ -0,0 +1,584 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import org.apache.jute.Record; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.CheckVersionRequest; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.CreateTTLRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.GetChildrenRequest; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.server.EphemeralType; + +/** + * Represents a single operation in a multi-operation transaction. Each operation can be a create, update, + * delete, a version check or just read operations like getChildren or getData. + * + * Sub-classes of Op each represent each detailed type but should not normally be referenced except via + * the provided factory methods. + * + * @see ZooKeeper#create(String, byte[], java.util.List, CreateMode) + * @see ZooKeeper#create(String, byte[], java.util.List, CreateMode, org.apache.zookeeper.AsyncCallback.StringCallback, Object) + * @see ZooKeeper#delete(String, int) + * @see ZooKeeper#setData(String, byte[], int) + * @see ZooKeeper#getData(String, boolean, Stat) + * @see ZooKeeper#getChildren(String, boolean) + */ +public abstract class Op { + + public enum OpKind { + TRANSACTION, + READ + } + + private int type; + private String path; + private OpKind opKind; + + // prevent untyped construction + private Op(int type, String path, OpKind opKind) { + this.type = type; + this.path = path; + this.opKind = opKind; + } + + /** + * Constructs a create operation. Arguments are as for the ZooKeeper method of the same name. + * @see ZooKeeper#create(String, byte[], java.util.List, CreateMode) + * @see CreateMode#fromFlag(int) + * + * @param path + * the path for the node + * @param data + * the initial data for the node + * @param acl + * the acl for the node + * @param flags + * specifying whether the node to be created is ephemeral + * and/or sequential but using the integer encoding. + */ + public static Op create(String path, byte[] data, List acl, int flags) { + return new Create(path, data, acl, flags); + } + + /** + * Constructs a create operation. Arguments are as for the ZooKeeper method of the same name + * but adding an optional ttl + * @see ZooKeeper#create(String, byte[], java.util.List, CreateMode) + * @see CreateMode#fromFlag(int) + * + * @param path + * the path for the node + * @param data + * the initial data for the node + * @param acl + * the acl for the node + * @param flags + * specifying whether the node to be created is ephemeral + * and/or sequential but using the integer encoding. + * @param ttl + * optional ttl or 0 (flags must imply a TTL creation mode) + */ + public static Op create(String path, byte[] data, List acl, int flags, long ttl) { + CreateMode createMode = CreateMode.fromFlag(flags, CreateMode.PERSISTENT); + if (createMode.isTTL()) { + return new CreateTTL(path, data, acl, createMode, ttl); + } + return new Create(path, data, acl, flags); + } + + /** + * Constructs a create operation. Arguments are as for the ZooKeeper method of the same name. + * @see ZooKeeper#create(String, byte[], java.util.List, CreateMode) + * + * @param path + * the path for the node + * @param data + * the initial data for the node + * @param acl + * the acl for the node + * @param createMode + * specifying whether the node to be created is ephemeral + * and/or sequential + */ + public static Op create(String path, byte[] data, List acl, CreateMode createMode) { + return new Create(path, data, acl, createMode); + } + + /** + * Constructs a create operation. Arguments are as for the ZooKeeper method of the same name + * but adding an optional ttl + * @see ZooKeeper#create(String, byte[], java.util.List, CreateMode) + * + * @param path + * the path for the node + * @param data + * the initial data for the node + * @param acl + * the acl for the node + * @param createMode + * specifying whether the node to be created is ephemeral + * and/or sequential + * @param ttl + * optional ttl or 0 (createMode must imply a TTL) + */ + public static Op create(String path, byte[] data, List acl, CreateMode createMode, long ttl) { + if (createMode.isTTL()) { + return new CreateTTL(path, data, acl, createMode, ttl); + } + return new Create(path, data, acl, createMode); + } + + /** + * Constructs a delete operation. Arguments are as for the ZooKeeper method of the same name. + * @see ZooKeeper#delete(String, int) + * + * @param path + * the path of the node to be deleted. + * @param version + * the expected node version. + */ + public static Op delete(String path, int version) { + return new Delete(path, version); + } + + /** + * Constructs an update operation. Arguments are as for the ZooKeeper method of the same name. + * @see ZooKeeper#setData(String, byte[], int) + * + * @param path + * the path of the node + * @param data + * the data to set + * @param version + * the expected matching version + */ + public static Op setData(String path, byte[] data, int version) { + return new SetData(path, data, version); + } + + /** + * Constructs an version check operation. Arguments are as for the ZooKeeper.setData method except that + * no data is provided since no update is intended. The purpose for this is to allow read-modify-write + * operations that apply to multiple znodes, but where some of the znodes are involved only in the read, + * not the write. A similar effect could be achieved by writing the same data back, but that leads to + * way more version updates than are necessary and more writing in general. + * + * @param path + * the path of the node + * @param version + * the expected matching version + */ + public static Op check(String path, int version) { + return new Check(path, version); + } + + public static Op getChildren(String path) { + return new GetChildren(path); + } + + public static Op getData(String path) { + return new GetData(path); + } + + /** + * Gets the integer type code for an Op. This code should be as from ZooDefs.OpCode + * @see ZooDefs.OpCode + * @return The type code. + */ + public int getType() { + return type; + } + + /** + * Gets the path for an Op. + * @return The path. + */ + public String getPath() { + return path; + } + + /** + * Gets the kind of an Op. + * @return The OpKind value. + */ + public OpKind getKind() { + return opKind; + } + + /** + * Encodes an op for wire transmission. + * @return An appropriate Record structure. + */ + public abstract Record toRequestRecord(); + + /** + * Reconstructs the transaction with the chroot prefix. + * @return transaction with chroot. + */ + abstract Op withChroot(String addRootPrefix); + + /** + * Performs client path validations. + * + * @throws IllegalArgumentException + * if an invalid path is specified + * @throws KeeperException.BadArgumentsException + * if an invalid create mode flag is specified + */ + void validate() throws KeeperException { + PathUtils.validatePath(path); + } + + ////////////////// + // these internal classes are public, but should not generally be referenced. + // + public static class Create extends Op { + + protected byte[] data; + protected List acl; + protected int flags; + + private Create(String path, byte[] data, List acl, int flags) { + super(getOpcode(CreateMode.fromFlag(flags, CreateMode.PERSISTENT)), path, OpKind.TRANSACTION); + this.data = data; + this.acl = acl; + this.flags = flags; + } + + private static int getOpcode(CreateMode createMode) { + if (createMode.isTTL()) { + return ZooDefs.OpCode.createTTL; + } + return createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create; + } + + private Create(String path, byte[] data, List acl, CreateMode createMode) { + super(getOpcode(createMode), path, OpKind.TRANSACTION); + this.data = data; + this.acl = acl; + this.flags = createMode.toFlag(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Create)) { + return false; + } + + Create op = (Create) o; + + boolean aclEquals = true; + Iterator i = op.acl.iterator(); + for (ACL acl : op.acl) { + boolean hasMoreData = i.hasNext(); + if (!hasMoreData) { + aclEquals = false; + break; + } + ACL otherAcl = i.next(); + if (!acl.equals(otherAcl)) { + aclEquals = false; + break; + } + } + return !i.hasNext() + && getType() == op.getType() + && Arrays.equals(data, op.data) + && flags == op.flags + && aclEquals; + } + + @Override + public int hashCode() { + return getType() + getPath().hashCode() + Arrays.hashCode(data); + } + + @Override + public Record toRequestRecord() { + return new CreateRequest(getPath(), data, acl, flags); + } + + @Override + Op withChroot(String path) { + return new Create(path, data, acl, flags); + } + + @Override + void validate() throws KeeperException { + CreateMode createMode = CreateMode.fromFlag(flags); + PathUtils.validatePath(getPath(), createMode.isSequential()); + EphemeralType.validateTTL(createMode, -1); + } + + } + + public static class CreateTTL extends Create { + + private final long ttl; + + private CreateTTL(String path, byte[] data, List acl, int flags, long ttl) { + super(path, data, acl, flags); + this.ttl = ttl; + } + + private CreateTTL(String path, byte[] data, List acl, CreateMode createMode, long ttl) { + super(path, data, acl, createMode); + this.ttl = ttl; + } + + @Override + public boolean equals(Object o) { + return super.equals(o) && (o instanceof CreateTTL) && (ttl == ((CreateTTL) o).ttl); + } + + @Override + public int hashCode() { + return super.hashCode() + (int) (ttl ^ (ttl >>> 32)); + } + + @Override + public Record toRequestRecord() { + return new CreateTTLRequest(getPath(), data, acl, flags, ttl); + } + + @Override + Op withChroot(String path) { + return new CreateTTL(path, data, acl, flags, ttl); + } + + @Override + void validate() throws KeeperException { + CreateMode createMode = CreateMode.fromFlag(flags); + PathUtils.validatePath(getPath(), createMode.isSequential()); + EphemeralType.validateTTL(createMode, ttl); + } + + } + + public static class Delete extends Op { + + private int version; + + private Delete(String path, int version) { + super(ZooDefs.OpCode.delete, path, OpKind.TRANSACTION); + this.version = version; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Delete)) { + return false; + } + + Delete op = (Delete) o; + + return getType() == op.getType() && version == op.version && getPath().equals(op.getPath()); + } + + @Override + public int hashCode() { + return getType() + getPath().hashCode() + version; + } + + @Override + public Record toRequestRecord() { + return new DeleteRequest(getPath(), version); + } + + @Override + Op withChroot(String path) { + return new Delete(path, version); + } + + } + + public static class SetData extends Op { + + private byte[] data; + private int version; + + private SetData(String path, byte[] data, int version) { + super(ZooDefs.OpCode.setData, path, OpKind.TRANSACTION); + this.data = data; + this.version = version; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SetData)) { + return false; + } + + SetData op = (SetData) o; + + return getType() == op.getType() + && version == op.version + && getPath().equals(op.getPath()) + && Arrays.equals(data, op.data); + } + + @Override + public int hashCode() { + return getType() + getPath().hashCode() + Arrays.hashCode(data) + version; + } + + @Override + public Record toRequestRecord() { + return new SetDataRequest(getPath(), data, version); + } + + @Override + Op withChroot(String path) { + return new SetData(path, data, version); + } + + } + + public static class Check extends Op { + + private int version; + + private Check(String path, int version) { + super(ZooDefs.OpCode.check, path, OpKind.TRANSACTION); + this.version = version; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Check)) { + return false; + } + + Check op = (Check) o; + + return getType() == op.getType() && getPath().equals(op.getPath()) && version == op.version; + } + + @Override + public int hashCode() { + return getType() + getPath().hashCode() + version; + } + + @Override + public Record toRequestRecord() { + return new CheckVersionRequest(getPath(), version); + } + + @Override + Op withChroot(String path) { + return new Check(path, version); + } + + } + + public static class GetChildren extends Op { + + GetChildren(String path) { + super(ZooDefs.OpCode.getChildren, path, OpKind.READ); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof GetChildren)) { + return false; + } + + GetChildren op = (GetChildren) o; + + return getType() == op.getType() && getPath().equals(op.getPath()); + } + + @Override + public int hashCode() { + return getType() + getPath().hashCode(); + } + + @Override + public Record toRequestRecord() { + return new GetChildrenRequest(getPath(), false); + } + + @Override + Op withChroot(String path) { + return new GetChildren(path); + } + + } + + public static class GetData extends Op { + + GetData(String path) { + super(ZooDefs.OpCode.getData, path, OpKind.READ); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof GetData)) { + return false; + } + + GetData op = (GetData) o; + + return getType() == op.getType() && getPath().equals(op.getPath()); + } + + @Override + public int hashCode() { + return getType() + getPath().hashCode(); + } + + @Override + public Record toRequestRecord() { + return new GetDataRequest(getPath(), false); + } + + @Override + Op withChroot(String path) { + return new GetData(path); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/OpResult.java b/zookeeper-server/src/main/java/org/apache/zookeeper/OpResult.java new file mode 100644 index 0000000..640875e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/OpResult.java @@ -0,0 +1,316 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper; + +import java.util.Arrays; +import java.util.List; + +import org.apache.zookeeper.data.Stat; + +/** + * Encodes the result of a single part of a multiple operation commit. + */ +public abstract class OpResult { + + private int type; + + private OpResult(int type) { + this.type = type; + } + + /** + * Encodes the return type as from ZooDefs.OpCode. Can be used + * to dispatch to the correct cast needed for getting the desired + * additional result data. + * + * @return an integer identifying what kind of operation this result came from. + * @see ZooDefs.OpCode + */ + public int getType() { + return type; + } + + /** + * A result from a create operation. This kind of result allows the + * path to be retrieved since the create might have been a sequential + * create. + */ + public static class CreateResult extends OpResult { + + private String path; + private Stat stat; + + public CreateResult(String path) { + this(ZooDefs.OpCode.create, path, null); + } + + public CreateResult(String path, Stat stat) { + this(ZooDefs.OpCode.create2, path, stat); + } + + private CreateResult(int opcode, String path, Stat stat) { + super(opcode); + this.path = path; + this.stat = stat; + } + + public String getPath() { + return path; + } + + public Stat getStat() { + return stat; + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof CreateResult)) { + return false; + } + + CreateResult other = (CreateResult) o; + + boolean statsAreEqual = stat == null && other.stat == null || (stat != null && other.stat != null && stat + .getMzxid() == other.stat.getMzxid()); + return getType() == other.getType() && path.equals(other.getPath()) && statsAreEqual; + } + + @Override + public int hashCode() { + return (int) (getType() * 35 + path.hashCode() + (stat == null ? 0 : stat.getMzxid())); + } + + } + + /** + * A result from a delete operation. No special values are available. + */ + public static class DeleteResult extends OpResult { + + public DeleteResult() { + super(ZooDefs.OpCode.delete); + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof DeleteResult)) { + return false; + } + + DeleteResult opResult = (DeleteResult) o; + return getType() == opResult.getType(); + } + + @Override + public int hashCode() { + return getType(); + } + + } + + /** + * A result from a setData operation. This kind of result provides access + * to the Stat structure from the update. + */ + public static class SetDataResult extends OpResult { + + private Stat stat; + + public SetDataResult(Stat stat) { + super(ZooDefs.OpCode.setData); + this.stat = stat; + } + + public Stat getStat() { + return stat; + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof SetDataResult)) { + return false; + } + + SetDataResult other = (SetDataResult) o; + return getType() == other.getType() && stat.getMzxid() == other.stat.getMzxid(); + } + + @Override + public int hashCode() { + return (int) (getType() * 35 + stat.getMzxid()); + } + + } + + /** + * A result from a version check operation. No special values are available. + */ + public static class CheckResult extends OpResult { + + public CheckResult() { + super(ZooDefs.OpCode.check); + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof CheckResult)) { + return false; + } + + CheckResult other = (CheckResult) o; + return getType() == other.getType(); + } + + @Override + public int hashCode() { + return getType(); + } + + } + + /** + * A result from a getChildren operation. Provides a list which contains + * the names of the children of a given node. + */ + public static class GetChildrenResult extends OpResult { + + private List children; + + public GetChildrenResult(List children) { + super(ZooDefs.OpCode.getChildren); + this.children = children; + } + + public List getChildren() { + return children; + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof GetChildrenResult)) { + return false; + } + + GetChildrenResult other = (GetChildrenResult) o; + return getType() == other.getType() && children.equals(other.children); + } + + @Override + public int hashCode() { + return getType() * 35 + children.hashCode(); + } + + } + + /** + * A result from a getData operation. The data is represented as a byte array. + */ + public static class GetDataResult extends OpResult { + + private byte[] data; + private Stat stat; + + public GetDataResult(byte[] data, Stat stat) { + super(ZooDefs.OpCode.getData); + this.data = (data == null ? null : Arrays.copyOf(data, data.length)); + this.stat = stat; + } + + public byte[] getData() { + return data == null ? null : Arrays.copyOf(data, data.length); + } + + public Stat getStat() { + return stat; + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof GetDataResult)) { + return false; + } + + GetDataResult other = (GetDataResult) o; + return getType() == other.getType() && stat.equals(other.stat) && Arrays.equals(data, other.data); + } + + @Override + public int hashCode() { + return (int) (getType() * 35 + stat.getMzxid() + Arrays.hashCode(data)); + } + + } + + /** + * An error result from any kind of operation. The point of error results + * is that they contain an error code which helps understand what happened. + * + * @see KeeperException.Code + */ + public static class ErrorResult extends OpResult { + + private int err; + + public ErrorResult(int err) { + super(ZooDefs.OpCode.error); + this.err = err; + } + + public int getErr() { + return err; + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(!(o instanceof ErrorResult)) { + return false; + } + + ErrorResult other = (ErrorResult) o; + return getType() == other.getType() && err == other.getErr(); + } + + @Override + public int hashCode() { + return getType() * 35 + err; + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Quotas.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Quotas.java new file mode 100644 index 0000000..031ff36 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Quotas.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * this class manages quotas + * and has many other utils + * for quota + */ +public class Quotas { + + /** the zookeeper nodes that acts as the management and status node **/ + public static final String procZookeeper = "/zookeeper"; + + /** the zookeeper quota node that acts as the quota + * management node for zookeeper */ + public static final String quotaZookeeper = "/zookeeper/quota"; + + /** + * the limit node that has the limit of + * a subtree + */ + public static final String limitNode = "zookeeper_limits"; + + /** + * the stat node that monitors the limit of + * a subtree. + */ + public static final String statNode = "zookeeper_stats"; + + /** + * return the quota path associated with this + * prefix + * @param path the actual path in zookeeper. + * @return the limit quota path + */ + public static String quotaPath(String path) { + return quotaZookeeper + path + "/" + limitNode; + } + + /** + * return the stat quota path associated with this + * prefix. + * @param path the actual path in zookeeper + * @return the stat quota path + */ + public static String statPath(String path) { + return quotaZookeeper + path + "/" + statNode; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/SaslClientCallbackHandler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/SaslClientCallbackHandler.java new file mode 100644 index 0000000..fbbb248 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/SaslClientCallbackHandler.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.sasl.AuthorizeCallback; +import javax.security.sasl.RealmCallback; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is used by the SASL mechanisms to get further information to complete + * the authentication. For example, a SASL mechanism might use this callback + * handler to do verification operation. The CallbackHandler interface here + * refers to javax.security.auth.callback.CallbackHandler. It should not be + * confused with ZooKeeper packet callbacks like + * org.apache.zookeeper.server.auth.SaslServerCallbackHandler. + */ +public class SaslClientCallbackHandler implements CallbackHandler { + + private String password = null; + private static final Logger LOG = LoggerFactory.getLogger(SaslClientCallbackHandler.class); + private final String entity; + public SaslClientCallbackHandler(String password, String client) { + this.password = password; + this.entity = client; + } + + public void handle(Callback[] callbacks) throws UnsupportedCallbackException { + for (Callback callback : callbacks) { + if (callback instanceof NameCallback) { + NameCallback nc = (NameCallback) callback; + nc.setName(nc.getDefaultName()); + } else { + if (callback instanceof PasswordCallback) { + PasswordCallback pc = (PasswordCallback) callback; + if (password != null) { + pc.setPassword(this.password.toCharArray()); + } else { + LOG.warn( + "Could not login: the {} is being asked for a password, but the ZooKeeper {}" + + " code does not currently support obtaining a password from the user." + + " Make sure that the {} is configured to use a ticket cache (using" + + " the JAAS configuration setting 'useTicketCache=true)' and restart the {}. If" + + " you still get this message after that, the TGT in the ticket cache has expired and must" + + " be manually refreshed. To do so, first determine if you are using a password or a" + + " keytab. If the former, run kinit in a Unix shell in the environment of the user who" + + " is running this Zookeeper {} using the command" + + " 'kinit ' (where is the name of the {}'s Kerberos principal)." + + " If the latter, do" + + " 'kinit -k -t ' (where is the name of the Kerberos principal, and" + + " is the location of the keytab file). After manually refreshing your cache," + + " restart this {}. If you continue to see this message after manually refreshing" + + " your cache, ensure that your KDC host's clock is in sync with this host's clock.", + entity, + entity, + entity, + entity, + entity, + entity, + entity); + } + } else { + if (callback instanceof RealmCallback) { + RealmCallback rc = (RealmCallback) callback; + rc.setText(rc.getDefaultText()); + } else { + if (callback instanceof AuthorizeCallback) { + AuthorizeCallback ac = (AuthorizeCallback) callback; + String authid = ac.getAuthenticationID(); + String authzid = ac.getAuthorizationID(); + if (authid.equals(authzid)) { + ac.setAuthorized(true); + } else { + ac.setAuthorized(false); + } + if (ac.isAuthorized()) { + ac.setAuthorizedID(authzid); + } + } else { + throw new UnsupportedCallbackException( + callback, + "Unrecognized SASL " + entity + "Callback"); + } + } + } + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/SaslServerPrincipal.java b/zookeeper-server/src/main/java/org/apache/zookeeper/SaslServerPrincipal.java new file mode 100644 index 0000000..8799554 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/SaslServerPrincipal.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import org.apache.zookeeper.client.ZKClientConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Computes the Server Principal for a SASL client. + */ +public class SaslServerPrincipal { + + private static final Logger LOG = LoggerFactory.getLogger(SaslServerPrincipal.class); + + /** + * Get the name of the server principal for a SASL client. + * @param addr the address of the host. + * @param clientConfig the configuration for the client. + * @return the name of the principal. + */ + static String getServerPrincipal(InetSocketAddress addr, ZKClientConfig clientConfig) { + return getServerPrincipal(new WrapperInetSocketAddress(addr), clientConfig); + } + + /** + * Get the name of the server principal for a SASL client. This is visible for testing purposes. + * @param addr the address of the host. + * @param clientConfig the configuration for the client. + * @return the name of the principal. + */ + static String getServerPrincipal(WrapperInetSocketAddress addr, ZKClientConfig clientConfig) { + String configuredServerPrincipal = clientConfig.getProperty(ZKClientConfig.ZOOKEEPER_SERVER_PRINCIPAL); + if (configuredServerPrincipal != null) { + // If server principal is already configured then return it + return configuredServerPrincipal; + } + String principalUserName = clientConfig.getProperty( + ZKClientConfig.ZK_SASL_CLIENT_USERNAME, + ZKClientConfig.ZK_SASL_CLIENT_USERNAME_DEFAULT); + String hostName = addr.getHostName(); + + boolean canonicalize = true; + String canonicalizeText = clientConfig.getProperty( + ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, + ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME_DEFAULT); + try { + canonicalize = Boolean.parseBoolean(canonicalizeText); + } catch (IllegalArgumentException ea) { + LOG.warn( + "Could not parse config {} \"{}\" into a boolean using default {}", + ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, + canonicalizeText, + canonicalize); + } + + if (canonicalize) { + WrapperInetAddress ia = addr.getAddress(); + if (ia == null) { + throw new IllegalArgumentException("Unable to canonicalize address " + addr + " because it's not resolvable"); + } + + String canonicalHostName = ia.getCanonicalHostName(); + //avoid using literal IP address when security check fails + if (!canonicalHostName.equals(ia.getHostAddress())) { + hostName = canonicalHostName; + } + LOG.debug("Canonicalized address to {}", hostName); + } + String serverPrincipal = principalUserName + "/" + hostName; + return serverPrincipal; + } + + /** + * This is here to provide a way to unit test the core logic as the methods for + * InetSocketAddress are marked as final. + */ + static class WrapperInetSocketAddress { + + private final InetSocketAddress addr; + + WrapperInetSocketAddress(InetSocketAddress addr) { + this.addr = addr; + } + + public String getHostName() { + return addr.getHostName(); + } + + public WrapperInetAddress getAddress() { + InetAddress ia = addr.getAddress(); + return ia == null ? null : new WrapperInetAddress(ia); + } + + @Override + public String toString() { + return addr.toString(); + } + + } + + /** + * This is here to provide a way to unit test the core logic as the methods for + * InetAddress are marked as final. + */ + static class WrapperInetAddress { + + private final InetAddress ia; + + WrapperInetAddress(InetAddress ia) { + this.ia = ia; + } + + public String getCanonicalHostName() { + return ia.getCanonicalHostName(); + } + + public String getHostAddress() { + return ia.getHostAddress(); + } + + @Override + public String toString() { + return ia.toString(); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ServerAdminClient.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ServerAdminClient.java new file mode 100644 index 0000000..ab7b08a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ServerAdminClient.java @@ -0,0 +1,279 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@InterfaceAudience.Public +public class ServerAdminClient { + + private static final Logger LOG = LoggerFactory.getLogger(ServerAdminClient.class); + + public static void ruok(String host, int port) { + Socket s = null; + try { + byte[] reqBytes = new byte[4]; + ByteBuffer req = ByteBuffer.wrap(reqBytes); + req.putInt(ByteBuffer.wrap("ruok".getBytes()).getInt()); + s = new Socket(); + s.setSoLinger(false, 10); + s.setSoTimeout(20000); + s.connect(new InetSocketAddress(host, port)); + + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(reqBytes); + + byte[] resBytes = new byte[4]; + + int rc = is.read(resBytes); + String retv = new String(resBytes); + System.out.println("rc=" + rc + " retv=" + retv); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + } + + public static void dump(String host, int port) { + Socket s = null; + try { + byte[] reqBytes = new byte[4]; + ByteBuffer req = ByteBuffer.wrap(reqBytes); + req.putInt(ByteBuffer.wrap("dump".getBytes()).getInt()); + s = new Socket(); + s.setSoLinger(false, 10); + s.setSoTimeout(20000); + s.connect(new InetSocketAddress(host, port)); + + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(reqBytes); + + byte[] resBytes = new byte[1024]; + + int rc = is.read(resBytes); + String retv = new String(resBytes); + System.out.println("rc=" + rc + " retv=" + retv); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + } + + public static void stat(String host, int port) { + Socket s = null; + try { + byte[] reqBytes = new byte[4]; + ByteBuffer req = ByteBuffer.wrap(reqBytes); + req.putInt(ByteBuffer.wrap("stat".getBytes()).getInt()); + s = new Socket(); + s.setSoLinger(false, 10); + s.setSoTimeout(20000); + s.connect(new InetSocketAddress(host, port)); + + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(reqBytes); + + byte[] resBytes = new byte[1024]; + + int rc = is.read(resBytes); + String retv = new String(resBytes); + System.out.println("rc=" + rc + " retv=" + retv); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + } + + public static void kill(String host, int port) { + Socket s = null; + try { + byte[] reqBytes = new byte[4]; + ByteBuffer req = ByteBuffer.wrap(reqBytes); + req.putInt(ByteBuffer.wrap("kill".getBytes()).getInt()); + s = new Socket(); + s.setSoLinger(false, 10); + s.setSoTimeout(20000); + s.connect(new InetSocketAddress(host, port)); + + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(reqBytes); + byte[] resBytes = new byte[4]; + + int rc = is.read(resBytes); + String retv = new String(resBytes); + System.out.println("rc=" + rc + " retv=" + retv); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + } + + public static void setTraceMask(String host, int port, String traceMaskStr) { + Socket s = null; + try { + byte[] reqBytes = new byte[12]; + ByteBuffer req = ByteBuffer.wrap(reqBytes); + long traceMask = Long.parseLong(traceMaskStr, 8); + req.putInt(ByteBuffer.wrap("stmk".getBytes()).getInt()); + req.putLong(traceMask); + + s = new Socket(); + s.setSoLinger(false, 10); + s.setSoTimeout(20000); + s.connect(new InetSocketAddress(host, port)); + + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(reqBytes); + + byte[] resBytes = new byte[8]; + + int rc = is.read(resBytes); + ByteBuffer res = ByteBuffer.wrap(resBytes); + long retv = res.getLong(); + System.out.println("rc=" + rc + + " retv=0" + Long.toOctalString(retv) + + " masks=0" + Long.toOctalString(traceMask)); + assert (retv == traceMask); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + } + + public static void getTraceMask(String host, int port) { + Socket s = null; + try { + byte[] reqBytes = new byte[12]; + ByteBuffer req = ByteBuffer.wrap(reqBytes); + req.putInt(ByteBuffer.wrap("gtmk".getBytes()).getInt()); + + s = new Socket(); + s.setSoLinger(false, 10); + s.setSoTimeout(20000); + s.connect(new InetSocketAddress(host, port)); + + InputStream is = s.getInputStream(); + OutputStream os = s.getOutputStream(); + + os.write(reqBytes); + + byte[] resBytes = new byte[8]; + + int rc = is.read(resBytes); + ByteBuffer res = ByteBuffer.wrap(resBytes); + long retv = res.getLong(); + System.out.println("rc=" + rc + " retv=0" + Long.toOctalString(retv)); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + if (s != null) { + try { + s.close(); + } catch (IOException e) { + LOG.warn("Unexpected exception", e); + } + } + } + } + + private static void usage() { + System.out.println("usage: java [-cp CLASSPATH] org.apache.zookeeper.ServerAdminClient " + + "host port op (ruok|stat|dump|kill|gettracemask|settracemask) [arguments]"); + + } + + public static void main(String[] args) { + if (args.length < 3) { + usage(); + return; + } + String host = args[0]; + int port = Integer.parseInt(args[1]); + String op = args[2]; + if (op.equalsIgnoreCase("gettracemask")) { + getTraceMask(host, port); + } else if (op.equalsIgnoreCase("settracemask")) { + setTraceMask(host, port, args[3]); + } else if (op.equalsIgnoreCase("ruok")) { + ruok(host, port); + } else if (op.equalsIgnoreCase("kill")) { + kill(host, port); + } else if (op.equalsIgnoreCase("stat")) { + stat(host, port); + } else if (op.equalsIgnoreCase("dump")) { + dump(host, port); + } else { + System.out.println("Unrecognized op: " + op); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Shell.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Shell.java new file mode 100644 index 0000000..d59079f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Shell.java @@ -0,0 +1,471 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* This file copied from Hadoop's security branch, + * with the following changes: + * 1. package changed from org.apache.hadoop.util to + * org.apache.zookeeper. + * 2. Usage of Hadoop's Configuration class removed since + * it is not available in Zookeeper: instead, system properties + * are used. + * 3. The deprecated getUlimitMemoryCommand() method removed since + * it is not needed. + */ + +package org.apache.zookeeper; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.ExitCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A base class for running a Unix command. + * + * Shell can be used to run unix commands like du or + * df. It also offers facilities to gate commands by + * time-intervals. + */ +public abstract class Shell { + + private static final Logger LOG = LoggerFactory.getLogger(Shell.class); + + /** a Unix command to get the current user's name */ + public static final String USER_NAME_COMMAND = "whoami"; + /** a Unix command to get the current user's groups list */ + public static String[] getGroupsCommand() { + return new String[]{"bash", "-c", "groups"}; + } + /** a Unix command to get a given user's groups list */ + public static String[] getGroupsForUserCommand(final String user) { + //'groups username' command return is non-consistent across different unixes + return new String[]{"bash", "-c", "id -Gn " + user}; + } + /** a Unix command to set permission */ + public static final String SET_PERMISSION_COMMAND = "chmod"; + /** a Unix command to set owner */ + public static final String SET_OWNER_COMMAND = "chown"; + public static final String SET_GROUP_COMMAND = "chgrp"; + /** Return a Unix command to get permission information. */ + public static String[] getGET_PERMISSION_COMMAND() { + //force /bin/ls, except on windows. + return new String[]{(WINDOWS ? "ls" : "/bin/ls"), "-ld"}; + } + + /**Time after which the executing script would be timedout*/ + protected long timeOutInterval = 0L; + /** If or not script timed out*/ + private AtomicBoolean timedOut; + + /** a Unix command to get ulimit of a process. */ + public static final String ULIMIT_COMMAND = "ulimit"; + + /** + * Get the Unix command for setting the maximum virtual memory available + * to a given child process. This is only relevant when we are forking a + * process from within the Mapper or the Reducer implementations. + * Also see Hadoop Pipes and Hadoop Streaming. + * + * It also checks to ensure that we are running on a *nix platform else + * (e.g. in Cygwin/Windows) it returns null. + * @param memoryLimit virtual memory limit + * @return a String[] with the ulimit command arguments or + * null if we are running on a non *nix platform or + * if the limit is unspecified. + */ + public static String[] getUlimitMemoryCommand(int memoryLimit) { + // ulimit isn't supported on Windows + if (WINDOWS) { + return null; + } + + return new String[]{ULIMIT_COMMAND, "-v", String.valueOf(memoryLimit)}; + } + + /** Set to true on Windows platforms */ + public static final boolean WINDOWS /* borrowed from Path.WINDOWS */ = System.getProperty("os.name").startsWith("Windows"); + + private long interval; // refresh interval in msec + private long lastTime; // last time the command was performed + private Map environment; // env for the command execution + private File dir; + private Process process; // sub process used to execute the command + private int exitCode; + + /**If or not script finished executing*/ + private volatile AtomicBoolean completed; + + public Shell() { + this(0L); + } + + /** + * @param interval the minimum duration to wait before re-executing the + * command. + */ + public Shell(long interval) { + this.interval = interval; + this.lastTime = (interval < 0) ? 0 : -interval; + } + + /** set the environment for the command + * @param env Mapping of environment variables + */ + protected void setEnvironment(Map env) { + this.environment = env; + } + + /** set the working directory + * @param dir The directory where the command would be executed + */ + protected void setWorkingDirectory(File dir) { + this.dir = dir; + } + + /** check to see if a command needs to be executed and execute if needed */ + protected void run() throws IOException { + if (lastTime + interval > Time.currentElapsedTime()) { + return; + } + exitCode = ExitCode.EXECUTION_FINISHED.getValue(); // reset for next run + runCommand(); + } + + /** Run a command */ + private void runCommand() throws IOException { + ProcessBuilder builder = new ProcessBuilder(getExecString()); + Timer timeOutTimer = null; + ShellTimeoutTimerTask timeoutTimerTask = null; + timedOut = new AtomicBoolean(false); + completed = new AtomicBoolean(false); + + if (environment != null) { + builder.environment().putAll(this.environment); + } + if (dir != null) { + builder.directory(this.dir); + } + + process = builder.start(); + if (timeOutInterval > 0) { + timeOutTimer = new Timer(); + timeoutTimerTask = new ShellTimeoutTimerTask(this); + //One time scheduling. + timeOutTimer.schedule(timeoutTimerTask, timeOutInterval); + } + final BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + final StringBuffer errMsg = new StringBuffer(); + + // read error and input streams as this would free up the buffers + // free the error stream buffer + Thread errThread = new Thread() { + @Override + public void run() { + try { + String line = errReader.readLine(); + while ((line != null) && !isInterrupted()) { + errMsg.append(line); + errMsg.append(System.getProperty("line.separator")); + line = errReader.readLine(); + } + } catch (IOException ioe) { + LOG.warn("Error reading the error stream", ioe); + } + } + }; + try { + errThread.start(); + } catch (IllegalStateException ise) { + } + try { + parseExecResult(inReader); // parse the output + // clear the input stream buffer + String line = inReader.readLine(); + while (line != null) { + line = inReader.readLine(); + } + // wait for the process to finish and check the exit code + exitCode = process.waitFor(); + try { + // make sure that the error thread exits + errThread.join(); + } catch (InterruptedException ie) { + LOG.warn("Interrupted while reading the error stream", ie); + } + completed.set(true); + //the timeout thread handling + //taken care in finally block + if (exitCode != ExitCode.EXECUTION_FINISHED.getValue()) { + throw new ExitCodeException(exitCode, errMsg.toString()); + } + } catch (InterruptedException ie) { + throw new IOException(ie.toString()); + } finally { + if ((timeOutTimer != null) && !timedOut.get()) { + timeOutTimer.cancel(); + } + // close the input stream + try { + inReader.close(); + } catch (IOException ioe) { + LOG.warn("Error while closing the input stream", ioe); + } + if (!completed.get()) { + errThread.interrupt(); + } + try { + errReader.close(); + } catch (IOException ioe) { + LOG.warn("Error while closing the error stream", ioe); + } + process.destroy(); + lastTime = Time.currentElapsedTime(); + } + } + + /** return an array containing the command name and its parameters */ + protected abstract String[] getExecString(); + + /** Parse the execution result */ + protected abstract void parseExecResult(BufferedReader lines) throws IOException; + + /** get the current sub-process executing the given command + * @return process executing the command + */ + public Process getProcess() { + return process; + } + + /** get the exit code + * @return the exit code of the process + */ + public int getExitCode() { + return exitCode; + } + + /** + * This is an IOException with exit code added. + */ + @SuppressWarnings("serial") + public static class ExitCodeException extends IOException { + + int exitCode; + + public ExitCodeException(int exitCode, String message) { + super(message); + this.exitCode = exitCode; + } + + public int getExitCode() { + return exitCode; + } + + } + + /** + * A simple shell command executor. + * + * ShellCommandExecutorshould be used in cases where the output + * of the command needs no explicit parsing and where the command, working + * directory and the environment remains unchanged. The output of the command + * is stored as-is and is expected to be small. + */ + public static class ShellCommandExecutor extends Shell { + + private String[] command; + private StringBuffer output; + + public ShellCommandExecutor(String[] execString) { + this(execString, null); + } + + public ShellCommandExecutor(String[] execString, File dir) { + this(execString, dir, null); + } + + public ShellCommandExecutor(String[] execString, File dir, Map env) { + this(execString, dir, env, 0L); + } + + /** + * Create a new instance of the ShellCommandExecutor to execute a command. + * + * @param execString The command to execute with arguments + * @param dir If not-null, specifies the directory which should be set + * as the current working directory for the command. + * If null, the current working directory is not modified. + * @param env If not-null, environment of the command will include the + * key-value pairs specified in the map. If null, the current + * environment is not modified. + * @param timeout Specifies the time in milliseconds, after which the + * command will be killed and the status marked as timedout. + * If 0, the command will not be timed out. + */ + public ShellCommandExecutor(String[] execString, File dir, Map env, long timeout) { + command = execString.clone(); + if (dir != null) { + setWorkingDirectory(dir); + } + if (env != null) { + setEnvironment(env); + } + timeOutInterval = timeout; + } + + /** Execute the shell command. */ + public void execute() throws IOException { + this.run(); + } + + protected String[] getExecString() { + return command; + } + + protected void parseExecResult(BufferedReader lines) throws IOException { + output = new StringBuffer(); + char[] buf = new char[512]; + int nRead; + while ((nRead = lines.read(buf, 0, buf.length)) > 0) { + output.append(buf, 0, nRead); + } + } + + /** Get the output of the shell command.*/ + public String getOutput() { + return (output == null) ? "" : output.toString(); + } + + /** + * Returns the commands of this instance. + * Arguments with spaces in are presented with quotes round; other + * arguments are presented raw + * + * @return a string representation of the object. + */ + public String toString() { + StringBuilder builder = new StringBuilder(); + String[] args = getExecString(); + for (String s : args) { + if (s.indexOf(' ') >= 0) { + builder.append('"').append(s).append('"'); + } else { + builder.append(s); + } + builder.append(' '); + } + return builder.toString(); + } + + } + + /** + * To check if the passed script to shell command executor timed out or + * not. + * + * @return if the script timed out. + */ + public boolean isTimedOut() { + return timedOut.get(); + } + + /** + * Set if the command has timed out. + * + */ + private void setTimedOut() { + this.timedOut.set(true); + } + + /** + * Static method to execute a shell command. + * Covers most of the simple cases without requiring the user to implement + * the Shell interface. + * @param cmd shell command to execute. + * @return the output of the executed command. + */ + public static String execCommand(String... cmd) throws IOException { + return execCommand(null, cmd, 0L); + } + + /** + * Static method to execute a shell command. + * Covers most of the simple cases without requiring the user to implement + * the Shell interface. + * @param env the map of environment key=value + * @param cmd shell command to execute. + * @param timeout time in milliseconds after which script should be marked timeout + * @return the output of the executed command.o + */ + + public static String execCommand(Map env, String[] cmd, long timeout) throws IOException { + ShellCommandExecutor exec = new ShellCommandExecutor(cmd, null, env, timeout); + exec.execute(); + return exec.getOutput(); + } + + /** + * Static method to execute a shell command. + * Covers most of the simple cases without requiring the user to implement + * the Shell interface. + * @param env the map of environment key=value + * @param cmd shell command to execute. + * @return the output of the executed command. + */ + public static String execCommand(Map env, String... cmd) throws IOException { + return execCommand(env, cmd, 0L); + } + + /** + * Timer which is used to timeout scripts spawned off by shell. + */ + private static class ShellTimeoutTimerTask extends TimerTask { + + private Shell shell; + + public ShellTimeoutTimerTask(Shell shell) { + this.shell = shell; + } + + @Override + public void run() { + Process p = shell.getProcess(); + try { + p.exitValue(); + } catch (Exception e) { + //Process has not terminated. + //So check if it has completed + //if not just destroy it. + if (p != null && !shell.completed.get()) { + shell.setTimedOut(); + p.destroy(); + } + } + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/StatsTrack.java b/zookeeper-server/src/main/java/org/apache/zookeeper/StatsTrack.java new file mode 100644 index 0000000..4bd88e3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/StatsTrack.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * a class that represents the stats associated with quotas + */ +public class StatsTrack { + + private int count; + private long bytes; + private String countStr = "count"; + private String byteStr = "bytes"; + + /** + * a default constructor for + * stats + */ + public StatsTrack() { + this(null); + } + /** + * the stat string should be of the form count=int,bytes=long + * if stats is called with null the count and bytes are initialized + * to -1. + * @param stats the stat string to be intialized with + */ + public StatsTrack(String stats) { + if (stats == null) { + stats = "count=-1,bytes=-1"; + } + String[] split = stats.split(","); + if (split.length != 2) { + throw new IllegalArgumentException("invalid string " + stats); + } + count = Integer.parseInt(split[0].split("=")[1]); + bytes = Long.parseLong(split[1].split("=")[1]); + } + + /** + * get the count of nodes allowed as part of quota + * + * @return the count as part of this string + */ + public int getCount() { + return this.count; + } + + /** + * set the count for this stat tracker. + * + * @param count + * the count to set with + */ + public void setCount(int count) { + this.count = count; + } + + /** + * get the count of bytes allowed as part of quota + * + * @return the bytes as part of this string + */ + public long getBytes() { + return this.bytes; + } + + /** + * set teh bytes for this stat tracker. + * + * @param bytes + * the bytes to set with + */ + public void setBytes(long bytes) { + this.bytes = bytes; + } + + @Override + /* + * returns the string that maps to this stat tracking. + */ public String toString() { + return countStr + "=" + count + "," + byteStr + "=" + bytes; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Testable.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Testable.java new file mode 100644 index 0000000..780cbe8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Testable.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * Abstraction that exposes various methods useful for testing ZooKeeper + */ +public interface Testable { + + /** + * Cause the ZooKeeper instance to behave as if the session expired + */ + void injectSessionExpiration(); + + /** + * Allow an event to be inserted into the event queue + * + * @param event event to insert + */ + void queueEvent(WatchedEvent event); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Transaction.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Transaction.java new file mode 100644 index 0000000..7a6ba73 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Transaction.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper; + +import java.util.ArrayList; +import java.util.List; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.data.ACL; + +/** + * Provides a builder style interface for doing multiple updates. This is + * really just a thin layer on top of Zookeeper.multi(). + * + * @since 3.4.0 + * + */ +@InterfaceAudience.Public +public class Transaction { + + private ZooKeeper zk; + private List ops = new ArrayList(); + + protected Transaction(ZooKeeper zk) { + this.zk = zk; + } + + public Transaction create(final String path, byte[] data, List acl, CreateMode createMode) { + ops.add(Op.create(path, data, acl, createMode.toFlag())); + return this; + } + + public Transaction delete(final String path, int version) { + ops.add(Op.delete(path, version)); + return this; + } + + public Transaction check(String path, int version) { + ops.add(Op.check(path, version)); + return this; + } + + public Transaction setData(final String path, byte[] data, int version) { + ops.add(Op.setData(path, data, version)); + return this; + } + + public List commit() throws InterruptedException, KeeperException { + return zk.multi(ops); + } + + public void commit(MultiCallback cb, Object ctx) { + zk.multi(ops, cb, ctx); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Version.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Version.java new file mode 100644 index 0000000..4fc0507 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Version.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.util.ServiceUtils; + +public class Version implements org.apache.zookeeper.version.Info { + + /* + * Since the SVN to Git port this field doesn't return the revision anymore + * In version 3.5.6, 3.5.7 and 3.6.0 this function is removed by accident. + * From version 3.5.8+ and 3.6.1+ it is restored for backward compatibility, but will be removed later + * @deprecated deprecated in 3.5.5, use @see {@link #getRevisionHash()} instead + * @return the default value -1 + */ + @Deprecated + public static int getRevision() { + return REVISION; + } + + public static String getRevisionHash() { + return REVISION_HASH; + } + + public static String getBuildDate() { + return BUILD_DATE; + } + + @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", justification = "Missing QUALIFIER causes redundant null-check") + public static String getVersion() { + return MAJOR + "." + MINOR + "." + MICRO + (QUALIFIER == null ? "" : "-" + QUALIFIER); + } + + public static String getVersionRevision() { + return getVersion() + "-" + getRevisionHash(); + } + + public static String getFullVersion() { + return getVersionRevision() + ", built on " + getBuildDate(); + } + + public static void printUsage() { + System.out.print("Usage:\tjava -cp ... org.apache.zookeeper.Version " + + "[--full | --short | --revision],\n\tPrints --full version " + + "info if no arg specified."); + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + + /** + * Prints the current version, revision and build date to the standard out. + * + * @param args + *

        + *
      • --short - prints a short version string "1.2.3" + *
      • --revision - prints a short version string with the Git + * repository revision "1.2.3-${revision_hash}" + *
      • --full - prints the revision and the build date + *
      + */ + public static void main(String[] args) { + if (args.length > 1) { + printUsage(); + } + if (args.length == 0 || (args.length == 1 && args[0].equals("--full"))) { + System.out.println(getFullVersion()); + ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue()); + } + if (args[0].equals("--short")) { + System.out.println(getVersion()); + } else if (args[0].equals("--revision")) { + System.out.println(getVersionRevision()); + } else { + printUsage(); + } + ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/WatchDeregistration.java b/zookeeper-server/src/main/java/org/apache/zookeeper/WatchDeregistration.java new file mode 100644 index 0000000..16c7f84 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/WatchDeregistration.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.ZooKeeper.ZKWatchManager; + +/** + * Handles the special case of removing watches which has registered for a + * client path + */ +public class WatchDeregistration { + + private final String clientPath; + private final Watcher watcher; + private final WatcherType watcherType; + private final boolean local; + private final ZKWatchManager zkManager; + + public WatchDeregistration( + String clientPath, + Watcher watcher, + WatcherType watcherType, + boolean local, + ZKWatchManager zkManager) { + this.clientPath = clientPath; + this.watcher = watcher; + this.watcherType = watcherType; + this.local = local; + this.zkManager = zkManager; + } + + /** + * Unregistering watcher that was added on path. + * + * @param rc + * the result code of the operation that attempted to remove + * watch on the path. + */ + public Map> unregister(int rc) throws KeeperException { + return zkManager.removeWatcher(clientPath, watcher, watcherType, local, rc); + } + + /** + * Returns client path which has specified for unregistering its watcher + * + * @return client path + */ + public String getClientPath() { + return clientPath; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/WatchedEvent.java b/zookeeper-server/src/main/java/org/apache/zookeeper/WatchedEvent.java new file mode 100644 index 0000000..1de3d3d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/WatchedEvent.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.proto.WatcherEvent; + +/** + * A WatchedEvent represents a change on the ZooKeeper that a Watcher + * is able to respond to. The WatchedEvent includes exactly what happened, + * the current state of the ZooKeeper, and the path of the znode that + * was involved in the event. + */ +@InterfaceAudience.Public +public class WatchedEvent { + + private final KeeperState keeperState; + private final EventType eventType; + private String path; + + /** + * Create a WatchedEvent with specified type, state and path + */ + public WatchedEvent(EventType eventType, KeeperState keeperState, String path) { + this.keeperState = keeperState; + this.eventType = eventType; + this.path = path; + } + + /** + * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent + */ + public WatchedEvent(WatcherEvent eventMessage) { + keeperState = KeeperState.fromInt(eventMessage.getState()); + eventType = EventType.fromInt(eventMessage.getType()); + path = eventMessage.getPath(); + } + + public KeeperState getState() { + return keeperState; + } + + public EventType getType() { + return eventType; + } + + public String getPath() { + return path; + } + + @Override + public String toString() { + return "WatchedEvent state:" + keeperState + " type:" + eventType + " path:" + path; + } + + /** + * Convert WatchedEvent to type that can be sent over network + */ + public WatcherEvent getWrapper() { + return new WatcherEvent(eventType.getIntValue(), keeperState.getIntValue(), path); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/Watcher.java b/zookeeper-server/src/main/java/org/apache/zookeeper/Watcher.java new file mode 100644 index 0000000..7802f49 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/Watcher.java @@ -0,0 +1,229 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import org.apache.yetus.audience.InterfaceAudience; + +/** + * This interface specifies the public interface an event handler class must + * implement. A ZooKeeper client will get various events from the ZooKeeper + * server it connects to. An application using such a client handles these + * events by registering a callback object with the client. The callback object + * is expected to be an instance of a class that implements Watcher interface. + * + */ +@InterfaceAudience.Public +public interface Watcher { + + /** + * This interface defines the possible states an Event may represent + */ + @InterfaceAudience.Public + interface Event { + + /** + * Enumeration of states the ZooKeeper may be at the event + */ + @InterfaceAudience.Public + enum KeeperState { + /** Unused, this state is never generated by the server */ + @Deprecated + Unknown(-1), + + /** The client is in the disconnected state - it is not connected + * to any server in the ensemble. */ + Disconnected(0), + + /** Unused, this state is never generated by the server */ + @Deprecated + NoSyncConnected(1), + + /** The client is in the connected state - it is connected + * to a server in the ensemble (one of the servers specified + * in the host connection parameter during ZooKeeper client + * creation). */ + SyncConnected(3), + + /** + * Auth failed state + */ + AuthFailed(4), + + /** + * The client is connected to a read-only server, that is the + * server which is not currently connected to the majority. + * The only operations allowed after receiving this state is + * read operations. + * This state is generated for read-only clients only since + * read/write clients aren't allowed to connect to r/o servers. + */ + ConnectedReadOnly(5), + + /** + * SaslAuthenticated: used to notify clients that they are SASL-authenticated, + * so that they can perform Zookeeper actions with their SASL-authorized permissions. + */ + SaslAuthenticated(6), + + /** The serving cluster has expired this session. The ZooKeeper + * client connection (the session) is no longer valid. You must + * create a new client connection (instantiate a new ZooKeeper + * instance) if you with to access the ensemble. */ + Expired(-112), + + /** + * The client has been closed. This state is never generated by + * the server, but is generated locally when a client calls + * {@link ZooKeeper#close()} or {@link ZooKeeper#close(int)} + */ + Closed(7); + + private final int intValue; // Integer representation of value + // for sending over wire + + KeeperState(int intValue) { + this.intValue = intValue; + } + + public int getIntValue() { + return intValue; + } + + public static KeeperState fromInt(int intValue) { + switch (intValue) { + case -1: + return KeeperState.Unknown; + case 0: + return KeeperState.Disconnected; + case 1: + return KeeperState.NoSyncConnected; + case 3: + return KeeperState.SyncConnected; + case 4: + return KeeperState.AuthFailed; + case 5: + return KeeperState.ConnectedReadOnly; + case 6: + return KeeperState.SaslAuthenticated; + case -112: + return KeeperState.Expired; + case 7: + return KeeperState.Closed; + + default: + throw new RuntimeException("Invalid integer value for conversion to KeeperState"); + } + } + } + + /** + * Enumeration of types of events that may occur on the ZooKeeper + */ + @InterfaceAudience.Public + enum EventType { + None(-1), + NodeCreated(1), // TODO_MA 马中华 注释: 节点创建 + NodeDeleted(2), // TODO_MA 马中华 注释: 节点删除 + NodeDataChanged(3), // TODO_MA 马中华 注释: 节点数据变化 + NodeChildrenChanged(4), // TODO_MA 马中华 注释: 节点下的子节点个数变化 + DataWatchRemoved(5), + ChildWatchRemoved(6), + PersistentWatchRemoved (7); + + private final int intValue; // Integer representation of value + // for sending over wire + + EventType(int intValue) { + this.intValue = intValue; + } + + public int getIntValue() { + return intValue; + } + + public static EventType fromInt(int intValue) { + switch (intValue) { + case -1: + return EventType.None; + case 1: + return EventType.NodeCreated; + case 2: + return EventType.NodeDeleted; + case 3: + return EventType.NodeDataChanged; + case 4: + return EventType.NodeChildrenChanged; + case 5: + return EventType.DataWatchRemoved; + case 6: + return EventType.ChildWatchRemoved; + case 7: + return EventType.PersistentWatchRemoved; + + default: + throw new RuntimeException("Invalid integer value for conversion to EventType"); + } + } + } + + } + + /** + * Enumeration of types of watchers + */ + @InterfaceAudience.Public + enum WatcherType { + Children(1), + Data(2), + Any(3); + + // Integer representation of value + private final int intValue; + + WatcherType(int intValue) { + this.intValue = intValue; + } + + public int getIntValue() { + return intValue; + } + + public static WatcherType fromInt(int intValue) { + switch (intValue) { + case 1: + return WatcherType.Children; + case 2: + return WatcherType.Data; + case 3: + return WatcherType.Any; + + default: + throw new RuntimeException("Invalid integer value for conversion to WatcherType"); + } + } + } + + // 该方法就是接收到服务端事件通知的监听回调方法。参数 event 有三个信息: + // 1、KeeperState state zk链接的状态 + // 2、String znodePath 发生事件的znode节点 + // 3、EventType type 事件的类型 + void process(WatchedEvent event); + + // TODO_MA 注释:谁调用这个方法,谁就需要传递一个 WatchedEvent 参数过来 +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java new file mode 100644 index 0000000..98a2656 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java @@ -0,0 +1,293 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.File; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZKUtil { + + private static final Logger LOG = LoggerFactory.getLogger(ZKUtil.class); + private static final Map permCache = new ConcurrentHashMap(); + /** + * Recursively delete the node with the given path. + *

      + * Important: All versions, of all nodes, under the given node are deleted. + *

      + * If there is an error with deleting one of the sub-nodes in the tree, + * this operation would abort and would be the responsibility of the app to handle the same. + * + * + * @throws IllegalArgumentException if an invalid path is specified + */ + public static boolean deleteRecursive( + ZooKeeper zk, + final String pathRoot, + final int batchSize) throws InterruptedException, KeeperException { + PathUtils.validatePath(pathRoot); + + List tree = listSubTreeBFS(zk, pathRoot); + LOG.debug("Deleting tree: {}", tree); + + return deleteInBatch(zk, tree, batchSize); + } + + /** + * Same as {@link #deleteRecursive(org.apache.zookeeper.ZooKeeper, java.lang.String, int) + * kept here for compatibility with 3.5 clients. + * + * @since 3.6.1 + */ + public static void deleteRecursive( + ZooKeeper zk, + final String pathRoot) throws InterruptedException, KeeperException { + deleteRecursive(zk, pathRoot, 1000); + } + + private static class BatchedDeleteCbContext { + + public Semaphore sem; + public AtomicBoolean success; + + public BatchedDeleteCbContext(int rateLimit) { + sem = new Semaphore(rateLimit); + success = new AtomicBoolean(true); + } + + } + + private static boolean deleteInBatch(ZooKeeper zk, List tree, int batchSize) throws InterruptedException { + int rateLimit = 10; + List ops = new ArrayList<>(); + BatchedDeleteCbContext context = new BatchedDeleteCbContext(rateLimit); + MultiCallback cb = (rc, path, ctx, opResults) -> { + ((BatchedDeleteCbContext) ctx).sem.release(); + if (rc != Code.OK.intValue()) { + ((BatchedDeleteCbContext) ctx).success.set(false); + } + }; + + // Delete the leaves first and eventually get rid of the root + for (int i = tree.size() - 1; i >= 0; --i) { + // Create Op to delete all versions of the node with -1. + ops.add(Op.delete(tree.get(i), -1)); + + if (ops.size() == batchSize || i == 0) { + if (!context.success.get()) { + // fail fast + break; + } + context.sem.acquire(); + zk.multi(ops, cb, context); + ops = new ArrayList<>(); + } + } + + // wait for all callbacks to finish + context.sem.acquire(rateLimit); + return context.success.get(); + } + + /** + * Recursively delete the node with the given path. (async version). + * + *

      + * Important: All versions, of all nodes, under the given node are deleted. + *

      + * If there is an error with deleting one of the sub-nodes in the tree, + * this operation would abort and would be the responsibility of the app to handle the same. + *

      + * @param zk the zookeeper handle + * @param pathRoot the path to be deleted + * @param cb call back method + * @param ctx the context the callback method is called with + * @throws IllegalArgumentException if an invalid path is specified + */ + public static void deleteRecursive( + ZooKeeper zk, + final String pathRoot, + VoidCallback cb, + Object ctx) throws InterruptedException, KeeperException { + PathUtils.validatePath(pathRoot); + + List tree = listSubTreeBFS(zk, pathRoot); + LOG.debug("Deleting tree: {}", tree); + for (int i = tree.size() - 1; i >= 0; --i) { + //Delete the leaves first and eventually get rid of the root + zk.delete(tree.get(i), -1, cb, ctx); //Delete all versions of the node with -1. + } + } + + /** + * @param filePath the file path to be validated + * @return Returns null if valid otherwise error message + */ + public static String validateFileInput(String filePath) { + File file = new File(filePath); + if (!file.exists()) { + return "File '" + file.getAbsolutePath() + "' does not exist."; + } + if (!file.canRead()) { + return "Read permission is denied on the file '" + file.getAbsolutePath() + "'"; + } + if (file.isDirectory()) { + return "'" + file.getAbsolutePath() + "' is a direcory. it must be a file."; + } + return null; + } + + /** + * BFS Traversal of the system under pathRoot, with the entries in the list, in the + * same order as that of the traversal. + *

      + * Important: This is not an atomic snapshot of the tree ever, but the + * state as it exists across multiple RPCs from zkClient to the ensemble. + * For practical purposes, it is suggested to bring the clients to the ensemble + * down (i.e. prevent writes to pathRoot) to 'simulate' a snapshot behavior. + * + * @param zk the zookeeper handle + * @param pathRoot The znode path, for which the entire subtree needs to be listed. + * @throws InterruptedException + * @throws KeeperException + */ + public static List listSubTreeBFS( + ZooKeeper zk, + final String pathRoot) throws KeeperException, InterruptedException { + Queue queue = new ArrayDeque<>(); + List tree = new ArrayList(); + queue.add(pathRoot); + tree.add(pathRoot); + while (!queue.isEmpty()) { + String node = queue.poll(); + List children = zk.getChildren(node, false); + for (final String child : children) { + final String childPath = node + "/" + child; + queue.add(childPath); + tree.add(childPath); + } + } + return tree; + } + + /** + * Visits the subtree with root as given path and calls the passed callback with each znode + * found during the search. It performs a depth-first, pre-order traversal of the tree. + *

      + * Important: This is not an atomic snapshot of the tree ever, but the + * state as it exists across multiple RPCs from zkClient to the ensemble. + * For practical purposes, it is suggested to bring the clients to the ensemble + * down (i.e. prevent writes to pathRoot) to 'simulate' a snapshot behavior. + */ + public static void visitSubTreeDFS( + ZooKeeper zk, + final String path, + boolean watch, + StringCallback cb) throws KeeperException, InterruptedException { + PathUtils.validatePath(path); + + zk.getData(path, watch, null); + cb.processResult(Code.OK.intValue(), path, null, path); + visitSubTreeDFSHelper(zk, path, watch, cb); + } + + @SuppressWarnings("unchecked") + private static void visitSubTreeDFSHelper( + ZooKeeper zk, + final String path, + boolean watch, + StringCallback cb) throws KeeperException, InterruptedException { + // we've already validated, therefore if the path is of length 1 it's the root + final boolean isRoot = path.length() == 1; + try { + List children = zk.getChildren(path, watch, null); + Collections.sort(children); + + for (String child : children) { + String childPath = (isRoot ? path : path + "/") + child; + cb.processResult(Code.OK.intValue(), childPath, null, child); + } + + for (String child : children) { + String childPath = (isRoot ? path : path + "/") + child; + visitSubTreeDFSHelper(zk, childPath, watch, cb); + } + } catch (KeeperException.NoNodeException e) { + // Handle race condition where a node is listed + // but gets deleted before it can be queried + return; // ignore + } + } + + /** + * @param perms + * ACL permissions + * @return string representation of permissions + */ + public static String getPermString(int perms) { + return permCache.computeIfAbsent(perms, k -> constructPermString(k)); + } + + private static String constructPermString(int perms) { + StringBuilder p = new StringBuilder(); + if ((perms & ZooDefs.Perms.CREATE) != 0) { + p.append('c'); + } + if ((perms & ZooDefs.Perms.DELETE) != 0) { + p.append('d'); + } + if ((perms & ZooDefs.Perms.READ) != 0) { + p.append('r'); + } + if ((perms & ZooDefs.Perms.WRITE) != 0) { + p.append('w'); + } + if ((perms & ZooDefs.Perms.ADMIN) != 0) { + p.append('a'); + } + return p.toString(); + } + + public static String aclToString(List acls) { + StringBuilder sb = new StringBuilder(); + for (ACL acl : acls) { + sb.append(acl.getId().getScheme()); + sb.append(":"); + sb.append(acl.getId().getId()); + sb.append(":"); + sb.append(getPermString(acl.getPerms())); + } + return sb.toString(); + } +} \ No newline at end of file diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooDefs.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooDefs.java new file mode 100644 index 0000000..d47cfd0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooDefs.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import java.util.ArrayList; +import java.util.Collections; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; + +@InterfaceAudience.Public +public class ZooDefs { + + public static final String CONFIG_NODE = "/zookeeper/config"; + + public static final String ZOOKEEPER_NODE_SUBTREE = "/zookeeper/"; + + @InterfaceAudience.Public + public interface OpCode { + + int notification = 0; + + int create = 1; + + int delete = 2; + + int exists = 3; + + int getData = 4; + + int setData = 5; + + int getACL = 6; + + int setACL = 7; + + int getChildren = 8; + + int sync = 9; + + int ping = 11; + + int getChildren2 = 12; + + int check = 13; + + int multi = 14; + + int create2 = 15; + + int reconfig = 16; + + int checkWatches = 17; + + int removeWatches = 18; + + int createContainer = 19; + + int deleteContainer = 20; + + int createTTL = 21; + + int multiRead = 22; + + int auth = 100; + + int setWatches = 101; + + int sasl = 102; + + int getEphemerals = 103; + + int getAllChildrenNumber = 104; + + int setWatches2 = 105; + + int addWatch = 106; + + int createSession = -10; + + int closeSession = -11; + + int error = -1; + + } + + @InterfaceAudience.Public + public interface Perms { + + int READ = 1 << 0; + + int WRITE = 1 << 1; + + int CREATE = 1 << 2; + + int DELETE = 1 << 3; + + int ADMIN = 1 << 4; + + int ALL = READ | WRITE | CREATE | DELETE | ADMIN; + + } + + @InterfaceAudience.Public + public interface Ids { + + /** + * This Id represents anyone. + */ + Id ANYONE_ID_UNSAFE = new Id("world", "anyone"); + + /** + * This Id is only usable to set ACLs. It will get substituted with the + * Id's the client authenticated with. + */ + Id AUTH_IDS = new Id("auth", ""); + + /** + * This is a completely open ACL . + */ + @SuppressFBWarnings(value = "MS_MUTABLE_COLLECTION", justification = "Cannot break API") + ArrayList OPEN_ACL_UNSAFE = new ArrayList( + Collections.singletonList(new ACL(Perms.ALL, ANYONE_ID_UNSAFE))); + + /** + * This ACL gives the creators authentication id's all permissions. + */ + @SuppressFBWarnings(value = "MS_MUTABLE_COLLECTION", justification = "Cannot break API") + ArrayList CREATOR_ALL_ACL = new ArrayList(Collections.singletonList(new ACL(Perms.ALL, AUTH_IDS))); + + /** + * This ACL gives the world the ability to read. + */ + @SuppressFBWarnings(value = "MS_MUTABLE_COLLECTION", justification = "Cannot break API") + ArrayList READ_ACL_UNSAFE = new ArrayList( + Collections.singletonList(new ACL(Perms.READ, ANYONE_ID_UNSAFE))); + + } + + @InterfaceAudience.Public + public interface AddWatchModes { + int persistent = 0; // matches AddWatchMode.PERSISTENT + + int persistentRecursive = 1; // matches AddWatchMode.PERSISTENT_RECURSIVE + } + + public static final String[] opNames = {"notification", "create", "delete", "exists", "getData", "setData", "getACL", "setACL", "getChildren", "getChildren2", "getMaxChildren", "setMaxChildren", "ping", "reconfig", "getConfig"}; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java new file mode 100644 index 0000000..b412e8b --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java @@ -0,0 +1,3220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import javafx.scene.chart.Axis; +import org.apache.jute.Record; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.AsyncCallback.ACLCallback; +import org.apache.zookeeper.AsyncCallback.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +import org.apache.zookeeper.AsyncCallback.Create2Callback; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.NoWatcherException; +import org.apache.zookeeper.OpResult.ErrorResult; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.client.ConnectStringParser; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.StaticHostProvider; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.AddWatchRequest; +import org.apache.zookeeper.proto.CheckWatchesRequest; +import org.apache.zookeeper.proto.Create2Response; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.CreateTTLRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.ErrorResponse; +import org.apache.zookeeper.proto.ExistsRequest; +import org.apache.zookeeper.proto.GetACLRequest; +import org.apache.zookeeper.proto.GetACLResponse; +import org.apache.zookeeper.proto.GetAllChildrenNumberRequest; +import org.apache.zookeeper.proto.GetAllChildrenNumberResponse; +import org.apache.zookeeper.proto.GetChildren2Request; +import org.apache.zookeeper.proto.GetChildren2Response; +import org.apache.zookeeper.proto.GetChildrenRequest; +import org.apache.zookeeper.proto.GetChildrenResponse; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.proto.GetDataResponse; +import org.apache.zookeeper.proto.GetEphemeralsRequest; +import org.apache.zookeeper.proto.GetEphemeralsResponse; +import org.apache.zookeeper.proto.RemoveWatchesRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.SetACLRequest; +import org.apache.zookeeper.proto.SetACLResponse; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.proto.SetDataResponse; +import org.apache.zookeeper.proto.SyncRequest; +import org.apache.zookeeper.proto.SyncResponse; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.EphemeralType; +import org.apache.zookeeper.server.watch.PathParentIterator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is the main class of ZooKeeper client library. To use a ZooKeeper + * service, an application must first instantiate an object of ZooKeeper class. + * All the iterations will be done by calling the methods of ZooKeeper class. + * The methods of this class are thread-safe unless otherwise noted. + *

      + * Once a connection to a server is established, a session ID is assigned to the + * client. The client will send heart beats to the server periodically to keep + * the session valid. + *

      + * The application can call ZooKeeper APIs through a client as long as the + * session ID of the client remains valid. + *

      + * If for some reason, the client fails to send heart beats to the server for a + * prolonged period of time (exceeding the sessionTimeout value, for instance), + * the server will expire the session, and the session ID will become invalid. + * The client object will no longer be usable. To make ZooKeeper API calls, the + * application must create a new client object. + *

      + * If the ZooKeeper server the client currently connects to fails or otherwise + * does not respond, the client will automatically try to connect to another + * server before its session ID expires. If successful, the application can + * continue to use the client. + *

      + * The ZooKeeper API methods are either synchronous or asynchronous. Synchronous + * methods blocks until the server has responded. Asynchronous methods just queue + * the request for sending and return immediately. They take a callback object that + * will be executed either on successful execution of the request or on error with + * an appropriate return code (rc) indicating the error. + *

      + * Some successful ZooKeeper API calls can leave watches on the "data nodes" in + * the ZooKeeper server. Other successful ZooKeeper API calls can trigger those + * watches. Once a watch is triggered, an event will be delivered to the client + * which left the watch at the first place. Each watch can be triggered only + * once. Thus, up to one event will be delivered to a client for every watch it + * leaves. + *

      + * A client needs an object of a class implementing Watcher interface for + * processing the events delivered to the client. + * + * When a client drops the current connection and re-connects to a server, all the + * existing watches are considered as being triggered but the undelivered events + * are lost. To emulate this, the client will generate a special event to tell + * the event handler a connection has been dropped. This special event has + * EventType None and KeeperState Disconnected. + */ +/* + * We suppress the "try" warning here because the close() method's signature + * allows it to throw InterruptedException which is strongly advised against + * by AutoCloseable (see: http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html#close()). + * close() will never throw an InterruptedException but the exception remains in the + * signature for backwards compatibility purposes. + */ +@SuppressWarnings("try") +@InterfaceAudience.Public +public class ZooKeeper implements AutoCloseable { + + /** + * @deprecated Use {@link ZKClientConfig#ZOOKEEPER_CLIENT_CNXN_SOCKET} + * instead. + */ + @Deprecated + public static final String ZOOKEEPER_CLIENT_CNXN_SOCKET = "zookeeper.clientCnxnSocket"; + // Setting this to "true" will enable encrypted client-server communication. + + /** + * @deprecated Use {@link ZKClientConfig#SECURE_CLIENT} + * instead. + */ + @Deprecated + public static final String SECURE_CLIENT = "zookeeper.client.secure"; + + protected final ClientCnxn cnxn; + private static final Logger LOG; + + static { + //Keep these two lines together to keep the initialization order explicit + LOG = LoggerFactory.getLogger(ZooKeeper.class); + Environment.logEnv("Client environment:", LOG); + } + + protected final HostProvider hostProvider; + + /** + * This function allows a client to update the connection string by providing + * a new comma separated list of host:port pairs, each corresponding to a + * ZooKeeper server. + *

      + * The function invokes a + * probabilistic load-balancing algorithm which may cause the client to disconnect from + * its current host with the goal to achieve expected uniform number of connections per server + * in the new list. In case the current host to which the client is connected is not in the new + * list this call will always cause the connection to be dropped. Otherwise, the decision + * is based on whether the number of servers has increased or decreased and by how much. + * For example, if the previous connection string contained 3 hosts and now the list contains + * these 3 hosts and 2 more hosts, 40% of clients connected to each of the 3 hosts will + * move to one of the new hosts in order to balance the load. The algorithm will disconnect + * from the current host with probability 0.4 and in this case cause the client to connect + * to one of the 2 new hosts, chosen at random. + *

      + * If the connection is dropped, the client moves to a special mode "reconfigMode" where he chooses + * a new server to connect to using the probabilistic algorithm. After finding a server, + * or exhausting all servers in the new list after trying all of them and failing to connect, + * the client moves back to the normal mode of operation where it will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed (or the session is expired by the server). + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * If the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @throws IOException in cases of network failure + */ + public void updateServerList(String connectString) throws IOException { + ConnectStringParser connectStringParser = new ConnectStringParser(connectString); + Collection serverAddresses = connectStringParser.getServerAddresses(); + + ClientCnxnSocket clientCnxnSocket = cnxn.sendThread.getClientCnxnSocket(); + InetSocketAddress currentHost = (InetSocketAddress) clientCnxnSocket.getRemoteSocketAddress(); + + boolean reconfigMode = hostProvider.updateServerList(serverAddresses, currentHost); + + // cause disconnection - this will cause next to be called + // which will in turn call nextReconfigMode + if (reconfigMode) { + clientCnxnSocket.testableCloseSocket(); + } + } + + public ZooKeeperSaslClient getSaslClient() { + return cnxn.zooKeeperSaslClient; + } + + protected final ZKWatchManager watchManager; + + private final ZKClientConfig clientConfig; + + public ZKClientConfig getClientConfig() { + return clientConfig; + } + + protected List getDataWatches() { + synchronized (watchManager.dataWatches) { + List rc = new ArrayList(watchManager.dataWatches.keySet()); + return rc; + } + } + + protected List getExistWatches() { + synchronized (watchManager.existWatches) { + List rc = new ArrayList(watchManager.existWatches.keySet()); + return rc; + } + } + + protected List getChildWatches() { + synchronized (watchManager.childWatches) { + List rc = new ArrayList(watchManager.childWatches.keySet()); + return rc; + } + } + + protected List getPersistentWatches() { + synchronized (watchManager.persistentWatches) { + List rc = new ArrayList(watchManager.persistentWatches.keySet()); + return rc; + } + } + + protected List getPersistentRecursiveWatches() { + synchronized (watchManager.persistentRecursiveWatches) { + List rc = new ArrayList(watchManager.persistentRecursiveWatches.keySet()); + return rc; + } + } + + /** + * Manage watchers and handle events generated by the ClientCnxn object. + * + * We are implementing this as a nested class of ZooKeeper so that + * the public methods will not be exposed as part of the ZooKeeper client + * API. + */ + static class ZKWatchManager implements ClientWatchManager { + + // TODO_MA 马中华 注释: 监听 节点数据变化 的监听集合 + private final Map> dataWatches = new HashMap>(); + // TODO_MA 马中华 注释: 监听 节点存在与否状态 的监听集合 + private final Map> existWatches = new HashMap>(); + // TODO_MA 马中华 注释: 监听 子节点列表变化的 监听集合 + private final Map> childWatches = new HashMap>(); + + private final Map> persistentWatches = new HashMap>(); + private final Map> persistentRecursiveWatches = new HashMap>(); + + private boolean disableAutoWatchReset; + + ZKWatchManager(boolean disableAutoWatchReset) { + this.disableAutoWatchReset = disableAutoWatchReset; + } + + protected volatile Watcher defaultWatcher; + + private void addTo(Set from, Set to) { + if (from != null) { + to.addAll(from); + } + } + + public Map> removeWatcher(String clientPath, Watcher watcher, WatcherType watcherType, + boolean local, int rc) throws KeeperException { + // Validate the provided znode path contains the given watcher of + // watcherType + containsWatcher(clientPath, watcher, watcherType); + + Map> removedWatchers = new HashMap<>(); + + HashSet childWatchersToRem = new HashSet<>(); + removedWatchers.put(EventType.ChildWatchRemoved, childWatchersToRem); + + HashSet dataWatchersToRem = new HashSet<>(); + removedWatchers.put(EventType.DataWatchRemoved, dataWatchersToRem); + + HashSet persistentWatchersToRem = new HashSet<>(); + removedWatchers.put(EventType.PersistentWatchRemoved, persistentWatchersToRem); + boolean removedWatcher = false; + switch (watcherType) { + case Children: { + synchronized (childWatches) { + removedWatcher = removeWatches(childWatches, watcher, clientPath, local, rc, childWatchersToRem); + } + break; + } + case Data: { + synchronized (dataWatches) { + removedWatcher = removeWatches(dataWatches, watcher, clientPath, local, rc, dataWatchersToRem); + } + + synchronized (existWatches) { + boolean removedDataWatcher = removeWatches(existWatches, watcher, clientPath, local, rc, + dataWatchersToRem); + removedWatcher |= removedDataWatcher; + } + break; + } + case Any: { + synchronized (childWatches) { + removedWatcher = removeWatches(childWatches, watcher, clientPath, local, rc, childWatchersToRem); + } + + synchronized (dataWatches) { + boolean removedDataWatcher = removeWatches(dataWatches, watcher, clientPath, local, rc, + dataWatchersToRem); + removedWatcher |= removedDataWatcher; + } + + synchronized (existWatches) { + boolean removedDataWatcher = removeWatches(existWatches, watcher, clientPath, local, rc, + dataWatchersToRem); + removedWatcher |= removedDataWatcher; + } + + synchronized (persistentWatches) { + boolean removedPersistentWatcher = removeWatches(persistentWatches, watcher, clientPath, local, + rc, persistentWatchersToRem); + removedWatcher |= removedPersistentWatcher; + } + + synchronized (persistentRecursiveWatches) { + boolean removedPersistentRecursiveWatcher = removeWatches(persistentRecursiveWatches, watcher, + clientPath, local, rc, persistentWatchersToRem); + removedWatcher |= removedPersistentRecursiveWatcher; + } + } + } + // Watcher function doesn't exists for the specified params + if (!removedWatcher) { + throw new KeeperException.NoWatcherException(clientPath); + } + return removedWatchers; + } + + private boolean contains(String path, Watcher watcherObj, Map> pathVsWatchers) { + boolean watcherExists = true; + if (pathVsWatchers == null || pathVsWatchers.size() == 0) { + watcherExists = false; + } else { + Set watchers = pathVsWatchers.get(path); + if (watchers == null) { + watcherExists = false; + } else if (watcherObj == null) { + watcherExists = watchers.size() > 0; + } else { + watcherExists = watchers.contains(watcherObj); + } + } + return watcherExists; + } + + /** + * Validate the provided znode path contains the given watcher and + * watcherType + * + * @param path - client path + * @param watcher - watcher object reference + * @param watcherType - type of the watcher + * @throws NoWatcherException + */ + void containsWatcher(String path, Watcher watcher, WatcherType watcherType) throws NoWatcherException { + boolean containsWatcher = false; + switch (watcherType) { + case Children: { + synchronized (childWatches) { + containsWatcher = contains(path, watcher, childWatches); + } + + synchronized (persistentWatches) { + boolean contains_temp = contains(path, watcher, persistentWatches); + containsWatcher |= contains_temp; + } + + synchronized (persistentRecursiveWatches) { + boolean contains_temp = contains(path, watcher, persistentRecursiveWatches); + containsWatcher |= contains_temp; + } + break; + } + case Data: { + synchronized (dataWatches) { + containsWatcher = contains(path, watcher, dataWatches); + } + + synchronized (existWatches) { + boolean contains_temp = contains(path, watcher, existWatches); + containsWatcher |= contains_temp; + } + + synchronized (persistentWatches) { + boolean contains_temp = contains(path, watcher, persistentWatches); + containsWatcher |= contains_temp; + } + + synchronized (persistentRecursiveWatches) { + boolean contains_temp = contains(path, watcher, persistentRecursiveWatches); + containsWatcher |= contains_temp; + } + break; + } + case Any: { + synchronized (childWatches) { + containsWatcher = contains(path, watcher, childWatches); + } + + synchronized (dataWatches) { + boolean contains_temp = contains(path, watcher, dataWatches); + containsWatcher |= contains_temp; + } + + synchronized (existWatches) { + boolean contains_temp = contains(path, watcher, existWatches); + containsWatcher |= contains_temp; + } + + synchronized (persistentWatches) { + boolean contains_temp = contains(path, watcher, persistentWatches); + containsWatcher |= contains_temp; + } + + synchronized (persistentRecursiveWatches) { + boolean contains_temp = contains(path, watcher, persistentRecursiveWatches); + containsWatcher |= contains_temp; + } + } + } + // Watcher function doesn't exists for the specified params + if (!containsWatcher) { + throw new KeeperException.NoWatcherException(path); + } + } + + protected boolean removeWatches(Map> pathVsWatcher, Watcher watcher, String path, + boolean local, int rc, Set removedWatchers) throws KeeperException { + if (!local && rc != Code.OK.intValue()) { + throw KeeperException.create(KeeperException.Code.get(rc), path); + } + boolean success = false; + // When local flag is true, remove watchers for the given path + // irrespective of rc. Otherwise shouldn't remove watchers locally + // when sees failure from server. + if (rc == Code.OK.intValue() || (local && rc != Code.OK.intValue())) { + // Remove all the watchers for the given path + if (watcher == null) { + Set pathWatchers = pathVsWatcher.remove(path); + if (pathWatchers != null) { + // found path watchers + removedWatchers.addAll(pathWatchers); + success = true; + } + } else { + Set watchers = pathVsWatcher.get(path); + if (watchers != null) { + if (watchers.remove(watcher)) { + // found path watcher + removedWatchers.add(watcher); + // cleanup + if (watchers.size() <= 0) { + pathVsWatcher.remove(path); + } + success = true; + } + } + } + } + return success; + } + + /* (non-Javadoc) + * @see org.apache.zookeeper.ClientWatchManager#materialize(Event.KeeperState, + * Event.EventType, java.lang.String) + */ + @Override + public Set materialize(Watcher.Event.KeeperState state, Watcher.Event.EventType type, + String clientPath) { + + // TODO_MA 马中华 注释: 结果集合 + Set result = new HashSet(); + + switch (type) { + case None: + result.add(defaultWatcher); + boolean clear = disableAutoWatchReset && state != Watcher.Event.KeeperState.SyncConnected; + synchronized (dataWatches) { + for (Set ws : dataWatches.values()) { + result.addAll(ws); + } + if (clear) { + dataWatches.clear(); + } + } + + synchronized (existWatches) { + for (Set ws : existWatches.values()) { + result.addAll(ws); + } + if (clear) { + existWatches.clear(); + } + } + + synchronized (childWatches) { + for (Set ws : childWatches.values()) { + result.addAll(ws); + } + if (clear) { + childWatches.clear(); + } + } + + synchronized (persistentWatches) { + for (Set ws : persistentWatches.values()) { + result.addAll(ws); + } + } + + synchronized (persistentRecursiveWatches) { + for (Set ws : persistentRecursiveWatches.values()) { + result.addAll(ws); + } + } + return result; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 节点创建,节点数据改变 + */ + case NodeDataChanged: + case NodeCreated: + synchronized (dataWatches) { + addTo(dataWatches.remove(clientPath), result); + } + synchronized (existWatches) { + addTo(existWatches.remove(clientPath), result); + } + addPersistentWatches(clientPath, result); + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 子节点个数变化 + */ + case NodeChildrenChanged: + synchronized (childWatches) { + addTo(childWatches.remove(clientPath), result); + } + addPersistentWatches(clientPath, result); + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 节点删除 + */ + case NodeDeleted: + synchronized (dataWatches) { + addTo(dataWatches.remove(clientPath), result); + } + // TODO This shouldn't be needed, but just in case + synchronized (existWatches) { + Set list = existWatches.remove(clientPath); + if (list != null) { + addTo(list, result); + LOG.warn("We are triggering an exists watch for delete! Shouldn't happen!"); + } + } + synchronized (childWatches) { + addTo(childWatches.remove(clientPath), result); + } + addPersistentWatches(clientPath, result); + break; + default: + String errorMsg = String + .format("Unhandled watch event type %s with state %s on path %s", type, state, clientPath); + LOG.error(errorMsg); + throw new RuntimeException(errorMsg); + } + + return result; + } + + private void addPersistentWatches(String clientPath, Set result) { + synchronized (persistentWatches) { + addTo(persistentWatches.get(clientPath), result); + } + synchronized (persistentRecursiveWatches) { + for (String path : PathParentIterator.forAll(clientPath).asIterable()) { + addTo(persistentRecursiveWatches.get(path), result); + } + } + } + } + + /** + * Register a watcher for a particular path. + */ + public abstract class WatchRegistration { + + private Watcher watcher; + private String clientPath; + + public WatchRegistration(Watcher watcher, String clientPath) { + this.watcher = watcher; + this.clientPath = clientPath; + } + + protected abstract Map> getWatches(int rc); + + /** + * Register the watcher with the set of watches on path. + * + * @param rc the result code of the operation that attempted to + * add the watch on the path. + */ + public void register(int rc) { + if (shouldAddWatch(rc)) { + Map> watches = getWatches(rc); + synchronized (watches) { + Set watchers = watches.get(clientPath); + if (watchers == null) { + watchers = new HashSet(); + watches.put(clientPath, watchers); + } + watchers.add(watcher); + } + } + } + + /** + * Determine whether the watch should be added based on return code. + * + * @param rc the result code of the operation that attempted to add the + * watch on the node + * @return true if the watch should be added, otw false + */ + protected boolean shouldAddWatch(int rc) { + return rc == 0; + } + + } + + /** + * Handle the special case of exists watches - they add a watcher + * even in the case where NONODE result code is returned. + */ + class ExistsWatchRegistration extends WatchRegistration { + + public ExistsWatchRegistration(Watcher watcher, String clientPath) { + super(watcher, clientPath); + } + + @Override + protected Map> getWatches(int rc) { + return rc == 0 ? watchManager.dataWatches : watchManager.existWatches; + } + + @Override + protected boolean shouldAddWatch(int rc) { + return rc == 0 || rc == KeeperException.Code.NONODE.intValue(); + } + + } + + class DataWatchRegistration extends WatchRegistration { + + public DataWatchRegistration(Watcher watcher, String clientPath) { + super(watcher, clientPath); + } + + @Override + protected Map> getWatches(int rc) { + return watchManager.dataWatches; + } + + } + + class ChildWatchRegistration extends WatchRegistration { + + public ChildWatchRegistration(Watcher watcher, String clientPath) { + super(watcher, clientPath); + } + + @Override + protected Map> getWatches(int rc) { + return watchManager.childWatches; + } + + } + + class AddWatchRegistration extends WatchRegistration { + private final AddWatchMode mode; + + public AddWatchRegistration(Watcher watcher, String clientPath, AddWatchMode mode) { + super(watcher, clientPath); + this.mode = mode; + } + + @Override + protected Map> getWatches(int rc) { + switch (mode) { + case PERSISTENT: + return watchManager.persistentWatches; + case PERSISTENT_RECURSIVE: + return watchManager.persistentRecursiveWatches; + } + throw new IllegalArgumentException("Mode not supported: " + mode); + } + + @Override + protected boolean shouldAddWatch(int rc) { + return rc == 0 || rc == KeeperException.Code.NONODE.intValue(); + } + } + + @InterfaceAudience.Public + public enum States { + CONNECTING, + ASSOCIATING, + CONNECTED, + CONNECTEDREADONLY, + CLOSED, + AUTH_FAILED, + NOT_CONNECTED; + + public boolean isAlive() { + return this != CLOSED && this != AUTH_FAILED; + } + + /** + * Returns whether we are connected to a server (which + * could possibly be read-only, if this client is allowed + * to go to read-only mode) + */ + public boolean isConnected() { + return this == CONNECTED || this == CONNECTEDREADONLY; + } + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed. + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this(connectString, sessionTimeout, watcher, false); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed. + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param conf (added in 3.5.2) passing this conf object gives each client the flexibility of + * configuring properties differently compared to other instances + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, ZKClientConfig conf) throws IOException { + this(connectString, sessionTimeout, watcher, false, conf); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed. + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * For backward compatibility, there is another version + * {@link #ZooKeeper(String, int, Watcher, boolean)} which uses + * default {@link StaticHostProvider} + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @param aHostProvider use this as HostProvider to enable custom behaviour. + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly, + HostProvider aHostProvider) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this(connectString, sessionTimeout, watcher, canBeReadOnly, aHostProvider, null); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed. + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * For backward compatibility, there is another version + * {@link #ZooKeeper(String, int, Watcher, boolean)} which uses default + * {@link StaticHostProvider} + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @param aHostProvider use this as HostProvider to enable custom behaviour. + * @param clientConfig (added in 3.5.2) passing this conf object gives each client the flexibility of + * configuring properties differently compared to other instances + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly, + HostProvider aHostProvider, ZKClientConfig clientConfig) throws IOException { + LOG.info("Initiating client connection, connectString={} sessionTimeout={} watcher={}", connectString, + sessionTimeout, watcher); + + if (clientConfig == null) { + clientConfig = new ZKClientConfig(); + } + this.clientConfig = clientConfig; + + // TODO_MA 注释: + watchManager = defaultWatchManager(); + + // TODO_MA 注释: + watchManager.defaultWatcher = watcher; + ConnectStringParser connectStringParser = new ConnectStringParser(connectString); + hostProvider = aHostProvider; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建和 ZK Server 的链接 + * 1、这个方法创建一个链接对象 + * 2、具体其实是创建: ClientCnxnSocketNIO,内部启动了一个 NIO 客户端 + * cnxn = ClientCnxn 包装了 ClientCnxnSocketNIO + */ + cnxn = createConnection(connectStringParser.getChrootPath(), hostProvider, sessionTimeout, this, watchManager, + getClientCnxnSocket(), canBeReadOnly); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动内部的两个线程 + */ + cnxn.start(); + } + + // @VisibleForTesting + protected ClientCnxn createConnection(String chrootPath, HostProvider hostProvider, int sessionTimeout, + ZooKeeper zooKeeper, ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 真正意义上的一个客户端。 + */ + return new ClientCnxn(chrootPath, hostProvider, sessionTimeout, this, watchManager, clientCnxnSocket, + canBeReadOnly); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed. + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, + boolean canBeReadOnly) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this(connectString, sessionTimeout, watcher, canBeReadOnly, createDefaultHostProvider(connectString)); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed. + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @param conf (added in 3.5.2) passing this conf object gives each client the flexibility of + * configuring properties differently compared to other instances + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly, + ZKClientConfig conf) throws IOException { + this(connectString, sessionTimeout, watcher, canBeReadOnly, createDefaultHostProvider(connectString), conf); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed (or the session is expired by the server). + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * Use {@link #getSessionId} and {@link #getSessionPasswd} on an established + * client connection, these values must be passed as sessionId and + * sessionPasswd respectively if reconnecting. Otherwise, if not + * reconnecting, use the other constructor which does not require these + * parameters. + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * If the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param sessionId specific session id to use if reconnecting + * @param sessionPasswd password for this session + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + * @throws IllegalArgumentException for an invalid list of ZooKeeper hosts + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, + byte[] sessionPasswd) throws IOException { + this(connectString, sessionTimeout, watcher, sessionId, sessionPasswd, false); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed (or the session is expired by the server). + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * Use {@link #getSessionId} and {@link #getSessionPasswd} on an established + * client connection, these values must be passed as sessionId and + * sessionPasswd respectively if reconnecting. Otherwise, if not + * reconnecting, use the other constructor which does not require these + * parameters. + *

      + * For backward compatibility, there is another version + * {@link #ZooKeeper(String, int, Watcher, long, byte[], boolean)} which uses + * default {@link StaticHostProvider} + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * If the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param sessionId specific session id to use if reconnecting + * @param sessionPasswd password for this session + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @param aHostProvider use this as HostProvider to enable custom behaviour. + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, + boolean canBeReadOnly, HostProvider aHostProvider) throws IOException { + this(connectString, sessionTimeout, watcher, sessionId, sessionPasswd, canBeReadOnly, aHostProvider, null); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed (or the session is expired by the server). + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * Use {@link #getSessionId} and {@link #getSessionPasswd} on an established + * client connection, these values must be passed as sessionId and + * sessionPasswd respectively if reconnecting. Otherwise, if not + * reconnecting, use the other constructor which does not require these + * parameters. + *

      + * For backward compatibility, there is another version + * {@link #ZooKeeper(String, int, Watcher, long, byte[], boolean)} which uses + * default {@link StaticHostProvider} + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * If the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param sessionId specific session id to use if reconnecting + * @param sessionPasswd password for this session + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @param aHostProvider use this as HostProvider to enable custom behaviour. + * @param clientConfig (added in 3.5.2) passing this conf object gives each client the flexibility of + * configuring properties differently compared to other instances + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + * @since 3.5.5 + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, + boolean canBeReadOnly, HostProvider aHostProvider, ZKClientConfig clientConfig) throws IOException { + LOG.info( + "Initiating client connection, connectString={} " + "sessionTimeout={} watcher={} sessionId=0x{} sessionPasswd={}", + connectString, sessionTimeout, watcher, Long.toHexString(sessionId), + (sessionPasswd == null ? "" : "")); + + if (clientConfig == null) { + clientConfig = new ZKClientConfig(); + } + this.clientConfig = clientConfig; + watchManager = defaultWatchManager(); + watchManager.defaultWatcher = watcher; + + ConnectStringParser connectStringParser = new ConnectStringParser(connectString); + hostProvider = aHostProvider; + + cnxn = new ClientCnxn(connectStringParser.getChrootPath(), hostProvider, sessionTimeout, this, watchManager, + getClientCnxnSocket(), sessionId, sessionPasswd, canBeReadOnly); + cnxn.seenRwServerBefore = true; // since user has provided sessionId + cnxn.start(); + } + + /** + * To create a ZooKeeper client object, the application needs to pass a + * connection string containing a comma separated list of host:port pairs, + * each corresponding to a ZooKeeper server. + *

      + * Session establishment is asynchronous. This constructor will initiate + * connection to the server and return immediately - potentially (usually) + * before the session is fully established. The watcher argument specifies + * the watcher that will be notified of any changes in state. This + * notification can come at any point before or after the constructor call + * has returned. + *

      + * The instantiated ZooKeeper client object will pick an arbitrary server + * from the connectString and attempt to connect to it. If establishment of + * the connection fails, another server in the connect string will be tried + * (the order is non-deterministic, as we random shuffle the list), until a + * connection is established. The client will continue attempts until the + * session is explicitly closed (or the session is expired by the server). + *

      + * Added in 3.2.0: An optional "chroot" suffix may also be appended to the + * connection string. This will run the client commands while interpreting + * all paths relative to this root (similar to the unix chroot command). + *

      + * Use {@link #getSessionId} and {@link #getSessionPasswd} on an established + * client connection, these values must be passed as sessionId and + * sessionPasswd respectively if reconnecting. Otherwise, if not + * reconnecting, use the other constructor which does not require these + * parameters. + *

      + * This constructor uses a StaticHostProvider; there is another one + * to enable custom behaviour. + * + * @param connectString comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" + * If the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout session timeout in milliseconds + * @param watcher a watcher object which will be notified of state changes, may + * also be notified for node events + * @param sessionId specific session id to use if reconnecting + * @param sessionPasswd password for this session + * @param canBeReadOnly (added in 3.4) whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @throws IOException in cases of network failure + * @throws IllegalArgumentException if an invalid chroot path is specified + */ + public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, + boolean canBeReadOnly) throws IOException { + this(connectString, sessionTimeout, watcher, sessionId, sessionPasswd, canBeReadOnly, + createDefaultHostProvider(connectString)); + } + + // default hostprovider + private static HostProvider createDefaultHostProvider(String connectString) { + return new StaticHostProvider(new ConnectStringParser(connectString).getServerAddresses()); + } + + // VisibleForTesting + public Testable getTestable() { + return new ZooKeeperTestable(cnxn); + } + + /* Useful for testing watch handling behavior */ + protected ZKWatchManager defaultWatchManager() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return new ZKWatchManager(getClientConfig().getBoolean(ZKClientConfig.DISABLE_AUTO_WATCH_RESET)); + } + + /** + * The session id for this ZooKeeper client instance. The value returned is + * not valid until the client connects to a server and may change after a + * re-connect. + * + * This method is NOT thread safe + * + * @return current session id + */ + public long getSessionId() { + return cnxn.getSessionId(); + } + + /** + * The session password for this ZooKeeper client instance. The value + * returned is not valid until the client connects to a server and may + * change after a re-connect. + * + * This method is NOT thread safe + * + * @return current session password + */ + public byte[] getSessionPasswd() { + return cnxn.getSessionPasswd(); + } + + /** + * The negotiated session timeout for this ZooKeeper client instance. The + * value returned is not valid until the client connects to a server and + * may change after a re-connect. + * + * This method is NOT thread safe + * + * @return current session timeout + */ + public int getSessionTimeout() { + return cnxn.getSessionTimeout(); + } + + /** + * Add the specified scheme:auth information to this connection. + * + * This method is NOT thread safe + * + * @param scheme + * @param auth + */ + public void addAuthInfo(String scheme, byte[] auth) { + cnxn.addAuthInfo(scheme, auth); + } + + /** + * Specify the default watcher for the connection (overrides the one + * specified during construction). + * + * @param watcher + */ + public synchronized void register(Watcher watcher) { + watchManager.defaultWatcher = watcher; + } + + /** + * Close this client object. Once the client is closed, its session becomes + * invalid. All the ephemeral nodes in the ZooKeeper server associated with + * the session will be removed. The watches left on those nodes (and on + * their parents) will be triggered. + *

      + * Added in 3.5.3: try-with-resources + * may be used instead of calling close directly. + *

      + *

      + * This method does not wait for all internal threads to exit. + * Use the {@link #close(int) } method to wait for all resources to be released + *

      + * + * @throws InterruptedException + */ + public synchronized void close() throws InterruptedException { + if (!cnxn.getState().isAlive()) { + LOG.debug("Close called on already closed client"); + return; + } + + LOG.debug("Closing session: 0x" + Long.toHexString(getSessionId())); + + try { + cnxn.close(); + } catch (IOException e) { + LOG.debug("Ignoring unexpected exception during close", e); + } + + LOG.info("Session: 0x{} closed", Long.toHexString(getSessionId())); + } + + /** + * Close this client object as the {@link #close() } method. + * This method will wait for internal resources to be released. + * + * @param waitForShutdownTimeoutMs timeout (in milliseconds) to wait for resources to be released. + * Use zero or a negative value to skip the wait + * @return true if waitForShutdownTimeout is greater than zero and all of the resources have been released + * @throws InterruptedException + * @since 3.5.4 + */ + public boolean close(int waitForShutdownTimeoutMs) throws InterruptedException { + close(); + return testableWaitForShutdown(waitForShutdownTimeoutMs); + } + + /** + * Prepend the chroot to the client path (if present). The expectation of + * this function is that the client path has been validated before this + * function is called + * + * @param clientPath path to the node + * @return server view of the path (chroot prepended to client path) + */ + private String prependChroot(String clientPath) { + if (cnxn.chrootPath != null) { + // handle clientPath = "/" + if (clientPath.length() == 1) { + return cnxn.chrootPath; + } + return cnxn.chrootPath + clientPath; + } else { + return clientPath; + } + } + + /** + * Create a node with the given path. The node data will be the given data, + * and node acl will be the given acl. + *

      + * The flags argument specifies whether the created node will be ephemeral + * or not. + *

      + * An ephemeral node will be removed by the ZooKeeper automatically when the + * session associated with the creation of the node expires. + *

      + * The flags argument can also specify to create a sequential node. The + * actual path name of a sequential node will be the given path plus a + * suffix "i" where i is the current sequential number of the node. The sequence + * number is always fixed length of 10 digits, 0 padded. Once + * such a node is created, the sequential number will be incremented by one. + *

      + * If a node with the same actual path already exists in the ZooKeeper, a + * KeeperException with error code KeeperException.NodeExists will be + * thrown. Note that since a different actual path is used for each + * invocation of creating sequential node with the same path argument, the + * call will never throw "file exists" KeeperException. + *

      + * If the parent node does not exist in the ZooKeeper, a KeeperException + * with error code KeeperException.NoNode will be thrown. + *

      + * An ephemeral node cannot have children. If the parent node of the given + * path is ephemeral, a KeeperException with error code + * KeeperException.NoChildrenForEphemerals will be thrown. + *

      + * This operation, if successful, will trigger all the watches left on the + * node of the given path by exists and getData API calls, and the watches + * left on the parent node by getChildren API calls. + *

      + * If a node is created successfully, the ZooKeeper server will trigger the + * watches on the path left by exists calls, and the watches on the parent + * of the node by getChildren calls. + *

      + * The maximum allowable size of the data array is 1 MB (1,048,576 bytes). + * Arrays larger than this will cause a KeeperExecption to be thrown. + * + * @param path the path for the node + * @param data the initial data for the node + * @param acl the acl for the node + * @param createMode specifying whether the node to be created is ephemeral + * and/or sequential + * @return the actual path of the created node + * @throws KeeperException if the server returns a non-zero error code + * @throws KeeperException.InvalidACLException if the ACL is invalid, null, or empty + * @throws InterruptedException if the transaction is interrupted + * @throws IllegalArgumentException if an invalid path is specified + */ + public String create(final String path, byte[] data, List acl, + CreateMode createMode) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath, createMode.isSequential()); + EphemeralType.validateTTL(createMode, -1); + validateACL(acl); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create); + + // TODO_MA 注释: 真正发送请求之前,封装请求对象,和响应对象 + CreateRequest request = new CreateRequest(); + CreateResponse response = new CreateResponse(); + request.setData(data); + request.setFlags(createMode.toFlag()); + request.setPath(serverPath); + request.setAcl(acl); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + if (cnxn.chrootPath == null) { + return response.getPath(); + } else { + return response.getPath().substring(cnxn.chrootPath.length()); + } + } + + /** + * Create a node with the given path and returns the Stat of that node. The + * node data will be the given data and node acl will be the given acl. + *

      + * The flags argument specifies whether the created node will be ephemeral + * or not. + *

      + * An ephemeral node will be removed by the ZooKeeper automatically when the + * session associated with the creation of the node expires. + *

      + * The flags argument can also specify to create a sequential node. The + * actual path name of a sequential node will be the given path plus a + * suffix "i" where i is the current sequential number of the node. The sequence + * number is always fixed length of 10 digits, 0 padded. Once + * such a node is created, the sequential number will be incremented by one. + *

      + * If a node with the same actual path already exists in the ZooKeeper, a + * KeeperException with error code KeeperException.NodeExists will be + * thrown. Note that since a different actual path is used for each + * invocation of creating sequential node with the same path argument, the + * call will never throw "file exists" KeeperException. + *

      + * If the parent node does not exist in the ZooKeeper, a KeeperException + * with error code KeeperException.NoNode will be thrown. + *

      + * An ephemeral node cannot have children. If the parent node of the given + * path is ephemeral, a KeeperException with error code + * KeeperException.NoChildrenForEphemerals will be thrown. + *

      + * This operation, if successful, will trigger all the watches left on the + * node of the given path by exists and getData API calls, and the watches + * left on the parent node by getChildren API calls. + *

      + * If a node is created successfully, the ZooKeeper server will trigger the + * watches on the path left by exists calls, and the watches on the parent + * of the node by getChildren calls. + *

      + * The maximum allowable size of the data array is 1 MB (1,048,576 bytes). + * Arrays larger than this will cause a KeeperExecption to be thrown. + * + * @param path the path for the node + * @param data the initial data for the node + * @param acl the acl for the node + * @param createMode specifying whether the node to be created is ephemeral + * and/or sequential + * @param stat The output Stat object. + * @return the actual path of the created node + * @throws KeeperException if the server returns a non-zero error code + * @throws KeeperException.InvalidACLException if the ACL is invalid, null, or empty + * @throws InterruptedException if the transaction is interrupted + * @throws IllegalArgumentException if an invalid path is specified + */ + public String create(final String path, byte[] data, List acl, CreateMode createMode, + Stat stat) throws KeeperException, InterruptedException { + return create(path, data, acl, createMode, stat, -1); + } + + /** + * same as {@link #create(String, byte[], List, CreateMode, Stat)} but + * allows for specifying a TTL when mode is {@link CreateMode#PERSISTENT_WITH_TTL} + * or {@link CreateMode#PERSISTENT_SEQUENTIAL_WITH_TTL}. If the znode has not been modified + * within the given TTL, it will be deleted once it has no children. The TTL unit is + * milliseconds and must be greater than 0 and less than or equal to + * {@link EphemeralType#maxValue()} for {@link EphemeralType#TTL}. + */ + public String create(final String path, byte[] data, List acl, CreateMode createMode, Stat stat, + long ttl) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath, createMode.isSequential()); + EphemeralType.validateTTL(createMode, ttl); + validateACL(acl); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + setCreateHeader(createMode, h); + Create2Response response = new Create2Response(); + Record record = makeCreateRecord(createMode, serverPath, data, acl, ttl); + ReplyHeader r = cnxn.submitRequest(h, record, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + if (stat != null) { + DataTree.copyStat(response.getStat(), stat); + } + if (cnxn.chrootPath == null) { + return response.getPath(); + } else { + return response.getPath().substring(cnxn.chrootPath.length()); + } + } + + private void setCreateHeader(CreateMode createMode, RequestHeader h) { + if (createMode.isTTL()) { + h.setType(ZooDefs.OpCode.createTTL); + } else { + h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create2); + } + } + + private Record makeCreateRecord(CreateMode createMode, String serverPath, byte[] data, List acl, long ttl) { + Record record; + if (createMode.isTTL()) { + CreateTTLRequest request = new CreateTTLRequest(); + request.setData(data); + request.setFlags(createMode.toFlag()); + request.setPath(serverPath); + request.setAcl(acl); + request.setTtl(ttl); + record = request; + } else { + CreateRequest request = new CreateRequest(); + request.setData(data); + request.setFlags(createMode.toFlag()); + request.setPath(serverPath); + request.setAcl(acl); + record = request; + } + return record; + } + + /** + * The asynchronous version of create. + * + * @see #create(String, byte[], List, CreateMode) + */ + public void create(final String path, byte[] data, List acl, CreateMode createMode, StringCallback cb, + Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath, createMode.isSequential()); + EphemeralType.validateTTL(createMode, -1); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(createMode.isContainer() ? ZooDefs.OpCode.createContainer : ZooDefs.OpCode.create); + CreateRequest request = new CreateRequest(); + CreateResponse response = new CreateResponse(); + ReplyHeader r = new ReplyHeader(); + request.setData(data); + request.setFlags(createMode.toFlag()); + request.setPath(serverPath); + request.setAcl(acl); + cnxn.queuePacket(h, r, request, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * The asynchronous version of create. + * + * @see #create(String, byte[], List, CreateMode, Stat) + */ + public void create(final String path, byte[] data, List acl, CreateMode createMode, Create2Callback cb, + Object ctx) { + create(path, data, acl, createMode, cb, ctx, -1); + } + + /** + * The asynchronous version of create with ttl. + * + * @see #create(String, byte[], List, CreateMode, Stat, long) + */ + public void create(final String path, byte[] data, List acl, CreateMode createMode, Create2Callback cb, + Object ctx, long ttl) { + final String clientPath = path; + PathUtils.validatePath(clientPath, createMode.isSequential()); + EphemeralType.validateTTL(createMode, ttl); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + setCreateHeader(createMode, h); + ReplyHeader r = new ReplyHeader(); + Create2Response response = new Create2Response(); + Record record = makeCreateRecord(createMode, serverPath, data, acl, ttl); + cnxn.queuePacket(h, r, record, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * Delete the node with the given path. The call will succeed if such a node + * exists, and the given version matches the node's version (if the given + * version is -1, it matches any node's versions). + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if the nodes does not exist. + *

      + * A KeeperException with error code KeeperException.BadVersion will be + * thrown if the given version does not match the node's version. + *

      + * A KeeperException with error code KeeperException.NotEmpty will be thrown + * if the node has children. + *

      + * This operation, if successful, will trigger all the watches on the node + * of the given path left by exists API calls, and the watches on the parent + * node left by getChildren API calls. + * + * @param path the path of the node to be deleted. + * @param version the expected node version. + * @throws InterruptedException IF the server transaction is interrupted + * @throws KeeperException If the server signals an error with a non-zero + * return code. + * @throws IllegalArgumentException if an invalid path is specified + */ + public void delete(final String path, int version) throws InterruptedException, KeeperException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath; + + // maintain semantics even in chroot case + // specifically - root cannot be deleted + // I think this makes sense even in chroot case. + if (clientPath.equals("/")) { + // a bit of a hack, but delete(/) will never succeed and ensures + // that the same semantics are maintained + serverPath = clientPath; + } else { + serverPath = prependChroot(clientPath); + } + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.delete); + DeleteRequest request = new DeleteRequest(); + request.setPath(serverPath); + request.setVersion(version); + ReplyHeader r = cnxn.submitRequest(h, request, null, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + } + + /** + * Executes multiple ZooKeeper operations. In case of transactions all of them or none of them will be executed. + *

      + * On success, a list of results is returned. + * On failure, an exception is raised which contains partial results and + * error details, see {@link KeeperException#getResults} + *

      + * Note: The maximum allowable size of all of the data arrays in all of + * the setData operations in this single request is typically 1 MB + * (1,048,576 bytes). This limit is specified on the server via + * jute.maxbuffer. + * Requests larger than this will cause a KeeperException to be + * thrown. + * + * @param ops An iterable that contains the operations to be done. + * These should be created using the factory methods on {@link Op} and must be the same kind of ops. + * @return A list of results, one for each input Op, the order of + * which exactly matches the order of the ops input + * operations. + * @throws InterruptedException If the operation was interrupted. + * The operation may or may not have succeeded, but will not have + * partially succeeded if this exception is thrown. + * @throws KeeperException If the operation could not be completed + * due to some error in doing one of the specified ops. + * @throws IllegalArgumentException if an invalid path is specified or different kind of ops are mixed + * @since 3.4.0 + */ + public List multi(Iterable ops) throws InterruptedException, KeeperException { + for (Op op : ops) { + op.validate(); + } + return multiInternal(generateMultiTransaction(ops)); + } + + /** + * The asynchronous version of multi. + * + * @see #multi(Iterable) + */ + public void multi(Iterable ops, MultiCallback cb, Object ctx) { + List results = validatePath(ops); + if (results.size() > 0) { + cb.processResult(KeeperException.Code.BADARGUMENTS.intValue(), null, ctx, results); + return; + } + multiInternal(generateMultiTransaction(ops), cb, ctx); + } + + private List validatePath(Iterable ops) { + List results = new ArrayList(); + boolean error = false; + for (Op op : ops) { + try { + op.validate(); + } catch (IllegalArgumentException iae) { + LOG.error("Unexpected exception", iae); + ErrorResult err = new ErrorResult(KeeperException.Code.BADARGUMENTS.intValue()); + results.add(err); + error = true; + continue; + } catch (KeeperException ke) { + LOG.error("Unexpected exception", ke); + ErrorResult err = new ErrorResult(ke.code().intValue()); + results.add(err); + error = true; + continue; + } + ErrorResult err = new ErrorResult(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + results.add(err); + } + if (!error) { + results.clear(); + } + return results; + } + + private MultiOperationRecord generateMultiTransaction(Iterable ops) { + // reconstructing transaction with the chroot prefix + List transaction = new ArrayList(); + for (Op op : ops) { + transaction.add(withRootPrefix(op)); + } + return new MultiOperationRecord(transaction); + } + + private Op withRootPrefix(Op op) { + if (null != op.getPath()) { + final String serverPath = prependChroot(op.getPath()); + if (!op.getPath().equals(serverPath)) { + return op.withChroot(serverPath); + } + } + return op; + } + + protected void multiInternal(MultiOperationRecord request, MultiCallback cb, + Object ctx) throws IllegalArgumentException { + if (request.size() == 0) { + // nothing to do, early exit + cnxn.queueCallback(cb, KeeperException.Code.OK.intValue(), null, ctx); + return; + } + RequestHeader h = new RequestHeader(); + switch (request.getOpKind()) { + case TRANSACTION: + h.setType(ZooDefs.OpCode.multi); + break; + case READ: + h.setType(ZooDefs.OpCode.multiRead); + break; + default: + throw new IllegalArgumentException("Unsupported OpKind: " + request.getOpKind()); + } + MultiResponse response = new MultiResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, null, null, ctx, null); + } + + protected List multiInternal( + MultiOperationRecord request) throws InterruptedException, KeeperException, IllegalArgumentException { + RequestHeader h = new RequestHeader(); + if (request.size() == 0) { + // nothing to do, early exit + return Collections.emptyList(); + } + switch (request.getOpKind()) { + case TRANSACTION: + h.setType(ZooDefs.OpCode.multi); + break; + case READ: + h.setType(ZooDefs.OpCode.multiRead); + break; + default: + throw new IllegalArgumentException("Unsupported OpKind: " + request.getOpKind()); + } + MultiResponse response = new MultiResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr())); + } + + List results = response.getResultList(); + // In case of only read operations there is no need to throw an exception + // as the subResults are still possibly valid. + if (request.getOpKind() == Op.OpKind.READ) { + return results; + } + + ErrorResult fatalError = null; + for (OpResult result : results) { + if (result instanceof ErrorResult && ((ErrorResult) result).getErr() != KeeperException.Code.OK.intValue()) { + fatalError = (ErrorResult) result; + break; + } + } + + if (fatalError != null) { + KeeperException ex = KeeperException.create(KeeperException.Code.get(fatalError.getErr())); + ex.setMultiResults(results); + throw ex; + } + + return results; + } + + /** + * A Transaction is a thin wrapper on the {@link #multi} method + * which provides a builder object that can be used to construct + * and commit an atomic set of operations. + * + * @return a Transaction builder object + * @since 3.4.0 + */ + public Transaction transaction() { + return new Transaction(this); + } + + /** + * The asynchronous version of delete. + * + * @see #delete(String, int) + */ + public void delete(final String path, int version, VoidCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath; + + // maintain semantics even in chroot case + // specifically - root cannot be deleted + // I think this makes sense even in chroot case. + if (clientPath.equals("/")) { + // a bit of a hack, but delete(/) will never succeed and ensures + // that the same semantics are maintained + serverPath = clientPath; + } else { + serverPath = prependChroot(clientPath); + } + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.delete); + DeleteRequest request = new DeleteRequest(); + request.setPath(serverPath); + request.setVersion(version); + cnxn.queuePacket(h, new ReplyHeader(), request, null, cb, clientPath, serverPath, ctx, null); + } + + /** + * Return the stat of the node of the given path. Return null if no such a + * node exists. + *

      + * If the watch is non-null and the call is successful (no exception is thrown), + * a watch will be left on the node with the given path. The watch will be + * triggered by a successful operation that creates/delete the node or sets + * the data on the node. + * + * @param path the node path + * @param watcher explicit watcher + * @return the stat of the node of the given path; return null if no such a + * node exists. + * @throws KeeperException If the server signals an error + * @throws InterruptedException If the server transaction is interrupted. + * @throws IllegalArgumentException if an invalid path is specified + */ + public Stat exists(final String path, Watcher watcher) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new ExistsWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.exists); + ExistsRequest request = new ExistsRequest(); + request.setPath(serverPath); + request.setWatch(watcher != null); + SetDataResponse response = new SetDataResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, wcb); + if (r.getErr() != 0) { + if (r.getErr() == KeeperException.Code.NONODE.intValue()) { + return null; + } + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + + return response.getStat().getCzxid() == -1 ? null : response.getStat(); + } + + /** + * Return the stat of the node of the given path. Return null if no such a + * node exists. + *

      + * If the watch is true and the call is successful (no exception is thrown), + * a watch will be left on the node with the given path. The watch will be + * triggered by a successful operation that creates/delete the node or sets + * the data on the node. + * + * @param path the node path + * @param watch whether need to watch this node + * @return the stat of the node of the given path; return null if no such a + * node exists. + * @throws KeeperException If the server signals an error + * @throws InterruptedException If the server transaction is interrupted. + */ + public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException { + return exists(path, watch ? watchManager.defaultWatcher : null); + } + + /** + * The asynchronous version of exists. + * + * @see #exists(String, Watcher) + */ + public void exists(final String path, Watcher watcher, StatCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new ExistsWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.exists); + ExistsRequest request = new ExistsRequest(); + request.setPath(serverPath); + request.setWatch(watcher != null); + SetDataResponse response = new SetDataResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb); + } + + /** + * The asynchronous version of exists. + * + * @see #exists(String, boolean) + */ + public void exists(String path, boolean watch, StatCallback cb, Object ctx) { + exists(path, watch ? watchManager.defaultWatcher : null, cb, ctx); + } + + /** + * Return the data and the stat of the node of the given path. + *

      + * If the watch is non-null and the call is successful (no exception is + * thrown), a watch will be left on the node with the given path. The watch + * will be triggered by a successful operation that sets data on the node, or + * deletes the node. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path the given path + * @param watcher explicit watcher + * @param stat the stat of the node + * @return the data of the node + * @throws KeeperException If the server signals an error with a non-zero error code + * @throws InterruptedException If the server transaction is interrupted. + * @throws IllegalArgumentException if an invalid path is specified + */ + public byte[] getData(final String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new DataWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getData); + GetDataRequest request = new GetDataRequest(); + request.setPath(serverPath); + request.setWatch(watcher != null); + GetDataResponse response = new GetDataResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, wcb); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + if (stat != null) { + DataTree.copyStat(response.getStat(), stat); + } + return response.getData(); + } + + /** + * Return the data and the stat of the node of the given path. + *

      + * If the watch is true and the call is successful (no exception is + * thrown), a watch will be left on the node with the given path. The watch + * will be triggered by a successful operation that sets data on the node, or + * deletes the node. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path the given path + * @param watch whether need to watch this node + * @param stat the stat of the node + * @return the data of the node + * @throws KeeperException If the server signals an error with a non-zero error code + * @throws InterruptedException If the server transaction is interrupted. + */ + public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, InterruptedException { + return getData(path, watch ? watchManager.defaultWatcher : null, stat); + } + + /** + * The asynchronous version of getData. + * + * @see #getData(String, Watcher, Stat) + */ + public void getData(final String path, Watcher watcher, DataCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new DataWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getData); + GetDataRequest request = new GetDataRequest(); + request.setPath(serverPath); + request.setWatch(watcher != null); + GetDataResponse response = new GetDataResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb); + } + + /** + * The asynchronous version of getData. + * + * @see #getData(String, boolean, Stat) + */ + public void getData(String path, boolean watch, DataCallback cb, Object ctx) { + getData(path, watch ? watchManager.defaultWatcher : null, cb, ctx); + } + + /** + * Return the last committed configuration (as known to the server to which the client is connected) + * and the stat of the configuration. + *

      + * If the watch is non-null and the call is successful (no exception is + * thrown), a watch will be left on the configuration node (ZooDefs.CONFIG_NODE). The watch + * will be triggered by a successful reconfig operation + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if the configuration node doesn't exists. + * + * @param watcher explicit watcher + * @param stat the stat of the configuration node ZooDefs.CONFIG_NODE + * @return configuration data stored in ZooDefs.CONFIG_NODE + * @throws KeeperException If the server signals an error with a non-zero error code + * @throws InterruptedException If the server transaction is interrupted. + */ + public byte[] getConfig(Watcher watcher, Stat stat) throws KeeperException, InterruptedException { + final String configZnode = ZooDefs.CONFIG_NODE; + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new DataWatchRegistration(watcher, configZnode); + } + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getData); + GetDataRequest request = new GetDataRequest(); + request.setPath(configZnode); + request.setWatch(watcher != null); + GetDataResponse response = new GetDataResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, wcb); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), configZnode); + } + if (stat != null) { + DataTree.copyStat(response.getStat(), stat); + } + return response.getData(); + } + + /** + * The asynchronous version of getConfig. + * + * @see #getConfig(Watcher, Stat) + */ + public void getConfig(Watcher watcher, DataCallback cb, Object ctx) { + final String configZnode = ZooDefs.CONFIG_NODE; + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new DataWatchRegistration(watcher, configZnode); + } + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getData); + GetDataRequest request = new GetDataRequest(); + request.setPath(configZnode); + request.setWatch(watcher != null); + GetDataResponse response = new GetDataResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, configZnode, configZnode, ctx, wcb); + } + + /** + * Return the last committed configuration (as known to the server to which the client is connected) + * and the stat of the configuration. + *

      + * If the watch is true and the call is successful (no exception is + * thrown), a watch will be left on the configuration node (ZooDefs.CONFIG_NODE). The watch + * will be triggered by a successful reconfig operation + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param watch whether need to watch this node + * @param stat the stat of the configuration node ZooDefs.CONFIG_NODE + * @return configuration data stored in ZooDefs.CONFIG_NODE + * @throws KeeperException If the server signals an error with a non-zero error code + * @throws InterruptedException If the server transaction is interrupted. + */ + public byte[] getConfig(boolean watch, Stat stat) throws KeeperException, InterruptedException { + return getConfig(watch ? watchManager.defaultWatcher : null, stat); + } + + /** + * The Asynchronous version of getConfig. + * + * @see #getData(String, boolean, Stat) + */ + public void getConfig(boolean watch, DataCallback cb, Object ctx) { + getConfig(watch ? watchManager.defaultWatcher : null, cb, ctx); + } + + /** + * Set the data for the node of the given path if such a node exists and the + * given version matches the version of the node (if the given version is + * -1, it matches any node's versions). Return the stat of the node. + *

      + * This operation, if successful, will trigger all the watches on the node + * of the given path left by getData calls. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + *

      + * A KeeperException with error code KeeperException.BadVersion will be + * thrown if the given version does not match the node's version. + *

      + * The maximum allowable size of the data array is 1 MB (1,048,576 bytes). + * Arrays larger than this will cause a KeeperException to be thrown. + * + * @param path the path of the node + * @param data the data to set + * @param version the expected matching version + * @return the state of the node + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + * @throws IllegalArgumentException if an invalid path is specified + */ + public Stat setData(final String path, byte[] data, int version) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setData); + SetDataRequest request = new SetDataRequest(); + request.setPath(serverPath); + request.setData(data); + request.setVersion(version); + SetDataResponse response = new SetDataResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + return response.getStat(); + } + + /** + * The asynchronous version of setData. + * + * @see #setData(String, byte[], int) + */ + public void setData(final String path, byte[] data, int version, StatCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setData); + SetDataRequest request = new SetDataRequest(); + request.setPath(serverPath); + request.setData(data); + request.setVersion(version); + SetDataResponse response = new SetDataResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * Return the ACL and stat of the node of the given path. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path the given path for the node + * @param stat the stat of the node will be copied to this parameter if + * not null. + * @return the ACL array of the given node. + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + * @throws IllegalArgumentException if an invalid path is specified + */ + public List getACL(final String path, Stat stat) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getACL); + GetACLRequest request = new GetACLRequest(); + request.setPath(serverPath); + GetACLResponse response = new GetACLResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + if (stat != null) { + DataTree.copyStat(response.getStat(), stat); + } + return response.getAcl(); + } + + /** + * The asynchronous version of getACL. + * + * @see #getACL(String, Stat) + */ + public void getACL(final String path, Stat stat, ACLCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getACL); + GetACLRequest request = new GetACLRequest(); + request.setPath(serverPath); + GetACLResponse response = new GetACLResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * Set the ACL for the node of the given path if such a node exists and the + * given aclVersion matches the acl version of the node. Return the stat of the + * node. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + *

      + * A KeeperException with error code KeeperException.BadVersion will be + * thrown if the given aclVersion does not match the node's aclVersion. + * + * @param path the given path for the node + * @param acl the given acl for the node + * @param aclVersion the given acl version of the node + * @return the stat of the node. + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + * @throws org.apache.zookeeper.KeeperException.InvalidACLException If the acl is invalide. + * @throws IllegalArgumentException if an invalid path is specified + */ + public Stat setACL(final String path, List acl, int aclVersion) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + validateACL(acl); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setACL); + SetACLRequest request = new SetACLRequest(); + request.setPath(serverPath); + request.setAcl(acl); + request.setVersion(aclVersion); + SetACLResponse response = new SetACLResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + return response.getStat(); + } + + /** + * The asynchronous version of setACL. + * + * @see #setACL(String, List, int) + */ + public void setACL(final String path, List acl, int version, StatCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setACL); + SetACLRequest request = new SetACLRequest(); + request.setPath(serverPath); + request.setAcl(acl); + request.setVersion(version); + SetACLResponse response = new SetACLResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * Return the list of the children of the node of the given path. + *

      + * If the watch is non-null and the call is successful (no exception is thrown), + * a watch will be left on the node with the given path. The watch will be + * triggered by a successful operation that deletes the node of the given + * path or creates/delete a child under the node. + *

      + * The list of children returned is not sorted and no guarantee is provided + * as to its natural or lexical order. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path + * @param watcher explicit watcher + * @return an unordered array of children of the node with the given path + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + * @throws IllegalArgumentException if an invalid path is specified + */ + public List getChildren(final String path, Watcher watcher) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new ChildWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getChildren); + GetChildrenRequest request = new GetChildrenRequest(); + request.setPath(serverPath); + request.setWatch(watcher != null); + GetChildrenResponse response = new GetChildrenResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, wcb); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + return response.getChildren(); + } + + /** + * Return the list of the children of the node of the given path. + *

      + * If the watch is true and the call is successful (no exception is thrown), + * a watch will be left on the node with the given path. The watch will be + * triggered by a successful operation that deletes the node of the given + * path or creates/delete a child under the node. + *

      + * The list of children returned is not sorted and no guarantee is provided + * as to its natural or lexical order. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path + * @param watch + * @return an unordered array of children of the node with the given path + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + */ + public List getChildren(String path, boolean watch) throws KeeperException, InterruptedException { + return getChildren(path, watch ? watchManager.defaultWatcher : null); + } + + /** + * The asynchronous version of getChildren. + * + * @see #getChildren(String, Watcher) + */ + public void getChildren(final String path, Watcher watcher, ChildrenCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new ChildWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getChildren); + GetChildrenRequest request = new GetChildrenRequest(); + request.setPath(serverPath); + request.setWatch(watcher != null); + GetChildrenResponse response = new GetChildrenResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb); + } + + /** + * The asynchronous version of getChildren. + * + * @see #getChildren(String, boolean) + */ + public void getChildren(String path, boolean watch, ChildrenCallback cb, Object ctx) { + getChildren(path, watch ? watchManager.defaultWatcher : null, cb, ctx); + } + + /** + * For the given znode path return the stat and children list. + *

      + * If the watch is non-null and the call is successful (no exception is thrown), + * a watch will be left on the node with the given path. The watch will be + * triggered by a successful operation that deletes the node of the given + * path or creates/delete a child under the node. + *

      + * The list of children returned is not sorted and no guarantee is provided + * as to its natural or lexical order. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path + * @param watcher explicit watcher + * @param stat stat of the znode designated by path + * @return an unordered array of children of the node with the given path + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + * @throws IllegalArgumentException if an invalid path is specified + * @since 3.3.0 + */ + public List getChildren(final String path, Watcher watcher, + Stat stat) throws KeeperException, InterruptedException { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new ChildWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getChildren2); + GetChildren2Request request = new GetChildren2Request(); + request.setPath(serverPath); + request.setWatch(watcher != null); + GetChildren2Response response = new GetChildren2Response(); + ReplyHeader r = cnxn.submitRequest(h, request, response, wcb); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + if (stat != null) { + DataTree.copyStat(response.getStat(), stat); + } + return response.getChildren(); + } + + /** + * For the given znode path return the stat and children list. + *

      + * If the watch is true and the call is successful (no exception is thrown), + * a watch will be left on the node with the given path. The watch will be + * triggered by a successful operation that deletes the node of the given + * path or creates/delete a child under the node. + *

      + * The list of children returned is not sorted and no guarantee is provided + * as to its natural or lexical order. + *

      + * A KeeperException with error code KeeperException.NoNode will be thrown + * if no node with the given path exists. + * + * @param path + * @param watch + * @param stat stat of the znode designated by path + * @return an unordered array of children of the node with the given path + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero + * error code. + * @since 3.3.0 + */ + public List getChildren(String path, boolean watch, Stat stat) throws KeeperException, InterruptedException { + return getChildren(path, watch ? watchManager.defaultWatcher : null, stat); + } + + /** + * The asynchronous version of getChildren. + * + * @see #getChildren(String, Watcher, Stat) + * @since 3.3.0 + */ + public void getChildren(final String path, Watcher watcher, Children2Callback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + // the watch contains the un-chroot path + WatchRegistration wcb = null; + if (watcher != null) { + wcb = new ChildWatchRegistration(watcher, clientPath); + } + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getChildren2); + GetChildren2Request request = new GetChildren2Request(); + request.setPath(serverPath); + request.setWatch(watcher != null); + GetChildren2Response response = new GetChildren2Response(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, wcb); + } + + /** + * The asynchronous version of getChildren. + * + * @see #getChildren(String, boolean, Stat) + * @since 3.3.0 + */ + public void getChildren(String path, boolean watch, Children2Callback cb, Object ctx) { + getChildren(path, watch ? watchManager.defaultWatcher : null, cb, ctx); + } + + /** + * Synchronously gets all numbers of children nodes under a specific path + * + * @param path + * @return Children nodes count under path + * @throws KeeperException + * @throws InterruptedException + * @since 3.6.0 + */ + public int getAllChildrenNumber(final String path) throws KeeperException, InterruptedException { + + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getAllChildrenNumber); + GetAllChildrenNumberRequest request = new GetAllChildrenNumberRequest(serverPath); + GetAllChildrenNumberResponse response = new GetAllChildrenNumberResponse(); + + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + return response.getTotalNumber(); + } + + /** + * Asynchronously gets all numbers of children nodes under a specific path + * + * @param path + * @since 3.6.0 + */ + public void getAllChildrenNumber(final String path, AsyncCallback.AllChildrenNumberCallback cb, Object ctx) { + + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getAllChildrenNumber); + GetAllChildrenNumberRequest request = new GetAllChildrenNumberRequest(serverPath); + GetAllChildrenNumberResponse response = new GetAllChildrenNumberResponse(); + + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * Synchronously gets all the ephemeral nodes created by this session. + * + * @since 3.6.0 + */ + public List getEphemerals() throws KeeperException, InterruptedException { + return getEphemerals("/"); + } + + /** + * Synchronously gets all the ephemeral nodes matching prefixPath + * created by this session. If prefixPath is "/" then it returns all + * ephemerals + * + * @since 3.6.0 + */ + public List getEphemerals(String prefixPath) throws KeeperException, InterruptedException { + PathUtils.validatePath(prefixPath); + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getEphemerals); + GetEphemeralsRequest request = new GetEphemeralsRequest(prefixPath); + GetEphemeralsResponse response = new GetEphemeralsResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr())); + } + return response.getEphemerals(); + } + + /** + * Asynchronously gets all the ephemeral nodes matching prefixPath + * created by this session. If prefixPath is "/" then it returns all + * ephemerals + * + * @since 3.6.0 + */ + public void getEphemerals(String prefixPath, AsyncCallback.EphemeralsCallback cb, Object ctx) { + PathUtils.validatePath(prefixPath); + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.getEphemerals); + GetEphemeralsRequest request = new GetEphemeralsRequest(prefixPath); + GetEphemeralsResponse response = new GetEphemeralsResponse(); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, null, null, ctx, null); + } + + /** + * Asynchronously gets all the ephemeral nodes created by this session. + * ephemerals + * + * @since 3.6.0 + */ + public void getEphemerals(AsyncCallback.EphemeralsCallback cb, Object ctx) { + getEphemerals("/", cb, ctx); + } + + /** + * Asynchronous sync. Flushes channel between process and leader. + * + * @param path + * @param cb a handler for the callback + * @param ctx context to be provided to the callback + * @throws IllegalArgumentException if an invalid path is specified + */ + public void sync(final String path, VoidCallback cb, Object ctx) { + final String clientPath = path; + PathUtils.validatePath(clientPath); + + final String serverPath = prependChroot(clientPath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.sync); + SyncRequest request = new SyncRequest(); + SyncResponse response = new SyncResponse(); + request.setPath(serverPath); + cnxn.queuePacket(h, new ReplyHeader(), request, response, cb, clientPath, serverPath, ctx, null); + } + + /** + * For the given znode path, removes the specified watcher of given + * watcherType. + * + *

      + * Watcher shouldn't be null. A successful call guarantees that, the + * removed watcher won't be triggered. + *

      + * + * @param path - the path of the node + * @param watcher - a concrete watcher + * @param watcherType - the type of watcher to be removed + * @param local - whether the watcher can be removed locally when there is no + * server connection + * @throws InterruptedException if the server transaction is interrupted. + * @throws KeeperException.NoWatcherException if no watcher exists that match the specified parameters + * @throws KeeperException if the server signals an error with a non-zero error code. + * @throws IllegalArgumentException if any of the following is true: + *
        + *
      • {@code path} is invalid + *
      • {@code watcher} is null + *
      + * @since 3.5.0 + */ + public void removeWatches(String path, Watcher watcher, WatcherType watcherType, + boolean local) throws InterruptedException, KeeperException { + validateWatcher(watcher); + removeWatches(ZooDefs.OpCode.checkWatches, path, watcher, watcherType, local); + } + + /** + * The asynchronous version of removeWatches. + * + * @see #removeWatches + */ + public void removeWatches(String path, Watcher watcher, WatcherType watcherType, boolean local, VoidCallback cb, + Object ctx) { + validateWatcher(watcher); + removeWatches(ZooDefs.OpCode.checkWatches, path, watcher, watcherType, local, cb, ctx); + } + + /** + * For the given znode path, removes all the registered watchers of given + * watcherType. + * + *

      + * A successful call guarantees that, the removed watchers won't be + * triggered. + *

      + * + * @param path - the path of the node + * @param watcherType - the type of watcher to be removed + * @param local - whether watches can be removed locally when there is no + * server connection + * @throws InterruptedException if the server transaction is interrupted. + * @throws KeeperException.NoWatcherException if no watcher exists that match the specified parameters + * @throws KeeperException if the server signals an error with a non-zero error code. + * @throws IllegalArgumentException if an invalid {@code path} is specified + * @since 3.5.0 + */ + public void removeAllWatches(String path, WatcherType watcherType, + boolean local) throws InterruptedException, KeeperException { + + removeWatches(ZooDefs.OpCode.removeWatches, path, null, watcherType, local); + } + + /** + * The asynchronous version of removeAllWatches. + * + * @see #removeAllWatches + */ + public void removeAllWatches(String path, WatcherType watcherType, boolean local, VoidCallback cb, Object ctx) { + + removeWatches(ZooDefs.OpCode.removeWatches, path, null, watcherType, local, cb, ctx); + } + + /** + * Add a watch to the given znode using the given mode. Note: not all + * watch types can be set with this method. Only the modes available + * in {@link AddWatchMode} can be set with this method. + * + * @param basePath the path that the watcher applies to + * @param watcher the watcher + * @param mode type of watcher to add + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero + * error code. + * @since 3.6.0 + */ + public void addWatch(String basePath, Watcher watcher, + AddWatchMode mode) throws KeeperException, InterruptedException { + PathUtils.validatePath(basePath); + validateWatcher(watcher); + String serverPath = prependChroot(basePath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.addWatch); + AddWatchRequest request = new AddWatchRequest(serverPath, mode.getMode()); + ReplyHeader r = cnxn + .submitRequest(h, request, new ErrorResponse(), new AddWatchRegistration(watcher, basePath, mode)); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), basePath); + } + } + + /** + * Add a watch to the given znode using the given mode. Note: not all + * watch types can be set with this method. Only the modes available + * in {@link AddWatchMode} can be set with this method. In this version of the method, + * the default watcher is used + * + * @param basePath the path that the watcher applies to + * @param mode type of watcher to add + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero + * error code. + * @since 3.6.0 + */ + public void addWatch(String basePath, AddWatchMode mode) throws KeeperException, InterruptedException { + addWatch(basePath, watchManager.defaultWatcher, mode); + } + + /** + * Async version of {@link #addWatch(String, Watcher, AddWatchMode)} (see it for details) + * + * @param basePath the path that the watcher applies to + * @param watcher the watcher + * @param mode type of watcher to add + * @param cb a handler for the callback + * @param ctx context to be provided to the callback + * @throws IllegalArgumentException if an invalid path is specified + * @since 3.6.0 + */ + public void addWatch(String basePath, Watcher watcher, AddWatchMode mode, VoidCallback cb, Object ctx) { + PathUtils.validatePath(basePath); + validateWatcher(watcher); + String serverPath = prependChroot(basePath); + + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.addWatch); + AddWatchRequest request = new AddWatchRequest(serverPath, mode.getMode()); + cnxn.queuePacket(h, new ReplyHeader(), request, new ErrorResponse(), cb, basePath, serverPath, ctx, + new AddWatchRegistration(watcher, basePath, mode)); + } + + /** + * Async version of {@link #addWatch(String, AddWatchMode)} (see it for details) + * + * @param basePath the path that the watcher applies to + * @param mode type of watcher to add + * @param cb a handler for the callback + * @param ctx context to be provided to the callback + * @throws IllegalArgumentException if an invalid path is specified + * @since 3.6.0 + */ + public void addWatch(String basePath, AddWatchMode mode, VoidCallback cb, Object ctx) { + addWatch(basePath, watchManager.defaultWatcher, mode, cb, ctx); + } + + private void validateWatcher(Watcher watcher) { + if (watcher == null) { + throw new IllegalArgumentException("Invalid Watcher, shouldn't be null!"); + } + } + + private void removeWatches(int opCode, String path, Watcher watcher, WatcherType watcherType, + boolean local) throws InterruptedException, KeeperException { + PathUtils.validatePath(path); + final String clientPath = path; + final String serverPath = prependChroot(clientPath); + WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher, watcherType, local, watchManager); + + RequestHeader h = new RequestHeader(); + h.setType(opCode); + Record request = getRemoveWatchesRequest(opCode, watcherType, serverPath); + + ReplyHeader r = cnxn.submitRequest(h, request, null, null, wcb); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), clientPath); + } + } + + private void removeWatches(int opCode, String path, Watcher watcher, WatcherType watcherType, boolean local, + VoidCallback cb, Object ctx) { + PathUtils.validatePath(path); + final String clientPath = path; + final String serverPath = prependChroot(clientPath); + WatchDeregistration wcb = new WatchDeregistration(clientPath, watcher, watcherType, local, watchManager); + + RequestHeader h = new RequestHeader(); + h.setType(opCode); + Record request = getRemoveWatchesRequest(opCode, watcherType, serverPath); + + cnxn.queuePacket(h, new ReplyHeader(), request, null, cb, clientPath, serverPath, ctx, null, wcb); + } + + private Record getRemoveWatchesRequest(int opCode, WatcherType watcherType, final String serverPath) { + Record request = null; + switch (opCode) { + case ZooDefs.OpCode.checkWatches: + CheckWatchesRequest chkReq = new CheckWatchesRequest(); + chkReq.setPath(serverPath); + chkReq.setType(watcherType.getIntValue()); + request = chkReq; + break; + case ZooDefs.OpCode.removeWatches: + RemoveWatchesRequest rmReq = new RemoveWatchesRequest(); + rmReq.setPath(serverPath); + rmReq.setType(watcherType.getIntValue()); + request = rmReq; + break; + default: + LOG.warn("unknown type " + opCode); + break; + } + return request; + } + + public States getState() { + return cnxn.getState(); + } + + /** + * String representation of this ZooKeeper client. Suitable for things + * like logging. + * + * Do NOT count on the format of this string, it may change without + * warning. + * + * @since 3.3.0 + */ + @Override + public String toString() { + States state = getState(); + return ("State:" + state.toString() + (state + .isConnected() ? " Timeout:" + getSessionTimeout() + " " : " ") + cnxn); + } + + /* + * Methods to aid in testing follow. + * + * THESE METHODS ARE EXPECTED TO BE USED FOR TESTING ONLY!!! + */ + + /** + * Wait up to wait milliseconds for the underlying threads to shutdown. + * THIS METHOD IS EXPECTED TO BE USED FOR TESTING ONLY!!! + * + * @param wait max wait in milliseconds + * @return true iff all threads are shutdown, otw false + * @since 3.3.0 + */ + protected boolean testableWaitForShutdown(int wait) throws InterruptedException { + cnxn.sendThread.join(wait); + if (cnxn.sendThread.isAlive()) { + return false; + } + cnxn.eventThread.join(wait); + return !cnxn.eventThread.isAlive(); + } + + /** + * Returns the address to which the socket is connected. Useful for testing + * against an ensemble - test client may need to know which server + * to shutdown if interested in verifying that the code handles + * disconnection/reconnection correctly. + * THIS METHOD IS EXPECTED TO BE USED FOR TESTING ONLY!!! + * + * @return ip address of the remote side of the connection or null if + * not connected + * @since 3.3.0 + */ + protected SocketAddress testableRemoteSocketAddress() { + return cnxn.sendThread.getClientCnxnSocket().getRemoteSocketAddress(); + } + + /** + * Returns the local address to which the socket is bound. + * THIS METHOD IS EXPECTED TO BE USED FOR TESTING ONLY!!! + * + * @return ip address of the remote side of the connection or null if + * not connected + * @since 3.3.0 + */ + protected SocketAddress testableLocalSocketAddress() { + return cnxn.sendThread.getClientCnxnSocket().getLocalSocketAddress(); + } + + private ClientCnxnSocket getClientCnxnSocket() throws IOException { + + // TODO_MA 注释: 通过 zookeeper.clientCnxnSocket 这个 key 配置具体的实现类 + String clientCnxnSocketName = getClientConfig().getProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + + // TODO_MA 注释: 这个是默认实现! + if (clientCnxnSocketName == null) { + clientCnxnSocketName = ClientCnxnSocketNIO.class.getName(); + } + + // TODO_MA 注释: 通过反射来创建实例对象 + try { + Constructor clientCxnConstructor = Class.forName(clientCnxnSocketName) + .getDeclaredConstructor(ZKClientConfig.class); + ClientCnxnSocket clientCxnSocket = (ClientCnxnSocket) clientCxnConstructor.newInstance(getClientConfig()); + return clientCxnSocket; + } catch (Exception e) { + throw new IOException("Couldn't instantiate " + clientCnxnSocketName, e); + } + } + + /** + * Validates the provided ACL list for null, empty or null value in it. + * + * @param acl ACL list + * @throws KeeperException.InvalidACLException if ACL list is not valid + */ + private void validateACL(List acl) throws KeeperException.InvalidACLException { + if (acl == null || acl.isEmpty() || acl.contains(null)) { + throw new KeeperException.InvalidACLException(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java new file mode 100644 index 0000000..435a3f4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperMain.java @@ -0,0 +1,484 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.NoSuchElementException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.cli.AddAuthCommand; +import org.apache.zookeeper.cli.AddWatchCommand; +import org.apache.zookeeper.cli.CliCommand; +import org.apache.zookeeper.cli.CliException; +import org.apache.zookeeper.cli.CloseCommand; +import org.apache.zookeeper.cli.CommandNotFoundException; +import org.apache.zookeeper.cli.CreateCommand; +import org.apache.zookeeper.cli.DelQuotaCommand; +import org.apache.zookeeper.cli.DeleteAllCommand; +import org.apache.zookeeper.cli.DeleteCommand; +import org.apache.zookeeper.cli.GetAclCommand; +import org.apache.zookeeper.cli.GetAllChildrenNumberCommand; +import org.apache.zookeeper.cli.GetCommand; +import org.apache.zookeeper.cli.GetConfigCommand; +import org.apache.zookeeper.cli.GetEphemeralsCommand; +import org.apache.zookeeper.cli.ListQuotaCommand; +import org.apache.zookeeper.cli.LsCommand; +import org.apache.zookeeper.cli.MalformedCommandException; +import org.apache.zookeeper.cli.ReconfigCommand; +import org.apache.zookeeper.cli.RemoveWatchesCommand; +import org.apache.zookeeper.cli.SetAclCommand; +import org.apache.zookeeper.cli.SetCommand; +import org.apache.zookeeper.cli.SetQuotaCommand; +import org.apache.zookeeper.cli.StatCommand; +import org.apache.zookeeper.cli.SyncCommand; +import org.apache.zookeeper.cli.VersionCommand; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The command line client to ZooKeeper. + * + */ +@InterfaceAudience.Public +public class ZooKeeperMain { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperMain.class); + static final Map commandMap = new HashMap(); + static final Map commandMapCli = new HashMap(); + + protected MyCommandOptions cl = new MyCommandOptions(); + protected HashMap history = new HashMap(); + protected int commandCount = 0; + protected boolean printWatches = true; + protected int exitCode = ExitCode.EXECUTION_FINISHED.getValue(); + + protected ZooKeeper zk; + protected String host = ""; + private CountDownLatch connectLatch = null; + + public boolean getPrintWatches() { + return printWatches; + } + + static { + commandMap.put("connect", "host:port"); + commandMap.put("history", ""); + commandMap.put("redo", "cmdno"); + commandMap.put("printwatches", "on|off"); + commandMap.put("quit", ""); + + new CloseCommand().addToMap(commandMapCli); + new CreateCommand().addToMap(commandMapCli); + new DeleteCommand().addToMap(commandMapCli); + new DeleteAllCommand().addToMap(commandMapCli); + new SetCommand().addToMap(commandMapCli); + new GetCommand().addToMap(commandMapCli); + new LsCommand().addToMap(commandMapCli); + new GetAclCommand().addToMap(commandMapCli); + new SetAclCommand().addToMap(commandMapCli); + new StatCommand().addToMap(commandMapCli); + new SyncCommand().addToMap(commandMapCli); + new SetQuotaCommand().addToMap(commandMapCli); + new ListQuotaCommand().addToMap(commandMapCli); + new DelQuotaCommand().addToMap(commandMapCli); + new AddAuthCommand().addToMap(commandMapCli); + new ReconfigCommand().addToMap(commandMapCli); + new GetConfigCommand().addToMap(commandMapCli); + new RemoveWatchesCommand().addToMap(commandMapCli); + new GetEphemeralsCommand().addToMap(commandMapCli); + new GetAllChildrenNumberCommand().addToMap(commandMapCli); + new VersionCommand().addToMap(commandMapCli); + new AddWatchCommand().addToMap(commandMapCli); + + // add all to commandMap + for (Entry entry : commandMapCli.entrySet()) { + commandMap.put(entry.getKey(), entry.getValue().getOptionStr()); + } + } + + static void usage() { + System.err.println("ZooKeeper -server host:port -client-configuration properties-file cmd args"); + List cmdList = new ArrayList(commandMap.keySet()); + Collections.sort(cmdList); + for (String cmd : cmdList) { + System.err.println("\t" + cmd + " " + commandMap.get(cmd)); + } + } + + private class MyWatcher implements Watcher { + + public void process(WatchedEvent event) { + if (getPrintWatches()) { + ZooKeeperMain.printMessage("WATCHER::"); + ZooKeeperMain.printMessage(event.toString()); + } + if (connectLatch != null) { + // connection success + if (event.getType() == Event.EventType.None + && event.getState() == Event.KeeperState.SyncConnected) { + connectLatch.countDown(); + } + } + } + + } + + /** + * A storage class for both command line options and shell commands. + * + */ + static class MyCommandOptions { + + private Map options = new HashMap(); + private List cmdArgs = null; + private String command = null; + public static final Pattern ARGS_PATTERN = Pattern.compile("\\s*([^\"\']\\S*|\"[^\"]*\"|'[^']*')\\s*"); + public static final Pattern QUOTED_PATTERN = Pattern.compile("^([\'\"])(.*)(\\1)$"); + + public MyCommandOptions() { + options.put("server", "localhost:2181"); + options.put("timeout", "30000"); + } + + public String getOption(String opt) { + return options.get(opt); + } + + public String getCommand() { + return command; + } + + public String getCmdArgument(int index) { + return cmdArgs.get(index); + } + + public int getNumArguments() { + return cmdArgs.size(); + } + + public String[] getArgArray() { + return cmdArgs.toArray(new String[0]); + } + + /** + * Parses a command line that may contain one or more flags + * before an optional command string + * @param args command line arguments + * @return true if parsing succeeded, false otherwise. + */ + public boolean parseOptions(String[] args) { + List argList = Arrays.asList(args); + Iterator it = argList.iterator(); + + while (it.hasNext()) { + String opt = it.next(); + try { + if (opt.equals("-server")) { + options.put("server", it.next()); + } else if (opt.equals("-timeout")) { + options.put("timeout", it.next()); + } else if (opt.equals("-r")) { + options.put("readonly", "true"); + } else if (opt.equals("-client-configuration")) { + options.put("client-configuration", it.next()); + } else if (opt.equals("-waitforconnection")) { + options.put("waitforconnection", "true"); + } + } catch (NoSuchElementException e) { + System.err.println("Error: no argument found for option " + opt); + return false; + } + + if (!opt.startsWith("-")) { + command = opt; + cmdArgs = new ArrayList(); + cmdArgs.add(command); + while (it.hasNext()) { + cmdArgs.add(it.next()); + } + return true; + } + } + return true; + } + + /** + * Breaks a string into command + arguments. + * @param cmdstring string of form "cmd arg1 arg2..etc" + * @return true if parsing succeeded. + */ + public boolean parseCommand(String cmdstring) { + Matcher matcher = ARGS_PATTERN.matcher(cmdstring); + + List args = new LinkedList(); + while (matcher.find()) { + String value = matcher.group(1); + if (QUOTED_PATTERN.matcher(value).matches()) { + // Strip off the surrounding quotes + value = value.substring(1, value.length() - 1); + } + args.add(value); + } + if (args.isEmpty()) { + return false; + } + command = args.get(0); + cmdArgs = args; + return true; + } + + } + + /** + * Makes a list of possible completions, either for commands + * or for zk nodes if the token to complete begins with / + * + */ + + protected void addToHistory(int i, String cmd) { + history.put(i, cmd); + } + + public static List getCommands() { + List cmdList = new ArrayList(commandMap.keySet()); + Collections.sort(cmdList); + return cmdList; + } + + protected String getPrompt() { + return "[zk: " + host + "(" + zk.getState() + ")" + " " + commandCount + "] "; + } + + public static void printMessage(String msg) { + System.out.println("\n" + msg); + } + + protected void connectToZK(String newHost) throws InterruptedException, IOException { + if (zk != null && zk.getState().isAlive()) { + zk.close(); + } + + host = newHost; + boolean readOnly = cl.getOption("readonly") != null; + if (cl.getOption("secure") != null) { + System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); + System.out.println("Secure connection is enabled"); + } + + ZKClientConfig clientConfig = null; + + if (cl.getOption("client-configuration") != null) { + try { + clientConfig = new ZKClientConfig(cl.getOption("client-configuration")); + } catch (QuorumPeerConfig.ConfigException e) { + e.printStackTrace(); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + } + + if (cl.getOption("waitforconnection") != null) { + connectLatch = new CountDownLatch(1); + } + int timeout = Integer.parseInt(cl.getOption("timeout")); + zk = new ZooKeeperAdmin(host, timeout, new MyWatcher(), readOnly, clientConfig); + if (connectLatch != null) { + if (!connectLatch.await(timeout, TimeUnit.MILLISECONDS)) { + zk.close(); + throw new IOException(KeeperException.create(KeeperException.Code.CONNECTIONLOSS)); + } + connectLatch = null; + } + } + + public static void main(String[] args) throws IOException, InterruptedException { + ZooKeeperMain main = new ZooKeeperMain(args); + main.run(); + } + + public ZooKeeperMain(String[] args) throws IOException, InterruptedException { + cl.parseOptions(args); + System.out.println("Connecting to " + cl.getOption("server")); + connectToZK(cl.getOption("server")); + } + + public ZooKeeperMain(ZooKeeper zk) { + this.zk = zk; + } + + void run() throws IOException, InterruptedException { + if (cl.getCommand() == null) { + System.out.println("Welcome to ZooKeeper!"); + + boolean jlinemissing = false; + // only use jline if it's in the classpath + try { + Class consoleC = Class.forName("jline.console.ConsoleReader"); + Class completorC = Class.forName("org.apache.zookeeper.JLineZNodeCompleter"); + + System.out.println("JLine support is enabled"); + + Object console = consoleC.getConstructor().newInstance(); + + Object completor = completorC.getConstructor(ZooKeeper.class).newInstance(zk); + Method addCompletor = consoleC.getMethod("addCompleter", Class.forName("jline.console.completer.Completer")); + addCompletor.invoke(console, completor); + + String line; + Method readLine = consoleC.getMethod("readLine", String.class); + while ((line = (String) readLine.invoke(console, getPrompt())) != null) { + executeLine(line); + } + } catch (ClassNotFoundException + | NoSuchMethodException + | InvocationTargetException + | IllegalAccessException + | InstantiationException e + ) { + LOG.debug("Unable to start jline", e); + jlinemissing = true; + } + + if (jlinemissing) { + System.out.println("JLine support is disabled"); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + String line; + while ((line = br.readLine()) != null) { + executeLine(line); + } + } + } else { + // Command line args non-null. Run what was passed. + processCmd(cl); + } + ServiceUtils.requestSystemExit(exitCode); + } + + public void executeLine(String line) throws InterruptedException, IOException { + if (!line.equals("")) { + cl.parseCommand(line); + addToHistory(commandCount, line); + processCmd(cl); + commandCount++; + } + } + + protected boolean processCmd(MyCommandOptions co) throws IOException, InterruptedException { + boolean watch = false; + try { + watch = processZKCmd(co); + exitCode = ExitCode.EXECUTION_FINISHED.getValue(); + } catch (CliException ex) { + exitCode = ex.getExitCode(); + System.err.println(ex.getMessage()); + } + return watch; + } + + protected boolean processZKCmd(MyCommandOptions co) throws CliException, IOException, InterruptedException { + String[] args = co.getArgArray(); + String cmd = co.getCommand(); + if (args.length < 1) { + usage(); + throw new MalformedCommandException("No command entered"); + } + + if (!commandMap.containsKey(cmd)) { + usage(); + throw new CommandNotFoundException("Command not found " + cmd); + } + + boolean watch = false; + + LOG.debug("Processing {}", cmd); + + if (cmd.equals("quit")) { + zk.close(); + ServiceUtils.requestSystemExit(exitCode); + } else if (cmd.equals("redo") && args.length >= 2) { + Integer i = Integer.decode(args[1]); + if (commandCount <= i || i < 0) { // don't allow redoing this redo + throw new MalformedCommandException("Command index out of range"); + } + cl.parseCommand(history.get(i)); + if (cl.getCommand().equals("redo")) { + throw new MalformedCommandException("No redoing redos"); + } + history.put(commandCount, history.get(i)); + processCmd(cl); + } else if (cmd.equals("history")) { + for (int i = commandCount - 10; i <= commandCount; ++i) { + if (i < 0) { + continue; + } + System.out.println(i + " - " + history.get(i)); + } + } else if (cmd.equals("printwatches")) { + if (args.length == 1) { + System.out.println("printwatches is " + (printWatches ? "on" : "off")); + } else { + printWatches = args[1].equals("on"); + } + } else if (cmd.equals("connect")) { + if (args.length >= 2) { + connectToZK(args[1]); + } else { + connectToZK(host); + } + } + + // Below commands all need a live connection + if (zk == null || !zk.getState().isAlive()) { + System.out.println("Not connected"); + return false; + } + + // execute from commandMap + CliCommand cliCmd = commandMapCli.get(cmd); + if (cliCmd != null) { + cliCmd.setZk(zk); + watch = cliCmd.parse(args).exec(); + } else if (!commandMap.containsKey(cmd)) { + usage(); + } + return watch; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java new file mode 100644 index 0000000..f7e403c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeperTestable.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class ZooKeeperTestable implements Testable { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperTestable.class); + + private final ClientCnxn clientCnxn; + + ZooKeeperTestable(ClientCnxn clientCnxn) { + this.clientCnxn = clientCnxn; + } + + @Override + public void injectSessionExpiration() { + LOG.info("injectSessionExpiration() called"); + + clientCnxn.eventThread.queueEvent(new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Expired, null)); + clientCnxn.eventThread.queueEventOfDeath(); + clientCnxn.state = ZooKeeper.States.CLOSED; + clientCnxn.sendThread.getClientCnxnSocket().onClosing(); + } + + @Override + public void queueEvent(WatchedEvent event) { + LOG.info("queueEvent() called: {}", event); + clientCnxn.eventThread.queueEvent(event); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/ZookeeperBanner.java b/zookeeper-server/src/main/java/org/apache/zookeeper/ZookeeperBanner.java new file mode 100644 index 0000000..0f69bbc --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/ZookeeperBanner.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import org.slf4j.Logger; + +/** + * ZookeeperBanner which writes the 'Zookeeper' banner at the start of zk server. + * + */ +public class ZookeeperBanner { + + private static final String[] BANNER = { + "", + " ______ _ ", + " |___ / | | ", + " / / ___ ___ | | __ ___ ___ _ __ ___ _ __ ", + " / / / _ \\ / _ \\ | |/ / / _ \\ / _ \\ | '_ \\ / _ \\ | '__|", + " / /__ | (_) | | (_) | | < | __/ | __/ | |_) | | __/ | | ", + " /_____| \\___/ \\___/ |_|\\_\\ \\___| \\___| | .__/ \\___| |_|", + " | | ", + " |_| ", ""}; + + public static void printBanner(Logger log) { + for (String line : BANNER) { + log.info(line); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/admin/ZooKeeperAdmin.java b/zookeeper-server/src/main/java/org/apache/zookeeper/admin/ZooKeeperAdmin.java new file mode 100644 index 0000000..8240526 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/admin/ZooKeeperAdmin.java @@ -0,0 +1,339 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.admin; + +import java.io.IOException; +import java.util.List; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.StringUtils; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.GetDataResponse; +import org.apache.zookeeper.proto.ReconfigRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.server.DataTree; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is the main class for ZooKeeperAdmin client library. + * This library is used to perform cluster administration tasks, + * such as reconfigure cluster membership. The ZooKeeperAdmin class + * inherits ZooKeeper and has similar usage pattern as ZooKeeper class. + * Please check {@link ZooKeeper} class document for more details. + * + * @since 3.5.3 + */ +// See ZooKeeper.java for an explanation of why we need @SuppressWarnings("try") +@SuppressWarnings("try") +@InterfaceAudience.Public +public class ZooKeeperAdmin extends ZooKeeper { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperAdmin.class); + + /** + * Create a ZooKeeperAdmin object which is used to perform dynamic reconfiguration + * operations. + * + * @param connectString + * comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout + * session timeout in milliseconds + * @param watcher + * a watcher object which will be notified of state changes, may + * also be notified for node events + * + * @throws IOException + * in cases of network failure + * @throws IllegalArgumentException + * if an invalid chroot path is specified + * + * @see ZooKeeper#ZooKeeper(String, int, Watcher) + * + */ + public ZooKeeperAdmin( + String connectString, + int sessionTimeout, + Watcher watcher) throws IOException { + super(connectString, sessionTimeout, watcher); + } + + /** + * Create a ZooKeeperAdmin object which is used to perform dynamic reconfiguration + * operations. + * + * @param connectString + * comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout + * session timeout in milliseconds + * @param watcher + * a watcher object which will be notified of state changes, may + * also be notified for node events + * @param conf + * passing this conf object gives each client the flexibility of + * configuring properties differently compared to other instances + * + * @throws IOException + * in cases of network failure + * @throws IllegalArgumentException + * if an invalid chroot path is specified + * + * @see ZooKeeper#ZooKeeper(String, int, Watcher, ZKClientConfig) + */ + public ZooKeeperAdmin( + String connectString, + int sessionTimeout, + Watcher watcher, + ZKClientConfig conf) throws IOException { + super(connectString, sessionTimeout, watcher, conf); + } + + /** + * Create a ZooKeeperAdmin object which is used to perform dynamic reconfiguration + * operations. + * + * @param connectString + * comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout + * session timeout in milliseconds + * @param watcher + * a watcher object which will be notified of state changes, may + * also be notified for node events + * @param canBeReadOnly + * whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * @param conf + * passing this conf object gives each client the flexibility of + * configuring properties differently compared to other instances + * + * @throws IOException + * in cases of network failure + * @throws IllegalArgumentException + * if an invalid chroot path is specified + * + * @see ZooKeeper#ZooKeeper(String, int, Watcher, boolean, ZKClientConfig) + * + * @since 3.6.1 + */ + public ZooKeeperAdmin( + String connectString, + int sessionTimeout, + Watcher watcher, + boolean canBeReadOnly, + ZKClientConfig conf) throws IOException { + super(connectString, sessionTimeout, watcher, canBeReadOnly, conf); + } + + /** + * Create a ZooKeeperAdmin object which is used to perform dynamic reconfiguration + * operations. + * + * @param connectString + * comma separated host:port pairs, each corresponding to a zk + * server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If + * the optional chroot suffix is used the example would look + * like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" + * where the client would be rooted at "/app/a" and all paths + * would be relative to this root - ie getting/setting/etc... + * "/foo/bar" would result in operations being run on + * "/app/a/foo/bar" (from the server perspective). + * @param sessionTimeout + * session timeout in milliseconds + * @param watcher + * a watcher object which will be notified of state changes, may + * also be notified for node events + * @param canBeReadOnly + * whether the created client is allowed to go to + * read-only mode in case of partitioning. Read-only mode + * basically means that if the client can't find any majority + * servers but there's partitioned server it could reach, it + * connects to one in read-only mode, i.e. read requests are + * allowed while write requests are not. It continues seeking for + * majority in the background. + * + * @throws IOException + * in cases of network failure + * @throws IllegalArgumentException + * if an invalid chroot path is specified + * + * @see ZooKeeper#ZooKeeper(String, int, Watcher, boolean) + */ + public ZooKeeperAdmin( + String connectString, + int sessionTimeout, + Watcher watcher, + boolean canBeReadOnly) throws IOException { + super(connectString, sessionTimeout, watcher, canBeReadOnly); + } + + /** + * Reconfigure - add/remove servers. Return the new configuration. + * @param joiningServers + * a comma separated list of servers being added (incremental reconfiguration) + * @param leavingServers + * a comma separated list of servers being removed (incremental reconfiguration) + * @param newMembers + * a comma separated list of new membership (non-incremental reconfiguration) + * @param fromConfig + * version of the current configuration + * (optional - causes reconfiguration to throw an exception if configuration is no longer current) + * @param stat the stat of /zookeeper/config znode will be copied to this + * parameter if not null. + * @return new configuration + * @throws InterruptedException If the server transaction is interrupted. + * @throws KeeperException If the server signals an error with a non-zero error code. + */ + public byte[] reconfigure( + String joiningServers, + String leavingServers, + String newMembers, + long fromConfig, + Stat stat) throws KeeperException, InterruptedException { + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.reconfig); + ReconfigRequest request = new ReconfigRequest(joiningServers, leavingServers, newMembers, fromConfig); + GetDataResponse response = new GetDataResponse(); + ReplyHeader r = cnxn.submitRequest(h, request, response, null); + if (r.getErr() != 0) { + throw KeeperException.create(KeeperException.Code.get(r.getErr()), ""); + } + if (stat != null) { + DataTree.copyStat(response.getStat(), stat); + } + return response.getData(); + } + + /** + * Convenience wrapper around reconfig that takes Lists of strings instead of comma-separated servers. + * + * @see #reconfigure + * + */ + public byte[] reconfigure( + List joiningServers, + List leavingServers, + List newMembers, + long fromConfig, + Stat stat) throws KeeperException, InterruptedException { + return reconfigure( + StringUtils.joinStrings(joiningServers, ","), + StringUtils.joinStrings(leavingServers, ","), + StringUtils.joinStrings(newMembers, ","), + fromConfig, + stat); + } + + /** + * The Asynchronous version of reconfig. + * + * @see #reconfigure + * + **/ + public void reconfigure( + String joiningServers, + String leavingServers, + String newMembers, + long fromConfig, + DataCallback cb, + Object ctx) { + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.reconfig); + ReconfigRequest request = new ReconfigRequest(joiningServers, leavingServers, newMembers, fromConfig); + GetDataResponse response = new GetDataResponse(); + cnxn.queuePacket( + h, + new ReplyHeader(), + request, + response, + cb, + ZooDefs.CONFIG_NODE, + ZooDefs.CONFIG_NODE, + ctx, + null); + } + + /** + * Convenience wrapper around asynchronous reconfig that takes Lists of strings instead of comma-separated servers. + * + * @see #reconfigure + * + */ + public void reconfigure( + List joiningServers, + List leavingServers, + List newMembers, + long fromConfig, + DataCallback cb, + Object ctx) { + reconfigure( + StringUtils.joinStrings(joiningServers, ","), + StringUtils.joinStrings(leavingServers, ","), + StringUtils.joinStrings(newMembers, ","), + fromConfig, + cb, + ctx); + } + + /** + * String representation of this ZooKeeperAdmin client. Suitable for things + * like logging. + * + * Do NOT count on the format of this string, it may change without + * warning. + * + * @since 3.5.3 + */ + @Override + public String toString() { + return super.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditConstants.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditConstants.java new file mode 100644 index 0000000..22fd856 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditConstants.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +public final class AuditConstants { + private AuditConstants() { + //Utility classes should not have public constructors + } + + static final String OP_START = "serverStart"; + static final String OP_STOP = "serverStop"; + public static final String OP_CREATE = "create"; + public static final String OP_DELETE = "delete"; + public static final String OP_SETDATA = "setData"; + public static final String OP_SETACL = "setAcl"; + public static final String OP_MULTI_OP = "multiOperation"; + public static final String OP_RECONFIG = "reconfig"; + public static final String OP_DEL_EZNODE_EXP = "ephemeralZNodeDeletionOnSessionCloseOrExpire"; +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditEvent.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditEvent.java new file mode 100644 index 0000000..e499552 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditEvent.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +public final class AuditEvent { + private static final char PAIR_SEPARATOR = '\t'; + private static final String KEY_VAL_SEPARATOR = "="; + // Holds the entries which to be logged. + private Map logEntries = new LinkedHashMap<>(); + private Result result; + + AuditEvent(Result result) { + this.result = result; + } + + /** + * Gives all entries to be logged. + * + * @return log entries + */ + public Set> getLogEntries() { + return logEntries.entrySet(); + } + + void addEntry(FieldName fieldName, String value) { + if (value != null) { + logEntries.put(fieldName.name().toLowerCase(), value); + } + } + + public String getValue(FieldName fieldName) { + return logEntries.get(fieldName.name().toLowerCase()); + } + + public Result getResult() { + return result; + } + + /** + * Gives the string to be logged, ignores fields with null values + * + * @return String + */ + @Override + public String toString() { + StringBuilder buffer = new StringBuilder(); + boolean first = true; + for (Map.Entry entry : logEntries.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if (null != value) { + // if first field then no need to add the tabs + if (first) { + first = false; + } else { + buffer.append(PAIR_SEPARATOR); + } + buffer.append(key).append(KEY_VAL_SEPARATOR) + .append(value); + } + } + //add result field + if (buffer.length() > 0) { + buffer.append(PAIR_SEPARATOR); + } + buffer.append("result").append(KEY_VAL_SEPARATOR) + .append(result.name().toLowerCase()); + return buffer.toString(); + } + + public enum FieldName { + USER, OPERATION, IP, ACL, ZNODE, SESSION, ZNODE_TYPE + } + + public enum Result { + SUCCESS, FAILURE, INVOKED + } +} + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java new file mode 100644 index 0000000..ce0d58a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java @@ -0,0 +1,226 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.MultiOperationRecord; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.audit.AuditEvent.Result; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.SetACLRequest; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Helper class to decouple audit log code. + */ +public final class AuditHelper { + private static final Logger LOG = LoggerFactory.getLogger(AuditHelper.class); + + public static void addAuditLog(Request request, ProcessTxnResult rc) { + addAuditLog(request, rc, false); + } + + /** + * Add audit log if audit log is enabled and operation is of type which to be audit logged. + * + * @param request user request + * @param txnResult ProcessTxnResult + * @param failedTxn whether audit is being done failed transaction for normal transaction + */ + public static void addAuditLog(Request request, ProcessTxnResult txnResult, boolean failedTxn) { + if (!ZKAuditProvider.isAuditEnabled()) { + return; + } + String op = null; + //For failed transaction rc.path is null + String path = txnResult.path; + String acls = null; + String createMode = null; + try { + switch (request.type) { + case ZooDefs.OpCode.create: + case ZooDefs.OpCode.create2: + case ZooDefs.OpCode.createContainer: + op = AuditConstants.OP_CREATE; + if (failedTxn) { + CreateRequest createRequest = new CreateRequest(); + deserialize(request, createRequest); + path = createRequest.getPath(); + createMode = + getCreateMode(createRequest); + } else { + createMode = getCreateMode(request); + } + break; + case ZooDefs.OpCode.delete: + case ZooDefs.OpCode.deleteContainer: + op = AuditConstants.OP_DELETE; + if (failedTxn) { + DeleteRequest deleteRequest = new DeleteRequest(); + deserialize(request, deleteRequest); + path = deleteRequest.getPath(); + } + break; + case ZooDefs.OpCode.setData: + op = AuditConstants.OP_SETDATA; + if (failedTxn) { + SetDataRequest setDataRequest = new SetDataRequest(); + deserialize(request, setDataRequest); + path = setDataRequest.getPath(); + } + break; + case ZooDefs.OpCode.setACL: + op = AuditConstants.OP_SETACL; + if (failedTxn) { + SetACLRequest setACLRequest = new SetACLRequest(); + deserialize(request, setACLRequest); + path = setACLRequest.getPath(); + acls = ZKUtil.aclToString(setACLRequest.getAcl()); + } else { + acls = getACLs(request); + } + break; + case ZooDefs.OpCode.multi: + if (failedTxn) { + op = AuditConstants.OP_MULTI_OP; + } else { + logMultiOperation(request, txnResult); + //operation si already logged + return; + } + break; + case ZooDefs.OpCode.reconfig: + op = AuditConstants.OP_RECONFIG; + break; + default: + //Not an audit log operation + return; + } + Result result = getResult(txnResult, failedTxn); + log(request, path, op, acls, createMode, result); + } catch (Throwable e) { + LOG.error("Failed to audit log request {}", request.type, e); + } + } + + private static void deserialize(Request request, Record record) throws IOException { + request.request.rewind(); + ByteBufferInputStream.byteBuffer2Record(request.request.slice(), record); + } + + private static Result getResult(ProcessTxnResult rc, boolean failedTxn) { + if (failedTxn) { + return Result.FAILURE; + } else { + return rc.err == KeeperException.Code.OK.intValue() ? Result.SUCCESS : Result.FAILURE; + } + } + + private static void logMultiOperation(Request request, ProcessTxnResult rc) throws IOException, KeeperException { + Map createModes = AuditHelper.getCreateModes(request); + boolean multiFailed = false; + for (ProcessTxnResult subTxnResult : rc.multiResult) { + switch (subTxnResult.type) { + case ZooDefs.OpCode.create: + case ZooDefs.OpCode.create2: + case ZooDefs.OpCode.createTTL: + case ZooDefs.OpCode.createContainer: + log(request, subTxnResult.path, AuditConstants.OP_CREATE, null, + createModes.get(subTxnResult.path), Result.SUCCESS); + break; + case ZooDefs.OpCode.delete: + case ZooDefs.OpCode.deleteContainer: + log(request, subTxnResult.path, AuditConstants.OP_DELETE, null, + null, Result.SUCCESS); + break; + case ZooDefs.OpCode.setData: + log(request, subTxnResult.path, AuditConstants.OP_SETDATA, null, + null, Result.SUCCESS); + break; + case ZooDefs.OpCode.error: + multiFailed = true; + break; + default: + // Do nothing, it ok, we do not log all multi operations + } + } + if (multiFailed) { + log(request, rc.path, AuditConstants.OP_MULTI_OP, null, + null, Result.FAILURE); + } + } + + private static void log(Request request, String path, String op, String acls, String createMode, Result result) { + log(request.getUsers(), op, path, acls, createMode, + request.cnxn.getSessionIdHex(), request.cnxn.getHostAddress(), result); + } + + private static void log(String user, String operation, String znode, String acl, + String createMode, String session, String ip, Result result) { + ZKAuditProvider.log(user, operation, znode, acl, createMode, session, ip, result); + } + + private static String getACLs(Request request) throws IOException { + SetACLRequest setACLRequest = new SetACLRequest(); + deserialize(request, setACLRequest); + return ZKUtil.aclToString(setACLRequest.getAcl()); + } + + private static String getCreateMode(Request request) throws IOException, KeeperException { + CreateRequest createRequest = new CreateRequest(); + deserialize(request, createRequest); + return getCreateMode(createRequest); + } + + private static String getCreateMode(CreateRequest createRequest) throws KeeperException { + return CreateMode.fromFlag(createRequest.getFlags()).toString().toLowerCase(); + } + + private static Map getCreateModes(Request request) + throws IOException, KeeperException { + Map createModes = new HashMap<>(); + if (!ZKAuditProvider.isAuditEnabled()) { + return createModes; + } + MultiOperationRecord multiRequest = new MultiOperationRecord(); + deserialize(request, multiRequest); + for (Op op : multiRequest) { + if (op.getType() == ZooDefs.OpCode.create || op.getType() == ZooDefs.OpCode.create2 + || op.getType() == ZooDefs.OpCode.createContainer) { + CreateRequest requestRecord = (CreateRequest) op.toRequestRecord(); + createModes.put(requestRecord.getPath(), + getCreateMode(requestRecord)); + } + } + return createModes; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditLogger.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditLogger.java new file mode 100644 index 0000000..0f84488 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditLogger.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +public interface AuditLogger { + + /** + * Called during initialization of the logger. + */ + default void initialize() { + } + + /** + * Called to log an audit event. + * + * @param auditEvent contains all the fields to be logged + */ + void logAuditEvent(AuditEvent auditEvent); +} \ No newline at end of file diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/Log4jAuditLogger.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/Log4jAuditLogger.java new file mode 100644 index 0000000..c4efc1c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/Log4jAuditLogger.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import org.apache.zookeeper.audit.AuditEvent.Result; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Log4j based audit logger + */ +public class Log4jAuditLogger implements AuditLogger { + private static final Logger LOG = LoggerFactory.getLogger(Log4jAuditLogger.class); + + @Override + public void logAuditEvent(AuditEvent auditEvent) { + if (auditEvent.getResult() == Result.FAILURE) { + LOG.error(auditEvent.toString()); + } else { + LOG.info(auditEvent.toString()); + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/audit/ZKAuditProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/ZKAuditProvider.java new file mode 100644 index 0000000..c8c4a8f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/audit/ZKAuditProvider.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import static org.apache.zookeeper.audit.AuditEvent.FieldName; + +import java.lang.reflect.Constructor; + +import org.apache.zookeeper.audit.AuditEvent.Result; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZKAuditProvider { + static final String AUDIT_ENABLE = "zookeeper.audit.enable"; + static final String AUDIT_IMPL_CLASS = "zookeeper.audit.impl.class"; + private static final Logger LOG = LoggerFactory.getLogger(ZKAuditProvider.class); + // By default audit logging is disabled + private static boolean auditEnabled; + private static AuditLogger auditLogger; + + static { + auditEnabled = Boolean.getBoolean(AUDIT_ENABLE); + if(auditEnabled) { + //initialise only when audit logging is enabled + auditLogger = getAuditLogger(); + LOG.info("ZooKeeper audit is enabled."); + } else { + LOG.info("ZooKeeper audit is disabled."); + } + } + + private static AuditLogger getAuditLogger() { + String auditLoggerClass = System.getProperty(AUDIT_IMPL_CLASS); + if(auditLoggerClass == null) { + auditLoggerClass = Log4jAuditLogger.class.getName(); + } + try { + Constructor clientCxnConstructor = Class.forName(auditLoggerClass).getDeclaredConstructor(); + AuditLogger auditLogger = (AuditLogger) clientCxnConstructor.newInstance(); + auditLogger.initialize(); + return auditLogger; + } catch(Exception e) { + throw new RuntimeException("Couldn't instantiate " + auditLoggerClass, e); + } + } + + /** + * @return true if audit log is enabled + */ + public static boolean isAuditEnabled() { + return auditEnabled; + } + + public static void log(String user, String operation, String znode, String acl, String createMode, String session, + String ip, Result result) { + auditLogger.logAuditEvent(createLogEvent(user, operation, znode, acl, createMode, session, ip, result)); + } + + /** + * A helper api for creating an AuditEvent object. + */ + static AuditEvent createLogEvent(String user, String operation, Result result) { + AuditEvent event = new AuditEvent(result); + event.addEntry(FieldName.USER, user); + event.addEntry(FieldName.OPERATION, operation); + return event; + } + + /** + * A helper api for creating an AuditEvent object. + */ + static AuditEvent createLogEvent(String user, String operation, String znode, String acl, String createMode, + String session, String ip, Result result) { + AuditEvent event = new AuditEvent(result); + event.addEntry(FieldName.SESSION, session); + event.addEntry(FieldName.USER, user); + event.addEntry(FieldName.IP, ip); + event.addEntry(FieldName.OPERATION, operation); + event.addEntry(FieldName.ZNODE, znode); + event.addEntry(FieldName.ZNODE_TYPE, createMode); + event.addEntry(FieldName.ACL, acl); + return event; + } + + /** + * Add audit log for server start and register server stop log. + */ + public static void addZKStartStopAuditLog() { + if(isAuditEnabled()) { + log(getZKUser(), AuditConstants.OP_START, Result.SUCCESS); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + log(getZKUser(), AuditConstants.OP_STOP, Result.INVOKED); + })); + } + } + + /** + * Add audit log for server start fail. + */ + public static void addServerStartFailureAuditLog() { + if(isAuditEnabled()) { + log(ZKAuditProvider.getZKUser(), AuditConstants.OP_START, Result.FAILURE); + } + } + + private static void log(String user, String operation, Result result) { + auditLogger.logAuditEvent(createLogEvent(user, operation, result)); + } + + /** + * User who has started the ZooKeeper server user, it will be the logged-in + * user. If no user logged-in then system user. + */ + public static String getZKUser() { + return ServerCnxnFactory.getUserName(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AclParser.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AclParser.java new file mode 100644 index 0000000..8111158 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AclParser.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; + +/** + * a parser for ACL strings + */ +public class AclParser { + + /** + * parse string into list of ACL + * @param aclString + * @return + */ + public static List parse(String aclString) { + List acl; + String[] acls = aclString.split(","); + acl = new ArrayList(); + for (String a : acls) { + int firstColon = a.indexOf(':'); + int lastColon = a.lastIndexOf(':'); + if (firstColon == -1 || lastColon == -1 || firstColon == lastColon) { + System.err.println(a + " does not have the form scheme:id:perm"); + continue; + } + ACL newAcl = new ACL(); + newAcl.setId(new Id(a.substring(0, firstColon), a.substring(firstColon + 1, lastColon))); + newAcl.setPerms(getPermFromString(a.substring(lastColon + 1))); + acl.add(newAcl); + } + return acl; + } + + private static int getPermFromString(String permString) { + int perm = 0; + for (int i = 0; i < permString.length(); i++) { + switch (permString.charAt(i)) { + case 'r': + perm |= ZooDefs.Perms.READ; + break; + case 'w': + perm |= ZooDefs.Perms.WRITE; + break; + case 'c': + perm |= ZooDefs.Perms.CREATE; + break; + case 'd': + perm |= ZooDefs.Perms.DELETE; + break; + case 'a': + perm |= ZooDefs.Perms.ADMIN; + break; + default: + System.err.println("Unknown perm type: " + permString.charAt(i)); + } + } + return perm; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AddAuthCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AddAuthCommand.java new file mode 100644 index 0000000..de466f6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AddAuthCommand.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; + +/** + * addAuth command for cli + */ +public class AddAuthCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + + public AddAuthCommand() { + super("addauth", "scheme auth"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + CommandLine cl; + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + byte[] b = null; + if (args.length >= 3) { + b = args[2].getBytes(); + } + + zk.addAuthInfo(args[1], b); + + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AddWatchCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AddWatchCommand.java new file mode 100644 index 0000000..1e34b10 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/AddWatchCommand.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.util.Arrays; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.AddWatchMode; +import org.apache.zookeeper.KeeperException; + +/** + * addWatch command for cli. + * Matches the ZooKeeper API addWatch() + */ +public class AddWatchCommand extends CliCommand { + + private static final Options options = new Options(); + private static final AddWatchMode defaultMode = AddWatchMode.PERSISTENT_RECURSIVE; + + private CommandLine cl; + private AddWatchMode mode = defaultMode; + + static { + options.addOption("m", true, ""); + } + + public AddWatchCommand() { + super("addWatch", "[-m mode] path # optional mode is one of " + + Arrays.toString(AddWatchMode.values()) + " - default is " + defaultMode.name()); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + if (cl.getArgs().length != 2) { + throw new CliParseException(getUsageStr()); + } + + if (cl.hasOption("m")) { + try { + mode = AddWatchMode.valueOf(cl.getOptionValue("m").toUpperCase()); + } catch (IllegalArgumentException e) { + throw new CliParseException(getUsageStr()); + } + } + + return this; + } + + @Override + public boolean exec() throws CliException { + String path = cl.getArgs()[1]; + try { + zk.addWatch(path, mode); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + + return false; + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliCommand.java new file mode 100644 index 0000000..83e03a8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliCommand.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.io.PrintStream; +import java.util.Map; +import org.apache.zookeeper.ZooKeeper; + +/** + * base class for all CLI commands + */ +public abstract class CliCommand { + + protected ZooKeeper zk; + protected PrintStream out; + protected PrintStream err; + private String cmdStr; + private String optionStr; + + /** + * a CLI command with command string and options. + * Using System.out and System.err for printing + * @param cmdStr the string used to call this command + * @param optionStr the string used to call this command + */ + public CliCommand(String cmdStr, String optionStr) { + this.out = System.out; + this.err = System.err; + this.cmdStr = cmdStr; + this.optionStr = optionStr; + } + + /** + * Set out printStream (usable for testing) + * @param out + */ + public void setOut(PrintStream out) { + this.out = out; + } + + /** + * Set err printStream (usable for testing) + * @param err + */ + public void setErr(PrintStream err) { + this.err = err; + } + + /** + * set the zookeeper instance + * @param zk the ZooKeeper instance. + */ + public void setZk(ZooKeeper zk) { + this.zk = zk; + } + + /** + * get the string used to call this command + * @return + */ + public String getCmdStr() { + return cmdStr; + } + + /** + * get the option string + * @return + */ + public String getOptionStr() { + return optionStr; + } + + /** + * get a usage string, contains the command and the options + * @return + */ + public String getUsageStr() { + return cmdStr + " " + optionStr; + } + + /** + * add this command to a map. Use the command string as key. + * @param cmdMap + */ + public void addToMap(Map cmdMap) { + cmdMap.put(cmdStr, this); + } + + /** + * parse the command arguments + * @param cmdArgs + * @return this CliCommand + * @throws CliParseException + */ + public abstract CliCommand parse(String[] cmdArgs) throws CliParseException; + + /** + * + * @return + * @throws CliException + */ + public abstract boolean exec() throws CliException; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliException.java new file mode 100644 index 0000000..abee803 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliException.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +@SuppressWarnings("serial") +public class CliException extends Exception { + + protected int exitCode; + + protected static final int DEFAULT_EXCEPTION_EXIT_CODE = 1; + + public CliException(String message) { + this(message, DEFAULT_EXCEPTION_EXIT_CODE); + } + + public CliException(String message, int exitCode) { + super(message); + this.exitCode = exitCode; + } + + public CliException(Throwable cause) { + this(cause, DEFAULT_EXCEPTION_EXIT_CODE); + } + + public CliException(Throwable cause, int exitCode) { + super(cause); + this.exitCode = exitCode; + } + + public CliException(String message, Throwable cause) { + this(message, cause, DEFAULT_EXCEPTION_EXIT_CODE); + } + + public CliException(String message, Throwable cause, int exitCode) { + super(message, cause); + this.exitCode = exitCode; + } + + public int getExitCode() { + return exitCode; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliParseException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliParseException.java new file mode 100644 index 0000000..7311210 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliParseException.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.ParseException; + +@SuppressWarnings("serial") +public class CliParseException extends CliException { + + public CliParseException(ParseException parseException) { + super(parseException); + } + + public CliParseException(String message) { + super(message); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliWrapperException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliWrapperException.java new file mode 100644 index 0000000..fcf46d9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CliWrapperException.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.zookeeper.KeeperException; + +@SuppressWarnings("serial") +public class CliWrapperException extends CliException { + + public CliWrapperException(Throwable cause) { + super(getMessage(cause), cause); + } + + private static String getMessage(Throwable cause) { + if (cause instanceof KeeperException) { + KeeperException keeperException = (KeeperException) cause; + if (keeperException instanceof KeeperException.NoNodeException) { + return "Node does not exist: " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.NoChildrenForEphemeralsException) { + return "Ephemerals cannot have children: " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.NodeExistsException) { + return "Node already exists: " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.NotEmptyException) { + return "Node not empty: " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.NotReadOnlyException) { + return "Not a read-only call: " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.InvalidACLException) { + return "Acl is not valid : " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.NoAuthException) { + return "Insufficient permission : " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.BadArgumentsException) { + return "Arguments are not valid : " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.BadVersionException) { + return "version No is not valid : " + keeperException.getPath(); + } else if (keeperException instanceof KeeperException.ReconfigInProgress) { + return "Another reconfiguration is in progress -- concurrent " + "reconfigs not supported (yet)"; + } else if (keeperException instanceof KeeperException.NewConfigNoQuorum) { + return "No quorum of new config is connected and " + + "up-to-date with the leader of last commmitted config - try invoking reconfiguration after " + + "new servers are connected and synced"; + } + } + return cause.getMessage(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CloseCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CloseCommand.java new file mode 100644 index 0000000..37317e9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CloseCommand.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +/** + * close command for cli + */ +public class CloseCommand extends CliCommand { + + public CloseCommand() { + super("close", ""); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + return this; + } + + @Override + public boolean exec() throws CliException { + try { + zk.close(); + } catch (Exception ex) { + throw new CliWrapperException(ex); + } + + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandNotFoundException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandNotFoundException.java new file mode 100644 index 0000000..6f4b1c3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CommandNotFoundException.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +@SuppressWarnings("serial") +public class CommandNotFoundException extends CliException { + + public CommandNotFoundException(String command) { + super("Command not found: " + command, 127); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CreateCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CreateCommand.java new file mode 100644 index 0000000..d906238 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/CreateCommand.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.EphemeralType; + +/** + * create command for cli + */ +public class CreateCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption(new Option("e", false, "ephemeral")); + options.addOption(new Option("s", false, "sequential")); + options.addOption(new Option("c", false, "container")); + options.addOption(new Option("t", true, "ttl")); + } + + public CreateCommand() { + super("create", "[-s] [-e] [-c] [-t ttl] path [data] [acl]"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + return this; + } + + @Override + public boolean exec() throws CliException { + boolean hasE = cl.hasOption("e"); + boolean hasS = cl.hasOption("s"); + boolean hasC = cl.hasOption("c"); + boolean hasT = cl.hasOption("t"); + if (hasC && (hasE || hasS)) { + throw new MalformedCommandException("-c cannot be combined with -s or -e. Containers cannot be ephemeral or sequential."); + } + long ttl; + try { + ttl = hasT ? Long.parseLong(cl.getOptionValue("t")) : 0; + } catch (NumberFormatException e) { + throw new MalformedCommandException("-t argument must be a long value"); + } + + if (hasT && hasE) { + throw new MalformedCommandException("TTLs cannot be used with Ephemeral znodes"); + } + if (hasT && hasC) { + throw new MalformedCommandException("TTLs cannot be used with Container znodes"); + } + + CreateMode flags; + if (hasE && hasS) { + flags = CreateMode.EPHEMERAL_SEQUENTIAL; + } else if (hasE) { + flags = CreateMode.EPHEMERAL; + } else if (hasS) { + flags = hasT ? CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL : CreateMode.PERSISTENT_SEQUENTIAL; + } else if (hasC) { + flags = CreateMode.CONTAINER; + } else { + flags = hasT ? CreateMode.PERSISTENT_WITH_TTL : CreateMode.PERSISTENT; + } + if (hasT) { + try { + EphemeralType.TTL.toEphemeralOwner(ttl); + } catch (IllegalArgumentException e) { + throw new MalformedCommandException(e.getMessage()); + } + } + + String path = args[1]; + byte[] data = null; + if (args.length > 2) { + data = args[2].getBytes(); + } + List acl = ZooDefs.Ids.OPEN_ACL_UNSAFE; + if (args.length > 3) { + acl = AclParser.parse(args[3]); + } + try { + String newPath = hasT + ? zk.create(path, data, acl, flags, new Stat(), ttl) + : zk.create(path, data, acl, flags); + err.println("Created " + newPath); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException.EphemeralOnLocalSessionException e) { + err.println("Unable to create ephemeral node on a local session"); + throw new CliWrapperException(e); + } catch (KeeperException.InvalidACLException ex) { + err.println(ex.getMessage()); + throw new CliWrapperException(ex); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DelQuotaCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DelQuotaCommand.java new file mode 100644 index 0000000..56e630b --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DelQuotaCommand.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.io.IOException; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; + +/** + * delQuota command for cli + */ +public class DelQuotaCommand extends CliCommand { + + private Options options = new Options(); + private String[] args; + private CommandLine cl; + + public DelQuotaCommand() { + super("delquota", "[-n|-b] path"); + + OptionGroup og1 = new OptionGroup(); + og1.addOption(new Option("b", false, "bytes quota")); + og1.addOption(new Option("n", false, "num quota")); + options.addOptionGroup(og1); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + //if neither option -n or -b is specified, we delete + // the quota node for this node. + String path = args[1]; + try { + if (cl.hasOption("b")) { + delQuota(zk, path, true, false); + } else if (cl.hasOption("n")) { + delQuota(zk, path, false, true); + } else if (args.length == 2) { + // we don't have an option specified. + // just delete whole quota node + delQuota(zk, path, true, true); + } + } catch (KeeperException | InterruptedException | IOException ex) { + throw new CliWrapperException(ex); + } + return false; + } + + /** + * this method deletes quota for a node. + * + * @param zk the zookeeper client + * @param path the path to delete quota for + * @param bytes true if number of bytes needs to be unset + * @param numNodes true if number of nodes needs to be unset + * @return true if quota deletion is successful + * @throws KeeperException + * @throws IOException + * @throws InterruptedException + */ + public static boolean delQuota( + ZooKeeper zk, + String path, + boolean bytes, + boolean numNodes) throws KeeperException, IOException, InterruptedException, MalformedPathException { + String parentPath = Quotas.quotaZookeeper + path; + String quotaPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; + if (zk.exists(quotaPath, false) == null) { + System.out.println("Quota does not exist for " + path); + return true; + } + byte[] data = null; + try { + data = zk.getData(quotaPath, false, new Stat()); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException.NoNodeException ne) { + System.err.println("quota does not exist for " + path); + return true; + } + StatsTrack strack = new StatsTrack(new String(data)); + if (bytes && !numNodes) { + strack.setBytes(-1L); + zk.setData(quotaPath, strack.toString().getBytes(), -1); + } else if (!bytes && numNodes) { + strack.setCount(-1); + zk.setData(quotaPath, strack.toString().getBytes(), -1); + } else if (bytes && numNodes) { + // delete till you can find a node with more than + // one child + List children = zk.getChildren(parentPath, false); + /// delete the direct children first + for (String child : children) { + zk.delete(parentPath + "/" + child, -1); + } + // cut the tree till their is more than one child + trimProcQuotas(zk, parentPath); + } + return true; + } + + /** + * trim the quota tree to recover unwanted tree elements in the quota's tree + * + * @param zk the zookeeper client + * @param path the path to start from and go up and see if their is any + * unwanted parent in the path. + * @return true if successful + * @throws KeeperException + * @throws IOException + * @throws InterruptedException + */ + private static boolean trimProcQuotas( + ZooKeeper zk, + String path) throws KeeperException, IOException, InterruptedException { + if (Quotas.quotaZookeeper.equals(path)) { + return true; + } + List children = zk.getChildren(path, false); + if (children.size() == 0) { + zk.delete(path, -1); + String parent = path.substring(0, path.lastIndexOf('/')); + return trimProcQuotas(zk, parent); + } else { + return true; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DeleteAllCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DeleteAllCommand.java new file mode 100644 index 0000000..cbeceb0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DeleteAllCommand.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKUtil; + +/** + * deleteAll command for cli + */ +public class DeleteAllCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption(new Option("b", true, "batch size")); + } + + public DeleteAllCommand() { + this("deleteall"); + } + + public DeleteAllCommand(String cmdStr) { + super(cmdStr, "path [-b batch size]"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + int batchSize; + try { + batchSize = cl.hasOption("b") ? Integer.parseInt(cl.getOptionValue("b")) : 1000; + } catch (NumberFormatException e) { + throw new MalformedCommandException("-b argument must be an int value"); + } + + String path = args[1]; + try { + boolean success = ZKUtil.deleteRecursive(zk, path, batchSize); + if (!success) { + err.println("Failed to delete some node(s) in the subtree!"); + } + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DeleteCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DeleteCommand.java new file mode 100644 index 0000000..228eba4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/DeleteCommand.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; + +/** + * delete command for cli + */ +public class DeleteCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("v", true, "version"); + } + + public DeleteCommand() { + super("delete", "[-v version] path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + retainCompatibility(cmdArgs); + + return this; + } + + private void retainCompatibility(String[] cmdArgs) throws CliParseException { + if (args.length > 2) { + err.println("'delete path [version]' has been deprecated. " + + "Please use 'delete [-v version] path' instead."); + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + } + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + int version; + if (cl.hasOption("v")) { + version = Integer.parseInt(cl.getOptionValue("v")); + } else { + version = -1; + } + + try { + zk.delete(path, version); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetAclCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetAclCommand.java new file mode 100644 index 0000000..5533950 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetAclCommand.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.cli; + +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; + +/** + * getAcl command for cli + */ +public class GetAclCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("s", false, "stats"); + } + + public GetAclCommand() { + super("getAcl", "[-s] path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + Stat stat = new Stat(); + List acl; + try { + acl = zk.getACL(path, stat); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + + for (ACL a : acl) { + out.println(a.getId() + ": " + ZKUtil.getPermString(a.getPerms())); + } + + if (cl.hasOption("s")) { + new StatPrinter(out).print(stat); + } + return false; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetAllChildrenNumberCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetAllChildrenNumberCommand.java new file mode 100644 index 0000000..dda48d1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetAllChildrenNumberCommand.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; + +/** + * getAllChildrenNumber command for CLI + */ +public class GetAllChildrenNumberCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + + public GetAllChildrenNumberCommand() { + super("getAllChildrenNumber", "path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + CommandLine cl; + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + + return this; + } + + @Override + public boolean exec() throws CliException { + if (args.length < 2) { + throw new MalformedCommandException(getUsageStr()); + } + + try { + String path = args[1]; + int allChildrenNumber = zk.getAllChildrenNumber(path); + + out.println(allChildrenNumber); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetCommand.java new file mode 100644 index 0000000..0558f09 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetCommand.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Stat; + +/** + * get command for cli + */ +public class GetCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("s", false, "stats"); + options.addOption("w", false, "watch"); + } + + public GetCommand() { + super("get", "[-s] [-w] path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + retainCompatibility(cmdArgs); + + return this; + } + + private void retainCompatibility(String[] cmdArgs) throws CliParseException { + // get path [watch] + if (args.length > 2) { + // rewrite to option + cmdArgs[2] = "-w"; + err.println("'get path [watch]' has been deprecated. " + "Please use 'get [-s] [-w] path' instead."); + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + } + } + + @Override + public boolean exec() throws CliException { + boolean watch = cl.hasOption("w"); + String path = args[1]; + Stat stat = new Stat(); + byte[] data; + try { + data = zk.getData(path, watch, stat); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + data = (data == null) ? "null".getBytes() : data; + out.println(new String(data)); + if (cl.hasOption("s")) { + new StatPrinter(out).print(stat); + } + return watch; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetConfigCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetConfigCommand.java new file mode 100644 index 0000000..4fc681f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetConfigCommand.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.util.ConfigUtils; + +/** + * get command for cli + */ +public class GetConfigCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("s", false, "stats"); + options.addOption("w", false, "watch"); + options.addOption("c", false, "client connection string"); + } + + public GetConfigCommand() { + super("config", "[-c] [-w] [-s]"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 1) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + boolean watch = cl.hasOption("w"); + Stat stat = new Stat(); + byte[] data; + try { + data = zk.getConfig(watch, stat); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + data = (data == null) ? "null".getBytes() : data; + if (cl.hasOption("c")) { + out.println(ConfigUtils.getClientConfigStr(new String(data))); + } else { + out.println(new String(data)); + } + + if (cl.hasOption("s")) { + new StatPrinter(out).print(stat); + } + + return watch; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetEphemeralsCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetEphemeralsCommand.java new file mode 100644 index 0000000..8d39a59 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/GetEphemeralsCommand.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.cli; + +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; + +/** + * getEphemerals command for CLI + */ +public class GetEphemeralsCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + + public GetEphemeralsCommand() { + super("getEphemerals", "path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + CommandLine cl; + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + + return this; + } + + @Override + public boolean exec() throws CliException { + String path; + List ephemerals; + try { + if (args.length < 2) { + // gets all the ephemeral nodes for the session + ephemerals = zk.getEphemerals(); + } else { + path = args[1]; + ephemerals = zk.getEphemerals(path); + } + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + out.println(ephemerals); + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/ListQuotaCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/ListQuotaCommand.java new file mode 100644 index 0000000..b6d9f51 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/ListQuotaCommand.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.data.Stat; + +/** + * listQuota command for cli + */ +public class ListQuotaCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + + public ListQuotaCommand() { + super("listquota", "path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + CommandLine cl; + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; + try { + err.println("absolute path is " + absolutePath); + Stat stat = new Stat(); + byte[] data = zk.getData(absolutePath, false, stat); + StatsTrack st = new StatsTrack(new String(data)); + out.println("Output quota for " + path + " " + st.toString()); + + data = zk.getData(Quotas.quotaZookeeper + path + "/" + Quotas.statNode, false, stat); + out.println("Output stat for " + path + " " + new StatsTrack(new String(data)).toString()); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException.NoNodeException ne) { + err.println("quota for " + path + " does not exist."); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/LsCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/LsCommand.java new file mode 100644 index 0000000..770a4a9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/LsCommand.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.cli; + +import java.util.Collections; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.data.Stat; + +/** + * ls command for cli + */ +public class LsCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("?", false, "help"); + options.addOption("s", false, "stat"); + options.addOption("w", false, "watch"); + options.addOption("R", false, "recurse"); + } + + public LsCommand() { + super("ls", "[-s] [-w] [-R] path"); + } + + private void printHelp() { + HelpFormatter formatter = new HelpFormatter(); + formatter.printHelp("ls [options] path", options); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + + args = cl.getArgs(); + if (cl.hasOption("?")) { + printHelp(); + } + + retainCompatibility(cmdArgs); + + return this; + } + + private void retainCompatibility(String[] cmdArgs) throws CliParseException { + // get path [watch] + if (args.length > 2) { + // rewrite to option + cmdArgs[2] = "-w"; + err.println("'ls path [watch]' has been deprecated. " + "Please use 'ls [-w] path' instead."); + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + } + } + + @Override + public boolean exec() throws CliException { + if (args.length < 2) { + throw new MalformedCommandException(getUsageStr()); + } + + String path = args[1]; + boolean watch = cl.hasOption("w"); + boolean withStat = cl.hasOption("s"); + boolean recursive = cl.hasOption("R"); + try { + if (recursive) { + ZKUtil.visitSubTreeDFS(zk, path, watch, new StringCallback() { + @Override + public void processResult(int rc, String path, Object ctx, String name) { + out.println(path); + } + }); + } else { + Stat stat = withStat ? new Stat() : null; + List children = zk.getChildren(path, watch, stat); + printChildren(children, stat); + } + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return watch; + } + + private void printChildren(List children, Stat stat) { + Collections.sort(children); + out.append("["); + boolean first = true; + for (String child : children) { + if (!first) { + out.append(", "); + } else { + first = false; + } + out.append(child); + } + out.append("]\n"); + if (stat != null) { + new StatPrinter(out).print(stat); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/MalformedCommandException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/MalformedCommandException.java new file mode 100644 index 0000000..4d6bc16 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/MalformedCommandException.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +@SuppressWarnings("serial") +public class MalformedCommandException extends CliException { + + public MalformedCommandException(String message) { + super(message); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/MalformedPathException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/MalformedPathException.java new file mode 100644 index 0000000..4deeba7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/MalformedPathException.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +@SuppressWarnings("serial") +public class MalformedPathException extends CliException { + + public MalformedPathException(String message) { + super(message); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/ReconfigCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/ReconfigCommand.java new file mode 100644 index 0000000..d48d16e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/ReconfigCommand.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.io.FileInputStream; +import java.util.Properties; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; + +/** + * reconfig command for cli + */ +public class ReconfigCommand extends CliCommand { + + private static Options options = new Options(); + + /* joining - comma separated list of server config strings for servers to be added to the ensemble. + * Each entry is identical in syntax as it would appear in a configuration file. Only used for + * incremental reconfigurations. + */ + private String joining; + + /* leaving - comma separated list of server IDs to be removed from the ensemble. Only used for + * incremental reconfigurations. + */ + private String leaving; + + /* members - comma separated list of new membership information (e.g., contents of a membership + * configuration file) - for use only with a non-incremental reconfiguration. This may be specified + * manually via the -members flag or it will automatically be filled in by reading the contents + * of an actual configuration file using the -file flag. + */ + private String members; + + /* version - version of config from which we want to reconfigure - if current config is different + * reconfiguration will fail. Should be committed from the CLI to disable this option. + */ long version = -1; + private CommandLine cl; + + static { + options.addOption("s", false, "stats"); + options.addOption("v", true, "required current config version"); + options.addOption("file", true, "path of config file to parse for membership"); + options.addOption("members", true, "comma-separated list of config strings for " + "non-incremental reconfig"); + options.addOption("add", true, "comma-separated list of config strings for " + "new servers"); + options.addOption("remove", true, "comma-separated list of server IDs to remove"); + } + + public ReconfigCommand() { + super("reconfig", "[-s] " + + "[-v version] " + + "[[-file path] | " + + "[-members serverID=host:port1:port2;port3[,...]*]] | " + + "[-add serverId=host:port1:port2;port3[,...]]* " + + "[-remove serverId[,...]*]"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + joining = null; + leaving = null; + members = null; + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + if (!(cl.hasOption("file") || cl.hasOption("members")) && !cl.hasOption("add") && !cl.hasOption("remove")) { + throw new CliParseException(getUsageStr()); + } + if (cl.hasOption("v")) { + try { + version = Long.parseLong(cl.getOptionValue("v"), 16); + } catch (NumberFormatException e) { + throw new CliParseException("-v must be followed by a long (configuration version)"); + } + } else { + version = -1; + } + + // Simple error checking for conflicting modes + if ((cl.hasOption("file") || cl.hasOption("members")) && (cl.hasOption("add") || cl.hasOption("remove"))) { + throw new CliParseException("Can't use -file or -members together with -add or -remove (mixing incremental" + + " and non-incremental modes is not allowed)"); + } + if (cl.hasOption("file") && cl.hasOption("members")) { + throw new CliParseException("Can't use -file and -members together (conflicting non-incremental modes)"); + } + + // Set the joining/leaving/members values based on the mode we're in + if (cl.hasOption("add")) { + joining = cl.getOptionValue("add").toLowerCase(); + } + if (cl.hasOption("remove")) { + leaving = cl.getOptionValue("remove").toLowerCase(); + } + if (cl.hasOption("members")) { + members = cl.getOptionValue("members").toLowerCase(); + } + if (cl.hasOption("file")) { + try { + Properties dynamicCfg = new Properties(); + try (FileInputStream inConfig = new FileInputStream(cl.getOptionValue("file"))) { + dynamicCfg.load(inConfig); + } + //check that membership makes sense; leader will make these checks again + //don't check for leader election ports since + //client doesn't know what leader election alg is used + members = QuorumPeerConfig.parseDynamicConfig(dynamicCfg, 0, true, false).toString(); + } catch (Exception e) { + throw new CliParseException("Error processing " + cl.getOptionValue("file") + e.getMessage()); + } + } + return this; + } + + @Override + public boolean exec() throws CliException { + try { + Stat stat = new Stat(); + if (!(zk instanceof ZooKeeperAdmin)) { + // This should never happen when executing reconfig command line, + // because it is guaranteed that we have a ZooKeeperAdmin instance ready + // to use in CliCommand stack. + // The only exception would be in test code where clients can directly set + // ZooKeeper object to ZooKeeperMain. + return false; + } + + byte[] curConfig = ((ZooKeeperAdmin) zk).reconfigure(joining, leaving, members, version, stat); + out.println("Committed new configuration:\n" + new String(curConfig)); + + if (cl.hasOption("s")) { + new StatPrinter(out).print(stat); + } + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/RemoveWatchesCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/RemoveWatchesCommand.java new file mode 100644 index 0000000..1603cd4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/RemoveWatchesCommand.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher.WatcherType; + +/** + * Remove watches command for cli + */ +public class RemoveWatchesCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("c", false, "child watcher type"); + options.addOption("d", false, "data watcher type"); + options.addOption("a", false, "any watcher type"); + options.addOption("l", false, "remove locally when there is no server connection"); + } + + public RemoveWatchesCommand() { + super("removewatches", "path [-c|-d|-a] [-l]"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + return this; + } + + @Override + public boolean exec() throws CliWrapperException, MalformedPathException { + String path = args[1]; + WatcherType wtype = WatcherType.Any; + // if no matching option -c or -d or -a is specified, we remove + // the watches of the given node by choosing WatcherType.Any + if (cl.hasOption("c")) { + wtype = WatcherType.Children; + } else if (cl.hasOption("d")) { + wtype = WatcherType.Data; + } else if (cl.hasOption("a")) { + wtype = WatcherType.Any; + } + // whether to remove the watches locally + boolean local = cl.hasOption("l"); + + try { + zk.removeAllWatches(path, wtype, local); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetAclCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetAclCommand.java new file mode 100644 index 0000000..ba94790 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetAclCommand.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; + +/** + * setAcl command for cli. + * Available options are s for printing znode's stats, v for set version of znode(s), R for + * recursive setting. User can combine v and R options together, but not s and R considering the + * number of znodes could be large. + */ +public class SetAclCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("s", false, "stats"); + options.addOption("v", true, "version"); + options.addOption("R", false, "recursive"); + } + + public SetAclCommand() { + super("setAcl", "[-s] [-v version] [-R] path acl"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 3) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + String aclStr = args[2]; + List acl = AclParser.parse(aclStr); + int version; + if (cl.hasOption("v")) { + version = Integer.parseInt(cl.getOptionValue("v")); + } else { + version = -1; + } + try { + if (cl.hasOption("R")) { + ZKUtil.visitSubTreeDFS(zk, path, false, new StringCallback() { + @Override + public void processResult(int rc, String p, Object ctx, String name) { + try { + zk.setACL(p, acl, version); + } catch (KeeperException | InterruptedException e) { + out.print(e.getMessage()); + } + } + }); + } else { + Stat stat = zk.setACL(path, acl, version); + if (cl.hasOption("s")) { + new StatPrinter(out).print(stat); + } + } + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + + return false; + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetCommand.java new file mode 100644 index 0000000..e8a0634 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetCommand.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Stat; + +/** + * set command for cli + */ +public class SetCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("s", false, "stats"); + options.addOption("v", true, "version"); + } + + public SetCommand() { + super("set", "[-s] [-v version] path data"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 3) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + byte[] data = args[2].getBytes(); + int version; + if (cl.hasOption("v")) { + version = Integer.parseInt(cl.getOptionValue("v")); + } else { + version = -1; + } + + try { + Stat stat = zk.setData(path, data, version); + if (cl.hasOption("s")) { + new StatPrinter(out).print(stat); + } + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetQuotaCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetQuotaCommand.java new file mode 100644 index 0000000..613d509 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SetQuotaCommand.java @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.OptionGroup; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * setQuota command for cli + */ +public class SetQuotaCommand extends CliCommand { + + private static final Logger LOG = LoggerFactory.getLogger(SetQuotaCommand.class); + private Options options = new Options(); + private String[] args; + private CommandLine cl; + + public SetQuotaCommand() { + super("setquota", "-n|-b val path"); + + OptionGroup og1 = new OptionGroup(); + og1.addOption(new Option("b", true, "bytes quota")); + og1.addOption(new Option("n", true, "num quota")); + og1.setRequired(true); + options.addOptionGroup(og1); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + // get the args + String path = args[1]; + if (path.startsWith(Quotas.quotaZookeeper)) { + err.println("cannot set a quota under the path: " + Quotas.quotaZookeeper); + return false; + } + + if (cl.hasOption("b")) { + // we are setting the bytes quota + long bytes = Long.parseLong(cl.getOptionValue("b")); + try { + createQuota(zk, path, bytes, -1); + } catch (KeeperException | InterruptedException | IllegalArgumentException ex) { + throw new CliWrapperException(ex); + } + } else if (cl.hasOption("n")) { + // we are setting the num quota + int numNodes = Integer.parseInt(cl.getOptionValue("n")); + try { + createQuota(zk, path, -1L, numNodes); + } catch (KeeperException | InterruptedException | IllegalArgumentException ex) { + throw new CliWrapperException(ex); + } + } else { + throw new MalformedCommandException(getUsageStr()); + } + + return false; + } + + public static boolean createQuota( + ZooKeeper zk, + String path, + long bytes, + int numNodes) throws KeeperException, InterruptedException, IllegalArgumentException, MalformedPathException { + // check if the path exists. We cannot create + // quota for a path that already exists in zookeeper + // for now. + Stat initStat; + try { + initStat = zk.exists(path, false); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } + if (initStat == null) { + throw new IllegalArgumentException(path + " does not exist."); + } + // now check if their is already existing + // parent or child that has quota + + String quotaPath = Quotas.quotaZookeeper; + // check for more than 2 children -- + // if zookeeper_stats and zookeeper_quotas + // are not the children then this path + // is an ancestor of some path that + // already has quota + + //check if the child node has a quota. + checkIfChildQuota(zk, path); + + //check for any parent that has been quota + checkIfParentQuota(zk, path); + + // this is valid node for quota + // start creating all the parents + if (zk.exists(quotaPath, false) == null) { + try { + zk.create(Quotas.procZookeeper, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create(Quotas.quotaZookeeper, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException ne) { + // do nothing + } + } + + // now create the direct children + // and the stat and quota nodes + String[] splits = path.split("/"); + StringBuilder sb = new StringBuilder(); + sb.append(quotaPath); + for (int i = 1; i < splits.length; i++) { + sb.append("/").append(splits[i]); + quotaPath = sb.toString(); + try { + zk.create(quotaPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException ne) { + //do nothing + } + } + String statPath = quotaPath + "/" + Quotas.statNode; + quotaPath = quotaPath + "/" + Quotas.limitNode; + StatsTrack strack = new StatsTrack(null); + strack.setBytes(bytes); + strack.setCount(numNodes); + try { + zk.create(quotaPath, strack.toString().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + StatsTrack stats = new StatsTrack(null); + stats.setBytes(0L); + stats.setCount(0); + zk.create(statPath, stats.toString().getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException ne) { + byte[] data = zk.getData(quotaPath, false, new Stat()); + StatsTrack strackC = new StatsTrack(new String(data)); + if (bytes != -1L) { + strackC.setBytes(bytes); + } + if (numNodes != -1) { + strackC.setCount(numNodes); + } + zk.setData(quotaPath, strackC.toString().getBytes(), -1); + } + return true; + } + + private static void checkIfChildQuota(ZooKeeper zk, String path) throws KeeperException, InterruptedException { + String realPath = Quotas.quotaZookeeper + path; + + try { + ZKUtil.visitSubTreeDFS(zk, realPath, false, new AsyncCallback.StringCallback() { + + @Override + public void processResult(int rc, String quotaPath, Object ctx, String name) { + List children = new ArrayList<>(); + try { + children = zk.getChildren(quotaPath, false); + } catch (KeeperException.NoNodeException ne) { + LOG.debug("child removed during quota check", ne); + return; + } catch (InterruptedException | KeeperException e) { + e.printStackTrace(); + } + + if (children.size() == 0) { + return; + } + for (String child : children) { + if (!quotaPath.equals(Quotas.quotaZookeeper + path) && Quotas.limitNode.equals(child)) { + throw new IllegalArgumentException(path + " has a child " + quotaPath.substring(Quotas.quotaZookeeper.length()) + " which has a quota"); + } + } + } + }); + } catch (KeeperException.NoNodeException ne) { + // this is fine + } + } + + private static void checkIfParentQuota(ZooKeeper zk, String path) throws InterruptedException, KeeperException { + final String[] splits = path.split("/"); + String quotaPath = Quotas.quotaZookeeper; + for (String str : splits) { + if (str.length() == 0) { + // this should only be for the beginning of the path + // i.e. "/..." - split(path)[0] is empty string before first '/' + continue; + } + quotaPath += "/" + str; + List children = null; + try { + children = zk.getChildren(quotaPath, false); + } catch (KeeperException.NoNodeException ne) { + LOG.debug("child removed during quota check", ne); + return; + } + if (children.size() == 0) { + return; + } + for (String child : children) { + if (!quotaPath.equals(Quotas.quotaZookeeper + path) && Quotas.limitNode.equals(child)) { + throw new IllegalArgumentException(path + " has a parent " + quotaPath.substring(Quotas.quotaZookeeper.length()) + " which has a quota"); + } + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/StatCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/StatCommand.java new file mode 100644 index 0000000..7fd962c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/StatCommand.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Stat; + +/** + * stat command for cli + */ +public class StatCommand extends CliCommand { + + private static final Options options = new Options(); + private String[] args; + private CommandLine cl; + + static { + options.addOption("w", false, "watch"); + } + + public StatCommand() { + super("stat", "[-w] path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + retainCompatibility(cmdArgs); + + return this; + } + + private void retainCompatibility(String[] cmdArgs) throws CliParseException { + // stat path [watch] + if (args.length > 2) { + // rewrite to option + cmdArgs[2] = "-w"; + err.println("'stat path [watch]' has been deprecated. " + "Please use 'stat [-w] path' instead."); + Parser parser = new PosixParser(); + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + } + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + boolean watch = cl.hasOption("w"); + Stat stat; + try { + stat = zk.exists(path, watch); + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (KeeperException | InterruptedException ex) { + throw new CliWrapperException(ex); + } + if (stat == null) { + throw new CliWrapperException(new KeeperException.NoNodeException(path)); + } + new StatPrinter(out).print(stat); + return watch; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/StatPrinter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/StatPrinter.java new file mode 100644 index 0000000..98c3906 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/StatPrinter.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.cli; + +import java.io.PrintStream; +import java.util.Date; +import org.apache.zookeeper.data.Stat; + +/** + * utility for printing stat values s + */ +public class StatPrinter { + + protected PrintStream out; + + public StatPrinter(PrintStream out) { + this.out = out; + } + + public void print(Stat stat) { + out.println("cZxid = 0x" + Long.toHexString(stat.getCzxid())); + out.println("ctime = " + new Date(stat.getCtime()).toString()); + out.println("mZxid = 0x" + Long.toHexString(stat.getMzxid())); + out.println("mtime = " + new Date(stat.getMtime()).toString()); + out.println("pZxid = 0x" + Long.toHexString(stat.getPzxid())); + out.println("cversion = " + stat.getCversion()); + out.println("dataVersion = " + stat.getVersion()); + out.println("aclVersion = " + stat.getAversion()); + out.println("ephemeralOwner = 0x" + Long.toHexString(stat.getEphemeralOwner())); + out.println("dataLength = " + stat.getDataLength()); + out.println("numChildren = " + stat.getNumChildren()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java new file mode 100644 index 0000000..1c04575 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/SyncCommand.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.cli; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.AsyncCallback; + +/** + * sync command for cli + */ +public class SyncCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + public static final long SYNC_TIMEOUT = TimeUnit.SECONDS.toMillis(30L); + + public SyncCommand() { + super("sync", "path"); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + CommandLine cl; + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length < 2) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + String path = args[1]; + CompletableFuture cf = new CompletableFuture<>(); + + try { + zk.sync(path, new AsyncCallback.VoidCallback() { + public void processResult(int rc, String path, Object ctx) { + cf.complete(rc); + } + }, null); + + int resultCode = cf.get(SYNC_TIMEOUT, TimeUnit.MILLISECONDS); + if (resultCode == 0) { + out.println("Sync is OK"); + } else { + out.println("Sync has failed. rc=" + resultCode); + } + } catch (IllegalArgumentException ex) { + throw new MalformedPathException(ex.getMessage()); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + throw new CliWrapperException(ie); + } catch (TimeoutException | ExecutionException ex) { + throw new CliWrapperException(ex); + } + + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/cli/VersionCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/VersionCommand.java new file mode 100644 index 0000000..6af21bd --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/cli/VersionCommand.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.cli; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.Parser; +import org.apache.commons.cli.PosixParser; +import org.apache.zookeeper.Version; + +/** + * version command for cli + */ +public class VersionCommand extends CliCommand { + + private static Options options = new Options(); + private String[] args; + + public VersionCommand() { + super("version", ""); + } + + @Override + public CliCommand parse(String[] cmdArgs) throws CliParseException { + Parser parser = new PosixParser(); + CommandLine cl; + try { + cl = parser.parse(options, cmdArgs); + } catch (ParseException ex) { + throw new CliParseException(ex); + } + args = cl.getArgs(); + if (args.length > 1) { + throw new CliParseException(getUsageStr()); + } + + return this; + } + + @Override + public boolean exec() throws CliException { + out.println("ZooKeeper CLI version: " + Version.getFullVersion()); + + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/ConnectStringParser.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/ConnectStringParser.java new file mode 100644 index 0000000..d677b69 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/ConnectStringParser.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import static org.apache.zookeeper.common.StringUtils.split; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.common.NetUtils; +import org.apache.zookeeper.common.PathUtils; + +/** + * A parser for ZooKeeper Client connect strings. + * + * This class is not meant to be seen or used outside of ZooKeeper itself. + * + * The chrootPath member should be replaced by a Path object in issue + * ZOOKEEPER-849. + * + * @see org.apache.zookeeper.ZooKeeper + */ +public final class ConnectStringParser { + + private static final int DEFAULT_PORT = 2181; + + private final String chrootPath; + + private final ArrayList serverAddresses = new ArrayList(); + + /** + * Parse host and port by spliting client connectString + * with support for IPv6 literals + * @throws IllegalArgumentException + * for an invalid chroot path. + */ + public ConnectStringParser(String connectString) { + // parse out chroot, if any + int off = connectString.indexOf('/'); + if (off >= 0) { + String chrootPath = connectString.substring(off); + // ignore "/" chroot spec, same as null + if (chrootPath.length() == 1) { + this.chrootPath = null; + } else { + PathUtils.validatePath(chrootPath); + this.chrootPath = chrootPath; + } + connectString = connectString.substring(0, off); + } else { + this.chrootPath = null; + } + + List hostsList = split(connectString, ","); + for (String host : hostsList) { + int port = DEFAULT_PORT; + String[] hostAndPort = NetUtils.getIPV6HostAndPort(host); + if (hostAndPort.length != 0) { + host = hostAndPort[0]; + if (hostAndPort.length == 2) { + port = Integer.parseInt(hostAndPort[1]); + } + } else { + int pidx = host.lastIndexOf(':'); + if (pidx >= 0) { + // otherwise : is at the end of the string, ignore + if (pidx < host.length() - 1) { + port = Integer.parseInt(host.substring(pidx + 1)); + } + host = host.substring(0, pidx); + } + } + serverAddresses.add(InetSocketAddress.createUnresolved(host, port)); + } + } + + public String getChrootPath() { + return chrootPath; + } + + public ArrayList getServerAddresses() { + return serverAddresses; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java new file mode 100644 index 0000000..9fa042a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/FourLetterWordMain.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketTimeoutException; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.common.X509Util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@InterfaceAudience.Public +public class FourLetterWordMain { + + //in milliseconds, socket should connect/read within this period otherwise SocketTimeoutException + private static final int DEFAULT_SOCKET_TIMEOUT = 5000; + protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordMain.class); + /** + * Send the 4letterword + * @param host the destination host + * @param port the destination port + * @param cmd the 4letterword + * @return server response + * @throws java.io.IOException + * @throws SSLContextException + */ + public static String send4LetterWord(String host, int port, String cmd) throws IOException, SSLContextException { + return send4LetterWord(host, port, cmd, false, DEFAULT_SOCKET_TIMEOUT); + } + + /** + * Send the 4letterword + * @param host the destination host + * @param port the destination port + * @param cmd the 4letterword + * @param secure whether to use SSL + * @return server response + * @throws java.io.IOException + * @throws SSLContextException + */ + public static String send4LetterWord( + String host, + int port, + String cmd, + boolean secure) throws IOException, SSLContextException { + return send4LetterWord(host, port, cmd, secure, DEFAULT_SOCKET_TIMEOUT); + } + + /** + * Send the 4letterword + * @param host the destination host + * @param port the destination port + * @param cmd the 4letterword + * @param secure whether to use SSL + * @param timeout in milliseconds, maximum time to wait while connecting/reading data + * @return server response + * @throws java.io.IOException + * @throws SSLContextException + */ + public static String send4LetterWord( + String host, + int port, + String cmd, + boolean secure, + int timeout) throws IOException, SSLContextException { + LOG.info("connecting to {} {}", host, port); + Socket sock; + InetSocketAddress hostaddress = host != null + ? new InetSocketAddress(host, port) + : new InetSocketAddress(InetAddress.getByName(null), port); + if (secure) { + LOG.info("using secure socket"); + try (X509Util x509Util = new ClientX509Util()) { + SSLContext sslContext = x509Util.getDefaultSSLContext(); + SSLSocketFactory socketFactory = sslContext.getSocketFactory(); + SSLSocket sslSock = (SSLSocket) socketFactory.createSocket(); + sslSock.connect(hostaddress, timeout); + sslSock.startHandshake(); + sock = sslSock; + } + } else { + sock = new Socket(); + sock.connect(hostaddress, timeout); + } + sock.setSoTimeout(timeout); + BufferedReader reader = null; + try { + OutputStream outstream = sock.getOutputStream(); + outstream.write(cmd.getBytes()); + outstream.flush(); + + // this replicates NC - close the output stream before reading + if (!secure) { + // SSL prohibits unilateral half-close + sock.shutdownOutput(); + } + + reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + sb.append(line).append("\n"); + } + return sb.toString(); + } catch (SocketTimeoutException e) { + throw new IOException("Exception while executing four letter word: " + cmd, e); + } finally { + sock.close(); + if (reader != null) { + reader.close(); + } + } + } + + public static void main(String[] args) throws IOException, SSLContextException { + if (args.length == 3) { + System.out.println(send4LetterWord(args[0], Integer.parseInt(args[1]), args[2])); + } else if (args.length == 4) { + System.out.println(send4LetterWord(args[0], Integer.parseInt(args[1]), args[2], Boolean.parseBoolean(args[3]))); + } else { + System.out.println("Usage: FourLetterWordMain "); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/HostProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/HostProvider.java new file mode 100644 index 0000000..73a102f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/HostProvider.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import java.net.InetSocketAddress; +import java.util.Collection; +import org.apache.yetus.audience.InterfaceAudience; + +/** + * A set of hosts a ZooKeeper client should connect to. + * + * Classes implementing this interface must guarantee the following: + * + * * Every call to next() returns an InetSocketAddress. So the iterator never + * ends. + * + * * The size() of a HostProvider may never be zero. + * + * A HostProvider must return resolved InetSocketAddress instances on next() if the next address is resolvable. + * In that case, it's up to the HostProvider, whether it returns the next resolvable address in the list or return + * the next one as UnResolved. + * + * Different HostProvider could be imagined: + * + * * A HostProvider that loads the list of Hosts from an URL or from DNS + * * A HostProvider that re-resolves the InetSocketAddress after a timeout. + * * A HostProvider that prefers nearby hosts. + */ +@InterfaceAudience.Public +public interface HostProvider { + + int size(); + + /** + * The next host to try to connect to. + * + * For a spinDelay of 0 there should be no wait. + * + * @param spinDelay + * Milliseconds to wait if all hosts have been tried once. + */ + InetSocketAddress next(long spinDelay); + + /** + * Notify the HostProvider of a successful connection. + * + * The HostProvider may use this notification to reset it's inner state. + */ + void onConnected(); + + /** + * Update the list of servers. This returns true if changing connections is necessary for load-balancing, false otherwise. + * @param serverAddresses new host list + * @param currentHost the host to which this client is currently connected + * @return true if changing connections is necessary for load-balancing, false otherwise + */ + boolean updateServerList(Collection serverAddresses, InetSocketAddress currentHost); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/StaticHostProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/StaticHostProvider.java new file mode 100644 index 0000000..3f1ce4f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/StaticHostProvider.java @@ -0,0 +1,376 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Most simple HostProvider, resolves on every next() call. + * + * Please be aware that although this class doesn't do any DNS caching, there're multiple levels of caching already + * present across the stack like in JVM, OS level, hardware, etc. The best we could do here is to get the most recent + * address from the underlying system which is considered up-to-date. + * + */ +@InterfaceAudience.Public +public final class StaticHostProvider implements HostProvider { + + public interface Resolver { + + InetAddress[] getAllByName(String name) throws UnknownHostException; + + } + + private static final Logger LOG = LoggerFactory.getLogger(StaticHostProvider.class); + + private List serverAddresses = new ArrayList(5); + + private Random sourceOfRandomness; + private int lastIndex = -1; + + private int currentIndex = -1; + + /** + * The following fields are used to migrate clients during reconfiguration + */ + private boolean reconfigMode = false; + + private final List oldServers = new ArrayList(5); + + private final List newServers = new ArrayList(5); + + private int currentIndexOld = -1; + private int currentIndexNew = -1; + + private float pOld, pNew; + + private Resolver resolver; + + /** + * Constructs a SimpleHostSet. + * + * @param serverAddresses + * possibly unresolved ZooKeeper server addresses + * @throws IllegalArgumentException + * if serverAddresses is empty or resolves to an empty list + */ + public StaticHostProvider(Collection serverAddresses) { + init(serverAddresses, System.currentTimeMillis() ^ this.hashCode(), new Resolver() { + @Override + public InetAddress[] getAllByName(String name) throws UnknownHostException { + return InetAddress.getAllByName(name); + } + }); + } + + /** + * Constructs a SimpleHostSet. + * + * Introduced for testing purposes. getAllByName() is a static method of InetAddress, therefore cannot be easily mocked. + * By abstraction of Resolver interface we can easily inject a mocked implementation in tests. + * + * @param serverAddresses + * possibly unresolved ZooKeeper server addresses + * @param resolver + * custom resolver implementation + */ + public StaticHostProvider(Collection serverAddresses, Resolver resolver) { + init(serverAddresses, System.currentTimeMillis() ^ this.hashCode(), resolver); + } + + /** + * Constructs a SimpleHostSet. This constructor is used from StaticHostProviderTest to produce deterministic test results + * by initializing sourceOfRandomness with the same seed + * + * @param serverAddresses + * possibly unresolved ZooKeeper server addresses + * @param randomnessSeed a seed used to initialize sourceOfRandomnes + * @throws IllegalArgumentException + * if serverAddresses is empty or resolves to an empty list + */ + public StaticHostProvider(Collection serverAddresses, long randomnessSeed) { + init(serverAddresses, randomnessSeed, new Resolver() { + @Override + public InetAddress[] getAllByName(String name) throws UnknownHostException { + return InetAddress.getAllByName(name); + } + }); + } + + private void init(Collection serverAddresses, long randomnessSeed, Resolver resolver) { + this.sourceOfRandomness = new Random(randomnessSeed); + this.resolver = resolver; + if (serverAddresses.isEmpty()) { + throw new IllegalArgumentException("A HostProvider may not be empty!"); + } + this.serverAddresses = shuffle(serverAddresses); + currentIndex = -1; + lastIndex = -1; + } + + private InetSocketAddress resolve(InetSocketAddress address) { + try { + String curHostString = address.getHostString(); + List resolvedAddresses = new ArrayList<>(Arrays.asList(this.resolver.getAllByName(curHostString))); + if (resolvedAddresses.isEmpty()) { + return address; + } + Collections.shuffle(resolvedAddresses); + return new InetSocketAddress(resolvedAddresses.get(0), address.getPort()); + } catch (UnknownHostException e) { + LOG.error("Unable to resolve address: {}", address.toString(), e); + return address; + } + } + + private List shuffle(Collection serverAddresses) { + List tmpList = new ArrayList<>(serverAddresses.size()); + tmpList.addAll(serverAddresses); + Collections.shuffle(tmpList, sourceOfRandomness); + return tmpList; + } + + /** + * Update the list of servers. This returns true if changing connections is necessary for load-balancing, false + * otherwise. Changing connections is necessary if one of the following holds: + * a) the host to which this client is currently connected is not in serverAddresses. + * Otherwise (if currentHost is in the new list serverAddresses): + * b) the number of servers in the cluster is increasing - in this case the load on currentHost should decrease, + * which means that SOME of the clients connected to it will migrate to the new servers. The decision whether + * this client migrates or not (i.e., whether true or false is returned) is probabilistic so that the expected + * number of clients connected to each server is the same. + * + * If true is returned, the function sets pOld and pNew that correspond to the probability to migrate to ones of the + * new servers in serverAddresses or one of the old servers (migrating to one of the old servers is done only + * if our client's currentHost is not in serverAddresses). See nextHostInReconfigMode for the selection logic. + * + * See ZOOKEEPER-1355 + * for the protocol and its evaluation, and StaticHostProviderTest for the tests that illustrate how load balancing + * works with this policy. + * + * @param serverAddresses new host list + * @param currentHost the host to which this client is currently connected + * @return true if changing connections is necessary for load-balancing, false otherwise + */ + @Override + public synchronized boolean updateServerList( + Collection serverAddresses, + InetSocketAddress currentHost) { + List shuffledList = shuffle(serverAddresses); + if (shuffledList.isEmpty()) { + throw new IllegalArgumentException("A HostProvider may not be empty!"); + } + // Check if client's current server is in the new list of servers + boolean myServerInNewConfig = false; + + InetSocketAddress myServer = currentHost; + + // choose "current" server according to the client rebalancing algorithm + if (reconfigMode) { + myServer = next(0); + } + + // if the client is not currently connected to any server + if (myServer == null) { + // reconfigMode = false (next shouldn't return null). + if (lastIndex >= 0) { + // take the last server to which we were connected + myServer = this.serverAddresses.get(lastIndex); + } else { + // take the first server on the list + myServer = this.serverAddresses.get(0); + } + } + + for (InetSocketAddress addr : shuffledList) { + if (addr.getPort() == myServer.getPort() + && ((addr.getAddress() != null + && myServer.getAddress() != null + && addr.getAddress().equals(myServer.getAddress())) + || addr.getHostString().equals(myServer.getHostString()))) { + myServerInNewConfig = true; + break; + } + } + + reconfigMode = true; + + newServers.clear(); + oldServers.clear(); + // Divide the new servers into oldServers that were in the previous list + // and newServers that were not in the previous list + for (InetSocketAddress address : shuffledList) { + if (this.serverAddresses.contains(address)) { + oldServers.add(address); + } else { + newServers.add(address); + } + } + + int numOld = oldServers.size(); + int numNew = newServers.size(); + + // number of servers increased + if (numOld + numNew > this.serverAddresses.size()) { + if (myServerInNewConfig) { + // my server is in new config, but load should be decreased. + // Need to decide if this client + // is moving to one of the new servers + if (sourceOfRandomness.nextFloat() <= (1 - ((float) this.serverAddresses.size()) / (numOld + numNew))) { + pNew = 1; + pOld = 0; + } else { + // do nothing special - stay with the current server + reconfigMode = false; + } + } else { + // my server is not in new config, and load on old servers must + // be decreased, so connect to + // one of the new servers + pNew = 1; + pOld = 0; + } + } else { // number of servers stayed the same or decreased + if (myServerInNewConfig) { + // my server is in new config, and load should be increased, so + // stay with this server and do nothing special + reconfigMode = false; + } else { + pOld = ((float) (numOld * (this.serverAddresses.size() - (numOld + numNew)))) + / ((numOld + numNew) * (this.serverAddresses.size() - numOld)); + pNew = 1 - pOld; + } + } + + if (!reconfigMode) { + currentIndex = shuffledList.indexOf(getServerAtCurrentIndex()); + } else { + currentIndex = -1; + } + this.serverAddresses = shuffledList; + currentIndexOld = -1; + currentIndexNew = -1; + lastIndex = currentIndex; + return reconfigMode; + } + + public synchronized InetSocketAddress getServerAtIndex(int i) { + if (i < 0 || i >= serverAddresses.size()) { + return null; + } + return serverAddresses.get(i); + } + + public synchronized InetSocketAddress getServerAtCurrentIndex() { + return getServerAtIndex(currentIndex); + } + + public synchronized int size() { + return serverAddresses.size(); + } + + /** + * Get the next server to connect to, when in "reconfigMode", which means that + * you've just updated the server list, and now trying to find some server to connect to. + * Once onConnected() is called, reconfigMode is set to false. Similarly, if we tried to connect + * to all servers in new config and failed, reconfigMode is set to false. + * + * While in reconfigMode, we should connect to a server in newServers with probability pNew and to servers in + * oldServers with probability pOld (which is just 1-pNew). If we tried out all servers in either oldServers + * or newServers we continue to try servers from the other set, regardless of pNew or pOld. If we tried all servers + * we give up and go back to the normal round robin mode + * + * When called, this should be protected by synchronized(this) + */ + private InetSocketAddress nextHostInReconfigMode() { + boolean takeNew = (sourceOfRandomness.nextFloat() <= pNew); + + // take one of the new servers if it is possible (there are still such + // servers we didn't try), + // and either the probability tells us to connect to one of the new + // servers or if we already + // tried all the old servers + if (((currentIndexNew + 1) < newServers.size()) && (takeNew || (currentIndexOld + 1) >= oldServers.size())) { + ++currentIndexNew; + return newServers.get(currentIndexNew); + } + + // start taking old servers + if ((currentIndexOld + 1) < oldServers.size()) { + ++currentIndexOld; + return oldServers.get(currentIndexOld); + } + + return null; + } + + public InetSocketAddress next(long spinDelay) { + boolean needToSleep = false; + InetSocketAddress addr; + + synchronized (this) { + if (reconfigMode) { + addr = nextHostInReconfigMode(); + if (addr != null) { + currentIndex = serverAddresses.indexOf(addr); + return resolve(addr); + } + //tried all servers and couldn't connect + reconfigMode = false; + needToSleep = (spinDelay > 0); + } + ++currentIndex; + if (currentIndex == serverAddresses.size()) { + currentIndex = 0; + } + addr = serverAddresses.get(currentIndex); + needToSleep = needToSleep || (currentIndex == lastIndex && spinDelay > 0); + if (lastIndex == -1) { + // We don't want to sleep on the first ever connect attempt. + lastIndex = 0; + } + } + if (needToSleep) { + try { + Thread.sleep(spinDelay); + } catch (InterruptedException e) { + LOG.warn("Unexpected exception", e); + } + } + + return resolve(addr); + } + + public synchronized void onConnected() { + lastIndex = currentIndex; + reconfigMode = false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/ZKClientConfig.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/ZKClientConfig.java new file mode 100644 index 0000000..10c6137 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/ZKClientConfig.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import java.io.File; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +/** + * Handles client specific properties + * @since 3.5.2 + */ +@InterfaceAudience.Public +public class ZKClientConfig extends ZKConfig { + + public static final String ZK_SASL_CLIENT_USERNAME = "zookeeper.sasl.client.username"; + public static final String ZK_SASL_CLIENT_USERNAME_DEFAULT = "zookeeper"; + public static final String ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME = "zookeeper.sasl.client.canonicalize.hostname"; + public static final String ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME_DEFAULT = "true"; + @SuppressWarnings("deprecation") + public static final String LOGIN_CONTEXT_NAME_KEY = ZooKeeperSaslClient.LOGIN_CONTEXT_NAME_KEY; + public static final String LOGIN_CONTEXT_NAME_KEY_DEFAULT = "Client"; + @SuppressWarnings("deprecation") + public static final String ENABLE_CLIENT_SASL_KEY = ZooKeeperSaslClient.ENABLE_CLIENT_SASL_KEY; + @SuppressWarnings("deprecation") + public static final String ENABLE_CLIENT_SASL_DEFAULT = ZooKeeperSaslClient.ENABLE_CLIENT_SASL_DEFAULT; + public static final String ZOOKEEPER_SERVER_REALM = "zookeeper.server.realm"; + /** + * This controls whether automatic watch resetting is enabled. Clients + * automatically reset watches during session reconnect, this option allows + * the client to turn off this behavior by setting the property + * "zookeeper.disableAutoWatchReset" to "true" + */ + public static final String DISABLE_AUTO_WATCH_RESET = "zookeeper.disableAutoWatchReset"; + @SuppressWarnings("deprecation") + public static final String ZOOKEEPER_CLIENT_CNXN_SOCKET = ZooKeeper.ZOOKEEPER_CLIENT_CNXN_SOCKET; + /** + * Setting this to "true" will enable encrypted client-server communication. + */ + @SuppressWarnings("deprecation") + public static final String SECURE_CLIENT = ZooKeeper.SECURE_CLIENT; + public static final int CLIENT_MAX_PACKET_LENGTH_DEFAULT = 0xfffff; /* 1 MB */ + public static final String ZOOKEEPER_REQUEST_TIMEOUT = "zookeeper.request.timeout"; + public static final String ZOOKEEPER_SERVER_PRINCIPAL = "zookeeper.server.principal"; + /** + * Feature is disabled by default. + */ + public static final long ZOOKEEPER_REQUEST_TIMEOUT_DEFAULT = 0; + + public ZKClientConfig() { + super(); + initFromJavaSystemProperties(); + } + + public ZKClientConfig(File configFile) throws ConfigException { + super(configFile); + } + + public ZKClientConfig(String configPath) throws ConfigException { + super(configPath); + } + + /** + * Initialize all the ZooKeeper client properties which are configurable as + * java system property + */ + private void initFromJavaSystemProperties() { + setProperty(ZOOKEEPER_REQUEST_TIMEOUT, System.getProperty(ZOOKEEPER_REQUEST_TIMEOUT)); + setProperty(ZOOKEEPER_SERVER_PRINCIPAL, System.getProperty(ZOOKEEPER_SERVER_PRINCIPAL)); + } + + @Override + protected void handleBackwardCompatibility() { + /** + * backward compatibility for properties which are common to both client + * and server + */ + super.handleBackwardCompatibility(); + + /** + * backward compatibility for client specific properties + */ + setProperty(ZK_SASL_CLIENT_USERNAME, System.getProperty(ZK_SASL_CLIENT_USERNAME)); + setProperty(ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, System.getProperty(ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME)); + setProperty(LOGIN_CONTEXT_NAME_KEY, System.getProperty(LOGIN_CONTEXT_NAME_KEY)); + setProperty(ENABLE_CLIENT_SASL_KEY, System.getProperty(ENABLE_CLIENT_SASL_KEY)); + setProperty(ZOOKEEPER_SERVER_REALM, System.getProperty(ZOOKEEPER_SERVER_REALM)); + setProperty(DISABLE_AUTO_WATCH_RESET, System.getProperty(DISABLE_AUTO_WATCH_RESET)); + setProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET, System.getProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET)); + setProperty(SECURE_CLIENT, System.getProperty(SECURE_CLIENT)); + } + + /** + * Returns true if the SASL client is enabled. By default, the client is + * enabled but can be disabled by setting the system property + * zookeeper.sasl.client to false. See + * ZOOKEEPER-1657 for more information. + * + * @return true if the SASL client is enabled. + */ + public boolean isSaslClientEnabled() { + return Boolean.valueOf(getProperty(ENABLE_CLIENT_SASL_KEY, ENABLE_CLIENT_SASL_DEFAULT)); + } + + /** + * Get the value of the key property as an long. + * If property is not set, the provided defaultValue is + * returned + * + * @param key + * property key. + * @param defaultValue + * default value. + * @throws NumberFormatException + * when the value is invalid + * @return return property value as an long, or + * defaultValue + */ + public long getLong(String key, long defaultValue) { + String value = getProperty(key); + if (value != null) { + return Long.parseLong(value.trim()); + } + return defaultValue; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/client/ZooKeeperSaslClient.java b/zookeeper-server/src/main/java/org/apache/zookeeper/client/ZooKeeperSaslClient.java new file mode 100644 index 0000000..b0598bc --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/client/ZooKeeperSaslClient.java @@ -0,0 +1,464 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import java.io.IOException; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import javax.security.auth.Subject; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginException; +import javax.security.sasl.SaslClient; +import javax.security.sasl.SaslException; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.ClientCnxn; +import org.apache.zookeeper.Login; +import org.apache.zookeeper.SaslClientCallbackHandler; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.GetSASLRequest; +import org.apache.zookeeper.proto.SetSASLResponse; +import org.apache.zookeeper.util.SecurityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class manages SASL authentication for the client. It + * allows ClientCnxn to authenticate using SASL with a ZooKeeper server. + */ +public class ZooKeeperSaslClient { + + /** + * @deprecated Use {@link ZKClientConfig#LOGIN_CONTEXT_NAME_KEY} + * instead. + */ + @Deprecated + public static final String LOGIN_CONTEXT_NAME_KEY = "zookeeper.sasl.clientconfig"; + /** + * @deprecated Use {@link ZKClientConfig#ENABLE_CLIENT_SASL_KEY} + * instead. + */ + @Deprecated + public static final String ENABLE_CLIENT_SASL_KEY = "zookeeper.sasl.client"; + /** + * @deprecated Use {@link ZKClientConfig#ENABLE_CLIENT_SASL_DEFAULT} + * instead. + */ + @Deprecated + public static final String ENABLE_CLIENT_SASL_DEFAULT = "true"; + private volatile boolean initializedLogin = false; + + /** + * Returns true if the SASL client is enabled. By default, the client + * is enabled but can be disabled by setting the system property + * zookeeper.sasl.client to false. See + * ZOOKEEPER-1657 for more information. + * + * @return true if the SASL client is enabled. + * @deprecated Use {@link ZKClientConfig#isSaslClientEnabled} instead + */ + @Deprecated + public static boolean isEnabled() { + return Boolean.parseBoolean(System.getProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, ZKClientConfig.ENABLE_CLIENT_SASL_DEFAULT)); + } + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperSaslClient.class); + private Login login = null; + private SaslClient saslClient; + private boolean isSASLConfigured = true; + private final ZKClientConfig clientConfig; + + private byte[] saslToken = new byte[0]; + + public enum SaslState { + INITIAL, + INTERMEDIATE, + COMPLETE, + FAILED + } + + private SaslState saslState = SaslState.INITIAL; + + private boolean gotLastPacket = false; + /** informational message indicating the current configuration status */ + private final String configStatus; + + public SaslState getSaslState() { + return saslState; + } + + public String getLoginContext() { + if (login != null) { + return login.getLoginContextName(); + } + return null; + } + + public ZooKeeperSaslClient(final String serverPrincipal, ZKClientConfig clientConfig) throws LoginException { + /** + * ZOOKEEPER-1373: allow system property to specify the JAAS + * configuration section that the zookeeper client should use. + * Default to "Client". + */ + String clientSection = clientConfig.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT); + this.clientConfig = clientConfig; + // Note that 'Configuration' here refers to javax.security.auth.login.Configuration. + AppConfigurationEntry[] entries = null; + RuntimeException runtimeException = null; + try { + entries = Configuration.getConfiguration().getAppConfigurationEntry(clientSection); + } catch (SecurityException e) { + // handle below: might be harmless if the user doesn't intend to use JAAS authentication. + runtimeException = e; + } catch (IllegalArgumentException e) { + // third party customized getAppConfigurationEntry could throw IllegalArgumentException when JAAS + // configuration isn't set. We can reevaluate whether to catch RuntimeException instead when more + // different types of RuntimeException found + runtimeException = e; + } + if (entries != null) { + this.configStatus = "Will attempt to SASL-authenticate using Login Context section '" + clientSection + "'"; + this.saslClient = createSaslClient(serverPrincipal, clientSection); + } else { + // Handle situation of clientSection's being null: it might simply because the client does not intend to + // use SASL, so not necessarily an error. + saslState = SaslState.FAILED; + String explicitClientSection = clientConfig.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY); + if (explicitClientSection != null) { + // If the user explicitly overrides the default Login Context, they probably expected SASL to + // succeed. But if we got here, SASL failed. + if (runtimeException != null) { + throw new LoginException("Zookeeper client cannot authenticate using the " + + explicitClientSection + + " section of the supplied JAAS configuration: '" + + clientConfig.getJaasConfKey() + + "' because of a " + + "RuntimeException: " + + runtimeException); + } else { + throw new LoginException("Client cannot SASL-authenticate because the specified JAAS configuration " + + "section '" + + explicitClientSection + + "' could not be found."); + } + } else { + // The user did not override the default context. It might be that they just don't intend to use SASL, + // so log at INFO, not WARN, since they don't expect any SASL-related information. + String msg = "Will not attempt to authenticate using SASL "; + if (runtimeException != null) { + msg += "(" + runtimeException + ")"; + } else { + msg += "(unknown error)"; + } + this.configStatus = msg; + this.isSASLConfigured = false; + } + if (clientConfig.getJaasConfKey() != null) { + // Again, the user explicitly set something SASL-related, so + // they probably expected SASL to succeed. + if (runtimeException != null) { + throw new LoginException("Zookeeper client cannot authenticate using the '" + + clientConfig.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT) + + "' section of the supplied JAAS configuration: '" + + clientConfig.getJaasConfKey() + + "' because of a " + + "RuntimeException: " + + runtimeException); + } else { + throw new LoginException("No JAAS configuration section named '" + + clientConfig.getProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT) + + "' was found in specified JAAS configuration file: '" + + clientConfig.getJaasConfKey() + + "'."); + } + } + } + } + + /** + * @return informational message indicating the current configuration status. + */ + public String getConfigStatus() { + return configStatus; + } + + public boolean isComplete() { + return (saslState == SaslState.COMPLETE); + } + + public boolean isFailed() { + return (saslState == SaslState.FAILED); + } + + public static class ServerSaslResponseCallback implements AsyncCallback.DataCallback { + + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + // processResult() is used by ClientCnxn's sendThread to respond to + // data[] contains the Zookeeper Server's SASL token. + // ctx is the ZooKeeperSaslClient object. We use this object's respondToServer() method + // to reply to the Zookeeper Server's SASL token + ZooKeeperSaslClient client = ((ClientCnxn) ctx).zooKeeperSaslClient; + if (client == null) { + LOG.warn("sasl client was unexpectedly null: cannot respond to Zookeeper server."); + return; + } + byte[] usedata = data; + if (data != null) { + LOG.debug("ServerSaslResponseCallback(): saslToken server response: (length={})", usedata.length); + } else { + usedata = new byte[0]; + LOG.debug("ServerSaslResponseCallback(): using empty data[] as server response (length={})", usedata.length); + } + client.respondToServer(usedata, (ClientCnxn) ctx); + } + + } + + private SaslClient createSaslClient( + final String servicePrincipal, + final String loginContext) throws LoginException { + try { + if (!initializedLogin) { + synchronized (this) { + if (login == null) { + LOG.debug("JAAS loginContext is: {}", loginContext); + // note that the login object is static: it's shared amongst all zookeeper-related connections. + // in order to ensure the login is initialized only once, it must be synchronized the code snippet. + login = new Login(loginContext, new SaslClientCallbackHandler(null, "Client"), clientConfig); + login.startThreadIfNeeded(); + initializedLogin = true; + } + } + } + return SecurityUtils.createSaslClient(login.getSubject(), servicePrincipal, "zookeeper", "zk-sasl-md5", LOG, "Client"); + } catch (LoginException e) { + // We throw LoginExceptions... + throw e; + } catch (Exception e) { + // ..but consume (with a log message) all other types of exceptions. + LOG.error("Exception while trying to create SASL client.", e); + return null; + } + } + + public void respondToServer(byte[] serverToken, ClientCnxn cnxn) { + if (saslClient == null) { + LOG.error("saslClient is unexpectedly null. Cannot respond to server's SASL message; ignoring."); + return; + } + + if (!(saslClient.isComplete())) { + try { + saslToken = createSaslToken(serverToken); + if (saslToken != null) { + sendSaslPacket(saslToken, cnxn); + } + } catch (SaslException e) { + LOG.error( + "SASL authentication failed using login context '{}'.", + this.getLoginContext(), + e); + saslState = SaslState.FAILED; + gotLastPacket = true; + } + } + + if (saslClient.isComplete()) { + // GSSAPI: server sends a final packet after authentication succeeds + // or fails. + if ((serverToken == null) && (saslClient.getMechanismName().equals("GSSAPI"))) { + gotLastPacket = true; + } + // non-GSSAPI: no final packet from server. + if (!saslClient.getMechanismName().equals("GSSAPI")) { + gotLastPacket = true; + } + // SASL authentication is completed, successfully or not: + // enable the socket's writable flag so that any packets waiting for authentication to complete in + // the outgoing queue will be sent to the Zookeeper server. + cnxn.saslCompleted(); + } + } + + private byte[] createSaslToken() throws SaslException { + saslState = SaslState.INTERMEDIATE; + return createSaslToken(saslToken); + } + + private byte[] createSaslToken(final byte[] saslToken) throws SaslException { + if (saslToken == null) { + // TODO: introspect about runtime environment (such as jaas.conf) + saslState = SaslState.FAILED; + throw new SaslException("Error in authenticating with a Zookeeper Quorum member: the quorum member's saslToken is null."); + } + + Subject subject = login.getSubject(); + if (subject != null) { + synchronized (login) { + try { + final byte[] retval = Subject.doAs(subject, new PrivilegedExceptionAction() { + public byte[] run() throws SaslException { + LOG.debug("saslClient.evaluateChallenge(len={})", saslToken.length); + return saslClient.evaluateChallenge(saslToken); + } + }); + return retval; + } catch (PrivilegedActionException e) { + String error = "An error: (" + e + ") occurred when evaluating Zookeeper Quorum Member's " + + " received SASL token."; + // Try to provide hints to use about what went wrong so they can fix their configuration. + // TODO: introspect about e: look for GSS information. + final String UNKNOWN_SERVER_ERROR_TEXT = "(Mechanism level: Server not found in Kerberos database (7) - UNKNOWN_SERVER)"; + if (e.toString().contains(UNKNOWN_SERVER_ERROR_TEXT)) { + error += " This may be caused by Java's being unable to resolve the Zookeeper Quorum Member's" + + " hostname correctly. You may want to try to adding" + + " '-Dsun.net.spi.nameservice.provider.1=dns,sun' to your client's JVMFLAGS environment."; + } + error += " Zookeeper Client will go to AUTH_FAILED state."; + LOG.error(error); + saslState = SaslState.FAILED; + throw new SaslException(error, e); + } + } + } else { + throw new SaslException("Cannot make SASL token without subject defined. " + + "For diagnosis, please look for WARNs and ERRORs in your log related to the Login class."); + } + } + + private void sendSaslPacket(byte[] saslToken, ClientCnxn cnxn) throws SaslException { + LOG.debug("ClientCnxn:sendSaslPacket:length={}", saslToken.length); + + GetSASLRequest request = new GetSASLRequest(); + request.setToken(saslToken); + SetSASLResponse response = new SetSASLResponse(); + ServerSaslResponseCallback cb = new ServerSaslResponseCallback(); + + try { + cnxn.sendPacket(request, response, cb, ZooDefs.OpCode.sasl); + } catch (IOException e) { + throw new SaslException("Failed to send SASL packet to server.", e); + } + } + + private void sendSaslPacket(ClientCnxn cnxn) throws SaslException { + LOG.debug("ClientCnxn:sendSaslPacket:length={}", saslToken.length); + + GetSASLRequest request = new GetSASLRequest(); + request.setToken(createSaslToken()); + SetSASLResponse response = new SetSASLResponse(); + ServerSaslResponseCallback cb = new ServerSaslResponseCallback(); + try { + cnxn.sendPacket(request, response, cb, ZooDefs.OpCode.sasl); + } catch (IOException e) { + throw new SaslException("Failed to send SASL packet to server due " + "to IOException:", e); + } + } + + // used by ClientCnxn to know whether to emit a SASL-related event: either AuthFailed or SaslAuthenticated, + // or none, if not ready yet. Sets saslState to COMPLETE as a side-effect. + public KeeperState getKeeperState() { + if (saslClient != null) { + if (saslState == SaslState.FAILED) { + return KeeperState.AuthFailed; + } + if (saslClient.isComplete()) { + if (saslState == SaslState.INTERMEDIATE) { + saslState = SaslState.COMPLETE; + return KeeperState.SaslAuthenticated; + } + } + } + // No event ready to emit yet. + return null; + } + + // Initialize the client's communications with the Zookeeper server by sending the server the first + // authentication packet. + public void initialize(ClientCnxn cnxn) throws SaslException { + if (saslClient == null) { + saslState = SaslState.FAILED; + throw new SaslException("saslClient failed to initialize properly: it's null."); + } + if (saslState == SaslState.INITIAL) { + if (saslClient.hasInitialResponse()) { + sendSaslPacket(cnxn); + } else { + byte[] emptyToken = new byte[0]; + sendSaslPacket(emptyToken, cnxn); + } + saslState = SaslState.INTERMEDIATE; + } + } + + public boolean clientTunneledAuthenticationInProgress() { + if (!isSASLConfigured) { + return false; + } + // TODO: Rather than checking a disjunction here, should be a single member + // variable or method in this class to determine whether the client is + // configured to use SASL. (see also ZOOKEEPER-1455). + try { + if ((clientConfig.getJaasConfKey() != null) + || ((Configuration.getConfiguration() != null) + && (Configuration.getConfiguration().getAppConfigurationEntry( + clientConfig.getProperty( + ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, + ZKClientConfig.LOGIN_CONTEXT_NAME_KEY_DEFAULT)) != null))) { + // Client is configured to use a valid login Configuration, so + // authentication is either in progress, successful, or failed. + + // 1. Authentication hasn't finished yet: we must wait for it to do so. + if (!isComplete() && !isFailed()) { + return true; + } + + // 2. SASL authentication has succeeded or failed.. + //noinspection RedundantIfStatement + if (!gotLastPacket) { + // ..but still in progress, because there is a final SASL + // message from server which must be received. + return true; + } + } + // Either client is not configured to use a tunnelled authentication + // scheme, or tunnelled authentication has completed (successfully or + // not), and all server SASL messages have been received. + return false; + } catch (SecurityException e) { + // Thrown if the caller does not have permission to retrieve the Configuration. + // In this case, simply returning false is correct. + LOG.debug("Could not retrieve login configuration", e); + + return false; + } + } + + /** + * close login thread if running + */ + public void shutdown() { + if (null != login) { + login.shutdown(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java new file mode 100644 index 0000000..d6b7cf6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileOutputStream.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FilterOutputStream; +import java.io.IOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * This code is originally from HDFS, see the similarly named files there + * in case of bug fixing, history, etc... + */ + +/** + * A FileOutputStream that has the property that it will only show up at its + * destination once it has been entirely written and flushed to disk. While + * being written, it will use a .tmp suffix. + * + * When the output stream is closed, it is flushed, fsynced, and will be moved + * into place, overwriting any file that already exists at that location. + * + * NOTE: on Windows platforms, it will not atomically replace the target + * file - instead the target file is deleted before this one is moved into + * place. + */ +public class AtomicFileOutputStream extends FilterOutputStream { + + public static final String TMP_EXTENSION = ".tmp"; + + private static final Logger LOG = LoggerFactory.getLogger(AtomicFileOutputStream.class); + + private final File origFile; + private final File tmpFile; + + public AtomicFileOutputStream(File f) throws FileNotFoundException { + // Code unfortunately must be duplicated below since we can't assign + // anything + // before calling super + super(new FileOutputStream(new File(f.getParentFile(), f.getName() + TMP_EXTENSION))); + origFile = f.getAbsoluteFile(); + tmpFile = new File(f.getParentFile(), f.getName() + TMP_EXTENSION).getAbsoluteFile(); + } + + /** + * The default write method in FilterOutputStream does not call the write + * method of its underlying input stream with the same arguments. Instead + * it writes the data byte by byte, override it here to make it more + * efficient. + */ + @Override + public void write(byte[] b, int off, int len) throws IOException { + out.write(b, off, len); + } + + @Override + public void close() throws IOException { + boolean triedToClose = false, success = false; + try { + flush(); + ((FileOutputStream) out).getFD().sync(); + + triedToClose = true; + super.close(); + success = true; + } finally { + if (success) { + boolean renamed = tmpFile.renameTo(origFile); + if (!renamed) { + // On windows, renameTo does not replace. + if (!origFile.delete() || !tmpFile.renameTo(origFile)) { + throw new IOException("Could not rename temporary file " + tmpFile + " to " + origFile); + } + } + } else { + if (!triedToClose) { + // If we failed when flushing, try to close it to not leak + // an FD + IOUtils.closeStream(out); + } + // close wasn't successful, try to delete the tmp file + if (!tmpFile.delete()) { + LOG.warn("Unable to delete tmp file {}", tmpFile); + } + } + } + } + + /** + * Close the atomic file, but do not "commit" the temporary file on top of + * the destination. This should be used if there is a failure in writing. + */ + public void abort() { + try { + super.close(); + } catch (IOException ioe) { + LOG.warn("Unable to abort file {}", tmpFile, ioe); + } + + if (!tmpFile.delete()) { + LOG.warn("Unable to delete tmp file during abort {}", tmpFile); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java new file mode 100644 index 0000000..6e20b78 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/AtomicFileWritingIdiom.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; + +/* + * Used to perform an atomic write into a file. + * If there is a failure in the middle of the writing operation, + * the original file (if it exists) is left intact. + * Based on the org.apache.zookeeper.server.quorum.QuorumPeer.writeLongToFile(...) idiom + * using the HDFS AtomicFileOutputStream class. + */ +public class AtomicFileWritingIdiom { + + public interface OutputStreamStatement { + + void write(OutputStream os) throws IOException; + + } + + public interface WriterStatement { + + void write(Writer os) throws IOException; + + } + + public AtomicFileWritingIdiom(File targetFile, OutputStreamStatement osStmt) throws IOException { + this(targetFile, osStmt, null); + } + + public AtomicFileWritingIdiom(File targetFile, WriterStatement wStmt) throws IOException { + this(targetFile, null, wStmt); + } + + private AtomicFileWritingIdiom( + File targetFile, + OutputStreamStatement osStmt, + WriterStatement wStmt) throws IOException { + AtomicFileOutputStream out = null; + boolean error = true; + try { + out = new AtomicFileOutputStream(targetFile); + if (wStmt == null) { + // execute output stream operation + osStmt.write(out); + } else { + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out)); + // execute writer operation and flush + wStmt.write(bw); + bw.flush(); + } + out.flush(); + // everything went ok + error = false; + } finally { + // nothing interesting to do if out == null + if (out != null) { + if (error) { + // worst case here the tmp file/resources(fd) are not cleaned up + // and the caller will be notified (IOException) + out.abort(); + } else { + // if the close operation (rename) fails we'll get notified. + // worst case the tmp file may still exist + IOUtils.closeStream(out); + } + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/BCFKSFileLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/BCFKSFileLoader.java new file mode 100644 index 0000000..d59f40a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/BCFKSFileLoader.java @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + + +/** + * Implementation of {@link FileKeyStoreLoader} that loads from BCKFS files. + */ +class BCFKSFileLoader extends StandardTypeFileKeyStoreLoader { + private BCFKSFileLoader(String keyStorePath, + String trustStorePath, + String keyStorePassword, + String trustStorePassword) { + super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword, SupportedStandardKeyFormat.BCFKS); + } + + static class Builder extends FileKeyStoreLoader.Builder { + @Override + BCFKSFileLoader build() { + return new BCFKSFileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/ClientX509Util.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ClientX509Util.java new file mode 100644 index 0000000..34be0be --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ClientX509Util.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +public class ClientX509Util extends X509Util { + + private final String sslAuthProviderProperty = getConfigPrefix() + "authProvider"; + + @Override + protected String getConfigPrefix() { + return "zookeeper.ssl."; + } + + @Override + protected boolean shouldVerifyClientHostname() { + return false; + } + + public String getSslAuthProviderProperty() { + return sslAuthProviderProperty; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileChangeWatcher.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileChangeWatcher.java new file mode 100644 index 0000000..274e212 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileChangeWatcher.java @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.IOException; +import java.nio.file.ClosedWatchServiceException; +import java.nio.file.FileSystem; +import java.nio.file.Path; +import java.nio.file.StandardWatchEventKinds; +import java.nio.file.WatchEvent; +import java.nio.file.WatchKey; +import java.nio.file.WatchService; +import java.util.function.Consumer; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Instances of this class can be used to watch a directory for file changes. When a file is added to, deleted from, + * or is modified in the given directory, the callback provided by the user will be called from a background thread. + * Some things to keep in mind: + *
        + *
      • The callback should be thread-safe.
      • + *
      • Changes that happen around the time the thread is started may be missed.
      • + *
      • There is a delay between a file changing and the callback firing.
      • + *
      • The watch is not recursive - changes to subdirectories will not trigger a callback.
      • + *
      + */ +public final class FileChangeWatcher { + + private static final Logger LOG = LoggerFactory.getLogger(FileChangeWatcher.class); + + public enum State { + NEW, // object created but start() not called yet + STARTING, // start() called but background thread has not entered main loop + RUNNING, // background thread is running + STOPPING, // stop() called but background thread has not exited main loop + STOPPED // stop() called and background thread has exited, or background thread crashed + } + + private final WatcherThread watcherThread; + private State state; // protected by synchronized(this) + + /** + * Creates a watcher that watches dirPath and invokes callback on changes. + * + * @param dirPath the directory to watch. + * @param callback the callback to invoke with events. event.kind() will return the type of event, + * and event.context() will return the filename relative to dirPath. + * @throws IOException if there is an error creating the WatchService. + */ + public FileChangeWatcher(Path dirPath, Consumer> callback) throws IOException { + FileSystem fs = dirPath.getFileSystem(); + WatchService watchService = fs.newWatchService(); + + LOG.debug("Registering with watch service: {}", dirPath); + + dirPath.register(watchService, new WatchEvent.Kind[]{StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.OVERFLOW}); + state = State.NEW; + this.watcherThread = new WatcherThread(watchService, callback); + this.watcherThread.setDaemon(true); + } + + /** + * Returns the current {@link FileChangeWatcher.State}. + * @return the current state. + */ + public synchronized State getState() { + return state; + } + + /** + * Blocks until the current state becomes desiredState. + * Currently only used by tests, thus package-private. + * @param desiredState the desired state. + * @throws InterruptedException if the current thread gets interrupted. + */ + synchronized void waitForState(State desiredState) throws InterruptedException { + while (this.state != desiredState) { + this.wait(); + } + } + + /** + * Sets the state to newState. + * @param newState the new state. + */ + private synchronized void setState(State newState) { + state = newState; + this.notifyAll(); + } + + /** + * Atomically sets the state to update if and only if the + * state is currently expected. + * @param expected the expected state. + * @param update the new state. + * @return true if the update succeeds, or false if the current state + * does not equal expected. + */ + private synchronized boolean compareAndSetState(State expected, State update) { + if (state == expected) { + setState(update); + return true; + } else { + return false; + } + } + + /** + * Atomically sets the state to update if and only if the + * state is currently one of expectedStates. + * @param expectedStates the expected states. + * @param update the new state. + * @return true if the update succeeds, or false if the current state + * does not equal any of the expectedStates. + */ + private synchronized boolean compareAndSetState(State[] expectedStates, State update) { + for (State expected : expectedStates) { + if (state == expected) { + setState(update); + return true; + } + } + return false; + } + + /** + * Tells the background thread to start. Does not wait for it to be running. + * Calling this method more than once has no effect. + */ + public void start() { + if (!compareAndSetState(State.NEW, State.STARTING)) { + // If previous state was not NEW, start() has already been called. + return; + } + this.watcherThread.start(); + } + + /** + * Tells the background thread to stop. Does not wait for it to exit. + */ + public void stop() { + if (compareAndSetState(new State[]{State.RUNNING, State.STARTING}, State.STOPPING)) { + watcherThread.interrupt(); + } + } + + /** + * Inner class that implements the watcher thread logic. + */ + private class WatcherThread extends ZooKeeperThread { + + private static final String THREAD_NAME = "FileChangeWatcher"; + + final WatchService watchService; + final Consumer> callback; + + WatcherThread(WatchService watchService, Consumer> callback) { + super(THREAD_NAME); + this.watchService = watchService; + this.callback = callback; + } + + @Override + public void run() { + try { + LOG.info("{} thread started", getName()); + if (!compareAndSetState(FileChangeWatcher.State.STARTING, FileChangeWatcher.State.RUNNING)) { + // stop() called shortly after start(), before + // this thread started running. + FileChangeWatcher.State state = FileChangeWatcher.this.getState(); + if (state != FileChangeWatcher.State.STOPPING) { + throw new IllegalStateException("Unexpected state: " + state); + } + return; + } + runLoop(); + } catch (Exception e) { + LOG.warn("Error in runLoop()", e); + throw e; + } finally { + try { + watchService.close(); + } catch (IOException e) { + LOG.warn("Error closing watch service", e); + } + LOG.info("{} thread finished", getName()); + FileChangeWatcher.this.setState(FileChangeWatcher.State.STOPPED); + } + } + + private void runLoop() { + while (FileChangeWatcher.this.getState() == FileChangeWatcher.State.RUNNING) { + WatchKey key; + try { + key = watchService.take(); + } catch (InterruptedException | ClosedWatchServiceException e) { + LOG.debug("{} was interrupted and is shutting down...", getName()); + break; + } + for (WatchEvent event : key.pollEvents()) { + LOG.debug("Got file changed event: {} with context: {}", event.kind(), event.context()); + try { + callback.accept(event); + } catch (Throwable e) { + LOG.error("Error from callback", e); + } + } + boolean isKeyValid = key.reset(); + if (!isKeyValid) { + // This is likely a problem, it means that file reloading is broken, probably because the + // directory we are watching was deleted or otherwise became inaccessible (unmounted, permissions + // changed, ???). + // For now, we log an error and exit the watcher thread. + LOG.error("Watch key no longer valid, maybe the directory is inaccessible?"); + break; + } + } + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoader.java new file mode 100644 index 0000000..3e8537c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoader.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.util.Objects; + +/** + * Base class for instances of {@link KeyStoreLoader} which load the key/trust + * stores from files on a filesystem. + */ +abstract class FileKeyStoreLoader implements KeyStoreLoader { + + final String keyStorePath; + final String trustStorePath; + final String keyStorePassword; + final String trustStorePassword; + + FileKeyStoreLoader(String keyStorePath, String trustStorePath, String keyStorePassword, String trustStorePassword) { + this.keyStorePath = keyStorePath; + this.trustStorePath = trustStorePath; + this.keyStorePassword = keyStorePassword; + this.trustStorePassword = trustStorePassword; + } + + /** + * Base class for builder pattern used by subclasses. + * @param the subtype of FileKeyStoreLoader created by the Builder. + */ + abstract static class Builder { + + String keyStorePath; + String trustStorePath; + String keyStorePassword; + String trustStorePassword; + + Builder() { + } + + Builder setKeyStorePath(String keyStorePath) { + this.keyStorePath = Objects.requireNonNull(keyStorePath); + return this; + } + + Builder setTrustStorePath(String trustStorePath) { + this.trustStorePath = Objects.requireNonNull(trustStorePath); + return this; + } + + Builder setKeyStorePassword(String keyStorePassword) { + this.keyStorePassword = Objects.requireNonNull(keyStorePassword); + return this; + } + + Builder setTrustStorePassword(String trustStorePassword) { + this.trustStorePassword = Objects.requireNonNull(trustStorePassword); + return this; + } + + abstract T build(); + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProvider.java new file mode 100644 index 0000000..f4798af --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProvider.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.util.Objects; + +public class FileKeyStoreLoaderBuilderProvider { + + /** + * Returns a {@link FileKeyStoreLoader.Builder} that can build a loader + * which loads keys and certs from files of the given + * {@link KeyStoreFileType}. + * + * @param type the file type to load keys/certs from. + * @return a new Builder. + */ + static FileKeyStoreLoader.Builder getBuilderForKeyStoreFileType(KeyStoreFileType type) { + switch (Objects.requireNonNull(type)) { + case JKS: + return new JKSFileLoader.Builder(); + case PEM: + return new PEMFileLoader.Builder(); + case PKCS12: + return new PKCS12FileLoader.Builder(); + case BCFKS: + return new BCFKSFileLoader.Builder(); + default: + throw new AssertionError("Unexpected StoreFileType: " + type.name()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/IOUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/IOUtils.java new file mode 100644 index 0000000..6dc1ee9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/IOUtils.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import org.slf4j.Logger; + +/* + * This code is originally from HDFS, see the similarly named files there + * in case of bug fixing, history, etc... + */ + +public class IOUtils { + + /** + * Closes the stream ignoring {@link IOException}. Must only be called in + * cleaning up from exception handlers. + * + * @param stream + * the Stream to close + */ + public static void closeStream(Closeable stream) { + cleanup(null, stream); + } + + /** + * Close the Closeable objects and ignore any {@link IOException} or + * null pointers. Must only be used for cleanup in exception handlers. + * + * @param log + * the log to record problems to at debug level. Can be null. + * @param closeables + * the objects to close + */ + public static void cleanup(Logger log, Closeable... closeables) { + for (Closeable c : closeables) { + if (c != null) { + try { + c.close(); + } catch (IOException e) { + if (log != null) { + log.warn("Exception in closing " + c, e); + } + } + } + } + } + + /** + * Copies from one stream to another. + * + * @param in + * InputStrem to read from + * @param out + * OutputStream to write to + * @param buffSize + * the size of the buffer + * @param close + * whether or not close the InputStream and OutputStream at the + * end. The streams are closed in the finally clause. + */ + public static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close) throws IOException { + try { + copyBytes(in, out, buffSize); + if (close) { + out.close(); + out = null; + in.close(); + in = null; + } + } finally { + if (close) { + closeStream(out); + closeStream(in); + } + } + } + + /** + * Copies from one stream to another. + * + * @param in + * InputStrem to read from + * @param out + * OutputStream to write to + * @param buffSize + * the size of the buffer + */ + public static void copyBytes(InputStream in, OutputStream out, int buffSize) throws IOException { + PrintStream ps = out instanceof PrintStream ? (PrintStream) out : null; + byte[] buf = new byte[buffSize]; + int bytesRead = in.read(buf); + while (bytesRead >= 0) { + out.write(buf, 0, bytesRead); + if ((ps != null) && ps.checkError()) { + throw new IOException("Unable to write to output stream."); + } + bytesRead = in.read(buf); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/JKSFileLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/JKSFileLoader.java new file mode 100644 index 0000000..d8709d7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/JKSFileLoader.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + + +/** + * Implementation of {@link FileKeyStoreLoader} that loads from JKS files. + */ +class JKSFileLoader extends StandardTypeFileKeyStoreLoader { + + private JKSFileLoader( + String keyStorePath, + String trustStorePath, + String keyStorePassword, + String trustStorePassword) { + super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword, SupportedStandardKeyFormat.JKS); + } + + static class Builder extends FileKeyStoreLoader.Builder { + + @Override + JKSFileLoader build() { + return new JKSFileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreFileType.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreFileType.java new file mode 100644 index 0000000..69283f3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreFileType.java @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +/** + * This enum represents the file type of a KeyStore or TrustStore. + * Currently, JKS (Java keystore), PEM, PKCS12, and BCFKS types are supported. + */ +public enum KeyStoreFileType { + JKS(".jks"), + PEM(".pem"), + PKCS12(".p12"), + BCFKS(".bcfks"); + + private final String defaultFileExtension; + + KeyStoreFileType(String defaultFileExtension) { + this.defaultFileExtension = defaultFileExtension; + } + + /** + * The property string that specifies that a key store or trust store + * should use this store file type. + */ + public String getPropertyValue() { + return this.name(); + } + + /** + * The file extension that is associated with this file type. + */ + public String getDefaultFileExtension() { + return defaultFileExtension; + } + + /** + * Converts a property value to a StoreFileType enum. If the property value + * is null or an empty string, returns null. + * @param propertyValue the property value. + * @return the KeyStoreFileType, or null if + * propertyValue is null or empty. + * @throws IllegalArgumentException if propertyValue is not + * one of "JKS", "PEM", "BCFKS", "PKCS12", or empty/null. + */ + public static KeyStoreFileType fromPropertyValue(String propertyValue) { + if (propertyValue == null || propertyValue.length() == 0) { + return null; + } + return KeyStoreFileType.valueOf(propertyValue.toUpperCase()); + } + + /** + * Detects the type of KeyStore / TrustStore file from the file extension. + * If the file name ends with ".jks", returns StoreFileType.JKS. + * If the file name ends with ".pem", returns StoreFileType.PEM. + * If the file name ends with ".p12", returns StoreFileType.PKCS12. + * If the file name ends with ".bckfs", returns StoreFileType.BCKFS. + * Otherwise, throws an IllegalArgumentException. + * @param filename the filename of the key store or trust store file. + * @return a KeyStoreFileType. + * @throws IllegalArgumentException if the filename does not end with + * ".jks", ".pem", "p12" or "bcfks". + */ + public static KeyStoreFileType fromFilename(String filename) { + int i = filename.lastIndexOf('.'); + if (i >= 0) { + String extension = filename.substring(i); + for (KeyStoreFileType storeFileType : KeyStoreFileType.values()) { + if (storeFileType.getDefaultFileExtension().equals(extension)) { + return storeFileType; + } + } + } + throw new IllegalArgumentException("Unable to auto-detect store file type from file name: " + filename); + } + + /** + * If propertyValue is not null or empty, returns the result + * of KeyStoreFileType.fromPropertyValue(propertyValue). Else, + * returns the result of KeyStoreFileType.fromFileName(filename). + * @param propertyValue property value describing the KeyStoreFileType, or + * null/empty to auto-detect the type from the file + * name. + * @param filename file name of the key store file. The file extension is + * used to auto-detect the KeyStoreFileType when + * propertyValue is null or empty. + * @return a KeyStoreFileType. + * @throws IllegalArgumentException if propertyValue is not + * one of "JKS", "PEM", "PKCS12", "BCFKS", or empty/null. + * @throws IllegalArgumentException if propertyValueis empty + * or null and the type could not be determined from the file name. + */ + public static KeyStoreFileType fromPropertyValueOrFileName(String propertyValue, String filename) { + KeyStoreFileType result = KeyStoreFileType.fromPropertyValue(propertyValue); + if (result == null) { + result = KeyStoreFileType.fromFilename(filename); + } + return result; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreLoader.java new file mode 100644 index 0000000..615172d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreLoader.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyStore; + +/** + * An interface for an object that can load key stores or trust stores. + */ +interface KeyStoreLoader { + + /** + * Loads a KeyStore which contains at least one private key and the + * associated X509 cert chain. + * + * @return a new KeyStore + * @throws IOException if loading the key store fails due to an IO error, + * such as "file not found". + * @throws GeneralSecurityException if loading the key store fails due to + * a security error, such as "unsupported crypto algorithm". + */ + KeyStore loadKeyStore() throws IOException, GeneralSecurityException; + + /** + * Loads a KeyStore which contains at least one X509 cert chain for a + * trusted Certificate Authority (CA). + * + * @return a new KeyStore + * @throws IOException if loading the trust store fails due to an IO error, + * such as "file not found". + * @throws GeneralSecurityException if loading the trust store fails due to + * a security error, such as "unsupported crypto algorithm". + */ + KeyStore loadTrustStore() throws IOException, GeneralSecurityException; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/NetUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/NetUtils.java new file mode 100644 index 0000000..be8cb9a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/NetUtils.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; + +/** + * This class contains common utilities for netstuff. Like printing IPv6 literals correctly + */ +public class NetUtils { + + public static String formatInetAddr(InetSocketAddress addr) { + InetAddress ia = addr.getAddress(); + + if (ia == null) { + return String.format("%s:%s", addr.getHostString(), addr.getPort()); + } + + if (ia instanceof Inet6Address) { + return String.format("[%s]:%s", ia.getHostAddress(), addr.getPort()); + } else { + return String.format("%s:%s", ia.getHostAddress(), addr.getPort()); + } + } + + /** + * Separates host and port from given host port string if host port string is enclosed + * within square bracket. + * + * @param hostPort host port string + * @return String[]{host, port} if host port string is host:port + * or String[] {host, port:port} if host port string is host:port:port + * or String[] {host} if host port string is host + * or String[]{} if not a ipv6 host port string. + */ + public static String[] getIPV6HostAndPort(String hostPort) { + if (hostPort.startsWith("[")) { + int i = hostPort.lastIndexOf(']'); + if (i < 0) { + throw new IllegalArgumentException( + hostPort + " starts with '[' but has no matching ']'"); + } + String host = hostPort.substring(1, i); + if (host.isEmpty()) { + throw new IllegalArgumentException(host + " is empty."); + } + if (hostPort.length() > i + 1) { + return getHostPort(hostPort, i, host); + } + return new String[] { host }; + } else { + //Not an IPV6 host port string + return new String[] {}; + } + } + + private static String[] getHostPort(String hostPort, int indexOfClosingBracket, String host) { + // [127::1]:2181 , check separator : exits + if (hostPort.charAt(indexOfClosingBracket + 1) != ':') { + throw new IllegalArgumentException(hostPort + " does not have : after ]"); + } + // [127::1]: scenario + if (indexOfClosingBracket + 2 == hostPort.length()) { + throw new IllegalArgumentException(hostPort + " doesn't have a port after colon."); + } + //do not include + String port = hostPort.substring(indexOfClosingBracket + 2); + return new String[] { host, port }; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/NettyUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/NettyUtils.java new file mode 100644 index 0000000..dc1fe78 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/NettyUtils.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import io.netty.channel.EventLoopGroup; +import io.netty.channel.epoll.Epoll; +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.ServerSocketChannel; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Helper methods for netty code. + */ +public class NettyUtils { + + private static final Logger LOG = LoggerFactory.getLogger(NettyUtils.class); + + private static final int DEFAULT_INET_ADDRESS_COUNT = 1; + + /** + * If {@link Epoll#isAvailable()} == true, returns a new + * {@link EpollEventLoopGroup}, otherwise returns a new + * {@link NioEventLoopGroup}. Creates the event loop group using the + * default number of threads. + * @return a new {@link EventLoopGroup}. + */ + public static EventLoopGroup newNioOrEpollEventLoopGroup() { + if (Epoll.isAvailable()) { + return new EpollEventLoopGroup(); + } else { + return new NioEventLoopGroup(); + } + } + + /** + * If {@link Epoll#isAvailable()} == true, returns a new + * {@link EpollEventLoopGroup}, otherwise returns a new + * {@link NioEventLoopGroup}. Creates the event loop group using the + * specified number of threads instead of the default. + * @param nThreads see {@link NioEventLoopGroup#NioEventLoopGroup(int)}. + * @return a new {@link EventLoopGroup}. + */ + public static EventLoopGroup newNioOrEpollEventLoopGroup(int nThreads) { + if (Epoll.isAvailable()) { + return new EpollEventLoopGroup(nThreads); + } else { + return new NioEventLoopGroup(nThreads); + } + } + + /** + * If {@link Epoll#isAvailable()} == true, returns + * {@link EpollSocketChannel}, otherwise returns {@link NioSocketChannel}. + * @return a socket channel class. + */ + public static Class nioOrEpollSocketChannel() { + if (Epoll.isAvailable()) { + return EpollSocketChannel.class; + } else { + return NioSocketChannel.class; + } + } + + /** + * If {@link Epoll#isAvailable()} == true, returns + * {@link EpollServerSocketChannel}, otherwise returns + * {@link NioServerSocketChannel}. + * @return a server socket channel class. + */ + public static Class nioOrEpollServerSocketChannel() { + if (Epoll.isAvailable()) { + return EpollServerSocketChannel.class; + } else { + return NioServerSocketChannel.class; + } + } + + /** + * Attempts to detect and return the number of local network addresses that could be + * used by a client to reach this server. This means we exclude the following address types: + *
        + *
      • Multicast addresses. Zookeeper server sockets use TCP, thus cannot bind to a multicast address.
      • + *
      • Link-local addresses. Routers don't forward traffic sent to a link-local address, so + * any realistic server deployment would not have clients using these.
      • + *
      • Loopback addresses. These are typically only used for testing.
      • + *
      + * Any remaining addresses are counted, and the total count is returned. This number is + * used to configure the number of threads for the "boss" event loop group, to make sure we have + * enough threads for each address in case the server is configured to listen on + * all available addresses. + * If listing the network interfaces fails, this method will return 1. + * + * @return the number of client-reachable local network addresses found, or + * 1 if listing the network interfaces fails. + */ + public static int getClientReachableLocalInetAddressCount() { + try { + Set validInetAddresses = new HashSet<>(); + Enumeration allNetworkInterfaces = NetworkInterface.getNetworkInterfaces(); + for (NetworkInterface networkInterface : Collections.list(allNetworkInterfaces)) { + for (InetAddress inetAddress : Collections.list(networkInterface.getInetAddresses())) { + if (inetAddress.isLinkLocalAddress()) { + LOG.debug("Ignoring link-local InetAddress {}", inetAddress); + continue; + } + if (inetAddress.isMulticastAddress()) { + LOG.debug("Ignoring multicast InetAddress {}", inetAddress); + continue; + } + if (inetAddress.isLoopbackAddress()) { + LOG.debug("Ignoring loopback InetAddress {}", inetAddress); + continue; + } + validInetAddresses.add(inetAddress); + } + } + LOG.debug("Detected {} local network addresses: {}", validInetAddresses.size(), validInetAddresses); + return validInetAddresses.size() > 0 ? validInetAddresses.size() : DEFAULT_INET_ADDRESS_COUNT; + } catch (SocketException ex) { + LOG.warn("Failed to list all network interfaces, assuming 1", ex); + return DEFAULT_INET_ADDRESS_COUNT; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/PEMFileLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PEMFileLoader.java new file mode 100644 index 0000000..ab4b133 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PEMFileLoader.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.File; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.util.Optional; +import org.apache.zookeeper.util.PemReader; + +/** + * Implementation of {@link FileKeyStoreLoader} that loads from PEM files. + */ +class PEMFileLoader extends FileKeyStoreLoader { + + private PEMFileLoader( + String keyStorePath, + String trustStorePath, + String keyStorePassword, + String trustStorePassword) { + super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); + } + + @Override + public KeyStore loadKeyStore() throws IOException, GeneralSecurityException { + Optional passwordOption; + if (keyStorePassword == null || keyStorePassword.length() == 0) { + passwordOption = Optional.empty(); + } else { + passwordOption = Optional.of(keyStorePassword); + } + File file = new File(keyStorePath); + return PemReader.loadKeyStore(file, file, passwordOption); + } + + @Override + public KeyStore loadTrustStore() throws IOException, GeneralSecurityException { + return PemReader.loadTrustStore(new File(trustStorePath)); + } + + static class Builder extends FileKeyStoreLoader.Builder { + + @Override + PEMFileLoader build() { + return new PEMFileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/PKCS12FileLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PKCS12FileLoader.java new file mode 100644 index 0000000..8903f28 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PKCS12FileLoader.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + + +/** + * Implementation of {@link FileKeyStoreLoader} that loads from PKCS12 files. + */ +class PKCS12FileLoader extends StandardTypeFileKeyStoreLoader { + + private PKCS12FileLoader( + String keyStorePath, + String trustStorePath, + String keyStorePassword, + String trustStorePassword) { + super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword, SupportedStandardKeyFormat.PKCS12); + } + + static class Builder extends FileKeyStoreLoader.Builder { + + @Override + PKCS12FileLoader build() { + return new PKCS12FileLoader(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/PathTrie.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PathTrie.java new file mode 100644 index 0000000..86acdd9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PathTrie.java @@ -0,0 +1,354 @@ + /* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + + import java.util.ArrayDeque; + import java.util.Collection; + import java.util.Deque; + import java.util.HashMap; + import java.util.Map; + import java.util.Objects; + import java.util.concurrent.locks.Lock; + import java.util.concurrent.locks.ReadWriteLock; + import java.util.concurrent.locks.ReentrantReadWriteLock; + import java.util.stream.Stream; + import org.slf4j.Logger; + import org.slf4j.LoggerFactory; + + /** + * a class that implements prefix matching for + * components of a filesystem path. the trie + * looks like a tree with edges mapping to + * the component of a path. + * example /ab/bc/cf would map to a trie + * / + * ab/ + * (ab) + * bc/ + * / + * (bc) + * cf/ + * (cf) + */ + public class PathTrie { + + /** Logger for this class */ + private static final Logger LOG = LoggerFactory.getLogger(PathTrie.class); + + /** Root node of PathTrie */ + private final TrieNode rootNode; + + private final ReadWriteLock lock = new ReentrantReadWriteLock(true); + + private final Lock readLock = lock.readLock(); + + private final Lock writeLock = lock.writeLock(); + + static class TrieNode { + + final String value; + final Map children; + boolean property; + TrieNode parent; + + /** + * Create a trie node with parent as parameter. + * + * @param parent the parent of this node + * @param value the value stored in this node + */ + private TrieNode(TrieNode parent, String value) { + this.value = value; + this.parent = parent; + this.property = false; + this.children = new HashMap<>(4); + } + + /** + * Get the parent of this node. + * + * @return the parent node + */ + TrieNode getParent() { + return this.parent; + } + + /** + * set the parent of this node. + * + * @param parent the parent to set to + */ + void setParent(TrieNode parent) { + this.parent = parent; + } + + /** + * A property that is set for a node - making it special. + */ + void setProperty(boolean prop) { + this.property = prop; + } + + /** + * The property of this node. + * + * @return the property for this node + */ + boolean hasProperty() { + return this.property; + } + + /** + * The value stored in this node. + * + * @return the value stored in this node + */ + public String getValue() { + return this.value; + } + + /** + * Add a child to the existing node. + * + * @param childName the string name of the child + * @param node the node that is the child + */ + void addChild(String childName, TrieNode node) { + this.children.putIfAbsent(childName, node); + } + + /** + * Delete child from this node. + * + * @param childName the name of the child to be deleted + */ + void deleteChild(String childName) { + this.children.computeIfPresent(childName, (key, childNode) -> { + // Node no longer has an external property associated + childNode.setProperty(false); + + // Delete it if it has no children (is a leaf node) + if (childNode.isLeafNode()) { + childNode.setParent(null); + return null; + } + + return childNode; + }); + } + + /** + * Return the child of a node mapping to the input child name. + * + * @param childName the name of the child + * @return the child of a node + */ + TrieNode getChild(String childName) { + return this.children.get(childName); + } + + /** + * Get the list of children of this trienode. + * + * @return A collection containing the node's children + */ + Collection getChildren() { + return children.keySet(); + } + + /** + * Determine if this node is a leaf (has no children). + * + * @return true if this node is a lead node; otherwise false + */ + boolean isLeafNode() { + return children.isEmpty(); + } + + @Override + public String toString() { + return "TrieNode [name=" + value + ", property=" + property + ", children=" + children.keySet() + "]"; + } + + } + + /** + * Construct a new PathTrie with a root node. + */ + public PathTrie() { + this.rootNode = new TrieNode(null, "/"); + } + + /** + * Add a path to the path trie. All paths are relative to the root node. + * + * @param path the path to add to the trie + */ + public void addPath(final String path) { + Objects.requireNonNull(path, "Path cannot be null"); + + if (path.length() == 0) { + throw new IllegalArgumentException("Invalid path: " + path); + } + final String[] pathComponents = split(path); + + writeLock.lock(); + try { + TrieNode parent = rootNode; + for (final String part : pathComponents) { + TrieNode child = parent.getChild(part); + if (child == null) { + child = new TrieNode(parent, part); + parent.addChild(part, child); + } + parent = child; + } + parent.setProperty(true); + } finally { + writeLock.unlock(); + } + } + + /** + * Delete a path from the trie. All paths are relative to the root node. + * + * @param path the path to be deleted + */ + public void deletePath(final String path) { + Objects.requireNonNull(path, "Path cannot be null"); + + if (path.length() == 0) { + throw new IllegalArgumentException("Invalid path: " + path); + } + final String[] pathComponents = split(path); + + + writeLock.lock(); + try { + TrieNode parent = rootNode; + for (final String part : pathComponents) { + if (parent.getChild(part) == null) { + // the path does not exist + return; + } + parent = parent.getChild(part); + LOG.debug("{}", parent); + } + + final TrieNode realParent = parent.getParent(); + realParent.deleteChild(parent.getValue()); + } finally { + writeLock.unlock(); + } + } + + /** + * Return true if the given path exists in the trie, otherwise return false; + * All paths are relative to the root node. + * + * @param path the input path + * @return the largest prefix for the + */ + public boolean existsNode(final String path) { + Objects.requireNonNull(path, "Path cannot be null"); + + if (path.length() == 0) { + throw new IllegalArgumentException("Invalid path: " + path); + } + final String[] pathComponents = split(path); + + readLock.lock(); + try { + TrieNode parent = rootNode; + for (final String part : pathComponents) { + if (parent.getChild(part) == null) { + // the path does not exist + return false; + } + parent = parent.getChild(part); + LOG.debug("{}", parent); + } + } finally { + readLock.unlock(); + } + return true; + } + + /** + * Return the largest prefix for the input path. All paths are relative to the + * root node. + * + * @param path the input path + * @return the largest prefix for the input path + */ + public String findMaxPrefix(final String path) { + Objects.requireNonNull(path, "Path cannot be null"); + + final String[] pathComponents = split(path); + + readLock.lock(); + try { + TrieNode parent = rootNode; + TrieNode deepestPropertyNode = null; + for (final String element : pathComponents) { + parent = parent.getChild(element); + if (parent == null) { + LOG.debug("{}", element); + break; + } + if (parent.hasProperty()) { + deepestPropertyNode = parent; + } + } + + if (deepestPropertyNode == null) { + return "/"; + } + + final Deque treePath = new ArrayDeque<>(); + TrieNode node = deepestPropertyNode; + while (node != this.rootNode) { + treePath.offerFirst(node.getValue()); + node = node.parent; + } + return "/" + String.join("/", treePath); + } finally { + readLock.unlock(); + } + } + + /** + * Clear all nodes in the trie. + */ + public void clear() { + writeLock.lock(); + try { + rootNode.getChildren().clear(); + } finally { + writeLock.unlock(); + } + } + + private static String[] split(final String path){ + return Stream.of(path.split("/")) + .filter(t -> !t.trim().isEmpty()) + .toArray(String[]::new); + } + + } diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/PathUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PathUtils.java new file mode 100644 index 0000000..114df6e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/PathUtils.java @@ -0,0 +1,112 @@ + /* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + + /** + * Path related utilities + */ + public class PathUtils { + + /** validate the provided znode path string + * @param path znode path string + * @param isSequential if the path is being created + * with a sequential flag + * @throws IllegalArgumentException if the path is invalid + */ + public static void validatePath(String path, boolean isSequential) throws IllegalArgumentException { + validatePath(isSequential ? path + "1" : path); + } + + /** + * Validate the provided znode path string + * @param path znode path string + * @throws IllegalArgumentException if the path is invalid + */ + public static void validatePath(String path) throws IllegalArgumentException { + if (path == null) { + throw new IllegalArgumentException("Path cannot be null"); + } + if (path.length() == 0) { + throw new IllegalArgumentException("Path length must be > 0"); + } + if (path.charAt(0) != '/') { + throw new IllegalArgumentException("Path must start with / character"); + } + if (path.length() == 1) { // done checking - it's the root + return; + } + if (path.charAt(path.length() - 1) == '/') { + throw new IllegalArgumentException("Path must not end with / character"); + } + + String reason = null; + char lastc = '/'; + char[] chars = path.toCharArray(); + char c; + for (int i = 1; i < chars.length; lastc = chars[i], i++) { + c = chars[i]; + + if (c == 0) { + reason = "null character not allowed @" + i; + break; + } else if (c == '/' && lastc == '/') { + reason = "empty node name specified @" + i; + break; + } else if (c == '.' && lastc == '.') { + if (chars[i - 2] == '/' && ((i + 1 == chars.length) || chars[i + 1] == '/')) { + reason = "relative paths not allowed @" + i; + break; + } + } else if (c == '.') { + if (chars[i - 1] == '/' && ((i + 1 == chars.length) || chars[i + 1] == '/')) { + reason = "relative paths not allowed @" + i; + break; + } + } else if (c > '\u0000' && c <= '\u001f' + || c >= '\u007f' && c <= '\u009F' + || c >= '\ud800' && c <= '\uf8ff' + || c >= '\ufff0' && c <= '\uffff') { + reason = "invalid character @" + i; + break; + } + } + + if (reason != null) { + throw new IllegalArgumentException("Invalid path string \"" + path + "\" caused by " + reason); + } + } + + /** + * Convert Windows path to Unix + * + * @param path + * file path + * @return converted file path + */ + public static String normalizeFileSystemPath(String path) { + if (path != null) { + String osname = java.lang.System.getProperty("os.name"); + if (osname.toLowerCase().contains("windows")) { + return path.replace('\\', '/'); + } + } + return path; + } + + } diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/QuorumX509Util.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/QuorumX509Util.java new file mode 100644 index 0000000..d4cf19f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/QuorumX509Util.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +public class QuorumX509Util extends X509Util { + + @Override + protected String getConfigPrefix() { + return "zookeeper.ssl.quorum."; + } + + @Override + protected boolean shouldVerifyClientHostname() { + return true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/SSLContextAndOptions.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/SSLContextAndOptions.java new file mode 100644 index 0000000..01e97c3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/SSLContextAndOptions.java @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static java.util.Objects.requireNonNull; +import io.netty.handler.ssl.IdentityCipherSuiteFilter; +import io.netty.handler.ssl.JdkSslContext; +import io.netty.handler.ssl.SslContext; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLSocket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Wrapper class for an SSLContext + some config options that can't be set on the context when it is created but + * must be set on a secure socket created by the context after the socket creation. By wrapping the options in this + * class we avoid reading from global system properties during socket configuration. This makes testing easier + * since we can create different X509Util instances with different configurations in a single test process, and + * unit test interactions between them. + */ +public class SSLContextAndOptions { + + private static final Logger LOG = LoggerFactory.getLogger(SSLContextAndOptions.class); + + private final X509Util x509Util; + private final String[] enabledProtocols; + private final String[] cipherSuites; + private final List cipherSuitesAsList; + private final X509Util.ClientAuth clientAuth; + private final SSLContext sslContext; + private final int handshakeDetectionTimeoutMillis; + + /** + * Note: constructor is intentionally package-private, only the X509Util class should be creating instances of this + * class. + * @param x509Util the X509Util that created this object. + * @param config a ZKConfig that holds config properties. + * @param sslContext the SSLContext. + */ + SSLContextAndOptions(final X509Util x509Util, final ZKConfig config, final SSLContext sslContext) { + this.x509Util = requireNonNull(x509Util); + this.sslContext = requireNonNull(sslContext); + this.enabledProtocols = getEnabledProtocols(requireNonNull(config), sslContext); + String[] ciphers = getCipherSuites(config); + this.cipherSuites = ciphers; + this.cipherSuitesAsList = Collections.unmodifiableList(Arrays.asList(ciphers)); + this.clientAuth = getClientAuth(config); + this.handshakeDetectionTimeoutMillis = getHandshakeDetectionTimeoutMillis(config); + } + + public SSLContext getSSLContext() { + return sslContext; + } + + public SSLSocket createSSLSocket() throws IOException { + return configureSSLSocket((SSLSocket) sslContext.getSocketFactory().createSocket(), true); + } + + public SSLSocket createSSLSocket(Socket socket, byte[] pushbackBytes) throws IOException { + SSLSocket sslSocket; + if (pushbackBytes != null && pushbackBytes.length > 0) { + sslSocket = (SSLSocket) sslContext.getSocketFactory() + .createSocket(socket, new ByteArrayInputStream(pushbackBytes), true); + } else { + sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(socket, null, socket.getPort(), true); + } + return configureSSLSocket(sslSocket, false); + } + + public SSLServerSocket createSSLServerSocket() throws IOException { + SSLServerSocket sslServerSocket = (SSLServerSocket) sslContext.getServerSocketFactory().createServerSocket(); + return configureSSLServerSocket(sslServerSocket); + } + + public SSLServerSocket createSSLServerSocket(int port) throws IOException { + SSLServerSocket sslServerSocket = (SSLServerSocket) sslContext.getServerSocketFactory().createServerSocket(port); + return configureSSLServerSocket(sslServerSocket); + } + + public SslContext createNettyJdkSslContext(SSLContext sslContext, boolean isClientSocket) { + return new JdkSslContext( + sslContext, + isClientSocket, + cipherSuitesAsList, + IdentityCipherSuiteFilter.INSTANCE, + null, + isClientSocket + ? X509Util.ClientAuth.NONE.toNettyClientAuth() + : clientAuth.toNettyClientAuth(), + enabledProtocols, + false); + } + + public int getHandshakeDetectionTimeoutMillis() { + return handshakeDetectionTimeoutMillis; + } + + private SSLSocket configureSSLSocket(SSLSocket socket, boolean isClientSocket) { + SSLParameters sslParameters = socket.getSSLParameters(); + configureSslParameters(sslParameters, isClientSocket); + socket.setSSLParameters(sslParameters); + socket.setUseClientMode(isClientSocket); + return socket; + } + + private SSLServerSocket configureSSLServerSocket(SSLServerSocket socket) { + SSLParameters sslParameters = socket.getSSLParameters(); + configureSslParameters(sslParameters, false); + socket.setSSLParameters(sslParameters); + socket.setUseClientMode(false); + return socket; + } + + private void configureSslParameters(SSLParameters sslParameters, boolean isClientSocket) { + if (cipherSuites != null) { + LOG.debug( + "Setup cipher suites for {} socket: {}", + isClientSocket ? "client" : "server", + Arrays.toString(cipherSuites)); + sslParameters.setCipherSuites(cipherSuites); + } + + if (enabledProtocols != null) { + LOG.debug( + "Setup enabled protocols for {} socket: {}", + isClientSocket ? "client" : "server", + Arrays.toString(enabledProtocols)); + sslParameters.setProtocols(enabledProtocols); + } + + if (!isClientSocket) { + switch (clientAuth) { + case NEED: + sslParameters.setNeedClientAuth(true); + break; + case WANT: + sslParameters.setWantClientAuth(true); + break; + default: + sslParameters.setNeedClientAuth(false); // also clears the wantClientAuth flag according to docs + break; + } + } + } + + private String[] getEnabledProtocols(final ZKConfig config, final SSLContext sslContext) { + String enabledProtocolsInput = config.getProperty(x509Util.getSslEnabledProtocolsProperty()); + if (enabledProtocolsInput == null) { + return new String[]{sslContext.getProtocol()}; + } + return enabledProtocolsInput.split(","); + } + + private String[] getCipherSuites(final ZKConfig config) { + String cipherSuitesInput = config.getProperty(x509Util.getSslCipherSuitesProperty()); + if (cipherSuitesInput == null) { + return X509Util.getDefaultCipherSuites(); + } else { + return cipherSuitesInput.split(","); + } + } + + private X509Util.ClientAuth getClientAuth(final ZKConfig config) { + return X509Util.ClientAuth.fromPropertyValue(config.getProperty(x509Util.getSslClientAuthProperty())); + } + + private int getHandshakeDetectionTimeoutMillis(final ZKConfig config) { + String propertyString = config.getProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty()); + int result; + if (propertyString == null) { + result = X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS; + } else { + result = Integer.parseInt(propertyString); + if (result < 1) { + // Timeout of 0 is not allowed, since an infinite timeout can permanently lock up an + // accept() thread. + LOG.warn( + "Invalid value for {}: {}, using the default value of {}", + x509Util.getSslHandshakeDetectionTimeoutMillisProperty(), + result, + X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS); + result = X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS; + } + } + return result; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/StandardTypeFileKeyStoreLoader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/StandardTypeFileKeyStoreLoader.java new file mode 100644 index 0000000..8f068f7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/StandardTypeFileKeyStoreLoader.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.security.KeyStoreException; + +/** + * Base class for instances of {@link KeyStoreLoader} which load the key/trust + * stores from files on a filesystem using standard {@link KeyStore} types like + * JKS or PKCS12. + */ +abstract class StandardTypeFileKeyStoreLoader extends FileKeyStoreLoader { + + private static final char[] EMPTY_CHAR_ARRAY = new char[0]; + + protected final SupportedStandardKeyFormat format; + + protected enum SupportedStandardKeyFormat { + JKS, PKCS12, BCFKS + } + + + StandardTypeFileKeyStoreLoader(String keyStorePath, String trustStorePath, String keyStorePassword, + String trustStorePassword, SupportedStandardKeyFormat format) { + super(keyStorePath, trustStorePath, keyStorePassword, trustStorePassword); + this.format = format; + } + + @Override + public KeyStore loadKeyStore() throws IOException, GeneralSecurityException { + try (InputStream inputStream = new FileInputStream(new File(keyStorePath))) { + KeyStore ks = keyStoreInstance(); + ks.load(inputStream, passwordStringToCharArray(keyStorePassword)); + return ks; + } + } + + @Override + public KeyStore loadTrustStore() throws IOException, GeneralSecurityException { + try (InputStream inputStream = new FileInputStream(new File(trustStorePath))) { + KeyStore ts = keyStoreInstance(); + ts.load(inputStream, passwordStringToCharArray(trustStorePassword)); + return ts; + } + } + + private KeyStore keyStoreInstance() throws KeyStoreException { + return KeyStore.getInstance(format.name()); + } + + private static char[] passwordStringToCharArray(String password) { + return password == null ? EMPTY_CHAR_ARRAY : password.toCharArray(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java new file mode 100644 index 0000000..36ee2d7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java @@ -0,0 +1,65 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class StringUtils { + + private StringUtils() {/** non instantiable and non inheritable **/} + + /** + * This method returns an immutable List<String>, but different from String's split() + * it trims the results in the input String, and removes any empty string from + * the resulting List. + * + */ + public static List split(String value, String separator) { + String[] splits = value.split(separator); + List results = new ArrayList(); + for (int i = 0; i < splits.length; i++) { + splits[i] = splits[i].trim(); + if (splits[i].length() > 0) { + results.add(splits[i]); + } + } + return Collections.unmodifiableList(results); + } + + /** + * This method takes a List<String> and a delimiter and joins the strings + * into a single string, where the original strings are separated using + * the given delimiter. + * + */ + public static String joinStrings(List list, String delim) { + if (list == null) { + return null; + } + + StringBuilder builder = new StringBuilder(list.get(0)); + for (String s : list.subList(1, list.size())) { + builder.append(delim).append(s); + } + + return builder.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java new file mode 100644 index 0000000..08891f7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/Time.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.util.Date; + +public class Time { + + /** + * Returns time in milliseconds as does System.currentTimeMillis(), + * but uses elapsed time from an arbitrary epoch more like System.nanoTime(). + * The difference is that if somebody changes the system clock, + * Time.currentElapsedTime will change but nanoTime won't. On the other hand, + * all of ZK assumes that time is measured in milliseconds. + * @return The time in milliseconds from some arbitrary point in time. + */ + public static long currentElapsedTime() { + return System.nanoTime() / 1000000; + } + + /** + * Explicitly returns system dependent current wall time. + * @return Current time in msec. + */ + public static long currentWallTime() { + return System.currentTimeMillis(); + } + + /** + * This is to convert the elapsedTime to a Date. + * @return A date object indicated by the elapsedTime. + */ + public static Date elapsedTimeToDate(long elapsedTime) { + long wallTime = currentWallTime() + elapsedTime - currentElapsedTime(); + return new Date(wallTime); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/X509Exception.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/X509Exception.java new file mode 100644 index 0000000..2d16df5 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/X509Exception.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +@SuppressWarnings("serial") +public class X509Exception extends Exception { + + public X509Exception(String message) { + super(message); + } + + public X509Exception(Throwable cause) { + super(cause); + } + + public X509Exception(String message, Throwable cause) { + super(message, cause); + } + + public static class KeyManagerException extends X509Exception { + + public KeyManagerException(String message) { + super(message); + } + + public KeyManagerException(Throwable cause) { + super(cause); + } + + } + + public static class TrustManagerException extends X509Exception { + + public TrustManagerException(String message) { + super(message); + } + + public TrustManagerException(Throwable cause) { + super(cause); + } + + } + + public static class SSLContextException extends X509Exception { + + public SSLContextException(String message) { + super(message); + } + + public SSLContextException(Throwable cause) { + super(cause); + } + + public SSLContextException(String message, Throwable cause) { + super(message, cause); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/X509Util.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/X509Util.java new file mode 100644 index 0000000..52cb5fe --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/X509Util.java @@ -0,0 +1,648 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.net.Socket; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardWatchEventKinds; +import java.nio.file.WatchEvent; +import java.security.GeneralSecurityException; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.NoSuchAlgorithmException; +import java.security.Security; +import java.security.cert.PKIXBuilderParameters; +import java.security.cert.X509CertSelector; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; +import javax.net.ssl.CertPathTrustManagerParameters; +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import javax.net.ssl.X509ExtendedTrustManager; +import javax.net.ssl.X509KeyManager; +import javax.net.ssl.X509TrustManager; +import org.apache.zookeeper.common.X509Exception.KeyManagerException; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.common.X509Exception.TrustManagerException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility code for X509 handling + * + * Default cipher suites: + * + * Performance testing done by Facebook engineers shows that on Intel x86_64 machines, Java9 performs better with + * GCM and Java8 performs better with CBC, so these seem like reasonable defaults. + */ +public abstract class X509Util implements Closeable, AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(X509Util.class); + + private static final String REJECT_CLIENT_RENEGOTIATION_PROPERTY = "jdk.tls.rejectClientInitiatedRenegotiation"; + + static { + // Client-initiated renegotiation in TLS is unsafe and + // allows MITM attacks, so we should disable it unless + // it was explicitly enabled by the user. + // A brief summary of the issue can be found at + // https://www.ietf.org/proceedings/76/slides/tls-7.pdf + if (System.getProperty(REJECT_CLIENT_RENEGOTIATION_PROPERTY) == null) { + LOG.info("Setting -D {}=true to disable client-initiated TLS renegotiation", REJECT_CLIENT_RENEGOTIATION_PROPERTY); + System.setProperty(REJECT_CLIENT_RENEGOTIATION_PROPERTY, Boolean.TRUE.toString()); + } + } + + public static final String DEFAULT_PROTOCOL = "TLSv1.2"; + private static String[] getGCMCiphers() { + return new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384"}; + } + + private static String[] getCBCCiphers() { + return new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA"}; + } + + private static String[] concatArrays(String[] left, String[] right) { + String[] result = new String[left.length + right.length]; + System.arraycopy(left, 0, result, 0, left.length); + System.arraycopy(right, 0, result, left.length, right.length); + return result; + } + + // On Java 8, prefer CBC ciphers since AES-NI support is lacking and GCM is slower than CBC. + private static final String[] DEFAULT_CIPHERS_JAVA8 = concatArrays(getCBCCiphers(), getGCMCiphers()); + // On Java 9 and later, prefer GCM ciphers due to improved AES-NI support. + // Note that this performance assumption might not hold true for architectures other than x86_64. + private static final String[] DEFAULT_CIPHERS_JAVA9 = concatArrays(getGCMCiphers(), getCBCCiphers()); + + public static final int DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS = 5000; + + /** + * Enum specifying the client auth requirement of server-side TLS sockets created by this X509Util. + *
        + *
      • NONE - do not request a client certificate.
      • + *
      • WANT - request a client certificate, but allow anonymous clients to connect.
      • + *
      • NEED - require a client certificate, disconnect anonymous clients.
      • + *
      + * + * If the config property is not set, the default value is NEED. + */ + public enum ClientAuth { + NONE(io.netty.handler.ssl.ClientAuth.NONE), + WANT(io.netty.handler.ssl.ClientAuth.OPTIONAL), + NEED(io.netty.handler.ssl.ClientAuth.REQUIRE); + + private final io.netty.handler.ssl.ClientAuth nettyAuth; + + ClientAuth(io.netty.handler.ssl.ClientAuth nettyAuth) { + this.nettyAuth = nettyAuth; + } + + /** + * Converts a property value to a ClientAuth enum. If the input string is empty or null, returns + * ClientAuth.NEED. + * @param prop the property string. + * @return the ClientAuth. + * @throws IllegalArgumentException if the property value is not "NONE", "WANT", "NEED", or empty/null. + */ + public static ClientAuth fromPropertyValue(String prop) { + if (prop == null || prop.length() == 0) { + return NEED; + } + return ClientAuth.valueOf(prop.toUpperCase()); + } + + public io.netty.handler.ssl.ClientAuth toNettyClientAuth() { + return nettyAuth; + } + } + + private String sslProtocolProperty = getConfigPrefix() + "protocol"; + private String sslEnabledProtocolsProperty = getConfigPrefix() + "enabledProtocols"; + private String cipherSuitesProperty = getConfigPrefix() + "ciphersuites"; + private String sslKeystoreLocationProperty = getConfigPrefix() + "keyStore.location"; + private String sslKeystorePasswdProperty = getConfigPrefix() + "keyStore.password"; + private String sslKeystoreTypeProperty = getConfigPrefix() + "keyStore.type"; + private String sslTruststoreLocationProperty = getConfigPrefix() + "trustStore.location"; + private String sslTruststorePasswdProperty = getConfigPrefix() + "trustStore.password"; + private String sslTruststoreTypeProperty = getConfigPrefix() + "trustStore.type"; + private String sslContextSupplierClassProperty = getConfigPrefix() + "context.supplier.class"; + private String sslHostnameVerificationEnabledProperty = getConfigPrefix() + "hostnameVerification"; + private String sslCrlEnabledProperty = getConfigPrefix() + "crl"; + private String sslOcspEnabledProperty = getConfigPrefix() + "ocsp"; + private String sslClientAuthProperty = getConfigPrefix() + "clientAuth"; + private String sslHandshakeDetectionTimeoutMillisProperty = getConfigPrefix() + "handshakeDetectionTimeoutMillis"; + + private ZKConfig zkConfig; + private AtomicReference defaultSSLContextAndOptions = new AtomicReference<>(null); + + private FileChangeWatcher keyStoreFileWatcher; + private FileChangeWatcher trustStoreFileWatcher; + + public X509Util() { + this(null); + } + + public X509Util(ZKConfig zkConfig) { + this.zkConfig = zkConfig; + keyStoreFileWatcher = trustStoreFileWatcher = null; + } + + protected abstract String getConfigPrefix(); + + protected abstract boolean shouldVerifyClientHostname(); + + public String getSslProtocolProperty() { + return sslProtocolProperty; + } + + public String getSslEnabledProtocolsProperty() { + return sslEnabledProtocolsProperty; + } + + public String getCipherSuitesProperty() { + return cipherSuitesProperty; + } + + public String getSslKeystoreLocationProperty() { + return sslKeystoreLocationProperty; + } + + public String getSslCipherSuitesProperty() { + return cipherSuitesProperty; + } + + public String getSslKeystorePasswdProperty() { + return sslKeystorePasswdProperty; + } + + public String getSslKeystoreTypeProperty() { + return sslKeystoreTypeProperty; + } + + public String getSslTruststoreLocationProperty() { + return sslTruststoreLocationProperty; + } + + public String getSslTruststorePasswdProperty() { + return sslTruststorePasswdProperty; + } + + public String getSslTruststoreTypeProperty() { + return sslTruststoreTypeProperty; + } + + public String getSslContextSupplierClassProperty() { + return sslContextSupplierClassProperty; + } + + public String getSslHostnameVerificationEnabledProperty() { + return sslHostnameVerificationEnabledProperty; + } + + public String getSslCrlEnabledProperty() { + return sslCrlEnabledProperty; + } + + public String getSslOcspEnabledProperty() { + return sslOcspEnabledProperty; + } + + public String getSslClientAuthProperty() { + return sslClientAuthProperty; + } + + /** + * Returns the config property key that controls the amount of time, in milliseconds, that the first + * UnifiedServerSocket read operation will block for when trying to detect the client mode (TLS or PLAINTEXT). + * + * @return the config property key. + */ + public String getSslHandshakeDetectionTimeoutMillisProperty() { + return sslHandshakeDetectionTimeoutMillisProperty; + } + + public SSLContext getDefaultSSLContext() throws X509Exception.SSLContextException { + return getDefaultSSLContextAndOptions().getSSLContext(); + } + + public SSLContext createSSLContext(ZKConfig config) throws SSLContextException { + return createSSLContextAndOptions(config).getSSLContext(); + } + + public SSLContextAndOptions getDefaultSSLContextAndOptions() throws X509Exception.SSLContextException { + SSLContextAndOptions result = defaultSSLContextAndOptions.get(); + if (result == null) { + result = createSSLContextAndOptions(); + if (!defaultSSLContextAndOptions.compareAndSet(null, result)) { + // lost the race, another thread already set the value + result = defaultSSLContextAndOptions.get(); + } + } + return result; + } + + private void resetDefaultSSLContextAndOptions() throws X509Exception.SSLContextException { + SSLContextAndOptions newContext = createSSLContextAndOptions(); + defaultSSLContextAndOptions.set(newContext); + } + + private SSLContextAndOptions createSSLContextAndOptions() throws SSLContextException { + /* + * Since Configuration initializes the key store and trust store related + * configuration from system property. Reading property from + * configuration will be same reading from system property + */ + return createSSLContextAndOptions(zkConfig == null ? new ZKConfig() : zkConfig); + } + + /** + * Returns the max amount of time, in milliseconds, that the first UnifiedServerSocket read() operation should + * block for when trying to detect the client mode (TLS or PLAINTEXT). + * Defaults to {@link X509Util#DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS}. + * + * @return the handshake detection timeout, in milliseconds. + */ + public int getSslHandshakeTimeoutMillis() { + try { + SSLContextAndOptions ctx = getDefaultSSLContextAndOptions(); + return ctx.getHandshakeDetectionTimeoutMillis(); + } catch (SSLContextException e) { + LOG.error("Error creating SSL context and options", e); + return DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS; + } catch (Exception e) { + LOG.error("Error parsing config property {}", getSslHandshakeDetectionTimeoutMillisProperty(), e); + return DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS; + } + } + + @SuppressWarnings("unchecked") + public SSLContextAndOptions createSSLContextAndOptions(ZKConfig config) throws SSLContextException { + final String supplierContextClassName = config.getProperty(sslContextSupplierClassProperty); + if (supplierContextClassName != null) { + LOG.debug("Loading SSLContext supplier from property '{}'", sslContextSupplierClassProperty); + + try { + Class sslContextClass = Class.forName(supplierContextClassName); + Supplier sslContextSupplier = (Supplier) sslContextClass.getConstructor().newInstance(); + return new SSLContextAndOptions(this, config, sslContextSupplier.get()); + } catch (ClassNotFoundException + | ClassCastException + | NoSuchMethodException + | InvocationTargetException + | InstantiationException + | IllegalAccessException e) { + throw new SSLContextException("Could not retrieve the SSLContext from supplier source '" + + supplierContextClassName + + "' provided in the property '" + + sslContextSupplierClassProperty + + "'", e); + } + } else { + return createSSLContextAndOptionsFromConfig(config); + } + } + + public SSLContextAndOptions createSSLContextAndOptionsFromConfig(ZKConfig config) throws SSLContextException { + KeyManager[] keyManagers = null; + TrustManager[] trustManagers = null; + + String keyStoreLocationProp = config.getProperty(sslKeystoreLocationProperty, ""); + String keyStorePasswordProp = config.getProperty(sslKeystorePasswdProperty, ""); + String keyStoreTypeProp = config.getProperty(sslKeystoreTypeProperty); + + // There are legal states in some use cases for null KeyManager or TrustManager. + // But if a user wanna specify one, location is required. Password defaults to empty string if it is not + // specified by the user. + + if (keyStoreLocationProp.isEmpty()) { + LOG.warn("{} not specified", getSslKeystoreLocationProperty()); + } else { + try { + keyManagers = new KeyManager[]{createKeyManager(keyStoreLocationProp, keyStorePasswordProp, keyStoreTypeProp)}; + } catch (KeyManagerException keyManagerException) { + throw new SSLContextException("Failed to create KeyManager", keyManagerException); + } catch (IllegalArgumentException e) { + throw new SSLContextException("Bad value for " + sslKeystoreTypeProperty + ": " + keyStoreTypeProp, e); + } + } + + String trustStoreLocationProp = config.getProperty(sslTruststoreLocationProperty, ""); + String trustStorePasswordProp = config.getProperty(sslTruststorePasswdProperty, ""); + String trustStoreTypeProp = config.getProperty(sslTruststoreTypeProperty); + + boolean sslCrlEnabled = config.getBoolean(this.sslCrlEnabledProperty); + boolean sslOcspEnabled = config.getBoolean(this.sslOcspEnabledProperty); + boolean sslServerHostnameVerificationEnabled = config.getBoolean(this.getSslHostnameVerificationEnabledProperty(), true); + boolean sslClientHostnameVerificationEnabled = sslServerHostnameVerificationEnabled && shouldVerifyClientHostname(); + + if (trustStoreLocationProp.isEmpty()) { + LOG.warn("{} not specified", getSslTruststoreLocationProperty()); + } else { + try { + trustManagers = new TrustManager[]{createTrustManager(trustStoreLocationProp, trustStorePasswordProp, trustStoreTypeProp, sslCrlEnabled, sslOcspEnabled, sslServerHostnameVerificationEnabled, sslClientHostnameVerificationEnabled)}; + } catch (TrustManagerException trustManagerException) { + throw new SSLContextException("Failed to create TrustManager", trustManagerException); + } catch (IllegalArgumentException e) { + throw new SSLContextException("Bad value for " + + sslTruststoreTypeProperty + + ": " + + trustStoreTypeProp, e); + } + } + + String protocol = config.getProperty(sslProtocolProperty, DEFAULT_PROTOCOL); + try { + SSLContext sslContext = SSLContext.getInstance(protocol); + sslContext.init(keyManagers, trustManagers, null); + return new SSLContextAndOptions(this, config, sslContext); + } catch (NoSuchAlgorithmException | KeyManagementException sslContextInitException) { + throw new SSLContextException(sslContextInitException); + } + } + + public static KeyStore loadKeyStore( + String keyStoreLocation, + String keyStorePassword, + String keyStoreTypeProp) throws IOException, GeneralSecurityException { + KeyStoreFileType storeFileType = KeyStoreFileType.fromPropertyValueOrFileName(keyStoreTypeProp, keyStoreLocation); + return FileKeyStoreLoaderBuilderProvider + .getBuilderForKeyStoreFileType(storeFileType) + .setKeyStorePath(keyStoreLocation) + .setKeyStorePassword(keyStorePassword) + .build() + .loadKeyStore(); + } + + public static KeyStore loadTrustStore( + String trustStoreLocation, + String trustStorePassword, + String trustStoreTypeProp) throws IOException, GeneralSecurityException { + KeyStoreFileType storeFileType = KeyStoreFileType.fromPropertyValueOrFileName(trustStoreTypeProp, trustStoreLocation); + return FileKeyStoreLoaderBuilderProvider + .getBuilderForKeyStoreFileType(storeFileType) + .setTrustStorePath(trustStoreLocation) + .setTrustStorePassword(trustStorePassword) + .build() + .loadTrustStore(); + } + + /** + * Creates a key manager by loading the key store from the given file of + * the given type, optionally decrypting it using the given password. + * @param keyStoreLocation the location of the key store file. + * @param keyStorePassword optional password to decrypt the key store. If + * empty, assumes the key store is not encrypted. + * @param keyStoreTypeProp must be JKS, PEM, PKCS12, BCFKS or null. If null, + * attempts to autodetect the key store type from + * the file extension (e.g. .jks / .pem). + * @return the key manager. + * @throws KeyManagerException if something goes wrong. + */ + public static X509KeyManager createKeyManager( + String keyStoreLocation, + String keyStorePassword, + String keyStoreTypeProp) throws KeyManagerException { + if (keyStorePassword == null) { + keyStorePassword = ""; + } + try { + KeyStore ks = loadKeyStore(keyStoreLocation, keyStorePassword, keyStoreTypeProp); + KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX"); + kmf.init(ks, keyStorePassword.toCharArray()); + + for (KeyManager km : kmf.getKeyManagers()) { + if (km instanceof X509KeyManager) { + return (X509KeyManager) km; + } + } + throw new KeyManagerException("Couldn't find X509KeyManager"); + } catch (IOException | GeneralSecurityException | IllegalArgumentException e) { + throw new KeyManagerException(e); + } + } + + /** + * Creates a trust manager by loading the trust store from the given file + * of the given type, optionally decrypting it using the given password. + * @param trustStoreLocation the location of the trust store file. + * @param trustStorePassword optional password to decrypt the trust store + * (only applies to JKS trust stores). If empty, + * assumes the trust store is not encrypted. + * @param trustStoreTypeProp must be JKS, PEM, PKCS12, BCFKS or null. If + * null, attempts to autodetect the trust store + * type from the file extension (e.g. .jks / .pem). + * @param crlEnabled enable CRL (certificate revocation list) checks. + * @param ocspEnabled enable OCSP (online certificate status protocol) + * checks. + * @param serverHostnameVerificationEnabled if true, verify hostnames of + * remote servers that client + * sockets created by this + * X509Util connect to. + * @param clientHostnameVerificationEnabled if true, verify hostnames of + * remote clients that server + * sockets created by this + * X509Util accept connections + * from. + * @return the trust manager. + * @throws TrustManagerException if something goes wrong. + */ + public static X509TrustManager createTrustManager( + String trustStoreLocation, + String trustStorePassword, + String trustStoreTypeProp, + boolean crlEnabled, + boolean ocspEnabled, + final boolean serverHostnameVerificationEnabled, + final boolean clientHostnameVerificationEnabled) throws TrustManagerException { + if (trustStorePassword == null) { + trustStorePassword = ""; + } + try { + KeyStore ts = loadTrustStore(trustStoreLocation, trustStorePassword, trustStoreTypeProp); + PKIXBuilderParameters pbParams = new PKIXBuilderParameters(ts, new X509CertSelector()); + if (crlEnabled || ocspEnabled) { + pbParams.setRevocationEnabled(true); + System.setProperty("com.sun.net.ssl.checkRevocation", "true"); + System.setProperty("com.sun.security.enableCRLDP", "true"); + if (ocspEnabled) { + Security.setProperty("ocsp.enable", "true"); + } + } else { + pbParams.setRevocationEnabled(false); + } + + // Revocation checking is only supported with the PKIX algorithm + TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); + tmf.init(new CertPathTrustManagerParameters(pbParams)); + + for (final TrustManager tm : tmf.getTrustManagers()) { + if (tm instanceof X509ExtendedTrustManager) { + return new ZKTrustManager((X509ExtendedTrustManager) tm, serverHostnameVerificationEnabled, clientHostnameVerificationEnabled); + } + } + throw new TrustManagerException("Couldn't find X509TrustManager"); + } catch (IOException | GeneralSecurityException | IllegalArgumentException e) { + throw new TrustManagerException(e); + } + } + + public SSLSocket createSSLSocket() throws X509Exception, IOException { + return getDefaultSSLContextAndOptions().createSSLSocket(); + } + + public SSLSocket createSSLSocket(Socket socket, byte[] pushbackBytes) throws X509Exception, IOException { + return getDefaultSSLContextAndOptions().createSSLSocket(socket, pushbackBytes); + } + + public SSLServerSocket createSSLServerSocket() throws X509Exception, IOException { + return getDefaultSSLContextAndOptions().createSSLServerSocket(); + } + + public SSLServerSocket createSSLServerSocket(int port) throws X509Exception, IOException { + return getDefaultSSLContextAndOptions().createSSLServerSocket(port); + } + + static String[] getDefaultCipherSuites() { + return getDefaultCipherSuitesForJavaVersion(System.getProperty("java.specification.version")); + } + + static String[] getDefaultCipherSuitesForJavaVersion(String javaVersion) { + Objects.requireNonNull(javaVersion); + if (javaVersion.matches("\\d+")) { + // Must be Java 9 or later + LOG.debug("Using Java9+ optimized cipher suites for Java version {}", javaVersion); + return DEFAULT_CIPHERS_JAVA9; + } else if (javaVersion.startsWith("1.")) { + // Must be Java 1.8 or earlier + LOG.debug("Using Java8 optimized cipher suites for Java version {}", javaVersion); + return DEFAULT_CIPHERS_JAVA8; + } else { + LOG.debug("Could not parse java version {}, using Java8 optimized cipher suites", javaVersion); + return DEFAULT_CIPHERS_JAVA8; + } + } + + private FileChangeWatcher newFileChangeWatcher(String fileLocation) throws IOException { + if (fileLocation == null || fileLocation.isEmpty()) { + return null; + } + final Path filePath = Paths.get(fileLocation).toAbsolutePath(); + Path parentPath = filePath.getParent(); + if (parentPath == null) { + throw new IOException("Key/trust store path does not have a parent: " + filePath); + } + return new FileChangeWatcher(parentPath, watchEvent -> { + handleWatchEvent(filePath, watchEvent); + }); + } + + /** + * Enables automatic reloading of the trust store and key store files when they change on disk. + * + * @throws IOException if creating the FileChangeWatcher objects fails. + */ + public void enableCertFileReloading() throws IOException { + LOG.info("enabling cert file reloading"); + ZKConfig config = zkConfig == null ? new ZKConfig() : zkConfig; + FileChangeWatcher newKeyStoreFileWatcher = newFileChangeWatcher(config.getProperty(sslKeystoreLocationProperty)); + if (newKeyStoreFileWatcher != null) { + // stop old watcher if there is one + if (keyStoreFileWatcher != null) { + keyStoreFileWatcher.stop(); + } + keyStoreFileWatcher = newKeyStoreFileWatcher; + keyStoreFileWatcher.start(); + } + FileChangeWatcher newTrustStoreFileWatcher = newFileChangeWatcher(config.getProperty(sslTruststoreLocationProperty)); + if (newTrustStoreFileWatcher != null) { + // stop old watcher if there is one + if (trustStoreFileWatcher != null) { + trustStoreFileWatcher.stop(); + } + trustStoreFileWatcher = newTrustStoreFileWatcher; + trustStoreFileWatcher.start(); + } + } + + /** + * Disables automatic reloading of the trust store and key store files when they change on disk. + * Stops background threads and closes WatchService instances. + */ + @Override + public void close() { + if (keyStoreFileWatcher != null) { + keyStoreFileWatcher.stop(); + keyStoreFileWatcher = null; + } + if (trustStoreFileWatcher != null) { + trustStoreFileWatcher.stop(); + trustStoreFileWatcher = null; + } + } + + /** + * Handler for watch events that let us know a file we may care about has changed on disk. + * + * @param filePath the path to the file we are watching for changes. + * @param event the WatchEvent. + */ + private void handleWatchEvent(Path filePath, WatchEvent event) { + boolean shouldResetContext = false; + Path dirPath = filePath.getParent(); + if (event.kind().equals(StandardWatchEventKinds.OVERFLOW)) { + // If we get notified about possibly missed events, reload the key store / trust store just to be sure. + shouldResetContext = true; + } else if (event.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY) + || event.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) { + Path eventFilePath = dirPath.resolve((Path) event.context()); + if (filePath.equals(eventFilePath)) { + shouldResetContext = true; + } + } + // Note: we don't care about delete events + if (shouldResetContext) { + LOG.debug( + "Attempting to reset default SSL context after receiving watch event: {} with context: {}", + event.kind(), + event.context()); + try { + this.resetDefaultSSLContextAndOptions(); + } catch (SSLContextException e) { + throw new RuntimeException(e); + } + } else { + LOG.debug( + "Ignoring watch event and keeping previous default SSL context. Event kind: {} with context: {}", + event.kind(), + event.context()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java new file mode 100644 index 0000000..6bbe698 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKConfig.java @@ -0,0 +1,281 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.util.VerifyingFileFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is a base class for the configurations of both client and server. + * It supports reading client configuration from both system properties and + * configuration file. A user can override any system property by calling + * {@link #setProperty(String, String)}. + * @since 3.5.2 + */ +public class ZKConfig { + + private static final Logger LOG = LoggerFactory.getLogger(ZKConfig.class); + + public static final String JUTE_MAXBUFFER = "jute.maxbuffer"; + + /** + * Path to a kinit binary: {@value}. Defaults to + * "/usr/bin/kinit" + */ + public static final String KINIT_COMMAND = "zookeeper.kinit"; + public static final String JGSS_NATIVE = "sun.security.jgss.native"; + + private final Map properties = new HashMap(); + + /** + * properties, which are common to both client and server, are initialized + * from system properties + */ + public ZKConfig() { + init(); + } + + /** + * @param configPath + * Configuration file path + * @throws ConfigException + * if failed to load configuration properties + */ + + public ZKConfig(String configPath) throws ConfigException { + this(new File(configPath)); + } + + /** + * + * @param configFile + * Configuration file + * @throws ConfigException + * if failed to load configuration properties + */ + public ZKConfig(File configFile) throws ConfigException { + this(); + addConfiguration(configFile); + } + + private void init() { + /** + * backward compatibility for all currently available client properties + */ + handleBackwardCompatibility(); + } + + /** + * Now onwards client code will use properties from this class but older + * clients still be setting properties through system properties. So to make + * this change backward compatible we should set old system properties in + * this configuration. + */ + protected void handleBackwardCompatibility() { + properties.put(JUTE_MAXBUFFER, System.getProperty(JUTE_MAXBUFFER)); + properties.put(KINIT_COMMAND, System.getProperty(KINIT_COMMAND)); + properties.put(JGSS_NATIVE, System.getProperty(JGSS_NATIVE)); + + try (ClientX509Util clientX509Util = new ClientX509Util()) { + putSSLProperties(clientX509Util); + properties.put(clientX509Util.getSslAuthProviderProperty(), System.getProperty(clientX509Util.getSslAuthProviderProperty())); + } + + try (X509Util x509Util = new QuorumX509Util()) { + putSSLProperties(x509Util); + } + } + + private void putSSLProperties(X509Util x509Util) { + properties.put(x509Util.getSslProtocolProperty(), System.getProperty(x509Util.getSslProtocolProperty())); + properties.put(x509Util.getSslEnabledProtocolsProperty(), System.getProperty(x509Util.getSslEnabledProtocolsProperty())); + properties.put(x509Util.getSslCipherSuitesProperty(), System.getProperty(x509Util.getSslCipherSuitesProperty())); + properties.put(x509Util.getSslKeystoreLocationProperty(), System.getProperty(x509Util.getSslKeystoreLocationProperty())); + properties.put(x509Util.getSslKeystorePasswdProperty(), System.getProperty(x509Util.getSslKeystorePasswdProperty())); + properties.put(x509Util.getSslKeystoreTypeProperty(), System.getProperty(x509Util.getSslKeystoreTypeProperty())); + properties.put(x509Util.getSslTruststoreLocationProperty(), System.getProperty(x509Util.getSslTruststoreLocationProperty())); + properties.put(x509Util.getSslTruststorePasswdProperty(), System.getProperty(x509Util.getSslTruststorePasswdProperty())); + properties.put(x509Util.getSslTruststoreTypeProperty(), System.getProperty(x509Util.getSslTruststoreTypeProperty())); + properties.put(x509Util.getSslContextSupplierClassProperty(), System.getProperty(x509Util.getSslContextSupplierClassProperty())); + properties.put(x509Util.getSslHostnameVerificationEnabledProperty(), System.getProperty(x509Util.getSslHostnameVerificationEnabledProperty())); + properties.put(x509Util.getSslCrlEnabledProperty(), System.getProperty(x509Util.getSslCrlEnabledProperty())); + properties.put(x509Util.getSslOcspEnabledProperty(), System.getProperty(x509Util.getSslOcspEnabledProperty())); + properties.put(x509Util.getSslClientAuthProperty(), System.getProperty(x509Util.getSslClientAuthProperty())); + properties.put(x509Util.getSslHandshakeDetectionTimeoutMillisProperty(), System.getProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty())); + } + + /** + * Get the property value + * + * @param key + * @return property value + */ + public String getProperty(String key) { + return properties.get(key); + } + + /** + * Get the property value, if it is null return default value + * + * @param key + * property key + * @param defaultValue + * @return property value or default value + */ + public String getProperty(String key, String defaultValue) { + String value = properties.get(key); + return (value == null) ? defaultValue : value; + } + + /** + * Return the value of "java.security.auth.login.config" system property + * + * @return value + */ + public String getJaasConfKey() { + return System.getProperty(Environment.JAAS_CONF_KEY); + } + + /** + * Maps the specified key to the specified value. + * key can not be null. If key is already mapped then the old + * value of the key is replaced by the specified + * value. + * + * @param key + * @param value + */ + public void setProperty(String key, String value) { + if (null == key) { + throw new IllegalArgumentException("property key is null."); + } + String oldValue = properties.put(key, value); + if (null != oldValue && !oldValue.equals(value)) { + LOG.debug("key {}'s value {} is replaced with new value {}", key, oldValue, value); + } + } + + /** + * Add a configuration resource. The properties form this configuration will + * overwrite corresponding already loaded property and system property + * + * @param configFile + * Configuration file. + */ + public void addConfiguration(File configFile) throws ConfigException { + LOG.info("Reading configuration from: {}", configFile.getAbsolutePath()); + try { + configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath() + .failForNonExistingPath() + .build()).validate(configFile); + Properties cfg = new Properties(); + FileInputStream in = new FileInputStream(configFile); + try { + cfg.load(in); + } finally { + in.close(); + } + parseProperties(cfg); + } catch (IOException | IllegalArgumentException e) { + LOG.error("Error while configuration from: {}", configFile.getAbsolutePath(), e); + throw new ConfigException("Error while processing " + configFile.getAbsolutePath(), e); + } + } + + /** + * Add a configuration resource. The properties form this configuration will + * overwrite corresponding already loaded property and system property + * + * @param configPath + * Configuration file path. + */ + public void addConfiguration(String configPath) throws ConfigException { + addConfiguration(new File(configPath)); + } + + private void parseProperties(Properties cfg) { + for (Entry entry : cfg.entrySet()) { + String key = entry.getKey().toString().trim(); + String value = entry.getValue().toString().trim(); + setProperty(key, value); + } + } + + /** + * Returns {@code true} if and only if the property named by the argument + * exists and is equal to the string {@code "true"}. + */ + public boolean getBoolean(String key) { + return getBoolean(key, false); + } + + /** + * Get the value of the key property as a boolean. Returns + * {@code true} if and only if the property named by the argument exists and is equal + * to the string {@code "true"}. If the property is not set, the provided + * defaultValue is returned. + * + * @param key + * property key. + * @param defaultValue + * default value. + * @return return property value as an boolean, or + * defaultValue + */ + public boolean getBoolean(String key, boolean defaultValue) { + String propertyValue = getProperty(key); + if (propertyValue == null) { + return defaultValue; + } else { + return Boolean.parseBoolean(propertyValue.trim()); + } + } + + /** + * Get the value of the key property as an int. If + * property is not set, the provided defaultValue is returned + * + * @param key + * property key. + * @param defaultValue + * default value. + * @throws NumberFormatException + * when the value is invalid + * @return return property value as an int, or + * defaultValue + */ + public int getInt(String key, int defaultValue) { + String value = getProperty(key); + if (value != null) { + return Integer.decode(value.trim()); + } + return defaultValue; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKHostnameVerifier.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKHostnameVerifier.java new file mode 100644 index 0000000..db5af19 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKHostnameVerifier.java @@ -0,0 +1,358 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.security.cert.Certificate; +import java.security.cert.CertificateParsingException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.regex.Pattern; +import javax.naming.InvalidNameException; +import javax.naming.NamingException; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.ldap.LdapName; +import javax.naming.ldap.Rdn; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLException; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSession; +import javax.security.auth.x500.X500Principal; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Note: copied from Apache httpclient with some modifications. We want host verification, but depending + * on the httpclient jar caused unexplained performance regressions (even when the code was not used). + */ +final class ZKHostnameVerifier implements HostnameVerifier { + + /** + * Note: copied from Apache httpclient with some minor modifications. We want host verification, but depending + * on the httpclient jar caused unexplained performance regressions (even when the code was not used). + */ + private static final class SubjectName { + + static final int DNS = 2; + static final int IP = 7; + + private final String value; + private final int type; + + static SubjectName IP(final String value) { + return new SubjectName(value, IP); + } + + static SubjectName DNS(final String value) { + return new SubjectName(value, DNS); + } + + SubjectName(final String value, final int type) { + if (type != DNS && type != IP) { + throw new IllegalArgumentException("Invalid type: " + type); + } + this.value = Objects.requireNonNull(value); + this.type = type; + } + + public int getType() { + return type; + } + + public String getValue() { + return value; + } + + @Override + public String toString() { + return value; + } + + } + + /** + * Note: copied from Apache httpclient. We want host verification, but depending on the + * httpclient jar caused unexplained performance regressions (even when the code was not used). + */ + private static class InetAddressUtils { + + private InetAddressUtils() { + } + + private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); + + private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); + + private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile("^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$"); + + static boolean isIPv4Address(final String input) { + return IPV4_PATTERN.matcher(input).matches(); + } + + static boolean isIPv6StdAddress(final String input) { + return IPV6_STD_PATTERN.matcher(input).matches(); + } + + static boolean isIPv6HexCompressedAddress(final String input) { + return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches(); + } + + static boolean isIPv6Address(final String input) { + return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input); + } + + } + + enum HostNameType { + + IPv4(7), + IPv6(7), + DNS(2); + + final int subjectType; + + HostNameType(final int subjectType) { + this.subjectType = subjectType; + } + + } + + private final Logger log = LoggerFactory.getLogger(ZKHostnameVerifier.class); + + @Override + public boolean verify(final String host, final SSLSession session) { + try { + final Certificate[] certs = session.getPeerCertificates(); + final X509Certificate x509 = (X509Certificate) certs[0]; + verify(host, x509); + return true; + } catch (final SSLException ex) { + log.debug("Unexpected exception", ex); + return false; + } + } + + void verify(final String host, final X509Certificate cert) throws SSLException { + final HostNameType hostType = determineHostFormat(host); + final List subjectAlts = getSubjectAltNames(cert); + if (subjectAlts != null && !subjectAlts.isEmpty()) { + switch (hostType) { + case IPv4: + matchIPAddress(host, subjectAlts); + break; + case IPv6: + matchIPv6Address(host, subjectAlts); + break; + default: + matchDNSName(host, subjectAlts); + } + } else { + // CN matching has been deprecated by rfc2818 and can be used + // as fallback only when no subjectAlts are available + final X500Principal subjectPrincipal = cert.getSubjectX500Principal(); + final String cn = extractCN(subjectPrincipal.getName(X500Principal.RFC2253)); + if (cn == null) { + throw new SSLException("Certificate subject for <" + + host + + "> doesn't contain " + + "a common name and does not have alternative names"); + } + matchCN(host, cn); + } + } + + private static void matchIPAddress(final String host, final List subjectAlts) throws SSLException { + for (int i = 0; i < subjectAlts.size(); i++) { + final SubjectName subjectAlt = subjectAlts.get(i); + if (subjectAlt.getType() == SubjectName.IP) { + if (host.equals(subjectAlt.getValue())) { + return; + } + } + } + throw new SSLPeerUnverifiedException("Certificate for <" + host + "> doesn't match any " + + "of the subject alternative names: " + subjectAlts); + } + + private static void matchIPv6Address(final String host, final List subjectAlts) throws SSLException { + final String normalisedHost = normaliseAddress(host); + for (int i = 0; i < subjectAlts.size(); i++) { + final SubjectName subjectAlt = subjectAlts.get(i); + if (subjectAlt.getType() == SubjectName.IP) { + final String normalizedSubjectAlt = normaliseAddress(subjectAlt.getValue()); + if (normalisedHost.equals(normalizedSubjectAlt)) { + return; + } + } + } + throw new SSLPeerUnverifiedException("Certificate for <" + + host + + "> doesn't match any " + + "of the subject alternative names: " + + subjectAlts); + } + + private static void matchDNSName(final String host, final List subjectAlts) throws SSLException { + final String normalizedHost = host.toLowerCase(Locale.ROOT); + for (int i = 0; i < subjectAlts.size(); i++) { + final SubjectName subjectAlt = subjectAlts.get(i); + if (subjectAlt.getType() == SubjectName.DNS) { + final String normalizedSubjectAlt = subjectAlt.getValue().toLowerCase(Locale.ROOT); + if (matchIdentityStrict(normalizedHost, normalizedSubjectAlt)) { + return; + } + } + } + throw new SSLPeerUnverifiedException("Certificate for <" + host + "> doesn't match any " + + "of the subject alternative names: " + subjectAlts); + } + + private static void matchCN(final String host, final String cn) throws SSLException { + final String normalizedHost = host.toLowerCase(Locale.ROOT); + final String normalizedCn = cn.toLowerCase(Locale.ROOT); + if (!matchIdentityStrict(normalizedHost, normalizedCn)) { + throw new SSLPeerUnverifiedException("Certificate for <" + host + "> doesn't match " + + "common name of the certificate subject: " + cn); + } + } + + private static boolean matchIdentity(final String host, final String identity, final boolean strict) { + // RFC 2818, 3.1. Server Identity + // "...Names may contain the wildcard + // character * which is considered to match any single domain name + // component or component fragment..." + // Based on this statement presuming only singular wildcard is legal + final int asteriskIdx = identity.indexOf('*'); + if (asteriskIdx != -1) { + final String prefix = identity.substring(0, asteriskIdx); + final String suffix = identity.substring(asteriskIdx + 1); + if (!prefix.isEmpty() && !host.startsWith(prefix)) { + return false; + } + if (!suffix.isEmpty() && !host.endsWith(suffix)) { + return false; + } + // Additional sanity checks on content selected by wildcard can be done here + if (strict) { + final String remainder = host.substring(prefix.length(), host.length() - suffix.length()); + return !remainder.contains("."); + } + return true; + } + return host.equalsIgnoreCase(identity); + } + + private static boolean matchIdentityStrict(final String host, final String identity) { + return matchIdentity(host, identity, true); + } + + private static String extractCN(final String subjectPrincipal) throws SSLException { + if (subjectPrincipal == null) { + return null; + } + try { + final LdapName subjectDN = new LdapName(subjectPrincipal); + final List rdns = subjectDN.getRdns(); + for (int i = rdns.size() - 1; i >= 0; i--) { + final Rdn rds = rdns.get(i); + final Attributes attributes = rds.toAttributes(); + final Attribute cn = attributes.get("cn"); + if (cn != null) { + try { + final Object value = cn.get(); + if (value != null) { + return value.toString(); + } + } catch (final NoSuchElementException ignore) { + // ignore exception + } catch (final NamingException ignore) { + // ignore exception + } + } + } + return null; + } catch (final InvalidNameException e) { + throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name"); + } + } + + private static HostNameType determineHostFormat(final String host) { + if (InetAddressUtils.isIPv4Address(host)) { + return HostNameType.IPv4; + } + String s = host; + if (s.startsWith("[") && s.endsWith("]")) { + s = host.substring(1, host.length() - 1); + } + if (InetAddressUtils.isIPv6Address(s)) { + return HostNameType.IPv6; + } + return HostNameType.DNS; + } + + private static List getSubjectAltNames(final X509Certificate cert) { + try { + final Collection> entries = cert.getSubjectAlternativeNames(); + if (entries == null) { + return Collections.emptyList(); + } + final List result = new ArrayList(); + for (List entry : entries) { + final Integer type = entry.size() >= 2 ? (Integer) entry.get(0) : null; + if (type != null) { + if (type == SubjectName.DNS || type == SubjectName.IP) { + final Object o = entry.get(1); + if (o instanceof String) { + result.add(new SubjectName((String) o, type)); + } else if (o instanceof byte[]) { + // TODO ASN.1 DER encoded form + } + } + } + } + return result; + } catch (final CertificateParsingException ignore) { + return Collections.emptyList(); + } + } + + /* + * Normalize IPv6 or DNS name. + */ + private static String normaliseAddress(final String hostname) { + if (hostname == null) { + return hostname; + } + try { + final InetAddress inetAddress = InetAddress.getByName(hostname); + return inetAddress.getHostAddress(); + } catch (final UnknownHostException unexpected) { // Should not happen, because we check for IPv6 address above + return hostname; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKTrustManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKTrustManager.java new file mode 100644 index 0000000..34bf4dc --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/common/ZKTrustManager.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.net.InetAddress; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLException; +import javax.net.ssl.X509ExtendedTrustManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A custom TrustManager that supports hostname verification via org.apache.http.conn.ssl.DefaultHostnameVerifier. + * + * We attempt to perform verification using just the IP address first and if that fails will attempt to perform a + * reverse DNS lookup and verify using the hostname. + */ +public class ZKTrustManager extends X509ExtendedTrustManager { + + private static final Logger LOG = LoggerFactory.getLogger(ZKTrustManager.class); + + private X509ExtendedTrustManager x509ExtendedTrustManager; + private boolean serverHostnameVerificationEnabled; + private boolean clientHostnameVerificationEnabled; + + private ZKHostnameVerifier hostnameVerifier; + + /** + * Instantiate a new ZKTrustManager. + * + * @param x509ExtendedTrustManager The trustmanager to use for checkClientTrusted/checkServerTrusted logic + * @param serverHostnameVerificationEnabled If true, this TrustManager should verify hostnames of servers that this + * instance connects to. + * @param clientHostnameVerificationEnabled If true, the hostname of a client connecting to this machine will be + * verified. + */ + ZKTrustManager( + X509ExtendedTrustManager x509ExtendedTrustManager, + boolean serverHostnameVerificationEnabled, + boolean clientHostnameVerificationEnabled) { + this.x509ExtendedTrustManager = x509ExtendedTrustManager; + this.serverHostnameVerificationEnabled = serverHostnameVerificationEnabled; + this.clientHostnameVerificationEnabled = clientHostnameVerificationEnabled; + hostnameVerifier = new ZKHostnameVerifier(); + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return x509ExtendedTrustManager.getAcceptedIssuers(); + } + + @Override + public void checkClientTrusted( + X509Certificate[] chain, + String authType, + Socket socket) throws CertificateException { + x509ExtendedTrustManager.checkClientTrusted(chain, authType, socket); + if (clientHostnameVerificationEnabled) { + performHostVerification(socket.getInetAddress(), chain[0]); + } + } + + @Override + public void checkServerTrusted( + X509Certificate[] chain, + String authType, + Socket socket) throws CertificateException { + x509ExtendedTrustManager.checkServerTrusted(chain, authType, socket); + if (serverHostnameVerificationEnabled) { + performHostVerification(socket.getInetAddress(), chain[0]); + } + } + + @Override + public void checkClientTrusted( + X509Certificate[] chain, + String authType, + SSLEngine engine) throws CertificateException { + x509ExtendedTrustManager.checkClientTrusted(chain, authType, engine); + if (clientHostnameVerificationEnabled) { + try { + performHostVerification(InetAddress.getByName(engine.getPeerHost()), chain[0]); + } catch (UnknownHostException e) { + throw new CertificateException("Failed to verify host", e); + } + } + } + + @Override + public void checkServerTrusted( + X509Certificate[] chain, + String authType, + SSLEngine engine + ) throws CertificateException { + x509ExtendedTrustManager.checkServerTrusted(chain, authType, engine); + if (serverHostnameVerificationEnabled) { + try { + performHostVerification(InetAddress.getByName(engine.getPeerHost()), chain[0]); + } catch (UnknownHostException e) { + throw new CertificateException("Failed to verify host", e); + } + } + } + + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + x509ExtendedTrustManager.checkClientTrusted(chain, authType); + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + x509ExtendedTrustManager.checkServerTrusted(chain, authType); + } + + /** + * Compares peer's hostname with the one stored in the provided client certificate. Performs verification + * with the help of provided HostnameVerifier. + * + * @param inetAddress Peer's inet address. + * @param certificate Peer's certificate + * @throws CertificateException Thrown if the provided certificate doesn't match the peer hostname. + */ + private void performHostVerification( + InetAddress inetAddress, + X509Certificate certificate + ) throws CertificateException { + String hostAddress = ""; + String hostName = ""; + try { + hostAddress = inetAddress.getHostAddress(); + hostnameVerifier.verify(hostAddress, certificate); + } catch (SSLException addressVerificationException) { + try { + LOG.debug( + "Failed to verify host address: {} attempting to verify host name with reverse dns lookup", + hostAddress, + addressVerificationException); + hostName = inetAddress.getHostName(); + hostnameVerifier.verify(hostName, certificate); + } catch (SSLException hostnameVerificationException) { + LOG.error("Failed to verify host address: {}", hostAddress, addressVerificationException); + LOG.error("Failed to verify hostname: {}", hostName, hostnameVerificationException); + throw new CertificateException("Failed to verify both host address and host name", hostnameVerificationException); + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/MBeanRegistry.java b/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/MBeanRegistry.java new file mode 100644 index 0000000..5bfa8bb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/MBeanRegistry.java @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.jmx; + +import java.lang.management.ManagementFactory; +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import javax.management.JMException; +import javax.management.MBeanServer; +import javax.management.MBeanServerFactory; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class provides a unified interface for registering/unregistering of + * zookeeper MBeans with the platform MBean server. It builds a hierarchy of MBeans + * where each MBean represented by a filesystem-like path. Eventually, this hierarchy + * will be stored in the zookeeper data tree instance as a virtual data tree. + */ +public class MBeanRegistry { + + public static final String DOMAIN = "org.apache.ZooKeeperService"; + + private static final Logger LOG = LoggerFactory.getLogger(MBeanRegistry.class); + private static volatile MBeanRegistry instance = new MBeanRegistry(); + + private final Object LOCK = new Object(); + + private Map mapBean2Path = new ConcurrentHashMap(); + + private MBeanServer mBeanServer; + + /** + * Useful for unit tests. Change the MBeanRegistry instance + * + * @param instance new instance + */ + public static void setInstance(MBeanRegistry instance) { + MBeanRegistry.instance = instance; + } + + public static MBeanRegistry getInstance() { + return instance; + } + + public MBeanRegistry() { + try { + mBeanServer = ManagementFactory.getPlatformMBeanServer(); + } catch (Error e) { + // Account for running within IKVM and create a new MBeanServer + // if the PlatformMBeanServer does not exist. + mBeanServer = MBeanServerFactory.createMBeanServer(); + } + } + + /** + * Return the underlying MBeanServer that is being + * used to register MBean's. The returned MBeanServer + * may be a new empty MBeanServer if running through IKVM. + */ + public MBeanServer getPlatformMBeanServer() { + return mBeanServer; + } + + /** + * Registers a new MBean with the platform MBean server. + * @param bean the bean being registered + * @param parent if not null, the new bean will be registered as a child + * node of this parent. + */ + public void register(ZKMBeanInfo bean, ZKMBeanInfo parent) throws JMException { + assert bean != null; + String path = null; + if (parent != null) { + path = mapBean2Path.get(parent); + assert path != null; + } + path = makeFullPath(path, parent); + if (bean.isHidden()) { + return; + } + ObjectName oname = makeObjectName(path, bean); + try { + synchronized (LOCK) { + mBeanServer.registerMBean(bean, oname); + mapBean2Path.put(bean, path); + } + } catch (JMException e) { + LOG.warn("Failed to register MBean {}", bean.getName()); + throw e; + } + } + + /** + * Unregister the MBean identified by the path. + * @param path + * @param bean + */ + private void unregister(String path, ZKMBeanInfo bean) throws JMException { + if (path == null) { + return; + } + if (!bean.isHidden()) { + final ObjectName objName = makeObjectName(path, bean); + LOG.debug("Unregister MBean [{}]", objName); + synchronized (LOCK) { + mBeanServer.unregisterMBean(objName); + } + } + } + + /** + * @return a {@link Collection} with the {@link ZKMBeanInfo} instances not + * unregistered. Mainly for testing purposes. + */ + public Set getRegisteredBeans() { + return new HashSet(mapBean2Path.keySet()); + } + + /** + * Unregister MBean. + * @param bean + */ + public void unregister(ZKMBeanInfo bean) { + if (bean == null) { + return; + } + String path = mapBean2Path.remove(bean); + try { + unregister(path, bean); + } catch (JMException e) { + LOG.warn("Error during unregister of [{}]", bean.getName(), e); + } catch (Throwable t) { + LOG.error("Unexpected exception during unregister of [{}]. It should be reviewed and fixed.", bean.getName(), t); + } + } + + /** + * Generate a filesystem-like path. + * @param prefix path prefix + * @param name path elements + * @return absolute path + */ + public String makeFullPath(String prefix, String... name) { + StringBuilder sb = new StringBuilder(prefix == null ? "/" : (prefix.equals("/") ? prefix : prefix + "/")); + boolean first = true; + for (String s : name) { + if (s == null) { + continue; + } + if (!first) { + sb.append("/"); + } else { + first = false; + } + sb.append(s); + } + return sb.toString(); + } + + protected String makeFullPath(String prefix, ZKMBeanInfo bean) { + return makeFullPath(prefix, bean == null ? null : bean.getName()); + } + + /** + * This takes a path, such as /a/b/c, and converts it to + * name0=a,name1=b,name2=c + */ + private int tokenize(StringBuilder sb, String path, int index) { + String[] tokens = path.split("/"); + for (String s : tokens) { + if (s.length() == 0) { + continue; + } + sb.append("name").append(index++).append("=").append(s).append(","); + } + return index; + } + /** + * Builds an MBean path and creates an ObjectName instance using the path. + * @param path MBean path + * @param bean the MBean instance + * @return ObjectName to be registered with the platform MBean server + */ + protected ObjectName makeObjectName(String path, ZKMBeanInfo bean) throws MalformedObjectNameException { + if (path == null) { + return null; + } + StringBuilder beanName = new StringBuilder(DOMAIN + ":"); + int counter = 0; + counter = tokenize(beanName, path, counter); + tokenize(beanName, bean.getName(), counter); + beanName.deleteCharAt(beanName.length() - 1); + try { + return new ObjectName(beanName.toString()); + } catch (MalformedObjectNameException e) { + LOG.warn("Invalid name \"{}\" for class {}", beanName, bean.getClass()); + throw e; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/ManagedUtil.java b/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/ManagedUtil.java new file mode 100644 index 0000000..633cb17 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/ManagedUtil.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.jmx; + +import java.util.Enumeration; +import javax.management.JMException; +import javax.management.MBeanServer; +import javax.management.ObjectName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Shared utilities + */ +public class ManagedUtil { + + private static final Logger LOG = LoggerFactory.getLogger(ManagedUtil.class); + + private static boolean isLog4jJmxEnabled() { + boolean enabled = false; + + if (Boolean.getBoolean("zookeeper.jmx.log4j.disable")) { + LOG.info("Log4j 1.2 jmx support is disabled by property."); + } else { + try { + Class.forName("org.apache.log4j.jmx.HierarchyDynamicMBean"); + enabled = true; + LOG.info("Log4j 1.2 jmx support found and enabled."); + } catch (ClassNotFoundException e) { + LOG.info("Log4j 1.2 jmx support not found; jmx disabled."); + } + } + + return enabled; + } + + /** + * Register the log4j JMX mbeans. Set system property + * "zookeeper.jmx.log4j.disable" to true to disable registration. + * @see http://logging.apache.org/log4j/1.2/apidocs/index.html?org/apache/log4j/jmx/package-summary.html + * @throws JMException if registration fails + */ + @SuppressWarnings("rawtypes") + public static void registerLog4jMBeans() throws JMException { + if (isLog4jJmxEnabled()) { + LOG.debug("registerLog4jMBeans()"); + MBeanServer mbs = MBeanRegistry.getInstance().getPlatformMBeanServer(); + + try { + // Create and Register the top level Log4J MBean + // org.apache.log4j.jmx.HierarchyDynamicMBean hdm = new org.apache.log4j.jmx.HierarchyDynamicMBean(); + Object hdm = Class.forName("org.apache.log4j.jmx.HierarchyDynamicMBean").getConstructor().newInstance(); + + String mbean = System.getProperty("zookeeper.jmx.log4j.mbean", "log4j:hierarchy=default"); + ObjectName mbo = new ObjectName(mbean); + mbs.registerMBean(hdm, mbo); + + // Add the root logger to the Hierarchy MBean + // org.apache.log4j.Logger rootLogger = + // org.apache.log4j.Logger.getRootLogger(); + Object rootLogger = Class.forName("org.apache.log4j.Logger") + .getMethod("getRootLogger", (Class[]) null) + .invoke(null, (Object[]) null); + + // hdm.addLoggerMBean(rootLogger.getName()); + Object rootLoggerName = rootLogger.getClass() + .getMethod("getName", (Class[]) null) + .invoke(rootLogger, (Object[]) null); + hdm.getClass().getMethod("addLoggerMBean", String.class).invoke(hdm, rootLoggerName); + + // Get each logger from the Log4J Repository and add it to the + // Hierarchy MBean created above. + // org.apache.log4j.spi.LoggerRepository r = + // org.apache.log4j.LogManager.getLoggerRepository(); + Object r = Class.forName("org.apache.log4j.LogManager") + .getMethod("getLoggerRepository", (Class[]) null) + .invoke(null, (Object[]) null); + + // Enumeration enumer = r.getCurrentLoggers(); + Enumeration enumer = (Enumeration) r.getClass() + .getMethod("getCurrentLoggers", (Class[]) null) + .invoke(r, (Object[]) null); + + while (enumer.hasMoreElements()) { + Object logger = enumer.nextElement(); + // hdm.addLoggerMBean(logger.getName()); + Object loggerName = logger.getClass() + .getMethod("getName", (Class[]) null) + .invoke(logger, (Object[]) null); + hdm.getClass().getMethod("addLoggerMBean", String.class).invoke(hdm, loggerName); + } + } catch (Exception e) { + LOG.error("Problems while registering log4j 1.2 jmx beans!", e); + throw new JMException(e.toString()); + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/ZKMBeanInfo.java b/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/ZKMBeanInfo.java new file mode 100644 index 0000000..f33fa3f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/jmx/ZKMBeanInfo.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.jmx; + +/** + * Zookeeper MBean info interface. MBeanRegistry uses the interface to generate + * JMX object name. + */ +public interface ZKMBeanInfo { + + /** + * @return a string identifying the MBean + */ + String getName(); + /** + * If isHidden returns true, the MBean won't be registered with MBean server, + * and thus won't be available for management tools. Used for grouping MBeans. + * @return true if the MBean is hidden. + */ + boolean isHidden(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Counter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Counter.java new file mode 100644 index 0000000..8a7f819 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Counter.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +/** + * A counter refers to a value which can only increase. + * Usually the value is reset when the process starts. + */ +public interface Counter { + + /** + * Increment the value by one. + *

      This method is thread safe, The MetricsProvider will take care of synchronization.

      + */ + default void inc() { + add(1); + } + + /** + * Increment the value by a given amount. + *

      This method is thread safe, The MetricsProvider will take care of synchronization.

      + * + * @param delta amount to increment, this cannot be a negative number. + */ + void add(long delta); + + /** + * Get the current value held by the counter. + *

      This method is thread safe, The MetricsProvider will take care of synchronization.

      + * + * @return the current value + */ + long get(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Gauge.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Gauge.java new file mode 100644 index 0000000..67ed84e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Gauge.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +/** + * A Gauge is an application provided object which will be called by the framework in order to sample the value + * of an integer value. + */ +public interface Gauge { + + /** + * Returns the current value associated with this gauge. + * The MetricsProvider will call this callback without taking care of synchronization, it is up to the application + * to handle thread safety. + * + * @return the current value for the gauge + */ + Number get(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsContext.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsContext.java new file mode 100644 index 0000000..3ea8f8c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsContext.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +/** + * A MetricsContext is like a namespace for metrics. Each component/submodule + * will have its own MetricsContext. + *

      + * In some cases it is possible to have a separate MetricsContext for each + * instance of a component, for instance on the server side a possible usecase + * it to gather metrics for every other peer. + *

      + *

      + * Contexts are organized in a hierarchy. + *

      + * + */ +public interface MetricsContext { + + /** + * Returns a sub context. + * + * @param name the name of the subcontext + * + * @return a new metrics context. + */ + MetricsContext getContext(String name); + + /** + * Returns a counter. + * + * @param name + * @return the counter identified by name in this context. + */ + Counter getCounter(String name); + + /** + * Registers an user provided {@link Gauge} which will be called by the + * MetricsProvider in order to sample an integer value. + * If another Gauge was already registered the new one will + * take its place. + * Registering a null callback is not allowed. + * + * @param name unique name of the Gauge in this context + * @param gauge the implementation of the Gauge + * + */ + void registerGauge(String name, Gauge gauge); + + /** + * Unregisters the user provided {@link Gauge} bound to the given name. + * + * @param name unique name of the Gauge in this context + * + */ + void unregisterGauge(String name); + + enum DetailLevel { + /** + * The returned Summary is expected to track only simple aggregated + * values, like min/max/avg + */ + BASIC, + /** + * It is expected that the returned Summary performs expensive + * aggregations, like percentiles. + */ + ADVANCED + } + + /** + * Returns a summary. + * + * @param name + * @param detailLevel + * @return the summary identified by name in this context. + */ + Summary getSummary(String name, DetailLevel detailLevel); + + /** + * Returns a set of summaries. + * + * @param name + * @param detailLevel + * @return the summary identified by name in this context. + */ + SummarySet getSummarySet(String name, DetailLevel detailLevel); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsProvider.java new file mode 100644 index 0000000..1cc2833 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsProvider.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +import java.util.Properties; +import java.util.function.BiConsumer; + +/** + * A MetricsProvider is a system which collects Metrics and publishes current values to external facilities. + * + * The system will create an instance of the configured class using the default constructor, which must be public.
      + * After the instantiation of the provider, the system will call {@link #configure(java.util.Properties) } in order to provide configuration, + * and then when the system is ready to work it will call {@link #start() }. + *
      + * Providers can be used both on ZooKeeper servers and on ZooKeeper clients. + */ +public interface MetricsProvider { + + /** + * Configure the provider. + * + * @param configuration the configuration. + * + * @throws MetricsProviderLifeCycleException in case of invalid configuration. + */ + void configure(Properties configuration) throws MetricsProviderLifeCycleException; + + /** + * Start the provider. + * For instance such method will start a network endpoint. + * + * @throws MetricsProviderLifeCycleException in case of failure + */ + void start() throws MetricsProviderLifeCycleException; + + /** + * Provides access to the root context. + * + * @return the root context + */ + MetricsContext getRootContext(); + + /** + * Releases resources held by the provider.
      + * This method must not throw exceptions.
      + * This method can be called more than once. + */ + void stop(); + + /** + * Dumps all metrics as a key-value pair. + * This method will be used in legacy monitor command. + * @param sink the receiver of all of the current values. + */ + void dump(BiConsumer sink); + + /** + * Reset all values. + * This method is optional and can be noop, depending + * on the underlying implementation. + */ + void resetAllValues(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsProviderLifeCycleException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsProviderLifeCycleException.java new file mode 100644 index 0000000..24a88ef --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/MetricsProviderLifeCycleException.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +/** + * A generic exception thrown during the licecycle of a MetricsProvider. + *

      These exception will prevent the system from booting.

      + *

      Normally these exception will be ignored during shutdown.

      + */ +public class MetricsProviderLifeCycleException extends Exception { + + private static final long serialVersionUID = 1L; + + public MetricsProviderLifeCycleException() { + } + + public MetricsProviderLifeCycleException(String message) { + super(message); + } + + public MetricsProviderLifeCycleException(String message, Throwable cause) { + super(message, cause); + } + + public MetricsProviderLifeCycleException(Throwable cause) { + super(cause); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Summary.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Summary.java new file mode 100644 index 0000000..f8280da --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/Summary.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +/** + * Summaries track the size and number of events. + * They are able to publish minumum, maximum, average values, depending on the capabilities of the MetricsProvider. + */ +public interface Summary { + + /** + * Register a value. + *

      This method is thread safe, The MetricsProvider will take care of synchronization.

      + * + * @param value current value + */ + void add(long value); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/SummarySet.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/SummarySet.java new file mode 100644 index 0000000..8386d18 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/SummarySet.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +/** + * Summaries track the size and number of events. + * They are able to publish minumum, maximum, average values, depending on the capabilities of the MetricsProvider. + * A SummarySet is a set of {@link Summary}. + */ +public interface SummarySet { + + /** + * Register a value. + *

      This method is thread safe, The MetricsProvider will take care of synchronization.

      + * + * @param key the key to access the Summary for the given key + * @param value current value + */ + void add(String key, long value); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java new file mode 100644 index 0000000..85f86db --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.impl; + +import java.util.Objects; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.BiConsumer; +import org.apache.zookeeper.metrics.Counter; +import org.apache.zookeeper.metrics.Gauge; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; +import org.apache.zookeeper.metrics.Summary; +import org.apache.zookeeper.metrics.SummarySet; +import org.apache.zookeeper.server.metric.AvgMinMaxCounter; +import org.apache.zookeeper.server.metric.AvgMinMaxCounterSet; +import org.apache.zookeeper.server.metric.AvgMinMaxPercentileCounter; +import org.apache.zookeeper.server.metric.AvgMinMaxPercentileCounterSet; +import org.apache.zookeeper.server.metric.SimpleCounter; + +/** + * Default implementation of {@link MetricsProvider}.
      + * It does not implement a real hierarchy of contexts, but metrics are flattened + * in a single namespace.
      + * It is mostly useful to make the legacy 4 letter words interface work as + * expected. + */ +public class DefaultMetricsProvider implements MetricsProvider { + + private final DefaultMetricsContext rootMetricsContext = new DefaultMetricsContext(); + + @Override + public void configure(Properties configuration) throws MetricsProviderLifeCycleException { + } + + @Override + public void start() throws MetricsProviderLifeCycleException { + } + + @Override + public MetricsContext getRootContext() { + return rootMetricsContext; + } + + @Override + public void stop() { + // release all references to external objects + rootMetricsContext.gauges.clear(); + } + + @Override + public void dump(BiConsumer sink) { + rootMetricsContext.dump(sink); + } + + @Override + public void resetAllValues() { + rootMetricsContext.reset(); + } + + private static final class DefaultMetricsContext implements MetricsContext { + + private final ConcurrentMap gauges = new ConcurrentHashMap<>(); + private final ConcurrentMap counters = new ConcurrentHashMap<>(); + private final ConcurrentMap basicSummaries = new ConcurrentHashMap<>(); + private final ConcurrentMap summaries = new ConcurrentHashMap<>(); + private final ConcurrentMap basicSummarySets = new ConcurrentHashMap<>(); + private final ConcurrentMap summarySets = new ConcurrentHashMap<>(); + + @Override + public MetricsContext getContext(String name) { + // no hierarchy yet + return this; + } + + @Override + public Counter getCounter(String name) { + return counters.computeIfAbsent(name, (n) -> { + return new SimpleCounter(n); + }); + } + + @Override + public void registerGauge(String name, Gauge gauge) { + Objects.requireNonNull(gauge, "Cannot register a null Gauge for " + name); + gauges.put(name, gauge); + } + + @Override + public void unregisterGauge(String name) { + gauges.remove(name); + } + + @Override + public Summary getSummary(String name, DetailLevel detailLevel) { + if (detailLevel == DetailLevel.BASIC) { + return basicSummaries.computeIfAbsent(name, (n) -> { + if (summaries.containsKey(n)) { + throw new IllegalArgumentException("Already registered a non basic summary as " + n); + } + return new AvgMinMaxCounter(name); + }); + } else { + return summaries.computeIfAbsent(name, (n) -> { + if (basicSummaries.containsKey(n)) { + throw new IllegalArgumentException("Already registered a basic summary as " + n); + } + return new AvgMinMaxPercentileCounter(name); + }); + } + } + + @Override + public SummarySet getSummarySet(String name, DetailLevel detailLevel) { + if (detailLevel == DetailLevel.BASIC) { + return basicSummarySets.computeIfAbsent(name, (n) -> { + if (summarySets.containsKey(n)) { + throw new IllegalArgumentException("Already registered a non basic summary set as " + n); + } + return new AvgMinMaxCounterSet(name); + }); + } else { + return summarySets.computeIfAbsent(name, (n) -> { + if (basicSummarySets.containsKey(n)) { + throw new IllegalArgumentException("Already registered a basic summary set as " + n); + } + return new AvgMinMaxPercentileCounterSet(name); + }); + } + } + + void dump(BiConsumer sink) { + gauges.forEach((name, metric) -> { + Number value = metric.get(); + if (value != null) { + sink.accept(name, value); + } + }); + counters.values().forEach(metric -> { + metric.values().forEach(sink); + }); + basicSummaries.values().forEach(metric -> { + metric.values().forEach(sink); + }); + summaries.values().forEach(metric -> { + metric.values().forEach(sink); + }); + basicSummarySets.values().forEach(metric -> { + metric.values().forEach(sink); + }); + summarySets.values().forEach(metric -> { + metric.values().forEach(sink); + }); + } + + void reset() { + counters.values().forEach(metric -> { + metric.reset(); + }); + basicSummaries.values().forEach(metric -> { + metric.reset(); + }); + summaries.values().forEach(metric -> { + metric.reset(); + }); + basicSummarySets.values().forEach(metric -> { + metric.reset(); + }); + summarySets.values().forEach(metric -> { + metric.reset(); + }); + // no need to reset gauges + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/MetricsProviderBootstrap.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/MetricsProviderBootstrap.java new file mode 100644 index 0000000..b873c83 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/MetricsProviderBootstrap.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.impl; + +import java.lang.reflect.InvocationTargetException; +import java.util.Properties; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility for bootstrap process of MetricsProviders + */ +public abstract class MetricsProviderBootstrap { + + private static final Logger LOG = LoggerFactory.getLogger(MetricsProviderBootstrap.class); + + public static MetricsProvider startMetricsProvider( + String metricsProviderClassName, + Properties configuration) throws MetricsProviderLifeCycleException { + try { + Class clazz = Class.forName( + metricsProviderClassName, + true, + Thread.currentThread().getContextClassLoader()); + MetricsProvider metricsProvider = (MetricsProvider) clazz.getConstructor().newInstance(); + metricsProvider.configure(configuration); + metricsProvider.start(); + return metricsProvider; + } catch (ClassNotFoundException + | IllegalAccessException + | InvocationTargetException + | NoSuchMethodException + | InstantiationException error) { + LOG.error("Cannot boot MetricsProvider {}", metricsProviderClassName, error); + throw new MetricsProviderLifeCycleException("Cannot boot MetricsProvider " + metricsProviderClassName, error); + } catch (MetricsProviderLifeCycleException error) { + LOG.error("Cannot boot MetricsProvider {}", metricsProviderClassName, error); + throw error; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java new file mode 100644 index 0000000..30a1079 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.impl; + +import java.util.Properties; +import java.util.function.BiConsumer; +import org.apache.zookeeper.metrics.Counter; +import org.apache.zookeeper.metrics.Gauge; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; +import org.apache.zookeeper.metrics.Summary; +import org.apache.zookeeper.metrics.SummarySet; + +/** + * This is a dummy MetricsProvider which does nothing. + */ +public class NullMetricsProvider implements MetricsProvider { + + /** + * Instance of NullMetricsProvider useful for tests. + */ + public static final MetricsProvider INSTANCE = new NullMetricsProvider(); + + @Override + public void configure(Properties configuration) throws MetricsProviderLifeCycleException { + } + + @Override + public void start() throws MetricsProviderLifeCycleException { + } + + @Override + public MetricsContext getRootContext() { + return NullMetricsContext.INSTANCE; + } + + @Override + public void dump(BiConsumer sink) { + } + + @Override + public void resetAllValues() { + } + + @Override + public void stop() { + } + + public static final class NullMetricsContext implements MetricsContext { + + public static final NullMetricsContext INSTANCE = new NullMetricsContext(); + + @Override + public MetricsContext getContext(String name) { + return INSTANCE; + } + + @Override + public Counter getCounter(String name) { + return NullCounter.INSTANCE; + } + + @Override + public void registerGauge(String name, Gauge gauge) { + } + + @Override + public void unregisterGauge(String name) { + } + + @Override + public Summary getSummary(String name, DetailLevel detailLevel) { + return NullSummary.INSTANCE; + } + + @Override + public SummarySet getSummarySet(String name, DetailLevel detailLevel) { + return NullSummarySet.INSTANCE; + } + + } + + private static final class NullCounter implements Counter { + + private static final NullCounter INSTANCE = new NullCounter(); + + @Override + public void add(long delta) { + } + + @Override + public long get() { + return 0; + } + + } + + private static final class NullSummary implements Summary { + + private static final NullSummary INSTANCE = new NullSummary(); + + @Override + public void add(long value) { + } + + } + + private static final class NullSummarySet implements SummarySet { + + private static final NullSummarySet INSTANCE = new NullSummarySet(); + + @Override + public void add(String key, long value) { + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/BlueThrottle.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/BlueThrottle.java new file mode 100644 index 0000000..90be270 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/BlueThrottle.java @@ -0,0 +1,366 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.Random; +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implements a token-bucket based rate limiting mechanism with optional + * probabilistic dropping inspired by the BLUE queue management algorithm [1]. + * + * The throttle provides the {@link #checkLimit(int)} method which provides + * a binary yes/no decision. + * + * The core token bucket algorithm starts with an initial set of tokens based + * on the maxTokens setting. Tokens are dispensed each + * {@link #checkLimit(int)} call, which fails if there are not enough tokens to + * satisfy a given request. + * + * The token bucket refills over time, providing fillCount tokens + * every fillTime milliseconds, capping at maxTokens. + * + * This design allows the throttle to allow short bursts to pass, while still + * capping the total number of requests per time interval. + * + * One issue with a pure token bucket approach for something like request or + * connection throttling is that the wall clock arrival time of requests affects + * the probability of a request being allowed to pass or not. Under constant + * load this can lead to request starvation for requests that constantly arrive + * later than the majority. + * + * In an attempt to combat this, this throttle can also provide probabilistic + * dropping. This is enabled anytime freezeTime is set to a value + * other than -1. + * + * The probabilistic algorithm starts with an initial drop probability of 0, and + * adjusts this probability roughly every freezeTime milliseconds. + * The first request after freezeTime, the algorithm checks the + * token bucket. If the token bucket is empty, the drop probability is increased + * by dropIncrease up to a maximum of 1. Otherwise, if + * the bucket has a token deficit less than decreasePoint * maxTokens, + * the probability is decreased by dropDecrease. + * + * Given a call to {@link #checkLimit(int)}, requests are first dropped randomly + * based on the current drop probability, and only surviving requests are then + * checked against the token bucket. + * + * When under constant load, the probabilistic algorithm will adapt to a drop + * frequency that should keep requests within the token limit. When load drops, + * the drop probability will decrease, eventually returning to zero if possible. + * + * [1] "BLUE: A New Class of Active Queue Management Algorithms" + **/ + +public class BlueThrottle { + private static final Logger LOG = LoggerFactory.getLogger(BlueThrottle.class); + + private int maxTokens; + private int fillTime; + private int fillCount; + private int tokens; + private long lastTime; + + private int freezeTime; + private long lastFreeze; + private double dropIncrease; + private double dropDecrease; + private double decreasePoint; + private double drop; + + Random rng; + + public static final String CONNECTION_THROTTLE_TOKENS = "zookeeper.connection_throttle_tokens"; + private static final int DEFAULT_CONNECTION_THROTTLE_TOKENS; + + public static final String CONNECTION_THROTTLE_FILL_TIME = "zookeeper.connection_throttle_fill_time"; + private static final int DEFAULT_CONNECTION_THROTTLE_FILL_TIME; + + public static final String CONNECTION_THROTTLE_FILL_COUNT = "zookeeper.connection_throttle_fill_count"; + private static final int DEFAULT_CONNECTION_THROTTLE_FILL_COUNT; + + public static final String CONNECTION_THROTTLE_FREEZE_TIME = "zookeeper.connection_throttle_freeze_time"; + private static final int DEFAULT_CONNECTION_THROTTLE_FREEZE_TIME; + + public static final String CONNECTION_THROTTLE_DROP_INCREASE = "zookeeper.connection_throttle_drop_increase"; + private static final double DEFAULT_CONNECTION_THROTTLE_DROP_INCREASE; + + public static final String CONNECTION_THROTTLE_DROP_DECREASE = "zookeeper.connection_throttle_drop_decrease"; + private static final double DEFAULT_CONNECTION_THROTTLE_DROP_DECREASE; + + public static final String CONNECTION_THROTTLE_DECREASE_RATIO = "zookeeper.connection_throttle_decrease_ratio"; + private static final double DEFAULT_CONNECTION_THROTTLE_DECREASE_RATIO; + + public static final String WEIGHED_CONNECTION_THROTTLE = "zookeeper.connection_throttle_weight_enabled"; + private static boolean connectionWeightEnabled; + + public static final String GLOBAL_SESSION_WEIGHT = "zookeeper.connection_throttle_global_session_weight"; + private static final int DEFAULT_GLOBAL_SESSION_WEIGHT; + + public static final String LOCAL_SESSION_WEIGHT = "zookeeper.connection_throttle_local_session_weight"; + private static final int DEFAULT_LOCAL_SESSION_WEIGHT; + + public static final String RENEW_SESSION_WEIGHT = "zookeeper.connection_throttle_renew_session_weight"; + private static final int DEFAULT_RENEW_SESSION_WEIGHT; + + // for unit tests only + protected static void setConnectionWeightEnabled(boolean enabled) { + connectionWeightEnabled = enabled; + logWeighedThrottlingSetting(); + } + + private static void logWeighedThrottlingSetting() { + if (connectionWeightEnabled) { + LOG.info("Weighed connection throttling is enabled. " + + "But it will only be effective if connection throttling is enabled"); + LOG.info( + "The weights for different session types are: global {} renew {} local {}", + DEFAULT_GLOBAL_SESSION_WEIGHT, + DEFAULT_RENEW_SESSION_WEIGHT, + DEFAULT_LOCAL_SESSION_WEIGHT + ); + } else { + LOG.info("Weighed connection throttling is disabled"); + } + } + + static { + int tokens = Integer.getInteger(CONNECTION_THROTTLE_TOKENS, 0); + int fillCount = Integer.getInteger(CONNECTION_THROTTLE_FILL_COUNT, 1); + + connectionWeightEnabled = Boolean.getBoolean(WEIGHED_CONNECTION_THROTTLE); + + // if not specified, the weights for a global session, a local session, and a renew session + // are 3, 1, 2 respectively. The weight for a global session is 3 because in our connection benchmarking, + // the throughput of global sessions is about one third of that of local sessions. Renewing a session + // requires is more expensive than establishing a local session and cheaper than creating a global session so + // its default weight is set to 2. + int globalWeight = Integer.getInteger(GLOBAL_SESSION_WEIGHT, 3); + int localWeight = Integer.getInteger(LOCAL_SESSION_WEIGHT, 1); + int renewWeight = Integer.getInteger(RENEW_SESSION_WEIGHT, 2); + + if (globalWeight <= 0) { + LOG.warn("Invalid global session weight {}. It should be larger than 0", globalWeight); + DEFAULT_GLOBAL_SESSION_WEIGHT = 3; + } else if (globalWeight < localWeight) { + LOG.warn( + "The global session weight {} is less than the local session weight {}. Use the local session weight.", + globalWeight, + localWeight); + DEFAULT_GLOBAL_SESSION_WEIGHT = localWeight; + } else { + DEFAULT_GLOBAL_SESSION_WEIGHT = globalWeight; + } + + if (localWeight <= 0) { + LOG.warn("Invalid local session weight {}. It should be larger than 0", localWeight); + DEFAULT_LOCAL_SESSION_WEIGHT = 1; + } else { + DEFAULT_LOCAL_SESSION_WEIGHT = localWeight; + } + + if (renewWeight <= 0) { + LOG.warn("Invalid renew session weight {}. It should be larger than 0", renewWeight); + DEFAULT_RENEW_SESSION_WEIGHT = 2; + } else if (renewWeight < localWeight) { + LOG.warn( + "The renew session weight {} is less than the local session weight {}. Use the local session weight.", + renewWeight, + localWeight); + DEFAULT_RENEW_SESSION_WEIGHT = localWeight; + } else { + DEFAULT_RENEW_SESSION_WEIGHT = renewWeight; + } + + // This is based on the assumption that tokens set in config are for global sessions + DEFAULT_CONNECTION_THROTTLE_TOKENS = connectionWeightEnabled + ? DEFAULT_GLOBAL_SESSION_WEIGHT * tokens : tokens; + DEFAULT_CONNECTION_THROTTLE_FILL_TIME = Integer.getInteger(CONNECTION_THROTTLE_FILL_TIME, 1); + DEFAULT_CONNECTION_THROTTLE_FILL_COUNT = connectionWeightEnabled + ? DEFAULT_GLOBAL_SESSION_WEIGHT * fillCount : fillCount; + DEFAULT_CONNECTION_THROTTLE_FREEZE_TIME = Integer.getInteger(CONNECTION_THROTTLE_FREEZE_TIME, -1); + DEFAULT_CONNECTION_THROTTLE_DROP_INCREASE = getDoubleProp(CONNECTION_THROTTLE_DROP_INCREASE, 0.02); + DEFAULT_CONNECTION_THROTTLE_DROP_DECREASE = getDoubleProp(CONNECTION_THROTTLE_DROP_DECREASE, 0.002); + DEFAULT_CONNECTION_THROTTLE_DECREASE_RATIO = getDoubleProp(CONNECTION_THROTTLE_DECREASE_RATIO, 0); + + logWeighedThrottlingSetting(); + } + + /* Varation of Integer.getInteger for real number properties */ + private static double getDoubleProp(String name, double def) { + String val = System.getProperty(name); + if (val != null) { + return Double.parseDouble(val); + } else { + return def; + } + } + + public BlueThrottle() { + // Disable throttling by default (maxTokens = 0) + this.maxTokens = DEFAULT_CONNECTION_THROTTLE_TOKENS; + this.fillTime = DEFAULT_CONNECTION_THROTTLE_FILL_TIME; + this.fillCount = DEFAULT_CONNECTION_THROTTLE_FILL_COUNT; + this.tokens = maxTokens; + this.lastTime = Time.currentElapsedTime(); + + // Disable BLUE throttling by default (freezeTime = -1) + this.freezeTime = DEFAULT_CONNECTION_THROTTLE_FREEZE_TIME; + this.lastFreeze = Time.currentElapsedTime(); + this.dropIncrease = DEFAULT_CONNECTION_THROTTLE_DROP_INCREASE; + this.dropDecrease = DEFAULT_CONNECTION_THROTTLE_DROP_DECREASE; + this.decreasePoint = DEFAULT_CONNECTION_THROTTLE_DECREASE_RATIO; + this.drop = 0; + + this.rng = new Random(); + } + + public synchronized void setMaxTokens(int max) { + int deficit = maxTokens - tokens; + maxTokens = max; + tokens = max - deficit; + } + + public synchronized void setFillTime(int time) { + fillTime = time; + } + + public synchronized void setFillCount(int count) { + fillCount = count; + } + + public synchronized void setFreezeTime(int time) { + freezeTime = time; + } + + public synchronized void setDropIncrease(double increase) { + dropIncrease = increase; + } + + public synchronized void setDropDecrease(double decrease) { + dropDecrease = decrease; + } + + public synchronized void setDecreasePoint(double ratio) { + decreasePoint = ratio; + } + + public synchronized int getMaxTokens() { + return maxTokens; + } + + public synchronized int getFillTime() { + return fillTime; + } + + public synchronized int getFillCount() { + return fillCount; + } + + public synchronized int getFreezeTime() { + return freezeTime; + } + + public synchronized double getDropIncrease() { + return dropIncrease; + } + + public synchronized double getDropDecrease() { + return dropDecrease; + } + + public synchronized double getDecreasePoint() { + return decreasePoint; + } + + public synchronized double getDropChance() { + return drop; + } + + public synchronized int getDeficit() { + return maxTokens - tokens; + } + + public int getRequiredTokensForGlobal() { + return BlueThrottle.DEFAULT_GLOBAL_SESSION_WEIGHT; + } + + public int getRequiredTokensForLocal() { + return BlueThrottle.DEFAULT_LOCAL_SESSION_WEIGHT; + } + + public int getRequiredTokensForRenew() { + return BlueThrottle.DEFAULT_RENEW_SESSION_WEIGHT; + } + + public boolean isConnectionWeightEnabled() { + return BlueThrottle.connectionWeightEnabled; + } + + public synchronized boolean checkLimit(int need) { + // A maxTokens setting of zero disables throttling + if (maxTokens == 0) { + return true; + } + + long now = Time.currentElapsedTime(); + long diff = now - lastTime; + + if (diff > fillTime) { + int refill = (int) (diff * fillCount / fillTime); + tokens = Math.min(tokens + refill, maxTokens); + lastTime = now; + } + + // A freeze time of -1 disables BLUE randomized throttling + if (freezeTime != -1) { + if (!checkBlue(now)) { + return false; + } + } + + if (tokens < need) { + return false; + } + + tokens -= need; + return true; + } + + public synchronized boolean checkBlue(long now) { + int length = maxTokens - tokens; + int limit = maxTokens; + long diff = now - lastFreeze; + long threshold = Math.round(maxTokens * decreasePoint); + + if (diff > freezeTime) { + if ((length == limit) && (drop < 1)) { + drop = Math.min(drop + dropIncrease, 1); + } else if ((length <= threshold) && (drop > 0)) { + drop = Math.max(drop - dropDecrease, 0); + } + lastFreeze = now; + } + + return !(rng.nextDouble() < drop); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ByteBufferInputStream.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ByteBufferInputStream.java new file mode 100644 index 0000000..9a93abd --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ByteBufferInputStream.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.Record; + +public class ByteBufferInputStream extends InputStream { + + ByteBuffer bb; + + public ByteBufferInputStream(ByteBuffer bb) { + this.bb = bb; + } + + @Override + public int read() throws IOException { + if (bb.remaining() == 0) { + return -1; + } + return bb.get() & 0xff; + } + + @Override + public int available() throws IOException { + return bb.remaining(); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (bb.remaining() == 0) { + return -1; + } + if (len > bb.remaining()) { + len = bb.remaining(); + } + bb.get(b, off, len); + return len; + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public long skip(long n) throws IOException { + if (n < 0L) { + return 0; + } + n = Math.min(n, bb.remaining()); + bb.position(bb.position() + (int) n); + return n; + } + + public static void byteBuffer2Record(ByteBuffer bb, Record record) throws IOException { + BinaryInputArchive ia; + ia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb)); + record.deserialize(ia, "request"); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ByteBufferOutputStream.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ByteBufferOutputStream.java new file mode 100644 index 0000000..2531cba --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ByteBufferOutputStream.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; + +public class ByteBufferOutputStream extends OutputStream { + + ByteBuffer bb; + public ByteBufferOutputStream(ByteBuffer bb) { + this.bb = bb; + } + @Override + public void write(int b) throws IOException { + bb.put((byte) b); + } + @Override + public void write(byte[] b) throws IOException { + bb.put(b); + } + @Override + public void write(byte[] b, int off, int len) throws IOException { + bb.put(b, off, len); + } + public static void record2ByteBuffer(Record record, ByteBuffer bb) throws IOException { + BinaryOutputArchive oa; + oa = BinaryOutputArchive.getArchive(new ByteBufferOutputStream(bb)); + record.serialize(oa, "request"); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ClientCnxnLimitException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ClientCnxnLimitException.java new file mode 100644 index 0000000..79bc201 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ClientCnxnLimitException.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * Indicates that the number of client connections has exceeded some limit. + */ +public class ClientCnxnLimitException extends Exception { + + private static final long serialVersionUID = -8655587505476768446L; + + public ClientCnxnLimitException() { + super("Connection throttle rejected connection"); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ConnectionBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ConnectionBean.java new file mode 100644 index 0000000..2e24dc8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ConnectionBean.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.Arrays; +import javax.management.ObjectName; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of connection MBean interface. + */ +public class ConnectionBean implements ConnectionMXBean, ZKMBeanInfo { + + private static final Logger LOG = LoggerFactory.getLogger(ConnectionBean.class); + + private final ServerCnxn connection; + private final Stats stats; + + private final ZooKeeperServer zk; + + private final String remoteIP; + private final long sessionId; + + public ConnectionBean(ServerCnxn connection, ZooKeeperServer zk) { + this.connection = connection; + this.stats = connection; + this.zk = zk; + + InetSocketAddress sockAddr = connection.getRemoteSocketAddress(); + if (sockAddr == null) { + remoteIP = "Unknown"; + } else { + InetAddress addr = sockAddr.getAddress(); + if (addr instanceof Inet6Address) { + remoteIP = ObjectName.quote(addr.getHostAddress()); + } else { + remoteIP = addr.getHostAddress(); + } + } + sessionId = connection.getSessionId(); + } + + public String getSessionId() { + return "0x" + Long.toHexString(sessionId); + } + + public String getSourceIP() { + InetSocketAddress sockAddr = connection.getRemoteSocketAddress(); + if (sockAddr == null) { + return null; + } + return sockAddr.getAddress().getHostAddress() + ":" + sockAddr.getPort(); + } + + public String getName() { + return MBeanRegistry.getInstance().makeFullPath("Connections", remoteIP, getSessionId()); + } + + public boolean isHidden() { + return false; + } + + public String[] getEphemeralNodes() { + if (zk.getZKDatabase() != null) { + String[] res = zk.getZKDatabase().getEphemerals(sessionId).toArray(new String[0]); + Arrays.sort(res); + return res; + } + return null; + } + + public String getStartedTime() { + return stats.getEstablished().toString(); + } + + public void terminateSession() { + try { + zk.closeSession(sessionId); + } catch (Exception e) { + LOG.warn("Unable to closeSession() for session: 0x{}", getSessionId(), e); + } + } + + public void terminateConnection() { + connection.sendCloseSession(); + } + + public void resetCounters() { + stats.resetStats(); + } + + @Override + public String toString() { + return "ConnectionBean{ClientIP=" + ObjectName.quote(getSourceIP()) + ",SessionId=0x" + getSessionId() + "}"; + } + + public long getOutstandingRequests() { + return stats.getOutstandingRequests(); + } + + public long getPacketsReceived() { + return stats.getPacketsReceived(); + } + + public long getPacketsSent() { + return stats.getPacketsSent(); + } + + public int getSessionTimeout() { + return connection.getSessionTimeout(); + } + + public long getMinLatency() { + return stats.getMinLatency(); + } + + public long getAvgLatency() { + return stats.getAvgLatency(); + } + + public long getMaxLatency() { + return stats.getMaxLatency(); + } + + public String getLastOperation() { + return stats.getLastOperation(); + } + + public String getLastCxid() { + return "0x" + Long.toHexString(stats.getLastCxid()); + } + + public String getLastZxid() { + return "0x" + Long.toHexString(stats.getLastZxid()); + } + + public String getLastResponseTime() { + return Time.elapsedTimeToDate(stats.getLastResponseTime()).toString(); + } + + public long getLastLatency() { + return stats.getLastLatency(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ConnectionMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ConnectionMXBean.java new file mode 100644 index 0000000..7d48313 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ConnectionMXBean.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * This MBean represents a client connection. + */ +public interface ConnectionMXBean { + + /** + * @return source (client) IP address + */ + String getSourceIP(); + /** + * @return client's session id + */ + String getSessionId(); + /** + * @return time the connection was started + */ + String getStartedTime(); + /** + * @return number of ephemeral nodes owned by this connection + */ + String[] getEphemeralNodes(); + /** + * @return packets received from this client + */ + long getPacketsReceived(); + /** + * @return number of packets sent to this client + */ + long getPacketsSent(); + /** + * @return number of requets being processed + */ + long getOutstandingRequests(); + /** + * @return session timeout in ms + */ + int getSessionTimeout(); + + /** + * Terminate this client session. The client will reconnect with a different + * session id. + */ + void terminateSession(); + /** + * Terminate thei client connection. The client will immediately attempt to + * reconnect with the same session id. + */ + void terminateConnection(); + + /** Min latency in ms + * @since 3.3.0 */ + long getMinLatency(); + /** Average latency in ms + * @since 3.3.0 */ + long getAvgLatency(); + /** Max latency in ms + * @since 3.3.0 */ + long getMaxLatency(); + /** Last operation performed by this connection + * @since 3.3.0 */ + String getLastOperation(); + /** Last cxid of this connection + * @since 3.3.0 */ + String getLastCxid(); + /** Last zxid of this connection + * @since 3.3.0 */ + String getLastZxid(); + /** Last time server sent a response to client on this connection + * @since 3.3.0 */ + String getLastResponseTime(); + /** Latency of last response to client on this connection in ms + * @since 3.3.0 */ + long getLastLatency(); + + /** Reset counters + * @since 3.3.0 */ + void resetCounters(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ContainerManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ContainerManager.java new file mode 100644 index 0000000..7085262 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ContainerManager.java @@ -0,0 +1,229 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * // TODO_MA 注释: ContainerManager 用来管理zookeeper container类型的节点, + * // TODO_MA 注释: container 类型的节点作为容器借来用来存放别的节点,当一个 container 类型节点的所有子节点都被删除之后, + * // TODO_MA 注释: ContainerManager 会按照固定的检查周期去找到这些空的 container 节点然后把他们删除掉 + * Manages cleanup of container ZNodes. This class is meant to only be run from the leader. + * There's no harm in running from followers/observers but that will be extra work that's not needed. + * Once started, it periodically checks container nodes that have a cversion > 0 and have no children. + * A delete is attempted on the node. The result of the delete is unimportant. + * If the proposal fails or the container node is not empty there's no harm. + */ +public class ContainerManager { + + private static final Logger LOG = LoggerFactory.getLogger(ContainerManager.class); + private final ZKDatabase zkDb; + private final RequestProcessor requestProcessor; + private final int checkIntervalMs; + private final int maxPerMinute; + private final long maxNeverUsedIntervalMs; + private final Timer timer; + private final AtomicReference task = new AtomicReference(null); + + /** + * @param zkDb the ZK database + * @param requestProcessor request processer - used to inject delete + * container requests + * @param checkIntervalMs how often to check containers in milliseconds + * @param maxPerMinute the max containers to delete per second - avoids + * herding of container deletions + */ + public ContainerManager(ZKDatabase zkDb, RequestProcessor requestProcessor, int checkIntervalMs, int maxPerMinute) { + this(zkDb, requestProcessor, checkIntervalMs, maxPerMinute, 0); + } + + /** + * @param zkDb the ZK database + * @param requestProcessor request processer - used to inject delete + * container requests + * @param checkIntervalMs how often to check containers in milliseconds + * @param maxPerMinute the max containers to delete per second - avoids + * herding of container deletions + * @param maxNeverUsedIntervalMs the max time in milliseconds that a container that has never had + * any children is retained + */ + public ContainerManager(ZKDatabase zkDb, RequestProcessor requestProcessor, int checkIntervalMs, int maxPerMinute, + long maxNeverUsedIntervalMs) { + this.zkDb = zkDb; + this.requestProcessor = requestProcessor; + this.checkIntervalMs = checkIntervalMs; + this.maxPerMinute = maxPerMinute; + this.maxNeverUsedIntervalMs = maxNeverUsedIntervalMs; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + timer = new Timer("ContainerManagerTask", true); + + LOG.info("Using checkIntervalMs={} maxPerMinute={} maxNeverUsedIntervalMs={}", checkIntervalMs, maxPerMinute, + maxNeverUsedIntervalMs); + } + + /** + * start/restart the timer the runs the check. Can safely be called + * multiple times. + */ + public void start() { + if(task.get() == null) { + TimerTask timerTask = new TimerTask() { + @Override + public void run() { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 任务核心内容 + */ + checkContainers(); + } catch(InterruptedException e) { + Thread.currentThread().interrupt(); + LOG.info("interrupted"); + cancel(); + } catch(Throwable e) { + LOG.error("Error checking containers", e); + } + } + }; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动一个定时调度任务 + */ + if(task.compareAndSet(null, timerTask)) { + timer.scheduleAtFixedRate(timerTask, checkIntervalMs, checkIntervalMs); + } + } + } + + /** + * stop the timer if necessary. Can safely be called multiple times. + */ + public void stop() { + TimerTask timerTask = task.getAndSet(null); + if(timerTask != null) { + timerTask.cancel(); + } + timer.cancel(); + } + + /** + * Manually check the containers. Not normally used directly + */ + public void checkContainers() throws InterruptedException { + long minIntervalMs = getMinIntervalMs(); + for(String containerPath : getCandidates()) { + long startMs = Time.currentElapsedTime(); + + ByteBuffer path = ByteBuffer.wrap(containerPath.getBytes()); + Request request = new Request(null, 0, 0, ZooDefs.OpCode.deleteContainer, path, null); + try { + LOG.info("Attempting to delete candidate container: {}", containerPath); + postDeleteRequest(request); + } catch(Exception e) { + LOG.error("Could not delete container: {}", containerPath, e); + } + + long elapsedMs = Time.currentElapsedTime() - startMs; + long waitMs = minIntervalMs - elapsedMs; + if(waitMs > 0) { + Thread.sleep(waitMs); + } + } + } + + // VisibleForTesting + protected void postDeleteRequest(Request request) throws RequestProcessor.RequestProcessorException { + requestProcessor.processRequest(request); + } + + // VisibleForTesting + protected long getMinIntervalMs() { + return TimeUnit.MINUTES.toMillis(1) / maxPerMinute; + } + + // VisibleForTesting + protected Collection getCandidates() { + Set candidates = new HashSet(); + for(String containerPath : zkDb.getDataTree().getContainers()) { + DataNode node = zkDb.getDataTree().getNode(containerPath); + if((node != null) && node.getChildren().isEmpty()) { + /* + cversion > 0: keep newly created containers from being deleted + before any children have been added. If you were to create the + container just before a container cleaning period the container + would be immediately be deleted. + */ + if(node.stat.getCversion() > 0) { + candidates.add(containerPath); + } else { + /* + Users may not want unused containers to live indefinitely. Allow a system + property to be set that sets the max time for a cversion-0 container + to stay before being deleted + */ + if((maxNeverUsedIntervalMs != 0) && (getElapsed(node) > maxNeverUsedIntervalMs)) { + candidates.add(containerPath); + } + } + } + if((node != null) && (node.stat.getCversion() > 0) && (node.getChildren().isEmpty())) { + candidates.add(containerPath); + } + } + for(String ttlPath : zkDb.getDataTree().getTtls()) { + DataNode node = zkDb.getDataTree().getNode(ttlPath); + if(node != null) { + Set children = node.getChildren(); + if(children.isEmpty()) { + if(EphemeralType.get(node.stat.getEphemeralOwner()) == EphemeralType.TTL) { + long ttl = EphemeralType.TTL.getValue(node.stat.getEphemeralOwner()); + if((ttl != 0) && (getElapsed(node) > ttl)) { + candidates.add(ttlPath); + } + } + } + } + } + return candidates; + } + + // VisibleForTesting + protected long getElapsed(DataNode node) { + return Time.currentWallTime() - node.stat.getMtime(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataNode.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataNode.java new file mode 100644 index 0000000..8ac8f61 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataNode.java @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.data.StatPersisted; + +/** + * This class contains the data for a node in the data tree. + *

      + * A data node contains a reference to its parent, a byte array as its data, an + * array of ACLs, a stat object, and a set of its children's paths. + * + */ +@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) +public class DataNode implements Record { + + // the digest value of this node, calculated from path, data and stat + private volatile long digest; + + // indicate if the digest of this node is up to date or not, used to + // optimize the performance. + volatile boolean digestCached; + + /** the data for this datanode */ + byte[] data; + + /** + * the acl map long for this datanode. the datatree has the map + */ + Long acl; + + /** + * the stat for this node that is persisted to disk. + */ + public StatPersisted stat; + + /** + * the list of children for this node. note that the list of children string + * does not contain the parent path -- just the last part of the path. This + * should be synchronized on except deserializing (for speed up issues). + */ + private Set children = null; + + private static final Set EMPTY_SET = Collections.emptySet(); + + /** + * default constructor for the datanode + */ + DataNode() { + // default constructor + } + + /** + * create a DataNode with parent, data, acls and stat + * + * @param data + * the data to be set + * @param acl + * the acls for this node + * @param stat + * the stat for this node. + */ + public DataNode(byte[] data, Long acl, StatPersisted stat) { + this.data = data; + this.acl = acl; + this.stat = stat; + } + + /** + * Method that inserts a child into the children set + * + * @param child + * to be inserted + * @return true if this set did not already contain the specified element + */ + public synchronized boolean addChild(String child) { + if (children == null) { + // let's be conservative on the typical number of children + children = new HashSet(8); + } + return children.add(child); + } + + /** + * Method that removes a child from the children set + * + * @param child + * @return true if this set contained the specified element + */ + public synchronized boolean removeChild(String child) { + if (children == null) { + return false; + } + return children.remove(child); + } + + /** + * convenience method for setting the children for this datanode + * + * @param children + */ + public synchronized void setChildren(HashSet children) { + this.children = children; + } + + /** + * convenience methods to get the children + * + * @return the children of this datanode. If the datanode has no children, empty + * set is returned + */ + public synchronized Set getChildren() { + if (children == null) { + return EMPTY_SET; + } + + return Collections.unmodifiableSet(children); + } + + public synchronized void copyStat(Stat to) { + to.setAversion(stat.getAversion()); + to.setCtime(stat.getCtime()); + to.setCzxid(stat.getCzxid()); + to.setMtime(stat.getMtime()); + to.setMzxid(stat.getMzxid()); + to.setPzxid(stat.getPzxid()); + to.setVersion(stat.getVersion()); + to.setEphemeralOwner(getClientEphemeralOwner(stat)); + to.setDataLength(data == null ? 0 : data.length); + int numChildren = 0; + if (this.children != null) { + numChildren = children.size(); + } + // when we do the Cversion we need to translate from the count of the creates + // to the count of the changes (v3 semantics) + // for every create there is a delete except for the children still present + to.setCversion(stat.getCversion() * 2 - numChildren); + to.setNumChildren(numChildren); + } + + private static long getClientEphemeralOwner(StatPersisted stat) { + EphemeralType ephemeralType = EphemeralType.get(stat.getEphemeralOwner()); + if (ephemeralType != EphemeralType.NORMAL) { + return 0; + } + return stat.getEphemeralOwner(); + } + + public synchronized void deserialize(InputArchive archive, String tag) throws IOException { + archive.startRecord("node"); + data = archive.readBuffer("data"); + acl = archive.readLong("acl"); + stat = new StatPersisted(); + stat.deserialize(archive, "statpersisted"); + archive.endRecord("node"); + } + + public synchronized void serialize(OutputArchive archive, String tag) throws IOException { + archive.startRecord(this, "node"); + archive.writeBuffer(data, "data"); + archive.writeLong(acl, "acl"); + stat.serialize(archive, "statpersisted"); + archive.endRecord(this, "node"); + } + + public boolean isDigestCached() { + return digestCached; + } + + public void setDigestCached(boolean digestCached) { + this.digestCached = digestCached; + } + + public long getDigest() { + return digest; + } + + public void setDigest(long digest) { + this.digest = digest; + } + + public synchronized byte[] getData() { + return data; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTree.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTree.java new file mode 100644 index 0000000..e0ee953 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTree.java @@ -0,0 +1,2019 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.EOFException; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.util.*; +import java.util.Map.Entry; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.DigestWatcher; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.KeeperException.NodeExistsException; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.audit.AuditConstants; +import org.apache.zookeeper.audit.AuditEvent.Result; +import org.apache.zookeeper.audit.ZKAuditProvider; +import org.apache.zookeeper.common.PathTrie; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.server.watch.IWatchManager; +import org.apache.zookeeper.server.watch.WatchManagerFactory; +import org.apache.zookeeper.server.watch.WatcherMode; +import org.apache.zookeeper.server.watch.WatcherOrBitSet; +import org.apache.zookeeper.server.watch.WatchesPathReport; +import org.apache.zookeeper.server.watch.WatchesReport; +import org.apache.zookeeper.server.watch.WatchesSummary; +import org.apache.zookeeper.txn.CheckVersionTxn; +import org.apache.zookeeper.txn.CloseSessionTxn; +import org.apache.zookeeper.txn.CreateContainerTxn; +import org.apache.zookeeper.txn.CreateTTLTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.SetACLTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class maintains the tree data structure. It doesn't have any networking + * or client connection code in it so that it can be tested in a stand alone + * way. + *

      + * The tree maintains two parallel data structures: a hashtable that maps from + * full paths to DataNodes and a tree of DataNodes. All accesses to a path is + * through the hashtable. The tree is traversed only when serializing to disk. + */ +public class DataTree { + + private static final Logger LOG = LoggerFactory.getLogger(DataTree.class); + + private final RateLogger RATE_LOGGER = new RateLogger(LOG, 15 * 60 * 1000); + + /** + * This map provides a fast lookup to the datanodes. The tree is the + * source of truth and is where all the locking occurs + */ + private final NodeHashMap nodes; + + private IWatchManager dataWatches; + + private IWatchManager childWatches; + + /** + * cached total size of paths and data for all DataNodes + */ + private final AtomicLong nodeDataSize = new AtomicLong(0); + + /** + * the root of zookeeper tree + */ + private static final String rootZookeeper = "/"; + + /** + * the zookeeper nodes that acts as the management and status node + **/ + private static final String procZookeeper = Quotas.procZookeeper; + + /** + * this will be the string thats stored as a child of root + */ + private static final String procChildZookeeper = procZookeeper.substring(1); + + /** + * the zookeeper quota node that acts as the quota management node for + * zookeeper + */ + private static final String quotaZookeeper = Quotas.quotaZookeeper; + + /** + * this will be the string thats stored as a child of /zookeeper + */ + private static final String quotaChildZookeeper = quotaZookeeper.substring(procZookeeper.length() + 1); + + /** + * the zookeeper config node that acts as the config management node for + * zookeeper + */ + private static final String configZookeeper = ZooDefs.CONFIG_NODE; + + /** + * this will be the string thats stored as a child of /zookeeper + */ + private static final String configChildZookeeper = configZookeeper.substring(procZookeeper.length() + 1); + + /** + * the path trie that keeps track of the quota nodes in this datatree + */ + private final PathTrie pTrie = new PathTrie(); + + /** + * over-the-wire size of znode's stat. Counting the fields of Stat class + */ + public static final int STAT_OVERHEAD_BYTES = (6 * 8) + (5 * 4); + + /** + * This hashtable lists the paths of the ephemeral nodes of a session. + */ + private final Map> ephemerals = new ConcurrentHashMap>(); + + /** + * This set contains the paths of all container nodes + */ + private final Set containers = Collections.newSetFromMap(new ConcurrentHashMap()); + + /** + * This set contains the paths of all ttl nodes + */ + private final Set ttls = Collections.newSetFromMap(new ConcurrentHashMap()); + + private final ReferenceCountedACLCache aclCache = new ReferenceCountedACLCache(); + + // The maximum number of tree digests that we will keep in our history + public static final int DIGEST_LOG_LIMIT = 1024; + + // Dump digest every 128 txns, in hex it's 80, which will make it easier + // to align and compare between servers. + public static final int DIGEST_LOG_INTERVAL = 128; + + // If this is not null, we are actively looking for a target zxid that we + // want to validate the digest for + private ZxidDigest digestFromLoadedSnapshot; + + // The digest associated with the highest zxid in the data tree. + private volatile ZxidDigest lastProcessedZxidDigest; + + private boolean firstMismatchTxn = true; + + // Will be notified when digest mismatch event triggered. + private final List digestWatchers = new ArrayList<>(); + + // The historical digests list. + private LinkedList digestLog = new LinkedList<>(); + + private final DigestCalculator digestCalculator; + + @SuppressWarnings("unchecked") + public Set getEphemerals(long sessionId) { + HashSet retv = ephemerals.get(sessionId); + if(retv == null) { + return new HashSet(); + } + Set cloned = null; + synchronized(retv) { + cloned = (HashSet) retv.clone(); + } + return cloned; + } + + public Set getContainers() { + return new HashSet(containers); + } + + public Set getTtls() { + return new HashSet(ttls); + } + + public Collection getSessions() { + return ephemerals.keySet(); + } + + public DataNode getNode(String path) { + return nodes.get(path); + } + + public int getNodeCount() { + return nodes.size(); + } + + public int getWatchCount() { + return dataWatches.size() + childWatches.size(); + } + + public int getEphemeralsCount() { + int result = 0; + for(HashSet set : ephemerals.values()) { + result += set.size(); + } + return result; + } + + /** + * Get the size of the nodes based on path and data length. + * + * @return size of the data + */ + public long approximateDataSize() { + long result = 0; + for(Map.Entry entry : nodes.entrySet()) { + DataNode value = entry.getValue(); + synchronized(value) { + result += getNodeSize(entry.getKey(), value.data); + } + } + return result; + } + + /** + * Get the size of the node based on path and data length. + */ + private static long getNodeSize(String path, byte[] data) { + return (path == null ? 0 : path.length()) + (data == null ? 0 : data.length); + } + + public long cachedApproximateDataSize() { + return nodeDataSize.get(); + } + + /** + * This is a pointer to the root of the DataTree. It is the source of truth, + * but we usually use the nodes hashmap to find nodes in the tree. + */ + private DataNode root = new DataNode(new byte[0], -1L, new StatPersisted()); + + /** + * create a /zookeeper filesystem that is the proc filesystem of zookeeper + */ + private final DataNode procDataNode = new DataNode(new byte[0], -1L, new StatPersisted()); + + /** + * create a /zookeeper/quota node for maintaining quota properties for + * zookeeper + */ + private final DataNode quotaDataNode = new DataNode(new byte[0], -1L, new StatPersisted()); + + public DataTree() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: DigestCalculator 摘要计算器 + */ + this(new DigestCalculator()); + } + + DataTree(DigestCalculator digestCalculator) { + this.digestCalculator = digestCalculator; + + // TODO_MA 注释: 存储所有的 datanode 节点的映射数据 + // TODO_MA 注释: key就是 znodepath , value = DataNode 对象 + nodes = new NodeHashMapImpl(digestCalculator); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + /* Rather than fight it, let root have an alias */ + nodes.put("", root); + nodes.putWithoutDigest(rootZookeeper, root); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: /proc/quota + */ + /** add the proc node and quota node */ + root.addChild(procChildZookeeper); + nodes.put(procZookeeper, procDataNode); + procDataNode.addChild(quotaChildZookeeper); + nodes.put(quotaZookeeper, quotaDataNode); + + addConfigNode(); + + nodeDataSize.set(approximateDataSize()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 默认实现就是: WatchManager + */ + try { + + // TODO_MA 马中华 注释: + dataWatches = WatchManagerFactory.createWatchManager(); + + // TODO_MA 马中华 注释: + childWatches = WatchManagerFactory.createWatchManager(); + + } catch(Exception e) { + LOG.error("Unexpected exception when creating WatchManager, exiting abnormally", e); + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + } + + /** + * create a /zookeeper/config node for maintaining the configuration (membership and quorum system) info for + * zookeeper + */ + public void addConfigNode() { + DataNode zookeeperZnode = nodes.get(procZookeeper); + if(zookeeperZnode != null) { // should always be the case + zookeeperZnode.addChild(configChildZookeeper); + } else { + assert false : "There's no /zookeeper znode - this should never happen."; + } + + nodes.put(configZookeeper, new DataNode(new byte[0], -1L, new StatPersisted())); + try { + // Reconfig node is access controlled by default (ZOOKEEPER-2014). + setACL(configZookeeper, ZooDefs.Ids.READ_ACL_UNSAFE, -1); + } catch(KeeperException.NoNodeException e) { + assert false : "There's no " + configZookeeper + " znode - this should never happen."; + } + } + + /** + * is the path one of the special paths owned by zookeeper. + * + * @param path the path to be checked + * @return true if a special path. false if not. + */ + boolean isSpecialPath(String path) { + return rootZookeeper.equals(path) || procZookeeper.equals(path) || quotaZookeeper.equals(path) || configZookeeper + .equals(path); + } + + public static void copyStatPersisted(StatPersisted from, StatPersisted to) { + to.setAversion(from.getAversion()); + to.setCtime(from.getCtime()); + to.setCversion(from.getCversion()); + to.setCzxid(from.getCzxid()); + to.setMtime(from.getMtime()); + to.setMzxid(from.getMzxid()); + to.setPzxid(from.getPzxid()); + to.setVersion(from.getVersion()); + to.setEphemeralOwner(from.getEphemeralOwner()); + } + + public static void copyStat(Stat from, Stat to) { + to.setAversion(from.getAversion()); + to.setCtime(from.getCtime()); + to.setCversion(from.getCversion()); + to.setCzxid(from.getCzxid()); + to.setMtime(from.getMtime()); + to.setMzxid(from.getMzxid()); + to.setPzxid(from.getPzxid()); + to.setVersion(from.getVersion()); + to.setEphemeralOwner(from.getEphemeralOwner()); + to.setDataLength(from.getDataLength()); + to.setNumChildren(from.getNumChildren()); + } + + /** + * update the count/count of bytes of this stat datanode + * + * @param lastPrefix the path of the node that is quotaed. + * @param bytesDiff the diff to be added to number of bytes + * @param countDiff the diff to be added to the count + */ + public void updateCountBytes(String lastPrefix, long bytesDiff, int countDiff) { + String statNode = Quotas.statPath(lastPrefix); + DataNode node = nodes.get(statNode); + + StatsTrack updatedStat = null; + if(node == null) { + // should not happen + LOG.error("Missing count node for stat {}", statNode); + return; + } + synchronized(node) { + updatedStat = new StatsTrack(new String(node.data)); + updatedStat.setCount(updatedStat.getCount() + countDiff); + updatedStat.setBytes(updatedStat.getBytes() + bytesDiff); + node.data = updatedStat.toString().getBytes(); + } + // now check if the counts match the quota + String quotaNode = Quotas.quotaPath(lastPrefix); + node = nodes.get(quotaNode); + StatsTrack thisStats = null; + if(node == null) { + // should not happen + LOG.error("Missing count node for quota {}", quotaNode); + return; + } + synchronized(node) { + thisStats = new StatsTrack(new String(node.data)); + } + if(thisStats.getCount() > -1 && (thisStats.getCount() < updatedStat.getCount())) { + LOG.warn("Quota exceeded: {} count={} limit={}", lastPrefix, updatedStat.getCount(), thisStats.getCount()); + } + if(thisStats.getBytes() > -1 && (thisStats.getBytes() < updatedStat.getBytes())) { + LOG.warn("Quota exceeded: {} bytes={} limit={}", lastPrefix, updatedStat.getBytes(), thisStats.getBytes()); + } + } + + /** + * Add a new node to the DataTree. + * + * @param path Path for the new node. + * @param data Data to store in the node. + * @param acl Node acls + * @param ephemeralOwner the session id that owns this node. -1 indicates this is not + * an ephemeral node. + * @param zxid Transaction ID + * @param time + * @throws NodeExistsException + * @throws NoNodeException + */ + public void createNode(final String path, byte[] data, List acl, long ephemeralOwner, int parentCVersion, + long zxid, long time) throws NoNodeException, NodeExistsException { + createNode(path, data, acl, ephemeralOwner, parentCVersion, zxid, time, null); + } + + /** + * Add a new node to the DataTree. + * + * @param path Path for the new node. + * @param data Data to store in the node. + * @param acl Node acls + * @param ephemeralOwner the session id that owns this node. -1 indicates this is not + * an ephemeral node. + * @param zxid Transaction ID + * @param time + * @param outputStat A Stat object to store Stat output results into. + * @throws NodeExistsException + * @throws NoNodeException + */ + public void createNode(final String path, byte[] data, List acl, long ephemeralOwner, int parentCVersion, + long zxid, long time, + Stat outputStat) throws KeeperException.NoNodeException, KeeperException.NodeExistsException { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + String childName = path.substring(lastSlash + 1); + StatPersisted stat = createStat(zxid, time, ephemeralOwner); + DataNode parent = nodes.get(parentName); + if(parent == null) { + throw new KeeperException.NoNodeException(); + } + synchronized(parent) { + // Add the ACL to ACL cache first, to avoid the ACL not being + // created race condition during fuzzy snapshot sync. + // + // This is the simplest fix, which may add ACL reference count + // again if it's already counted in in the ACL map of fuzzy + // snapshot, which might also happen for deleteNode txn, but + // at least it won't cause the ACL not exist issue. + // + // Later we can audit and delete all non-referenced ACLs from + // ACL map when loading the snapshot/txns from disk, like what + // we did for the global sessions. + Long longval = aclCache.convertAcls(acl); + + Set children = parent.getChildren(); + if(children.contains(childName)) { + throw new KeeperException.NodeExistsException(); + } + + nodes.preChange(parentName, parent); + if(parentCVersion == -1) { + parentCVersion = parent.stat.getCversion(); + parentCVersion++; + } + // There is possibility that we'll replay txns for a node which + // was created and then deleted in the fuzzy range, and it's not + // exist in the snapshot, so replay the creation might revert the + // cversion and pzxid, need to check and only update when it's larger. + if(parentCVersion > parent.stat.getCversion()) { + parent.stat.setCversion(parentCVersion); + parent.stat.setPzxid(zxid); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + DataNode child = new DataNode(data, longval, stat); + parent.addChild(childName); + nodes.postChange(parentName, parent); + nodeDataSize.addAndGet(getNodeSize(path, child.data)); + nodes.put(path, child); + + EphemeralType ephemeralType = EphemeralType.get(ephemeralOwner); + if(ephemeralType == EphemeralType.CONTAINER) { + containers.add(path); + } else if(ephemeralType == EphemeralType.TTL) { + ttls.add(path); + } else if(ephemeralOwner != 0) { + HashSet list = ephemerals.get(ephemeralOwner); + if(list == null) { + list = new HashSet(); + ephemerals.put(ephemeralOwner, list); + } + synchronized(list) { + list.add(path); + } + } + if(outputStat != null) { + child.copyStat(outputStat); + } + } + // now check if its one of the zookeeper node child + if(parentName.startsWith(quotaZookeeper)) { + // now check if its the limit node + if(Quotas.limitNode.equals(childName)) { + // this is the limit node + // get the parent and add it to the trie + pTrie.addPath(parentName.substring(quotaZookeeper.length())); + } + if(Quotas.statNode.equals(childName)) { + updateQuotaForPath(parentName.substring(quotaZookeeper.length())); + } + } + // also check to update the quotas for this node + String lastPrefix = getMaxPrefixWithQuota(path); + long bytes = data == null ? 0 : data.length; + if(lastPrefix != null) { + // ok we have some match and need to update + updateCountBytes(lastPrefix, bytes, 1); + } + updateWriteStat(path, bytes); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、触发当前节点的 NodeCreated 事件 + * 2、触发父节点的 NodeChildrenChanged 事件 + */ + dataWatches.triggerWatch(path, Event.EventType.NodeCreated); + childWatches.triggerWatch(parentName.equals("") ? "/" : parentName, Event.EventType.NodeChildrenChanged); + } + + /** + * remove the path from the datatree + * + * @param path the path to of the node to be deleted + * @param zxid the current zxid + * @throws KeeperException.NoNodeException + */ + public void deleteNode(String path, long zxid) throws KeeperException.NoNodeException { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + String childName = path.substring(lastSlash + 1); + + // The child might already be deleted during taking fuzzy snapshot, + // but we still need to update the pzxid here before throw exception + // for no such child + DataNode parent = nodes.get(parentName); + if(parent == null) { + throw new KeeperException.NoNodeException(); + } + synchronized(parent) { + nodes.preChange(parentName, parent); + parent.removeChild(childName); + // Only update pzxid when the zxid is larger than the current pzxid, + // otherwise we might override some higher pzxid set by a create + // Txn, which could cause the cversion and pzxid inconsistent + if(zxid > parent.stat.getPzxid()) { + parent.stat.setPzxid(zxid); + } + nodes.postChange(parentName, parent); + } + + DataNode node = nodes.get(path); + if(node == null) { + throw new KeeperException.NoNodeException(); + } + nodes.remove(path); + synchronized(node) { + aclCache.removeUsage(node.acl); + nodeDataSize.addAndGet(-getNodeSize(path, node.data)); + } + + // Synchronized to sync the containers and ttls change, probably + // only need to sync on containers and ttls, will update it in a + // separate patch. + synchronized(parent) { + long eowner = node.stat.getEphemeralOwner(); + EphemeralType ephemeralType = EphemeralType.get(eowner); + if(ephemeralType == EphemeralType.CONTAINER) { + containers.remove(path); + } else if(ephemeralType == EphemeralType.TTL) { + ttls.remove(path); + } else if(eowner != 0) { + Set nodes = ephemerals.get(eowner); + if(nodes != null) { + synchronized(nodes) { + nodes.remove(path); + } + } + } + } + + if(parentName.startsWith(procZookeeper) && Quotas.limitNode.equals(childName)) { + // delete the node in the trie. + // we need to update the trie as well + pTrie.deletePath(parentName.substring(quotaZookeeper.length())); + } + + // also check to update the quotas for this node + String lastPrefix = getMaxPrefixWithQuota(path); + if(lastPrefix != null) { + // ok we have some match and need to update + int bytes = 0; + synchronized(node) { + bytes = (node.data == null ? 0 : -(node.data.length)); + } + updateCountBytes(lastPrefix, bytes, -1); + } + + updateWriteStat(path, 0L); + + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "dataWatches.triggerWatch " + path); + ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "childWatches.triggerWatch " + parentName); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 触发回调 + */ + WatcherOrBitSet processed = dataWatches.triggerWatch(path, EventType.NodeDeleted); + childWatches.triggerWatch(path, EventType.NodeDeleted, processed); + childWatches.triggerWatch("".equals(parentName) ? "/" : parentName, EventType.NodeChildrenChanged); + } + + public Stat setData(String path, byte[] data, int version, long zxid, + long time) throws KeeperException.NoNodeException { + Stat s = new Stat(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 根据节点 路径找到 DataNode 对象 + */ + DataNode n = nodes.get(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + byte[] lastdata = null; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 修改数据 + */ + synchronized(n) { + lastdata = n.data; + nodes.preChange(path, n); + n.data = data; + n.stat.setMtime(time); + n.stat.setMzxid(zxid); + n.stat.setVersion(version); + n.copyStat(s); + nodes.postChange(path, n); + } + // now update if the path is in a quota subtree. + String lastPrefix = getMaxPrefixWithQuota(path); + long dataBytes = data == null ? 0 : data.length; + if(lastPrefix != null) { + this.updateCountBytes(lastPrefix, dataBytes - (lastdata == null ? 0 : lastdata.length), 0); + } + nodeDataSize.addAndGet(getNodeSize(path, data) - getNodeSize(path, lastdata)); + + updateWriteStat(path, dataBytes); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 触发监听 + * 修改数据,触发的是 NodeDataChanged + */ + dataWatches.triggerWatch(path, EventType.NodeDataChanged); + return s; + } + + /** + * If there is a quota set, return the appropriate prefix for that quota + * Else return null + * + * @param path The ZK path to check for quota + * @return Max quota prefix, or null if none + */ + public String getMaxPrefixWithQuota(String path) { + // do nothing for the root. + // we are not keeping a quota on the zookeeper + // root node for now. + String lastPrefix = pTrie.findMaxPrefix(path); + + if(rootZookeeper.equals(lastPrefix) || lastPrefix.isEmpty()) { + return null; + } else { + return lastPrefix; + } + } + + public void addWatch(String basePath, Watcher watcher, int mode) { + WatcherMode watcherMode = WatcherMode.fromZooDef(mode); + dataWatches.addWatch(basePath, watcher, watcherMode); + childWatches.addWatch(basePath, watcher, watcherMode); + } + + public byte[] getData(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { + DataNode n = nodes.get(path); + byte[] data = null; + if(n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized(n) { + n.copyStat(stat); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 监听注册 + */ + if(watcher != null) { + dataWatches.addWatch(path, watcher); + } + data = n.data; + } + updateReadStat(path, data == null ? 0 : data.length); + return data; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 判断节点是否存在 + */ + public Stat statNode(String path, Watcher watcher) throws KeeperException.NoNodeException { + Stat stat = new Stat(); + DataNode n = nodes.get(path); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 监听注册 + */ + if(watcher != null) { + dataWatches.addWatch(path, watcher); + } + if(n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized(n) { + n.copyStat(stat); + } + updateReadStat(path, 0L); + return stat; + } + + public List getChildren(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { + DataNode n = nodes.get(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + List children; + synchronized(n) { + if(stat != null) { + n.copyStat(stat); + } + children = new ArrayList(n.getChildren()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 监听注册 + */ + if(watcher != null) { + childWatches.addWatch(path, watcher); + } + } + + int bytes = 0; + for(String child : children) { + bytes += child.length(); + } + updateReadStat(path, bytes); + + return children; + } + + public int getAllChildrenNumber(String path) { + //cull out these two keys:"", "/" + if("/".equals(path)) { + return nodes.size() - 2; + } + + return (int) nodes.entrySet().parallelStream().filter(entry -> entry.getKey().startsWith(path + "/")).count(); + } + + public Stat setACL(String path, List acl, int version) throws KeeperException.NoNodeException { + Stat stat = new Stat(); + DataNode n = nodes.get(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized(n) { + aclCache.removeUsage(n.acl); + nodes.preChange(path, n); + n.stat.setAversion(version); + n.acl = aclCache.convertAcls(acl); + n.copyStat(stat); + nodes.postChange(path, n); + return stat; + } + } + + public List getACL(String path, Stat stat) throws KeeperException.NoNodeException { + DataNode n = nodes.get(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + synchronized(n) { + if(stat != null) { + n.copyStat(stat); + } + return new ArrayList(aclCache.convertLong(n.acl)); + } + } + + public List getACL(DataNode node) { + synchronized(node) { + return aclCache.convertLong(node.acl); + } + } + + public int aclCacheSize() { + return aclCache.size(); + } + + public static class ProcessTxnResult { + + public long clientId; + + public int cxid; + + public long zxid; + + public int err; + + public int type; + + public String path; + + public Stat stat; + + public List multiResult; + + /** + * Equality is defined as the clientId and the cxid being the same. This + * allows us to use hash tables to track completion of transactions. + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + if(o instanceof ProcessTxnResult) { + ProcessTxnResult other = (ProcessTxnResult) o; + return other.clientId == clientId && other.cxid == cxid; + } + return false; + } + + /** + * See equals() to find the rational for how this hashcode is generated. + * + * @see ProcessTxnResult#equals(Object) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return (int) ((clientId ^ cxid) % Integer.MAX_VALUE); + } + + } + + public volatile long lastProcessedZxid = 0; + + public ProcessTxnResult processTxn(TxnHeader header, Record txn, TxnDigest digest) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + ProcessTxnResult result = processTxn(header, txn); + compareDigest(header, txn, digest); + return result; + } + + public ProcessTxnResult processTxn(TxnHeader header, Record txn) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return this.processTxn(header, txn, false); + } + + public ProcessTxnResult processTxn(TxnHeader header, Record txn, boolean isSubTxn) { + ProcessTxnResult rc = new ProcessTxnResult(); + + try { + rc.clientId = header.getClientId(); + rc.cxid = header.getCxid(); + rc.zxid = header.getZxid(); + rc.type = header.getType(); + rc.err = 0; + rc.multiResult = null; + switch(header.getType()) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 事务类型是 OpCode.create, 之前的动作是创建 znode + * 此时恢复执行: replay : 调用 createNode() 创建一个 znode 节点插入到 datatree 中 + */ + case OpCode.create: + CreateTxn createTxn = (CreateTxn) txn; + rc.path = createTxn.getPath(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建节点,将来在讲解 创建节点的时候,会走到这个方法,到时候详细讲 + */ + createNode(createTxn.getPath(), createTxn.getData(), createTxn.getAcl(), + createTxn.getEphemeral() ? header.getClientId() : 0, createTxn.getParentCVersion(), + header.getZxid(), header.getTime(), null); + break; + case OpCode.create2: + CreateTxn create2Txn = (CreateTxn) txn; + rc.path = create2Txn.getPath(); + Stat stat = new Stat(); + createNode(create2Txn.getPath(), create2Txn.getData(), create2Txn.getAcl(), + create2Txn.getEphemeral() ? header.getClientId() : 0, create2Txn.getParentCVersion(), + header.getZxid(), header.getTime(), stat); + rc.stat = stat; + break; + case OpCode.createTTL: + CreateTTLTxn createTtlTxn = (CreateTTLTxn) txn; + rc.path = createTtlTxn.getPath(); + stat = new Stat(); + createNode(createTtlTxn.getPath(), createTtlTxn.getData(), createTtlTxn.getAcl(), + EphemeralType.TTL.toEphemeralOwner(createTtlTxn.getTtl()), createTtlTxn.getParentCVersion(), + header.getZxid(), header.getTime(), stat); + rc.stat = stat; + break; + case OpCode.createContainer: + CreateContainerTxn createContainerTxn = (CreateContainerTxn) txn; + rc.path = createContainerTxn.getPath(); + stat = new Stat(); + createNode(createContainerTxn.getPath(), createContainerTxn.getData(), createContainerTxn.getAcl(), + EphemeralType.CONTAINER_EPHEMERAL_OWNER, createContainerTxn.getParentCVersion(), + header.getZxid(), header.getTime(), stat); + rc.stat = stat; + break; + case OpCode.delete: + case OpCode.deleteContainer: + DeleteTxn deleteTxn = (DeleteTxn) txn; + rc.path = deleteTxn.getPath(); + deleteNode(deleteTxn.getPath(), header.getZxid()); + break; + case OpCode.reconfig: + case OpCode.setData: + SetDataTxn setDataTxn = (SetDataTxn) txn; + rc.path = setDataTxn.getPath(); + rc.stat = setData(setDataTxn.getPath(), setDataTxn.getData(), setDataTxn.getVersion(), + header.getZxid(), header.getTime()); + break; + case OpCode.setACL: + SetACLTxn setACLTxn = (SetACLTxn) txn; + rc.path = setACLTxn.getPath(); + rc.stat = setACL(setACLTxn.getPath(), setACLTxn.getAcl(), setACLTxn.getVersion()); + break; + case OpCode.closeSession: + long sessionId = header.getClientId(); + if(txn != null) { + killSession(sessionId, header.getZxid(), ephemerals.remove(sessionId), + ((CloseSessionTxn) txn).getPaths2Delete()); + } else { + killSession(sessionId, header.getZxid()); + } + break; + case OpCode.error: + ErrorTxn errTxn = (ErrorTxn) txn; + rc.err = errTxn.getErr(); + break; + case OpCode.check: + CheckVersionTxn checkTxn = (CheckVersionTxn) txn; + rc.path = checkTxn.getPath(); + break; + case OpCode.multi: + MultiTxn multiTxn = (MultiTxn) txn; + List txns = multiTxn.getTxns(); + rc.multiResult = new ArrayList(); + boolean failed = false; + for(Txn subtxn : txns) { + if(subtxn.getType() == OpCode.error) { + failed = true; + break; + } + } + + boolean post_failed = false; + for(Txn subtxn : txns) { + ByteBuffer bb = ByteBuffer.wrap(subtxn.getData()); + Record record = null; + switch(subtxn.getType()) { + case OpCode.create: + record = new CreateTxn(); + break; + case OpCode.createTTL: + record = new CreateTTLTxn(); + break; + case OpCode.createContainer: + record = new CreateContainerTxn(); + break; + case OpCode.delete: + case OpCode.deleteContainer: + record = new DeleteTxn(); + break; + case OpCode.setData: + record = new SetDataTxn(); + break; + case OpCode.error: + record = new ErrorTxn(); + post_failed = true; + break; + case OpCode.check: + record = new CheckVersionTxn(); + break; + default: + throw new IOException("Invalid type of op: " + subtxn.getType()); + } + assert (record != null); + + ByteBufferInputStream.byteBuffer2Record(bb, record); + + if(failed && subtxn.getType() != OpCode.error) { + int ec = post_failed ? Code.RUNTIMEINCONSISTENCY.intValue() : Code.OK.intValue(); + + subtxn.setType(OpCode.error); + record = new ErrorTxn(ec); + } + + assert !failed || (subtxn.getType() == OpCode.error); + + TxnHeader subHdr = new TxnHeader(header.getClientId(), header.getCxid(), header.getZxid(), + header.getTime(), subtxn.getType()); + ProcessTxnResult subRc = processTxn(subHdr, record, true); + rc.multiResult.add(subRc); + if(subRc.err != 0 && rc.err == 0) { + rc.err = subRc.err; + } + } + break; + } + } catch(KeeperException e) { + LOG.debug("Failed: {}:{}", header, txn, e); + rc.err = e.code().intValue(); + } catch(IOException e) { + LOG.debug("Failed: {}:{}", header, txn, e); + } + + /* + * Snapshots are taken lazily. When serializing a node, it's data + * and children copied in a synchronization block on that node, + * which means newly created node won't be in the snapshot, so + * we won't have mismatched cversion and pzxid when replaying the + * createNode txn. + * + * But there is a tricky scenario that if the child is deleted due + * to session close and re-created in a different global session + * after that the parent is serialized, then when replay the txn + * because the node is belonging to a different session, replay the + * closeSession txn won't delete it anymore, and we'll get NODEEXISTS + * error when replay the createNode txn. In this case, we need to + * update the cversion and pzxid to the new value. + * + * Note, such failures on DT should be seen only during + * restore. + */ + if(header.getType() == OpCode.create && rc.err == Code.NODEEXISTS.intValue()) { + LOG.debug("Adjusting parent cversion for Txn: {} path: {} err: {}", header.getType(), rc.path, rc.err); + int lastSlash = rc.path.lastIndexOf('/'); + String parentName = rc.path.substring(0, lastSlash); + CreateTxn cTxn = (CreateTxn) txn; + try { + setCversionPzxid(parentName, cTxn.getParentCVersion(), header.getZxid()); + } catch(KeeperException.NoNodeException e) { + LOG.error("Failed to set parent cversion for: {}", parentName, e); + rc.err = e.code().intValue(); + } + } else if(rc.err != Code.OK.intValue()) { + LOG.debug("Ignoring processTxn failure hdr: {} : error: {}", header.getType(), rc.err); + } + + /* + * Things we can only update after the whole txn is applied to data + * tree. + * + * If we update the lastProcessedZxid with the first sub txn in multi + * and there is a snapshot in progress, it's possible that the zxid + * associated with the snapshot only include partial of the multi op. + * + * When loading snapshot, it will only load the txns after the zxid + * associated with snapshot file, which could cause data inconsistency + * due to missing sub txns. + * + * To avoid this, we only update the lastProcessedZxid when the whole + * multi-op txn is applied to DataTree. + */ + if(!isSubTxn) { + /* + * A snapshot might be in progress while we are modifying the data + * tree. If we set lastProcessedZxid prior to making corresponding + * change to the tree, then the zxid associated with the snapshot + * file will be ahead of its contents. Thus, while restoring from + * the snapshot, the restore method will not apply the transaction + * for zxid associated with the snapshot file, since the restore + * method assumes that transaction to be present in the snapshot. + * + * To avoid this, we first apply the transaction and then modify + * lastProcessedZxid. During restore, we correctly handle the + * case where the snapshot contains data ahead of the zxid associated + * with the file. + */ + if(rc.zxid > lastProcessedZxid) { + lastProcessedZxid = rc.zxid; + } + + if(digestFromLoadedSnapshot != null) { + compareSnapshotDigests(rc.zxid); + } else { + // only start recording digest when we're not in fuzzy state + logZxidDigest(rc.zxid, getTreeDigest()); + } + } + + return rc; + } + + void killSession(long session, long zxid) { + // the list is already removed from the ephemerals + // so we do not have to worry about synchronizing on + // the list. This is only called from FinalRequestProcessor + // so there is no need for synchronization. The list is not + // changed here. Only create and delete change the list which + // are again called from FinalRequestProcessor in sequence. + killSession(session, zxid, ephemerals.remove(session), null); + } + + void killSession(long session, long zxid, Set paths2DeleteLocal, List paths2DeleteInTxn) { + if(paths2DeleteInTxn != null) { + deleteNodes(session, zxid, paths2DeleteInTxn); + } + + if(paths2DeleteLocal == null) { + return; + } + + if(paths2DeleteInTxn != null) { + // explicitly check and remove to avoid potential performance + // issue when using removeAll + for(String path : paths2DeleteInTxn) { + paths2DeleteLocal.remove(path); + } + if(!paths2DeleteLocal.isEmpty()) { + LOG.warn("Unexpected extra paths under session {} which are not in txn 0x{}", paths2DeleteLocal, + Long.toHexString(zxid)); + } + } + + deleteNodes(session, zxid, paths2DeleteLocal); + } + + void deleteNodes(long session, long zxid, Iterable paths2Delete) { + for(String path : paths2Delete) { + boolean deleted = false; + String sessionHex = "0x" + Long.toHexString(session); + try { + deleteNode(path, zxid); + deleted = true; + LOG.debug("Deleting ephemeral node {} for session {}", path, sessionHex); + } catch(NoNodeException e) { + LOG.warn("Ignoring NoNodeException for path {} while removing ephemeral for dead session {}", path, + sessionHex); + } + if(ZKAuditProvider.isAuditEnabled()) { + if(deleted) { + ZKAuditProvider.log(ZKAuditProvider.getZKUser(), AuditConstants.OP_DEL_EZNODE_EXP, path, null, null, + sessionHex, null, Result.SUCCESS); + } else { + ZKAuditProvider.log(ZKAuditProvider.getZKUser(), AuditConstants.OP_DEL_EZNODE_EXP, path, null, null, + sessionHex, null, Result.FAILURE); + } + } + } + } + + /** + * a encapsultaing class for return value + */ + private static class Counts { + + long bytes; + int count; + + } + + /** + * this method gets the count of nodes and the bytes under a subtree + * + * @param path the path to be used + * @param counts the int count + */ + private void getCounts(String path, Counts counts) { + DataNode node = getNode(path); + if(node == null) { + return; + } + String[] children = null; + int len = 0; + synchronized(node) { + Set childs = node.getChildren(); + children = childs.toArray(new String[childs.size()]); + len = (node.data == null ? 0 : node.data.length); + } + // add itself + counts.count += 1; + counts.bytes += len; + for(String child : children) { + getCounts(path + "/" + child, counts); + } + } + + /** + * update the quota for the given path + * + * @param path the path to be used + */ + private void updateQuotaForPath(String path) { + Counts c = new Counts(); + getCounts(path, c); + StatsTrack strack = new StatsTrack(); + strack.setBytes(c.bytes); + strack.setCount(c.count); + String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; + DataNode node = getNode(statPath); + // it should exist + if(node == null) { + LOG.warn("Missing quota stat node {}", statPath); + return; + } + synchronized(node) { + nodes.preChange(statPath, node); + node.data = strack.toString().getBytes(); + nodes.postChange(statPath, node); + } + } + + /** + * this method traverses the quota path and update the path trie and sets + * + * @param path + */ + private void traverseNode(String path) { + DataNode node = getNode(path); + String[] children = null; + synchronized(node) { + Set childs = node.getChildren(); + children = childs.toArray(new String[childs.size()]); + } + if(children.length == 0) { + // this node does not have a child + // is the leaf node + // check if its the leaf node + String endString = "/" + Quotas.limitNode; + if(path.endsWith(endString)) { + // ok this is the limit node + // get the real node and update + // the count and the bytes + String realPath = path.substring(Quotas.quotaZookeeper.length(), path.indexOf(endString)); + updateQuotaForPath(realPath); + this.pTrie.addPath(realPath); + } + return; + } + for(String child : children) { + traverseNode(path + "/" + child); + } + } + + /** + * this method sets up the path trie and sets up stats for quota nodes + */ + private void setupQuota() { + String quotaPath = Quotas.quotaZookeeper; + DataNode node = getNode(quotaPath); + if(node == null) { + return; + } + traverseNode(quotaPath); + } + + /** + * this method uses a stringbuilder to create a new path for children. This + * is faster than string appends ( str1 + str2). + * + * @param oa OutputArchive to write to. + * @param path a string builder. + * @throws IOException + */ + void serializeNode(OutputArchive oa, StringBuilder path) throws IOException { + String pathString = path.toString(); + DataNode node = getNode(pathString); + if(node == null) { + return; + } + String[] children = null; + DataNode nodeCopy; + synchronized(node) { + StatPersisted statCopy = new StatPersisted(); + copyStatPersisted(node.stat, statCopy); + //we do not need to make a copy of node.data because the contents + //are never changed + nodeCopy = new DataNode(node.data, node.acl, statCopy); + Set childs = node.getChildren(); + children = childs.toArray(new String[childs.size()]); + } + serializeNodeData(oa, pathString, nodeCopy); + path.append('/'); + int off = path.length(); + for(String child : children) { + // since this is single buffer being resused + // we need + // to truncate the previous bytes of string. + path.delete(off, Integer.MAX_VALUE); + path.append(child); + serializeNode(oa, path); + } + } + + // visiable for test + public void serializeNodeData(OutputArchive oa, String path, DataNode node) throws IOException { + oa.writeString(path, "path"); + oa.writeRecord(node, "node"); + } + + public void serializeAcls(OutputArchive oa) throws IOException { + aclCache.serialize(oa); + } + + public void serializeNodes(OutputArchive oa) throws IOException { + serializeNode(oa, new StringBuilder()); + // / marks end of stream + // we need to check if clear had been called in between the snapshot. + if(root != null) { + oa.writeString("/", "path"); + } + } + + public void serialize(OutputArchive oa, String tag) throws IOException { + serializeAcls(oa); + serializeNodes(oa); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 持久化 + * 把内存中的 DataTree 保存在磁盘文件中形成快照文件 + * DataTree 由一堆 datanode 节点组成的。其实就是把 这一堆 datnode 实例对象,给保存到磁盘文件 + * datanode之间的关系,就由对应的 path 路径来决定 + * - + * 有一个写请求过来: LSM Tree 存储引擎 + * 1、先记录日志 append() + * 2、然后写数据到内存 datatree 中 + * 3、提交日志 commit() + * - + * zookeeper 的所有事务请求,全部都是由 leader 严格有序串行执行 + * 来一条事务,执行一条提交一条 buffer flush xxxxxx + */ + public void deserialize(InputArchive ia, String tag) throws IOException { + aclCache.deserialize(ia); + nodes.clear(); + pTrie.clear(); + nodeDataSize.set(0); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从快照文件中,依次恢复 znode 节点到 DataTree 中 + * 方式: + * 1、先读 path + * 2、再读 node + * datatree 在 snapfile 中的组织形式: + * path ==> node + * path ==> node + * .... + */ + String path = ia.readString("path"); + + // TODO_MA 注释: 一直不停的读 + while(!"/".equals(path)) { + + DataNode node = new DataNode(); + ia.readRecord(node, "node"); + nodes.put(path, node); + + synchronized(node) { + aclCache.addUsage(node.acl); + } + int lastSlash = path.lastIndexOf('/'); + if(lastSlash == -1) { + root = node; + } else { + String parentPath = path.substring(0, lastSlash); + DataNode parent = nodes.get(parentPath); + if(parent == null) { + throw new IOException( + "Invalid Datatree, unable to find " + "parent " + parentPath + " of path " + path); + } + parent.addChild(path.substring(lastSlash + 1)); + long eowner = node.stat.getEphemeralOwner(); + EphemeralType ephemeralType = EphemeralType.get(eowner); + if(ephemeralType == EphemeralType.CONTAINER) { + containers.add(path); + } else if(ephemeralType == EphemeralType.TTL) { + ttls.add(path); + } else if(eowner != 0) { + HashSet list = ephemerals.get(eowner); + if(list == null) { + list = new HashSet(); + ephemerals.put(eowner, list); + } + list.add(path); + } + } + + // TODO_MA 注释: 再读一个path,如果 path 为空,证明 znode 节点都恢复完了 + path = ia.readString("path"); + } + + // have counted digest for root node with "", ignore here to avoid + // counting twice for root node + nodes.putWithoutDigest("/", root); + + // TODO_MA 注释: 计算总结点数 + nodeDataSize.set(approximateDataSize()); + + // we are done with deserializing the the datatree + // update the quotas - create path trie and also update the stat nodes + setupQuota(); + + // TODO_MA 注释: 去重无用的 acl 信息 + aclCache.purgeUnused(); + } + + /** + * Summary of the watches on the datatree. + * + * @param pwriter the output to write to + */ + public synchronized void dumpWatchesSummary(PrintWriter pwriter) { + pwriter.print(dataWatches.toString()); + } + + /** + * Write a text dump of all the watches on the datatree. + * Warning, this is expensive, use sparingly! + * + * @param pwriter the output to write to + */ + public synchronized void dumpWatches(PrintWriter pwriter, boolean byPath) { + dataWatches.dumpWatches(pwriter, byPath); + } + + /** + * Returns a watch report. + * + * @return watch report + * @see WatchesReport + */ + public synchronized WatchesReport getWatches() { + return dataWatches.getWatches(); + } + + /** + * Returns a watch report by path. + * + * @return watch report + * @see WatchesPathReport + */ + public synchronized WatchesPathReport getWatchesByPath() { + return dataWatches.getWatchesByPath(); + } + + /** + * Returns a watch summary. + * + * @return watch summary + * @see WatchesSummary + */ + public synchronized WatchesSummary getWatchesSummary() { + return dataWatches.getWatchesSummary(); + } + + /** + * Write a text dump of all the ephemerals in the datatree. + * + * @param pwriter the output to write to + */ + public void dumpEphemerals(PrintWriter pwriter) { + pwriter.println("Sessions with Ephemerals (" + ephemerals.keySet().size() + "):"); + for(Entry> entry : ephemerals.entrySet()) { + pwriter.print("0x" + Long.toHexString(entry.getKey())); + pwriter.println(":"); + Set tmp = entry.getValue(); + if(tmp != null) { + synchronized(tmp) { + for(String path : tmp) { + pwriter.println("\t" + path); + } + } + } + } + } + + public void shutdownWatcher() { + dataWatches.shutdown(); + childWatches.shutdown(); + } + + /** + * Returns a mapping of session ID to ephemeral znodes. + * + * @return map of session ID to sets of ephemeral znodes + */ + public Map> getEphemerals() { + Map> ephemeralsCopy = new HashMap>(); + for(Entry> e : ephemerals.entrySet()) { + synchronized(e.getValue()) { + ephemeralsCopy.put(e.getKey(), new HashSet(e.getValue())); + } + } + return ephemeralsCopy; + } + + public void removeCnxn(Watcher watcher) { + dataWatches.removeWatcher(watcher); + childWatches.removeWatcher(watcher); + } + + public void setWatches(long relativeZxid, List dataWatches, List existWatches, + List childWatches, List persistentWatches, List persistentRecursiveWatches, + Watcher watcher) { + for(String path : dataWatches) { + DataNode node = getNode(path); + WatchedEvent e = null; + if(node == null) { + watcher.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path)); + } else if(node.stat.getMzxid() > relativeZxid) { + watcher.process(new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, path)); + } else { + this.dataWatches.addWatch(path, watcher); + } + } + for(String path : existWatches) { + DataNode node = getNode(path); + if(node != null) { + watcher.process(new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, path)); + } else { + this.dataWatches.addWatch(path, watcher); + } + } + for(String path : childWatches) { + DataNode node = getNode(path); + if(node == null) { + watcher.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path)); + } else if(node.stat.getPzxid() > relativeZxid) { + watcher.process(new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, path)); + } else { + this.childWatches.addWatch(path, watcher); + } + } + for(String path : persistentWatches) { + this.childWatches.addWatch(path, watcher, WatcherMode.PERSISTENT); + this.dataWatches.addWatch(path, watcher, WatcherMode.PERSISTENT); + } + for(String path : persistentRecursiveWatches) { + this.childWatches.addWatch(path, watcher, WatcherMode.PERSISTENT_RECURSIVE); + this.dataWatches.addWatch(path, watcher, WatcherMode.PERSISTENT_RECURSIVE); + } + } + + /** + * This method sets the Cversion and Pzxid for the specified node to the + * values passed as arguments. The values are modified only if newCversion + * is greater than the current Cversion. A NoNodeException is thrown if + * a znode for the specified path is not found. + * + * @param path Full path to the znode whose Cversion needs to be modified. + * A "/" at the end of the path is ignored. + * @param newCversion Value to be assigned to Cversion + * @param zxid Value to be assigned to Pzxid + * @throws KeeperException.NoNodeException If znode not found. + **/ + public void setCversionPzxid(String path, int newCversion, long zxid) throws KeeperException.NoNodeException { + if(path.endsWith("/")) { + path = path.substring(0, path.length() - 1); + } + DataNode node = nodes.get(path); + if(node == null) { + throw new KeeperException.NoNodeException(path); + } + synchronized(node) { + if(newCversion == -1) { + newCversion = node.stat.getCversion() + 1; + } + if(newCversion > node.stat.getCversion()) { + nodes.preChange(path, node); + node.stat.setCversion(newCversion); + node.stat.setPzxid(zxid); + nodes.postChange(path, node); + } + } + } + + public boolean containsWatcher(String path, WatcherType type, Watcher watcher) { + boolean containsWatcher = false; + switch(type) { + case Children: + containsWatcher = this.childWatches.containsWatcher(path, watcher); + break; + case Data: + containsWatcher = this.dataWatches.containsWatcher(path, watcher); + break; + case Any: + if(this.childWatches.containsWatcher(path, watcher)) { + containsWatcher = true; + } + if(this.dataWatches.containsWatcher(path, watcher)) { + containsWatcher = true; + } + break; + } + return containsWatcher; + } + + public boolean removeWatch(String path, WatcherType type, Watcher watcher) { + boolean removed = false; + switch(type) { + case Children: + removed = this.childWatches.removeWatcher(path, watcher); + break; + case Data: + removed = this.dataWatches.removeWatcher(path, watcher); + break; + case Any: + if(this.childWatches.removeWatcher(path, watcher)) { + removed = true; + } + if(this.dataWatches.removeWatcher(path, watcher)) { + removed = true; + } + break; + } + return removed; + } + + // visible for testing + public ReferenceCountedACLCache getReferenceCountedAclCache() { + return aclCache; + } + + private String getTopNamespace(String path) { + String[] parts = path.split("/"); + return parts.length > 1 ? parts[1] : null; + } + + private void updateReadStat(String path, long bytes) { + String namespace = getTopNamespace(path); + if(namespace == null) { + return; + } + long totalBytes = path.length() + bytes + STAT_OVERHEAD_BYTES; + ServerMetrics.getMetrics().READ_PER_NAMESPACE.add(namespace, totalBytes); + } + + private void updateWriteStat(String path, long bytes) { + String namespace = getTopNamespace(path); + if(namespace == null) { + return; + } + ServerMetrics.getMetrics().WRITE_PER_NAMESPACE.add(namespace, path.length() + bytes); + } + + /** + * Add the digest to the historical list, and update the latest zxid digest. + */ + private void logZxidDigest(long zxid, long digest) { + ZxidDigest zxidDigest = new ZxidDigest(zxid, digestCalculator.getDigestVersion(), digest); + lastProcessedZxidDigest = zxidDigest; + if(zxidDigest.zxid % DIGEST_LOG_INTERVAL == 0) { + synchronized(digestLog) { + digestLog.add(zxidDigest); + if(digestLog.size() > DIGEST_LOG_LIMIT) { + digestLog.poll(); + } + } + } + } + + /** + * Serializing the digest to snapshot, this is done after the data tree + * is being serialized, so when we replay the txns and it hits this zxid + * we know we should be in a non-fuzzy state, and have the same digest. + * + * @param oa the output stream to write to + * @return true if the digest is serialized successfully + */ + public boolean serializeZxidDigest(OutputArchive oa) throws IOException { + if(!ZooKeeperServer.isDigestEnabled()) { + return false; + } + + ZxidDigest zxidDigest = lastProcessedZxidDigest; + if(zxidDigest == null) { + // write an empty digest + zxidDigest = new ZxidDigest(); + } + zxidDigest.serialize(oa); + return true; + } + + /** + * Deserializing the zxid digest from the input stream and update the + * digestFromLoadedSnapshot. + * + * @param ia the input stream to read from + * @param startZxidOfSnapshot the zxid of snapshot file + * @return the true if it deserialized successfully + */ + public boolean deserializeZxidDigest(InputArchive ia, long startZxidOfSnapshot) throws IOException { + if(!ZooKeeperServer.isDigestEnabled()) { + return false; + } + + try { + ZxidDigest zxidDigest = new ZxidDigest(); + zxidDigest.deserialize(ia); + if(zxidDigest.zxid > 0) { + digestFromLoadedSnapshot = zxidDigest; + LOG.info( + "The digest in the snapshot has digest version of {}, " + ", with zxid as 0x{}, and digest value as {}", + digestFromLoadedSnapshot.digestVersion, Long.toHexString(digestFromLoadedSnapshot.zxid), + digestFromLoadedSnapshot.digest); + } else { + digestFromLoadedSnapshot = null; + LOG.info("The digest value is empty in snapshot"); + } + + // There is possibility that the start zxid of a snapshot might + // be larger than the digest zxid in snapshot. + // + // Known cases: + // + // The new leader set the last processed zxid to be the new + // epoch + 0, which is not mapping to any txn, and it uses + // this to take snapshot, which is possible if we don't + // clean database before switching to LOOKING. In this case + // the currentZxidDigest will be the zxid of last epoch and + // it's smaller than the zxid of the snapshot file. + // + // It's safe to reset the targetZxidDigest to null and start + // to compare digest when replaying the first txn, since it's + // a non fuzzy snapshot. + if(digestFromLoadedSnapshot != null && digestFromLoadedSnapshot.zxid < startZxidOfSnapshot) { + LOG.info( + "The zxid of snapshot digest 0x{} is smaller " + "than the known snapshot highest zxid, the snapshot " + "started with zxid 0x{}. It will be invalid to use " + "this snapshot digest associated with this zxid, will " + "ignore comparing it.", + Long.toHexString(digestFromLoadedSnapshot.zxid), Long.toHexString(startZxidOfSnapshot)); + digestFromLoadedSnapshot = null; + } + + return true; + } catch(EOFException e) { + LOG.warn("Got EOF exception while reading the digest, likely due to the reading an older snapshot."); + return false; + } + } + + /** + * Compares the actual tree's digest with that in the snapshot. + * Resets digestFromLoadedSnapshot after comparision. + * + * @param zxid zxid + */ + public void compareSnapshotDigests(long zxid) { + if(zxid == digestFromLoadedSnapshot.zxid) { + if(digestCalculator.getDigestVersion() != digestFromLoadedSnapshot.digestVersion) { + LOG.info("Digest version changed, local: {}, new: {}, skip comparing digest now.", + digestFromLoadedSnapshot.digestVersion, digestCalculator.getDigestVersion()); + digestFromLoadedSnapshot = null; + return; + } + if(getTreeDigest() != digestFromLoadedSnapshot.getDigest()) { + reportDigestMismatch(zxid); + } + digestFromLoadedSnapshot = null; + } else if(digestFromLoadedSnapshot.zxid != 0 && zxid > digestFromLoadedSnapshot.zxid) { + RATE_LOGGER.rateLimitLog("The txn 0x{} of snapshot digest does not " + "exist.", + Long.toHexString(digestFromLoadedSnapshot.zxid)); + } + } + + /** + * Compares the digest of the tree with the digest present in transaction digest. + * If there is any error, logs and alerts the watchers. + * + * @param header transaction header being applied + * @param txn transaction + * @param digest transaction digest + * @return false if digest in the txn doesn't match what we have now in + * the data tree + */ + public boolean compareDigest(TxnHeader header, Record txn, TxnDigest digest) { + long zxid = header.getZxid(); + + if(!ZooKeeperServer.isDigestEnabled() || digest == null) { + return true; + } + // do not compare digest if we're still in fuzzy state + if(digestFromLoadedSnapshot != null) { + return true; + } + // do not compare digest if there is digest version change + if(digestCalculator.getDigestVersion() != digest.getVersion()) { + RATE_LOGGER.rateLimitLog("Digest version not the same on zxid.", String.valueOf(zxid)); + return true; + } + + long logDigest = digest.getTreeDigest(); + long actualDigest = getTreeDigest(); + if(logDigest != actualDigest) { + reportDigestMismatch(zxid); + LOG.debug("Digest in log: {}, actual tree: {}", logDigest, actualDigest); + if(firstMismatchTxn) { + LOG.error("First digest mismatch on txn: {}, {}, " + "expected digest is {}, actual digest is {}, ", + header, txn, digest, actualDigest); + firstMismatchTxn = false; + } + return false; + } else { + RATE_LOGGER.flush(); + LOG.debug("Digests are matching for Zxid: {}, Digest in log " + "and actual tree: {}", Long.toHexString(zxid), + logDigest); + return true; + } + } + + /** + * Reports any mismatch in the transaction digest. + * + * @param zxid zxid for which the error is being reported. + */ + public void reportDigestMismatch(long zxid) { + ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT.add(1); + RATE_LOGGER.rateLimitLog("Digests are not matching. Value is Zxid.", String.valueOf(zxid)); + + for(DigestWatcher watcher : digestWatchers) { + watcher.process(zxid); + } + } + + public long getTreeDigest() { + return nodes.getDigest(); + } + + public ZxidDigest getLastProcessedZxidDigest() { + return lastProcessedZxidDigest; + } + + public ZxidDigest getDigestFromLoadedSnapshot() { + return digestFromLoadedSnapshot; + } + + /** + * Add digest mismatch event handler. + * + * @param digestWatcher the handler to add + */ + public void addDigestWatcher(DigestWatcher digestWatcher) { + digestWatchers.add(digestWatcher); + } + + /** + * Return all the digests in the historical digest list. + */ + public List getDigestLog() { + synchronized(digestLog) { + // Return a copy of current digest log + return new LinkedList(digestLog); + } + } + + /** + * A helper class to maintain the digest meta associated with specific zxid. + */ + public class ZxidDigest { + + long zxid; + // the digest value associated with this zxid + long digest; + // the version when the digest was calculated + int digestVersion; + + ZxidDigest() { + this(0, digestCalculator.getDigestVersion(), 0); + } + + ZxidDigest(long zxid, int digestVersion, long digest) { + this.zxid = zxid; + this.digestVersion = digestVersion; + this.digest = digest; + } + + public void serialize(OutputArchive oa) throws IOException { + oa.writeLong(zxid, "zxid"); + oa.writeInt(digestVersion, "digestVersion"); + oa.writeLong(digest, "digest"); + } + + public void deserialize(InputArchive ia) throws IOException { + zxid = ia.readLong("zxid"); + digestVersion = ia.readInt("digestVersion"); + // the old version is using hex string as the digest + if(digestVersion < 2) { + String d = ia.readString("digest"); + if(d != null) { + digest = Long.parseLong(d, 16); + } + } else { + digest = ia.readLong("digest"); + } + } + + public long getZxid() { + return zxid; + } + + public int getDigestVersion() { + return digestVersion; + } + + public long getDigest() { + return digest; + } + + } + + /** + * Create a node stat from the given params. + * + * @param zxid the zxid associated with the txn + * @param time the time when the txn is created + * @param ephemeralOwner the owner if the node is an ephemeral + * @return the stat + */ + public static StatPersisted createStat(long zxid, long time, long ephemeralOwner) { + StatPersisted stat = new StatPersisted(); + stat.setCtime(time); + stat.setMtime(time); + stat.setCzxid(zxid); + stat.setMzxid(zxid); + stat.setPzxid(zxid); + stat.setVersion(0); + stat.setAversion(0); + stat.setEphemeralOwner(ephemeralOwner); + return stat; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTreeBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTreeBean.java new file mode 100644 index 0000000..021e528 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTreeBean.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.apache.zookeeper.jmx.ZKMBeanInfo; + +/** + * This class implements the data tree MBean. + */ +public class DataTreeBean implements DataTreeMXBean, ZKMBeanInfo { + + DataTree dataTree; + + public DataTreeBean(org.apache.zookeeper.server.DataTree dataTree) { + this.dataTree = dataTree; + } + + public int getNodeCount() { + return dataTree.getNodeCount(); + } + + public long approximateDataSize() { + return dataTree.cachedApproximateDataSize(); + } + + public int countEphemerals() { + return dataTree.getEphemeralsCount(); + } + + public int getWatchCount() { + return dataTree.getWatchCount(); + } + + public String getName() { + return "InMemoryDataTree"; + } + + public boolean isHidden() { + return false; + } + + public String getLastZxid() { + return "0x" + Long.toHexString(dataTree.lastProcessedZxid); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTreeMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTreeMXBean.java new file mode 100644 index 0000000..e470b10 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DataTreeMXBean.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * Zookeeper data tree MBean. + */ +public interface DataTreeMXBean { + + /** + * @return number of znodes in the data tree. + */ + int getNodeCount(); + /** + * @return the most recent zxid processed by the data tree. + */ + String getLastZxid(); + /** + * @return number of watches set. + */ + int getWatchCount(); + + /** + * @return data tree size in bytes. The size includes the znode path and + * its value. + */ + long approximateDataSize(); + /** + * @return number of ephemeral nodes in the data tree + */ + int countEphemerals(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java new file mode 100644 index 0000000..096ae09 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DatadirCleanupManager.java @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.File; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class manages the cleanup of snapshots and corresponding transaction + * logs by scheduling the auto purge task with the specified + * 'autopurge.purgeInterval'. It keeps the most recent + * 'autopurge.snapRetainCount' number of snapshots and corresponding transaction + * logs. + */ +public class DatadirCleanupManager { + + private static final Logger LOG = LoggerFactory.getLogger(DatadirCleanupManager.class); + + /** + * Status of the dataDir purge task + */ + public enum PurgeTaskStatus { + NOT_STARTED, + STARTED, + COMPLETED + } + + private PurgeTaskStatus purgeTaskStatus = PurgeTaskStatus.NOT_STARTED; + + private final File snapDir; + + private final File dataLogDir; + + private final int snapRetainCount; + + private final int purgeInterval; + + private Timer timer; + + /** + * Constructor of DatadirCleanupManager. It takes the parameters to schedule + * the purge task. + * + * @param snapDir + * snapshot directory + * @param dataLogDir + * transaction log directory + * @param snapRetainCount + * number of snapshots to be retained after purge + * @param purgeInterval + * purge interval in hours + */ + public DatadirCleanupManager(File snapDir, File dataLogDir, int snapRetainCount, int purgeInterval) { + this.snapDir = snapDir; + this.dataLogDir = dataLogDir; + this.snapRetainCount = snapRetainCount; + this.purgeInterval = purgeInterval; + LOG.info("autopurge.snapRetainCount set to {}", snapRetainCount); + LOG.info("autopurge.purgeInterval set to {}", purgeInterval); + } + + /** + * Validates the purge configuration and schedules the purge task. Purge + * task keeps the most recent snapRetainCount number of + * snapshots and deletes the remaining for every purgeInterval + * hour(s). + *

      + * purgeInterval of 0 or + * negative integer will not schedule the purge task. + *

      + * + * @see PurgeTxnLog#purge(File, File, int) + */ + public void start() { + if (PurgeTaskStatus.STARTED == purgeTaskStatus) { + LOG.warn("Purge task is already running."); + return; + } + // Don't schedule the purge task with zero or negative purge interval. + if (purgeInterval <= 0) { + LOG.info("Purge task is not scheduled."); + return; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 调度一个 PurgeTask 任务 + * 有两个重要的参数,一定要注意: + * 1、snapRetainCount 至少保留的快照个数,默认3 + * 2、purgeInterval 定时任务的时间间隔,默认好像是1 + * - + * 所以如果,这两个值需要调整,请在 zoo.cfg 中进行配置 + */ + timer = new Timer("PurgeTask", true); + TimerTask task = new PurgeTask(dataLogDir, snapDir, snapRetainCount); + timer.scheduleAtFixedRate(task, 0, TimeUnit.HOURS.toMillis(purgeInterval)); + + // TODO_MA 注释: 这句代码结束之后,其实就是定期,执行 PurgeTask.run() 一次 + + purgeTaskStatus = PurgeTaskStatus.STARTED; + } + + /** + * Shutdown the purge task. + */ + public void shutdown() { + if (PurgeTaskStatus.STARTED == purgeTaskStatus) { + LOG.info("Shutting down purge task."); + timer.cancel(); + purgeTaskStatus = PurgeTaskStatus.COMPLETED; + } else { + LOG.warn("Purge task not started. Ignoring shutdown!"); + } + } + + static class PurgeTask extends TimerTask { + + private File logsDir; + private File snapsDir; + private int snapRetainCount; + + public PurgeTask(File dataDir, File snapDir, int count) { + logsDir = dataDir; + snapsDir = snapDir; + snapRetainCount = count; + } + + @Override + public void run() { + LOG.info("Purge task started."); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + PurgeTxnLog.purge(logsDir, snapsDir, snapRetainCount); + } catch (Exception e) { + LOG.error("Error occurred while purging.", e); + } + LOG.info("Purge task completed."); + } + + } + + /** + * Returns the status of the purge task. + * + * @return the status of the purge task + */ + public PurgeTaskStatus getPurgeTaskStatus() { + return purgeTaskStatus; + } + + /** + * Returns the snapshot directory. + * + * @return the snapshot directory. + */ + public File getSnapDir() { + return snapDir; + } + + /** + * Returns transaction log directory. + * + * @return the transaction log directory. + */ + public File getDataLogDir() { + return dataLogDir; + } + + /** + * Returns purge interval in hours. + * + * @return the purge interval in hours. + */ + public int getPurgeInterval() { + return purgeInterval; + } + + /** + * Returns the number of snapshots to be retained after purge. + * + * @return the number of snapshots to be retained after purge. + */ + public int getSnapRetainCount() { + return snapRetainCount; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DigestCalculator.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DigestCalculator.java new file mode 100644 index 0000000..0568af6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DigestCalculator.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.nio.ByteBuffer; +import java.util.zip.CRC32; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.StatPersisted; + +/** + * Defines how to calculate the digest for a given node. + */ +public class DigestCalculator { + + // The hardcoded digest version, should bump up this version whenever + // we changed the digest method or fields. + private static final int DIGEST_VERSION = 2; + + + /** + * Calculate the digest based on the given params. + * + * Besides the path and data, the following stat fields are included in + * the digest calculation: + * + * - long czxid 8 bytes + * - long mzxid 8 bytes + * - long pzxid 8 bytes + * - long ctime 8 bytes + * - long mtime 8 bytes + * - int version 4 bytes + * - int cversion 4 bytes + * - int aversion 4 bytes + * - long ephemeralOwner 8 bytes + * + * @param path the path of the node + * @param data the data of the node + * @param stat the stat associated with the node + * @return the digest calculated from the given params + */ + long calculateDigest(String path, byte[] data, StatPersisted stat) { + + if (!ZooKeeperServer.isDigestEnabled()) { + return 0; + } + + // Quota nodes are updated locally, there is inconsistent issue + // when we tried to release digest feature at the beginning. + // + // Instead of taking time to fix that, we decided to disable digest + // check for all the nodes under /zookeeper/ first. + // + // We can enable this after fixing that inconsistent problem. The + // digest version in the protocol enables us to change the digest + // calculation without disrupting the system. + if (path.startsWith(ZooDefs.ZOOKEEPER_NODE_SUBTREE)) { + return 0; + } + + // "" and "/" are aliases to each other, in DataTree when adding child + // under "/", it will use "" as the path, but when set data or change + // ACL on "/", it will use "/" as the path. Always mapping "/" to "" + // to avoid mismatch. + if (path.equals("/")) { + path = ""; + } + + // TODO_MA 注释: 总共 60 个字节 + // total = 8 * 6 + 4 * 3 = 60 bytes + byte[] b = new byte[60]; + + ByteBuffer bb = ByteBuffer.wrap(b); + bb.putLong(stat.getCzxid()); + bb.putLong(stat.getMzxid()); + bb.putLong(stat.getPzxid()); + bb.putLong(stat.getCtime()); + bb.putLong(stat.getMtime()); + bb.putInt(stat.getVersion()); + bb.putInt(stat.getCversion()); + bb.putInt(stat.getAversion()); + bb.putLong(stat.getEphemeralOwner()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 通过 CRC32(循环冗余校验/加密算法) 算法进行加密编码 + * 检错能力极强,开销小,易于用编码器 + */ + CRC32 crc = new CRC32(); + crc.update(path.getBytes()); + if (data != null) { + crc.update(data); + } + crc.update(b); + return crc.getValue(); + } + + /** + * Calculate the digest based on the given path and data node. + */ + long calculateDigest(String path, DataNode node) { + if (!node.isDigestCached()) { + node.setDigest(calculateDigest(path, node.getData(), node.stat)); + node.setDigestCached(true); + } + return node.getDigest(); + } + + /** + * Returns with the current digest version. + */ + int getDigestVersion() { + return DIGEST_VERSION; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/DumbWatcher.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DumbWatcher.java new file mode 100644 index 0000000..9b9672a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/DumbWatcher.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.security.cert.Certificate; +import org.apache.jute.Record; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.ReplyHeader; + +/** + * A empty watcher implementation used in bench and unit test. + */ +public class DumbWatcher extends ServerCnxn { + + private long sessionId; + + public DumbWatcher() { + this(0); + } + + public DumbWatcher(long sessionId) { + super(null); + this.sessionId = sessionId; + } + + @Override + void setSessionTimeout(int sessionTimeout) { + } + + @Override + public void process(WatchedEvent event) { + } + + @Override + int getSessionTimeout() { + return 0; + } + + @Override + public void close(DisconnectReason reason) { + } + + @Override + public void sendResponse(ReplyHeader h, Record r, String tag, + String cacheKey, Stat stat, int opCode) throws IOException { + } + + @Override + public void sendCloseSession() { + } + + @Override + public long getSessionId() { + return sessionId; + } + + @Override + void setSessionId(long sessionId) { + } + + @Override + void sendBuffer(ByteBuffer... closeConn) { + } + + @Override + void enableRecv() { + } + + @Override + void disableRecv(boolean waitDisableRecv) { + } + + @Override + protected ServerStats serverStats() { + return null; + } + + @Override + public long getOutstandingRequests() { + return 0; + } + + @Override + public InetSocketAddress getRemoteSocketAddress() { + return null; + } + + @Override + public int getInterestOps() { + return 0; + } + + @Override + public boolean isSecure() { + return false; + } + + @Override + public Certificate[] getClientCertificateChain() { + return null; + } + + @Override + public void setClientCertificateChain(Certificate[] chain) { + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/EphemeralType.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/EphemeralType.java new file mode 100644 index 0000000..3775b98 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/EphemeralType.java @@ -0,0 +1,233 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.CreateMode; + +/** + *

      + * Abstraction that interprets the ephemeralOwner field of a ZNode. Originally, + * the ephemeralOwner noted that a ZNode is ephemeral and which session created the node. + * Through an optional system property (zookeeper.extendedTypesEnabled) "extended" + * features such as TTL Nodes can be enabled. Special bits of the ephemeralOwner are used to + * denote which feature is enabled and the remaining bits of the ephemeralOwner are feature + * specific. + *

      + *

      + *

      + * When the system property zookeeper.extendedTypesEnabled is true, extended types + * are enabled. An extended ephemeralOwner is defined as an ephemeralOwner whose high 8 bits are + * set (0xff00000000000000L). The two bytes that follow the high 8 bits are + * used to denote which extended feature the ephemeralOwner represents. The remaining 5 bytes are + * used by the feature for whatever purpose is needed + *

      + *

      + *

      + * Currently, the only extended feature is TTL Nodes. It is denoted by the extended feature value of 0. + * i.e. for TTL Nodes, the ephemeralOwner has the high byte set to 0xff and the next 2 bytes are 0 followed + * by 5 bytes that represent the TTL value in milliseconds. So, an ephemeralOwner with a TTL value of 1 + * millisecond is: 0xff00000000000001. + *

      + *

      + *

      + * To add new extended features: a) Add a new name to the enum, b) define a constant EXTENDED_BIT_xxxx that's next + * in line (after TTLs, that would be 0x0001), c) add a mapping to the extendedFeatureMap via the static + * initializer + *

      + *

      + *

      + * NOTE: "Container" nodes technically are extended types but as it was implemented before this feature they are + * denoted specially. An ephemeral owner with only the high bit set (0x8000000000000000L) is by definition + * a container node (irrespective of whether or not extended types are enabled). + *

      + */ +public enum EphemeralType { + /** + * Not ephemeral + */ + VOID, + /** + * Standard, pre-3.5.x EPHEMERAL + */ + NORMAL, + /** + * Container node + */ + CONTAINER, + /** + * TTL node + */ + TTL() { + @Override + public long maxValue() { + return EXTENDED_FEATURE_VALUE_MASK; // 12725 days, about 34 years + } + + @Override + public long toEphemeralOwner(long ttl) { + if ((ttl > TTL.maxValue()) || (ttl <= 0)) { + throw new IllegalArgumentException("ttl must be positive and cannot be larger than: " + TTL.maxValue()); + } + //noinspection PointlessBitwiseExpression + return EXTENDED_MASK + | EXTENDED_BIT_TTL + | ttl; // TTL_RESERVED_BIT is actually zero - but it serves to document that the proper extended bit needs to be set + } + + @Override + public long getValue(long ephemeralOwner) { + return getExtendedFeatureValue(ephemeralOwner); + } + }; + + /** + * For types that support it, the maximum extended value + * + * @return 0 or max + */ + public long maxValue() { + return 0; + } + + /** + * For types that support it, convert a value to an extended ephemeral owner + * + * @return 0 or extended ephemeral owner + */ + public long toEphemeralOwner(long value) { + return 0; + } + + /** + * For types that support it, return the extended value from an extended ephemeral owner + * + * @return 0 or extended value + */ + public long getValue(long ephemeralOwner) { + return 0; + } + + public static final long CONTAINER_EPHEMERAL_OWNER = Long.MIN_VALUE; + public static final long MAX_EXTENDED_SERVER_ID = 0xfe; // 254 + + private static final long EXTENDED_MASK = 0xff00000000000000L; + private static final long EXTENDED_BIT_TTL = 0x0000; + private static final long RESERVED_BITS_MASK = 0x00ffff0000000000L; + private static final long RESERVED_BITS_SHIFT = 40; + + private static final Map extendedFeatureMap; + + static { + Map map = new HashMap<>(); + map.put(EXTENDED_BIT_TTL, TTL); + extendedFeatureMap = Collections.unmodifiableMap(map); + } + + private static final long EXTENDED_FEATURE_VALUE_MASK = ~(EXTENDED_MASK | RESERVED_BITS_MASK); + + // Visible for testing + static final String EXTENDED_TYPES_ENABLED_PROPERTY = "zookeeper.extendedTypesEnabled"; + static final String TTL_3_5_3_EMULATION_PROPERTY = "zookeeper.emulate353TTLNodes"; + + /** + * Return true if extended ephemeral types are enabled + * + * @return true/false + */ + public static boolean extendedEphemeralTypesEnabled() { + return Boolean.getBoolean(EXTENDED_TYPES_ENABLED_PROPERTY); + } + + /** + * Convert a ZNode ephemeral owner to an ephemeral type. If extended types are not + * enabled, VOID or NORMAL is always returned + * + * @param ephemeralOwner the ZNode's ephemeral owner + * @return type + */ + public static EphemeralType get(long ephemeralOwner) { + if (extendedEphemeralTypesEnabled()) { + if (Boolean.getBoolean(TTL_3_5_3_EMULATION_PROPERTY)) { + if (EphemeralTypeEmulate353.get(ephemeralOwner) == EphemeralTypeEmulate353.TTL) { + return TTL; + } + } + + if ((ephemeralOwner & EXTENDED_MASK) == EXTENDED_MASK) { + long extendedFeatureBit = getExtendedFeatureBit(ephemeralOwner); + EphemeralType ephemeralType = extendedFeatureMap.get(extendedFeatureBit); + if (ephemeralType == null) { + throw new IllegalArgumentException(String.format("Invalid ephemeralOwner. [%s]", Long.toHexString(ephemeralOwner))); + } + return ephemeralType; + } + } + if (ephemeralOwner == CONTAINER_EPHEMERAL_OWNER) { + return CONTAINER; + } + return (ephemeralOwner == 0) ? VOID : NORMAL; + } + + /** + * Make sure the given server ID is compatible with the current extended ephemeral setting + * + * @param serverId Server ID + * @throws RuntimeException extendedTypesEnabled is true but Server ID is too large + */ + public static void validateServerId(long serverId) { + // TODO: in the future, serverId should be validated for all cases, not just the extendedEphemeralTypesEnabled case + // TODO: however, for now, it would be too disruptive + + if (extendedEphemeralTypesEnabled()) { + if (serverId > EphemeralType.MAX_EXTENDED_SERVER_ID) { + throw new RuntimeException( + "extendedTypesEnabled is true but Server ID is too large. Cannot be larger than " + + EphemeralType.MAX_EXTENDED_SERVER_ID); + } + } + } + + /** + * Utility to validate a create mode and a ttl + * + * @param mode create mode + * @param ttl ttl + * @throws IllegalArgumentException if the ttl is not valid for the mode + */ + @SuppressFBWarnings(value = "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", justification = "toEphemeralOwner may throw IllegalArgumentException") + public static void validateTTL(CreateMode mode, long ttl) { + if (mode.isTTL()) { + TTL.toEphemeralOwner(ttl); + } else if (ttl >= 0) { + throw new IllegalArgumentException("ttl not valid for mode: " + mode); + } + } + + private static long getExtendedFeatureBit(long ephemeralOwner) { + return (ephemeralOwner & RESERVED_BITS_MASK) >> RESERVED_BITS_SHIFT; + } + + private static long getExtendedFeatureValue(long ephemeralOwner) { + return ephemeralOwner & EXTENDED_FEATURE_VALUE_MASK; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/EphemeralTypeEmulate353.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/EphemeralTypeEmulate353.java new file mode 100644 index 0000000..e56da89 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/EphemeralTypeEmulate353.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * See https://issues.apache.org/jira/browse/ZOOKEEPER-2901 + * + * version 3.5.3 introduced bugs associated with how TTL nodes were implemented. version 3.5.4 + * fixes the problems but makes TTL nodes created in 3.5.3 invalid. EphemeralTypeEmulate353 is a copy + * of the old - bad - implementation that is provided as a workaround. {@link EphemeralType#TTL_3_5_3_EMULATION_PROPERTY} + * can be used to emulate support of the badly specified TTL nodes. + */ +public enum EphemeralTypeEmulate353 { + /** + * Not ephemeral + */ + VOID, + /** + * Standard, pre-3.5.x EPHEMERAL + */ + NORMAL, + /** + * Container node + */ + CONTAINER, + /** + * TTL node + */ + TTL; + + public static final long CONTAINER_EPHEMERAL_OWNER = Long.MIN_VALUE; + public static final long MAX_TTL = 0x0fffffffffffffffL; + public static final long TTL_MASK = 0x8000000000000000L; + + public static EphemeralTypeEmulate353 get(long ephemeralOwner) { + if (ephemeralOwner == CONTAINER_EPHEMERAL_OWNER) { + return CONTAINER; + } + if (ephemeralOwner < 0) { + return TTL; + } + return (ephemeralOwner == 0) ? VOID : NORMAL; + } + + public static long ttlToEphemeralOwner(long ttl) { + if ((ttl > MAX_TTL) || (ttl <= 0)) { + throw new IllegalArgumentException("ttl must be positive and cannot be larger than: " + MAX_TTL); + } + return TTL_MASK | ttl; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ExitCode.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ExitCode.java new file mode 100644 index 0000000..67af2c8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ExitCode.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * Exit code used to exit server + */ +public enum ExitCode { + + /** Execution finished normally */ + EXECUTION_FINISHED(0), + + /** Unexpected errors like IO Exceptions */ + UNEXPECTED_ERROR(1), + + /** Invalid arguments during invocations */ + INVALID_INVOCATION(2), + + /** Cannot access datadir when trying to replicate server */ + UNABLE_TO_ACCESS_DATADIR(3), + + /** Unable to start admin server at ZooKeeper startup */ + ERROR_STARTING_ADMIN_SERVER(4), + + /** Severe error during snapshot IO */ + TXNLOG_ERROR_TAKING_SNAPSHOT(10), + + /** zxid from COMMIT does not match the one from pendingTxns queue */ + UNMATCHED_TXN_COMMIT(12), + + /** Unexpected packet from leader, or unable to truncate log on Leader.TRUNC */ + QUORUM_PACKET_ERROR(13), + + /** Unable to bind to the quorum (election) port after multiple retry */ + UNABLE_TO_BIND_QUORUM_PORT(14); + + private final int value; + + ExitCode(final int newValue) { + value = newValue; + } + + public int getValue() { + return value; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ExpiryQueue.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ExpiryQueue.java new file mode 100644 index 0000000..8787e64 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ExpiryQueue.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.zookeeper.common.Time; + +/** + * ExpiryQueue tracks elements in time sorted fixed duration buckets. + * It's used by SessionTrackerImpl to expire sessions and NIOServerCnxnFactory to expire connections. + * // TODO_MA 注释: 两个作用: + * // TODO_MA 注释: 1、SessionTrackerImpl to expire sessions + * // TODO_MA 注释: 2、NIOServerCnxnFactory to expire connections + */ +public class ExpiryQueue { + + // TODO_MA 注释: E 是管理对象,比如 Session, value Long 是超时时间 + private final ConcurrentHashMap elemMap = new ConcurrentHashMap(); + + /** + * The maximum number of buckets is equal to max timeout/expirationInterval, + * so the expirationInterval should not be too small compared to the + * max timeout that this expiry queue needs to maintain. + * // TODO_MA 注释: 最核心的数据结构 key = 超时时间, value = 需要进行超时处理的一个集合 + */ + private final ConcurrentHashMap> expiryMap = new ConcurrentHashMap>(); + + private final AtomicLong nextExpirationTime = new AtomicLong(); + + // TODO_MA 注释: 默认 10 s,桶间隔,意味着,每隔 10s 执行一个 Set 的过期 + private final int expirationInterval; + + public ExpiryQueue(int expirationInterval) { + this.expirationInterval = expirationInterval; + nextExpirationTime.set(roundToNextInterval(Time.currentElapsedTime())); + } + + private long roundToNextInterval(long time) { + return (time / expirationInterval + 1) * expirationInterval; + } + + /** + * Removes element from the queue. + * + * @param elem element to remove + * @return time at which the element was set to expire, or null if + * it wasn't present + */ + public Long remove(E elem) { + Long expiryTime = elemMap.remove(elem); + if(expiryTime != null) { + Set set = expiryMap.get(expiryTime); + if(set != null) { + set.remove(elem); + // We don't need to worry about removing empty sets, + // they'll eventually be removed when they expire. + } + } + return expiryTime; + } + + /** + * Adds or updates expiration time for element in queue, rounding the + * timeout to the expiry interval bucketed used by this queue. + * + * @param elem element to add/update + * @param timeout timout in milliseconds + * @return time at which the element is now set to expire if + * changed, or null if unchanged + */ + public Long update(E elem, int timeout) { + + // TODO_MA 注释: 获取当前 Session elem 的超时时间 + Long prevExpiryTime = elemMap.get(elem); + + // TODO_MA 注释: 计算 它对应的 session bucket + long now = Time.currentElapsedTime(); + Long newExpiryTime = roundToNextInterval(now + timeout); + + // TODO_MA 注释: 如果前后两次 expireTime 的更新依然处于同一个桶,则不做任何操作 + if(newExpiryTime.equals(prevExpiryTime)) { + // No change, so nothing to update + return null; + } + + // TODO_MA 注释: 根据桶对应的 ExpiryTime 找到 存储会话的 set 集合, 这个 set 集合就是一个所谓的 桶 + // TODO_MA 注释: 所以其实 Session 管理,就是把所有 Session 分散成多个桶。每隔一段时间,对一个桶的所有 Session 执行过期处理 + // First add the elem to the new expiry time bucket in expiryMap. + Set set = expiryMap.get(newExpiryTime); + + // TODO_MA 注释: 如果桶为空,则创建桶,加入到 expiryMap 中 + if(set == null) { + // Construct a ConcurrentHashSet using a ConcurrentHashMap + set = Collections.newSetFromMap(new ConcurrentHashMap()); + // Put the new set in the map, but only if another thread + // hasn't beaten us to it + Set existingSet = expiryMap.putIfAbsent(newExpiryTime, set); + if(existingSet != null) { + set = existingSet; + } + } + // TODO_MA 注释: 将该 Session 加入到新的桶中 + set.add(elem); + + // TODO_MA 注释: 更新 elemMap 中的桶信息 + // Map the elem to the new expiry time. If a different previous + // mapping was present, clean up the previous expiry bucket. + prevExpiryTime = elemMap.put(elem, newExpiryTime); + if(prevExpiryTime != null && !newExpiryTime.equals(prevExpiryTime)) { + Set prevSet = expiryMap.get(prevExpiryTime); + if(prevSet != null) { + prevSet.remove(elem); + } + } + return newExpiryTime; + } + + /** + * @return milliseconds until next expiration time, or 0 if has already past + */ + public long getWaitTime() { + long now = Time.currentElapsedTime(); + long expirationTime = nextExpirationTime.get(); + return now < expirationTime ? (expirationTime - now) : 0L; + } + + /** + * Remove the next expired set of elements from expireMap. This method needs + * to be called frequently enough by checking getWaitTime(), otherwise there + * will be a backlog of empty sets queued up in expiryMap. + * + * @return next set of expired elements, or an empty set if none are ready + */ + public Set poll() { + long now = Time.currentElapsedTime(); + long expirationTime = nextExpirationTime.get(); + if(now < expirationTime) { + return Collections.emptySet(); + } + + Set set = null; + long newExpirationTime = expirationTime + expirationInterval; + if(nextExpirationTime.compareAndSet(expirationTime, newExpirationTime)) { + set = expiryMap.remove(expirationTime); + } + if(set == null) { + return Collections.emptySet(); + } + return set; + } + + public void dump(PrintWriter pwriter) { + pwriter.print("Sets ("); + pwriter.print(expiryMap.size()); + pwriter.print(")/("); + pwriter.print(elemMap.size()); + pwriter.println("):"); + ArrayList keys = new ArrayList(expiryMap.keySet()); + Collections.sort(keys); + for(long time : keys) { + Set set = expiryMap.get(time); + if(set != null) { + pwriter.print(set.size()); + pwriter.print(" expire at "); + pwriter.print(Time.elapsedTimeToDate(time)); + pwriter.println(":"); + for(E elem : set) { + pwriter.print("\t"); + pwriter.println(elem.toString()); + } + } + } + } + + /** + * Returns an unmodifiable view of the expiration time -> elements mapping. + */ + public Map> getExpiryMap() { + return Collections.unmodifiableMap(expiryMap); + } + +} + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/FinalRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/FinalRequestProcessor.java new file mode 100644 index 0000000..50e1eca --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/FinalRequestProcessor.java @@ -0,0 +1,680 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import org.apache.jute.Record; +import org.apache.zookeeper.ClientCnxn; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.MultiOperationRecord; +import org.apache.zookeeper.MultiResponse; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.OpResult.CheckResult; +import org.apache.zookeeper.OpResult.CreateResult; +import org.apache.zookeeper.OpResult.DeleteResult; +import org.apache.zookeeper.OpResult.ErrorResult; +import org.apache.zookeeper.OpResult.GetChildrenResult; +import org.apache.zookeeper.OpResult.GetDataResult; +import org.apache.zookeeper.OpResult.SetDataResult; +import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.audit.AuditHelper; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.AddWatchRequest; +import org.apache.zookeeper.proto.CheckWatchesRequest; +import org.apache.zookeeper.proto.Create2Response; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.ErrorResponse; +import org.apache.zookeeper.proto.ExistsRequest; +import org.apache.zookeeper.proto.ExistsResponse; +import org.apache.zookeeper.proto.GetACLRequest; +import org.apache.zookeeper.proto.GetACLResponse; +import org.apache.zookeeper.proto.GetAllChildrenNumberRequest; +import org.apache.zookeeper.proto.GetAllChildrenNumberResponse; +import org.apache.zookeeper.proto.GetChildren2Request; +import org.apache.zookeeper.proto.GetChildren2Response; +import org.apache.zookeeper.proto.GetChildrenRequest; +import org.apache.zookeeper.proto.GetChildrenResponse; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.proto.GetDataResponse; +import org.apache.zookeeper.proto.GetEphemeralsRequest; +import org.apache.zookeeper.proto.GetEphemeralsResponse; +import org.apache.zookeeper.proto.RemoveWatchesRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.SetACLResponse; +import org.apache.zookeeper.proto.SetDataResponse; +import org.apache.zookeeper.proto.SetWatches; +import org.apache.zookeeper.proto.SetWatches2; +import org.apache.zookeeper.proto.SyncRequest; +import org.apache.zookeeper.proto.SyncResponse; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.quorum.QuorumZooKeeperServer; +import org.apache.zookeeper.server.util.RequestPathMetricsCollector; +import org.apache.zookeeper.txn.ErrorTxn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This Request processor actually applies any transaction associated with a + * request and services any queries. It is always at the end of a + * RequestProcessor chain (hence the name), so it does not have a nextProcessor member. + * + * This RequestProcessor counts on ZooKeeperServer to populate the + * outstandingRequests member of ZooKeeperServer. + */ +public class FinalRequestProcessor implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(FinalRequestProcessor.class); + + private final RequestPathMetricsCollector requestPathMetricsCollector; + + ZooKeeperServer zks; + + public FinalRequestProcessor(ZooKeeperServer zks) { + this.zks = zks; + this.requestPathMetricsCollector = zks.getRequestPathMetricsCollector(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: Final 更新数据到内存 + */ + public void processRequest(Request request) { + LOG.debug("Processing request:: {}", request); + + // request.addRQRec(">final"); + long traceMask = ZooTrace.CLIENT_REQUEST_TRACE_MASK; + if(request.type == OpCode.ping) { + traceMask = ZooTrace.SERVER_PING_TRACE_MASK; + } + if(LOG.isTraceEnabled()) { + ZooTrace.logRequest(LOG, traceMask, 'E', request, ""); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理事务请求,更新数据到 内存/DataTree + * ZKDatabase: + * 1、先广播事务到所有的事务参与者节点 + * 2、记录操作日志 + * 3、最终提交 + * 4、更新数据到内存 + */ + ProcessTxnResult rc = zks.processTxn(request); + + // ZOOKEEPER-558: + // In some cases the server does not close the connection (e.g., closeconn buffer + // was not being queued — ZOOKEEPER-558) properly. This happens, for example, + // when the client closes the connection. The server should still close the session, though. + // Calling closeSession() after losing the cnxn, results in the client close session response being dropped. + if(request.type == OpCode.closeSession && connClosedByClient(request)) { + // We need to check if we can close the session id. + // Sometimes the corresponding ServerCnxnFactory could be null because + // we are just playing diffs from the leader. + if(closeSession(zks.serverCnxnFactory, request.sessionId) || closeSession(zks.secureServerCnxnFactory, + request.sessionId)) { + return; + } + } + + if(request.getHdr() != null) { + /* + * Request header is created only by the leader, so this must be + * a quorum request. Since we're comparing timestamps across hosts, + * this metric may be incorrect. However, it's still a very useful + * metric to track in the happy case. If there is clock drift, + * the latency can go negative. Note: headers use wall time, not + * CLOCK_MONOTONIC. + */ + long propagationLatency = Time.currentWallTime() - request.getHdr().getTime(); + if(propagationLatency >= 0) { + ServerMetrics.getMetrics().PROPAGATION_LATENCY.add(propagationLatency); + } + } + + if(request.cnxn == null) { + return; + } + ServerCnxn cnxn = request.cnxn; + + // TODO_MA 注释: 得到 zxid + long lastZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + + String lastOp = "NA"; + // Notify ZooKeeperServer that the request has finished so that it can + // update any request accounting/throttling limits + zks.decInProcess(); + zks.requestFinished(request); + + Code err = Code.OK; + Record rsp = null; + String path = null; + try { + if(request.getHdr() != null && request.getHdr().getType() == OpCode.error) { + AuditHelper.addAuditLog(request, rc, true); + /* + * When local session upgrading is disabled, leader will + * reject the ephemeral node creation due to session expire. + * However, if this is the follower that issue the request, + * it will have the correct error code, so we should use that + * and report to user + */ + if(request.getException() != null) { + throw request.getException(); + } else { + throw KeeperException.create(KeeperException.Code.get(((ErrorTxn) request.getTxn()).getErr())); + } + } + + KeeperException ke = request.getException(); + if(ke instanceof SessionMovedException) { + throw ke; + } + if(ke != null && request.type != OpCode.multi) { + throw ke; + } + + LOG.debug("{}", request); + + if(request.isStale()) { + ServerMetrics.getMetrics().STALE_REPLIES.add(1); + } + AuditHelper.addAuditLog(request, rc); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 根据请求类型,返回响应 + */ + switch(request.type) { + case OpCode.ping: { + lastOp = "PING"; + updateStats(request, lastOp, lastZxid); + // TODO_MA 注释: 发送响应回 Client + cnxn.sendResponse(new ReplyHeader(ClientCnxn.PING_XID, lastZxid, 0), null, "response"); + return; + } + case OpCode.createSession: { + lastOp = "SESS"; + updateStats(request, lastOp, lastZxid); + zks.finishSessionInit(request.cnxn, true); + return; + } + case OpCode.multi: { + lastOp = "MULT"; + rsp = new MultiResponse(); + + // TODO_MA 注释: 构建事务请求的操作结果 + for(ProcessTxnResult subTxnResult : rc.multiResult) { + OpResult subResult; + switch(subTxnResult.type) { + case OpCode.check: + subResult = new CheckResult(); + break; + case OpCode.create: + subResult = new CreateResult(subTxnResult.path); + break; + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + subResult = new CreateResult(subTxnResult.path, subTxnResult.stat); + break; + case OpCode.delete: + case OpCode.deleteContainer: + subResult = new DeleteResult(); + break; + case OpCode.setData: + subResult = new SetDataResult(subTxnResult.stat); + break; + case OpCode.error: + subResult = new ErrorResult(subTxnResult.err); + if(subTxnResult.err == Code.SESSIONMOVED.intValue()) { + throw new SessionMovedException(); + } + break; + default: + throw new IOException("Invalid type of op"); + } + ((MultiResponse) rsp).add(subResult); + } + + break; + } + case OpCode.multiRead: { + lastOp = "MLTR"; + MultiOperationRecord multiReadRecord = new MultiOperationRecord(); + ByteBufferInputStream.byteBuffer2Record(request.request, multiReadRecord); + rsp = new MultiResponse(); + OpResult subResult; + for(Op readOp : multiReadRecord) { + try { + Record rec; + switch(readOp.getType()) { + case OpCode.getChildren: + rec = handleGetChildrenRequest(readOp.toRequestRecord(), cnxn, request.authInfo); + subResult = new GetChildrenResult(((GetChildrenResponse) rec).getChildren()); + break; + case OpCode.getData: + rec = handleGetDataRequest(readOp.toRequestRecord(), cnxn, request.authInfo); + GetDataResponse gdr = (GetDataResponse) rec; + subResult = new GetDataResult(gdr.getData(), gdr.getStat()); + break; + default: + throw new IOException("Invalid type of readOp"); + } + } catch(KeeperException e) { + subResult = new ErrorResult(e.code().intValue()); + } + ((MultiResponse) rsp).add(subResult); + } + break; + } + case OpCode.create: { + lastOp = "CREA"; + rsp = new CreateResponse(rc.path); + err = Code.get(rc.err); + requestPathMetricsCollector.registerRequest(request.type, rc.path); + break; + } + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: { + lastOp = "CREA"; + rsp = new Create2Response(rc.path, rc.stat); + err = Code.get(rc.err); + requestPathMetricsCollector.registerRequest(request.type, rc.path); + break; + } + case OpCode.delete: + case OpCode.deleteContainer: { + lastOp = "DELE"; + err = Code.get(rc.err); + requestPathMetricsCollector.registerRequest(request.type, rc.path); + break; + } + case OpCode.setData: { + lastOp = "SETD"; + rsp = new SetDataResponse(rc.stat); + err = Code.get(rc.err); + requestPathMetricsCollector.registerRequest(request.type, rc.path); + break; + } + case OpCode.reconfig: { + lastOp = "RECO"; + rsp = new GetDataResponse( + ((QuorumZooKeeperServer) zks).self.getQuorumVerifier().toString().getBytes(), rc.stat); + err = Code.get(rc.err); + break; + } + case OpCode.setACL: { + lastOp = "SETA"; + rsp = new SetACLResponse(rc.stat); + err = Code.get(rc.err); + requestPathMetricsCollector.registerRequest(request.type, rc.path); + break; + } + case OpCode.closeSession: { + lastOp = "CLOS"; + err = Code.get(rc.err); + break; + } + case OpCode.sync: { + lastOp = "SYNC"; + SyncRequest syncRequest = new SyncRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, syncRequest); + rsp = new SyncResponse(syncRequest.getPath()); + requestPathMetricsCollector.registerRequest(request.type, syncRequest.getPath()); + break; + } + case OpCode.check: { + lastOp = "CHEC"; + rsp = new SetDataResponse(rc.stat); + err = Code.get(rc.err); + break; + } + case OpCode.exists: { + lastOp = "EXIS"; + // TODO we need to figure out the security requirement for this! + ExistsRequest existsRequest = new ExistsRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, existsRequest); + path = existsRequest.getPath(); + if(path.indexOf('\0') != -1) { + throw new KeeperException.BadArgumentsException(); + } + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + Stat stat = zks.getZKDatabase().statNode(path, existsRequest.getWatch() ? cnxn : null); + rsp = new ExistsResponse(stat); + requestPathMetricsCollector.registerRequest(request.type, path); + break; + } + case OpCode.getData: { + lastOp = "GETD"; + GetDataRequest getDataRequest = new GetDataRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, getDataRequest); + path = getDataRequest.getPath(); + rsp = handleGetDataRequest(getDataRequest, cnxn, request.authInfo); + requestPathMetricsCollector.registerRequest(request.type, path); + break; + } + case OpCode.setWatches: { + lastOp = "SETW"; + SetWatches setWatches = new SetWatches(); + // TODO we really should not need this + request.request.rewind(); + ByteBufferInputStream.byteBuffer2Record(request.request, setWatches); + long relativeZxid = setWatches.getRelativeZxid(); + zks.getZKDatabase() + .setWatches(relativeZxid, setWatches.getDataWatches(), setWatches.getExistWatches(), + setWatches.getChildWatches(), Collections.emptyList(), Collections.emptyList(), cnxn); + break; + } + case OpCode.setWatches2: { + lastOp = "STW2"; + SetWatches2 setWatches = new SetWatches2(); + // TODO we really should not need this + request.request.rewind(); + ByteBufferInputStream.byteBuffer2Record(request.request, setWatches); + long relativeZxid = setWatches.getRelativeZxid(); + zks.getZKDatabase() + .setWatches(relativeZxid, setWatches.getDataWatches(), setWatches.getExistWatches(), + setWatches.getChildWatches(), setWatches.getPersistentWatches(), + setWatches.getPersistentRecursiveWatches(), cnxn); + break; + } + case OpCode.addWatch: { + lastOp = "ADDW"; + AddWatchRequest addWatcherRequest = new AddWatchRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, addWatcherRequest); + zks.getZKDatabase().addWatch(addWatcherRequest.getPath(), cnxn, addWatcherRequest.getMode()); + rsp = new ErrorResponse(0); + break; + } + case OpCode.getACL: { + lastOp = "GETA"; + GetACLRequest getACLRequest = new GetACLRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, getACLRequest); + path = getACLRequest.getPath(); + DataNode n = zks.getZKDatabase().getNode(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + zks.checkACL(request.cnxn, zks.getZKDatabase().aclForNode(n), + ZooDefs.Perms.READ | ZooDefs.Perms.ADMIN, request.authInfo, path, null); + + Stat stat = new Stat(); + List acl = zks.getZKDatabase().getACL(path, stat); + requestPathMetricsCollector.registerRequest(request.type, getACLRequest.getPath()); + + try { + zks.checkACL(request.cnxn, zks.getZKDatabase().aclForNode(n), ZooDefs.Perms.ADMIN, + request.authInfo, path, null); + rsp = new GetACLResponse(acl, stat); + } catch(KeeperException.NoAuthException e) { + List acl1 = new ArrayList(acl.size()); + for(ACL a : acl) { + if("digest".equals(a.getId().getScheme())) { + Id id = a.getId(); + Id id1 = new Id(id.getScheme(), id.getId().replaceAll(":.*", ":x")); + acl1.add(new ACL(a.getPerms(), id1)); + } else { + acl1.add(a); + } + } + rsp = new GetACLResponse(acl1, stat); + } + break; + } + case OpCode.getChildren: { + lastOp = "GETC"; + GetChildrenRequest getChildrenRequest = new GetChildrenRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, getChildrenRequest); + path = getChildrenRequest.getPath(); + rsp = handleGetChildrenRequest(getChildrenRequest, cnxn, request.authInfo); + requestPathMetricsCollector.registerRequest(request.type, path); + break; + } + case OpCode.getAllChildrenNumber: { + lastOp = "GETACN"; + GetAllChildrenNumberRequest getAllChildrenNumberRequest = new GetAllChildrenNumberRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, getAllChildrenNumberRequest); + path = getAllChildrenNumberRequest.getPath(); + DataNode n = zks.getZKDatabase().getNode(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + zks.checkACL(request.cnxn, zks.getZKDatabase().aclForNode(n), ZooDefs.Perms.READ, request.authInfo, + path, null); + int number = zks.getZKDatabase().getAllChildrenNumber(path); + rsp = new GetAllChildrenNumberResponse(number); + break; + } + case OpCode.getChildren2: { + lastOp = "GETC"; + GetChildren2Request getChildren2Request = new GetChildren2Request(); + ByteBufferInputStream.byteBuffer2Record(request.request, getChildren2Request); + Stat stat = new Stat(); + path = getChildren2Request.getPath(); + DataNode n = zks.getZKDatabase().getNode(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + zks.checkACL(request.cnxn, zks.getZKDatabase().aclForNode(n), ZooDefs.Perms.READ, request.authInfo, + path, null); + List children = zks.getZKDatabase() + .getChildren(path, stat, getChildren2Request.getWatch() ? cnxn : null); + rsp = new GetChildren2Response(children, stat); + requestPathMetricsCollector.registerRequest(request.type, path); + break; + } + case OpCode.checkWatches: { + lastOp = "CHKW"; + CheckWatchesRequest checkWatches = new CheckWatchesRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, checkWatches); + WatcherType type = WatcherType.fromInt(checkWatches.getType()); + path = checkWatches.getPath(); + boolean containsWatcher = zks.getZKDatabase().containsWatcher(path, type, cnxn); + if(!containsWatcher) { + String msg = String.format(Locale.ENGLISH, "%s (type: %s)", path, type); + throw new KeeperException.NoWatcherException(msg); + } + requestPathMetricsCollector.registerRequest(request.type, checkWatches.getPath()); + break; + } + case OpCode.removeWatches: { + lastOp = "REMW"; + RemoveWatchesRequest removeWatches = new RemoveWatchesRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, removeWatches); + WatcherType type = WatcherType.fromInt(removeWatches.getType()); + path = removeWatches.getPath(); + boolean removed = zks.getZKDatabase().removeWatch(path, type, cnxn); + if(!removed) { + String msg = String.format(Locale.ENGLISH, "%s (type: %s)", path, type); + throw new KeeperException.NoWatcherException(msg); + } + requestPathMetricsCollector.registerRequest(request.type, removeWatches.getPath()); + break; + } + case OpCode.getEphemerals: { + lastOp = "GETE"; + GetEphemeralsRequest getEphemerals = new GetEphemeralsRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, getEphemerals); + String prefixPath = getEphemerals.getPrefixPath(); + Set allEphems = zks.getZKDatabase().getDataTree().getEphemerals(request.sessionId); + List ephemerals = new ArrayList<>(); + if(prefixPath == null || prefixPath.trim().isEmpty() || "/".equals(prefixPath.trim())) { + ephemerals.addAll(allEphems); + } else { + for(String p : allEphems) { + if(p.startsWith(prefixPath)) { + ephemerals.add(p); + } + } + } + rsp = new GetEphemeralsResponse(ephemerals); + break; + } + } + } catch(SessionMovedException e) { + // session moved is a connection level error, we need to tear + // down the connection otw ZOOKEEPER-710 might happen + // ie client on slow follower starts to renew session, fails + // before this completes, then tries the fast follower (leader) + // and is successful, however the initial renew is then + // successfully fwd/processed by the leader and as a result + // the client and leader disagree on where the client is most + // recently attached (and therefore invalid SESSION MOVED generated) + cnxn.sendCloseSession(); + return; + } catch(KeeperException e) { + err = e.code(); + } catch(Exception e) { + // log at error level as we are returning a marshalling + // error to the user + LOG.error("Failed to process {}", request, e); + StringBuilder sb = new StringBuilder(); + ByteBuffer bb = request.request; + bb.rewind(); + while(bb.hasRemaining()) { + sb.append(Integer.toHexString(bb.get() & 0xff)); + } + LOG.error("Dumping request buffer: 0x{}", sb.toString()); + err = Code.MARSHALLINGERROR; + } + + ReplyHeader hdr = new ReplyHeader(request.cxid, lastZxid, err.intValue()); + updateStats(request, lastOp, lastZxid); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送响应给客户端 + */ + try { + if(path == null || rsp == null) { + cnxn.sendResponse(hdr, rsp, "response"); + } else { + int opCode = request.type; + Stat stat = null; + // Serialized read and get children responses could be cached by the connection + // object. Cache entries are identified by their path and last modified zxid, + // so these values are passed along with the response. + switch(opCode) { + case OpCode.getData: { + GetDataResponse getDataResponse = (GetDataResponse) rsp; + stat = getDataResponse.getStat(); + cnxn.sendResponse(hdr, rsp, "response", path, stat, opCode); + break; + } + case OpCode.getChildren2: { + GetChildren2Response getChildren2Response = (GetChildren2Response) rsp; + stat = getChildren2Response.getStat(); + cnxn.sendResponse(hdr, rsp, "response", path, stat, opCode); + break; + } + default: + cnxn.sendResponse(hdr, rsp, "response"); + } + } + + if(request.type == OpCode.closeSession) { + cnxn.sendCloseSession(); + } + } catch(IOException e) { + LOG.error("FIXMSG", e); + } + } + + private Record handleGetChildrenRequest(Record request, ServerCnxn cnxn, + List authInfo) throws KeeperException, IOException { + GetChildrenRequest getChildrenRequest = (GetChildrenRequest) request; + String path = getChildrenRequest.getPath(); + DataNode n = zks.getZKDatabase().getNode(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + zks.checkACL(cnxn, zks.getZKDatabase().aclForNode(n), ZooDefs.Perms.READ, authInfo, path, null); + List children = zks.getZKDatabase().getChildren(path, null, getChildrenRequest.getWatch() ? cnxn : null); + return new GetChildrenResponse(children); + } + + private Record handleGetDataRequest(Record request, ServerCnxn cnxn, + List authInfo) throws KeeperException, IOException { + GetDataRequest getDataRequest = (GetDataRequest) request; + String path = getDataRequest.getPath(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + DataNode n = zks.getZKDatabase().getNode(path); + if(n == null) { + throw new KeeperException.NoNodeException(); + } + zks.checkACL(cnxn, zks.getZKDatabase().aclForNode(n), ZooDefs.Perms.READ, authInfo, path, null); + Stat stat = new Stat(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + byte[] b = zks.getZKDatabase().getData(path, stat, getDataRequest.getWatch() ? cnxn : null); + return new GetDataResponse(b, stat); + } + + private boolean closeSession(ServerCnxnFactory serverCnxnFactory, long sessionId) { + if(serverCnxnFactory == null) { + return false; + } + return serverCnxnFactory.closeSession(sessionId, ServerCnxn.DisconnectReason.CLIENT_CLOSED_SESSION); + } + + private boolean connClosedByClient(Request request) { + return request.cnxn == null; + } + + public void shutdown() { + // we are the final link in the chain + LOG.info("shutdown of request processor complete"); + } + + private void updateStats(Request request, String lastOp, long lastZxid) { + if(request.cnxn == null) { + return; + } + long currentTime = Time.currentElapsedTime(); + zks.serverStats().updateLatency(request, currentTime); + request.cnxn.updateStatsForResponse(request.cxid, lastZxid, lastOp, request.createTime, currentTime); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java new file mode 100644 index 0000000..81392df --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/LogFormatter.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.IOException; +import java.text.DateFormat; +import java.util.Date; +import java.util.zip.Adler32; +import java.util.zip.Checksum; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.Record; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @deprecated deprecated in 3.5.5, use @see TxnLogToolkit instead + */ +@Deprecated +@InterfaceAudience.Public +public class LogFormatter { + + private static final Logger LOG = LoggerFactory.getLogger(LogFormatter.class); + + /** + * @param args + */ + public static void main(String[] args) throws Exception { + if (args.length != 1) { + System.err.println("USAGE: LogFormatter log_file"); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + + String error = ZKUtil.validateFileInput(args[0]); + if (null != error) { + System.err.println(error); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + + FileInputStream fis = new FileInputStream(args[0]); + BinaryInputArchive logStream = BinaryInputArchive.getArchive(fis); + FileHeader fhdr = new FileHeader(); + fhdr.deserialize(logStream, "fileheader"); + + if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) { + System.err.println("Invalid magic number for " + args[0]); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + System.out.println("ZooKeeper Transactional Log File with dbid " + + fhdr.getDbid() + + " txnlog format version " + + fhdr.getVersion()); + + // enable digest + ZooKeeperServer.setDigestEnabled(true); + + int count = 0; + while (true) { + long crcValue; + byte[] bytes; + try { + crcValue = logStream.readLong("crcvalue"); + + bytes = logStream.readBuffer("txnEntry"); + } catch (EOFException e) { + System.out.println("EOF reached after " + count + " txns."); + return; + } + if (bytes.length == 0) { + // Since we preallocate, we define EOF to be an + // empty transaction + System.out.println("EOF reached after " + count + " txns."); + return; + } + Checksum crc = new Adler32(); + crc.update(bytes, 0, bytes.length); + if (crcValue != crc.getValue()) { + throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue()); + } + TxnLogEntry entry = SerializeUtils.deserializeTxn(bytes); + TxnHeader hdr = entry.getHeader(); + Record txn = entry.getTxn(); + TxnDigest digest = entry.getDigest(); + System.out.println( + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())) + + " session 0x" + Long.toHexString(hdr.getClientId()) + + " cxid 0x" + Long.toHexString(hdr.getCxid()) + + " zxid 0x" + Long.toHexString(hdr.getZxid()) + + " " + Request.op2String(hdr.getType()) + + " " + txn + + " " + digest); + if (logStream.readByte("EOR") != 'B') { + LOG.error("Last transaction was partial."); + throw new EOFException("Last transaction was partial."); + } + count++; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxn.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxn.java new file mode 100644 index 0000000..ff51090 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxn.java @@ -0,0 +1,854 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.CancelledKeyException; +import java.nio.channels.SelectionKey; +import java.nio.channels.SocketChannel; +import java.security.cert.Certificate; +import java.util.Queue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ClientCnxn; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.WatcherEvent; +import org.apache.zookeeper.server.NIOServerCnxnFactory.SelectorThread; +import org.apache.zookeeper.server.command.CommandExecutor; +import org.apache.zookeeper.server.command.FourLetterCommands; +import org.apache.zookeeper.server.command.NopCommand; +import org.apache.zookeeper.server.command.SetTraceMaskCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class handles communication with clients using NIO. There is one per + * client, but only one thread doing the communication. + */ +public class NIOServerCnxn extends ServerCnxn { + + private static final Logger LOG = LoggerFactory.getLogger(NIOServerCnxn.class); + + private final NIOServerCnxnFactory factory; + + private final SocketChannel sock; + + private final SelectorThread selectorThread; + + private final SelectionKey sk; + + private boolean initialized; + + private final ByteBuffer lenBuffer = ByteBuffer.allocate(4); + + private ByteBuffer incomingBuffer = lenBuffer; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 响应队列 + */ + private final Queue outgoingBuffers = new LinkedBlockingQueue(); + + private int sessionTimeout; + + /** + * This is the id that uniquely identifies the session of a client. Once + * this session is no longer active, the ephemeral nodes will go away. + */ + private long sessionId; + + public NIOServerCnxn(ZooKeeperServer zk, SocketChannel sock, SelectionKey sk, NIOServerCnxnFactory factory, + SelectorThread selectorThread) throws IOException { + super(zk); + this.sock = sock; + this.sk = sk; + this.factory = factory; + this.selectorThread = selectorThread; + if(this.factory.login != null) { + this.zooKeeperSaslServer = new ZooKeeperSaslServer(factory.login); + } + sock.socket().setTcpNoDelay(true); + /* set socket linger to false, so that socket close does not block */ + sock.socket().setSoLinger(false, -1); + InetAddress addr = ((InetSocketAddress) sock.socket().getRemoteSocketAddress()).getAddress(); + addAuthInfo(new Id("ip", addr.getHostAddress())); + this.sessionTimeout = factory.sessionlessCnxnTimeout; + } + + /* Send close connection packet to the client, doIO will eventually + * close the underlying machinery (like socket, selectorkey, etc...) + */ + public void sendCloseSession() { + sendBuffer(ServerCnxnFactory.closeConn); + } + + /** + * send buffer without using the asynchronous + * calls to selector and then close the socket + * + * @param bb + */ + void sendBufferSync(ByteBuffer bb) { + try { + /* configure socket to be blocking + * so that we dont have to do write in + * a tight while loop + */ + if(bb != ServerCnxnFactory.closeConn) { + if(sock.isOpen()) { + sock.configureBlocking(true); + sock.write(bb); + } + packetSent(); + } + } catch(IOException ie) { + LOG.error("Error sending data synchronously ", ie); + } + } + + /** + * sendBuffer pushes a byte buffer onto the outgoing buffer queue for asynchronous writes. + */ + public void sendBuffer(ByteBuffer... buffers) { + if(LOG.isTraceEnabled()) { + LOG.trace("Add a buffer to outgoingBuffers, sk {} is valid: {}", sk, sk.isValid()); + } + + synchronized(outgoingBuffers) { + + // TODO_MA 注释: 加入 outgoingBuffers 队列 + for(ByteBuffer buffer : buffers) { + outgoingBuffers.add(buffer); + } + outgoingBuffers.add(packetSentinel); + } + requestInterestOpsUpdate(); + } + + /** + * When read on socket failed, this is typically because client closed the + * connection. In most cases, the client does this when the server doesn't + * respond within 2/3 of session timeout. This possibly indicates server + * health/performance issue, so we need to log and keep track of stat + * + * @throws EndOfStreamException + */ + private void handleFailedRead() throws EndOfStreamException { + setStale(); + ServerMetrics.getMetrics().CONNECTION_DROP_COUNT.add(1); + throw new EndOfStreamException( + "Unable to read additional data from client," + " it probably closed the socket:" + " address = " + sock + .socket().getRemoteSocketAddress() + "," + " session = 0x" + Long.toHexString(sessionId), + DisconnectReason.UNABLE_TO_READ_FROM_CLIENT); + } + + /** + * Read the request payload (everything following the length prefix) + */ + private void readPayload() throws IOException, InterruptedException, ClientCnxnLimitException { + if(incomingBuffer.remaining() != 0) { // have we read length bytes? + int rc = sock.read(incomingBuffer); // sock is non-blocking, so ok + if(rc < 0) { + handleFailedRead(); + } + } + + if(incomingBuffer.remaining() == 0) { // have we read length bytes? + incomingBuffer.flip(); + packetReceived(4 + incomingBuffer.remaining()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取链接请求 + * ConnectRequest + */ + if(!initialized) { + readConnectRequest(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取正常请求: 读写请求 + * GetDataRequest CreateRequest + */ + else { + readRequest(); + } + lenBuffer.clear(); + incomingBuffer = lenBuffer; + } + } + + /** + * This boolean tracks whether the connection is ready for selection or + * not. A connection is marked as not ready for selection while it is + * processing an IO request. The flag is used to gatekeep pushing interest + * op updates onto the selector. + */ + private final AtomicBoolean selectable = new AtomicBoolean(true); + + public boolean isSelectable() { + return sk.isValid() && selectable.get(); + } + + public void disableSelectable() { + selectable.set(false); + } + + public void enableSelectable() { + selectable.set(true); + } + + private void requestInterestOpsUpdate() { + if(isSelectable()) { + selectorThread.addInterestOpsUpdateRequest(sk); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理响应 + */ + void handleWrite(SelectionKey k) throws IOException { + if(outgoingBuffers.isEmpty()) { + return; + } + + /* + * This is going to reset the buffer position to 0 and the + * limit to the size of the buffer, so that we can fill it + * with data from the non-direct buffers that we need to send. + */ + ByteBuffer directBuffer = NIOServerCnxnFactory.getDirectBuffer(); + if(directBuffer == null) { + ByteBuffer[] bufferList = new ByteBuffer[outgoingBuffers.size()]; + // Use gathered write call. This updates the positions of the + // byte buffers to reflect the bytes that were written out. + sock.write(outgoingBuffers.toArray(bufferList)); + + // Remove the buffers that we have sent + ByteBuffer bb; + while((bb = outgoingBuffers.peek()) != null) { + if(bb == ServerCnxnFactory.closeConn) { + throw new CloseRequestException("close requested", DisconnectReason.CLIENT_CLOSED_CONNECTION); + } + if(bb == packetSentinel) { + packetSent(); + } + if(bb.remaining() > 0) { + break; + } + outgoingBuffers.remove(); + } + } else { + directBuffer.clear(); + + for(ByteBuffer b : outgoingBuffers) { + if(directBuffer.remaining() < b.remaining()) { + /* + * When we call put later, if the directBuffer is to + * small to hold everything, nothing will be copied, + * so we've got to slice the buffer if it's too big. + */ + b = (ByteBuffer) b.slice().limit(directBuffer.remaining()); + } + /* + * put() is going to modify the positions of both + * buffers, put we don't want to change the position of + * the source buffers (we'll do that after the send, if + * needed), so we save and reset the position after the + * copy + */ + int p = b.position(); + directBuffer.put(b); + b.position(p); + if(directBuffer.remaining() == 0) { + break; + } + } + /* + * Do the flip: limit becomes position, position gets set to + * 0. This sets us up for the write. + */ + directBuffer.flip(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 写出响应给 客户端 + */ + int sent = sock.write(directBuffer); + + ByteBuffer bb; + + // Remove the buffers that we have sent + while((bb = outgoingBuffers.peek()) != null) { + if(bb == ServerCnxnFactory.closeConn) { + throw new CloseRequestException("close requested", DisconnectReason.CLIENT_CLOSED_CONNECTION); + } + if(bb == packetSentinel) { + packetSent(); + } + if(sent < bb.remaining()) { + /* + * We only partially sent this buffer, so we update the position and exit the loop. + */ + bb.position(bb.position() + sent); + break; + } + /* We've sent the whole buffer, so drop the buffer */ + sent -= bb.remaining(); + outgoingBuffers.remove(); + } + } + } + + /** + * Only used in order to allow testing + */ + protected boolean isSocketOpen() { + return sock.isOpen(); + } + + /** + * // TODO_MA 注释: 服务端跟客户端的一切 IO 都是走这个 方法 + * // TODO_MA 注释: NIOServerCnxn.doIO(); + * Handles read/write IO on connection. + */ + void doIO(SelectionKey k) throws InterruptedException { + try { + if(!isSocketOpen()) { + LOG.warn("trying to do i/o on a null socket for session: 0x{}", Long.toHexString(sessionId)); + return; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理读 + * 客户端发送数据过来了,有数据可读 + */ + if(k.isReadable()) { + int rc = sock.read(incomingBuffer); + if(rc < 0) { + handleFailedRead(); + } + if(incomingBuffer.remaining() == 0) { + boolean isPayload; + if(incomingBuffer == lenBuffer) { // start of next request + incomingBuffer.flip(); + isPayload = readLength(k); + incomingBuffer.clear(); + } else { + // continuation + isPayload = true; + } + + // TODO_MA 注释: 读取数据 + if(isPayload) { // not the case for 4letterword + readPayload(); + } else { + // four letter words take care need not do anything else + return; + } + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理写 + * 我服务端需要像客户端返回响应 + */ + if(k.isWritable()) { + + // TODO_MA 注释: 写数据返回客户端 + handleWrite(k); + + if(!initialized && !getReadInterest() && !getWriteInterest()) { + throw new CloseRequestException("responded to info probe", DisconnectReason.INFO_PROBE); + } + } + + } catch(CancelledKeyException e) { + LOG.warn("CancelledKeyException causing close of session: 0x{}", Long.toHexString(sessionId)); + + LOG.debug("CancelledKeyException stack trace", e); + + close(DisconnectReason.CANCELLED_KEY_EXCEPTION); + } catch(CloseRequestException e) { + // expecting close to log session closure + close(); + } catch(EndOfStreamException e) { + LOG.warn("Unexpected exception", e); + // expecting close to log session closure + close(e.getReason()); + } catch(ClientCnxnLimitException e) { + // Common case exception, print at debug level + ServerMetrics.getMetrics().CONNECTION_REJECTED.add(1); + LOG.warn("Closing session 0x{}", Long.toHexString(sessionId), e); + close(DisconnectReason.CLIENT_CNX_LIMIT); + } catch(IOException e) { + LOG.warn("Close of session 0x{}", Long.toHexString(sessionId), e); + close(DisconnectReason.IO_EXCEPTION); + } + } + + private void readRequest() throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: create znode 或者 get znode data 的请求 + */ + zkServer.processPacket(this, incomingBuffer); + } + + // returns whether we are interested in writing, which is determined + // by whether we have any pending buffers on the output queue or not + private boolean getWriteInterest() { + return !outgoingBuffers.isEmpty(); + } + + // returns whether we are interested in taking new requests, which is + // determined by whether we are currently throttled or not + private boolean getReadInterest() { + return !throttled.get(); + } + + private final AtomicBoolean throttled = new AtomicBoolean(false); + + // Throttle acceptance of new requests. If this entailed a state change, + // register an interest op update request with the selector. + // + // Don't support wait disable receive in NIO, ignore the parameter + public void disableRecv(boolean waitDisableRecv) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 打开限流阀 + */ + if(throttled.compareAndSet(false, true)) { + requestInterestOpsUpdate(); + } + } + + // Disable throttling and resume acceptance of new requests. If this + // entailed a state change, register an interest op update request with + // the selector. + public void enableRecv() { + if(throttled.compareAndSet(true, false)) { + requestInterestOpsUpdate(); + } + } + + private void readConnectRequest() throws IOException, InterruptedException, ClientCnxnLimitException { + if(!isZKServerRunning()) { + throw new IOException("ZooKeeperServer not running"); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理链接请求 + */ + zkServer.processConnectRequest(this, incomingBuffer); + initialized = true; + } + + /** + * This class wraps the sendBuffer method of NIOServerCnxn. It is + * responsible for chunking up the response to a client. Rather + * than cons'ing up a response fully in memory, which may be large + * for some commands, this class chunks up the result. + */ + private class SendBufferWriter extends Writer { + + private StringBuffer sb = new StringBuffer(); + + /** + * Check if we are ready to send another chunk. + * + * @param force force sending, even if not a full chunk + */ + private void checkFlush(boolean force) { + if((force && sb.length() > 0) || sb.length() > 2048) { + sendBufferSync(ByteBuffer.wrap(sb.toString().getBytes())); + // clear our internal buffer + sb.setLength(0); + } + } + + @Override + public void close() throws IOException { + if(sb == null) { + return; + } + checkFlush(true); + sb = null; // clear out the ref to ensure no reuse + } + + @Override + public void flush() throws IOException { + checkFlush(true); + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + sb.append(cbuf, off, len); + checkFlush(false); + } + + } + + /** + * Return if four letter word found and responded to, otw false + **/ + private boolean checkFourLetterWord(final SelectionKey k, final int len) throws IOException { + // We take advantage of the limited size of the length to look + // for cmds. They are all 4-bytes which fits inside of an int + if(!FourLetterCommands.isKnown(len)) { + return false; + } + + String cmd = FourLetterCommands.getCommandString(len); + packetReceived(4); + + /** cancel the selection key to remove the socket handling + * from selector. This is to prevent netcat problem wherein + * netcat immediately closes the sending side after sending the + * commands and still keeps the receiving channel open. + * The idea is to remove the selectionkey from the selector + * so that the selector does not notice the closed read on the + * socket channel and keep the socket alive to write the data to + * and makes sure to close the socket after its done writing the data + */ + if(k != null) { + try { + k.cancel(); + } catch(Exception e) { + LOG.error("Error cancelling command selection key", e); + } + } + + final PrintWriter pwriter = new PrintWriter(new BufferedWriter(new SendBufferWriter())); + + // ZOOKEEPER-2693: don't execute 4lw if it's not enabled. + if(!FourLetterCommands.isEnabled(cmd)) { + LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd); + NopCommand nopCmd = new NopCommand(pwriter, this, + cmd + " is not executed because it is not in the whitelist."); + nopCmd.start(); + return true; + } + + LOG.info("Processing {} command from {}", cmd, sock.socket().getRemoteSocketAddress()); + + if(len == FourLetterCommands.setTraceMaskCmd) { + incomingBuffer = ByteBuffer.allocate(8); + int rc = sock.read(incomingBuffer); + if(rc < 0) { + throw new IOException("Read error"); + } + incomingBuffer.flip(); + long traceMask = incomingBuffer.getLong(); + ZooTrace.setTextTraceLevel(traceMask); + SetTraceMaskCommand setMask = new SetTraceMaskCommand(pwriter, this, traceMask); + setMask.start(); + return true; + } else { + CommandExecutor commandExecutor = new CommandExecutor(); + return commandExecutor.execute(this, pwriter, len, zkServer, factory); + } + } + + /** + * Reads the first 4 bytes of lenBuffer, which could be true length or + * four letter word. + * + * @param k selection key + * @return true if length read, otw false (wasn't really the length) + * @throws IOException if buffer size exceeds maxBuffer size + */ + private boolean readLength(SelectionKey k) throws IOException { + // Read the length, now get the buffer + int len = lenBuffer.getInt(); + if(!initialized && checkFourLetterWord(sk, len)) { + return false; + } + if(len < 0 || len > BinaryInputArchive.maxBuffer) { + throw new IOException("Len error " + len); + } + if(!isZKServerRunning()) { + throw new IOException("ZooKeeperServer not running"); + } + // checkRequestSize will throw IOException if request is rejected + zkServer.checkRequestSizeWhenReceivingMessage(len); + incomingBuffer = ByteBuffer.allocate(len); + return true; + } + + /** + * @return true if the server is running, false otherwise. + */ + boolean isZKServerRunning() { + return zkServer != null && zkServer.isRunning(); + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#getSessionTimeout() + */ + public int getSessionTimeout() { + return sessionTimeout; + } + + /** + * Used by "dump" 4-letter command to list all connection in + * cnxnExpiryMap + */ + @Override + public String toString() { + return "ip: " + sock.socket().getRemoteSocketAddress() + " sessionId: 0x" + Long.toHexString(sessionId); + } + + /** + * Close the cnxn and remove it from the factory cnxns list. + */ + @Override + public void close(DisconnectReason reason) { + disconnectReason = reason; + close(); + } + + private void close() { + setStale(); + if(!factory.removeCnxn(this)) { + return; + } + + if(zkServer != null) { + zkServer.removeCnxn(this); + } + + if(sk != null) { + try { + // need to cancel this selection key from the selector + sk.cancel(); + } catch(Exception e) { + LOG.debug("ignoring exception during selectionkey cancel", e); + } + } + + closeSock(); + } + + /** + * Close resources associated with the sock of this cnxn. + */ + private void closeSock() { + if(!sock.isOpen()) { + return; + } + + String logMsg = String.format("Closed socket connection for client %s %s", sock.socket().getRemoteSocketAddress(), + sessionId != 0 ? "which had sessionid 0x" + Long + .toHexString(sessionId) : "(no session established for client)"); + LOG.debug(logMsg); + + closeSock(sock); + } + + /** + * Close resources associated with a sock. + */ + public static void closeSock(SocketChannel sock) { + if(!sock.isOpen()) { + return; + } + + try { + /* + * The following sequence of code is stupid! You would think that + * only sock.close() is needed, but alas, it doesn't work that way. + * If you just do sock.close() there are cases where the socket + * doesn't actually close... + */ + sock.socket().shutdownOutput(); + } catch(IOException e) { + // This is a relatively common exception that we can't avoid + LOG.debug("ignoring exception during output shutdown", e); + } + try { + sock.socket().shutdownInput(); + } catch(IOException e) { + // This is a relatively common exception that we can't avoid + LOG.debug("ignoring exception during input shutdown", e); + } + try { + sock.socket().close(); + } catch(IOException e) { + LOG.debug("ignoring exception during socket close", e); + } + try { + sock.close(); + } catch(IOException e) { + LOG.debug("ignoring exception during socketchannel close", e); + } + } + + private static final ByteBuffer packetSentinel = ByteBuffer.allocate(0); + + @Override + public void sendResponse(ReplyHeader h, Record r, String tag, String cacheKey, Stat stat, int opCode) { + try { + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、生成响应报文 + * 2、返回结果给客户端 + */ + sendBuffer(serialize(h, r, tag, cacheKey, stat, opCode)); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + decrOutstandingAndCheckThrottle(h); + } catch(Exception e) { + LOG.warn("Unexpected exception. Destruction averted.", e); + } + } + + /** + * // TODO_MA 马中华 注释: 服务端处理 Watcher + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#process(org.apache.zookeeper.proto.WatcherEvent) + */ + @Override + public void process(WatchedEvent event) { + + // TODO_MA 注释: NOTIFICATION_XID 代表 WATCHER_EVENT 事件 + ReplyHeader h = new ReplyHeader(ClientCnxn.NOTIFICATION_XID, -1L, 0); + + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, + "Deliver event " + event + " to 0x" + Long.toHexString(this.sessionId) + " through " + this); + } + + // TODO_MA 马中华 注释: 需要返回给客户端的事件信息 + // Convert WatchedEvent to a type that can be sent over the wire + WatcherEvent e = event.getWrapper(); + + // The last parameter OpCode here is used to select the response cache. + // Passing OpCode.error (with a value of -1) means we don't care, as we don't need + // response cache on delivering watcher events. + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送事件 给客户端 + */ + sendResponse(h, e, "notification", null, null, ZooDefs.OpCode.error); + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.ServerCnxnIface#getSessionId() + */ + @Override + public long getSessionId() { + return sessionId; + } + + @Override + public void setSessionId(long sessionId) { + this.sessionId = sessionId; + factory.addSession(sessionId, this); + } + + @Override + public void setSessionTimeout(int sessionTimeout) { + this.sessionTimeout = sessionTimeout; + factory.touchCnxn(this); + } + + @Override + public int getInterestOps() { + if(!isSelectable()) { + return 0; + } + int interestOps = 0; + if(getReadInterest()) { + interestOps |= SelectionKey.OP_READ; + } + if(getWriteInterest()) { + interestOps |= SelectionKey.OP_WRITE; + } + return interestOps; + } + + @Override + public InetSocketAddress getRemoteSocketAddress() { + if(!sock.isOpen()) { + return null; + } + return (InetSocketAddress) sock.socket().getRemoteSocketAddress(); + } + + public InetAddress getSocketAddress() { + if(!sock.isOpen()) { + return null; + } + return sock.socket().getInetAddress(); + } + + @Override + protected ServerStats serverStats() { + if(zkServer == null) { + return null; + } + return zkServer.serverStats(); + } + + @Override + public boolean isSecure() { + return false; + } + + @Override + public Certificate[] getClientCertificateChain() { + throw new UnsupportedOperationException("SSL is unsupported in NIOServerCnxn"); + } + + @Override + public void setClientCertificateChain(Certificate[] chain) { + throw new UnsupportedOperationException("SSL is unsupported in NIOServerCnxn"); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxnFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxnFactory.java new file mode 100644 index 0000000..0bcfa55 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NIOServerCnxnFactory.java @@ -0,0 +1,1171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketException; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.LinkedBlockingQueue; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * NIOServerCnxnFactory implements a multi-threaded ServerCnxnFactory using + * NIO non-blocking socket calls. Communication between threads is handled via + * queues. + * + * - 1 accept thread, which accepts new connections and assigns to a + * selector thread + * - 1-N selector threads, each of which selects on 1/N of the connections. + * The reason the factory supports more than one selector thread is that + * with large numbers of connections, select() itself can become a + * performance bottleneck. + * - 0-M socket I/O worker threads, which perform basic socket reads and + * writes. If configured with 0 worker threads, the selector threads + * do the socket I/O directly. + * - 1 connection expiration thread, which closes idle connections; this is + * necessary to expire connections on which no session is established. + * + * Typical (default) thread counts are: on a 32 core machine, 1 accept thread, + * 1 connection expiration thread, 4 selector threads, and 64 worker threads. + */ +public class NIOServerCnxnFactory extends ServerCnxnFactory { + + private static final Logger LOG = LoggerFactory.getLogger(NIOServerCnxnFactory.class); + + /** + * Default sessionless connection timeout in ms: 10000 (10s) + */ + public static final String ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT = "zookeeper.nio.sessionlessCnxnTimeout"; + + /** + * With 500 connections to an observer with watchers firing on each, is + * unable to exceed 1GigE rates with only 1 selector. + * Defaults to using 2 selector threads with 8 cores and 4 with 32 cores. + * Expressed as sqrt(numCores/2). Must have at least 1 selector thread. + */ + public static final String ZOOKEEPER_NIO_NUM_SELECTOR_THREADS = "zookeeper.nio.numSelectorThreads"; + /** + * Default: 2 * numCores + */ + public static final String ZOOKEEPER_NIO_NUM_WORKER_THREADS = "zookeeper.nio.numWorkerThreads"; + /** + * Default: 64kB + */ + public static final String ZOOKEEPER_NIO_DIRECT_BUFFER_BYTES = "zookeeper.nio.directBufferBytes"; + /** + * Default worker pool shutdown timeout in ms: 5000 (5s) + */ + public static final String ZOOKEEPER_NIO_SHUTDOWN_TIMEOUT = "zookeeper.nio.shutdownTimeout"; + + static { + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + public void uncaughtException(Thread t, Throwable e) { + LOG.error("Thread {} died", t, e); + } + }); + /** + * this is to avoid the jvm bug: + * NullPointerException in Selector.open() + * http://bugs.sun.com/view_bug.do?bug_id=6427854 + */ + try { + Selector.open().close(); + } catch(IOException ie) { + LOG.error("Selector failed to open", ie); + } + + /** + * Value of 0 disables use of direct buffers and instead uses + * gathered write call. + * + * Default to using 64k direct buffers. + */ + directBufferBytes = Integer.getInteger(ZOOKEEPER_NIO_DIRECT_BUFFER_BYTES, 64 * 1024); + } + + /** + * AbstractSelectThread is an abstract base class containing a few bits + * of code shared by the AcceptThread (which selects on the listen socket) + * and SelectorThread (which selects on client connections) classes. + */ + private abstract class AbstractSelectThread extends ZooKeeperThread { + + protected final Selector selector; + + public AbstractSelectThread(String name) throws IOException { + super(name); + // Allows the JVM to shutdown even if this thread is still running. + setDaemon(true); + this.selector = Selector.open(); + } + + public void wakeupSelector() { + selector.wakeup(); + } + + /** + * Close the selector. This should be called when the thread is about to + * exit and no operation is going to be performed on the Selector or + * SelectionKey + */ + protected void closeSelector() { + try { + selector.close(); + } catch(IOException e) { + LOG.warn("ignored exception during selector close.", e); + } + } + + protected void cleanupSelectionKey(SelectionKey key) { + if(key != null) { + try { + key.cancel(); + } catch(Exception ex) { + LOG.debug("ignoring exception during selectionkey cancel", ex); + } + } + } + + protected void fastCloseSock(SocketChannel sc) { + if(sc != null) { + try { + // Hard close immediately, discarding buffers + sc.socket().setSoLinger(true, 0); + } catch(SocketException e) { + LOG.warn("Unable to set socket linger to 0, socket close may stall in CLOSE_WAIT", e); + } + NIOServerCnxn.closeSock(sc); + } + } + + } + + /** + * There is a single AcceptThread which accepts new connections and assigns + * them to a SelectorThread using a simple round-robin scheme to spread + * them across the SelectorThreads. It enforces maximum number of + * connections per IP and attempts to cope with running out of file + * descriptors by briefly sleeping before retrying. + */ + private class AcceptThread extends AbstractSelectThread { + + private final ServerSocketChannel acceptSocket; + private final SelectionKey acceptKey; + private final RateLogger acceptErrorLogger = new RateLogger(LOG); + private final Collection selectorThreads; + private Iterator selectorIterator; + private volatile boolean reconfiguring = false; + + public AcceptThread(ServerSocketChannel ss, InetSocketAddress addr, + Set selectorThreads) throws IOException { + super("NIOServerCxnFactory.AcceptThread:" + addr); + this.acceptSocket = ss; + + // TODO_MA 注释: 只负责 SelectionKey.OP_ACCEP 事件 + this.acceptKey = acceptSocket.register(selector, SelectionKey.OP_ACCEPT); + + this.selectorThreads = Collections.unmodifiableList(new ArrayList(selectorThreads)); + selectorIterator = this.selectorThreads.iterator(); + } + + public void run() { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 执行 select + */ + while(!stopped && !acceptSocket.socket().isClosed()) { + try { + select(); + } catch(RuntimeException e) { + LOG.warn("Ignoring unexpected runtime exception", e); + } catch(Exception e) { + LOG.warn("Ignoring unexpected exception", e); + } + } + } finally { + closeSelector(); + // This will wake up the selector threads, and tell the + // worker thread pool to begin shutdown. + if(!reconfiguring) { + NIOServerCnxnFactory.this.stop(); + } + LOG.info("accept thread exitted run method"); + } + } + + public void setReconfiguring() { + reconfiguring = true; + } + + private void select() { + try { + + // TODO_MA 注释: 进行 select + selector.select(); + Iterator selectedKeys = selector.selectedKeys().iterator(); + + while(!stopped && selectedKeys.hasNext()) { + SelectionKey key = selectedKeys.next(); + selectedKeys.remove(); + + if(!key.isValid()) { + continue; + } + + // TODO_MA 注释: 完成链接 + if(key.isAcceptable()) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 完成链接 + */ + if(!doAccept()) { + // If unable to pull a new connection off the accept + // queue, pause accepting to give us time to free + // up file descriptors and so the accept thread + // doesn't spin in a tight loop. + pauseAccept(10); + } + } else { + LOG.warn("Unexpected ops in accept select {}", key.readyOps()); + } + } + } catch(IOException e) { + LOG.warn("Ignoring IOException while selecting", e); + } + } + + /** + * Mask off the listen socket interest ops and use select() to sleep + * so that other threads can wake us up by calling wakeup() on the + * selector. + */ + private void pauseAccept(long millisecs) { + acceptKey.interestOps(0); + try { + selector.select(millisecs); + } catch(IOException e) { + // ignore + } finally { + acceptKey.interestOps(SelectionKey.OP_ACCEPT); + } + } + + /** + * Accept new socket connections. Enforces maximum number of connections + * per client IP address. Round-robin assigns to selector thread for + * handling. Returns whether pulled a connection off the accept queue + * or not. If encounters an error attempts to fast close the socket. + * + * @return whether was able to accept a connection or not + */ + private boolean doAccept() { + boolean accepted = false; + SocketChannel sc = null; + try { + + // TODO_MA 注释: 完成链接, 客户端发送网络链接请求过来了,则这句代码返回! + sc = acceptSocket.accept(); + accepted = true; + + if(limitTotalNumberOfCnxns()) { + throw new IOException("Too many connections max allowed is " + maxCnxns); + } + InetAddress ia = sc.socket().getInetAddress(); + int cnxncount = getClientCnxnCount(ia); + + if(maxClientCnxns > 0 && cnxncount >= maxClientCnxns) { + throw new IOException("Too many connections from " + ia + " - max is " + maxClientCnxns); + } + + LOG.debug("Accepted socket connection from {}", sc.socket().getRemoteSocketAddress()); + + sc.configureBlocking(false); + + // TODO_MA 注释: Round-robin 轮询模式使用 SelectorThread + // Round-robin assign this connection to a selector thread + if(!selectorIterator.hasNext()) { + selectorIterator = selectorThreads.iterator(); + } + + // TODO_MA 注释: 遍历得到一个 SelectorThread + SelectorThread selectorThread = selectorIterator.next(); + + // TODO_MA 注释: 丢入 acceptedQueue 队列,让 selector 来接待 + // TODO_MA 注释: 完成了 accept 链接的 SocketChannel 在 selectorThread 上注册 OP_READ 和 OP_WRITE + if(!selectorThread.addAcceptedConnection(sc)) { + throw new IOException( + "Unable to add connection to selector queue" + (stopped ? " (shutdown in progress)" : "")); + } + acceptErrorLogger.flush(); + } catch(IOException e) { + // accept, maxClientCnxns, configureBlocking + ServerMetrics.getMetrics().CONNECTION_REJECTED.add(1); + acceptErrorLogger.rateLimitLog("Error accepting new connection: " + e.getMessage()); + fastCloseSock(sc); + } + return accepted; + } + + } + + /** + * The SelectorThread receives newly accepted connections from the + * AcceptThread and is responsible for selecting for I/O readiness + * across the connections. This thread is the only thread that performs + * any non-threadsafe or potentially blocking calls on the selector + * (registering new connections and reading/writing interest ops). + * + * Assignment of a connection to a SelectorThread is permanent and only + * one SelectorThread will ever interact with the connection. There are + * 1-N SelectorThreads, with connections evenly apportioned between the + * SelectorThreads. + * + * If there is a worker thread pool, when a connection has I/O to perform + * the SelectorThread removes it from selection by clearing its interest + * ops and schedules the I/O for processing by a worker thread. When the + * work is complete, the connection is placed on the ready queue to have + * its interest ops restored and resume selection. + * + * If there is no worker thread pool, the SelectorThread performs the I/O + * directly. + */ + class SelectorThread extends AbstractSelectThread { + + private final int id; + private final Queue acceptedQueue; + private final Queue updateQueue; + + public SelectorThread(int id) throws IOException { + super("NIOServerCxnFactory.SelectorThread-" + id); + this.id = id; + acceptedQueue = new LinkedBlockingQueue(); + updateQueue = new LinkedBlockingQueue(); + } + + /** + * Place new accepted connection onto a queue for adding. Do this + * so only the selector thread modifies what keys are registered + * with the selector. + */ + public boolean addAcceptedConnection(SocketChannel accepted) { + + // TODO_MA 注释: 完成链接的 客户端加入 acceptedQueue + if(stopped || !acceptedQueue.offer(accepted)) { + return false; + } + + // TODO_MA 注释: 唤醒 selectorThread + wakeupSelector(); + return true; + } + + /** + * Place interest op update requests onto a queue so that only the + * selector thread modifies interest ops, because interest ops + * reads/sets are potentially blocking operations if other select + * operations are happening. + */ + public boolean addInterestOpsUpdateRequest(SelectionKey sk) { + if(stopped || !updateQueue.offer(sk)) { + return false; + } + + // TODO_MA 注释: 唤醒 Selector + wakeupSelector(); + return true; + } + + /** + * The main loop for the thread selects() on the connections and + * dispatches ready I/O work requests, then registers all pending + * newly accepted connections and updates any interest ops on the + * queue. + */ + public void run() { + try { + + // TODO_MA 注释: 循环 + while(!stopped) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 执行 select: 处理读写请求 + */ + select(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 给完成链接的客户端注册 OP_READ 事件,并生成 NIOServerCnxn,并且完成注册 + */ + processAcceptedConnections(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 注册 OP_READ 或者 OP_WRITE 事件 + */ + processInterestOpsUpdateRequests(); + } catch(RuntimeException e) { + LOG.warn("Ignoring unexpected runtime exception", e); + } catch(Exception e) { + LOG.warn("Ignoring unexpected exception", e); + } + } + + // TODO_MA 注释: 如果退出了这个循环,意味着 服务端 shut down 了 + + // Close connections still pending on the selector. Any others + // with in-flight work, let drain out of the work queue. + for(SelectionKey key : selector.keys()) { + NIOServerCnxn cnxn = (NIOServerCnxn) key.attachment(); + if(cnxn.isSelectable()) { + cnxn.close(ServerCnxn.DisconnectReason.SERVER_SHUTDOWN); + } + cleanupSelectionKey(key); + } + SocketChannel accepted; + while((accepted = acceptedQueue.poll()) != null) { + fastCloseSock(accepted); + } + updateQueue.clear(); + } finally { + closeSelector(); + // This will wake up the accept thread and the other selector + // threads, and tell the worker thread pool to begin shutdown. + NIOServerCnxnFactory.this.stop(); + LOG.info("selector thread exitted run method"); + } + } + + private void select() { + try { + + // TODO_MA 注释: 这个地方阻塞,SelectThread.wakeupSelector(); + selector.select(); + Set selected = selector.selectedKeys(); + ArrayList selectedList = new ArrayList(selected); + Collections.shuffle(selectedList); + + // TODO_MA 注释: 迭代 SelectionKey 执行处理 + Iterator selectedKeys = selectedList.iterator(); + while(!stopped && selectedKeys.hasNext()) { + SelectionKey key = selectedKeys.next(); + selected.remove(key); + + if(!key.isValid()) { + cleanupSelectionKey(key); + continue; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理读写请求 + */ + if(key.isReadable() || key.isWritable()) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、客户端发送数据过来 + * 2、服务端需要给客户端写数据过去 + */ + handleIO(key); + } else { + LOG.warn("Unexpected ops in select {}", key.readyOps()); + } + } + } catch(IOException e) { + LOG.warn("Ignoring IOException while selecting", e); + } + } + + /** + * Schedule I/O for processing on the connection associated with + * the given SelectionKey. If a worker thread pool is not being used, + * I/O is run directly by this thread. + */ + private void handleIO(SelectionKey key) { + + // TODO_MA 注释: 生成一个 IOWorkRequest + IOWorkRequest workRequest = new IOWorkRequest(this, key); + NIOServerCnxn cnxn = (NIOServerCnxn) key.attachment(); + + // Stop selecting this key while processing on its connection + cnxn.disableSelectable(); + key.interestOps(0); + + // TODO_MA 注释: session 每次活跃的时候,都要更新自己的超时信息 + touchCnxn(cnxn); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 调度请求处理 + * workerPool = WorkerServer(List workers) => 线程池 + */ + workerPool.schedule(workRequest); + } + + /** + * Iterate over the queue of accepted connections that have been + * assigned to this thread but not yet placed on the selector. + */ + private void processAcceptedConnections() { + SocketChannel accepted; + + // TODO_MA 注释: acceptedQueue 不为空,意味着有新建的链接 + while(!stopped && (accepted = acceptedQueue.poll()) != null) { + SelectionKey key = null; + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 注册 OP_READ 事件 + * 注册这个 SelectionKey.OP_READ 的目的,就是 服务端已经完成了和 客户端的网络连接 + * 接下来,客户端会立即发送 ConnectRequest 过来的 + */ + key = accepted.register(selector, SelectionKey.OP_READ); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建链接 + * accepted = SocketChannel + */ + NIOServerCnxn cnxn = createConnection(accepted, key, this); + key.attach(cnxn); + + // TODO_MA 注释: 客户端注册 + addCnxn(cnxn); + } catch(IOException e) { + // register, createConnection + cleanupSelectionKey(key); + fastCloseSock(accepted); + } + } + } + + /** + * Iterate over the queue of connections ready to resume selection, + * and restore their interest ops selection mask. + */ + private void processInterestOpsUpdateRequests() { + SelectionKey key; + + // TODO_MA 注释: + while(!stopped && (key = updateQueue.poll()) != null) { + if(!key.isValid()) { + cleanupSelectionKey(key); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + NIOServerCnxn cnxn = (NIOServerCnxn) key.attachment(); + if(cnxn.isSelectable()) { + key.interestOps(cnxn.getInterestOps()); + } + } + } + } + + /** + * IOWorkRequest is a small wrapper class to allow doIO() calls to be + * run on a connection using a WorkerService. + */ + private class IOWorkRequest extends WorkerService.WorkRequest { + + private final SelectorThread selectorThread; + private final SelectionKey key; + + // TODO_MA 注释: 给某一个客户端提供服务的 服务组件: NIOServerCnxn ==> ClientCnxn + private final NIOServerCnxn cnxn; + + IOWorkRequest(SelectorThread selectorThread, SelectionKey key) { + this.selectorThread = selectorThread; + this.key = key; + this.cnxn = (NIOServerCnxn) key.attachment(); + } + + public void doWork() throws InterruptedException { + if(!key.isValid()) { + selectorThread.cleanupSelectionKey(key); + return; + } + + if(key.isReadable() || key.isWritable()) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 服务端,执行 IO 处理 + * 1、cnxn 就是一个唯一的服务组件 + * 2、key 中会告诉我们,到底是 read 还是 write + */ + cnxn.doIO(key); + + // Check if we shutdown or doIO() closed this connection + if(stopped) { + cnxn.close(ServerCnxn.DisconnectReason.SERVER_SHUTDOWN); + return; + } + if(!key.isValid()) { + selectorThread.cleanupSelectionKey(key); + return; + } + + // TODO_MA 注释: 更新会话信息 + touchCnxn(cnxn); + } + + // Mark this connection as once again ready for selection + cnxn.enableSelectable(); + + // Push an update request on the queue to resume selecting + // on the current set of interest ops, which may have changed + // as a result of the I/O operations we just performed. + if(!selectorThread.addInterestOpsUpdateRequest(key)) { + cnxn.close(ServerCnxn.DisconnectReason.CONNECTION_MODE_CHANGED); + } + } + + @Override + public void cleanup() { + cnxn.close(ServerCnxn.DisconnectReason.CLEAN_UP); + } + + } + + /** + * This thread is responsible for closing stale connections so that + * connections on which no session is established are properly expired. + */ + private class ConnectionExpirerThread extends ZooKeeperThread { + + ConnectionExpirerThread() { + super("ConnnectionExpirer"); + } + + public void run() { + try { + while(!stopped) { + + // TODO_MA 注释: 每隔一段时间 + // TODO_MA 注释: 现在是一个时刻 t1, 下一个过期时刻 是t2 + // TODO_MA 注释: 还得等: waitTime = t2 - t1 + long waitTime = cnxnExpiryQueue.getWaitTime(); + if(waitTime > 0) { + Thread.sleep(waitTime); + continue; + } + + // TODO_MA 注释: 每次session 有新的操作的时候,超时时间会重置 + // TODO_MA 注释: 通过一个方法来实现: touchCnxn(); + + // TODO_MA 注释: 获取该失效的客户端链接,执行会话超时处理 + for(NIOServerCnxn conn : cnxnExpiryQueue.poll()) { + ServerMetrics.getMetrics().SESSIONLESS_CONNECTIONS_EXPIRED.add(1); + conn.close(ServerCnxn.DisconnectReason.CONNECTION_EXPIRED); + } + } + + } catch(InterruptedException e) { + LOG.info("ConnnectionExpirerThread interrupted"); + } + } + + } + + ServerSocketChannel ss; + + /** + * We use this buffer to do efficient socket I/O. Because I/O is handled + * by the worker threads (or the selector threads directly, if no worker + * thread pool is created), we can create a fixed set of these to be + * shared by connections. + */ + private static final ThreadLocal directBuffer = new ThreadLocal() { + @Override + protected ByteBuffer initialValue() { + return ByteBuffer.allocateDirect(directBufferBytes); + } + }; + + public static ByteBuffer getDirectBuffer() { + return directBufferBytes > 0 ? directBuffer.get() : null; + } + + // ipMap is used to limit connections per IP + private final ConcurrentHashMap> ipMap = new ConcurrentHashMap>(); + + protected int maxClientCnxns = 60; + int listenBacklog = -1; + + int sessionlessCnxnTimeout; + private ExpiryQueue cnxnExpiryQueue; + + protected WorkerService workerPool; + + private static int directBufferBytes; + private int numSelectorThreads; + private int numWorkerThreads; + private long workerShutdownTimeoutMS; + + /** + * Construct a new server connection factory which will accept an unlimited number + * of concurrent connections from each client (up to the file descriptor + * limits of the operating system). startup(zks) must be called subsequently. + */ + public NIOServerCnxnFactory() { + } + + private volatile boolean stopped = true; + private ConnectionExpirerThread expirerThread; + private AcceptThread acceptThread; + private final Set selectorThreads = new HashSet(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: NIOSeverCnxnFactory 的启动分为两个部分 + * 1、NIOSeverCnxnFactory.configure(); + * 初始化 AcceptThread 和 SelectorThread + * 2、NIOSeverCnxnFactory.start(); + * 初始化 WorkerThread + */ + @Override + public void configure(InetSocketAddress addr, int maxcc, int backlog, boolean secure) throws IOException { + if(secure) { + throw new UnsupportedOperationException("SSL isn't supported in NIOServerCnxn"); + } + configureSaslLogin(); + maxClientCnxns = maxcc; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动会话过期线程 + * 很优秀的一种,桶管理机制 + */ + initMaxCnxns(); + + // TODO_MA 注释: 管理 NIOServerCnxn 的超时 + sessionlessCnxnTimeout = Integer.getInteger(ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT, 10000); + // We also use the sessionlessCnxnTimeout as expiring interval for + // cnxnExpiryQueue. These don't need to be the same, but the expiring + // interval passed into the ExpiryQueue() constructor below should be + // less than or equal to the timeout. + cnxnExpiryQueue = new ExpiryQueue(sessionlessCnxnTimeout); + expirerThread = new ConnectionExpirerThread(); + + // TODO_MA 注释: 获取 cpu 个数 + int numCores = Runtime.getRuntime().availableProcessors(); + + // TODO_MA 注释: 注意计算规则,动态的根据资源来选择合适的 selector 线程 + // TODO_MA 注释: cpucores = 32 ==> (32/2 开根号 跟1 求最大值) + // TODO_MA 注释: 32 个 32/2 = 16 => 4 + // TODO_MA 注释: 16 个 16/2 => 8 => 2 + // TODO_MA 注释: 64 个 = 64/2 = 32 = 5个线程 + // TODO_MA 注释: 128 个 = 128 / 2 = 64 = 8 个线程 + // 32 cores sweet spot seems to be 4 selector threads + numSelectorThreads = Integer + .getInteger(ZOOKEEPER_NIO_NUM_SELECTOR_THREADS, Math.max((int) Math.sqrt((float) numCores / 2), 1)); + if(numSelectorThreads < 1) { + throw new IOException("numSelectorThreads must be at least 1"); + } + + // TODO_MA 注释: worker 线程数量: 2 倍 cpu 数量 + numWorkerThreads = Integer.getInteger(ZOOKEEPER_NIO_NUM_WORKER_THREADS, 2 * numCores); + + // TODO_MA 注释: worker 线程超时时间 50s + workerShutdownTimeoutMS = Long.getLong(ZOOKEEPER_NIO_SHUTDOWN_TIMEOUT, 5000); + + String logMsg = "Configuring NIO connection handler with " + (sessionlessCnxnTimeout / 1000) + "s sessionless connection timeout, " + numSelectorThreads + " selector thread(s), " + (numWorkerThreads > 0 ? numWorkerThreads : "no") + " worker threads, and " + (directBufferBytes == 0 ? "gathered writes." : ("" + (directBufferBytes / 1024) + " kB direct buffers.")); + LOG.info(logMsg); + + // TODO_MA 注释: 初始化 SelectorThread + for(int i = 0; i < numSelectorThreads; ++i) { + selectorThreads.add(new SelectorThread(i)); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 NIO 服务端 ServerSocketChannel + */ + listenBacklog = backlog; + this.ss = ServerSocketChannel.open(); + ss.socket().setReuseAddress(true); + + // TODO_MA 马中华 注释: 绑定端口号 2181 + LOG.info("binding to port {}", addr); + if(listenBacklog == -1) { + ss.socket().bind(addr); + } else { + ss.socket().bind(addr, listenBacklog); + } + ss.configureBlocking(false); + + // TODO_MA 注释: 初始化一个 AcceptThread + acceptThread = new AcceptThread(ss, addr, selectorThreads); + } + + private void tryClose(ServerSocketChannel s) { + try { + s.close(); + } catch(IOException sse) { + LOG.error("Error while closing server socket.", sse); + } + } + + @Override + public void reconfigure(InetSocketAddress addr) { + ServerSocketChannel oldSS = ss; + try { + acceptThread.setReconfiguring(); + tryClose(oldSS); + acceptThread.wakeupSelector(); + try { + acceptThread.join(); + } catch(InterruptedException e) { + LOG.error("Error joining old acceptThread when reconfiguring client port.", e); + Thread.currentThread().interrupt(); + } + this.ss = ServerSocketChannel.open(); + ss.socket().setReuseAddress(true); + LOG.info("binding to port {}", addr); + ss.socket().bind(addr); + ss.configureBlocking(false); + acceptThread = new AcceptThread(ss, addr, selectorThreads); + acceptThread.start(); + } catch(IOException e) { + LOG.error("Error reconfiguring client port to {}", addr, e); + tryClose(oldSS); + } + } + + /** + * {@inheritDoc} + */ + public int getMaxClientCnxnsPerHost() { + return maxClientCnxns; + } + + /** + * {@inheritDoc} + */ + public void setMaxClientCnxnsPerHost(int max) { + maxClientCnxns = max; + } + + /** + * {@inheritDoc} + */ + public int getSocketListenBacklog() { + return listenBacklog; + } + + @Override + public void start() { + stopped = false; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化工作线程 + * 初始化了一个线程池: 线程池的线程个数 = coucore * 2 + */ + if(workerPool == null) { + workerPool = new WorkerService("NIOWorker", numWorkerThreads, false); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 SelectorThread + */ + for(SelectorThread thread : selectorThreads) { + if(thread.getState() == Thread.State.NEW) { + thread.start(); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 AcceptThread + */ + // ensure thread is started once and only once + if(acceptThread.getState() == Thread.State.NEW) { + acceptThread.start(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 ConnectionExpirerThread + */ + if(expirerThread.getState() == Thread.State.NEW) { + expirerThread.start(); + } + } + + @Override + public void startup(ZooKeeperServer zks, boolean startServer) throws IOException, InterruptedException { + start(); + setZooKeeperServer(zks); + if(startServer) { + zks.startdata(); + zks.startup(); + } + } + + @Override + public InetSocketAddress getLocalAddress() { + return (InetSocketAddress) ss.socket().getLocalSocketAddress(); + } + + @Override + public int getLocalPort() { + return ss.socket().getLocalPort(); + } + + /** + * De-registers the connection from the various mappings maintained + * by the factory. + */ + public boolean removeCnxn(NIOServerCnxn cnxn) { + // If the connection is not in the master list it's already been closed + if(!cnxns.remove(cnxn)) { + return false; + } + cnxnExpiryQueue.remove(cnxn); + + removeCnxnFromSessionMap(cnxn); + + InetAddress addr = cnxn.getSocketAddress(); + if(addr != null) { + Set set = ipMap.get(addr); + if(set != null) { + set.remove(cnxn); + // Note that we make no effort here to remove empty mappings + // from ipMap. + } + } + + // unregister from JMX + unregisterConnection(cnxn); + return true; + } + + /** + * Add or update cnxn in our cnxnExpiryQueue + * + * @param cnxn + */ + public void touchCnxn(NIOServerCnxn cnxn) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 将自己更新到对应的 session bucket 中 + */ + cnxnExpiryQueue.update(cnxn, cnxn.getSessionTimeout()); + } + + private void addCnxn(NIOServerCnxn cnxn) throws IOException { + InetAddress addr = cnxn.getSocketAddress(); + if(addr == null) { + throw new IOException("Socket of " + cnxn + " has been closed"); + } + + // TODO_MA 注释: 根据 IP 地址,映射到一个 NIOServerCnxn Set + Set set = ipMap.get(addr); + if(set == null) { + // in general we will see 1 connection from each + // host, setting the initial cap to 2 allows us + // to minimize mem usage in the common case + // of 1 entry -- we need to set the initial cap + // to 2 to avoid rehash when the first entry is added + // Construct a ConcurrentHashSet using a ConcurrentHashMap + set = Collections.newSetFromMap(new ConcurrentHashMap(2)); + // Put the new set in the map, but only if another thread + // hasn't beaten us to it + Set existingSet = ipMap.putIfAbsent(addr, set); + if(existingSet != null) { + set = existingSet; + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 加入集合 + */ + set.add(cnxn); + cnxns.add(cnxn); + + // TODO_MA 注释: 将自己加入 session expire 管理 + touchCnxn(cnxn); + } + + protected NIOServerCnxn createConnection(SocketChannel sock, SelectionKey sk, + SelectorThread selectorThread) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return new NIOServerCnxn(zkServer, sock, sk, this, selectorThread); + } + + private int getClientCnxnCount(InetAddress cl) { + Set s = ipMap.get(cl); + if(s == null) { + return 0; + } + return s.size(); + } + + /** + * clear all the connections in the selector + */ + @Override + @SuppressWarnings("unchecked") + public void closeAll(ServerCnxn.DisconnectReason reason) { + // clear all the connections on which we are selecting + for(ServerCnxn cnxn : cnxns) { + try { + // This will remove the cnxn from cnxns + cnxn.close(reason); + } catch(Exception e) { + LOG.warn("Ignoring exception closing cnxn session id 0x{}", Long.toHexString(cnxn.getSessionId()), e); + } + } + } + + public void stop() { + stopped = true; + + // Stop queuing connection attempts + try { + ss.close(); + } catch(IOException e) { + LOG.warn("Error closing listen socket", e); + } + + if(acceptThread != null) { + if(acceptThread.isAlive()) { + acceptThread.wakeupSelector(); + } else { + acceptThread.closeSelector(); + } + } + if(expirerThread != null) { + expirerThread.interrupt(); + } + for(SelectorThread thread : selectorThreads) { + if(thread.isAlive()) { + thread.wakeupSelector(); + } else { + thread.closeSelector(); + } + } + if(workerPool != null) { + workerPool.stop(); + } + } + + public void shutdown() { + try { + // close listen socket and signal selector threads to stop + stop(); + + // wait for selector and worker threads to shutdown + join(); + + // close all open connections + closeAll(ServerCnxn.DisconnectReason.SERVER_SHUTDOWN); + + if(login != null) { + login.shutdown(); + } + } catch(InterruptedException e) { + LOG.warn("Ignoring interrupted exception during shutdown", e); + } catch(Exception e) { + LOG.warn("Ignoring unexpected exception during shutdown", e); + } + + if(zkServer != null) { + zkServer.shutdown(); + } + } + + @Override + public void join() throws InterruptedException { + if(acceptThread != null) { + acceptThread.join(); + } + for(SelectorThread thread : selectorThreads) { + thread.join(); + } + if(workerPool != null) { + workerPool.join(workerShutdownTimeoutMS); + } + } + + @Override + public Iterable getConnections() { + return cnxns; + } + + public void dumpConnections(PrintWriter pwriter) { + pwriter.print("Connections "); + cnxnExpiryQueue.dump(pwriter); + } + + @Override + public void resetAllConnectionStats() { + // No need to synchronize since cnxns is backed by a ConcurrentHashMap + for(ServerCnxn c : cnxns) { + c.resetStats(); + } + } + + @Override + public Iterable> getAllConnectionInfo(boolean brief) { + HashSet> info = new HashSet>(); + // No need to synchronize since cnxns is backed by a ConcurrentHashMap + for(ServerCnxn c : cnxns) { + info.add(c.getConnectionInfo(brief)); + } + return info; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java new file mode 100644 index 0000000..f25f9a4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxn.java @@ -0,0 +1,652 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.buffer.CompositeByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SelectionKey; +import java.security.cert.Certificate; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ClientCnxn; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.WatcherEvent; +import org.apache.zookeeper.server.command.CommandExecutor; +import org.apache.zookeeper.server.command.FourLetterCommands; +import org.apache.zookeeper.server.command.NopCommand; +import org.apache.zookeeper.server.command.SetTraceMaskCommand; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NettyServerCnxn extends ServerCnxn { + + private static final Logger LOG = LoggerFactory.getLogger(NettyServerCnxn.class); + private final Channel channel; + private CompositeByteBuf queuedBuffer; + private final AtomicBoolean throttled = new AtomicBoolean(false); + private ByteBuffer bb; + private final ByteBuffer bbLen = ByteBuffer.allocate(4); + private long sessionId; + private int sessionTimeout; + private Certificate[] clientChain; + private volatile boolean closingChannel; + + private final NettyServerCnxnFactory factory; + private boolean initialized; + + public int readIssuedAfterReadComplete; + + private volatile HandshakeState handshakeState = HandshakeState.NONE; + + public enum HandshakeState { + NONE, + STARTED, + FINISHED + } + + NettyServerCnxn(Channel channel, ZooKeeperServer zks, NettyServerCnxnFactory factory) { + super(zks); + this.channel = channel; + this.closingChannel = false; + this.factory = factory; + if (this.factory.login != null) { + this.zooKeeperSaslServer = new ZooKeeperSaslServer(factory.login); + } + InetAddress addr = ((InetSocketAddress) channel.remoteAddress()).getAddress(); + addAuthInfo(new Id("ip", addr.getHostAddress())); + } + + /** + * Close the cnxn and remove it from the factory cnxns list. + */ + @Override + public void close(DisconnectReason reason) { + disconnectReason = reason; + close(); + } + + public void close() { + closingChannel = true; + + LOG.debug("close called for session id: 0x{}", Long.toHexString(sessionId)); + + setStale(); + + // ZOOKEEPER-2743: + // Always unregister connection upon close to prevent + // connection bean leak under certain race conditions. + factory.unregisterConnection(this); + + // if this is not in cnxns then it's already closed + if (!factory.cnxns.remove(this)) { + LOG.debug("cnxns size:{}", factory.cnxns.size()); + if (channel.isOpen()) { + channel.close(); + } + return; + } + + LOG.debug("close in progress for session id: 0x{}", Long.toHexString(sessionId)); + + factory.removeCnxnFromSessionMap(this); + + factory.removeCnxnFromIpMap(this, ((InetSocketAddress) channel.remoteAddress()).getAddress()); + + if (zkServer != null) { + zkServer.removeCnxn(this); + } + + if (channel.isOpen()) { + // Since we don't check on the futures created by write calls to the channel complete we need to make sure + // that all writes have been completed before closing the channel or we risk data loss + // See: http://lists.jboss.org/pipermail/netty-users/2009-August/001122.html + channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(new ChannelFutureListener() { + @Override + public void operationComplete(ChannelFuture future) { + future.channel().close().addListener(f -> releaseQueuedBuffer()); + } + }); + } else { + ServerMetrics.getMetrics().CONNECTION_DROP_COUNT.add(1); + channel.eventLoop().execute(this::releaseQueuedBuffer); + } + } + + @Override + public long getSessionId() { + return sessionId; + } + + @Override + public int getSessionTimeout() { + return sessionTimeout; + } + + @Override + public void process(WatchedEvent event) { + ReplyHeader h = new ReplyHeader(ClientCnxn.NOTIFICATION_XID, -1L, 0); + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage( + LOG, + ZooTrace.EVENT_DELIVERY_TRACE_MASK, + "Deliver event " + event + " to 0x" + Long.toHexString(this.sessionId) + " through " + this); + } + + // Convert WatchedEvent to a type that can be sent over the wire + WatcherEvent e = event.getWrapper(); + + try { + sendResponse(h, e, "notification"); + } catch (IOException e1) { + LOG.debug("Problem sending to {}", getRemoteSocketAddress(), e1); + close(); + } + } + + @Override + public void sendResponse(ReplyHeader h, Record r, String tag, + String cacheKey, Stat stat, int opCode) throws IOException { + // cacheKey and stat are used in caching, which is not + // implemented here. Implementation example can be found in NIOServerCnxn. + if (closingChannel || !channel.isOpen()) { + return; + } + sendBuffer(serialize(h, r, tag, cacheKey, stat, opCode)); + decrOutstandingAndCheckThrottle(h); + } + + @Override + public void setSessionId(long sessionId) { + this.sessionId = sessionId; + factory.addSession(sessionId, this); + } + + // Use a single listener instance to reduce GC + private final GenericFutureListener> onSendBufferDoneListener = f -> { + if (f.isSuccess()) { + packetSent(); + } + }; + + @Override + public void sendBuffer(ByteBuffer... buffers) { + if (buffers.length == 1 && buffers[0] == ServerCnxnFactory.closeConn) { + close(DisconnectReason.CLIENT_CLOSED_CONNECTION); + return; + } + channel.writeAndFlush(Unpooled.wrappedBuffer(buffers)).addListener(onSendBufferDoneListener); + } + + /** + * This class wraps the sendBuffer method of NIOServerCnxn. It is + * responsible for chunking up the response to a client. Rather + * than cons'ing up a response fully in memory, which may be large + * for some commands, this class chunks up the result. + */ + private class SendBufferWriter extends Writer { + + private StringBuffer sb = new StringBuffer(); + + /** + * Check if we are ready to send another chunk. + * @param force force sending, even if not a full chunk + */ + private void checkFlush(boolean force) { + if ((force && sb.length() > 0) || sb.length() > 2048) { + sendBuffer(ByteBuffer.wrap(sb.toString().getBytes())); + // clear our internal buffer + sb.setLength(0); + } + } + + @Override + public void close() throws IOException { + if (sb == null) { + return; + } + checkFlush(true); + sb = null; // clear out the ref to ensure no reuse + } + + @Override + public void flush() throws IOException { + checkFlush(true); + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + sb.append(cbuf, off, len); + checkFlush(false); + } + + } + + /** Return if four letter word found and responded to, otw false **/ + private boolean checkFourLetterWord(final Channel channel, ByteBuf message, final int len) { + // We take advantage of the limited size of the length to look + // for cmds. They are all 4-bytes which fits inside of an int + if (!FourLetterCommands.isKnown(len)) { + return false; + } + + String cmd = FourLetterCommands.getCommandString(len); + + // Stops automatic reads of incoming data on this channel. We don't + // expect any more traffic from the client when processing a 4LW + // so this shouldn't break anything. + channel.config().setAutoRead(false); + packetReceived(4); + + final PrintWriter pwriter = new PrintWriter(new BufferedWriter(new SendBufferWriter())); + + // ZOOKEEPER-2693: don't execute 4lw if it's not enabled. + if (!FourLetterCommands.isEnabled(cmd)) { + LOG.debug("Command {} is not executed because it is not in the whitelist.", cmd); + NopCommand nopCmd = new NopCommand( + pwriter, + this, + cmd + " is not executed because it is not in the whitelist."); + nopCmd.start(); + return true; + } + + LOG.info("Processing {} command from {}", cmd, channel.remoteAddress()); + + if (len == FourLetterCommands.setTraceMaskCmd) { + ByteBuffer mask = ByteBuffer.allocate(8); + message.readBytes(mask); + mask.flip(); + long traceMask = mask.getLong(); + ZooTrace.setTextTraceLevel(traceMask); + SetTraceMaskCommand setMask = new SetTraceMaskCommand(pwriter, this, traceMask); + setMask.start(); + return true; + } else { + CommandExecutor commandExecutor = new CommandExecutor(); + return commandExecutor.execute(this, pwriter, len, zkServer, factory); + } + } + + /** + * Helper that throws an IllegalStateException if the current thread is not + * executing in the channel's event loop thread. + * @param callerMethodName the name of the calling method to add to the exception message. + */ + private void checkIsInEventLoop(String callerMethodName) { + if (!channel.eventLoop().inEventLoop()) { + throw new IllegalStateException(callerMethodName + "() called from non-EventLoop thread"); + } + } + + /** + * Appends buf to queuedBuffer. Does not duplicate buf + * or call any flavor of {@link ByteBuf#retain()}. Caller must ensure that buf + * is not owned by anyone else, as this call transfers ownership of buf to the + * queuedBuffer. + * + * This method should only be called from the event loop thread. + * @param buf the buffer to append to the queue. + */ + private void appendToQueuedBuffer(ByteBuf buf) { + checkIsInEventLoop("appendToQueuedBuffer"); + if (queuedBuffer.numComponents() == queuedBuffer.maxNumComponents()) { + // queuedBuffer has reached its component limit, so combine the existing components. + queuedBuffer.consolidate(); + } + queuedBuffer.addComponent(true, buf); + ServerMetrics.getMetrics().NETTY_QUEUED_BUFFER.add(queuedBuffer.capacity()); + } + + /** + * Process incoming message. This should only be called from the event + * loop thread. + * Note that this method does not call buf.release(). The caller + * is responsible for making sure the buf is released after this method + * returns. + * @param buf the message bytes to process. + */ + void processMessage(ByteBuf buf) { + checkIsInEventLoop("processMessage"); + LOG.debug("0x{} queuedBuffer: {}", Long.toHexString(sessionId), queuedBuffer); + + if (LOG.isTraceEnabled()) { + LOG.trace("0x{} buf {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(buf)); + } + + if (throttled.get()) { + LOG.debug("Received message while throttled"); + // we are throttled, so we need to queue + if (queuedBuffer == null) { + LOG.debug("allocating queue"); + queuedBuffer = channel.alloc().compositeBuffer(); + } + appendToQueuedBuffer(buf.retainedDuplicate()); + if (LOG.isTraceEnabled()) { + LOG.trace("0x{} queuedBuffer {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(queuedBuffer)); + } + } else { + LOG.debug("not throttled"); + if (queuedBuffer != null) { + appendToQueuedBuffer(buf.retainedDuplicate()); + processQueuedBuffer(); + } else { + receiveMessage(buf); + // Have to check !closingChannel, because an error in + // receiveMessage() could have led to close() being called. + if (!closingChannel && buf.isReadable()) { + if (LOG.isTraceEnabled()) { + LOG.trace("Before copy {}", buf); + } + + if (queuedBuffer == null) { + queuedBuffer = channel.alloc().compositeBuffer(); + } + appendToQueuedBuffer(buf.retainedSlice(buf.readerIndex(), buf.readableBytes())); + if (LOG.isTraceEnabled()) { + LOG.trace("Copy is {}", queuedBuffer); + LOG.trace("0x{} queuedBuffer {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(queuedBuffer)); + } + } + } + } + } + + /** + * Try to process previously queued message. This should only be called + * from the event loop thread. + */ + void processQueuedBuffer() { + checkIsInEventLoop("processQueuedBuffer"); + if (queuedBuffer != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("processing queue 0x{} queuedBuffer {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(queuedBuffer)); + } + receiveMessage(queuedBuffer); + if (closingChannel) { + // close() could have been called if receiveMessage() failed + LOG.debug("Processed queue - channel closed, dropping remaining bytes"); + } else if (!queuedBuffer.isReadable()) { + LOG.debug("Processed queue - no bytes remaining"); + releaseQueuedBuffer(); + } else { + LOG.debug("Processed queue - bytes remaining"); + // Try to reduce memory consumption by freeing up buffer space + // which is no longer needed. + queuedBuffer.discardReadComponents(); + } + } else { + LOG.debug("queue empty"); + } + } + + /** + * Clean up queued buffer once it's no longer needed. This should only be + * called from the event loop thread. + */ + private void releaseQueuedBuffer() { + checkIsInEventLoop("releaseQueuedBuffer"); + if (queuedBuffer != null) { + queuedBuffer.release(); + queuedBuffer = null; + } + } + + /** + * Receive a message, which can come from the queued buffer or from a new + * buffer coming in over the channel. This should only be called from the + * event loop thread. + * Note that this method does not call message.release(). The + * caller is responsible for making sure the message is released after this + * method returns. + * @param message the message bytes to process. + */ + private void receiveMessage(ByteBuf message) { + checkIsInEventLoop("receiveMessage"); + try { + while (message.isReadable() && !throttled.get()) { + if (bb != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("message readable {} bb len {} {}", message.readableBytes(), bb.remaining(), bb); + ByteBuffer dat = bb.duplicate(); + dat.flip(); + LOG.trace("0x{} bb {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(dat))); + } + + if (bb.remaining() > message.readableBytes()) { + int newLimit = bb.position() + message.readableBytes(); + bb.limit(newLimit); + } + message.readBytes(bb); + bb.limit(bb.capacity()); + + if (LOG.isTraceEnabled()) { + LOG.trace("after readBytes message readable {} bb len {} {}", message.readableBytes(), bb.remaining(), bb); + ByteBuffer dat = bb.duplicate(); + dat.flip(); + LOG.trace("after readbytes 0x{} bb {}", + Long.toHexString(sessionId), + ByteBufUtil.hexDump(Unpooled.wrappedBuffer(dat))); + } + if (bb.remaining() == 0) { + bb.flip(); + packetReceived(4 + bb.remaining()); + + ZooKeeperServer zks = this.zkServer; + if (zks == null || !zks.isRunning()) { + throw new IOException("ZK down"); + } + if (initialized) { + // TODO: if zks.processPacket() is changed to take a ByteBuffer[], + // we could implement zero-copy queueing. + zks.processPacket(this, bb); + } else { + LOG.debug("got conn req request from {}", getRemoteSocketAddress()); + zks.processConnectRequest(this, bb); + initialized = true; + } + bb = null; + } + } else { + if (LOG.isTraceEnabled()) { + LOG.trace("message readable {} bblenrem {}", message.readableBytes(), bbLen.remaining()); + ByteBuffer dat = bbLen.duplicate(); + dat.flip(); + LOG.trace("0x{} bbLen {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(dat))); + } + + if (message.readableBytes() < bbLen.remaining()) { + bbLen.limit(bbLen.position() + message.readableBytes()); + } + message.readBytes(bbLen); + bbLen.limit(bbLen.capacity()); + if (bbLen.remaining() == 0) { + bbLen.flip(); + + if (LOG.isTraceEnabled()) { + LOG.trace("0x{} bbLen {}", Long.toHexString(sessionId), ByteBufUtil.hexDump(Unpooled.wrappedBuffer(bbLen))); + } + int len = bbLen.getInt(); + if (LOG.isTraceEnabled()) { + LOG.trace("0x{} bbLen len is {}", Long.toHexString(sessionId), len); + } + + bbLen.clear(); + if (!initialized) { + if (checkFourLetterWord(channel, message, len)) { + return; + } + } + if (len < 0 || len > BinaryInputArchive.maxBuffer) { + throw new IOException("Len error " + len); + } + // checkRequestSize will throw IOException if request is rejected + zkServer.checkRequestSizeWhenReceivingMessage(len); + bb = ByteBuffer.allocate(len); + } + } + } + } catch (IOException e) { + LOG.warn("Closing connection to {}", getRemoteSocketAddress(), e); + close(DisconnectReason.IO_EXCEPTION); + } catch (ClientCnxnLimitException e) { + // Common case exception, print at debug level + ServerMetrics.getMetrics().CONNECTION_REJECTED.add(1); + + LOG.debug("Closing connection to {}", getRemoteSocketAddress(), e); + close(DisconnectReason.CLIENT_RATE_LIMIT); + } + } + + /** + * An event that triggers a change in the channel's read setting. + * Used for throttling. By using an enum we can treat the two values as + * singletons and compare with ==. + */ + enum ReadEvent { + DISABLE, + ENABLE + } + + /** + * Note that the netty implementation ignores the waitDisableRecv + * parameter and is always asynchronous. + * @param waitDisableRecv ignored by this implementation. + */ + @Override + public void disableRecv(boolean waitDisableRecv) { + if (throttled.compareAndSet(false, true)) { + LOG.debug("Throttling - disabling recv {}", this); + channel.pipeline().fireUserEventTriggered(ReadEvent.DISABLE); + } + } + + @Override + public void enableRecv() { + if (throttled.compareAndSet(true, false)) { + LOG.debug("Sending unthrottle event {}", this); + channel.pipeline().fireUserEventTriggered(ReadEvent.ENABLE); + } + } + + @Override + public void setSessionTimeout(int sessionTimeout) { + this.sessionTimeout = sessionTimeout; + } + + @Override + public int getInterestOps() { + // This might not be 100% right, but it's only used for printing + // connection info in the netty implementation so it's probably ok. + if (channel == null || !channel.isOpen()) { + return 0; + } + int interestOps = 0; + if (!throttled.get()) { + interestOps |= SelectionKey.OP_READ; + } + if (!channel.isWritable()) { + // OP_READ means "can read", but OP_WRITE means "cannot write", + // it's weird. + interestOps |= SelectionKey.OP_WRITE; + } + return interestOps; + } + + @Override + public InetSocketAddress getRemoteSocketAddress() { + return (InetSocketAddress) channel.remoteAddress(); + } + + /** Send close connection packet to the client. + */ + @Override + public void sendCloseSession() { + sendBuffer(ServerCnxnFactory.closeConn); + } + + @Override + protected ServerStats serverStats() { + if (zkServer == null) { + return null; + } + return zkServer.serverStats(); + } + + @Override + public boolean isSecure() { + return factory.secure; + } + + @Override + public Certificate[] getClientCertificateChain() { + if (clientChain == null) { + return null; + } + return Arrays.copyOf(clientChain, clientChain.length); + } + + @Override + public void setClientCertificateChain(Certificate[] chain) { + if (chain == null) { + clientChain = null; + } else { + clientChain = Arrays.copyOf(chain, chain.length); + } + } + + // For tests and NettyServerCnxnFactory only, thus package-private. + Channel getChannel() { + return channel; + } + + public int getQueuedReadableBytes() { + checkIsInEventLoop("getQueuedReadableBytes"); + if (queuedBuffer != null) { + return queuedBuffer.readableBytes(); + } + return 0; + } + + public void setHandshakeState(HandshakeState state) { + this.handshakeState = state; + } + + public HandshakeState getHandshakeState() { + return this.handshakeState; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxnFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxnFactory.java new file mode 100644 index 0000000..6416cf0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NettyServerCnxnFactory.java @@ -0,0 +1,822 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.Channel; +import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandler; +import io.netty.channel.ChannelHandler.Sharable; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelOption; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.ChannelPromise; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.group.ChannelGroup; +import io.netty.channel.group.ChannelGroupFuture; +import io.netty.channel.group.DefaultChannelGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.ssl.OptionalSslHandler; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslHandler; +import io.netty.util.AttributeKey; +import io.netty.util.ReferenceCountUtil; +import io.netty.util.concurrent.DefaultEventExecutor; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLPeerUnverifiedException; +import javax.net.ssl.SSLSession; +import javax.net.ssl.X509KeyManager; +import javax.net.ssl.X509TrustManager; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.NettyUtils; +import org.apache.zookeeper.common.SSLContextAndOptions; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.server.NettyServerCnxn.HandshakeState; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.auth.X509AuthenticationProvider; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NettyServerCnxnFactory extends ServerCnxnFactory { + + private static final Logger LOG = LoggerFactory.getLogger(NettyServerCnxnFactory.class); + + /** + * Allow client-server sockets to accept both SSL and plaintext connections + */ + public static final String PORT_UNIFICATION_KEY = "zookeeper.client.portUnification"; + private final boolean shouldUsePortUnification; + + /** + * The first byte in TLS protocol is the content type of the subsequent record. + * Handshakes use value 22 (0x16) so the first byte offered on any TCP connection + * attempting to establish a TLS connection will be this value. + * https://tools.ietf.org/html/rfc8446#page-79 + */ + private static final byte TLS_HANDSHAKE_RECORD_TYPE = 0x16; + + private final AtomicInteger outstandingHandshake = new AtomicInteger(); + public static final String OUTSTANDING_HANDSHAKE_LIMIT = "zookeeper.netty.server.outstandingHandshake.limit"; + private int outstandingHandshakeLimit; + private boolean handshakeThrottlingEnabled; + + public void setOutstandingHandshakeLimit(int limit) { + outstandingHandshakeLimit = limit; + handshakeThrottlingEnabled = (secure || shouldUsePortUnification) && outstandingHandshakeLimit > 0; + LOG.info("handshakeThrottlingEnabled = {}, {} = {}", + handshakeThrottlingEnabled, OUTSTANDING_HANDSHAKE_LIMIT, outstandingHandshakeLimit); + } + + private final ServerBootstrap bootstrap; + private Channel parentChannel; + private final ChannelGroup allChannels = new DefaultChannelGroup("zkServerCnxns", new DefaultEventExecutor()); + private final Map ipMap = new ConcurrentHashMap<>(); + private InetSocketAddress localAddress; + private int maxClientCnxns = 60; + int listenBacklog = -1; + private final ClientX509Util x509Util; + + public static final String NETTY_ADVANCED_FLOW_CONTROL = "zookeeper.netty.advancedFlowControl.enabled"; + private boolean advancedFlowControlEnabled = false; + + private static final AttributeKey CONNECTION_ATTRIBUTE = AttributeKey.valueOf("NettyServerCnxn"); + + private static final AtomicReference TEST_ALLOCATOR = new AtomicReference<>(null); + + /** + * A handler that detects whether the client would like to use + * TLS or not and responds in kind. The first bytes are examined + * for the static TLS headers to make the determination and + * placed back in the stream with the correct ChannelHandler + * instantiated. + */ + class DualModeSslHandler extends OptionalSslHandler { + + DualModeSslHandler(SslContext sslContext) { + super(sslContext); + } + + @Override + protected void decode(ChannelHandlerContext context, ByteBuf in, List out) throws Exception { + if (in.readableBytes() >= 5) { + super.decode(context, in, out); + } else if (in.readableBytes() > 0) { + // It requires 5 bytes to detect a proper ssl connection. In the + // case that the server receives fewer, check if we can fail to plaintext. + // This will occur when for any four letter work commands. + if (TLS_HANDSHAKE_RECORD_TYPE != in.getByte(0)) { + LOG.debug("first byte {} does not match TLS handshake, failing to plaintext", in.getByte(0)); + handleNonSsl(context); + } + } + } + + /** + * pulled directly from OptionalSslHandler to allow for access + * @param context + */ + private void handleNonSsl(ChannelHandlerContext context) { + ChannelHandler handler = this.newNonSslHandler(context); + if (handler != null) { + context.pipeline().replace(this, this.newNonSslHandlerName(), handler); + } else { + context.pipeline().remove(this); + } + } + + @Override + protected SslHandler newSslHandler(ChannelHandlerContext context, SslContext sslContext) { + NettyServerCnxn cnxn = Objects.requireNonNull(context.channel().attr(CONNECTION_ATTRIBUTE).get()); + LOG.debug("creating ssl handler for session {}", cnxn.getSessionId()); + SslHandler handler = super.newSslHandler(context, sslContext); + Future handshakeFuture = handler.handshakeFuture(); + handshakeFuture.addListener(new CertificateVerifier(handler, cnxn)); + return handler; + } + + @Override + protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) { + NettyServerCnxn cnxn = Objects.requireNonNull(context.channel().attr(CONNECTION_ATTRIBUTE).get()); + LOG.debug("creating plaintext handler for session {}", cnxn.getSessionId()); + // Mark handshake finished if it's a insecure cnxn + updateHandshakeCountIfStarted(cnxn); + allChannels.add(context.channel()); + addCnxn(cnxn); + return super.newNonSslHandler(context); + } + + } + + private void updateHandshakeCountIfStarted(NettyServerCnxn cnxn) { + if (cnxn != null && cnxn.getHandshakeState() == HandshakeState.STARTED) { + cnxn.setHandshakeState(HandshakeState.FINISHED); + outstandingHandshake.addAndGet(-1); + } + } + + /** + * This is an inner class since we need to extend ChannelDuplexHandler, but + * NettyServerCnxnFactory already extends ServerCnxnFactory. By making it inner + * this class gets access to the member variables and methods. + */ + @Sharable + class CnxnChannelHandler extends ChannelDuplexHandler { + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel active {}", ctx.channel()); + } + + final Channel channel = ctx.channel(); + if (limitTotalNumberOfCnxns()) { + ServerMetrics.getMetrics().CONNECTION_REJECTED.add(1); + channel.close(); + return; + } + InetAddress addr = ((InetSocketAddress) channel.remoteAddress()).getAddress(); + if (maxClientCnxns > 0 && getClientCnxnCount(addr) >= maxClientCnxns) { + ServerMetrics.getMetrics().CONNECTION_REJECTED.add(1); + LOG.warn("Too many connections from {} - max is {}", addr, maxClientCnxns); + channel.close(); + return; + } + + NettyServerCnxn cnxn = new NettyServerCnxn(channel, zkServer, NettyServerCnxnFactory.this); + ctx.channel().attr(CONNECTION_ATTRIBUTE).set(cnxn); + + if (handshakeThrottlingEnabled) { + // Favor to check and throttling even in dual mode which + // accepts both secure and insecure connections, since + // it's more efficient than throttling when we know it's + // a secure connection in DualModeSslHandler. + // + // From benchmark, this reduced around 15% reconnect time. + int outstandingHandshakesNum = outstandingHandshake.addAndGet(1); + if (outstandingHandshakesNum > outstandingHandshakeLimit) { + outstandingHandshake.addAndGet(-1); + channel.close(); + ServerMetrics.getMetrics().TLS_HANDSHAKE_EXCEEDED.add(1); + } else { + cnxn.setHandshakeState(HandshakeState.STARTED); + } + } + + if (secure) { + SslHandler sslHandler = ctx.pipeline().get(SslHandler.class); + Future handshakeFuture = sslHandler.handshakeFuture(); + handshakeFuture.addListener(new CertificateVerifier(sslHandler, cnxn)); + } else if (!shouldUsePortUnification) { + allChannels.add(ctx.channel()); + addCnxn(cnxn); + } + if (ctx.channel().pipeline().get(SslHandler.class) == null) { + SocketAddress remoteAddress = cnxn.getChannel().remoteAddress(); + if (remoteAddress != null + && !((InetSocketAddress) remoteAddress).getAddress().isLoopbackAddress()) { + LOG.trace("NettyChannelHandler channelActive: remote={} local={}", remoteAddress, cnxn.getChannel().localAddress()); + zkServer.serverStats().incrementNonMTLSRemoteConnCount(); + } else { + zkServer.serverStats().incrementNonMTLSLocalConnCount(); + } + } + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel inactive {}", ctx.channel()); + } + + allChannels.remove(ctx.channel()); + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); + if (cnxn != null) { + if (LOG.isTraceEnabled()) { + LOG.trace("Channel inactive caused close {}", cnxn); + } + updateHandshakeCountIfStarted(cnxn); + cnxn.close(ServerCnxn.DisconnectReason.CHANNEL_DISCONNECTED); + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + LOG.warn("Exception caught", cause); + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); + if (cnxn != null) { + LOG.debug("Closing {}", cnxn); + updateHandshakeCountIfStarted(cnxn); + cnxn.close(ServerCnxn.DisconnectReason.CHANNEL_CLOSED_EXCEPTION); + } + } + + @Override + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { + try { + if (evt == NettyServerCnxn.ReadEvent.ENABLE) { + LOG.debug("Received ReadEvent.ENABLE"); + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).get(); + // TODO: Not sure if cnxn can be null here. It becomes null if channelInactive() + // or exceptionCaught() trigger, but it's unclear to me if userEventTriggered() can run + // after either of those. Check for null just to be safe ... + if (cnxn != null) { + if (cnxn.getQueuedReadableBytes() > 0) { + cnxn.processQueuedBuffer(); + if (advancedFlowControlEnabled && cnxn.getQueuedReadableBytes() == 0) { + // trigger a read if we have consumed all + // backlog + ctx.read(); + LOG.debug("Issued a read after queuedBuffer drained"); + } + } + } + if (!advancedFlowControlEnabled) { + ctx.channel().config().setAutoRead(true); + } + } else if (evt == NettyServerCnxn.ReadEvent.DISABLE) { + LOG.debug("Received ReadEvent.DISABLE"); + ctx.channel().config().setAutoRead(false); + } + } finally { + ReferenceCountUtil.release(evt); + } + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + try { + if (LOG.isTraceEnabled()) { + LOG.trace("message received called {}", msg); + } + try { + LOG.debug("New message {} from {}", msg, ctx.channel()); + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).get(); + if (cnxn == null) { + LOG.error("channelRead() on a closed or closing NettyServerCnxn"); + } else { + cnxn.processMessage((ByteBuf) msg); + } + } catch (Exception ex) { + LOG.error("Unexpected exception in receive", ex); + throw ex; + } + } finally { + ReferenceCountUtil.release(msg); + } + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + if (advancedFlowControlEnabled) { + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).get(); + if (cnxn != null && cnxn.getQueuedReadableBytes() == 0 && cnxn.readIssuedAfterReadComplete == 0) { + ctx.read(); + LOG.debug("Issued a read since we do not have anything to consume after channelReadComplete"); + } + } + + ctx.fireChannelReadComplete(); + } + + // Use a single listener instance to reduce GC + // Note: this listener is only added when LOG.isTraceEnabled() is true, + // so it should not do any work other than trace logging. + private final GenericFutureListener> onWriteCompletedTracer = (f) -> { + if (LOG.isTraceEnabled()) { + LOG.trace("write success: {}", f.isSuccess()); + } + }; + + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + if (LOG.isTraceEnabled()) { + promise.addListener(onWriteCompletedTracer); + } + super.write(ctx, msg, promise); + } + + } + + final class CertificateVerifier implements GenericFutureListener> { + + private final SslHandler sslHandler; + private final NettyServerCnxn cnxn; + + CertificateVerifier(SslHandler sslHandler, NettyServerCnxn cnxn) { + this.sslHandler = sslHandler; + this.cnxn = cnxn; + } + + /** + * Only allow the connection to stay open if certificate passes auth + */ + public void operationComplete(Future future) { + updateHandshakeCountIfStarted(cnxn); + + if (future.isSuccess()) { + LOG.debug("Successful handshake with session 0x{}", Long.toHexString(cnxn.getSessionId())); + SSLEngine eng = sslHandler.engine(); + // Don't try to verify certificate if we didn't ask client to present one + if (eng.getNeedClientAuth() || eng.getWantClientAuth()) { + SSLSession session = eng.getSession(); + try { + cnxn.setClientCertificateChain(session.getPeerCertificates()); + } catch (SSLPeerUnverifiedException e) { + if (eng.getNeedClientAuth()) { + // Certificate was requested but not present + LOG.error("Error getting peer certificates", e); + cnxn.close(); + return; + } else { + // Certificate was requested but was optional + // TODO: what auth info should we set on the connection? + final Channel futureChannel = future.getNow(); + allChannels.add(Objects.requireNonNull(futureChannel)); + addCnxn(cnxn); + return; + } + } catch (Exception e) { + LOG.error("Error getting peer certificates", e); + cnxn.close(); + return; + } + + String authProviderProp = System.getProperty(x509Util.getSslAuthProviderProperty(), "x509"); + + X509AuthenticationProvider authProvider = (X509AuthenticationProvider) ProviderRegistry.getProvider(authProviderProp); + + if (authProvider == null) { + LOG.error("X509 Auth provider not found: {}", authProviderProp); + cnxn.close(ServerCnxn.DisconnectReason.AUTH_PROVIDER_NOT_FOUND); + return; + } + + KeeperException.Code code = authProvider.handleAuthentication(cnxn, null); + if (KeeperException.Code.OK != code) { + zkServer.serverStats().incrementAuthFailedCount(); + LOG.error("Authentication failed for session 0x{}", Long.toHexString(cnxn.getSessionId())); + cnxn.close(ServerCnxn.DisconnectReason.SASL_AUTH_FAILURE); + return; + } + } + + final Channel futureChannel = future.getNow(); + allChannels.add(Objects.requireNonNull(futureChannel)); + addCnxn(cnxn); + } else { + zkServer.serverStats().incrementAuthFailedCount(); + LOG.error("Unsuccessful handshake with session 0x{}", Long.toHexString(cnxn.getSessionId())); + cnxn.close(ServerCnxn.DisconnectReason.FAILED_HANDSHAKE); + } + } + + } + + @Sharable + static class ReadIssuedTrackingHandler extends ChannelDuplexHandler { + + @Override + public void read(ChannelHandlerContext ctx) throws Exception { + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).get(); + if (cnxn != null) { + cnxn.readIssuedAfterReadComplete++; + } + + ctx.read(); + } + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { + NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).get(); + if (cnxn != null) { + cnxn.readIssuedAfterReadComplete = 0; + } + + ctx.fireChannelReadComplete(); + } + + } + + CnxnChannelHandler channelHandler = new CnxnChannelHandler(); + ReadIssuedTrackingHandler readIssuedTrackingHandler = new ReadIssuedTrackingHandler(); + + private ServerBootstrap configureBootstrapAllocator(ServerBootstrap bootstrap) { + ByteBufAllocator testAllocator = TEST_ALLOCATOR.get(); + if (testAllocator != null) { + return bootstrap.option(ChannelOption.ALLOCATOR, testAllocator) + .childOption(ChannelOption.ALLOCATOR, testAllocator); + } else { + return bootstrap; + } + } + + NettyServerCnxnFactory() { + x509Util = new ClientX509Util(); + + boolean usePortUnification = Boolean.getBoolean(PORT_UNIFICATION_KEY); + LOG.info("{}={}", PORT_UNIFICATION_KEY, usePortUnification); + if (usePortUnification) { + try { + QuorumPeerConfig.configureSSLAuth(); + } catch (QuorumPeerConfig.ConfigException e) { + LOG.error("unable to set up SslAuthProvider, turning off client port unification", e); + usePortUnification = false; + } + } + this.shouldUsePortUnification = usePortUnification; + + this.advancedFlowControlEnabled = Boolean.getBoolean(NETTY_ADVANCED_FLOW_CONTROL); + LOG.info("{} = {}", NETTY_ADVANCED_FLOW_CONTROL, this.advancedFlowControlEnabled); + + setOutstandingHandshakeLimit(Integer.getInteger(OUTSTANDING_HANDSHAKE_LIMIT, -1)); + + EventLoopGroup bossGroup = NettyUtils.newNioOrEpollEventLoopGroup(NettyUtils.getClientReachableLocalInetAddressCount()); + EventLoopGroup workerGroup = NettyUtils.newNioOrEpollEventLoopGroup(); + ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup) + .channel(NettyUtils.nioOrEpollServerSocketChannel()) + // parent channel options + .option(ChannelOption.SO_REUSEADDR, true) + // child channels options + .childOption(ChannelOption.TCP_NODELAY, true) + .childOption(ChannelOption.SO_LINGER, -1) + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + if (advancedFlowControlEnabled) { + pipeline.addLast(readIssuedTrackingHandler); + } + if (secure) { + initSSL(pipeline, false); + } else if (shouldUsePortUnification) { + initSSL(pipeline, true); + } + pipeline.addLast("servercnxnfactory", channelHandler); + } + }); + this.bootstrap = configureBootstrapAllocator(bootstrap); + this.bootstrap.validate(); + } + + private synchronized void initSSL(ChannelPipeline p, boolean supportPlaintext) throws X509Exception, KeyManagementException, NoSuchAlgorithmException { + String authProviderProp = System.getProperty(x509Util.getSslAuthProviderProperty()); + SslContext nettySslContext; + if (authProviderProp == null) { + SSLContextAndOptions sslContextAndOptions = x509Util.getDefaultSSLContextAndOptions(); + nettySslContext = sslContextAndOptions.createNettyJdkSslContext(sslContextAndOptions.getSSLContext(), false); + } else { + SSLContext sslContext = SSLContext.getInstance(ClientX509Util.DEFAULT_PROTOCOL); + X509AuthenticationProvider authProvider = (X509AuthenticationProvider) ProviderRegistry.getProvider( + System.getProperty(x509Util.getSslAuthProviderProperty(), "x509")); + + if (authProvider == null) { + LOG.error("Auth provider not found: {}", authProviderProp); + throw new SSLContextException("Could not create SSLContext with specified auth provider: " + authProviderProp); + } + + sslContext.init(new X509KeyManager[]{authProvider.getKeyManager()}, new X509TrustManager[]{authProvider.getTrustManager()}, null); + nettySslContext = x509Util.getDefaultSSLContextAndOptions().createNettyJdkSslContext(sslContext, false); + } + + if (supportPlaintext) { + p.addLast("ssl", new DualModeSslHandler(nettySslContext)); + LOG.debug("dual mode SSL handler added for channel: {}", p.channel()); + } else { + p.addLast("ssl", nettySslContext.newHandler(p.channel().alloc())); + LOG.debug("SSL handler added for channel: {}", p.channel()); + } + } + + @Override + public void closeAll(ServerCnxn.DisconnectReason reason) { + LOG.debug("closeAll()"); + + // clear all the connections on which we are selecting + int length = cnxns.size(); + for (ServerCnxn cnxn : cnxns) { + try { + // This will remove the cnxn from cnxns + cnxn.close(reason); + } catch (Exception e) { + LOG.warn("Ignoring exception closing cnxn sessionid 0x{}", Long.toHexString(cnxn.getSessionId()), e); + } + } + + LOG.debug("allChannels size: {} cnxns size: {}", allChannels.size(), length); + } + + @Override + public void configure(InetSocketAddress addr, int maxClientCnxns, int backlog, boolean secure) throws IOException { + configureSaslLogin(); + initMaxCnxns(); + localAddress = addr; + this.maxClientCnxns = maxClientCnxns; + this.secure = secure; + this.listenBacklog = backlog; + } + + /** {@inheritDoc} */ + public int getMaxClientCnxnsPerHost() { + return maxClientCnxns; + } + + /** {@inheritDoc} */ + public void setMaxClientCnxnsPerHost(int max) { + maxClientCnxns = max; + } + + /** {@inheritDoc} */ + public int getSocketListenBacklog() { + return listenBacklog; + } + + @Override + public int getLocalPort() { + return localAddress.getPort(); + } + + private boolean killed; // use synchronized(this) to access + @Override + public void join() throws InterruptedException { + synchronized (this) { + while (!killed) { + wait(); + } + } + } + + @Override + public void shutdown() { + synchronized (this) { + if (killed) { + LOG.info("already shutdown {}", localAddress); + return; + } + } + LOG.info("shutdown called {}", localAddress); + + x509Util.close(); + + if (login != null) { + login.shutdown(); + } + + final EventLoopGroup bossGroup = bootstrap.config().group(); + final EventLoopGroup workerGroup = bootstrap.config().childGroup(); + // null if factory never started + if (parentChannel != null) { + ChannelFuture parentCloseFuture = parentChannel.close(); + if (bossGroup != null) { + parentCloseFuture.addListener(future -> { + bossGroup.shutdownGracefully(); + }); + } + closeAll(ServerCnxn.DisconnectReason.SERVER_SHUTDOWN); + ChannelGroupFuture allChannelsCloseFuture = allChannels.close(); + if (workerGroup != null) { + allChannelsCloseFuture.addListener(future -> { + workerGroup.shutdownGracefully(); + }); + } + } else { + if (bossGroup != null) { + bossGroup.shutdownGracefully(); + } + if (workerGroup != null) { + workerGroup.shutdownGracefully(); + } + } + + if (zkServer != null) { + zkServer.shutdown(); + } + synchronized (this) { + killed = true; + notifyAll(); + } + } + + @Override + public void start() { + if (listenBacklog != -1) { + bootstrap.option(ChannelOption.SO_BACKLOG, listenBacklog); + } + LOG.info("binding to port {}", localAddress); + parentChannel = bootstrap.bind(localAddress).syncUninterruptibly().channel(); + // Port changes after bind() if the original port was 0, update + // localAddress to get the real port. + localAddress = (InetSocketAddress) parentChannel.localAddress(); + LOG.info("bound to port {}", getLocalPort()); + } + + public void reconfigure(InetSocketAddress addr) { + LOG.info("binding to port {}, {}", addr, localAddress); + if (addr != null && localAddress != null) { + if (addr.equals(localAddress) || (addr.getAddress().isAnyLocalAddress() + && localAddress.getAddress().isAnyLocalAddress() + && addr.getPort() == localAddress.getPort())) { + LOG.info("address is the same, skip rebinding"); + return; + } + } + + Channel oldChannel = parentChannel; + try { + parentChannel = bootstrap.bind(addr).syncUninterruptibly().channel(); + // Port changes after bind() if the original port was 0, update + // localAddress to get the real port. + localAddress = (InetSocketAddress) parentChannel.localAddress(); + LOG.info("bound to port {}", getLocalPort()); + } catch (Exception e) { + LOG.error("Error while reconfiguring", e); + } finally { + oldChannel.close(); + } + } + + @Override + public void startup(ZooKeeperServer zks, boolean startServer) throws IOException, InterruptedException { + start(); + setZooKeeperServer(zks); + if (startServer) { + zks.startdata(); + zks.startup(); + } + } + + @Override + public Iterable getConnections() { + return cnxns; + } + + @Override + public InetSocketAddress getLocalAddress() { + return localAddress; + } + + private void addCnxn(final NettyServerCnxn cnxn) { + cnxns.add(cnxn); + InetAddress addr = ((InetSocketAddress) cnxn.getChannel().remoteAddress()).getAddress(); + + ipMap.compute(addr, (a, cnxnCount) -> { + if (cnxnCount == null) { + cnxnCount = new AtomicInteger(); + } + cnxnCount.incrementAndGet(); + return cnxnCount; + }); + } + + void removeCnxnFromIpMap(NettyServerCnxn cnxn, InetAddress remoteAddress) { + ipMap.compute(remoteAddress, (addr, cnxnCount) -> { + if (cnxnCount == null) { + LOG.error("Unexpected remote address {} when removing cnxn {}", remoteAddress, cnxn); + return null; + } + final int newValue = cnxnCount.decrementAndGet(); + return newValue == 0 ? null : cnxnCount; + }); + } + + private int getClientCnxnCount(final InetAddress addr) { + final AtomicInteger count = ipMap.get(addr); + return count == null ? 0 : count.get(); + } + + @Override + public void resetAllConnectionStats() { + // No need to synchronize since cnxns is backed by a ConcurrentHashMap + for (ServerCnxn c : cnxns) { + c.resetStats(); + } + } + + @Override + public Iterable> getAllConnectionInfo(boolean brief) { + Set> info = new HashSet>(); + // No need to synchronize since cnxns is backed by a ConcurrentHashMap + for (ServerCnxn c : cnxns) { + info.add(c.getConnectionInfo(brief)); + } + return info; + } + + /** + * Sets the test ByteBufAllocator. This allocator will be used by all + * future instances of this class. + * It is not recommended to use this method outside of testing. + * @param allocator the ByteBufAllocator to use for all netty buffer + * allocations. + */ + static void setTestAllocator(ByteBufAllocator allocator) { + TEST_ALLOCATOR.set(allocator); + } + + /** + * Clears the test ByteBufAllocator. The default allocator will be used + * by all future instances of this class. + * It is not recommended to use this method outside of testing. + */ + static void clearTestAllocator() { + TEST_ALLOCATOR.set(null); + } + + // VisibleForTest + public void setAdvancedFlowControlEnabled(boolean advancedFlowControlEnabled) { + this.advancedFlowControlEnabled = advancedFlowControlEnabled; + } + + // VisibleForTest + public void setSecure(boolean secure) { + this.secure = secure; + } + + // VisibleForTest + public Channel getParentChannel() { + return parentChannel; + } + + public int getOutstandingHandshakeNum() { + return outstandingHandshake.get(); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NodeHashMap.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NodeHashMap.java new file mode 100644 index 0000000..bcdc5e9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NodeHashMap.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.Map; +import java.util.Set; + +/** + * The interface defined to manage the hash based on the entries in the nodes map. + */ +public interface NodeHashMap { + + /** + * Add the node into the map and update the digest with the new node. + * + * @param path the path of the node + * @param node the actual node associated with this path + */ + DataNode put(String path, DataNode node); + + /** + * Add the node into the map without update the digest. + * + * @param path the path of the node + * @param node the actual node associated with this path + */ + DataNode putWithoutDigest(String path, DataNode node); + + /** + * Return the data node associated with the path. + * + * @param path the path to read from + */ + DataNode get(String path); + + /** + * Remove the path from the internal nodes map. + * + * @param path the path to remove + * @return the node being removed + */ + DataNode remove(String path); + + /** + * Return all the entries inside this map. + */ + Set> entrySet(); + + /** + * Clear all the items stored inside this map. + */ + void clear(); + + /** + * Return the size of the nodes stored in this map. + */ + int size(); + + /** + * Called before we made the change on the node, which will clear + * the digest associated with it. + * + * @param path the path being changed + * @param node the node associated with the path + */ + void preChange(String path, DataNode node); + + /** + * Called after making the changes on the node, which will update + * the digest. + * + * @param path the path being changed + * @param node the node associated with the path + */ + void postChange(String path, DataNode node); + + /** + * Return the digest value. + */ + long getDigest(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/NodeHashMapImpl.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NodeHashMapImpl.java new file mode 100644 index 0000000..04f48ae --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/NodeHashMapImpl.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.util.AdHash; + +/** + * a simple wrapper to ConcurrentHashMap that recalculates a digest after + * each mutation. + */ +public class NodeHashMapImpl implements NodeHashMap { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、key 是 路径 + * 2、DataNode 一个节点的抽象 + */ + private final ConcurrentHashMap nodes; + + private final boolean digestEnabled; + private final DigestCalculator digestCalculator; + + private final AdHash hash; + + public NodeHashMapImpl(DigestCalculator digestCalculator) { + this.digestCalculator = digestCalculator; + nodes = new ConcurrentHashMap<>(); + hash = new AdHash(); + digestEnabled = ZooKeeperServer.isDigestEnabled(); + } + + @Override + public DataNode put(String path, DataNode node) { + DataNode oldNode = nodes.put(path, node); + addDigest(path, node); + if (oldNode != null) { + removeDigest(path, oldNode); + } + return oldNode; + } + + @Override + public DataNode putWithoutDigest(String path, DataNode node) { + return nodes.put(path, node); + } + + @Override + public DataNode get(String path) { + return nodes.get(path); + } + + @Override + public DataNode remove(String path) { + DataNode oldNode = nodes.remove(path); + if (oldNode != null) { + removeDigest(path, oldNode); + } + return oldNode; + } + + @Override + public Set> entrySet() { + return nodes.entrySet(); + } + + @Override + public void clear() { + nodes.clear(); + hash.clear(); + } + + @Override + public int size() { + return nodes.size(); + } + + @Override + public void preChange(String path, DataNode node) { + removeDigest(path, node); + } + + @Override + public void postChange(String path, DataNode node) { + // we just made a change, so make sure the digest is + // invalidated + node.digestCached = false; + addDigest(path, node); + } + + private void addDigest(String path, DataNode node) { + // Excluding everything under '/zookeeper/' for digest calculation. + if (path.startsWith(ZooDefs.ZOOKEEPER_NODE_SUBTREE)) { + return; + } + if (digestEnabled) { + hash.addDigest(digestCalculator.calculateDigest(path, node)); + } + } + + private void removeDigest(String path, DataNode node) { + // Excluding everything under '/zookeeper/' for digest calculation. + if (path.startsWith(ZooDefs.ZOOKEEPER_NODE_SUBTREE)) { + return; + } + if (digestEnabled) { + hash.removeDigest(digestCalculator.calculateDigest(path, node)); + } + } + + @Override + public long getDigest() { + return hash.getHash(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ObserverBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ObserverBean.java new file mode 100644 index 0000000..91e7223 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ObserverBean.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.net.InetSocketAddress; +import org.apache.zookeeper.server.quorum.Observer; +import org.apache.zookeeper.server.quorum.ObserverMXBean; +import org.apache.zookeeper.server.quorum.QuorumPeer; + +/** + * ObserverBean + * + */ +public class ObserverBean extends ZooKeeperServerBean implements ObserverMXBean { + + private Observer observer; + + public ObserverBean(Observer observer, ZooKeeperServer zks) { + super(zks); + this.observer = observer; + } + + public String getName() { + return "Observer"; + } + + public int getPendingRevalidationCount() { + return this.observer.getPendingRevalidationsCount(); + } + + public String getQuorumAddress() { + return observer.getSocket().toString(); + } + + public String getLearnerMaster() { + QuorumPeer.QuorumServer learnerMaster = observer.getCurrentLearnerMaster(); + if (learnerMaster == null || learnerMaster.addr.isEmpty()) { + return "Unknown"; + } + InetSocketAddress address = learnerMaster.addr.getReachableOrOne(); + return address.getAddress().getHostAddress() + ":" + address.getPort(); + } + + public void setLearnerMaster(String learnerMaster) { + if (!observer.setLearnerMaster(learnerMaster)) { + throw new IllegalArgumentException("Not a valid learner master"); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java new file mode 100644 index 0000000..542915d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/PrepRequestProcessor.java @@ -0,0 +1,1163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringReader; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.BadArgumentsException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.MultiOperationRecord; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.common.StringUtils; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.proto.CheckVersionRequest; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.CreateTTLRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.ReconfigRequest; +import org.apache.zookeeper.proto.SetACLRequest; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord; +import org.apache.zookeeper.server.ZooKeeperServer.PrecalculatedDigest; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.auth.ServerAuthenticationProvider; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.txn.CheckVersionTxn; +import org.apache.zookeeper.txn.CloseSessionTxn; +import org.apache.zookeeper.txn.CreateContainerTxn; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.CreateTTLTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.SetACLTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This request processor is generally at the start of a RequestProcessor + * change. It sets up any transactions associated with requests that change the + * state of the system. It counts on ZooKeeperServer to update + * outstandingRequests, so that it can take into account transactions that are + * in the queue to be applied when generating a transaction. + */ +public class PrepRequestProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(PrepRequestProcessor.class); + + /** + * this is only for testing purposes. + * should never be used otherwise + */ + private static boolean failCreate = false; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 存储请求的队列 + */ + LinkedBlockingQueue submittedRequests = new LinkedBlockingQueue(); + + private final RequestProcessor nextProcessor; + private final boolean digestEnabled; + private DigestCalculator digestCalculator; + + ZooKeeperServer zks; + + public enum DigestOpCode { + NOOP, + ADD, + REMOVE, + UPDATE; + } + + public PrepRequestProcessor(ZooKeeperServer zks, RequestProcessor nextProcessor) { + super("ProcessThread(sid:" + zks.getServerId() + " cport:" + zks.getClientPort() + "):", + zks.getZooKeeperServerListener()); + this.nextProcessor = nextProcessor; + this.zks = zks; + this.digestEnabled = ZooKeeperServer.isDigestEnabled(); + if(this.digestEnabled) { + this.digestCalculator = new DigestCalculator(); + } + } + + /** + * method for tests to set failCreate + * + * @param b + */ + public static void setFailCreate(boolean b) { + failCreate = b; + } + + @Override + public void run() { + LOG.info(String.format("PrepRequestProcessor (sid:%d) started, reconfigEnabled=%s", zks.getServerId(), + zks.reconfigEnabled)); + try { + while(true) { + ServerMetrics.getMetrics().PREP_PROCESSOR_QUEUE_SIZE.add(submittedRequests.size()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 消费 submittedRequests 队列,取出请求 + */ + Request request = submittedRequests.take(); + + ServerMetrics.getMetrics().PREP_PROCESSOR_QUEUE_TIME + .add(Time.currentElapsedTime() - request.prepQueueStartTime); + long traceMask = ZooTrace.CLIENT_REQUEST_TRACE_MASK; + if(request.type == OpCode.ping) { + traceMask = ZooTrace.CLIENT_PING_TRACE_MASK; + } + if(LOG.isTraceEnabled()) { + ZooTrace.logRequest(LOG, traceMask, 'P', request, ""); + } + if(Request.requestOfDeath == request) { + break; + } + + // TODO_MA 马中华 注释: 记录开始处理的时间 + request.prepStartTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 请求预处理 + * 识别这个请求,到底是事务请求,还是非事务请求 + */ + pRequest(request); + } + } catch(Exception e) { + handleException(this.getName(), e); + } + LOG.info("PrepRequestProcessor exited loop!"); + } + + private ChangeRecord getRecordForPath(String path) throws KeeperException.NoNodeException { + ChangeRecord lastChange = null; + synchronized(zks.outstandingChanges) { + lastChange = zks.outstandingChangesForPath.get(path); + if(lastChange == null) { + DataNode n = zks.getZKDatabase().getNode(path); + if(n != null) { + Set children; + synchronized(n) { + children = n.getChildren(); + } + lastChange = new ChangeRecord(-1, path, n.stat, children.size(), zks.getZKDatabase().aclForNode(n)); + + if(digestEnabled) { + lastChange.precalculatedDigest = new PrecalculatedDigest( + digestCalculator.calculateDigest(path, n), 0); + } + lastChange.data = n.getData(); + } + } + } + if(lastChange == null || lastChange.stat == null) { + throw new KeeperException.NoNodeException(path); + } + return lastChange; + } + + private ChangeRecord getOutstandingChange(String path) { + synchronized(zks.outstandingChanges) { + return zks.outstandingChangesForPath.get(path); + } + } + + protected void addChangeRecord(ChangeRecord c) { + synchronized(zks.outstandingChanges) { + zks.outstandingChanges.add(c); + zks.outstandingChangesForPath.put(c.path, c); + ServerMetrics.getMetrics().OUTSTANDING_CHANGES_QUEUED.add(1); + } + } + + /** + * Grab current pending change records for each op in a multi-op. + * + * This is used inside MultiOp error code path to rollback in the event + * of a failed multi-op. + * + * @param multiRequest + * @return a map that contains previously existed records that probably need to be + * rolled back in any failure. + */ + private Map getPendingChanges(MultiOperationRecord multiRequest) { + Map pendingChangeRecords = new HashMap(); + + for(Op op : multiRequest) { + String path = op.getPath(); + ChangeRecord cr = getOutstandingChange(path); + // only previously existing records need to be rolled back. + if(cr != null) { + pendingChangeRecords.put(path, cr); + } + + /* + * ZOOKEEPER-1624 - We need to store for parent's ChangeRecord + * of the parent node of a request. So that if this is a + * sequential node creation request, rollbackPendingChanges() + * can restore previous parent's ChangeRecord correctly. + * + * Otherwise, sequential node name generation will be incorrect + * for a subsequent request. + */ + int lastSlash = path.lastIndexOf('/'); + if(lastSlash == -1 || path.indexOf('\0') != -1) { + continue; + } + String parentPath = path.substring(0, lastSlash); + ChangeRecord parentCr = getOutstandingChange(parentPath); + if(parentCr != null) { + pendingChangeRecords.put(parentPath, parentCr); + } + } + + return pendingChangeRecords; + } + + /** + * Rollback pending changes records from a failed multi-op. + * + * If a multi-op fails, we can't leave any invalid change records we created + * around. We also need to restore their prior value (if any) if their prior + * value is still valid. + * + * @param zxid + * @param pendingChangeRecords + */ + void rollbackPendingChanges(long zxid, Map pendingChangeRecords) { + synchronized(zks.outstandingChanges) { + // Grab a list iterator starting at the END of the list so we can iterate in reverse + Iterator iter = zks.outstandingChanges.descendingIterator(); + while(iter.hasNext()) { + ChangeRecord c = iter.next(); + if(c.zxid == zxid) { + iter.remove(); + // Remove all outstanding changes for paths of this multi. + // Previous records will be added back later. + zks.outstandingChangesForPath.remove(c.path); + } else { + break; + } + } + + // we don't need to roll back any records because there is nothing left. + if(zks.outstandingChanges.isEmpty()) { + return; + } + + long firstZxid = zks.outstandingChanges.peek().zxid; + + for(ChangeRecord c : pendingChangeRecords.values()) { + // Don't apply any prior change records less than firstZxid. + // Note that previous outstanding requests might have been removed + // once they are completed. + if(c.zxid < firstZxid) { + continue; + } + + // add previously existing records back. + zks.outstandingChangesForPath.put(c.path, c); + } + } + } + + /** + * Performs basic validation of a path for a create request. + * Throws if the path is not valid and returns the parent path. + * + * @throws BadArgumentsException + */ + private String validatePathForCreate(String path, long sessionId) throws BadArgumentsException { + int lastSlash = path.lastIndexOf('/'); + if(lastSlash == -1 || path.indexOf('\0') != -1 || failCreate) { + LOG.info("Invalid path {} with session 0x{}", path, Long.toHexString(sessionId)); + throw new KeeperException.BadArgumentsException(path); + } + return path.substring(0, lastSlash); + } + + /** + * This method will be called inside the ProcessRequestThread, which is a + * singleton, so there will be a single thread calling this code. + * + * @param type + * @param zxid + * @param request + * @param record + */ + protected void pRequest2Txn(int type, long zxid, Request request, Record record, + boolean deserialize) throws KeeperException, IOException, RequestProcessorException { + if(request.getHdr() == null) { + request.setHdr(new TxnHeader(request.sessionId, request.cxid, zxid, Time.currentWallTime(), type)); + } + + PrecalculatedDigest precalculatedDigest; + switch(type) { + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: { + pRequest2TxnCreate(type, request, record, deserialize); + break; + } + case OpCode.deleteContainer: { + String path = new String(request.request.array()); + String parentPath = getParentPathAndValidate(path); + ChangeRecord nodeRecord = getRecordForPath(path); + if(nodeRecord.childCount > 0) { + throw new KeeperException.NotEmptyException(path); + } + if(EphemeralType.get(nodeRecord.stat.getEphemeralOwner()) == EphemeralType.NORMAL) { + throw new KeeperException.BadVersionException(path); + } + ChangeRecord parentRecord = getRecordForPath(parentPath); + request.setTxn(new DeleteTxn(path)); + parentRecord = parentRecord.duplicate(request.getHdr().getZxid()); + parentRecord.childCount--; + parentRecord.stat.setPzxid(request.getHdr().getZxid()); + parentRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, parentPath, parentRecord.data, + parentRecord.stat); + addChangeRecord(parentRecord); + + nodeRecord = new ChangeRecord(request.getHdr().getZxid(), path, null, -1, null); + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.REMOVE, path); + setTxnDigest(request, nodeRecord.precalculatedDigest); + addChangeRecord(nodeRecord); + break; + } + case OpCode.delete: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + DeleteRequest deleteRequest = (DeleteRequest) record; + if(deserialize) { + ByteBufferInputStream.byteBuffer2Record(request.request, deleteRequest); + } + String path = deleteRequest.getPath(); + String parentPath = getParentPathAndValidate(path); + ChangeRecord parentRecord = getRecordForPath(parentPath); + zks.checkACL(request.cnxn, parentRecord.acl, ZooDefs.Perms.DELETE, request.authInfo, path, null); + ChangeRecord nodeRecord = getRecordForPath(path); + checkAndIncVersion(nodeRecord.stat.getVersion(), deleteRequest.getVersion(), path); + if(nodeRecord.childCount > 0) { + throw new KeeperException.NotEmptyException(path); + } + request.setTxn(new DeleteTxn(path)); + parentRecord = parentRecord.duplicate(request.getHdr().getZxid()); + parentRecord.childCount--; + parentRecord.stat.setPzxid(request.getHdr().getZxid()); + parentRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, parentPath, parentRecord.data, + parentRecord.stat); + addChangeRecord(parentRecord); + + nodeRecord = new ChangeRecord(request.getHdr().getZxid(), path, null, -1, null); + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.REMOVE, path); + setTxnDigest(request, nodeRecord.precalculatedDigest); + addChangeRecord(nodeRecord); + break; + case OpCode.setData: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + SetDataRequest setDataRequest = (SetDataRequest) record; + if(deserialize) { + ByteBufferInputStream.byteBuffer2Record(request.request, setDataRequest); + } + path = setDataRequest.getPath(); + validatePath(path, request.sessionId); + nodeRecord = getRecordForPath(path); + zks.checkACL(request.cnxn, nodeRecord.acl, ZooDefs.Perms.WRITE, request.authInfo, path, null); + int newVersion = checkAndIncVersion(nodeRecord.stat.getVersion(), setDataRequest.getVersion(), path); + request.setTxn(new SetDataTxn(path, setDataRequest.getData(), newVersion)); + nodeRecord = nodeRecord.duplicate(request.getHdr().getZxid()); + nodeRecord.stat.setVersion(newVersion); + nodeRecord.stat.setMtime(request.getHdr().getTime()); + nodeRecord.stat.setMzxid(zxid); + nodeRecord.data = setDataRequest.getData(); + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, path, nodeRecord.data, + nodeRecord.stat); + setTxnDigest(request, nodeRecord.precalculatedDigest); + addChangeRecord(nodeRecord); + break; + case OpCode.reconfig: + if(!zks.isReconfigEnabled()) { + LOG.error("Reconfig operation requested but reconfig feature is disabled."); + throw new KeeperException.ReconfigDisabledException(); + } + + if(ZooKeeperServer.skipACL) { + LOG.warn("skipACL is set, reconfig operation will skip ACL checks!"); + } + + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + LeaderZooKeeperServer lzks; + try { + lzks = (LeaderZooKeeperServer) zks; + } catch(ClassCastException e) { + // standalone mode - reconfiguration currently not supported + throw new KeeperException.UnimplementedException(); + } + QuorumVerifier lastSeenQV = lzks.self.getLastSeenQuorumVerifier(); + // check that there's no reconfig in progress + if(lastSeenQV.getVersion() != lzks.self.getQuorumVerifier().getVersion()) { + throw new KeeperException.ReconfigInProgress(); + } + ReconfigRequest reconfigRequest = (ReconfigRequest) record; + long configId = reconfigRequest.getCurConfigId(); + + if(configId != -1 && configId != lzks.self.getLastSeenQuorumVerifier().getVersion()) { + String msg = "Reconfiguration from version " + configId + " failed -- last seen version is " + lzks.self + .getLastSeenQuorumVerifier().getVersion(); + throw new KeeperException.BadVersionException(msg); + } + + String newMembers = reconfigRequest.getNewMembers(); + + if(newMembers != null) { //non-incremental membership change + LOG.info("Non-incremental reconfig"); + + // Input may be delimited by either commas or newlines so convert to common newline separated format + newMembers = newMembers.replaceAll(",", "\n"); + + try { + Properties props = new Properties(); + props.load(new StringReader(newMembers)); + request.qv = QuorumPeerConfig.parseDynamicConfig(props, lzks.self.getElectionType(), true, false); + request.qv.setVersion(request.getHdr().getZxid()); + } catch(IOException | ConfigException e) { + throw new KeeperException.BadArgumentsException(e.getMessage()); + } + } else { //incremental change - must be a majority quorum system + LOG.info("Incremental reconfig"); + + List joiningServers = null; + String joiningServersString = reconfigRequest.getJoiningServers(); + if(joiningServersString != null) { + joiningServers = StringUtils.split(joiningServersString, ","); + } + + List leavingServers = null; + String leavingServersString = reconfigRequest.getLeavingServers(); + if(leavingServersString != null) { + leavingServers = StringUtils.split(leavingServersString, ","); + } + + if(!(lastSeenQV instanceof QuorumMaj)) { + String msg = "Incremental reconfiguration requested but last configuration seen has a non-majority quorum system"; + LOG.warn(msg); + throw new KeeperException.BadArgumentsException(msg); + } + Map nextServers = new HashMap(lastSeenQV.getAllMembers()); + try { + if(leavingServers != null) { + for(String leaving : leavingServers) { + long sid = Long.parseLong(leaving); + nextServers.remove(sid); + } + } + if(joiningServers != null) { + for(String joiner : joiningServers) { + // joiner should have the following format: server.x = server_spec;client_spec + String[] parts = StringUtils.split(joiner, "=").toArray(new String[0]); + if(parts.length != 2) { + throw new KeeperException.BadArgumentsException("Wrong format of server string"); + } + // extract server id x from first part of joiner: server.x + Long sid = Long.parseLong(parts[0].substring(parts[0].lastIndexOf('.') + 1)); + QuorumServer qs = new QuorumServer(sid, parts[1]); + if(qs.clientAddr == null || qs.electionAddr == null || qs.addr == null) { + throw new KeeperException.BadArgumentsException( + "Wrong format of server string - each server should have 3 ports specified"); + } + + // check duplication of addresses and ports + for(QuorumServer nqs : nextServers.values()) { + if(qs.id == nqs.id) { + continue; + } + qs.checkAddressDuplicate(nqs); + } + + nextServers.remove(qs.id); + nextServers.put(qs.id, qs); + } + } + } catch(ConfigException e) { + throw new KeeperException.BadArgumentsException("Reconfiguration failed"); + } + request.qv = new QuorumMaj(nextServers); + request.qv.setVersion(request.getHdr().getZxid()); + } + if(QuorumPeerConfig.isStandaloneEnabled() && request.qv.getVotingMembers().size() < 2) { + String msg = "Reconfig failed - new configuration must include at least 2 followers"; + LOG.warn(msg); + throw new KeeperException.BadArgumentsException(msg); + } else if(request.qv.getVotingMembers().size() < 1) { + String msg = "Reconfig failed - new configuration must include at least 1 follower"; + LOG.warn(msg); + throw new KeeperException.BadArgumentsException(msg); + } + + if(!lzks.getLeader().isQuorumSynced(request.qv)) { + String msg2 = "Reconfig failed - there must be a connected and synced quorum in new configuration"; + LOG.warn(msg2); + throw new KeeperException.NewConfigNoQuorum(); + } + + nodeRecord = getRecordForPath(ZooDefs.CONFIG_NODE); + zks.checkACL(request.cnxn, nodeRecord.acl, ZooDefs.Perms.WRITE, request.authInfo, null, null); + SetDataTxn setDataTxn = new SetDataTxn(ZooDefs.CONFIG_NODE, request.qv.toString().getBytes(), -1); + request.setTxn(setDataTxn); + nodeRecord = nodeRecord.duplicate(request.getHdr().getZxid()); + nodeRecord.stat.setVersion(-1); + nodeRecord.stat.setMtime(request.getHdr().getTime()); + nodeRecord.stat.setMzxid(zxid); + nodeRecord.data = setDataTxn.getData(); + // Reconfig is currently a noop from digest computation + // perspective since config node is not covered by the digests. + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.NOOP, ZooDefs.CONFIG_NODE, + nodeRecord.data, nodeRecord.stat); + setTxnDigest(request, nodeRecord.precalculatedDigest); + addChangeRecord(nodeRecord); + + break; + case OpCode.setACL: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + SetACLRequest setAclRequest = (SetACLRequest) record; + if(deserialize) { + ByteBufferInputStream.byteBuffer2Record(request.request, setAclRequest); + } + path = setAclRequest.getPath(); + validatePath(path, request.sessionId); + List listACL = fixupACL(path, request.authInfo, setAclRequest.getAcl()); + nodeRecord = getRecordForPath(path); + zks.checkACL(request.cnxn, nodeRecord.acl, ZooDefs.Perms.ADMIN, request.authInfo, path, listACL); + newVersion = checkAndIncVersion(nodeRecord.stat.getAversion(), setAclRequest.getVersion(), path); + request.setTxn(new SetACLTxn(path, listACL, newVersion)); + nodeRecord = nodeRecord.duplicate(request.getHdr().getZxid()); + nodeRecord.stat.setAversion(newVersion); + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, path, nodeRecord.data, + nodeRecord.stat); + setTxnDigest(request, nodeRecord.precalculatedDigest); + addChangeRecord(nodeRecord); + break; + case OpCode.createSession: + request.request.rewind(); + int to = request.request.getInt(); + request.setTxn(new CreateSessionTxn(to)); + request.request.rewind(); + // only add the global session tracker but not to ZKDb + zks.sessionTracker.trackSession(request.sessionId, to); + zks.setOwner(request.sessionId, request.getOwner()); + break; + case OpCode.closeSession: + // We don't want to do this check since the session expiration thread + // queues up this operation without being the session owner. + // this request is the last of the session so it should be ok + //zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + long startTime = Time.currentElapsedTime(); + synchronized(zks.outstandingChanges) { + // need to move getEphemerals into zks.outstandingChanges + // synchronized block, otherwise there will be a race + // condition with the on flying deleteNode txn, and we'll + // delete the node again here, which is not correct + Set es = zks.getZKDatabase().getEphemerals(request.sessionId); + for(ChangeRecord c : zks.outstandingChanges) { + if(c.stat == null) { + // Doing a delete + es.remove(c.path); + } else if(c.stat.getEphemeralOwner() == request.sessionId) { + es.add(c.path); + } + } + for(String path2Delete : es) { + if(digestEnabled) { + parentPath = getParentPathAndValidate(path2Delete); + parentRecord = getRecordForPath(parentPath); + parentRecord = parentRecord.duplicate(request.getHdr().getZxid()); + parentRecord.stat.setPzxid(request.getHdr().getZxid()); + parentRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, parentPath, + parentRecord.data, parentRecord.stat); + addChangeRecord(parentRecord); + } + nodeRecord = new ChangeRecord(request.getHdr().getZxid(), path2Delete, null, 0, null); + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.REMOVE, path2Delete); + addChangeRecord(nodeRecord); + } + if(ZooKeeperServer.isCloseSessionTxnEnabled()) { + request.setTxn(new CloseSessionTxn(new ArrayList(es))); + } + zks.sessionTracker.setSessionClosing(request.sessionId); + } + ServerMetrics.getMetrics().CLOSE_SESSION_PREP_TIME.add(Time.currentElapsedTime() - startTime); + break; + case OpCode.check: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + CheckVersionRequest checkVersionRequest = (CheckVersionRequest) record; + if(deserialize) { + ByteBufferInputStream.byteBuffer2Record(request.request, checkVersionRequest); + } + path = checkVersionRequest.getPath(); + validatePath(path, request.sessionId); + nodeRecord = getRecordForPath(path); + zks.checkACL(request.cnxn, nodeRecord.acl, ZooDefs.Perms.READ, request.authInfo, path, null); + request.setTxn(new CheckVersionTxn(path, + checkAndIncVersion(nodeRecord.stat.getVersion(), checkVersionRequest.getVersion(), path))); + break; + default: + LOG.warn("unknown type {}", type); + break; + } + + // If the txn is not going to mutate anything, like createSession, + // we just set the current tree digest in it + if(request.getTxnDigest() == null && digestEnabled) { + setTxnDigest(request); + } + } + + private void pRequest2TxnCreate(int type, Request request, Record record, + boolean deserialize) throws IOException, KeeperException { + if(deserialize) { + ByteBufferInputStream.byteBuffer2Record(request.request, record); + } + + int flags; + String path; + List acl; + byte[] data; + long ttl; + if(type == OpCode.createTTL) { + CreateTTLRequest createTtlRequest = (CreateTTLRequest) record; + flags = createTtlRequest.getFlags(); + path = createTtlRequest.getPath(); + acl = createTtlRequest.getAcl(); + data = createTtlRequest.getData(); + ttl = createTtlRequest.getTtl(); + } else { + CreateRequest createRequest = (CreateRequest) record; + flags = createRequest.getFlags(); + path = createRequest.getPath(); + acl = createRequest.getAcl(); + data = createRequest.getData(); + ttl = -1; + } + CreateMode createMode = CreateMode.fromFlag(flags); + validateCreateRequest(path, createMode, request, ttl); + String parentPath = validatePathForCreate(path, request.sessionId); + + List listACL = fixupACL(path, request.authInfo, acl); + ChangeRecord parentRecord = getRecordForPath(parentPath); + + zks.checkACL(request.cnxn, parentRecord.acl, ZooDefs.Perms.CREATE, request.authInfo, path, listACL); + int parentCVersion = parentRecord.stat.getCversion(); + if(createMode.isSequential()) { + path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion); + } + validatePath(path, request.sessionId); + try { + if(getRecordForPath(path) != null) { + throw new KeeperException.NodeExistsException(path); + } + } catch(KeeperException.NoNodeException e) { + // ignore this one + } + boolean ephemeralParent = EphemeralType.get(parentRecord.stat.getEphemeralOwner()) == EphemeralType.NORMAL; + if(ephemeralParent) { + throw new KeeperException.NoChildrenForEphemeralsException(path); + } + int newCversion = parentRecord.stat.getCversion() + 1; + if(type == OpCode.createContainer) { + request.setTxn(new CreateContainerTxn(path, data, listACL, newCversion)); + } else if(type == OpCode.createTTL) { + request.setTxn(new CreateTTLTxn(path, data, listACL, newCversion, ttl)); + } else { + request.setTxn(new CreateTxn(path, data, listACL, createMode.isEphemeral(), newCversion)); + } + + TxnHeader hdr = request.getHdr(); + long ephemeralOwner = 0; + if(createMode.isContainer()) { + ephemeralOwner = EphemeralType.CONTAINER_EPHEMERAL_OWNER; + } else if(createMode.isTTL()) { + ephemeralOwner = EphemeralType.TTL.toEphemeralOwner(ttl); + } else if(createMode.isEphemeral()) { + ephemeralOwner = request.sessionId; + } + StatPersisted s = DataTree.createStat(hdr.getZxid(), hdr.getTime(), ephemeralOwner); + parentRecord = parentRecord.duplicate(request.getHdr().getZxid()); + parentRecord.childCount++; + parentRecord.stat.setCversion(newCversion); + parentRecord.stat.setPzxid(request.getHdr().getZxid()); + parentRecord.precalculatedDigest = precalculateDigest(DigestOpCode.UPDATE, parentPath, parentRecord.data, + parentRecord.stat); + addChangeRecord(parentRecord); + ChangeRecord nodeRecord = new ChangeRecord(request.getHdr().getZxid(), path, s, 0, listACL); + nodeRecord.data = data; + nodeRecord.precalculatedDigest = precalculateDigest(DigestOpCode.ADD, path, nodeRecord.data, s); + setTxnDigest(request, nodeRecord.precalculatedDigest); + addChangeRecord(nodeRecord); + } + + private void validatePath(String path, long sessionId) throws BadArgumentsException { + try { + PathUtils.validatePath(path); + } catch(IllegalArgumentException ie) { + LOG.info("Invalid path {} with session 0x{}, reason: {}", path, Long.toHexString(sessionId), ie.getMessage()); + throw new BadArgumentsException(path); + } + } + + private String getParentPathAndValidate(String path) throws BadArgumentsException { + int lastSlash = path.lastIndexOf('/'); + if(lastSlash == -1 || path.indexOf('\0') != -1 || zks.getZKDatabase().isSpecialPath(path)) { + throw new BadArgumentsException(path); + } + return path.substring(0, lastSlash); + } + + private static int checkAndIncVersion(int currentVersion, int expectedVersion, + String path) throws KeeperException.BadVersionException { + if(expectedVersion != -1 && expectedVersion != currentVersion) { + throw new KeeperException.BadVersionException(path); + } + return currentVersion + 1; + } + + /** + * // TODO_MA 马中华 注释: 单线程 + * This method will be called inside the ProcessRequestThread, which is a + * singleton, so there will be a single thread calling this code. + * + * @param request + */ + protected void pRequest(Request request) throws RequestProcessorException { + // LOG.info("Prep>>> cxid = " + request.cxid + " type = " + + // request.type + " id = 0x" + Long.toHexString(request.sessionId)); + request.setHdr(null); + request.setTxn(null); + + try { + switch(request.type) { + + // TODO_MA 注释: 事务请求 + // TODO_MA 注释: ZNode 节点有四种: 持久化, 临时节点,容器节点, TTL节点 + case OpCode.createContainer: + case OpCode.create: + case OpCode.create2: + CreateRequest create2Request = new CreateRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, create2Request, true); + break; + case OpCode.createTTL: + CreateTTLRequest createTtlRequest = new CreateTTLRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, createTtlRequest, true); + break; + case OpCode.deleteContainer: + case OpCode.delete: + DeleteRequest deleteRequest = new DeleteRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, deleteRequest, true); + break; + case OpCode.setData: + SetDataRequest setDataRequest = new SetDataRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, setDataRequest, true); + break; + case OpCode.reconfig: + ReconfigRequest reconfigRequest = new ReconfigRequest(); + ByteBufferInputStream.byteBuffer2Record(request.request, reconfigRequest); + pRequest2Txn(request.type, zks.getNextZxid(), request, reconfigRequest, true); + break; + case OpCode.setACL: + SetACLRequest setAclRequest = new SetACLRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, setAclRequest, true); + break; + case OpCode.check: + CheckVersionRequest checkRequest = new CheckVersionRequest(); + pRequest2Txn(request.type, zks.getNextZxid(), request, checkRequest, true); + break; + case OpCode.multi: + MultiOperationRecord multiRequest = new MultiOperationRecord(); + try { + ByteBufferInputStream.byteBuffer2Record(request.request, multiRequest); + } catch(IOException e) { + request.setHdr( + new TxnHeader(request.sessionId, request.cxid, zks.getNextZxid(), Time.currentWallTime(), + OpCode.multi)); + throw e; + } + List txns = new ArrayList(); + //Each op in a multi-op must have the same zxid! + long zxid = zks.getNextZxid(); + KeeperException ke = null; + + //Store off current pending change records in case we need to rollback + Map pendingChanges = getPendingChanges(multiRequest); + request.setHdr( + new TxnHeader(request.sessionId, request.cxid, zxid, Time.currentWallTime(), request.type)); + + for(Op op : multiRequest) { + Record subrequest = op.toRequestRecord(); + int type; + Record txn; + + /* If we've already failed one of the ops, don't bother + * trying the rest as we know it's going to fail and it + * would be confusing in the logfiles. + */ + if(ke != null) { + type = OpCode.error; + txn = new ErrorTxn(Code.RUNTIMEINCONSISTENCY.intValue()); + } else { + /* Prep the request and convert to a Txn */ + try { + pRequest2Txn(op.getType(), zxid, request, subrequest, false); + type = op.getType(); + txn = request.getTxn(); + } catch(KeeperException e) { + ke = e; + type = OpCode.error; + txn = new ErrorTxn(e.code().intValue()); + + if(e.code().intValue() > Code.APIERROR.intValue()) { + LOG.info( + "Got user-level KeeperException when processing {} aborting" + " remaining multi ops. Error Path:{} Error:{}", + request.toString(), e.getPath(), e.getMessage()); + } + + request.setException(e); + + /* Rollback change records from failed multi-op */ + rollbackPendingChanges(zxid, pendingChanges); + } + } + + // TODO: I don't want to have to serialize it here and then + // immediately deserialize in next processor. But I'm + // not sure how else to get the txn stored into our list. + try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + txn.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + txns.add(new Txn(type, bb.array())); + } + } + + request.setTxn(new MultiTxn(txns)); + if(digestEnabled) { + setTxnDigest(request); + } + + break; + + // create/close session don't require request record + case OpCode.createSession: + case OpCode.closeSession: + if(!request.isLocalSession()) { + pRequest2Txn(request.type, zks.getNextZxid(), request, null, true); + } + break; + + // TODO_MA 注释: 非事务请求,只是检查 session 做相关处理 + //All the rest don't need to create a Txn - just verify session + case OpCode.sync: + case OpCode.exists: + case OpCode.getData: + case OpCode.getACL: + case OpCode.getChildren: + case OpCode.getAllChildrenNumber: + case OpCode.getChildren2: + case OpCode.ping: + case OpCode.setWatches: + case OpCode.setWatches2: + case OpCode.checkWatches: + case OpCode.removeWatches: + case OpCode.getEphemerals: + case OpCode.multiRead: + case OpCode.addWatch: + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + break; + default: + LOG.warn("unknown type {}", request.type); + break; + } + } catch(KeeperException e) { + if(request.getHdr() != null) { + request.getHdr().setType(OpCode.error); + request.setTxn(new ErrorTxn(e.code().intValue())); + } + + if(e.code().intValue() > Code.APIERROR.intValue()) { + LOG.info("Got user-level KeeperException when processing {} Error Path:{} Error:{}", request.toString(), + e.getPath(), e.getMessage()); + } + request.setException(e); + } catch(Exception e) { + // log at error level as we are returning a marshalling + // error to the user + LOG.error("Failed to process {}", request, e); + + StringBuilder sb = new StringBuilder(); + ByteBuffer bb = request.request; + if(bb != null) { + bb.rewind(); + while(bb.hasRemaining()) { + sb.append(Integer.toHexString(bb.get() & 0xff)); + } + } else { + sb.append("request buffer is null"); + } + + LOG.error("Dumping request buffer: 0x{}", sb.toString()); + if(request.getHdr() != null) { + request.getHdr().setType(OpCode.error); + request.setTxn(new ErrorTxn(Code.MARSHALLINGERROR.intValue())); + } + } + request.zxid = zks.getZxid(); + ServerMetrics.getMetrics().PREP_PROCESS_TIME.add(Time.currentElapsedTime() - request.prepStartTime); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 调用下一个 RequestProcessor = ProposalRequestProcessor 继续处理 + */ + nextProcessor.processRequest(request); + } + + private static List removeDuplicates(final List acls) { + if(acls == null || acls.isEmpty()) { + return Collections.emptyList(); + } + + // This would be done better with a Set but ACL hashcode/equals do not + // allow for null values + final ArrayList retval = new ArrayList<>(acls.size()); + for(final ACL acl : acls) { + if(!retval.contains(acl)) { + retval.add(acl); + } + } + return retval; + } + + private void validateCreateRequest(String path, CreateMode createMode, Request request, + long ttl) throws KeeperException { + if(createMode.isTTL() && !EphemeralType.extendedEphemeralTypesEnabled()) { + throw new KeeperException.UnimplementedException(); + } + try { + EphemeralType.validateTTL(createMode, ttl); + } catch(IllegalArgumentException e) { + throw new BadArgumentsException(path); + } + if(createMode.isEphemeral()) { + // Exception is set when local session failed to upgrade + // so we just need to report the error + if(request.getException() != null) { + throw request.getException(); + } + zks.sessionTracker.checkGlobalSession(request.sessionId, request.getOwner()); + } else { + zks.sessionTracker.checkSession(request.sessionId, request.getOwner()); + } + } + + /** + * This method checks out the acl making sure it isn't null or empty, + * it has valid schemes and ids, and expanding any relative ids that + * depend on the requestor's authentication information. + * + * @param authInfo list of ACL IDs associated with the client connection + * @param acls list of ACLs being assigned to the node (create or setACL operation) + * @return verified and expanded ACLs + * @throws KeeperException.InvalidACLException + */ + public static List fixupACL(String path, List authInfo, + List acls) throws KeeperException.InvalidACLException { + // check for well formed ACLs + // This resolves https://issues.apache.org/jira/browse/ZOOKEEPER-1877 + List uniqacls = removeDuplicates(acls); + if(uniqacls == null || uniqacls.size() == 0) { + throw new KeeperException.InvalidACLException(path); + } + List rv = new ArrayList<>(); + for(ACL a : uniqacls) { + LOG.debug("Processing ACL: {}", a); + if(a == null) { + throw new KeeperException.InvalidACLException(path); + } + Id id = a.getId(); + if(id == null || id.getScheme() == null) { + throw new KeeperException.InvalidACLException(path); + } + if(id.getScheme().equals("world") && id.getId().equals("anyone")) { + rv.add(a); + } else if(id.getScheme().equals("auth")) { + // This is the "auth" id, so we have to expand it to the + // authenticated ids of the requestor + boolean authIdValid = false; + for(Id cid : authInfo) { + ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(cid.getScheme()); + if(ap == null) { + LOG.error("Missing AuthenticationProvider for {}", cid.getScheme()); + } else if(ap.isAuthenticated()) { + authIdValid = true; + rv.add(new ACL(a.getPerms(), cid)); + } + } + if(!authIdValid) { + throw new KeeperException.InvalidACLException(path); + } + } else { + ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(id.getScheme()); + if(ap == null || !ap.isValid(id.getId())) { + throw new KeeperException.InvalidACLException(path); + } + rv.add(a); + } + } + return rv; + } + + public void processRequest(Request request) { + request.prepQueueStartTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + submittedRequests.add(request); + ServerMetrics.getMetrics().PREP_PROCESSOR_QUEUED.add(1); + } + + public void shutdown() { + LOG.info("Shutting down"); + submittedRequests.clear(); + submittedRequests.add(Request.requestOfDeath); + nextProcessor.shutdown(); + } + + /** + * Calculate the node digest and tree digest after the change. + * + * @param type the type of operations about the digest change + * @param path the path of the node + * @param data the data of the node + * @param s the stat of the node + * @return PrecalculatedDigest the pair of node and tree digest + */ + private PrecalculatedDigest precalculateDigest(DigestOpCode type, String path, byte[] data, + StatPersisted s) throws KeeperException.NoNodeException { + + if(!digestEnabled) { + return null; + } + + long prevNodeDigest; + long newNodeDigest; + + switch(type) { + case ADD: + prevNodeDigest = 0; + newNodeDigest = digestCalculator.calculateDigest(path, data, s); + break; + case REMOVE: + prevNodeDigest = getRecordForPath(path).precalculatedDigest.nodeDigest; + newNodeDigest = 0; + break; + case UPDATE: + prevNodeDigest = getRecordForPath(path).precalculatedDigest.nodeDigest; + newNodeDigest = digestCalculator.calculateDigest(path, data, s); + break; + case NOOP: + newNodeDigest = prevNodeDigest = 0; + break; + default: + return null; + } + long treeDigest = getCurrentTreeDigest() - prevNodeDigest + newNodeDigest; + return new PrecalculatedDigest(newNodeDigest, treeDigest); + } + + private PrecalculatedDigest precalculateDigest(DigestOpCode type, + String path) throws KeeperException.NoNodeException { + return precalculateDigest(type, path, null, null); + } + + /** + * Query the current tree digest from DataTree or outstandingChanges list. + * + * @return current tree digest + */ + private long getCurrentTreeDigest() { + long digest; + synchronized(zks.outstandingChanges) { + if(zks.outstandingChanges.isEmpty()) { + digest = zks.getZKDatabase().getDataTree().getTreeDigest(); + LOG.debug("Digest got from data tree is: {}", digest); + } else { + digest = zks.outstandingChanges.peekLast().precalculatedDigest.treeDigest; + LOG.debug("Digest got from outstandingChanges is: {}", digest); + } + } + return digest; + } + + private void setTxnDigest(Request request) { + request.setTxnDigest(new TxnDigest(digestCalculator.getDigestVersion(), getCurrentTreeDigest())); + } + + private void setTxnDigest(Request request, PrecalculatedDigest preCalculatedDigest) { + if(preCalculatedDigest == null) { + return; + } + request.setTxnDigest(new TxnDigest(digestCalculator.getDigestVersion(), preCalculatedDigest.treeDigest)); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/PurgeTxnLog.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/PurgeTxnLog.java new file mode 100644 index 0000000..5846401 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/PurgeTxnLog.java @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.File; +import java.io.FileFilter; +import java.io.IOException; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * this class is used to clean up the + * snapshot and data log dir's. This is usually + * run as a cronjob on the zookeeper server machine. + * Invocation of this class will clean up the datalogdir + * files and snapdir files keeping the last "-n" snapshot files + * and the corresponding logs. + */ +@InterfaceAudience.Public +public class PurgeTxnLog { + + private static final Logger LOG = LoggerFactory.getLogger(PurgeTxnLog.class); + + private static final String COUNT_ERR_MSG = "count should be greater than or equal to 3"; + + static void printUsage() { + System.out.println("Usage:"); + System.out.println("PurgeTxnLog dataLogDir [snapDir] -n count"); + System.out.println("\tdataLogDir -- path to the txn log directory"); + System.out.println("\tsnapDir -- path to the snapshot directory"); + System.out.println("\tcount -- the number of old snaps/logs you want " + + "to keep, value should be greater than or equal to 3"); + } + + private static final String PREFIX_SNAPSHOT = "snapshot"; + private static final String PREFIX_LOG = "log"; + + /** + * Purges the snapshot and logs keeping the last num snapshots and the + * corresponding logs. If logs are rolling or a new snapshot is created + * during this process, these newest N snapshots or any data logs will be + * excluded from current purging cycle. + * + * @param dataDir the dir that has the logs + * @param snapDir the dir that has the snapshots + * @param num the number of snapshots to keep + * @throws IOException + */ + public static void purge(File dataDir, File snapDir, int num) throws IOException { + if (num < 3) { + throw new IllegalArgumentException(COUNT_ERR_MSG); + } + + FileTxnSnapLog txnLog = new FileTxnSnapLog(dataDir, snapDir); + + // TODO_MA 注释: + List snaps = txnLog.findNValidSnapshots(num); + int numSnaps = snaps.size(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: snaps.get(numSnaps - 1) 是获取最旧的一个快照文件! + */ + if (numSnaps > 0) { + purgeOlderSnapshots(txnLog, snaps.get(numSnaps - 1)); + } + } + + // VisibleForTesting + static void purgeOlderSnapshots(FileTxnSnapLog txnLog, File snapShot) { + + // TODO_MA 注释: + final long leastZxidToBeRetain = Util.getZxidFromName(snapShot.getName(), PREFIX_SNAPSHOT); + + /** + * We delete all files with a zxid in their name that is less than leastZxidToBeRetain. + * This rule applies to both snapshot files as well as log files, with the following + * exception for log files. + * + * A log file with zxid less than X may contain transactions with zxid larger than X. More + * precisely, a log file named log.(X-a) may contain transactions newer than snapshot.X if + * there are no other log files with starting zxid in the interval (X-a, X]. Assuming the + * latter condition is true, log.(X-a) must be retained to ensure that snapshot.X is + * recoverable. In fact, this log file may very well extend beyond snapshot.X to newer + * snapshot files if these newer snapshots were not accompanied by log rollover (possible in + * the learner state machine at the time of this writing). We can make more precise + * determination of whether log.(leastZxidToBeRetain-a) for the smallest 'a' is actually + * needed or not (e.g. not needed if there's a log file named log.(leastZxidToBeRetain+1)), + * but the complexity quickly adds up with gains only in uncommon scenarios. It's safe and + * simple to just preserve log.(leastZxidToBeRetain-a) for the smallest 'a' to ensure + * recoverability of all snapshots being retained. We determine that log file here by + * calling txnLog.getSnapshotLogs(). + */ + final Set retainedTxnLogs = new HashSet(); + retainedTxnLogs.addAll(Arrays.asList(txnLog.getSnapshotLogs(leastZxidToBeRetain))); + + /** + * Finds all candidates for deletion, which are files with a zxid in their name that is less + * than leastZxidToBeRetain. There's an exception to this rule, as noted above. + */ + class MyFileFilter implements FileFilter { + + private final String prefix; + MyFileFilter(String prefix) { + this.prefix = prefix; + } + public boolean accept(File f) { + if (!f.getName().startsWith(prefix + ".")) { + return false; + } + if (retainedTxnLogs.contains(f)) { + return false; + } + long fZxid = Util.getZxidFromName(f.getName(), prefix); + + // TODO_MA 注释: + return fZxid < leastZxidToBeRetain; + } + + } + + // TODO_MA 注释: + // add all non-excluded log files + File[] logs = txnLog.getDataDir().listFiles(new MyFileFilter(PREFIX_LOG)); + List files = new ArrayList<>(); + if (logs != null) { + files.addAll(Arrays.asList(logs)); + } + + // TODO_MA 注释: + // add all non-excluded snapshot files to the deletion list + File[] snapshots = txnLog.getSnapDir().listFiles(new MyFileFilter(PREFIX_SNAPSHOT)); + if (snapshots != null) { + files.addAll(Arrays.asList(snapshots)); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + // remove the old files + for (File f : files) { + final String msg = String.format( + "Removing file: %s\t%s", + DateFormat.getDateTimeInstance().format(f.lastModified()), + f.getPath()); + + LOG.info(msg); + System.out.println(msg); + + if (!f.delete()) { + System.err.println("Failed to remove " + f.getPath()); + } + } + + } + + /** + * @param args dataLogDir [snapDir] -n count + * dataLogDir -- path to the txn log directory + * snapDir -- path to the snapshot directory + * count -- the number of old snaps/logs you want to keep, value should be greater than or equal to 3
      + */ + public static void main(String[] args) throws IOException { + if (args.length < 3 || args.length > 4) { + printUsageThenExit(); + } + File dataDir = validateAndGetFile(args[0]); + File snapDir = dataDir; + int num = -1; + String countOption = ""; + if (args.length == 3) { + countOption = args[1]; + num = validateAndGetCount(args[2]); + } else { + snapDir = validateAndGetFile(args[1]); + countOption = args[2]; + num = validateAndGetCount(args[3]); + } + if (!"-n".equals(countOption)) { + printUsageThenExit(); + } + purge(dataDir, snapDir, num); + } + + /** + * validates file existence and returns the file + * + * @param path + * @return File + */ + private static File validateAndGetFile(String path) { + File file = new File(path); + if (!file.exists()) { + System.err.println("Path '" + file.getAbsolutePath() + "' does not exist. "); + printUsageThenExit(); + } + return file; + } + + /** + * Returns integer if parsed successfully and it is valid otherwise prints + * error and usage and then exits + * + * @param number + * @return count + */ + private static int validateAndGetCount(String number) { + int result = 0; + try { + result = Integer.parseInt(number); + if (result < 3) { + System.err.println(COUNT_ERR_MSG); + printUsageThenExit(); + } + } catch (NumberFormatException e) { + System.err.println("'" + number + "' can not be parsed to integer."); + printUsageThenExit(); + } + return result; + } + + private static void printUsageThenExit() { + printUsage(); + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/RateLogger.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/RateLogger.java new file mode 100644 index 0000000..1cae448 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/RateLogger.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; + +/** + * This logs the message once in the beginning and once every LOG_INTERVAL. + */ +public class RateLogger { + + private final long LOG_INTERVAL; // Duration is in ms + + public RateLogger(Logger log) { + this(log, 100); + } + + public RateLogger(Logger log, long interval) { + LOG = log; + LOG_INTERVAL = interval; + } + + private final Logger LOG; + private String msg = null; + private long timestamp; + private int count = 0; + private String value = null; + + public void flush() { + if (msg != null && count > 0) { + String log = ""; + if (count > 1) { + log = "[" + count + " times] "; + } + log += "Message: " + msg; + if (value != null) { + log += " Last value:" + value; + } + LOG.warn(log); + } + msg = null; + value = null; + count = 0; + } + + public void rateLimitLog(String newMsg) { + rateLimitLog(newMsg, null); + } + + /** + * In addition to the message, it also takes a value. + */ + public void rateLimitLog(String newMsg, String value) { + long now = Time.currentElapsedTime(); + if (newMsg.equals(msg)) { + ++count; + this.value = value; + if (now - timestamp >= LOG_INTERVAL) { + flush(); + msg = newMsg; + timestamp = now; + this.value = value; + } + } else { + flush(); + msg = newMsg; + this.value = value; + timestamp = now; + LOG.warn("Message:{} Value:{}", msg, value); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ReferenceCountedACLCache.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ReferenceCountedACLCache.java new file mode 100644 index 0000000..307a53d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ReferenceCountedACLCache.java @@ -0,0 +1,248 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.jute.Index; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReferenceCountedACLCache { + + private static final Logger LOG = LoggerFactory.getLogger(ReferenceCountedACLCache.class); + + final Map> longKeyMap = new HashMap>(); + + final Map, Long> aclKeyMap = new HashMap, Long>(); + + final Map referenceCounter = new HashMap(); + private static final long OPEN_UNSAFE_ACL_ID = -1L; + + /** + * these are the number of acls that we have in the datatree + */ + long aclIndex = 0; + + /** + * converts the list of acls to a long. + * Increments the reference counter for this ACL. + * @param acls + * @return a long that map to the acls + */ + public synchronized Long convertAcls(List acls) { + if (acls == null) { + return OPEN_UNSAFE_ACL_ID; + } + + // get the value from the map + Long ret = aclKeyMap.get(acls); + if (ret == null) { + ret = incrementIndex(); + longKeyMap.put(ret, acls); + aclKeyMap.put(acls, ret); + } + + addUsage(ret); + + return ret; + } + + /** + * converts a long to a list of acls. + * + * @param longVal + * @return a list of ACLs that map to the long + */ + public synchronized List convertLong(Long longVal) { + if (longVal == null) { + return null; + } + if (longVal == OPEN_UNSAFE_ACL_ID) { + return ZooDefs.Ids.OPEN_ACL_UNSAFE; + } + List acls = longKeyMap.get(longVal); + if (acls == null) { + LOG.error("ERROR: ACL not available for long {}", longVal); + throw new RuntimeException("Failed to fetch acls for " + longVal); + } + return acls; + } + + private long incrementIndex() { + return ++aclIndex; + } + + public void deserialize(InputArchive ia) throws IOException { + clear(); + int i = ia.readInt("map"); + + LinkedHashMap> deserializedMap = new LinkedHashMap<>(); + // keep read operations out of synchronization block + while (i > 0) { + Long val = ia.readLong("long"); + List aclList = new ArrayList(); + Index j = ia.startVector("acls"); + if (j == null) { + throw new RuntimeException("Incorrent format of InputArchive when deserialize DataTree - missing acls"); + } + while (!j.done()) { + ACL acl = new ACL(); + acl.deserialize(ia, "acl"); + aclList.add(acl); + j.incr(); + } + + deserializedMap.put(val, aclList); + i--; + } + + synchronized (this) { + for (Map.Entry> entry : deserializedMap.entrySet()) { + Long val = entry.getKey(); + List aclList = entry.getValue(); + if (aclIndex < val) { + aclIndex = val; + } + + longKeyMap.put(val, aclList); + aclKeyMap.put(aclList, val); + referenceCounter.put(val, new AtomicLongWithEquals(0)); + } + } + } + + public void serialize(OutputArchive oa) throws IOException { + Map> clonedLongKeyMap; + synchronized (this) { + clonedLongKeyMap = new HashMap<>(longKeyMap); + } + oa.writeInt(clonedLongKeyMap.size(), "map"); + for (Map.Entry> val : clonedLongKeyMap.entrySet()) { + oa.writeLong(val.getKey(), "long"); + List aclList = val.getValue(); + oa.startVector(aclList, "acls"); + for (ACL acl : aclList) { + acl.serialize(oa, "acl"); + } + oa.endVector(aclList, "acls"); + } + } + + public int size() { + return aclKeyMap.size(); + } + + private void clear() { + aclKeyMap.clear(); + longKeyMap.clear(); + referenceCounter.clear(); + } + + public synchronized void addUsage(Long acl) { + if (acl == OPEN_UNSAFE_ACL_ID) { + return; + } + + if (!longKeyMap.containsKey(acl)) { + LOG.info("Ignoring acl {} as it does not exist in the cache", acl); + return; + } + + AtomicLong count = referenceCounter.get(acl); + if (count == null) { + referenceCounter.put(acl, new AtomicLongWithEquals(1)); + } else { + count.incrementAndGet(); + } + } + + public synchronized void removeUsage(Long acl) { + if (acl == OPEN_UNSAFE_ACL_ID) { + return; + } + + if (!longKeyMap.containsKey(acl)) { + LOG.info("Ignoring acl {} as it does not exist in the cache", acl); + return; + } + + long newCount = referenceCounter.get(acl).decrementAndGet(); + if (newCount <= 0) { + referenceCounter.remove(acl); + aclKeyMap.remove(longKeyMap.get(acl)); + longKeyMap.remove(acl); + } + } + + public synchronized void purgeUnused() { + Iterator> refCountIter = referenceCounter.entrySet().iterator(); + while (refCountIter.hasNext()) { + Map.Entry entry = refCountIter.next(); + if (entry.getValue().get() <= 0) { + Long acl = entry.getKey(); + aclKeyMap.remove(longKeyMap.get(acl)); + longKeyMap.remove(acl); + refCountIter.remove(); + } + } + } + + private static class AtomicLongWithEquals extends AtomicLong { + + private static final long serialVersionUID = 3355155896813725462L; + + public AtomicLongWithEquals(long i) { + super(i); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + return equals((AtomicLongWithEquals) o); + } + + public boolean equals(AtomicLongWithEquals that) { + return get() == that.get(); + } + + @Override + public int hashCode() { + return 31 * Long.valueOf(get()).hashCode(); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/Request.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/Request.java new file mode 100644 index 0000000..e386c9d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/Request.java @@ -0,0 +1,472 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + *uuuuu + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "/RequuuAS 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 org.apache.zookeeper.server; + +import java.nio.ByteBuffer; +import java.util.List; + +import org.apache.jute.Record; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.metrics.Summary; +import org.apache.zookeeper.metrics.SummarySet; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.AuthUtil; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * This is the structure that represents a request moving through a chain of + * RequestProcessors. There are various pieces of information that is tacked + * onto the request as it is processed. + */ +public class Request { + + // TODO_MA 马中华 注释: + public static final Request requestOfDeath = new Request(null, 0, 0, 0, null, null); + + // Considers a request stale if the request's connection has closed. Enabled by default. + private static volatile boolean staleConnectionCheck = Boolean + .parseBoolean(System.getProperty("zookeeper.request_stale_connection_check", "true")); + + // Considers a request stale if the request latency is higher than its + // associated session timeout. Disabled by default. + private static volatile boolean staleLatencyCheck = Boolean + .parseBoolean(System.getProperty("zookeeper.request_stale_latency_check", "false")); + + public Request(ServerCnxn cnxn, long sessionId, int xid, int type, ByteBuffer bb, List authInfo) { + this.cnxn = cnxn; + this.sessionId = sessionId; + this.cxid = xid; + this.type = type; + this.request = bb; + this.authInfo = authInfo; + } + + public Request(long sessionId, int xid, int type, TxnHeader hdr, Record txn, long zxid) { + this.sessionId = sessionId; + this.cxid = xid; + this.type = type; + this.hdr = hdr; + this.txn = txn; + this.zxid = zxid; + this.request = null; + this.cnxn = null; + this.authInfo = null; + } + + public final long sessionId; + + public final int cxid; + + public final int type; + + public final ByteBuffer request; + + public final ServerCnxn cnxn; + + private TxnHeader hdr; + + private Record txn; + + public long zxid = -1; + + public final List authInfo; + + public final long createTime = Time.currentElapsedTime(); + + public long prepQueueStartTime = -1; + + public long prepStartTime = -1; + + public long commitProcQueueStartTime = -1; + + public long commitRecvTime = -1; + + public long syncQueueStartTime; + + private Object owner; + + private KeeperException e; + + public QuorumVerifier qv = null; + + private TxnDigest txnDigest; + + /** + * If this is a create or close request for a local-only session. + */ + private boolean isLocalSession = false; + + private int largeRequestSize = -1; + + public boolean isLocalSession() { + return isLocalSession; + } + + public void setLocalSession(boolean isLocalSession) { + this.isLocalSession = isLocalSession; + } + + public void setLargeRequestSize(int size) { + largeRequestSize = size; + } + + public int getLargeRequestSize() { + return largeRequestSize; + } + + public Object getOwner() { + return owner; + } + + public void setOwner(Object owner) { + this.owner = owner; + } + + public TxnHeader getHdr() { + return hdr; + } + + public void setHdr(TxnHeader hdr) { + this.hdr = hdr; + } + + public Record getTxn() { + return txn; + } + + public void setTxn(Record txn) { + this.txn = txn; + } + + public ServerCnxn getConnection() { + return cnxn; + } + + public static boolean getStaleLatencyCheck() { + return staleLatencyCheck; + } + + public static void setStaleLatencyCheck(boolean check) { + staleLatencyCheck = check; + } + + public static boolean getStaleConnectionCheck() { + return staleConnectionCheck; + } + + public static void setStaleConnectionCheck(boolean check) { + staleConnectionCheck = check; + } + + public boolean isStale() { + if(cnxn == null) { + return false; + } + + // closeSession requests should be able to outlive the session in order + // to clean-up state. + if(type == OpCode.closeSession) { + return false; + } + + if(staleConnectionCheck) { + // If the connection is closed, consider the request stale. + if(cnxn.isStale() || cnxn.isInvalid()) { + return true; + } + } + + if(staleLatencyCheck) { + // If the request latency is higher than session timeout, consider + // the request stale. + long currentTime = Time.currentElapsedTime(); + return (currentTime - createTime) > cnxn.getSessionTimeout(); + } + + return false; + } + + /** + * A prior request was dropped on this request's connection and + * therefore this request must also be dropped to ensure correct + * ordering semantics. + */ + public boolean mustDrop() { + return ((cnxn != null) && cnxn.isInvalid()); + } + + /** + * is the packet type a valid packet in zookeeper + * + * @param type the type of the packet + * @return true if a valid packet, false if not + */ + static boolean isValid(int type) { + // make sure this is always synchronized with Zoodefs!! + switch(type) { + case OpCode.notification: + return false; + case OpCode.check: + case OpCode.closeSession: + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + case OpCode.createSession: + case OpCode.delete: + case OpCode.deleteContainer: + case OpCode.exists: + case OpCode.getACL: + case OpCode.getChildren: + case OpCode.getAllChildrenNumber: + case OpCode.getChildren2: + case OpCode.getData: + case OpCode.getEphemerals: + case OpCode.multi: + case OpCode.multiRead: + case OpCode.ping: + case OpCode.reconfig: + case OpCode.setACL: + case OpCode.setData: + case OpCode.setWatches: + case OpCode.setWatches2: + case OpCode.sync: + case OpCode.checkWatches: + case OpCode.removeWatches: + case OpCode.addWatch: + return true; + default: + return false; + } + } + + public boolean isQuorum() { + switch(this.type) { + case OpCode.exists: + case OpCode.getACL: + case OpCode.getChildren: + case OpCode.getAllChildrenNumber: + case OpCode.getChildren2: + case OpCode.getData: + case OpCode.getEphemerals: + case OpCode.multiRead: + return false; + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + case OpCode.error: + case OpCode.delete: + case OpCode.deleteContainer: + case OpCode.setACL: + case OpCode.setData: + case OpCode.check: + case OpCode.multi: + case OpCode.reconfig: + return true; + case OpCode.closeSession: + case OpCode.createSession: + return !this.isLocalSession; + default: + return false; + } + } + + public static String op2String(int op) { + switch(op) { + case OpCode.notification: + return "notification"; + case OpCode.create: + return "create"; + case OpCode.delete: + return "delete"; + case OpCode.exists: + return "exists"; + case OpCode.getData: + return "getData"; + case OpCode.setData: + return "setData"; + case OpCode.getACL: + return "getACL"; + case OpCode.setACL: + return "setACL"; + case OpCode.getChildren: + return "getChildren"; + case OpCode.sync: + return "sync"; + case OpCode.ping: + return "ping"; + case OpCode.getChildren2: + return "getChildren2"; + case OpCode.check: + return "check"; + case OpCode.multi: + return "multi"; + case OpCode.create2: + return "create2"; + case OpCode.reconfig: + return "reconfig"; + case OpCode.checkWatches: + return "checkWatches"; + case OpCode.removeWatches: + return "removeWatches"; + case OpCode.createContainer: + return "createContainer"; + case OpCode.deleteContainer: + return "deleteContainer"; + case OpCode.createTTL: + return "createTtl"; + case OpCode.multiRead: + return "multiRead"; + case OpCode.auth: + return "auth"; + case OpCode.setWatches: + return "setWatches"; + case OpCode.setWatches2: + return "setWatches2"; + case OpCode.sasl: + return "sasl"; + case OpCode.getEphemerals: + return "getEphemerals"; + case OpCode.getAllChildrenNumber: + return "getAllChildrenNumber"; + case OpCode.createSession: + return "createSession"; + case OpCode.closeSession: + return "closeSession"; + case OpCode.error: + return "error"; + default: + return "unknown " + op; + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("sessionid:0x").append(Long.toHexString(sessionId)).append(" type:").append(op2String(type)) + .append(" cxid:0x").append(Long.toHexString(cxid)).append(" zxid:0x") + .append(Long.toHexString(hdr == null ? -2 : hdr.getZxid())).append(" txntype:") + .append(hdr == null ? "unknown" : "" + hdr.getType()); + + // best effort to print the path assoc with this request + String path = "n/a"; + if(type != OpCode.createSession && type != OpCode.setWatches && type != OpCode.setWatches2 && type != OpCode.closeSession && request != null && request + .remaining() >= 4) { + try { + // make sure we don't mess with request itself + ByteBuffer rbuf = request.asReadOnlyBuffer(); + rbuf.clear(); + int pathLen = rbuf.getInt(); + // sanity check + if(pathLen >= 0 && pathLen < 4096 && rbuf.remaining() >= pathLen) { + byte[] b = new byte[pathLen]; + rbuf.get(b); + path = new String(b); + } + } catch(Exception e) { + // ignore - can't find the path, will output "n/a" instead + } + } + sb.append(" reqpath:").append(path); + + return sb.toString(); + } + + public void setException(KeeperException e) { + this.e = e; + } + + public KeeperException getException() { + return e; + } + + public void logLatency(Summary metric) { + logLatency(metric, Time.currentWallTime()); + } + + public void logLatency(Summary metric, long currentTime) { + if(hdr != null) { + /* Request header is created by leader. If there is clock drift + * latency might be negative. Headers use wall time, not + * CLOCK_MONOTONIC. + */ + long latency = currentTime - hdr.getTime(); + if(latency >= 0) { + metric.add(latency); + } + } + } + + public void logLatency(SummarySet metric, String key, long currentTime) { + if(hdr != null) { + /* Request header is created by leader. If there is clock drift + * latency might be negative. Headers use wall time, not + * CLOCK_MONOTONIC. + */ + long latency = currentTime - hdr.getTime(); + if(latency >= 0) { + metric.add(key, latency); + } + } + } + + public void logLatency(SummarySet metric, String key) { + logLatency(metric, key, Time.currentWallTime()); + } + + /** + * Returns comma separated list of users authenticated in the current + * session + */ + public String getUsers() { + if(authInfo == null) { + return (String) null; + } + if(authInfo.size() == 1) { + return AuthUtil.getUser(authInfo.get(0)); + } + StringBuilder users = new StringBuilder(); + boolean first = true; + for(Id id : authInfo) { + String user = AuthUtil.getUser(id); + if(user != null) { + if(first) { + first = false; + } else { + users.append(","); + } + users.append(user); + } + } + return users.toString(); + } + + public TxnDigest getTxnDigest() { + return txnDigest; + } + + public void setTxnDigest(TxnDigest txnDigest) { + this.txnDigest = txnDigest; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/RequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/RequestProcessor.java new file mode 100644 index 0000000..a1af3d1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/RequestProcessor.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * RequestProcessors are chained together to process transactions. Requests are + * always processed in order. The standalone server, follower, and leader all + * have slightly different RequestProcessors chained together. + * + * Requests always move forward through the chain of RequestProcessors. Requests + * are passed to a RequestProcessor through processRequest(). Generally method + * will always be invoked by a single thread. + * + * When shutdown is called, the request RequestProcessor should also shutdown + * any RequestProcessors that it is connected to. + */ +public interface RequestProcessor { + + @SuppressWarnings("serial") + class RequestProcessorException extends Exception { + + public RequestProcessorException(String msg, Throwable t) { + super(msg, t); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 用来处理 Request + */ + void processRequest(Request request) throws RequestProcessorException; + + void shutdown(); +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/RequestThrottler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/RequestThrottler.java new file mode 100644 index 0000000..a459a7f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/RequestThrottler.java @@ -0,0 +1,290 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * When enabled, the RequestThrottler limits the number of outstanding requests + * currently submitted to the request processor pipeline. The throttler augments + * the limit imposed by the globalOutstandingLimit that is enforced + * by the connection layer ({@link NIOServerCnxn}, {@link NettyServerCnxn}). + * + * The connection layer limit applies backpressure against the TCP connection by + * disabling selection on connections once the request limit is reached. However, + * the connection layer always allows a connection to send at least one request + * before disabling selection on that connection. Thus, in a scenario with 40000 + * client connections, the total number of requests inflight may be as high as + * 40000 even if the globalOustandingLimit was set lower. + * + * The RequestThrottler addresses this issue by adding additional queueing. When + * enabled, client connections no longer submit requests directly to the request + * processor pipeline but instead to the RequestThrottler. The RequestThrottler + * is then responsible for issuing requests to the request processors, and + * enforces a separate maxRequests limit. If the total number of + * outstanding requests is higher than maxRequests, the throttler + * will continually stall for stallTime milliseconds until + * underlimit. + * + * The RequestThrottler can also optionally drop stale requests rather than + * submit them to the processor pipeline. A stale request is a request sent + * by a connection that is already closed, and/or a request whose latency + * will end up being higher than its associated session timeout. The notion + * of staleness is configurable, @see Request for more details. + * + * To ensure ordering guarantees, if a request is ever dropped from a connection + * that connection is closed and flagged as invalid. All subsequent requests + * inflight from that connection are then dropped as well. + */ +public class RequestThrottler extends ZooKeeperCriticalThread { + + private static final Logger LOG = LoggerFactory.getLogger(RequestThrottler.class); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 提交的客户端请求,暂存在这儿 + */ + private final LinkedBlockingQueue submittedRequests = new LinkedBlockingQueue(); + + private final ZooKeeperServer zks; + private volatile boolean stopping; + private volatile boolean killed; + + private static final String SHUTDOWN_TIMEOUT = "zookeeper.request_throttler.shutdownTimeout"; + private static int shutdownTimeout = 10000; + + static { + shutdownTimeout = Integer.getInteger(SHUTDOWN_TIMEOUT, 10000); + LOG.info("{} = {}", SHUTDOWN_TIMEOUT, shutdownTimeout); + } + + /** + * The total number of outstanding requests allowed before the throttler + * starts stalling. + * + * When maxRequests = 0, throttling is disabled. + */ + private static volatile int maxRequests = Integer.getInteger("zookeeper.request_throttle_max_requests", 0); + + /** + * The time (in milliseconds) this is the maximum time for which throttler + * thread may wait to be notified that it may proceed processing a request. + */ + private static volatile int stallTime = Integer.getInteger("zookeeper.request_throttle_stall_time", 100); + + /** + * // TODO_MA 注释: 如果为true,则调节器将丢弃陈旧的请求,而不是将其发布到请求管道。 + * When true, the throttler will drop stale requests rather than issue them to the request pipeline. + * + * // TODO_MA 注释: 过时的请求是由现在关闭的连接发送的请求,和/或请求的延迟将比sessionTimeout高的请求。 + * A stale request is a request sent by a connection that is now closed, and/or a request that will have a request + * latency higher than the sessionTimeout. + * + * // TODO_MA 注释: 请求的陈旧性是可调属性,有关详细信息,请参见Request + * The staleness of a request is tunable property, @see Request for details. + */ + private static volatile boolean dropStaleRequests = Boolean.parseBoolean(System.getProperty("zookeeper.request_throttle_drop_stale", "true")); + + public RequestThrottler(ZooKeeperServer zks) { + super("RequestThrottler", zks.getZooKeeperServerListener()); + this.zks = zks; + this.stopping = false; + this.killed = false; + } + + public static int getMaxRequests() { + return maxRequests; + } + + public static void setMaxRequests(int requests) { + maxRequests = requests; + } + + public static int getStallTime() { + return stallTime; + } + + public static void setStallTime(int time) { + stallTime = time; + } + + public static boolean getDropStaleRequests() { + return dropStaleRequests; + } + + public static void setDropStaleRequests(boolean drop) { + dropStaleRequests = drop; + } + + @Override + public void run() { + try { + while (true) { + if (killed) { + break; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 获取请求 + */ + Request request = submittedRequests.take(); + if (Request.requestOfDeath == request) { + break; + } + + if (request.mustDrop()) { + continue; + } + + // TODO_MA 注释: 限流处理 + // Throttling is disabled when maxRequests = 0 + if (maxRequests > 0) { + while (!killed) { + if (dropStaleRequests && request.isStale()) { + // Note: this will close the connection + dropRequest(request); + ServerMetrics.getMetrics().STALE_REQUESTS_DROPPED.add(1); + request = null; + break; + } + if (zks.getInProcess() < maxRequests) { + break; + } + + // TODO_MA 注释: 等待一段时间,默认 0.1s + throttleSleep(stallTime); + } + } + + if (killed) { + break; + } + + // A dropped stale request will be null + if (request != null) { + if (request.isStale()) { + ServerMetrics.getMetrics().STALE_REQUESTS.add(1); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 立即提交 + */ + zks.submitRequestNow(request); + } + } + } catch (InterruptedException e) { + LOG.error("Unexpected interruption", e); + } + int dropped = drainQueue(); + LOG.info("RequestThrottler shutdown. Dropped {} requests", dropped); + } + + private synchronized void throttleSleep(int stallTime) { + try { + ServerMetrics.getMetrics().REQUEST_THROTTLE_WAIT_COUNT.add(1); + this.wait(stallTime); + } catch (InterruptedException ie) { + return; + } + } + + @SuppressFBWarnings(value = "NN_NAKED_NOTIFY", justification = "state change is in ZooKeeperServer.decInProgress() ") + public synchronized void throttleWake() { + this.notify(); + } + + private int drainQueue() { + // If the throttler shutdown gracefully, the queue will be empty. + // However, if the shutdown time limit was reached and the throttler + // was killed, we have no other option than to drop all remaining + // requests on the floor. + int dropped = 0; + Request request; + LOG.info("Draining request throttler queue"); + while ((request = submittedRequests.poll()) != null) { + dropped += 1; + dropRequest(request); + } + return dropped; + } + + private void dropRequest(Request request) { + // Since we're dropping a request on the floor, we must mark the + // connection as invalid to ensure any future requests from this + // connection are also dropped in order to ensure ordering + // semantics. + ServerCnxn conn = request.getConnection(); + if (conn != null) { + // Note: this will close the connection + conn.setInvalid(); + } + // Notify ZooKeeperServer that the request has finished so that it can + // update any request accounting/throttling limits. + zks.requestFinished(request); + } + + public void submitRequest(Request request) { + + // TODO_MA 注释: 如果服务器正在关闭,则丢弃这个请求 + if (stopping) { + LOG.debug("Shutdown in progress. Request cannot be processed"); + dropRequest(request); + } + + // TODO_MA 注释: 正常处理 + else { + submittedRequests.add(request); + } + } + + public int getInflight() { + return submittedRequests.size(); + } + + @SuppressFBWarnings("DM_EXIT") + public void shutdown() { + // Try to shutdown gracefully + LOG.info("Shutting down"); + stopping = true; + submittedRequests.add(Request.requestOfDeath); + try { + this.join(shutdownTimeout); + } catch (InterruptedException e) { + LOG.warn("Interrupted while waiting for {} to finish", this); + } + + // Forcibly shutdown if necessary in order to ensure request + // queue is drained. + killed = true; + try { + this.join(); + } catch (InterruptedException e) { + LOG.warn("Interrupted while waiting for {} to finish", this); + //TODO apply ZOOKEEPER-575 and remove this line. + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ResponseCache.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ResponseCache.java new file mode 100644 index 0000000..4a76a0f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ResponseCache.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import org.apache.zookeeper.data.Stat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@SuppressWarnings("serial") +public class ResponseCache { + private static final Logger LOG = LoggerFactory.getLogger(ResponseCache.class); + + // Magic number chosen to be "big enough but not too big" + public static final int DEFAULT_RESPONSE_CACHE_SIZE = 400; + private final int cacheSize; + private static class Entry { + public Stat stat; + public byte[] data; + } + + private final Map cache; + + public ResponseCache(int cacheSize) { + this.cacheSize = cacheSize; + cache = Collections.synchronizedMap(new LRUCache<>(cacheSize)); + LOG.info("Response cache size is initialized with value {}.", cacheSize); + } + + public int getCacheSize() { + return cacheSize; + } + + public void put(String path, byte[] data, Stat stat) { + Entry entry = new Entry(); + entry.data = data; + entry.stat = stat; + cache.put(path, entry); + } + + public byte[] get(String key, Stat stat) { + Entry entry = cache.get(key); + if (entry == null) { + return null; + } + if (!stat.equals(entry.stat)) { + // The node has been modified, invalidate cache. + cache.remove(key); + return null; + } else { + return entry.data; + } + } + + public boolean isEnabled() { + return cacheSize > 0; + } + + private static class LRUCache extends LinkedHashMap { + + private int cacheSize; + + LRUCache(int cacheSize) { + super(cacheSize / 4); + this.cacheSize = cacheSize; + } + + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() >= cacheSize; + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxn.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxn.java new file mode 100644 index 0000000..035a576 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxn.java @@ -0,0 +1,643 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.security.cert.Certificate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.metrics.Counter; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Interface to a Server connection - represents a connection from a client + * to the server. + */ +public abstract class ServerCnxn implements Stats, Watcher { + + // This is just an arbitrary object to represent requests issued by + // (aka owned by) this class + public static final Object me = new Object(); + private static final Logger LOG = LoggerFactory.getLogger(ServerCnxn.class); + + private Set authInfo = Collections.newSetFromMap(new ConcurrentHashMap()); + + private static final byte[] fourBytes = new byte[4]; + + /** + * If the client is of old version, we don't send r-o mode info to it. + * The reason is that if we would, old C client doesn't read it, which + * results in TCP RST packet, i.e. "connection reset by peer". + */ + boolean isOldClient = true; + + AtomicLong outstandingCount = new AtomicLong(); + + /** + * The ZooKeeperServer for this connection. May be null if the server + * is not currently serving requests (for example if the server is not + * an active quorum participant. + */ + final ZooKeeperServer zkServer; + + public enum DisconnectReason { + UNKNOWN("unknown"), + SERVER_SHUTDOWN("server_shutdown"), + CLOSE_ALL_CONNECTIONS_FORCED("close_all_connections_forced"), + CONNECTION_CLOSE_FORCED("connection_close_forced"), + CONNECTION_EXPIRED("connection_expired"), + CLIENT_CLOSED_CONNECTION("client_closed_connection"), + CLIENT_CLOSED_SESSION("client_closed_session"), + UNABLE_TO_READ_FROM_CLIENT("unable_to_read_from_client"), + NOT_READ_ONLY_CLIENT("not_read_only_client"), + CLIENT_ZXID_AHEAD("client_zxid_ahead"), + INFO_PROBE("info_probe"), + CLIENT_RECONNECT("client_reconnect"), + CANCELLED_KEY_EXCEPTION("cancelled_key_exception"), + IO_EXCEPTION("io_exception"), + IO_EXCEPTION_IN_SESSION_INIT("io_exception_in_session_init"), + BUFFER_UNDERFLOW_EXCEPTION("buffer_underflow_exception"), + SASL_AUTH_FAILURE("sasl_auth_failure"), + RESET_COMMAND("reset_command"), + CLOSE_CONNECTION_COMMAND("close_connection_command"), + CLEAN_UP("clean_up"), + CONNECTION_MODE_CHANGED("connection_mode_changed"), + // Below reasons are NettyServerCnxnFactory only + CHANNEL_DISCONNECTED("channel disconnected"), + CHANNEL_CLOSED_EXCEPTION("channel_closed_exception"), + AUTH_PROVIDER_NOT_FOUND("auth provider not found"), + FAILED_HANDSHAKE("Unsuccessful handshake"), + CLIENT_RATE_LIMIT("Client hits rate limiting threshold"), + CLIENT_CNX_LIMIT("Client hits connection limiting threshold"); + + String disconnectReason; + + DisconnectReason(String reason) { + this.disconnectReason = reason; + } + + public String toDisconnectReasonString() { + return disconnectReason; + } + } + + public ServerCnxn(final ZooKeeperServer zkServer) { + this.zkServer = zkServer; + } + + /** + * Flag that indicates that this connection is known to be closed/closing + * and from which we can optionally ignore outstanding requests as part + * of request throttling. This flag may be false when a connection is + * actually closed (false negative), but should never be true with + * a connection is still alive (false positive). + */ + private volatile boolean stale = false; + + /** + * Flag that indicates that a request for this connection was previously + * dropped as part of request throttling and therefore all future requests + * must also be dropped to ensure ordering guarantees. + */ + private volatile boolean invalid = false; + + abstract int getSessionTimeout(); + + public void incrOutstandingAndCheckThrottle(RequestHeader h) { + if(h.getXid() <= 0) { + return; + } + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 判断是否需要进行限流 + */ + if(zkServer.shouldThrottle(outstandingCount.incrementAndGet())) { + disableRecv(false); + } + } + + // will be called from zkServer.processPacket + public void decrOutstandingAndCheckThrottle(ReplyHeader h) { + if(h.getXid() <= 0) { + return; + } + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + if(!zkServer.shouldThrottle(outstandingCount.decrementAndGet())) { + enableRecv(); + } + } + + public abstract void close(DisconnectReason reason); + + /** + * Serializes a ZooKeeper response and enqueues it for sending. + * + * Serializes client response parts and enqueues them into outgoing queue. + * + * If both cache key and last modified zxid are provided, the serialized + * response is caсhed under the provided key, the last modified zxid is + * stored along with the value. A cache entry is invalidated if the + * provided last modified zxid is more recent than the stored one. + * + * Attention: this function is not thread safe, due to caching not being + * thread safe. + * + * @param h reply header + * @param r reply payload, can be null + * @param tag Jute serialization tag, can be null + * @param cacheKey Key for caching the serialized payload. A null value prevents caching. + * @param stat Stat information for the the reply payload, used for cache invalidation. + * A value of 0 prevents caching. + * @param opCode The op code appertains to the corresponding request of the response, + * used to decide which cache (e.g. read response cache, + * list of children response cache, ...) object to look up to when applicable. + */ + public abstract void sendResponse(ReplyHeader h, Record r, String tag, String cacheKey, Stat stat, + int opCode) throws IOException; + + public void sendResponse(ReplyHeader h, Record r, String tag) throws IOException { + sendResponse(h, r, tag, null, null, -1); + } + + protected byte[] serializeRecord(Record record) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(ZooKeeperServer.intBufferStartingSizeBytes); + BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos); + bos.writeRecord(record, null); + return baos.toByteArray(); + } + + protected ByteBuffer[] serialize(ReplyHeader h, Record r, String tag, String cacheKey, Stat stat, + int opCode) throws IOException { + byte[] header = serializeRecord(h); + byte[] data = null; + if(r != null) { + ResponseCache cache = null; + Counter cacheHit = null, cacheMiss = null; + switch(opCode) { + case OpCode.getData: { + cache = zkServer.getReadResponseCache(); + cacheHit = ServerMetrics.getMetrics().RESPONSE_PACKET_CACHE_HITS; + cacheMiss = ServerMetrics.getMetrics().RESPONSE_PACKET_CACHE_MISSING; + break; + } + case OpCode.getChildren2: { + cache = zkServer.getGetChildrenResponseCache(); + cacheHit = ServerMetrics.getMetrics().RESPONSE_PACKET_GET_CHILDREN_CACHE_HITS; + cacheMiss = ServerMetrics.getMetrics().RESPONSE_PACKET_GET_CHILDREN_CACHE_MISSING; + break; + } + default: + // op codes where response cache is not supported. + } + + if(cache != null && stat != null && cacheKey != null && !cacheKey.endsWith(Quotas.statNode)) { + // Use cache to get serialized data. + // + // NB: Tag is ignored both during cache lookup and serialization, + // since is is not used in read responses, which are being cached. + data = cache.get(cacheKey, stat); + if(data == null) { + // Cache miss, serialize the response and put it in cache. + data = serializeRecord(r); + cache.put(cacheKey, data, stat); + cacheMiss.add(1); + } else { + cacheHit.add(1); + } + } else { + data = serializeRecord(r); + } + } + int dataLength = data == null ? 0 : data.length; + int packetLength = header.length + dataLength; + ServerStats serverStats = serverStats(); + if(serverStats != null) { + serverStats.updateClientResponseSize(packetLength); + } + ByteBuffer lengthBuffer = ByteBuffer.allocate(4).putInt(packetLength); + lengthBuffer.rewind(); + + int bufferLen = data != null ? 3 : 2; + ByteBuffer[] buffers = new ByteBuffer[bufferLen]; + + buffers[0] = lengthBuffer; + buffers[1] = ByteBuffer.wrap(header); + if(data != null) { + buffers[2] = ByteBuffer.wrap(data); + } + return buffers; + } + + /* notify the client the session is closing and close/cleanup socket */ + public abstract void sendCloseSession(); + + public abstract void process(WatchedEvent event); + + public abstract long getSessionId(); + + abstract void setSessionId(long sessionId); + + /** + * auth info for the cnxn, returns an unmodifyable list + */ + public List getAuthInfo() { + return Collections.unmodifiableList(new ArrayList<>(authInfo)); + } + + public void addAuthInfo(Id id) { + authInfo.add(id); + } + + public boolean removeAuthInfo(Id id) { + return authInfo.remove(id); + } + + abstract void sendBuffer(ByteBuffer... buffers); + + abstract void enableRecv(); + + void disableRecv() { + disableRecv(true); + } + + abstract void disableRecv(boolean waitDisableRecv); + + abstract void setSessionTimeout(int sessionTimeout); + + protected ZooKeeperSaslServer zooKeeperSaslServer = null; + + protected static class CloseRequestException extends IOException { + + private static final long serialVersionUID = -7854505709816442681L; + private DisconnectReason reason; + + public CloseRequestException(String msg, DisconnectReason reason) { + super(msg); + this.reason = reason; + } + + public DisconnectReason getReason() { + return reason; + } + + } + + protected static class EndOfStreamException extends IOException { + + private static final long serialVersionUID = -8255690282104294178L; + private DisconnectReason reason; + + public EndOfStreamException(String msg, DisconnectReason reason) { + super(msg); + this.reason = reason; + } + + public String toString() { + return "EndOfStreamException: " + getMessage(); + } + + public DisconnectReason getReason() { + return reason; + } + + } + + public boolean isStale() { + return stale; + } + + public void setStale() { + stale = true; + } + + public boolean isInvalid() { + return invalid; + } + + public void setInvalid() { + if(!invalid) { + if(!stale) { + sendCloseSession(); + } + invalid = true; + } + } + + protected void packetReceived(long bytes) { + incrPacketsReceived(); + ServerStats serverStats = serverStats(); + if(serverStats != null) { + serverStats().incrementPacketsReceived(); + } + ServerMetrics.getMetrics().BYTES_RECEIVED_COUNT.add(bytes); + } + + protected void packetSent() { + incrPacketsSent(); + ServerStats serverStats = serverStats(); + if(serverStats != null) { + serverStats.incrementPacketsSent(); + } + } + + protected abstract ServerStats serverStats(); + + protected final Date established = new Date(); + + protected final AtomicLong packetsReceived = new AtomicLong(); + protected final AtomicLong packetsSent = new AtomicLong(); + + protected long minLatency; + protected long maxLatency; + protected String lastOp; + protected long lastCxid; + protected long lastZxid; + protected long lastResponseTime; + protected long lastLatency; + + protected long count; + protected long totalLatency; + protected long requestsProcessedCount; + protected DisconnectReason disconnectReason = DisconnectReason.UNKNOWN; + + public synchronized void resetStats() { + disconnectReason = DisconnectReason.RESET_COMMAND; + packetsReceived.set(0); + packetsSent.set(0); + minLatency = Long.MAX_VALUE; + maxLatency = 0; + lastOp = "NA"; + lastCxid = -1; + lastZxid = -1; + lastResponseTime = 0; + lastLatency = 0; + + count = 0; + totalLatency = 0; + } + + protected long incrPacketsReceived() { + return packetsReceived.incrementAndGet(); + } + + protected long incrPacketsSent() { + return packetsSent.incrementAndGet(); + } + + protected synchronized void updateStatsForResponse(long cxid, long zxid, String op, long start, long end) { + // don't overwrite with "special" xids - we're interested + // in the clients last real operation + if(cxid >= 0) { + lastCxid = cxid; + } + lastZxid = zxid; + lastOp = op; + lastResponseTime = end; + long elapsed = end - start; + lastLatency = elapsed; + if(elapsed < minLatency) { + minLatency = elapsed; + } + if(elapsed > maxLatency) { + maxLatency = elapsed; + } + count++; + totalLatency += elapsed; + } + + public Date getEstablished() { + return (Date) established.clone(); + } + + public long getOutstandingRequests() { + return outstandingCount.longValue(); + } + + public long getPacketsReceived() { + return packetsReceived.longValue(); + } + + public long getPacketsSent() { + return packetsSent.longValue(); + } + + public synchronized long getMinLatency() { + return minLatency == Long.MAX_VALUE ? 0 : minLatency; + } + + public synchronized long getAvgLatency() { + return count == 0 ? 0 : totalLatency / count; + } + + public synchronized long getMaxLatency() { + return maxLatency; + } + + public synchronized String getLastOperation() { + return lastOp; + } + + public synchronized long getLastCxid() { + return lastCxid; + } + + public synchronized long getLastZxid() { + return lastZxid; + } + + public synchronized long getLastResponseTime() { + return lastResponseTime; + } + + public synchronized long getLastLatency() { + return lastLatency; + } + + /** + * Prints detailed stats information for the connection. + * + * @see #dumpConnectionInfo(PrintWriter, boolean) for brief stats + */ + @Override + public String toString() { + StringWriter sw = new StringWriter(); + PrintWriter pwriter = new PrintWriter(sw); + dumpConnectionInfo(pwriter, false); + pwriter.flush(); + pwriter.close(); + return sw.toString(); + } + + public abstract InetSocketAddress getRemoteSocketAddress(); + + public abstract int getInterestOps(); + + public abstract boolean isSecure(); + + public abstract Certificate[] getClientCertificateChain(); + + public abstract void setClientCertificateChain(Certificate[] chain); + + /** + * Print information about the connection. + * + * @param brief iff true prints brief details, otw full detail + */ + public synchronized void dumpConnectionInfo(PrintWriter pwriter, boolean brief) { + pwriter.print(" "); + pwriter.print(getRemoteSocketAddress()); + pwriter.print("["); + int interestOps = getInterestOps(); + pwriter.print(interestOps == 0 ? "0" : Integer.toHexString(interestOps)); + pwriter.print("](queued="); + pwriter.print(getOutstandingRequests()); + pwriter.print(",recved="); + pwriter.print(getPacketsReceived()); + pwriter.print(",sent="); + pwriter.print(getPacketsSent()); + + if(!brief) { + long sessionId = getSessionId(); + if(sessionId != 0) { + pwriter.print(",sid=0x"); + pwriter.print(Long.toHexString(sessionId)); + pwriter.print(",lop="); + pwriter.print(getLastOperation()); + pwriter.print(",est="); + pwriter.print(getEstablished().getTime()); + pwriter.print(",to="); + pwriter.print(getSessionTimeout()); + long lastCxid = getLastCxid(); + if(lastCxid >= 0) { + pwriter.print(",lcxid=0x"); + pwriter.print(Long.toHexString(lastCxid)); + } + pwriter.print(",lzxid=0x"); + pwriter.print(Long.toHexString(getLastZxid())); + pwriter.print(",lresp="); + pwriter.print(getLastResponseTime()); + pwriter.print(",llat="); + pwriter.print(getLastLatency()); + pwriter.print(",minlat="); + pwriter.print(getMinLatency()); + pwriter.print(",avglat="); + pwriter.print(getAvgLatency()); + pwriter.print(",maxlat="); + pwriter.print(getMaxLatency()); + } + } + pwriter.print(")"); + } + + public synchronized Map getConnectionInfo(boolean brief) { + Map info = new LinkedHashMap(); + info.put("remote_socket_address", getRemoteSocketAddress()); + info.put("interest_ops", getInterestOps()); + info.put("outstanding_requests", getOutstandingRequests()); + info.put("packets_received", getPacketsReceived()); + info.put("packets_sent", getPacketsSent()); + if(!brief) { + info.put("session_id", getSessionId()); + info.put("last_operation", getLastOperation()); + info.put("established", getEstablished()); + info.put("session_timeout", getSessionTimeout()); + info.put("last_cxid", getLastCxid()); + info.put("last_zxid", getLastZxid()); + info.put("last_response_time", getLastResponseTime()); + info.put("last_latency", getLastLatency()); + info.put("min_latency", getMinLatency()); + info.put("avg_latency", getAvgLatency()); + info.put("max_latency", getMaxLatency()); + } + return info; + } + + /** + * clean up the socket related to a command and also make sure we flush the + * data before we do that + * + * @param pwriter the pwriter for a command socket + */ + public void cleanupWriterSocket(PrintWriter pwriter) { + try { + if(pwriter != null) { + pwriter.flush(); + pwriter.close(); + } + } catch(Exception e) { + LOG.info("Error closing PrintWriter ", e); + } finally { + try { + close(DisconnectReason.CLOSE_CONNECTION_COMMAND); + } catch(Exception e) { + LOG.error("Error closing a command socket ", e); + } + } + } + + /** + * Returns the IP address or empty string. + */ + public String getHostAddress() { + InetSocketAddress remoteSocketAddress = getRemoteSocketAddress(); + if(remoteSocketAddress == null) { + return ""; + } + InetAddress address = remoteSocketAddress.getAddress(); + if(address == null) { + return ""; + } + return address.getHostAddress(); + } + + /** + * Get session id in hexadecimal notation. + */ + public String getSessionIdHex() { + return "0x" + Long.toHexString(getSessionId()); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnFactory.java new file mode 100644 index 0000000..5b72927 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnFactory.java @@ -0,0 +1,346 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import javax.management.JMException; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginException; + +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.Login; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.auth.SaslServerCallbackHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class ServerCnxnFactory { + + public static final String ZOOKEEPER_SERVER_CNXN_FACTORY = "zookeeper.serverCnxnFactory"; + private static final String ZOOKEEPER_MAX_CONNECTION = "zookeeper.maxCnxns"; + public static final int ZOOKEEPER_MAX_CONNECTION_DEFAULT = 0; + + private static final Logger LOG = LoggerFactory.getLogger(ServerCnxnFactory.class); + + // Tells whether SSL is enabled on this ServerCnxnFactory + protected boolean secure; + + /** + * The buffer will cause the connection to be close when we do a send. + */ + static final ByteBuffer closeConn = ByteBuffer.allocate(0); + + // total number of connections accepted by the ZooKeeper server + protected int maxCnxns; + + // sessionMap is used by closeSession() + final ConcurrentHashMap sessionMap = new ConcurrentHashMap(); + + private static String loginUser = Login.SYSTEM_USER; + + public void addSession(long sessionId, ServerCnxn cnxn) { + sessionMap.put(sessionId, cnxn); + } + + public void removeCnxnFromSessionMap(ServerCnxn cnxn) { + long sessionId = cnxn.getSessionId(); + if(sessionId != 0) { + sessionMap.remove(sessionId); + } + } + + /** + * @return true if the cnxn that contains the sessionId exists in this ServerCnxnFactory + * and it's closed. Otherwise false. + */ + public boolean closeSession(long sessionId, ServerCnxn.DisconnectReason reason) { + ServerCnxn cnxn = sessionMap.remove(sessionId); + if(cnxn != null) { + try { + cnxn.close(reason); + } catch(Exception e) { + LOG.warn("exception during session close", e); + } + return true; + } + return false; + } + + public abstract int getLocalPort(); + + public abstract Iterable getConnections(); + + public int getNumAliveConnections() { + return cnxns.size(); + } + + public final ZooKeeperServer getZooKeeperServer() { + return zkServer; + } + + public void configure(InetSocketAddress addr, int maxcc) throws IOException { + configure(addr, maxcc, -1); + } + + public void configure(InetSocketAddress addr, int maxcc, int backlog) throws IOException { + configure(addr, maxcc, backlog, false); + } + + public abstract void configure(InetSocketAddress addr, int maxcc, int backlog, boolean secure) throws IOException; + + public abstract void reconfigure(InetSocketAddress addr); + + protected SaslServerCallbackHandler saslServerCallbackHandler; + public Login login; + + /** + * Maximum number of connections allowed from particular host (ip) + */ + public abstract int getMaxClientCnxnsPerHost(); + + /** + * Maximum number of connections allowed from particular host (ip) + */ + public abstract void setMaxClientCnxnsPerHost(int max); + + public boolean isSecure() { + return secure; + } + + public void startup(ZooKeeperServer zkServer) throws IOException, InterruptedException { + startup(zkServer, true); + } + + // This method is to maintain compatiblity of startup(zks) and enable sharing of zks + // when we add secureCnxnFactory. + public abstract void startup(ZooKeeperServer zkServer, boolean startServer) throws IOException, InterruptedException; + + /** + * The maximum queue length of the ZooKeeper server's socket + */ + public abstract int getSocketListenBacklog(); + + public abstract void join() throws InterruptedException; + + public abstract void shutdown(); + + public abstract void start(); + + protected ZooKeeperServer zkServer; + + public final void setZooKeeperServer(ZooKeeperServer zks) { + this.zkServer = zks; + if(zks != null) { + if(secure) { + zks.setSecureServerCnxnFactory(this); + } else { + zks.setServerCnxnFactory(this); + } + } + } + + public abstract void closeAll(ServerCnxn.DisconnectReason reason); + + public static ServerCnxnFactory createFactory() throws IOException { + + // TODO_MA 注释: 从环境变量中,找到 zookeeper.serverCnxnFactory 对应的 value 一般是么有配置的 + // TODO_MA 注释: 如果需要更改,则: zookeeper.serverCnxnFactory = org.apache.zookeeper.server.NettyServerCnxnFactory + // TODO_MA 马中华 注释: 默认实现:NIOServerCnxnFactory + String serverCnxnFactoryName = System.getProperty(ZOOKEEPER_SERVER_CNXN_FACTORY); + if(serverCnxnFactoryName == null) { + serverCnxnFactoryName = NIOServerCnxnFactory.class.getName(); + } + + // TODO_MA 注释: 通过反射来创建一个 实例对象 + try { + ServerCnxnFactory serverCnxnFactory = (ServerCnxnFactory) Class.forName(serverCnxnFactoryName) + .getDeclaredConstructor().newInstance(); + LOG.info("Using {} as server connection factory", serverCnxnFactoryName); + return serverCnxnFactory; + } catch(Exception e) { + IOException ioe = new IOException("Couldn't instantiate " + serverCnxnFactoryName, e); + throw ioe; + } + } + + public static ServerCnxnFactory createFactory(int clientPort, int maxClientCnxns) throws IOException { + return createFactory(new InetSocketAddress(clientPort), maxClientCnxns, -1); + } + + public static ServerCnxnFactory createFactory(int clientPort, int maxClientCnxns, int backlog) throws IOException { + return createFactory(new InetSocketAddress(clientPort), maxClientCnxns, backlog); + } + + public static ServerCnxnFactory createFactory(InetSocketAddress addr, int maxClientCnxns) throws IOException { + return createFactory(addr, maxClientCnxns, -1); + } + + public static ServerCnxnFactory createFactory(InetSocketAddress addr, int maxClientCnxns, + int backlog) throws IOException { + ServerCnxnFactory factory = createFactory(); + factory.configure(addr, maxClientCnxns, backlog); + return factory; + } + + public abstract InetSocketAddress getLocalAddress(); + + public abstract void resetAllConnectionStats(); + + public abstract Iterable> getAllConnectionInfo(boolean brief); + + private final ConcurrentHashMap connectionBeans = new ConcurrentHashMap(); + + // Connection set is relied on heavily by four letter commands + // Construct a ConcurrentHashSet using a ConcurrentHashMap + protected final Set cnxns = Collections.newSetFromMap(new ConcurrentHashMap()); + + public void unregisterConnection(ServerCnxn serverCnxn) { + ConnectionBean jmxConnectionBean = connectionBeans.remove(serverCnxn); + if(jmxConnectionBean != null) { + MBeanRegistry.getInstance().unregister(jmxConnectionBean); + } + } + + public void registerConnection(ServerCnxn serverCnxn) { + if(zkServer != null) { + ConnectionBean jmxConnectionBean = new ConnectionBean(serverCnxn, zkServer); + try { + MBeanRegistry.getInstance().register(jmxConnectionBean, zkServer.jmxServerBean); + connectionBeans.put(serverCnxn, jmxConnectionBean); + } catch(JMException e) { + LOG.warn("Could not register connection", e); + } + } + + } + + /** + * Initialize the server SASL if specified. + * + * If the user has specified a "ZooKeeperServer.LOGIN_CONTEXT_NAME_KEY" + * or a jaas.conf using "java.security.auth.login.config" + * the authentication is required and an exception is raised. + * Otherwise no authentication is configured and no exception is raised. + * + * @throws IOException if jaas.conf is missing or there's an error in it. + */ + protected void configureSaslLogin() throws IOException { + String serverSection = System + .getProperty(ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY, ZooKeeperSaslServer.DEFAULT_LOGIN_CONTEXT_NAME); + + // Note that 'Configuration' here refers to javax.security.auth.login.Configuration. + AppConfigurationEntry[] entries = null; + SecurityException securityException = null; + try { + entries = Configuration.getConfiguration().getAppConfigurationEntry(serverSection); + } catch(SecurityException e) { + // handle below: might be harmless if the user doesn't intend to use JAAS authentication. + securityException = e; + } + + // No entries in jaas.conf + // If there's a configuration exception fetching the jaas section and + // the user has required sasl by specifying a LOGIN_CONTEXT_NAME_KEY or a jaas file + // we throw an exception otherwise we continue without authentication. + if(entries == null) { + String jaasFile = System.getProperty(Environment.JAAS_CONF_KEY); + String loginContextName = System.getProperty(ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY); + if(securityException != null && (loginContextName != null || jaasFile != null)) { + String errorMessage = "No JAAS configuration section named '" + serverSection + "' was found"; + if(jaasFile != null) { + errorMessage += " in '" + jaasFile + "'."; + } + if(loginContextName != null) { + errorMessage += " But " + ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY + " was set."; + } + LOG.error(errorMessage); + throw new IOException(errorMessage); + } + return; + } + + // jaas.conf entry available + try { + saslServerCallbackHandler = new SaslServerCallbackHandler(Configuration.getConfiguration()); + login = new Login(serverSection, saslServerCallbackHandler, new ZKConfig()); + setLoginUser(login.getUserName()); + login.startThreadIfNeeded(); + } catch(LoginException e) { + throw new IOException( + "Could not configure server because SASL configuration did not allow the " + " ZooKeeper server to authenticate itself properly: " + e); + } + } + + private static void setLoginUser(String name) { + //Created this method to avoid ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD find bug issue + loginUser = name; + } + + /** + * User who has started the ZooKeeper server user, it will be the logged-in + * user. If no user logged-in then system user + */ + public static String getUserName() { + return loginUser; + } + + /** + * Maximum number of connections allowed in the ZooKeeper system + */ + public int getMaxCnxns() { + return maxCnxns; + } + + protected void initMaxCnxns() { + maxCnxns = Integer.getInteger(ZOOKEEPER_MAX_CONNECTION, ZOOKEEPER_MAX_CONNECTION_DEFAULT); + if(maxCnxns < 0) { + maxCnxns = ZOOKEEPER_MAX_CONNECTION_DEFAULT; + LOG.warn("maxCnxns should be greater than or equal to 0, using default vlaue {}.", + ZOOKEEPER_MAX_CONNECTION_DEFAULT); + } else if(maxCnxns == ZOOKEEPER_MAX_CONNECTION_DEFAULT) { + LOG.warn("maxCnxns is not configured, using default value {}.", ZOOKEEPER_MAX_CONNECTION_DEFAULT); + } else { + LOG.info("maxCnxns configured value is {}.", maxCnxns); + } + } + + /** + * Ensure total number of connections are less than the maxCnxns + */ + protected boolean limitTotalNumberOfCnxns() { + if(maxCnxns <= 0) { + // maxCnxns limit is disabled + return false; + } + int cnxns = getNumAliveConnections(); + if(cnxns >= maxCnxns) { + LOG.error("Too many connections " + cnxns + " - max is " + maxCnxns); + return true; + } + return false; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnHelper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnHelper.java new file mode 100644 index 0000000..8f78c7c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerCnxnHelper.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +public class ServerCnxnHelper { + + /** gets maximum number of connections in ZooKeeper */ + public static int getMaxCnxns(ServerCnxnFactory secureServerCnxnFactory, ServerCnxnFactory serverCnxnFactory) { + if (serverCnxnFactory != null) { + return serverCnxnFactory.getMaxCnxns(); + } + if (secureServerCnxnFactory != null) { + return secureServerCnxnFactory.getMaxCnxns(); + } + // default + return ServerCnxnFactory.ZOOKEEPER_MAX_CONNECTION_DEFAULT; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerConfig.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerConfig.java new file mode 100644 index 0000000..9da0e53 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerConfig.java @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.File; +import java.net.InetSocketAddress; +import java.util.Arrays; +import java.util.Properties; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +/** + * Server configuration storage. + * + * We use this instead of Properties as it's typed. + * + */ +@InterfaceAudience.Public +public class ServerConfig { + + //// + //// If you update the configuration parameters be sure + //// to update the "conf" 4letter word + //// + protected InetSocketAddress clientPortAddress; + protected InetSocketAddress secureClientPortAddress; + protected File dataDir; + protected File dataLogDir; + protected int tickTime = ZooKeeperServer.DEFAULT_TICK_TIME; + protected int maxClientCnxns; + /** defaults to -1 if not set explicitly */ + protected int minSessionTimeout = -1; + /** defaults to -1 if not set explicitly */ + protected int maxSessionTimeout = -1; + protected String metricsProviderClassName = DefaultMetricsProvider.class.getName(); + protected Properties metricsProviderConfiguration = new Properties(); + /** defaults to -1 if not set explicitly */ + protected int listenBacklog = -1; + protected String initialConfig; + + /** JVM Pause Monitor feature switch */ + protected boolean jvmPauseMonitorToRun = false; + /** JVM Pause Monitor warn threshold in ms */ + protected long jvmPauseWarnThresholdMs; + /** JVM Pause Monitor info threshold in ms */ + protected long jvmPauseInfoThresholdMs; + /** JVM Pause Monitor sleep time in ms */ + protected long jvmPauseSleepTimeMs; + + /** + * Parse arguments for server configuration + * @param args clientPort dataDir and optional tickTime and maxClientCnxns + * @throws IllegalArgumentException on invalid usage + */ + public void parse(String[] args) { + if (args.length < 2 || args.length > 4) { + throw new IllegalArgumentException("Invalid number of arguments:" + Arrays.toString(args)); + } + + clientPortAddress = new InetSocketAddress(Integer.parseInt(args[0])); + dataDir = new File(args[1]); + dataLogDir = dataDir; + if (args.length >= 3) { + tickTime = Integer.parseInt(args[2]); + } + if (args.length == 4) { + maxClientCnxns = Integer.parseInt(args[3]); + } + } + + /** + * Parse a ZooKeeper configuration file + * @param path the patch of the configuration file + * @throws ConfigException error processing configuration + */ + public void parse(String path) throws ConfigException { + QuorumPeerConfig config = new QuorumPeerConfig(); + config.parse(path); + + // let qpconfig parse the file and then pull the stuff we are + // interested in + readFrom(config); + } + + /** + * Read attributes from a QuorumPeerConfig. + * @param config + */ + public void readFrom(QuorumPeerConfig config) { + clientPortAddress = config.getClientPortAddress(); + secureClientPortAddress = config.getSecureClientPortAddress(); + dataDir = config.getDataDir(); + dataLogDir = config.getDataLogDir(); + tickTime = config.getTickTime(); + maxClientCnxns = config.getMaxClientCnxns(); + minSessionTimeout = config.getMinSessionTimeout(); + maxSessionTimeout = config.getMaxSessionTimeout(); + jvmPauseMonitorToRun = config.isJvmPauseMonitorToRun(); + jvmPauseInfoThresholdMs = config.getJvmPauseInfoThresholdMs(); + jvmPauseWarnThresholdMs = config.getJvmPauseWarnThresholdMs(); + jvmPauseSleepTimeMs = config.getJvmPauseSleepTimeMs(); + metricsProviderClassName = config.getMetricsProviderClassName(); + metricsProviderConfiguration = config.getMetricsProviderConfiguration(); + listenBacklog = config.getClientPortListenBacklog(); + initialConfig = config.getInitialConfig(); + } + + public InetSocketAddress getClientPortAddress() { + return clientPortAddress; + } + public InetSocketAddress getSecureClientPortAddress() { + return secureClientPortAddress; + } + public File getDataDir() { + return dataDir; + } + public File getDataLogDir() { + return dataLogDir; + } + public int getTickTime() { + return tickTime; + } + public int getMaxClientCnxns() { + return maxClientCnxns; + } + /** minimum session timeout in milliseconds, -1 if unset */ + public int getMinSessionTimeout() { + return minSessionTimeout; + } + /** maximum session timeout in milliseconds, -1 if unset */ + public int getMaxSessionTimeout() { + return maxSessionTimeout; + } + + public long getJvmPauseInfoThresholdMs() { + return jvmPauseInfoThresholdMs; + } + public long getJvmPauseWarnThresholdMs() { + return jvmPauseWarnThresholdMs; + } + public long getJvmPauseSleepTimeMs() { + return jvmPauseSleepTimeMs; + } + public boolean isJvmPauseMonitorToRun() { + return jvmPauseMonitorToRun; + } + public String getMetricsProviderClassName() { + return metricsProviderClassName; + } + public Properties getMetricsProviderConfiguration() { + return metricsProviderConfiguration; + } + /** Maximum number of pending socket connections to read, -1 if unset */ + public int getClientPortListenBacklog() { + return listenBacklog; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerMetrics.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerMetrics.java new file mode 100644 index 0000000..cee2205 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerMetrics.java @@ -0,0 +1,467 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.apache.zookeeper.metrics.Counter; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.metrics.MetricsContext.DetailLevel; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.Summary; +import org.apache.zookeeper.metrics.SummarySet; +import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider; +import org.apache.zookeeper.metrics.impl.NullMetricsProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class ServerMetrics { + + private static final Logger LOG = LoggerFactory.getLogger(ServerMetrics.class); + + /** + * Dummy instance useful for tests. + */ + public static final ServerMetrics NULL_METRICS = new ServerMetrics(NullMetricsProvider.INSTANCE); + + /** + * Dummy instance useful for tests. + */ + public static final ServerMetrics DEFAULT_METRICS_FOR_TESTS = new ServerMetrics(new DefaultMetricsProvider()); + + /** + * Real instance used for tracking server side metrics. The final value is + * assigned after the {@link MetricsProvider} bootstrap. + */ + private static volatile ServerMetrics CURRENT = DEFAULT_METRICS_FOR_TESTS; + + /** + * Access current ServerMetrics. + * + * @return a reference to the current Metrics + */ + public static ServerMetrics getMetrics() { + return CURRENT; + } + + public static void metricsProviderInitialized(MetricsProvider metricsProvider) { + LOG.info("ServerMetrics initialized with provider {}", metricsProvider); + CURRENT = new ServerMetrics(metricsProvider); + } + + private ServerMetrics(MetricsProvider metricsProvider) { + this.metricsProvider = metricsProvider; + MetricsContext metricsContext = this.metricsProvider.getRootContext(); + + FSYNC_TIME = metricsContext.getSummary("fsynctime", DetailLevel.BASIC); + + SNAPSHOT_TIME = metricsContext.getSummary("snapshottime", DetailLevel.BASIC); + DB_INIT_TIME = metricsContext.getSummary("dbinittime", DetailLevel.BASIC); + READ_LATENCY = metricsContext.getSummary("readlatency", DetailLevel.ADVANCED); + UPDATE_LATENCY = metricsContext.getSummary("updatelatency", DetailLevel.ADVANCED); + PROPAGATION_LATENCY = metricsContext.getSummary("propagation_latency", DetailLevel.ADVANCED); + FOLLOWER_SYNC_TIME = metricsContext.getSummary("follower_sync_time", DetailLevel.BASIC); + ELECTION_TIME = metricsContext.getSummary("election_time", DetailLevel.BASIC); + LOOKING_COUNT = metricsContext.getCounter("looking_count"); + DIFF_COUNT = metricsContext.getCounter("diff_count"); + SNAP_COUNT = metricsContext.getCounter("snap_count"); + COMMIT_COUNT = metricsContext.getCounter("commit_count"); + CONNECTION_REQUEST_COUNT = metricsContext.getCounter("connection_request_count"); + CONNECTION_TOKEN_DEFICIT = metricsContext.getSummary("connection_token_deficit", DetailLevel.BASIC); + CONNECTION_REJECTED = metricsContext.getCounter("connection_rejected"); + + INFLIGHT_SNAP_COUNT = metricsContext.getSummary("inflight_snap_count", DetailLevel.BASIC); + INFLIGHT_DIFF_COUNT = metricsContext.getSummary("inflight_diff_count", DetailLevel.BASIC); + + WRITE_PER_NAMESPACE = metricsContext.getSummarySet("write_per_namespace", DetailLevel.BASIC); + READ_PER_NAMESPACE = metricsContext.getSummarySet("read_per_namespace", DetailLevel.BASIC); + + BYTES_RECEIVED_COUNT = metricsContext.getCounter("bytes_received_count"); + UNRECOVERABLE_ERROR_COUNT = metricsContext.getCounter("unrecoverable_error_count"); + + NODE_CREATED_WATCHER = metricsContext.getSummary("node_created_watch_count", DetailLevel.BASIC); + NODE_DELETED_WATCHER = metricsContext.getSummary("node_deleted_watch_count", DetailLevel.BASIC); + NODE_CHANGED_WATCHER = metricsContext.getSummary("node_changed_watch_count", DetailLevel.BASIC); + NODE_CHILDREN_WATCHER = metricsContext.getSummary("node_children_watch_count", DetailLevel.BASIC); + + /* + * Number of dead watchers in DeadWatcherListener + */ + ADD_DEAD_WATCHER_STALL_TIME = metricsContext.getCounter("add_dead_watcher_stall_time"); + DEAD_WATCHERS_QUEUED = metricsContext.getCounter("dead_watchers_queued"); + DEAD_WATCHERS_CLEARED = metricsContext.getCounter("dead_watchers_cleared"); + DEAD_WATCHERS_CLEANER_LATENCY = metricsContext.getSummary("dead_watchers_cleaner_latency", DetailLevel.ADVANCED); + + RESPONSE_PACKET_CACHE_HITS = metricsContext.getCounter("response_packet_cache_hits"); + RESPONSE_PACKET_CACHE_MISSING = metricsContext.getCounter("response_packet_cache_misses"); + RESPONSE_PACKET_GET_CHILDREN_CACHE_HITS = metricsContext.getCounter("response_packet_get_children_cache_hits"); + RESPONSE_PACKET_GET_CHILDREN_CACHE_MISSING = metricsContext.getCounter("response_packet_get_children_cache_misses"); + + ENSEMBLE_AUTH_SUCCESS = metricsContext.getCounter("ensemble_auth_success"); + + ENSEMBLE_AUTH_FAIL = metricsContext.getCounter("ensemble_auth_fail"); + + ENSEMBLE_AUTH_SKIP = metricsContext.getCounter("ensemble_auth_skip"); + + PREP_PROCESSOR_QUEUE_TIME = metricsContext.getSummary("prep_processor_queue_time_ms", DetailLevel.ADVANCED); + PREP_PROCESSOR_QUEUE_SIZE = metricsContext.getSummary("prep_processor_queue_size", DetailLevel.BASIC); + PREP_PROCESSOR_QUEUED = metricsContext.getCounter("prep_processor_request_queued"); + OUTSTANDING_CHANGES_QUEUED = metricsContext.getCounter("outstanding_changes_queued"); + OUTSTANDING_CHANGES_REMOVED = metricsContext.getCounter("outstanding_changes_removed"); + PREP_PROCESS_TIME = metricsContext.getSummary("prep_process_time", DetailLevel.BASIC); + CLOSE_SESSION_PREP_TIME = metricsContext.getSummary("close_session_prep_time", DetailLevel.ADVANCED); + + REVALIDATE_COUNT = metricsContext.getCounter("revalidate_count"); + CONNECTION_DROP_COUNT = metricsContext.getCounter("connection_drop_count"); + CONNECTION_REVALIDATE_COUNT = metricsContext.getCounter("connection_revalidate_count"); + + // Expiry queue stats + SESSIONLESS_CONNECTIONS_EXPIRED = metricsContext.getCounter("sessionless_connections_expired"); + STALE_SESSIONS_EXPIRED = metricsContext.getCounter("stale_sessions_expired"); + + /* + * Number of requests that are in the session queue. + */ + REQUESTS_IN_SESSION_QUEUE = metricsContext.getSummary("requests_in_session_queue", DetailLevel.BASIC); + PENDING_SESSION_QUEUE_SIZE = metricsContext.getSummary("pending_session_queue_size", DetailLevel.BASIC); + /* + * Consecutive number of read requests that are in the session queue right after a commit request. + */ + READS_AFTER_WRITE_IN_SESSION_QUEUE = metricsContext.getSummary("reads_after_write_in_session_queue", DetailLevel.BASIC); + READ_ISSUED_FROM_SESSION_QUEUE = metricsContext.getSummary("reads_issued_from_session_queue", DetailLevel.BASIC); + SESSION_QUEUES_DRAINED = metricsContext.getSummary("session_queues_drained", DetailLevel.BASIC); + + TIME_WAITING_EMPTY_POOL_IN_COMMIT_PROCESSOR_READ = metricsContext.getSummary("time_waiting_empty_pool_in_commit_processor_read_ms", DetailLevel.BASIC); + WRITE_BATCH_TIME_IN_COMMIT_PROCESSOR = metricsContext.getSummary("write_batch_time_in_commit_processor", DetailLevel.BASIC); + + CONCURRENT_REQUEST_PROCESSING_IN_COMMIT_PROCESSOR = metricsContext.getSummary("concurrent_request_processing_in_commit_processor", DetailLevel.BASIC); + + READS_QUEUED_IN_COMMIT_PROCESSOR = metricsContext.getSummary("read_commit_proc_req_queued", DetailLevel.BASIC); + WRITES_QUEUED_IN_COMMIT_PROCESSOR = metricsContext.getSummary("write_commit_proc_req_queued", DetailLevel.BASIC); + COMMITS_QUEUED_IN_COMMIT_PROCESSOR = metricsContext.getSummary("commit_commit_proc_req_queued", DetailLevel.BASIC); + COMMITS_QUEUED = metricsContext.getCounter("request_commit_queued"); + READS_ISSUED_IN_COMMIT_PROC = metricsContext.getSummary("read_commit_proc_issued", DetailLevel.BASIC); + WRITES_ISSUED_IN_COMMIT_PROC = metricsContext.getSummary("write_commit_proc_issued", DetailLevel.BASIC); + + /** + * Time spent by a read request in the commit processor. + */ + READ_COMMITPROC_TIME = metricsContext.getSummary("read_commitproc_time_ms", DetailLevel.ADVANCED); + + /** + * Time spent by a write request in the commit processor. + */ + WRITE_COMMITPROC_TIME = metricsContext.getSummary("write_commitproc_time_ms", DetailLevel.ADVANCED); + + /** + * Time spent by a committed request, for a locally issued write, in the + * commit processor. + */ + LOCAL_WRITE_COMMITTED_TIME = metricsContext.getSummary("local_write_committed_time_ms", DetailLevel.ADVANCED); + + /** + * Time spent by a committed request for a write, issued by other server, in the + * commit processor. + */ + SERVER_WRITE_COMMITTED_TIME = metricsContext.getSummary("server_write_committed_time_ms", DetailLevel.ADVANCED); + + COMMIT_PROCESS_TIME = metricsContext.getSummary("commit_process_time", DetailLevel.BASIC); + + /** + * Observer Master processing metrics. + */ + OM_PROPOSAL_PROCESS_TIME = metricsContext.getSummary("om_proposal_process_time_ms", DetailLevel.ADVANCED); + OM_COMMIT_PROCESS_TIME = metricsContext.getSummary("om_commit_process_time_ms", DetailLevel.ADVANCED); + + /** + * Time spent by the final processor. This is tracked in the commit processor. + */ + READ_FINAL_PROC_TIME = metricsContext.getSummary("read_final_proc_time_ms", DetailLevel.ADVANCED); + WRITE_FINAL_PROC_TIME = metricsContext.getSummary("write_final_proc_time_ms", DetailLevel.ADVANCED); + + PROPOSAL_LATENCY = metricsContext.getSummary("proposal_latency", DetailLevel.ADVANCED); + PROPOSAL_ACK_CREATION_LATENCY = metricsContext.getSummary("proposal_ack_creation_latency", DetailLevel.ADVANCED); + COMMIT_PROPAGATION_LATENCY = metricsContext.getSummary("commit_propagation_latency", DetailLevel.ADVANCED); + LEARNER_PROPOSAL_RECEIVED_COUNT = metricsContext.getCounter("learner_proposal_received_count"); + LEARNER_COMMIT_RECEIVED_COUNT = metricsContext.getCounter("learner_commit_received_count"); + + /** + * Learner handler quorum packet metrics. + */ + LEARNER_HANDLER_QP_SIZE = metricsContext.getSummarySet("learner_handler_qp_size", DetailLevel.BASIC); + LEARNER_HANDLER_QP_TIME = metricsContext.getSummarySet("learner_handler_qp_time_ms", DetailLevel.ADVANCED); + + STARTUP_TXNS_LOADED = metricsContext.getSummary("startup_txns_loaded", DetailLevel.BASIC); + STARTUP_TXNS_LOAD_TIME = metricsContext.getSummary("startup_txns_load_time", DetailLevel.BASIC); + STARTUP_SNAP_LOAD_TIME = metricsContext.getSummary("startup_snap_load_time", DetailLevel.BASIC); + + SYNC_PROCESSOR_QUEUE_AND_FLUSH_TIME = metricsContext.getSummary("sync_processor_queue_and_flush_time_ms", DetailLevel.ADVANCED); + SYNC_PROCESSOR_QUEUE_SIZE = metricsContext.getSummary("sync_processor_queue_size", DetailLevel.BASIC); + SYNC_PROCESSOR_QUEUED = metricsContext.getCounter("sync_processor_request_queued"); + SYNC_PROCESSOR_QUEUE_TIME = metricsContext.getSummary("sync_processor_queue_time_ms", DetailLevel.ADVANCED); + SYNC_PROCESSOR_FLUSH_TIME = metricsContext.getSummary("sync_processor_queue_flush_time_ms", DetailLevel.ADVANCED); + SYNC_PROCESS_TIME = metricsContext.getSummary("sync_process_time", DetailLevel.BASIC); + + BATCH_SIZE = metricsContext.getSummary("sync_processor_batch_size", DetailLevel.BASIC); + + QUORUM_ACK_LATENCY = metricsContext.getSummary("quorum_ack_latency", DetailLevel.ADVANCED); + ACK_LATENCY = metricsContext.getSummarySet("ack_latency", DetailLevel.ADVANCED); + PROPOSAL_COUNT = metricsContext.getCounter("proposal_count"); + QUIT_LEADING_DUE_TO_DISLOYAL_VOTER = metricsContext.getCounter("quit_leading_due_to_disloyal_voter"); + + STALE_REQUESTS = metricsContext.getCounter("stale_requests"); + STALE_REQUESTS_DROPPED = metricsContext.getCounter("stale_requests_dropped"); + STALE_REPLIES = metricsContext.getCounter("stale_replies"); + REQUEST_THROTTLE_WAIT_COUNT = metricsContext.getCounter("request_throttle_wait_count"); + LARGE_REQUESTS_REJECTED = metricsContext.getCounter("large_requests_rejected"); + + NETTY_QUEUED_BUFFER = metricsContext.getSummary("netty_queued_buffer_capacity", DetailLevel.BASIC); + + DIGEST_MISMATCHES_COUNT = metricsContext.getCounter("digest_mismatches_count"); + TLS_HANDSHAKE_EXCEEDED = metricsContext.getCounter("tls_handshake_exceeded"); + + JVM_PAUSE_TIME = metricsContext.getSummary("jvm_pause_time_ms", DetailLevel.ADVANCED); + } + + /** + * Txnlog fsync time + */ + public final Summary FSYNC_TIME; + + /** + * Snapshot writing time + */ + public final Summary SNAPSHOT_TIME; + + /** + * Db init time (snapshot loading + txnlog replay) + */ + public final Summary DB_INIT_TIME; + + /** + * Stats for read request. The timing start from when the server see the + * request until it leave final request processor. + */ + public final Summary READ_LATENCY; + + /** + * Stats for request that need quorum voting. Timing is the same as read + * request. We only keep track of stats for request that originated from + * this machine only. + */ + public final Summary UPDATE_LATENCY; + + /** + * Stats for all quorum request. The timing start from when the leader see + * the request until it reach the learner. + */ + public final Summary PROPAGATION_LATENCY; + + public final Summary FOLLOWER_SYNC_TIME; + + public final Summary ELECTION_TIME; + + public final Counter LOOKING_COUNT; + public final Counter DIFF_COUNT; + public final Counter SNAP_COUNT; + public final Counter COMMIT_COUNT; + public final Counter CONNECTION_REQUEST_COUNT; + + public final Counter REVALIDATE_COUNT; + public final Counter CONNECTION_DROP_COUNT; + public final Counter CONNECTION_REVALIDATE_COUNT; + + // Expiry queue stats + public final Counter SESSIONLESS_CONNECTIONS_EXPIRED; + public final Counter STALE_SESSIONS_EXPIRED; + + // Connection throttling related + public final Summary CONNECTION_TOKEN_DEFICIT; + public final Counter CONNECTION_REJECTED; + + public final Summary INFLIGHT_SNAP_COUNT; + public final Summary INFLIGHT_DIFF_COUNT; + + public final Counter UNRECOVERABLE_ERROR_COUNT; + public final SummarySet WRITE_PER_NAMESPACE; + public final SummarySet READ_PER_NAMESPACE; + public final Counter BYTES_RECEIVED_COUNT; + + public final Summary PREP_PROCESSOR_QUEUE_TIME; + public final Summary PREP_PROCESSOR_QUEUE_SIZE; + public final Counter PREP_PROCESSOR_QUEUED; + public final Counter OUTSTANDING_CHANGES_QUEUED; + public final Counter OUTSTANDING_CHANGES_REMOVED; + public final Summary PREP_PROCESS_TIME; + public final Summary CLOSE_SESSION_PREP_TIME; + + public final Summary PROPOSAL_LATENCY; + public final Summary PROPOSAL_ACK_CREATION_LATENCY; + public final Summary COMMIT_PROPAGATION_LATENCY; + public final Counter LEARNER_PROPOSAL_RECEIVED_COUNT; + public final Counter LEARNER_COMMIT_RECEIVED_COUNT; + + public final Summary STARTUP_TXNS_LOADED; + public final Summary STARTUP_TXNS_LOAD_TIME; + public final Summary STARTUP_SNAP_LOAD_TIME; + + public final Summary SYNC_PROCESSOR_QUEUE_AND_FLUSH_TIME; + public final Summary SYNC_PROCESSOR_QUEUE_SIZE; + public final Counter SYNC_PROCESSOR_QUEUED; + public final Summary SYNC_PROCESSOR_QUEUE_TIME; + public final Summary SYNC_PROCESSOR_FLUSH_TIME; + public final Summary SYNC_PROCESS_TIME; + + public final Summary BATCH_SIZE; + + public final Summary QUORUM_ACK_LATENCY; + public final SummarySet ACK_LATENCY; + public final Counter PROPOSAL_COUNT; + public final Counter QUIT_LEADING_DUE_TO_DISLOYAL_VOTER; + + /** + * Fired watcher stats. + */ + public final Summary NODE_CREATED_WATCHER; + public final Summary NODE_DELETED_WATCHER; + public final Summary NODE_CHANGED_WATCHER; + public final Summary NODE_CHILDREN_WATCHER; + + /* + * Number of dead watchers in DeadWatcherListener + */ + public final Counter ADD_DEAD_WATCHER_STALL_TIME; + public final Counter DEAD_WATCHERS_QUEUED; + public final Counter DEAD_WATCHERS_CLEARED; + public final Summary DEAD_WATCHERS_CLEANER_LATENCY; + + /* + * Response cache hit and miss metrics. + */ + public final Counter RESPONSE_PACKET_CACHE_HITS; + public final Counter RESPONSE_PACKET_CACHE_MISSING; + public final Counter RESPONSE_PACKET_GET_CHILDREN_CACHE_HITS; + public final Counter RESPONSE_PACKET_GET_CHILDREN_CACHE_MISSING; + + /** + * Learner handler quorum packet metrics. + */ + public final SummarySet LEARNER_HANDLER_QP_SIZE; + public final SummarySet LEARNER_HANDLER_QP_TIME; + + /* + * Number of requests that are in the session queue. + */ + public final Summary REQUESTS_IN_SESSION_QUEUE; + public final Summary PENDING_SESSION_QUEUE_SIZE; + /* + * Consecutive number of read requests that are in the session queue right after a commit request. + */ + public final Summary READS_AFTER_WRITE_IN_SESSION_QUEUE; + public final Summary READ_ISSUED_FROM_SESSION_QUEUE; + public final Summary SESSION_QUEUES_DRAINED; + + public final Summary TIME_WAITING_EMPTY_POOL_IN_COMMIT_PROCESSOR_READ; + public final Summary WRITE_BATCH_TIME_IN_COMMIT_PROCESSOR; + + public final Summary CONCURRENT_REQUEST_PROCESSING_IN_COMMIT_PROCESSOR; + + public final Summary READS_QUEUED_IN_COMMIT_PROCESSOR; + public final Summary WRITES_QUEUED_IN_COMMIT_PROCESSOR; + public final Summary COMMITS_QUEUED_IN_COMMIT_PROCESSOR; + public final Counter COMMITS_QUEUED; + public final Summary READS_ISSUED_IN_COMMIT_PROC; + public final Summary WRITES_ISSUED_IN_COMMIT_PROC; + + /** + * Time spent by a read request in the commit processor. + */ + public final Summary READ_COMMITPROC_TIME; + + /** + * Time spent by a write request in the commit processor. + */ + public final Summary WRITE_COMMITPROC_TIME; + + /** + * Time spent by a committed request, for a locally issued write, in the + * commit processor. + */ + public final Summary LOCAL_WRITE_COMMITTED_TIME; + + /** + * Time spent by a committed request for a write, issued by other server, in the + * commit processor. + */ + public final Summary SERVER_WRITE_COMMITTED_TIME; + + public final Summary COMMIT_PROCESS_TIME; + + /** + * Observer Master processing metrics. + */ + public final Summary OM_PROPOSAL_PROCESS_TIME; + public final Summary OM_COMMIT_PROCESS_TIME; + + /** + * Time spent by the final processor. This is tracked in the commit processor. + */ + public final Summary READ_FINAL_PROC_TIME; + public final Summary WRITE_FINAL_PROC_TIME; + + /* + * Number of successful matches of expected ensemble name in EnsembleAuthenticationProvider. + */ + public final Counter ENSEMBLE_AUTH_SUCCESS; + + /* + * Number of unsuccessful matches of expected ensemble name in EnsembleAuthenticationProvider. + */ + public final Counter ENSEMBLE_AUTH_FAIL; + + /* + * Number of client auth requests with no ensemble set in EnsembleAuthenticationProvider. + */ + public final Counter ENSEMBLE_AUTH_SKIP; + + public final Counter STALE_REQUESTS; + public final Counter STALE_REQUESTS_DROPPED; + public final Counter STALE_REPLIES; + public final Counter REQUEST_THROTTLE_WAIT_COUNT; + public final Counter LARGE_REQUESTS_REJECTED; + + public final Summary NETTY_QUEUED_BUFFER; + + // Total number of digest mismatches that are observed when applying + // txns to data tree. + public final Counter DIGEST_MISMATCHES_COUNT; + + public final Counter TLS_HANDSHAKE_EXCEEDED; + + public final Summary JVM_PAUSE_TIME; + + private final MetricsProvider metricsProvider; + + public void resetAll() { + metricsProvider.resetAllValues(); + } + + public MetricsProvider getMetricsProvider() { + return metricsProvider; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerStats.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerStats.java new file mode 100644 index 0000000..5c1a4e7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ServerStats.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.metric.AvgMinMaxCounter; +import org.apache.zookeeper.server.quorum.BufferStats; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Basic Server Statistics + */ +public class ServerStats { + + private static final Logger LOG = LoggerFactory.getLogger(ServerStats.class); + + private final AtomicLong packetsSent = new AtomicLong(); + private final AtomicLong packetsReceived = new AtomicLong(); + + private final AvgMinMaxCounter requestLatency = new AvgMinMaxCounter("request_latency"); + + private final AtomicLong fsyncThresholdExceedCount = new AtomicLong(0); + + private final BufferStats clientResponseStats = new BufferStats(); + + private AtomicLong nonMTLSRemoteConnCntr = new AtomicLong(0); + + private AtomicLong nonMTLSLocalConnCntr = new AtomicLong(0); + + private AtomicLong authFailedCntr = new AtomicLong(0); + + private final Provider provider; + private final long startTime = Time.currentElapsedTime(); + + public interface Provider { + + long getOutstandingRequests(); + long getLastProcessedZxid(); + String getState(); + int getNumAliveConnections(); + long getDataDirSize(); + long getLogDirSize(); + + } + + public ServerStats(Provider provider) { + this.provider = provider; + } + + // getters + public long getMinLatency() { + return requestLatency.getMin(); + } + + public double getAvgLatency() { + return requestLatency.getAvg(); + } + + public long getMaxLatency() { + return requestLatency.getMax(); + } + + public long getOutstandingRequests() { + return provider.getOutstandingRequests(); + } + + public long getLastProcessedZxid() { + return provider.getLastProcessedZxid(); + } + + public long getDataDirSize() { + return provider.getDataDirSize(); + } + + public long getLogDirSize() { + return provider.getLogDirSize(); + } + + public long getPacketsReceived() { + return packetsReceived.get(); + } + + public long getPacketsSent() { + return packetsSent.get(); + } + + public String getServerState() { + return provider.getState(); + } + + /** The number of client connections alive to this server */ + public int getNumAliveClientConnections() { + return provider.getNumAliveConnections(); + } + + public long getUptime() { + return Time.currentElapsedTime() - startTime; + } + + public boolean isProviderNull() { + return provider == null; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Latency min/avg/max: " + getMinLatency() + "/" + getAvgLatency() + "/" + getMaxLatency() + "\n"); + sb.append("Received: " + getPacketsReceived() + "\n"); + sb.append("Sent: " + getPacketsSent() + "\n"); + sb.append("Connections: " + getNumAliveClientConnections() + "\n"); + + if (provider != null) { + sb.append("Outstanding: " + getOutstandingRequests() + "\n"); + sb.append("Zxid: 0x" + Long.toHexString(getLastProcessedZxid()) + "\n"); + } + sb.append("Mode: " + getServerState() + "\n"); + return sb.toString(); + } + + /** + * Update request statistic. This should only be called from a request + * that originated from that machine. + */ + public void updateLatency(Request request, long currentTime) { + long latency = currentTime - request.createTime; + if (latency < 0) { + return; + } + requestLatency.addDataPoint(latency); + if (request.getHdr() != null) { + // Only quorum request should have header + ServerMetrics.getMetrics().UPDATE_LATENCY.add(latency); + } else { + // All read request should goes here + ServerMetrics.getMetrics().READ_LATENCY.add(latency); + } + } + + public void resetLatency() { + requestLatency.reset(); + } + + public void resetMaxLatency() { + requestLatency.resetMax(); + } + + public void incrementPacketsReceived() { + packetsReceived.incrementAndGet(); + } + + public void incrementPacketsSent() { + packetsSent.incrementAndGet(); + } + + public void resetRequestCounters() { + packetsReceived.set(0); + packetsSent.set(0); + } + + public long getFsyncThresholdExceedCount() { + return fsyncThresholdExceedCount.get(); + } + + public void incrementFsyncThresholdExceedCount() { + fsyncThresholdExceedCount.incrementAndGet(); + } + + public void resetFsyncThresholdExceedCount() { + fsyncThresholdExceedCount.set(0); + } + + public long getNonMTLSLocalConnCount() { + return nonMTLSLocalConnCntr.get(); + } + + public void incrementNonMTLSLocalConnCount() { + nonMTLSLocalConnCntr.incrementAndGet(); + } + + public void resetNonMTLSLocalConnCount() { + nonMTLSLocalConnCntr.set(0); + } + + public long getNonMTLSRemoteConnCount() { + return nonMTLSRemoteConnCntr.get(); + } + + public void incrementNonMTLSRemoteConnCount() { + nonMTLSRemoteConnCntr.incrementAndGet(); + } + + public void resetNonMTLSRemoteConnCount() { + nonMTLSRemoteConnCntr.set(0); + } + + public long getAuthFailedCount() { + return authFailedCntr.get(); + } + + public void incrementAuthFailedCount() { + authFailedCntr.incrementAndGet(); + } + + public void resetAuthFailedCount() { + authFailedCntr.set(0); + } + + public void reset() { + resetLatency(); + resetRequestCounters(); + clientResponseStats.reset(); + ServerMetrics.getMetrics().resetAll(); + } + + public void updateClientResponseSize(int size) { + clientResponseStats.setLastBufferSize(size); + } + + public BufferStats getClientResponseStats() { + return clientResponseStats; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTracker.java new file mode 100644 index 0000000..d3bcc45 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTracker.java @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.PrintWriter; +import java.util.Map; +import java.util.Set; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.SessionExpiredException; + +/** + * This is the basic interface that ZooKeeperServer uses to track sessions. The + * standalone and leader ZooKeeperServer use the same SessionTracker. The + * FollowerZooKeeperServer uses a SessionTracker which is basically a simple + * shell to track information to be forwarded to the leader. + */ +public interface SessionTracker { + + interface Session { + + long getSessionId(); + + int getTimeout(); + + boolean isClosing(); + + } + + interface SessionExpirer { + + void expire(Session session); + + long getServerId(); + + } + + long createSession(int sessionTimeout); + + /** + * Track the session expire, not add to ZkDb. + * + * @param id sessionId + * @param to sessionTimeout + * @return whether the session was newly tracked (if false, already tracked) + */ + boolean trackSession(long id, int to); + + /** + * Add the session to the local session map or global one in zkDB. + * + * @param id sessionId + * @param to sessionTimeout + * @return whether the session was newly added (if false, already existed) + */ + boolean commitSession(long id, int to); + + /** + * @param sessionId + * @param sessionTimeout + * @return false if session is no longer active + */ + boolean touchSession(long sessionId, int sessionTimeout); + + /** + * Mark that the session is in the process of closing. + * + * @param sessionId + */ + void setSessionClosing(long sessionId); + + /** + * + */ + void shutdown(); + + /** + * @param sessionId + */ + void removeSession(long sessionId); + + /** + * @param sessionId + * @return whether or not the SessionTracker is aware of this session + */ + boolean isTrackingSession(long sessionId); + + /** + * Checks whether the SessionTracker is aware of this session, the session + * is still active, and the owner matches. If the owner wasn't previously + * set, this sets the owner of the session. + * + * UnknownSessionException should never been thrown to the client. It is + * only used internally to deal with possible local session from other + * machine + * + * @param sessionId + * @param owner + */ + void checkSession(long sessionId, + Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException, KeeperException.UnknownSessionException; + + /** + * Strictly check that a given session is a global session or not + * + * @param sessionId + * @param owner + * @throws KeeperException.SessionExpiredException + * @throws KeeperException.SessionMovedException + */ + void checkGlobalSession(long sessionId, + Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException; + + void setOwner(long id, Object owner) throws SessionExpiredException; + + /** + * Text dump of session information, suitable for debugging. + * + * @param pwriter the output writer + */ + void dumpSessions(PrintWriter pwriter); + + /** + * Returns a mapping of time to session IDs that expire at that time. + */ + Map> getSessionExpiryMap(); + + /** + * If this session tracker supports local sessions, return how many. + * otherwise returns 0; + */ + long getLocalSessionCount(); + + boolean isLocalSessionsEnabled(); +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTrackerImpl.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTrackerImpl.java new file mode 100644 index 0000000..2fa8ab2 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SessionTrackerImpl.java @@ -0,0 +1,372 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.MessageFormat; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a full featured SessionTracker. It tracks session in grouped by tick + * interval. It always rounds up the tick interval to provide a sort of grace + * period. Sessions are thus expired in batches made up of sessions that expire + * in a given interval. + */ +public class SessionTrackerImpl extends ZooKeeperCriticalThread implements SessionTracker { + + private static final Logger LOG = LoggerFactory.getLogger(SessionTrackerImpl.class); + + // TODO_MA 注释: 存储 session 信息,key 是 sessionID, value 是 Session 对象 + protected final ConcurrentHashMap sessionsById = new ConcurrentHashMap(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 内部核心是一个类似于时间轮的设计 + * private final ConcurrentHashMap> expiryMap = new ConcurrentHashMap>(); + * 此时,这个 E = SessionImpl + * 注意这个数据结构,这是理解 Session 和 Connection 超时管理的关键,是一个类似于 时间轮 的数据结构设计 + * 将所有要进行管理的 Session 或者 Connection 加入到不同时刻对应的某个队列中,ZK 只会每隔一段时间,针对一个队列,进行全部过期操作处理 + */ + private final ExpiryQueue sessionExpiryQueue; + + // TODO_MA 注释: 存储 session 的超时时间信息,key 是 sessionID, value 是 超时时间 + private final ConcurrentMap sessionsWithTimeout; + private final AtomicLong nextSessionId = new AtomicLong(); + + public static class SessionImpl implements Session { + + SessionImpl(long sessionId, int timeout) { + this.sessionId = sessionId; + this.timeout = timeout; + isClosing = false; + } + + final long sessionId; + final int timeout; + boolean isClosing; + + Object owner; + + public long getSessionId() { + return sessionId; + } + + public int getTimeout() { + return timeout; + } + + public boolean isClosing() { + return isClosing; + } + + public String toString() { + return "0x" + Long.toHexString(sessionId); + } + + } + + /** + * Generates an initial sessionId. High order 1 byte is serverId, next + * 5 bytes are from timestamp, and low order 2 bytes are 0s. + * Use ">>> 8", not ">> 8" to make sure that the high order 1 byte is entirely up to the server Id(@see ZOOKEEPER-1622). + * + * @param id server Id + * @return the Session Id + */ + public static long initializeNextSessionId(long id) { + long nextSid; + nextSid = (Time.currentElapsedTime() << 24) >>> 8; + nextSid = nextSid | (id << 56); + if(nextSid == EphemeralType.CONTAINER_EPHEMERAL_OWNER) { + ++nextSid; // this is an unlikely edge case, but check it just in case + } + return nextSid; + } + + private final SessionExpirer expirer; + + public SessionTrackerImpl(SessionExpirer expirer, ConcurrentMap sessionsWithTimeout, int tickTime, + long serverId, ZooKeeperServerListener listener) { + super("SessionTracker", listener); + this.expirer = expirer; + this.sessionExpiryQueue = new ExpiryQueue(tickTime); + this.sessionsWithTimeout = sessionsWithTimeout; + this.nextSessionId.set(initializeNextSessionId(serverId)); + for(Entry e : sessionsWithTimeout.entrySet()) { + trackSession(e.getKey(), e.getValue()); + } + + EphemeralType.validateServerId(serverId); + } + + volatile boolean running = true; + + public void dumpSessions(PrintWriter pwriter) { + pwriter.print("Session "); + sessionExpiryQueue.dump(pwriter); + } + + /** + * Returns a mapping from time to session IDs of sessions expiring at that time. + */ + public synchronized Map> getSessionExpiryMap() { + // Convert time -> sessions map to time -> session IDs map + Map> expiryMap = sessionExpiryQueue.getExpiryMap(); + Map> sessionExpiryMap = new TreeMap>(); + for(Entry> e : expiryMap.entrySet()) { + Set ids = new HashSet(); + sessionExpiryMap.put(e.getKey(), ids); + for(SessionImpl s : e.getValue()) { + ids.add(s.sessionId); + } + } + return sessionExpiryMap; + } + + @Override + public String toString() { + StringWriter sw = new StringWriter(); + PrintWriter pwriter = new PrintWriter(sw); + dumpSessions(pwriter); + pwriter.flush(); + pwriter.close(); + return sw.toString(); + } + + @Override + public void run() { + try { + while(running) { + + // TODO_MA 注释: 获取下一个队列的超时时间,等待 + long waitTime = sessionExpiryQueue.getWaitTime(); + if(waitTime > 0) { + Thread.sleep(waitTime); + continue; + } + + // TODO_MA 注释: 执行 expire + for(SessionImpl s : sessionExpiryQueue.poll()) { + ServerMetrics.getMetrics().STALE_SESSIONS_EXPIRED.add(1); + setSessionClosing(s.sessionId); + expirer.expire(s); + } + } + } catch(InterruptedException e) { + handleException(this.getName(), e); + } + LOG.info("SessionTrackerImpl exited loop!"); + } + + public synchronized boolean touchSession(long sessionId, int timeout) { + + // TODO_MA 注释: 根据 SessionID 获取 Session + SessionImpl s = sessionsById.get(sessionId); + + if(s == null) { + logTraceTouchInvalidSession(sessionId, timeout); + return false; + } + + if(s.isClosing()) { + logTraceTouchClosingSession(sessionId, timeout); + return false; + } + + // TODO_MA 注释: 更新该 Session 的超时信息 + updateSessionExpiry(s, timeout); + return true; + } + + private void updateSessionExpiry(SessionImpl s, int timeout) { + logTraceTouchSession(s.sessionId, timeout, ""); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + sessionExpiryQueue.update(s, timeout); + } + + private void logTraceTouchSession(long sessionId, int timeout, String sessionStatus) { + if(LOG.isTraceEnabled()) { + String msg = MessageFormat + .format("SessionTrackerImpl --- Touch {0}session: 0x{1} with timeout {2}", sessionStatus, + Long.toHexString(sessionId), Integer.toString(timeout)); + + ZooTrace.logTraceMessage(LOG, ZooTrace.CLIENT_PING_TRACE_MASK, msg); + } + } + + private void logTraceTouchInvalidSession(long sessionId, int timeout) { + logTraceTouchSession(sessionId, timeout, "invalid "); + } + + private void logTraceTouchClosingSession(long sessionId, int timeout) { + logTraceTouchSession(sessionId, timeout, "closing "); + } + + public int getSessionTimeout(long sessionId) { + return sessionsWithTimeout.get(sessionId); + } + + public synchronized void setSessionClosing(long sessionId) { + if(LOG.isTraceEnabled()) { + LOG.trace("Session closing: 0x{}", Long.toHexString(sessionId)); + } + + SessionImpl s = sessionsById.get(sessionId); + if(s == null) { + return; + } + s.isClosing = true; + } + + public synchronized void removeSession(long sessionId) { + LOG.debug("Removing session 0x{}", Long.toHexString(sessionId)); + SessionImpl s = sessionsById.remove(sessionId); + sessionsWithTimeout.remove(sessionId); + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "SessionTrackerImpl --- Removing session 0x" + Long.toHexString(sessionId)); + } + if(s != null) { + sessionExpiryQueue.remove(s); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + + running = false; + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.getTextTraceLevel(), "Shutdown SessionTrackerImpl!"); + } + } + + public long createSession(int sessionTimeout) { + + // TODO_MA 注释: 获取一个 sessionID + long sessionId = nextSessionId.getAndIncrement(); + trackSession(sessionId, sessionTimeout); + return sessionId; + } + + @Override + public synchronized boolean trackSession(long id, int sessionTimeout) { + boolean added = false; + + SessionImpl session = sessionsById.get(id); + if(session == null) { + session = new SessionImpl(id, sessionTimeout); + } + + // findbugs2.0.3 complains about get after put. + // long term strategy would be use computeIfAbsent after JDK 1.8 + SessionImpl existedSession = sessionsById.putIfAbsent(id, session); + + if(existedSession != null) { + session = existedSession; + } else { + added = true; + LOG.debug("Adding session 0x{}", Long.toHexString(id)); + } + + if(LOG.isTraceEnabled()) { + String actionStr = added ? "Adding" : "Existing"; + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "SessionTrackerImpl --- " + actionStr + " session 0x" + Long.toHexString(id) + " " + sessionTimeout); + } + + updateSessionExpiry(session, sessionTimeout); + return added; + } + + public synchronized boolean commitSession(long id, int sessionTimeout) { + return sessionsWithTimeout.put(id, sessionTimeout) == null; + } + + public boolean isTrackingSession(long sessionId) { + return sessionsById.containsKey(sessionId); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 检查 session + */ + public synchronized void checkSession(long sessionId, + Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException, KeeperException.UnknownSessionException { + LOG.debug("Checking session 0x{}", Long.toHexString(sessionId)); + SessionImpl session = sessionsById.get(sessionId); + + if(session == null) { + throw new KeeperException.UnknownSessionException(); + } + + if(session.isClosing()) { + throw new KeeperException.SessionExpiredException(); + } + + if(session.owner == null) { + session.owner = owner; + } else if(session.owner != owner) { + throw new KeeperException.SessionMovedException(); + } + } + + public synchronized void setOwner(long id, Object owner) throws SessionExpiredException { + SessionImpl session = sessionsById.get(id); + if(session == null || session.isClosing()) { + throw new KeeperException.SessionExpiredException(); + } + session.owner = owner; + } + + public void checkGlobalSession(long sessionId, + Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException { + try { + checkSession(sessionId, owner); + } catch(KeeperException.UnknownSessionException e) { + throw new KeeperException.SessionExpiredException(); + } + } + + public long getLocalSessionCount() { + return 0; + } + + @Override + public boolean isLocalSessionsEnabled() { + return false; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java new file mode 100644 index 0000000..5fce206 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SnapshotFormatter.java @@ -0,0 +1,234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.server.persistence.FileSnap.SNAPSHOT_FILE_PREFIX; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Base64; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.server.persistence.FileSnap; +import org.apache.zookeeper.server.persistence.SnapStream; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.util.ServiceUtils; +import org.json.simple.JSONValue; + +/** + * Dump a snapshot file to stdout. + * + * For JSON format, followed https://dev.yorhel.nl/ncdu/jsonfmt + */ +@InterfaceAudience.Public +public class SnapshotFormatter { + + // per-znode counter so ncdu treats each as a unique object + private static Integer INODE_IDX = 1000; + + /** + * USAGE: SnapshotFormatter snapshot_file or the ready-made script: zkSnapShotToolkit.sh + */ + public static void main(String[] args) throws Exception { + String snapshotFile = null; + boolean dumpData = false; + boolean dumpJson = false; + + int i; + for (i = 0; i < args.length; i++) { + if (args[i].equals("-d")) { + dumpData = true; + } else if (args[i].equals("-json")) { + dumpJson = true; + } else { + snapshotFile = args[i]; + i++; + break; + } + } + if (args.length != i || snapshotFile == null) { + System.err.println("USAGE: SnapshotFormatter [-d|-json] snapshot_file"); + System.err.println(" -d dump the data for each znode"); + System.err.println(" -json dump znode info in json format"); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + return; + } + + String error = ZKUtil.validateFileInput(snapshotFile); + if (null != error) { + System.err.println(error); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + + if (dumpData && dumpJson) { + System.err.println("Cannot specify both data dump (-d) and json mode (-json) in same call"); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + + new SnapshotFormatter().run(snapshotFile, dumpData, dumpJson); + } + + public void run(String snapshotFileName, boolean dumpData, boolean dumpJson) throws IOException { + File snapshotFile = new File(snapshotFileName); + try (InputStream is = SnapStream.getInputStream(snapshotFile)) { + InputArchive ia = BinaryInputArchive.getArchive(is); + + FileSnap fileSnap = new FileSnap(null); + + DataTree dataTree = new DataTree(); + Map sessions = new HashMap(); + + fileSnap.deserialize(dataTree, sessions, ia); + long fileNameZxid = Util.getZxidFromName(snapshotFile.getName(), SNAPSHOT_FILE_PREFIX); + + if (dumpJson) { + printSnapshotJson(dataTree); + } else { + printDetails(dataTree, sessions, dumpData, fileNameZxid); + } + } + } + + private void printDetails(DataTree dataTree, Map sessions, boolean dumpData, long fileNameZxid) { + long dtZxid = printZnodeDetails(dataTree, dumpData); + printSessionDetails(dataTree, sessions); + DataTree.ZxidDigest targetZxidDigest = dataTree.getDigestFromLoadedSnapshot(); + if (targetZxidDigest != null) { + System.out.println(String.format("Target zxid digest is: %s, %s", + Long.toHexString(targetZxidDigest.zxid), targetZxidDigest.digest)); + } + System.out.println(String.format("----%nLast zxid: 0x%s", Long.toHexString(Math.max(fileNameZxid, dtZxid)))); + } + + private long printZnodeDetails(DataTree dataTree, boolean dumpData) { + System.out.println(String.format("ZNode Details (count=%d):", dataTree.getNodeCount())); + + final long zxid = printZnode(dataTree, "/", dumpData); + System.out.println("----"); + return zxid; + } + + private long printZnode(DataTree dataTree, String name, boolean dumpData) { + System.out.println("----"); + DataNode n = dataTree.getNode(name); + Set children; + long zxid; + synchronized (n) { // keep findbugs happy + System.out.println(name); + printStat(n.stat); + zxid = Math.max(n.stat.getMzxid(), n.stat.getPzxid()); + if (dumpData) { + System.out.println(" data = " + (n.data == null ? "" : Base64.getEncoder().encodeToString(n.data))); + } else { + System.out.println(" dataLength = " + (n.data == null ? 0 : n.data.length)); + } + children = n.getChildren(); + } + if (children != null) { + for (String child : children) { + long cxid = printZnode(dataTree, name + (name.equals("/") ? "" : "/") + child, dumpData); + zxid = Math.max(zxid, cxid); + } + } + return zxid; + } + + private void printSessionDetails(DataTree dataTree, Map sessions) { + System.out.println("Session Details (sid, timeout, ephemeralCount):"); + for (Map.Entry e : sessions.entrySet()) { + long sid = e.getKey(); + System.out.println(String.format("%#016x, %d, %d", sid, e.getValue(), dataTree.getEphemerals(sid).size())); + } + } + + private void printStat(StatPersisted stat) { + printHex("cZxid", stat.getCzxid()); + System.out.println(" ctime = " + new Date(stat.getCtime()).toString()); + printHex("mZxid", stat.getMzxid()); + System.out.println(" mtime = " + new Date(stat.getMtime()).toString()); + printHex("pZxid", stat.getPzxid()); + System.out.println(" cversion = " + stat.getCversion()); + System.out.println(" dataVersion = " + stat.getVersion()); + System.out.println(" aclVersion = " + stat.getAversion()); + printHex("ephemeralOwner", stat.getEphemeralOwner()); + } + + private void printHex(String prefix, long value) { + System.out.println(String.format(" %s = %#016x", prefix, value)); + } + + private void printSnapshotJson(final DataTree dataTree) { + System.out.printf( + "[1,0,{\"progname\":\"SnapshotFormatter.java\",\"progver\":\"0.01\",\"timestamp\":%d}", + System.currentTimeMillis()); + printZnodeJson(dataTree, "/"); + System.out.print("]"); + } + + private void printZnodeJson(final DataTree dataTree, final String fullPath) { + + final DataNode n = dataTree.getNode(fullPath); + + if (null == n) { + System.err.println("DataTree Node for " + fullPath + " doesn't exist"); + return; + } + + final String name = fullPath.equals("/") + ? fullPath + : fullPath.substring(fullPath.lastIndexOf("/") + 1); + + System.out.print(","); + + int dataLen; + synchronized (n) { // keep findbugs happy + dataLen = (n.data == null) ? 0 : n.data.length; + } + StringBuilder nodeSB = new StringBuilder(); + nodeSB.append("{"); + nodeSB.append("\"name\":\"").append(JSONValue.escape(name)).append("\"").append(","); + nodeSB.append("\"asize\":").append(dataLen).append(","); + nodeSB.append("\"dsize\":").append(dataLen).append(","); + nodeSB.append("\"dev\":").append(0).append(","); + nodeSB.append("\"ino\":").append(++INODE_IDX); + nodeSB.append("}"); + + Set children; + synchronized (n) { // keep findbugs happy + children = n.getChildren(); + } + if (children != null && children.size() > 0) { + System.out.print("[" + nodeSB); + for (String child : children) { + printZnodeJson(dataTree, fullPath + (fullPath.equals("/") ? "" : "/") + child); + } + System.out.print("]"); + } else { + System.out.print(nodeSB); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/Stats.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/Stats.java new file mode 100644 index 0000000..aaeee35 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/Stats.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.Date; + +/** + * Statistics on the ServerCnxn + */ +interface Stats { + + /** Date/time the connection was established + * @since 3.3.0 */ + Date getEstablished(); + + /** + * The number of requests that have been submitted but not yet + * responded to. + */ + long getOutstandingRequests(); + /** Number of packets received */ + long getPacketsReceived(); + /** Number of packets sent (incl notifications) */ + long getPacketsSent(); + /** Min latency in ms + * @since 3.3.0 */ + long getMinLatency(); + /** Average latency in ms + * @since 3.3.0 */ + long getAvgLatency(); + /** Max latency in ms + * @since 3.3.0 */ + long getMaxLatency(); + /** Last operation performed by this connection + * @since 3.3.0 */ + String getLastOperation(); + /** Last cxid of this connection + * @since 3.3.0 */ + long getLastCxid(); + /** Last zxid of this connection + * @since 3.3.0 */ + long getLastZxid(); + /** Last time server sent a response to client on this connection + * @since 3.3.0 */ + long getLastResponseTime(); + /** Latency of last response to client on this connection in ms + * @since 3.3.0 */ + long getLastLatency(); + + /** Reset counters + * @since 3.3.0 */ + void resetStats(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/SyncRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SyncRequestProcessor.java new file mode 100644 index 0000000..71c96e1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/SyncRequestProcessor.java @@ -0,0 +1,318 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.Flushable; +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * // TODO_MA 注释: 此 RequestProcessor 将请求记录到磁盘。它对请求进行批处理以有效地执行 io。 + * This RequestProcessor logs requests to disk. It batches the requests to do the io efficiently. + * + * // TODO_MA 注释: 在其日志同步到磁盘之前,请求不会传递到下一个 RequestProcessor。 + * The request is not passed to the next RequestProcessor until its log has been synced to disk. + * + * // TODO_MA 注释: SyncRequestProcessor 用于 3 种不同的情况 + * SyncRequestProcessor is used in 3 different cases + * + * // TODO_MA 注释: 将请求同步到磁盘并将其转​​发给 AckRequestProcessor,后者将 ack 发送回自身。 + * 1. Leader - Sync request to disk and forward it to AckRequestProcessor which send ack back to itself. + * + * // TODO_MA 注释: 将请求同步到磁盘并将请求转发到 SendAckRequestProcessor,后者将数据包发送给领导者。 + * 2. Follower - Sync request to disk and forward request to SendAckRequestProcessor which send the packets to leader. + * // TODO_MA 注释: SendAckRequestProcessor 是可刷新的,它允许我们强制将数据包推送到领导者。 + * SendAckRequestProcessor is flushable which allow us to force push packets to leader. + * + * // TODO_MA 注释: 将提交的请求同步到磁盘(作为 INFORM 数据包接收)。 + * 3. Observer - Sync committed request to disk (received as INFORM packet). + * // TODO_MA 注释: 它永远不会将 ack 发送回领导者,因此 nextProcessor 将为空。 + * It never send ack back to the leader, so the nextProcessor will be null. + * // TODO_MA 注释: 因为它只包含提交的txns,所以这改变了观察者上txnlog的语义。 + * This change the semantic of txnlog on the observer since it only contains committed txns. + */ +public class SyncRequestProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(SyncRequestProcessor.class); + + private static final Request REQUEST_OF_DEATH = Request.requestOfDeath; + + /** The number of log entries to log before starting a snapshot */ + private static int snapCount = ZooKeeperServer.getSnapCount(); + + /** + * The total size of log entries before starting a snapshot + */ + private static long snapSizeInBytes = ZooKeeperServer.getSnapSizeInBytes(); + + /** + * Random numbers used to vary snapshot timing + */ + private int randRoll; + private long randSize; + + // TODO_MA 注释: 队列 + private final BlockingQueue queuedRequests = new LinkedBlockingQueue(); + + private final Semaphore snapThreadMutex = new Semaphore(1); + + private final ZooKeeperServer zks; + + // TODO_MA 注释: 下一个 RP: SendAckRequestProcessor + private final RequestProcessor nextProcessor; + + /** + * Transactions that have been written and are waiting to be flushed to + * disk. Basically this is the list of SyncItems whose callbacks will be + * invoked after flush returns successfully. + */ + private final Queue toFlush; + private long lastFlushTime; + + public SyncRequestProcessor(ZooKeeperServer zks, RequestProcessor nextProcessor) { + super("SyncThread:" + zks.getServerId(), zks.getZooKeeperServerListener()); + this.zks = zks; + this.nextProcessor = nextProcessor; + this.toFlush = new ArrayDeque<>(zks.getMaxBatchSize()); + } + + /** + * used by tests to check for changing + * snapcounts + * @param count + */ + public static void setSnapCount(int count) { + snapCount = count; + } + + /** + * used by tests to get the snapcount + * @return the snapcount + */ + public static int getSnapCount() { + return snapCount; + } + + private long getRemainingDelay() { + long flushDelay = zks.getFlushDelay(); + long duration = Time.currentElapsedTime() - lastFlushTime; + if (duration < flushDelay) { + return flushDelay - duration; + } + return 0; + } + + /** If both flushDelay and maxMaxBatchSize are set (bigger than 0), flush + * whenever either condition is hit. If only one or the other is + * set, flush only when the relevant condition is hit. + */ + private boolean shouldFlush() { + long flushDelay = zks.getFlushDelay(); + long maxBatchSize = zks.getMaxBatchSize(); + if ((flushDelay > 0) && (getRemainingDelay() == 0)) { + return true; + } + return (maxBatchSize > 0) && (toFlush.size() >= maxBatchSize); + } + + /** + * used by tests to check for changing + * snapcounts + * @param size + */ + public static void setSnapSizeInBytes(long size) { + snapSizeInBytes = size; + } + + private boolean shouldSnapshot() { + + // TODO_MA 注释: 这是规矩 + int logCount = zks.getZKDatabase().getTxnCount(); + long logSize = zks.getZKDatabase().getTxnSize(); + return (logCount > (snapCount / 2 + randRoll)) + || (snapSizeInBytes > 0 && logSize > (snapSizeInBytes / 2 + randSize)); + } + + private void resetSnapshotStats() { + randRoll = ThreadLocalRandom.current().nextInt(snapCount / 2); + randSize = Math.abs(ThreadLocalRandom.current().nextLong() % (snapSizeInBytes / 2)); + } + + @Override + public void run() { + try { + // we do this in an attempt to ensure that not all of the servers + // in the ensemble take a snapshot at the same time + resetSnapshotStats(); + lastFlushTime = Time.currentElapsedTime(); + + // TODO_MA 注释: 循环 + while (true) { + ServerMetrics.getMetrics().SYNC_PROCESSOR_QUEUE_SIZE.add(queuedRequests.size()); + + long pollTime = Math.min(zks.getMaxWriteQueuePollTime(), getRemainingDelay()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 获取请求 + */ + Request si = queuedRequests.poll(pollTime, TimeUnit.MILLISECONDS); + if (si == null) { + /* We timed out looking for more writes to batch, go ahead and flush immediately */ + flush(); + si = queuedRequests.take(); + } + + if (si == REQUEST_OF_DEATH) { + break; + } + + long startProcessTime = Time.currentElapsedTime(); + ServerMetrics.getMetrics().SYNC_PROCESSOR_QUEUE_TIME.add(startProcessTime - si.syncQueueStartTime); + + // TODO_MA 注释: 记录日志 + // track the number of records written to the log + if (zks.getZKDatabase().append(si)) { + + // TODO_MA 注释: 判断是否需要进行拍摄快照 + if (shouldSnapshot()) { + resetSnapshotStats(); + // roll the log + zks.getZKDatabase().rollLog(); + + // TODO_MA 注释: 如果能立即拍摄快照则立即拍摄,否则启动一个线程来完成快照拍摄 + // take a snapshot + if (!snapThreadMutex.tryAcquire()) { + LOG.warn("Too busy to snap, skipping"); + } else { + new ZooKeeperThread("Snapshot Thread") { + public void run() { + try { + zks.takeSnapshot(); + } catch (Exception e) { + LOG.warn("Unexpected exception", e); + } finally { + snapThreadMutex.release(); + } + } + }.start(); + } + } + } else if (toFlush.isEmpty()) { + // optimization for read heavy workloads + // iff this is a read, and there are no pending + // flushes (writes), then just pass this to the next processor + if (nextProcessor != null) { + nextProcessor.processRequest(si); + if (nextProcessor instanceof Flushable) { + ((Flushable) nextProcessor).flush(); + } + } + continue; + } + + // TODO_MA 注释: Request 加入 flush 队列 + toFlush.add(si); + if (shouldFlush()) { + flush(); + } + ServerMetrics.getMetrics().SYNC_PROCESS_TIME.add(Time.currentElapsedTime() - startProcessTime); + } + } catch (Throwable t) { + handleException(this.getName(), t); + } + LOG.info("SyncRequestProcessor exited!"); + } + + private void flush() throws IOException, RequestProcessorException { + if (this.toFlush.isEmpty()) { + return; + } + + ServerMetrics.getMetrics().BATCH_SIZE.add(toFlush.size()); + + long flushStartTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 提交 + */ + zks.getZKDatabase().commit(); + + ServerMetrics.getMetrics().SYNC_PROCESSOR_FLUSH_TIME.add(Time.currentElapsedTime() - flushStartTime); + + if (this.nextProcessor == null) { + this.toFlush.clear(); + } else { + while (!this.toFlush.isEmpty()) { + final Request i = this.toFlush.remove(); + long latency = Time.currentElapsedTime() - i.syncQueueStartTime; + ServerMetrics.getMetrics().SYNC_PROCESSOR_QUEUE_AND_FLUSH_TIME.add(latency); + this.nextProcessor.processRequest(i); + } + if (this.nextProcessor instanceof Flushable) { + ((Flushable) this.nextProcessor).flush(); + } + lastFlushTime = Time.currentElapsedTime(); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + queuedRequests.add(REQUEST_OF_DEATH); + try { + this.join(); + this.flush(); + } catch (InterruptedException e) { + LOG.warn("Interrupted while wating for {} to finish", this); + Thread.currentThread().interrupt(); + } catch (IOException e) { + LOG.warn("Got IO exception during shutdown"); + } catch (RequestProcessorException e) { + LOG.warn("Got request processor exception during shutdown"); + } + if (nextProcessor != null) { + nextProcessor.shutdown(); + } + } + + public void processRequest(final Request request) { + Objects.requireNonNull(request, "Request cannot be null"); + + request.syncQueueStartTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + queuedRequests.add(request); + ServerMetrics.getMetrics().SYNC_PROCESSOR_QUEUED.add(1); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/TraceFormatter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/TraceFormatter.java new file mode 100644 index 0000000..d1fd989 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/TraceFormatter.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.text.DateFormat; +import java.util.Date; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.util.ServiceUtils; + +public class TraceFormatter { + + /** + * @param args + * @throws IOException + */ + public static void main(String[] args) throws IOException { + if (args.length != 1) { + System.err.println("USAGE: TraceFormatter trace_file"); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } + FileChannel fc = new FileInputStream(args[0]).getChannel(); + while (true) { + ByteBuffer bb = ByteBuffer.allocate(41); + fc.read(bb); + bb.flip(); + + byte app = bb.get(); + long time = bb.getLong(); + long id = bb.getLong(); + int cxid = bb.getInt(); + long zxid = bb.getLong(); + int txnType = bb.getInt(); + int type = bb.getInt(); + int len = bb.getInt(); + bb = ByteBuffer.allocate(len); + fc.read(bb); + bb.flip(); + String path = "n/a"; + if (bb.remaining() > 0) { + if (type != OpCode.createSession) { + int pathLen = bb.getInt(); + byte[] b = new byte[pathLen]; + bb.get(b); + path = new String(b); + } + } + System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, + DateFormat.LONG).format(new Date(time)) + + ": " + + (char) app + + " id=0x" + + Long.toHexString(id) + + " cxid=" + + cxid + + " op=" + + Request.op2String(type) + + " zxid=0x" + + Long.toHexString(zxid) + + " txnType=" + + txnType + + " len=" + + len + " path=" + path); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/TxnLogEntry.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/TxnLogEntry.java new file mode 100644 index 0000000..352eb81 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/TxnLogEntry.java @@ -0,0 +1,50 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.apache.jute.Record; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * A helper class to represent the txn entry. + */ +public final class TxnLogEntry { + private final Record txn; + private final TxnHeader header; + private final TxnDigest digest; + + public TxnLogEntry(Record txn, TxnHeader header, TxnDigest digest) { + this.txn = txn; + this.header = header; + this.digest = digest; + } + + public Record getTxn() { + return txn; + } + + public TxnHeader getHeader() { + return header; + } + + public TxnDigest getDigest() { + return digest; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/TxnLogProposalIterator.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/TxnLogProposalIterator.java new file mode 100644 index 0000000..847e3b2 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/TxnLogProposalIterator.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.util.Iterator; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.quorum.QuorumPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class provides an iterator interface to access Proposal deserialized + * from on-disk txnlog. The iterator deserializes one proposal at a time + * to reduce memory footprint. Note that the request part of the proposal + * is not initialized and set to null since we don't need it during + * follower sync-up. + * + */ +public class TxnLogProposalIterator implements Iterator { + + private static final Logger LOG = LoggerFactory.getLogger(TxnLogProposalIterator.class); + + public static final TxnLogProposalIterator EMPTY_ITERATOR = new TxnLogProposalIterator(); + + private boolean hasNext = false; + + private TxnIterator itr; + + @Override + public boolean hasNext() { + return hasNext; + } + + /** + * Proposal returned by this iterator has request part set to null, since + * it is not used for follower sync-up. + */ + @Override + public Proposal next() { + + Proposal p = new Proposal(); + try { + byte[] serializedData = Util.marshallTxnEntry(itr.getHeader(), itr.getTxn(), itr.getDigest()); + + QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, itr.getHeader().getZxid(), serializedData, null); + p.packet = pp; + p.request = null; + + // This is the only place that can throw IO exception + hasNext = itr.next(); + + } catch (IOException e) { + LOG.error("Unable to read txnlog from disk", e); + hasNext = false; + } + + return p; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + /** + * Close the files and release the resources which are used for iterating + * transaction records + */ + public void close() { + if (itr != null) { + try { + itr.close(); + } catch (IOException ioe) { + LOG.warn("Error closing file iterator", ioe); + } + } + } + + private TxnLogProposalIterator() { + } + + public TxnLogProposalIterator(TxnIterator itr) { + if (itr != null) { + this.itr = itr; + hasNext = (itr.getHeader() != null); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/UnimplementedRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/UnimplementedRequestProcessor.java new file mode 100644 index 0000000..5f66add --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/UnimplementedRequestProcessor.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.proto.ReplyHeader; + +/** + * Manages the unknown requests (i.e. unknown OpCode), by: + * - sending back the KeeperException.UnimplementedException() error code to the client + * - closing the connection. + */ +public class UnimplementedRequestProcessor implements RequestProcessor { + + public void processRequest(Request request) throws RequestProcessorException { + KeeperException ke = new KeeperException.UnimplementedException(); + request.setException(ke); + ReplyHeader rh = new ReplyHeader(request.cxid, request.zxid, ke.code().intValue()); + try { + request.cnxn.sendResponse(rh, null, "response"); + } catch (IOException e) { + throw new RequestProcessorException("Can't send the response", e); + } + + request.cnxn.sendCloseSession(); + } + + public void shutdown() { + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/WorkerService.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/WorkerService.java new file mode 100644 index 0000000..eb54075 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/WorkerService.java @@ -0,0 +1,279 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.ArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.zookeeper.common.Time; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * WorkerService is a worker thread pool for running tasks and is implemented + * using one or more ExecutorServices. A WorkerService can support assignable + * threads, which it does by creating N separate single thread ExecutorServices, + * or non-assignable threads, which it does by creating a single N-thread + * ExecutorService. + * - NIOServerCnxnFactory uses a non-assignable WorkerService because the + * socket IO requests are order independent and allowing the + * ExecutorService to handle thread assignment gives optimal performance. + * - CommitProcessor uses an assignable WorkerService because requests for + * a given session must be processed in order. + * ExecutorService provides queue management and thread restarting, so it's + * useful even with a single thread. + */ +public class WorkerService { + + private static final Logger LOG = LoggerFactory.getLogger(WorkerService.class); + + // TODO_MA 注释: 这是一个线程池的 List + private final ArrayList workers = new ArrayList(); + + private final String threadNamePrefix; + private int numWorkerThreads; + private boolean threadsAreAssignable; + private long shutdownTimeoutMS = 5000; + + private volatile boolean stopped = true; + + /** + * @param name worker threads are named <name>Thread-## + * @param numThreads number of worker threads (0 - N) + * If 0, scheduled work is run immediately by + * the calling thread. + * @param useAssignableThreads whether the worker threads should be + * individually assignable or not + */ + public WorkerService(String name, int numThreads, boolean useAssignableThreads) { + this.threadNamePrefix = (name == null ? "" : name) + "Thread"; + this.numWorkerThreads = numThreads; + this.threadsAreAssignable = useAssignableThreads; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + start(); + } + + /** + * Callers should implement a class extending WorkRequest in order to + * schedule work with the service. + */ + public abstract static class WorkRequest { + + /** + * Must be implemented. Is called when the work request is run. + */ + public abstract void doWork() throws Exception; + + /** + * (Optional) If implemented, is called if the service is stopped + * or unable to schedule the request. + */ + public void cleanup() { + } + + } + + /** + * Schedule work to be done. If a worker thread pool is not being + * used, work is done directly by this thread. This schedule API is + * for use with non-assignable WorkerServices. For assignable + * WorkerServices, will always run on the first thread. + */ + public void schedule(WorkRequest workRequest) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + schedule(workRequest, 0); + } + + /** + * Schedule work to be done by the thread assigned to this id. Thread + * assignment is a single mod operation on the number of threads. If a + * worker thread pool is not being used, work is done directly by + * this thread. + */ + public void schedule(WorkRequest workRequest, long id) { + if(stopped) { + workRequest.cleanup(); + return; + } + + // TODO_MA 注释: 生成一个请求对象 + ScheduledWorkRequest scheduledWorkRequest = new ScheduledWorkRequest(workRequest); + + // TODO_MA 注释: workers 默认等于 1,也就是说,workers 中只有一个线程池 + // If we have a worker thread pool, use that; otherwise, do the work directly. + int size = workers.size(); + + if(size > 0) { + try { + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 提交任务 + */ + // make sure to map negative ids as well to [0, size-1] + int workerNum = ((int) (id % size) + size) % size; + ExecutorService worker = workers.get(workerNum); + + // TODO_MA 注释: 通过线程池 执行 ScheduledWorkRequest 请求 + worker.execute(scheduledWorkRequest); + + } catch(RejectedExecutionException e) { + LOG.warn("ExecutorService rejected execution", e); + workRequest.cleanup(); + } + } else { + // When there is no worker thread pool, do the work directly + // and wait for its completion + scheduledWorkRequest.run(); + } + } + + private class ScheduledWorkRequest implements Runnable { + + private final WorkRequest workRequest; + + ScheduledWorkRequest(WorkRequest workRequest) { + this.workRequest = workRequest; + } + + @Override + public void run() { + try { + // Check if stopped while request was on queue + if(stopped) { + workRequest.cleanup(); + return; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 工作 + * 真正开始工作了!(处理读写) + * workRequest = IOWorkRequest + */ + workRequest.doWork(); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + workRequest.cleanup(); + } + } + + } + + /** + * ThreadFactory for the worker thread pool. We don't use the default + * thread factory because (1) we want to give the worker threads easier + * to identify names; and (2) we want to make the worker threads daemon + * threads so they don't block the server from shutting down. + */ + private static class DaemonThreadFactory implements ThreadFactory { + + final ThreadGroup group; + final AtomicInteger threadNumber = new AtomicInteger(1); + final String namePrefix; + + DaemonThreadFactory(String name) { + this(name, 1); + } + + DaemonThreadFactory(String name, int firstThreadNum) { + threadNumber.set(firstThreadNum); + SecurityManager s = System.getSecurityManager(); + group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + namePrefix = name + "-"; + } + + public Thread newThread(Runnable r) { + Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0); + if(!t.isDaemon()) { + t.setDaemon(true); + } + if(t.getPriority() != Thread.NORM_PRIORITY) { + t.setPriority(Thread.NORM_PRIORITY); + } + return t; + } + + } + + public void start() { + + // TODO_MA 注释: numWorkerThreads = 5 + if(numWorkerThreads > 0) { + + // TODO_MA 注释: 默认情况下,这个值是 false + if(threadsAreAssignable) { + // TODO_MA 注释: 五个线程池,每个线程池,一个线程 + for(int i = 1; i <= numWorkerThreads; ++i) { + workers.add(Executors.newFixedThreadPool(1, new DaemonThreadFactory(threadNamePrefix, i))); + } + } + + // TODO_MA 注释: 走这儿 + else { + // TODO_MA 注释: 一个线程池,但是这个线程池有 5 个线程 + workers.add(Executors.newFixedThreadPool(numWorkerThreads, new DaemonThreadFactory(threadNamePrefix))); + } + } + stopped = false; + } + + public void stop() { + stopped = true; + + // Signal for graceful shutdown + for(ExecutorService worker : workers) { + worker.shutdown(); + } + } + + public void join(long shutdownTimeoutMS) { + // Give the worker threads time to finish executing + long now = Time.currentElapsedTime(); + long endTime = now + shutdownTimeoutMS; + for(ExecutorService worker : workers) { + boolean terminated = false; + while((now = Time.currentElapsedTime()) <= endTime) { + try { + terminated = worker.awaitTermination(endTime - now, TimeUnit.MILLISECONDS); + break; + } catch(InterruptedException e) { + // ignore + } + } + if(!terminated) { + // If we've timed out, do a hard shutdown + worker.shutdownNow(); + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZKDatabase.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZKDatabase.java new file mode 100644 index 0000000..41a90c3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZKDatabase.java @@ -0,0 +1,822 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; +import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog.PlayBackListener; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.quorum.QuorumPacket; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class maintains the in memory database of zookeeper + * server states that includes the sessions, datatree and the + * committed logs. It is booted up after reading the logs + * and snapshots from the disk. + */ +public class ZKDatabase { + + private static final Logger LOG = LoggerFactory.getLogger(ZKDatabase.class); + + /** + * make sure on a clear you take care of all these members. + */ + protected DataTree dataTree; + protected FileTxnSnapLog snapLog; + + protected ConcurrentHashMap sessionsWithTimeouts; + protected long minCommittedLog, maxCommittedLog; + + /** + * // TODO_MA 注释: 如果 txnlog 大小超过快照大小的 1/3,则默认值为使用快照 + * Default value is to use snapshot if txnlog size exceeds 1/3 the size of snapshot + */ + public static final String SNAPSHOT_SIZE_FACTOR = "zookeeper.snapshotSizeFactor"; + public static final double DEFAULT_SNAPSHOT_SIZE_FACTOR = 0.33; + private double snapshotSizeFactor; + + public static final String COMMIT_LOG_COUNT = "zookeeper.commitLogCount"; + public static final int DEFAULT_COMMIT_LOG_COUNT = 500; + public int commitLogCount; + protected static int commitLogBuffer = 700; + protected Queue committedLog = new ArrayDeque<>(); + protected ReentrantReadWriteLock logLock = new ReentrantReadWriteLock(); + private volatile boolean initialized = false; + + /** + * Number of txn since last snapshot; + */ + private AtomicInteger txnCount = new AtomicInteger(0); + + /** + * the filetxnsnaplog that this zk database + * maps to. There is a one to one relationship + * between a filetxnsnaplog and zkdatabase. + * + * @param snapLog the FileTxnSnapLog mapping this zkdatabase + */ + public ZKDatabase(FileTxnSnapLog snapLog) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建一颗空树 + */ + dataTree = createDataTree(); + + // TODO_MA 注释: 存储 session 超时时间 + sessionsWithTimeouts = new ConcurrentHashMap(); + this.snapLog = snapLog; + + // TODO_MA 注释: zookeeper.snapshotSizeFactor = 0.33 = 1/3 + try { + snapshotSizeFactor = Double + .parseDouble(System.getProperty(SNAPSHOT_SIZE_FACTOR, Double.toString(DEFAULT_SNAPSHOT_SIZE_FACTOR))); + if(snapshotSizeFactor > 1) { + snapshotSizeFactor = DEFAULT_SNAPSHOT_SIZE_FACTOR; + LOG.warn("The configured {} is invalid, going to use the default {}", SNAPSHOT_SIZE_FACTOR, + DEFAULT_SNAPSHOT_SIZE_FACTOR); + } + } catch(NumberFormatException e) { + LOG.error("Error parsing {}, using default value {}", SNAPSHOT_SIZE_FACTOR, DEFAULT_SNAPSHOT_SIZE_FACTOR); + snapshotSizeFactor = DEFAULT_SNAPSHOT_SIZE_FACTOR; + } + LOG.info("{} = {}", SNAPSHOT_SIZE_FACTOR, snapshotSizeFactor); + + // TODO_MA 注释: zookeeper.commitLogCount = 500 + try { + commitLogCount = Integer + .parseInt(System.getProperty(COMMIT_LOG_COUNT, Integer.toString(DEFAULT_COMMIT_LOG_COUNT))); + if(commitLogCount < DEFAULT_COMMIT_LOG_COUNT) { + commitLogCount = DEFAULT_COMMIT_LOG_COUNT; + LOG.warn( + "The configured commitLogCount {} is less than the recommended {}, going to use the recommended one", + COMMIT_LOG_COUNT, DEFAULT_COMMIT_LOG_COUNT); + } + } catch(NumberFormatException e) { + LOG.error("Error parsing {} - use default value {}", COMMIT_LOG_COUNT, DEFAULT_COMMIT_LOG_COUNT); + commitLogCount = DEFAULT_COMMIT_LOG_COUNT; + } + LOG.info("{}={}", COMMIT_LOG_COUNT, commitLogCount); + } + + /** + * checks to see if the zk database has been + * initialized or not. + * + * @return true if zk database is initialized and false if not + */ + public boolean isInitialized() { + return initialized; + } + + /** + * clear the zkdatabase. + * Note to developers - be careful to see that + * the clear method does clear out all the + * data structures in zkdatabase. + */ + public void clear() { + minCommittedLog = 0; + maxCommittedLog = 0; + /* to be safe we just create a new + * datatree. + */ + dataTree.shutdownWatcher(); + dataTree = createDataTree(); + sessionsWithTimeouts.clear(); + WriteLock lock = logLock.writeLock(); + try { + lock.lock(); + committedLog.clear(); + } finally { + lock.unlock(); + } + initialized = false; + } + + /** + * the datatree for this zkdatabase + * + * @return the datatree for this zkdatabase + */ + public DataTree getDataTree() { + return this.dataTree; + } + + /** + * the committed log for this zk database + * + * @return the committed log for this zkdatabase + */ + public long getmaxCommittedLog() { + return maxCommittedLog; + } + + /** + * the minimum committed transaction log + * available in memory + * + * @return the minimum committed transaction + * log available in memory + */ + public long getminCommittedLog() { + return minCommittedLog; + } + + /** + * Get the lock that controls the committedLog. If you want to get the pointer to the committedLog, you need + * to use this lock to acquire a read lock before calling getCommittedLog() + * + * @return the lock that controls the committed log + */ + public ReentrantReadWriteLock getLogLock() { + return logLock; + } + + public synchronized Collection getCommittedLog() { + final Collection result; + ReadLock rl = logLock.readLock(); + // make a copy if this thread is not already holding a lock + if(logLock.getReadHoldCount() > 0) { + result = this.committedLog; + } else { + rl.lock(); + try { + result = new ArrayList<>(this.committedLog); + } finally { + rl.unlock(); + } + } + return Collections.unmodifiableCollection(result); + } + + /** + * get the last processed zxid from a datatree + * + * @return the last processed zxid of a datatree + */ + public long getDataTreeLastProcessedZxid() { + return dataTree.lastProcessedZxid; + } + + /** + * return the sessions in the datatree + * + * @return the data tree sessions + */ + public Collection getSessions() { + return dataTree.getSessions(); + } + + /** + * @return number of (global) sessions + */ + public long getSessionCount() { + return sessionsWithTimeouts.size(); + } + + /** + * get sessions with timeouts + * + * @return the hashmap of sessions with timeouts + */ + public ConcurrentHashMap getSessionWithTimeOuts() { + return sessionsWithTimeouts; + } + + private final PlayBackListener commitProposalPlaybackListener = new PlayBackListener() { + public void onTxnLoaded(TxnHeader hdr, Record txn, TxnDigest digest) { + addCommittedProposal(hdr, txn, digest); + } + }; + + /** + * load the database from the disk onto memory and also add + * the transactions to the committedlog in memory. + * // TODO_MA 注释: 这个方法就是 ZK 集群做冷启动数据恢复的入口 + * // TODO_MA 注释: DataTree FileTxnSnapLog + * + * @return the last valid zxid on disk + * @throws IOException + */ + public long loadDataBase() throws IOException { + long startTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 重新恢复 + * 1、参数: dataTree 容器 + * 2、zxid 返回值, 代表的是 当前这个 datatree 中的最大的 事务id + * snapLog = FileTxnSnapLog + * zxid = 100 + */ + long zxid = snapLog.restore(dataTree, sessionsWithTimeouts, commitProposalPlaybackListener); + + // TODO_MA 注释: 表示已经初始化 + initialized = true; + + // TODO_MA 注释: 计算数据加载恢复时间 + long loadTime = Time.currentElapsedTime() - startTime; + + ServerMetrics.getMetrics().DB_INIT_TIME.add(loadTime); + LOG.info("Snapshot loaded in {} ms, highest zxid is 0x{}, digest is {}", loadTime, Long.toHexString(zxid), + dataTree.getTreeDigest()); + return zxid; + } + + /** + * Fast forward the database adding transactions from the committed log into memory. + * + * @return the last valid zxid. + * @throws IOException + */ + public long fastForwardDataBase() throws IOException { + long zxid = snapLog.fastForwardFromEdits(dataTree, sessionsWithTimeouts, commitProposalPlaybackListener); + initialized = true; + return zxid; + } + + private void addCommittedProposal(TxnHeader hdr, Record txn, TxnDigest digest) { + Request r = new Request(0, hdr.getCxid(), hdr.getType(), hdr, txn, hdr.getZxid()); + r.setTxnDigest(digest); + addCommittedProposal(r); + } + + /** + * maintains a list of last committedLog + * or so committed requests. This is used for + * fast follower synchronization. + * + * @param request committed request + */ + public void addCommittedProposal(Request request) { + WriteLock wl = logLock.writeLock(); + try { + wl.lock(); + if(committedLog.size() > commitLogCount) { + committedLog.remove(); + minCommittedLog = committedLog.peek().packet.getZxid(); + } + if(committedLog.isEmpty()) { + minCommittedLog = request.zxid; + maxCommittedLog = request.zxid; + } + + byte[] data = SerializeUtils.serializeRequest(request); + + // TODO_MA 注释: 生成一个 PROPOSAL + QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, data, null); + Proposal p = new Proposal(); + p.packet = pp; + p.request = request; + + // TODO_MA 注释: 对应的 PROPOSAL 缓存在内存中 + committedLog.add(p); + maxCommittedLog = p.packet.getZxid(); + } finally { + wl.unlock(); + } + } + + public boolean isTxnLogSyncEnabled() { + boolean enabled = snapshotSizeFactor >= 0; + if(enabled) { + LOG.info("On disk txn sync enabled with snapshotSizeFactor {}", snapshotSizeFactor); + } else { + LOG.info("On disk txn sync disabled"); + } + return enabled; + } + + public long calculateTxnLogSizeLimit() { + long snapSize = 0; + try { + File snapFile = snapLog.findMostRecentSnapshot(); + if(snapFile != null) { + snapSize = snapFile.length(); + } + } catch(IOException e) { + LOG.error("Unable to get size of most recent snapshot"); + } + return (long) (snapSize * snapshotSizeFactor); + } + + /** + * Get proposals from txnlog. Only packet part of proposal is populated. + * + * @param startZxid the starting zxid of the proposal + * @param sizeLimit maximum on-disk size of txnlog to fetch + * 0 is unlimited, negative value means disable. + * @return list of proposal (request part of each proposal is null) + */ + public Iterator getProposalsFromTxnLog(long startZxid, long sizeLimit) { + if(sizeLimit < 0) { + LOG.debug("Negative size limit - retrieving proposal via txnlog is disabled"); + return TxnLogProposalIterator.EMPTY_ITERATOR; + } + + TxnIterator itr = null; + try { + + itr = snapLog.readTxnLog(startZxid, false); + + // If we cannot guarantee that this is strictly the starting txn + // after a given zxid, we should fail. + if((itr.getHeader() != null) && (itr.getHeader().getZxid() > startZxid)) { + LOG.warn("Unable to find proposals from txnlog for zxid: 0x{}", Long.toHexString(startZxid)); + itr.close(); + return TxnLogProposalIterator.EMPTY_ITERATOR; + } + + if(sizeLimit > 0) { + long txnSize = itr.getStorageSize(); + if(txnSize > sizeLimit) { + LOG.info("Txnlog size: {} exceeds sizeLimit: {}", txnSize, sizeLimit); + itr.close(); + return TxnLogProposalIterator.EMPTY_ITERATOR; + } + } + } catch(IOException e) { + LOG.error("Unable to read txnlog from disk", e); + try { + if(itr != null) { + itr.close(); + } + } catch(IOException ioe) { + LOG.warn("Error closing file iterator", ioe); + } + return TxnLogProposalIterator.EMPTY_ITERATOR; + } + return new TxnLogProposalIterator(itr); + } + + public List aclForNode(DataNode n) { + return dataTree.getACL(n); + } + + /** + * remove a cnxn from the datatree + * + * @param cnxn the cnxn to remove from the datatree + */ + public void removeCnxn(ServerCnxn cnxn) { + dataTree.removeCnxn(cnxn); + } + + /** + * kill a given session in the datatree + * + * @param sessionId the session id to be killed + * @param zxid the zxid of kill session transaction + */ + public void killSession(long sessionId, long zxid) { + dataTree.killSession(sessionId, zxid); + } + + /** + * write a text dump of all the ephemerals in the datatree + * + * @param pwriter the output to write to + */ + public void dumpEphemerals(PrintWriter pwriter) { + dataTree.dumpEphemerals(pwriter); + } + + public Map> getEphemerals() { + return dataTree.getEphemerals(); + } + + /** + * the node count of the datatree + * + * @return the node count of datatree + */ + public int getNodeCount() { + return dataTree.getNodeCount(); + } + + /** + * the paths for ephemeral session id + * + * @param sessionId the session id for which paths match to + * @return the paths for a session id + */ + public Set getEphemerals(long sessionId) { + return dataTree.getEphemerals(sessionId); + } + + /** + * the last processed zxid in the datatree + * + * @param zxid the last processed zxid in the datatree + */ + public void setlastProcessedZxid(long zxid) { + dataTree.lastProcessedZxid = zxid; + } + + /** + * the process txn on the data and perform digest comparision. + * + * @param hdr the txnheader for the txn + * @param txn the transaction that needs to be processed + * @param digest the expected digest. A null value would skip the check + * @return the result of processing the transaction on this + * datatree/zkdatabase + */ + public ProcessTxnResult processTxn(TxnHeader hdr, Record txn, TxnDigest digest) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return dataTree.processTxn(hdr, txn, digest); + } + + /** + * stat the path + * + * @param path the path for which stat is to be done + * @param serverCnxn the servercnxn attached to this request + * @return the stat of this node + * @throws KeeperException.NoNodeException + */ + public Stat statNode(String path, ServerCnxn serverCnxn) throws KeeperException.NoNodeException { + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return dataTree.statNode(path, serverCnxn); + } + + /** + * get the datanode for this path + * + * @param path the path to lookup + * @return the datanode for getting the path + */ + public DataNode getNode(String path) { + return dataTree.getNode(path); + } + + /** + * get data and stat for a path + * + * @param path the path being queried + * @param stat the stat for this path + * @param watcher the watcher function + * @return + * @throws KeeperException.NoNodeException + */ + public byte[] getData(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { + return dataTree.getData(path, stat, watcher); + } + + /** + * set watches on the datatree + * + * @param relativeZxid the relative zxid that client has seen + * @param dataWatches the data watches the client wants to reset + * @param existWatches the exists watches the client wants to reset + * @param childWatches the child watches the client wants to reset + * @param persistentWatches the persistent watches the client wants to reset + * @param persistentRecursiveWatches the persistent recursive watches the client wants to reset + * @param watcher the watcher function + */ + public void setWatches(long relativeZxid, List dataWatches, List existWatches, + List childWatches, List persistentWatches, List persistentRecursiveWatches, + Watcher watcher) { + dataTree.setWatches(relativeZxid, dataWatches, existWatches, childWatches, persistentWatches, + persistentRecursiveWatches, watcher); + } + + /** + * Add a watch + * + * @param basePath watch base + * @param watcher the watcher + * @param mode a mode from ZooDefs.AddWatchModes + */ + public void addWatch(String basePath, Watcher watcher, int mode) { + dataTree.addWatch(basePath, watcher, mode); + } + + /** + * get acl for a path + * + * @param path the path to query for acl + * @param stat the stat for the node + * @return the acl list for this path + * @throws NoNodeException + */ + public List getACL(String path, Stat stat) throws NoNodeException { + return dataTree.getACL(path, stat); + } + + /** + * get children list for this path + * + * @param path the path of the node + * @param stat the stat of the node + * @param watcher the watcher function for this path + * @return the list of children for this path + * @throws KeeperException.NoNodeException + */ + public List getChildren(String path, Stat stat, Watcher watcher) throws KeeperException.NoNodeException { + return dataTree.getChildren(path, stat, watcher); + } + + /* + * get all sub-children number of this node + * */ + public int getAllChildrenNumber(String path) throws KeeperException.NoNodeException { + return dataTree.getAllChildrenNumber(path); + } + + /** + * check if the path is special or not + * + * @param path the input path + * @return true if path is special and false if not + */ + public boolean isSpecialPath(String path) { + return dataTree.isSpecialPath(path); + } + + /** + * get the acl size of the datatree + * + * @return the acl size of the datatree + */ + public int getAclSize() { + return dataTree.aclCacheSize(); + } + + /** + * Truncate the ZKDatabase to the specified zxid + * + * @param zxid the zxid to truncate zk database to + * @return true if the truncate is successful and false if not + * @throws IOException + */ + public boolean truncateLog(long zxid) throws IOException { + clear(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + // truncate the log + boolean truncated = snapLog.truncateLog(zxid); + + if(!truncated) { + return false; + } + + loadDataBase(); + return true; + } + + /** + * deserialize a snapshot from an input archive + * + * @param ia the input archive you want to deserialize from + * @throws IOException + */ + public void deserializeSnapshot(InputArchive ia) throws IOException { + clear(); + SerializeUtils.deserializeSnapshot(getDataTree(), ia, getSessionWithTimeOuts()); + initialized = true; + } + + /** + * serialize the snapshot + * + * @param oa the output archive to which the snapshot needs to be serialized + * @throws IOException + * @throws InterruptedException + */ + public void serializeSnapshot(OutputArchive oa) throws IOException, InterruptedException { + SerializeUtils.serializeSnapshot(getDataTree(), oa, getSessionWithTimeOuts()); + } + + /** + * append to the underlying transaction log + * + * @param si the request to append + * @return true if the append was succesfull and false if not + */ + public boolean append(Request si) throws IOException { + txnCount.incrementAndGet(); + return this.snapLog.append(si); + } + + /** + * roll the underlying log + */ + public void rollLog() throws IOException { + this.snapLog.rollLog(); + resetTxnCount(); + } + + /** + * commit to the underlying transaction log + * + * @throws IOException + */ + public void commit() throws IOException { + this.snapLog.commit(); + } + + /** + * close this database. free the resources + * + * @throws IOException + */ + public void close() throws IOException { + this.snapLog.close(); + } + + public synchronized void initConfigInZKDatabase(QuorumVerifier qv) { + if(qv == null) { + return; // only happens during tests + } + try { + + // TODO_MA 注释: 如果不存在,则创建一个 ZooDefs.CONFIG_NODE 节点 + if(this.dataTree.getNode(ZooDefs.CONFIG_NODE) == null) { + // should only happen during upgrade + LOG.warn("configuration znode missing (should only happen during upgrade), creating the node"); + this.dataTree.addConfigNode(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 修改 /zookeeper/config znode 的数据 + */ + this.dataTree + .setData(ZooDefs.CONFIG_NODE, qv.toString().getBytes(), -1, qv.getVersion(), Time.currentWallTime()); + } catch(NoNodeException e) { + System.out.println("configuration node missing - should not happen"); + } + } + + /** + * Use for unit testing, so we can turn this feature on/off + * + * @param snapshotSizeFactor Set to minus value to turn this off. + */ + public void setSnapshotSizeFactor(double snapshotSizeFactor) { + this.snapshotSizeFactor = snapshotSizeFactor; + } + + /** + * Check whether the given watcher exists in datatree + * + * @param path node to check watcher existence + * @param type type of watcher + * @param watcher watcher function + */ + public boolean containsWatcher(String path, WatcherType type, Watcher watcher) { + return dataTree.containsWatcher(path, type, watcher); + } + + /** + * Remove watch from the datatree + * + * @param path node to remove watches from + * @param type type of watcher to remove + * @param watcher watcher function to remove + */ + public boolean removeWatch(String path, WatcherType type, Watcher watcher) { + return dataTree.removeWatch(path, type, watcher); + } + + // visible for testing + public DataTree createDataTree() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return new DataTree(); + } + + /** + * Reset the number of txn since last rollLog + */ + public void resetTxnCount() { + txnCount.set(0); + snapLog.setTotalLogSize(0); + } + + /** + * Get the number of txn since last snapshot + */ + public int getTxnCount() { + return txnCount.get(); + } + + /** + * Get the size of txn since last snapshot + */ + public long getTxnSize() { + return snapLog.getTotalLogSize(); + } + + public boolean compareDigest(TxnHeader header, Record txn, TxnDigest digest) { + return dataTree.compareDigest(header, txn, digest); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperCriticalThread.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperCriticalThread.java new file mode 100644 index 0000000..4514686 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperCriticalThread.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Represents critical thread. When there is an uncaught exception thrown by the + * thread this will exit the system. + */ +public class ZooKeeperCriticalThread extends ZooKeeperThread { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperCriticalThread.class); + private final ZooKeeperServerListener listener; + + public ZooKeeperCriticalThread(String threadName, ZooKeeperServerListener listener) { + super(threadName); + this.listener = listener; + } + + /** + * This will be used by the uncaught exception handler and make the system + * exit. + * + * @param threadName + * - thread name + * @param e + * - exception object + */ + @Override + protected void handleException(String threadName, Throwable e) { + LOG.error("Severe unrecoverable error, from thread : {}", threadName, e); + listener.notifyStopping(threadName, ExitCode.UNEXPECTED_ERROR.getValue()); + ServerMetrics.getMetrics().UNRECOVERABLE_ERROR_COUNT.add(1); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperSaslServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperSaslServer.java new file mode 100644 index 0000000..5bdabfe --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperSaslServer.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import javax.security.auth.Subject; +import javax.security.sasl.SaslException; +import javax.security.sasl.SaslServer; +import org.apache.zookeeper.Login; +import org.apache.zookeeper.util.SecurityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZooKeeperSaslServer { + + public static final String LOGIN_CONTEXT_NAME_KEY = "zookeeper.sasl.serverconfig"; + public static final String DEFAULT_LOGIN_CONTEXT_NAME = "Server"; + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperSaslServer.class); + private SaslServer saslServer; + + ZooKeeperSaslServer(final Login login) { + saslServer = createSaslServer(login); + } + + private SaslServer createSaslServer(final Login login) { + synchronized (login) { + Subject subject = login.getSubject(); + return SecurityUtils.createSaslServer(subject, "zookeeper", "zk-sasl-md5", login.callbackHandler, LOG); + } + } + + public byte[] evaluateResponse(byte[] response) throws SaslException { + return saslServer.evaluateResponse(response); + } + + public boolean isComplete() { + return saslServer.isComplete(); + } + + public String getAuthorizationID() { + return saslServer.getAuthorizationID(); + } + +} + + + + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java new file mode 100644 index 0000000..37ffe34 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java @@ -0,0 +1,2349 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Deque; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; +import javax.security.sasl.SaslException; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZookeeperBanner; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.proto.AuthPacket; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.proto.ConnectResponse; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.GetSASLRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.SetACLRequest; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.proto.SetSASLResponse; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.RequestProcessor.RequestProcessorException; +import org.apache.zookeeper.server.ServerCnxn.CloseRequestException; +import org.apache.zookeeper.server.SessionTracker.Session; +import org.apache.zookeeper.server.SessionTracker.SessionExpirer; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.auth.ServerAuthenticationProvider; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; +import org.apache.zookeeper.server.util.JvmPauseMonitor; +import org.apache.zookeeper.server.util.OSMXBean; +import org.apache.zookeeper.server.util.RequestPathMetricsCollector; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements a simple standalone ZooKeeperServer. It sets up the + * following chain of RequestProcessors to process requests: + * PrepRequestProcessor -> SyncRequestProcessor -> FinalRequestProcessor + */ +public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider { + + protected static final Logger LOG; + + public static final String GLOBAL_OUTSTANDING_LIMIT = "zookeeper.globalOutstandingLimit"; + + public static final String ENABLE_EAGER_ACL_CHECK = "zookeeper.enableEagerACLCheck"; + public static final String SKIP_ACL = "zookeeper.skipACL"; + + // When enabled, will check ACL constraints appertained to the requests first, + // before sending the requests to the quorum. + static final boolean enableEagerACLCheck; + + static final boolean skipACL; + + public static final String ALLOW_SASL_FAILED_CLIENTS = "zookeeper.allowSaslFailedClients"; + public static final String SESSION_REQUIRE_CLIENT_SASL_AUTH = "zookeeper.sessionRequireClientSASLAuth"; + public static final String SASL_AUTH_SCHEME = "sasl"; + + public static final String ZOOKEEPER_DIGEST_ENABLED = "zookeeper.digest.enabled"; + private static boolean digestEnabled; + + // Add a enable/disable option for now, we should remove this one when + // this feature is confirmed to be stable + public static final String CLOSE_SESSION_TXN_ENABLED = "zookeeper.closeSessionTxn.enabled"; + private static boolean closeSessionTxnEnabled = true; + + static { + LOG = LoggerFactory.getLogger(ZooKeeperServer.class); + + ZookeeperBanner.printBanner(LOG); + + Environment.logEnv("Server environment:", LOG); + + enableEagerACLCheck = Boolean.getBoolean(ENABLE_EAGER_ACL_CHECK); + LOG.info("{} = {}", ENABLE_EAGER_ACL_CHECK, enableEagerACLCheck); + + skipACL = System.getProperty(SKIP_ACL, "no").equals("yes"); + if(skipACL) { + LOG.info("{}==\"yes\", ACL checks will be skipped", SKIP_ACL); + } + + digestEnabled = Boolean.parseBoolean(System.getProperty(ZOOKEEPER_DIGEST_ENABLED, "true")); + LOG.info("{} = {}", ZOOKEEPER_DIGEST_ENABLED, digestEnabled); + + closeSessionTxnEnabled = Boolean.parseBoolean(System.getProperty(CLOSE_SESSION_TXN_ENABLED, "true")); + LOG.info("{} = {}", CLOSE_SESSION_TXN_ENABLED, closeSessionTxnEnabled); + } + + public static boolean isCloseSessionTxnEnabled() { + return closeSessionTxnEnabled; + } + + public static void setCloseSessionTxnEnabled(boolean enabled) { + ZooKeeperServer.closeSessionTxnEnabled = enabled; + LOG.info("Update {} to {}", CLOSE_SESSION_TXN_ENABLED, ZooKeeperServer.closeSessionTxnEnabled); + } + + protected ZooKeeperServerBean jmxServerBean; + protected DataTreeBean jmxDataTreeBean; + + public static final int DEFAULT_TICK_TIME = 3000; + protected int tickTime = DEFAULT_TICK_TIME; + /** + * value of -1 indicates unset, use default + */ + protected int minSessionTimeout = -1; + /** + * value of -1 indicates unset, use default + */ + protected int maxSessionTimeout = -1; + /** + * Socket listen backlog. Value of -1 indicates unset + */ + protected int listenBacklog = -1; + protected SessionTracker sessionTracker; + private FileTxnSnapLog txnLogFactory = null; + private ZKDatabase zkDb; + private ResponseCache readResponseCache; + private ResponseCache getChildrenResponseCache; + private final AtomicLong hzxid = new AtomicLong(0); + public static final Exception ok = new Exception("No prob"); + protected RequestProcessor firstProcessor; + protected JvmPauseMonitor jvmPauseMonitor; + protected volatile State state = State.INITIAL; + private boolean isResponseCachingEnabled = true; + /* contains the configuration file content read at startup */ + protected String initialConfig; + protected boolean reconfigEnabled; + private final RequestPathMetricsCollector requestPathMetricsCollector; + + private boolean localSessionEnabled = false; + + protected enum State { + INITIAL, + RUNNING, + SHUTDOWN, + ERROR + } + + /** + * This is the secret that we use to generate passwords. For the moment, + * it's more of a checksum that's used in reconnection, which carries no + * security weight, and is treated internally as if it carries no + * security weight. + */ + private static final long superSecret = 0XB3415C00L; + + private final AtomicInteger requestsInProcess = new AtomicInteger(0); + final Deque outstandingChanges = new ArrayDeque<>(); + // this data structure must be accessed under the outstandingChanges lock + final Map outstandingChangesForPath = new HashMap(); + + protected ServerCnxnFactory serverCnxnFactory; + protected ServerCnxnFactory secureServerCnxnFactory; + + private final ServerStats serverStats; + private final ZooKeeperServerListener listener; + private ZooKeeperServerShutdownHandler zkShutdownHandler; + private volatile int createSessionTrackerServerId = 1; + + private static final String FLUSH_DELAY = "zookeeper.flushDelay"; + private static volatile long flushDelay; + private static final String MAX_WRITE_QUEUE_POLL_SIZE = "zookeeper.maxWriteQueuePollTime"; + private static volatile long maxWriteQueuePollTime; + private static final String MAX_BATCH_SIZE = "zookeeper.maxBatchSize"; + private static volatile int maxBatchSize; + + /** + * Starting size of read and write ByteArroyOuputBuffers. Default is 32 bytes. + * Flag not used for small transfers like connectResponses. + */ + public static final String INT_BUFFER_STARTING_SIZE_BYTES = "zookeeper.intBufferStartingSizeBytes"; + public static final int DEFAULT_STARTING_BUFFER_SIZE = 1024; + public static final int intBufferStartingSizeBytes; + + public static final String GET_DATA_RESPONSE_CACHE_SIZE = "zookeeper.maxResponseCacheSize"; + public static final String GET_CHILDREN_RESPONSE_CACHE_SIZE = "zookeeper.maxGetChildrenResponseCacheSize"; + + static { + long configuredFlushDelay = Long.getLong(FLUSH_DELAY, 0); + setFlushDelay(configuredFlushDelay); + setMaxWriteQueuePollTime(Long.getLong(MAX_WRITE_QUEUE_POLL_SIZE, configuredFlushDelay / 3)); + setMaxBatchSize(Integer.getInteger(MAX_BATCH_SIZE, 1000)); + + intBufferStartingSizeBytes = Integer.getInteger(INT_BUFFER_STARTING_SIZE_BYTES, DEFAULT_STARTING_BUFFER_SIZE); + + if(intBufferStartingSizeBytes < 32) { + String msg = "Buffer starting size must be greater than 0." + "Configure with \"-Dzookeeper.intBufferStartingSizeBytes=\" "; + LOG.error(msg); + throw new IllegalArgumentException(msg); + } + + LOG.info("{} = {}", INT_BUFFER_STARTING_SIZE_BYTES, intBufferStartingSizeBytes); + } + + // Connection throttling + private BlueThrottle connThrottle = new BlueThrottle(); + + @SuppressFBWarnings(value = "IS2_INCONSISTENT_SYNC", justification = "Internally the throttler has a BlockingQueue so " + "once the throttler is created and started, it is thread-safe") + private RequestThrottler requestThrottler; + public static final String SNAP_COUNT = "zookeeper.snapCount"; + + /** + * This setting sets a limit on the total number of large requests that + * can be inflight and is designed to prevent ZooKeeper from accepting + * too many large requests such that the JVM runs out of usable heap and + * ultimately crashes. + * + * The limit is enforced by the {@link checkRequestSize(int, boolean)} + * method which is called by the connection layer ({@link NIOServerCnxn}, + * {@link NettyServerCnxn}) before allocating a byte buffer and pulling + * data off the TCP socket. The limit is then checked again by the + * ZooKeeper server in {@link processPacket(ServerCnxn, ByteBuffer)} which + * also atomically updates {@link currentLargeRequestBytes}. The request is + * then marked as a large request, with the request size stored in the Request + * object so that it can later be decremented from {@link currentLargeRequestsBytes}. + * + * When a request is completed or dropped, the relevant code path calls the + * {@link requestFinished(Request)} method which performs the decrement if + * needed. + */ + private volatile int largeRequestMaxBytes = 100 * 1024 * 1024; + + /** + * The size threshold after which a request is considered a large request + * and is checked against the large request byte limit. + */ + private volatile int largeRequestThreshold = -1; + + private final AtomicInteger currentLargeRequestBytes = new AtomicInteger(0); + + void removeCnxn(ServerCnxn cnxn) { + zkDb.removeCnxn(cnxn); + } + + /** + * Creates a ZooKeeperServer instance. Nothing is setup, use the setX + * methods to prepare the instance (eg datadir, datalogdir, ticktime, + * builder, etc...) + */ + public ZooKeeperServer() { + listener = new ZooKeeperServerListenerImpl(this); + serverStats = new ServerStats(this); + this.requestPathMetricsCollector = new RequestPathMetricsCollector(); + } + + /** + * Keeping this constructor for backward compatibility + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, + int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig) { + this(txnLogFactory, tickTime, minSessionTimeout, maxSessionTimeout, clientPortListenBacklog, zkDb, initialConfig, + QuorumPeerConfig.isReconfigEnabled()); + } + + /** + * * Creates a ZooKeeperServer instance. It sets everything up, but doesn't + * actually start listening for clients until run() is invoked. + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, + int clientPortListenBacklog, ZKDatabase zkDb, String initialConfig, boolean reconfigEnabled) { + serverStats = new ServerStats(this); + this.txnLogFactory = txnLogFactory; + this.txnLogFactory.setServerStats(this.serverStats); + this.zkDb = zkDb; + this.tickTime = tickTime; + setMinSessionTimeout(minSessionTimeout); + setMaxSessionTimeout(maxSessionTimeout); + this.listenBacklog = clientPortListenBacklog; + this.reconfigEnabled = reconfigEnabled; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 监听器 + */ + listener = new ZooKeeperServerListenerImpl(this); + + readResponseCache = new ResponseCache( + Integer.getInteger(GET_DATA_RESPONSE_CACHE_SIZE, ResponseCache.DEFAULT_RESPONSE_CACHE_SIZE)); + + getChildrenResponseCache = new ResponseCache( + Integer.getInteger(GET_CHILDREN_RESPONSE_CACHE_SIZE, ResponseCache.DEFAULT_RESPONSE_CACHE_SIZE)); + + this.initialConfig = initialConfig; + + this.requestPathMetricsCollector = new RequestPathMetricsCollector(); + + // TODO_MA 注释: 初始化 限流器的一些参数 + this.initLargeRequestThrottlingSettings(); + + LOG.info( + "Created server with" + " tickTime {}" + " minSessionTimeout {}" + " maxSessionTimeout {}" + " clientPortListenBacklog {}" + " datadir {}" + " snapdir {}", + tickTime, getMinSessionTimeout(), getMaxSessionTimeout(), getClientPortListenBacklog(), + txnLogFactory.getDataDir(), txnLogFactory.getSnapDir()); + } + + public String getInitialConfig() { + return initialConfig; + } + + /** + * Adds JvmPauseMonitor and calls + * {@link #ZooKeeperServer(FileTxnSnapLog, int, int, int, int, ZKDatabase, String)} + */ + public ZooKeeperServer(JvmPauseMonitor jvmPauseMonitor, FileTxnSnapLog txnLogFactory, int tickTime, + int minSessionTimeout, int maxSessionTimeout, int clientPortListenBacklog, ZKDatabase zkDb, + String initialConfig) { + this(txnLogFactory, tickTime, minSessionTimeout, maxSessionTimeout, clientPortListenBacklog, zkDb, initialConfig, + QuorumPeerConfig.isReconfigEnabled()); + this.jvmPauseMonitor = jvmPauseMonitor; + if(jvmPauseMonitor != null) { + LOG.info("Added JvmPauseMonitor to server"); + } + } + + /** + * creates a zookeeperserver instance. + * + * @param txnLogFactory the file transaction snapshot logging class + * @param tickTime the ticktime for the server + * @throws IOException + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory, int tickTime, String initialConfig) { + this(txnLogFactory, tickTime, -1, -1, -1, new ZKDatabase(txnLogFactory), initialConfig, + QuorumPeerConfig.isReconfigEnabled()); + } + + public ServerStats serverStats() { + return serverStats; + } + + public RequestPathMetricsCollector getRequestPathMetricsCollector() { + return requestPathMetricsCollector; + } + + public BlueThrottle connThrottle() { + return connThrottle; + } + + public void dumpConf(PrintWriter pwriter) { + pwriter.print("clientPort="); + pwriter.println(getClientPort()); + pwriter.print("secureClientPort="); + pwriter.println(getSecureClientPort()); + pwriter.print("dataDir="); + pwriter.println(zkDb.snapLog.getSnapDir().getAbsolutePath()); + pwriter.print("dataDirSize="); + pwriter.println(getDataDirSize()); + pwriter.print("dataLogDir="); + pwriter.println(zkDb.snapLog.getDataDir().getAbsolutePath()); + pwriter.print("dataLogSize="); + pwriter.println(getLogDirSize()); + pwriter.print("tickTime="); + pwriter.println(getTickTime()); + pwriter.print("maxClientCnxns="); + pwriter.println(getMaxClientCnxnsPerHost()); + pwriter.print("minSessionTimeout="); + pwriter.println(getMinSessionTimeout()); + pwriter.print("maxSessionTimeout="); + pwriter.println(getMaxSessionTimeout()); + pwriter.print("clientPortListenBacklog="); + pwriter.println(getClientPortListenBacklog()); + + pwriter.print("serverId="); + pwriter.println(getServerId()); + } + + public ZooKeeperServerConf getConf() { + return new ZooKeeperServerConf(getClientPort(), zkDb.snapLog.getSnapDir().getAbsolutePath(), + zkDb.snapLog.getDataDir().getAbsolutePath(), getTickTime(), getMaxClientCnxnsPerHost(), + getMinSessionTimeout(), getMaxSessionTimeout(), getServerId(), getClientPortListenBacklog()); + } + + /** + * This constructor is for backward compatibility with the existing unit + * test code. + * It defaults to FileLogProvider persistence provider. + */ + public ZooKeeperServer(File snapDir, File logDir, int tickTime) throws IOException { + this(new FileTxnSnapLog(snapDir, logDir), tickTime, ""); + } + + /** + * Default constructor, relies on the config for its argument values + * + * @throws IOException + */ + public ZooKeeperServer(FileTxnSnapLog txnLogFactory) throws IOException { + this(txnLogFactory, DEFAULT_TICK_TIME, -1, -1, -1, new ZKDatabase(txnLogFactory), "", + QuorumPeerConfig.isReconfigEnabled()); + } + + /** + * get the zookeeper database for this server + * + * @return the zookeeper database for this server + */ + public ZKDatabase getZKDatabase() { + return this.zkDb; + } + + /** + * set the zkdatabase for this zookeeper server + * + * @param zkDb + */ + public void setZKDatabase(ZKDatabase zkDb) { + this.zkDb = zkDb; + } + + /** + * Restore sessions and data + */ + public void loadData() throws IOException, InterruptedException { + /* + * When a new leader starts executing Leader#lead, it + * invokes this method. The database, however, has been + * initialized before running leader election so that + * the server could pick its zxid for its initial vote. + * It does it by invoking QuorumPeer#getLastLoggedZxid. + * Consequently, we don't need to initialize it once more + * and avoid the penalty of loading it a second time. Not + * reloading it is particularly important for applications + * that host a large database. + * + * The following if block checks whether the database has + * been initialized or not. Note that this method is + * invoked by at least one other method: + * ZooKeeperServer#startdata. + * + * See ZOOKEEPER-1642 for more detail. + */ + if(zkDb.isInitialized()) { + setZxid(zkDb.getDataTreeLastProcessedZxid()); + } else { + setZxid(zkDb.loadDataBase()); + } + + // TODO_MA 注释: 清楚 Dead Session + // Clean up dead sessions + List deadSessions = new ArrayList<>(); + for(Long session : zkDb.getSessions()) { + if(zkDb.getSessionWithTimeOuts().get(session) == null) { + deadSessions.add(session); + } + } + for(long session : deadSessions) { + // TODO: Is lastProcessedZxid really the best thing to use? + killSession(session, zkDb.getDataTreeLastProcessedZxid()); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 拍摄快照 + */ + // Make a clean snapshot + takeSnapshot(); + } + + public void takeSnapshot() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + takeSnapshot(false); + } + + public void takeSnapshot(boolean syncSnap) { + long start = Time.currentElapsedTime(); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 将 DataTree 进行持久化 + */ + txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap); + + } catch(IOException e) { + LOG.error("Severe unrecoverable error, exiting", e); + // This is a severe error that we cannot recover from, + // so we need to exit + ServiceUtils.requestSystemExit(ExitCode.TXNLOG_ERROR_TAKING_SNAPSHOT.getValue()); + } + long elapsed = Time.currentElapsedTime() - start; + LOG.info("Snapshot taken in {} ms", elapsed); + ServerMetrics.getMetrics().SNAPSHOT_TIME.add(elapsed); + } + + @Override + public long getDataDirSize() { + if(zkDb == null) { + return 0L; + } + File path = zkDb.snapLog.getDataDir(); + return getDirSize(path); + } + + @Override + public long getLogDirSize() { + if(zkDb == null) { + return 0L; + } + File path = zkDb.snapLog.getSnapDir(); + return getDirSize(path); + } + + private long getDirSize(File file) { + long size = 0L; + if(file.isDirectory()) { + File[] files = file.listFiles(); + if(files != null) { + for(File f : files) { + size += getDirSize(f); + } + } + } else { + size = file.length(); + } + return size; + } + + public long getZxid() { + return hzxid.get(); + } + + public SessionTracker getSessionTracker() { + return sessionTracker; + } + + long getNextZxid() { + return hzxid.incrementAndGet(); + } + + public void setZxid(long zxid) { + hzxid.set(zxid); + } + + private void close(long sessionId) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + Request si = new Request(null, sessionId, 0, OpCode.closeSession, null, null); + submitRequest(si); + } + + public void closeSession(long sessionId) { + LOG.info("Closing session 0x{}", Long.toHexString(sessionId)); + + // we do not want to wait for a session close. send it as soon as we + // detect it! + close(sessionId); + } + + protected void killSession(long sessionId, long zxid) { + zkDb.killSession(sessionId, zxid); + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "ZooKeeperServer --- killSession: 0x" + Long.toHexString(sessionId)); + } + if(sessionTracker != null) { + sessionTracker.removeSession(sessionId); + } + } + + public void expire(Session session) { + long sessionId = session.getSessionId(); + LOG.info("Expiring session 0x{}, timeout of {}ms exceeded", Long.toHexString(sessionId), session.getTimeout()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + close(sessionId); + } + + public static class MissingSessionException extends IOException { + + private static final long serialVersionUID = 7467414635467261007L; + + public MissingSessionException(String msg) { + super(msg); + } + + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 更新 Session 信息 + */ + void touch(ServerCnxn cnxn) throws MissingSessionException { + if(cnxn == null) { + return; + } + long id = cnxn.getSessionId(); + + // TODO_MA 注释: 超时时间 + int to = cnxn.getSessionTimeout(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 更新 session 超时时间 + */ + if(!sessionTracker.touchSession(id, to)) { + throw new MissingSessionException( + "No session with sessionid 0x" + Long.toHexString(id) + " exists, probably expired and removed"); + } + } + + protected void registerJMX() { + // register with JMX + try { + jmxServerBean = new ZooKeeperServerBean(this); + MBeanRegistry.getInstance().register(jmxServerBean, null); + + try { + jmxDataTreeBean = new DataTreeBean(zkDb.getDataTree()); + MBeanRegistry.getInstance().register(jmxDataTreeBean, jmxServerBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxDataTreeBean = null; + } + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxServerBean = null; + } + } + + public void startdata() throws IOException, InterruptedException { + //check to see if zkDb is not null + if(zkDb == null) { + zkDb = new ZKDatabase(this.txnLogFactory); + } + if(!zkDb.isInitialized()) { + loadData(); + } + } + + public synchronized void startup() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 进入 RUNNING 状态 + * 此时,这个地方更多的作用,是切换 Leader 的状态为: running + */ + startupWithServerState(State.RUNNING); + } + + public synchronized void startupWithoutServing() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: Initial 状态 + */ + startupWithServerState(State.INITIAL); + } + + public synchronized void startServing() { + + // TODO_MA 注释: 修改状态为 RUNNING + setState(State.RUNNING); + notifyAll(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 关于 server 的分类: + * 1、participant + + * 2、 + */ + private void startupWithServerState(State state) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建 SessionTracker + * 1、对于 Leader 来说: LeaderSessionTracker + * 2、对于 Learner 来说: LearnerSessionTracker + * 都是用来完成 Session 管理的 + * 最后都是通过一个 SessionTrackerImpl 的组件来完成管理 + */ + if(sessionTracker == null) { + createSessionTracker(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 SessionTracker + */ + startSessionTracker(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动一系列 RequestProcessor + * 有三种情况: + * 1、Leader + * 2、Follower + * 3、Observer + * 这三个组件的 RequestProcessor 的组成各不一样 + * 使用设计模式中的 责任链设计模式来进行设计实现 + * RP1 ==> RP2 ==> RP3 + * 每个 RP 定义,内部都有一个 nextProcessor 的成员变量! + * class RequestProcessor{ + * RequestProcessor nextProcessor; + * processResult(){ + * 1、做业务动作 + * 2、nextProcessor.processResult(); + * } + * } + * Leader处理事务请求 + Leader处理非事务请求: 有一部分工作是一样的,有一部分工作是不一样的 + * 每个RP 只是完成一个复杂流程中的一个动作! 解耦(各司其职) + * - + * 还一种更复杂,更高级的设计实现: + * 状态机 => YARN 的源码的时候: 一个工作分为多个状态 State + Transition(State1 => State2) + */ + setupRequestProcessors(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 其他基础服务 + * RequestThrottler 限流服务, zk-3.4 版本没有这个东西 + */ + startRequestThrottler(); + registerJMX(); + startJvmPauseMonitor(); + registerMetrics(); + + // TODO_MA 注释: 更新状态为 RUNNING + setState(state); + requestPathMetricsCollector.start(); + localSessionEnabled = sessionTracker.isLocalSessionsEnabled(); + notifyAll(); + } + + protected void startJvmPauseMonitor() { + if(this.jvmPauseMonitor != null) { + this.jvmPauseMonitor.serviceStart(); + } + } + + protected void startRequestThrottler() { + requestThrottler = new RequestThrottler(this); + requestThrottler.start(); + } + + protected void setupRequestProcessors() { + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + RequestProcessor syncProcessor = new SyncRequestProcessor(this, finalProcessor); + ((SyncRequestProcessor) syncProcessor).start(); + firstProcessor = new PrepRequestProcessor(this, syncProcessor); + ((PrepRequestProcessor) firstProcessor).start(); + } + + public ZooKeeperServerListener getZooKeeperServerListener() { + return listener; + } + + /** + * Change the server ID used by {@link #createSessionTracker()}. Must be called prior to + * {@link #startup()} being called + * + * @param newId ID to use + */ + public void setCreateSessionTrackerServerId(int newId) { + createSessionTrackerServerId = newId; + } + + protected void createSessionTracker() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 父类实现 + */ + sessionTracker = new SessionTrackerImpl(this, zkDb.getSessionWithTimeOuts(), tickTime, + createSessionTrackerServerId, getZooKeeperServerListener()); + } + + protected void startSessionTracker() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 有两种情况: + * 1、对于 Leader 来说: LeaderSessionTracker + * 2、对于 Learner 来说: LearnerSessionTracker + */ + ((SessionTrackerImpl) sessionTracker).start(); + } + + /** + * Sets the state of ZooKeeper server. After changing the state, it notifies + * the server state change to a registered shutdown handler, if any. + *

      + * The following are the server state transitions: + *

      • During startup the server will be in the INITIAL state.
      • + *
      • After successfully starting, the server sets the state to RUNNING. + *
      • + *
      • The server transitions to the ERROR state if it hits an internal + * error. {@link ZooKeeperServerListenerImpl} notifies any critical resource + * error events, e.g., SyncRequestProcessor not being able to write a txn to + * disk.
      • + *
      • During shutdown the server sets the state to SHUTDOWN, which + * corresponds to the server not running.
      + * + * @param state new server state. + */ + protected void setState(State state) { + this.state = state; + // Notify server state changes to the registered shutdown handler, if any. + if(zkShutdownHandler != null) { + zkShutdownHandler.handle(state); + } else { + LOG.debug( + "ZKShutdownHandler is not registered, so ZooKeeper server" + " won't take any action on ERROR or SHUTDOWN server state changes"); + } + } + + /** + * This can be used while shutting down the server to see whether the server + * is already shutdown or not. + * + * @return true if the server is running or server hits an error, false + * otherwise. + */ + protected boolean canShutdown() { + return state == State.RUNNING || state == State.ERROR; + } + + /** + * @return true if the server is running, false otherwise. + */ + public boolean isRunning() { + return state == State.RUNNING; + } + + public void shutdown() { + shutdown(false); + } + + /** + * Shut down the server instance + * + * @param fullyShutDown true if another server using the same database will not replace this one in the same process + */ + public synchronized void shutdown(boolean fullyShutDown) { + if(!canShutdown()) { + if(fullyShutDown && zkDb != null) { + zkDb.clear(); + } + LOG.debug("ZooKeeper server is not running, so not proceeding to shutdown!"); + return; + } + LOG.info("shutting down"); + + // new RuntimeException("Calling shutdown").printStackTrace(); + setState(State.SHUTDOWN); + + // unregister all metrics that are keeping a strong reference to this object + // subclasses will do their specific clean up + unregisterMetrics(); + + if(requestThrottler != null) { + requestThrottler.shutdown(); + } + + // Since sessionTracker and syncThreads poll we just have to + // set running to false and they will detect it during the poll + // interval. + if(sessionTracker != null) { + sessionTracker.shutdown(); + } + if(firstProcessor != null) { + firstProcessor.shutdown(); + } + if(jvmPauseMonitor != null) { + jvmPauseMonitor.serviceStop(); + } + + if(zkDb != null) { + if(fullyShutDown) { + zkDb.clear(); + } else { + // else there is no need to clear the database + // * When a new quorum is established we can still apply the diff + // on top of the same zkDb data + // * If we fetch a new snapshot from leader, the zkDb will be + // cleared anyway before loading the snapshot + try { + //This will fast forward the database to the latest recorded transactions + zkDb.fastForwardDataBase(); + } catch(IOException e) { + LOG.error("Error updating DB", e); + zkDb.clear(); + } + } + } + + requestPathMetricsCollector.shutdown(); + unregisterJMX(); + } + + protected void unregisterJMX() { + // unregister from JMX + try { + if(jmxDataTreeBean != null) { + MBeanRegistry.getInstance().unregister(jmxDataTreeBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + try { + if(jmxServerBean != null) { + MBeanRegistry.getInstance().unregister(jmxServerBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxServerBean = null; + jmxDataTreeBean = null; + } + + public void incInProcess() { + requestsInProcess.incrementAndGet(); + } + + public void decInProcess() { + requestsInProcess.decrementAndGet(); + if(requestThrottler != null) { + requestThrottler.throttleWake(); + } + } + + public int getInProcess() { + return requestsInProcess.get(); + } + + public int getInflight() { + return requestThrottleInflight(); + } + + private int requestThrottleInflight() { + if(requestThrottler != null) { + return requestThrottler.getInflight(); + } + return 0; + } + + static class PrecalculatedDigest { + final long nodeDigest; + final long treeDigest; + + PrecalculatedDigest(long nodeDigest, long treeDigest) { + this.nodeDigest = nodeDigest; + this.treeDigest = treeDigest; + } + } + + + /** + * This structure is used to facilitate information sharing between PrepRP + * and FinalRP. + */ + static class ChangeRecord { + PrecalculatedDigest precalculatedDigest; + byte[] data; + + ChangeRecord(long zxid, String path, StatPersisted stat, int childCount, List acl) { + this.zxid = zxid; + this.path = path; + this.stat = stat; + this.childCount = childCount; + this.acl = acl; + } + + long zxid; + + String path; + + StatPersisted stat; /* Make sure to create a new object when changing */ + + int childCount; + + List acl; /* Make sure to create a new object when changing */ + + ChangeRecord duplicate(long zxid) { + StatPersisted stat = new StatPersisted(); + if(this.stat != null) { + DataTree.copyStatPersisted(this.stat, stat); + } + ChangeRecord changeRecord = new ChangeRecord(zxid, path, stat, childCount, + acl == null ? new ArrayList<>() : new ArrayList<>(acl)); + changeRecord.precalculatedDigest = precalculatedDigest; + changeRecord.data = data; + return changeRecord; + } + + } + + byte[] generatePasswd(long id) { + Random r = new Random(id ^ superSecret); + byte[] p = new byte[16]; + r.nextBytes(p); + return p; + } + + protected boolean checkPasswd(long sessionId, byte[] passwd) { + return sessionId != 0 && Arrays.equals(passwd, generatePasswd(sessionId)); + } + + long createSession(ServerCnxn cnxn, byte[] passwd, int timeout) { + if(passwd == null) { + // Possible since it's just deserialized from a packet on the wire. + passwd = new byte[0]; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 通过 SessionTracker 创建 SessionID + */ + long sessionId = sessionTracker.createSession(timeout); + + Random r = new Random(sessionId ^ superSecret); + r.nextBytes(passwd); + ByteBuffer to = ByteBuffer.allocate(4); + to.putInt(timeout); + cnxn.setSessionId(sessionId); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 提交创建 Sessoin 的请求 + * 请求代号: OpCode.createSession + */ + Request si = new Request(cnxn, sessionId, 0, OpCode.createSession, to, null); + submitRequest(si); + return sessionId; + } + + /** + * set the owner of this session as owner + * + * @param id the session id + * @param owner the owner of the session + * @throws SessionExpiredException + */ + public void setOwner(long id, Object owner) throws SessionExpiredException { + sessionTracker.setOwner(id, owner); + } + + protected void revalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout) throws IOException { + boolean rc = sessionTracker.touchSession(sessionId, sessionTimeout); + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "Session 0x" + Long.toHexString(sessionId) + " is valid: " + rc); + } + finishSessionInit(cnxn, rc); + } + + public void reopenSession(ServerCnxn cnxn, long sessionId, byte[] passwd, int sessionTimeout) throws IOException { + if(checkPasswd(sessionId, passwd)) { + revalidateSession(cnxn, sessionId, sessionTimeout); + } else { + LOG.warn("Incorrect password from {} for session 0x{}", cnxn.getRemoteSocketAddress(), + Long.toHexString(sessionId)); + finishSessionInit(cnxn, false); + } + } + + public void finishSessionInit(ServerCnxn cnxn, boolean valid) { + // register with JMX + try { + if(valid) { + if(serverCnxnFactory != null && serverCnxnFactory.cnxns.contains(cnxn)) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + serverCnxnFactory.registerConnection(cnxn); + } else if(secureServerCnxnFactory != null && secureServerCnxnFactory.cnxns.contains(cnxn)) { + secureServerCnxnFactory.registerConnection(cnxn); + } + } + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + } + + try { + ConnectResponse rsp = new ConnectResponse(0, valid ? cnxn.getSessionTimeout() : 0, + valid ? cnxn.getSessionId() : 0, // send 0 if session is no + // longer valid + valid ? generatePasswd(cnxn.getSessionId()) : new byte[16]); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos); + bos.writeInt(-1, "len"); + rsp.serialize(bos, "connect"); + if(!cnxn.isOldClient) { + bos.writeBool(this instanceof ReadOnlyZooKeeperServer, "readOnly"); + } + baos.close(); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + bb.putInt(bb.remaining() - 4).rewind(); + cnxn.sendBuffer(bb); + + if(valid) { + LOG.debug("Established session 0x{} with negotiated timeout {} for client {}", + Long.toHexString(cnxn.getSessionId()), cnxn.getSessionTimeout(), cnxn.getRemoteSocketAddress()); + cnxn.enableRecv(); + } else { + + LOG.info("Invalid session 0x{} for client {}, probably expired", Long.toHexString(cnxn.getSessionId()), + cnxn.getRemoteSocketAddress()); + cnxn.sendBuffer(ServerCnxnFactory.closeConn); + } + + } catch(Exception e) { + LOG.warn("Exception while establishing session, closing", e); + cnxn.close(ServerCnxn.DisconnectReason.IO_EXCEPTION_IN_SESSION_INIT); + } + } + + public void closeSession(ServerCnxn cnxn, RequestHeader requestHeader) { + closeSession(cnxn.getSessionId()); + } + + public long getServerId() { + return 0; + } + + /** + * If the underlying Zookeeper server support local session, this method + * will set a isLocalSession to true if a request is associated with + * a local session. + * + * @param si + */ + protected void setLocalSessionFlag(Request si) { + } + + public void submitRequest(Request si) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 请求入 submittedRequests 队列 + * - + * 关于客户端发送过来的请求, + * 最终都会被解析成为 Request 对象,然后加入到 : ZooKeeperServer 中的 submittedRequests 队列! + * (ARequest, BRequest ,... ) => Request(OpCode.type) 来标识! + */ + enqueueRequest(si); + } + + public void enqueueRequest(Request si) { + + // TODO_MA 马中华 注释: 等待 requestThrottler 启动 + if(requestThrottler == null) { + synchronized(this) { + try { + // Since all requests are passed to the request + // processor it should wait for setting up the request + // processor chain. The state will be updated to RUNNING after the setup. + while(state == State.INITIAL) { + wait(1000); + } + } catch(InterruptedException e) { + LOG.warn("Unexpected interruption", e); + } + if(requestThrottler == null) { + throw new RuntimeException("Not started"); + } + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: requestThrottler = 请求限流器 + */ + requestThrottler.submitRequest(si); + } + + public void submitRequestNow(Request si) { + + // TODO_MA 马中华 注释: 如果走了这个分支,表示集群启动并未完成。 + // TODO_MA 马中华 注释: 因为在 QuorumPeer 启动过程中;大致的启动顺序是: + // TODO_MA 马中华 注释: 1、冷启动数据恢复 + // TODO_MA 马中华 注释: 2、初始化和启动 NIOServerCnxnFactory + // TODO_MA 马中华 注释: 3、选举,状态同步,启动基础服务 + // TODO_MA 马中华 注释: 这个逻辑,其实在 限流器哪儿有见到过,是一样的代码编写方式 + if(firstProcessor == null) { + synchronized(this) { + try { + // Since all requests are passed to the request + // processor it should wait for setting up the request + // processor chain. The state will be updated to RUNNING + // after the setup. + while(state == State.INITIAL) { + wait(1000); + } + } catch(InterruptedException e) { + LOG.warn("Unexpected interruption", e); + } + if(firstProcessor == null || state != State.RUNNING) { + throw new RuntimeException("Not started"); + } + } + } + try { + + // TODO_MA 注释: 更新 session 超时时间信息 + // TODO_MA 注释: 只要有一个请求需要处理,这个请求必然属于某个会话。 + // TODO_MA 注释: 首先第一件事,先更新会话的超时时间! + touch(si.cnxn); + + // TODO_MA 注释: 根据请求类型,校验请求数据包的合法性 + boolean validpacket = Request.isValid(si.type); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 开始 RequestProcessor 的处理 + */ + if(validpacket) { + setLocalSessionFlag(si); + + // TODO_MA 注释: firstProcessor 开始处理 + // TODO_MA 注释: 到此为止,终于开始进行责任链的 RP 处理了 + // TODO_MA 注释: si 是一个通用的请求对象。事务请求,非事务请求 + firstProcessor.processRequest(si); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、有可能当前服务器是 leader + * 2、有可能当前服务器是 follower + */ + if(si.cnxn != null) { + incInProcess(); + } + } else { + LOG.warn("Received packet at server of unknown type {}", si.type); + // Update request accounting/throttling limits + requestFinished(si); + new UnimplementedRequestProcessor().processRequest(si); + } + } catch(MissingSessionException e) { + LOG.debug("Dropping request.", e); + // Update request accounting/throttling limits + requestFinished(si); + } catch(RequestProcessorException e) { + LOG.error("Unable to process request", e); + // Update request accounting/throttling limits + requestFinished(si); + } + } + + public static int getSnapCount() { + String sc = System.getProperty(SNAP_COUNT); + try { + int snapCount = Integer.parseInt(sc); + + // snapCount must be 2 or more. See org.apache.zookeeper.server.SyncRequestProcessor + if(snapCount < 2) { + LOG.warn("SnapCount should be 2 or more. Now, snapCount is reset to 2"); + snapCount = 2; + } + return snapCount; + } catch(Exception e) { + return 100000; + } + } + + public int getGlobalOutstandingLimit() { + String sc = System.getProperty(GLOBAL_OUTSTANDING_LIMIT); + int limit; + try { + limit = Integer.parseInt(sc); + } catch(Exception e) { + limit = 1000; + } + return limit; + } + + public static long getSnapSizeInBytes() { + long size = Long.getLong("zookeeper.snapSizeLimitInKb", 4194304L); // 4GB by default + if(size <= 0) { + LOG.info("zookeeper.snapSizeLimitInKb set to a non-positive value {}; disabling feature", size); + } + return size * 1024; // Convert to bytes + } + + public void setServerCnxnFactory(ServerCnxnFactory factory) { + serverCnxnFactory = factory; + } + + public ServerCnxnFactory getServerCnxnFactory() { + return serverCnxnFactory; + } + + public ServerCnxnFactory getSecureServerCnxnFactory() { + return secureServerCnxnFactory; + } + + public void setSecureServerCnxnFactory(ServerCnxnFactory factory) { + secureServerCnxnFactory = factory; + } + + /** + * return the last proceesed id from the + * datatree + */ + public long getLastProcessedZxid() { + return zkDb.getDataTreeLastProcessedZxid(); + } + + /** + * return the outstanding requests + * in the queue, which havent been + * processed yet + */ + public long getOutstandingRequests() { + return getInProcess(); + } + + /** + * return the total number of client connections that are alive + * to this server + */ + public int getNumAliveConnections() { + int numAliveConnections = 0; + + if(serverCnxnFactory != null) { + numAliveConnections += serverCnxnFactory.getNumAliveConnections(); + } + + if(secureServerCnxnFactory != null) { + numAliveConnections += secureServerCnxnFactory.getNumAliveConnections(); + } + + return numAliveConnections; + } + + /** + * trunccate the log to get in sync with others + * if in a quorum + * + * @param zxid the zxid that it needs to get in sync + * with others + * @throws IOException + */ + public void truncateLog(long zxid) throws IOException { + this.zkDb.truncateLog(zxid); + } + + public int getTickTime() { + return tickTime; + } + + public void setTickTime(int tickTime) { + LOG.info("tickTime set to {}", tickTime); + this.tickTime = tickTime; + } + + public int getMinSessionTimeout() { + return minSessionTimeout; + } + + public void setMinSessionTimeout(int min) { + this.minSessionTimeout = min == -1 ? tickTime * 2 : min; + LOG.info("minSessionTimeout set to {}", this.minSessionTimeout); + } + + public int getMaxSessionTimeout() { + return maxSessionTimeout; + } + + public void setMaxSessionTimeout(int max) { + this.maxSessionTimeout = max == -1 ? tickTime * 20 : max; + LOG.info("maxSessionTimeout set to {}", this.maxSessionTimeout); + } + + public int getClientPortListenBacklog() { + return listenBacklog; + } + + public void setClientPortListenBacklog(int backlog) { + this.listenBacklog = backlog; + LOG.info("clientPortListenBacklog set to {}", backlog); + } + + public int getClientPort() { + return serverCnxnFactory != null ? serverCnxnFactory.getLocalPort() : -1; + } + + public int getSecureClientPort() { + return secureServerCnxnFactory != null ? secureServerCnxnFactory.getLocalPort() : -1; + } + + /** + * Maximum number of connections allowed from particular host (ip) + */ + public int getMaxClientCnxnsPerHost() { + if(serverCnxnFactory != null) { + return serverCnxnFactory.getMaxClientCnxnsPerHost(); + } + if(secureServerCnxnFactory != null) { + return secureServerCnxnFactory.getMaxClientCnxnsPerHost(); + } + return -1; + } + + public void setTxnLogFactory(FileTxnSnapLog txnLog) { + this.txnLogFactory = txnLog; + } + + public FileTxnSnapLog getTxnLogFactory() { + return this.txnLogFactory; + } + + /** + * Returns the elapsed sync of time of transaction log in milliseconds. + */ + public long getTxnLogElapsedSyncTime() { + return txnLogFactory.getTxnLogElapsedSyncTime(); + } + + public String getState() { + return "standalone"; + } + + public void dumpEphemerals(PrintWriter pwriter) { + zkDb.dumpEphemerals(pwriter); + } + + public Map> getEphemerals() { + return zkDb.getEphemerals(); + } + + public double getConnectionDropChance() { + return connThrottle.getDropChance(); + } + + @SuppressFBWarnings(value = "IS2_INCONSISTENT_SYNC", justification = "the value won't change after startup") + public void processConnectRequest(ServerCnxn cnxn, + ByteBuffer incomingBuffer) throws IOException, ClientCnxnLimitException { + + // TODO_MA 注释: 反序列化得到链接请求对象 + BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(incomingBuffer)); + ConnectRequest connReq = new ConnectRequest(); + connReq.deserialize(bia, "connect"); + + LOG.debug("Session establishment request from client {} client's lastZxid is 0x{}", cnxn.getRemoteSocketAddress(), + Long.toHexString(connReq.getLastZxidSeen())); + + // TODO_MA 注释: 如果是重连,则 sessionId != 0 ,否则是等于 0 的 + long sessionId = connReq.getSessionId(); + + int tokensNeeded = 1; + if(connThrottle.isConnectionWeightEnabled()) { + if(sessionId == 0) { + if(localSessionEnabled) { + tokensNeeded = connThrottle.getRequiredTokensForLocal(); + } else { + tokensNeeded = connThrottle.getRequiredTokensForGlobal(); + } + } else { + tokensNeeded = connThrottle.getRequiredTokensForRenew(); + } + } + + if(!connThrottle.checkLimit(tokensNeeded)) { + throw new ClientCnxnLimitException(); + } + ServerMetrics.getMetrics().CONNECTION_TOKEN_DEFICIT.add(connThrottle.getDeficit()); + + ServerMetrics.getMetrics().CONNECTION_REQUEST_COUNT.add(1); + + boolean readOnly = false; + try { + readOnly = bia.readBool("readOnly"); + cnxn.isOldClient = false; + } catch(IOException e) { + // this is ok -- just a packet from an old client which + // doesn't contain readOnly field + LOG.warn("Connection request from old client {}; will be dropped if server is in r-o mode", + cnxn.getRemoteSocketAddress()); + } + if(!readOnly && this instanceof ReadOnlyZooKeeperServer) { + String msg = "Refusing session request for not-read-only client " + cnxn.getRemoteSocketAddress(); + LOG.info(msg); + throw new CloseRequestException(msg, ServerCnxn.DisconnectReason.CLIENT_ZXID_AHEAD); + } + + // TODO_MA 注释: 读取链接请求中的 zxid + if(connReq.getLastZxidSeen() > zkDb.dataTree.lastProcessedZxid) { + String msg = "Refusing session request for client " + cnxn + .getRemoteSocketAddress() + " as it has seen zxid 0x" + Long + .toHexString(connReq.getLastZxidSeen()) + " our last zxid is 0x" + Long + .toHexString(getZKDatabase().getDataTreeLastProcessedZxid()) + " client must try another server"; + + LOG.info(msg); + throw new CloseRequestException(msg, ServerCnxn.DisconnectReason.NOT_READ_ONLY_CLIENT); + } + int sessionTimeout = connReq.getTimeOut(); + byte[] passwd = connReq.getPasswd(); + int minSessionTimeout = getMinSessionTimeout(); + if(sessionTimeout < minSessionTimeout) { + sessionTimeout = minSessionTimeout; + } + int maxSessionTimeout = getMaxSessionTimeout(); + if(sessionTimeout > maxSessionTimeout) { + sessionTimeout = maxSessionTimeout; + } + cnxn.setSessionTimeout(sessionTimeout); + // We don't want to receive any packets until we are sure that the session is setup + cnxn.disableRecv(); + + // TODO_MA 注释: 如果 sessionId == 0,则是一个崭新的请求,创建 Session + if(sessionId == 0) { + + // TODO_MA 注释: 是一个事务操作 + long id = createSession(cnxn, passwd, sessionTimeout); + LOG.debug( + "Client attempting to establish new session: session = 0x{}, zxid = 0x{}, timeout = {}, address = {}", + Long.toHexString(id), Long.toHexString(connReq.getLastZxidSeen()), connReq.getTimeOut(), + cnxn.getRemoteSocketAddress()); + } + + // TODO_MA 注释: 否则,不是新链接请求,则恢复 session + else { + long clientSessionId = connReq.getSessionId(); + LOG.debug("Client attempting to renew session: session = 0x{}, zxid = 0x{}, timeout = {}, address = {}", + Long.toHexString(clientSessionId), Long.toHexString(connReq.getLastZxidSeen()), connReq.getTimeOut(), + cnxn.getRemoteSocketAddress()); + if(serverCnxnFactory != null) { + serverCnxnFactory.closeSession(sessionId, ServerCnxn.DisconnectReason.CLIENT_RECONNECT); + } + if(secureServerCnxnFactory != null) { + secureServerCnxnFactory.closeSession(sessionId, ServerCnxn.DisconnectReason.CLIENT_RECONNECT); + } + cnxn.setSessionId(sessionId); + + // TODO_MA 注释: 重新启用 session + reopenSession(cnxn, sessionId, passwd, sessionTimeout); + ServerMetrics.getMetrics().CONNECTION_REVALIDATE_COUNT.add(1); + + } + } + + public boolean shouldThrottle(long outStandingCount) { + int globalOutstandingLimit = getGlobalOutstandingLimit(); + if(globalOutstandingLimit < getInflight() || globalOutstandingLimit < getInProcess()) { + return outStandingCount > 0; + } + return false; + } + + long getFlushDelay() { + return flushDelay; + } + + static void setFlushDelay(long delay) { + LOG.info("{}={}", FLUSH_DELAY, delay); + flushDelay = delay; + } + + long getMaxWriteQueuePollTime() { + return maxWriteQueuePollTime; + } + + static void setMaxWriteQueuePollTime(long maxTime) { + LOG.info("{}={}", MAX_WRITE_QUEUE_POLL_SIZE, maxTime); + maxWriteQueuePollTime = maxTime; + } + + int getMaxBatchSize() { + return maxBatchSize; + } + + static void setMaxBatchSize(int size) { + LOG.info("{}={}", MAX_BATCH_SIZE, size); + maxBatchSize = size; + } + + private void initLargeRequestThrottlingSettings() { + + // TODO_MA 注释: 大请求字节数:默认 100M + setLargeRequestMaxBytes(Integer.getInteger("zookeeper.largeRequestMaxBytes", largeRequestMaxBytes)); + setLargeRequestThreshold(Integer.getInteger("zookeeper.largeRequestThreshold", -1)); + } + + public int getLargeRequestMaxBytes() { + return largeRequestMaxBytes; + } + + public void setLargeRequestMaxBytes(int bytes) { + if(bytes <= 0) { + LOG.warn("Invalid max bytes for all large requests {}. It should be a positive number.", bytes); + LOG.warn("Will not change the setting. The max bytes stay at {}", largeRequestMaxBytes); + } else { + largeRequestMaxBytes = bytes; + LOG.info("The max bytes for all large requests are set to {}", largeRequestMaxBytes); + } + } + + public int getLargeRequestThreshold() { + return largeRequestThreshold; + } + + public void setLargeRequestThreshold(int threshold) { + if(threshold == 0 || threshold < -1) { + LOG.warn("Invalid large request threshold {}. It should be -1 or positive. Setting to -1 ", threshold); + largeRequestThreshold = -1; + } else { + largeRequestThreshold = threshold; + LOG.info("The large request threshold is set to {}", largeRequestThreshold); + } + } + + public int getLargeRequestBytes() { + return currentLargeRequestBytes.get(); + } + + private boolean isLargeRequest(int length) { + // The large request limit is disabled when threshold is -1 + if(largeRequestThreshold == -1) { + return false; + } + return length > largeRequestThreshold; + } + + public boolean checkRequestSizeWhenReceivingMessage(int length) throws IOException { + if(!isLargeRequest(length)) { + return true; + } + if(currentLargeRequestBytes.get() + length <= largeRequestMaxBytes) { + return true; + } else { + ServerMetrics.getMetrics().LARGE_REQUESTS_REJECTED.add(1); + throw new IOException("Rejecting large request"); + } + + } + + private boolean checkRequestSizeWhenMessageReceived(int length) throws IOException { + if(!isLargeRequest(length)) { + return true; + } + + // TODO_MA 注释: 100M + int bytes = currentLargeRequestBytes.addAndGet(length); + + // TODO_MA 注释: 是否是大请求的校验处理 + if(bytes > largeRequestMaxBytes) { + currentLargeRequestBytes.addAndGet(-length); + ServerMetrics.getMetrics().LARGE_REQUESTS_REJECTED.add(1); + throw new IOException("Rejecting large request"); + } + return true; + } + + public void requestFinished(Request request) { + int largeRequestLength = request.getLargeRequestSize(); + if(largeRequestLength != -1) { + currentLargeRequestBytes.addAndGet(-largeRequestLength); + } + } + + public void processPacket(ServerCnxn cnxn, ByteBuffer incomingBuffer) throws IOException { + + // TODO_MA 注释: 反序列化得到 RequestHeader + // We have the request, now process and setup for next + InputStream bais = new ByteBufferInputStream(incomingBuffer); + BinaryInputArchive bia = BinaryInputArchive.getArchive(bais); + RequestHeader h = new RequestHeader(); + h.deserialize(bia, "header"); + + // Need to increase the outstanding request count first, otherwise + // there might be a race condition that it enabled recv after + // processing request and then disabled when check throttling. + // + // Be aware that we're actually checking the global outstanding + // request before this request. + // + // It's fine if the IOException thrown before we decrease the count + // in cnxn, since it will close the cnxn anyway. + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 判断是否需要进行限流 + * 从 zk-3.5 新增加的一个工鞥: Throttle 限流 + */ + cnxn.incrOutstandingAndCheckThrottle(h); + + // Through the magic of byte buffers, txn will not be + // pointing to the start of the txn + incomingBuffer = incomingBuffer.slice(); + + // TODO_MA 注释: 认证请求 + if(h.getType() == OpCode.auth) { + LOG.info("got auth packet {}", cnxn.getRemoteSocketAddress()); + AuthPacket authPacket = new AuthPacket(); + ByteBufferInputStream.byteBuffer2Record(incomingBuffer, authPacket); + String scheme = authPacket.getScheme(); + ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(scheme); + Code authReturn = KeeperException.Code.AUTHFAILED; + if(ap != null) { + try { + // handleAuthentication may close the connection, to allow the client to choose + // a different server to connect to. + authReturn = ap.handleAuthentication(new ServerAuthenticationProvider.ServerObjs(this, cnxn), + authPacket.getAuth()); + } catch(RuntimeException e) { + LOG.warn("Caught runtime exception from AuthenticationProvider: {}", scheme, e); + authReturn = KeeperException.Code.AUTHFAILED; + } + } + if(authReturn == KeeperException.Code.OK) { + LOG.debug("Authentication succeeded for scheme: {}", scheme); + LOG.info("auth success {}", cnxn.getRemoteSocketAddress()); + ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.OK.intValue()); + cnxn.sendResponse(rh, null, null); + } else { + if(ap == null) { + LOG.warn("No authentication provider for scheme: {} has {}", scheme, + ProviderRegistry.listProviders()); + } else { + LOG.warn("Authentication failed for scheme: {}", scheme); + } + // send a response... + ReplyHeader rh = new ReplyHeader(h.getXid(), 0, KeeperException.Code.AUTHFAILED.intValue()); + cnxn.sendResponse(rh, null, null); + // ... and close connection + cnxn.sendBuffer(ServerCnxnFactory.closeConn); + cnxn.disableRecv(); + } + return; + } + + // TODO_MA 注释: SASL 请求 + // TODO_MA 注释: SASL是一种用来扩充C/S模式验证能力的机制认证机制, 全称Simple Authentication and Security Layer + else if(h.getType() == OpCode.sasl) { + processSasl(incomingBuffer, cnxn, h); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 正常请求 + */ + else { + + // TODO_MA 注释: 认证没通过,返回响应 + if(shouldRequireClientSaslAuth() && !hasCnxSASLAuthenticated(cnxn)) { + ReplyHeader replyHeader = new ReplyHeader(h.getXid(), 0, Code.SESSIONCLOSEDREQUIRESASLAUTH.intValue()); + cnxn.sendResponse(replyHeader, null, "response"); + cnxn.sendCloseSession(); + cnxn.disableRecv(); + } + + // TODO_MA 注释: 正常处理 + else { + + // TODO_MA 注释: 构建请求对象 + Request si = new Request(cnxn, cnxn.getSessionId(), h.getXid(), h.getType(), incomingBuffer, + cnxn.getAuthInfo()); + int length = incomingBuffer.limit(); + if(isLargeRequest(length)) { + // checkRequestSize will throw IOException if request is rejected + checkRequestSizeWhenMessageReceived(length); + si.setLargeRequestSize(length); + } + si.setOwner(ServerCnxn.me); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 提交请求 + */ + submitRequest(si); + } + } + } + + private static boolean shouldAllowSaslFailedClientsConnect() { + return Boolean.getBoolean(ALLOW_SASL_FAILED_CLIENTS); + } + + private static boolean shouldRequireClientSaslAuth() { + return Boolean.getBoolean(SESSION_REQUIRE_CLIENT_SASL_AUTH); + } + + private boolean hasCnxSASLAuthenticated(ServerCnxn cnxn) { + for(Id id : cnxn.getAuthInfo()) { + if(id.getScheme().equals(SASL_AUTH_SCHEME)) { + return true; + } + } + return false; + } + + private void processSasl(ByteBuffer incomingBuffer, ServerCnxn cnxn, RequestHeader requestHeader) throws IOException { + LOG.debug("Responding to client SASL token."); + GetSASLRequest clientTokenRecord = new GetSASLRequest(); + ByteBufferInputStream.byteBuffer2Record(incomingBuffer, clientTokenRecord); + byte[] clientToken = clientTokenRecord.getToken(); + LOG.debug("Size of client SASL token: {}", clientToken.length); + byte[] responseToken = null; + try { + ZooKeeperSaslServer saslServer = cnxn.zooKeeperSaslServer; + try { + // note that clientToken might be empty (clientToken.length == 0): + // if using the DIGEST-MD5 mechanism, clientToken will be empty at the beginning of the + // SASL negotiation process. + responseToken = saslServer.evaluateResponse(clientToken); + if(saslServer.isComplete()) { + String authorizationID = saslServer.getAuthorizationID(); + LOG.info("adding SASL authorization for authorizationID: {}", authorizationID); + cnxn.addAuthInfo(new Id("sasl", authorizationID)); + if(System.getProperty("zookeeper.superUser") != null && authorizationID + .equals(System.getProperty("zookeeper.superUser"))) { + cnxn.addAuthInfo(new Id("super", "")); + } + } + } catch(SaslException e) { + LOG.warn("Client {} failed to SASL authenticate: {}", cnxn.getRemoteSocketAddress(), e); + if(shouldAllowSaslFailedClientsConnect() && !shouldRequireClientSaslAuth()) { + LOG.warn("Maintaining client connection despite SASL authentication failure."); + } else { + int error; + if(shouldRequireClientSaslAuth()) { + LOG.warn( + "Closing client connection due to server requires client SASL authenticaiton," + "but client SASL authentication has failed, or client is not configured with SASL " + "authentication."); + error = Code.SESSIONCLOSEDREQUIRESASLAUTH.intValue(); + } else { + LOG.warn("Closing client connection due to SASL authentication failure."); + error = Code.AUTHFAILED.intValue(); + } + + ReplyHeader replyHeader = new ReplyHeader(requestHeader.getXid(), 0, error); + cnxn.sendResponse(replyHeader, new SetSASLResponse(null), "response"); + cnxn.sendCloseSession(); + cnxn.disableRecv(); + return; + } + } + } catch(NullPointerException e) { + LOG.error("cnxn.saslServer is null: cnxn object did not initialize its saslServer properly."); + } + if(responseToken != null) { + LOG.debug("Size of server SASL response: {}", responseToken.length); + } + + ReplyHeader replyHeader = new ReplyHeader(requestHeader.getXid(), 0, Code.OK.intValue()); + Record record = new SetSASLResponse(responseToken); + cnxn.sendResponse(replyHeader, record, "response"); + } + + // entry point for quorum/Learner.java + public ProcessTxnResult processTxn(TxnHeader hdr, Record txn) { + processTxnForSessionEvents(null, hdr, txn); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return processTxnInDB(hdr, txn, null); + } + + // entry point for FinalRequestProcessor.java + public ProcessTxnResult processTxn(Request request) { + TxnHeader hdr = request.getHdr(); + processTxnForSessionEvents(request, hdr, request.getTxn()); + + final boolean writeRequest = (hdr != null); + final boolean quorumRequest = request.isQuorum(); + + // return fast w/o synchronization when we get a read + if(!writeRequest && !quorumRequest) { + return new ProcessTxnResult(); + } + + // TODO_MA 注释: + synchronized(outstandingChanges) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 在 DataTree 中执行请求 + */ + ProcessTxnResult rc = processTxnInDB(hdr, request.getTxn(), request.getTxnDigest()); + + // request.hdr is set for write requests, which are the only ones + // that add to outstandingChanges. + if(writeRequest) { + long zxid = hdr.getZxid(); + while(!outstandingChanges.isEmpty() && outstandingChanges.peek().zxid <= zxid) { + ChangeRecord cr = outstandingChanges.remove(); + ServerMetrics.getMetrics().OUTSTANDING_CHANGES_REMOVED.add(1); + if(cr.zxid < zxid) { + LOG.warn("Zxid outstanding 0x{} is less than current 0x{}", Long.toHexString(cr.zxid), + Long.toHexString(zxid)); + } + if(outstandingChangesForPath.get(cr.path) == cr) { + outstandingChangesForPath.remove(cr.path); + } + } + } + + // TODO_MA 注释: 将刚才执行的 PROPOSAL 缓存在内存中 + // do not add non quorum packets to the queue. + if(quorumRequest) { + getZKDatabase().addCommittedProposal(request); + } + return rc; + } + } + + private void processTxnForSessionEvents(Request request, TxnHeader hdr, Record txn) { + int opCode = (request == null) ? hdr.getType() : request.type; + long sessionId = (request == null) ? hdr.getClientId() : request.sessionId; + + if(opCode == OpCode.createSession) { + if(hdr != null && txn instanceof CreateSessionTxn) { + CreateSessionTxn cst = (CreateSessionTxn) txn; + sessionTracker.commitSession(sessionId, cst.getTimeOut()); + } else if(request == null || !request.isLocalSession()) { + LOG.warn("*****>>>>> Got {} {}", txn.getClass(), txn.toString()); + } + } else if(opCode == OpCode.closeSession) { + sessionTracker.removeSession(sessionId); + } + } + + private ProcessTxnResult processTxnInDB(TxnHeader hdr, Record txn, TxnDigest digest) { + if(hdr == null) { + return new ProcessTxnResult(); + } else { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 真正的处理一个事务,内部会调用 DataTree 去完成 + */ + return getZKDatabase().processTxn(hdr, txn, digest); + } + } + + public Map> getSessionExpiryMap() { + return sessionTracker.getSessionExpiryMap(); + } + + /** + * This method is used to register the ZooKeeperServerShutdownHandler to get + * server's error or shutdown state change notifications. + * {@link ZooKeeperServerShutdownHandler#handle(State)} will be called for + * every server state changes {@link #setState(State)}. + * + * @param zkShutdownHandler shutdown handler + */ + void registerServerShutdownHandler(ZooKeeperServerShutdownHandler zkShutdownHandler) { + this.zkShutdownHandler = zkShutdownHandler; + } + + public boolean isResponseCachingEnabled() { + return isResponseCachingEnabled; + } + + public void setResponseCachingEnabled(boolean isEnabled) { + isResponseCachingEnabled = isEnabled; + } + + public ResponseCache getReadResponseCache() { + return isResponseCachingEnabled ? readResponseCache : null; + } + + public ResponseCache getGetChildrenResponseCache() { + return isResponseCachingEnabled ? getChildrenResponseCache : null; + } + + protected void registerMetrics() { + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + + final ZKDatabase zkdb = this.getZKDatabase(); + final ServerStats stats = this.serverStats(); + + rootContext.registerGauge("avg_latency", stats::getAvgLatency); + + rootContext.registerGauge("max_latency", stats::getMaxLatency); + rootContext.registerGauge("min_latency", stats::getMinLatency); + + rootContext.registerGauge("packets_received", stats::getPacketsReceived); + rootContext.registerGauge("packets_sent", stats::getPacketsSent); + rootContext.registerGauge("num_alive_connections", stats::getNumAliveClientConnections); + + rootContext.registerGauge("outstanding_requests", stats::getOutstandingRequests); + rootContext.registerGauge("uptime", stats::getUptime); + + rootContext.registerGauge("znode_count", zkdb::getNodeCount); + + rootContext.registerGauge("watch_count", zkdb.getDataTree()::getWatchCount); + rootContext.registerGauge("ephemerals_count", zkdb.getDataTree()::getEphemeralsCount); + + rootContext.registerGauge("approximate_data_size", zkdb.getDataTree()::cachedApproximateDataSize); + + rootContext.registerGauge("global_sessions", zkdb::getSessionCount); + rootContext.registerGauge("local_sessions", this.getSessionTracker()::getLocalSessionCount); + + OSMXBean osMbean = new OSMXBean(); + rootContext.registerGauge("open_file_descriptor_count", osMbean::getOpenFileDescriptorCount); + rootContext.registerGauge("max_file_descriptor_count", osMbean::getMaxFileDescriptorCount); + rootContext.registerGauge("connection_drop_probability", this::getConnectionDropChance); + + rootContext.registerGauge("last_client_response_size", stats.getClientResponseStats()::getLastBufferSize); + rootContext.registerGauge("max_client_response_size", stats.getClientResponseStats()::getMaxBufferSize); + rootContext.registerGauge("min_client_response_size", stats.getClientResponseStats()::getMinBufferSize); + + rootContext.registerGauge("outstanding_tls_handshake", this::getOutstandingHandshakeNum); + rootContext.registerGauge("auth_failed_count", stats::getAuthFailedCount); + rootContext.registerGauge("non_mtls_remote_conn_count", stats::getNonMTLSRemoteConnCount); + rootContext.registerGauge("non_mtls_local_conn_count", stats::getNonMTLSLocalConnCount); + } + + protected void unregisterMetrics() { + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + + rootContext.unregisterGauge("avg_latency"); + + rootContext.unregisterGauge("max_latency"); + rootContext.unregisterGauge("min_latency"); + + rootContext.unregisterGauge("packets_received"); + rootContext.unregisterGauge("packets_sent"); + rootContext.unregisterGauge("num_alive_connections"); + + rootContext.unregisterGauge("outstanding_requests"); + rootContext.unregisterGauge("uptime"); + + rootContext.unregisterGauge("znode_count"); + + rootContext.unregisterGauge("watch_count"); + rootContext.unregisterGauge("ephemerals_count"); + rootContext.unregisterGauge("approximate_data_size"); + + rootContext.unregisterGauge("global_sessions"); + rootContext.unregisterGauge("local_sessions"); + + rootContext.unregisterGauge("open_file_descriptor_count"); + rootContext.unregisterGauge("max_file_descriptor_count"); + rootContext.unregisterGauge("connection_drop_probability"); + + rootContext.unregisterGauge("last_client_response_size"); + rootContext.unregisterGauge("max_client_response_size"); + rootContext.unregisterGauge("min_client_response_size"); + + rootContext.unregisterGauge("auth_failed_count"); + rootContext.unregisterGauge("non_mtls_remote_conn_count"); + rootContext.unregisterGauge("non_mtls_local_conn_count"); + + + } + + /** + * Hook into admin server, useful to expose additional data + * that do not represent metrics. + * + * @param response a sink which collects the data. + */ + public void dumpMonitorValues(BiConsumer response) { + ServerStats stats = serverStats(); + response.accept("version", Version.getFullVersion()); + response.accept("server_state", stats.getServerState()); + } + + /** + * Grant or deny authorization to an operation on a node as a function of: + * + * @param cnxn : the server connection + * @param acl : set of ACLs for the node + * @param perm : the permission that the client is requesting + * @param ids : the credentials supplied by the client + * @param path : the ZNode path + * @param setAcls : for set ACL operations, the list of ACLs being set. Otherwise null. + */ + public void checkACL(ServerCnxn cnxn, List acl, int perm, List ids, String path, + List setAcls) throws KeeperException.NoAuthException { + if(skipACL) { + return; + } + + LOG.debug("Permission requested: {} ", perm); + LOG.debug("ACLs for node: {}", acl); + LOG.debug("Client credentials: {}", ids); + + if(acl == null || acl.size() == 0) { + return; + } + for(Id authId : ids) { + if(authId.getScheme().equals("super")) { + return; + } + } + for(ACL a : acl) { + Id id = a.getId(); + if((a.getPerms() & perm) != 0) { + if(id.getScheme().equals("world") && id.getId().equals("anyone")) { + return; + } + ServerAuthenticationProvider ap = ProviderRegistry.getServerProvider(id.getScheme()); + if(ap != null) { + for(Id authId : ids) { + if(authId.getScheme().equals(id.getScheme()) && ap + .matches(new ServerAuthenticationProvider.ServerObjs(this, cnxn), + new ServerAuthenticationProvider.MatchValues(path, authId.getId(), id.getId(), + perm, setAcls))) { + return; + } + } + } + } + } + throw new KeeperException.NoAuthException(); + } + + public static boolean isDigestEnabled() { + return digestEnabled; + } + + public static void setDigestEnabled(boolean digestEnabled) { + LOG.info("{} = {}", ZOOKEEPER_DIGEST_ENABLED, digestEnabled); + ZooKeeperServer.digestEnabled = digestEnabled; + } + + /** + * Trim a path to get the immediate predecessor. + * + * @param path + * @return + * @throws KeeperException.BadArgumentsException + */ + private String parentPath(String path) throws KeeperException.BadArgumentsException { + int lastSlash = path.lastIndexOf('/'); + if(lastSlash == -1 || path.indexOf('\0') != -1 || getZKDatabase().isSpecialPath(path)) { + throw new KeeperException.BadArgumentsException(path); + } + return lastSlash == 0 ? "/" : path.substring(0, lastSlash); + } + + private String effectiveACLPath( + Request request) throws KeeperException.BadArgumentsException, KeeperException.InvalidACLException { + boolean mustCheckACL = false; + String path = null; + List acl = null; + + switch(request.type) { + case OpCode.create: + case OpCode.create2: { + CreateRequest req = new CreateRequest(); + if(buffer2Record(request.request, req)) { + mustCheckACL = true; + acl = req.getAcl(); + path = parentPath(req.getPath()); + } + break; + } + case OpCode.delete: { + DeleteRequest req = new DeleteRequest(); + if(buffer2Record(request.request, req)) { + path = parentPath(req.getPath()); + } + break; + } + case OpCode.setData: { + SetDataRequest req = new SetDataRequest(); + if(buffer2Record(request.request, req)) { + path = req.getPath(); + } + break; + } + case OpCode.setACL: { + SetACLRequest req = new SetACLRequest(); + if(buffer2Record(request.request, req)) { + mustCheckACL = true; + acl = req.getAcl(); + path = req.getPath(); + } + break; + } + } + + if(mustCheckACL) { + /* we ignore the extrapolated ACL returned by fixupACL because + * we only care about it being well-formed (and if it isn't, an + * exception will be raised). + */ + PrepRequestProcessor.fixupACL(path, request.authInfo, acl); + } + + return path; + } + + private int effectiveACLPerms(Request request) { + switch(request.type) { + case OpCode.create: + case OpCode.create2: + return ZooDefs.Perms.CREATE; + case OpCode.delete: + return ZooDefs.Perms.DELETE; + case OpCode.setData: + return ZooDefs.Perms.WRITE; + case OpCode.setACL: + return ZooDefs.Perms.ADMIN; + default: + return ZooDefs.Perms.ALL; + } + } + + /** + * Check Write Requests for Potential Access Restrictions + *

      + * Before a request is being proposed to the quorum, lets check it + * against local ACLs. Non-write requests (read, session, etc.) + * are passed along. Invalid requests are sent a response. + *

      + * While we are at it, if the request will set an ACL: make sure it's + * a valid one. + * + * @param request + * @return true if request is permitted, false if not. + * @throws java.io.IOException + */ + public boolean authWriteRequest(Request request) { + int err; + String pathToCheck; + + if(!enableEagerACLCheck) { + return true; + } + + err = KeeperException.Code.OK.intValue(); + + try { + pathToCheck = effectiveACLPath(request); + if(pathToCheck != null) { + checkACL(request.cnxn, zkDb.getACL(pathToCheck, null), effectiveACLPerms(request), request.authInfo, + pathToCheck, null); + } + } catch(KeeperException.NoAuthException e) { + LOG.debug("Request failed ACL check", e); + err = e.code().intValue(); + } catch(KeeperException.InvalidACLException e) { + LOG.debug("Request has an invalid ACL check", e); + err = e.code().intValue(); + } catch(KeeperException.NoNodeException e) { + LOG.debug("ACL check against non-existent node: {}", e.getMessage()); + } catch(KeeperException.BadArgumentsException e) { + LOG.debug("ACL check against illegal node path: {}", e.getMessage()); + } catch(Throwable t) { + LOG.error("Uncaught exception in authWriteRequest with: ", t); + throw t; + } finally { + if(err != KeeperException.Code.OK.intValue()) { + /* This request has a bad ACL, so we are dismissing it early. */ + decInProcess(); + ReplyHeader rh = new ReplyHeader(request.cxid, 0, err); + try { + request.cnxn.sendResponse(rh, null, null); + } catch(IOException e) { + LOG.error("IOException : {}", e); + } + } + } + + return err == KeeperException.Code.OK.intValue(); + } + + private boolean buffer2Record(ByteBuffer request, Record record) { + boolean rv = false; + try { + ByteBufferInputStream.byteBuffer2Record(request, record); + request.rewind(); + rv = true; + } catch(IOException ex) { + } + + return rv; + } + + public int getOutstandingHandshakeNum() { + if(serverCnxnFactory instanceof NettyServerCnxnFactory) { + return ((NettyServerCnxnFactory) serverCnxnFactory).getOutstandingHandshakeNum(); + } else { + return 0; + } + } + + public boolean isReconfigEnabled() { + return this.reconfigEnabled; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerBean.java new file mode 100644 index 0000000..e119409 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerBean.java @@ -0,0 +1,434 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.Date; +import org.apache.jute.BinaryInputArchive; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.quorum.CommitProcessor; + +/** + * This class implements the ZooKeeper server MBean interface. + */ +public class ZooKeeperServerBean implements ZooKeeperServerMXBean, ZKMBeanInfo { + + private final Date startTime; + private final String name; + + protected final ZooKeeperServer zks; + + public ZooKeeperServerBean(ZooKeeperServer zks) { + startTime = new Date(); + this.zks = zks; + name = "StandaloneServer_port" + zks.getClientPort(); + } + + public String getClientPort() { + return Integer.toString(zks.getClientPort()); + } + + public String getName() { + return name; + } + + public boolean isHidden() { + return false; + } + + public String getStartTime() { + return startTime.toString(); + } + + public String getVersion() { + return Version.getFullVersion(); + } + + public double getAvgRequestLatency() { + return zks.serverStats().getAvgLatency(); + } + + public long getMaxRequestLatency() { + return zks.serverStats().getMaxLatency(); + } + + public long getMinRequestLatency() { + return zks.serverStats().getMinLatency(); + } + + public long getOutstandingRequests() { + return zks.serverStats().getOutstandingRequests(); + } + + public int getTickTime() { + return zks.getTickTime(); + } + + public void setTickTime(int tickTime) { + zks.setTickTime(tickTime); + } + + public int getMaxClientCnxnsPerHost() { + return zks.getMaxClientCnxnsPerHost(); + } + + public void setMaxClientCnxnsPerHost(int max) { + if (zks.serverCnxnFactory != null) { + zks.serverCnxnFactory.setMaxClientCnxnsPerHost(max); + } + if (zks.secureServerCnxnFactory != null) { + zks.secureServerCnxnFactory.setMaxClientCnxnsPerHost(max); + } + } + + public int getMinSessionTimeout() { + return zks.getMinSessionTimeout(); + } + + public void setMinSessionTimeout(int min) { + zks.setMinSessionTimeout(min); + } + + public int getMaxSessionTimeout() { + return zks.getMaxSessionTimeout(); + } + + public void setMaxSessionTimeout(int max) { + zks.setMaxSessionTimeout(max); + } + + public long getDataDirSize() { + return zks.getDataDirSize(); + } + + public long getLogDirSize() { + return zks.getLogDirSize(); + } + + public long getPacketsReceived() { + return zks.serverStats().getPacketsReceived(); + } + + public long getPacketsSent() { + return zks.serverStats().getPacketsSent(); + } + + public long getFsyncThresholdExceedCount() { + return zks.serverStats().getFsyncThresholdExceedCount(); + } + + public long getAuthFailedCount() { + return zks.serverStats().getAuthFailedCount(); + } + + public long getNonMTLSRemoteConnCount() { + return zks.serverStats().getNonMTLSRemoteConnCount(); + } + + public long getNonMTLSLocalConnCount() { + return zks.serverStats().getNonMTLSLocalConnCount(); + } + + public void resetLatency() { + zks.serverStats().resetLatency(); + } + + public void resetMaxLatency() { + zks.serverStats().resetMaxLatency(); + } + + public void resetFsyncThresholdExceedCount() { + zks.serverStats().resetFsyncThresholdExceedCount(); + } + + public void resetAuthFailedCount() { + zks.serverStats().resetAuthFailedCount(); + } + + public void resetNonMTLSConnCount() { + zks.serverStats().resetNonMTLSRemoteConnCount(); + zks.serverStats().resetNonMTLSLocalConnCount(); + } + + public void resetStatistics() { + ServerStats serverStats = zks.serverStats(); + serverStats.resetRequestCounters(); + serverStats.resetLatency(); + serverStats.resetFsyncThresholdExceedCount(); + serverStats.resetAuthFailedCount(); + serverStats.resetNonMTLSRemoteConnCount(); + serverStats.resetNonMTLSLocalConnCount(); + } + + public long getNumAliveConnections() { + return zks.getNumAliveConnections(); + } + + @Override + public String getSecureClientPort() { + if (zks.secureServerCnxnFactory != null) { + return Integer.toString(zks.secureServerCnxnFactory.getLocalPort()); + } + return ""; + } + + @Override + public String getSecureClientAddress() { + if (zks.secureServerCnxnFactory != null) { + return String.format("%s:%d", + zks.secureServerCnxnFactory.getLocalAddress().getHostString(), + zks.secureServerCnxnFactory.getLocalPort()); + } + return ""; + } + + @Override + public long getTxnLogElapsedSyncTime() { + return zks.getTxnLogElapsedSyncTime(); + } + + @Override + public int getJuteMaxBufferSize() { + return BinaryInputArchive.maxBuffer; + } + + @Override + public int getLastClientResponseSize() { + return zks.serverStats().getClientResponseStats().getLastBufferSize(); + } + + @Override + public int getMinClientResponseSize() { + return zks.serverStats().getClientResponseStats().getMinBufferSize(); + } + + @Override + public int getMaxClientResponseSize() { + return zks.serverStats().getClientResponseStats().getMaxBufferSize(); + } + + @Override + public boolean getResponseCachingEnabled() { + return zks.isResponseCachingEnabled(); + } + + @Override + public void setResponseCachingEnabled(boolean isEnabled) { + zks.setResponseCachingEnabled(isEnabled); + } + + // Connection throttling settings + /////////////////////////////////////////////////////////////////////////// + + public int getConnectionMaxTokens() { + return zks.connThrottle().getMaxTokens(); + } + + public void setConnectionMaxTokens(int val) { + zks.connThrottle().setMaxTokens(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getConnectionTokenFillTime() { + return zks.connThrottle().getFillTime(); + } + + public void setConnectionTokenFillTime(int val) { + zks.connThrottle().setFillTime(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getConnectionTokenFillCount() { + return zks.connThrottle().getFillCount(); + } + + public void setConnectionTokenFillCount(int val) { + zks.connThrottle().setFillCount(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getConnectionFreezeTime() { + return zks.connThrottle().getFreezeTime(); + } + + public void setConnectionFreezeTime(int val) { + zks.connThrottle().setFreezeTime(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public double getConnectionDropIncrease() { + return zks.connThrottle().getDropIncrease(); + } + + public void setConnectionDropIncrease(double val) { + zks.connThrottle().setDropIncrease(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public double getConnectionDropDecrease() { + return zks.connThrottle().getDropDecrease(); + } + + public void setConnectionDropDecrease(double val) { + zks.connThrottle().setDropDecrease(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public double getConnectionDecreaseRatio() { + return zks.connThrottle().getDecreasePoint(); + } + + public void setConnectionDecreaseRatio(double val) { + zks.connThrottle().setDecreasePoint(val); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getCommitProcMaxReadBatchSize() { + return CommitProcessor.getMaxReadBatchSize(); + } + + public void setCommitProcMaxReadBatchSize(int size) { + CommitProcessor.setMaxReadBatchSize(size); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getCommitProcMaxCommitBatchSize() { + return CommitProcessor.getMaxCommitBatchSize(); + } + + public void setCommitProcMaxCommitBatchSize(int size) { + CommitProcessor.setMaxCommitBatchSize(size); + } + + /////////////////////////////////////////////////////////////////////////// + + @Override + public long getFlushDelay() { + return zks.getFlushDelay(); + } + + @Override + public void setFlushDelay(long delay) { + ZooKeeperServer.setFlushDelay(delay); + } + + // Request throttling settings + /////////////////////////////////////////////////////////////////////////// + + public int getRequestThrottleLimit() { + return RequestThrottler.getMaxRequests(); + } + + public void setRequestThrottleLimit(int requests) { + RequestThrottler.setMaxRequests(requests); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getRequestThrottleStallTime() { + return RequestThrottler.getStallTime(); + } + + public void setRequestThrottleStallTime(int time) { + RequestThrottler.setStallTime(time); + } + + /////////////////////////////////////////////////////////////////////////// + + public boolean getRequestThrottleDropStale() { + return RequestThrottler.getDropStaleRequests(); + } + + public void setRequestThrottleDropStale(boolean drop) { + RequestThrottler.setDropStaleRequests(drop); + } + + /////////////////////////////////////////////////////////////////////////// + + @Override + public long getMaxWriteQueuePollTime() { + return zks.getMaxWriteQueuePollTime(); + } + + @Override + public void setMaxWriteQueuePollTime(long delay) { + ZooKeeperServer.setMaxWriteQueuePollTime(delay); + } + + public boolean getRequestStaleLatencyCheck() { + return Request.getStaleLatencyCheck(); + } + + public void setRequestStaleLatencyCheck(boolean check) { + Request.setStaleLatencyCheck(check); + } + + /////////////////////////////////////////////////////////////////////////// + + @Override + public int getMaxBatchSize() { + return zks.getMaxBatchSize(); + } + + @Override + public void setMaxBatchSize(int size) { + ZooKeeperServer.setMaxBatchSize(size); + } + + public boolean getRequestStaleConnectionCheck() { + return Request.getStaleConnectionCheck(); + } + + public void setRequestStaleConnectionCheck(boolean check) { + Request.setStaleConnectionCheck(check); + } + + + /////////////////////////////////////////////////////////////////////////// + + public int getLargeRequestMaxBytes() { + return zks.getLargeRequestMaxBytes(); + } + + public void setLargeRequestMaxBytes(int bytes) { + zks.setLargeRequestMaxBytes(bytes); + } + + /////////////////////////////////////////////////////////////////////////// + + public int getLargeRequestThreshold() { + return zks.getLargeRequestThreshold(); + } + + public void setLargeRequestThreshold(int threshold) { + zks.setLargeRequestThreshold(threshold); + } + + public int getMaxCnxns() { + return ServerCnxnHelper.getMaxCnxns(zks.secureServerCnxnFactory, zks.serverCnxnFactory); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerConf.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerConf.java new file mode 100644 index 0000000..499dd56 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerConf.java @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Configuration data for a {@link ZooKeeperServer}. This class is immutable. + */ +public class ZooKeeperServerConf { + + /** + * The key in the map returned by {@link #toMap()} for the client port. + */ + public static final String KEY_CLIENT_PORT = "client_port"; + /** + * The key in the map returned by {@link #toMap()} for the data directory. + */ + public static final String KEY_DATA_DIR = "data_dir"; + /** + * The key in the map returned by {@link #toMap()} for the data log + * directory. + */ + public static final String KEY_DATA_LOG_DIR = "data_log_dir"; + /** + * The key in the map returned by {@link #toMap()} for the tick time. + */ + public static final String KEY_TICK_TIME = "tick_time"; + /** + * The key in the map returned by {@link #toMap()} for the maximum + * client connections per host. + */ + public static final String KEY_MAX_CLIENT_CNXNS = "max_client_cnxns"; + /** + * The key in the map returned by {@link #toMap()} for the minimum session + * timeout. + */ + public static final String KEY_MIN_SESSION_TIMEOUT = "min_session_timeout"; + /** + * The key in the map returned by {@link #toMap()} for the maximum session + * timeout. + */ + public static final String KEY_MAX_SESSION_TIMEOUT = "max_session_timeout"; + /** + * The key in the map returned by {@link #toMap()} for the server ID. + */ + public static final String KEY_SERVER_ID = "server_id"; + /** + * The key in the map returned by {@link #toMap()} for the server socket + * listen backlog. + */ + public static final String KEY_CLIENT_PORT_LISTEN_BACKLOG = "client_port_listen_backlog"; + + private final int clientPort; + private final String dataDir; + private final String dataLogDir; + private final int tickTime; + private final int maxClientCnxnsPerHost; + private final int minSessionTimeout; + private final int maxSessionTimeout; + private final long serverId; + private final int clientPortListenBacklog; + + /** + * Creates a new configuration. + * + * @param clientPort client port + * @param dataDir absolute path to data directory + * @param dataLogDir absolute path to data log directory + * @param tickTime tick time + * @param maxClientCnxnsPerHost maximum number of client connections + * @param minSessionTimeout minimum session timeout + * @param maxSessionTimeout maximum session timeout + * @param serverId server ID + */ + ZooKeeperServerConf(int clientPort, String dataDir, String dataLogDir, int tickTime, int maxClientCnxnsPerHost, int minSessionTimeout, int maxSessionTimeout, long serverId, int clientPortListenBacklog) { + this.clientPort = clientPort; + this.dataDir = dataDir; + this.dataLogDir = dataLogDir; + this.tickTime = tickTime; + this.maxClientCnxnsPerHost = maxClientCnxnsPerHost; + this.minSessionTimeout = minSessionTimeout; + this.maxSessionTimeout = maxSessionTimeout; + this.serverId = serverId; + this.clientPortListenBacklog = clientPortListenBacklog; + } + + /** + * Gets the client port. + * + * @return client port + */ + public int getClientPort() { + return clientPort; + } + + /** + * Gets the data directory. + * + * @return data directory + */ + public String getDataDir() { + return dataDir; + } + + /** + * Gets the data log directory. + * + * @return data log directory + */ + public String getDataLogDir() { + return dataLogDir; + } + + /** + * Gets the tick time. + * + * @return tick time + */ + public int getTickTime() { + return tickTime; + } + + /** + * Gets the maximum client connections per host. + * + * @return maximum client connections per host + */ + public int getMaxClientCnxnsPerHost() { + return maxClientCnxnsPerHost; + } + + /** + * Gets the minimum session timeout. + * + * @return minimum session timeout + */ + public int getMinSessionTimeout() { + return minSessionTimeout; + } + + /** + * Gets the maximum session timeout. + * + * @return maximum session timeout + */ + public int getMaxSessionTimeout() { + return maxSessionTimeout; + } + + /** + * Gets the server ID. + * + * @return server ID + */ + public long getServerId() { + return serverId; + } + + /** + * Returns the server socket listen backlog length. + */ + public int getClientPortListenBacklog() { + return clientPortListenBacklog; + } + + /** + * Converts this configuration to a map. The returned map is mutable, and + * changes to it do not reflect back into this configuration. + * + * @return map representation of configuration + */ + public Map toMap() { + Map conf = new LinkedHashMap(); + conf.put(KEY_CLIENT_PORT, clientPort); + conf.put(KEY_DATA_DIR, dataDir); + conf.put(KEY_DATA_LOG_DIR, dataLogDir); + conf.put(KEY_TICK_TIME, tickTime); + conf.put(KEY_MAX_CLIENT_CNXNS, maxClientCnxnsPerHost); + conf.put(KEY_MIN_SESSION_TIMEOUT, minSessionTimeout); + conf.put(KEY_MAX_SESSION_TIMEOUT, maxSessionTimeout); + conf.put(KEY_SERVER_ID, serverId); + conf.put(KEY_CLIENT_PORT_LISTEN_BACKLOG, clientPortListenBacklog); + return conf; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerListener.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerListener.java new file mode 100644 index 0000000..a09e355 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerListener.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * Listener for the critical resource events. + */ +public interface ZooKeeperServerListener { + + /** + * This will notify the server that some critical thread has stopped. + * It usually takes place when fatal error occurred. + * + * @param threadName + * - name of the thread + * @param errorCode + * - error code + */ + void notifyStopping(String threadName, int errorCode); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerListenerImpl.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerListenerImpl.java new file mode 100644 index 0000000..c48dbff --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerListenerImpl.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.apache.zookeeper.server.ZooKeeperServer.State; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Default listener implementation, which will be used to notify internal + * errors. For example, if some critical thread has stopped due to fatal errors, + * then it will get notifications and will change the state of ZooKeeper server + * to ERROR representing an error status. + */ +class ZooKeeperServerListenerImpl implements ZooKeeperServerListener { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperServerListenerImpl.class); + + private final ZooKeeperServer zkServer; + + ZooKeeperServerListenerImpl(ZooKeeperServer zkServer) { + this.zkServer = zkServer; + } + + @Override + public void notifyStopping(String threadName, int exitCode) { + LOG.info("Thread {} exits, error code {}", threadName, exitCode); + zkServer.setState(State.ERROR); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMXBean.java new file mode 100644 index 0000000..6b2fbee --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMXBean.java @@ -0,0 +1,249 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +/** + * ZooKeeper server MBean. + */ +public interface ZooKeeperServerMXBean { + + /** + * @return the server socket port number + */ + String getClientPort(); + /** + * @return the zookeeper server version + */ + String getVersion(); + /** + * @return time the server was started + */ + String getStartTime(); + /** + * @return min request latency in ms + */ + long getMinRequestLatency(); + /** + * @return average request latency in ms + */ + double getAvgRequestLatency(); + /** + * @return max request latency in ms + */ + long getMaxRequestLatency(); + /** + * @return number of packets received so far + */ + long getPacketsReceived(); + /** + * @return number of packets sent so far + */ + long getPacketsSent(); + /** + * @return number of fsync threshold exceeds so far + */ + long getFsyncThresholdExceedCount(); + /** + * @return number of AuthFailedCount so far + */ + long getAuthFailedCount(); + /** + * @return number of NonMTLSLocalConnCount so far + */ + long getNonMTLSLocalConnCount(); + /** + * @return number of NonMTLSRemoteConnCount so far + */ + long getNonMTLSRemoteConnCount(); + /** + * @return number of outstanding requests. + */ + long getOutstandingRequests(); + /** + * Current TickTime of server in milliseconds + */ + int getTickTime(); + /** + * Set TickTime of server in milliseconds + */ + void setTickTime(int tickTime); + + /** Current maxClientCnxns allowed from a particular host */ + int getMaxClientCnxnsPerHost(); + + /** Set maxClientCnxns allowed from a particular host */ + void setMaxClientCnxnsPerHost(int max); + + /** + * Current minSessionTimeout of the server in milliseconds + */ + int getMinSessionTimeout(); + /** + * Set minSessionTimeout of server in milliseconds + */ + void setMinSessionTimeout(int min); + + /** + * Current maxSessionTimeout of the server in milliseconds + */ + int getMaxSessionTimeout(); + /** + * Set maxSessionTimeout of server in milliseconds + */ + void setMaxSessionTimeout(int max); + + boolean getResponseCachingEnabled(); + void setResponseCachingEnabled(boolean isEnabled); + + /* Connection throttling settings */ + int getConnectionMaxTokens(); + void setConnectionMaxTokens(int val); + + int getConnectionTokenFillTime(); + void setConnectionTokenFillTime(int val); + + int getConnectionTokenFillCount(); + void setConnectionTokenFillCount(int val); + + int getConnectionFreezeTime(); + void setConnectionFreezeTime(int val); + + double getConnectionDropIncrease(); + void setConnectionDropIncrease(double val); + + double getConnectionDropDecrease(); + void setConnectionDropDecrease(double val); + + double getConnectionDecreaseRatio(); + void setConnectionDecreaseRatio(double val); + + int getCommitProcMaxReadBatchSize(); + void setCommitProcMaxReadBatchSize(int size); + + int getCommitProcMaxCommitBatchSize(); + void setCommitProcMaxCommitBatchSize(int size); + + int getRequestThrottleLimit(); + void setRequestThrottleLimit(int requests); + + int getRequestThrottleStallTime(); + void setRequestThrottleStallTime(int time); + + boolean getRequestThrottleDropStale(); + void setRequestThrottleDropStale(boolean drop); + + boolean getRequestStaleLatencyCheck(); + void setRequestStaleLatencyCheck(boolean check); + + boolean getRequestStaleConnectionCheck(); + void setRequestStaleConnectionCheck(boolean check); + + int getLargeRequestMaxBytes(); + void setLargeRequestMaxBytes(int bytes); + + int getLargeRequestThreshold(); + void setLargeRequestThreshold(int threshold); + + /** + * Reset packet and latency statistics + */ + void resetStatistics(); + /** + * Reset min/avg/max latency statistics + */ + void resetLatency(); + /** + * Reset max latency statistics only. + */ + void resetMaxLatency(); + /** + * Reset Fsync Threshold Exceed Count statistics only. + */ + void resetFsyncThresholdExceedCount(); + /** + * Reset NonMTLS(Local+Remote)ConnCount statistics only. + */ + void resetNonMTLSConnCount(); + /** + * Reset AuthFailedCount statistics only. + */ + void resetAuthFailedCount(); + /** + * @return number of alive client connections + */ + long getNumAliveConnections(); + + /** + * @return estimated size of data directory in bytes + */ + long getDataDirSize(); + /** + * @return estimated size of log directory in bytes + */ + long getLogDirSize(); + + /** + * @return secure client port + */ + String getSecureClientPort(); + /** + * @return secure client address + */ + String getSecureClientAddress(); + + /** + * Returns the elapsed sync of time of transaction log in milliseconds. + */ + long getTxnLogElapsedSyncTime(); + + /** + * @return Returns the value of the following config setting: jute.maxbuffer + */ + int getJuteMaxBufferSize(); + + /** + * @return size of latest generated client response + */ + int getLastClientResponseSize(); + + /** + * @return size of smallest generated client response + */ + int getMinClientResponseSize(); + + /** + * @return size of largest generated client response + */ + int getMaxClientResponseSize(); + + long getFlushDelay(); + void setFlushDelay(long delay); + + long getMaxWriteQueuePollTime(); + void setMaxWriteQueuePollTime(long delay); + + int getMaxBatchSize(); + void setMaxBatchSize(int size); + + /** + * @return Current maxCnxns allowed to a single ZooKeeper server + */ + int getMaxCnxns(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMain.java new file mode 100644 index 0000000..411d5d1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerMain.java @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javax.management.JMException; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.audit.ZKAuditProvider; +import org.apache.zookeeper.jmx.ManagedUtil; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; +import org.apache.zookeeper.metrics.impl.MetricsProviderBootstrap; +import org.apache.zookeeper.server.admin.AdminServer; +import org.apache.zookeeper.server.admin.AdminServer.AdminServerException; +import org.apache.zookeeper.server.admin.AdminServerFactory; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog.DatadirException; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.util.JvmPauseMonitor; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class starts and runs a standalone ZooKeeperServer. + */ +@InterfaceAudience.Public +public class ZooKeeperServerMain { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperServerMain.class); + + private static final String USAGE = "Usage: ZooKeeperServerMain configfile | port datadir [ticktime] [maxcnxns]"; + + // ZooKeeper server supports two kinds of connection: unencrypted and encrypted. + private ServerCnxnFactory cnxnFactory; + private ServerCnxnFactory secureCnxnFactory; + private ContainerManager containerManager; + private MetricsProvider metricsProvider; + private AdminServer adminServer; + + /* + * Start up the ZooKeeper server. + * + * @param args the configfile or the port datadir [ticktime] + */ + public static void main(String[] args) { + ZooKeeperServerMain main = new ZooKeeperServerMain(); + try { + main.initializeAndRun(args); + } catch (IllegalArgumentException e) { + LOG.error("Invalid arguments, exiting abnormally", e); + LOG.info(USAGE); + System.err.println(USAGE); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } catch (ConfigException e) { + LOG.error("Invalid config, exiting abnormally", e); + System.err.println("Invalid config, exiting abnormally"); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } catch (DatadirException e) { + LOG.error("Unable to access datadir, exiting abnormally", e); + System.err.println("Unable to access datadir, exiting abnormally"); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue()); + } catch (AdminServerException e) { + LOG.error("Unable to start AdminServer, exiting abnormally", e); + System.err.println("Unable to start AdminServer, exiting abnormally"); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue()); + } catch (Exception e) { + LOG.error("Unexpected exception, exiting abnormally", e); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + LOG.info("Exiting normally"); + ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue()); + } + + protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServerException { + try { + ManagedUtil.registerLog4jMBeans(); + } catch (JMException e) { + LOG.warn("Unable to register log4j JMX control", e); + } + + ServerConfig config = new ServerConfig(); + if (args.length == 1) { + config.parse(args[0]); + } else { + config.parse(args); + } + + runFromConfig(config); + } + + /** + * Run from a ServerConfig. + * @param config ServerConfig to use. + * @throws IOException + * @throws AdminServerException + */ + public void runFromConfig(ServerConfig config) throws IOException, AdminServerException { + LOG.info("Starting server"); + FileTxnSnapLog txnLog = null; + try { + try { + metricsProvider = MetricsProviderBootstrap.startMetricsProvider( + config.getMetricsProviderClassName(), + config.getMetricsProviderConfiguration()); + } catch (MetricsProviderLifeCycleException error) { + throw new IOException("Cannot boot MetricsProvider " + config.getMetricsProviderClassName(), error); + } + ServerMetrics.metricsProviderInitialized(metricsProvider); + // Note that this thread isn't going to be doing anything else, + // so rather than spawning another thread, we will just call + // run() in this thread. + // create a file logger url from the command line args + txnLog = new FileTxnSnapLog(config.dataLogDir, config.dataDir); + JvmPauseMonitor jvmPauseMonitor = null; + if (config.jvmPauseMonitorToRun) { + jvmPauseMonitor = new JvmPauseMonitor(config); + } + final ZooKeeperServer zkServer = new ZooKeeperServer(jvmPauseMonitor, txnLog, config.tickTime, config.minSessionTimeout, config.maxSessionTimeout, config.listenBacklog, null, config.initialConfig); + txnLog.setServerStats(zkServer.serverStats()); + + // Registers shutdown handler which will be used to know the + // server error or shutdown state changes. + final CountDownLatch shutdownLatch = new CountDownLatch(1); + zkServer.registerServerShutdownHandler(new ZooKeeperServerShutdownHandler(shutdownLatch)); + + // Start Admin server + adminServer = AdminServerFactory.createAdminServer(); + adminServer.setZooKeeperServer(zkServer); + adminServer.start(); + + boolean needStartZKServer = true; + if (config.getClientPortAddress() != null) { + cnxnFactory = ServerCnxnFactory.createFactory(); + cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns(), config.getClientPortListenBacklog(), false); + cnxnFactory.startup(zkServer); + // zkServer has been started. So we don't need to start it again in secureCnxnFactory. + needStartZKServer = false; + } + if (config.getSecureClientPortAddress() != null) { + secureCnxnFactory = ServerCnxnFactory.createFactory(); + secureCnxnFactory.configure(config.getSecureClientPortAddress(), config.getMaxClientCnxns(), config.getClientPortListenBacklog(), true); + secureCnxnFactory.startup(zkServer, needStartZKServer); + } + + containerManager = new ContainerManager( + zkServer.getZKDatabase(), + zkServer.firstProcessor, + Integer.getInteger("znode.container.checkIntervalMs", (int) TimeUnit.MINUTES.toMillis(1)), + Integer.getInteger("znode.container.maxPerMinute", 10000), + Long.getLong("znode.container.maxNeverUsedIntervalMs", 0) + ); + containerManager.start(); + ZKAuditProvider.addZKStartStopAuditLog(); + + // Watch status of ZooKeeper server. It will do a graceful shutdown + // if the server is not running or hits an internal error. + shutdownLatch.await(); + + shutdown(); + + if (cnxnFactory != null) { + cnxnFactory.join(); + } + if (secureCnxnFactory != null) { + secureCnxnFactory.join(); + } + if (zkServer.canShutdown()) { + zkServer.shutdown(true); + } + } catch (InterruptedException e) { + // warn, but generally this is ok + LOG.warn("Server interrupted", e); + } finally { + if (txnLog != null) { + txnLog.close(); + } + if (metricsProvider != null) { + try { + metricsProvider.stop(); + } catch (Throwable error) { + LOG.warn("Error while stopping metrics", error); + } + } + } + } + + /** + * Shutdown the serving instance + */ + protected void shutdown() { + if (containerManager != null) { + containerManager.stop(); + } + if (cnxnFactory != null) { + cnxnFactory.shutdown(); + } + if (secureCnxnFactory != null) { + secureCnxnFactory.shutdown(); + } + try { + if (adminServer != null) { + adminServer.shutdown(); + } + } catch (AdminServerException e) { + LOG.warn("Problem stopping AdminServer", e); + } + } + + // VisibleForTesting + ServerCnxnFactory getCnxnFactory() { + return cnxnFactory; + } + + // VisibleForTesting + ServerCnxnFactory getSecureCnxnFactory() { + return secureCnxnFactory; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerShutdownHandler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerShutdownHandler.java new file mode 100644 index 0000000..28cbcc9 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServerShutdownHandler.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.server.ZooKeeperServer.State; + +/** + * ZooKeeper server shutdown handler which will be used to handle ERROR or + * SHUTDOWN server state transitions, which in turn releases the associated + * shutdown latch. + */ +class ZooKeeperServerShutdownHandler { + + private final CountDownLatch shutdownLatch; + + ZooKeeperServerShutdownHandler(CountDownLatch shutdownLatch) { + this.shutdownLatch = shutdownLatch; + } + + /** + * This will be invoked when the server transition to a new server state. + * + * @param state new server state + */ + void handle(State state) { + if (state == State.ERROR || state == State.SHUTDOWN) { + shutdownLatch.countDown(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperThread.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperThread.java new file mode 100644 index 0000000..ab5c5f0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperThread.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is the main class for catching all the uncaught exceptions thrown by the + * threads. + */ +public class ZooKeeperThread extends Thread { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperThread.class); + + private UncaughtExceptionHandler uncaughtExceptionalHandler = new UncaughtExceptionHandler() { + + @Override + public void uncaughtException(Thread t, Throwable e) { + handleException(t.getName(), e); + } + }; + + public ZooKeeperThread(String threadName) { + super(threadName); + setUncaughtExceptionHandler(uncaughtExceptionalHandler); + } + + /** + * This will be used by the uncaught exception handler and just log a + * warning message and return. + * + * @param thName + * - thread name + * @param e + * - exception object + */ + protected void handleException(String thName, Throwable e) { + LOG.warn("Exception occurred from thread {}", thName, e); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooTrace.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooTrace.java new file mode 100644 index 0000000..e3a1b87 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooTrace.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import org.apache.zookeeper.server.quorum.LearnerHandler; +import org.apache.zookeeper.server.quorum.QuorumPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class encapsulates and centralizes tracing for the ZooKeeper server. + * Trace messages go to the log with TRACE level. + *

      + * Log4j must be correctly configured to capture the TRACE messages. + */ +public class ZooTrace { + + public static final long CLIENT_REQUEST_TRACE_MASK = 1 << 1; + + public static final long CLIENT_DATA_PACKET_TRACE_MASK = 1 << 2; + + public static final long CLIENT_PING_TRACE_MASK = 1 << 3; + + public static final long SERVER_PACKET_TRACE_MASK = 1 << 4; + + public static final long SESSION_TRACE_MASK = 1 << 5; + + public static final long EVENT_DELIVERY_TRACE_MASK = 1 << 6; + + public static final long SERVER_PING_TRACE_MASK = 1 << 7; + + public static final long WARNING_TRACE_MASK = 1 << 8; + + public static final long JMX_TRACE_MASK = 1 << 9; + + private static long traceMask = CLIENT_REQUEST_TRACE_MASK | SERVER_PACKET_TRACE_MASK | SESSION_TRACE_MASK | WARNING_TRACE_MASK; + + public static synchronized long getTextTraceLevel() { + return traceMask; + } + + public static synchronized void setTextTraceLevel(long mask) { + traceMask = mask; + final Logger LOG = LoggerFactory.getLogger(ZooTrace.class); + LOG.info("Set text trace mask to 0x{}", Long.toHexString(mask)); + } + + public static synchronized boolean isTraceEnabled(Logger log, long mask) { + return log.isTraceEnabled() && (mask & traceMask) != 0; + } + + public static void logTraceMessage(Logger log, long mask, String msg) { + if(isTraceEnabled(log, mask)) { + log.trace(msg); + } + } + + public static void logQuorumPacket(Logger log, long mask, char direction, QuorumPacket qp) { + if(isTraceEnabled(log, mask)) { + logTraceMessage(log, mask, direction + " " + LearnerHandler.packetToString(qp)); + } + } + + public static void logRequest(Logger log, long mask, char rp, Request request, String header) { + if(isTraceEnabled(log, mask)) { + log.trace(header + ":" + rp + request.toString()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/AdminServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/AdminServer.java new file mode 100644 index 0000000..88c6e24 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/AdminServer.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.server.ZooKeeperServer; + +/** + * Interface for an embedded admin server that runs Commands. There is only one + * functional implementation, JettyAdminServer. DummyAdminServer, which does + * nothing, is used when we do not wish to run a server. + */ +@InterfaceAudience.Public +public interface AdminServer { + + void start() throws AdminServerException; + void shutdown() throws AdminServerException; + void setZooKeeperServer(ZooKeeperServer zkServer); + + @InterfaceAudience.Public + class AdminServerException extends Exception { + + private static final long serialVersionUID = 1L; + public AdminServerException(String message, Throwable cause) { + super(message, cause); + } + public AdminServerException(Throwable cause) { + super(cause); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/AdminServerFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/AdminServerFactory.java new file mode 100644 index 0000000..2794060 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/AdminServerFactory.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import java.lang.reflect.InvocationTargetException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Factory class for creating an AdminServer. + */ +public class AdminServerFactory { + + private static final Logger LOG = LoggerFactory.getLogger(AdminServerFactory.class); + + /** + * This method encapsulates the logic for whether we should use a + * JettyAdminServer (i.e., the AdminServer is enabled) or a DummyAdminServer + * (i.e., the AdminServer is disabled). It uses reflection when attempting + * to create a JettyAdminServer, rather than referencing the class directly, + * so that it's ok to omit Jetty from the classpath if a user doesn't wish + * to pull in Jetty with ZooKeeper. + */ + public static AdminServer createAdminServer() { + if (!"false".equals(System.getProperty("zookeeper.admin.enableServer"))) { + try { + Class jettyAdminServerC = Class.forName("org.apache.zookeeper.server.admin.JettyAdminServer"); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动一个基于 jetty 的 web 服务 + */ + Object adminServer = jettyAdminServerC.getConstructor().newInstance(); + return (AdminServer) adminServer; + + } catch (ClassNotFoundException e) { + LOG.warn("Unable to start JettyAdminServer", e); + } catch (InstantiationException e) { + LOG.warn("Unable to start JettyAdminServer", e); + } catch (IllegalAccessException e) { + LOG.warn("Unable to start JettyAdminServer", e); + } catch (InvocationTargetException e) { + LOG.warn("Unable to start JettyAdminServer", e); + } catch (NoSuchMethodException e) { + LOG.warn("Unable to start JettyAdminServer", e); + } catch (NoClassDefFoundError e) { + LOG.warn("Unable to load jetty, not starting JettyAdminServer", e); + } + } + return new DummyAdminServer(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Command.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Command.java new file mode 100644 index 0000000..b422715 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Command.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.server.ZooKeeperServer; + +/** + * Interface implemented by all commands runnable by JettyAdminServer. + * + * @see CommandBase + * @see Commands + * @see JettyAdminServer + */ +public interface Command { + + /** + * The set of all names that can be used to refer to this command (e.g., + * "configuration", "config", and "conf"). + */ + Set getNames(); + + /** + * The name that is returned with the command response and that appears in + * the list of all commands. This should be a member of the set returned by + * getNames(). + */ + String getPrimaryName(); + + /** + * A string documenting this command (e.g., what it does, any arguments it + * takes). + */ + String getDoc(); + + /** + * @return true if the command requires an active ZooKeeperServer or a + * synced peer in order to resolve + */ + boolean isServerRequired(); + + /** + * Run this command. Commands take a ZooKeeperServer and String-valued + * keyword arguments and return a map containing any information + * constituting the response to the command. Commands are responsible for + * parsing keyword arguments and performing any error handling if necessary. + * Errors should be reported by setting the "error" entry of the returned + * map with an appropriate message rather than throwing an exception. + * + * @param zkServer + * @param kwargs keyword -> argument value mapping + * @return Map representing response to command containing at minimum: + * - "command" key containing the command's primary name + * - "error" key containing a String error message or null if no error + */ + CommandResponse run(ZooKeeperServer zkServer, Map kwargs); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandBase.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandBase.java new file mode 100644 index 0000000..322de17 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandBase.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public abstract class CommandBase implements Command { + + private final String primaryName; + private final Set names; + private final String doc; + private final boolean serverRequired; + + /** + * @param names The possible names of this command, with the primary name first. + */ + protected CommandBase(List names) { + this(names, true, null); + } + protected CommandBase(List names, boolean serverRequired) { + this(names, serverRequired, null); + } + + protected CommandBase(List names, boolean serverRequired, String doc) { + this.primaryName = names.get(0); + this.names = new HashSet(names); + this.doc = doc; + this.serverRequired = serverRequired; + } + + @Override + public String getPrimaryName() { + return primaryName; + } + + @Override + public Set getNames() { + return names; + } + + @Override + public String getDoc() { + return doc; + } + + @Override + public boolean isServerRequired() { + return serverRequired; + } + + /** + * @return A response with the command set to the primary name and the + * error set to null (these are the two entries that all command + * responses are required to include). + */ + protected CommandResponse initializeResponse() { + return new CommandResponse(primaryName); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandOutputter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandOutputter.java new file mode 100644 index 0000000..a8fe8bd --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandOutputter.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import java.io.PrintWriter; + +/** + * CommandOutputters are used to format the responses from Commands. + * + * @see Command + * @see JettyAdminServer + */ +public interface CommandOutputter { + + /** The MIME type of this output (e.g., "application/json") */ + String getContentType(); + + void output(CommandResponse response, PrintWriter pw); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandResponse.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandResponse.java new file mode 100644 index 0000000..d9e7239 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/CommandResponse.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.admin; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * A response from running a {@link Command}. + */ +public class CommandResponse { + + /** + * The key in the map returned by {@link #toMap()} for the command name. + */ + public static final String KEY_COMMAND = "command"; + /** + * The key in the map returned by {@link #toMap()} for the error string. + */ + public static final String KEY_ERROR = "error"; + + private final String command; + private final String error; + private final Map data; + + /** + * Creates a new response with no error string. + * + * @param command command name + */ + public CommandResponse(String command) { + this(command, null); + } + /** + * Creates a new response. + * + * @param command command name + * @param error error string (may be null) + */ + public CommandResponse(String command, String error) { + this.command = command; + this.error = error; + data = new LinkedHashMap(); + } + + /** + * Gets the command name. + * + * @return command name + */ + public String getCommand() { + return command; + } + + /** + * Gets the error string (may be null). + * + * @return error string + */ + public String getError() { + return error; + } + + /** + * Adds a key/value pair to this response. + * + * @param key key + * @param value value + * @return prior value for key, or null if none + */ + public Object put(String key, Object value) { + return data.put(key, value); + } + + /** + * Adds all key/value pairs in the given map to this response. + * + * @param m map of key/value pairs + */ + public void putAll(Map m) { + data.putAll(m); + } + + /** + * Converts this response to a map. The returned map is mutable, and + * changes to it do not reflect back into this response. + * + * @return map representation of response + */ + public Map toMap() { + Map m = new LinkedHashMap(data); + m.put(KEY_COMMAND, command); + m.put(KEY_ERROR, error); + m.putAll(data); + return m; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java new file mode 100644 index 0000000..705c6cc --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/Commands.java @@ -0,0 +1,859 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import com.fasterxml.jackson.annotation.JsonProperty; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.net.InetSocketAddress; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.stream.Collectors; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.Environment.Entry; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.persistence.SnapshotInfo; +import org.apache.zookeeper.server.quorum.Follower; +import org.apache.zookeeper.server.quorum.FollowerZooKeeperServer; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.server.quorum.MultipleAddresses; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumZooKeeperServer; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class containing static methods for registering and running Commands, as well + * as default Command definitions. + * + * @see Command + * @see JettyAdminServer + */ +public class Commands { + + static final Logger LOG = LoggerFactory.getLogger(Commands.class); + + /** Maps command names to Command instances */ + private static Map commands = new HashMap(); + private static Set primaryNames = new HashSet(); + + /** + * Registers the given command. Registered commands can be run by passing + * any of their names to runCommand. + */ + public static void registerCommand(Command command) { + for (String name : command.getNames()) { + Command prev = commands.put(name, command); + if (prev != null) { + LOG.warn("Re-registering command {} (primary name = {})", name, command.getPrimaryName()); + } + } + primaryNames.add(command.getPrimaryName()); + } + + /** + * Run the registered command with name cmdName. Commands should not produce + * any exceptions; any (anticipated) errors should be reported in the + * "error" entry of the returned map. Likewise, if no command with the given + * name is registered, this will be noted in the "error" entry. + * + * @param cmdName + * @param zkServer + * @param kwargs String-valued keyword arguments to the command + * (may be null if command requires no additional arguments) + * @return Map representing response to command containing at minimum: + * - "command" key containing the command's primary name + * - "error" key containing a String error message or null if no error + */ + public static CommandResponse runCommand( + String cmdName, + ZooKeeperServer zkServer, + Map kwargs) { + Command command = getCommand(cmdName); + if (command == null) { + return new CommandResponse(cmdName, "Unknown command: " + cmdName); + } + if (command.isServerRequired() && (zkServer == null || !zkServer.isRunning())) { + return new CommandResponse(cmdName, "This ZooKeeper instance is not currently serving requests"); + } + return command.run(zkServer, kwargs); + } + + /** + * Returns the primary names of all registered commands. + */ + public static Set getPrimaryNames() { + return primaryNames; + } + + /** + * Returns the commands registered under cmdName with registerCommand, or + * null if no command is registered with that name. + */ + public static Command getCommand(String cmdName) { + return commands.get(cmdName); + } + + static { + registerCommand(new CnxnStatResetCommand()); + registerCommand(new ConfCommand()); + registerCommand(new ConsCommand()); + registerCommand(new DigestCommand()); + registerCommand(new DirsCommand()); + registerCommand(new DumpCommand()); + registerCommand(new EnvCommand()); + registerCommand(new GetTraceMaskCommand()); + registerCommand(new InitialConfigurationCommand()); + registerCommand(new IsroCommand()); + registerCommand(new LastSnapshotCommand()); + registerCommand(new LeaderCommand()); + registerCommand(new MonitorCommand()); + registerCommand(new ObserverCnxnStatResetCommand()); + registerCommand(new RuokCommand()); + registerCommand(new SetTraceMaskCommand()); + registerCommand(new SrvrCommand()); + registerCommand(new StatCommand()); + registerCommand(new StatResetCommand()); + registerCommand(new SyncedObserverConsCommand()); + registerCommand(new SystemPropertiesCommand()); + registerCommand(new VotingViewCommand()); + registerCommand(new WatchCommand()); + registerCommand(new WatchesByPathCommand()); + registerCommand(new WatchSummaryCommand()); + registerCommand(new ZabStateCommand()); + } + + /** + * Reset all connection statistics. + */ + public static class CnxnStatResetCommand extends CommandBase { + + public CnxnStatResetCommand() { + super(Arrays.asList("connection_stat_reset", "crst")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + zkServer.getServerCnxnFactory().resetAllConnectionStats(); + return response; + + } + + } + + /** + * Server configuration parameters. + * @see ZooKeeperServer#getConf() + */ + public static class ConfCommand extends CommandBase { + + public ConfCommand() { + super(Arrays.asList("configuration", "conf", "config")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.putAll(zkServer.getConf().toMap()); + return response; + } + + } + + /** + * Information on client connections to server. Returned Map contains: + * - "connections": list of connection info objects + * @see org.apache.zookeeper.server.ServerCnxn#getConnectionInfo(boolean) + */ + public static class ConsCommand extends CommandBase { + + public ConsCommand() { + super(Arrays.asList("connections", "cons")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + ServerCnxnFactory serverCnxnFactory = zkServer.getServerCnxnFactory(); + if (serverCnxnFactory != null) { + response.put("connections", serverCnxnFactory.getAllConnectionInfo(false)); + } else { + response.put("connections", Collections.emptyList()); + } + ServerCnxnFactory secureServerCnxnFactory = zkServer.getSecureServerCnxnFactory(); + if (secureServerCnxnFactory != null) { + response.put("secure_connections", secureServerCnxnFactory.getAllConnectionInfo(false)); + } else { + response.put("secure_connections", Collections.emptyList()); + } + return response; + } + + } + + /** + * Information on ZK datadir and snapdir size in bytes + */ + public static class DirsCommand extends CommandBase { + + public DirsCommand() { + super(Arrays.asList("dirs")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.put("datadir_size", zkServer.getDataDirSize()); + response.put("logdir_size", zkServer.getLogDirSize()); + return response; + } + + } + + /** + * Information on session expirations and ephemerals. Returned map contains: + * - "expiry_time_to_session_ids": Map<Long, Set<Long>> + * time -> sessions IDs of sessions that expire at time + * - "session_id_to_ephemeral_paths": Map<Long, Set<String>> + * session ID -> ephemeral paths created by that session + * @see ZooKeeperServer#getSessionExpiryMap() + * @see ZooKeeperServer#getEphemerals() + */ + public static class DumpCommand extends CommandBase { + + public DumpCommand() { + super(Arrays.asList("dump")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.put("expiry_time_to_session_ids", zkServer.getSessionExpiryMap()); + response.put("session_id_to_ephemeral_paths", zkServer.getEphemerals()); + return response; + } + + } + + /** + * All defined environment variables. + */ + public static class EnvCommand extends CommandBase { + + public EnvCommand() { + super(Arrays.asList("environment", "env", "envi"), false); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + for (Entry e : Environment.list()) { + response.put(e.getKey(), e.getValue()); + } + return response; + } + + } + + /** + * Digest histories for every specific number of txns. + */ + public static class DigestCommand extends CommandBase { + + public DigestCommand() { + super(Arrays.asList("hash")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.put("digests", zkServer.getZKDatabase().getDataTree().getDigestLog()); + return response; + } + + } + + /** + * The current trace mask. Returned map contains: + * - "tracemask": Long + */ + public static class GetTraceMaskCommand extends CommandBase { + + public GetTraceMaskCommand() { + super(Arrays.asList("get_trace_mask", "gtmk"), false); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.put("tracemask", ZooTrace.getTextTraceLevel()); + return response; + } + + } + + public static class InitialConfigurationCommand extends CommandBase { + + public InitialConfigurationCommand() { + super(Arrays.asList("initial_configuration", "icfg")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.put("initial_configuration", zkServer.getInitialConfig()); + return response; + } + + } + + /** + * Is this server in read-only mode. Returned map contains: + * - "is_read_only": Boolean + */ + public static class IsroCommand extends CommandBase { + + public IsroCommand() { + super(Arrays.asList("is_read_only", "isro")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + response.put("read_only", zkServer instanceof ReadOnlyZooKeeperServer); + return response; + } + + } + + /** + * Command returns information of the last snapshot that zookeeper server + * has finished saving to disk. During the time between the server starts up + * and it finishes saving its first snapshot, the command returns the zxid + * and last modified time of the snapshot file used for restoration at + * server startup. Returned map contains: + * - "zxid": String + * - "timestamp": Long + */ + public static class LastSnapshotCommand extends CommandBase { + + public LastSnapshotCommand() { + super(Arrays.asList("last_snapshot", "lsnp")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + SnapshotInfo info = zkServer.getTxnLogFactory().getLastSnapshotInfo(); + response.put("zxid", Long.toHexString(info == null ? -1L : info.zxid)); + response.put("timestamp", info == null ? -1L : info.timestamp); + return response; + } + + } + + /** + * Returns the leader status of this instance and the leader host string. + */ + public static class LeaderCommand extends CommandBase { + + public LeaderCommand() { + super(Arrays.asList("leader", "lead")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + if (zkServer instanceof QuorumZooKeeperServer) { + response.put("is_leader", zkServer instanceof LeaderZooKeeperServer); + QuorumPeer peer = ((QuorumZooKeeperServer) zkServer).self; + response.put("leader_id", peer.getLeaderId()); + String leaderAddress = peer.getLeaderAddress(); + response.put("leader_ip", leaderAddress != null ? leaderAddress : ""); + } else { + response.put("error", "server is not initialized"); + } + return response; + } + + } + + /** + * Some useful info for monitoring. Returned map contains: + * - "version": String + * server version + * - "avg_latency": Long + * - "max_latency": Long + * - "min_latency": Long + * - "packets_received": Long + * - "packets_sents": Long + * - "num_alive_connections": Integer + * - "outstanding_requests": Long + * number of unprocessed requests + * - "server_state": "leader", "follower", or "standalone" + * - "znode_count": Integer + * - "watch_count": Integer + * - "ephemerals_count": Integer + * - "approximate_data_size": Long + * - "open_file_descriptor_count": Long (unix only) + * - "max_file_descriptor_count": Long (unix only) + * - "fsync_threshold_exceed_count": Long + * - "non_mtls_conn_count": Long + * - "non_mtls_remote_conn_count": Long + * - "non_mtls_local_conn_count": Long + * - "followers": Integer (leader only) + * - "synced_followers": Integer (leader only) + * - "pending_syncs": Integer (leader only) + */ + public static class MonitorCommand extends CommandBase { + + public MonitorCommand() { + super(Arrays.asList("monitor", "mntr"), false); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + zkServer.dumpMonitorValues(response::put); + ServerMetrics.getMetrics().getMetricsProvider().dump(response::put); + return response; + + } + + } + + /** + * Reset all observer connection statistics. + */ + public static class ObserverCnxnStatResetCommand extends CommandBase { + + public ObserverCnxnStatResetCommand() { + super(Arrays.asList("observer_connection_stat_reset", "orst")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + if (zkServer instanceof LeaderZooKeeperServer) { + Leader leader = ((LeaderZooKeeperServer) zkServer).getLeader(); + leader.resetObserverConnectionStats(); + } else if (zkServer instanceof FollowerZooKeeperServer) { + Follower follower = ((FollowerZooKeeperServer) zkServer).getFollower(); + follower.resetObserverConnectionStats(); + } + return response; + } + + } + + /** + * No-op command, check if the server is running + */ + public static class RuokCommand extends CommandBase { + + public RuokCommand() { + super(Arrays.asList("ruok")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + return initializeResponse(); + } + + } + + /** + * Sets the trace mask. Required arguments: + * - "traceMask": Long + * Returned Map contains: + * - "tracemask": Long + */ + public static class SetTraceMaskCommand extends CommandBase { + + public SetTraceMaskCommand() { + super(Arrays.asList("set_trace_mask", "stmk"), false); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + long traceMask; + if (!kwargs.containsKey("traceMask")) { + response.put("error", "setTraceMask requires long traceMask argument"); + return response; + } + try { + traceMask = Long.parseLong(kwargs.get("traceMask")); + } catch (NumberFormatException e) { + response.put("error", "setTraceMask requires long traceMask argument, got " + kwargs.get("traceMask")); + return response; + } + + ZooTrace.setTextTraceLevel(traceMask); + response.put("tracemask", traceMask); + return response; + } + + } + + /** + * Server information. Returned map contains: + * - "version": String + * version of server + * - "read_only": Boolean + * is server in read-only mode + * - "server_stats": ServerStats object + * - "node_count": Integer + */ + public static class SrvrCommand extends CommandBase { + + public SrvrCommand() { + super(Arrays.asList("server_stats", "srvr")); + } + + // Allow subclasses (e.g. StatCommand) to specify their own names + protected SrvrCommand(List names) { + super(names); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + LOG.info("running stat"); + response.put("version", Version.getFullVersion()); + response.put("read_only", zkServer instanceof ReadOnlyZooKeeperServer); + response.put("server_stats", zkServer.serverStats()); + response.put("client_response", zkServer.serverStats().getClientResponseStats()); + if (zkServer instanceof LeaderZooKeeperServer) { + Leader leader = ((LeaderZooKeeperServer) zkServer).getLeader(); + response.put("proposal_stats", leader.getProposalStats()); + } + response.put("node_count", zkServer.getZKDatabase().getNodeCount()); + return response; + } + + } + + /** + * Same as SrvrCommand but has extra "connections" entry. + */ + public static class StatCommand extends SrvrCommand { + + public StatCommand() { + super(Arrays.asList("stats", "stat")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = super.run(zkServer, kwargs); + + final Iterable> connections; + if (zkServer.getServerCnxnFactory() != null) { + connections = zkServer.getServerCnxnFactory().getAllConnectionInfo(true); + } else { + connections = Collections.emptyList(); + } + response.put("connections", connections); + + final Iterable> secureConnections; + if (zkServer.getSecureServerCnxnFactory() != null) { + secureConnections = zkServer.getSecureServerCnxnFactory().getAllConnectionInfo(true); + } else { + secureConnections = Collections.emptyList(); + } + response.put("secure_connections", secureConnections); + return response; + } + + } + + /** + * Resets server statistics. + */ + public static class StatResetCommand extends CommandBase { + + public StatResetCommand() { + super(Arrays.asList("stat_reset", "srst")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + zkServer.serverStats().reset(); + return response; + } + + } + + /** + * Information on observer connections to server. Returned Map contains: + * - "synced_observers": Integer (leader/follower only) + * - "observers": list of observer learner handler info objects (leader/follower only) + * @see org.apache.zookeeper.server.quorum.LearnerHandler#getLearnerHandlerInfo() + */ + public static class SyncedObserverConsCommand extends CommandBase { + + public SyncedObserverConsCommand() { + super(Arrays.asList("observers", "obsr")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + + CommandResponse response = initializeResponse(); + + if (zkServer instanceof LeaderZooKeeperServer) { + Leader leader = ((LeaderZooKeeperServer) zkServer).getLeader(); + + response.put("synced_observers", leader.getObservingLearners().size()); + response.put("observers", leader.getObservingLearnersInfo()); + return response; + } else if (zkServer instanceof FollowerZooKeeperServer) { + Follower follower = ((FollowerZooKeeperServer) zkServer).getFollower(); + Integer syncedObservers = follower.getSyncedObserverSize(); + if (syncedObservers != null) { + response.put("synced_observers", syncedObservers); + response.put("observers", follower.getSyncedObserversInfo()); + return response; + } + } + + response.put("synced_observers", 0); + response.put("observers", Collections.emptySet()); + return response; + } + + } + + /** + * All defined system properties. + */ + public static class SystemPropertiesCommand extends CommandBase { + + public SystemPropertiesCommand() { + super(Arrays.asList("system_properties", "sysp"), false); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + Properties systemProperties = System.getProperties(); + SortedMap sortedSystemProperties = new TreeMap<>(); + systemProperties.forEach((k, v) -> sortedSystemProperties.put(k.toString(), v.toString())); + response.putAll(sortedSystemProperties); + return response; + } + + } + + /** + * Returns the current ensemble configuration information. + * It provides list of current voting members in the ensemble. + */ + public static class VotingViewCommand extends CommandBase { + + public VotingViewCommand() { + super(Arrays.asList("voting_view")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + if (zkServer instanceof QuorumZooKeeperServer) { + QuorumPeer peer = ((QuorumZooKeeperServer) zkServer).self; + Map votingView = peer.getVotingView().entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, e -> new QuorumServerView(e.getValue()))); + response.put("current_config", votingView); + } else { + response.put("current_config", Collections.emptyMap()); + } + return response; + } + + @SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "class is used only for JSON serialization") + private static class QuorumServerView { + + @JsonProperty + private List serverAddresses; + + @JsonProperty + private List electionAddresses; + + @JsonProperty + private String clientAddress; + + @JsonProperty + private String learnerType; + + public QuorumServerView(QuorumPeer.QuorumServer quorumServer) { + this.serverAddresses = getMultiAddressString(quorumServer.addr); + this.electionAddresses = getMultiAddressString(quorumServer.electionAddr); + this.learnerType = quorumServer.type.equals(LearnerType.PARTICIPANT) ? "participant" : "observer"; + this.clientAddress = getAddressString(quorumServer.clientAddr); + } + + private static List getMultiAddressString(MultipleAddresses multipleAddresses) { + if (multipleAddresses == null) { + return Collections.emptyList(); + } + + return multipleAddresses.getAllAddresses().stream() + .map(QuorumServerView::getAddressString) + .collect(Collectors.toList()); + } + + private static String getAddressString(InetSocketAddress address) { + if (address == null) { + return ""; + } + return String.format("%s:%d", QuorumPeer.QuorumServer.delimitedHostString(address), address.getPort()); + } + } + + } + + /** + * Watch information aggregated by session. Returned Map contains: + * - "session_id_to_watched_paths": Map<Long, Set<String>> session ID -> watched paths + * @see DataTree#getWatches() + * @see DataTree#getWatches() + */ + public static class WatchCommand extends CommandBase { + + public WatchCommand() { + super(Arrays.asList("watches", "wchc")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + DataTree dt = zkServer.getZKDatabase().getDataTree(); + CommandResponse response = initializeResponse(); + response.put("session_id_to_watched_paths", dt.getWatches().toMap()); + return response; + } + + } + + /** + * Watch information aggregated by path. Returned Map contains: + * - "path_to_session_ids": Map<String, Set<Long>> path -> session IDs of sessions watching path + * @see DataTree#getWatchesByPath() + */ + public static class WatchesByPathCommand extends CommandBase { + + public WatchesByPathCommand() { + super(Arrays.asList("watches_by_path", "wchp")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + DataTree dt = zkServer.getZKDatabase().getDataTree(); + CommandResponse response = initializeResponse(); + response.put("path_to_session_ids", dt.getWatchesByPath().toMap()); + return response; + } + + } + + /** + * Summarized watch information. + * @see DataTree#getWatchesSummary() + */ + public static class WatchSummaryCommand extends CommandBase { + + public WatchSummaryCommand() { + super(Arrays.asList("watch_summary", "wchs")); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + DataTree dt = zkServer.getZKDatabase().getDataTree(); + CommandResponse response = initializeResponse(); + response.putAll(dt.getWatchesSummary().toMap()); + return response; + } + + } + + /** + * Returns the current phase of Zab protocol that peer is running. + * It can be in one of these phases: ELECTION, DISCOVERY, SYNCHRONIZATION, BROADCAST + */ + public static class ZabStateCommand extends CommandBase { + + public ZabStateCommand() { + super(Arrays.asList("zabstate"), false); + } + + @Override + public CommandResponse run(ZooKeeperServer zkServer, Map kwargs) { + CommandResponse response = initializeResponse(); + if (zkServer instanceof QuorumZooKeeperServer) { + QuorumPeer peer = ((QuorumZooKeeperServer) zkServer).self; + QuorumPeer.ZabState zabState = peer.getZabState(); + QuorumVerifier qv = peer.getQuorumVerifier(); + + QuorumPeer.QuorumServer voter = qv.getVotingMembers().get(peer.getId()); + boolean voting = ( + voter != null + && voter.addr.equals(peer.getQuorumAddress()) + && voter.electionAddr.equals(peer.getElectionAddress()) + ); + response.put("myid", zkServer.getConf().getServerId()); + response.put("is_leader", zkServer instanceof LeaderZooKeeperServer); + response.put("quorum_address", peer.getQuorumAddress()); + response.put("election_address", peer.getElectionAddress()); + response.put("client_address", peer.getClientAddress()); + response.put("voting", voting); + long lastProcessedZxid = zkServer.getZKDatabase().getDataTreeLastProcessedZxid(); + response.put("last_zxid", "0x" + ZxidUtils.zxidToString(lastProcessedZxid)); + response.put("zab_epoch", ZxidUtils.getEpochFromZxid(lastProcessedZxid)); + response.put("zab_counter", ZxidUtils.getCounterFromZxid(lastProcessedZxid)); + response.put("zabstate", zabState.name().toLowerCase()); + } else { + response.put("voting", false); + response.put("zabstate", ""); + } + return response; + } + + } + + private Commands() { + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/DummyAdminServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/DummyAdminServer.java new file mode 100644 index 0000000..88d80fc --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/DummyAdminServer.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import org.apache.zookeeper.server.ZooKeeperServer; + +/** + * An AdminServer that does nothing. + * + * We use this class when we wish to disable the AdminServer. (This way we only + * have to consider whether the server is enabled when we create the + * AdminServer, which is handled by AdminServerFactory.) + */ +public class DummyAdminServer implements AdminServer { + + @Override + public void start() throws AdminServerException { + } + + @Override + public void shutdown() throws AdminServerException { + } + + @Override + public void setZooKeeperServer(ZooKeeperServer zkServer) { + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/JettyAdminServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/JettyAdminServer.java new file mode 100644 index 0000000..9f44a92 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/JettyAdminServer.java @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.zookeeper.common.QuorumX509Util; +import org.apache.zookeeper.common.X509Util; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.eclipse.jetty.http.HttpVersion; +import org.eclipse.jetty.security.ConstraintMapping; +import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.SecureRequestCustomizer; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.SslConnectionFactory; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.util.security.Constraint; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class encapsulates a Jetty server for running Commands. + * + * Given the default settings, start a ZooKeeper server and visit + * http://hostname:8080/commands for links to all registered commands. Visiting + * http://hostname:8080/commands/commandname will execute the associated + * Command and return the result in the body of the response. Any keyword + * arguments to the command are specified with URL parameters (e.g., + * http://localhost:8080/commands/set_trace_mask?traceMask=306). + * + * @see Commands + * @see CommandOutputter + */ +public class JettyAdminServer implements AdminServer { + + static final Logger LOG = LoggerFactory.getLogger(JettyAdminServer.class); + + public static final int DEFAULT_PORT = 8080; + public static final int DEFAULT_IDLE_TIMEOUT = 30000; + public static final String DEFAULT_COMMAND_URL = "/commands"; + private static final String DEFAULT_ADDRESS = "0.0.0.0"; + public static final int DEFAULT_STS_MAX_AGE = 1 * 24 * 60 * 60; // seconds in a day + public static final int DEFAULT_HTTP_VERSION = 11; // based on HttpVersion.java in jetty + + private final Server server; + private final String address; + private final int port; + private final int idleTimeout; + private final String commandUrl; + private ZooKeeperServer zkServer; + + public JettyAdminServer() throws AdminServerException, IOException, GeneralSecurityException { + this(System.getProperty("zookeeper.admin.serverAddress", DEFAULT_ADDRESS), + Integer.getInteger("zookeeper.admin.serverPort", DEFAULT_PORT), + Integer.getInteger("zookeeper.admin.idleTimeout", DEFAULT_IDLE_TIMEOUT), + System.getProperty("zookeeper.admin.commandURL", DEFAULT_COMMAND_URL), + Integer.getInteger("zookeeper.admin.httpVersion", DEFAULT_HTTP_VERSION), + Boolean.getBoolean("zookeeper.admin.portUnification"), Boolean.getBoolean("zookeeper.admin.forceHttps")); + } + + public JettyAdminServer(String address, int port, int timeout, String commandUrl, int httpVersion, + boolean portUnification, boolean forceHttps) throws IOException, GeneralSecurityException { + + this.port = port; + this.idleTimeout = timeout; + this.commandUrl = commandUrl; + this.address = address; + + server = new Server(); + ServerConnector connector = null; + + if(!portUnification && !forceHttps) { + connector = new ServerConnector(server); + } else { + SecureRequestCustomizer customizer = new SecureRequestCustomizer(); + customizer.setStsMaxAge(DEFAULT_STS_MAX_AGE); + customizer.setStsIncludeSubDomains(true); + + HttpConfiguration config = new HttpConfiguration(); + config.setSecureScheme("https"); + config.addCustomizer(customizer); + + try(QuorumX509Util x509Util = new QuorumX509Util()) { + String privateKeyType = System.getProperty(x509Util.getSslKeystoreTypeProperty(), ""); + String privateKeyPath = System.getProperty(x509Util.getSslKeystoreLocationProperty(), ""); + String privateKeyPassword = System.getProperty(x509Util.getSslKeystorePasswdProperty(), ""); + String certAuthType = System.getProperty(x509Util.getSslTruststoreTypeProperty(), ""); + String certAuthPath = System.getProperty(x509Util.getSslTruststoreLocationProperty(), ""); + String certAuthPassword = System.getProperty(x509Util.getSslTruststorePasswdProperty(), ""); + KeyStore keyStore = null, trustStore = null; + + try { + keyStore = X509Util.loadKeyStore(privateKeyPath, privateKeyPassword, privateKeyType); + trustStore = X509Util.loadTrustStore(certAuthPath, certAuthPassword, certAuthType); + LOG.info("Successfully loaded private key from {}", privateKeyPath); + LOG.info("Successfully loaded certificate authority from {}", certAuthPath); + } catch(Exception e) { + LOG.error("Failed to load authentication certificates for admin server.", e); + throw e; + } + + SslContextFactory sslContextFactory = new SslContextFactory.Server(); + sslContextFactory.setKeyStore(keyStore); + sslContextFactory.setKeyStorePassword(privateKeyPassword); + sslContextFactory.setTrustStore(trustStore); + sslContextFactory.setTrustStorePassword(certAuthPassword); + + if(forceHttps) { + connector = new ServerConnector(server, + new SslConnectionFactory(sslContextFactory, HttpVersion.fromVersion(httpVersion).asString()), + new HttpConnectionFactory(config)); + } else { + connector = new ServerConnector(server, new UnifiedConnectionFactory(sslContextFactory, + HttpVersion.fromVersion(httpVersion).asString()), new HttpConnectionFactory(config)); + } + } + } + + connector.setHost(address); + connector.setPort(port); + connector.setIdleTimeout(idleTimeout); + + server.addConnector(connector); + + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/*"); + constrainTraceMethod(context); + server.setHandler(context); + + context.addServlet(new ServletHolder(new CommandServlet()), commandUrl + "/*"); + } + + /** + * Start the embedded Jetty server. + */ + @Override + public void start() throws AdminServerException { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + server.start(); + } catch(Exception e) { + // Server.start() only throws Exception, so let's at least wrap it + // in an identifiable subclass + String message = String + .format("Problem starting AdminServer on address %s, port %d and command URL %s", address, port, + commandUrl); + throw new AdminServerException(message, e); + } + LOG.info("Started AdminServer on address {}, port {} and command URL {}", address, port, commandUrl); + } + + /** + * Stop the embedded Jetty server. + * + * This is not very important except for tests where multiple + * JettyAdminServers are started and may try to bind to the same ports if + * previous servers aren't shut down. + */ + @Override + public void shutdown() throws AdminServerException { + try { + server.stop(); + } catch(Exception e) { + String message = String + .format("Problem stopping AdminServer on address %s, port %d and command URL %s", address, port, + commandUrl); + throw new AdminServerException(message, e); + } + } + + /** + * Set the ZooKeeperServer that will be used to run Commands. + * + * It is not necessary to set the ZK server before calling + * AdminServer.start(), and the ZK server can be set to null when, e.g., + * that server is being shut down. If the ZK server is not set or set to + * null, the AdminServer will still be able to issue Commands, but they will + * return an error until a ZK server is set. + */ + @Override + public void setZooKeeperServer(ZooKeeperServer zkServer) { + this.zkServer = zkServer; + } + + private class CommandServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + protected void doGet(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + // Capture the command name from the URL + String cmd = request.getPathInfo(); + if(cmd == null || cmd.equals("/")) { + // No command specified, print links to all commands instead + for(String link : commandLinks()) { + response.getWriter().println(link); + response.getWriter().println("
      "); + } + return; + } + // Strip leading "/" + cmd = cmd.substring(1); + + // Extract keyword arguments to command from request parameters + @SuppressWarnings("unchecked") Map parameterMap = request.getParameterMap(); + Map kwargs = new HashMap(); + for(Map.Entry entry : parameterMap.entrySet()) { + kwargs.put(entry.getKey(), entry.getValue()[0]); + } + + // Run the command + CommandResponse cmdResponse = Commands.runCommand(cmd, zkServer, kwargs); + + // Format and print the output of the command + CommandOutputter outputter = new JsonOutputter(); + response.setStatus(HttpServletResponse.SC_OK); + response.setContentType(outputter.getContentType()); + outputter.output(cmdResponse, response.getWriter()); + } + + } + + /** + * Returns a list of URLs to each registered Command. + */ + private List commandLinks() { + List links = new ArrayList(); + List commands = new ArrayList(Commands.getPrimaryNames()); + Collections.sort(commands); + for(String command : commands) { + String url = commandUrl + "/" + command; + links.add(String.format("%s", url, command)); + } + return links; + } + + /** + * Add constraint to a given context to disallow TRACE method + * + * @param ctxHandler the context to modify + */ + private void constrainTraceMethod(ServletContextHandler ctxHandler) { + Constraint c = new Constraint(); + c.setAuthenticate(true); + + ConstraintMapping cmt = new ConstraintMapping(); + cmt.setConstraint(c); + cmt.setMethod("TRACE"); + cmt.setPathSpec("/*"); + + ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); + securityHandler.setConstraintMappings(new ConstraintMapping[]{cmt}); + + ctxHandler.setSecurityHandler(securityHandler); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/JsonOutputter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/JsonOutputter.java new file mode 100644 index 0000000..0679cfa --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/JsonOutputter.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import com.fasterxml.jackson.core.JsonGenerationException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.SerializationFeature; +import java.io.IOException; +import java.io.PrintWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JsonOutputter implements CommandOutputter { + + static final Logger LOG = LoggerFactory.getLogger(JsonOutputter.class); + + public static final String ERROR_RESPONSE = "{\"error\": \"Exception writing command response to JSON\"}"; + + private ObjectMapper mapper; + + public JsonOutputter() { + mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); + mapper.configure(SerializationFeature.INDENT_OUTPUT, true); + mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); + } + + @Override + public String getContentType() { + return "application/json"; + } + + @Override + public void output(CommandResponse response, PrintWriter pw) { + try { + mapper.writeValue(pw, response.toMap()); + } catch (JsonGenerationException e) { + LOG.warn("Exception writing command response to JSON:", e); + pw.write(ERROR_RESPONSE); + } catch (JsonMappingException e) { + LOG.warn("Exception writing command response to JSON:", e); + pw.write(ERROR_RESPONSE); + } catch (IOException e) { + LOG.warn("Exception writing command response to JSON:", e); + pw.write(ERROR_RESPONSE); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/ReadAheadEndpoint.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/ReadAheadEndpoint.java new file mode 100644 index 0000000..9f7dd17 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/ReadAheadEndpoint.java @@ -0,0 +1,209 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +// This code was found and refactored from here: +// https://stackoverflow.com/questions/11182192/how-do-i-serve-https-and-http-for-jetty-from-one-port/40076056#40076056 + +package org.apache.zookeeper.server.admin; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.ReadPendingException; +import java.nio.channels.WritePendingException; +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.util.Callback; + +public class ReadAheadEndpoint implements EndPoint { + + private final EndPoint endPoint; + private final ByteBuffer start; + private final byte[] bytes; + private int leftToRead; + private IOException pendingException = null; + + @Override + public InetSocketAddress getLocalAddress() { + return endPoint.getLocalAddress(); + } + @Override + public InetSocketAddress getRemoteAddress() { + return endPoint.getRemoteAddress(); + } + @Override + public boolean isOpen() { + return endPoint.isOpen(); + } + @Override + public long getCreatedTimeStamp() { + return endPoint.getCreatedTimeStamp(); + } + @Override + public boolean isOutputShutdown() { + return endPoint.isOutputShutdown(); + } + @Override + public boolean isInputShutdown() { + return endPoint.isInputShutdown(); + } + @Override + public void shutdownOutput() { + endPoint.shutdownOutput(); + } + @Override + public void close() { + endPoint.close(); + } + @Override + public Object getTransport() { + return endPoint.getTransport(); + } + @Override + public long getIdleTimeout() { + return endPoint.getIdleTimeout(); + } + @Override + public Connection getConnection() { + return endPoint.getConnection(); + } + @Override + public void onOpen() { + endPoint.onOpen(); + } + @Override + public void onClose() { + endPoint.onClose(); + } + @Override + public boolean isOptimizedForDirectBuffers() { + return endPoint.isOptimizedForDirectBuffers(); + } + @Override + public boolean isFillInterested() { + return endPoint.isFillInterested(); + } + @Override + public boolean tryFillInterested(Callback v) { + return endPoint.tryFillInterested(v); + } + @Override + public boolean flush(ByteBuffer... v) throws IOException { + return endPoint.flush(v); + } + @Override + public void setIdleTimeout(long v) { + endPoint.setIdleTimeout(v); + } + @Override + public void write(Callback v, ByteBuffer... b) throws WritePendingException { + endPoint.write(v, b); + } + @Override + public void setConnection(Connection v) { + endPoint.setConnection(v); + } + @Override + public void upgrade(Connection v) { + endPoint.upgrade(v); + } + @Override + public void fillInterested(Callback v) throws ReadPendingException { + endPoint.fillInterested(v); + } + + public ReadAheadEndpoint(final EndPoint channel, final int readAheadLength) { + if (channel == null) { + throw new IllegalArgumentException("channel cannot be null"); + } + + this.endPoint = channel; + start = ByteBuffer.wrap(bytes = new byte[readAheadLength]); + start.flip(); + leftToRead = readAheadLength; + } + + private synchronized void readAhead() throws IOException { + if (leftToRead > 0) { + int n = 0; + do { + n = endPoint.fill(start); + } while (n == 0 && endPoint.isOpen() && !endPoint.isInputShutdown()); + if (n == -1) { + leftToRead = -1; + } else { + leftToRead -= n; + } + if (leftToRead <= 0) { + start.rewind(); + } + } + } + + private int readFromStart(final ByteBuffer dst) throws IOException { + final int n = Math.min(dst.remaining(), start.remaining()); + if (n > 0) { + dst.put(bytes, start.position(), n); + start.position(start.position() + n); + dst.flip(); + } + return n; + } + + @Override + public synchronized int fill(final ByteBuffer dst) throws IOException { + throwPendingException(); + if (leftToRead > 0) { + readAhead(); + } + if (leftToRead > 0) { + return 0; + } + final int sr = start.remaining(); + if (sr > 0) { + dst.compact(); + final int n = readFromStart(dst); + if (n < sr) { + return n; + } + } + return sr + endPoint.fill(dst); + } + + public byte[] getBytes() { + if (pendingException == null) { + try { + readAhead(); + } catch (IOException e) { + pendingException = e; + } + } + byte[] ret = new byte[bytes.length]; + System.arraycopy(bytes, 0, ret, 0, ret.length); + return ret; + } + + private void throwPendingException() throws IOException { + if (pendingException != null) { + IOException e = pendingException; + pendingException = null; + throw e; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/UnifiedConnectionFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/UnifiedConnectionFactory.java new file mode 100644 index 0000000..71622df --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/admin/UnifiedConnectionFactory.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import javax.net.ssl.SSLEngine; +import javax.net.ssl.SSLSession; +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.EndPoint; +import org.eclipse.jetty.io.ssl.SslConnection; +import org.eclipse.jetty.server.AbstractConnectionFactory; +import org.eclipse.jetty.server.ConnectionFactory; +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The purpose of this class is to dynamically determine whether to create + * a plaintext or SSL connection whenever newConnection() is called. It works + * in conjunction with ReadAheadEndpoint to inspect bytes on the incoming + * connection. + */ +public class UnifiedConnectionFactory extends AbstractConnectionFactory { + + private static final Logger LOG = LoggerFactory.getLogger(UnifiedConnectionFactory.class); + + private final SslContextFactory sslContextFactory; + private final String nextProtocol; + + public UnifiedConnectionFactory(String nextProtocol) { + this(null, nextProtocol); + } + + public UnifiedConnectionFactory(SslContextFactory factory, String nextProtocol) { + super("SSL"); + this.sslContextFactory = (factory == null) ? new SslContextFactory.Server() : factory; + this.nextProtocol = nextProtocol; + this.addBean(this.sslContextFactory); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + SSLEngine engine = this.sslContextFactory.newSSLEngine(); + SSLSession session = engine.getSession(); + engine.setUseClientMode(false); + if (session.getPacketBufferSize() > this.getInputBufferSize()) { + this.setInputBufferSize(session.getPacketBufferSize()); + } + } + + @Override + public Connection newConnection(Connector connector, EndPoint realEndPoint) { + ReadAheadEndpoint aheadEndpoint = new ReadAheadEndpoint(realEndPoint, 1); + byte[] bytes = aheadEndpoint.getBytes(); + boolean isSSL; + + if (bytes == null || bytes.length == 0) { + isSSL = false; + LOG.warn("Incoming connection has no data"); + } else { + byte b = bytes[0]; // TLS first byte is 0x16, let's not support SSLv3 and below + isSSL = b == 0x16; // matches SSL detection in NettyServerCnxnFactory.java + } + + LOG.debug(String.format("UnifiedConnectionFactory: newConnection() with SSL = %b", isSSL)); + + EndPoint plainEndpoint; + SslConnection sslConnection; + + if (isSSL) { + SSLEngine engine = this.sslContextFactory.newSSLEngine(aheadEndpoint.getRemoteAddress()); + engine.setUseClientMode(false); + sslConnection = this.newSslConnection(connector, aheadEndpoint, engine); + sslConnection.setRenegotiationAllowed(this.sslContextFactory.isRenegotiationAllowed()); + this.configure(sslConnection, connector, aheadEndpoint); + plainEndpoint = sslConnection.getDecryptedEndPoint(); + } else { + sslConnection = null; + plainEndpoint = aheadEndpoint; + } + + ConnectionFactory next = connector.getConnectionFactory(nextProtocol); + Connection connection = next.newConnection(connector, plainEndpoint); + plainEndpoint.setConnection(connection); + + return (sslConnection == null) ? connection : sslConnection; + } + + protected SslConnection newSslConnection( + final Connector connector, + final EndPoint endPoint, + final SSLEngine engine) { + return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine); + } + + @Override + public String toString() { + return String.format( + "%s@%x{%s->%s}", + this.getClass().getSimpleName(), + this.hashCode(), + this.getProtocol(), + this.nextProtocol); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/AuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/AuthenticationProvider.java new file mode 100644 index 0000000..179eac8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/AuthenticationProvider.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.ServerCnxn; + +/** + * This interface is implemented by authentication providers to add new kinds of + * authentication schemes to ZooKeeper. + */ +public interface AuthenticationProvider { + + /** + * The String used to represent this provider. This will correspond to the + * scheme field of an Id. + * + * @return the scheme of this provider. + */ + String getScheme(); + + /** + * This method is called when a client passes authentication data for this + * scheme. The authData is directly from the authentication packet. The + * implementor may attach new ids to the authInfo field of cnxn or may use + * cnxn to send packets back to the client. + * + * @param cnxn + * the cnxn that received the authentication information. + * @param authData + * the authentication data received. + * @return TODO + */ + KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData); + + /** + * This method is called to see if the given id matches the given id + * expression in the ACL. This allows schemes to use application specific + * wild cards. + * + * @param id + * the id to check. + * @param aclExpr + * the expression to match ids against. + * @return true if the id can be matched by the expression. + */ + boolean matches(String id, String aclExpr); + + /** + * This method is used to check if the authentication done by this provider + * should be used to identify the creator of a node. Some ids such as hosts + * and ip addresses are rather transient and in general don't really + * identify a client even though sometimes they do. + * + * @return true if this provider identifies creators. + */ + boolean isAuthenticated(); + + /** + * Validates the syntax of an id. + * + * @param id + * the id to validate. + * @return true if id is well formed. + */ + boolean isValid(String id); + + /** + * id represents the authentication info which is set in server connection. + * id may contain both user name as well as password. + * This method should be implemented to extract the user name. + * + * @param id authentication info set by client. + * @return String user name + */ + default String getUserName(String id) { + // Most of the authentication providers id contains only user name. + return id; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java new file mode 100644 index 0000000..2330000 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestAuthenticationProvider.java @@ -0,0 +1,143 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.ServerCnxn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DigestAuthenticationProvider implements AuthenticationProvider { + + private static final Logger LOG = LoggerFactory.getLogger(DigestAuthenticationProvider.class); + + /** specify a command line property with key of + * "zookeeper.DigestAuthenticationProvider.superDigest" + * and value of "super:<base64encoded(SHA1(password))>" to enable + * super user access (i.e. acls disabled) + */ + private static final String superDigest = System.getProperty("zookeeper.DigestAuthenticationProvider.superDigest"); + + public String getScheme() { + return "digest"; + } + + private static String base64Encode(byte[] b) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < b.length; ) { + int pad = 0; + int v = (b[i++] & 0xff) << 16; + if (i < b.length) { + v |= (b[i++] & 0xff) << 8; + } else { + pad++; + } + if (i < b.length) { + v |= (b[i++] & 0xff); + } else { + pad++; + } + sb.append(encode(v >> 18)); + sb.append(encode(v >> 12)); + if (pad < 2) { + sb.append(encode(v >> 6)); + } else { + sb.append('='); + } + if (pad < 1) { + sb.append(encode(v)); + } else { + sb.append('='); + } + } + return sb.toString(); + } + + private static char encode(int i) { + i &= 0x3f; + if (i < 26) { + return (char) ('A' + i); + } + if (i < 52) { + return (char) ('a' + i - 26); + } + if (i < 62) { + return (char) ('0' + i - 52); + } + return i == 62 ? '+' : '/'; + } + + public static String generateDigest(String idPassword) throws NoSuchAlgorithmException { + String[] parts = idPassword.split(":", 2); + byte[] digest = MessageDigest.getInstance("SHA1").digest(idPassword.getBytes()); + return parts[0] + ":" + base64Encode(digest); + } + + public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + String id = new String(authData); + try { + String digest = generateDigest(id); + if (digest.equals(superDigest)) { + cnxn.addAuthInfo(new Id("super", "")); + } + cnxn.addAuthInfo(new Id(getScheme(), digest)); + return KeeperException.Code.OK; + } catch (NoSuchAlgorithmException e) { + LOG.error("Missing algorithm", e); + } + return KeeperException.Code.AUTHFAILED; + } + + public boolean isAuthenticated() { + return true; + } + + public boolean isValid(String id) { + String[] parts = id.split(":"); + return parts.length == 2; + } + + public boolean matches(String id, String aclExpr) { + return id.equals(aclExpr); + } + + @Override + public String getUserName(String id) { + /** + * format is already enforced in server code. so no need to check it + * again, just assume it is in correct format + */ + return id.split(":")[0]; + } + + /** Call with a single argument of user:pass to generate authdata. + * Authdata output can be used when setting superDigest for example. + * @param args single argument of user:pass + * @throws NoSuchAlgorithmException + */ + public static void main(String[] args) throws NoSuchAlgorithmException { + for (int i = 0; i < args.length; i++) { + System.out.println(args[i] + "->" + generateDigest(args[i])); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestLoginModule.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestLoginModule.java new file mode 100644 index 0000000..245c2b1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/DigestLoginModule.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.util.Map; +import javax.security.auth.Subject; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.spi.LoginModule; + +public class DigestLoginModule implements LoginModule { + + private Subject subject; + + public boolean abort() { + return false; + } + + public boolean commit() { + return true; + } + + public void initialize( + Subject subject, + CallbackHandler callbackHandler, + Map sharedState, + Map options) { + if (options.containsKey("username")) { + // Zookeeper client: get username and password from JAAS conf (only used if using DIGEST-MD5). + this.subject = subject; + String username = (String) options.get("username"); + this.subject.getPublicCredentials().add(username); + String password = (String) options.get("password"); + this.subject.getPrivateCredentials().add(password); + } + return; + } + + public boolean logout() { + return true; + } + + public boolean login() { + // Unlike with Krb5LoginModule, we don't do any actual login or credential passing here: authentication to Zookeeper + // is done later, through the SASLClient object. + return true; + } + +} + + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/EnsembleAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/EnsembleAuthenticationProvider.java new file mode 100644 index 0000000..5fb4de2 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/EnsembleAuthenticationProvider.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.util.HashSet; +import java.util.Set; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerMetrics; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is not a true AuthenticationProvider in the strict sense. it does + * handle add auth requests, but rather than authenticate the client, it checks + * to make sure that the ensemble name the client intends to connect to + * matches the name that the server thinks it belongs to. if the name does not match, + * this provider will close the connection. + */ + +public class EnsembleAuthenticationProvider implements AuthenticationProvider { + + private static final Logger LOG = LoggerFactory.getLogger(EnsembleAuthenticationProvider.class); + + public static final String ENSEMBLE_PROPERTY = "zookeeper.ensembleAuthName"; + private static final int MIN_LOGGING_INTERVAL_MS = 1000; + private Set ensembleNames; + + public EnsembleAuthenticationProvider() { + String namesCSV = System.getProperty(ENSEMBLE_PROPERTY); + if (namesCSV != null) { + LOG.info("Set expected ensemble names to {}", namesCSV); + setEnsembleNames(namesCSV); + } + } + + public void setEnsembleNames(String namesCSV) { + ensembleNames = new HashSet(); + for (String name : namesCSV.split(",")) { + ensembleNames.add(name.trim()); + } + } + + /* provider methods */ + @Override + public String getScheme() { + return "ensemble"; + } + + /** + * if things go bad, we don't want to freak out with the logging, so track + * the last time we logged something here. + */ + private long lastFailureLogged; + + @Override + public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + if (authData == null || authData.length == 0) { + ServerMetrics.getMetrics().ENSEMBLE_AUTH_SKIP.add(1); + return KeeperException.Code.OK; + } + + String receivedEnsembleName = new String(authData); + + if (ensembleNames == null) { + ServerMetrics.getMetrics().ENSEMBLE_AUTH_SKIP.add(1); + return KeeperException.Code.OK; + } + + if (ensembleNames.contains(receivedEnsembleName)) { + ServerMetrics.getMetrics().ENSEMBLE_AUTH_SUCCESS.add(1); + return KeeperException.Code.OK; + } + + long currentTime = System.currentTimeMillis(); + if (lastFailureLogged + MIN_LOGGING_INTERVAL_MS < currentTime) { + String id = cnxn.getRemoteSocketAddress().getAddress().getHostAddress(); + LOG.warn("Unexpected ensemble name: ensemble name: {} client ip: {}", receivedEnsembleName, id); + lastFailureLogged = currentTime; + } + /* + * we are doing a close here rather than returning some other error + * since we want the client to choose another server to connect to. if + * we return an error, the client will get a fatal auth error and + * shutdown. + */ + ServerMetrics.getMetrics().ENSEMBLE_AUTH_FAIL.add(1); + cnxn.close(ServerCnxn.DisconnectReason.FAILED_HANDSHAKE); + return KeeperException.Code.BADARGUMENTS; + } + + /* + * since we aren't a true provider we return false for everything so that + * it isn't used in ACLs. + */ + @Override + public boolean matches(String id, String aclExpr) { + return false; + } + + @Override + public boolean isAuthenticated() { + return false; + } + + @Override + public boolean isValid(String id) { + return false; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java new file mode 100644 index 0000000..b93e55a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/IPAuthenticationProvider.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.ServerCnxn; + +public class IPAuthenticationProvider implements AuthenticationProvider { + + public String getScheme() { + return "ip"; + } + + public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + String id = cnxn.getRemoteSocketAddress().getAddress().getHostAddress(); + cnxn.addAuthInfo(new Id(getScheme(), id)); + return KeeperException.Code.OK; + } + + // This is a bit weird but we need to return the address and the number of + // bytes (to distinguish between IPv4 and IPv6 + private byte[] addr2Bytes(String addr) { + byte[] b = v4addr2Bytes(addr); + // TODO Write the v6addr2Bytes + return b; + } + + private byte[] v4addr2Bytes(String addr) { + String[] parts = addr.split("\\.", -1); + if (parts.length != 4) { + return null; + } + byte[] b = new byte[4]; + for (int i = 0; i < 4; i++) { + try { + int v = Integer.parseInt(parts[i]); + if (v >= 0 && v <= 255) { + b[i] = (byte) v; + } else { + return null; + } + } catch (NumberFormatException e) { + return null; + } + } + return b; + } + + private void mask(byte[] b, int bits) { + int start = bits / 8; + int startMask = (1 << (8 - (bits % 8))) - 1; + startMask = ~startMask; + while (start < b.length) { + b[start] &= startMask; + startMask = 0; + start++; + } + } + + public boolean matches(String id, String aclExpr) { + String[] parts = aclExpr.split("/", 2); + byte[] aclAddr = addr2Bytes(parts[0]); + if (aclAddr == null) { + return false; + } + int bits = aclAddr.length * 8; + if (parts.length == 2) { + try { + bits = Integer.parseInt(parts[1]); + if (bits < 0 || bits > aclAddr.length * 8) { + return false; + } + } catch (NumberFormatException e) { + return false; + } + } + mask(aclAddr, bits); + byte[] remoteAddr = addr2Bytes(id); + if (remoteAddr == null) { + return false; + } + mask(remoteAddr, bits); + for (int i = 0; i < remoteAddr.length; i++) { + if (remoteAddr[i] != aclAddr[i]) { + return false; + } + } + return true; + } + + public boolean isAuthenticated() { + return false; + } + + public boolean isValid(String id) { + String[] parts = id.split("/", 2); + byte[] aclAddr = addr2Bytes(parts[0]); + if (aclAddr == null) { + return false; + } + if (parts.length == 2) { + try { + int bits = Integer.parseInt(parts[1]); + if (bits < 0 || bits > aclAddr.length * 8) { + return false; + } + } catch (NumberFormatException e) { + return false; + } + } + return true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/KerberosName.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/KerberosName.java new file mode 100644 index 0000000..02ec593 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/KerberosName.java @@ -0,0 +1,418 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +/* This file copied from Hadoop's security branch, + * with the following changes: + * 1. package changed from org.apache.hadoop.security to + * org.apache.zookeeper.server.auth. + * 2. Usage of Hadoop's Configuration class removed since + * it is not available in Zookeeper: instead, system property + * "zookeeper.security.auth_to_local" is used. + */ + +package org.apache.zookeeper.server.auth; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.zookeeper.server.util.KerberosUtil; + +/** + * This class implements parsing and handling of Kerberos principal names. In + * particular, it splits them apart and translates them down into local + * operating system names. + */ +public class KerberosName { + + /** The first component of the name */ + private final String serviceName; + /** The second component of the name. It may be null. */ + private final String hostName; + /** The realm of the name. */ + private final String realm; + + /** + * A pattern that matches a Kerberos name with at most 2 components. + */ + private static final Pattern nameParser = Pattern.compile("([^/@]*)(/([^/@]*))?@([^/@]*)"); + + /** + * A pattern that matches a string with out '$' and then a single + * parameter with $n. + */ + private static Pattern parameterPattern = Pattern.compile("([^$]*)(\\$(\\d*))?"); + + /** + * A pattern for parsing a auth_to_local rule. + */ + private static final Pattern ruleParser = Pattern.compile( + "\\s*((DEFAULT)|(RULE:\\[(\\d*):([^\\]]*)](\\(([^)]*)\\))?" + + "(s/([^/]*)/([^/]*)/(g)?)?))"); + + /** + * A pattern that recognizes simple/non-simple names. + */ + private static final Pattern nonSimplePattern = Pattern.compile("[/@]"); + + /** + * The list of translation rules. + */ + private static List rules; + + private static String defaultRealm; + + static { + try { + defaultRealm = KerberosUtil.getDefaultRealm(); + } catch (Exception ke) { + if ((System.getProperty("zookeeper.requireKerberosConfig") != null) + && (System.getProperty("zookeeper.requireKerberosConfig").equals("true"))) { + throw new IllegalArgumentException("Can't get Kerberos configuration", ke); + } else { + defaultRealm = ""; + } + } + try { + // setConfiguration() will work even if the above try() fails due + // to a missing Kerberos configuration (unless zookeeper.requireKerberosConfig + // is set to true, which would not allow execution to reach here due to the + // throwing of an IllegalArgumentException above). + setConfiguration(); + } catch (IOException e) { + throw new IllegalArgumentException("Could not configure Kerberos principal name mapping."); + } + } + + /** + * Create a name from the full Kerberos principal name. + * @param name + */ + public KerberosName(String name) { + Matcher match = nameParser.matcher(name); + if (!match.matches()) { + if (name.contains("@")) { + throw new IllegalArgumentException("Malformed Kerberos name: " + name); + } else { + serviceName = name; + hostName = null; + realm = null; + } + } else { + serviceName = match.group(1); + hostName = match.group(3); + realm = match.group(4); + } + } + + /** + * Get the configured default realm. + * @return the default realm from the krb5.conf + */ + public String getDefaultRealm() { + return defaultRealm; + } + + /** + * Put the name back together from the parts. + */ + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + result.append(serviceName); + if (hostName != null) { + result.append('/'); + result.append(hostName); + } + if (realm != null) { + result.append('@'); + result.append(realm); + } + return result.toString(); + } + + /** + * Get the first component of the name. + * @return the first section of the Kerberos principal name + */ + public String getServiceName() { + return serviceName; + } + + /** + * Get the second component of the name. + * @return the second section of the Kerberos principal name, and may be null + */ + public String getHostName() { + return hostName; + } + + /** + * Get the realm of the name. + * @return the realm of the name, may be null + */ + public String getRealm() { + return realm; + } + + /** + * An encoding of a rule for translating kerberos names. + */ + private static class Rule { + + private final boolean isDefault; + private final int numOfComponents; + private final String format; + private final Pattern match; + private final Pattern fromPattern; + private final String toPattern; + private final boolean repeat; + + Rule() { + isDefault = true; + numOfComponents = 0; + format = null; + match = null; + fromPattern = null; + toPattern = null; + repeat = false; + } + + Rule(int numOfComponents, String format, String match, String fromPattern, String toPattern, boolean repeat) { + isDefault = false; + this.numOfComponents = numOfComponents; + this.format = format; + this.match = match == null ? null : Pattern.compile(match); + this.fromPattern = fromPattern == null ? null : Pattern.compile(fromPattern); + this.toPattern = toPattern; + this.repeat = repeat; + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + if (isDefault) { + buf.append("DEFAULT"); + } else { + buf.append("RULE:["); + buf.append(numOfComponents); + buf.append(':'); + buf.append(format); + buf.append(']'); + if (match != null) { + buf.append('('); + buf.append(match); + buf.append(')'); + } + if (fromPattern != null) { + buf.append("s/"); + buf.append(fromPattern); + buf.append('/'); + buf.append(toPattern); + buf.append('/'); + if (repeat) { + buf.append('g'); + } + } + } + return buf.toString(); + } + + /** + * Replace the numbered parameters of the form $n where n is from 1 to + * the length of params. Normal text is copied directly and $n is replaced + * by the corresponding parameter. + * @param format the string to replace parameters again + * @param params the list of parameters + * @return the generated string with the parameter references replaced. + * @throws BadFormatString + */ + static String replaceParameters(String format, String[] params) throws BadFormatString { + Matcher match = parameterPattern.matcher(format); + int start = 0; + StringBuilder result = new StringBuilder(); + while (start < format.length() && match.find(start)) { + result.append(match.group(1)); + String paramNum = match.group(3); + if (paramNum != null) { + try { + int num = Integer.parseInt(paramNum); + if (num < 0 || num > params.length) { + throw new BadFormatString(String.format( + "index %d from %s is outside of the valid range 0 to %d", + num, + format, + (params.length - 1))); + } + result.append(params[num]); + } catch (NumberFormatException nfe) { + throw new BadFormatString("bad format in username mapping in " + paramNum, nfe); + } + + } + start = match.end(); + } + return result.toString(); + } + + /** + * Replace the matches of the from pattern in the base string with the value + * of the to string. + * @param base the string to transform + * @param from the pattern to look for in the base string + * @param to the string to replace matches of the pattern with + * @param repeat whether the substitution should be repeated + * @return + */ + static String replaceSubstitution(String base, Pattern from, String to, boolean repeat) { + Matcher match = from.matcher(base); + if (repeat) { + return match.replaceAll(to); + } else { + return match.replaceFirst(to); + } + } + + /** + * Try to apply this rule to the given name represented as a parameter + * array. + * @param params first element is the realm, second and later elements are + * are the components of the name "a/b@FOO" -> {"FOO", "a", "b"} + * @return the short name if this rule applies or null + * @throws IOException throws if something is wrong with the rules + */ + String apply(String[] params) throws IOException { + String result = null; + if (isDefault) { + if (defaultRealm.equals(params[0])) { + result = params[1]; + } + } else if (params.length - 1 == numOfComponents) { + String base = replaceParameters(format, params); + if (match == null || match.matcher(base).matches()) { + if (fromPattern == null) { + result = base; + } else { + result = replaceSubstitution(base, fromPattern, toPattern, repeat); + } + } + } + if (result != null && nonSimplePattern.matcher(result).find()) { + throw new NoMatchingRule("Non-simple name " + result + " after auth_to_local rule " + this); + } + return result; + } + + } + + static List parseRules(String rules) { + List result = new ArrayList(); + String remaining = rules.trim(); + while (remaining.length() > 0) { + Matcher matcher = ruleParser.matcher(remaining); + if (!matcher.lookingAt()) { + throw new IllegalArgumentException("Invalid rule: " + remaining); + } + if (matcher.group(2) != null) { + result.add(new Rule()); + } else { + result.add(new Rule( + Integer.parseInt(matcher.group(4)), + matcher.group(5), + matcher.group(7), + matcher.group(9), + matcher.group(10), + "g".equals(matcher.group(11)))); + } + remaining = remaining.substring(matcher.end()); + } + return result; + } + + /** + * Set the static configuration to get the rules. + * @throws IOException + */ + public static void setConfiguration() throws IOException { + String ruleString = System.getProperty("zookeeper.security.auth_to_local", "DEFAULT"); + rules = parseRules(ruleString); + } + + @SuppressWarnings("serial") + public static class BadFormatString extends IOException { + + BadFormatString(String msg) { + super(msg); + } + BadFormatString(String msg, Throwable err) { + super(msg, err); + } + + } + + @SuppressWarnings("serial") + public static class NoMatchingRule extends IOException { + + NoMatchingRule(String msg) { + super(msg); + } + + } + + /** + * Get the translation of the principal name into an operating system + * user name. + * @return the short name + * @throws IOException + */ + public String getShortName() throws IOException { + String[] params; + if (hostName == null) { + // if it is already simple, just return it + if (realm == null) { + return serviceName; + } + params = new String[]{realm, serviceName}; + } else { + params = new String[]{realm, serviceName, hostName}; + } + for (Rule r : rules) { + String result = r.apply(params); + if (result != null) { + return result; + } + } + throw new NoMatchingRule("No rules applied to " + toString()); + } + + static void printRules() throws IOException { + int i = 0; + for (Rule r : rules) { + System.out.println(++i + " " + r); + } + } + + public static void main(String[] args) throws Exception { + for (String arg : args) { + KerberosName name = new KerberosName(arg); + System.out.println("Name: " + name + " to " + name.getShortName()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/KeyAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/KeyAuthenticationProvider.java new file mode 100644 index 0000000..03c15a1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/KeyAuthenticationProvider.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.nio.charset.StandardCharsets; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/* + * This class is a sample implementation of being passed the ZooKeeperServer + * handle in the constructor, and reading data from zknodes to authenticate. + * At a minimum, a real Auth provider would need to override validate() to + * e.g. perform certificate validation of auth based a public key. + * + * See the "Pluggable ZooKeeper authentication" section of the + * "Zookeeper Programmer's Guide" for general details of implementing an + * authentication plugin. e.g. + * http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html#sc_ZooKeeperPluggableAuthentication + * + * This class looks for a numeric "key" under the /key node. + * Authorization is granted if the user passes in as authorization a number + * which is a multiple of the key value, i.e. + * (auth % key) == 0 + * In a real implementation, you might do something like storing a public + * key in /key, and using it to verify that auth tokens passed in were signed + * by the corresponding private key. + * + * When the node /key does not exist, any auth token is accepted, so that + * bootstrapping may occur. + * + */ +public class KeyAuthenticationProvider extends ServerAuthenticationProvider { + + private static final Logger LOG = LoggerFactory.getLogger(KeyAuthenticationProvider.class); + + public String getScheme() { + return "key"; + } + + private byte[] getKey(ZooKeeperServer zks) { + ZKDatabase db = zks.getZKDatabase(); + if (db != null) { + try { + Stat stat = new Stat(); + return db.getData("/key", stat, null); + } catch (NoNodeException e) { + LOG.error("getData failed", e); + } + } + return null; + } + + private boolean validate(byte[] key, byte[] auth) { + // perform arbitrary function (auth is a multiple of key) + try { + String keyStr = new String(key, StandardCharsets.UTF_8); + String authStr = new String(auth, StandardCharsets.UTF_8); + int keyVal = Integer.parseInt(keyStr); + int authVal = Integer.parseInt(authStr); + if (keyVal != 0 && ((authVal % keyVal) != 0)) { + return false; + } + } catch (NumberFormatException nfe) { + LOG.error("bad formatting", nfe); + return false; + } + return true; + } + + @Override + public KeeperException.Code handleAuthentication(ServerObjs serverObjs, byte[] authData) { + byte[] key = getKey(serverObjs.getZks()); + String authStr = ""; + String keyStr = ""; + try { + authStr = new String(authData, StandardCharsets.UTF_8); + } catch (Exception e) { + LOG.error("UTF-8", e); + } + if (key != null) { + if (!validate(key, authData)) { + try { + keyStr = new String(key, StandardCharsets.UTF_8); + } catch (Exception e) { + LOG.error("UTF-8", e); + // empty key + keyStr = authStr; + } + LOG.debug("KeyAuthenticationProvider handleAuthentication ({}, {}) -> FAIL.\n", keyStr, authStr); + return KeeperException.Code.AUTHFAILED; + } + } + // default to allow, so the key can be initially written + LOG.debug("KeyAuthenticationProvider handleAuthentication -> OK.\n"); + // NOTE: keyStr in addAuthInfo() sticks with the created node ACLs. + // For transient keys or certificates, this presents a problem. + // In that case, replace it with something non-ephemeral (or punt with null). + // + // BOTH addAuthInfo and an OK return-code are needed for authentication. + serverObjs.getCnxn().addAuthInfo(new Id(getScheme(), keyStr)); + return KeeperException.Code.OK; + } + + @Override + public boolean matches(ServerObjs serverObjs, MatchValues matchValues) { + return matchValues.getId().equals(matchValues.getAclExpr()); + } + + @Override + public boolean isAuthenticated() { + return true; + } + + @Override + public boolean isValid(String id) { + return true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java new file mode 100644 index 0000000..856cf78 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ProviderRegistry.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ProviderRegistry { + + private static final Logger LOG = LoggerFactory.getLogger(ProviderRegistry.class); + + public static final String AUTHPROVIDER_PROPERTY_PREFIX = "zookeeper.authProvider."; + + private static boolean initialized = false; + private static final Map authenticationProviders = new HashMap<>(); + + //VisibleForTesting + public static void reset() { + synchronized (ProviderRegistry.class) { + initialized = false; + authenticationProviders.clear(); + } + } + + public static void initialize() { + synchronized (ProviderRegistry.class) { + IPAuthenticationProvider ipp = new IPAuthenticationProvider(); + DigestAuthenticationProvider digp = new DigestAuthenticationProvider(); + authenticationProviders.put(ipp.getScheme(), ipp); + authenticationProviders.put(digp.getScheme(), digp); + Enumeration en = System.getProperties().keys(); + while (en.hasMoreElements()) { + String k = (String) en.nextElement(); + if (k.startsWith(AUTHPROVIDER_PROPERTY_PREFIX)) { + String className = System.getProperty(k); + try { + Class c = ZooKeeperServer.class.getClassLoader().loadClass(className); + AuthenticationProvider ap = (AuthenticationProvider) c.getDeclaredConstructor().newInstance(); + authenticationProviders.put(ap.getScheme(), ap); + } catch (Exception e) { + LOG.warn("Problems loading {}", className, e); + } + } + } + initialized = true; + } + } + + public static ServerAuthenticationProvider getServerProvider(String scheme) { + return WrappedAuthenticationProvider.wrap(getProvider(scheme)); + } + + public static AuthenticationProvider getProvider(String scheme) { + if (!initialized) { + initialize(); + } + return authenticationProviders.get(scheme); + } + + public static void removeProvider(String scheme) { + authenticationProviders.remove(scheme); + } + + public static String listProviders() { + StringBuilder sb = new StringBuilder(); + for (String s : authenticationProviders.keySet()) { + sb.append(s).append(" "); + } + return sb.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/SASLAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/SASLAuthenticationProvider.java new file mode 100644 index 0000000..9438732 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/SASLAuthenticationProvider.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.ServerCnxn; + +public class SASLAuthenticationProvider implements AuthenticationProvider { + + public String getScheme() { + return "sasl"; + } + + public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + // Should never call this: SASL authentication is negotiated at session initiation. + // TODO: consider substituting current implementation of direct ClientCnxn manipulation with + // a call to this method (SASLAuthenticationProvider:handleAuthentication()) at session initiation. + return KeeperException.Code.AUTHFAILED; + + } + + public boolean matches(String id, String aclExpr) { + if ((id.equals("super") || id.equals(aclExpr))) { + return true; + } + String readAccessUser = System.getProperty("zookeeper.letAnySaslUserDoX"); + return readAccessUser != null && aclExpr.equals(readAccessUser); + } + + public boolean isAuthenticated() { + return true; + } + + public boolean isValid(String id) { + // Since the SASL authenticator will usually be used with Kerberos authentication, + // it should enforce that these names are valid according to Kerberos's + // syntax for principals. + // + // Use the KerberosName(id) constructor to define validity: + // if KerberosName(id) throws IllegalArgumentException, then id is invalid. + // otherwise, it is valid. + // + try { + new KerberosName(id); + return true; + } catch (IllegalArgumentException e) { + return false; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/SaslServerCallbackHandler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/SaslServerCallbackHandler.java new file mode 100644 index 0000000..b6415fd --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/SaslServerCallbackHandler.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.sasl.AuthorizeCallback; +import javax.security.sasl.RealmCallback; +import org.apache.zookeeper.server.ZooKeeperSaslServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SaslServerCallbackHandler implements CallbackHandler { + + private static final String USER_PREFIX = "user_"; + private static final Logger LOG = LoggerFactory.getLogger(SaslServerCallbackHandler.class); + private static final String SYSPROP_SUPER_PASSWORD = "zookeeper.SASLAuthenticationProvider.superPassword"; + private static final String SYSPROP_REMOVE_HOST = "zookeeper.kerberos.removeHostFromPrincipal"; + private static final String SYSPROP_REMOVE_REALM = "zookeeper.kerberos.removeRealmFromPrincipal"; + + private String userName; + private final Map credentials = new HashMap(); + + public SaslServerCallbackHandler(Configuration configuration) throws IOException { + String serverSection = System.getProperty( + ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY, + ZooKeeperSaslServer.DEFAULT_LOGIN_CONTEXT_NAME); + + AppConfigurationEntry[] configurationEntries = configuration.getAppConfigurationEntry(serverSection); + + if (configurationEntries == null) { + String errorMessage = "Could not find a '" + serverSection + "' entry in this configuration: Server cannot start."; + LOG.error(errorMessage); + throw new IOException(errorMessage); + } + credentials.clear(); + for (AppConfigurationEntry entry : configurationEntries) { + Map options = entry.getOptions(); + // Populate DIGEST-MD5 user -> password map with JAAS configuration entries from the "Server" section. + // Usernames are distinguished from other options by prefixing the username with a "user_" prefix. + for (Map.Entry pair : options.entrySet()) { + String key = pair.getKey(); + if (key.startsWith(USER_PREFIX)) { + String userName = key.substring(USER_PREFIX.length()); + credentials.put(userName, (String) pair.getValue()); + } + } + } + } + + public void handle(Callback[] callbacks) throws UnsupportedCallbackException { + for (Callback callback : callbacks) { + if (callback instanceof NameCallback) { + handleNameCallback((NameCallback) callback); + } else if (callback instanceof PasswordCallback) { + handlePasswordCallback((PasswordCallback) callback); + } else if (callback instanceof RealmCallback) { + handleRealmCallback((RealmCallback) callback); + } else if (callback instanceof AuthorizeCallback) { + handleAuthorizeCallback((AuthorizeCallback) callback); + } + } + } + + private void handleNameCallback(NameCallback nc) { + // check to see if this user is in the user password database. + if (credentials.get(nc.getDefaultName()) == null) { + LOG.warn("User '{}' not found in list of DIGEST-MD5 authenticateable users.", nc.getDefaultName()); + return; + } + nc.setName(nc.getDefaultName()); + userName = nc.getDefaultName(); + } + + private void handlePasswordCallback(PasswordCallback pc) { + if ("super".equals(this.userName) && System.getProperty(SYSPROP_SUPER_PASSWORD) != null) { + // superuser: use Java system property for password, if available. + pc.setPassword(System.getProperty(SYSPROP_SUPER_PASSWORD).toCharArray()); + } else if (credentials.containsKey(userName)) { + pc.setPassword(credentials.get(userName).toCharArray()); + } else { + LOG.warn("No password found for user: {}", userName); + } + } + + private void handleRealmCallback(RealmCallback rc) { + LOG.debug("client supplied realm: {}", rc.getDefaultText()); + rc.setText(rc.getDefaultText()); + } + + private void handleAuthorizeCallback(AuthorizeCallback ac) { + String authenticationID = ac.getAuthenticationID(); + String authorizationID = ac.getAuthorizationID(); + + LOG.info("Successfully authenticated client: authenticationID={}; authorizationID={}.", + authenticationID, authorizationID); + ac.setAuthorized(true); + + // canonicalize authorization id according to system properties: + // zookeeper.kerberos.removeRealmFromPrincipal(={true,false}) + // zookeeper.kerberos.removeHostFromPrincipal(={true,false}) + KerberosName kerberosName = new KerberosName(authenticationID); + try { + StringBuilder userNameBuilder = new StringBuilder(kerberosName.getShortName()); + if (shouldAppendHost(kerberosName)) { + userNameBuilder.append("/").append(kerberosName.getHostName()); + } + if (shouldAppendRealm(kerberosName)) { + userNameBuilder.append("@").append(kerberosName.getRealm()); + } + LOG.info("Setting authorizedID: {}", userNameBuilder); + ac.setAuthorizedID(userNameBuilder.toString()); + } catch (IOException e) { + LOG.error("Failed to set name based on Kerberos authentication rules.", e); + } + } + + private boolean shouldAppendRealm(KerberosName kerberosName) { + return !isSystemPropertyTrue(SYSPROP_REMOVE_REALM) && kerberosName.getRealm() != null; + } + + private boolean shouldAppendHost(KerberosName kerberosName) { + return !isSystemPropertyTrue(SYSPROP_REMOVE_HOST) && kerberosName.getHostName() != null; + } + + private boolean isSystemPropertyTrue(String propertyName) { + return "true".equals(System.getProperty(propertyName)); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ServerAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ServerAuthenticationProvider.java new file mode 100644 index 0000000..0842296 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/ServerAuthenticationProvider.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.util.List; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ZooKeeperServer; + +/** + * A variation on {@link AuthenticationProvider} that provides additional + * parameters for more detailed authentication + */ +public abstract class ServerAuthenticationProvider implements AuthenticationProvider { + + public static class ServerObjs { + + private final ZooKeeperServer zks; + private final ServerCnxn cnxn; + + /** + * @param zks + * the ZooKeeper server instance + * @param cnxn + * the cnxn that received the authentication information. + */ + public ServerObjs(ZooKeeperServer zks, ServerCnxn cnxn) { + this.zks = zks; + this.cnxn = cnxn; + } + + public ZooKeeperServer getZks() { + return zks; + } + + public ServerCnxn getCnxn() { + return cnxn; + } + + } + + public static class MatchValues { + + private final String path; + private final String id; + private final String aclExpr; + private final int perm; + private final List setAcls; + + /** + * @param path + * the path of the operation being authenticated + * @param id + * the id to check. + * @param aclExpr + * the expression to match ids against. + * @param perm + * the permission value being authenticated + * @param setAcls + * for set ACL operations, the list of ACLs being set. Otherwise null. + */ + public MatchValues(String path, String id, String aclExpr, int perm, List setAcls) { + this.path = path; + this.id = id; + this.aclExpr = aclExpr; + this.perm = perm; + this.setAcls = setAcls; + } + + public String getPath() { + return path; + } + + public String getId() { + return id; + } + + public String getAclExpr() { + return aclExpr; + } + + public int getPerm() { + return perm; + } + + public List getSetAcls() { + return setAcls; + } + + } + + /** + * This method is called when a client passes authentication data for this + * scheme. The authData is directly from the authentication packet. The + * implementor may attach new ids to the authInfo field of cnxn or may use + * cnxn to send packets back to the client. + * + * @param serverObjs + * cnxn/server/etc that received the authentication information. + * @param authData + * the authentication data received. + * @return indication of success or failure + */ + public abstract KeeperException.Code handleAuthentication(ServerObjs serverObjs, byte[] authData); + + /** + * This method is called to see if the given id matches the given id + * expression in the ACL. This allows schemes to use application specific + * wild cards. + * + * @param serverObjs + * cnxn/server/etc that received the authentication information. + * @param matchValues + * values to be matched + */ + public abstract boolean matches(ServerObjs serverObjs, MatchValues matchValues); + + @Override + public final KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + throw new UnsupportedOperationException(); + } + + @Override + public final boolean matches(String id, String aclExpr) { + throw new UnsupportedOperationException(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/WrappedAuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/WrappedAuthenticationProvider.java new file mode 100644 index 0000000..65dc437 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/WrappedAuthenticationProvider.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.ServerCnxn; + +/** + * Provides backwards compatibility between older {@link AuthenticationProvider} + * implementations and the new {@link ServerAuthenticationProvider} interface. + */ +class WrappedAuthenticationProvider extends ServerAuthenticationProvider { + + private final AuthenticationProvider implementation; + + static ServerAuthenticationProvider wrap(AuthenticationProvider provider) { + if (provider == null) { + return null; + } + return (provider instanceof ServerAuthenticationProvider) + ? (ServerAuthenticationProvider) provider + : new WrappedAuthenticationProvider(provider); + } + + private WrappedAuthenticationProvider(AuthenticationProvider implementation) { + this.implementation = implementation; + } + + /** + * {@inheritDoc} + * + * forwards to older method {@link #handleAuthentication(ServerCnxn, byte[])} + */ + @Override + public KeeperException.Code handleAuthentication(ServerObjs serverObjs, byte[] authData) { + return implementation.handleAuthentication(serverObjs.getCnxn(), authData); + } + + /** + * {@inheritDoc} + * + * forwards to older method {@link #matches(String, String)} + */ + @Override + public boolean matches(ServerObjs serverObjs, MatchValues matchValues) { + return implementation.matches(matchValues.getId(), matchValues.getAclExpr()); + } + + @Override + public String getScheme() { + return implementation.getScheme(); + } + + @Override + public boolean isAuthenticated() { + return implementation.isAuthenticated(); + } + + @Override + public boolean isValid(String id) { + return implementation.isValid(id); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/X509AuthenticationProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/X509AuthenticationProvider.java new file mode 100644 index 0000000..1f42b24 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/auth/X509AuthenticationProvider.java @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.auth; + +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import javax.net.ssl.X509KeyManager; +import javax.net.ssl.X509TrustManager; +import javax.security.auth.x500.X500Principal; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.common.X509Exception.KeyManagerException; +import org.apache.zookeeper.common.X509Exception.TrustManagerException; +import org.apache.zookeeper.common.X509Util; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.ServerCnxn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * An AuthenticationProvider backed by an X509TrustManager and an X509KeyManager + * to perform remote host certificate authentication. The default algorithm is + * SunX509 and a JKS KeyStore. To specify the locations of the key store and + * trust store, set the following system properties: + *
      zookeeper.ssl.keyStore.location + *
      zookeeper.ssl.trustStore.location + *
      To specify store passwords, set the following system properties: + *
      zookeeper.ssl.keyStore.password + *
      zookeeper.ssl.trustStore.password + *
      Alternatively, this can be plugged with any X509TrustManager and + * X509KeyManager implementation. + */ +public class X509AuthenticationProvider implements AuthenticationProvider { + + static final String ZOOKEEPER_X509AUTHENTICATIONPROVIDER_SUPERUSER = "zookeeper.X509AuthenticationProvider.superUser"; + private static final Logger LOG = LoggerFactory.getLogger(X509AuthenticationProvider.class); + private final X509TrustManager trustManager; + private final X509KeyManager keyManager; + + /** + * Initialize the X509AuthenticationProvider with a JKS KeyStore and JKS + * TrustStore according to the following system properties: + *
      zookeeper.ssl.keyStore.location + *
      zookeeper.ssl.trustStore.location + *
      zookeeper.ssl.keyStore.password + *
      zookeeper.ssl.trustStore.password + */ + public X509AuthenticationProvider() throws X509Exception { + ZKConfig config = new ZKConfig(); + try (X509Util x509Util = new ClientX509Util()) { + String keyStoreLocation = config.getProperty(x509Util.getSslKeystoreLocationProperty(), ""); + String keyStorePassword = config.getProperty(x509Util.getSslKeystorePasswdProperty(), ""); + String keyStoreTypeProp = config.getProperty(x509Util.getSslKeystoreTypeProperty()); + + boolean crlEnabled = Boolean.parseBoolean(config.getProperty(x509Util.getSslCrlEnabledProperty())); + boolean ocspEnabled = Boolean.parseBoolean(config.getProperty(x509Util.getSslOcspEnabledProperty())); + boolean hostnameVerificationEnabled = Boolean.parseBoolean(config.getProperty(x509Util.getSslHostnameVerificationEnabledProperty())); + + X509KeyManager km = null; + X509TrustManager tm = null; + if (keyStoreLocation.isEmpty()) { + LOG.warn("keystore not specified for client connection"); + } else { + try { + km = X509Util.createKeyManager(keyStoreLocation, keyStorePassword, keyStoreTypeProp); + } catch (KeyManagerException e) { + LOG.error("Failed to create key manager", e); + } + } + + String trustStoreLocation = config.getProperty(x509Util.getSslTruststoreLocationProperty(), ""); + String trustStorePassword = config.getProperty(x509Util.getSslTruststorePasswdProperty(), ""); + String trustStoreTypeProp = config.getProperty(x509Util.getSslTruststoreTypeProperty()); + + if (trustStoreLocation.isEmpty()) { + LOG.warn("Truststore not specified for client connection"); + } else { + try { + tm = X509Util.createTrustManager( + trustStoreLocation, + trustStorePassword, + trustStoreTypeProp, + crlEnabled, + ocspEnabled, + hostnameVerificationEnabled, + false); + } catch (TrustManagerException e) { + LOG.error("Failed to create trust manager", e); + } + } + this.keyManager = km; + this.trustManager = tm; + } + } + + /** + * Initialize the X509AuthenticationProvider with the provided + * X509TrustManager and X509KeyManager. + * + * @param trustManager X509TrustManager implementation to use for remote + * host authentication. + * @param keyManager X509KeyManager implementation to use for certificate + * management. + */ + public X509AuthenticationProvider(X509TrustManager trustManager, X509KeyManager keyManager) { + this.trustManager = trustManager; + this.keyManager = keyManager; + } + + @Override + public String getScheme() { + return "x509"; + } + + @Override + public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + X509Certificate[] certChain = (X509Certificate[]) cnxn.getClientCertificateChain(); + + if (certChain == null || certChain.length == 0) { + return KeeperException.Code.AUTHFAILED; + } + + if (trustManager == null) { + LOG.error("No trust manager available to authenticate session 0x{}", Long.toHexString(cnxn.getSessionId())); + return KeeperException.Code.AUTHFAILED; + } + + X509Certificate clientCert = certChain[0]; + + try { + // Authenticate client certificate + trustManager.checkClientTrusted(certChain, clientCert.getPublicKey().getAlgorithm()); + } catch (CertificateException ce) { + LOG.error("Failed to trust certificate for session 0x{}", Long.toHexString(cnxn.getSessionId()), ce); + return KeeperException.Code.AUTHFAILED; + } + + String clientId = getClientId(clientCert); + + if (clientId.equals(System.getProperty(ZOOKEEPER_X509AUTHENTICATIONPROVIDER_SUPERUSER))) { + cnxn.addAuthInfo(new Id("super", clientId)); + LOG.info("Authenticated Id '{}' as super user", clientId); + } + + Id authInfo = new Id(getScheme(), clientId); + cnxn.addAuthInfo(authInfo); + + LOG.info("Authenticated Id '{}' for Scheme '{}'", authInfo.getId(), authInfo.getScheme()); + return KeeperException.Code.OK; + } + + /** + * Determine the string to be used as the remote host session Id for + * authorization purposes. Associate this client identifier with a + * ServerCnxn that has been authenticated over SSL, and any ACLs that refer + * to the authenticated client. + * + * @param clientCert Authenticated X509Certificate associated with the + * remote host. + * @return Identifier string to be associated with the client. + */ + protected String getClientId(X509Certificate clientCert) { + return clientCert.getSubjectX500Principal().getName(); + } + + @Override + public boolean matches(String id, String aclExpr) { + if (System.getProperty(ZOOKEEPER_X509AUTHENTICATIONPROVIDER_SUPERUSER) != null) { + return id.equals(System.getProperty(ZOOKEEPER_X509AUTHENTICATIONPROVIDER_SUPERUSER)) + || id.equals(aclExpr); + } + + return id.equals(aclExpr); + } + + @Override + public boolean isAuthenticated() { + return true; + } + + @Override + public boolean isValid(String id) { + try { + new X500Principal(id); + return true; + } catch (IllegalArgumentException e) { + return false; + } + } + + /** + * Get the X509TrustManager implementation used for remote host + * authentication. + * + * @return The X509TrustManager. + * @throws TrustManagerException When there is no trust manager available. + */ + public X509TrustManager getTrustManager() throws TrustManagerException { + if (trustManager == null) { + throw new TrustManagerException("No trust manager available"); + } + return trustManager; + } + + /** + * Get the X509KeyManager implementation used for certificate management. + * + * @return The X509KeyManager. + * @throws KeyManagerException When there is no key manager available. + */ + public X509KeyManager getKeyManager() throws KeyManagerException { + if (keyManager == null) { + throw new KeyManagerException("No key manager available"); + } + return keyManager; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/AbstractFourLetterCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/AbstractFourLetterCommand.java new file mode 100644 index 0000000..42d2e5d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/AbstractFourLetterCommand.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.IOException; +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Set of threads for command ports. All the 4 letter commands are run via a + * thread. Each class maps to a corresponding 4 letter command. CommandThread is + * the abstract class from which all the others inherit. + */ +public abstract class AbstractFourLetterCommand { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractFourLetterCommand.class); + + public static final String ZK_NOT_SERVING = "This ZooKeeper instance is not currently serving requests"; + + protected PrintWriter pw; + protected ServerCnxn serverCnxn; + protected ZooKeeperServer zkServer; + protected ServerCnxnFactory factory; + + public AbstractFourLetterCommand(PrintWriter pw, ServerCnxn serverCnxn) { + this.pw = pw; + this.serverCnxn = serverCnxn; + } + + public void start() { + run(); + } + + public void run() { + try { + commandRun(); + } catch (IOException ie) { + LOG.error("Error in running command ", ie); + } finally { + serverCnxn.cleanupWriterSocket(pw); + } + } + + public void setZkServer(ZooKeeperServer zkServer) { + this.zkServer = zkServer; + } + + /** + * @return true if the server is running, false otherwise. + */ + boolean isZKServerRunning() { + return zkServer != null && zkServer.isRunning(); + } + + public void setFactory(ServerCnxnFactory factory) { + this.factory = factory; + } + + public abstract void commandRun() throws IOException; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/CnxnStatResetCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/CnxnStatResetCommand.java new file mode 100644 index 0000000..53b20ef --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/CnxnStatResetCommand.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +public class CnxnStatResetCommand extends AbstractFourLetterCommand { + + public CnxnStatResetCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + factory.resetAllConnectionStats(); + pw.println("Connection stats reset."); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/CommandExecutor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/CommandExecutor.java new file mode 100644 index 0000000..9ea6a12 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/CommandExecutor.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; + +public class CommandExecutor { + + /** + * This class decides which command to be executed and then executes + */ + public boolean execute( + ServerCnxn serverCnxn, + PrintWriter pwriter, + final int commandCode, + ZooKeeperServer zkServer, + ServerCnxnFactory factory) { + AbstractFourLetterCommand command = getCommand(serverCnxn, pwriter, commandCode); + + if (command == null) { + return false; + } + + command.setZkServer(zkServer); + command.setFactory(factory); + command.start(); + return true; + } + + private AbstractFourLetterCommand getCommand( + ServerCnxn serverCnxn, + PrintWriter pwriter, + final int commandCode) { + AbstractFourLetterCommand command = null; + if (commandCode == FourLetterCommands.ruokCmd) { + command = new RuokCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.getTraceMaskCmd) { + command = new TraceMaskCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.enviCmd) { + command = new EnvCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.confCmd) { + command = new ConfCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.srstCmd) { + command = new StatResetCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.crstCmd) { + command = new CnxnStatResetCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.dirsCmd) { + command = new DirsCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.dumpCmd) { + command = new DumpCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.statCmd || commandCode == FourLetterCommands.srvrCmd) { + command = new StatCommand(pwriter, serverCnxn, commandCode); + } else if (commandCode == FourLetterCommands.consCmd) { + command = new ConsCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.wchpCmd + || commandCode == FourLetterCommands.wchcCmd + || commandCode == FourLetterCommands.wchsCmd) { + command = new WatchCommand(pwriter, serverCnxn, commandCode); + } else if (commandCode == FourLetterCommands.mntrCmd) { + command = new MonitorCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.isroCmd) { + command = new IsroCommand(pwriter, serverCnxn); + } else if (commandCode == FourLetterCommands.hashCmd) { + command = new DigestCommand(pwriter, serverCnxn); + } + + return command; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/ConfCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/ConfCommand.java new file mode 100644 index 0000000..bd9446d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/ConfCommand.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +public class ConfCommand extends AbstractFourLetterCommand { + + ConfCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + zkServer.dumpConf(pw); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/ConsCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/ConsCommand.java new file mode 100644 index 0000000..06d5fee --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/ConsCommand.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +public class ConsCommand extends AbstractFourLetterCommand { + + public ConsCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + for (ServerCnxn c : factory.getConnections()) { + c.dumpConnectionInfo(pw, false); + pw.println(); + } + pw.println(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DigestCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DigestCommand.java new file mode 100644 index 0000000..af11eaf --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DigestCommand.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import java.util.List; +import org.apache.zookeeper.server.DataTree.ZxidDigest; +import org.apache.zookeeper.server.ServerCnxn; + +/** + * Command used to dump the latest digest histories. + */ +public class DigestCommand extends AbstractFourLetterCommand { + + public DigestCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.print(ZK_NOT_SERVING); + } else { + List digestLog = zkServer.getZKDatabase().getDataTree().getDigestLog(); + for (ZxidDigest zd : digestLog) { + pw.println(Long.toHexString(zd.getZxid()) + ": " + zd.getDigest()); + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DirsCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DirsCommand.java new file mode 100644 index 0000000..a80f921 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DirsCommand.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.IOException; +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +public class DirsCommand extends AbstractFourLetterCommand { + + public DirsCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() throws IOException { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + return; + } + pw.println("datadir_size: " + zkServer.getDataDirSize()); + pw.println("logdir_size: " + zkServer.getLogDirSize()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DumpCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DumpCommand.java new file mode 100644 index 0000000..0018342 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/DumpCommand.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.NIOServerCnxnFactory; +import org.apache.zookeeper.server.ServerCnxn; + +public class DumpCommand extends AbstractFourLetterCommand { + + public DumpCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + pw.println("SessionTracker dump:"); + zkServer.getSessionTracker().dumpSessions(pw); + pw.println("ephemeral nodes dump:"); + zkServer.dumpEphemerals(pw); + pw.println("Connections dump:"); + //dumpConnections connection is implemented only in NIOServerCnxnFactory + if (factory instanceof NIOServerCnxnFactory) { + ((NIOServerCnxnFactory) factory).dumpConnections(pw); + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/EnvCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/EnvCommand.java new file mode 100644 index 0000000..ded0f94 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/EnvCommand.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import java.util.List; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.server.ServerCnxn; + +public class EnvCommand extends AbstractFourLetterCommand { + + EnvCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + List env = Environment.list(); + + pw.println("Environment:"); + for (Environment.Entry e : env) { + pw.print(e.getKey()); + pw.print("="); + pw.println(e.getValue()); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/FourLetterCommands.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/FourLetterCommands.java new file mode 100644 index 0000000..40b1698 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/FourLetterCommands.java @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class contains constants for all the four letter commands + */ +public class FourLetterCommands { + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int confCmd = ByteBuffer.wrap("conf".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int consCmd = ByteBuffer.wrap("cons".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int crstCmd = ByteBuffer.wrap("crst".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int dirsCmd = ByteBuffer.wrap("dirs".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int dumpCmd = ByteBuffer.wrap("dump".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int enviCmd = ByteBuffer.wrap("envi".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int getTraceMaskCmd = ByteBuffer.wrap("gtmk".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int ruokCmd = ByteBuffer.wrap("ruok".getBytes()).getInt(); + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int setTraceMaskCmd = ByteBuffer.wrap("stmk".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int srvrCmd = ByteBuffer.wrap("srvr".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int srstCmd = ByteBuffer.wrap("srst".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int statCmd = ByteBuffer.wrap("stat".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int wchcCmd = ByteBuffer.wrap("wchc".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int wchpCmd = ByteBuffer.wrap("wchp".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int wchsCmd = ByteBuffer.wrap("wchs".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int mntrCmd = ByteBuffer.wrap("mntr".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + public static final int isroCmd = ByteBuffer.wrap("isro".getBytes()).getInt(); + + /* + * See + * Zk Admin. this link is for all the commands. + */ + protected static final int hashCmd = ByteBuffer.wrap("hash".getBytes()).getInt(); + + /* + * The control sequence sent by the telnet program when it closes a + * connection. Include simply to keep the logs cleaner (the server would + * close the connection anyway because it would parse this as a negative + * length). + */ + public static final int telnetCloseCmd = 0xfff4fffd; + + private static final String ZOOKEEPER_4LW_COMMANDS_WHITELIST = "zookeeper.4lw.commands.whitelist"; + + private static final Logger LOG = LoggerFactory.getLogger(FourLetterCommands.class); + + private static final Map cmd2String = new HashMap(); + + private static final Set whiteListedCommands = new HashSet(); + + private static boolean whiteListInitialized = false; + + // @VisibleForTesting + public static synchronized void resetWhiteList() { + whiteListInitialized = false; + whiteListedCommands.clear(); + } + + /** + * Return the string representation of the specified command code. + */ + public static String getCommandString(int command) { + return cmd2String.get(command); + } + + /** + * Check if the specified command code is from a known command. + * + * @param command The integer code of command. + * @return true if the specified command is known, false otherwise. + */ + public static boolean isKnown(int command) { + return cmd2String.containsKey(command); + } + + /** + * Check if the specified command is enabled. + * + * In ZOOKEEPER-2693 we introduce a configuration option to only + * allow a specific set of white listed commands to execute. + * A command will only be executed if it is also configured + * in the white list. + * + * @param command The command string. + * @return true if the specified command is enabled + */ + public static synchronized boolean isEnabled(String command) { + if (whiteListInitialized) { + return whiteListedCommands.contains(command); + } + + String commands = System.getProperty(ZOOKEEPER_4LW_COMMANDS_WHITELIST); + if (commands != null) { + String[] list = commands.split(","); + for (String cmd : list) { + if (cmd.trim().equals("*")) { + for (Map.Entry entry : cmd2String.entrySet()) { + whiteListedCommands.add(entry.getValue()); + } + break; + } + if (!cmd.trim().isEmpty()) { + whiteListedCommands.add(cmd.trim()); + } + } + } + + // It is sad that isro and srvr are used by ZooKeeper itself. Need fix this + // before deprecating 4lw. + if (System.getProperty("readonlymode.enabled", "false").equals("true")) { + whiteListedCommands.add("isro"); + } + // zkServer.sh depends on "srvr". + whiteListedCommands.add("srvr"); + whiteListInitialized = true; + LOG.info("The list of known four letter word commands is : {}", Arrays.asList(cmd2String)); + LOG.info("The list of enabled four letter word commands is : {}", Arrays.asList(whiteListedCommands)); + return whiteListedCommands.contains(command); + } + + // specify all of the commands that are available + static { + cmd2String.put(confCmd, "conf"); + cmd2String.put(consCmd, "cons"); + cmd2String.put(crstCmd, "crst"); + cmd2String.put(dirsCmd, "dirs"); + cmd2String.put(dumpCmd, "dump"); + cmd2String.put(enviCmd, "envi"); + cmd2String.put(getTraceMaskCmd, "gtmk"); + cmd2String.put(ruokCmd, "ruok"); + cmd2String.put(setTraceMaskCmd, "stmk"); + cmd2String.put(srstCmd, "srst"); + cmd2String.put(srvrCmd, "srvr"); + cmd2String.put(statCmd, "stat"); + cmd2String.put(wchcCmd, "wchc"); + cmd2String.put(wchpCmd, "wchp"); + cmd2String.put(wchsCmd, "wchs"); + cmd2String.put(mntrCmd, "mntr"); + cmd2String.put(isroCmd, "isro"); + cmd2String.put(telnetCloseCmd, "telnet close"); + cmd2String.put(hashCmd, "hash"); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/IsroCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/IsroCommand.java new file mode 100644 index 0000000..cfaca69 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/IsroCommand.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; + +public class IsroCommand extends AbstractFourLetterCommand { + + public IsroCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.print("null"); + } else if (zkServer instanceof ReadOnlyZooKeeperServer) { + pw.print("ro"); + } else { + pw.print("rw"); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/MonitorCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/MonitorCommand.java new file mode 100644 index 0000000..0c81f33 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/MonitorCommand.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerMetrics; + +public class MonitorCommand extends AbstractFourLetterCommand { + + MonitorCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + return; + } + + // non metrics + zkServer.dumpMonitorValues(this::print); + + ServerMetrics.getMetrics().getMetricsProvider().dump(this::print); + } + + private void print(String key, Object value) { + if (value == null) { + output(key, null); + } else if (value instanceof Long || value instanceof Integer) { + // format as integers + output(key, value + ""); + } else if (value instanceof Number) { + // format as floating point + output(key, ((Number) value).doubleValue() + ""); + } else { + output(key, value.toString()); + } + } + + private void output(String key, String value) { + pw.print("zk_"); + pw.print(key); + pw.print("\t"); + pw.println(value); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/NopCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/NopCommand.java new file mode 100644 index 0000000..3e4a7f4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/NopCommand.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +/** + * A command that does not do anything except reply to client with predefined message. + * It is used to inform clients who execute none white listed four letter word commands. + */ +public class NopCommand extends AbstractFourLetterCommand { + + private String msg; + + public NopCommand(PrintWriter pw, ServerCnxn serverCnxn, String msg) { + super(pw, serverCnxn); + this.msg = msg; + } + + @Override + public void commandRun() { + pw.println(msg); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/RuokCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/RuokCommand.java new file mode 100644 index 0000000..02bcc80 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/RuokCommand.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +public class RuokCommand extends AbstractFourLetterCommand { + + public RuokCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + pw.print("imok"); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/SetTraceMaskCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/SetTraceMaskCommand.java new file mode 100644 index 0000000..8c2c766 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/SetTraceMaskCommand.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; + +public class SetTraceMaskCommand extends AbstractFourLetterCommand { + + long trace = 0; + public SetTraceMaskCommand(PrintWriter pw, ServerCnxn serverCnxn, long trace) { + super(pw, serverCnxn); + this.trace = trace; + } + + @Override + public void commandRun() { + pw.print(trace); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/StatCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/StatCommand.java new file mode 100644 index 0000000..2631aac --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/StatCommand.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.Version; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.quorum.BufferStats; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class StatCommand extends AbstractFourLetterCommand { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractFourLetterCommand.class); + private int len; + public StatCommand(PrintWriter pw, ServerCnxn serverCnxn, int len) { + super(pw, serverCnxn); + this.len = len; + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + pw.print("Zookeeper version: "); + pw.println(Version.getFullVersion()); + if (zkServer instanceof ReadOnlyZooKeeperServer) { + pw.println("READ-ONLY mode; serving only read-only clients"); + } + if (len == FourLetterCommands.statCmd) { + LOG.info("Stat command output"); + pw.println("Clients:"); + for (ServerCnxn c : factory.getConnections()) { + c.dumpConnectionInfo(pw, true); + pw.println(); + } + pw.println(); + } + ServerStats serverStats = zkServer.serverStats(); + pw.print(serverStats.toString()); + pw.print("Node count: "); + pw.println(zkServer.getZKDatabase().getNodeCount()); + if (serverStats.getServerState().equals("leader")) { + Leader leader = ((LeaderZooKeeperServer) zkServer).getLeader(); + BufferStats proposalStats = leader.getProposalStats(); + pw.printf("Proposal sizes last/min/max: %s%n", proposalStats.toString()); + } + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/StatResetCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/StatResetCommand.java new file mode 100644 index 0000000..185c905 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/StatResetCommand.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; + +public class StatResetCommand extends AbstractFourLetterCommand { + + public StatResetCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + ServerStats serverStats = zkServer.serverStats(); + serverStats.reset(); + if (serverStats.getServerState().equals("leader")) { + ((LeaderZooKeeperServer) zkServer).getLeader().getProposalStats().reset(); + } + pw.println("Server stats reset."); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/TraceMaskCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/TraceMaskCommand.java new file mode 100644 index 0000000..589bd43 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/TraceMaskCommand.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ZooTrace; + +public class TraceMaskCommand extends AbstractFourLetterCommand { + + TraceMaskCommand(PrintWriter pw, ServerCnxn serverCnxn) { + super(pw, serverCnxn); + } + + @Override + public void commandRun() { + long traceMask = ZooTrace.getTextTraceLevel(); + pw.print(traceMask); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/WatchCommand.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/WatchCommand.java new file mode 100644 index 0000000..5b59d45 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/command/WatchCommand.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.command; + +import java.io.PrintWriter; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.ServerCnxn; + +public class WatchCommand extends AbstractFourLetterCommand { + + int len = 0; + public WatchCommand(PrintWriter pw, ServerCnxn serverCnxn, int len) { + super(pw, serverCnxn); + this.len = len; + } + + @Override + public void commandRun() { + if (!isZKServerRunning()) { + pw.println(ZK_NOT_SERVING); + } else { + DataTree dt = zkServer.getZKDatabase().getDataTree(); + if (len == FourLetterCommands.wchsCmd) { + dt.dumpWatchesSummary(pw); + } else if (len == FourLetterCommands.wchpCmd) { + dt.dumpWatches(pw, true); + } else { + dt.dumpWatches(pw, false); + } + pw.println(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxCounter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxCounter.java new file mode 100644 index 0000000..0f06e4a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxCounter.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.metrics.Summary; + +/** + * Generic long counter that keep track of min/max/avg. The counter is + * thread-safe + */ +public class AvgMinMaxCounter extends Metric implements Summary { + + private final String name; + private final AtomicLong total = new AtomicLong(); + private final AtomicLong min = new AtomicLong(Long.MAX_VALUE); + private final AtomicLong max = new AtomicLong(Long.MIN_VALUE); + private final AtomicLong count = new AtomicLong(); + + public AvgMinMaxCounter(String name) { + this.name = name; + } + + public void addDataPoint(long value) { + total.addAndGet(value); + count.incrementAndGet(); + setMin(value); + setMax(value); + } + + private void setMax(long value) { + long current; + while (value > (current = max.get()) && !max.compareAndSet(current, value)) { + // no op + } + } + + private void setMin(long value) { + long current; + while (value < (current = min.get()) && !min.compareAndSet(current, value)) { + // no op + } + } + + public double getAvg() { + // There is possible race-condition but we don't need the stats to be + // extremely accurate. + long currentCount = count.get(); + long currentTotal = total.get(); + if (currentCount > 0) { + double avgLatency = currentTotal / (double) currentCount; + BigDecimal bg = new BigDecimal(avgLatency); + return bg.setScale(4, RoundingMode.HALF_UP).doubleValue(); + } + return 0; + } + + public long getCount() { + return count.get(); + } + + public long getMax() { + long current = max.get(); + return (current == Long.MIN_VALUE) ? 0 : current; + } + + public long getMin() { + long current = min.get(); + return (current == Long.MAX_VALUE) ? 0 : current; + } + + public long getTotal() { + return total.get(); + } + + public void resetMax() { + max.set(getMin()); + } + + public void reset() { + count.set(0); + total.set(0); + min.set(Long.MAX_VALUE); + max.set(Long.MIN_VALUE); + } + + public void add(long value) { + addDataPoint(value); + } + + public Map values() { + Map m = new LinkedHashMap(); + m.put("avg_" + name, this.getAvg()); + m.put("min_" + name, this.getMin()); + m.put("max_" + name, this.getMax()); + m.put("cnt_" + name, this.getCount()); + m.put("sum_" + name, this.getTotal()); + return m; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxCounterSet.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxCounterSet.java new file mode 100644 index 0000000..8d96111 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxCounterSet.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.metrics.SummarySet; + +/** + * Generic set of long counters that keep track of min/max/avg + * for different keys. + * The counter is thread-safe + */ +public class AvgMinMaxCounterSet extends Metric implements SummarySet { + + private final String name; + + private ConcurrentHashMap counters = new ConcurrentHashMap<>(); + + public AvgMinMaxCounterSet(String name) { + this.name = name; + } + + private AvgMinMaxCounter getCounterForKey(String key) { + AvgMinMaxCounter counter = counters.get(key); + if (counter == null) { + counters.putIfAbsent(key, new AvgMinMaxCounter(key + "_" + name)); + counter = counters.get(key); + } + + return counter; + } + + public void addDataPoint(String key, long value) { + getCounterForKey(key).addDataPoint(value); + } + + public void resetMax() { + for (Map.Entry entry : counters.entrySet()) { + entry.getValue().resetMax(); + } + } + + public void reset() { + for (Map.Entry entry : counters.entrySet()) { + entry.getValue().reset(); + } + } + + @Override + public void add(String key, long value) { + addDataPoint(key, value); + } + + @Override + public Map values() { + Map m = new LinkedHashMap<>(); + for (Map.Entry entry : counters.entrySet()) { + m.putAll(entry.getValue().values()); + } + return m; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounter.java new file mode 100644 index 0000000..3162043 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounter.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import com.codahale.metrics.Histogram; +import com.codahale.metrics.Reservoir; +import com.codahale.metrics.Snapshot; +import com.codahale.metrics.UniformSnapshot; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicLongArray; +import org.apache.zookeeper.metrics.Summary; + +/** + * Generic long counter that keep track of min/max/avg/percentiles. + * The counter is thread-safe + */ +public class AvgMinMaxPercentileCounter extends Metric implements Summary { + + private final String name; + private final AvgMinMaxCounter counter; + private final ResettableUniformReservoir reservoir; + private final Histogram histogram; + + static class ResettableUniformReservoir implements Reservoir { + + private static final int DEFAULT_SIZE = 4096; + private static final int BITS_PER_LONG = 63; + + private final AtomicLong count = new AtomicLong(); + private volatile AtomicLongArray values = new AtomicLongArray(DEFAULT_SIZE); + + @Override + public int size() { + final long c = count.get(); + if (c > values.length()) { + return values.length(); + } + return (int) c; + } + + @Override + public void update(long value) { + final long c = count.incrementAndGet(); + if (c <= values.length()) { + values.set((int) c - 1, value); + } else { + final long r = nextLong(c); + if (r < values.length()) { + values.set((int) r, value); + } + } + } + + private static long nextLong(long n) { + long bits, val; + do { + bits = ThreadLocalRandom.current().nextLong() & (~(1L << BITS_PER_LONG)); + val = bits % n; + } while (bits - val + (n - 1) < 0L); + return val; + } + + @Override + public Snapshot getSnapshot() { + final int s = size(); + final List copy = new ArrayList(s); + for (int i = 0; i < s; i++) { + copy.add(values.get(i)); + } + return new UniformSnapshot(copy); + } + + public void reset() { + count.set(0); + values = new AtomicLongArray(DEFAULT_SIZE); + } + + } + + public AvgMinMaxPercentileCounter(String name) { + + this.name = name; + this.counter = new AvgMinMaxCounter(this.name); + reservoir = new ResettableUniformReservoir(); + histogram = new Histogram(reservoir); + } + + public void addDataPoint(long value) { + counter.add(value); + histogram.update(value); + } + + public void resetMax() { + // To match existing behavior in upstream + counter.resetMax(); + } + + public void reset() { + counter.reset(); + reservoir.reset(); + } + + public void add(long value) { + addDataPoint(value); + } + + public Map values() { + Map m = new LinkedHashMap<>(); + m.putAll(counter.values()); + m.put("p50_" + name, Math.round(this.histogram.getSnapshot().getMedian())); + m.put("p95_" + name, Math.round(this.histogram.getSnapshot().get95thPercentile())); + m.put("p99_" + name, Math.round(this.histogram.getSnapshot().get99thPercentile())); + m.put("p999_" + name, Math.round(this.histogram.getSnapshot().get999thPercentile())); + return m; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterSet.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterSet.java new file mode 100644 index 0000000..a656995 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterSet.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.metrics.SummarySet; + +/** + * Generic set of long counters that keep track of min/max/avg + * for different keys. + * The counter is thread-safe + */ +public class AvgMinMaxPercentileCounterSet extends Metric implements SummarySet { + + private final String name; + + private ConcurrentHashMap counters = new ConcurrentHashMap<>(); + + public AvgMinMaxPercentileCounterSet(String name) { + this.name = name; + } + + private AvgMinMaxPercentileCounter getCounterForKey(String key) { + AvgMinMaxPercentileCounter counter = counters.get(key); + if (counter == null) { + counters.putIfAbsent(key, new AvgMinMaxPercentileCounter(key + "_" + name)); + counter = counters.get(key); + } + + return counter; + } + + public void addDataPoint(String key, long value) { + getCounterForKey(key).addDataPoint(value); + } + + public void resetMax() { + for (Map.Entry entry : counters.entrySet()) { + entry.getValue().resetMax(); + } + } + + public void reset() { + for (Map.Entry entry : counters.entrySet()) { + entry.getValue().reset(); + } + } + + @Override + public void add(String key, long value) { + addDataPoint(key, value); + } + + @Override + public Map values() { + Map m = new LinkedHashMap<>(); + for (Map.Entry entry : counters.entrySet()) { + m.putAll(entry.getValue().values()); + } + return m; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/Metric.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/Metric.java new file mode 100644 index 0000000..7f3e388 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/Metric.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import java.util.Map; + +public abstract class Metric { + + public void add(long value) { + } + public void add(int key, long value) { + } + public void add(String key, long value) { + } + public void reset() { + } + public abstract Map values(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/SimpleCounter.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/SimpleCounter.java new file mode 100644 index 0000000..7121833 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/metric/SimpleCounter.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.metrics.Counter; + +public class SimpleCounter extends Metric implements Counter { + + private final String name; + private final AtomicLong counter = new AtomicLong(); + + public SimpleCounter(String name) { + this.name = name; + } + + @Override + public void add(long value) { + counter.addAndGet(value); + } + + @Override + public void reset() { + counter.set(0); + } + + public long get() { + return counter.get(); + } + + @Override + public Map values() { + Map m = new LinkedHashMap(); + m.put(name, this.get()); + return m; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/package.html b/zookeeper-server/src/main/java/org/apache/zookeeper/server/package.html new file mode 100644 index 0000000..89a5d7e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/package.html @@ -0,0 +1,101 @@ + + + + +

      ZooKeeper server theory of operation

      +ZooKeeperServer is designed to work in standalone mode and also +be extensible so that it can be used to implement the quorum based +version of ZooKeeper. +

      +ZooKeeper maintains a order when processing requests: +

        +
      • All requests will be processed in order. +
      • All responses will return in order. +
      • All watches will be sent in the order that the update takes place. +
      +

      +We will explain the three aspects of ZooKeeperServer: request processing, data +structure maintenance, and session tracking. + +

      Request processing

      + +Requests are received by the ServerCnxn. Demarshalling of a request is +done by ClientRequestHandler. After a request has been demarshalled, +ClientRequestHandler invokes the relevant method in ZooKeeper and marshals +the result. +

      +If the request is just a query, it will be processed by ZooKeeper and returned. +Otherwise, the request will be validated and a transaction will be generated +and logged. This the request will then wait until the request has been logged +before continuing processing. +

      +Requests are logged as a group. Transactions are queued up and the SyncThread +will process them at predefined intervals. (Currently 20ms) The SyncThread +interacts with ZooKeeperServer the txnQueue. Transactions are added to the +txnQueue of SyncThread via queueItem. When the transaction has been synced to +disk, its callback will be invoked which will cause the request processing to +be completed. + +

      Data structure maintenance

      + +ZooKeeper data is stored in-memory. Each znode is stored in a DataNode object. +This object is accessed through a hash table that maps paths to DataNodes. +DataNodes also organize themselves into a tree. This tree is only used for +serializing nodes. +

      +We guarantee that changes to nodes are stored to non-volatile media before +responding to a client. We do this quickly by writing changes as a sequence +of transactions in a log file. Even though we flush transactions as a group, +we need to avoid seeks as much as possible. Also, since the server can fail +at any point, we need to be careful of partial records. +

      +We address the above problems by +

        +
      • Pre-allocating 1M chunks of file space. This allows us to append to the +file without causing seeks to update file size. It also means that we need +to check for the end of the log by looking for a zero length transaction +rather than simply end of file. +
      • Writing a signature at the end of each transaction. When processing +transactions, we only use transactions that have a valid signature at the end. +
      +

      +As the server runs, the log file will grow quite large. To avoid long startup +times we periodically take a snapshot of the tree of DataNodes. We cannot +take the snapshot synchronously as the data takes a while to write out, so +instead we asynchronously write out the tree. This means that we end up +with a "corrupt" snapshot of the data tree. More formally if we define T +to be the real snapshot of the tree at the time we begin taking the snapshot +and l as the sequence of transactions that are applied to the tree between +the time the snapshot begins and the time the snapshot completes, we write +to disk T+l' where l' is a subset of the transactions in l. While we do not +have a way of figuring out which transactions make up l', it doesn't really +matter. T+l'+l = T+l since the transactions we log are idempotent (applying +the transaction multiple times has the same result as applying the transaction +once). So when we restore the snapshot we also play all transactions in the log +that occur after the snapshot was begun. We can easily figure out where to +start the replay because we start a new logfile when we start a snapshot. Both +the snapshot file and log file have a numeric suffix that represent the +transaction id that created the respective files. + +

      Session tracking

      +Rather than tracking sessions exactly, we track them in batches. That are +processed at fixed intervals. This is easier to implement than exact +session tracking and it is more efficient in terms of performance. It also +provides a small grace period for session renewal. + + \ No newline at end of file diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FilePadding.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FilePadding.java new file mode 100644 index 0000000..b258559 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FilePadding.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FilePadding { + + private static final Logger LOG; + private static long preAllocSize = 65536 * 1024; + private static final ByteBuffer fill = ByteBuffer.allocateDirect(1); + + static { + LOG = LoggerFactory.getLogger(FileTxnLog.class); + + String size = System.getProperty("zookeeper.preAllocSize"); + if (size != null) { + try { + preAllocSize = Long.parseLong(size) * 1024; + } catch (NumberFormatException e) { + LOG.warn("{} is not a valid value for preAllocSize", size); + } + } + } + + private long currentSize; + + /** + * Getter of preAllocSize has been added for testing + */ + public static long getPreAllocSize() { + return preAllocSize; + } + + /** + * method to allow setting preallocate size + * of log file to pad the file. + * + * @param size the size to set to in bytes + */ + public static void setPreallocSize(long size) { + preAllocSize = size; + } + + public void setCurrentSize(long currentSize) { + this.currentSize = currentSize; + } + + /** + * pad the current file to increase its size to the next multiple of preAllocSize greater than the current size and position + * + * @param fileChannel the fileChannel of the file to be padded + * @throws IOException + */ + long padFile(FileChannel fileChannel) throws IOException { + long newFileSize = calculateFileSizeWithPadding(fileChannel.position(), currentSize, preAllocSize); + if (currentSize != newFileSize) { + fileChannel.write((ByteBuffer) fill.position(0), newFileSize - fill.remaining()); + currentSize = newFileSize; + } + return currentSize; + } + + /** + * Calculates a new file size with padding. We only return a new size if + * the current file position is sufficiently close (less than 4K) to end of + * file and preAllocSize is > 0. + * + * @param position the point in the file we have written to + * @param fileSize application keeps track of the current file size + * @param preAllocSize how many bytes to pad + * @return the new file size. It can be the same as fileSize if no + * padding was done. + */ + // VisibleForTesting + public static long calculateFileSizeWithPadding(long position, long fileSize, long preAllocSize) { + // If preAllocSize is positive and we are within 4KB of the known end of the file calculate a new file size + if (preAllocSize > 0 && position + 4096 >= fileSize) { + // If we have written more than we have previously preallocated we need to make sure the new + // file size is larger than what we already have + if (position > fileSize) { + fileSize = position + preAllocSize; + fileSize -= fileSize % preAllocSize; + } else { + fileSize += preAllocSize; + } + } + + return fileSize; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileSnap.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileSnap.java new file mode 100644 index 0000000..baea316 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileSnap.java @@ -0,0 +1,338 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.zip.CheckedInputStream; +import java.util.zip.CheckedOutputStream; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements the snapshot interface. + * it is responsible for storing, serializing + * and deserializing the right snapshot. + * and provides access to the snapshots. + */ +public class FileSnap implements SnapShot { + + File snapDir; + SnapshotInfo lastSnapshotInfo = null; + private volatile boolean close = false; + private static final int VERSION = 2; + private static final long dbId = -1; + private static final Logger LOG = LoggerFactory.getLogger(FileSnap.class); + public static final int SNAP_MAGIC = ByteBuffer.wrap("ZKSN".getBytes()).getInt(); + + public static final String SNAPSHOT_FILE_PREFIX = "snapshot"; + + public FileSnap(File snapDir) { + this.snapDir = snapDir; + } + + /** + * get information of the last saved/restored snapshot + * + * @return info of last snapshot + */ + public SnapshotInfo getLastSnapshotInfo() { + return this.lastSnapshotInfo; + } + + /** + * deserialize a data tree from the most recent snapshot + * + * @return the zxid of the snapshot + */ + public long deserialize(DataTree dt, Map sessions) throws IOException { + // we run through 100 snapshots (not all of them) + // if we cannot get it running within 100 snapshots we should give up + List snapList = findNValidSnapshots(100); + if(snapList.size() == 0) { + return -1L; + } + File snap = null; + long snapZxid = -1; + boolean foundValid = false; + + // TODO_MA 注释: 遍历快照 + for(int i = 0, snapListSize = snapList.size(); i < snapListSize; i++) { + + // TODO_MA 注释: 获取最近的一次快照的数据文件 + snap = snapList.get(i); + + // TODO_MA 注释: 从快照文件名中,获取 zxid + LOG.info("Reading snapshot {}", snap); + snapZxid = Util.getZxidFromName(snap.getName(), SNAPSHOT_FILE_PREFIX); + + try(CheckedInputStream snapIS = SnapStream.getInputStream(snap)) { + InputArchive ia = BinaryInputArchive.getArchive(snapIS); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 执行恢复 + * 1、session持久化了,要恢复 + * 2、datatree持久化要恢复 + */ + deserialize(dt, sessions, ia); + + // TODO_MA 注释: 执行 CRC 校验 + SnapStream.checkSealIntegrity(snapIS, ia); + // Digest feature was added after the CRC to make it backward + // compatible, the older code can still read snapshots which includes digest. + // To check the intact, after adding digest we added another CRC check. + if(dt.deserializeZxidDigest(ia, snapZxid)) { + SnapStream.checkSealIntegrity(snapIS, ia); + } + + // TODO_MA 注释: 恢复 OK ,退出循环 + foundValid = true; + break; + } catch(IOException e) { + LOG.warn("problem reading snap file {}", snap, e); + } + } + if(!foundValid) { + throw new IOException("Not able to find valid snapshots in " + snapDir); + } + + // TODO_MA 注释: 最大的 zxid 保存到 datatree 中 + dt.lastProcessedZxid = snapZxid; + + // TODO_MA 注释: 生成一个快照信息 + lastSnapshotInfo = new SnapshotInfo(dt.lastProcessedZxid, snap.lastModified() / 1000); + + // compare the digest if this is not a fuzzy snapshot, we want to compare + // and find inconsistent asap. + if(dt.getDigestFromLoadedSnapshot() != null) { + dt.compareSnapshotDigests(dt.lastProcessedZxid); + } + + // TODO_MA 注释: 返回一个最大的 zxid + return dt.lastProcessedZxid; + } + + /** + * deserialize the datatree from an inputarchive + * + * @param dt the datatree to be serialized into + * @param sessions the sessions to be filled up + * @param ia the input archive to restore from + * @throws IOException + */ + public void deserialize(DataTree dt, Map sessions, InputArchive ia) throws IOException { + + // TODO_MA 注释: 先从 fileheader 中读取 SNAP_MAGIC = ZKSN + FileHeader header = new FileHeader(); + header.deserialize(ia, "fileheader"); + + // TODO_MA 注释: 如果 魔法值 被破坏了,证明 快照文件失效了。 + if(header.getMagic() != SNAP_MAGIC) { + throw new IOException("mismatching magic headers " + header.getMagic() + " != " + FileSnap.SNAP_MAGIC); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + SerializeUtils.deserializeSnapshot(dt, ia, sessions); + } + + /** + * find the most recent snapshot in the database. + * + * @return the file containing the most recent snapshot + */ + public File findMostRecentSnapshot() throws IOException { + List files = findNValidSnapshots(1); + if(files.size() == 0) { + return null; + } + return files.get(0); + } + + /** + * find the last (maybe) valid n snapshots. this does some + * minor checks on the validity of the snapshots. It just + * checks for / at the end of the snapshot. This does + * not mean that the snapshot is truly valid but is + * valid with a high probability. also, the most recent + * will be first on the list. + * + * @param n the number of most recent snapshots + * @return the last n snapshots (the number might be + * less than n in case enough snapshots are not available). + * @throws IOException + */ + protected List findNValidSnapshots(int n) throws IOException { + + // TODO_MA 注释: 快照文件降序排序(最近的快照是第一个) + List files = Util.sortDataDir(snapDir.listFiles(), SNAPSHOT_FILE_PREFIX, false); + + int count = 0; + List list = new ArrayList(); + for(File f : files) { + // we should catch the exceptions + // from the valid snapshot and continue + // until we find a valid one + try { + + // TODO_MA 注释: + if(SnapStream.isValidSnapshot(f)) { + list.add(f); + count++; + + // TODO_MA 注释: 找到 100个就退出了。 + if(count == n) { + break; + } + } + } catch(IOException e) { + LOG.warn("invalid snapshot {}", f, e); + } + } + return list; + } + + /** + * find the last n snapshots. this does not have + * any checks if the snapshot might be valid or not + * + * @param n the number of most recent snapshots + * @return the last n snapshots + * @throws IOException + */ + public List findNRecentSnapshots(int n) throws IOException { + List files = Util.sortDataDir(snapDir.listFiles(), SNAPSHOT_FILE_PREFIX, false); + int count = 0; + List list = new ArrayList(); + for(File f : files) { + if(count == n) { + break; + } + if(Util.getZxidFromName(f.getName(), SNAPSHOT_FILE_PREFIX) != -1) { + count++; + list.add(f); + } + } + return list; + } + + /** + * serialize the datatree and sessions + * + * @param dt the datatree to be serialized + * @param sessions the sessions to be serialized + * @param oa the output archive to serialize into + * @param header the header of this snapshot + * @throws IOException + */ + protected void serialize(DataTree dt, Map sessions, OutputArchive oa, + FileHeader header) throws IOException { + // this is really a programmatic error and not something that can + // happen at runtime + if(header == null) { + throw new IllegalStateException("Snapshot's not open for writing: uninitialized header"); + } + header.serialize(oa, "fileheader"); + SerializeUtils.serializeSnapshot(dt, oa, sessions); + } + + /** + * serialize the datatree and session into the file snapshot + * + * @param dt the datatree to be serialized + * @param sessions the sessions to be serialized + * @param snapShot the file to store snapshot into + * @param fsync sync the file immediately after write + */ + public synchronized void serialize(DataTree dt, Map sessions, File snapShot, + boolean fsync) throws IOException { + if(!close) { + + // TODO_MA 注释: + try(CheckedOutputStream snapOS = SnapStream.getOutputStream(snapShot, fsync)) { + OutputArchive oa = BinaryOutputArchive.getArchive(snapOS); + + // TODO_MA 注释: 构建快照文件头信息 + FileHeader header = new FileHeader(SNAP_MAGIC, VERSION, dbId); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 拍摄快照 + */ + serialize(dt, sessions, oa, header); + SnapStream.sealStream(snapOS, oa); + + // Digest feature was added after the CRC to make it backward + // compatible, the older code cal still read snapshots which includes digest. + // + // To check the intact, after adding digest we added another CRC check. + if(dt.serializeZxidDigest(oa)) { + SnapStream.sealStream(snapOS, oa); + } + + // TODO_MA 注释: 快照信息 + lastSnapshotInfo = new SnapshotInfo(Util.getZxidFromName(snapShot.getName(), SNAPSHOT_FILE_PREFIX), + snapShot.lastModified() / 1000); + } + } else { + throw new IOException("FileSnap has already been closed"); + } + } + + private void writeChecksum(CheckedOutputStream crcOut, OutputArchive oa) throws IOException { + long val = crcOut.getChecksum().getValue(); + oa.writeLong(val, "val"); + oa.writeString("/", "path"); + } + + private void checkChecksum(CheckedInputStream crcIn, InputArchive ia) throws IOException { + long checkSum = crcIn.getChecksum().getValue(); + long val = ia.readLong("val"); + // read and ignore "/" written by writeChecksum + ia.readString("path"); + if(val != checkSum) { + throw new IOException("CRC corruption"); + } + } + + /** + * synchronized close just so that if serialize is in place + * the close operation will block and will wait till serialize + * is done and will set the close flag + */ + @Override + public synchronized void close() throws IOException { + close = true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java new file mode 100644 index 0000000..cc9b549 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnLog.java @@ -0,0 +1,854 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.Closeable; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.TimeUnit; +import java.util.zip.Adler32; +import java.util.zip.Checksum; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements the TxnLog interface. It provides api's + * to access the txnlogs and add entries to it. + *

      + * The format of a Transactional log is as follows: + *

      + * LogFile:
      + *     FileHeader TxnList ZeroPad
      + *
      + * FileHeader: {
      + *     magic 4bytes (ZKLG)
      + *     version 4bytes
      + *     dbid 8bytes
      + *   }
      + *
      + * TxnList:
      + *     Txn || Txn TxnList
      + *
      + * Txn:
      + *     checksum Txnlen TxnHeader Record 0x42
      + *
      + * checksum: 8bytes Adler32 is currently used
      + *   calculated across payload -- Txnlen, TxnHeader, Record and 0x42
      + *
      + * Txnlen:
      + *     len 4bytes
      + *
      + * TxnHeader: {
      + *     sessionid 8bytes
      + *     cxid 4bytes
      + *     zxid 8bytes
      + *     time 8bytes
      + *     type 4bytes
      + *   }
      + *
      + * Record:
      + *     See Jute definition file for details on the various record types
      + *
      + * ZeroPad:
      + *     0 padded to EOF (filled during preallocation stage)
      + * 
      + */ +public class FileTxnLog implements TxnLog, Closeable { + + private static final Logger LOG; + + public static final int TXNLOG_MAGIC = ByteBuffer.wrap("ZKLG".getBytes()).getInt(); + + public static final int VERSION = 2; + + public static final String LOG_FILE_PREFIX = "log"; + + static final String FSYNC_WARNING_THRESHOLD_MS_PROPERTY = "fsync.warningthresholdms"; + static final String ZOOKEEPER_FSYNC_WARNING_THRESHOLD_MS_PROPERTY = "zookeeper." + FSYNC_WARNING_THRESHOLD_MS_PROPERTY; + + /** + * Maximum time we allow for elapsed fsync before WARNing + */ + private static final long fsyncWarningThresholdMS; + + /** + * This parameter limit the size of each txnlog to a given limit (KB). + * It does not affect how often the system will take a snapshot [zookeeper.snapCount] + * We roll the txnlog when either of the two limits are reached. + * Also since we only roll the logs at transaction boundaries, actual file size can exceed + * this limit by the maximum size of a serialized transaction. + * The feature is disabled by default (-1) + */ + private static final String txnLogSizeLimitSetting = "zookeeper.txnLogSizeLimitInKb"; + + /** + * The actual txnlog size limit in bytes. + */ + private static long txnLogSizeLimit = -1; + + static { + LOG = LoggerFactory.getLogger(FileTxnLog.class); + + /** Local variable to read fsync.warningthresholdms into */ + Long fsyncWarningThreshold; + if((fsyncWarningThreshold = Long.getLong(ZOOKEEPER_FSYNC_WARNING_THRESHOLD_MS_PROPERTY)) == null) { + fsyncWarningThreshold = Long.getLong(FSYNC_WARNING_THRESHOLD_MS_PROPERTY, 1000); + } + fsyncWarningThresholdMS = fsyncWarningThreshold; + + Long logSize = Long.getLong(txnLogSizeLimitSetting, -1); + if(logSize > 0) { + LOG.info("{} = {}", txnLogSizeLimitSetting, logSize); + + // Convert to bytes + logSize = logSize * 1024; + txnLogSizeLimit = logSize; + } + } + + long lastZxidSeen; + volatile BufferedOutputStream logStream = null; + volatile OutputArchive oa; + volatile FileOutputStream fos = null; + + File logDir; + private final boolean forceSync = !System.getProperty("zookeeper.forceSync", "yes").equals("no"); + long dbId; + private final Queue streamsToFlush = new ArrayDeque<>(); + File logFileWrite = null; + private FilePadding filePadding = new FilePadding(); + + private ServerStats serverStats; + + private volatile long syncElapsedMS = -1L; + + /** + * A running total of all complete log files + * This does not include the current file being written to + */ + private long prevLogsRunningTotal; + + /** + * constructor for FileTxnLog. Take the directory + * where the txnlogs are stored + * + * @param logDir the directory where the txnlogs are stored + */ + public FileTxnLog(File logDir) { + this.logDir = logDir; + } + + /** + * method to allow setting preallocate size + * of log file to pad the file. + * + * @param size the size to set to in bytes + */ + public static void setPreallocSize(long size) { + FilePadding.setPreallocSize(size); + } + + /** + * Setter for ServerStats to monitor fsync threshold exceed + * + * @param serverStats used to update fsyncThresholdExceedCount + */ + @Override + public synchronized void setServerStats(ServerStats serverStats) { + this.serverStats = serverStats; + } + + /** + * Set log size limit + */ + public static void setTxnLogSizeLimit(long size) { + txnLogSizeLimit = size; + } + + /** + * Return the current on-disk size of log size. This will be accurate only + * after commit() is called. Otherwise, unflushed txns may not be included. + */ + public synchronized long getCurrentLogSize() { + if(logFileWrite != null) { + return logFileWrite.length(); + } + return 0; + } + + public synchronized void setTotalLogSize(long size) { + prevLogsRunningTotal = size; + } + + public synchronized long getTotalLogSize() { + return prevLogsRunningTotal + getCurrentLogSize(); + } + + /** + * creates a checksum algorithm to be used + * + * @return the checksum used for this txnlog + */ + protected Checksum makeChecksumAlgorithm() { + return new Adler32(); + } + + /** + * rollover the current log file to a new one. + * + * @throws IOException + */ + public synchronized void rollLog() throws IOException { + if(logStream != null) { + this.logStream.flush(); + prevLogsRunningTotal += getCurrentLogSize(); + this.logStream = null; + oa = null; + + // Roll over the current log file into the running total + } + } + + /** + * close all the open file handles + * + * @throws IOException + */ + public synchronized void close() throws IOException { + if(logStream != null) { + logStream.close(); + } + for(FileOutputStream log : streamsToFlush) { + log.close(); + } + } + + /** + * append an entry to the transaction log + * + * @param hdr the header of the transaction + * @param txn the transaction part of the entry + * returns true iff something appended, otw false + */ + public synchronized boolean append(TxnHeader hdr, Record txn) throws IOException { + return append(hdr, txn, null); + } + + @Override + public synchronized boolean append(TxnHeader hdr, Record txn, TxnDigest digest) throws IOException { + if(hdr == null) { + return false; + } + if(hdr.getZxid() <= lastZxidSeen) { + LOG.warn("Current zxid {} is <= {} for {}", hdr.getZxid(), lastZxidSeen, hdr.getType()); + } else { + lastZxidSeen = hdr.getZxid(); + } + if(logStream == null) { + LOG.info("Creating new log file: {}", Util.makeLogName(hdr.getZxid())); + + logFileWrite = new File(logDir, Util.makeLogName(hdr.getZxid())); + fos = new FileOutputStream(logFileWrite); + logStream = new BufferedOutputStream(fos); + oa = BinaryOutputArchive.getArchive(logStream); + FileHeader fhdr = new FileHeader(TXNLOG_MAGIC, VERSION, dbId); + fhdr.serialize(oa, "fileheader"); + // Make sure that the magic number is written before padding. + logStream.flush(); + filePadding.setCurrentSize(fos.getChannel().position()); + streamsToFlush.add(fos); + } + filePadding.padFile(fos.getChannel()); + byte[] buf = Util.marshallTxnEntry(hdr, txn, digest); + if(buf == null || buf.length == 0) { + throw new IOException("Faulty serialization for header " + "and txn"); + } + Checksum crc = makeChecksumAlgorithm(); + crc.update(buf, 0, buf.length); + oa.writeLong(crc.getValue(), "txnEntryCRC"); + Util.writeTxnBytes(oa, buf); + + return true; + } + + /** + * Find the log file that starts at, or just before, the snapshot. Return + * this and all subsequent logs. Results are ordered by zxid of file, + * ascending order. + * + * @param logDirList array of files + * @param snapshotZxid return files at, or before this zxid + * @return + */ + public static File[] getLogFiles(File[] logDirList, long snapshotZxid) { + List files = Util.sortDataDir(logDirList, LOG_FILE_PREFIX, true); + long logZxid = 0; + // Find the log file that starts before or at the same time as the + // zxid of the snapshot + for(File f : files) { + long fzxid = Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX); + if(fzxid > snapshotZxid) { + break; + } + // the files + // are sorted with zxid's + if(fzxid > logZxid) { + logZxid = fzxid; + } + } + List v = new ArrayList(5); + for(File f : files) { + long fzxid = Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX); + if(fzxid < logZxid) { + continue; + } + v.add(f); + } + return v.toArray(new File[0]); + + } + + /** + * get the last zxid that was logged in the transaction logs + * + * @return the last zxid logged in the transaction logs + */ + public long getLastLoggedZxid() { + File[] files = getLogFiles(logDir.listFiles(), 0); + long maxLog = files.length > 0 ? Util.getZxidFromName(files[files.length - 1].getName(), LOG_FILE_PREFIX) : -1; + + // if a log file is more recent we must scan it to find + // the highest zxid + long zxid = maxLog; + TxnIterator itr = null; + try { + FileTxnLog txn = new FileTxnLog(logDir); + itr = txn.read(maxLog); + while(true) { + if(!itr.next()) { + break; + } + TxnHeader hdr = itr.getHeader(); + zxid = hdr.getZxid(); + } + } catch(IOException e) { + LOG.warn("Unexpected exception", e); + } finally { + close(itr); + } + return zxid; + } + + private void close(TxnIterator itr) { + if(itr != null) { + try { + itr.close(); + } catch(IOException ioe) { + LOG.warn("Error closing file iterator", ioe); + } + } + } + + /** + * commit the logs. make sure that everything hits the + * disk + */ + public synchronized void commit() throws IOException { + if(logStream != null) { + logStream.flush(); + } + for(FileOutputStream log : streamsToFlush) { + log.flush(); + if(forceSync) { + long startSyncNS = System.nanoTime(); + + FileChannel channel = log.getChannel(); + channel.force(false); + + syncElapsedMS = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startSyncNS); + if(syncElapsedMS > fsyncWarningThresholdMS) { + if(serverStats != null) { + serverStats.incrementFsyncThresholdExceedCount(); + } + + LOG.warn( + "fsync-ing the write ahead log in {} took {}ms which will adversely effect operation latency." + "File size is {} bytes. See the ZooKeeper troubleshooting guide", + Thread.currentThread().getName(), syncElapsedMS, channel.size()); + } + + ServerMetrics.getMetrics().FSYNC_TIME.add(syncElapsedMS); + } + } + while(streamsToFlush.size() > 1) { + streamsToFlush.poll().close(); + } + + // Roll the log file if we exceed the size limit + if(txnLogSizeLimit > 0) { + long logSize = getCurrentLogSize(); + + if(logSize > txnLogSizeLimit) { + LOG.debug("Log size limit reached: {}", logSize); + rollLog(); + } + } + } + + /** + * @return elapsed sync time of transaction log in milliseconds + */ + public long getTxnLogSyncElapsedTime() { + return syncElapsedMS; + } + + /** + * start reading all the transactions from the given zxid + * + * @param zxid the zxid to start reading transactions from + * @return returns an iterator to iterate through the transaction + * logs + */ + public TxnIterator read(long zxid) throws IOException { + return read(zxid, true); + } + + /** + * start reading all the transactions from the given zxid. + * + * @param zxid the zxid to start reading transactions from + * @param fastForward true if the iterator should be fast forwarded to point + * to the txn of a given zxid, else the iterator will point to the + * starting txn of a txnlog that may contain txn of a given zxid + * @return returns an iterator to iterate through the transaction logs + */ + public TxnIterator read(long zxid, boolean fastForward) throws IOException { + return new FileTxnIterator(logDir, zxid, fastForward); + } + + /** + * truncate the current transaction logs + * + * @param zxid the zxid to truncate the logs to + * @return true if successful false if not + */ + public boolean truncate(long zxid) throws IOException { + FileTxnIterator itr = null; + try { + itr = new FileTxnIterator(this.logDir, zxid); + PositionInputStream input = itr.inputStream; + if(input == null) { + throw new IOException( + "No log files found to truncate! This could " + "happen if you still have snapshots from an old setup or " + "log files were deleted accidentally or dataLogDir was changed in zoo.cfg."); + } + long pos = input.getPosition(); + // now, truncate at the current position + RandomAccessFile raf = new RandomAccessFile(itr.logFile, "rw"); + raf.setLength(pos); + raf.close(); + while(itr.goToNextLog()) { + if(!itr.logFile.delete()) { + LOG.warn("Unable to truncate {}", itr.logFile); + } + } + } finally { + close(itr); + } + return true; + } + + /** + * read the header of the transaction file + * + * @param file the transaction file to read + * @return header that was read from the file + * @throws IOException + */ + private static FileHeader readHeader(File file) throws IOException { + InputStream is = null; + try { + is = new BufferedInputStream(new FileInputStream(file)); + InputArchive ia = BinaryInputArchive.getArchive(is); + FileHeader hdr = new FileHeader(); + hdr.deserialize(ia, "fileheader"); + return hdr; + } finally { + try { + if(is != null) { + is.close(); + } + } catch(IOException e) { + LOG.warn("Ignoring exception during close", e); + } + } + } + + /** + * the dbid of this transaction database + * + * @return the dbid of this database + */ + public long getDbId() throws IOException { + FileTxnIterator itr = new FileTxnIterator(logDir, 0); + FileHeader fh = readHeader(itr.logFile); + itr.close(); + if(fh == null) { + throw new IOException("Unsupported Format."); + } + return fh.getDbid(); + } + + /** + * the forceSync value. true if forceSync is enabled, false otherwise. + * + * @return the forceSync value + */ + public boolean isForceSync() { + return forceSync; + } + + /** + * a class that keeps track of the position + * in the input stream. The position points to offset + * that has been consumed by the applications. It can + * wrap buffered input streams to provide the right offset + * for the application. + */ + static class PositionInputStream extends FilterInputStream { + + long position; + + protected PositionInputStream(InputStream in) { + super(in); + position = 0; + } + + @Override + public int read() throws IOException { + int rc = super.read(); + if(rc > -1) { + position++; + } + return rc; + } + + public int read(byte[] b) throws IOException { + int rc = super.read(b); + if(rc > 0) { + position += rc; + } + return rc; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + int rc = super.read(b, off, len); + if(rc > 0) { + position += rc; + } + return rc; + } + + @Override + public long skip(long n) throws IOException { + long rc = super.skip(n); + if(rc > 0) { + position += rc; + } + return rc; + } + + public long getPosition() { + return position; + } + + @Override + public boolean markSupported() { + return false; + } + + @Override + public void mark(int readLimit) { + throw new UnsupportedOperationException("mark"); + } + + @Override + public void reset() { + throw new UnsupportedOperationException("reset"); + } + + } + + /** + * this class implements the txnlog iterator interface + * which is used for reading the transaction logs + */ + public static class FileTxnIterator implements TxnLog.TxnIterator { + + File logDir; + long zxid; + TxnHeader hdr; + Record record; + TxnDigest digest; + File logFile; + InputArchive ia; + static final String CRC_ERROR = "CRC check failed"; + + PositionInputStream inputStream = null; + //stored files is the list of files greater than + //the zxid we are looking for. + private ArrayList storedFiles; + + /** + * create an iterator over a transaction database directory + * + * @param logDir the transaction database directory + * @param zxid the zxid to start reading from + * @param fastForward true if the iterator should be fast forwarded to + * point to the txn of a given zxid, else the iterator will + * point to the starting txn of a txnlog that may contain txn of + * a given zxid + * @throws IOException + */ + public FileTxnIterator(File logDir, long zxid, boolean fastForward) throws IOException { + this.logDir = logDir; + this.zxid = zxid; + init(); + + if(fastForward && hdr != null) { + while(hdr.getZxid() < zxid) { + if(!next()) { + break; + } + } + } + } + + /** + * create an iterator over a transaction database directory + * + * @param logDir the transaction database directory + * @param zxid the zxid to start reading from + * @throws IOException + */ + public FileTxnIterator(File logDir, long zxid) throws IOException { + this(logDir, zxid, true); + } + + /** + * initialize to the zxid specified + * this is inclusive of the zxid + * + * @throws IOException + */ + void init() throws IOException { + storedFiles = new ArrayList<>(); + List files = Util.sortDataDir(FileTxnLog.getLogFiles(logDir.listFiles(), 0), LOG_FILE_PREFIX, false); + for(File f : files) { + if(Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) >= zxid) { + storedFiles.add(f); + } else if(Util.getZxidFromName(f.getName(), LOG_FILE_PREFIX) < zxid) { + // add the last logfile that is less than the zxid + storedFiles.add(f); + break; + } + } + goToNextLog(); + next(); + } + + /** + * Return total storage size of txnlog that will return by this iterator. + */ + public long getStorageSize() { + long sum = 0; + for(File f : storedFiles) { + sum += f.length(); + } + return sum; + } + + /** + * go to the next logfile + * + * @return true if there is one and false if there is no + * new file to be read + * @throws IOException + */ + private boolean goToNextLog() throws IOException { + if(storedFiles.size() > 0) { + this.logFile = storedFiles.remove(storedFiles.size() - 1); + ia = createInputArchive(this.logFile); + return true; + } + return false; + } + + /** + * read the header from the inputarchive + * + * @param ia the inputarchive to be read from + * @param is the inputstream + * @throws IOException + */ + protected void inStreamCreated(InputArchive ia, InputStream is) throws IOException { + FileHeader header = new FileHeader(); + header.deserialize(ia, "fileheader"); + if(header.getMagic() != FileTxnLog.TXNLOG_MAGIC) { + throw new IOException("Transaction log: " + this.logFile + " has invalid magic number " + header + .getMagic() + " != " + FileTxnLog.TXNLOG_MAGIC); + } + } + + /** + * Invoked to indicate that the input stream has been created. + * + * @param logFile the file to read. + * @throws IOException + **/ + protected InputArchive createInputArchive(File logFile) throws IOException { + if(inputStream == null) { + inputStream = new PositionInputStream(new BufferedInputStream(new FileInputStream(logFile))); + LOG.debug("Created new input stream: {}", logFile); + ia = BinaryInputArchive.getArchive(inputStream); + inStreamCreated(ia, inputStream); + LOG.debug("Created new input archive: {}", logFile); + } + return ia; + } + + /** + * create a checksum algorithm + * + * @return the checksum algorithm + */ + protected Checksum makeChecksumAlgorithm() { + return new Adler32(); + } + + /** + * the iterator that moves to the next transaction + * + * @return true if there is more transactions to be read + * false if not. + */ + public boolean next() throws IOException { + if(ia == null) { + return false; + } + try { + long crcValue = ia.readLong("crcvalue"); + byte[] bytes = Util.readTxnBytes(ia); + // Since we preallocate, we define EOF to be an + if(bytes == null || bytes.length == 0) { + throw new EOFException("Failed to read " + logFile); + } + // EOF or corrupted record + // validate CRC + Checksum crc = makeChecksumAlgorithm(); + crc.update(bytes, 0, bytes.length); + if(crcValue != crc.getValue()) { + throw new IOException(CRC_ERROR); + } + TxnLogEntry logEntry = SerializeUtils.deserializeTxn(bytes); + hdr = logEntry.getHeader(); + record = logEntry.getTxn(); + digest = logEntry.getDigest(); + } catch(EOFException e) { + LOG.debug("EOF exception", e); + inputStream.close(); + inputStream = null; + ia = null; + hdr = null; + // this means that the file has ended + // we should go to the next file + if(!goToNextLog()) { + return false; + } + // if we went to the next log file, we should call next() again + return next(); + } catch(IOException e) { + inputStream.close(); + throw e; + } + return true; + } + + /** + * return the current header + * + * @return the current header that + * is read + */ + public TxnHeader getHeader() { + return hdr; + } + + /** + * return the current transaction + * + * @return the current transaction + * that is read + */ + public Record getTxn() { + return record; + } + + public TxnDigest getDigest() { + return digest; + } + + /** + * close the iterator + * and release the resources. + */ + public void close() throws IOException { + if(inputStream != null) { + inputStream.close(); + } + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java new file mode 100644 index 0000000..ac7b96f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java @@ -0,0 +1,786 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.jute.Record; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a helper class + * above the implementations + * of txnlog and snapshot + * classes + * // TODO_MA 马中华 注释: ZKDatabase zk 数据库 + * // TODO_MA 马中华 注释: ZKDatabase 维护了两个东西 + * // TODO_MA 马中华 注释: 1、FileTxnSnapLog 提供相关动作的实现 + * // TODO_MA 马中华 注释: 2、DataTree 内存中的所有数据 + */ +public class FileTxnSnapLog { + + //the directory containing the + //the transaction logs + final File dataDir; + //the directory containing the + //the snapshot directory + final File snapDir; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 负责 日志记录相关 工作 + */ + TxnLog txnLog; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 负责 内存数据 和 磁盘数据 的转化 工作 + */ + SnapShot snapLog; + + private final boolean autoCreateDB; + private final boolean trustEmptySnapshot; + public static final int VERSION = 2; + public static final String version = "version-"; + + private static final Logger LOG = LoggerFactory.getLogger(FileTxnSnapLog.class); + + public static final String ZOOKEEPER_DATADIR_AUTOCREATE = "zookeeper.datadir.autocreate"; + + public static final String ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT = "true"; + + static final String ZOOKEEPER_DB_AUTOCREATE = "zookeeper.db.autocreate"; + + private static final String ZOOKEEPER_DB_AUTOCREATE_DEFAULT = "true"; + + public static final String ZOOKEEPER_SNAPSHOT_TRUST_EMPTY = "zookeeper.snapshot.trust.empty"; + + private static final String EMPTY_SNAPSHOT_WARNING = "No snapshot found, but there are log entries. "; + + /** + * This listener helps + * the external apis calling + * restore to gather information + * while the data is being + * restored. + */ + public interface PlayBackListener { + + void onTxnLoaded(TxnHeader hdr, Record rec, TxnDigest digest); + + } + + /** + * Finalizing restore of data tree through + * a set of operations (replaying transaction logs, + * calculating data tree digests, and so on.). + */ + private interface RestoreFinalizer { + /** + * @return the highest zxid of restored data tree. + */ + long run() throws IOException; + } + + /** + * the constructor which takes the datadir and + * snapdir. + * + * @param dataDir the transaction directory + * @param snapDir the snapshot directory + */ + public FileTxnSnapLog(File dataDir, File snapDir) throws IOException { + LOG.debug("Opening datadir:{} snapDir:{}", dataDir, snapDir); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.dataDir = new File(dataDir, version + VERSION); + this.snapDir = new File(snapDir, version + VERSION); + + // by default create snap/log dirs, but otherwise complain instead + // See ZOOKEEPER-1161 for more details + boolean enableAutocreate = Boolean + .parseBoolean(System.getProperty(ZOOKEEPER_DATADIR_AUTOCREATE, ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT)); + + trustEmptySnapshot = Boolean.getBoolean(ZOOKEEPER_SNAPSHOT_TRUST_EMPTY); + LOG.info("{} : {}", ZOOKEEPER_SNAPSHOT_TRUST_EMPTY, trustEmptySnapshot); + + // TODO_MA 注释: 检查数据文件夹是否存在 + if(!this.dataDir.exists()) { + if(!enableAutocreate) { + throw new DatadirException(String.format( + "Missing data directory %s, automatic data directory creation is disabled (%s is false)." + " Please create this directory manually.", + this.dataDir, ZOOKEEPER_DATADIR_AUTOCREATE)); + } + + if(!this.dataDir.mkdirs() && !this.dataDir.exists()) { + throw new DatadirException("Unable to create data directory " + this.dataDir); + } + } + if(!this.dataDir.canWrite()) { + throw new DatadirException("Cannot write to data directory " + this.dataDir); + } + + // TODO_MA 注释: 检查快照文件夹是否存在 + if(!this.snapDir.exists()) { + // by default create this directory, but otherwise complain instead + // See ZOOKEEPER-1161 for more details + if(!enableAutocreate) { + throw new DatadirException(String.format( + "Missing snap directory %s, automatic data directory creation is disabled (%s is false)." + "Please create this directory manually.", + this.snapDir, ZOOKEEPER_DATADIR_AUTOCREATE)); + } + + if(!this.snapDir.mkdirs() && !this.snapDir.exists()) { + throw new DatadirException("Unable to create snap directory " + this.snapDir); + } + } + if(!this.snapDir.canWrite()) { + throw new DatadirException("Cannot write to snap directory " + this.snapDir); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + // check content of transaction log and snapshot dirs if they are two different directories + // See ZOOKEEPER-2967 for more details + if(!this.dataDir.getPath().equals(this.snapDir.getPath())) { + checkLogDir(); + checkSnapDir(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + txnLog = new FileTxnLog(this.dataDir); + snapLog = new FileSnap(this.snapDir); + + autoCreateDB = Boolean.parseBoolean(System.getProperty(ZOOKEEPER_DB_AUTOCREATE, ZOOKEEPER_DB_AUTOCREATE_DEFAULT)); + } + + public void setServerStats(ServerStats serverStats) { + txnLog.setServerStats(serverStats); + } + + private void checkLogDir() throws LogDirContentCheckException { + File[] files = this.dataDir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return Util.isSnapshotFileName(name); + } + }); + if(files != null && files.length > 0) { + throw new LogDirContentCheckException( + "Log directory has snapshot files. Check if dataLogDir and dataDir configuration is correct."); + } + } + + private void checkSnapDir() throws SnapDirContentCheckException { + File[] files = this.snapDir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return Util.isLogFileName(name); + } + }); + if(files != null && files.length > 0) { + throw new SnapDirContentCheckException( + "Snapshot directory has log files. Check if dataLogDir and dataDir configuration is correct."); + } + } + + /** + * get the datadir used by this filetxn + * snap log + * + * @return the data dir + */ + public File getDataDir() { + return this.dataDir; + } + + /** + * get the snap dir used by this + * filetxn snap log + * + * @return the snap dir + */ + public File getSnapDir() { + return this.snapDir; + } + + /** + * get information of the last saved/restored snapshot + * + * @return info of last snapshot + */ + public SnapshotInfo getLastSnapshotInfo() { + return this.snapLog.getLastSnapshotInfo(); + } + + /** + * this function restores the server + * database after reading from the + * snapshots and transaction logs + * + * // TODO_MA 注释: 假设当前 zookeeper 最大 事务 id = 100 + * + * @param dt the datatree to be restored + * @param sessions the sessions to be restored + * @param listener the playback listener to run on the + * database restoration + * @return the highest zxid restored + * @throws IOException + */ + public long restore(DataTree dt, Map sessions, PlayBackListener listener) throws IOException { + long snapLoadingStartTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从快照恢复绝大部分数据 + * 快照文件恢复: 1-95 + * 里面又包含恢复 session 信息 和 datatree 数据 + * dt.lastProcessID = 95 + * snapLog = SnapShot 快照功能 + */ + long deserializeResult = snapLog.deserialize(dt, sessions); + + ServerMetrics.getMetrics().STARTUP_SNAP_LOAD_TIME.add(Time.currentElapsedTime() - snapLoadingStartTime); + FileTxnLog txnLog = new FileTxnLog(dataDir); + boolean trustEmptyDB; + File initFile = new File(dataDir.getParent(), "initialize"); + if(Files.deleteIfExists(initFile.toPath())) { + LOG.info("Initialize file found, an empty database will not block voting participation"); + trustEmptyDB = true; + } else { + trustEmptyDB = autoCreateDB; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从日志恢复一小部分最新数据 + * 96-100 这五条事务的数据,从日志中执行恢复 + * 扫描日志文件 + * highestZxid = 100 + */ + RestoreFinalizer finalizer = () -> { + long highestZxid = fastForwardFromEdits(dt, sessions, listener); + // The snapshotZxidDigest will reset after replaying the txn of the + // zxid in the snapshotZxidDigest, if it's not reset to null after + // restoring, it means either there are not enough txns to cover that + // zxid or that txn is missing + DataTree.ZxidDigest snapshotZxidDigest = dt.getDigestFromLoadedSnapshot(); + if(snapshotZxidDigest != null) { + LOG.warn( + "Highest txn zxid 0x{} is not covering the snapshot digest zxid 0x{}, " + "which might lead to inconsistent state", + Long.toHexString(highestZxid), Long.toHexString(snapshotZxidDigest.getZxid())); + } + + // TODO_MA 注释: 返回 最大的 zxid + return highestZxid; + }; + + // TODO_MA 注释: 如果还没有快照文件,则执行 ZKDatabase 初始化 + if(-1L == deserializeResult) { + /* this means that we couldn't find any snapshot, so we need to + * initialize an empty database (reported in ZOOKEEPER-2325) */ + if(txnLog.getLastLoggedZxid() != -1) { + // ZOOKEEPER-3056: provides an escape hatch for users upgrading + // from old versions of zookeeper (3.4.x, pre 3.5.3). + if(!trustEmptySnapshot) { + throw new IOException(EMPTY_SNAPSHOT_WARNING + "Something is broken!"); + } else { + LOG.warn("{}This should only be allowed during upgrading.", EMPTY_SNAPSHOT_WARNING); + return finalizer.run(); + } + } + + if(trustEmptyDB) { + /* TODO: (br33d) we should either put a ConcurrentHashMap on restore() or use Map on save() */ + save(dt, (ConcurrentHashMap) sessions, false); + + /* return a zxid of 0, since we know the database is empty */ + return 0L; + } else { + /* return a zxid of -1, since we are possibly missing data */ + LOG.warn("Unexpected empty data tree, setting zxid to -1"); + dt.lastProcessedZxid = -1L; + return -1L; + } + } + + return finalizer.run(); + } + + /** + * This function will fast forward the server database to have the latest + * transactions in it. This is the same as restore, but only reads from + * the transaction logs and not restores from a snapshot. + * + * @param dt the datatree to write transactions to. + * @param sessions the sessions to be restored. + * @param listener the playback listener to run on the + * database transactions. + * @return the highest zxid restored. + * @throws IOException + * // TODO_MA 注释: snap.deserialize(); + * // TODO_MA 注释: txnLog.read(dt.lastProcessedZxid + 1) + * dt.lastProcessedZxid = 95 + */ + public long fastForwardFromEdits(DataTree dt, Map sessions, + PlayBackListener listener) throws IOException { + + // TODO_MA 注释: 注意此处: 从 dt.lastProcessedZxid + 1 处开始读取 日志文件中的 日志进行恢复 + // TODO_MA 注释: 从日志文件中,开始读取第 96 事务的数据 + TxnIterator itr = txnLog.read(dt.lastProcessedZxid + 1); + // TODO_MA 注释: 等于举例的 95 + long highestZxid = dt.lastProcessedZxid; + + TxnHeader hdr; + int txnLoaded = 0; + long startTime = Time.currentElapsedTime(); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + while(true) { + + // TODO_MA 注释: 先读取 日志消息的 header,这里面包含了 zxid + // iterator points to the first valid txn when initialized + hdr = itr.getHeader(); + + // TODO_MA 注释: 如果为空,则直接返回 + if(hdr == null) { + //empty logs + return dt.lastProcessedZxid; + } + + // TODO_MA 注释: 如果从日志文件中读取的 zxid 小于 datatree 中的,则忽略 + if(hdr.getZxid() < highestZxid && highestZxid != 0) { + LOG.error("{}(highestZxid) > {}(next log) for type {}", highestZxid, hdr.getZxid(), hdr.getType()); + } else { + // TODO_MA 注释: while每执行一次,其实这个 highestZxid 就 + 1 + highestZxid = hdr.getZxid(); + } + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 恢复事务的执行,更新数据到内存 datatree 中 + * 1、session + * 2、datatree的datanode节点 + * replay = 复现 = 重复执行 + */ + processTransaction(hdr, dt, sessions, itr.getTxn()); + + dt.compareDigest(hdr, itr.getTxn(), itr.getDigest()); + + // TODO_MA 注释: 读取的日志 计数 +1 + txnLoaded++; + } catch(KeeperException.NoNodeException e) { + throw new IOException( + "Failed to process transaction type: " + hdr.getType() + " error: " + e.getMessage(), e); + } + listener.onTxnLoaded(hdr, itr.getTxn(), itr.getDigest()); + + // TODO_MA 注释: 如果读取完毕,则退出循环 + if(!itr.next()) { + break; + } + } + } finally { + if(itr != null) { + itr.close(); + } + } + + // TODO_MA 注释: 计算从日志读取的时间消耗 + long loadTime = Time.currentElapsedTime() - startTime; + LOG.info("{} txns loaded in {} ms", txnLoaded, loadTime); + + ServerMetrics.getMetrics().STARTUP_TXNS_LOADED.add(txnLoaded); + ServerMetrics.getMetrics().STARTUP_TXNS_LOAD_TIME.add(loadTime); + + // TODO_MA 注释: 返回最大的 zxid + return highestZxid; + } + + /** + * Get TxnIterator for iterating through txnlog starting at a given zxid + * + * @param zxid starting zxid + * @return TxnIterator + * @throws IOException + */ + public TxnIterator readTxnLog(long zxid) throws IOException { + return readTxnLog(zxid, true); + } + + /** + * Get TxnIterator for iterating through txnlog starting at a given zxid + * + * @param zxid starting zxid + * @param fastForward true if the iterator should be fast forwarded to point + * to the txn of a given zxid, else the iterator will point to the + * starting txn of a txnlog that may contain txn of a given zxid + * @return TxnIterator + * @throws IOException + */ + public TxnIterator readTxnLog(long zxid, boolean fastForward) throws IOException { + FileTxnLog txnLog = new FileTxnLog(dataDir); + return txnLog.read(zxid, fastForward); + } + + /** + * process the transaction on the datatree + * + * @param hdr the hdr of the transaction + * @param dt the datatree to apply transaction to + * @param sessions the sessions to be restored + * @param txn the transaction to be applied + */ + public void processTransaction(TxnHeader hdr, DataTree dt, Map sessions, + Record txn) throws KeeperException.NoNodeException { + ProcessTxnResult rc; + + // TODO_MA 注释: session 相关动作, znode 相关动作 + switch(hdr.getType()) { + case OpCode.createSession: + sessions.put(hdr.getClientId(), ((CreateSessionTxn) txn).getTimeOut()); + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "playLog --- create session in log: 0x" + Long + .toHexString(hdr.getClientId()) + " with timeout: " + ((CreateSessionTxn) txn) + .getTimeOut()); + } + // give dataTree a chance to sync its lastProcessedZxid + rc = dt.processTxn(hdr, txn); + break; + case OpCode.closeSession: + sessions.remove(hdr.getClientId()); + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "playLog --- close session in log: 0x" + Long.toHexString(hdr.getClientId())); + } + rc = dt.processTxn(hdr, txn); + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 增删改 DataNode 节点的动作 + */ + default: + rc = dt.processTxn(hdr, txn); + } + + /* + * Snapshots are lazily created. So when a snapshot is in progress, + * there is a chance for later transactions to make into the + * snapshot. Then when the snapshot is restored, NONODE/NODEEXISTS + * errors could occur. It should be safe to ignore these. + */ + if(rc.err != Code.OK.intValue()) { + LOG.debug("Ignoring processTxn failure hdr: {}, error: {}, path: {}", hdr.getType(), rc.err, rc.path); + } + } + + /** + * the last logged zxid on the transaction logs + * + * @return the last logged zxid + */ + public long getLastLoggedZxid() { + FileTxnLog txnLog = new FileTxnLog(dataDir); + return txnLog.getLastLoggedZxid(); + } + + /** + * save the datatree and the sessions into a snapshot + * + * @param dataTree the datatree to be serialized onto disk + * @param sessionsWithTimeouts the session timeouts to be + * serialized onto disk + * @param syncSnap sync the snapshot immediately after write + * @throws IOException + */ + public void save(DataTree dataTree, ConcurrentHashMap sessionsWithTimeouts, + boolean syncSnap) throws IOException { + + // TODO_MA 注释: 获取 zxid + long lastZxid = dataTree.lastProcessedZxid; + + // TODO_MA 注释: 生成快照文件名 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 594 Feb 28 14:17 snapshot.0 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 594 Feb 28 14:17 snapshot.100000000 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 147203 Jul 14 17:10 snapshot.10000001c3 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 50221 Mar 1 10:26 snapshot.20000d77d + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 122839 Apr 26 15:55 snapshot.60000068e + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 118703 May 11 19:27 snapshot.70000003d + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 122408 Jun 19 17:34 snapshot.900000084 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 123100 Jun 22 17:43 snapshot.b00000002 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 142611 Jul 9 12:58 snapshot.c00000064 + // TODO_MA 注释: -rw-rw-r-- 1 bigdata bigdata 142793 Jul 9 12:59 snapshot.d00000010 + File snapshotFile = new File(snapDir, Util.makeSnapshotName(lastZxid)); + LOG.info("Snapshotting: 0x{} to {}", Long.toHexString(lastZxid), snapshotFile); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 拍摄快照 + */ + try { + snapLog.serialize(dataTree, sessionsWithTimeouts, snapshotFile, syncSnap); + } catch(IOException e) { + if(snapshotFile.length() == 0) { + /* This may be caused by a full disk. In such a case, the server + * will get stuck in a loop where it tries to write a snapshot + * out to disk, and ends up creating an empty file instead. + * Doing so will eventually result in valid snapshots being + * removed during cleanup. */ + if(snapshotFile.delete()) { + LOG.info("Deleted empty snapshot file: {}", snapshotFile.getAbsolutePath()); + } else { + LOG.warn("Could not delete empty snapshot file: {}", snapshotFile.getAbsolutePath()); + } + } else { + /* Something else went wrong when writing the snapshot out to + * disk. If this snapshot file is invalid, when restarting, + * ZooKeeper will skip it, and find the last known good snapshot + * instead. */ + } + throw e; + } + } + + /** + * truncate the transaction logs the zxid + * specified + * + * @param zxid the zxid to truncate the logs to + * @return true if able to truncate the log, false if not + * @throws IOException + */ + public boolean truncateLog(long zxid) { + try { + // close the existing txnLog and snapLog + close(); + + // truncate it + try(FileTxnLog truncLog = new FileTxnLog(dataDir)) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + boolean truncated = truncLog.truncate(zxid); + + // re-open the txnLog and snapLog + // I'd rather just close/reopen this object itself, however that + // would have a big impact outside ZKDatabase as there are other + // objects holding a reference to this object. + txnLog = new FileTxnLog(dataDir); + snapLog = new FileSnap(snapDir); + + return truncated; + } + } catch(IOException e) { + LOG.error("Unable to truncate Txn log", e); + return false; + } + } + + /** + * the most recent snapshot in the snapshot + * directory + * + * @return the file that contains the most + * recent snapshot + * @throws IOException + */ + public File findMostRecentSnapshot() throws IOException { + FileSnap snaplog = new FileSnap(snapDir); + return snaplog.findMostRecentSnapshot(); + } + + /** + * the n most recent snapshots + * + * @param n the number of recent snapshots + * @return the list of n most recent snapshots, with + * the most recent in front + * @throws IOException + */ + public List findNRecentSnapshots(int n) throws IOException { + FileSnap snaplog = new FileSnap(snapDir); + return snaplog.findNRecentSnapshots(n); + } + + /** + * the n recent valid snapshots + * + * @param n the number of recent valid snapshots + * @return the list of n recent valid snapshots, with + * the most recent in front + * @throws IOException + */ + public List findNValidSnapshots(int n) throws IOException { + FileSnap snaplog = new FileSnap(snapDir); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return snaplog.findNValidSnapshots(n); + } + + /** + * get the snapshot logs which may contain transactions newer than the given zxid. + * This includes logs with starting zxid greater than given zxid, as well as the + * newest transaction log with starting zxid less than given zxid. The latter log + * file may contain transactions beyond given zxid. + * + * @param zxid the zxid that contains logs greater than + * zxid + * @return + */ + public File[] getSnapshotLogs(long zxid) { + return FileTxnLog.getLogFiles(dataDir.listFiles(), zxid); + } + + /** + * append the request to the transaction logs + * + * @param si the request to be appended + * @return true iff something appended, otw false + * @throws IOException + */ + public boolean append(Request si) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 记录一条日志 + */ + return txnLog.append(si.getHdr(), si.getTxn(), si.getTxnDigest()); + } + + /** + * commit the transaction of logs + * + * @throws IOException + */ + public void commit() throws IOException { + txnLog.commit(); + } + + /** + * @return elapsed sync time of transaction log commit in milliseconds + */ + public long getTxnLogElapsedSyncTime() { + return txnLog.getTxnLogSyncElapsedTime(); + } + + /** + * roll the transaction logs + * + * @throws IOException + */ + public void rollLog() throws IOException { + txnLog.rollLog(); + } + + /** + * close the transaction log files + * + * @throws IOException + */ + public void close() throws IOException { + if(txnLog != null) { + txnLog.close(); + txnLog = null; + } + if(snapLog != null) { + snapLog.close(); + snapLog = null; + } + } + + @SuppressWarnings("serial") + public static class DatadirException extends IOException { + + public DatadirException(String msg) { + super(msg); + } + + public DatadirException(String msg, Exception e) { + super(msg, e); + } + + } + + @SuppressWarnings("serial") + public static class LogDirContentCheckException extends DatadirException { + + public LogDirContentCheckException(String msg) { + super(msg); + } + + } + + @SuppressWarnings("serial") + public static class SnapDirContentCheckException extends DatadirException { + + public SnapDirContentCheckException(String msg) { + super(msg); + } + + } + + public void setTotalLogSize(long size) { + txnLog.setTotalLogSize(size); + } + + public long getTotalLogSize() { + return txnLog.getTotalLogSize(); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapShot.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapShot.java new file mode 100644 index 0000000..daf2cd5 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapShot.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.File; +import java.io.IOException; +import java.util.Map; +import org.apache.zookeeper.server.DataTree; + +/** + * snapshot interface for the persistence layer. + * implement this interface for implementing + * snapshots. + * // TODO_MA 马中华 注释: 背景知识: ZK 内提供了一个 ZKDatabase 的数据库, 负责维护 znode 数据模型 + * // TODO_MA 马中华 注释: 持久化 ZKDatabase 的方式:定期拍摄快照 + * // TODO_MA 马中华 注释: ZKDatabase 的内部,维护了一棵树:DataTree,每个节点叫做 DataNode + */ +public interface SnapShot { + + /** + * deserialize a data tree from the last valid snapshot and + * return the last zxid that was deserialized + * @param dt the datatree to be deserialized into + * @param sessions the sessions to be deserialized into + * @return the last zxid that was deserialized from the snapshot + * @throws IOException + */ + long deserialize(DataTree dt, Map sessions) throws IOException; + + /** + * persist the datatree and the sessions into a persistence storage + * @param dt the datatree to be serialized + * @param sessions the session timeouts to be serialized + * @param name the object name to store snapshot into + * @param fsync sync the snapshot immediately after write + * @throws IOException + */ + void serialize(DataTree dt, Map sessions, File name, boolean fsync) throws IOException; + + /** + * find the most recent snapshot file + * @return the most recent snapshot file + * @throws IOException + */ + File findMostRecentSnapshot() throws IOException; + + /** + * get information of the last saved/restored snapshot + * @return info of last snapshot + */ + SnapshotInfo getLastSnapshotInfo(); + + /** + * free resources from this snapshot immediately + * @throws IOException + */ + void close() throws IOException; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java new file mode 100644 index 0000000..1813811 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapStream.java @@ -0,0 +1,313 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.zip.Adler32; +import java.util.zip.CheckedInputStream; +import java.util.zip.CheckedOutputStream; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.common.AtomicFileOutputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xerial.snappy.SnappyCodec; +import org.xerial.snappy.SnappyInputStream; +import org.xerial.snappy.SnappyOutputStream; + +/** + * Represent the Stream used in serialize and deserialize the Snapshot. + */ +public class SnapStream { + + private static final Logger LOG = LoggerFactory.getLogger(SnapStream.class); + + public static final String ZOOKEEPER_SHAPSHOT_STREAM_MODE = "zookeeper.snapshot.compression.method"; + + private static StreamMode streamMode = StreamMode + .fromString(System.getProperty(ZOOKEEPER_SHAPSHOT_STREAM_MODE, StreamMode.DEFAULT_MODE.getName())); + + static { + LOG.info("{} = {}", ZOOKEEPER_SHAPSHOT_STREAM_MODE, streamMode); + } + + public enum StreamMode { + GZIP("gz"), SNAPPY("snappy"), CHECKED(""); + + public static final StreamMode DEFAULT_MODE = CHECKED; + + private String name; + + StreamMode(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public String getFileExtension() { + return name.isEmpty() ? "" : "." + name; + } + + public static StreamMode fromString(String name) { + for(StreamMode c : values()) { + if(c.getName().compareToIgnoreCase(name) == 0) { + return c; + } + } + return DEFAULT_MODE; + } + } + + /** + * Return the CheckedInputStream based on the extension of the fileName. + * + * @param file the file the InputStream read from + * @return the specific InputStream + * @throws IOException + */ + public static CheckedInputStream getInputStream(File file) throws IOException { + FileInputStream fis = new FileInputStream(file); + InputStream is; + switch(getStreamMode(file.getName())) { + case GZIP: + is = new GZIPInputStream(fis); + break; + case SNAPPY: + is = new SnappyInputStream(fis); + break; + case CHECKED: + default: + is = new BufferedInputStream(fis); + } + return new CheckedInputStream(is, new Adler32()); + } + + /** + * Return the OutputStream based on predefined stream mode. + * + * @param file the file the OutputStream writes to + * @param fsync sync the file immediately after write + * @return the specific OutputStream + * @throws IOException + */ + public static CheckedOutputStream getOutputStream(File file, boolean fsync) throws IOException { + OutputStream fos = fsync ? new AtomicFileOutputStream(file) : new FileOutputStream(file); + OutputStream os; + switch(streamMode) { + case GZIP: + os = new GZIPOutputStream(fos); + break; + case SNAPPY: + os = new SnappyOutputStream(fos); + break; + case CHECKED: + default: + os = new BufferedOutputStream(fos); + } + return new CheckedOutputStream(os, new Adler32()); + } + + /** + * Write specific seal to the OutputArchive and close the OutputStream. + * Currently, only CheckedOutputStream will write it's checkSum to the + * end of the stream. + */ + public static void sealStream(CheckedOutputStream os, OutputArchive oa) throws IOException { + long val = os.getChecksum().getValue(); + oa.writeLong(val, "val"); + oa.writeString("/", "path"); + } + + /** + * Verify the integrity of the seal, only CheckedInputStream will verify + * the checkSum of the content. + */ + static void checkSealIntegrity(CheckedInputStream is, InputArchive ia) throws IOException { + long checkSum = is.getChecksum().getValue(); + long val = ia.readLong("val"); + ia.readString("path"); // Read and ignore "/" written by SealStream. + if(val != checkSum) { + throw new IOException("CRC corruption"); + } + } + + /** + * Verifies that the file is a valid snapshot. Snapshot may be invalid if + * it's incomplete as in a situation when the server dies while in the + * process of storing a snapshot. Any files that are improperly formated + * or corrupted are invalid. Any file that is not a snapshot is also an + * invalid snapshot. + * + * @param file file to verify + * @return true if the snapshot is valid + * @throws IOException + */ + public static boolean isValidSnapshot(File file) throws IOException { + if(file == null || Util.getZxidFromName(file.getName(), FileSnap.SNAPSHOT_FILE_PREFIX) == -1) { + return false; + } + + boolean isValid = false; + switch(getStreamMode(file.getName())) { + case GZIP: + isValid = isValidGZipStream(file); + break; + case SNAPPY: + isValid = isValidSnappyStream(file); + break; + case CHECKED: + default: + isValid = isValidCheckedStream(file); + } + return isValid; + } + + public static void setStreamMode(StreamMode mode) { + streamMode = mode; + } + + public static StreamMode getStreamMode() { + return streamMode; + } + + /** + * Detect the stream mode from file name extension + * + * @param fileName + * @return + */ + public static StreamMode getStreamMode(String fileName) { + String[] splitSnapName = fileName.split("\\."); + + // Use file extension to detect format + if(splitSnapName.length > 1) { + String mode = splitSnapName[splitSnapName.length - 1]; + return StreamMode.fromString(mode); + } + + return StreamMode.CHECKED; + } + + /** + * Certify the GZip stream integrity by checking the header + * for the GZip magic string + * + * @param f file to verify + * @return true if it has the correct GZip magic string + * @throws IOException + */ + private static boolean isValidGZipStream(File f) throws IOException { + byte[] byteArray = new byte[2]; + try(FileInputStream fis = new FileInputStream(f)) { + if(2 != fis.read(byteArray, 0, 2)) { + LOG.error("Read incorrect number of bytes from {}", f.getName()); + return false; + } + ByteBuffer bb = ByteBuffer.wrap(byteArray); + byte[] magicHeader = new byte[2]; + bb.get(magicHeader, 0, 2); + int magic = magicHeader[0] & 0xff | ((magicHeader[1] << 8) & 0xff00); + return magic == GZIPInputStream.GZIP_MAGIC; + } catch(FileNotFoundException e) { + LOG.error("Unable to open file {}", f.getName(), e); + return false; + } + } + + /** + * Certify the Snappy stream integrity by checking the header + * for the Snappy magic string + * + * @param f file to verify + * @return true if it has the correct Snappy magic string + * @throws IOException + */ + private static boolean isValidSnappyStream(File f) throws IOException { + byte[] byteArray = new byte[SnappyCodec.MAGIC_LEN]; + try(FileInputStream fis = new FileInputStream(f)) { + if(SnappyCodec.MAGIC_LEN != fis.read(byteArray, 0, SnappyCodec.MAGIC_LEN)) { + LOG.error("Read incorrect number of bytes from {}", f.getName()); + return false; + } + ByteBuffer bb = ByteBuffer.wrap(byteArray); + byte[] magicHeader = new byte[SnappyCodec.MAGIC_LEN]; + bb.get(magicHeader, 0, SnappyCodec.MAGIC_LEN); + return Arrays.equals(magicHeader, SnappyCodec.getMagicHeader()); + } catch(FileNotFoundException e) { + LOG.error("Unable to open file {}", f.getName(), e); + return false; + } + } + + /** + * Certify the Checked stream integrity by checking the header + * length and format + * + * @param f file to verify + * @return true if it has the correct header + * @throws IOException + */ + private static boolean isValidCheckedStream(File f) throws IOException { + try(RandomAccessFile raf = new RandomAccessFile(f, "r")) { + // including the header and the last / bytes + // the snapshot should be at least 10 bytes + if(raf.length() < 10) { + return false; + } + + raf.seek(raf.length() - 5); + byte[] bytes = new byte[5]; + int readlen = 0; + int l; + while(readlen < 5 && (l = raf.read(bytes, readlen, bytes.length - readlen)) >= 0) { + readlen += l; + } + if(readlen != bytes.length) { + LOG.info("Invalid snapshot {}. too short, len = {} bytes", f.getName(), readlen); + return false; + } + ByteBuffer bb = ByteBuffer.wrap(bytes); + int len = bb.getInt(); + byte b = bb.get(); + if(len != 1 || b != '/') { + LOG.info("Invalid snapshot {}. len = {}, byte = {}", f.getName(), len, (b & 0xff)); + return false; + } + } + + return true; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapshotInfo.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapshotInfo.java new file mode 100644 index 0000000..097f875 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/SnapshotInfo.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +/** + * stores the zxid (as in its file name) and the last modified timestamp + * of a snapshot file + */ +public class SnapshotInfo { + + public long zxid; + public long timestamp; + + SnapshotInfo(long zxid, long timestamp) { + this.zxid = zxid; + this.timestamp = timestamp; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/TxnLog.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/TxnLog.java new file mode 100644 index 0000000..b557208 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/TxnLog.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.Closeable; +import java.io.IOException; +import org.apache.jute.Record; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * Interface for reading transaction logs. + * + */ +public interface TxnLog extends Closeable { + + /** + * Setter for ServerStats to monitor fsync threshold exceed + * @param serverStats used to update fsyncThresholdExceedCount + */ + void setServerStats(ServerStats serverStats); + + /** + * roll the current + * log being appended to + * @throws IOException + */ + void rollLog() throws IOException; + /** + * Append a request to the transaction log + * @param hdr the transaction header + * @param r the transaction itself + * @return true iff something appended, otw false + * @throws IOException + */ + boolean append(TxnHeader hdr, Record r) throws IOException; + + /** + * Append a request to the transaction log with a digset + * @param hdr the transaction header + * @param r the transaction itself + * @param digest transaction digest + * returns true iff something appended, otw false + * @throws IOException + */ + boolean append(TxnHeader hdr, Record r, TxnDigest digest) throws IOException; + + /** + * Start reading the transaction logs + * from a given zxid + * @param zxid + * @return returns an iterator to read the + * next transaction in the logs. + * @throws IOException + */ + TxnIterator read(long zxid) throws IOException; + + /** + * the last zxid of the logged transactions. + * @return the last zxid of the logged transactions. + * @throws IOException + */ + long getLastLoggedZxid() throws IOException; + + /** + * truncate the log to get in sync with the + * leader. + * @param zxid the zxid to truncate at. + * @throws IOException + */ + boolean truncate(long zxid) throws IOException; + + /** + * the dbid for this transaction log. + * @return the dbid for this transaction log. + * @throws IOException + */ + long getDbId() throws IOException; + + /** + * commit the transaction and make sure + * they are persisted + * @throws IOException + */ + void commit() throws IOException; + + /** + * + * @return transaction log's elapsed sync time in milliseconds + */ + long getTxnLogSyncElapsedTime(); + + /** + * close the transactions logs + */ + void close() throws IOException; + + /** + * Sets the total size of all log files + */ + void setTotalLogSize(long size); + + /** + * Gets the total size of all log files + */ + long getTotalLogSize(); + + /** + * an iterating interface for reading + * transaction logs. + */ + interface TxnIterator extends Closeable { + + /** + * return the transaction header. + * @return return the transaction header. + */ + TxnHeader getHeader(); + + /** + * return the transaction record. + * @return return the transaction record. + */ + Record getTxn(); + + /** + * @return the digest associated with the transaction. + */ + TxnDigest getDigest(); + + /** + * go to the next transaction record. + * @throws IOException + */ + boolean next() throws IOException; + + /** + * close files and release the + * resources + * @throws IOException + */ + void close() throws IOException; + + /** + * Get an estimated storage space used to store transaction records + * that will return by this iterator + * @throws IOException + */ + long getStorageSize() throws IOException; + + } + +} + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/TxnLogToolkit.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/TxnLogToolkit.java new file mode 100644 index 0000000..389caea --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/TxnLogToolkit.java @@ -0,0 +1,447 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.apache.zookeeper.server.persistence.FileTxnLog.TXNLOG_MAGIC; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.Closeable; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.text.DateFormat; +import java.util.Date; +import java.util.List; +import java.util.Scanner; +import java.util.zip.Adler32; +import java.util.zip.Checksum; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.util.LogChopper; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.CreateContainerTxn; +import org.apache.zookeeper.txn.CreateTTLTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; + +public class TxnLogToolkit implements Closeable { + + static class TxnLogToolkitException extends Exception { + + private static final long serialVersionUID = 1L; + private int exitCode; + + TxnLogToolkitException(int exitCode, String message, Object... params) { + super(String.format(message, params)); + this.exitCode = exitCode; + } + + int getExitCode() { + return exitCode; + } + + } + + static class TxnLogToolkitParseException extends TxnLogToolkitException { + + private static final long serialVersionUID = 1L; + private Options options; + + TxnLogToolkitParseException(Options options, int exitCode, String message, Object... params) { + super(exitCode, message, params); + this.options = options; + } + + Options getOptions() { + return options; + } + + } + + private File txnLogFile; + private boolean recoveryMode = false; + private boolean verbose = false; + private FileInputStream txnFis; + private BinaryInputArchive logStream; + + // Recovery mode + private int crcFixed = 0; + private FileOutputStream recoveryFos; + private BinaryOutputArchive recoveryOa; + private File recoveryLogFile; + private FilePadding filePadding = new FilePadding(); + private boolean force = false; + + // chop mode + private long zxid = -1L; + + /** + * @param args Command line arguments + */ + public static void main(String[] args) throws Exception { + try (final TxnLogToolkit lt = parseCommandLine(args)) { + if (lt.isDumpMode()) { + lt.dump(new Scanner(System.in)); + lt.printStat(); + } else { + lt.chop(); + } + } catch (TxnLogToolkitParseException e) { + System.err.println(e.getMessage() + "\n"); + printHelpAndExit(e.getExitCode(), e.getOptions()); + } catch (TxnLogToolkitException e) { + System.err.println(e.getMessage()); + ServiceUtils.requestSystemExit(e.getExitCode()); + } + } + + public TxnLogToolkit( + boolean recoveryMode, + boolean verbose, + String txnLogFileName, + boolean force) throws FileNotFoundException, TxnLogToolkitException { + this.recoveryMode = recoveryMode; + this.verbose = verbose; + this.force = force; + txnLogFile = loadTxnFile(txnLogFileName); + if (recoveryMode) { + recoveryLogFile = new File(txnLogFile.toString() + ".fixed"); + if (recoveryLogFile.exists()) { + throw new TxnLogToolkitException( + ExitCode.UNEXPECTED_ERROR.getValue(), + "Recovery file %s already exists or not writable", + recoveryLogFile); + } + } + + openTxnLogFile(); + if (recoveryMode) { + openRecoveryFile(); + } + } + + public TxnLogToolkit(String txnLogFileName, String zxidName) throws TxnLogToolkitException { + txnLogFile = loadTxnFile(txnLogFileName); + zxid = Long.decode(zxidName); + } + + private File loadTxnFile(String txnLogFileName) throws TxnLogToolkitException { + File logFile = new File(txnLogFileName); + if (!logFile.exists() || !logFile.canRead()) { + throw new TxnLogToolkitException( + ExitCode.UNEXPECTED_ERROR.getValue(), + "File doesn't exist or not readable: %s", + logFile); + } + return logFile; + } + + public void dump(Scanner scanner) throws Exception { + crcFixed = 0; + + FileHeader fhdr = new FileHeader(); + fhdr.deserialize(logStream, "fileheader"); + if (fhdr.getMagic() != TXNLOG_MAGIC) { + throw new TxnLogToolkitException( + ExitCode.INVALID_INVOCATION.getValue(), + "Invalid magic number for %s", + txnLogFile.getName()); + } + System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + + " txnlog format version " + fhdr.getVersion()); + + if (recoveryMode) { + fhdr.serialize(recoveryOa, "fileheader"); + recoveryFos.flush(); + filePadding.setCurrentSize(recoveryFos.getChannel().position()); + } + + int count = 0; + while (true) { + long crcValue; + byte[] bytes; + try { + crcValue = logStream.readLong("crcvalue"); + bytes = logStream.readBuffer("txnEntry"); + } catch (EOFException e) { + System.out.println("EOF reached after " + count + " txns."); + return; + } + if (bytes.length == 0) { + // Since we preallocate, we define EOF to be an + // empty transaction + System.out.println("EOF reached after " + count + " txns."); + return; + } + Checksum crc = new Adler32(); + crc.update(bytes, 0, bytes.length); + if (crcValue != crc.getValue()) { + if (recoveryMode) { + if (!force) { + printTxn(bytes, "CRC ERROR"); + if (askForFix(scanner)) { + crcValue = crc.getValue(); + ++crcFixed; + } + } else { + crcValue = crc.getValue(); + printTxn(bytes, "CRC FIXED"); + ++crcFixed; + } + } else { + printTxn(bytes, "CRC ERROR"); + } + } + if (!recoveryMode || verbose) { + printTxn(bytes); + } + if (logStream.readByte("EOR") != 'B') { + throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "Last transaction was partial."); + } + if (recoveryMode) { + filePadding.padFile(recoveryFos.getChannel()); + recoveryOa.writeLong(crcValue, "crcvalue"); + recoveryOa.writeBuffer(bytes, "txnEntry"); + recoveryOa.writeByte((byte) 'B', "EOR"); + } + count++; + } + } + + public void chop() { + File targetFile = new File(txnLogFile.getParentFile(), txnLogFile.getName() + ".chopped" + zxid); + try (InputStream is = new BufferedInputStream(new FileInputStream(txnLogFile)); + OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile))) { + if (!LogChopper.chop(is, os, zxid)) { + throw new TxnLogToolkitException( + ExitCode.INVALID_INVOCATION.getValue(), + "Failed to chop %s", + txnLogFile.getName()); + } + } catch (Exception e) { + System.out.println("Got exception: " + e.getMessage()); + } + } + + public boolean isDumpMode() { + return zxid < 0; + } + + private boolean askForFix(Scanner scanner) throws TxnLogToolkitException { + while (true) { + System.out.print("Would you like to fix it (Yes/No/Abort) ? "); + char answer = Character.toUpperCase(scanner.next().charAt(0)); + switch (answer) { + case 'Y': + return true; + case 'N': + return false; + case 'A': + throw new TxnLogToolkitException(ExitCode.EXECUTION_FINISHED.getValue(), "Recovery aborted."); + } + } + } + + private void printTxn(byte[] bytes) throws IOException { + printTxn(bytes, ""); + } + + private void printTxn(byte[] bytes, String prefix) throws IOException { + TxnLogEntry logEntry = SerializeUtils.deserializeTxn(bytes); + TxnHeader hdr = logEntry.getHeader(); + Record txn = logEntry.getTxn(); + String txnStr = getFormattedTxnStr(txn); + String txns = String.format( + "%s session 0x%s cxid 0x%s zxid 0x%s %s %s", + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(new Date(hdr.getTime())), + Long.toHexString(hdr.getClientId()), + Long.toHexString(hdr.getCxid()), + Long.toHexString(hdr.getZxid()), + Request.op2String(hdr.getType()), + txnStr); + if (prefix != null && !"".equals(prefix.trim())) { + System.out.print(prefix + " - "); + } + if (txns.endsWith("\n")) { + System.out.print(txns); + } else { + System.out.println(txns); + } + } + + /** + * get the formatted string from the txn. + * @param txn transaction log data + * @return the formatted string + */ + private static String getFormattedTxnStr(Record txn) throws IOException { + StringBuilder txnData = new StringBuilder(); + if (txn == null) { + return txnData.toString(); + } + if (txn instanceof CreateTxn) { + CreateTxn createTxn = ((CreateTxn) txn); + txnData.append(createTxn.getPath() + "," + checkNullToEmpty(createTxn.getData())) + .append("," + createTxn.getAcl() + "," + createTxn.getEphemeral()) + .append("," + createTxn.getParentCVersion()); + } else if (txn instanceof SetDataTxn) { + SetDataTxn setDataTxn = ((SetDataTxn) txn); + txnData.append(setDataTxn.getPath() + "," + checkNullToEmpty(setDataTxn.getData())) + .append("," + setDataTxn.getVersion()); + } else if (txn instanceof CreateContainerTxn) { + CreateContainerTxn createContainerTxn = ((CreateContainerTxn) txn); + txnData.append(createContainerTxn.getPath() + "," + checkNullToEmpty(createContainerTxn.getData())) + .append("," + createContainerTxn.getAcl() + "," + createContainerTxn.getParentCVersion()); + } else if (txn instanceof CreateTTLTxn) { + CreateTTLTxn createTTLTxn = ((CreateTTLTxn) txn); + txnData.append(createTTLTxn.getPath() + "," + checkNullToEmpty(createTTLTxn.getData())) + .append("," + createTTLTxn.getAcl() + "," + createTTLTxn.getParentCVersion()) + .append("," + createTTLTxn.getTtl()); + } else if (txn instanceof MultiTxn) { + MultiTxn multiTxn = ((MultiTxn) txn); + List txnList = multiTxn.getTxns(); + for (int i = 0; i < txnList.size(); i++) { + Txn t = txnList.get(i); + if (i == 0) { + txnData.append(Request.op2String(t.getType()) + ":" + checkNullToEmpty(t.getData())); + } else { + txnData.append(";" + Request.op2String(t.getType()) + ":" + checkNullToEmpty(t.getData())); + } + } + } else { + txnData.append(txn.toString()); + } + + return txnData.toString(); + } + + private static String checkNullToEmpty(byte[] data) throws IOException { + if (data == null || data.length == 0) { + return ""; + } + + return new String(data, StandardCharsets.UTF_8); + } + + private void openTxnLogFile() throws FileNotFoundException { + txnFis = new FileInputStream(txnLogFile); + logStream = BinaryInputArchive.getArchive(txnFis); + } + + private void closeTxnLogFile() throws IOException { + if (txnFis != null) { + txnFis.close(); + } + } + + private void openRecoveryFile() throws FileNotFoundException { + recoveryFos = new FileOutputStream(recoveryLogFile); + recoveryOa = BinaryOutputArchive.getArchive(recoveryFos); + } + + private void closeRecoveryFile() throws IOException { + if (recoveryFos != null) { + recoveryFos.close(); + } + } + + private static TxnLogToolkit parseCommandLine(String[] args) throws TxnLogToolkitException, FileNotFoundException { + CommandLineParser parser = new PosixParser(); + Options options = new Options(); + + Option helpOpt = new Option("h", "help", false, "Print help message"); + options.addOption(helpOpt); + + Option recoverOpt = new Option("r", "recover", false, "Recovery mode. Re-calculate CRC for broken entries."); + options.addOption(recoverOpt); + + Option quietOpt = new Option("v", "verbose", false, "Be verbose in recovery mode: print all entries, not just fixed ones."); + options.addOption(quietOpt); + + Option dumpOpt = new Option("d", "dump", false, "Dump mode. Dump all entries of the log file with printing the content of a nodepath (default)"); + options.addOption(dumpOpt); + + Option forceOpt = new Option("y", "yes", false, "Non-interactive mode: repair all CRC errors without asking"); + options.addOption(forceOpt); + + // Chop mode options + Option chopOpt = new Option("c", "chop", false, "Chop mode. Chop txn file to a zxid."); + Option zxidOpt = new Option("z", "zxid", true, "Used with chop. Zxid to which to chop."); + options.addOption(chopOpt); + options.addOption(zxidOpt); + + try { + CommandLine cli = parser.parse(options, args); + if (cli.hasOption("help")) { + printHelpAndExit(0, options); + } + if (cli.getArgs().length < 1) { + printHelpAndExit(1, options); + } + if (cli.hasOption("chop") && cli.hasOption("zxid")) { + return new TxnLogToolkit(cli.getArgs()[0], cli.getOptionValue("zxid")); + } + return new TxnLogToolkit(cli.hasOption("recover"), cli.hasOption("verbose"), cli.getArgs()[0], cli.hasOption("yes")); + } catch (ParseException e) { + throw new TxnLogToolkitParseException(options, ExitCode.UNEXPECTED_ERROR.getValue(), e.getMessage()); + } + } + + private static void printHelpAndExit(int exitCode, Options options) { + HelpFormatter help = new HelpFormatter(); + help.printHelp(120, "TxnLogToolkit [-dhrvc] (-z )", "", options, ""); + ServiceUtils.requestSystemExit(exitCode); + } + + private void printStat() { + if (recoveryMode) { + System.out.printf("Recovery file %s has been written with %d fixed CRC error(s)%n", recoveryLogFile, crcFixed); + } + } + + @Override + public void close() throws IOException { + if (recoveryMode) { + closeRecoveryFile(); + } + closeTxnLogFile(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/Util.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/Util.java new file mode 100644 index 0000000..4e97f4c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/persistence/Util.java @@ -0,0 +1,280 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.File; +import java.io.IOException; +import java.io.Serializable; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Properties; + +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A collection of utility methods for dealing with file name parsing, + * low level I/O file operations and marshalling/unmarshalling. + */ +public class Util { + + private static final Logger LOG = LoggerFactory.getLogger(Util.class); + private static final String SNAP_DIR = "snapDir"; + private static final String LOG_DIR = "logDir"; + private static final String DB_FORMAT_CONV = "dbFormatConversion"; + + public static String makeURIString(String dataDir, String dataLogDir, String convPolicy) { + String uri = "file:" + SNAP_DIR + "=" + dataDir + ";" + LOG_DIR + "=" + dataLogDir; + if(convPolicy != null) { + uri += ";" + DB_FORMAT_CONV + "=" + convPolicy; + } + return uri.replace('\\', '/'); + } + + /** + * Given two directory files the method returns a well-formed + * logfile provider URI. This method is for backward compatibility with the + * existing code that only supports logfile persistence and expects these two + * parameters passed either on the command-line or in the configuration file. + * + * @param dataDir snapshot directory + * @param dataLogDir transaction log directory + * @return logfile provider URI + */ + public static URI makeFileLoggerURL(File dataDir, File dataLogDir) { + return URI.create(makeURIString(dataDir.getPath(), dataLogDir.getPath(), null)); + } + + public static URI makeFileLoggerURL(File dataDir, File dataLogDir, String convPolicy) { + return URI.create(makeURIString(dataDir.getPath(), dataLogDir.getPath(), convPolicy)); + } + + /** + * Creates a valid transaction log file name. + * + * @param zxid used as a file name suffix (extension) + * @return file name + */ + public static String makeLogName(long zxid) { + return FileTxnLog.LOG_FILE_PREFIX + "." + Long.toHexString(zxid); + } + + /** + * Creates a snapshot file name. + * + * @param zxid used as a suffix + * @return file name + */ + public static String makeSnapshotName(long zxid) { + return FileSnap.SNAPSHOT_FILE_PREFIX + "." + Long.toHexString(zxid) + SnapStream.getStreamMode() + .getFileExtension(); + } + + /** + * Extracts snapshot directory property value from the container. + * + * @param props properties container + * @return file representing the snapshot directory + */ + public static File getSnapDir(Properties props) { + return new File(props.getProperty(SNAP_DIR)); + } + + /** + * Extracts transaction log directory property value from the container. + * + * @param props properties container + * @return file representing the txn log directory + */ + public static File getLogDir(Properties props) { + return new File(props.getProperty(LOG_DIR)); + } + + /** + * Extracts the value of the dbFormatConversion attribute. + * + * @param props properties container + * @return value of the dbFormatConversion attribute + */ + public static String getFormatConversionPolicy(Properties props) { + return props.getProperty(DB_FORMAT_CONV); + } + + /** + * Extracts zxid from the file name. The file name should have been created + * using one of the {@link #makeLogName(long)} or {@link #makeSnapshotName(long)}. + * + * @param name the file name to parse + * @param prefix the file name prefix (snapshot or log) + * @return zxid + */ + public static long getZxidFromName(String name, String prefix) { + long zxid = -1; + String[] nameParts = name.split("\\."); + if(nameParts.length >= 2 && nameParts[0].equals(prefix)) { + try { + zxid = Long.parseLong(nameParts[1], 16); + } catch(NumberFormatException e) { + } + } + return zxid; + } + + /** + * Reads a transaction entry from the input archive. + * + * @param ia archive to read from + * @return null if the entry is corrupted or EOF has been reached; a buffer + * (possible empty) containing serialized transaction record. + * @throws IOException + */ + public static byte[] readTxnBytes(InputArchive ia) throws IOException { + try { + byte[] bytes = ia.readBuffer("txtEntry"); + // Since we preallocate, we define EOF to be an + // empty transaction + if(bytes.length == 0) { + return bytes; + } + if(ia.readByte("EOF") != 'B') { + LOG.error("Last transaction was partial."); + return null; + } + return bytes; + } catch(EOFException e) { + } + return null; + } + + /** + * Serializes transaction header and transaction data into a byte buffer. + * + * @param hdr transaction header + * @param txn transaction data + * @return serialized transaction record + * @throws IOException + */ + public static byte[] marshallTxnEntry(TxnHeader hdr, Record txn) throws IOException { + return marshallTxnEntry(hdr, txn, null); + } + + public static byte[] marshallTxnEntry(TxnHeader hdr, Record txn, TxnDigest digest) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputArchive boa = BinaryOutputArchive.getArchive(baos); + + hdr.serialize(boa, "hdr"); + if(txn != null) { + txn.serialize(boa, "txn"); + } + if(digest != null) { + digest.serialize(boa, "digest"); + } + return baos.toByteArray(); + } + + /** + * Write the serialized transaction record to the output archive. + * + * @param oa output archive + * @param bytes serialized transaction record + * @throws IOException + */ + public static void writeTxnBytes(OutputArchive oa, byte[] bytes) throws IOException { + oa.writeBuffer(bytes, "txnEntry"); + oa.writeByte((byte) 0x42, "EOR"); // 'B' + } + + /** + * Compare file file names of form "prefix.version". Sort order result + * returned in order of version. + */ + private static class DataDirFileComparator implements Comparator, Serializable { + + private static final long serialVersionUID = -2648639884525140318L; + + private String prefix; + private boolean ascending; + + public DataDirFileComparator(String prefix, boolean ascending) { + this.prefix = prefix; + this.ascending = ascending; + } + + public int compare(File o1, File o2) { + long z1 = Util.getZxidFromName(o1.getName(), prefix); + long z2 = Util.getZxidFromName(o2.getName(), prefix); + int result = z1 < z2 ? -1 : (z1 > z2 ? 1 : 0); + return ascending ? result : -result; + } + + } + + /** + * Sort the list of files. Recency as determined by the version component + * of the file name. + * + * @param files array of files + * @param prefix files not matching this prefix are assumed to have a + * version = -1) + * @param ascending true sorted in ascending order, false results in + * descending order + * @return sorted input files + */ + public static List sortDataDir(File[] files, String prefix, boolean ascending) { + if(files == null) { + return new ArrayList(0); + } + List filelist = Arrays.asList(files); + Collections.sort(filelist, new DataDirFileComparator(prefix, ascending)); + return filelist; + } + + /** + * Returns true if fileName is a log file name. + * + * @param fileName + * @return + */ + public static boolean isLogFileName(String fileName) { + return fileName.startsWith(FileTxnLog.LOG_FILE_PREFIX + "."); + } + + /** + * Returns true if fileName is a snapshot file name. + * + * @param fileName + * @return + */ + public static boolean isSnapshotFileName(String fileName) { + return fileName.startsWith(FileSnap.SNAPSHOT_FILE_PREFIX + "."); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/AckRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/AckRequestProcessor.java new file mode 100644 index 0000000..05b9003 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/AckRequestProcessor.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerMetrics; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is a very simple RequestProcessor that simply forwards a request from a + * previous stage to the leader as an ACK. + */ +class AckRequestProcessor implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(AckRequestProcessor.class); + Leader leader; + + AckRequestProcessor(Leader leader) { + this.leader = leader; + } + + /** + * Forward the request as an ACK to the leader + */ + public void processRequest(Request request) { + QuorumPeer self = leader.self; + if (self != null) { + request.logLatency(ServerMetrics.getMetrics().PROPOSAL_ACK_CREATION_LATENCY); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理 ACK + */ + leader.processAck(self.getId(), request.zxid, null); + } else { + LOG.error("Null QuorumPeer"); + } + } + + public void shutdown() { + // TODO No need to do anything + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/BufferStats.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/BufferStats.java new file mode 100644 index 0000000..0d0f4ba --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/BufferStats.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * Provides live statistics about Jute buffer usage in term of proposal and client request size. + */ +public class BufferStats { + + public static final int INIT_VALUE = -1; + + /** + * Size of the last buffer usage. + */ + private int lastBufferSize = INIT_VALUE; + + /** + * Size of the smallest buffer usage. + */ + private int minBufferSize = INIT_VALUE; + + /** + * Size of the largest buffer usage. + */ + private int maxBufferSize = INIT_VALUE; + + /** + * Size of the last buffer usage. + */ + public synchronized int getLastBufferSize() { + return lastBufferSize; + } + + /** + * Updates statistics by setting the last buffer usage size. + */ + public synchronized void setLastBufferSize(int value) { + lastBufferSize = value; + if (minBufferSize == INIT_VALUE || value < minBufferSize) { + minBufferSize = value; + } + if (value > maxBufferSize) { + maxBufferSize = value; + } + } + + /** + * Size of the smallest buffer usage. + */ + public synchronized int getMinBufferSize() { + return minBufferSize; + } + + /** + * Size of the largest buffer usage. + */ + public synchronized int getMaxBufferSize() { + return maxBufferSize; + } + + /** + * Reset statistics. + */ + public synchronized void reset() { + lastBufferSize = INIT_VALUE; + minBufferSize = INIT_VALUE; + maxBufferSize = INIT_VALUE; + } + + @Override + public synchronized String toString() { + return String.format("%d/%d/%d", lastBufferSize, minBufferSize, maxBufferSize); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/CommitProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/CommitProcessor.java new file mode 100644 index 0000000..a64a432 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/CommitProcessor.java @@ -0,0 +1,681 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.WorkerService; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooKeeperServerListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This RequestProcessor matches the incoming committed requests with the + * locally submitted requests. The trick is that locally submitted requests that + * change the state of the system will come back as incoming committed requests, + * so we need to match them up. Instead of just waiting for the committed requests, + * we process the uncommitted requests that belong to other sessions. + * + * The CommitProcessor is multi-threaded. Communication between threads is + * handled via queues, atomics, and wait/notifyAll synchronized on the + * processor. The CommitProcessor acts as a gateway for allowing requests to + * continue with the remainder of the processing pipeline. It will allow many + * read requests but only a single write request to be in flight simultaneously, + * thus ensuring that write requests are processed in transaction id order. + * + * - 1 commit processor main thread, which watches the request queues and + * assigns requests to worker threads based on their sessionId so that + * read and write requests for a particular session are always assigned + * to the same thread (and hence are guaranteed to run in order). + * - 0-N worker threads, which run the rest of the request processor pipeline + * on the requests. If configured with 0 worker threads, the primary + * commit processor thread runs the pipeline directly. + * + * Typical (default) thread counts are: on a 32 core machine, 1 commit + * processor thread and 32 worker threads. + * + * Multi-threading constraints: + * - Each session's requests must be processed in order. + * - Write requests must be processed in zxid order + * - Must ensure no race condition between writes in one session that would + * trigger a watch being set by a read request in another session + * + * The current implementation solves the third constraint by simply allowing no + * read requests to be processed in parallel with write requests. + */ +public class CommitProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(CommitProcessor.class); + + /** + * Default: numCores + */ + public static final String ZOOKEEPER_COMMIT_PROC_NUM_WORKER_THREADS = "zookeeper.commitProcessor.numWorkerThreads"; + /** + * Default worker pool shutdown timeout in ms: 5000 (5s) + */ + public static final String ZOOKEEPER_COMMIT_PROC_SHUTDOWN_TIMEOUT = "zookeeper.commitProcessor.shutdownTimeout"; + /** + * Default max read batch size: -1 to disable the feature + */ + public static final String ZOOKEEPER_COMMIT_PROC_MAX_READ_BATCH_SIZE = "zookeeper.commitProcessor.maxReadBatchSize"; + /** + * Default max commit batch size: 1 + */ + public static final String ZOOKEEPER_COMMIT_PROC_MAX_COMMIT_BATCH_SIZE = "zookeeper.commitProcessor.maxCommitBatchSize"; + + /** + * Incoming requests. + */ + protected LinkedBlockingQueue queuedRequests = new LinkedBlockingQueue(); + + /** + * Incoming requests that are waiting on a commit, contained in order of arrival + */ + protected final LinkedBlockingQueue queuedWriteRequests = new LinkedBlockingQueue<>(); + + /** + * The number of read requests currently held in all session queues + */ + private AtomicInteger numReadQueuedRequests = new AtomicInteger(0); + + /** + * The number of quorum requests currently held in all session queued + */ + private AtomicInteger numWriteQueuedRequests = new AtomicInteger(0); + + /** + * Requests that have been committed. + */ + protected final LinkedBlockingQueue committedRequests = new LinkedBlockingQueue(); + + /** + * Requests that we are holding until commit comes in. Keys represent + * session ids, each value is a linked list of the session's requests. + */ + protected final Map> pendingRequests = new HashMap<>(10000); + + /** + * The number of requests currently being processed + */ + protected final AtomicInteger numRequestsProcessing = new AtomicInteger(0); + + RequestProcessor nextProcessor; + + /** + * For testing purposes, we use a separated stopping condition for the + * outer loop. + */ + protected volatile boolean stoppedMainLoop = true; + protected volatile boolean stopped = true; + private long workerShutdownTimeoutMS; + protected WorkerService workerPool; + private Object emptyPoolSync = new Object(); + + /** + * Max number of reads to process from queuedRequests before switching to + * processing commits. If the value is negative, we switch whenever we have + * a local write, and pending commits. + * A high read batch size will delay commit processing causing us to + * serve stale data. + */ + private static volatile int maxReadBatchSize; + /** + * Max number of commits to process before processing reads. We will try to + * process as many remote/local commits as we can till we reach this + * count. + * A high commit batch size will delay reads while processing more commits. + * A low commit batch size will favor reads. + */ + private static volatile int maxCommitBatchSize; + + /** + * This flag indicates whether we need to wait for a response to come back from the + * leader or we just let the sync operation flow through like a read. The flag will + * be false if the CommitProcessor is in a Leader pipeline. + */ + boolean matchSyncs; + + public CommitProcessor(RequestProcessor nextProcessor, String id, boolean matchSyncs, + ZooKeeperServerListener listener) { + super("CommitProcessor:" + id, listener); + this.nextProcessor = nextProcessor; + this.matchSyncs = matchSyncs; + } + + private boolean isProcessingRequest() { + return numRequestsProcessing.get() != 0; + } + + protected boolean needCommit(Request request) { + switch(request.type) { + + // TODO_MA 注释: 事务请求才需要提交 + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + case OpCode.delete: + case OpCode.deleteContainer: + case OpCode.setData: + case OpCode.reconfig: + case OpCode.multi: + case OpCode.setACL: + case OpCode.check: + return true; + case OpCode.sync: + return matchSyncs; + case OpCode.createSession: + case OpCode.closeSession: + return !request.isLocalSession(); + + // TODO_MA 注释: 其他分非事务请求不需要提交动作 + default: + return false; + } + } + + @Override + public void run() { + try { + /* + * In each iteration of the following loop we process at most + * requestsToProcess requests of queuedRequests. We have to limit + * the number of request we poll from queuedRequests, since it is + * possible to endlessly poll read requests from queuedRequests, and + * that will lead to a starvation of non-local committed requests. + */ + int requestsToProcess = 0; + boolean commitIsWaiting = false; + do { + /* + * Since requests are placed in the queue before being sent to + * the leader, if commitIsWaiting = true, the commit belongs to + * the first update operation in the queuedRequests or to a + * request from a client on another server (i.e., the order of + * the following two lines is important!). + */ + commitIsWaiting = !committedRequests.isEmpty(); + requestsToProcess = queuedRequests.size(); + + // Avoid sync if we have something to do + if(requestsToProcess == 0 && !commitIsWaiting) { + + // Waiting for requests to process + synchronized(this) { + while(!stopped && requestsToProcess == 0 && !commitIsWaiting) { + wait(); + commitIsWaiting = !committedRequests.isEmpty(); + requestsToProcess = queuedRequests.size(); + } + } + } + + ServerMetrics.getMetrics().READS_QUEUED_IN_COMMIT_PROCESSOR.add(numReadQueuedRequests.get()); + ServerMetrics.getMetrics().WRITES_QUEUED_IN_COMMIT_PROCESSOR.add(numWriteQueuedRequests.get()); + ServerMetrics.getMetrics().COMMITS_QUEUED_IN_COMMIT_PROCESSOR.add(committedRequests.size()); + + long time = Time.currentElapsedTime(); + + /* + * Processing up to requestsToProcess requests from the incoming + * queue (queuedRequests). If maxReadBatchSize is set then no + * commits will be processed until maxReadBatchSize number of + * reads are processed (or no more reads remain in the queue). + * After the loop a single committed request is processed if + * one is waiting (or a batch of commits if maxCommitBatchSize + * is set). + */ + Request request; + int readsProcessed = 0; + while(!stopped + && requestsToProcess > 0 + && (maxReadBatchSize < 0 || readsProcessed <= maxReadBatchSize) + && (request = queuedRequests.poll()) != null) { + requestsToProcess--; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + if(needCommit(request) || pendingRequests.containsKey(request.sessionId)) { + // Add request to pending + Deque requests = pendingRequests + .computeIfAbsent(request.sessionId, sid -> new ArrayDeque<>()); + requests.addLast(request); + ServerMetrics.getMetrics().REQUESTS_IN_SESSION_QUEUE.add(requests.size()); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + else { + readsProcessed++; + numReadQueuedRequests.decrementAndGet(); + sendToNextProcessor(request); + } + /* + * Stop feeding the pool if there is a local pending update + * and a committed request that is ready. Once we have a + * pending request with a waiting committed request, we know + * we can process the committed one. This is because commits + * for local requests arrive in the order they appeared in + * the queue, so if we have a pending request and a + * committed request, the committed request must be for that + * pending write or for a write originating at a different + * server. We skip this if maxReadBatchSize is set. + */ + if(maxReadBatchSize < 0 && !pendingRequests.isEmpty() && !committedRequests.isEmpty()) { + /* + * We set commitIsWaiting so that we won't check + * committedRequests again. + */ + commitIsWaiting = true; + break; + } + } + ServerMetrics.getMetrics().READS_ISSUED_IN_COMMIT_PROC.add(readsProcessed); + + if(!commitIsWaiting) { + commitIsWaiting = !committedRequests.isEmpty(); + } + + /* + * Handle commits, if any. + */ + if(commitIsWaiting && !stopped) { + /* + * Drain outstanding reads + */ + waitForEmptyPool(); + + if(stopped) { + return; + } + + int commitsToProcess = maxCommitBatchSize; + + /* + * Loop through all the commits, and try to drain them. + */ + Set queuesToDrain = new HashSet<>(); + long startWriteTime = Time.currentElapsedTime(); + int commitsProcessed = 0; + + while(commitIsWaiting && !stopped && commitsToProcess > 0) { + + // Process committed head + request = committedRequests.peek(); + + /* + * Check if this is a local write request is pending, + * if so, update it with the committed info. If the commit matches + * the first write queued in the blockedRequestQueue, we know this is + * a commit for a local write, as commits are received in order. Else + * it must be a commit for a remote write. + */ + if(!queuedWriteRequests.isEmpty() && queuedWriteRequests + .peek().sessionId == request.sessionId && queuedWriteRequests + .peek().cxid == request.cxid) { + /* + * Commit matches the earliest write in our write queue. + */ + Deque sessionQueue = pendingRequests.get(request.sessionId); + ServerMetrics.getMetrics().PENDING_SESSION_QUEUE_SIZE.add(pendingRequests.size()); + if(sessionQueue == null || sessionQueue.isEmpty() || !needCommit(sessionQueue.peek())) { + /* + * Can't process this write yet. + * Either there are reads pending in this session, or we + * haven't gotten to this write yet. + */ + break; + } else { + ServerMetrics.getMetrics().REQUESTS_IN_SESSION_QUEUE.add(sessionQueue.size()); + // If session queue != null, then it is also not empty. + Request topPending = sessionQueue.poll(); + /* + * Generally, we want to send to the next processor our version of the request, + * since it contains the session information that is needed for post update processing. + * In more details, when a request is in the local queue, there is (or could be) a client + * attached to this server waiting for a response, and there is other bookkeeping of + * requests that are outstanding and have originated from this server + * (e.g., for setting the max outstanding requests) - we need to update this info when an + * outstanding request completes. Note that in the other case, the operation + * originated from a different server and there is no local bookkeeping or a local client + * session that needs to be notified. + */ + topPending.setHdr(request.getHdr()); + topPending.setTxn(request.getTxn()); + topPending.setTxnDigest(request.getTxnDigest()); + topPending.zxid = request.zxid; + topPending.commitRecvTime = request.commitRecvTime; + request = topPending; + // Only decrement if we take a request off the queue. + numWriteQueuedRequests.decrementAndGet(); + queuedWriteRequests.poll(); + queuesToDrain.add(request.sessionId); + } + } + /* + * Pull the request off the commit queue, now that we are going + * to process it. + */ + committedRequests.remove(); + commitsToProcess--; + commitsProcessed++; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + // Process the write inline. + processWrite(request); + + commitIsWaiting = !committedRequests.isEmpty(); + } + ServerMetrics.getMetrics().WRITE_BATCH_TIME_IN_COMMIT_PROCESSOR + .add(Time.currentElapsedTime() - startWriteTime); + ServerMetrics.getMetrics().WRITES_ISSUED_IN_COMMIT_PROC.add(commitsProcessed); + + /* + * Process following reads if any, remove session queue(s) if empty. + */ + readsProcessed = 0; + for(Long sessionId : queuesToDrain) { + Deque sessionQueue = pendingRequests.get(sessionId); + int readsAfterWrite = 0; + while(!stopped && !sessionQueue.isEmpty() && !needCommit(sessionQueue.peek())) { + numReadQueuedRequests.decrementAndGet(); + sendToNextProcessor(sessionQueue.poll()); + readsAfterWrite++; + } + ServerMetrics.getMetrics().READS_AFTER_WRITE_IN_SESSION_QUEUE.add(readsAfterWrite); + readsProcessed += readsAfterWrite; + + // Remove empty queues + if(sessionQueue.isEmpty()) { + pendingRequests.remove(sessionId); + } + } + ServerMetrics.getMetrics().SESSION_QUEUES_DRAINED.add(queuesToDrain.size()); + ServerMetrics.getMetrics().READ_ISSUED_FROM_SESSION_QUEUE.add(readsProcessed); + } + + ServerMetrics.getMetrics().COMMIT_PROCESS_TIME.add(Time.currentElapsedTime() - time); + endOfIteration(); + } while(!stoppedMainLoop); + } catch(Throwable e) { + handleException(this.getName(), e); + } + LOG.info("CommitProcessor exited loop!"); + } + + //for test only + protected void endOfIteration() { + + } + + protected void waitForEmptyPool() throws InterruptedException { + int numRequestsInProcess = numRequestsProcessing.get(); + if(numRequestsInProcess != 0) { + ServerMetrics.getMetrics().CONCURRENT_REQUEST_PROCESSING_IN_COMMIT_PROCESSOR.add(numRequestsInProcess); + } + + long startWaitTime = Time.currentElapsedTime(); + synchronized(emptyPoolSync) { + while((!stopped) && isProcessingRequest()) { + emptyPoolSync.wait(); + } + } + ServerMetrics.getMetrics().TIME_WAITING_EMPTY_POOL_IN_COMMIT_PROCESSOR_READ + .add(Time.currentElapsedTime() - startWaitTime); + } + + @Override + public void start() { + int numCores = Runtime.getRuntime().availableProcessors(); + int numWorkerThreads = Integer.getInteger(ZOOKEEPER_COMMIT_PROC_NUM_WORKER_THREADS, numCores); + workerShutdownTimeoutMS = Long.getLong(ZOOKEEPER_COMMIT_PROC_SHUTDOWN_TIMEOUT, 5000); + + initBatchSizes(); + + LOG.info("Configuring CommitProcessor with {} worker threads.", numWorkerThreads > 0 ? numWorkerThreads : "no"); + if(workerPool == null) { + workerPool = new WorkerService("CommitProcWork", numWorkerThreads, true); + } + stopped = false; + stoppedMainLoop = false; + super.start(); + } + + /** + * Schedule final request processing; if a worker thread pool is not being + * used, processing is done directly by this thread. + */ + private void sendToNextProcessor(Request request) { + numRequestsProcessing.incrementAndGet(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + workerPool.schedule(new CommitWorkRequest(request), request.sessionId); + } + + private void processWrite(Request request) throws RequestProcessorException { + processCommitMetrics(request, true); + + long timeBeforeFinalProc = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: nextProcessor = ToBeAppliedRequestProcessor + */ + nextProcessor.processRequest(request); + ServerMetrics.getMetrics().WRITE_FINAL_PROC_TIME.add(Time.currentElapsedTime() - timeBeforeFinalProc); + } + + private static void initBatchSizes() { + maxReadBatchSize = Integer.getInteger(ZOOKEEPER_COMMIT_PROC_MAX_READ_BATCH_SIZE, -1); + maxCommitBatchSize = Integer.getInteger(ZOOKEEPER_COMMIT_PROC_MAX_COMMIT_BATCH_SIZE, 1); + + if(maxCommitBatchSize <= 0) { + String errorMsg = "maxCommitBatchSize must be positive, was " + maxCommitBatchSize; + throw new IllegalArgumentException(errorMsg); + } + + LOG.info("Configuring CommitProcessor with readBatchSize {} commitBatchSize {}", maxReadBatchSize, + maxCommitBatchSize); + } + + private static void processCommitMetrics(Request request, boolean isWrite) { + if(isWrite) { + if(request.commitProcQueueStartTime != -1 && request.commitRecvTime != -1) { + // Locally issued writes. + long currentTime = Time.currentElapsedTime(); + ServerMetrics.getMetrics().WRITE_COMMITPROC_TIME.add(currentTime - request.commitProcQueueStartTime); + ServerMetrics.getMetrics().LOCAL_WRITE_COMMITTED_TIME.add(currentTime - request.commitRecvTime); + } else if(request.commitRecvTime != -1) { + // Writes issued by other servers. + ServerMetrics.getMetrics().SERVER_WRITE_COMMITTED_TIME + .add(Time.currentElapsedTime() - request.commitRecvTime); + } + } else { + if(request.commitProcQueueStartTime != -1) { + ServerMetrics.getMetrics().READ_COMMITPROC_TIME + .add(Time.currentElapsedTime() - request.commitProcQueueStartTime); + } + } + } + + public static int getMaxReadBatchSize() { + return maxReadBatchSize; + } + + public static int getMaxCommitBatchSize() { + return maxCommitBatchSize; + } + + public static void setMaxReadBatchSize(int size) { + maxReadBatchSize = size; + LOG.info("Configuring CommitProcessor with readBatchSize {}", maxReadBatchSize); + } + + public static void setMaxCommitBatchSize(int size) { + if(size > 0) { + maxCommitBatchSize = size; + LOG.info("Configuring CommitProcessor with commitBatchSize {}", maxCommitBatchSize); + } + } + + /** + * CommitWorkRequest is a small wrapper class to allow + * downstream processing to be run using the WorkerService + */ + private class CommitWorkRequest extends WorkerService.WorkRequest { + + private final Request request; + + CommitWorkRequest(Request request) { + this.request = request; + } + + @Override + public void cleanup() { + if(!stopped) { + LOG.error("Exception thrown by downstream processor, unable to continue."); + CommitProcessor.this.halt(); + } + } + + public void doWork() throws RequestProcessorException { + try { + processCommitMetrics(request, needCommit(request)); + + long timeBeforeFinalProc = Time.currentElapsedTime(); + nextProcessor.processRequest(request); + if(needCommit(request)) { + ServerMetrics.getMetrics().WRITE_FINAL_PROC_TIME.add(Time.currentElapsedTime() - timeBeforeFinalProc); + } else { + ServerMetrics.getMetrics().READ_FINAL_PROC_TIME.add(Time.currentElapsedTime() - timeBeforeFinalProc); + } + + } finally { + + if(numRequestsProcessing.decrementAndGet() == 0) { + wakeupOnEmpty(); + } + } + } + + } + + @SuppressFBWarnings("NN_NAKED_NOTIFY") + private synchronized void wakeup() { + notifyAll(); + } + + private void wakeupOnEmpty() { + synchronized(emptyPoolSync) { + emptyPoolSync.notifyAll(); + } + } + + public void commit(Request request) { + if(stopped || request == null) { + return; + } + LOG.debug("Committing request:: {}", request); + request.commitRecvTime = Time.currentElapsedTime(); + ServerMetrics.getMetrics().COMMITS_QUEUED.add(1); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + committedRequests.add(request); + wakeup(); + } + + @Override + public void processRequest(Request request) { + if(stopped) { + return; + } + LOG.debug("Processing request:: {}", request); + request.commitProcQueueStartTime = Time.currentElapsedTime(); + + // TODO_MA 注释: 入队列 + queuedRequests.add(request); + + // TODO_MA 注释: 判断是否需要提交 + // If the request will block, add it to the queue of blocking requests + if(needCommit(request)) { + + // TODO_MA 注释: 加入队列 + queuedWriteRequests.add(request); + numWriteQueuedRequests.incrementAndGet(); + } else { + numReadQueuedRequests.incrementAndGet(); + } + wakeup(); + } + + private void halt() { + stoppedMainLoop = true; + stopped = true; + wakeupOnEmpty(); + wakeup(); + queuedRequests.clear(); + if(workerPool != null) { + workerPool.stop(); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + + halt(); + + if(workerPool != null) { + workerPool.join(workerShutdownTimeoutMS); + } + + if(nextProcessor != null) { + nextProcessor.shutdown(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Election.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Election.java new file mode 100644 index 0000000..5810a53 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Election.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +public interface Election { + + Vote lookForLeader() throws InterruptedException; + void shutdown(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FastLeaderElection.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FastLeaderElection.java new file mode 100644 index 0000000..cc1e843 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FastLeaderElection.java @@ -0,0 +1,1343 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.quorum.QuorumCnxManager.Message; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of leader election using TCP. It uses an object of the class + * QuorumCnxManager to manage connections. Otherwise, the algorithm is push-based + * as with the other UDP implementations. + * + * There are a few parameters that can be tuned to change its behavior. First, + * finalizeWait determines the amount of time to wait until deciding upon a leader. + * This is part of the leader election algorithm. + */ + +public class FastLeaderElection implements Election { + + private static final Logger LOG = LoggerFactory.getLogger(FastLeaderElection.class); + + /** + * Determine how much time a process has to wait + * once it believes that it has reached the end of + * leader election. + */ + static final int finalizeWait = 200; + + /** + * Upper bound on the amount of time between two consecutive + * notification checks. This impacts the amount of time to get + * the system up again after long partitions. Currently 60 seconds. + */ + + private static int maxNotificationInterval = 60000; + + /** + * Lower bound for notification check. The observer don't need to use + * the same lower bound as participant members + */ + private static int minNotificationInterval = finalizeWait; + + /** + * Minimum notification interval, default is equal to finalizeWait + */ + public static final String MIN_NOTIFICATION_INTERVAL = "zookeeper.fastleader.minNotificationInterval"; + + /** + * Maximum notification interval, default is 60s + */ + public static final String MAX_NOTIFICATION_INTERVAL = "zookeeper.fastleader.maxNotificationInterval"; + + static { + minNotificationInterval = Integer.getInteger(MIN_NOTIFICATION_INTERVAL, minNotificationInterval); + LOG.info("{}={}", MIN_NOTIFICATION_INTERVAL, minNotificationInterval); + maxNotificationInterval = Integer.getInteger(MAX_NOTIFICATION_INTERVAL, maxNotificationInterval); + LOG.info("{}={}", MAX_NOTIFICATION_INTERVAL, maxNotificationInterval); + } + + /** + * Connection manager. Fast leader election uses TCP for + * communication between peers, and QuorumCnxManager manages + * such connections. + */ + + QuorumCnxManager manager; + + private SyncedLearnerTracker leadingVoteSet; + + /** + * Notifications are messages that let other peers know that + * a given peer has changed its vote, either because it has + * joined leader election or because it learned of another + * peer with higher zxid or same zxid and higher server id + */ + + public static class Notification { + /* + * Format version, introduced in 3.4.6 + */ + + public static final int CURRENTVERSION = 0x2; + int version; + + /* + * Proposed leader + */ long leader; + + /* + * zxid of the proposed leader + */ long zxid; + + /* + * Epoch + */ long electionEpoch; + + /* + * current state of sender + */ QuorumPeer.ServerState state; + + /* + * Address of sender + */ long sid; + + QuorumVerifier qv; + /* + * epoch of the proposed leader + */ long peerEpoch; + + } + + static byte[] dummyData = new byte[0]; + + /** + * Messages that a peer wants to send to other peers. + * These messages can be both Notifications and Acks + * of reception of notification. + */ + public static class ToSend { + + enum mType { + crequest, challenge, notification, ack + } + + ToSend(mType type, long leader, long zxid, long electionEpoch, ServerState state, long sid, long peerEpoch, + byte[] configData) { + + this.leader = leader; + this.zxid = zxid; + this.electionEpoch = electionEpoch; + this.state = state; + this.sid = sid; + this.peerEpoch = peerEpoch; + this.configData = configData; + } + + /* + * Proposed leader in the case of notification + */ long leader; + + /* + * id contains the tag for acks, and zxid for notifications + */ long zxid; + + /* + * Epoch + */ long electionEpoch; + + /* + * Current state; + */ QuorumPeer.ServerState state; + + /* + * Address of recipient + */ long sid; + + /* + * Used to send a QuorumVerifier (configuration info) + */ byte[] configData = dummyData; + + /* + * Leader epoch + */ long peerEpoch; + + } + + LinkedBlockingQueue sendqueue; + LinkedBlockingQueue recvqueue; + + /** + * Multi-threaded implementation of message handler. Messenger + * implements two sub-classes: WorkReceiver and WorkSender. The + * functionality of each is obvious from the name. Each of these + * spawns a new thread. + */ + + protected class Messenger { + + /** + * Receives messages from instance of QuorumCnxManager on + * method run(), and processes such messages. + */ + + class WorkerReceiver extends ZooKeeperThread { + + volatile boolean stop; + QuorumCnxManager manager; + + WorkerReceiver(QuorumCnxManager manager) { + super("WorkerReceiver"); + this.stop = false; + this.manager = manager; + } + + public void run() { + + Message response; + while(!stop) { + // Sleeps on receive + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从 recevQueue 中获取到一张投票(Message) + */ + response = manager.pollRecvQueue(3000, TimeUnit.MILLISECONDS); + if(response == null) { + continue; + } + + final int capacity = response.buffer.capacity(); + + // The current protocol and two previous generations all send at least 28 bytes + if(capacity < 28) { + LOG.error("Got a short response from server {}: {}", response.sid, capacity); + continue; + } + + // this is the backwardCompatibility mode in place before ZK-107 + // It is for a version of the protocol in which we didn't send peer epoch + // With peer epoch and version the message became 40 bytes + boolean backCompatibility28 = (capacity == 28); + + // this is the backwardCompatibility mode for no version information + boolean backCompatibility40 = (capacity == 40); + + response.buffer.clear(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 构建了一个空的 Notification + */ + // Instantiate Notification and set its attributes + Notification n = new Notification(); + + int rstate = response.buffer.getInt(); + long rleader = response.buffer.getLong(); + long rzxid = response.buffer.getLong(); + long relectionEpoch = response.buffer.getLong(); + long rpeerepoch; + + int version = 0x0; + QuorumVerifier rqv = null; + + try { + if(!backCompatibility28) { + rpeerepoch = response.buffer.getLong(); + if(!backCompatibility40) { + /* + * Version added in 3.4.6 + */ + + version = response.buffer.getInt(); + } else { + LOG.info("Backward compatibility mode (36 bits), server id: {}", response.sid); + } + } else { + LOG.info("Backward compatibility mode (28 bits), server id: {}", response.sid); + rpeerepoch = ZxidUtils.getEpochFromZxid(rzxid); + } + + // check if we have a version that includes config. If so extract config info from message. + if(version > 0x1) { + int configLength = response.buffer.getInt(); + + // we want to avoid errors caused by the allocation of a byte array with negative length + // (causing NegativeArraySizeException) or huge length (causing e.g. OutOfMemoryError) + if(configLength < 0 || configLength > capacity) { + throw new IOException(String.format( + "Invalid configLength in notification message! sid=%d, capacity=%d, version=%d, configLength=%d", + response.sid, capacity, version, configLength)); + } + + byte[] b = new byte[configLength]; + response.buffer.get(b); + + synchronized(self) { + try { + rqv = self.configFromString(new String(b)); + QuorumVerifier curQV = self.getQuorumVerifier(); + if(rqv.getVersion() > curQV.getVersion()) { + LOG.info("{} Received version: {} my version: {}", self.getId(), + Long.toHexString(rqv.getVersion()), + Long.toHexString(self.getQuorumVerifier().getVersion())); + if(self.getPeerState() == ServerState.LOOKING) { + LOG.debug("Invoking processReconfig(), state: {}", self.getServerState()); + self.processReconfig(rqv, null, null, false); + if(!rqv.equals(curQV)) { + LOG.info("restarting leader election"); + self.shuttingDownLE = true; + self.getElectionAlg().shutdown(); + + break; + } + } else { + LOG.debug("Skip processReconfig(), state: {}", self.getServerState()); + } + } + } catch(IOException | ConfigException e) { + LOG.error("Something went wrong while processing config received from {}", + response.sid); + } + } + } else { + LOG.info("Backward compatibility mode (before reconfig), server id: {}", response.sid); + } + } catch(BufferUnderflowException | IOException e) { + LOG.warn( + "Skipping the processing of a partial / malformed response message sent by sid={} (message length: {})", + response.sid, capacity, e); + continue; + } + /* + * If it is from a non-voting server (such as an observer or + * a non-voting follower), respond right away. + */ + if(!validVoter(response.sid)) { + Vote current = self.getCurrentVote(); + QuorumVerifier qv = self.getQuorumVerifier(); + ToSend notmsg = new ToSend(ToSend.mType.notification, current.getId(), current.getZxid(), + logicalclock.get(), self.getPeerState(), response.sid, current.getPeerEpoch(), + qv.toString().getBytes()); + + sendqueue.offer(notmsg); + } else { + // Receive new message + LOG.debug("Receive new notification message. My id = {}", self.getId()); + + // State of peer that sent this message + QuorumPeer.ServerState ackstate = QuorumPeer.ServerState.LOOKING; + switch(rstate) { + case 0: + ackstate = QuorumPeer.ServerState.LOOKING; + break; + case 1: + ackstate = QuorumPeer.ServerState.FOLLOWING; + break; + case 2: + ackstate = QuorumPeer.ServerState.LEADING; + break; + case 3: + ackstate = QuorumPeer.ServerState.OBSERVING; + break; + default: + continue; + } + + // TODO_MA 注释: 从 message 中拿到 选票的各种,经过校验之后,全部设置到 Notification 中 + n.leader = rleader; + n.zxid = rzxid; + n.electionEpoch = relectionEpoch; + n.state = ackstate; + n.sid = response.sid; + n.peerEpoch = rpeerepoch; + n.version = version; + n.qv = rqv; + /* + * Print notification info + */ + LOG.info( + "Notification: my state:{}; n.sid:{}, n.state:{}, n.leader:{}, n.round:0x{}, " + "n.peerEpoch:0x{}, n.zxid:0x{}, message format version:0x{}, n.config version:0x{}", + self.getPeerState(), n.sid, n.state, n.leader, Long.toHexString(n.electionEpoch), + Long.toHexString(n.peerEpoch), Long.toHexString(n.zxid), Long.toHexString(n.version), + (n.qv != null ? (Long.toHexString(n.qv.getVersion())) : "0")); + + /* + * If this server is looking, then send proposed leader + */ + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果对方服务器也是 looking 状态,证明对方也在执行选举 + * 此时,将构建好的 Notification 加入到 recvqueue 队列中 + */ + if(self.getPeerState() == QuorumPeer.ServerState.LOOKING) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 到此为止,可以这么理解: + * 一台服务器终于把一张票发送给了另外一台服务器 + */ + recvqueue.offer(n); + + /* + * Send a notification back if the peer that sent this + * message is also looking and its logical clock is + * lagging behind. + */ + if((ackstate == QuorumPeer.ServerState.LOOKING) && (n.electionEpoch < logicalclock + .get())) { + Vote v = getVote(); + QuorumVerifier qv = self.getQuorumVerifier(); + ToSend notmsg = new ToSend(ToSend.mType.notification, v.getId(), v.getZxid(), + logicalclock.get(), self.getPeerState(), response.sid, v.getPeerEpoch(), + qv.toString().getBytes()); + sendqueue.offer(notmsg); + } + } else { + /* + * If this server is not looking, but the one that sent the ack + * is looking, then send back what it believes to be the leader. + */ + Vote current = self.getCurrentVote(); + if(ackstate == QuorumPeer.ServerState.LOOKING) { + if(self.leader != null) { + if(leadingVoteSet != null) { + self.leader.setLeadingVoteSet(leadingVoteSet); + leadingVoteSet = null; + } + self.leader.reportLookingSid(response.sid); + } + + + LOG.debug( + "Sending new notification. My id ={} recipient={} zxid=0x{} leader={} config version = {}", + self.getId(), response.sid, Long.toHexString(current.getZxid()), + current.getId(), Long.toHexString(self.getQuorumVerifier().getVersion())); + + QuorumVerifier qv = self.getQuorumVerifier(); + ToSend notmsg = new ToSend(ToSend.mType.notification, current.getId(), + current.getZxid(), current.getElectionEpoch(), self.getPeerState(), + response.sid, current.getPeerEpoch(), qv.toString().getBytes()); + sendqueue.offer(notmsg); + } + } + } + } catch(InterruptedException e) { + LOG.warn("Interrupted Exception while waiting for new message", e); + } + } + LOG.info("WorkerReceiver is down"); + } + + } + + /** + * This worker simply dequeues a message to send and + * and queues it on the manager's queue. + */ + + class WorkerSender extends ZooKeeperThread { + + volatile boolean stop; + QuorumCnxManager manager; + + WorkerSender(QuorumCnxManager manager) { + super("WorkerSender"); + this.stop = false; + this.manager = manager; + } + + public void run() { + while(!stop) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 获取选票 ToSend 对象 + */ + ToSend m = sendqueue.poll(3000, TimeUnit.MILLISECONDS); + if(m == null) { + continue; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送选票给所有服务器节点 + */ + process(m); + } catch(InterruptedException e) { + break; + } + } + LOG.info("WorkerSender is down"); + } + + /** + * Called by run() once there is a new message to send. + * + * @param m message to send + */ + void process(ToSend m) { + + // TODO_MA 注释: 将 ToSend 构建成 ByteBuffer + ByteBuffer requestBuffer = buildMsg(m.state.ordinal(), m.leader, m.zxid, m.electionEpoch, m.peerEpoch, + m.configData); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 执行 ByteBuffer 的发送 + * m.sid = 对方服务器的 myid + */ + manager.toSend(m.sid, requestBuffer); + } + + } + + WorkerSender ws; + WorkerReceiver wr; + Thread wsThread = null; + Thread wrThread = null; + + /** + * Constructor of class Messenger. + * + * @param manager Connection manager + */ + Messenger(QuorumCnxManager manager) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.ws = new WorkerSender(manager); + this.wsThread = new Thread(this.ws, "WorkerSender[myid=" + self.getId() + "]"); + this.wsThread.setDaemon(true); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.wr = new WorkerReceiver(manager); + this.wrThread = new Thread(this.wr, "WorkerReceiver[myid=" + self.getId() + "]"); + this.wrThread.setDaemon(true); + } + + /** + * Starts instances of WorkerSender and WorkerReceiver + */ + void start() { + + // TODO_MA 注释: + this.wsThread.start(); + + // TODO_MA 注释: + this.wrThread.start(); + } + + /** + * Stops instances of WorkerSender and WorkerReceiver + */ + void halt() { + this.ws.stop = true; + this.wr.stop = true; + } + + } + + QuorumPeer self; + Messenger messenger; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 逻辑时钟 = 选举轮次! + * 每个节点,在执行第一轮选举的时候,logicalclock 这个值都是 1 + * - + * epoch 靠齐! epoch 叫做 任期(康熙 乾隆 ) + * 上一个 leader 死掉了,下一个leader 被选举出来,则这个 epoch +1 + * 但凡发现,我们已经进行到了 logicalclock = 6, 则 logicalclock = 5 的所有票都作废! + * 选票PK : epoch + zxid + myid + */ + AtomicLong logicalclock = new AtomicLong(); /* Election instance */ + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 当前服务器节点,推举谁成为 leader , + * 这三个信息,就是这个 leader 的信息 + */ + long proposedLeader; // TODO_MA 注释: myid + long proposedZxid; // TODO_MA 注释: zxid + long proposedEpoch; // TODO_MA 注释: 逻辑时钟 = 选举伦次 + + // TODO_MA 注释: 每次从对方接受到一张票的时候,就会执行一次 选票的PK + // TODO_MA 注释: 如果对方的票更优,则把对方票的信息,存储到这三个成员变量 + // TODO_MA 注释: 直到选举结束了之后,这三个成员变量,都会被存储到 Vote 中 + + /** + * Returns the current vlue of the logical clock counter + */ + public long getLogicalClock() { + return logicalclock.get(); + } + + static ByteBuffer buildMsg(int state, long leader, long zxid, long electionEpoch, long epoch) { + byte[] requestBytes = new byte[40]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + + /* + * Building notification packet to send, this is called directly only in tests + */ + + requestBuffer.clear(); + requestBuffer.putInt(state); + requestBuffer.putLong(leader); + requestBuffer.putLong(zxid); + requestBuffer.putLong(electionEpoch); + requestBuffer.putLong(epoch); + requestBuffer.putInt(0x1); + + return requestBuffer; + } + + static ByteBuffer buildMsg(int state, long leader, long zxid, long electionEpoch, long epoch, byte[] configData) { + byte[] requestBytes = new byte[44 + configData.length]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + + /* + * Building notification packet to send + */ + + requestBuffer.clear(); + requestBuffer.putInt(state); + requestBuffer.putLong(leader); + requestBuffer.putLong(zxid); + requestBuffer.putLong(electionEpoch); + requestBuffer.putLong(epoch); + requestBuffer.putInt(Notification.CURRENTVERSION); + requestBuffer.putInt(configData.length); + requestBuffer.put(configData); + + return requestBuffer; + } + + /** + * Constructor of FastLeaderElection. It takes two parameters, one + * is the QuorumPeer object that instantiated this object, and the other + * is the connection manager. Such an object should be created only once + * by each peer during an instance of the ZooKeeper service. + * + * @param self QuorumPeer that created this object + * @param manager Connection manager + */ + public FastLeaderElection(QuorumPeer self, QuorumCnxManager manager) { + this.stop = false; + this.manager = manager; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + starter(self, manager); + } + + /** + * This method is invoked by the constructor. Because it is a + * part of the starting procedure of the object that must be on + * any constructor of this class, it is probably best to keep as + * a separate method. As we have a single constructor currently, + * it is not strictly necessary to have it separate. + * + * @param self QuorumPeer that created this object + * @param manager Connection manager + */ + private void starter(QuorumPeer self, QuorumCnxManager manager) { + this.self = self; + proposedLeader = -1; + proposedZxid = -1; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + sendqueue = new LinkedBlockingQueue(); + recvqueue = new LinkedBlockingQueue(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.messenger = new Messenger(manager); + } + + /** + * This method starts the sender and receiver threads. + */ + public void start() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.messenger.start(); + } + + private void leaveInstance(Vote v) { + LOG.debug("About to leave FLE instance: leader={}, zxid=0x{}, my id={}, my state={}", v.getId(), + Long.toHexString(v.getZxid()), self.getId(), self.getPeerState()); + recvqueue.clear(); + } + + public QuorumCnxManager getCnxManager() { + return manager; + } + + volatile boolean stop; + + public void shutdown() { + stop = true; + proposedLeader = -1; + proposedZxid = -1; + leadingVoteSet = null; + LOG.debug("Shutting down connection manager"); + manager.halt(); + LOG.debug("Shutting down messenger"); + messenger.halt(); + LOG.debug("FLE is down"); + } + + /** + * Send notifications to all peers upon a change in our vote + */ + private void sendNotifications() { + + // TODO_MA 注释: 获取定胜算法: QuorumMaj + // TODO_MA 注释: 遍历每一台服务器,给每一台服务器发送选票 + for(long sid : self.getCurrentAndNextConfigVoters()) { + QuorumVerifier qv = self.getQuorumVerifier(); + + // TODO_MA 注释: 构建待发送的选票对象 + ToSend notmsg = new ToSend(ToSend.mType.notification, proposedLeader, proposedZxid, logicalclock.get(), + QuorumPeer.ServerState.LOOKING, sid, proposedEpoch, qv.toString().getBytes()); + + LOG.debug( + "Sending Notification: {} (n.leader), 0x{} (n.zxid), 0x{} (n.round), {} (recipient)," + " {} (myid), 0x{} (n.peerEpoch) ", + proposedLeader, Long.toHexString(proposedZxid), Long.toHexString(logicalclock.get()), sid, + self.getId(), Long.toHexString(proposedEpoch)); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 将选票加入 发送队列: sendqueue + * WorkerSender 线程消费该队列 + */ + sendqueue.offer(notmsg); + } + } + + /** + * Check if a pair (server id, zxid) succeeds our + * current vote. + */ + protected boolean totalOrderPredicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, + long curEpoch) { + LOG.debug("id: {}, proposed id: {}, zxid: 0x{}, proposed zxid: 0x{}", newId, curId, Long.toHexString(newZxid), + Long.toHexString(curZxid)); + + if(self.getQuorumVerifier().getWeight(newId) == 0) { + return false; + } + + /* + * We return true if one of the following three cases hold: + * 1- New epoch is higher + * 2- New epoch is the same as current epoch, but new zxid is higher + * 3- New epoch is the same as current epoch, new zxid is the same + * as current zxid, but server id is higher. + */ + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 三个条件 + * 1、逻辑时钟 epoch logicallock + * 2、zxid + * 3、myid + */ + return ((newEpoch > curEpoch) || + ((newEpoch == curEpoch) && ((newZxid > curZxid) || + ((newZxid == curZxid) && (newId > curId))))); + } + + /** + * // TODO_MA 注释: 给定一组投票,返回 SyncedLearnerTracker,用于确定是否足以宣布选举回合结束。 + * Given a set of votes, return the SyncedLearnerTracker which is used to + * determines if have sufficient to declare the end of the election round. + * + * @param votes Set of votes + * @param vote Identifier of the vote received last + * @return the SyncedLearnerTracker with vote details + */ + protected SyncedLearnerTracker getVoteTracker(Map votes, Vote vote) { + + SyncedLearnerTracker voteSet = new SyncedLearnerTracker(); + + voteSet.addQuorumVerifier(self.getQuorumVerifier()); + + if(self.getLastSeenQuorumVerifier() != null && self.getLastSeenQuorumVerifier().getVersion() > self + .getQuorumVerifier().getVersion()) { + voteSet.addQuorumVerifier(self.getLastSeenQuorumVerifier()); + } + + /* + * First make the views consistent. Sometimes peers will have different + * zxids for a server depending on timing. + */ + for(Map.Entry entry : votes.entrySet()) { + if(vote.equals(entry.getValue())) { + voteSet.addAck(entry.getKey()); + } + } + + return voteSet; + } + + /** + * In the case there is a leader elected, and a quorum supporting + * this leader, we have to check if the leader has voted and acked + * that it is leading. We need this check to avoid that peers keep + * electing over and over a peer that has crashed and it is no + * longer leading. + * + * @param votes set of votes + * @param leader leader id + * @param electionEpoch epoch id + */ + protected boolean checkLeader(Map votes, long leader, long electionEpoch) { + + boolean predicate = true; + + /* + * If everyone else thinks I'm the leader, I must be the leader. + * The other two checks are just for the case in which I'm not the + * leader. If I'm not the leader and I haven't received a message + * from leader stating that it is leading, then predicate is false. + */ + + if(leader != self.getId()) { + if(votes.get(leader) == null) { + predicate = false; + } else if(votes.get(leader).getState() != ServerState.LEADING) { + predicate = false; + } + } else if(logicalclock.get() != electionEpoch) { + predicate = false; + } + + return predicate; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 更新推举的 leader 的信息 + * 这三个参数: long leader, long zxid, long epoch 都是自己的信息 + */ + synchronized void updateProposal(long leader, long zxid, long epoch) { + LOG.debug("Updating proposal: {} (newleader), 0x{} (newzxid), {} (oldleader), 0x{} (oldzxid)", leader, + Long.toHexString(zxid), proposedLeader, Long.toHexString(proposedZxid)); + + // TODO_MA 注释: 这三个成员变量保存的就是当前节点推举的 Leader 节点的信息 + // TODO_MA 注释: 当执行第一轮选举的时候,必然是每个节点都是推举自己 + // TODO_MA 注释: 所以在这种情况下,这三个成员变量保存的都是自己的信息 + proposedLeader = leader; + proposedZxid = zxid; + proposedEpoch = epoch; + + // TODO_MA 注释: 当第一轮选举不出来 leader 的时候,在后续的轮次中我们会执行选票的更新 + // TODO_MA 注释: 拿 我推举的 leader 的信息, 和 对方节点推举的 leader 的信息做对比 + // TODO_MA 注释: 这个逻辑: 1、epoch 2、zxid 3、myid + // TODO_MA 注释: 先比较 epoch , 获胜的是 epoch 大的 leader 推举信息 + // TODO_MA 注释: 再比较 zxid , 获胜的是 zxid 大的 + // TODO_MA 注释: 当 zxid 一样的时候,则比较 myid,由于每个 server 节点的 myid 一定不一样, + // TODO_MA 注释: 所以这个比较规则一定能比较出来更适合当 leader 的节点 + + // TODO_MA 注释: zxid 是包含两个信息: epoch + txid + // TODO_MA 注释: epoch = 任期 = 44 45 46 , 康熙 乾隆 雍正 光绪 + + // TODO_MA 注释: 如果如果涉及到选票更新: + // TODO_MA 注释: ServerA (100,25,2) ServerB (100,24,3)==> (100,25,2) + } + + public synchronized Vote getVote() { + return new Vote(proposedLeader, proposedZxid, proposedEpoch); + } + + /** + * A learning state can be either FOLLOWING or OBSERVING. + * This method simply decides which one depending on the + * role of the server. + * + * @return ServerState + */ + private ServerState learningState() { + if(self.getLearnerType() == LearnerType.PARTICIPANT) { + LOG.debug("I am a participant: {}", self.getId()); + return ServerState.FOLLOWING; + } else { + LOG.debug("I am an observer: {}", self.getId()); + return ServerState.OBSERVING; + } + } + + /** + * Returns the initial vote value of server identifier. + * + * @return long + */ + private long getInitId() { + // TODO_MA 注释: VotingMembers 有选举权的服务器节点的集合 + if(self.getQuorumVerifier().getVotingMembers().containsKey(self.getId())) { + return self.getId(); + } else { + return Long.MIN_VALUE; + } + } + + /** + * Returns initial last logged zxid. + * + * @return long + */ + private long getInitLastLoggedZxid() { + if(self.getLearnerType() == LearnerType.PARTICIPANT) { + return self.getLastLoggedZxid(); + } else { + return Long.MIN_VALUE; + } + } + + /** + * Returns the initial vote value of the peer epoch. + * + * @return long + */ + private long getPeerEpoch() { + if(self.getLearnerType() == LearnerType.PARTICIPANT) { + try { + return self.getCurrentEpoch(); + } catch(IOException e) { + RuntimeException re = new RuntimeException(e.getMessage()); + re.setStackTrace(e.getStackTrace()); + throw re; + } + } else { + return Long.MIN_VALUE; + } + } + + /** + * Update the peer state based on the given proposedLeader. Also update + * the leadingVoteSet if it becomes the leader. + */ + private void setPeerState(long proposedLeader, SyncedLearnerTracker voteSet) { + + // TODO_MA 注释: 根据选举结果来更新状态 + ServerState ss = (proposedLeader == self.getId()) ? ServerState.LEADING : learningState(); + + // TODO_MA 注释: 更新 QuorumPeer 的状态 + self.setPeerState(ss); + if(ss == ServerState.LEADING) { + leadingVoteSet = voteSet; + } + } + + /** + * Starts a new round of leader election. Whenever our QuorumPeer + * changes its state to LOOKING, this method is invoked, and it + * sends notifications to all other peers. + */ + public Vote lookForLeader() throws InterruptedException { + try { + self.jmxLeaderElectionBean = new LeaderElectionBean(); + MBeanRegistry.getInstance().register(self.jmxLeaderElectionBean, self.jmxLocalPeerBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + self.jmxLeaderElectionBean = null; + } + + // TODO_MA 注释: 选举开始时间 + self.start_fle = Time.currentElapsedTime(); + try { + /* + * The votes from the current leader election are stored in recvset. In other words, a vote v is in recvset + * if v.electionEpoch == logicalclock. The current participant uses recvset to deduce on whether a majority + * of participants has voted for it. + */ + // TODO_MA 注释: 存储投票的集合 + // TODO_MA 注释: long key = serverID + // TODO_MA 注释: Vote value = 对应的 serverID 的选票 + // TODO_MA 注释: 如果 zk 有 7 台服务器,则 recvset 最多有 7 个 + Map recvset = new HashMap(); + + /* + * The votes from previous leader elections, as well as the votes from the current leader election are + * stored in outofelection. Note that notifications in a LOOKING state are not stored in outofelection. + * Only FOLLOWING or LEADING notifications are stored in outofelection. The current participant could use + * outofelection to learn which participant is the leader if it arrives late (i.e., higher logicalclock than + * the electionEpoch of the received notifications) in a leader election. + */ + // TODO_MA 注释: 存储了上一次选举和这一次选举的票(leading 或者 following 的选票存储在这里) + Map outofelection = new HashMap(); + + int notTimeout = minNotificationInterval; + + synchronized(this) { + // TODO_MA 注释: 第一个步骤: 自增选举轮次 + logicalclock.incrementAndGet(); + + // TODO_MA 注释: 第二个步骤: 更新选票 + // TODO_MA 注释: 选票中的信息,都是自己的 + // TODO_MA 注释: epoch, zxid, myid + updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); + } + + // 准备选票 + // currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch()); + + // TODO_MA 注释: 第三个步骤: 发送选票给所有其他服务器节点 + LOG.info("New election. My id = {}, proposed zxid=0x{}", self.getId(), Long.toHexString(proposedZxid)); + sendNotifications(); + + SyncedLearnerTracker voteSet; + + /* + * Loop in which we exchange notifications until we find a leader + */ + + // TODO_MA 注释: 进入循环,直到选出来 Leader 就退出,或者 当前节点关闭了 + while((self.getPeerState() == ServerState.LOOKING) && (!stop)) { + /* + * Remove next notification from queue, times out after 2 times + * the termination time + */ + // TODO_MA 注释: 第四个步骤: 获取一个合法投票 + Notification n = recvqueue.poll(notTimeout, TimeUnit.MILLISECONDS); + + // TODO_MA 注释: 从此往后的代码,就是执行选举的逻辑处理 + + /* + * Sends more notifications if haven't received enough. + * Otherwise processes new notification. + */ + // TODO_MA 注释: 如果 n = null, 意味着,可能选票并没有发送出去。 + // TODO_MA 注释: 如果如果之前发了,则重发,如果之前没发,则联系其他服务器进行发送 + if(n == null) { + if(manager.haveDelivered()) { + sendNotifications(); + } else { + manager.connectAll(); + } + + /* + * Exponential backoff + * 超时时间是一个这样的序列: 0.2 0.4 0.8 1.6 ..... 60s + */ + int tmpTimeOut = notTimeout * 2; + notTimeout = Math.min(tmpTimeOut, maxNotificationInterval); + LOG.info("Notification time out: {}", notTimeout); + } + + // TODO_MA 注释: 如果 n != null, 则校验它的合法性 + // TODO_MA 注释: 投票节点 n.sid 和 推举的leader节点 n.leader 必须都是 participant 成员 + // TODO_MA 注释: n.sid 发票过来的 server 的 myid = myid1 + // TODO_MA 注释: n.leader 发票过来的 server 推举的 leader 的 myid = myid2 + // TODO_MA 注释: 验证 myid1 有没有选举权, 验证 myid2 有没有被选举权 + else if(validVoter(n.sid) && validVoter(n.leader)) { + + // TODO_MA 注释: 这个地方就体现出来了 follower 和 observer 的区别了 + + // TODO_MA 注释: 根据对方的状态来判断 + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * switch(n.state) { + * case LOOKING: + * case LEADING: + * case OBSERVING: + * case FOLLOWING: + */ + /* + * Only proceed if the vote comes from a replica in the current or next + * voting view for a replica in the current or next voting view. + * // TODO_MA 注释: n.state 就是对方给我发票的那个服务器的状态 + * // TODO_MA 注释: 如果 zk1 zk2 zk3 zk4 先启动, 能选出来leader,假设是 3 + * // TODO_MA 注释: 当 zk3 成为leader 之后, zk5 启动了 + * // TODO_MA 注释: zk5 的状态是 LOOKING。 zk1 zk2 zk4 是 FOLLOWING, zk3 是 leading + * + * // TODO_MA 注释: 大家一起启动! 大家都是 LOOKING + * + * // TODO_MA 注释: 如果此时从 对方接收到 票。 对方有可能是 leader 有可能是 follower,也有可能是在执行选举 + * // TODO_MA 注释: 判断对方的状态是什么 + * // TODO_MA 注释: 如果对方是 follower 或者 leader, 则他们返回的信息,就是当前 Leader 的信息 + * // TODO_MA 注释: 如果对方是 LOOKING 状态,则意味着对方也在执行选举。 + */ + switch(n.state) { + + // TODO_MA 注释: 如果对方为 LOOKING, 证明对方,也是在执行选举 + case LOOKING: + if(getInitLastLoggedZxid() == -1) { + LOG.debug("Ignoring notification as our zxid is -1"); + break; + } + if(n.zxid == -1) { + LOG.debug("Ignoring notification from member with -1 zxid {}", n.sid); + break; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 第五个步骤: 逻辑时钟校准 + */ + + // TODO_MA 注释: 第5.1步骤: 如果对方逻辑时钟,大于当前节点 + // If notification > current, replace and send messages out + if(n.electionEpoch > logicalclock.get()) { + + // TODO_MA 注释: 更新逻辑时钟 + logicalclock.set(n.electionEpoch); + // TODO_MA 注释: 清空所有投票 + recvset.clear(); + + // TODO_MA 注释: 校验投票,如果对方的选票更优,则更新自己的选票和对方的一样 + // TODO_MA 注释: 前三个参数 n.leader, n.zxid, n.peerEpoch 是对方推举的 的 leader + // TODO_MA 注释: 后三个参数 getInitId(), getInitLastLoggedZxid(), getPeerEpoch() 我推举的 + if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, getInitId(), + getInitLastLoggedZxid(), getPeerEpoch())) { + updateProposal(n.leader, n.zxid, n.peerEpoch); + } else { + updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch()); + } + + // TODO_MA 注释: 更新了自己的选票之后,继续发 + sendNotifications(); + } + + // TODO_MA 注释: 第5.2步骤: 如果对方的逻辑时钟比我的小,则忽略对方的投票 + else if(n.electionEpoch < logicalclock.get()) { + LOG.debug( + "Notification election epoch is smaller than logicalclock. n.electionEpoch = 0x{}, logicalclock=0x{}", + Long.toHexString(n.electionEpoch), Long.toHexString(logicalclock.get())); + break; + } + + // TODO_MA 注释: 只要某个节点,发现,有节点比它推举的leader更优 + // TODO_MA 注释: 则当前节点就一定会更新自己的选票为对方节点推举的leader的信息 + // TODO_MA 注释: 然后继续广播一次 + // TODO_MA 注释: 其实,只要两两节点之间的数据发送是一定到达的情况, + // TODO_MA 注释: 那么其实所有节点接收到的票的集合是一样的 + // TODO_MA 注释: 只要某一个节点能判断出来选举算法可以结束了,则其他所有节点都可以判断出来 + + // TODO_MA 注释: 第5.3步骤: 终于统一逻辑时钟了。 + // TODO_MA 注释: 第六个步骤: 选票PK。统一了逻辑中,还需要判断,是不是对方的选票更优,如果是,则更新 + else if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, proposedLeader, proposedZxid, + proposedEpoch)) { + updateProposal(n.leader, n.zxid, n.peerEpoch); + sendNotifications(); + } + + // TODO_MA 注释: 到这儿,是已经统一了逻辑时钟 + + LOG.debug( + "Adding vote: from={}, proposed leader={}, proposed zxid=0x{}, proposed election epoch=0x{}", + n.sid, n.leader, Long.toHexString(n.zxid), Long.toHexString(n.electionEpoch)); + + // TODO_MA 注释: 第八个步骤: 选票归档。将投票加入到接收集合中 + // don't care about the version if it's in LOOKING state + recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch)); + + // TODO_MA 注释: 1张票: 1 => 2 n.sid = 1, n.leader = 2 + // TODO_MA 注释: 2张票: 2 => 2 + // TODO_MA 注释: 3张票: 3 => 3 + + // TODO_MA 注释: 假设 第一张票是刚加进来的,那么 n.leader = 2, 2 的所有的票都放入到 voteSet 中 + + // TODO_MA 注释: 第九个步骤: 唱票 + // TODO_MA 注释: 根据当前投票节点推举的 leader 来获取该 推举leader的投票集合 + // TODO_MA 注释: 这句代码是从 recvset 获取刚才这个 n 这个票推举的 leader 的选票的集合 + voteSet = getVoteTracker(recvset, + new Vote(proposedLeader, proposedZxid, logicalclock.get(), proposedEpoch)); + // TODO_MA 注释: 总共 5 个节点 + // TODO_MA 注释: 1 2 3 2 3 + // TODO_MA 注释: 2 + // TODO_MA 注释: 3 + + // TODO_MA 注释: 判断这个集合,是否超过了半数 + if(voteSet.hasAllQuorums()) { + + // TODO_MA 注释: 总共五个节点: 1 2 2 2 + // Verify if there is any change in the proposed leader + while((n = recvqueue.poll(finalizeWait, TimeUnit.MILLISECONDS)) != null) { + + // TODO_MA 注释: 如果发现有更优投票,则退出该循环 + if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch, proposedLeader, proposedZxid, + proposedEpoch)) { + recvqueue.put(n); + break; + } + } + + // TODO_MA 注释: 当 n = null 的时候,证明 recvqueue 中已经没有多余投票了,而且也没有更优投票 + /* + * This predicate is true once we don't read any new relevant message from the + * reception queue + */ + if(n == null) { + + // TODO_MA 注释: 第十个步骤: 切换状态 + setPeerState(proposedLeader, voteSet); + + // TODO_MA 注释: 存储最终胜选投票 + Vote endVote = new Vote(proposedLeader, proposedZxid, logicalclock.get(), + proposedEpoch); + + // TODO_MA 注释: 清空 recvqueue 队列 + leaveInstance(endVote); + + // TODO_MA 注释: 到此为止,选举结束! + // TODO_MA 注释: 最后得到的是: 一个 endVote(存储的是: 被推举成为leader 的服务器的信息) + return endVote; + } + } + break; + + // TODO_MA 注释: 如果对方是 observer,不管 + case OBSERVING: + LOG.debug("Notification from observer: {}", n.sid); + break; + + // TODO_MA 注释: 如果对方是 following 或者 leading 证明,对方已经通过获取的投票抉择出来了谁是 leader 了 + case FOLLOWING: + case LEADING: + /* + * Consider all notifications from the same epoch together. + * // TODO_MA 注释: 如果逻辑时钟一样,则自己也存储投票,检查leader是否合法,切换自己的状态 + */ + if(n.electionEpoch == logicalclock.get()) { + // TODO_MA 注释: 当接受到来自于 Leader 或者 Follower 的选票。 + // TODO_MA 注释: 而不是来自于 LOOKING 的选票,则直接就是 Leader 的信息,直接加入 recvset + recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); + voteSet = getVoteTracker(recvset, + new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); + + // TODO_MA 注释: 接着执行判断 + // TODO_MA 注释: 判断是否有超过半数节点,返回的这个 leader 是同一个,如果是,则这个leader 就是合法leader + // TODO_MA 注释: 当前这个这个节点,也就自动追随这个leader,不用再继续选举了。 + if(voteSet.hasAllQuorums() && checkLeader(recvset, n.leader, n.electionEpoch)) { + + // TODO_MA 注释: 切换 + setPeerState(n.leader, voteSet); + + // TODO_MA 注释: endVote 存储就是此时选举算法选举结束之后,成功当选leader 的节点的信息 + Vote endVote = new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch); + leaveInstance(endVote); + + // TODO_MA 注释: 这个地方有 return 就意味着 这个 lookForLeader 选举方法结束了。 + // TODO_MA 注释: 选举出来了 Leader + return endVote; + } + } + + /* + * // TODO_MA 注释: 确认,是不是大家都在追随同一个领导 + * Before joining an established ensemble, verify that a majority are following the same + * leader. + * + * Note that the outofelection map also stores votes from the current leader election. + * See ZOOKEEPER-1732 for more information. + */ + outofelection.put(n.sid, + new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); + voteSet = getVoteTracker(outofelection, + new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)); + + if(voteSet.hasAllQuorums() && checkLeader(outofelection, n.leader, n.electionEpoch)) { + synchronized(this) { + logicalclock.set(n.electionEpoch); + setPeerState(n.leader, voteSet); + } + Vote endVote = new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch); + leaveInstance(endVote); + return endVote; + } + break; + default: + LOG.warn("Notification state unrecoginized: {} (n.state), {}(n.sid)", n.state, n.sid); + break; + } + } else { + if(!validVoter(n.leader)) { + LOG.warn("Ignoring notification for non-cluster member sid {} from sid {}", n.leader, n.sid); + } + if(!validVoter(n.sid)) { + LOG.warn("Ignoring notification for sid {} from non-quorum member sid {}", n.leader, n.sid); + } + } + } + return null; + } finally { + try { + if(self.jmxLeaderElectionBean != null) { + MBeanRegistry.getInstance().unregister(self.jmxLeaderElectionBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + self.jmxLeaderElectionBean = null; + LOG.debug("Number of connection processing threads: {}", manager.getConnectionThreadCount()); + } + } + + /** + * Check if a given sid is represented in either the current or + * the next voting view + * + * @param sid Server identifier + * @return boolean + */ + private boolean validVoter(long sid) { + return self.getCurrentAndNextConfigVoters().contains(sid); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Follower.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Follower.java new file mode 100644 index 0000000..914fb01 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Follower.java @@ -0,0 +1,345 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.Map; + +import org.apache.jute.Record; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; + +/** + * This class has the control logic for the Follower. + */ +public class Follower extends Learner { + + private long lastQueued; + + // This is the same object as this.zk, but we cache the downcast op + final FollowerZooKeeperServer fzk; + + ObserverMaster om; + + Follower(QuorumPeer self, FollowerZooKeeperServer zk) { + this.self = self; + this.zk = zk; + this.fzk = zk; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Follower ").append(sock); + sb.append(" lastQueuedZxid:").append(lastQueued); + sb.append(" pendingRevalidationCount:").append(pendingRevalidations.size()); + return sb.toString(); + } + + /** + * the main method called by the follower to follow the leader + * + * @throws InterruptedException + */ + void followLeader() throws InterruptedException { + self.end_fle = Time.currentElapsedTime(); + long electionTimeTaken = self.end_fle - self.start_fle; + self.setElectionTimeTaken(electionTimeTaken); + ServerMetrics.getMetrics().ELECTION_TIME.add(electionTimeTaken); + LOG.info("FOLLOWING - LEADER ELECTION TOOK - {} {}", electionTimeTaken, QuorumPeer.FLE_TIME_UNIT); + self.start_fle = 0; + self.end_fle = 0; + fzk.registerJMX(new FollowerBean(this, zk), self.jmxLocalPeerBean); + + long connectionTime = 0; + boolean completedSync = false; + + try { + + // TODO_MA 注释: 01 切换到 DISCOVERY 状态 + self.setZabState(QuorumPeer.ZabState.DISCOVERY); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 02 确认 leader 地址 + * 根据 lookForLeader 方法的返回值的信息,来找到一个 QuorumServer + */ + QuorumServer leaderServer = findLeader(); // 最终找到的就是 lookForLeader() 方法的结果 + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 03 链接 leader + * 内部通过 LeaderConnector 线程和 Leader 建立 BIO TCP 链接 + */ + connectToLeader(leaderServer.addr, leaderServer.hostname); + connectionTime = System.currentTimeMillis(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 04 向 Leader 注册 + * 1、follower 发送 FOLLOWERINFO 给 leader + * 2、leader 返回 LEADERINFO 给 follower + * 3、follower 返回 ACKEPOCH 给 leader + */ + long newEpochZxid = registerWithLeader(Leader.FOLLOWERINFO); + if(self.isReconfigStateChange()) { + throw new Exception("learned about role change"); + } + //check to see if the leader zxid is lower than ours + //this should never happen but is just a safety check + long newEpoch = ZxidUtils.getEpochFromZxid(newEpochZxid); + if(newEpoch < self.getAcceptedEpoch()) { + LOG.error("Proposed leader epoch " + ZxidUtils + .zxidToString(newEpochZxid) + " is less than our accepted epoch " + ZxidUtils + .zxidToString(self.getAcceptedEpoch())); + throw new IOException("Error: Epoch of leader is lower"); + } + long startTime = Time.currentElapsedTime(); + + try { + self.setLeaderAddressAndId(leaderServer.addr, leaderServer.getId()); + + // TODO_MA 注释: 进入 ZAB 的 SYNCHRONIZATION 状态 + self.setZabState(QuorumPeer.ZabState.SYNCHRONIZATION); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: Follower 和 Leader 进行同步 + * 1、先和 Leader 确认得到 同步方式 + * 2、执行状态同步 + */ + syncWithLeader(newEpochZxid); + + // TODO_MA 注释: 进入 ZAB 的 BROADCAST 状态 + self.setZabState(QuorumPeer.ZabState.BROADCAST); + + // TODO_MA 注释: 同步完成 + completedSync = true; + } finally { + long syncTime = Time.currentElapsedTime() - startTime; + ServerMetrics.getMetrics().FOLLOWER_SYNC_TIME.add(syncTime); + } + if(self.getObserverMasterPort() > 0) { + LOG.info("Starting ObserverMaster"); + om = new ObserverMaster(self, fzk, self.getObserverMasterPort()); + om.start(); + } else { + om = null; + } + + // TODO_MA 注释: 到此为止,意味着 Follower 和 Leader 的同步结束 + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建可重用的数据包以减少 gc 的影响 + * 不停的读取 Leader 广播过来的分布式事务执行 ZAB 的消息广播操作 + */ + // create a reusable packet to reduce gc impact + QuorumPacket qp = new QuorumPacket(); + while(this.isRunning()) { + readPacket(qp); + processPacket(qp); + } + } catch(Exception e) { + LOG.warn("Exception when following the leader", e); + closeSocket(); + + // clear pending revalidations + pendingRevalidations.clear(); + } + } finally { + if(om != null) { + om.stop(); + } + zk.unregisterJMX(this); + + if(connectionTime != 0) { + long connectionDuration = System.currentTimeMillis() - connectionTime; + LOG.info("Disconnected from leader (with address: {}). Was connected for {}ms. Sync state: {}", + leaderAddr, connectionDuration, completedSync); + messageTracker.dumpToLog(leaderAddr.toString()); + } + } + } + + /** + * Examine the packet received in qp and dispatch based on its contents. + * + * @param qp + * @throws IOException + */ + protected void processPacket(QuorumPacket qp) throws Exception { + switch(qp.getType()) { + + // TODO_MA 注释: 心跳消息 + case Leader.PING: + ping(qp); + break; + + // TODO_MA 注释: 事务请求 + case Leader.PROPOSAL: + ServerMetrics.getMetrics().LEARNER_PROPOSAL_RECEIVED_COUNT.add(1); + TxnLogEntry logEntry = SerializeUtils.deserializeTxn(qp.getData()); + TxnHeader hdr = logEntry.getHeader(); + Record txn = logEntry.getTxn(); + TxnDigest digest = logEntry.getDigest(); + if(hdr.getZxid() != lastQueued + 1) { + LOG.warn("Got zxid 0x{} expected 0x{}", Long.toHexString(hdr.getZxid()), + Long.toHexString(lastQueued + 1)); + } + lastQueued = hdr.getZxid(); + + if(hdr.getType() == OpCode.reconfig) { + SetDataTxn setDataTxn = (SetDataTxn) txn; + QuorumVerifier qv = self.configFromString(new String(setDataTxn.getData())); + self.setLastSeenQuorumVerifier(qv, true); + } + + fzk.logRequest(hdr, txn, digest); + if(hdr != null) { + /* + * Request header is created only by the leader, so this is only set + * for quorum packets. If there is a clock drift, the latency may be + * negative. Headers use wall time, not CLOCK_MONOTONIC. + */ + long now = Time.currentWallTime(); + long latency = now - hdr.getTime(); + if(latency >= 0) { + ServerMetrics.getMetrics().PROPOSAL_LATENCY.add(latency); + } + } + if(om != null) { + final long startTime = Time.currentElapsedTime(); + om.proposalReceived(qp); + ServerMetrics.getMetrics().OM_PROPOSAL_PROCESS_TIME.add(Time.currentElapsedTime() - startTime); + } + break; + + // TODO_MA 注释: 事务提交 + case Leader.COMMIT: + ServerMetrics.getMetrics().LEARNER_COMMIT_RECEIVED_COUNT.add(1); + fzk.commit(qp.getZxid()); + if(om != null) { + final long startTime = Time.currentElapsedTime(); + om.proposalCommitted(qp.getZxid()); + ServerMetrics.getMetrics().OM_COMMIT_PROCESS_TIME.add(Time.currentElapsedTime() - startTime); + } + break; + + case Leader.COMMITANDACTIVATE: + // get the new configuration from the request + Request request = fzk.pendingTxns.element(); + SetDataTxn setDataTxn = (SetDataTxn) request.getTxn(); + QuorumVerifier qv = self.configFromString(new String(setDataTxn.getData())); + + // get new designated leader from (current) leader's message + ByteBuffer buffer = ByteBuffer.wrap(qp.getData()); + long suggestedLeaderId = buffer.getLong(); + final long zxid = qp.getZxid(); + boolean majorChange = self.processReconfig(qv, suggestedLeaderId, zxid, true); + // commit (writes the new config to ZK tree (/zookeeper/config) + fzk.commit(zxid); + + if(om != null) { + om.informAndActivate(zxid, suggestedLeaderId); + } + if(majorChange) { + throw new Exception("changes proposed in reconfig"); + } + break; + case Leader.UPTODATE: + LOG.error("Received an UPTODATE message after Follower started"); + break; + case Leader.REVALIDATE: + if(om == null || !om.revalidateLearnerSession(qp)) { + revalidate(qp); + } + break; + // TODO_MA 注释: 同步请求 + case Leader.SYNC: + fzk.sync(); + break; + default: + LOG.warn("Unknown packet type: {}", LearnerHandler.packetToString(qp)); + break; + } + } + + /** + * The zxid of the last operation seen + * + * @return zxid + */ + public long getZxid() { + try { + synchronized(fzk) { + return fzk.getZxid(); + } + } catch(NullPointerException e) { + LOG.warn("error getting zxid", e); + } + return -1; + } + + /** + * The zxid of the last operation queued + * + * @return zxid + */ + protected long getLastQueued() { + return lastQueued; + } + + public Integer getSyncedObserverSize() { + return om == null ? null : om.getNumActiveObservers(); + } + + public Iterable> getSyncedObserversInfo() { + if(om != null && om.getNumActiveObservers() > 0) { + return om.getActiveObservers(); + } + return Collections.emptySet(); + } + + public void resetObserverConnectionStats() { + if(om != null && om.getNumActiveObservers() > 0) { + om.resetObserverConnectionStats(); + } + } + + @Override + public void shutdown() { + LOG.info("shutdown Follower"); + super.shutdown(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerBean.java new file mode 100644 index 0000000..8f1bafb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerBean.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperServerBean; + +/** + * Follower MBean interface implementation + */ +public class FollowerBean extends ZooKeeperServerBean implements FollowerMXBean { + + private final Follower follower; + + public FollowerBean(Follower follower, ZooKeeperServer zks) { + super(zks); + this.follower = follower; + } + + public String getName() { + return "Follower"; + } + + public String getQuorumAddress() { + return follower.sock.toString(); + } + + public String getLastQueuedZxid() { + return "0x" + Long.toHexString(follower.getLastQueued()); + } + + public int getPendingRevalidationCount() { + return follower.getPendingRevalidationsCount(); + } + + @Override + public long getElectionTimeTaken() { + return follower.self.getElectionTimeTaken(); + } + + @Override + public int getObserverMasterPacketSizeLimit() { + return follower.om == null ? -1 : follower.om.getPktsSizeLimit(); + } + + @Override + public void setObserverMasterPacketSizeLimit(int sizeLimit) { + ObserverMaster.setPktsSizeLimit(sizeLimit); + } + + @Override + public int getMaxConcurrentSnapSyncs() { + final ObserverMaster om = follower.om; + return om == null ? -1 : om.getMaxConcurrentSnapSyncs(); + } + + @Override + public void setMaxConcurrentSnapSyncs(int maxConcurrentSnapshots) { + final ObserverMaster om = follower.om; + if (om != null) { + om.setMaxConcurrentSnapSyncs(maxConcurrentSnapshots); + } + } + + @Override + public int getMaxConcurrentDiffSyncs() { + final ObserverMaster om = follower.om; + return om == null ? -1 : om.getMaxConcurrentDiffSyncs(); + } + + @Override + public void setMaxConcurrentDiffSyncs(int maxConcurrentDiffSyncs) { + final ObserverMaster om = follower.om; + if (om != null) { + om.setMaxConcurrentDiffSyncs(maxConcurrentDiffSyncs); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerMXBean.java new file mode 100644 index 0000000..a704990 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerMXBean.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.ZooKeeperServerMXBean; + +/** + * Follower MBean + */ +public interface FollowerMXBean extends ZooKeeperServerMXBean { + + /** + * @return socket address + */ + String getQuorumAddress(); + + /** + * @return last queued zxid + */ + String getLastQueuedZxid(); + + /** + * @return count of pending revalidations + */ + int getPendingRevalidationCount(); + + /** + * @return time taken for leader election in milliseconds. + */ + long getElectionTimeTaken(); + + /** + * @return the size limit in bytes for the observer master commit packet queue + */ + int getObserverMasterPacketSizeLimit(); + + /** + * set the size limit in bytes for the observer master commit packet queue + */ + void setObserverMasterPacketSizeLimit(int sizeLimit); + + /** + * @return Number of concurrent snapshots permitted to send to observers + */ + int getMaxConcurrentSnapSyncs(); + + /** + * @param maxConcurrentSnapSyncs Number of concurrent snapshots permitted to send to observers + */ + void setMaxConcurrentSnapSyncs(int maxConcurrentSnapSyncs); + + /** + * @return Number of concurrent diff syncs permitted to send to observers + */ + int getMaxConcurrentDiffSyncs(); + + /** + * @param maxConcurrentDiffSyncs Number of concurrent diff syncs permitted to send to observers + */ + void setMaxConcurrentDiffSyncs(int maxConcurrentDiffSyncs); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerRequestProcessor.java new file mode 100644 index 0000000..a04a62b --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerRequestProcessor.java @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.txn.ErrorTxn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This RequestProcessor forwards any requests that modify the state of the + * system to the Leader. + */ +public class FollowerRequestProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(FollowerRequestProcessor.class); + + FollowerZooKeeperServer zks; + + // TODO_MA 注释: 当前 RP 处理完之后,下一个 RP 接着处理 + RequestProcessor nextProcessor; + + // TODO_MA 注释: 队列 + LinkedBlockingQueue queuedRequests = new LinkedBlockingQueue(); + + boolean finished = false; + + public FollowerRequestProcessor(FollowerZooKeeperServer zks, RequestProcessor nextProcessor) { + super("FollowerRequestProcessor:" + zks.getServerId(), zks.getZooKeeperServerListener()); + this.zks = zks; + this.nextProcessor = nextProcessor; + } + + @Override + public void run() { + try { + while(!finished) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 获取提交该 RP 的 Request + */ + Request request = queuedRequests.take(); + if(LOG.isTraceEnabled()) { + ZooTrace.logRequest(LOG, ZooTrace.CLIENT_REQUEST_TRACE_MASK, 'F', request, ""); + } + if(request == Request.requestOfDeath) { + break; + } + + // Screen quorum requests against ACLs first + if(!zks.authWriteRequest(request)) { + continue; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + // We want to queue the request to be processed before we submit + // the request to the leader so that we are ready to receive the response + nextProcessor.processRequest(request); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 转发请求给 Leader + * 下述列表中的 请求类型,都是事务请求 + */ + // We now ship the request to the leader. As with all + // other quorum operations, sync also follows this code + // path, but different from others, we need to keep track + // of the sync operations this follower has pending, so we + // add it to pendingSyncs. + switch(request.type) { + + // TODO_MA 注释: 如果是同步请求 + case OpCode.sync: + zks.pendingSyncs.add(request); + zks.getFollower().request(request); + break; + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + case OpCode.delete: + case OpCode.deleteContainer: + case OpCode.setData: + case OpCode.reconfig: + case OpCode.setACL: + case OpCode.multi: + case OpCode.check: + zks.getFollower().request(request); + break; + case OpCode.createSession: + case OpCode.closeSession: + // Don't forward local sessions to the leader. + if(!request.isLocalSession()) { + zks.getFollower().request(request); + } + break; + } + } + } catch(Exception e) { + handleException(this.getName(), e); + } + LOG.info("FollowerRequestProcessor exited loop!"); + } + + public void processRequest(Request request) { + + // TODO_MA 注释: + processRequest(request, true); + } + + void processRequest(Request request, boolean checkForUpgrade) { + if(!finished) { + + // TODO_MA 注释: 处理 session 的事儿 + if(checkForUpgrade) { + // Before sending the request, check if the request requires a + // global session and what we have is a local session. If so do + // an upgrade. + Request upgradeRequest = null; + try { + upgradeRequest = zks.checkUpgradeSession(request); + } catch(KeeperException ke) { + if(request.getHdr() != null) { + request.getHdr().setType(OpCode.error); + request.setTxn(new ErrorTxn(ke.code().intValue())); + } + request.setException(ke); + LOG.warn("Error creating upgrade request", ke); + } catch(IOException ie) { + LOG.error("Unexpected error in upgrade", ie); + } + if(upgradeRequest != null) { + queuedRequests.add(upgradeRequest); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + queuedRequests.add(request); + } + } + + public void shutdown() { + LOG.info("Shutting down"); + finished = true; + queuedRequests.clear(); + queuedRequests.add(Request.requestOfDeath); + nextProcessor.shutdown(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerZooKeeperServer.java new file mode 100644 index 0000000..d3d1206 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/FollowerZooKeeperServer.java @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.LinkedBlockingQueue; +import javax.management.JMException; + +import org.apache.jute.Record; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Just like the standard ZooKeeperServer. We just replace the request + * processors: FollowerRequestProcessor -> CommitProcessor -> + * FinalRequestProcessor + * + * A SyncRequestProcessor is also spawned off to log proposals from the leader. + */ +public class FollowerZooKeeperServer extends LearnerZooKeeperServer { + + private static final Logger LOG = LoggerFactory.getLogger(FollowerZooKeeperServer.class); + + /** + * Pending sync requests + */ + ConcurrentLinkedQueue pendingSyncs; + + /** + * @throws IOException + */ + FollowerZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, ZKDatabase zkDb) throws IOException { + super(logFactory, self.tickTime, self.minSessionTimeout, self.maxSessionTimeout, self.clientPortListenBacklog, + zkDb, self); + + // TODO_MA 注释: 注意该队列 + this.pendingSyncs = new ConcurrentLinkedQueue(); + } + + public Follower getFollower() { + return self.follower; + } + + @Override + protected void setupRequestProcessors() { + + // TODO_MA 注释: 第三个: FinalRequestProcessor + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + + // TODO_MA 注释: 第二个: CommitProcessor + commitProcessor = new CommitProcessor(finalProcessor, Long.toString(getServerId()), true, + getZooKeeperServerListener()); + commitProcessor.start(); + + // TODO_MA 注释: 第一个: FollowerRequestProcessor + firstProcessor = new FollowerRequestProcessor(this, commitProcessor); + ((FollowerRequestProcessor) firstProcessor).start(); + + // TODO_MA 注释: SyncRequestProcessor + syncProcessor = new SyncRequestProcessor(this, new SendAckRequestProcessor(getFollower())); + syncProcessor.start(); + } + + // TODO_MA 注释: + LinkedBlockingQueue pendingTxns = new LinkedBlockingQueue(); + + public void logRequest(TxnHeader hdr, Record txn, TxnDigest digest) { + Request request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, hdr.getZxid()); + request.setTxnDigest(digest); + if((request.zxid & 0xffffffffL) != 0) { + pendingTxns.add(request); + } + syncProcessor.processRequest(request); + } + + /** + * When a COMMIT message is received, eventually this method is called, + * which matches up the zxid from the COMMIT with (hopefully) the head of + * the pendingTxns queue and hands it to the commitProcessor to commit. + * + * @param zxid - must correspond to the head of pendingTxns if it exists + */ + public void commit(long zxid) { + if(pendingTxns.size() == 0) { + LOG.warn("Committing " + Long.toHexString(zxid) + " without seeing txn"); + return; + } + long firstElementZxid = pendingTxns.element().zxid; + if(firstElementZxid != zxid) { + LOG.error("Committing zxid 0x" + Long.toHexString(zxid) + " but next pending txn 0x" + Long + .toHexString(firstElementZxid)); + ServiceUtils.requestSystemExit(ExitCode.UNMATCHED_TXN_COMMIT.getValue()); + } + Request request = pendingTxns.remove(); + request.logLatency(ServerMetrics.getMetrics().COMMIT_PROPAGATION_LATENCY); + commitProcessor.commit(request); + } + + public synchronized void sync() { + if(pendingSyncs.size() == 0) { + LOG.warn("Not expecting a sync."); + return; + } + + // TODO_MA 注释: 从队列中,获取同步请求 + Request r = pendingSyncs.remove(); + + // TODO_MA 注释: 发送同步请求给 Leader + if(r instanceof LearnerSyncRequest) { + LearnerSyncRequest lsr = (LearnerSyncRequest) r; + lsr.fh.queuePacket(new QuorumPacket(Leader.SYNC, 0, null, null)); + } + + // TODO_MA 注释: 提交 + commitProcessor.commit(r); + } + + @Override + public int getGlobalOutstandingLimit() { + int divisor = self.getQuorumSize() > 2 ? self.getQuorumSize() - 1 : 1; + int globalOutstandingLimit = super.getGlobalOutstandingLimit() / divisor; + return globalOutstandingLimit; + } + + @Override + public String getState() { + return "follower"; + } + + @Override + public Learner getLearner() { + return getFollower(); + } + + /** + * Process a request received from external Learner through the LearnerMaster + * These requests have already passed through validation and checks for + * session upgrade and can be injected into the middle of the pipeline. + * + * @param request received from external Learner + */ + void processObserverRequest(Request request) { + ((FollowerRequestProcessor) firstProcessor).processRequest(request, false); + } + + boolean registerJMX(LearnerHandlerBean handlerBean) { + try { + MBeanRegistry.getInstance().register(handlerBean, jmxServerBean); + return true; + } catch(JMException e) { + LOG.warn("Could not register connection", e); + } + return false; + } + + @Override + protected void registerMetrics() { + super.registerMetrics(); + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + + rootContext.registerGauge("synced_observers", self::getSynced_observers_metric); + + } + + @Override + protected void unregisterMetrics() { + super.unregisterMetrics(); + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + rootContext.unregisterGauge("synced_observers"); + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java new file mode 100644 index 0000000..d8398db --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java @@ -0,0 +1,1883 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; +import javax.security.sasl.SaslException; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class has the control logic for the Leader. + */ +public class Leader extends LearnerMaster { + + private static final Logger LOG = LoggerFactory.getLogger(Leader.class); + + private static final boolean nodelay = System.getProperty("leader.nodelay", "true").equals("true"); + + static { + LOG.info("TCP NoDelay set to: {}", nodelay); + } + + public static class Proposal extends SyncedLearnerTracker { + + public QuorumPacket packet; + public Request request; + + @Override + public String toString() { + return packet.getType() + ", " + packet.getZxid() + ", " + request; + } + + } + + // log ack latency if zxid is a multiple of ackLoggingFrequency. If <=0, disable logging. + private static final String ACK_LOGGING_FREQUENCY = "zookeeper.leader.ackLoggingFrequency"; + private static int ackLoggingFrequency; + + static { + ackLoggingFrequency = Integer.getInteger(ACK_LOGGING_FREQUENCY, 1000); + LOG.info("{} = {}", ACK_LOGGING_FREQUENCY, ackLoggingFrequency); + } + + public static void setAckLoggingFrequency(int frequency) { + ackLoggingFrequency = frequency; + } + + public static int getAckLoggingFrequency() { + return ackLoggingFrequency; + } + + final LeaderZooKeeperServer zk; + + final QuorumPeer self; + + // VisibleForTesting + protected boolean quorumFormed = false; + + // the follower acceptor thread + volatile LearnerCnxAcceptor cnxAcceptor = null; + + // list of all the learners, including followers and observers + private final HashSet learners = new HashSet(); + + private final BufferStats proposalStats; + + public BufferStats getProposalStats() { + return proposalStats; + } + + // beans for all learners + private final ConcurrentHashMap connectionBeans = new ConcurrentHashMap<>(); + + /** + * Returns a copy of the current learner snapshot + */ + public List getLearners() { + synchronized(learners) { + return new ArrayList(learners); + } + } + + // list of followers that are ready to follow (i.e synced with the leader) + private final HashSet forwardingFollowers = new HashSet(); + + /** + * Returns a copy of the current forwarding follower snapshot + */ + public List getForwardingFollowers() { + synchronized(forwardingFollowers) { + return new ArrayList(forwardingFollowers); + } + } + + public List getNonVotingFollowers() { + List nonVotingFollowers = new ArrayList(); + synchronized(forwardingFollowers) { + for(LearnerHandler lh : forwardingFollowers) { + if(!isParticipant(lh.getSid())) { + nonVotingFollowers.add(lh); + } + } + } + return nonVotingFollowers; + } + + void addForwardingFollower(LearnerHandler lh) { + synchronized(forwardingFollowers) { + forwardingFollowers.add(lh); + } + } + + private final HashSet observingLearners = new HashSet(); + + /** + * Returns a copy of the current observer snapshot + */ + public List getObservingLearners() { + synchronized(observingLearners) { + return new ArrayList(observingLearners); + } + } + + private void addObserverLearnerHandler(LearnerHandler lh) { + synchronized(observingLearners) { + observingLearners.add(lh); + } + } + + public Iterable> getObservingLearnersInfo() { + Set> info = new HashSet<>(); + synchronized(observingLearners) { + for(LearnerHandler lh : observingLearners) { + info.add(lh.getLearnerHandlerInfo()); + } + } + return info; + } + + public void resetObserverConnectionStats() { + synchronized(observingLearners) { + for(LearnerHandler lh : observingLearners) { + lh.resetObserverConnectionStats(); + } + } + } + + // TODO_MA 注释: 待处理的 sync 请求列表 + // Pending sync requests. Must access under 'this' lock. + private final Map> pendingSyncs = new HashMap>(); + + public synchronized int getNumPendingSyncs() { + return pendingSyncs.size(); + } + + //Follower counter + final AtomicLong followerCounter = new AtomicLong(-1); + + /** + * Adds peer to the leader. + * + * @param learner instance of learner handle + */ + @Override + public void addLearnerHandler(LearnerHandler learner) { + synchronized(learners) { + learners.add(learner); + } + } + + /** + * Remove the learner from the learner list + * + * @param peer + */ + @Override + public void removeLearnerHandler(LearnerHandler peer) { + synchronized(forwardingFollowers) { + forwardingFollowers.remove(peer); + } + synchronized(learners) { + learners.remove(peer); + } + synchronized(observingLearners) { + observingLearners.remove(peer); + } + } + + boolean isLearnerSynced(LearnerHandler peer) { + synchronized(forwardingFollowers) { + return forwardingFollowers.contains(peer); + } + } + + /** + * Returns true if a quorum in qv is connected and synced with the leader + * and false otherwise + * + * @param qv, a QuorumVerifier + */ + public boolean isQuorumSynced(QuorumVerifier qv) { + HashSet ids = new HashSet(); + if(qv.getVotingMembers().containsKey(self.getId())) { + ids.add(self.getId()); + } + synchronized(forwardingFollowers) { + for(LearnerHandler learnerHandler : forwardingFollowers) { + if(learnerHandler.synced() && qv.getVotingMembers().containsKey(learnerHandler.getSid())) { + ids.add(learnerHandler.getSid()); + } + } + } + return qv.containsQuorum(ids); + } + + private final List serverSockets = new LinkedList<>(); + + public Leader(QuorumPeer self, LeaderZooKeeperServer zk) throws IOException { + this.self = self; + this.proposalStats = new BufferStats(); + + Set addresses; + if(self.getQuorumListenOnAllIPs()) { + addresses = self.getQuorumAddress().getWildcardAddresses(); + } else { + addresses = self.getQuorumAddress().getAllAddresses(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 根据地址个数,生成多个 ServerSocket,然后加入到 serverSockets 集合中 + */ + addresses.stream() + .map(address -> createServerSocket(address, self.shouldUsePortUnification(), self.isSslQuorum())) + .filter(Optional::isPresent).map(Optional::get).forEach(serverSockets::add); + + if(serverSockets.isEmpty()) { + throw new IOException("Leader failed to initialize any of the following sockets: " + addresses); + } + + this.zk = zk; + } + + Optional createServerSocket(InetSocketAddress address, boolean portUnification, boolean sslQuorum) { + ServerSocket serverSocket; + try { + if(portUnification || sslQuorum) { + serverSocket = new UnifiedServerSocket(self.getX509Util(), portUnification); + } else { + serverSocket = new ServerSocket(); + } + serverSocket.setReuseAddress(true); + serverSocket.bind(address); + return Optional.of(serverSocket); + } catch(IOException e) { + LOG.error("Couldn't bind to {}", address.toString(), e); + } + return Optional.empty(); + } + + /** + * This message is for follower to expect diff + */ + static final int DIFF = 13; + + /** + * This is for follower to truncate its logs + */ + static final int TRUNC = 14; + + /** + * This is for follower to download the snapshots + */ + static final int SNAP = 15; + + /** + * This tells the leader that the connecting peer is actually an observer + */ + static final int OBSERVERINFO = 16; + + /** + * This message type is sent by the leader to indicate it's zxid and if + * needed, its database. + */ + static final int NEWLEADER = 10; + + /** + * This message type is sent by a follower to pass the last zxid. This is here + * for backward compatibility purposes. + */ + static final int FOLLOWERINFO = 11; + + /** + * This message type is sent by the leader to indicate that the follower is + * now uptodate andt can start responding to clients. + */ + static final int UPTODATE = 12; + + /** + * This message is the first that a follower receives from the leader. + * It has the protocol version and the epoch of the leader. + */ + public static final int LEADERINFO = 17; + + /** + * This message is used by the follow to ack a proposed epoch. + */ + public static final int ACKEPOCH = 18; + + /** + * This message type is sent to a leader to request and mutation operation. + * The payload will consist of a request header followed by a request. + */ + static final int REQUEST = 1; + + /** + * This message type is sent by a leader to propose a mutation. + */ + public static final int PROPOSAL = 2; + + /** + * This message type is sent by a follower after it has synced a proposal. + */ + static final int ACK = 3; + + /** + * This message type is sent by a leader to commit a proposal and cause + * followers to start serving the corresponding data. + */ + static final int COMMIT = 4; + + /** + * This message type is enchanged between follower and leader (initiated by + * follower) to determine liveliness. + */ + static final int PING = 5; + + /** + * This message type is to validate a session that should be active. + */ + static final int REVALIDATE = 6; + + /** + * This message is a reply to a synchronize command flushing the pipe + * between the leader and the follower. + */ + static final int SYNC = 7; + + /** + * This message type informs observers of a committed proposal. + */ + static final int INFORM = 8; + + /** + * Similar to COMMIT, only for a reconfig operation. + */ + static final int COMMITANDACTIVATE = 9; + + /** + * Similar to INFORM, only for a reconfig operation. + */ + static final int INFORMANDACTIVATE = 19; + + final ConcurrentMap outstandingProposals = new ConcurrentHashMap(); + + private final ConcurrentLinkedQueue toBeApplied = new ConcurrentLinkedQueue(); + + // VisibleForTesting + protected final Proposal newLeaderProposal = new Proposal(); + + class LearnerCnxAcceptor extends ZooKeeperCriticalThread { + + private final AtomicBoolean stop = new AtomicBoolean(false); + private final AtomicBoolean fail = new AtomicBoolean(false); + + LearnerCnxAcceptor() { + super("LearnerCnxAcceptor-" + serverSockets.stream().map(ServerSocket::getLocalSocketAddress) + .map(Objects::toString).collect(Collectors.joining("|")), zk.getZooKeeperServerListener()); + } + + @Override + public void run() { + if(!stop.get() && !serverSockets.isEmpty()) { + + // TODO_MA 注释: 线程池 + ExecutorService executor = Executors.newFixedThreadPool(serverSockets.size()); + CountDownLatch latch = new CountDownLatch(serverSockets.size()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 使用一个线程来完成 Leader 和 Follower 之间的状态同步 + * LearnerCnxAcceptorHandler 正常来说,只有一个,这个线程的内部,启动了一个 ServerSocket + * - + * serverSocket 是从 serverSockets 这个 list 集合中遍历到的一个元素 + */ + serverSockets + .forEach(serverSocket -> executor.submit(new LearnerCnxAcceptorHandler(serverSocket, latch))); + + try { + latch.await(); + } catch(InterruptedException ie) { + LOG.error("Interrupted while sleeping in LearnerCnxAcceptor.", ie); + } finally { + closeSockets(); + executor.shutdown(); + try { + if(!executor.awaitTermination(1, TimeUnit.SECONDS)) { + LOG.error("not all the LearnerCnxAcceptorHandler terminated properly"); + } + } catch(InterruptedException ie) { + LOG.error("Interrupted while terminating LearnerCnxAcceptor.", ie); + } + } + } + } + + public void halt() { + stop.set(true); + closeSockets(); + } + + class LearnerCnxAcceptorHandler implements Runnable { + + // TODO_MA 注释: 一个 LearnerCnxAcceptorHandler 运行一个 ServerSocket BIO 服务端 + private ServerSocket serverSocket; + private CountDownLatch latch; + + LearnerCnxAcceptorHandler(ServerSocket serverSocket, CountDownLatch latch) { + this.serverSocket = serverSocket; + this.latch = latch; + } + + @Override + public void run() { + try { + Thread.currentThread().setName("LearnerCnxAcceptorHandler-" + serverSocket.getLocalSocketAddress()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 不停接受 Follower 的链接请求 + * 1、完成链接 + * 2、封装和启动一个 LearnerHandler 线程专门对该 Follower 提供数据同步服务 + */ + while(!stop.get()) { + + // TODO_MA 注释: 接收一个 follower 的 BIO 客户端的链接 + acceptConnections(); + + // TODO_MA 注释: 创建 ServreSocket 对象 + // TODO_MA 注释: 调用 accept() 等待链接请求 + } + } catch(Exception e) { + LOG.warn("Exception while accepting follower", e); + if(fail.compareAndSet(false, true)) { + handleException(getName(), e); + halt(); + } + } finally { + latch.countDown(); + } + } + + private void acceptConnections() throws IOException { + Socket socket = null; + boolean error = false; + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 接受一个客户端的链接 + */ + socket = serverSocket.accept(); + + // start with the initLimit, once the ack is processed + // in LearnerHandler switch to the syncLimit + socket.setSoTimeout(self.tickTime * self.initLimit); + socket.setTcpNoDelay(nodelay); + + BufferedInputStream is = new BufferedInputStream(socket.getInputStream()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动一个 LearnerHandler 线程用来完成和客户端的状态同步 + */ + LearnerHandler fh = new LearnerHandler(socket, is, Leader.this); + fh.start(); + } catch(SocketException e) { + error = true; + if(stop.get()) { + LOG.warn("Exception while shutting down acceptor.", e); + } else { + throw e; + } + } catch(SaslException e) { + LOG.error("Exception while connecting to quorum learner", e); + error = true; + } catch(Exception e) { + error = true; + throw e; + } finally { + // Don't leak sockets on errors + if(error && socket != null && !socket.isClosed()) { + try { + socket.close(); + } catch(IOException e) { + LOG.warn("Error closing socket: " + socket, e); + } + } + } + } + + } + + } + + StateSummary leaderStateSummary; + + long epoch = -1; + boolean waitingForNewEpoch = true; + + // when a reconfig occurs where the leader is removed or becomes an observer, + // it does not commit ops after committing the reconfig + boolean allowedToCommit = true; + + /** + * Timestamp when this leader started serving request (Quorum is running) + */ + private long leaderStartTime; + + public long getUptime() { + if(leaderStartTime > 0) { + return Time.currentElapsedTime() - leaderStartTime; + } + return 0; + } + + /** + * This method is main function that is called to lead + * + * @throws IOException + * @throws InterruptedException + */ + void lead() throws IOException, InterruptedException { + self.end_fle = Time.currentElapsedTime(); + long electionTimeTaken = self.end_fle - self.start_fle; + self.setElectionTimeTaken(electionTimeTaken); + ServerMetrics.getMetrics().ELECTION_TIME.add(electionTimeTaken); + LOG.info("LEADING - LEADER ELECTION TOOK - {} {}", electionTimeTaken, QuorumPeer.FLE_TIME_UNIT); + self.start_fle = 0; + self.end_fle = 0; + + zk.registerJMX(new LeaderBean(this, zk), self.jmxLocalPeerBean); + + try { + // TODO_MA 注释: 切换到 DISCOVERY 状态 + self.setZabState(QuorumPeer.ZabState.DISCOVERY); + self.tick.set(0); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 加载得到数据 + */ + zk.loadData(); + // TODO_MA 注释: leader的重要信息: epoch + zxid + leaderStateSummary = new StateSummary(self.getCurrentEpoch(), zk.getLastProcessedZxid()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动一个线程,用来接受 Follower 发送过来的链接请求 + */ + // Start thread that waits for connection requests from new followers. + cnxAcceptor = new LearnerCnxAcceptor(); + cnxAcceptor.start(); + + // TODO_MA 注释: 获取 epoch 和 zxid,Follower 和 Leader 完成同步的重要依据 + long epoch = getEpochToPropose(self.getId(), self.getAcceptedEpoch()); + zk.setZxid(ZxidUtils.makeZxid(epoch, 0)); + synchronized(this) { + lastProposed = zk.getZxid(); + } + + // TODO_MA 注释: QuorumPacket zk 服务器之间进行一次网络通信的一个抽象对象! + newLeaderProposal.packet = new QuorumPacket(NEWLEADER, zk.getZxid(), null, null); + if((newLeaderProposal.packet.getZxid() & 0xffffffffL) != 0) { + LOG.info("NEWLEADER proposal has Zxid of {}", Long.toHexString(newLeaderProposal.packet.getZxid())); + } + + QuorumVerifier lastSeenQV = self.getLastSeenQuorumVerifier(); + QuorumVerifier curQV = self.getQuorumVerifier(); + if(curQV.getVersion() == 0 && curQV.getVersion() == lastSeenQV.getVersion()) { + // This was added in ZOOKEEPER-1783. The initial config has version 0 (not explicitly + // specified by the user; the lack of version in a config file is interpreted as version=0). + // As soon as a config is established we would like to increase its version so that it + // takes presedence over other initial configs that were not established (such as a config + // of a server trying to join the ensemble, which may be a partial view of the system, not the full config). + // We chose to set the new version to the one of the NEWLEADER message. However, before we can do that + // there must be agreement on the new version, so we can only change the version when sending/receiving UPTODATE, + // not when sending/receiving NEWLEADER. In other words, we can't change curQV here since its the committed quorum verifier, + // and there's still no agreement on the new version that we'd like to use. Instead, we use + // lastSeenQuorumVerifier which is being sent with NEWLEADER message + // so its a good way to let followers know about the new version. (The original reason for sending + // lastSeenQuorumVerifier with NEWLEADER is so that the leader completes any potentially uncommitted reconfigs + // that it finds before starting to propose operations. Here we're reusing the same code path for + // reaching consensus on the new version number.) + + // It is important that this is done before the leader executes waitForEpochAck, + // so before LearnerHandlers return from their waitForEpochAck + // hence before they construct the NEWLEADER message containing + // the last-seen-quorumverifier of the leader, which we change below + try { + LOG.debug( + String.format("set lastSeenQuorumVerifier to currentQuorumVerifier (%s)", curQV.toString())); + QuorumVerifier newQV = self.configFromString(curQV.toString()); + newQV.setVersion(zk.getZxid()); + self.setLastSeenQuorumVerifier(newQV, true); + } catch(Exception e) { + throw new IOException(e); + } + } + + newLeaderProposal.addQuorumVerifier(self.getQuorumVerifier()); + if(self.getLastSeenQuorumVerifier().getVersion() > self.getQuorumVerifier().getVersion()) { + newLeaderProposal.addQuorumVerifier(self.getLastSeenQuorumVerifier()); + } + + // We have to get at least a majority of servers in sync with + // us. We do this by waiting for the NEWLEADER packet to get acknowledged + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: Leader 等待所有的 ACKEPOCH 通知反馈 + * 通俗解释:选举终于结束 + */ + waitForEpochAck(self.getId(), leaderStateSummary); + + // TODO_MA 注释: ZAB 的 DISCOVERY 状态结束,进入 SYNCHRONIZATION 状态 + self.setCurrentEpoch(epoch); + self.setLeaderAddressAndId(self.getQuorumAddress(), self.getId()); + self.setZabState(QuorumPeer.ZabState.SYNCHRONIZATION); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: Leader 等待所有的 NEWLEADER 通知反馈 + * 通俗解释:状态同步终于结束 + */ + try { + waitForNewLeaderAck(self.getId(), zk.getZxid()); + } catch(InterruptedException e) { + shutdown("Waiting for a quorum of followers, only synced with sids: [ " + newLeaderProposal + .ackSetsToString() + " ]"); + HashSet followerSet = new HashSet(); + + for(LearnerHandler f : getLearners()) { + if(self.getQuorumVerifier().getVotingMembers().containsKey(f.getSid())) { + followerSet.add(f.getSid()); + } + } + boolean initTicksShouldBeIncreased = true; + for(Proposal.QuorumVerifierAcksetPair qvAckset : newLeaderProposal.qvAcksetPairs) { + if(!qvAckset.getQuorumVerifier().containsQuorum(followerSet)) { + initTicksShouldBeIncreased = false; + break; + } + } + if(initTicksShouldBeIncreased) { + LOG.warn("Enough followers present. Perhaps the initTicks need to be increased."); + } + return; + } + + // TODO_MA 注释: 所以,到此为止,意味着,集群有超过半数节点完成了和 Leader 的状态同步 + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 Leader 中 ZooKeeperServer 实例中的各种服务 + */ + startZkServer(); + + /** + * WARNING: do not use this for anything other than QA testing + * on a real cluster. Specifically to enable verification that quorum + * can handle the lower 32bit roll-over issue identified in + * ZOOKEEPER-1277. Without this option it would take a very long + * time (on order of a month say) to see the 4 billion writes + * necessary to cause the roll-over to occur. + * + * This field allows you to override the zxid of the server. Typically + * you'll want to set it to something like 0xfffffff0 and then + * start the quorum, run some operations and see the re-election. + */ + String initialZxid = System.getProperty("zookeeper.testingonly.initialZxid"); + if(initialZxid != null) { + long zxid = Long.parseLong(initialZxid); + zk.setZxid((zk.getZxid() & 0xffffffff00000000L) | zxid); + } + + if(!System.getProperty("zookeeper.leaderServes", "yes").equals("no")) { + self.setZooKeeperServer(zk); + } + + // TODO_MA 注释: 进入 ZAB 的 BROADCAST 模式 + self.setZabState(QuorumPeer.ZabState.BROADCAST); + self.adminServer.setZooKeeperServer(zk); + + // Everything is a go, simply start counting the ticks + // WARNING: I couldn't find any wait statement on a synchronized + // block that would be notified by this notifyAll() call, so + // I commented it out + //synchronized (this) { + // notifyAll(); + //} + // We ping twice a tick, so we only update the tick every other iteration + boolean tickSkip = true; + // If not null then shutdown this leader + String shutdownMessage = null; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 进入心跳工作机制 + */ + while(true) { + synchronized(this) { + long start = Time.currentElapsedTime(); + long cur = start; + long end = start + self.tickTime / 2; + while(cur < end) { + wait(end - cur); + cur = Time.currentElapsedTime(); + } + + if(!tickSkip) { + self.tick.incrementAndGet(); + } + + // We use an instance of SyncedLearnerTracker to + // track synced learners to make sure we still have a + // quorum of current (and potentially next pending) view. + SyncedLearnerTracker syncedAckSet = new SyncedLearnerTracker(); + syncedAckSet.addQuorumVerifier(self.getQuorumVerifier()); + if(self.getLastSeenQuorumVerifier() != null && self.getLastSeenQuorumVerifier().getVersion() > self + .getQuorumVerifier().getVersion()) { + syncedAckSet.addQuorumVerifier(self.getLastSeenQuorumVerifier()); + } + + syncedAckSet.addAck(self.getId()); + + // TODO_MA 注释: 遍历每个 Follower 判断是否是同步状态 + for(LearnerHandler f : getLearners()) { + + // TODO_MA 注释: 如果是同步状态,则添加进入 syncedAckSet 集合 + // TODO_MA 注释: 如果这个集合的大小,小于集群的半数节点,则集群要关闭 + if(f.synced()) { + syncedAckSet.addAck(f.getSid()); + } + } + + // check leader running status + if(!this.isRunning()) { + // set shutdown flag + shutdownMessage = "Unexpected internal error"; + break; + } + + // TODO_MA 注释: 大于集群半数节点的服务器没有心跳了 + if(!tickSkip && !syncedAckSet.hasAllQuorums()) { + // Lost quorum of last committed and/or last proposed config, set shutdown flag + shutdownMessage = "Not sufficient followers synced, only synced with sids: [ " + syncedAckSet + .ackSetsToString() + " ]"; + + // TODO_MA 注释: 如果执行了 break 了,意味着要切换自己的状态为 LOOKING 了 + // TODO_MA 注释: 集群不能正常对外提供服务了 + break; + + // TODO_MA 注释: 当前的 while 循环执行完了 + // TODO_MA 注释: 表示 leader 的 lead() 方法也执行完了。 + // TODO_MA 注释: 则该 leader 的状态,必然发生改变,继续进入 LOOKING 执行 lookForLeader 方法找 leader + } + tickSkip = !tickSkip; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送心跳消息给每个 Follower + */ + for(LearnerHandler f : getLearners()) { + f.ping(); + } + } + if(shutdownMessage != null) { + shutdown(shutdownMessage); + // leader goes in looking state + } + } finally { + zk.unregisterJMX(this); + } + } + + boolean isShutdown; + + /** + * Close down all the LearnerHandlers + */ + void shutdown(String reason) { + LOG.info("Shutting down"); + + if(isShutdown) { + return; + } + + LOG.info("Shutdown called. For the reason {}", reason); + + if(cnxAcceptor != null) { + cnxAcceptor.halt(); + } else { + closeSockets(); + } + + // NIO should not accept conenctions + self.setZooKeeperServer(null); + self.adminServer.setZooKeeperServer(null); + self.closeAllConnections(); + // shutdown the previous zk + if(zk != null) { + zk.shutdown(); + } + synchronized(learners) { + for(Iterator it = learners.iterator(); it.hasNext(); ) { + LearnerHandler f = it.next(); + it.remove(); + f.shutdown(); + } + } + isShutdown = true; + } + + synchronized void closeSockets() { + for(ServerSocket serverSocket : serverSockets) { + if(!serverSocket.isClosed()) { + try { + serverSocket.close(); + } catch(IOException e) { + LOG.warn("Ignoring unexpected exception during close {}", serverSocket, e); + } + } + } + } + + /** + * In a reconfig operation, this method attempts to find the best leader for next configuration. + * If the current leader is a voter in the next configuartion, then it remains the leader. + * Otherwise, choose one of the new voters that acked the reconfiguartion, such that it is as + * up-to-date as possible, i.e., acked as many outstanding proposals as possible. + * + * @param reconfigProposal + * @param zxid of the reconfigProposal + * @return server if of the designated leader + */ + + private long getDesignatedLeader(Proposal reconfigProposal, long zxid) { + //new configuration + Proposal.QuorumVerifierAcksetPair newQVAcksetPair = reconfigProposal.qvAcksetPairs + .get(reconfigProposal.qvAcksetPairs.size() - 1); + + //check if I'm in the new configuration with the same quorum address - + // if so, I'll remain the leader + if(newQVAcksetPair.getQuorumVerifier().getVotingMembers().containsKey(self.getId()) && newQVAcksetPair + .getQuorumVerifier().getVotingMembers().get(self.getId()).addr.equals(self.getQuorumAddress())) { + return self.getId(); + } + // start with an initial set of candidates that are voters from new config that + // acknowledged the reconfig op (there must be a quorum). Choose one of them as + // current leader candidate + HashSet candidates = new HashSet(newQVAcksetPair.getAckset()); + candidates.remove(self.getId()); // if we're here, I shouldn't be the leader + long curCandidate = candidates.iterator().next(); + + //go over outstanding ops in order, and try to find a candidate that acked the most ops. + //this way it will be the most up-to-date and we'll minimize the number of ops that get dropped + + long curZxid = zxid + 1; + Proposal p = outstandingProposals.get(curZxid); + + while(p != null && !candidates.isEmpty()) { + for(Proposal.QuorumVerifierAcksetPair qvAckset : p.qvAcksetPairs) { + //reduce the set of candidates to those that acknowledged p + candidates.retainAll(qvAckset.getAckset()); + //no candidate acked p, return the best candidate found so far + if(candidates.isEmpty()) { + return curCandidate; + } + //update the current candidate, and if it is the only one remaining, return it + curCandidate = candidates.iterator().next(); + if(candidates.size() == 1) { + return curCandidate; + } + } + curZxid++; + p = outstandingProposals.get(curZxid); + } + + return curCandidate; + } + + /** + * @return True if committed, otherwise false. + **/ + public synchronized boolean tryToCommit(Proposal p, long zxid, SocketAddress followerAddr) { + // make sure that ops are committed in order. With reconfigurations it is now possible + // that different operations wait for different sets of acks, and we still want to enforce + // that they are committed in order. Currently we only permit one outstanding reconfiguration + // such that the reconfiguration and subsequent outstanding ops proposed while the reconfig is + // pending all wait for a quorum of old and new config, so it's not possible to get enough acks + // for an operation without getting enough acks for preceding ops. But in the future if multiple + // concurrent reconfigs are allowed, this can happen. + if(outstandingProposals.containsKey(zxid - 1)) { + return false; + } + + // in order to be committed, a proposal must be accepted by a quorum. + // TODO_MA 注释: 没到一半,直接返回 + // getting a quorum from all necessary configurations. + if(!p.hasAllQuorums()) { + return false; + } + + // commit proposals in order + if(zxid != lastCommitted + 1) { + LOG.warn("Commiting zxid 0x{} from {} noy first!", Long.toHexString(zxid), followerAddr); + LOG.warn("First is {}", (lastCommitted + 1)); + } + + outstandingProposals.remove(zxid); + + if(p.request != null) { + toBeApplied.add(p); + } + + if(p.request == null) { + LOG.warn("Going to commit null: {}", p); + } else if(p.request.getHdr().getType() == OpCode.reconfig) { + LOG.debug("Committing a reconfiguration! {}", outstandingProposals.size()); + + //if this server is voter in new config with the same quorum address, + //then it will remain the leader + //otherwise an up-to-date follower will be designated as leader. This saves + //leader election time, unless the designated leader fails + Long designatedLeader = getDesignatedLeader(p, zxid); + //LOG.warn("designated leader is: " + designatedLeader); + + QuorumVerifier newQV = p.qvAcksetPairs.get(p.qvAcksetPairs.size() - 1).getQuorumVerifier(); + + self.processReconfig(newQV, designatedLeader, zk.getZxid(), true); + + if(designatedLeader != self.getId()) { + LOG.info(String.format( + "Committing a reconfiguration (reconfigEnabled=%s); this leader is not the designated " + "leader anymore, setting allowedToCommit=false", + self.isReconfigEnabled())); + allowedToCommit = false; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + // we're sending the designated leader, and if the leader is changing the followers are + // responsible for closing the connection - this way we are sure that at least a majority of them + // receive the commit message. + commitAndActivate(zxid, designatedLeader); + informAndActivate(p, designatedLeader); + //turnOffFollowers(); + } else { + p.request.logLatency(ServerMetrics.getMetrics().QUORUM_ACK_LATENCY); + commit(zxid); + inform(p); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + zk.commitProcessor.commit(p.request); + if(pendingSyncs.containsKey(zxid)) { + for(LearnerSyncRequest r : pendingSyncs.remove(zxid)) { + sendSync(r); + } + } + + return true; + } + + /** + * Keep a count of acks that are received by the leader for a particular + * proposal + * + * @param zxid, the zxid of the proposal sent out + * @param sid, the id of the server that sent the ack + * @param followerAddr + */ + @Override + public synchronized void processAck(long sid, long zxid, SocketAddress followerAddr) { + if(!allowedToCommit) { + return; // last op committed was a leader change - from now on + } + // the new leader should commit + if(LOG.isTraceEnabled()) { + LOG.trace("Ack zxid: 0x{}", Long.toHexString(zxid)); + for(Proposal p : outstandingProposals.values()) { + long packetZxid = p.packet.getZxid(); + LOG.trace("outstanding proposal: 0x{}", Long.toHexString(packetZxid)); + } + LOG.trace("outstanding proposals all"); + } + + if((zxid & 0xffffffffL) == 0) { + /* + * We no longer process NEWLEADER ack with this method. However, + * the learner sends an ack back to the leader after it gets + * UPTODATE, so we just ignore the message. + */ + return; + } + + if(outstandingProposals.size() == 0) { + LOG.debug("outstanding is 0"); + return; + } + if(lastCommitted >= zxid) { + LOG.debug("proposal has already been committed, pzxid: 0x{} zxid: 0x{}", Long.toHexString(lastCommitted), + Long.toHexString(zxid)); + // The proposal has already been committed + return; + } + + // TODO_MA 注释: 获取 proposal 出来 + Proposal p = outstandingProposals.get(zxid); + if(p == null) { + LOG.warn("Trying to commit future proposal: zxid 0x{} from {}", Long.toHexString(zxid), followerAddr); + return; + } + + if(ackLoggingFrequency > 0 && (zxid % ackLoggingFrequency == 0)) { + p.request.logLatency(ServerMetrics.getMetrics().ACK_LATENCY, Long.toString(sid)); + } + + // TODO_MA 注释: 添加一个 ack + // TODO_MA 注释: 在 Proposal 内部有一个 ack set 集合 + // TODO_MA 注释: 存放的是执行 事务成功的那些 follower 节点的 id + p.addAck(sid); + + // TODO_MA 注释: 只要成功接收到一个 ack 就会做u一次判断 + boolean hasCommitted = tryToCommit(p, zxid, followerAddr); + + // If p is a reconfiguration, multiple other operations may be ready to be committed, + // since operations wait for different sets of acks. + // Currently we only permit one outstanding reconfiguration at a time + // such that the reconfiguration and subsequent outstanding ops proposed while the reconfig is + // pending all wait for a quorum of old and new config, so its not possible to get enough acks + // for an operation without getting enough acks for preceding ops. But in the future if multiple + // concurrent reconfigs are allowed, this can happen and then we need to check whether some pending + // ops may already have enough acks and can be committed, which is what this code does. + + if(hasCommitted && p.request != null && p.request.getHdr().getType() == OpCode.reconfig) { + long curZxid = zxid; + while(allowedToCommit && hasCommitted && p != null) { + curZxid++; + p = outstandingProposals.get(curZxid); + if(p != null) { + hasCommitted = tryToCommit(p, curZxid, null); + } + } + } + } + + static class ToBeAppliedRequestProcessor implements RequestProcessor { + + // TODO_MA 注释: next = final + private final RequestProcessor next; + + private final Leader leader; + + /** + * This request processor simply maintains the toBeApplied list. For + * this to work next must be a FinalRequestProcessor and + * FinalRequestProcessor.processRequest MUST process the request + * synchronously! + * + * @param next a reference to the FinalRequestProcessor + */ + ToBeAppliedRequestProcessor(RequestProcessor next, Leader leader) { + if(!(next instanceof FinalRequestProcessor)) { + throw new RuntimeException(ToBeAppliedRequestProcessor.class + .getName() + " must be connected to " + FinalRequestProcessor.class.getName() + " not " + next + .getClass().getName()); + } + this.leader = leader; + this.next = next; + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.RequestProcessor#processRequest(org.apache.zookeeper.server.Request) + */ + public void processRequest(Request request) throws RequestProcessorException { + + // TODO_MA 注释: next = final + next.processRequest(request); + + // The only requests that should be on toBeApplied are write + // requests, for which we will have a hdr. We can't simply use + // request.zxid here because that is set on read requests to equal + // the zxid of the last write op. + if(request.getHdr() != null) { + long zxid = request.getHdr().getZxid(); + Iterator iter = leader.toBeApplied.iterator(); + if(iter.hasNext()) { + Proposal p = iter.next(); + if(p.request != null && p.request.zxid == zxid) { + iter.remove(); + return; + } + } + LOG.error("Committed request not found on toBeApplied: {}", request); + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.server.RequestProcessor#shutdown() + */ + public void shutdown() { + LOG.info("Shutting down"); + next.shutdown(); + } + + } + + /** + * send a packet to all the followers ready to follow + * + * @param qp the packet to be sent + */ + void sendPacket(QuorumPacket qp) { + synchronized(forwardingFollowers) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 将 PROPOSAL 广播给所有 Follower + */ + for(LearnerHandler f : forwardingFollowers) { + f.queuePacket(qp); + } + } + } + + /** + * send a packet to all observers + */ + void sendObserverPacket(QuorumPacket qp) { + for(LearnerHandler f : getObservingLearners()) { + f.queuePacket(qp); + } + } + + long lastCommitted = -1; + + /** + * Create a commit packet and send it to all the members of the quorum + * + * @param zxid + */ + public void commit(long zxid) { + synchronized(this) { + lastCommitted = zxid; + } + QuorumPacket qp = new QuorumPacket(Leader.COMMIT, zxid, null, null); + sendPacket(qp); + ServerMetrics.getMetrics().COMMIT_COUNT.add(1); + } + + //commit and send some info + public void commitAndActivate(long zxid, long designatedLeader) { + synchronized(this) { + lastCommitted = zxid; + } + + byte[] data = new byte[8]; + ByteBuffer buffer = ByteBuffer.wrap(data); + buffer.putLong(designatedLeader); + + QuorumPacket qp = new QuorumPacket(Leader.COMMITANDACTIVATE, zxid, data, null); + sendPacket(qp); + } + + /** + * Create an inform packet and send it to all observers. + */ + public void inform(Proposal proposal) { + QuorumPacket qp = new QuorumPacket(Leader.INFORM, proposal.request.zxid, proposal.packet.getData(), null); + sendObserverPacket(qp); + } + + public static QuorumPacket buildInformAndActivePacket(long zxid, long designatedLeader, byte[] proposalData) { + byte[] data = new byte[proposalData.length + 8]; + ByteBuffer buffer = ByteBuffer.wrap(data); + buffer.putLong(designatedLeader); + buffer.put(proposalData); + + return new QuorumPacket(Leader.INFORMANDACTIVATE, zxid, data, null); + } + + /** + * Create an inform and activate packet and send it to all observers. + */ + public void informAndActivate(Proposal proposal, long designatedLeader) { + sendObserverPacket( + buildInformAndActivePacket(proposal.request.zxid, designatedLeader, proposal.packet.getData())); + } + + long lastProposed; + + @Override + public synchronized long getLastProposed() { + return lastProposed; + } + + /** + * Returns the current epoch of the leader. + * + * @return + */ + public long getEpoch() { + return ZxidUtils.getEpochFromZxid(lastProposed); + } + + @SuppressWarnings("serial") + public static class XidRolloverException extends Exception { + + public XidRolloverException(String message) { + super(message); + } + + } + + /** + * create a proposal and send it out to all the members + * + * @param request + * @return the proposal that is queued to send to all the members + */ + public Proposal propose(Request request) throws XidRolloverException { + /** + * Address the rollover issue. All lower 32bits set indicate a new leader + * election. Force a re-election instead. See ZOOKEEPER-1277 + */ + if((request.zxid & 0xffffffffL) == 0xffffffffL) { + String msg = "zxid lower 32 bits have rolled over, forcing re-election, and therefore new epoch start"; + shutdown(msg); + throw new XidRolloverException(msg); + } + + byte[] data = SerializeUtils.serializeRequest(request); + proposalStats.setLastBufferSize(data.length); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 构建 PROPOSAL 请求 + */ + QuorumPacket pp = new QuorumPacket(Leader.PROPOSAL, request.zxid, data, null); + Proposal p = new Proposal(); + p.packet = pp; + p.request = request; + + synchronized(this) { + p.addQuorumVerifier(self.getQuorumVerifier()); + + if(request.getHdr().getType() == OpCode.reconfig) { + self.setLastSeenQuorumVerifier(request.qv, true); + } + + if(self.getQuorumVerifier().getVersion() < self.getLastSeenQuorumVerifier().getVersion()) { + p.addQuorumVerifier(self.getLastSeenQuorumVerifier()); + } + + LOG.debug("Proposing:: {}", request); + + lastProposed = p.packet.getZxid(); + outstandingProposals.put(lastProposed, p); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送 PROPOSAL 请求 + */ + sendPacket(pp); + } + ServerMetrics.getMetrics().PROPOSAL_COUNT.add(1); + return p; + } + + /** + * Process sync requests + * + * @param r the request + */ + + public synchronized void processSync(LearnerSyncRequest r) { + + // TODO_MA 注释: + if(outstandingProposals.isEmpty()) { + sendSync(r); + } else { + List l = pendingSyncs.get(lastProposed); + if(l == null) { + l = new ArrayList(); + } + l.add(r); + pendingSyncs.put(lastProposed, l); + } + } + + /** + * Sends a sync message to the appropriate server + */ + public void sendSync(LearnerSyncRequest r) { + QuorumPacket qp = new QuorumPacket(Leader.SYNC, 0, null, null); + r.fh.queuePacket(qp); + } + + /** + * lets the leader know that a follower is capable of following and is done syncing + * + * @param handler handler of the follower + * @return last proposed zxid + */ + @Override + public synchronized long startForwarding(LearnerHandler handler, long lastSeenZxid) { + // Queue up any outstanding requests enabling the receipt of new requests + if(lastProposed > lastSeenZxid) { + + // TODO_MA 注释: 广播 Proposal 给 Follower + // TODO_MA 注释: 对于每一条事务,都放入一条 PROPOSAL 和一条 COMMIT 消息 + for(Proposal p : toBeApplied) { + if(p.packet.getZxid() <= lastSeenZxid) { + continue; + } + + // TODO_MA 注释: 发送事务 + handler.queuePacket(p.packet); + // Since the proposal has been committed we need to send the commit message also + + // TODO_MA 注释: 发送提交命令 + QuorumPacket qp = new QuorumPacket(Leader.COMMIT, p.packet.getZxid(), null, null); + handler.queuePacket(qp); + } + // Only participant need to get outstanding proposals + if(handler.getLearnerType() == LearnerType.PARTICIPANT) { + List zxids = new ArrayList(outstandingProposals.keySet()); + Collections.sort(zxids); + for(Long zxid : zxids) { + if(zxid <= lastSeenZxid) { + continue; + } + handler.queuePacket(outstandingProposals.get(zxid).packet); + } + } + } + if(handler.getLearnerType() == LearnerType.PARTICIPANT) { + addForwardingFollower(handler); + } else { + addObserverLearnerHandler(handler); + } + + return lastProposed; + } + + @Override + public void waitForStartup() throws InterruptedException { + synchronized(zk) { + while(!zk.isRunning() && !Thread.currentThread().isInterrupted()) { + zk.wait(20); + } + } + } + + // VisibleForTesting + protected final Set connectingFollowers = new HashSet(); + + private volatile boolean quitWaitForEpoch = false; + private volatile long timeStartWaitForEpoch = -1; + private volatile SyncedLearnerTracker voteSet; + + public static final String MAX_TIME_TO_WAIT_FOR_EPOCH = "zookeeper.leader.maxTimeToWaitForEpoch"; + private static int maxTimeToWaitForEpoch; + + static { + maxTimeToWaitForEpoch = Integer.getInteger(MAX_TIME_TO_WAIT_FOR_EPOCH, -1); + LOG.info("{} = {}ms", MAX_TIME_TO_WAIT_FOR_EPOCH, maxTimeToWaitForEpoch); + } + + // visible for test + public static void setMaxTimeToWaitForEpoch(int maxTimeToWaitForEpoch) { + Leader.maxTimeToWaitForEpoch = maxTimeToWaitForEpoch; + LOG.info("Set {} to {}ms", MAX_TIME_TO_WAIT_FOR_EPOCH, Leader.maxTimeToWaitForEpoch); + } + + /** + * Quit condition: + * + * 1 voter goes to looking again and time waitForEpoch > maxTimeToWaitForEpoch + * + * Note: the voter may go to looking again in case of: + * 1. change mind in the last minute when received a different notification + * 2. the leader hadn't started leading when it tried to connect to it + * 3. connection broken between the voter and leader + * 4. voter being shutdown or restarted + */ + private void quitLeading() { + synchronized(connectingFollowers) { + quitWaitForEpoch = true; + connectingFollowers.notifyAll(); + } + ServerMetrics.getMetrics().QUIT_LEADING_DUE_TO_DISLOYAL_VOTER.add(1); + LOG.info("Quit leading due to voter changed mind."); + } + + public void setLeadingVoteSet(SyncedLearnerTracker voteSet) { + this.voteSet = voteSet; + } + + public void reportLookingSid(long sid) { + if(maxTimeToWaitForEpoch < 0 || timeStartWaitForEpoch < 0 || !waitingForNewEpoch) { + return; + } + if(voteSet == null || !voteSet.hasSid(sid)) { + return; + } + if(Time.currentElapsedTime() - timeStartWaitForEpoch > maxTimeToWaitForEpoch) { + quitLeading(); + } + } + + @Override + public long getEpochToPropose(long sid, long lastAcceptedEpoch) throws InterruptedException, IOException { + synchronized(connectingFollowers) { + if(!waitingForNewEpoch) { + return epoch; + } + if(lastAcceptedEpoch >= epoch) { + epoch = lastAcceptedEpoch + 1; + } + if(isParticipant(sid)) { + connectingFollowers.add(sid); + } + QuorumVerifier verifier = self.getQuorumVerifier(); + if(connectingFollowers.contains(self.getId()) && verifier.containsQuorum(connectingFollowers)) { + waitingForNewEpoch = false; + self.setAcceptedEpoch(epoch); + connectingFollowers.notifyAll(); + } else { + long start = Time.currentElapsedTime(); + if(sid == self.getId()) { + timeStartWaitForEpoch = start; + } + long cur = start; + long end = start + self.getInitLimit() * self.getTickTime(); + while(waitingForNewEpoch && cur < end && !quitWaitForEpoch) { + connectingFollowers.wait(end - cur); + cur = Time.currentElapsedTime(); + } + if(waitingForNewEpoch) { + throw new InterruptedException("Timeout while waiting for epoch from quorum"); + } + } + return epoch; + } + } + + @Override + public ZKDatabase getZKDatabase() { + return zk.getZKDatabase(); + } + + // VisibleForTesting + protected final Set electingFollowers = new HashSet(); + + // VisibleForTesting + protected boolean electionFinished = false; + + @Override + public void waitForEpochAck(long id, StateSummary ss) throws IOException, InterruptedException { + + synchronized(electingFollowers) { + if(electionFinished) { + return; + } + if(ss.getCurrentEpoch() != -1) { + if(ss.isMoreRecentThan(leaderStateSummary)) { + throw new IOException("Follower is ahead of the leader, leader summary: " + leaderStateSummary + .getCurrentEpoch() + " (current epoch), " + leaderStateSummary + .getLastZxid() + " (last zxid)"); + } + if(ss.getLastZxid() != -1 && isParticipant(id)) { + electingFollowers.add(id); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 少数服从多数: 有超过半数节点发送了 ACKEPOCH 消息给 Leader,则该 Leader 确认是 Leader 了,则选举结束 + * 如果判断结果为 true, 意味着 选举结束 + * verifier = QuorumMaj + */ + QuorumVerifier verifier = self.getQuorumVerifier(); + + // TODO_MA 注释: 超过半数 + if(electingFollowers.contains(self.getId()) && verifier.containsQuorum(electingFollowers)) { + + // TODO_MA 注释: 到此为止,选举才结束! + electionFinished = true; + + // TODO_MA 注释: 唤醒所有线程 + electingFollowers.notifyAll(); + } + + // TODO_MA 注释: 如果还没有,则等待 + else { + long start = Time.currentElapsedTime(); + long cur = start; + long end = start + self.getInitLimit() * self.getTickTime(); + + // TODO_MA 注释: 只要选举没结束,就等 + while(!electionFinished && cur < end) { + electingFollowers.wait(end - cur); + cur = Time.currentElapsedTime(); + } + if(!electionFinished) { + throw new InterruptedException("Timeout while waiting for epoch to be acked by quorum"); + } + } + } + } + + /** + * Return a list of sid in set as string + */ + private String getSidSetString(Set sidSet) { + StringBuilder sids = new StringBuilder(); + Iterator iter = sidSet.iterator(); + while(iter.hasNext()) { + sids.append(iter.next()); + if(!iter.hasNext()) { + break; + } + sids.append(","); + } + return sids.toString(); + } + + /** + * Start up Leader ZooKeeper server and initialize zxid to the new epoch + */ + private synchronized void startZkServer() { + // Update lastCommitted and Db's zxid to a value representing the new epoch + lastCommitted = zk.getZxid(); + LOG.info("Have quorum of supporters, sids: [{}]; starting up and setting last processed zxid: 0x{}", + newLeaderProposal.ackSetsToString(), Long.toHexString(zk.getZxid())); + + if(self.isReconfigEnabled()) { + /* + * ZOOKEEPER-1324. the leader sends the new config it must complete + * to others inside a NEWLEADER message (see LearnerHandler where + * the NEWLEADER message is constructed), and once it has enough + * acks we must execute the following code so that it applies the + * config to itself. + */ + QuorumVerifier newQV = self.getLastSeenQuorumVerifier(); + + Long designatedLeader = getDesignatedLeader(newLeaderProposal, zk.getZxid()); + + self.processReconfig(newQV, designatedLeader, zk.getZxid(), true); + if(designatedLeader != self.getId()) { + LOG.warn("This leader is not the designated leader, it will be initialized with allowedToCommit = false"); + allowedToCommit = false; + } + } else { + LOG.info("Dynamic reconfig feature is disabled, skip designatedLeader calculation and reconfig processing."); + } + + leaderStartTime = Time.currentElapsedTime(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: LeaderZooKeeperServer 启动 + */ + zk.startup(); + + /* + * Update the election vote here to ensure that all members of the + * ensemble report the same vote to new servers that start up and + * send leader election notifications to the ensemble. + * + * @see https://issues.apache.org/jira/browse/ZOOKEEPER-1732 + */ + self.updateElectionVote(getEpoch()); + + zk.getZKDatabase().setlastProcessedZxid(zk.getZxid()); + } + + /** + * Process NEWLEADER ack of a given sid and wait until the leader receives + * sufficient acks. + * + * @param sid + * @throws InterruptedException + */ + @Override + public void waitForNewLeaderAck(long sid, long zxid) throws InterruptedException { + + synchronized(newLeaderProposal.qvAcksetPairs) { + + if(quorumFormed) { + return; + } + + long currentZxid = newLeaderProposal.packet.getZxid(); + if(zxid != currentZxid) { + LOG.error("NEWLEADER ACK from sid: {} is from a different epoch - current 0x{} received 0x{}", sid, + Long.toHexString(currentZxid), Long.toHexString(zxid)); + return; + } + + /* + * Note that addAck already checks that the learner is a PARTICIPANT. + */ + newLeaderProposal.addAck(sid); + + // TODO_MA 注释: 如果超过半数写回了 NEWLEADER 消息的 ACK + if(newLeaderProposal.hasAllQuorums()) { + + // TODO_MA 注释: 则 ZK 启动终于完成! + quorumFormed = true; + newLeaderProposal.qvAcksetPairs.notifyAll(); + } else { + long start = Time.currentElapsedTime(); + long cur = start; + long end = start + self.getInitLimit() * self.getTickTime(); + while(!quorumFormed && cur < end) { + newLeaderProposal.qvAcksetPairs.wait(end - cur); + cur = Time.currentElapsedTime(); + } + if(!quorumFormed) { + throw new InterruptedException("Timeout while waiting for NEWLEADER to be acked by quorum"); + } + } + } + } + + /** + * Get string representation of a given packet type + * + * @param packetType + * @return string representing the packet type + */ + public static String getPacketType(int packetType) { + switch(packetType) { + case DIFF: + return "DIFF"; + case TRUNC: + return "TRUNC"; + case SNAP: + return "SNAP"; + case OBSERVERINFO: + return "OBSERVERINFO"; + case NEWLEADER: + return "NEWLEADER"; + case FOLLOWERINFO: + return "FOLLOWERINFO"; + case UPTODATE: + return "UPTODATE"; + case LEADERINFO: + return "LEADERINFO"; + case ACKEPOCH: + return "ACKEPOCH"; + case REQUEST: + return "REQUEST"; + case PROPOSAL: + return "PROPOSAL"; + case ACK: + return "ACK"; + case COMMIT: + return "COMMIT"; + case COMMITANDACTIVATE: + return "COMMITANDACTIVATE"; + case PING: + return "PING"; + case REVALIDATE: + return "REVALIDATE"; + case SYNC: + return "SYNC"; + case INFORM: + return "INFORM"; + case INFORMANDACTIVATE: + return "INFORMANDACTIVATE"; + default: + return "UNKNOWN"; + } + } + + private boolean isRunning() { + return self.isRunning() && zk.isRunning(); + } + + private boolean isParticipant(long sid) { + return self.getQuorumVerifier().getVotingMembers().containsKey(sid); + } + + @Override + public int getCurrentTick() { + return self.tick.get(); + } + + @Override + public int syncTimeout() { + return self.tickTime * self.syncLimit; + } + + @Override + public int getTickOfNextAckDeadline() { + return self.tick.get() + self.syncLimit; + } + + @Override + public int getTickOfInitialAckDeadline() { + return self.tick.get() + self.initLimit + self.syncLimit; + } + + @Override + public long getAndDecrementFollowerCounter() { + return followerCounter.getAndDecrement(); + } + + @Override + public void touch(long sess, int to) { + zk.touch(sess, to); + } + + @Override + public void submitLearnerRequest(Request si) { + zk.submitLearnerRequest(si); + } + + @Override + public long getQuorumVerifierVersion() { + return self.getQuorumVerifier().getVersion(); + } + + @Override + public String getPeerInfo(long sid) { + QuorumPeer.QuorumServer server = self.getView().get(sid); + return server == null ? "" : server.toString(); + } + + @Override + public byte[] getQuorumVerifierBytes() { + return self.getLastSeenQuorumVerifier().toString().getBytes(); + } + + @Override + public QuorumAuthServer getQuorumAuthServer() { + return (self == null) ? null : self.authServer; + } + + @Override + public void revalidateSession(QuorumPacket qp, LearnerHandler learnerHandler) throws IOException { + ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData()); + DataInputStream dis = new DataInputStream(bis); + long id = dis.readLong(); + int to = dis.readInt(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeLong(id); + boolean valid = zk.checkIfValidGlobalSession(id, to); + if(valid) { + try { + // set the session owner as the follower that owns the session + zk.setOwner(id, learnerHandler); + } catch(KeeperException.SessionExpiredException e) { + LOG.error("Somehow session 0x{} expired right after being renewed! (impossible)", Long.toHexString(id), + e); + } + } + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "Session 0x" + Long.toHexString(id) + " is valid: " + valid); + } + dos.writeBoolean(valid); + qp.setData(bos.toByteArray()); + learnerHandler.queuePacket(qp); + } + + @Override + public void registerLearnerHandlerBean(final LearnerHandler learnerHandler, Socket socket) { + LearnerHandlerBean bean = new LearnerHandlerBean(learnerHandler, socket); + if(zk.registerJMX(bean)) { + connectionBeans.put(learnerHandler, bean); + } + } + + @Override + public void unregisterLearnerHandlerBean(final LearnerHandler learnerHandler) { + LearnerHandlerBean bean = connectionBeans.remove(learnerHandler); + if(bean != null) { + MBeanRegistry.getInstance().unregister(bean); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderBean.java new file mode 100644 index 0000000..8d5b48f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderBean.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperServerBean; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; + +/** + * Leader MBean interface implementation. + */ +public class LeaderBean extends ZooKeeperServerBean implements LeaderMXBean { + + private final Leader leader; + + public LeaderBean(Leader leader, ZooKeeperServer zks) { + super(zks); + this.leader = leader; + } + + public String getName() { + return "Leader"; + } + + public String getCurrentZxid() { + return "0x" + Long.toHexString(zks.getZxid()); + } + + public String followerInfo() { + StringBuilder sb = new StringBuilder(); + for (LearnerHandler handler : leader.getLearners()) { + if (handler.getLearnerType() == LearnerType.PARTICIPANT) { + sb.append(handler.toString()).append("\n"); + } + } + return sb.toString(); + } + + @Override + public String nonVotingFollowerInfo() { + StringBuilder sb = new StringBuilder(); + for (LearnerHandler handler : leader.getNonVotingFollowers()) { + sb.append(handler.toString()).append("\n"); + } + return sb.toString(); + } + + @Override + public long getElectionTimeTaken() { + return leader.self.getElectionTimeTaken(); + } + + @Override + public int getLastProposalSize() { + return leader.getProposalStats().getLastBufferSize(); + } + + @Override + public int getMinProposalSize() { + return leader.getProposalStats().getMinBufferSize(); + } + + @Override + public int getMaxProposalSize() { + return leader.getProposalStats().getMaxBufferSize(); + } + + @Override + public void resetProposalStatistics() { + leader.getProposalStats().reset(); + } + + @Override + public int getMaxConcurrentSnapSyncs() { + return leader.getMaxConcurrentSnapSyncs(); + } + + @Override + public void setMaxConcurrentSnapSyncs(int maxConcurrentSnapshots) { + leader.setMaxConcurrentSnapSyncs(maxConcurrentSnapshots); + } + + @Override + public int getMaxConcurrentDiffSyncs() { + return leader.getMaxConcurrentDiffSyncs(); + } + + @Override + public void setMaxConcurrentDiffSyncs(int maxConcurrentDiffSyncs) { + leader.setMaxConcurrentDiffSyncs(maxConcurrentDiffSyncs); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderElectionBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderElectionBean.java new file mode 100644 index 0000000..25e9ec1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderElectionBean.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.Date; +import org.apache.zookeeper.jmx.ZKMBeanInfo; + +/** + * Leader election MBean interface implementation + */ +public class LeaderElectionBean implements LeaderElectionMXBean, ZKMBeanInfo { + + private final Date startTime = new Date(); + + public String getName() { + return "LeaderElection"; + } + + public boolean isHidden() { + return false; + } + + public String getStartTime() { + return startTime.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderElectionMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderElectionMXBean.java new file mode 100644 index 0000000..d750d28 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderElectionMXBean.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * Leader election protocol MBean. + */ +public interface LeaderElectionMXBean { + + /** + * + * @return the time when the leader election started + */ + String getStartTime(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderMXBean.java new file mode 100644 index 0000000..e64d2f3 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderMXBean.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.ZooKeeperServerMXBean; + +/** + * Leader MBean. + */ +public interface LeaderMXBean extends ZooKeeperServerMXBean { + + /** + * Current zxid of cluster. + */ + String getCurrentZxid(); + + /** + * @return information on current followers + */ + String followerInfo(); + + /** + * @return information about current non-voting followers + */ + String nonVotingFollowerInfo(); + + /** + * @return time taken for leader election in milliseconds. + */ + long getElectionTimeTaken(); + + /** + * @return size of latest generated proposal + */ + int getLastProposalSize(); + + /** + * @return size of smallest generated proposal + */ + int getMinProposalSize(); + + /** + * @return size of largest generated proposal + */ + int getMaxProposalSize(); + + /** + * Resets statistics of proposal size (min/max/last) + */ + void resetProposalStatistics(); + + /** + * @return Number of concurrent snapshots permitted to send to observers + */ + int getMaxConcurrentSnapSyncs(); + + /** + * @param maxConcurrentSnapSyncs Number of concurrent snapshots permitted to send to observers + */ + void setMaxConcurrentSnapSyncs(int maxConcurrentSnapSyncs); + + /** + * @return Number of concurrent diff syncs permitted to send to observers + */ + int getMaxConcurrentDiffSyncs(); + + /** + * @param maxConcurrentDiffSyncs Number of concurrent diff syncs permitted to send to observers + */ + void setMaxConcurrentDiffSyncs(int maxConcurrentDiffSyncs); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderRequestProcessor.java new file mode 100644 index 0000000..7afd1bf --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderRequestProcessor.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.txn.ErrorTxn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Responsible for performing local session upgrade. Only request submitted + * directly to the leader should go through this processor. + */ +public class LeaderRequestProcessor implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(LeaderRequestProcessor.class); + + private final LeaderZooKeeperServer lzks; + + private final RequestProcessor nextProcessor; + + public LeaderRequestProcessor(LeaderZooKeeperServer zks, RequestProcessor nextProcessor) { + this.lzks = zks; + this.nextProcessor = nextProcessor; + } + + @Override + public void processRequest(Request request) throws RequestProcessorException { + // Screen quorum requests against ACLs first + if (!lzks.authWriteRequest(request)) { + return; + } + + // TODO_MA 注释: 处理跟 session 有关的动作 + // TODO_MA 注释: 检查这是否是本地会话并且我们正在尝试创建一个临时节点,在这种情况下我们升级会话 + // Check if this is a local session and we are trying to create + // an ephemeral node, in which case we upgrade the session + Request upgradeRequest = null; + try { + upgradeRequest = lzks.checkUpgradeSession(request); + } catch (KeeperException ke) { + if (request.getHdr() != null) { + LOG.debug("Updating header"); + request.getHdr().setType(OpCode.error); + request.setTxn(new ErrorTxn(ke.code().intValue())); + } + request.setException(ke); + LOG.warn("Error creating upgrade request", ke); + } catch (IOException ie) { + LOG.error("Unexpected error in upgrade", ie); + } + if (upgradeRequest != null) { + nextProcessor.processRequest(upgradeRequest); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 调用下一个 RequestProcessor 执行处理 + * nextProcessor = 第二个 preRP + */ + nextProcessor.processRequest(request); + } + + @Override + public void shutdown() { + LOG.info("Shutting down"); + nextProcessor.shutdown(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderSessionTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderSessionTracker.java new file mode 100644 index 0000000..754f694 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderSessionTracker.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.PrintWriter; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.KeeperException.UnknownSessionException; +import org.apache.zookeeper.server.SessionTrackerImpl; +import org.apache.zookeeper.server.ZooKeeperServerListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The leader session tracker tracks local and global sessions on the leader. + */ +public class LeaderSessionTracker extends UpgradeableSessionTracker { + + private static final Logger LOG = LoggerFactory.getLogger(LeaderSessionTracker.class); + + private final SessionTrackerImpl globalSessionTracker; + + /** + * Server id of the leader + */ + private final long serverId; + + public LeaderSessionTracker(SessionExpirer expirer, ConcurrentMap sessionsWithTimeouts, int tickTime, + long id, boolean localSessionsEnabled, ZooKeeperServerListener listener) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建 SessionTrackerImpl + */ + this.globalSessionTracker = new SessionTrackerImpl(expirer, sessionsWithTimeouts, tickTime, id, listener); + + this.localSessionsEnabled = localSessionsEnabled; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建本地 LocalSessionTracker + */ + if(this.localSessionsEnabled) { + createLocalSessionTracker(expirer, tickTime, id, listener); + } + serverId = id; + } + + public void removeSession(long sessionId) { + if(localSessionTracker != null) { + localSessionTracker.removeSession(sessionId); + } + globalSessionTracker.removeSession(sessionId); + } + + public void start() { + globalSessionTracker.start(); + if(localSessionTracker != null) { + localSessionTracker.start(); + } + } + + public void shutdown() { + if(localSessionTracker != null) { + localSessionTracker.shutdown(); + } + globalSessionTracker.shutdown(); + } + + public boolean isGlobalSession(long sessionId) { + return globalSessionTracker.isTrackingSession(sessionId); + } + + public boolean trackSession(long sessionId, int sessionTimeout) { + boolean tracked = globalSessionTracker.trackSession(sessionId, sessionTimeout); + if(localSessionsEnabled && tracked) { + // Only do extra logging so we know what kind of session this is + // if we're supporting both kinds of sessions + LOG.info("Tracking global session 0x{}", Long.toHexString(sessionId)); + } + return tracked; + } + + /** + * Synchronized on this to avoid race condition of adding a local session + * after committed global session, which may cause the same session being + * tracked on this server and leader. + */ + public synchronized boolean commitSession(long sessionId, int sessionTimeout) { + boolean added = globalSessionTracker.commitSession(sessionId, sessionTimeout); + + if(added) { + LOG.info("Committing global session 0x{}", Long.toHexString(sessionId)); + } + + // If the session moved before the session upgrade finished, it's + // possible that the session will be added to the local session + // again. Need to double check and remove it from local session + // tracker when the global session is quorum committed, otherwise the + // local session might be tracked both locally and on leader. + // + // This cannot totally avoid the local session being upgraded again + // because there is still race condition between create another upgrade + // request and process the createSession commit, and there is no way + // to know there is a on flying createSession request because it might + // be upgraded by other server which owns the session before move. + if(localSessionsEnabled) { + removeLocalSession(sessionId); + finishedUpgrading(sessionId); + } + + return added; + } + + public boolean touchSession(long sessionId, int sessionTimeout) { + if(localSessionTracker != null && localSessionTracker.touchSession(sessionId, sessionTimeout)) { + return true; + } + return globalSessionTracker.touchSession(sessionId, sessionTimeout); + } + + public long createSession(int sessionTimeout) { + if(localSessionsEnabled) { + return localSessionTracker.createSession(sessionTimeout); + } + return globalSessionTracker.createSession(sessionTimeout); + } + + // Returns the serverId from the sessionId (the high order byte) + public static long getServerIdFromSessionId(long sessionId) { + return sessionId >> 56; + } + + public void checkSession(long sessionId, + Object owner) throws SessionExpiredException, SessionMovedException, UnknownSessionException { + + // TODO_MA 注释: local session 检查 + if(localSessionTracker != null) { + try { + localSessionTracker.checkSession(sessionId, owner); + // A session can both be a local and global session during + // upgrade + if(!isGlobalSession(sessionId)) { + return; + } + } catch(UnknownSessionException e) { + // Ignore. We'll check instead whether it's a global session + } + } + + // TODO_MA 注释: global session 检查 + try { + globalSessionTracker.checkSession(sessionId, owner); + // if we can get here, it is a valid global session + return; + } catch(UnknownSessionException e) { + // Ignore. This may be local session from other servers. + } + + /* + * if local session is not enabled or it used to be our local session + * throw sessions expires + */ + if(!localSessionsEnabled || (getServerIdFromSessionId(sessionId) == serverId)) { + throw new SessionExpiredException(); + } + } + + public void checkGlobalSession(long sessionId, Object owner) throws SessionExpiredException, SessionMovedException { + try { + globalSessionTracker.checkSession(sessionId, owner); + } catch(UnknownSessionException e) { + // For global session, if we don't know it, it is already expired + throw new SessionExpiredException(); + } + } + + public void setOwner(long sessionId, Object owner) throws SessionExpiredException { + if(localSessionTracker != null) { + try { + localSessionTracker.setOwner(sessionId, owner); + return; + } catch(SessionExpiredException e) { + // Ignore. We'll check instead whether it's a global session + } + } + globalSessionTracker.setOwner(sessionId, owner); + } + + public void dumpSessions(PrintWriter pwriter) { + if(localSessionTracker != null) { + pwriter.print("Local "); + localSessionTracker.dumpSessions(pwriter); + pwriter.print("Global "); + } + globalSessionTracker.dumpSessions(pwriter); + } + + public void setSessionClosing(long sessionId) { + // call is no-op if session isn't tracked so safe to call both + if(localSessionTracker != null) { + localSessionTracker.setSessionClosing(sessionId); + } + globalSessionTracker.setSessionClosing(sessionId); + } + + public Map> getSessionExpiryMap() { + Map> sessionExpiryMap; + // combine local and global sessions, getting local first so upgrades + // to global are caught + if(localSessionTracker != null) { + sessionExpiryMap = localSessionTracker.getSessionExpiryMap(); + } else { + sessionExpiryMap = new TreeMap>(); + } + sessionExpiryMap.putAll(globalSessionTracker.getSessionExpiryMap()); + return sessionExpiryMap; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java new file mode 100644 index 0000000..c79da86 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LeaderZooKeeperServer.java @@ -0,0 +1,351 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import javax.management.JMException; + +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.server.ContainerManager; +import org.apache.zookeeper.server.DataTreeBean; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.PrepRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; + +/** + * Just like the standard ZooKeeperServer. We just replace the request + * processors: PrepRequestProcessor -> ProposalRequestProcessor -> + * CommitProcessor -> Leader.ToBeAppliedRequestProcessor -> + * FinalRequestProcessor + */ +public class LeaderZooKeeperServer extends QuorumZooKeeperServer { + + private ContainerManager containerManager; // guarded by sync + + CommitProcessor commitProcessor; + + PrepRequestProcessor prepRequestProcessor; + + /** + * @throws IOException + */ + public LeaderZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, ZKDatabase zkDb) throws IOException { + super(logFactory, self.tickTime, self.minSessionTimeout, self.maxSessionTimeout, self.clientPortListenBacklog, + zkDb, self); + } + + public Leader getLeader() { + return self.leader; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: ZK 集群对外提供服务的时候,接收到客户端请求是如何处理的呢? + * 是通过 RP 链来完成的。事实上就是一个 责任链设计模式的实现 + * firstRP.processRequest() ==> firstRP.nextRp.processRequest() + */ + @Override + protected void setupRequestProcessors() { + + // TODO_MA 注释: 第六个: FinalRequestProcessor + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + + // TODO_MA 注释: 第五个: ToBeAppliedRequestProcessor + RequestProcessor toBeAppliedProcessor = new Leader.ToBeAppliedRequestProcessor(finalProcessor, getLeader()); + + // TODO_MA 注释: 第四个: CommitProcessor + commitProcessor = new CommitProcessor(toBeAppliedProcessor, Long.toString(getServerId()), false, + getZooKeeperServerListener()); + commitProcessor.start(); + + // TODO_MA 注释: 第三个: ProposalRequestProcessor + // TODO_MA 注释: 内部初始化 SyncRequestProcessor 和 AckRequestProcessor + ProposalRequestProcessor proposalProcessor = new ProposalRequestProcessor(this, commitProcessor); + proposalProcessor.initialize(); + + // TODO_MA 注释: 第二个: PrepRequestProcessor + prepRequestProcessor = new PrepRequestProcessor(this, proposalProcessor); + prepRequestProcessor.start(); + + // TODO_MA 注释: 第一个: LeaderRequestProcessor + firstProcessor = new LeaderRequestProcessor(this, prepRequestProcessor); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + setupContainerManager(); + } + + private synchronized void setupContainerManager() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + containerManager = new ContainerManager(getZKDatabase(), prepRequestProcessor, + Integer.getInteger("znode.container.checkIntervalMs", (int) TimeUnit.MINUTES.toMillis(1)), + Integer.getInteger("znode.container.maxPerMinute", 10000), + Long.getLong("znode.container.maxNeverUsedIntervalMs", 0)); + } + + @Override + public synchronized void startup() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 1、ZooKeeperServer 中的各种服务启动 + * 1、this = LeaderZooKeeperServer + * 2、super = ZooKeeperServer + */ + super.startup(); + + // TODO_MA 注释: zk-3.4 是没有的。容器服务: 如果一个 znode 下面有子 znode, 则该 znode 认为是一个容器 + // TODO_MA 注释: znode(file directory) + // TODO_MA 注释: 这个 containerManager 的作用,就是去检查,如果一个容器znode节点下面没有 子 znode 了,则要删除 + if(containerManager != null) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + containerManager.start(); + } + } + + @Override + protected void registerMetrics() { + super.registerMetrics(); + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + + rootContext.registerGauge("learners", () -> { + return getLeader().getLearners().size(); + }); + rootContext.registerGauge("synced_followers", () -> { + return getLeader().getForwardingFollowers().size(); + }); + rootContext.registerGauge("synced_non_voting_followers", () -> { + return getLeader().getNonVotingFollowers().size(); + }); + + rootContext.registerGauge("synced_observers", self::getSynced_observers_metric); + + rootContext.registerGauge("pending_syncs", () -> { + return getLeader().getNumPendingSyncs(); + }); + rootContext.registerGauge("leader_uptime", () -> { + return getLeader().getUptime(); + }); + rootContext.registerGauge("last_proposal_size", () -> { + return getLeader().getProposalStats().getLastBufferSize(); + }); + rootContext.registerGauge("max_proposal_size", () -> { + return getLeader().getProposalStats().getMaxBufferSize(); + }); + rootContext.registerGauge("min_proposal_size", () -> { + return getLeader().getProposalStats().getMinBufferSize(); + }); + } + + @Override + protected void unregisterMetrics() { + super.unregisterMetrics(); + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + rootContext.unregisterGauge("learners"); + rootContext.unregisterGauge("synced_followers"); + rootContext.unregisterGauge("synced_non_voting_followers"); + rootContext.unregisterGauge("synced_observers"); + rootContext.unregisterGauge("pending_syncs"); + rootContext.unregisterGauge("leader_uptime"); + + rootContext.unregisterGauge("last_proposal_size"); + rootContext.unregisterGauge("max_proposal_size"); + rootContext.unregisterGauge("min_proposal_size"); + } + + @Override + public synchronized void shutdown() { + if(containerManager != null) { + containerManager.stop(); + } + super.shutdown(); + } + + @Override + public int getGlobalOutstandingLimit() { + int divisor = self.getQuorumSize() > 2 ? self.getQuorumSize() - 1 : 1; + int globalOutstandingLimit = super.getGlobalOutstandingLimit() / divisor; + return globalOutstandingLimit; + } + + @Override + public void createSessionTracker() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建一个 LeaderSessionTracker + */ + sessionTracker = new LeaderSessionTracker(this, getZKDatabase().getSessionWithTimeOuts(), tickTime, self.getId(), + self.areLocalSessionsEnabled(), getZooKeeperServerListener()); + } + + public boolean touch(long sess, int to) { + return sessionTracker.touchSession(sess, to); + } + + public boolean checkIfValidGlobalSession(long sess, int to) { + if(self.areLocalSessionsEnabled() && !upgradeableSessionTracker.isGlobalSession(sess)) { + return false; + } + return sessionTracker.touchSession(sess, to); + } + + /** + * Requests coming from the learner should go directly to + * PrepRequestProcessor + * + * @param request + */ + public void submitLearnerRequest(Request request) { + /* + * Requests coming from the learner should have gone through + * submitRequest() on each server which already perform some request + * validation, so we don't need to do it again. + * + * Additionally, LearnerHandler should start submitting requests into + * the leader's pipeline only when the leader's server is started, so we + * can submit the request directly into PrepRequestProcessor. + * + * This is done so that requests from learners won't go through + * LeaderRequestProcessor which perform local session upgrade. + */ + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、first + * 2、second = prepRequestProcessor + * 没有直接调用 first 的原因,是因为,firstRP 的处理逻辑,follower 已经做完了。 + */ + prepRequestProcessor.processRequest(request); + } + + @Override + protected void registerJMX() { + // register with JMX + try { + jmxDataTreeBean = new DataTreeBean(getZKDatabase().getDataTree()); + MBeanRegistry.getInstance().register(jmxDataTreeBean, jmxServerBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxDataTreeBean = null; + } + } + + public void registerJMX(LeaderBean leaderBean, LocalPeerBean localPeerBean) { + // register with JMX + if(self.jmxLeaderElectionBean != null) { + try { + MBeanRegistry.getInstance().unregister(self.jmxLeaderElectionBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + } + self.jmxLeaderElectionBean = null; + } + + try { + jmxServerBean = leaderBean; + MBeanRegistry.getInstance().register(leaderBean, localPeerBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxServerBean = null; + } + } + + boolean registerJMX(LearnerHandlerBean handlerBean) { + try { + MBeanRegistry.getInstance().register(handlerBean, jmxServerBean); + return true; + } catch(JMException e) { + LOG.warn("Could not register connection", e); + } + return false; + } + + @Override + protected void unregisterJMX() { + // unregister from JMX + try { + if(jmxDataTreeBean != null) { + MBeanRegistry.getInstance().unregister(jmxDataTreeBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxDataTreeBean = null; + } + + protected void unregisterJMX(Leader leader) { + // unregister from JMX + try { + if(jmxServerBean != null) { + MBeanRegistry.getInstance().unregister(jmxServerBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxServerBean = null; + } + + @Override + public String getState() { + return "leader"; + } + + /** + * Returns the id of the associated QuorumPeer, which will do for a unique + * id of this server. + */ + @Override + public long getServerId() { + return self.getId(); + } + + @Override + protected void revalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout) throws IOException { + super.revalidateSession(cnxn, sessionId, sessionTimeout); + try { + // setowner as the leader itself, unless updated + // via the follower handlers + setOwner(sessionId, ServerCnxn.me); + } catch(SessionExpiredException e) { + // this is ok, it just means that the session revalidation failed. + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Learner.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Learner.java new file mode 100644 index 0000000..8c9d673 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Learner.java @@ -0,0 +1,1029 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.util.ArrayDeque; +import java.util.Deque; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import javax.net.ssl.SSLSocket; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.ConfigUtils; +import org.apache.zookeeper.server.util.MessageTracker; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is the superclass of two of the three main actors in a ZK + * ensemble: Followers and Observers. Both Followers and Observers share + * a good deal of code which is moved into Peer to avoid duplication. + */ +public class Learner { + + static class PacketInFlight { + + TxnHeader hdr; + Record rec; + TxnDigest digest; + + } + + QuorumPeer self; + LearnerZooKeeperServer zk; + + protected BufferedOutputStream bufferedOutput; + + protected Socket sock; + protected MultipleAddresses leaderAddr; + + /** + * Socket getter + * + * @return + */ + public Socket getSocket() { + return sock; + } + + LearnerSender sender = null; + protected InputArchive leaderIs; + protected OutputArchive leaderOs; + /** + * the protocol version of the leader + */ + protected int leaderProtocolVersion = 0x01; + + private static final int BUFFERED_MESSAGE_SIZE = 10; + protected final MessageTracker messageTracker = new MessageTracker(BUFFERED_MESSAGE_SIZE); + + protected static final Logger LOG = LoggerFactory.getLogger(Learner.class); + + /** + * Time to wait after connection attempt with the Leader or LearnerMaster before this + * Learner tries to connect again. + */ + private static final int leaderConnectDelayDuringRetryMs = Integer + .getInteger("zookeeper.leaderConnectDelayDuringRetryMs", 100); + + private static final boolean nodelay = System.getProperty("follower.nodelay", "true").equals("true"); + + public static final String LEARNER_ASYNC_SENDING = "zookeeper.learner.asyncSending"; + private static boolean asyncSending = Boolean + .parseBoolean(ConfigUtils.getPropertyBackwardCompatibleWay(LEARNER_ASYNC_SENDING)); + + static { + LOG.info("leaderConnectDelayDuringRetryMs: {}", leaderConnectDelayDuringRetryMs); + LOG.info("TCP NoDelay set to: {}", nodelay); + LOG.info("{} = {}", LEARNER_ASYNC_SENDING, asyncSending); + } + + final ConcurrentHashMap pendingRevalidations = new ConcurrentHashMap(); + + public int getPendingRevalidationsCount() { + return pendingRevalidations.size(); + } + + // for testing + protected static void setAsyncSending(boolean newMode) { + asyncSending = newMode; + LOG.info("{} = {}", LEARNER_ASYNC_SENDING, asyncSending); + + } + + protected static boolean getAsyncSending() { + return asyncSending; + } + + /** + * validate a session for a client + * + * @param clientId the client to be revalidated + * @param timeout the timeout for which the session is valid + * @throws IOException + */ + void validateSession(ServerCnxn cnxn, long clientId, int timeout) throws IOException { + LOG.info("Revalidating client: 0x{}", Long.toHexString(clientId)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + dos.writeLong(clientId); + dos.writeInt(timeout); + dos.close(); + QuorumPacket qp = new QuorumPacket(Leader.REVALIDATE, -1, baos.toByteArray(), null); + pendingRevalidations.put(clientId, cnxn); + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "To validate session 0x" + Long.toHexString(clientId)); + } + writePacket(qp, true); + } + + /** + * write a packet to the leader. + * + * This method is called by multiple threads. We need to make sure that only one thread is writing to leaderOs at a time. + * When packets are sent synchronously, writing is done within a synchronization block. + * When packets are sent asynchronously, sender.queuePacket() is called, which writes to a BlockingQueue, which is thread-safe. + * Reading from this BlockingQueue and writing to leaderOs is the learner sender thread only. + * So we have only one thread writing to leaderOs at a time in either case. + * + * @param pp the proposal packet to be sent to the leader + * @throws IOException + */ + void writePacket(QuorumPacket pp, boolean flush) throws IOException { + if(asyncSending) { + sender.queuePacket(pp); + } else { + writePacketNow(pp, flush); + } + } + + void writePacketNow(QuorumPacket pp, boolean flush) throws IOException { + synchronized(leaderOs) { + if(pp != null) { + messageTracker.trackSent(pp.getType()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 状态同步 + * 1、服务端有一个线程: LearnerHandler + */ + leaderOs.writeRecord(pp, "packet"); + } + if(flush) { + bufferedOutput.flush(); + } + } + } + + /** + * Start thread that will forward any packet in the queue to the leader + */ + protected void startSendingThread() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 LearnerSender + */ + sender = new LearnerSender(this); + sender.start(); + } + + /** + * read a packet from the leader + * + * @param pp the packet to be instantiated + * @throws IOException + */ + void readPacket(QuorumPacket pp) throws IOException { + synchronized(leaderIs) { + leaderIs.readRecord(pp, "packet"); + messageTracker.trackReceived(pp.getType()); + } + if(LOG.isTraceEnabled()) { + final long traceMask = (pp + .getType() == Leader.PING) ? ZooTrace.SERVER_PING_TRACE_MASK : ZooTrace.SERVER_PACKET_TRACE_MASK; + + ZooTrace.logQuorumPacket(LOG, traceMask, 'i', pp); + } + } + + /** + * send a request packet to the leader + * + * @param request the request from the client + * @throws IOException + */ + void request(Request request) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream oa = new DataOutputStream(baos); + oa.writeLong(request.sessionId); + oa.writeInt(request.cxid); + oa.writeInt(request.type); + if(request.request != null) { + request.request.rewind(); + int len = request.request.remaining(); + byte[] b = new byte[len]; + request.request.get(b); + request.request.rewind(); + oa.write(b); + } + oa.close(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 转发给 Leader + */ + QuorumPacket qp = new QuorumPacket(Leader.REQUEST, -1, baos.toByteArray(), request.authInfo); + writePacket(qp, true); + } + + /** + * Returns the address of the node we think is the leader. + */ + protected QuorumServer findLeader() { + QuorumServer leaderServer = null; + + // TODO_MA 注释: 选举结束之后,会将推举成为 leader 的 server 的信息存储在 currentVote 中 + // TODO_MA 注释: current = lookForLeader() 方法的返回值 + // Find the leader by id + Vote current = self.getCurrentVote(); + + // TODO_MA 注释: 从 Map allMembers 中根据 myid = serverID 去寻找 QuorumServer + // TODO_MA 注释: 这个 QuorumServer 就是 LeaderServer 节点 + // TODO_MA 注释: 解析 zoo.cfg 的时候,就构建了 QuorumMaj 这个对象 + for(QuorumServer s : self.getView().values()) { + if(s.id == current.getId()) { + // Ensure we have the leader's correct IP address before + // attempting to connect. + s.recreateSocketAddresses(); + + // TODO_MA 注释: 这个就得到我们想要的 leader 节点 + leaderServer = s; + break; + } + } + if(leaderServer == null) { + LOG.warn("Couldn't find the leader with id = {}", current.getId()); + } + return leaderServer; + } + + /** + * Overridable helper method to return the System.nanoTime(). + * This method behaves identical to System.nanoTime(). + */ + protected long nanoTime() { + return System.nanoTime(); + } + + /** + * Overridable helper method to simply call sock.connect(). This can be + * overriden in tests to fake connection success/failure for connectToLeader. + */ + protected void sockConnect(Socket sock, InetSocketAddress addr, int timeout) throws IOException { + + // TODO_MA 注释: 这个 BIO 的 API + sock.connect(addr, timeout); + } + + /** + * Establish a connection with the LearnerMaster found by findLearnerMaster. + * Followers only connect to Leaders, Observers can connect to any active LearnerMaster. + * Retries until either initLimit time has elapsed or 5 tries have happened. + * + * @param multiAddr - the address of the Peer to connect to. + * @throws IOException - if the socket connection fails on the 5th attempt + * if there is an authentication failure while connecting to leader + */ + protected void connectToLeader(MultipleAddresses multiAddr, String hostname) throws IOException { + + this.leaderAddr = multiAddr; + Set addresses; + if(self.isMultiAddressReachabilityCheckEnabled()) { + // even if none of the addresses are reachable, we want to try to establish connection + // see ZOOKEEPER-3758 + addresses = multiAddr.getAllReachableAddressesOrAll(); + } else { + addresses = multiAddr.getAllAddresses(); + } + ExecutorService executor = Executors.newFixedThreadPool(addresses.size()); + CountDownLatch latch = new CountDownLatch(addresses.size()); + AtomicReference socket = new AtomicReference<>(null); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 通过一个 LeaderConnector 线程来链接 Leader + * 每个 Follower 跟 Leader 做状态同步的时候,其实就是各自的一对线程来完成的 + * 1、leader: LearnHandler(BIO 服务端) + * 2、Follower: LeaderConnector(BIO 客户端) + */ + addresses.stream().map(address -> new LeaderConnector(address, socket, latch)).forEach(executor::submit); + + try { + latch.await(); + } catch(InterruptedException e) { + LOG.warn("Interrupted while trying to connect to Leader", e); + } finally { + executor.shutdown(); + try { + if(!executor.awaitTermination(1, TimeUnit.SECONDS)) { + LOG.error("not all the LeaderConnector terminated properly"); + } + } catch(InterruptedException ie) { + LOG.error("Interrupted while terminating LeaderConnector executor.", ie); + } + } + + if(socket.get() == null) { + throw new IOException("Failed connect to " + multiAddr); + } else { + sock = socket.get(); + } + + self.authLearner.authenticate(sock, hostname); + + // TODO_MA 注释: 获取输入和输出相关组件 + leaderIs = BinaryInputArchive.getArchive(new BufferedInputStream(sock.getInputStream())); + bufferedOutput = new BufferedOutputStream(sock.getOutputStream()); + leaderOs = BinaryOutputArchive.getArchive(bufferedOutput); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 LearnerSender + */ + if(asyncSending) { + startSendingThread(); + } + } + + class LeaderConnector implements Runnable { + + private AtomicReference socket; + private InetSocketAddress address; + private CountDownLatch latch; + + LeaderConnector(InetSocketAddress address, AtomicReference socket, CountDownLatch latch) { + this.address = address; + this.socket = socket; + this.latch = latch; + } + + @Override + public void run() { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 构建一个 Socket 链接 + */ + Thread.currentThread().setName("LeaderConnector-" + address); + Socket sock = connectToLeader(); + + if(sock != null && sock.isConnected()) { + + // TODO_MA 注释: 将和 Leader 建立链接的 Socket BIO 客户端对象设置到 LeaderConnector 的成员变量 + if(socket.compareAndSet(null, sock)) { + LOG.info("Successfully connected to leader, using address: {}", address); + } else { + LOG.info("Connection to the leader is already established, close the redundant connection"); + sock.close(); + } + } + + } catch(Exception e) { + LOG.error("Failed connect to {}", address, e); + } finally { + latch.countDown(); + } + } + + private Socket connectToLeader() throws IOException, X509Exception, InterruptedException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 构建一个 Socket 客户端对象 + */ + Socket sock = createSocket(); + + // leader connection timeout defaults to tickTime * initLimit + int connectTimeout = self.tickTime * self.initLimit; + + // but if connectToLearnerMasterLimit is specified, use that value to calculate + // timeout instead of using the initLimit value + if(self.connectToLearnerMasterLimit > 0) { + connectTimeout = self.tickTime * self.connectToLearnerMasterLimit; + } + + int remainingTimeout; + long startNanoTime = nanoTime(); + + for(int tries = 0; tries < 5 && socket.get() == null; tries++) { + try { + // recalculate the init limit time because retries sleep for 1000 milliseconds + remainingTimeout = connectTimeout - (int) ((nanoTime() - startNanoTime) / 1_000_000); + if(remainingTimeout <= 0) { + LOG.error("connectToLeader exceeded on retries."); + throw new IOException("connectToLeader exceeded on retries."); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 链接 Leader + */ + sockConnect(sock, address, Math.min(connectTimeout, remainingTimeout)); + if(self.isSslQuorum()) { + ((SSLSocket) sock).startHandshake(); + } + sock.setTcpNoDelay(nodelay); + break; + } catch(IOException e) { + remainingTimeout = connectTimeout - (int) ((nanoTime() - startNanoTime) / 1_000_000); + + if(remainingTimeout <= leaderConnectDelayDuringRetryMs) { + LOG.error( + "Unexpected exception, connectToLeader exceeded. tries={}, remaining init limit={}, connecting to {}", + tries, remainingTimeout, address, e); + throw e; + } else if(tries >= 4) { + LOG.error( + "Unexpected exception, retries exceeded. tries={}, remaining init limit={}, connecting to {}", + tries, remainingTimeout, address, e); + throw e; + } else { + LOG.warn("Unexpected exception, tries={}, remaining init limit={}, connecting to {}", tries, + remainingTimeout, address, e); + sock = createSocket(); + } + } + Thread.sleep(leaderConnectDelayDuringRetryMs); + } + + return sock; + } + } + + /** + * Creating a simple or and SSL socket. + * This can be overridden in tests to fake already connected sockets for connectToLeader. + */ + protected Socket createSocket() throws X509Exception, IOException { + Socket sock; + if(self.isSslQuorum()) { + sock = self.getX509Util().createSSLSocket(); + } else { + sock = new Socket(); + } + sock.setSoTimeout(self.tickTime * self.initLimit); + return sock; + } + + /** + * Once connected to the leader or learner master, perform the handshake + * protocol to establish a following / observing connection. + * + * @param pktType = Leader.FOLLOWERINFO + * @return the zxid the Leader sends for synchronization purposes. + * @throws IOException + */ + protected long registerWithLeader(int pktType) throws IOException { + /* + * Send follower info, including last zxid and sid + */ + long lastLoggedZxid = self.getLastLoggedZxid(); + + // TODO_MA 注释: 构建一个数据包,类型为: Leader.FOLLOWERINFO + QuorumPacket qp = new QuorumPacket(); + qp.setType(pktType); + qp.setZxid(ZxidUtils.makeZxid(self.getAcceptedEpoch(), 0)); + + /* + * Add sid to payload + */ + LearnerInfo li = new LearnerInfo(self.getId(), 0x10000, self.getQuorumVerifier().getVersion()); + ByteArrayOutputStream bsid = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(bsid); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送 Leader.FOLLOWERINFO 消息给 Leader + */ + boa.writeRecord(li, "LearnerInfo"); + qp.setData(bsid.toByteArray()); + writePacket(qp, true); // TODO_MA 注释: 写 FOLLOWERINFO 消息给 Leader + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取到 Leader 发送过来的 Leader.LEADERINFO + */ + readPacket(qp); + final long newEpoch = ZxidUtils.getEpochFromZxid(qp.getZxid()); + if(qp.getType() == Leader.LEADERINFO) { + // we are connected to a 1.0 server so accept the new epoch and read the next packet + leaderProtocolVersion = ByteBuffer.wrap(qp.getData()).getInt(); + byte[] epochBytes = new byte[4]; + final ByteBuffer wrappedEpochBytes = ByteBuffer.wrap(epochBytes); + if(newEpoch > self.getAcceptedEpoch()) { + wrappedEpochBytes.putInt((int) self.getCurrentEpoch()); + self.setAcceptedEpoch(newEpoch); + } else if(newEpoch == self.getAcceptedEpoch()) { + // since we have already acked an epoch equal to the leaders, we cannot ack + // again, but we still need to send our lastZxid to the leader so that we can + // sync with it if it does assume leadership of the epoch. + // the -1 indicates that this reply should not count as an ack for the new epoch + wrappedEpochBytes.putInt(-1); + } else { + throw new IOException( + "Leaders epoch, " + newEpoch + " is less than accepted epoch, " + self.getAcceptedEpoch()); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 在读取到 Leader 发送过来的 Leader.LEADERINFO 之后,给 Leader 返回一个 Leader.ACKEPOCH 消息 + */ + QuorumPacket ackNewEpoch = new QuorumPacket(Leader.ACKEPOCH, lastLoggedZxid, epochBytes, null); + writePacket(ackNewEpoch, true); + + return ZxidUtils.makeZxid(newEpoch, 0); + } else { + if(newEpoch > self.getAcceptedEpoch()) { + self.setAcceptedEpoch(newEpoch); + } + if(qp.getType() != Leader.NEWLEADER) { + LOG.error("First packet should have been NEWLEADER"); + throw new IOException("First packet should have been NEWLEADER"); + } + return qp.getZxid(); + } + } + + /** + * Finally, synchronize our history with the Leader (if Follower) + * or the LearnerMaster (if Observer). + * + * @param newLeaderZxid + * @throws IOException + * @throws InterruptedException + */ + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 和 Leader 执行同步 + */ + protected void syncWithLeader(long newLeaderZxid) throws Exception { + + // TODO_MA 注释: 准备一个最终返回给 leader 的 ack 数据包 + QuorumPacket ack = new QuorumPacket(Leader.ACK, 0, null, null); + + // TODO_MA 注释: 准备了一个数据包 + QuorumPacket qp = new QuorumPacket(); + long newEpoch = ZxidUtils.getEpochFromZxid(newLeaderZxid); + + QuorumVerifier newLeaderQV = null; + + // In the DIFF case we don't need to do a snapshot because the transactions will sync on top of any existing snapshot + // For SNAP and TRUNC the snapshot is needed to save that history + boolean snapshotNeeded = true; + boolean syncSnapshot = false; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取同步方式: DIFF TRUNC SNAP 等 + */ + readPacket(qp); + Deque packetsCommitted = new ArrayDeque<>(); + Deque packetsNotCommitted = new ArrayDeque<>(); + synchronized(zk) { + + // TODO_MA 注释: 如果是 DIFF + if(qp.getType() == Leader.DIFF) { + LOG.info("Getting a diff from the leader 0x{}", Long.toHexString(qp.getZxid())); + self.setSyncMode(QuorumPeer.SyncMode.DIFF); + snapshotNeeded = false; + } + + // TODO_MA 注释: 关于什么情况下使用 SNAP 方式来同步, 如果经过计算,发现需要广播的 日志的大小已经超过 + // TODO_MA 注释: 快照文件的 1/3, 则使用快照方式。 + // TODO_MA 注释: 第二个条件: 如果第一次使用的是 proposal + commit 失败了,下一次直接更改为 SNAP 的方式 + // TODO_MA 注释: 如果是 SNAP + else if(qp.getType() == Leader.SNAP) { + self.setSyncMode(QuorumPeer.SyncMode.SNAP); + LOG.info("Getting a snapshot from leader 0x{}", Long.toHexString(qp.getZxid())); + // The leader is going to dump the database + // db is clear as part of deserializeSnapshot() + // TODO_MA 注释: 反序列化得到 Leader 序列化过来的 ZKDatabase + // TODO_MA 注释: 在 leader 端有一个 序列化的动作 + // TODO_MA 注释: 在 follower 端有一个 反序列化的动作 + // TODO_MA 注释: 通过网络通信,传输 快照文件数据。 + zk.getZKDatabase().deserializeSnapshot(leaderIs); + // ZOOKEEPER-2819: overwrite config node content extracted + // from leader snapshot with local config, to avoid potential + // inconsistency of config node content during rolling restart. + if(!self.isReconfigEnabled()) { + LOG.debug("Reset config node content from local config after deserialization of snapshot."); + zk.getZKDatabase().initConfigInZKDatabase(self.getQuorumVerifier()); + } + String signature = leaderIs.readString("signature"); + if(!signature.equals("BenWasHere")) { + LOG.error("Missing signature. Got {}", signature); + throw new IOException("Missing signature"); + } + zk.getZKDatabase().setlastProcessedZxid(qp.getZxid()); + + // immediately persist the latest snapshot when there is txn log gap + syncSnapshot = true; + } + + // TODO_MA 注释: 如果是 TRUNC + else if(qp.getType() == Leader.TRUNC) { + //we need to truncate the log to the lastzxid of the leader + self.setSyncMode(QuorumPeer.SyncMode.TRUNC); + LOG.warn("Truncating log to get in sync with the leader 0x{}", Long.toHexString(qp.getZxid())); + boolean truncated = zk.getZKDatabase().truncateLog(qp.getZxid()); + if(!truncated) { + // not able to truncate the log + LOG.error("Not able to truncate the log 0x{}", Long.toHexString(qp.getZxid())); + ServiceUtils.requestSystemExit(ExitCode.QUORUM_PACKET_ERROR.getValue()); + } + zk.getZKDatabase().setlastProcessedZxid(qp.getZxid()); + + } else { + LOG.error("Got unexpected packet from leader: {}, exiting ... ", LearnerHandler.packetToString(qp)); + ServiceUtils.requestSystemExit(ExitCode.QUORUM_PACKET_ERROR.getValue()); + } + + // TODO_MA 注释: 创建 ZooDefs.CONFIG_NODE znode 节点 + zk.getZKDatabase().initConfigInZKDatabase(self.getQuorumVerifier()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建 SessionTracker + */ + zk.createSessionTracker(); + + long lastQueued = 0; + + // in Zab V1.0 (ZK 3.4+) we might take a snapshot when we get the NEWLEADER message, but in pre V1.0 + // we take the snapshot on the UPDATE message, since Zab V1.0 also gets the UPDATE (after the NEWLEADER) + // we need to make sure that we don't take the snapshot twice. + boolean isPreZAB1_0 = true; + //If we are not going to take the snapshot be sure the transactions are not applied in memory + // but written out to the transaction log + boolean writeToTxnLog = !snapshotNeeded; + TxnLogEntry logEntry; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 不停的读取 Leader 发送过来的 PROPOSAL 和 COMMIT + * outerLoop + * 这个循环的执行,就是真正的 LEADER 发送 DIFF + PROPOSAL + COMMIT + ACK 等 + */ + // we are now going to start getting transactions to apply followed by an UPTODATE + // TODO_MA 注释: 问题:为什么在接收到 UPTODATE 消息之后 就 break outerLoop 了呢? + // TODO_MA 注释: + outerLoop: + while(self.isRunning()) { + + // TODO_MA 注释: 读取一个数据包 + readPacket(qp); + + switch(qp.getType()) { + + // TODO_MA 注释: 如果是 PROPOSAL + case Leader.PROPOSAL: + PacketInFlight pif = new PacketInFlight(); + logEntry = SerializeUtils.deserializeTxn(qp.getData()); + pif.hdr = logEntry.getHeader(); + pif.rec = logEntry.getTxn(); + pif.digest = logEntry.getDigest(); + if(pif.hdr.getZxid() != lastQueued + 1) { + LOG.warn("Got zxid 0x{} expected 0x{}", Long.toHexString(pif.hdr.getZxid()), + Long.toHexString(lastQueued + 1)); + } + lastQueued = pif.hdr.getZxid(); + + if(pif.hdr.getType() == OpCode.reconfig) { + SetDataTxn setDataTxn = (SetDataTxn) pif.rec; + QuorumVerifier qv = self.configFromString(new String(setDataTxn.getData())); + self.setLastSeenQuorumVerifier(qv, true); + } + + // TODO_MA 注释: 处理完毕之后,加入未提交的 packets 队列 + packetsNotCommitted.add(pif); + break; + + // TODO_MA 注释: 接收到 COMMIT 命令执行提交! + case Leader.COMMIT: + case Leader.COMMITANDACTIVATE: + pif = packetsNotCommitted.peekFirst(); + if(pif.hdr.getZxid() == qp.getZxid() && qp.getType() == Leader.COMMITANDACTIVATE) { + QuorumVerifier qv = self.configFromString(new String(((SetDataTxn) pif.rec).getData())); + boolean majorChange = self + .processReconfig(qv, ByteBuffer.wrap(qp.getData()).getLong(), qp.getZxid(), true); + if(majorChange) { + throw new Exception("changes proposed in reconfig"); + } + } + if(!writeToTxnLog) { + if(pif.hdr.getZxid() != qp.getZxid()) { + LOG.warn("Committing 0x{}, but next proposal is 0x{}", Long.toHexString(qp.getZxid()), + Long.toHexString(pif.hdr.getZxid())); + } + + // TODO_MA 注释: 处理事务,并且成功之后,从 packetsNotCommitted 中移除掉 + else { + zk.processTxn(pif.hdr, pif.rec); + packetsNotCommitted.remove(); + } + } else { + packetsCommitted.add(qp.getZxid()); + } + break; + case Leader.INFORM: + case Leader.INFORMANDACTIVATE: + PacketInFlight packet = new PacketInFlight(); + + if(qp.getType() == Leader.INFORMANDACTIVATE) { + ByteBuffer buffer = ByteBuffer.wrap(qp.getData()); + long suggestedLeaderId = buffer.getLong(); + byte[] remainingdata = new byte[buffer.remaining()]; + buffer.get(remainingdata); + logEntry = SerializeUtils.deserializeTxn(remainingdata); + packet.hdr = logEntry.getHeader(); + packet.rec = logEntry.getTxn(); + packet.digest = logEntry.getDigest(); + QuorumVerifier qv = self.configFromString(new String(((SetDataTxn) packet.rec).getData())); + boolean majorChange = self.processReconfig(qv, suggestedLeaderId, qp.getZxid(), true); + if(majorChange) { + throw new Exception("changes proposed in reconfig"); + } + } else { + logEntry = SerializeUtils.deserializeTxn(qp.getData()); + packet.rec = logEntry.getTxn(); + packet.hdr = logEntry.getHeader(); + packet.digest = logEntry.getDigest(); + // Log warning message if txn comes out-of-order + if(packet.hdr.getZxid() != lastQueued + 1) { + LOG.warn("Got zxid 0x{} expected 0x{}", Long.toHexString(packet.hdr.getZxid()), + Long.toHexString(lastQueued + 1)); + } + lastQueued = packet.hdr.getZxid(); + } + if(!writeToTxnLog) { + // Apply to db directly if we haven't taken the snapshot + zk.processTxn(packet.hdr, packet.rec); + } else { + packetsNotCommitted.add(packet); + packetsCommitted.add(qp.getZxid()); + } + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果 Follower 接收到 Leader 发送过来的 UPTODATE, 则意味着同步结束,集群可以正常对外提供服务了 + * 标准: 有超过集群半数 Follower 节点完成了 和 Leader 的状态同步 + */ + case Leader.UPTODATE: + LOG.info("Learner received UPTODATE message"); + if(newLeaderQV != null) { + boolean majorChange = self.processReconfig(newLeaderQV, null, null, true); + if(majorChange) { + throw new Exception("changes proposed in reconfig"); + } + } + + if(isPreZAB1_0) { + zk.takeSnapshot(syncSnapshot); + self.setCurrentEpoch(newEpoch); + } + self.setZooKeeperServer(zk); + self.adminServer.setZooKeeperServer(zk); + + // TODO_MA 注释: 退出! + break outerLoop; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 接收到 Leader 发送过来的 NEWLEADER 消息 + * 表示,Leader 该发送给 Follower 的待同步的事务消息,都发送完毕了 + */ + case Leader.NEWLEADER: // Getting NEWLEADER here instead of in discovery + // means this is Zab 1.0 + LOG.info("Learner received NEWLEADER message"); + if(qp.getData() != null && qp.getData().length > 1) { + try { + QuorumVerifier qv = self.configFromString(new String(qp.getData())); + self.setLastSeenQuorumVerifier(qv, true); + newLeaderQV = qv; + } catch(Exception e) { + e.printStackTrace(); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 拍摄一个快照先 + * 只要完成了同步,就先拍摄一个快照! + * datatree 执行序列化存储在 快照文件中 + */ + if(snapshotNeeded) { + zk.takeSnapshot(syncSnapshot); + } + + self.setCurrentEpoch(newEpoch); + writeToTxnLog = true; + //Anything after this needs to go to the transaction log, not applied directly in memory + isPreZAB1_0 = false; + + // ZOOKEEPER-3911: make sure sync the uncommitted logs before commit them (ACK NEWLEADER). + sock.setSoTimeout(self.tickTime * self.syncLimit); + self.setSyncMode(QuorumPeer.SyncMode.NONE); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 ZooKeeper 中的各种服务 + */ + zk.startupWithoutServing(); + + if(zk instanceof FollowerZooKeeperServer) { + FollowerZooKeeperServer fzk = (FollowerZooKeeperServer) zk; + + // TODO_MA 注释: 记录日志 + for(PacketInFlight p : packetsNotCommitted) { + fzk.logRequest(p.hdr, p.rec, p.digest); + } + packetsNotCommitted.clear(); + } + + // TODO_MA 注释: 返回一个 ack 消息给 Leader + writePacket(new QuorumPacket(Leader.ACK, newLeaderZxid, null, null), true); + break; + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、DIFF TRUNC SNAP + * 2、多次 PROPOSAL + COMMIT + * 3、接收到 NEWLEADER + * 4、Follower 返回 ACK 消息给 Leader + * 5、接收到 Leader 发送过来的 UPTODATE + */ + } + ack.setZxid(ZxidUtils.makeZxid(newEpoch, 0)); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 在完成了同步之后,给 Leader 返回一个 ACK 消息 + */ + writePacket(ack, true); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 更新 Follower 节点的状态为: RUNNING + */ + zk.startServing(); + + /* + * Update the election vote here to ensure that all members of the + * ensemble report the same vote to new servers that start up and + * send leader election notifications to the ensemble. + * + * @see https://issues.apache.org/jira/browse/ZOOKEEPER-1732 + */ + self.updateElectionVote(newEpoch); + + // TODO_MA 注释: 完成同步之后,也拍摄了快照,记录一下日志 + // We need to log the stuff that came in between the snapshot and the uptodate + if(zk instanceof FollowerZooKeeperServer) { + FollowerZooKeeperServer fzk = (FollowerZooKeeperServer) zk; + for(PacketInFlight p : packetsNotCommitted) { + fzk.logRequest(p.hdr, p.rec, p.digest); + } + for(Long zxid : packetsCommitted) { + fzk.commit(zxid); + } + } else if(zk instanceof ObserverZooKeeperServer) { + // Similar to follower, we need to log requests between the snapshot and UPTODATE + ObserverZooKeeperServer ozk = (ObserverZooKeeperServer) zk; + for(PacketInFlight p : packetsNotCommitted) { + Long zxid = packetsCommitted.peekFirst(); + if(p.hdr.getZxid() != zxid) { + // log warning message if there is no matching commit + // old leader send outstanding proposal to observer + LOG.warn("Committing 0x{}, but next proposal is 0x{}", Long.toHexString(zxid), + Long.toHexString(p.hdr.getZxid())); + continue; + } + packetsCommitted.remove(); + Request request = new Request(null, p.hdr.getClientId(), p.hdr.getCxid(), p.hdr.getType(), null, null); + request.setTxn(p.rec); + request.setHdr(p.hdr); + request.setTxnDigest(p.digest); + ozk.commitRequest(request); + } + } else { + // New server type need to handle in-flight packets + throw new UnsupportedOperationException("Unknown server type"); + } + } + + protected void revalidate(QuorumPacket qp) throws IOException { + ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData()); + DataInputStream dis = new DataInputStream(bis); + long sessionId = dis.readLong(); + boolean valid = dis.readBoolean(); + ServerCnxn cnxn = pendingRevalidations.remove(sessionId); + if(cnxn == null) { + LOG.warn("Missing session 0x{} for validation", Long.toHexString(sessionId)); + } else { + zk.finishSessionInit(cnxn, valid); + } + if(LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.SESSION_TRACE_MASK, + "Session 0x" + Long.toHexString(sessionId) + " is valid: " + valid); + } + } + + protected void ping(QuorumPacket qp) throws IOException { + // Send back the ping with our session data + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + Map touchTable = zk.getTouchSnapshot(); + for(Entry entry : touchTable.entrySet()) { + dos.writeLong(entry.getKey()); + dos.writeInt(entry.getValue()); + } + + QuorumPacket pingReply = new QuorumPacket(qp.getType(), qp.getZxid(), bos.toByteArray(), qp.getAuthinfo()); + writePacket(pingReply, true); + } + + /** + * Shutdown the Peer + */ + public void shutdown() { + self.setZooKeeperServer(null); + self.closeAllConnections(); + self.adminServer.setZooKeeperServer(null); + + if(sender != null) { + sender.shutdown(); + } + + closeSocket(); + // shutdown previous zookeeper + if(zk != null) { + // If we haven't finished SNAP sync, force fully shutdown + // to avoid potential inconsistency + zk.shutdown(self.getSyncMode().equals(QuorumPeer.SyncMode.SNAP)); + } + } + + boolean isRunning() { + return self.isRunning() && zk.isRunning(); + } + + void closeSocket() { + try { + if(sock != null) { + sock.close(); + } + } catch(IOException e) { + LOG.warn("Ignoring error closing connection to leader", e); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandler.java new file mode 100644 index 0000000..38185b5 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandler.java @@ -0,0 +1,1281 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; +import javax.security.sasl.SaslException; + +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.TxnLogProposalIterator; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; +import org.apache.zookeeper.server.util.MessageTracker; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * There will be an instance of this class created by the Leader for each + * learner. All communication with a learner is handled by this + * class. + */ +public class LearnerHandler extends ZooKeeperThread { + + private static final Logger LOG = LoggerFactory.getLogger(LearnerHandler.class); + + protected final Socket sock; + + public Socket getSocket() { + return sock; + } + + final LearnerMaster learnerMaster; + + /** + * Deadline for receiving the next ack. If we are bootstrapping then + * it's based on the initLimit, if we are done bootstrapping it's based + * on the syncLimit. Once the deadline is past this learner should + * be considered no longer "sync'd" with the leader. + */ + volatile long tickOfNextAckDeadline; + + /** + * ZooKeeper server identifier of this learner + */ + protected long sid = 0; + + long getSid() { + return sid; + } + + String getRemoteAddress() { + return sock == null ? "" : sock.getRemoteSocketAddress().toString(); + } + + protected int version = 0x1; + + int getVersion() { + return version; + } + + /** + * The packets to be sent to the learner + */ + final LinkedBlockingQueue queuedPackets = new LinkedBlockingQueue(); + private final AtomicLong queuedPacketsSize = new AtomicLong(); + + protected final AtomicLong packetsReceived = new AtomicLong(); + protected final AtomicLong packetsSent = new AtomicLong(); + + protected final AtomicLong requestsReceived = new AtomicLong(); + + protected volatile long lastZxid = -1; + + public synchronized long getLastZxid() { + return lastZxid; + } + + protected final Date established = new Date(); + + public Date getEstablished() { + return (Date) established.clone(); + } + + /** + * Marker packets would be added to quorum packet queue after every + * markerPacketInterval packets. + * It is ok if packetCounter overflows. + */ + private final int markerPacketInterval = 1000; + private AtomicInteger packetCounter = new AtomicInteger(); + + /** + * This class controls the time that the Leader has been + * waiting for acknowledgement of a proposal from this Learner. + * If the time is above syncLimit, the connection will be closed. + * It keeps track of only one proposal at a time, when the ACK for + * that proposal arrives, it switches to the last proposal received + * or clears the value if there is no pending proposal. + */ + private class SyncLimitCheck { + + private boolean started = false; + private long currentZxid = 0; + private long currentTime = 0; + private long nextZxid = 0; + private long nextTime = 0; + + public synchronized void start() { + started = true; + } + + public synchronized void updateProposal(long zxid, long time) { + if(!started) { + return; + } + if(currentTime == 0) { + currentTime = time; + currentZxid = zxid; + } else { + nextTime = time; + nextZxid = zxid; + } + } + + public synchronized void updateAck(long zxid) { + if(currentZxid == zxid) { + currentTime = nextTime; + currentZxid = nextZxid; + nextTime = 0; + nextZxid = 0; + } else if(nextZxid == zxid) { + LOG.warn("ACK for 0x{} received before ACK for 0x{}", Long.toHexString(zxid), + Long.toHexString(currentZxid)); + nextTime = 0; + nextZxid = 0; + } + } + + public synchronized boolean check(long time) { + if(currentTime == 0) { + return true; + } else { + long msDelay = (time - currentTime) / 1000000; + return (msDelay < learnerMaster.syncTimeout()); + } + } + + } + + private SyncLimitCheck syncLimitCheck = new SyncLimitCheck(); + + private static class MarkerQuorumPacket extends QuorumPacket { + + long time; + + MarkerQuorumPacket(long time) { + this.time = time; + } + + @Override + public int hashCode() { + return Objects.hash(time); + } + + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + if(o == null || getClass() != o.getClass()) { + return false; + } + MarkerQuorumPacket that = (MarkerQuorumPacket) o; + return time == that.time; + } + + } + + private BinaryInputArchive ia; + + private BinaryOutputArchive oa; + + private final BufferedInputStream bufferedInput; + private BufferedOutputStream bufferedOutput; + + protected final MessageTracker messageTracker; + + // for test only + protected void setOutputArchive(BinaryOutputArchive oa) { + this.oa = oa; + } + + protected void setBufferedOutput(BufferedOutputStream bufferedOutput) { + this.bufferedOutput = bufferedOutput; + } + + /** + * Keep track of whether we have started send packets thread + */ + private volatile boolean sendingThreadStarted = false; + + /** + * // TODO_MA 注释: 强制快照同步 + * For testing purpose, force learnerMaster to use snapshot to sync with followers + */ + public static final String FORCE_SNAP_SYNC = "zookeeper.forceSnapshotSync"; + private boolean forceSnapSync = false; + + /** + * Keep track of whether we need to queue TRUNC or DIFF into packet queue + * that we are going to blast it to the learner + */ + private boolean needOpPacket = true; + + /** + * Last zxid sent to the learner as part of synchronization + */ + private long leaderLastZxid; + + /** + * for sync throttling + */ + private LearnerSyncThrottler syncThrottler = null; + + LearnerHandler(Socket sock, BufferedInputStream bufferedInput, LearnerMaster learnerMaster) throws IOException { + super("LearnerHandler-" + sock.getRemoteSocketAddress()); + this.sock = sock; + this.learnerMaster = learnerMaster; + this.bufferedInput = bufferedInput; + + if(Boolean.getBoolean(FORCE_SNAP_SYNC)) { + forceSnapSync = true; + LOG.info("Forcing snapshot sync is enabled"); + } + + try { + QuorumAuthServer authServer = learnerMaster.getQuorumAuthServer(); + if(authServer != null) { + authServer.authenticate(sock, new DataInputStream(bufferedInput)); + } + } catch(IOException e) { + LOG.error("Server failed to authenticate quorum learner, addr: {}, closing connection", + sock.getRemoteSocketAddress(), e); + try { + sock.close(); + } catch(IOException ie) { + LOG.error("Exception while closing socket", ie); + } + throw new SaslException("Authentication failure: " + e.getMessage()); + } + + this.messageTracker = new MessageTracker(MessageTracker.BUFFERED_MESSAGE_SIZE); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("LearnerHandler ").append(sock); + sb.append(" tickOfNextAckDeadline:").append(tickOfNextAckDeadline()); + sb.append(" synced?:").append(synced()); + sb.append(" queuedPacketLength:").append(queuedPackets.size()); + return sb.toString(); + } + + /** + * If this packet is queued, the sender thread will exit + */ + final QuorumPacket proposalOfDeath = new QuorumPacket(); + + private LearnerType learnerType = LearnerType.PARTICIPANT; + + public LearnerType getLearnerType() { + return learnerType; + } + + /** + * This method will use the thread to send packets added to the + * queuedPackets list + * + * @throws InterruptedException + */ + private void sendPackets() throws InterruptedException { + long traceMask = ZooTrace.SERVER_PACKET_TRACE_MASK; + while(true) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 获取 packet 执行发送 + */ + QuorumPacket p; + p = queuedPackets.poll(); + if(p == null) { + bufferedOutput.flush(); + p = queuedPackets.take(); + } + + ServerMetrics.getMetrics().LEARNER_HANDLER_QP_SIZE.add(Long.toString(this.sid), queuedPackets.size()); + + if(p instanceof MarkerQuorumPacket) { + MarkerQuorumPacket m = (MarkerQuorumPacket) p; + ServerMetrics.getMetrics().LEARNER_HANDLER_QP_TIME + .add(Long.toString(this.sid), (System.nanoTime() - m.time) / 1000000L); + continue; + } + + queuedPacketsSize.addAndGet(-packetSize(p)); + if(p == proposalOfDeath) { + // Packet of death! + break; + } + if(p.getType() == Leader.PING) { + traceMask = ZooTrace.SERVER_PING_TRACE_MASK; + } + if(p.getType() == Leader.PROPOSAL) { + syncLimitCheck.updateProposal(p.getZxid(), System.nanoTime()); + } + if(LOG.isTraceEnabled()) { + ZooTrace.logQuorumPacket(LOG, traceMask, 'o', p); + } + + // Log the zxid of the last request, if it is a valid zxid. + if(p.getZxid() > 0) { + lastZxid = p.getZxid(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 写出一个 QuorumPacket + * 发送 Follower 的请求到 Leader, 由 Follower 的 XXX 来处理 + */ + oa.writeRecord(p, "packet"); + + packetsSent.incrementAndGet(); + messageTracker.trackSent(p.getType()); + } catch(IOException e) { + if(!sock.isClosed()) { + LOG.warn("Unexpected exception at {}", this, e); + try { + // this will cause everything to shutdown on + // this learner handler and will help notify + // the learner/observer instantaneously + sock.close(); + } catch(IOException ie) { + LOG.warn("Error closing socket for handler {}", this, ie); + } + } + break; + } + } + } + + public static String packetToString(QuorumPacket p) { + String type; + String mess = null; + + switch(p.getType()) { + case Leader.ACK: + type = "ACK"; + break; + case Leader.COMMIT: + type = "COMMIT"; + break; + case Leader.FOLLOWERINFO: + type = "FOLLOWERINFO"; + break; + case Leader.NEWLEADER: + type = "NEWLEADER"; + break; + case Leader.PING: + type = "PING"; + break; + case Leader.PROPOSAL: + type = "PROPOSAL"; + break; + case Leader.REQUEST: + type = "REQUEST"; + break; + case Leader.REVALIDATE: + type = "REVALIDATE"; + ByteArrayInputStream bis = new ByteArrayInputStream(p.getData()); + DataInputStream dis = new DataInputStream(bis); + try { + long id = dis.readLong(); + mess = " sessionid = " + id; + } catch(IOException e) { + LOG.warn("Unexpected exception", e); + } + + break; + case Leader.UPTODATE: + type = "UPTODATE"; + break; + case Leader.DIFF: + type = "DIFF"; + break; + case Leader.TRUNC: + type = "TRUNC"; + break; + case Leader.SNAP: + type = "SNAP"; + break; + case Leader.ACKEPOCH: + type = "ACKEPOCH"; + break; + case Leader.SYNC: + type = "SYNC"; + break; + case Leader.INFORM: + type = "INFORM"; + break; + case Leader.COMMITANDACTIVATE: + type = "COMMITANDACTIVATE"; + break; + case Leader.INFORMANDACTIVATE: + type = "INFORMANDACTIVATE"; + break; + default: + type = "UNKNOWN" + p.getType(); + } + String entry = null; + if(type != null) { + entry = type + " " + Long.toHexString(p.getZxid()) + " " + mess; + } + return entry; + } + + /** + * This thread will receive packets from the peer and process them and + * also listen to new connections from new peers. + */ + @Override + public void run() { + try { + learnerMaster.addLearnerHandler(this); + tickOfNextAckDeadline = learnerMaster.getTickOfInitialAckDeadline(); + + // TODO_MA 注释: 获取输入输出组件 + ia = BinaryInputArchive.getArchive(bufferedInput); + bufferedOutput = new BufferedOutputStream(sock.getOutputStream()); + oa = BinaryOutputArchive.getArchive(bufferedOutput); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取到一个 Leader.FOLLOWERINFO 消息 + */ + QuorumPacket qp = new QuorumPacket(); + ia.readRecord(qp, "packet"); + + messageTracker.trackReceived(qp.getType()); + if(qp.getType() != Leader.FOLLOWERINFO && qp.getType() != Leader.OBSERVERINFO) { + LOG.error("First packet {} is not FOLLOWERINFO or OBSERVERINFO!", qp.toString()); + return; + } + + if(learnerMaster instanceof ObserverMaster && qp.getType() != Leader.OBSERVERINFO) { + throw new IOException("Non observer attempting to connect to ObserverMaster. type = " + qp.getType()); + } + byte[] learnerInfoData = qp.getData(); + if(learnerInfoData != null) { + ByteBuffer bbsid = ByteBuffer.wrap(learnerInfoData); + if(learnerInfoData.length >= 8) { + this.sid = bbsid.getLong(); + } + if(learnerInfoData.length >= 12) { + this.version = bbsid.getInt(); // protocolVersion + } + if(learnerInfoData.length >= 20) { + long configVersion = bbsid.getLong(); + if(configVersion > learnerMaster.getQuorumVerifierVersion()) { + throw new IOException("Follower is ahead of the leader (has a later activated configuration)"); + } + } + } else { + this.sid = learnerMaster.getAndDecrementFollowerCounter(); + } + + String followerInfo = learnerMaster.getPeerInfo(this.sid); + if(followerInfo.isEmpty()) { + LOG.info("Follower sid: {} not in the current config {}", this.sid, + Long.toHexString(learnerMaster.getQuorumVerifierVersion())); + } else { + LOG.info("Follower sid: {} : info : {}", this.sid, followerInfo); + } + + // TODO_MA 注释: 获取 Follower 的 Type + if(qp.getType() == Leader.OBSERVERINFO) { + learnerType = LearnerType.OBSERVER; + } + + learnerMaster.registerLearnerHandlerBean(this, sock); + + // TODO_MA 注释: 获取 Follower 的 epoch + long lastAcceptedEpoch = ZxidUtils.getEpochFromZxid(qp.getZxid()); + + long peerLastZxid; + StateSummary ss = null; + + // TODO_MA 注释: 获取 Follower 的 zxid + long zxid = qp.getZxid(); + + // TODO_MA 注释: 获取 Leader 的 epoch 和 zxid + long newEpoch = learnerMaster.getEpochToPropose(this.getSid(), lastAcceptedEpoch); + long newLeaderZxid = ZxidUtils.makeZxid(newEpoch, 0); + + if(this.getVersion() < 0x10000) { + // we are going to have to extrapolate the epoch information + long epoch = ZxidUtils.getEpochFromZxid(zxid); + ss = new StateSummary(epoch, zxid); + // fake the message + learnerMaster.waitForEpochAck(this.getSid(), ss); + } else { + byte[] ver = new byte[4]; + ByteBuffer.wrap(ver).putInt(0x10000); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 返回一个 Leader.LEADERINFO + */ + QuorumPacket newEpochPacket = new QuorumPacket(Leader.LEADERINFO, newLeaderZxid, ver, null); + oa.writeRecord(newEpochPacket, "packet"); + + messageTracker.trackSent(Leader.LEADERINFO); + bufferedOutput.flush(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取到 Follower 返回过来的 Leader.ACKEPOCH + */ + QuorumPacket ackEpochPacket = new QuorumPacket(); + ia.readRecord(ackEpochPacket, "packet"); + + messageTracker.trackReceived(ackEpochPacket.getType()); + if(ackEpochPacket.getType() != Leader.ACKEPOCH) { + LOG.error("{} is not ACKEPOCH", ackEpochPacket.toString()); + return; + } + ByteBuffer bbepoch = ByteBuffer.wrap(ackEpochPacket.getData()); + ss = new StateSummary(bbepoch.getInt(), ackEpochPacket.getZxid()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 等待有超过半数确认了 Leader.ACKEPOCH + */ + learnerMaster.waitForEpochAck(this.getSid(), ss); + } + peerLastZxid = ss.getLastZxid(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 和 Follower 确认同步方式 + */ + // Take any necessary action if we need to send TRUNC or DIFF + // startForwarding() will be called in all cases + boolean needSnap = syncFollower(peerLastZxid, learnerMaster); + + // syncs between followers and the leader are exempt from throttling because it + // is important to keep the state of quorum servers up-to-date. The exempted syncs + // are counted as concurrent syncs though + boolean exemptFromThrottle = getLearnerType() != LearnerType.OBSERVER; + /* if we are not truncating or sending a diff just send a snapshot */ + + // TODO_MA 注释: 如果确认是 snap 方式同步 + if(needSnap) { + syncThrottler = learnerMaster.getLearnerSnapSyncThrottler(); + syncThrottler.beginSync(exemptFromThrottle); + ServerMetrics.getMetrics().INFLIGHT_SNAP_COUNT.add(syncThrottler.getSyncInProgress()); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送 SNAP 消息给 Follower + */ + long zxidToSend = learnerMaster.getZKDatabase().getDataTreeLastProcessedZxid(); + oa.writeRecord(new QuorumPacket(Leader.SNAP, zxidToSend, null, null), "packet"); + messageTracker.trackSent(Leader.SNAP); + bufferedOutput.flush(); + + LOG.info( + "Sending snapshot last zxid of peer is 0x{}, zxid of leader is 0x{}, " + "send zxid of db as 0x{}, {} concurrent snapshot sync, " + "snapshot sync was {} from throttle", + Long.toHexString(peerLastZxid), Long.toHexString(leaderLastZxid), + Long.toHexString(zxidToSend), syncThrottler.getSyncInProgress(), + exemptFromThrottle ? "exempt" : "not exempt"); + + // TODO_MA 注释: 序列化 zkdatabase 到 follower + // Dump data to peer + learnerMaster.getZKDatabase().serializeSnapshot(oa); + oa.writeString("BenWasHere", "signature"); + bufferedOutput.flush(); + } finally { + ServerMetrics.getMetrics().SNAP_COUNT.add(1); + } + } else { + syncThrottler = learnerMaster.getLearnerDiffSyncThrottler(); + syncThrottler.beginSync(exemptFromThrottle); + ServerMetrics.getMetrics().INFLIGHT_DIFF_COUNT.add(syncThrottler.getSyncInProgress()); + ServerMetrics.getMetrics().DIFF_COUNT.add(1); + } + + // TODO_MA 注释: 发送 Leader.NEWLEADER 消息给 Follower + // TODO_MA 注释: 如果是 快照方式,则直接发送, 如果是其他方式,比如 DIFF TRUNC 等,则放入队列 + LOG.debug("Sending NEWLEADER message to {}", sid); + // the version of this quorumVerifier will be set by leader.lead() in case + // the leader is just being established. waitForEpochAck makes sure that readyToStart is true if + // we got here, so the version was set + if(getVersion() < 0x10000) { + QuorumPacket newLeaderQP = new QuorumPacket(Leader.NEWLEADER, newLeaderZxid, null, null); + oa.writeRecord(newLeaderQP, "packet"); + } else { + QuorumPacket newLeaderQP = new QuorumPacket(Leader.NEWLEADER, newLeaderZxid, + learnerMaster.getQuorumVerifierBytes(), null); + queuedPackets.add(newLeaderQP); + } + bufferedOutput.flush(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 开始同步 + * 启动一个线程,开始 queuedPackets 队列中的消息消费 + */ + // Start thread that blast packets in the queue to learner + startSendingPackets(); + + /* + * Have to wait for the first ACK, wait until + * the learnerMaster is ready, and only then we can start processing messages. + */ + // TODO_MA 注释: 读取一条消息: Follower 接收到 Leader.NEWLEADER 之后返回给 Leader 的 ACK 消息 + qp = new QuorumPacket(); + ia.readRecord(qp, "packet"); + + messageTracker.trackReceived(qp.getType()); + if(qp.getType() != Leader.ACK) { + LOG.error("Next packet was supposed to be an ACK, but received packet: {}", packetToString(qp)); + return; + } + + LOG.debug("Received NEWLEADER-ACK message from {}", sid); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 等待 Follower 发回 完成同步之后的 反馈 ACK + */ + learnerMaster.waitForNewLeaderAck(getSid(), qp.getZxid()); + + syncLimitCheck.start(); + // sync ends when NEWLEADER-ACK is received + syncThrottler.endSync(); + + if(needSnap) { + ServerMetrics.getMetrics().INFLIGHT_SNAP_COUNT.add(syncThrottler.getSyncInProgress()); + } else { + ServerMetrics.getMetrics().INFLIGHT_DIFF_COUNT.add(syncThrottler.getSyncInProgress()); + } + syncThrottler = null; + + // now that the ack has been processed expect the syncLimit + sock.setSoTimeout(learnerMaster.syncTimeout()); + + /* + * Wait until learnerMaster starts up + */ + // TODO_MA 注释: 确认状态为 RUNNING + learnerMaster.waitForStartup(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 当 Laeder 确认集群启动成功之后,给 所有发送了 ACK 的 Follower 发回一个 UPTODATE 消息 + */ + // Mutation packets will be queued during the serialize, + // so we need to mark when the peer can actually start using the data + LOG.debug("Sending UPTODATE message to {}", sid); + queuedPackets.add(new QuorumPacket(Leader.UPTODATE, -1, null, null)); + + + // TODO_MA 注释: 集群的真正启动成功,是到这儿为止 + // TODO_MA 注释: ZAB 的崩溃恢复模式的代码就到这儿 + // TODO_MA 注释: 接下来的代码,就是进入 消息广播模式 + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 进入正常工作模式 + * 该段代码,就是处理 Leader 和 Follower 之间的通信 + * 1、比如 ping 心跳 + * 2、比如 Follower 转发过来的 写请求等 + */ + while(true) { + + // TODO_MA 注释: 读取 Follower 发送过来的消息 + qp = new QuorumPacket(); + ia.readRecord(qp, "packet"); + messageTracker.trackReceived(qp.getType()); + + long traceMask = ZooTrace.SERVER_PACKET_TRACE_MASK; + if(qp.getType() == Leader.PING) { + traceMask = ZooTrace.SERVER_PING_TRACE_MASK; + } + if(LOG.isTraceEnabled()) { + ZooTrace.logQuorumPacket(LOG, traceMask, 'i', qp); + } + tickOfNextAckDeadline = learnerMaster.getTickOfNextAckDeadline(); + + packetsReceived.incrementAndGet(); + + ByteBuffer bb; + long sessionId; + int cxid; + int type; + + switch(qp.getType()) { + case Leader.ACK: + if(this.learnerType == LearnerType.OBSERVER) { + LOG.debug("Received ACK from Observer {}", this.sid); + } + syncLimitCheck.updateAck(qp.getZxid()); + learnerMaster.processAck(this.sid, qp.getZxid(), sock.getLocalSocketAddress()); + break; + case Leader.PING: + // Process the touches + ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData()); + DataInputStream dis = new DataInputStream(bis); + while(dis.available() > 0) { + long sess = dis.readLong(); + int to = dis.readInt(); + learnerMaster.touch(sess, to); + } + break; + case Leader.REVALIDATE: + ServerMetrics.getMetrics().REVALIDATE_COUNT.add(1); + learnerMaster.revalidateSession(qp, this); + break; + + // TODO_MA 注释: Leader.REQUEST 这就是 Follower 转发过来的事务请求 + case Leader.REQUEST: + bb = ByteBuffer.wrap(qp.getData()); + sessionId = bb.getLong(); + cxid = bb.getInt(); + type = bb.getInt(); + bb = bb.slice(); + Request si; + + // TODO_MA 注释: 同步请求(ZK不保证客户端一定能拿到最新数据,所以客户端为了能拿到最新数据,则调用个sync 操作) + // TODO_MA 注释: sync 的工作机制: follower 先向 Leader 进行同步, 然后给客户端返回结果 + if(type == OpCode.sync) { + si = new LearnerSyncRequest(this, sessionId, cxid, type, bb, qp.getAuthinfo()); + } + // TODO_MA 注释: 事务请求 + else { + si = new Request(null, sessionId, cxid, type, bb, qp.getAuthinfo()); + } + si.setOwner(this); + + // TODO_MA 注释: 提交这个请求 + learnerMaster.submitLearnerRequest(si); + requestsReceived.incrementAndGet(); + break; + default: + LOG.warn("unexpected quorum packet, type: {}", packetToString(qp)); + break; + } + } + } catch(IOException e) { + if(sock != null && !sock.isClosed()) { + LOG.error("Unexpected exception causing shutdown while sock still open", e); + //close the socket to make sure the + //other side can see it being close + try { + sock.close(); + } catch(IOException ie) { + // do nothing + } + } + } catch(InterruptedException e) { + LOG.error("Unexpected exception in LearnerHandler.", e); + } catch(SyncThrottleException e) { + LOG.error("too many concurrent sync.", e); + syncThrottler = null; + } catch(Exception e) { + LOG.error("Unexpected exception in LearnerHandler.", e); + throw e; + } finally { + if(syncThrottler != null) { + syncThrottler.endSync(); + syncThrottler = null; + } + String remoteAddr = getRemoteAddress(); + LOG.warn("******* GOODBYE {} ********", remoteAddr); + messageTracker.dumpToLog(remoteAddr); + shutdown(); + } + } + + /** + * Start thread that will forward any packet in the queue to the follower + */ + protected void startSendingPackets() { + if(!sendingThreadStarted) { + // Start sending packets + new Thread() { + public void run() { + Thread.currentThread().setName("Sender-" + sock.getRemoteSocketAddress()); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + sendPackets(); + } catch(InterruptedException e) { + LOG.warn("Unexpected interruption", e); + } + } + }.start(); + sendingThreadStarted = true; + } else { + LOG.error("Attempting to start sending thread after it already started"); + } + } + + /** + * Tests need not send marker packets as they are only needed to + * log quorum packet delays + */ + protected boolean shouldSendMarkerPacketForLogging() { + return true; + } + + /** + * Determine if we need to sync with follower using DIFF/TRUNC/SNAP + * and setup follower to receive packets from commit processor + * + * @param peerLastZxid + * @param learnerMaster + * @return true if snapshot transfer is needed. + */ + boolean syncFollower(long peerLastZxid, LearnerMaster learnerMaster) { + /* + * When leader election is completed, the leader will set its + * lastProcessedZxid to be (epoch < 32). There will be no txn associated + * with this zxid. + * + * The learner will set its lastProcessedZxid to the same value if + * it get DIFF or SNAP from the learnerMaster. If the same learner come + * back to sync with learnerMaster using this zxid, we will never find this + * zxid in our history. In this case, we will ignore TRUNC logic and + * always send DIFF if we have old enough history + */ + boolean isPeerNewEpochZxid = (peerLastZxid & 0xffffffffL) == 0; + // Keep track of the latest zxid which already queued + long currentZxid = peerLastZxid; + boolean needSnap = true; + ZKDatabase db = learnerMaster.getZKDatabase(); + + // TODO_MA 注释: 系数:1/3 + boolean txnLogSyncEnabled = db.isTxnLogSyncEnabled(); + + ReentrantReadWriteLock lock = db.getLogLock(); + ReadLock rl = lock.readLock(); + try { + rl.lock(); + + // TODO_MA 注释: 日志中的最小 和 最大 zxid + // TODO_MA 注释: 真正的最大的 zxid + // TODO_MA 注释: follower 的 zxid + long maxCommittedLog = db.getmaxCommittedLog(); + long minCommittedLog = db.getminCommittedLog(); + long lastProcessedZxid = db.getDataTreeLastProcessedZxid(); + + LOG.info( + "Synchronizing with Learner sid: {} maxCommittedLog=0x{}" + " minCommittedLog=0x{} lastProcessedZxid=0x{}" + " peerLastZxid=0x{}", + getSid(), Long.toHexString(maxCommittedLog), Long.toHexString(minCommittedLog), + Long.toHexString(lastProcessedZxid), Long.toHexString(peerLastZxid)); + + if(db.getCommittedLog().isEmpty()) { + /* + * It is possible that committedLog is empty. In that case + * setting these value to the latest txn in learnerMaster db + * will reduce the case that we need to handle + * + * Here is how each case handle by the if block below + * 1. lastProcessZxid == peerZxid -> Handle by (2) ==> empty diff + * 2. lastProcessZxid < peerZxid -> Handle by (3) ==> follower zxid 大,则发送 trunc + * 3. lastProcessZxid > peerZxid -> Handle by (5) ==> 尝试使用 txnlog+committedLog 方式同步,如果失败,则使用 snap 同步 + */ + minCommittedLog = lastProcessedZxid; + maxCommittedLog = lastProcessedZxid; + } + + /* + * Here are the cases that we want to handle + * + * 1. Force sending snapshot (for testing purpose) + * 2. Peer and learnerMaster is already sync, send empty diff + * 3. Follower has txn that we haven't seen. This may be old leader + * so we need to send TRUNC. However, if peer has newEpochZxid, + * we cannot send TRUNC since the follower has no txnlog + * 4. Follower is within committedLog range or already in-sync. + * We may need to send DIFF or TRUNC depending on follower's zxid + * We always send empty DIFF if follower is already in-sync + * 5. Follower missed the committedLog. We will try to use on-disk + * txnlog + committedLog to sync with follower. If that fail, + * we will send snapshot + */ + + // TODO_MA 注释: 默认为 false + if(forceSnapSync) { + // Force learnerMaster to use snapshot to sync with follower + LOG.warn("Forcing snapshot sync - should not see this in production"); + } + + // TODO_MA 注释: 如果 follower 的 zxid 和 leader 的一样。则发送 DIFF + // TODO_MA 注释: lastProcessedZxid 是 leader 的最大 zxid + // TODO_MA 注释: peerLastZxid 是 follower 的 zxid + else if(lastProcessedZxid == peerLastZxid) { + // Follower is already sync with us, send empty diff + LOG.info("Sending DIFF zxid=0x{} for peer sid: {}", Long.toHexString(peerLastZxid), getSid()); + + // TODO_MA 注释: 这句代码的内部,是把消息放在了队列里面 + queueOpPacket(Leader.DIFF, peerLastZxid); + needOpPacket = false; + needSnap = false; + } + + // TODO_MA 注释: 如果 follower 的 zxid 大于 leader 的最大 zxid,则发送 trunc + + else if(peerLastZxid > maxCommittedLog && !isPeerNewEpochZxid) { + // Newer than committedLog, send trunc and done + LOG.debug("Sending TRUNC to follower zxidToSend=0x{} for peer sid:{}", Long.toHexString(maxCommittedLog), + getSid()); + queueOpPacket(Leader.TRUNC, maxCommittedLog); + currentZxid = maxCommittedLog; + needOpPacket = false; + needSnap = false; + } + + // TODO_MA 注释: 如果 follower 的 zxid 在 minCommittedLog 和 maxCommittedLog 之间。 + // TODO_MA 注释: 则发送 DIFF 进行日志同步 + else if((maxCommittedLog >= peerLastZxid) && (minCommittedLog <= peerLastZxid)) { + // Follower is within commitLog range + LOG.info("Using committedLog for peer sid: {}", getSid()); + Iterator itr = db.getCommittedLog().iterator(); + currentZxid = queueCommittedProposals(itr, peerLastZxid, null, maxCommittedLog); + needSnap = false; + } + + // TODO_MA 注释: 如果 follower 的 zxid 比 minCommittedLog 还小,则同步日志 + else if(peerLastZxid < minCommittedLog && txnLogSyncEnabled) { + // Use txnlog and committedLog to sync + + // TODO_MA 注释: 计算得到最近一个快照文件的大小 + // Calculate sizeLimit that we allow to retrieve txnlog from disk + long sizeLimit = db.calculateTxnLogSizeLimit(); + + // This method can return empty iterator if the requested zxid + // is older than on-disk txnlog + Iterator txnLogItr = db.getProposalsFromTxnLog(peerLastZxid, sizeLimit); + + if(txnLogItr.hasNext()) { + + // TODO_MA 注释: 92 - 94 之间的三条数据放到队列 + LOG.info("Use txnlog and committedLog for peer sid: {}", getSid()); + currentZxid = queueCommittedProposals(txnLogItr, peerLastZxid, minCommittedLog, maxCommittedLog); + + // TODO_MA 注释: 特别注意: 如果 follower 的 zxid 小于 leader 的 minCommittedLog + // TODO_MA 注释: 则使用 snap 的方式进行同步 + if(currentZxid < minCommittedLog) { + LOG.info("Detected gap between end of txnlog: 0x{} and start of committedLog: 0x{}", + Long.toHexString(currentZxid), Long.toHexString(minCommittedLog)); + currentZxid = peerLastZxid; + // Clear out currently queued requests and revert to sending a snapshot. + queuedPackets.clear(); + needOpPacket = true; + } else { + // TODO_MA 注释: 把 95 - 100 之间的数据放到队列中 + LOG.debug("Queueing committedLog 0x{}", Long.toHexString(currentZxid)); + Iterator committedLogItr = db.getCommittedLog().iterator(); + currentZxid = queueCommittedProposals(committedLogItr, currentZxid, null, maxCommittedLog); + needSnap = false; + } + } + // closing the resources + if(txnLogItr instanceof TxnLogProposalIterator) { + TxnLogProposalIterator txnProposalItr = (TxnLogProposalIterator) txnLogItr; + txnProposalItr.close(); + } + } + + // TODO_MA 注释: 其他情况 + else { + LOG.warn( + "Unhandled scenario for peer sid: {} maxCommittedLog=0x{}" + " minCommittedLog=0x{} lastProcessedZxid=0x{}" + " peerLastZxid=0x{} txnLogSyncEnabled={}", + getSid(), Long.toHexString(maxCommittedLog), Long.toHexString(minCommittedLog), + Long.toHexString(lastProcessedZxid), Long.toHexString(peerLastZxid), txnLogSyncEnabled); + } + if(needSnap) { + currentZxid = db.getDataTreeLastProcessedZxid(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 开始进行数据同步! + */ + LOG.debug("Start forwarding 0x{} for peer sid: {}", Long.toHexString(currentZxid), getSid()); + leaderLastZxid = learnerMaster.startForwarding(this, currentZxid); + } finally { + rl.unlock(); + } + + // TODO_MA 注释: DIFF TRUNC SNAP + // TODO_MA 注释: DIFF 1、follower = leader, 2 follower 的差距并不大,差距不大于 500 + // TODO_MA 注释: TRUNC follower > leader + // TODO_MA 注释: SNAP leader - follower > 500 + if(needOpPacket && !needSnap) { + // This should never happen, but we should fall back to sending + // snapshot just in case. + LOG.error("Unhandled scenario for peer sid: {} fall back to use snapshot", getSid()); + needSnap = true; + } + + return needSnap; + } + + /** + * Queue committed proposals into packet queue. The range of packets which + * is going to be queued are (peerLaxtZxid, maxZxid] + * + * @param itr iterator point to the proposals + * @param peerLastZxid last zxid seen by the follower + * @param maxZxid max zxid of the proposal to queue, null if no limit + * @param lastCommittedZxid when sending diff, we need to send lastCommittedZxid + * on the leader to follow Zab 1.0 protocol. + * @return last zxid of the queued proposal + */ + protected long queueCommittedProposals(Iterator itr, long peerLastZxid, Long maxZxid, + Long lastCommittedZxid) { + boolean isPeerNewEpochZxid = (peerLastZxid & 0xffffffffL) == 0; + long queuedZxid = peerLastZxid; + // as we look through proposals, this variable keeps track of previous proposal Id. + long prevProposalZxid = -1; + while(itr.hasNext()) { + Proposal propose = itr.next(); + + long packetZxid = propose.packet.getZxid(); + // abort if we hit the limit + if((maxZxid != null) && (packetZxid > maxZxid)) { + break; + } + + // skip the proposals the peer already has + if(packetZxid < peerLastZxid) { + prevProposalZxid = packetZxid; + continue; + } + + // If we are sending the first packet, figure out whether to trunc or diff + if(needOpPacket) { + + // Send diff when we see the follower's zxid in our history + if(packetZxid == peerLastZxid) { + LOG.info("Sending DIFF zxid=0x{} for peer sid: {}", Long.toHexString(lastCommittedZxid), getSid()); + queueOpPacket(Leader.DIFF, lastCommittedZxid); + needOpPacket = false; + continue; + } + + if(isPeerNewEpochZxid) { + // Send diff and fall through if zxid is of a new-epoch + LOG.info("Sending DIFF zxid=0x{} for peer sid: {}", Long.toHexString(lastCommittedZxid), getSid()); + queueOpPacket(Leader.DIFF, lastCommittedZxid); + needOpPacket = false; + } else if(packetZxid > peerLastZxid) { + // Peer have some proposals that the learnerMaster hasn't seen yet + // it may used to be a leader + if(ZxidUtils.getEpochFromZxid(packetZxid) != ZxidUtils.getEpochFromZxid(peerLastZxid)) { + // We cannot send TRUNC that cross epoch boundary. + // The learner will crash if it is asked to do so. + // We will send snapshot this those cases. + LOG.warn("Cannot send TRUNC to peer sid: " + getSid() + " peer zxid is from different epoch"); + return queuedZxid; + } + + LOG.info("Sending TRUNC zxid=0x{} for peer sid: {}", Long.toHexString(prevProposalZxid), getSid()); + queueOpPacket(Leader.TRUNC, prevProposalZxid); + needOpPacket = false; + } + } + + if(packetZxid <= queuedZxid) { + // We can get here, if we don't have op packet to queue + // or there is a duplicate txn in a given iterator + continue; + } + + // TODO_MA 注释: 一条 proposal + // Since this is already a committed proposal, we need to follow it by a commit packet + queuePacket(propose.packet); + + // TODO_MA 注释: 一条 commit + queueOpPacket(Leader.COMMIT, packetZxid); + queuedZxid = packetZxid; + + } + + if(needOpPacket && isPeerNewEpochZxid) { + // We will send DIFF for this kind of zxid in any case. This if-block + // is the catch when our history older than learner and there is + // no new txn since then. So we need an empty diff + LOG.info("Sending TRUNC zxid=0x{} for peer sid: {}", Long.toHexString(lastCommittedZxid), getSid()); + queueOpPacket(Leader.DIFF, lastCommittedZxid); + needOpPacket = false; + } + + return queuedZxid; + } + + public void shutdown() { + // Send the packet of death + try { + queuedPackets.clear(); + queuedPackets.put(proposalOfDeath); + } catch(InterruptedException e) { + LOG.warn("Ignoring unexpected exception", e); + } + try { + if(sock != null && !sock.isClosed()) { + sock.close(); + } + } catch(IOException e) { + LOG.warn("Ignoring unexpected exception during socket close", e); + } + this.interrupt(); + learnerMaster.removeLearnerHandler(this); + learnerMaster.unregisterLearnerHandlerBean(this); + } + + public long tickOfNextAckDeadline() { + return tickOfNextAckDeadline; + } + + /** + * ping calls from the learnerMaster to the peers + */ + public void ping() { + // If learner hasn't sync properly yet, don't send ping packet + // otherwise, the learner will crash + if(!sendingThreadStarted) { + return; + } + long id; + if(syncLimitCheck.check(System.nanoTime())) { + id = learnerMaster.getLastProposed(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发送细腻套消息给 Follower + */ + QuorumPacket ping = new QuorumPacket(Leader.PING, id, null, null); + queuePacket(ping); + } else { + LOG.warn("Closing connection to peer due to transaction timeout."); + shutdown(); + } + } + + /** + * Queue leader packet of a given type + * + * @param type + * @param zxid + */ + private void queueOpPacket(int type, long zxid) { + QuorumPacket packet = new QuorumPacket(type, zxid, null, null); + queuePacket(packet); + } + + void queuePacket(QuorumPacket p) { + + // TODO_MA 注释: 加入队列 + queuedPackets.add(p); + // Add a MarkerQuorumPacket at regular intervals. + if(shouldSendMarkerPacketForLogging() && packetCounter.getAndIncrement() % markerPacketInterval == 0) { + queuedPackets.add(new MarkerQuorumPacket(System.nanoTime())); + } + queuedPacketsSize.addAndGet(packetSize(p)); + } + + static long packetSize(QuorumPacket p) { + /* Approximate base size of QuorumPacket: int + long + byte[] + List */ + long size = 4 + 8 + 8 + 8; + byte[] data = p.getData(); + if(data != null) { + size += data.length; + } + return size; + } + + public boolean synced() { + return isAlive() && learnerMaster.getCurrentTick() <= tickOfNextAckDeadline; + } + + public synchronized Map getLearnerHandlerInfo() { + Map info = new LinkedHashMap<>(9); + info.put("remote_socket_address", getRemoteAddress()); + info.put("sid", getSid()); + info.put("established", getEstablished()); + info.put("queued_packets", queuedPackets.size()); + info.put("queued_packets_size", queuedPacketsSize.get()); + info.put("packets_received", packetsReceived.longValue()); + info.put("packets_sent", packetsSent.longValue()); + info.put("requests", requestsReceived.longValue()); + info.put("last_zxid", getLastZxid()); + + return info; + } + + public synchronized void resetObserverConnectionStats() { + packetsReceived.set(0); + packetsSent.set(0); + requestsReceived.set(0); + + lastZxid = -1; + } + + /** + * For testing, return packet queue + * + * @return + */ + public Queue getQueuedPackets() { + return queuedPackets; + } + + /** + * For testing, we need to reset this value + */ + public void setFirstPacket(boolean value) { + needOpPacket = value; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandlerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandlerBean.java new file mode 100644 index 0000000..c09da7f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandlerBean.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.net.InetSocketAddress; +import java.net.Socket; +import javax.management.ObjectName; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LearnerHandlerBean implements LearnerHandlerMXBean, ZKMBeanInfo { + + private static final Logger LOG = LoggerFactory.getLogger(LearnerHandlerBean.class); + + private final LearnerHandler learnerHandler; + private final String remoteAddr; + + public LearnerHandlerBean(final LearnerHandler learnerHandler, final Socket socket) { + this.learnerHandler = learnerHandler; + InetSocketAddress sockAddr = (InetSocketAddress) socket.getRemoteSocketAddress(); + if (sockAddr == null) { + this.remoteAddr = "Unknown"; + } else { + this.remoteAddr = sockAddr.getAddress().getHostAddress() + ":" + sockAddr.getPort(); + } + } + + @Override + public String getName() { + return MBeanRegistry.getInstance() + .makeFullPath( + "Learner_Connections", + ObjectName.quote(remoteAddr), + String.format("\"id:%d\"", learnerHandler.getSid())); + } + + @Override + public boolean isHidden() { + return false; + } + + @Override + public void terminateConnection() { + LOG.info("terminating learner handler connection on demand {}", toString()); + learnerHandler.shutdown(); + } + + @Override + public String toString() { + return "LearnerHandlerBean{remoteIP=" + remoteAddr + ",ServerId=" + learnerHandler.getSid() + "}"; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandlerMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandlerMXBean.java new file mode 100644 index 0000000..e7aaf43 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerHandlerMXBean.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * This MBean represents a server connection for a learner. + */ +public interface LearnerHandlerMXBean { + + /** + * Terminate the connection. The learner will attempt to reconnect to + * the leader or to the next ObserverMaster if that feature is enabled + */ + void terminateConnection(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerMaster.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerMaster.java new file mode 100644 index 0000000..64333b1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerMaster.java @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.net.Socket; +import java.net.SocketAddress; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * interface for keeping Observers in sync + */ +public abstract class LearnerMaster { + + private static final Logger LOG = LoggerFactory.getLogger(LearnerMaster.class); + + // Throttle when there are too many concurrent snapshots being sent to observers + private static final String MAX_CONCURRENT_SNAPSYNCS = "zookeeper.leader.maxConcurrentSnapSyncs"; + private static final int DEFAULT_CONCURRENT_SNAPSYNCS; + + // Throttle when there are too many concurrent diff syncs being sent to observers + private static final String MAX_CONCURRENT_DIFF_SYNCS = "zookeeper.leader.maxConcurrentDiffSyncs"; + private static final int DEFAULT_CONCURRENT_DIFF_SYNCS; + + static { + DEFAULT_CONCURRENT_SNAPSYNCS = Integer.getInteger(MAX_CONCURRENT_SNAPSYNCS, 10); + LOG.info("{} = {}", MAX_CONCURRENT_SNAPSYNCS, DEFAULT_CONCURRENT_SNAPSYNCS); + + DEFAULT_CONCURRENT_DIFF_SYNCS = Integer.getInteger(MAX_CONCURRENT_DIFF_SYNCS, 100); + LOG.info("{} = {}", MAX_CONCURRENT_DIFF_SYNCS, DEFAULT_CONCURRENT_DIFF_SYNCS); + } + + private volatile int maxConcurrentSnapSyncs = DEFAULT_CONCURRENT_SNAPSYNCS; + private volatile int maxConcurrentDiffSyncs = DEFAULT_CONCURRENT_DIFF_SYNCS; + + private final LearnerSyncThrottler learnerSnapSyncThrottler = new LearnerSyncThrottler(maxConcurrentSnapSyncs, LearnerSyncThrottler.SyncType.SNAP); + + private final LearnerSyncThrottler learnerDiffSyncThrottler = new LearnerSyncThrottler(maxConcurrentDiffSyncs, LearnerSyncThrottler.SyncType.DIFF); + + public int getMaxConcurrentSnapSyncs() { + return maxConcurrentSnapSyncs; + } + + public void setMaxConcurrentSnapSyncs(int maxConcurrentSnapSyncs) { + LOG.info("Set maxConcurrentSnapSyncs to {}", maxConcurrentSnapSyncs); + this.maxConcurrentSnapSyncs = maxConcurrentSnapSyncs; + learnerSnapSyncThrottler.setMaxConcurrentSyncs(maxConcurrentSnapSyncs); + } + + public int getMaxConcurrentDiffSyncs() { + return maxConcurrentDiffSyncs; + } + + public void setMaxConcurrentDiffSyncs(int maxConcurrentDiffSyncs) { + LOG.info("Set maxConcurrentDiffSyncs to {}", maxConcurrentDiffSyncs); + this.maxConcurrentDiffSyncs = maxConcurrentDiffSyncs; + learnerDiffSyncThrottler.setMaxConcurrentSyncs(maxConcurrentDiffSyncs); + } + + /** + * snap sync throttler + * @return snapshot throttler + */ + public LearnerSyncThrottler getLearnerSnapSyncThrottler() { + return learnerSnapSyncThrottler; + } + + /** + * diff sync throttler + * @return diff throttler + */ + public LearnerSyncThrottler getLearnerDiffSyncThrottler() { + return learnerDiffSyncThrottler; + } + + /** + * start tracking a learner handler + * @param learnerHandler to track + */ + abstract void addLearnerHandler(LearnerHandler learnerHandler); + + /** + * stop tracking a learner handler + * @param learnerHandler to drop + */ + abstract void removeLearnerHandler(LearnerHandler learnerHandler); + + /** + * wait for the leader of the new epoch to be confirmed by followers + * @param sid learner id + * @param ss + * @throws IOException + * @throws InterruptedException + */ + abstract void waitForEpochAck(long sid, StateSummary ss) throws IOException, InterruptedException; + + /** + * wait for server to start + * @throws InterruptedException + */ + abstract void waitForStartup() throws InterruptedException; + + /** + * get the first zxid of the next epoch + * @param sid learner id + * @param lastAcceptedEpoch + * @return + * @throws InterruptedException + * @throws IOException + */ + abstract long getEpochToPropose(long sid, long lastAcceptedEpoch) throws InterruptedException, IOException; + + /** + * ZKDatabase + * @return ZKDatabase + */ + abstract ZKDatabase getZKDatabase(); + + /** + * wait for new leader to settle + * @param sid id of learner + * @param zxid zxid at learner + * @throws InterruptedException + */ + abstract void waitForNewLeaderAck(long sid, long zxid) throws InterruptedException; + + /** + * last proposed zxid + * @return last proposed zxid + */ + abstract long getLastProposed(); + + /** + * the current tick + * @return the current tick + */ + abstract int getCurrentTick(); + + /** + * time allowed for sync response + * @return time allowed for sync response + */ + abstract int syncTimeout(); + + /** + * deadline tick marking observer sync (initial) + * @return deadline tick marking observer sync (initial) + */ + abstract int getTickOfNextAckDeadline(); + + /** + * next deadline tick marking observer sync (steady state) + * @return next deadline tick marking observer sync (steady state) + */ + abstract int getTickOfInitialAckDeadline(); + + /** + * decrement follower count + * @return previous follower count + */ + abstract long getAndDecrementFollowerCounter(); + + /** + * handle ack packet + * @param sid leader id + * @param zxid packet zxid + * @param localSocketAddress forwarder's address + */ + abstract void processAck(long sid, long zxid, SocketAddress localSocketAddress); + + /** + * mark session as alive + * @param sess session id + * @param to timeout + */ + abstract void touch(long sess, int to); + + /** + * handle revalidate packet + * @param qp session packet + * @param learnerHandler learner + * @throws IOException + */ + abstract void revalidateSession(QuorumPacket qp, LearnerHandler learnerHandler) throws IOException; + + /** + * proxy request from learner to server + * @param si request + */ + abstract void submitLearnerRequest(Request si); + + /** + * begin forwarding packets to learner handler + * @param learnerHandler learner + * @param lastSeenZxid zxid of learner + * @return last zxid forwarded + */ + abstract long startForwarding(LearnerHandler learnerHandler, long lastSeenZxid); + + /** + * version of current quorum verifier + * @return version of current quorum verifier + */ + abstract long getQuorumVerifierVersion(); + + /** + * + * @param sid server id + * @return server information in the view + */ + abstract String getPeerInfo(long sid); + + /** + * identifier of current quorum verifier for new leader + * @return identifier of current quorum verifier for new leader + */ + abstract byte[] getQuorumVerifierBytes(); + + abstract QuorumAuthServer getQuorumAuthServer(); + + /** + * registers the handler's bean + * @param learnerHandler handler + * @param socket connection to learner + */ + abstract void registerLearnerHandlerBean(LearnerHandler learnerHandler, Socket socket); + + /** + * unregisters the handler's bean + * @param learnerHandler handler + */ + abstract void unregisterLearnerHandlerBean(LearnerHandler learnerHandler); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSender.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSender.java new file mode 100644 index 0000000..77d0015 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSender.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LearnerSender extends ZooKeeperCriticalThread { + private static final Logger LOG = LoggerFactory.getLogger(LearnerSender.class); + + private final LinkedBlockingQueue queuedPackets = new LinkedBlockingQueue<>(); + private final QuorumPacket proposalOfDeath = new QuorumPacket(); + + Learner learner; + + public LearnerSender(Learner learner) { + super("LearnerSender:" + learner.zk.getServerId(), learner.zk.getZooKeeperServerListener()); + this.learner = learner; + } + + @Override + public void run() { + while (true) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从 queuedPackets 获取 packet + */ + QuorumPacket p = queuedPackets.poll(); + if (p == null) { + learner.bufferedOutput.flush(); + p = queuedPackets.take(); + } + + if (p == proposalOfDeath) { + // Packet of death! + break; + } + + learner.messageTracker.trackSent(p.getType()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 写出去一个 packet 信息给 leader + */ + learner.leaderOs.writeRecord(p, "packet"); + } catch (IOException e) { + handleException(this.getName(), e); + break; + } catch (InterruptedException e) { + handleException(this.getName(), e); + break; + } + } + + LOG.info("LearnerSender exited"); + } + + public void queuePacket(QuorumPacket pp) throws IOException { + if (pp == null) { + learner.bufferedOutput.flush(); + } else { + queuedPackets.add(pp); + } + } + + public void shutdown() { + LOG.info("Shutting down LearnerSender"); + queuedPackets.clear(); + queuedPackets.add(proposalOfDeath); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSessionTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSessionTracker.java new file mode 100644 index 0000000..cb7058d --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSessionTracker.java @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.KeeperException.UnknownSessionException; +import org.apache.zookeeper.server.SessionTrackerImpl; +import org.apache.zookeeper.server.ZooKeeperServerListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The learner session tracker is used by learners (followers and observers) to + * track zookeeper sessions which may or may not be echoed to the leader. When + * a new session is created it is saved locally in a wrapped + * LocalSessionTracker. It can subsequently be upgraded to a global session + * as required. If an upgrade is requested the session is removed from local + * collections while keeping the same session ID. It is up to the caller to + * queue a session creation request for the leader. + * A secondary function of the learner session tracker is to remember sessions + * which have been touched in this service. This information is passed along + * to the leader with a ping. + */ +public class LearnerSessionTracker extends UpgradeableSessionTracker { + + private static final Logger LOG = LoggerFactory.getLogger(LearnerSessionTracker.class); + + private final SessionExpirer expirer; + // Touch table for the global sessions + private final AtomicReference> touchTable = new AtomicReference>(); + private final long serverId; + private final AtomicLong nextSessionId = new AtomicLong(); + + private final ConcurrentMap globalSessionsWithTimeouts; + + public LearnerSessionTracker(SessionExpirer expirer, ConcurrentMap sessionsWithTimeouts, int tickTime, + long id, boolean localSessionsEnabled, ZooKeeperServerListener listener) { + this.expirer = expirer; + this.touchTable.set(new ConcurrentHashMap()); + this.globalSessionsWithTimeouts = sessionsWithTimeouts; + this.serverId = id; + nextSessionId.set(SessionTrackerImpl.initializeNextSessionId(serverId)); + + this.localSessionsEnabled = localSessionsEnabled; + if(this.localSessionsEnabled) { + createLocalSessionTracker(expirer, tickTime, id, listener); + } + } + + public void removeSession(long sessionId) { + if(localSessionTracker != null) { + localSessionTracker.removeSession(sessionId); + } + globalSessionsWithTimeouts.remove(sessionId); + touchTable.get().remove(sessionId); + } + + public void start() { + if(localSessionTracker != null) { + localSessionTracker.start(); + } + } + + public void shutdown() { + if(localSessionTracker != null) { + localSessionTracker.shutdown(); + } + } + + public boolean isGlobalSession(long sessionId) { + return globalSessionsWithTimeouts.containsKey(sessionId); + } + + public boolean trackSession(long sessionId, int sessionTimeout) { + // Learner doesn't track global session, do nothing here + return false; + } + + /** + * Synchronized on this to avoid race condition of adding a local session + * after committed global session, which may cause the same session being + * tracked on this server and leader. + */ + public synchronized boolean commitSession(long sessionId, int sessionTimeout) { + boolean added = globalSessionsWithTimeouts.put(sessionId, sessionTimeout) == null; + + if(added) { + // Only do extra logging so we know what kind of session this is + // if we're supporting both kinds of sessions + LOG.info("Committing global session 0x{}", Long.toHexString(sessionId)); + } + + // If the session moved before the session upgrade finished, it's + // possible that the session will be added to the local session + // again. Need to double check and remove it from local session + // tracker when the global session is quorum committed, otherwise the + // local session might be tracked both locally and on leader. + // + // This cannot totally avoid the local session being upgraded again + // because there is still race condition between create another upgrade + // request and process the createSession commit, and there is no way + // to know there is a on flying createSession request because it might + // be upgraded by other server which owns the session before move. + if(localSessionsEnabled) { + removeLocalSession(sessionId); + finishedUpgrading(sessionId); + } + + touchTable.get().put(sessionId, sessionTimeout); + return added; + } + + public boolean touchSession(long sessionId, int sessionTimeout) { + if(localSessionsEnabled) { + if(localSessionTracker.touchSession(sessionId, sessionTimeout)) { + return true; + } + if(!isGlobalSession(sessionId) && !isUpgradingSession(sessionId)) { + return false; + } + } + touchTable.get().put(sessionId, sessionTimeout); + return true; + } + + public Map snapshot() { + return touchTable.getAndSet(new ConcurrentHashMap()); + } + + public long createSession(int sessionTimeout) { + if(localSessionsEnabled) { + return localSessionTracker.createSession(sessionTimeout); + } + return nextSessionId.getAndIncrement(); + } + + public void checkSession(long sessionId, Object owner) throws SessionExpiredException, SessionMovedException { + if(localSessionTracker != null) { + try { + localSessionTracker.checkSession(sessionId, owner); + return; + } catch(UnknownSessionException e) { + // Check whether it's a global session. We can ignore those + // because they are handled at the leader, but if not, rethrow. + // We check local session status first to avoid race condition + // with session upgrading. + if(!isGlobalSession(sessionId)) { + throw new SessionExpiredException(); + } + } + } + } + + public void setOwner(long sessionId, Object owner) throws SessionExpiredException { + if(localSessionTracker != null) { + try { + localSessionTracker.setOwner(sessionId, owner); + return; + } catch(SessionExpiredException e) { + // Check whether it's a global session. We can ignore those + // because they are handled at the leader, but if not, rethrow. + // We check local session status first to avoid race condition + // with session upgrading. + if(!isGlobalSession(sessionId)) { + throw e; + } + } + } + } + + public void dumpSessions(PrintWriter pwriter) { + if(localSessionTracker != null) { + pwriter.print("Local "); + localSessionTracker.dumpSessions(pwriter); + } + pwriter.print("Global Sessions("); + pwriter.print(globalSessionsWithTimeouts.size()); + pwriter.println("):"); + SortedSet sessionIds = new TreeSet(globalSessionsWithTimeouts.keySet()); + for(long sessionId : sessionIds) { + pwriter.print("0x"); + pwriter.print(Long.toHexString(sessionId)); + pwriter.print("\t"); + pwriter.print(globalSessionsWithTimeouts.get(sessionId)); + pwriter.println("ms"); + } + } + + public void setSessionClosing(long sessionId) { + // Global sessions handled on the leader; this call is a no-op if + // not tracked as a local session so safe to call in both cases. + if(localSessionTracker != null) { + localSessionTracker.setSessionClosing(sessionId); + } + } + + @Override + public Map> getSessionExpiryMap() { + return new HashMap>(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSyncRequest.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSyncRequest.java new file mode 100644 index 0000000..23125b4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSyncRequest.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.nio.ByteBuffer; +import java.util.List; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.Request; + +public class LearnerSyncRequest extends Request { + + LearnerHandler fh; + + public LearnerSyncRequest( + LearnerHandler fh, long sessionId, int xid, int type, ByteBuffer bb, List authInfo) { + super(null, sessionId, xid, type, bb, authInfo); + this.fh = fh; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSyncThrottler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSyncThrottler.java new file mode 100644 index 0000000..8b5de6a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerSyncThrottler.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility class to limit the number of concurrent syncs from a leader to + * observers and followers or from a follower to observers. {@link LearnerHandler} + * objects should call {@link #beginSync(boolean)} before sending a sync and + * {@link #endSync()} after finishing, successfully or not. + * + */ +public class LearnerSyncThrottler { + + private static final Logger LOG = LoggerFactory.getLogger(LearnerSyncThrottler.class); + + private final Object countSyncObject = new Object(); + private int syncInProgress; + + private volatile int maxConcurrentSyncs; + + public enum SyncType { + DIFF, + SNAP + } + + private final SyncType syncType; + + /** + * Constructs a new instance limiting the concurrent number of syncs to + * maxConcurrentSyncs. + * @param maxConcurrentSyncs maximum concurrent number of syncs + * @param syncType either a snapshot sync or a txn-based diff sync + * @throws java.lang.IllegalArgumentException when maxConcurrentSyncs + * is less than 1 + */ + public LearnerSyncThrottler(int maxConcurrentSyncs, SyncType syncType) throws IllegalArgumentException { + if (maxConcurrentSyncs <= 0) { + String errorMsg = "maxConcurrentSyncs must be positive, was " + maxConcurrentSyncs; + throw new IllegalArgumentException(errorMsg); + } + + this.maxConcurrentSyncs = maxConcurrentSyncs; + this.syncType = syncType; + + synchronized (countSyncObject) { + syncInProgress = 0; + } + } + + /** + * Indicates that a new sync is about to be sent. + * + * @param essential if true, do not throw an exception even + * if throttling limit is reached + * @throws SyncThrottleException if throttling limit has been exceeded + * and essential == false, + * even after waiting for the timeout + * period, if any + * @throws InterruptedException if thread is interrupted while trying + * to start a sync; cannot happen if + * timeout is zero + */ + protected void beginSync(boolean essential) throws SyncThrottleException, InterruptedException { + + synchronized (countSyncObject) { + if (essential || syncInProgress < maxConcurrentSyncs) { + syncInProgress++; + } else { + throw new SyncThrottleException(syncInProgress + 1, maxConcurrentSyncs, syncType); + } + } + } + + /** + * Indicates that a sync has been completed. + */ + public void endSync() { + int newCount; + synchronized (countSyncObject) { + syncInProgress--; + newCount = syncInProgress; + countSyncObject.notify(); + } + + if (newCount < 0) { + String errorMsg = "endSync() called incorrectly; current sync count is " + newCount; + LOG.error(errorMsg); + } + } + + public void setMaxConcurrentSyncs(int maxConcurrentSyncs) { + this.maxConcurrentSyncs = maxConcurrentSyncs; + } + + public int getSyncInProgress() { + return syncInProgress; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerZooKeeperServer.java new file mode 100644 index 0000000..2afd4c1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LearnerZooKeeperServer.java @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; + +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.DataTreeBean; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServerBean; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; + +/** + * Parent class for all ZooKeeperServers for Learners + */ +public abstract class LearnerZooKeeperServer extends QuorumZooKeeperServer { + + /* + * Request processors + */ + protected CommitProcessor commitProcessor; + protected SyncRequestProcessor syncProcessor; + + public LearnerZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, + int listenBacklog, ZKDatabase zkDb, QuorumPeer self) throws IOException { + super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, listenBacklog, zkDb, self); + } + + /** + * Abstract method to return the learner associated with this server. + * Since the Learner may change under our feet (when QuorumPeer reassigns + * it) we can't simply take a reference here. Instead, we need the + * subclasses to implement this. + */ + public abstract Learner getLearner(); + + /** + * Returns the current state of the session tracker. This is only currently + * used by a Learner to build a ping response packet. + */ + protected Map getTouchSnapshot() { + if(sessionTracker != null) { + return ((LearnerSessionTracker) sessionTracker).snapshot(); + } + Map map = Collections.emptyMap(); + return map; + } + + /** + * Returns the id of the associated QuorumPeer, which will do for a unique + * id of this server. + */ + @Override + public long getServerId() { + return self.getId(); + } + + @Override + public void createSessionTracker() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + sessionTracker = new LearnerSessionTracker(this, getZKDatabase().getSessionWithTimeOuts(), this.tickTime, + self.getId(), self.areLocalSessionsEnabled(), getZooKeeperServerListener()); + } + + @Override + protected void revalidateSession(ServerCnxn cnxn, long sessionId, int sessionTimeout) throws IOException { + if(upgradeableSessionTracker.isLocalSession(sessionId)) { + super.revalidateSession(cnxn, sessionId, sessionTimeout); + } else { + getLearner().validateSession(cnxn, sessionId, sessionTimeout); + } + } + + @Override + protected void registerJMX() { + // register with JMX + try { + jmxDataTreeBean = new DataTreeBean(getZKDatabase().getDataTree()); + MBeanRegistry.getInstance().register(jmxDataTreeBean, jmxServerBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxDataTreeBean = null; + } + } + + public void registerJMX(ZooKeeperServerBean serverBean, LocalPeerBean localPeerBean) { + // register with JMX + if(self.jmxLeaderElectionBean != null) { + try { + MBeanRegistry.getInstance().unregister(self.jmxLeaderElectionBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + } + self.jmxLeaderElectionBean = null; + } + + try { + jmxServerBean = serverBean; + MBeanRegistry.getInstance().register(serverBean, localPeerBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxServerBean = null; + } + } + + @Override + protected void unregisterJMX() { + // unregister from JMX + try { + if(jmxDataTreeBean != null) { + MBeanRegistry.getInstance().unregister(jmxDataTreeBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxDataTreeBean = null; + } + + protected void unregisterJMX(Learner peer) { + // unregister from JMX + try { + if(jmxServerBean != null) { + MBeanRegistry.getInstance().unregister(jmxServerBean); + } + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxServerBean = null; + } + + @Override + public synchronized void shutdown() { + if(!canShutdown()) { + LOG.debug("ZooKeeper server is not running, so not proceeding to shutdown!"); + return; + } + LOG.info("Shutting down"); + try { + super.shutdown(); + } catch(Exception e) { + LOG.warn("Ignoring unexpected exception during shutdown", e); + } + try { + if(syncProcessor != null) { + syncProcessor.shutdown(); + } + } catch(Exception e) { + LOG.warn("Ignoring unexpected exception in syncprocessor shutdown", e); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalPeerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalPeerBean.java new file mode 100644 index 0000000..f5d0dbe --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalPeerBean.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.common.NetUtils.formatInetAddr; +import java.util.stream.Collectors; +import org.apache.zookeeper.common.NetUtils; +import org.apache.zookeeper.server.ServerCnxnHelper; + +/** + * Implementation of the local peer MBean interface. + */ +public class LocalPeerBean extends ServerBean implements LocalPeerMXBean { + + private final QuorumPeer peer; + + public LocalPeerBean(QuorumPeer peer) { + this.peer = peer; + } + + public String getName() { + return "replica." + peer.getId(); + } + + public boolean isHidden() { + return false; + } + + public int getTickTime() { + return peer.getTickTime(); + } + + public int getMaxClientCnxnsPerHost() { + return peer.getMaxClientCnxnsPerHost(); + } + + public int getMinSessionTimeout() { + return peer.getMinSessionTimeout(); + } + + public int getMaxSessionTimeout() { + return peer.getMaxSessionTimeout(); + } + + public int getInitLimit() { + return peer.getInitLimit(); + } + + public int getSyncLimit() { + return peer.getSyncLimit(); + } + + public void setInitLimit(int initLimit) { + peer.setInitLimit(initLimit); + } + + public void setSyncLimit(int syncLimit) { + peer.setSyncLimit(syncLimit); + } + + public int getTick() { + return peer.getTick(); + } + + public String getState() { + return peer.getServerState(); + } + + public String getQuorumAddress() { + return peer.getQuorumAddress().getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|")); + } + + public int getElectionType() { + return peer.getElectionType(); + } + + public String getElectionAddress() { + return peer.getElectionAddress().getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|")); + } + + public String getClientAddress() { + if (null != peer.cnxnFactory) { + return formatInetAddr(peer.cnxnFactory.getLocalAddress()); + } else { + return ""; + } + } + + public String getLearnerType() { + return peer.getLearnerType().toString(); + } + + public long getConfigVersion() { + return peer.getQuorumVerifier().getVersion(); + } + + @Override + public String getQuorumSystemInfo() { + return peer.getQuorumVerifier().toString(); + } + + @Override + public boolean isPartOfEnsemble() { + return peer.getView().containsKey(peer.getId()); + } + + @Override + public boolean isLeader() { + return peer.isLeader(peer.getId()); + } + + @Override + public int getMaxCnxns() { + return ServerCnxnHelper.getMaxCnxns(peer.secureCnxnFactory, peer.cnxnFactory); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalPeerMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalPeerMXBean.java new file mode 100644 index 0000000..ca8edff --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalPeerMXBean.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * A local zookeeper server MBean interface. Unlike the remote peer, the local + * peer provides complete state/statistics at runtime and can be managed (just + * like a standalone zookeeper server). + */ +public interface LocalPeerMXBean extends ServerMXBean { + + /** + * @return the number of milliseconds of each tick + */ + int getTickTime(); + + /** Current maxClientCnxns allowed from a particular host */ + int getMaxClientCnxnsPerHost(); + + /** + * @return the minimum number of milliseconds allowed for a session timeout + */ + int getMinSessionTimeout(); + + /** + * @return the maximum number of milliseconds allowed for a session timeout + */ + int getMaxSessionTimeout(); + + /** + * @return the number of ticks that the initial sync phase can take + */ + int getInitLimit(); + + /** + * @return the number of ticks that can pass between sending a request + * and getting a acknowledgment + */ + int getSyncLimit(); + + /** + * Set the number of ticks that the initial sync phase can take + */ + void setInitLimit(int initLimit); + + /** + * Set the number of ticks that can pass between sending a request + * and getting a acknowledgment + */ + void setSyncLimit(int syncLimit); + + /** + * @return the current tick + */ + int getTick(); + + /** + * @return the current server state + */ + String getState(); + + /** + * @return the quorum address + */ + String getQuorumAddress(); + + /** + * @return the election type + */ + int getElectionType(); + + /** + * @return the election address + */ + String getElectionAddress(); + + /** + * @return the client address + */ + String getClientAddress(); + + /** + * @return the learner type + */ + String getLearnerType(); + + /** + * @return the config version + */ + long getConfigVersion(); + + /** + * @return the quorum system information + */ + String getQuorumSystemInfo(); + + /** + * @return true if quorum peer is part of the ensemble, false otherwise + */ + boolean isPartOfEnsemble(); + + /** + * @return true if the peer is the current leader + */ + boolean isLeader(); + + /** + * @return Current maxCnxns allowed to a single ZooKeeper server + */ + int getMaxCnxns(); +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalSessionTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalSessionTracker.java new file mode 100644 index 0000000..009fe03 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/LocalSessionTracker.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.concurrent.ConcurrentMap; +import org.apache.zookeeper.server.SessionTrackerImpl; +import org.apache.zookeeper.server.ZooKeeperServerListener; + +/** + * Local session tracker. + */ +public class LocalSessionTracker extends SessionTrackerImpl { + + public LocalSessionTracker(SessionExpirer expirer, ConcurrentMap sessionsWithTimeouts, int tickTime, long id, ZooKeeperServerListener listener) { + super(expirer, sessionsWithTimeouts, tickTime, id, listener); + } + + public boolean isLocalSession(long sessionId) { + return isTrackingSession(sessionId); + } + + public boolean isGlobalSession(long sessionId) { + return false; + } + + public long createSession(int sessionTimeout) { + long sessionId = super.createSession(sessionTimeout); + commitSession(sessionId, sessionTimeout); + return sessionId; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/MultipleAddresses.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/MultipleAddresses.java new file mode 100644 index 0000000..9f15389 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/MultipleAddresses.java @@ -0,0 +1,259 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static java.util.Arrays.asList; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NoRouteToHostException; +import java.net.UnknownHostException; +import java.time.Duration; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.NoSuchElementException; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** + * This class allows to store several quorum and electing addresses. + * + * See ZOOKEEPER-3188 for a discussion of this feature. + */ +public final class MultipleAddresses { + public static final Duration DEFAULT_TIMEOUT = Duration.ofMillis(1000); + + private static Set newConcurrentHashSet() { + return Collections.newSetFromMap(new ConcurrentHashMap<>()); + } + + private Set addresses; + private final Duration timeout; + + public MultipleAddresses() { + this(Collections.emptyList()); + } + + public MultipleAddresses(Collection addresses) { + this(addresses, DEFAULT_TIMEOUT); + } + + public MultipleAddresses(InetSocketAddress address) { + this(asList(address), DEFAULT_TIMEOUT); + } + + public MultipleAddresses(Collection addresses, Duration timeout) { + this.addresses = newConcurrentHashSet(); + this.addresses.addAll(addresses); + this.timeout = timeout; + } + + public boolean isEmpty() { + return addresses.isEmpty(); + } + + /** + * Returns all addresses in an unmodifiable set. + * + * @return set of all InetSocketAddress + */ + public Set getAllAddresses() { + return Collections.unmodifiableSet(addresses); + } + + /** + * Returns wildcard addresses for all ports + * + * @return set of InetSocketAddress with wildcards for all ports + */ + public Set getWildcardAddresses() { + return addresses.stream().map(a -> new InetSocketAddress(a.getPort())).collect(Collectors.toSet()); + } + + /** + * Returns all ports + * + * @return list of all ports + */ + public List getAllPorts() { + return addresses.stream().map(InetSocketAddress::getPort).distinct().collect(Collectors.toList()); + } + + /** + * Returns distinct list of all host strings + * + * @return list of all hosts + */ + public List getAllHostStrings() { + return addresses.stream().map(InetSocketAddress::getHostString).distinct().collect(Collectors.toList()); + } + + public void addAddress(InetSocketAddress address) { + addresses.add(address); + } + + /** + * Returns a reachable address. If none is reachable than throws exception. + * The function is nondeterministic in the sense that the result of calling this function + * twice with the same set of reachable addresses might lead to different results. + * + * @return address which is reachable. + * @throws NoRouteToHostException if none of the addresses are reachable + */ + public InetSocketAddress getReachableAddress() throws NoRouteToHostException { + // using parallelStream() + findAny() will help to minimize the time spent on network operations + return addresses.parallelStream() + .filter(this::checkIfAddressIsReachable) + .findAny() + .orElseThrow(() -> new NoRouteToHostException("No valid address among " + addresses)); + } + + /** + * Returns a set of all reachable addresses. If none is reachable than returns empty set. + * + * @return all addresses which are reachable. + */ + public Set getAllReachableAddresses() { + // using parallelStream() will help to minimize the time spent on network operations + return addresses.parallelStream() + .filter(this::checkIfAddressIsReachable) + .collect(Collectors.toSet()); + } + + /** + * Returns a set of all reachable addresses. If none is reachable than returns all addresses. + * + * @return all reachable addresses, or all addresses if none is reachable. + */ + public Set getAllReachableAddressesOrAll() { + // if there is only a single address provided then we don't need to do any reachability check + if (addresses.size() == 1) { + return getAllAddresses(); + } + + Set allReachable = getAllReachableAddresses(); + if (allReachable.isEmpty()) { + return getAllAddresses(); + } + return allReachable; + } + + /** + * Returns a reachable address or an arbitrary one, if none is reachable. It throws an exception + * if there are no addresses registered. The function is nondeterministic in the sense that the + * result of calling this function twice with the same set of reachable addresses might lead + * to different results. + * + * @return address which is reachable or fist one. + * @throws NoSuchElementException if there is no address registered + */ + public InetSocketAddress getReachableOrOne() { + InetSocketAddress address; + + // if there is only a single address provided then we don't do any reachability check + if (addresses.size() == 1) { + return getOne(); + } + + try { + address = getReachableAddress(); + } catch (NoRouteToHostException e) { + address = getOne(); + } + return address; + } + + /** + * Performs a parallel DNS lookup for all addresses. + * + * If the DNS lookup fails, then address remain unmodified. + */ + public void recreateSocketAddresses() { + addresses = addresses.parallelStream() + .map(this::recreateSocketAddress) + .collect(Collectors.toCollection(MultipleAddresses::newConcurrentHashSet)); + } + + /** + * Returns an address from the set. + * + * @return address from a set. + * @throws NoSuchElementException if there is no address registered + */ + public InetSocketAddress getOne() { + return addresses.iterator().next(); + } + + + /** + * Returns the number of addresses in the set. + * + * @return the number of addresses. + */ + public int size() { + return addresses.size(); + } + + private boolean checkIfAddressIsReachable(InetSocketAddress address) { + if (address.isUnresolved()) { + return false; + } + try { + if (address.getAddress().isReachable((int) timeout.toMillis())) { + return true; + } + } catch (IOException ignored) { + // ignore, we don't really care if we can't reach it for timeout or for IO problems + } + return false; + } + + private InetSocketAddress recreateSocketAddress(InetSocketAddress address) { + try { + return new InetSocketAddress(InetAddress.getByName(address.getHostString()), address.getPort()); + } catch (UnknownHostException e) { + return address; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (o == null || getClass() != o.getClass()) { + return false; + } + + MultipleAddresses that = (MultipleAddresses) o; + return Objects.equals(addresses, that.addresses); + } + + @Override + public int hashCode() { + return Objects.hash(addresses); + } + + @Override + public String toString() { + return addresses.stream().map(InetSocketAddress::toString).collect(Collectors.joining("|")); + } +} \ No newline at end of file diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Observer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Observer.java new file mode 100644 index 0000000..9d14ce5 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Observer.java @@ -0,0 +1,306 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.nio.ByteBuffer; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.jute.Record; +import org.apache.zookeeper.server.ObserverBean; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Observers are peers that do not take part in the atomic broadcast protocol. + * Instead, they are informed of successful proposals by the Leader. Observers + * therefore naturally act as a relay point for publishing the proposal stream + * and can relieve Followers of some of the connection load. Observers may + * submit proposals, but do not vote in their acceptance. + * + * See ZOOKEEPER-368 for a discussion of this feature. + */ +public class Observer extends Learner { + + private static final Logger LOG = LoggerFactory.getLogger(Observer.class); + + /** + * When observer lost its connection with the leader, it waits for 0 to the + * specified value before trying to reconnect with the leader. So that + * the entire observer fleet won't try to run leader election and reconnect + * to the leader at once. Default value is zero. + */ + public static final String OBSERVER_RECONNECT_DELAY_MS = "zookeeper.observer.reconnectDelayMs"; + + /** + * Delay the Observer's participation in a leader election upon disconnect + * so as to prevent unexpected additional load on the voting peers during + * the process. Default value is 200. + */ + public static final String OBSERVER_ELECTION_DELAY_MS = "zookeeper.observer.election.DelayMs"; + + private static final long reconnectDelayMs; + + private static volatile long observerElectionDelayMs; + + static { + reconnectDelayMs = Long.getLong(OBSERVER_RECONNECT_DELAY_MS, 0); + LOG.info("{} = {}", OBSERVER_RECONNECT_DELAY_MS, reconnectDelayMs); + observerElectionDelayMs = Long.getLong(OBSERVER_ELECTION_DELAY_MS, 200); + LOG.info("{} = {}", OBSERVER_ELECTION_DELAY_MS , observerElectionDelayMs); + } + + /** + * next learner master to try, when specified + */ + private static final AtomicReference nextLearnerMaster = new AtomicReference<>(); + + private QuorumPeer.QuorumServer currentLearnerMaster = null; + + Observer(QuorumPeer self, ObserverZooKeeperServer observerZooKeeperServer) { + this.self = self; + this.zk = observerZooKeeperServer; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("Observer ").append(sock); + sb.append(" pendingRevalidationCount:").append(pendingRevalidations.size()); + return sb.toString(); + } + + /** + * the main method called by the observer to observe the leader + * @throws Exception + */ + void observeLeader() throws Exception { + zk.registerJMX(new ObserverBean(this, zk), self.jmxLocalPeerBean); + long connectTime = 0; + boolean completedSync = false; + try { + self.setZabState(QuorumPeer.ZabState.DISCOVERY); + QuorumServer master = findLearnerMaster(); + try { + connectToLeader(master.addr, master.hostname); + connectTime = System.currentTimeMillis(); + long newLeaderZxid = registerWithLeader(Leader.OBSERVERINFO); + if (self.isReconfigStateChange()) { + throw new Exception("learned about role change"); + } + + self.setLeaderAddressAndId(master.addr, master.getId()); + self.setZabState(QuorumPeer.ZabState.SYNCHRONIZATION); + syncWithLeader(newLeaderZxid); + self.setZabState(QuorumPeer.ZabState.BROADCAST); + completedSync = true; + QuorumPacket qp = new QuorumPacket(); + while (this.isRunning() && nextLearnerMaster.get() == null) { + readPacket(qp); + processPacket(qp); + } + } catch (Exception e) { + LOG.warn("Exception when observing the leader", e); + closeSocket(); + + // clear pending revalidations + pendingRevalidations.clear(); + } + } finally { + currentLearnerMaster = null; + zk.unregisterJMX(this); + if (connectTime != 0) { + long connectionDuration = System.currentTimeMillis() - connectTime; + + LOG.info( + "Disconnected from leader (with address: {}). Was connected for {}ms. Sync state: {}", + leaderAddr, + connectionDuration, + completedSync); + messageTracker.dumpToLog(leaderAddr.toString()); + } + } + } + + private QuorumServer findLearnerMaster() { + QuorumPeer.QuorumServer prescribedLearnerMaster = nextLearnerMaster.getAndSet(null); + if (prescribedLearnerMaster != null + && self.validateLearnerMaster(Long.toString(prescribedLearnerMaster.id)) == null) { + LOG.warn("requested next learner master {} is no longer valid", prescribedLearnerMaster); + prescribedLearnerMaster = null; + } + final QuorumPeer.QuorumServer master = (prescribedLearnerMaster == null) + ? self.findLearnerMaster(findLeader()) + : prescribedLearnerMaster; + currentLearnerMaster = master; + if (master == null) { + LOG.warn("No learner master found"); + } else { + LOG.info("Observing new leader sid={} addr={}", master.id, master.addr); + } + return master; + } + + /** + * Controls the response of an observer to the receipt of a quorumpacket + * @param qp + * @throws Exception + */ + protected void processPacket(QuorumPacket qp) throws Exception { + TxnLogEntry logEntry; + TxnHeader hdr; + TxnDigest digest; + Record txn; + switch (qp.getType()) { + case Leader.PING: + ping(qp); + break; + case Leader.PROPOSAL: + LOG.warn("Ignoring proposal"); + break; + case Leader.COMMIT: + LOG.warn("Ignoring commit"); + break; + case Leader.UPTODATE: + LOG.error("Received an UPTODATE message after Observer started"); + break; + case Leader.REVALIDATE: + revalidate(qp); + break; + case Leader.SYNC: + ((ObserverZooKeeperServer) zk).sync(); + break; + case Leader.INFORM: + ServerMetrics.getMetrics().LEARNER_COMMIT_RECEIVED_COUNT.add(1); + logEntry = SerializeUtils.deserializeTxn(qp.getData()); + hdr = logEntry.getHeader(); + txn = logEntry.getTxn(); + digest = logEntry.getDigest(); + Request request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, 0); + request.logLatency(ServerMetrics.getMetrics().COMMIT_PROPAGATION_LATENCY); + request.setTxnDigest(digest); + ObserverZooKeeperServer obs = (ObserverZooKeeperServer) zk; + obs.commitRequest(request); + break; + case Leader.INFORMANDACTIVATE: + // get new designated leader from (current) leader's message + ByteBuffer buffer = ByteBuffer.wrap(qp.getData()); + long suggestedLeaderId = buffer.getLong(); + + byte[] remainingdata = new byte[buffer.remaining()]; + buffer.get(remainingdata); + logEntry = SerializeUtils.deserializeTxn(remainingdata); + hdr = logEntry.getHeader(); + txn = logEntry.getTxn(); + digest = logEntry.getDigest(); + QuorumVerifier qv = self.configFromString(new String(((SetDataTxn) txn).getData())); + + request = new Request(hdr.getClientId(), hdr.getCxid(), hdr.getType(), hdr, txn, 0); + request.setTxnDigest(digest); + obs = (ObserverZooKeeperServer) zk; + + boolean majorChange = self.processReconfig(qv, suggestedLeaderId, qp.getZxid(), true); + + obs.commitRequest(request); + + if (majorChange) { + throw new Exception("changes proposed in reconfig"); + } + break; + default: + LOG.warn("Unknown packet type: {}", LearnerHandler.packetToString(qp)); + break; + } + } + + /** + * Shutdown the Observer. + */ + public void shutdown() { + LOG.info("shutdown Observer"); + super.shutdown(); + } + + static void waitForReconnectDelay() { + waitForReconnectDelayHelper(reconnectDelayMs); + } + + static void waitForObserverElectionDelay() { + waitForReconnectDelayHelper(observerElectionDelayMs); + } + + private static void waitForReconnectDelayHelper(long delayValueMs) { + if (delayValueMs > 0) { + long randomDelay = ThreadLocalRandom.current().nextLong(delayValueMs); + LOG.info("Waiting for {} ms before reconnecting with the leader", randomDelay); + try { + Thread.sleep(randomDelay); + } catch (InterruptedException e) { + LOG.warn("Interrupted while waiting", e); + } + } + } + + public long getLearnerMasterId() { + QuorumPeer.QuorumServer current = currentLearnerMaster; + return current == null ? -1 : current.id; + } + + /** + * Prompts the Observer to disconnect from its current learner master and reconnect + * to the specified server. If that connection attempt fails, the Observer will + * fail over to the next available learner master. + */ + public boolean setLearnerMaster(String learnerMaster) { + final QuorumPeer.QuorumServer server = self.validateLearnerMaster(learnerMaster); + if (server == null) { + return false; + } else if (server.equals(currentLearnerMaster)) { + LOG.info("Already connected to requested learner master sid={} addr={}", server.id, server.addr); + return true; + } else { + LOG.info("Requesting disconnect and reconnect to new learner master sid={} addr={}", server.id, server.addr); + nextLearnerMaster.set(server); + return true; + } + } + + public QuorumPeer.QuorumServer getCurrentLearnerMaster() { + return currentLearnerMaster; + } + + public static long getObserverElectionDelayMs() { + return observerElectionDelayMs; + } + + public static void setObserverElectionDelayMs(long electionDelayMs) { + observerElectionDelayMs = electionDelayMs; + LOG.info("{} = {}", OBSERVER_ELECTION_DELAY_MS, observerElectionDelayMs); + } + +} + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverMXBean.java new file mode 100644 index 0000000..e441fd2 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverMXBean.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.ZooKeeperServerMXBean; + +/** + * Observer MX Bean interface, implemented by ObserverBean + * + */ +public interface ObserverMXBean extends ZooKeeperServerMXBean { + + /** + * @return count of pending revalidations + */ + int getPendingRevalidationCount(); + + /** + * @return socket address + */ + String getQuorumAddress(); + + /** + * @return address of the current learner master + */ + String getLearnerMaster(); + + /** + * requests the Observer switch to a new learner master + * + * @param learnerMaster address of the desired learner master + */ + void setLearnerMaster(String learnerMaster); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverMaster.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverMaster.java new file mode 100644 index 0000000..54a22c2 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverMaster.java @@ -0,0 +1,542 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Used by Followers to host Observers. This reduces the network load on the Leader process by pushing + * the responsibility for keeping Observers in sync off the leading peer. + * + * It is expected that Observers will continue to perform the initial vetting of clients and requests. + * Observers send the request to the follower where it is received by an ObserverMaster. + * + * The ObserverMaster forwards a copy of the request to the ensemble Leader and inserts it into its own + * request processor pipeline where it can be matched with the response comes back. All commits received + * from the Leader will be forwarded along to every Learner connected to the ObserverMaster. + * + * New Learners connecting to a Follower will receive a LearnerHandler object and be party to its syncing logic + * to be brought up to date. + * + * The logic is quite a bit simpler than the corresponding logic in Leader because it only hosts observers. + */ +public class ObserverMaster extends LearnerMaster implements Runnable { + + private static final Logger LOG = LoggerFactory.getLogger(ObserverMaster.class); + + //Follower counter + private final AtomicLong followerCounter = new AtomicLong(-1); + + private QuorumPeer self; + private FollowerZooKeeperServer zks; + private int port; + + private Set activeObservers = Collections.newSetFromMap(new ConcurrentHashMap()); + + private final ConcurrentHashMap connectionBeans = new ConcurrentHashMap<>(); + + /** + * we want to keep a log of past txns so that observers can sync up with us when we connect, + * but we can't keep everything in memory, so this limits how much memory will be dedicated + * to keeping recent txns. + */ + private static final int PKTS_SIZE_LIMIT = 32 * 1024 * 1024; + private static volatile int pktsSizeLimit = Integer.getInteger("zookeeper.observerMaster.sizeLimit", PKTS_SIZE_LIMIT); + private ConcurrentLinkedQueue proposedPkts = new ConcurrentLinkedQueue<>(); + private ConcurrentLinkedQueue committedPkts = new ConcurrentLinkedQueue<>(); + private int pktsSize = 0; + + private long lastProposedZxid; + + // ensure ordering of revalidations returned to this learner + private final Object revalidateSessionLock = new Object(); + + private final ConcurrentLinkedQueue pendingRevalidations = new ConcurrentLinkedQueue<>(); + + static class Revalidation { + + public final long sessionId; + public final int timeout; + public final LearnerHandler handler; + + Revalidation(final Long sessionId, final int timeout, final LearnerHandler handler) { + this.sessionId = sessionId; + this.timeout = timeout; + this.handler = handler; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + final Revalidation that = (Revalidation) o; + return sessionId == that.sessionId && timeout == that.timeout && handler.equals(that.handler); + } + + @Override + public int hashCode() { + int result = (int) (sessionId ^ (sessionId >>> 32)); + result = 31 * result + timeout; + result = 31 * result + handler.hashCode(); + return result; + } + + } + + private Thread thread; + private ServerSocket ss; + private boolean listenerRunning; + private ScheduledExecutorService pinger; + + Runnable ping = new Runnable() { + @Override + public void run() { + for (LearnerHandler lh : activeObservers) { + lh.ping(); + } + } + }; + + ObserverMaster(QuorumPeer self, FollowerZooKeeperServer zks, int port) { + this.self = self; + this.zks = zks; + this.port = port; + } + + @Override + public void addLearnerHandler(LearnerHandler learnerHandler) { + if (!listenerRunning) { + throw new RuntimeException(("ObserverMaster is not running")); + } + } + + @Override + public void removeLearnerHandler(LearnerHandler learnerHandler) { + activeObservers.remove(learnerHandler); + } + + @Override + public int syncTimeout() { + return self.getSyncLimit() * self.getTickTime(); + } + + @Override + public int getTickOfNextAckDeadline() { + return self.tick.get() + self.syncLimit; + } + + @Override + public int getTickOfInitialAckDeadline() { + return self.tick.get() + self.initLimit + self.syncLimit; + } + + @Override + public long getAndDecrementFollowerCounter() { + return followerCounter.getAndDecrement(); + } + + @Override + public void waitForEpochAck(long sid, StateSummary ss) throws IOException, InterruptedException { + // since this is done by an active follower, we don't need to wait for anything + } + + @Override + public void waitForStartup() throws InterruptedException { + // since this is done by an active follower, we don't need to wait for anything + } + + @Override + public synchronized long getLastProposed() { + return lastProposedZxid; + } + + @Override + public long getEpochToPropose(long sid, long lastAcceptedEpoch) throws InterruptedException, IOException { + return self.getCurrentEpoch(); + } + + @Override + public ZKDatabase getZKDatabase() { + return zks.getZKDatabase(); + } + + @Override + public void waitForNewLeaderAck(long sid, long zxid) throws InterruptedException { + // no need to wait since we are a follower + } + + @Override + public int getCurrentTick() { + return self.tick.get(); + } + + @Override + public void processAck(long sid, long zxid, SocketAddress localSocketAddress) { + if ((zxid & 0xffffffffL) == 0) { + /* + * We no longer process NEWLEADER ack by this method. However, + * the learner sends ack back to the leader after it gets UPTODATE + * so we just ignore the message. + */ + return; + } + + throw new RuntimeException("Observers shouldn't send ACKS ack = " + Long.toHexString(zxid)); + } + + @Override + public void touch(long sess, int to) { + zks.getSessionTracker().touchSession(sess, to); + } + + boolean revalidateLearnerSession(QuorumPacket qp) throws IOException { + ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData()); + DataInputStream dis = new DataInputStream(bis); + long id = dis.readLong(); + boolean valid = dis.readBoolean(); + Iterator itr = pendingRevalidations.iterator(); + if (!itr.hasNext()) { + // not a learner session, handle locally + return false; + } + Revalidation revalidation = itr.next(); + if (revalidation.sessionId != id) { + // not a learner session, handle locally + return false; + } + itr.remove(); + LearnerHandler learnerHandler = revalidation.handler; + // create a copy here as the qp object is reused by the Follower and may be mutated + QuorumPacket deepCopy = new QuorumPacket( + qp.getType(), + qp.getZxid(), + Arrays.copyOf(qp.getData(), qp.getData().length), + qp.getAuthinfo() == null ? null : new ArrayList<>(qp.getAuthinfo())); + learnerHandler.queuePacket(deepCopy); + // To keep consistent as leader, touch the session when it's + // revalidating the session, only update if it's a valid session. + if (valid) { + touch(revalidation.sessionId, revalidation.timeout); + } + return true; + } + + @Override + public void revalidateSession(QuorumPacket qp, LearnerHandler learnerHandler) throws IOException { + ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData()); + DataInputStream dis = new DataInputStream(bis); + long id = dis.readLong(); + int to = dis.readInt(); + synchronized (revalidateSessionLock) { + pendingRevalidations.add(new Revalidation(id, to, learnerHandler)); + Learner learner = zks.getLearner(); + if (learner != null) { + learner.writePacket(qp, true); + } + } + } + + @Override + public void submitLearnerRequest(Request si) { + zks.processObserverRequest(si); + } + + @Override + public synchronized long startForwarding(LearnerHandler learnerHandler, long lastSeenZxid) { + Iterator itr = committedPkts.iterator(); + if (itr.hasNext()) { + QuorumPacket packet = itr.next(); + if (packet.getZxid() > lastSeenZxid + 1) { + LOG.error( + "LearnerHandler is too far behind (0x{} < 0x{}), disconnecting {} at {}", + Long.toHexString(lastSeenZxid + 1), + Long.toHexString(packet.getZxid()), + learnerHandler.getSid(), + learnerHandler.getRemoteAddress()); + learnerHandler.shutdown(); + return -1; + } else if (packet.getZxid() == lastSeenZxid + 1) { + learnerHandler.queuePacket(packet); + } + long queueHeadZxid = packet.getZxid(); + long queueBytesUsed = LearnerHandler.packetSize(packet); + while (itr.hasNext()) { + packet = itr.next(); + if (packet.getZxid() <= lastSeenZxid) { + continue; + } + learnerHandler.queuePacket(packet); + queueBytesUsed += LearnerHandler.packetSize(packet); + } + LOG.info( + "finished syncing observer from retained commit queue: sid {}, " + + "queue head 0x{}, queue tail 0x{}, sync position 0x{}, num packets used {}, " + + "num bytes used {}", + learnerHandler.getSid(), + Long.toHexString(queueHeadZxid), + Long.toHexString(packet.getZxid()), + Long.toHexString(lastSeenZxid), + packet.getZxid() - lastSeenZxid, + queueBytesUsed); + } + activeObservers.add(learnerHandler); + return lastProposedZxid; + } + + @Override + public long getQuorumVerifierVersion() { + return self.getQuorumVerifier().getVersion(); + } + + @Override + public String getPeerInfo(long sid) { + QuorumPeer.QuorumServer server = self.getView().get(sid); + return server == null ? "" : server.toString(); + } + + @Override + public byte[] getQuorumVerifierBytes() { + return self.getLastSeenQuorumVerifier().toString().getBytes(); + } + + @Override + public QuorumAuthServer getQuorumAuthServer() { + return (self == null) ? null : self.authServer; + } + + void proposalReceived(QuorumPacket qp) { + proposedPkts.add(new QuorumPacket(Leader.INFORM, qp.getZxid(), qp.getData(), null)); + } + + private synchronized QuorumPacket removeProposedPacket(long zxid) { + QuorumPacket pkt = proposedPkts.peek(); + if (pkt == null || pkt.getZxid() > zxid) { + LOG.debug("ignore missing proposal packet for {}", Long.toHexString(zxid)); + return null; + } + if (pkt.getZxid() != zxid) { + final String m = String.format( + "Unexpected proposal packet on commit ack, expected zxid 0x%d got zxid 0x%d", + zxid, + pkt.getZxid()); + LOG.error(m); + throw new RuntimeException(m); + } + proposedPkts.remove(); + return pkt; + } + + private synchronized void cacheCommittedPacket(final QuorumPacket pkt) { + committedPkts.add(pkt); + pktsSize += LearnerHandler.packetSize(pkt); + // remove 5 packets for every one added as we near the size limit + for (int i = 0; pktsSize > pktsSizeLimit * 0.8 && i < 5; i++) { + QuorumPacket oldPkt = committedPkts.poll(); + if (oldPkt == null) { + pktsSize = 0; + break; + } + pktsSize -= LearnerHandler.packetSize(oldPkt); + } + // enforce the size limit as a hard cap + while (pktsSize > pktsSizeLimit) { + QuorumPacket oldPkt = committedPkts.poll(); + if (oldPkt == null) { + pktsSize = 0; + break; + } + pktsSize -= LearnerHandler.packetSize(oldPkt); + } + } + + private synchronized void sendPacket(final QuorumPacket pkt) { + for (LearnerHandler lh : activeObservers) { + lh.queuePacket(pkt); + } + lastProposedZxid = pkt.getZxid(); + } + + synchronized void proposalCommitted(long zxid) { + QuorumPacket pkt = removeProposedPacket(zxid); + if (pkt == null) { + return; + } + cacheCommittedPacket(pkt); + sendPacket(pkt); + } + + synchronized void informAndActivate(long zxid, long suggestedLeaderId) { + QuorumPacket pkt = removeProposedPacket(zxid); + if (pkt == null) { + return; + } + + // Build the INFORMANDACTIVATE packet + QuorumPacket informAndActivateQP = Leader.buildInformAndActivePacket(zxid, suggestedLeaderId, pkt.getData()); + cacheCommittedPacket(informAndActivateQP); + sendPacket(informAndActivateQP); + } + + public synchronized void start() throws IOException { + if (thread != null && thread.isAlive()) { + return; + } + listenerRunning = true; + int backlog = 10; // dog science + InetAddress address = self.getQuorumAddress().getReachableOrOne().getAddress(); + if (self.shouldUsePortUnification() || self.isSslQuorum()) { + boolean allowInsecureConnection = self.shouldUsePortUnification(); + if (self.getQuorumListenOnAllIPs()) { + ss = new UnifiedServerSocket(self.getX509Util(), allowInsecureConnection, port, backlog); + } else { + ss = new UnifiedServerSocket(self.getX509Util(), allowInsecureConnection, port, backlog, address); + } + } else { + if (self.getQuorumListenOnAllIPs()) { + ss = new ServerSocket(port, backlog); + } else { + ss = new ServerSocket(port, backlog, address); + } + } + thread = new Thread(this, "ObserverMaster"); + thread.start(); + pinger = Executors.newSingleThreadScheduledExecutor(); + pinger.scheduleAtFixedRate(ping, self.tickTime / 2, self.tickTime / 2, TimeUnit.MILLISECONDS); + } + + public void run() { + ServerSocket ss; + synchronized (this) { + ss = this.ss; + } + while (listenerRunning) { + try { + Socket s = ss.accept(); + + // start with the initLimit, once the ack is processed + // in LearnerHandler switch to the syncLimit + s.setSoTimeout(self.tickTime * self.initLimit); + BufferedInputStream is = new BufferedInputStream(s.getInputStream()); + LearnerHandler lh = new LearnerHandler(s, is, this); + lh.start(); + } catch (Exception e) { + if (listenerRunning) { + LOG.debug("Ignoring accept exception (maybe shutting down)", e); + } else { + LOG.debug("Ignoring accept exception (maybe client closed)", e); + } + } + } + /* + * we don't need to close ss because we only got here because listenerRunning is + * false and that is set and then ss is closed() in stop() + */ + } + + public synchronized void stop() { + listenerRunning = false; + if (pinger != null) { + pinger.shutdownNow(); + } + if (ss != null) { + try { + ss.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + for (LearnerHandler lh : activeObservers) { + lh.shutdown(); + } + } + + int getNumActiveObservers() { + return activeObservers.size(); + } + + public Iterable> getActiveObservers() { + Set> info = new HashSet<>(); + for (LearnerHandler lh : activeObservers) { + info.add(lh.getLearnerHandlerInfo()); + } + return info; + } + + public void resetObserverConnectionStats() { + for (LearnerHandler lh : activeObservers) { + lh.resetObserverConnectionStats(); + } + } + + int getPktsSizeLimit() { + return pktsSizeLimit; + } + + static void setPktsSizeLimit(final int sizeLimit) { + pktsSizeLimit = sizeLimit; + } + + @Override + public void registerLearnerHandlerBean(final LearnerHandler learnerHandler, Socket socket) { + LearnerHandlerBean bean = new LearnerHandlerBean(learnerHandler, socket); + if (zks.registerJMX(bean)) { + connectionBeans.put(learnerHandler, bean); + } + } + + @Override + public void unregisterLearnerHandlerBean(final LearnerHandler learnerHandler) { + LearnerHandlerBean bean = connectionBeans.remove(learnerHandler); + if (bean != null) { + MBeanRegistry.getInstance().unregister(bean); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverRequestProcessor.java new file mode 100644 index 0000000..4666203 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverRequestProcessor.java @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.txn.ErrorTxn; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This RequestProcessor forwards any requests that modify the state of the + * system to the Leader. + */ +public class ObserverRequestProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(ObserverRequestProcessor.class); + + ObserverZooKeeperServer zks; + + RequestProcessor nextProcessor; + + // We keep a queue of requests. As requests get submitted they are + // stored here. The queue is drained in the run() method. + LinkedBlockingQueue queuedRequests = new LinkedBlockingQueue(); + + boolean finished = false; + + /** + * Constructor - takes an ObserverZooKeeperServer to associate with + * and the next processor to pass requests to after we're finished. + * @param zks + * @param nextProcessor + */ + public ObserverRequestProcessor(ObserverZooKeeperServer zks, RequestProcessor nextProcessor) { + super("ObserverRequestProcessor:" + zks.getServerId(), zks.getZooKeeperServerListener()); + this.zks = zks; + this.nextProcessor = nextProcessor; + } + + @Override + public void run() { + try { + while (!finished) { + Request request = queuedRequests.take(); + if (LOG.isTraceEnabled()) { + ZooTrace.logRequest(LOG, ZooTrace.CLIENT_REQUEST_TRACE_MASK, 'F', request, ""); + } + if (request == Request.requestOfDeath) { + break; + } + + // Screen quorum requests against ACLs first + if (!zks.authWriteRequest(request)) { + continue; + } + + // We want to queue the request to be processed before we submit + // the request to the leader so that we are ready to receive + // the response + nextProcessor.processRequest(request); + + // We now ship the request to the leader. As with all + // other quorum operations, sync also follows this code + // path, but different from others, we need to keep track + // of the sync operations this Observer has pending, so we + // add it to pendingSyncs. + switch (request.type) { + case OpCode.sync: + zks.pendingSyncs.add(request); + zks.getObserver().request(request); + break; + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + case OpCode.delete: + case OpCode.deleteContainer: + case OpCode.setData: + case OpCode.reconfig: + case OpCode.setACL: + case OpCode.multi: + case OpCode.check: + zks.getObserver().request(request); + break; + case OpCode.createSession: + case OpCode.closeSession: + // Don't forward local sessions to the leader. + if (!request.isLocalSession()) { + zks.getObserver().request(request); + } + break; + } + } + } catch (Exception e) { + handleException(this.getName(), e); + } + LOG.info("ObserverRequestProcessor exited loop!"); + } + + /** + * Simply queue the request, which will be processed in FIFO order. + */ + public void processRequest(Request request) { + if (!finished) { + Request upgradeRequest = null; + try { + upgradeRequest = zks.checkUpgradeSession(request); + } catch (KeeperException ke) { + if (request.getHdr() != null) { + request.getHdr().setType(OpCode.error); + request.setTxn(new ErrorTxn(ke.code().intValue())); + } + request.setException(ke); + LOG.info("Error creating upgrade request", ke); + } catch (IOException ie) { + LOG.error("Unexpected error in upgrade", ie); + } + if (upgradeRequest != null) { + queuedRequests.add(upgradeRequest); + } + queuedRequests.add(request); + } + } + + /** + * Shutdown the processor. + */ + public void shutdown() { + LOG.info("Shutting down"); + finished = true; + queuedRequests.clear(); + queuedRequests.add(Request.requestOfDeath); + nextProcessor.shutdown(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverZooKeeperServer.java new file mode 100644 index 0000000..a41a918 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ObserverZooKeeperServer.java @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.function.BiConsumer; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A ZooKeeperServer for the Observer node type. Not much is different, but + * we anticipate specializing the request processors in the future. + * + */ +public class ObserverZooKeeperServer extends LearnerZooKeeperServer { + + private static final Logger LOG = LoggerFactory.getLogger(ObserverZooKeeperServer.class); + + /** + * Enable since request processor for writing txnlog to disk and + * take periodic snapshot. Default is ON. + */ + + private boolean syncRequestProcessorEnabled = this.self.getSyncEnabled(); + + /* + * Pending sync requests + */ ConcurrentLinkedQueue pendingSyncs = new ConcurrentLinkedQueue(); + + ObserverZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, ZKDatabase zkDb) throws IOException { + super(logFactory, self.tickTime, self.minSessionTimeout, self.maxSessionTimeout, self.clientPortListenBacklog, zkDb, self); + LOG.info("syncEnabled ={}", syncRequestProcessorEnabled); + } + + public Observer getObserver() { + return self.observer; + } + + @Override + public Learner getLearner() { + return self.observer; + } + + /** + * Unlike a Follower, which sees a full request only during the PROPOSAL + * phase, Observers get all the data required with the INFORM packet. + * This method commits a request that has been unpacked by from an INFORM + * received from the Leader. + * + * @param request + */ + public void commitRequest(Request request) { + if (syncRequestProcessorEnabled) { + // Write to txnlog and take periodic snapshot + syncProcessor.processRequest(request); + } + commitProcessor.commit(request); + } + + /** + * Set up the request processors for an Observer: + * firstProcesor->commitProcessor->finalProcessor + */ + @Override + protected void setupRequestProcessors() { + // We might consider changing the processor behaviour of + // Observers to, for example, remove the disk sync requirements. + // Currently, they behave almost exactly the same as followers. + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + commitProcessor = new CommitProcessor(finalProcessor, Long.toString(getServerId()), true, getZooKeeperServerListener()); + commitProcessor.start(); + firstProcessor = new ObserverRequestProcessor(this, commitProcessor); + ((ObserverRequestProcessor) firstProcessor).start(); + + /* + * Observer should write to disk, so that the it won't request + * too old txn from the leader which may lead to getting an entire + * snapshot. + * + * However, this may degrade performance as it has to write to disk + * and do periodic snapshot which may double the memory requirements + */ + if (syncRequestProcessorEnabled) { + syncProcessor = new SyncRequestProcessor(this, null); + syncProcessor.start(); + } + } + + /* + * Process a sync request + */ + public synchronized void sync() { + if (pendingSyncs.size() == 0) { + LOG.warn("Not expecting a sync."); + return; + } + + Request r = pendingSyncs.remove(); + commitProcessor.commit(r); + } + + @Override + public String getState() { + return "observer"; + } + + @Override + public synchronized void shutdown() { + if (!canShutdown()) { + LOG.debug("ZooKeeper server is not running, so not proceeding to shutdown!"); + return; + } + super.shutdown(); + if (syncRequestProcessorEnabled && syncProcessor != null) { + syncProcessor.shutdown(); + } + } + + @Override + public void dumpMonitorValues(BiConsumer response) { + super.dumpMonitorValues(response); + response.accept("observer_master_id", getObserver().getLearnerMasterId()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/PrependableSocket.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/PrependableSocket.java new file mode 100644 index 0000000..40eef83 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/PrependableSocket.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; +import java.net.Socket; +import java.net.SocketImpl; + +public class PrependableSocket extends Socket { + + private PushbackInputStream pushbackInputStream; + + public PrependableSocket(SocketImpl base) throws IOException { + super(base); + } + + @Override + public InputStream getInputStream() throws IOException { + if (pushbackInputStream == null) { + return super.getInputStream(); + } + + return pushbackInputStream; + } + + /** + * Prepend some bytes that have already been read back to the socket's input stream. Note that this method can be + * called at most once with a non-0 length per socket instance. + * @param bytes the bytes to prepend. + * @param offset offset in the byte array to start at. + * @param length number of bytes to prepend. + * @throws IOException if this method was already called on the socket instance, or if super.getInputStream() throws. + */ + public void prependToInputStream(byte[] bytes, int offset, int length) throws IOException { + if (length == 0) { + return; // nothing to prepend + } + if (pushbackInputStream != null) { + throw new IOException("prependToInputStream() called more than once"); + } + PushbackInputStream pushbackInputStream = new PushbackInputStream(getInputStream(), length); + pushbackInputStream.unread(bytes, offset, length); + this.pushbackInputStream = pushbackInputStream; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java new file mode 100644 index 0000000..a8a2f75 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ProposalRequestProcessor.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.quorum.Leader.XidRolloverException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This RequestProcessor simply forwards requests to an AckRequestProcessor and + * SyncRequestProcessor. + */ +public class ProposalRequestProcessor implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(ProposalRequestProcessor.class); + + LeaderZooKeeperServer zks; + + RequestProcessor nextProcessor; + + SyncRequestProcessor syncProcessor; + + public ProposalRequestProcessor(LeaderZooKeeperServer zks, RequestProcessor nextProcessor) { + this.zks = zks; + this.nextProcessor = nextProcessor; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + AckRequestProcessor ackProcessor = new AckRequestProcessor(zks.getLeader()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + syncProcessor = new SyncRequestProcessor(zks, ackProcessor); + } + + /** + * initialize this processor + */ + public void initialize() { + syncProcessor.start(); + } + + public void processRequest(Request request) throws RequestProcessorException { + // LOG.warn("Ack>>> cxid = " + request.cxid + " type = " + + // request.type + " id = " + request.sessionId); + // request.addRQRec(">prop"); + + // TODO_MA 注释: 在下面的 IF-THEN-ELSE 块中,我们在 leader 上处理同步。 + // TODO_MA 注释: 如果同步来自 follower,则 follower handler 将其添加到 syncHandler。 + /* In the following IF-THEN-ELSE block, we process syncs on the leader. + * If the sync is coming from a follower, then the follower handler adds it to syncHandler. + * + * // TODO_MA 注释: 否则,如果是领导者的客户端发出同步命令,那么 syncHandler 将不包含处理程序。 + * // TODO_MA 注释: 在这种情况下,我们将其添加到 syncHandler,并在下一个处理器上调用 processRequest。 + * Otherwise, if it is a client of the leader that issued the sync command, then syncHandler won't + * contain the handler. In this case, we add it to syncHandler, and + * call processRequest on the next processor. + */ + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果是 Follower 的同步请求 + */ + if (request instanceof LearnerSyncRequest) { + zks.getLeader().processSync((LearnerSyncRequest) request); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 两个分支: + * 1、CommitProcessor + * 2、syncProcessor + ackProcessor + */ + else { + + // TODO_MA 注释: 第一个分支 nextProcessor = CommitProcessor + nextProcessor.processRequest(request); + + // TODO_MA 注释: 第二个分支 syncProcessor + ackProcessor + if (request.getHdr() != null) { + // We need to sync and get consensus on any transactions + try { + + // TODO_MA 注释: 2PC + // TODO_MA 注释: 第一阶段: 发送命令让 事务参与者执行事务,但是不提交 + // TODO_MA 注释: 第二阶段: 执行提交或者回滚 + + // TODO_MA 注释: 广播事务 + zks.getLeader().propose(request); + } catch (XidRolloverException e) { + throw new RequestProcessorException(e.getMessage(), e); + } + + // TODO_MA 注释: Leader 记录事务操作的日志 + syncProcessor.processRequest(request); + } + } + } + + public void shutdown() { + LOG.info("Shutting down"); + nextProcessor.shutdown(); + syncProcessor.shutdown(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumBean.java new file mode 100644 index 0000000..aae4552 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumBean.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.ZooKeeperServer; + +public class QuorumBean implements QuorumMXBean, ZKMBeanInfo { + + private final QuorumPeer peer; + private final String name; + + public QuorumBean(QuorumPeer peer) { + this.peer = peer; + name = "ReplicatedServer_id" + peer.getId(); + } + + @Override + public String getName() { + return name; + } + + @Override + public boolean isHidden() { + return false; + } + + @Override + public int getQuorumSize() { + return peer.getQuorumSize(); + } + + public int getSyncLimit() { + return peer.getSyncLimit(); + } + + public int getInitLimit() { + return peer.getInitLimit(); + } + + public void setInitLimit(int initLimit) { + peer.setInitLimit(initLimit); + } + + public void setSyncLimit(int syncLimit) { + peer.setSyncLimit(syncLimit); + } + + @Override + public boolean isSslQuorum() { + return peer.isSslQuorum(); + } + + @Override + public boolean isPortUnification() { + return peer.shouldUsePortUnification(); + } + + @Override + public long getObserverElectionDelayMS() { + return Observer.getObserverElectionDelayMs(); + } + + @Override + public void setObserverElectionDelayMS(long delayMS) { + Observer.setObserverElectionDelayMs(delayMS); + } + + @Override + public boolean getDigestEnabled() { + return ZooKeeperServer.isDigestEnabled(); + } + + @Override + public void disableDigest() { + ZooKeeperServer.setDigestEnabled(false); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumCnxManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumCnxManager.java new file mode 100644 index 0000000..b912310 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumCnxManager.java @@ -0,0 +1,1711 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.common.NetUtils.formatInetAddr; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.Closeable; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.nio.channels.UnresolvedAddressException; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.SynchronousQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import javax.net.ssl.SSLSocket; +import javax.swing.table.TableModel; + +import org.apache.zookeeper.common.NetUtils; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthLearner; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.ConfigUtils; +import org.apache.zookeeper.util.CircularBlockingQueue; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class implements a connection manager for leader election using TCP. It + * maintains one connection for every pair of servers. The tricky part is to + * guarantee that there is exactly one connection for every pair of servers that + * are operating correctly and that can communicate over the network. + * + * If two servers try to start a connection concurrently, then the connection + * manager uses a very simple tie-breaking mechanism to decide which connection + * to drop based on the IP addressed of the two parties. + * + * For every peer, the manager maintains a queue of messages to send. If the + * connection to any particular peer drops, then the sender thread puts the + * message back on the list. As this implementation currently uses a queue + * implementation to maintain messages to send to another peer, we add the + * message to the tail of the queue, thus changing the order of messages. + * Although this is not a problem for the leader election, it could be a problem + * when consolidating peer communication. This is to be verified, though. + */ + +public class QuorumCnxManager { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumCnxManager.class); + + /* + * Maximum capacity of thread queues + */ + static final int RECV_CAPACITY = 100; + // Initialized to 1 to prevent sending + // stale notifications to peers + static final int SEND_CAPACITY = 1; + + // TODO_MA 注释: 数据包最大长度 + static final int PACKETMAXSIZE = 1024 * 512; + + /* + * Negative counter for observer server ids. + */ + + private AtomicLong observerCounter = new AtomicLong(-1); + + /* + * Protocol identifier used among peers (must be a negative number for backward compatibility reasons) + */ + // the following protocol version was sent in every connection initiation message since ZOOKEEPER-107 released in 3.5.0 + public static final long PROTOCOL_VERSION_V1 = -65536L; + // ZOOKEEPER-3188 introduced multiple addresses in the connection initiation message, released in 3.6.0 + public static final long PROTOCOL_VERSION_V2 = -65535L; + + /* + * Max buffer size to be read from the network. + */ + public static final int maxBuffer = 2048; + + /* + * Connection time out value in milliseconds + */ + + private int cnxTO = 5000; + + final QuorumPeer self; + + /* + * Local IP address + */ + final long mySid; + final int socketTimeout; + final Map view; + final boolean listenOnAllIPs; + private ThreadPoolExecutor connectionExecutor; + private final Set inprogressConnections = Collections.synchronizedSet(new HashSet<>()); + private QuorumAuthServer authServer; + private QuorumAuthLearner authLearner; + private boolean quorumSaslAuthEnabled; + /* + * Counter to count connection processing threads. + */ + private AtomicInteger connectionThreadCnt = new AtomicInteger(0); + + /* + * Mapping from Peer to Thread number + */ + final ConcurrentHashMap senderWorkerMap; + final ConcurrentHashMap> queueSendMap; + final ConcurrentHashMap lastMessageSent; + + /* + * Reception queue + */ + public final BlockingQueue recvQueue; + + /* + * Shutdown flag + */ + + volatile boolean shutdown = false; + + /* + * Listener thread + */ + public final Listener listener; + + /* + * Counter to count worker threads + */ + private AtomicInteger threadCnt = new AtomicInteger(0); + + /* + * Socket options for TCP keepalive + */ + private final boolean tcpKeepAlive = Boolean.getBoolean("zookeeper.tcpKeepAlive"); + + + /* + * Socket factory, allowing the injection of custom socket implementations for testing + */ + static final Supplier DEFAULT_SOCKET_FACTORY = () -> new Socket(); + private static Supplier SOCKET_FACTORY = DEFAULT_SOCKET_FACTORY; + + static void setSocketFactory(Supplier factory) { + SOCKET_FACTORY = factory; + } + + + public static class Message { + + Message(ByteBuffer buffer, long sid) { + this.buffer = buffer; + this.sid = sid; + } + + ByteBuffer buffer; + long sid; + + } + + /* + * This class parses the initial identification sent out by peers with their + * sid & hostname. + */ + public static class InitialMessage { + + public Long sid; + public List electionAddr; + + InitialMessage(Long sid, List addresses) { + this.sid = sid; + this.electionAddr = addresses; + } + + @SuppressWarnings("serial") + public static class InitialMessageException extends Exception { + + InitialMessageException(String message, Object... args) { + super(String.format(message, args)); + } + + } + + public static InitialMessage parse(Long protocolVersion, + DataInputStream din) throws InitialMessageException, IOException { + Long sid; + + if (protocolVersion != PROTOCOL_VERSION_V1 && protocolVersion != PROTOCOL_VERSION_V2) { + throw new InitialMessageException("Got unrecognized protocol version %s", protocolVersion); + } + + // TODO_MA 注释: 读取 myid + sid = din.readLong(); + + // TODO_MA 注释: 读取 address length + int remaining = din.readInt(); + if (remaining <= 0 || remaining > maxBuffer) { + throw new InitialMessageException("Unreasonable buffer length: %s", remaining); + } + + + // TODO_MA 注释: 读取 address + byte[] b = new byte[remaining]; + int num_read = din.read(b); + + if (num_read != remaining) { + throw new InitialMessageException("Read only %s bytes out of %s sent by server %s", num_read, remaining, + sid); + } + + // in PROTOCOL_VERSION_V1 we expect to get a single address here represented as a 'host:port' string + // in PROTOCOL_VERSION_V2 we expect to get multiple addresses like: 'host1:port1|host2:port2|...' + String[] addressStrings = new String(b).split("\\|"); + List addresses = new ArrayList<>(addressStrings.length); + for (String addr : addressStrings) { + + String[] host_port; + try { + host_port = ConfigUtils.getHostAndPort(addr); + } catch (ConfigException e) { + throw new InitialMessageException("Badly formed address: %s", addr); + } + + if (host_port.length != 2) { + throw new InitialMessageException("Badly formed address: %s", addr); + } + + int port; + try { + port = Integer.parseInt(host_port[1]); + } catch (NumberFormatException e) { + throw new InitialMessageException("Bad port number: %s", host_port[1]); + } catch (ArrayIndexOutOfBoundsException e) { + throw new InitialMessageException("No port number in: %s", addr); + } + if (!isWildcardAddress(host_port[0])) { + addresses.add(new InetSocketAddress(host_port[0], port)); + } + } + + return new InitialMessage(sid, addresses); + } + + /** + * Returns true if the specified hostname is a wildcard address, + * like 0.0.0.0 for IPv4 or :: for IPv6 + * + * (the function is package-private to be visible for testing) + */ + static boolean isWildcardAddress(final String hostname) { + try { + return InetAddress.getByName(hostname).isAnyLocalAddress(); + } catch (UnknownHostException e) { + // if we can not resolve, it can not be a wildcard address + return false; + } + } + + @Override + public String toString() { + return "InitialMessage{sid=" + sid + ", electionAddr=" + electionAddr + '}'; + } + } + + public QuorumCnxManager(QuorumPeer self, final long mySid, Map view, + QuorumAuthServer authServer, QuorumAuthLearner authLearner, int socketTimeout, boolean listenOnAllIPs, + int quorumCnxnThreadsSize, boolean quorumSaslAuthEnabled) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.recvQueue = new CircularBlockingQueue<>(RECV_CAPACITY); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.queueSendMap = new ConcurrentHashMap<>(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.senderWorkerMap = new ConcurrentHashMap<>(); + this.lastMessageSent = new ConcurrentHashMap<>(); + + String cnxToValue = System.getProperty("zookeeper.cnxTimeout"); + if (cnxToValue != null) { + this.cnxTO = Integer.parseInt(cnxToValue); + } + this.self = self; + this.mySid = mySid; + this.socketTimeout = socketTimeout; + this.view = view; + this.listenOnAllIPs = listenOnAllIPs; + this.authServer = authServer; + this.authLearner = authLearner; + this.quorumSaslAuthEnabled = quorumSaslAuthEnabled; + + // TODO_MA 注释: 启动 3 个线程用来完成选票交换的事儿,相当于 BIO 客户端 + initializeConnectionExecutor(mySid, quorumCnxnThreadsSize); + // TODO_MA 注释: 原来的实现:对方节点有多少个,则有多少个客户端 + // TODO_MA 注释: 此时做了优化:提供一个线程池来完成这个工作:初始化线程数量 = 3, 最大线程数量是 20 + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动监听,监听选举端口 + */ + // Starts listener thread that waits for connection requests + listener = new Listener(); + listener.setName("QuorumPeerListener"); + } + + // we always use the Connection Executor during connection initiation (to handle connection + // timeouts), and optionally use it during receiving connections (as the Quorum SASL authentication + // can take extra time) + private void initializeConnectionExecutor(final long mySid, final int quorumCnxnThreadsSize) { + final AtomicInteger threadIndex = new AtomicInteger(1); + SecurityManager s = System.getSecurityManager(); + final ThreadGroup group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + + final ThreadFactory daemonThFactory = runnable -> new Thread(group, runnable, + String.format("QuorumConnectionThread-[myid=%d]-%d", mySid, threadIndex.getAndIncrement())); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化 3 个线程,用来监听 选举链接请求 + */ + this.connectionExecutor = new ThreadPoolExecutor(3, quorumCnxnThreadsSize, 60, TimeUnit.SECONDS, + new SynchronousQueue<>(), daemonThFactory); + this.connectionExecutor.allowCoreThreadTimeOut(true); + } + + /** + * Invokes initiateConnection for testing purposes + * + * @param sid + */ + public void testInitiateConnection(long sid) { + LOG.debug("Opening channel to server {}", sid); + initiateConnection(self.getVotingView().get(sid).electionAddr, sid); + } + + /** + * First we create the socket, perform SSL handshake and authentication if needed. + * Then we perform the initiation protocol. + * If this server has initiated the connection, then it gives up on the + * connection if it loses challenge. Otherwise, it keeps the connection. + */ + public void initiateConnection(final MultipleAddresses electionAddr, final Long sid) { + + // TODO_MA 注释: 初始化一个 BIO 客户端实例 Socket,并且完成和对方的链接 + Socket sock = null; + try { + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化 Socket 客户端 + */ + LOG.debug("Opening channel to server {}", sid); + if (self.isSslQuorum()) { + sock = self.getX509Util().createSSLSocket(); + } else { + // TODO_MA 注释: 创建一个 Socket 对象 + sock = SOCKET_FACTORY.get(); + } + setSockOpts(sock); + + // TODO_MA 注释: 连接对方服务器 + // TODO_MA 注释: 服务器 A 发起链接请求 + sock.connect(electionAddr.getReachableOrOne(), cnxTO); + + if (sock instanceof SSLSocket) { + SSLSocket sslSock = (SSLSocket) sock; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 握手,建立链接 + */ + sslSock.startHandshake(); + LOG.info("SSL handshake complete with {} - {} - {}", sslSock.getRemoteSocketAddress(), + sslSock.getSession().getProtocol(), sslSock.getSession().getCipherSuite()); + } + + LOG.debug("Connected to server {} using election address: {}:{}", sid, sock.getInetAddress(), sock.getPort()); + } catch (X509Exception e) { + LOG.warn("Cannot open secure channel to {} at election address {}", sid, electionAddr, e); + closeSocket(sock); + return; + } catch (UnresolvedAddressException | IOException e) { + LOG.warn("Cannot open channel to {} at election address {}", sid, electionAddr, e); + closeSocket(sock); + return; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 开始链接 + */ + try { + startConnection(sock, sid); + } catch (IOException e) { + LOG.error("Exception while connecting, id: {}, addr: {}, closing learner connection", sid, + sock.getRemoteSocketAddress(), e); + closeSocket(sock); + } + } + + /** + * Server will initiate the connection request to its peer server + * asynchronously via separate connection thread. + */ + public boolean initiateConnectionAsync(final MultipleAddresses electionAddr, final Long sid) { + if (!inprogressConnections.add(sid)) { + // simply return as there is a connection request to + // server 'sid' already in progress. + LOG.debug("Connection request to server id: {} is already in progress, so skipping this request", sid); + return true; + } + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 通过一个线程来完成和对方的链接 + */ + connectionExecutor.execute(new QuorumConnectionReqThread(electionAddr, sid)); + connectionThreadCnt.incrementAndGet(); + + } catch (Throwable e) { + // Imp: Safer side catching all type of exceptions and remove 'sid' + // from inprogress connections. This is to avoid blocking further + // connection requests from this 'sid' in case of errors. + inprogressConnections.remove(sid); + LOG.error("Exception while submitting quorum connection request", e); + return false; + } + return true; + } + + /** + * Thread to send connection request to peer server. + */ + private class QuorumConnectionReqThread extends ZooKeeperThread { + final MultipleAddresses electionAddr; + final Long sid; + + QuorumConnectionReqThread(final MultipleAddresses electionAddr, final Long sid) { + super("QuorumConnectionReqThread-" + sid); + this.electionAddr = electionAddr; + this.sid = sid; + } + + @Override + public void run() { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化链接 + */ + initiateConnection(electionAddr, sid); + } finally { + inprogressConnections.remove(sid); + } + } + + } + + private boolean startConnection(Socket sock, Long sid) throws IOException { + DataOutputStream dout = null; + DataInputStream din = null; + LOG.debug("startConnection (myId:{} --> sid:{})", self.getId(), sid); + try { + + // TODO_MA 注释: 此时客户端中,初始化了饿一个 输出流 + // Use BufferedOutputStream to reduce the number of IP packets. This is + // important for x-DC scenarios. + BufferedOutputStream buf = new BufferedOutputStream(sock.getOutputStream()); + dout = new DataOutputStream(buf); + + // Sending id and challenge + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 写自己的 信息给 给对方 + * 1、protocolVersion + * 2、myid + * 3、ddr_bytes.lengtah + * 4、addr_bytes(hostname:port) + */ + // First sending the protocol version (in other words - message type). + // For backward compatibility reasons we stick to the old protocol version, unless the MultiAddress + // feature is enabled. During rolling upgrade, we must make sure that all the servers can + // understand the protocol version we use to avoid multiple partitions. see ZOOKEEPER-3720 + long protocolVersion = self.isMultiAddressEnabled() ? PROTOCOL_VERSION_V2 : PROTOCOL_VERSION_V1; + + // TODO_MA 注释: 写 protocolVersion + dout.writeLong(protocolVersion); + + // TODO_MA 注释: 写 myid + dout.writeLong(self.getId()); + + // now we send our election address. For the new protocol version, we can send multiple addresses. + Collection addressesToSend = protocolVersion == PROTOCOL_VERSION_V2 ? self + .getElectionAddress().getAllAddresses() : Arrays.asList(self.getElectionAddress().getOne()); + + String addr = addressesToSend.stream().map(NetUtils::formatInetAddr).collect(Collectors.joining("|")); + byte[] addr_bytes = addr.getBytes(); + + // TODO_MA 注释: 写 addr_bytes.length + dout.writeInt(addr_bytes.length); + + // TODO_MA 注释: 写具体的值: addr_bytes + dout.write(addr_bytes); + dout.flush(); + + din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); + } catch (IOException e) { + LOG.warn("Ignoring exception reading or writing challenge: ", e); + closeSocket(sock); + return false; + } + + // authenticate learner + QuorumPeer.QuorumServer qps = self.getVotingView().get(sid); + if (qps != null) { + // TODO - investigate why reconfig makes qps null. + authLearner.authenticate(sock, qps.hostname); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 进行 myid 比对,如果对方的比我大,关闭连接 + * 只有 myid 大的才有资格跟 myid 小的发起链接请求,建立链接。否则就关闭链接 + * 如果关闭了链接,则对方会发起链接请求过来的 + */ + // If lost the challenge, then drop the new connection + if (sid > self.getId()) { + LOG.info("Have smaller server identifier, so dropping the connection: (myId:{} --> sid:{})", self.getId(), + sid); + closeSocket(sock); + // Otherwise proceed with the connection + } + + // TODO_MA 注释: 如果代码走这儿,证明,我的 myid 比对方大。 + // TODO_MA 注释: 意味着,我并没有关闭链接,则初始化两个线程和一个队列 + else { + LOG.debug("Have larger server identifier, so keeping the connection: (myId:{} --> sid:{})", self.getId(), + sid); + SendWorker sw = new SendWorker(sock, sid); + RecvWorker rw = new RecvWorker(sock, din, sid, sw); + sw.setRecv(rw); + SendWorker vsw = senderWorkerMap.get(sid); + if (vsw != null) { + vsw.finish(); + } + senderWorkerMap.put(sid, sw); + queueSendMap.putIfAbsent(sid, new CircularBlockingQueue<>(SEND_CAPACITY)); + sw.start(); + rw.start(); + return true; + } + return false; + } + + /** + * If this server receives a connection request, then it gives up on the new + * connection if it wins. Notice that it checks whether it has a connection + * to this server already or not. If it does, then it sends the smallest + * possible long value to lose the challenge. + */ + public void receiveConnection(final Socket sock) { + DataInputStream din = null; + try { + + // TODO_MA 注释: 获取一个输入流用于读取数据 + din = new DataInputStream(new BufferedInputStream(sock.getInputStream())); + + LOG.debug("Sync handling of connection request received from: {}", sock.getRemoteSocketAddress()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 处理链接 + */ + handleConnection(sock, din); + } catch (IOException e) { + LOG.error("Exception handling connection, addr: {}, closing server connection", + sock.getRemoteSocketAddress()); + LOG.debug("Exception details: ", e); + closeSocket(sock); + } + } + + /** + * Server receives a connection request and handles it asynchronously via + * separate thread. + */ + public void receiveConnectionAsync(final Socket sock) { + try { + LOG.debug("Async handling of connection request received from: {}", sock.getRemoteSocketAddress()); + connectionExecutor.execute(new QuorumConnectionReceiverThread(sock)); + connectionThreadCnt.incrementAndGet(); + } catch (Throwable e) { + LOG.error("Exception handling connection, addr: {}, closing server connection", + sock.getRemoteSocketAddress()); + LOG.debug("Exception details: ", e); + closeSocket(sock); + } + } + + /** + * Thread to receive connection request from peer server. + */ + private class QuorumConnectionReceiverThread extends ZooKeeperThread { + + private final Socket sock; + + QuorumConnectionReceiverThread(final Socket sock) { + super("QuorumConnectionReceiverThread-" + sock.getRemoteSocketAddress()); + this.sock = sock; + } + + @Override + public void run() { + receiveConnection(sock); + } + + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 这是指 ServerSocket 接收到一个 Socket 的链接的时候 + */ + private void handleConnection(Socket sock, DataInputStream din) throws IOException { + Long sid = null, protocolVersion = null; + MultipleAddresses electionAddr = null; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 客户端其实会写过来四个东西: + * 1、protocolVersion + * 2、myid + * 3、address length + * 4、address + */ + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取 protocolVersion + */ + protocolVersion = din.readLong(); + + if (protocolVersion >= 0) { // this is a server id and not a protocol version + sid = protocolVersion; + } else { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 读取 myid, address length, address 信息 + */ + InitialMessage init = InitialMessage.parse(protocolVersion, din); + + // TODO_MA 注释: 读取到了对方服务器的 myid + sid = init.sid; + + if (!init.electionAddr.isEmpty()) { + electionAddr = new MultipleAddresses(init.electionAddr, + Duration.ofMillis(self.getMultiAddressReachabilityCheckTimeoutMs())); + } + LOG.debug("Initial message parsed by {}: {}", self.getId(), init.toString()); + } catch (InitialMessage.InitialMessageException ex) { + LOG.error("Initial message parsing error!", ex); + closeSocket(sock); + return; + } + } + + if (sid == QuorumPeer.OBSERVER_ID) { + /* + * Choose identifier at random. We need a value to identify the connection. + */ + sid = observerCounter.getAndDecrement(); + LOG.info("Setting arbitrary identifier to observer: {}", sid); + } + } catch (IOException e) { + LOG.warn("Exception reading or writing challenge", e); + closeSocket(sock); + return; + } + + // do authenticating learner + authServer.authenticate(sock, din); + //If wins the challenge, then close the new connection. + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果对方 myid 小于 我的, 则关闭链接 + * 1、sid 就是 对方 myid + * 2、self.getId() 就是自己的 myid + */ + if (sid < self.getId()) { + /* + * This replica might still believe that the connection to sid is + * up, so we have to shut down the workers before trying to open a + * new connection. + */ + SendWorker sw = senderWorkerMap.get(sid); + if (sw != null) { + sw.finish(); + } + + /* + * Now we start a new connection + * // TODO_MA 注释: 关闭链接! + */ + LOG.debug("Create new connection to server: {}", sid); + closeSocket(sock); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 重新建立链接 + */ + if (electionAddr != null) { + connectOne(sid, electionAddr); + } else { + connectOne(sid); + } + } + + // TODO_MA 注释: 相等不太可能 + else if (sid == self.getId()) { + // we saw this case in ZOOKEEPER-2164 + LOG.warn( + "We got a connection request from a server with our own ID. " + "This should be either a configuration error, or a bug."); + } + + + // TODO_MA 注释: 到底为止,两个 ZK Server 的 选举链接一定是建立好了 + // TODO_MA 注释: 两个 ZK Server 都要去创建一组工作组件 + + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 当对方的 myid 大于我的!!!!!! + * 相当于 客户端的 myid 要大!! 我靠!!!! + * if(sid > self.getId()) + * 1、sid 是客户端的 id + * 2、self.getId() 是服务端的 ID + */ + else { // Otherwise start worker threads to receive data. + SendWorker sw = new SendWorker(sock, sid); + RecvWorker rw = new RecvWorker(sock, din, sid, sw); + sw.setRecv(rw); + SendWorker vsw = senderWorkerMap.get(sid); + if (vsw != null) { + vsw.finish(); + } + senderWorkerMap.put(sid, sw); + queueSendMap.putIfAbsent(sid, new CircularBlockingQueue<>(SEND_CAPACITY)); + sw.start(); + rw.start(); + } + } + + /** + * Processes invoke this message to queue a message to send. Currently, + * only leader election uses it. + * // TODO_MA 注释: sid 是对方服务器的 myid + */ + public void toSend(Long sid, ByteBuffer b) { + /* + * If sending message to myself, then simply enqueue it (loopback). + * // TODO_MA 注释: 发给自己 + */ + if (this.mySid == sid) { + b.position(0); + addToRecvQueue(new Message(b.duplicate(), sid)); + /* + * Otherwise send to the corresponding thread to send. + */ + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发给对方 + */ + else { + /* + * Start a new connection if doesn't have one already. + * // TODO_MA 注释: 每一个对方服务器,都有一个专门的发送队列,和发送线程 + */ + BlockingQueue bq = queueSendMap + .computeIfAbsent(sid, serverId -> new CircularBlockingQueue<>(SEND_CAPACITY)); + addToSendQueue(bq, b); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 将选票放入对应的发送队列中 + */ + connectOne(sid); + } + } + + /** + * Try to establish a connection to server with id sid using its electionAddr. + * The function will return quickly and the connection will be established asynchronously. + * + * VisibleForTesting. + * + * @param sid server id + * @return boolean success indication + */ + synchronized boolean connectOne(long sid, MultipleAddresses electionAddr) { + if (senderWorkerMap.get(sid) != null) { + LOG.debug("There is a connection already for server {}", sid); + if (self.isMultiAddressEnabled() && electionAddr.size() > 1 && self.isMultiAddressReachabilityCheckEnabled()) { + // since ZOOKEEPER-3188 we can use multiple election addresses to reach a server. It is possible, that the + // one we are using is already dead and we need to clean-up, so when we will create a new connection + // then we will choose an other one, which is actually reachable + senderWorkerMap.get(sid).asyncValidateIfSocketIsStillReachable(); + } + return true; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化链接并且完成选票发送 + */ + // we are doing connection initiation always asynchronously, since it is possible that + // the socket connection timeouts or the SSL handshake takes too long and don't want + // to keep the rest of the connections to wait + return initiateConnectionAsync(electionAddr, sid); + } + + /** + * Try to establish a connection to server with id sid. + * The function will return quickly and the connection will be established asynchronously. + * + * @param sid server id + */ + synchronized void connectOne(long sid) { + if (senderWorkerMap.get(sid) != null) { + LOG.debug("There is a connection already for server {}", sid); + if (self.isMultiAddressEnabled() && self.isMultiAddressReachabilityCheckEnabled()) { + // since ZOOKEEPER-3188 we can use multiple election addresses to reach a server. It is possible, that the + // one we are using is already dead and we need to clean-up, so when we will create a new connection + // then we will choose an other one, which is actually reachable + senderWorkerMap.get(sid).asyncValidateIfSocketIsStillReachable(); + } + return; + } + synchronized (self.QV_LOCK) { + boolean knownId = false; + // Resolve hostname for the remote server before attempting to + // connect in case the underlying ip address has changed. + self.recreateSocketAddresses(sid); + Map lastCommittedView = self.getView(); + QuorumVerifier lastSeenQV = self.getLastSeenQuorumVerifier(); + Map lastProposedView = lastSeenQV.getAllMembers(); + + if (lastCommittedView.containsKey(sid)) { + knownId = true; + LOG.debug("Server {} knows {} already, it is in the lastCommittedView", self.getId(), sid); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 联系对方服务器,执行选票发送 + */ + if (connectOne(sid, lastCommittedView.get(sid).electionAddr)) { + return; + } + } + if (lastSeenQV != null && lastProposedView.containsKey(sid) && (!knownId || !lastProposedView + .get(sid).electionAddr.equals(lastCommittedView.get(sid).electionAddr))) { + knownId = true; + LOG.debug("Server {} knows {} already, it is in the lastProposedView", self.getId(), sid); + + if (connectOne(sid, lastProposedView.get(sid).electionAddr)) { + return; + } + } + if (!knownId) { + LOG.warn("Invalid server id: {} ", sid); + } + } + } + + /** + * Try to establish a connection with each server if one + * doesn't exist. + */ + + public void connectAll() { + long sid; + for (Enumeration en = queueSendMap.keys(); en.hasMoreElements(); ) { + sid = en.nextElement(); + connectOne(sid); + } + } + + /** + * Check if all queues are empty, indicating that all messages have been delivered. + */ + boolean haveDelivered() { + for (BlockingQueue queue : queueSendMap.values()) { + final int queueSize = queue.size(); + LOG.debug("Queue size: {}", queueSize); + if (queueSize == 0) { + return true; + } + } + + return false; + } + + /** + * Flag that it is time to wrap up all activities and interrupt the listener. + */ + public void halt() { + shutdown = true; + LOG.debug("Halting listener"); + listener.halt(); + + // Wait for the listener to terminate. + try { + listener.join(); + } catch (InterruptedException ex) { + LOG.warn("Got interrupted before joining the listener", ex); + } + softHalt(); + + // clear data structures used for auth + if (connectionExecutor != null) { + connectionExecutor.shutdown(); + } + inprogressConnections.clear(); + resetConnectionThreadCount(); + } + + /** + * A soft halt simply finishes workers. + */ + public void softHalt() { + for (SendWorker sw : senderWorkerMap.values()) { + LOG.debug("Server {} is soft-halting sender towards: {}", self.getId(), sw); + sw.finish(); + } + } + + /** + * Helper method to set socket options. + * + * @param sock Reference to socket + */ + private void setSockOpts(Socket sock) throws SocketException { + sock.setTcpNoDelay(true); + sock.setKeepAlive(tcpKeepAlive); + sock.setSoTimeout(this.socketTimeout); + } + + /** + * Helper method to close a socket. + * + * @param sock Reference to socket + */ + private void closeSocket(Socket sock) { + if (sock == null) { + return; + } + + try { + sock.close(); + } catch (IOException ie) { + LOG.error("Exception while closing", ie); + } + } + + /** + * Return number of worker threads + */ + public long getThreadCount() { + return threadCnt.get(); + } + + /** + * Return number of connection processing threads. + */ + public long getConnectionThreadCount() { + return connectionThreadCnt.get(); + } + + /** + * Reset the value of connection processing threads count to zero. + */ + private void resetConnectionThreadCount() { + connectionThreadCnt.set(0); + } + + /** + * Thread to listen on some ports + */ + public class Listener extends ZooKeeperThread { + + private static final String ELECTION_PORT_BIND_RETRY = "zookeeper.electionPortBindRetry"; + private static final int DEFAULT_PORT_BIND_MAX_RETRY = 3; + + private final int portBindMaxRetry; + private Runnable socketBindErrorHandler = () -> ServiceUtils + .requestSystemExit(ExitCode.UNABLE_TO_BIND_QUORUM_PORT.getValue()); + private List listenerHandlers; + private final AtomicBoolean socketException; + + public Listener() { + // During startup of thread, thread name will be overridden to + // specific election address + super("ListenerThread"); + + socketException = new AtomicBoolean(false); + + // maximum retry count while trying to bind to election port + // see ZOOKEEPER-3320 for more details + final Integer maxRetry = Integer.getInteger(ELECTION_PORT_BIND_RETRY, DEFAULT_PORT_BIND_MAX_RETRY); + if (maxRetry >= 0) { + LOG.info("Election port bind maximum retries is {}", maxRetry == 0 ? "infinite" : maxRetry); + portBindMaxRetry = maxRetry; + } else { + LOG.info("'{}' contains invalid value: {}(must be >= 0). Use default value of {} instead.", + ELECTION_PORT_BIND_RETRY, maxRetry, DEFAULT_PORT_BIND_MAX_RETRY); + portBindMaxRetry = DEFAULT_PORT_BIND_MAX_RETRY; + } + } + + /** + * Change socket bind error handler. Used for testing. + */ + void setSocketBindErrorHandler(Runnable errorHandler) { + this.socketBindErrorHandler = errorHandler; + } + + @Override + public void run() { + if (!shutdown) { + // TODO_MA 马中华 注释: 获取地址 + LOG.debug("Listener thread started, myId: {}", self.getId()); + Set addresses; + if (self.getQuorumListenOnAllIPs()) { + addresses = self.getElectionAddress().getWildcardAddresses(); + } else { + addresses = self.getElectionAddress().getAllAddresses(); + } + + CountDownLatch latch = new CountDownLatch(addresses.size()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化 ListenerHandler + * addresses 你就认为是一个地址 + * 一个地址,就被响应的构建了一个 ListenerHandler 的一个线程 + */ + listenerHandlers = addresses.stream() + .map(address -> new ListenerHandler(address, self.shouldUsePortUnification(), self.isSslQuorum(), + latch)).collect(Collectors.toList()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化一个线程池运行这些 ListenerHandler + */ + ExecutorService executor = Executors.newFixedThreadPool(addresses.size()); + listenerHandlers.forEach(executor::submit); + + try { + latch.await(); + } catch (InterruptedException ie) { + LOG.error("Interrupted while sleeping. Ignoring exception", ie); + } finally { + // Clean up for shutdown. + for (ListenerHandler handler : listenerHandlers) { + try { + handler.close(); + } catch (IOException ie) { + // Don't log an error for shutdown. + LOG.debug("Error closing server socket", ie); + } + } + } + } + + LOG.info("Leaving listener"); + if (!shutdown) { + LOG.error( + "As I'm leaving the listener thread, I won't be able to participate in leader election any longer: {}", + self.getElectionAddress().getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|"))); + if (socketException.get()) { + // After leaving listener thread, the host cannot join the quorum anymore, + // this is a severe error that we cannot recover from, so we need to exit + socketBindErrorHandler.run(); + } + } + } + + /** + * Halts this listener thread. + */ + void halt() { + LOG.debug("Halt called: Trying to close listeners"); + if (listenerHandlers != null) { + LOG.debug("Closing listener: {}", QuorumCnxManager.this.mySid); + for (ListenerHandler handler : listenerHandlers) { + try { + handler.close(); + } catch (IOException e) { + LOG.warn("Exception when shutting down listener: ", e); + } + } + } + } + + class ListenerHandler implements Runnable, Closeable { + + // TODO_MA 注释: 这个类就是 BIO 服务端 + private ServerSocket serverSocket; + private InetSocketAddress address; + private boolean portUnification; + private boolean sslQuorum; + private CountDownLatch latch; + + ListenerHandler(InetSocketAddress address, boolean portUnification, boolean sslQuorum, CountDownLatch latch) { + this.address = address; + this.portUnification = portUnification; + this.sslQuorum = sslQuorum; + this.latch = latch; + } + + /** + * Sleeps on acceptConnections(). + */ + @Override + public void run() { + try { + Thread.currentThread().setName("ListenerHandler-" + address); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 接受链接 + */ + acceptConnections(); + + try { + close(); + } catch (IOException e) { + LOG.warn("Exception when shutting down listener: ", e); + } + } catch (Exception e) { + // Output of unexpected exception, should never happen + LOG.error("Unexpected error ", e); + } finally { + latch.countDown(); + } + } + + @Override + public synchronized void close() throws IOException { + if (serverSocket != null && !serverSocket.isClosed()) { + LOG.debug("Trying to close listeners: {}", serverSocket); + serverSocket.close(); + } + } + + /** + * Sleeps on accept(). + */ + private void acceptConnections() { + int numRetries = 0; + Socket client = null; + + while ((!shutdown) && (portBindMaxRetry == 0 || numRetries < portBindMaxRetry)) { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建一个新的 ServerSocket + */ + serverSocket = createNewServerSocket(); + LOG.info("{} is accepting connections now, my election bind port: {}", + QuorumCnxManager.this.mySid, address.toString()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 开始运行 + */ + while (!shutdown) { + try { + + // TODO_MA 注释: 接受一个链接 + // TODO_MA 注释: 等待有 其他节点的 BIO 客户端发起链接请求过来 + client = serverSocket.accept(); + setSockOpts(client); + LOG.info("Received connection request from {}", client.getRemoteSocketAddress()); + // Receive and handle the connection request + // asynchronously if the quorum sasl authentication is + // enabled. This is required because sasl server + // authentication process may take few seconds to finish, + // this may delay next peer connection requests. + if (quorumSaslAuthEnabled) { + receiveConnectionAsync(client); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 完成选举链接建立 + */ + else { + receiveConnection(client); + } + numRetries = 0; + } catch (SocketTimeoutException e) { + LOG.warn( + "The socket is listening for the election accepted " + "and it timed out unexpectedly, but will retry." + "see ZOOKEEPER-2836"); + } + } + } catch (IOException e) { + if (shutdown) { + break; + } + + LOG.error("Exception while listening", e); + + if (e instanceof SocketException) { + socketException.set(true); + } + + numRetries++; + try { + close(); + Thread.sleep(1000); + } catch (IOException ie) { + LOG.error("Error closing server socket", ie); + } catch (InterruptedException ie) { + LOG.error("Interrupted while sleeping. Ignoring exception", ie); + } + closeSocket(client); + } + } + if (!shutdown) { + LOG.error( + "Leaving listener thread for address {} after {} errors. Use {} property to increase retry count.", + formatInetAddr(address), numRetries, ELECTION_PORT_BIND_RETRY); + } + } + + private ServerSocket createNewServerSocket() throws IOException { + ServerSocket socket; + + if (portUnification) { + LOG.info("Creating TLS-enabled quorum server socket"); + socket = new UnifiedServerSocket(self.getX509Util(), true); + } else if (sslQuorum) { + LOG.info("Creating TLS-only quorum server socket"); + socket = new UnifiedServerSocket(self.getX509Util(), false); + } else { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: ServerSocket + */ + socket = new ServerSocket(); + } + + socket.setReuseAddress(true); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 绑定端口号 + */ + address = new InetSocketAddress(address.getHostString(), address.getPort()); + socket.bind(address); + + return socket; + } + } + + } + + /** + * Thread to send messages. Instance waits on a queue, and send a message as + * soon as there is one available. If connection breaks, then opens a new + * one. + */ + class SendWorker extends ZooKeeperThread { + + Long sid; + Socket sock; + RecvWorker recvWorker; + volatile boolean running = true; + DataOutputStream dout; + AtomicBoolean ongoingAsyncValidation = new AtomicBoolean(false); + + /** + * An instance of this thread receives messages to send + * through a queue and sends them to the server sid. + * + * @param sock Socket to remote peer + * @param sid Server identifier of remote peer + */ + SendWorker(Socket sock, Long sid) { + super("SendWorker:" + sid); + this.sid = sid; + this.sock = sock; + recvWorker = null; + try { + dout = new DataOutputStream(sock.getOutputStream()); + } catch (IOException e) { + LOG.error("Unable to access socket output stream", e); + closeSocket(sock); + running = false; + } + LOG.debug("Address of remote peer: {}", this.sid); + } + + synchronized void setRecv(RecvWorker recvWorker) { + this.recvWorker = recvWorker; + } + + /** + * Returns RecvWorker that pairs up with this SendWorker. + * + * @return RecvWorker + */ + synchronized RecvWorker getRecvWorker() { + return recvWorker; + } + + synchronized boolean finish() { + LOG.debug("Calling SendWorker.finish for {}", sid); + + if (!running) { + /* + * Avoids running finish() twice. + */ + return running; + } + + running = false; + closeSocket(sock); + + this.interrupt(); + if (recvWorker != null) { + recvWorker.finish(); + } + + LOG.debug("Removing entry from senderWorkerMap sid={}", sid); + + senderWorkerMap.remove(sid, this); + threadCnt.decrementAndGet(); + return running; + } + + synchronized void send(ByteBuffer b) throws IOException { + byte[] msgBytes = new byte[b.capacity()]; + try { + b.position(0); + b.get(msgBytes); + } catch (BufferUnderflowException be) { + LOG.error("BufferUnderflowException ", be); + return; + } + + // TODO_MA 注释: 客户端把 选票的 长度 和 具体数据发送给了 服务端 + dout.writeInt(b.capacity()); + dout.write(b.array()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 真实的完成了 选票的一个发送 + */ + dout.flush(); + } + + @Override + public void run() { + threadCnt.incrementAndGet(); + try { + /** + * If there is nothing in the queue to send, then we + * send the lastMessage to ensure that the last message + * was received by the peer. The message could be dropped + * in case self or the peer shutdown their connection + * (and exit the thread) prior to reading/processing + * the last message. Duplicate messages are handled correctly + * by the peer. + * + * If the send queue is non-empty, then we have a recent + * message than that stored in lastMessage. To avoid sending + * stale message, we should send the message in the send queue. + */ + BlockingQueue bq = queueSendMap.get(sid); + if (bq == null || isSendQueueEmpty(bq)) { + ByteBuffer b = lastMessageSent.get(sid); + if (b != null) { + LOG.debug("Attempting to send lastMessage to sid={}", sid); + send(b); + } + } + } catch (IOException e) { + LOG.error("Failed to send last message. Shutting down thread.", e); + this.finish(); + } + LOG.debug("SendWorker thread started towards {}. myId: {}", sid, QuorumCnxManager.this.mySid); + + try { + while (running && !shutdown && sock != null) { + + ByteBuffer b = null; + try { + + // TODO_MA 注释: 根据对方 Server 的 myid 找到对应的 选票发送队列 + BlockingQueue bq = queueSendMap.get(sid); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 阻塞获取对应发送队列中的 ByteBuffer + */ + if (bq != null) { + b = pollSendQueue(bq, 1000, TimeUnit.MILLISECONDS); + } else { + LOG.error("No queue of incoming messages for server {}", sid); + break; + } + + if (b != null) { + + // TODO_MA 注释: lastMessageSent 用来记录,当前节点,给所有的其他服务器发送的最近一张票是什么 + lastMessageSent.put(sid, b); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 真正执行发送 + */ + send(b); + } + } catch (InterruptedException e) { + LOG.warn("Interrupted while waiting for message on queue", e); + } + } + } catch (Exception e) { + LOG.warn("Exception when using channel: for id {} my id = {}", sid, QuorumCnxManager.this.mySid, e); + } + this.finish(); + + LOG.warn("Send worker leaving thread id {} my id = {}", sid, self.getId()); + } + + + public void asyncValidateIfSocketIsStillReachable() { + if (ongoingAsyncValidation.compareAndSet(false, true)) { + new Thread(() -> { + LOG.debug("validate if destination address is reachable for sid {}", sid); + if (sock != null) { + InetAddress address = sock.getInetAddress(); + try { + if (address.isReachable(500)) { + LOG.debug("destination address {} is reachable for sid {}", address.toString(), sid); + ongoingAsyncValidation.set(false); + return; + } + } catch (NullPointerException | IOException ignored) { + } + LOG.warn("destination address {} not reachable anymore, shutting down the SendWorker for sid {}", + address.toString(), sid); + this.finish(); + } + }).start(); + } else { + LOG.debug("validation of destination address for sid {} is skipped (it is already running)", sid); + } + } + + } + + /** + * Thread to receive messages. Instance waits on a socket read. If the + * channel breaks, then removes itself from the pool of receivers. + */ + class RecvWorker extends ZooKeeperThread { + + Long sid; + Socket sock; + volatile boolean running = true; + final DataInputStream din; + final SendWorker sw; + + RecvWorker(Socket sock, DataInputStream din, Long sid, SendWorker sw) { + super("RecvWorker:" + sid); + this.sid = sid; + this.sock = sock; + this.sw = sw; + this.din = din; + try { + // OK to wait until socket disconnects while reading. + sock.setSoTimeout(0); + } catch (IOException e) { + LOG.error("Error while accessing socket for {}", sid, e); + closeSocket(sock); + running = false; + } + } + + /** + * Shuts down this worker + * + * @return boolean Value of variable running + */ + synchronized boolean finish() { + LOG.debug("RecvWorker.finish called. sid: {}. myId: {}", sid, QuorumCnxManager.this.mySid); + if (!running) { + /* + * Avoids running finish() twice. + */ + return running; + } + running = false; + + this.interrupt(); + threadCnt.decrementAndGet(); + return running; + } + + @Override + public void run() { + threadCnt.incrementAndGet(); + try { + + // TODO_MA 注释: 进入循环,不停的去读取其他服务发送过来的票 + LOG.debug("RecvWorker thread towards {} started. myId: {}", sid, QuorumCnxManager.this.mySid); + while (running && !shutdown && sock != null) { + /** + * Reads the first int to determine the length of the message + */ + // TODO_MA 注释: 读取到数据包的长度 + int length = din.readInt(); + if (length <= 0 || length > PACKETMAXSIZE) { + throw new IOException("Received packet with invalid packet: " + length); + } + /** + * Allocates a new ByteBuffer to receive the message + */ + final byte[] msgArray = new byte[length]; + + // TODO_MA 注释: 读取到整个数据 + din.readFully(msgArray, 0, length); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 把 msgArray 编程 Message 放入到 recevQueue 中 + */ + addToRecvQueue(new Message(ByteBuffer.wrap(msgArray), sid)); + } + } catch (Exception e) { + LOG.warn("Connection broken for id {}, my id = {}", sid, QuorumCnxManager.this.mySid, e); + } finally { + LOG.warn("Interrupting SendWorker thread from RecvWorker. sid: {}. myId: {}", sid, + QuorumCnxManager.this.mySid); + sw.finish(); + closeSocket(sock); + } + } + + } + + /** + * Inserts an element in the provided {@link BlockingQueue}. This method + * assumes that if the Queue is full, an element from the head of the Queue is + * removed and the new item is inserted at the tail of the queue. This is done + * to prevent a thread from blocking while inserting an element in the queue. + * + * @param queue Reference to the Queue + * @param buffer Reference to the buffer to be inserted in the queue + */ + private void addToSendQueue(final BlockingQueue queue, final ByteBuffer buffer) { + + // TODO_MA 注释: 将选票放入队列 + final boolean success = queue.offer(buffer); + if (!success) { + throw new RuntimeException("Could not insert into receive queue"); + } + } + + /** + * Returns true if queue is empty. + * + * @param queue Reference to the queue + * @return true if the specified queue is empty + */ + private boolean isSendQueueEmpty(final BlockingQueue queue) { + return queue.isEmpty(); + } + + /** + * Retrieves and removes buffer at the head of this queue, + * waiting up to the specified wait time if necessary for an element to + * become available. + * + * {@link BlockingQueue#poll(long, java.util.concurrent.TimeUnit)} + */ + private ByteBuffer pollSendQueue(final BlockingQueue queue, final long timeout, + final TimeUnit unit) throws InterruptedException { + return queue.poll(timeout, unit); + } + + /** + * Inserts an element in the {@link #recvQueue}. If the Queue is full, this + * methods removes an element from the head of the Queue and then inserts the + * element at the tail of the queue. + * + * @param msg Reference to the message to be inserted in the queue + */ + public void addToRecvQueue(final Message msg) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 发给自己 + */ + final boolean success = this.recvQueue.offer(msg); + if (!success) { + throw new RuntimeException("Could not insert into receive queue"); + } + } + + /** + * Retrieves and removes a message at the head of this queue, + * waiting up to the specified wait time if necessary for an element to + * become available. + * + * {@link BlockingQueue#poll(long, java.util.concurrent.TimeUnit)} + */ + public Message pollRecvQueue(final long timeout, final TimeUnit unit) throws InterruptedException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return this.recvQueue.poll(timeout, unit); + } + + public boolean connectedToPeer(long peerSid) { + return senderWorkerMap.get(peerSid) != null; + } + + public boolean isReconfigEnabled() { + return self.isReconfigEnabled(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumMXBean.java new file mode 100644 index 0000000..a7c4506 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumMXBean.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * An MBean representing a zookeeper cluster nodes (aka quorum peers) + */ +public interface QuorumMXBean { + + /** + * @return the name of the quorum + */ + String getName(); + + /** + * @return configured number of peers in the quorum + */ + int getQuorumSize(); + + /** + * @return the number of ticks that the initial synchronization phase can take + */ + int getInitLimit(); + + /** + * @return the number of ticks that can pass between sending a request and getting an acknowledgment + */ + int getSyncLimit(); + + /** + * @param initLimit the number of ticks that the initial synchronization phase can take + */ + void setInitLimit(int initLimit); + + /** + * @param syncLimit the number of ticks that can pass between sending a request and getting an acknowledgment + */ + void setSyncLimit(int syncLimit); + + /** + * @return SSL communication between quorum members required + */ + boolean isSslQuorum(); + + /** + * @return SSL communication between quorum members enabled + */ + boolean isPortUnification(); + + /** + * @return Observer Leader Election Reconnect Delay time in MS + */ + long getObserverElectionDelayMS(); + + /** + * Set the Observer Leader Election Reconnect Delay time in MS + */ + void setObserverElectionDelayMS(long delayMS); + + boolean getDigestEnabled(); + + void disableDigest(); +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java new file mode 100644 index 0000000..8a6de55 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeer.java @@ -0,0 +1,2850 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.common.NetUtils.formatInetAddr; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; +import java.io.Writer; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import javax.security.sasl.SaslException; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.KeeperException.BadArgumentsException; +import org.apache.zookeeper.common.AtomicFileOutputStream; +import org.apache.zookeeper.common.AtomicFileWritingIdiom; +import org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement; +import org.apache.zookeeper.common.QuorumX509Util; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperThread; +import org.apache.zookeeper.server.admin.AdminServer; +import org.apache.zookeeper.server.admin.AdminServer.AdminServerException; +import org.apache.zookeeper.server.admin.AdminServerFactory; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.quorum.auth.NullQuorumAuthLearner; +import org.apache.zookeeper.server.quorum.auth.NullQuorumAuthServer; +import org.apache.zookeeper.server.quorum.auth.QuorumAuth; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthLearner; +import org.apache.zookeeper.server.quorum.auth.QuorumAuthServer; +import org.apache.zookeeper.server.quorum.auth.SaslQuorumAuthLearner; +import org.apache.zookeeper.server.quorum.auth.SaslQuorumAuthServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.ConfigUtils; +import org.apache.zookeeper.server.util.JvmPauseMonitor; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class manages the quorum protocol. There are three states this server can be in: + *
        + *
      1. Leader election - each server will elect a leader (proposing itself as a leader initially).
      2. + *
      3. Follower - the server will synchronize with the leader and replicate any transactions.
      4. + *
      5. Leader - the server will process requests and forward them to followers. + * A majority of followers must log the request before it can be accepted. + *
      + * + * This class will setup a datagram socket that will always respond with its + * view of the current leader. The response will take the form of: + * + *
      + *  int xid;
      + *  long myid;
      + *  long leader_id;
      + *  long leader_zxid;
      + * 
      + * + * The request for the current leader will consist solely of an xid: int xid; + */ +public class QuorumPeer extends ZooKeeperThread implements QuorumStats.Provider { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumPeer.class); + + private QuorumBean jmxQuorumBean; + LocalPeerBean jmxLocalPeerBean; + private Map jmxRemotePeerBean; + LeaderElectionBean jmxLeaderElectionBean; + + // The QuorumCnxManager is held through an AtomicReference to ensure cross-thread visibility + // of updates; see the implementation comment at setLastSeenQuorumVerifier(). + private AtomicReference qcmRef = new AtomicReference<>(); + + QuorumAuthServer authServer; + QuorumAuthLearner authLearner; + + /** + * ZKDatabase is a top level member of quorumpeer which will be used in all the zookeeperservers + * instantiated later. + * Also, it is created once on bootup and only thrown away in case of a truncate message from the leader + */ + private ZKDatabase zkDb; + + private JvmPauseMonitor jvmPauseMonitor; + + public static final class AddressTuple { + + public final MultipleAddresses quorumAddr; + public final MultipleAddresses electionAddr; + public final InetSocketAddress clientAddr; + + public AddressTuple(MultipleAddresses quorumAddr, MultipleAddresses electionAddr, InetSocketAddress clientAddr) { + this.quorumAddr = quorumAddr; + this.electionAddr = electionAddr; + this.clientAddr = clientAddr; + } + + } + + private int observerMasterPort; + + public int getObserverMasterPort() { + return observerMasterPort; + } + + public void setObserverMasterPort(int observerMasterPort) { + this.observerMasterPort = observerMasterPort; + } + + public static final String CONFIG_KEY_MULTI_ADDRESS_ENABLED = "zookeeper.multiAddress.enabled"; + public static final String CONFIG_DEFAULT_MULTI_ADDRESS_ENABLED = "false"; + + private boolean multiAddressEnabled = true; + + public boolean isMultiAddressEnabled() { + return multiAddressEnabled; + } + + public void setMultiAddressEnabled(boolean multiAddressEnabled) { + this.multiAddressEnabled = multiAddressEnabled; + LOG.info("multiAddress.enabled set to {}", multiAddressEnabled); + } + + public static final String CONFIG_KEY_MULTI_ADDRESS_REACHABILITY_CHECK_TIMEOUT_MS = "zookeeper.multiAddress.reachabilityCheckTimeoutMs"; + + private int multiAddressReachabilityCheckTimeoutMs = (int) MultipleAddresses.DEFAULT_TIMEOUT.toMillis(); + + public int getMultiAddressReachabilityCheckTimeoutMs() { + return multiAddressReachabilityCheckTimeoutMs; + } + + public void setMultiAddressReachabilityCheckTimeoutMs(int multiAddressReachabilityCheckTimeoutMs) { + this.multiAddressReachabilityCheckTimeoutMs = multiAddressReachabilityCheckTimeoutMs; + LOG.info("multiAddress.reachabilityCheckTimeoutMs set to {}", multiAddressReachabilityCheckTimeoutMs); + } + + public static final String CONFIG_KEY_MULTI_ADDRESS_REACHABILITY_CHECK_ENABLED = "zookeeper.multiAddress.reachabilityCheckEnabled"; + + private boolean multiAddressReachabilityCheckEnabled = true; + + public boolean isMultiAddressReachabilityCheckEnabled() { + return multiAddressReachabilityCheckEnabled; + } + + public void setMultiAddressReachabilityCheckEnabled(boolean multiAddressReachabilityCheckEnabled) { + this.multiAddressReachabilityCheckEnabled = multiAddressReachabilityCheckEnabled; + LOG.info("multiAddress.reachabilityCheckEnabled set to {}", multiAddressReachabilityCheckEnabled); + } + + public static class QuorumServer { + + public MultipleAddresses addr = new MultipleAddresses(); + + public MultipleAddresses electionAddr = new MultipleAddresses(); + + public InetSocketAddress clientAddr = null; + + public long id; + + public String hostname; + + public LearnerType type = LearnerType.PARTICIPANT; + + public boolean isClientAddrFromStatic = false; + + private List myAddrs; + + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, + InetSocketAddress clientAddr) { + this(id, addr, electionAddr, clientAddr, LearnerType.PARTICIPANT); + } + + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr) { + this(id, addr, electionAddr, null, LearnerType.PARTICIPANT); + } + + // VisibleForTesting + public QuorumServer(long id, InetSocketAddress addr) { + this(id, addr, null, null, LearnerType.PARTICIPANT); + } + + public long getId() { + return id; + } + + /** + * Performs a DNS lookup for server address and election address. + * + * If the DNS lookup fails, this.addr and electionAddr remain + * unmodified. + */ + public void recreateSocketAddresses() { + if(this.addr.isEmpty()) { + LOG.warn("Server address has not been initialized"); + return; + } + if(this.electionAddr.isEmpty()) { + LOG.warn("Election address has not been initialized"); + return; + } + this.addr.recreateSocketAddresses(); + this.electionAddr.recreateSocketAddresses(); + } + + private LearnerType getType(String s) throws ConfigException { + switch(s.trim().toLowerCase()) { + case "observer": + return LearnerType.OBSERVER; + case "participant": + return LearnerType.PARTICIPANT; + default: + throw new ConfigException("Unrecognised peertype: " + s); + } + } + + private static final String wrongFormat = " does not have the form server_config or server_config;client_config" + " where server_config is the pipe separated list of host:port:port or host:port:port:type" + " and client_config is port or host:port"; + + public QuorumServer(long sid, String addressStr) throws ConfigException { + this.id = sid; + LearnerType newType = null; + String[] serverClientParts = addressStr.split(";"); + String[] serverAddresses = serverClientParts[0].split("\\|"); + + if(serverClientParts.length == 2) { + String[] clientParts = ConfigUtils.getHostAndPort(serverClientParts[1]); + if(clientParts.length > 2) { + throw new ConfigException(addressStr + wrongFormat); + } + + // is client_config a host:port or just a port + hostname = (clientParts.length == 2) ? clientParts[0] : "0.0.0.0"; + try { + clientAddr = new InetSocketAddress(hostname, Integer.parseInt(clientParts[clientParts.length - 1])); + } catch(NumberFormatException e) { + throw new ConfigException( + "Address unresolved: " + hostname + ":" + clientParts[clientParts.length - 1]); + } + } + + boolean multiAddressEnabled = Boolean.parseBoolean( + System.getProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, + QuorumPeer.CONFIG_DEFAULT_MULTI_ADDRESS_ENABLED)); + if(!multiAddressEnabled && serverAddresses.length > 1) { + throw new ConfigException( + "Multiple address feature is disabled, but multiple addresses were specified for sid " + sid); + } + + for(String serverAddress : serverAddresses) { + String serverParts[] = ConfigUtils.getHostAndPort(serverAddress); + + // TODO_MA 注释: 长度判断 + if((serverClientParts.length > 2) || (serverParts.length < 3) || (serverParts.length > 4)) { + throw new ConfigException(addressStr + wrongFormat); + } + + // TODO_MA 注释: serverParts[0] serverParts[1] serverParts[2] + // server_config should be either host:port:port or host:port:port:type + InetSocketAddress tempAddress; + InetSocketAddress tempElectionAddress; + try { + tempAddress = new InetSocketAddress(serverParts[0], Integer.parseInt(serverParts[1])); + addr.addAddress(tempAddress); + } catch(NumberFormatException e) { + throw new ConfigException("Address unresolved: " + serverParts[0] + ":" + serverParts[1]); + } + try { + tempElectionAddress = new InetSocketAddress(serverParts[0], Integer.parseInt(serverParts[2])); + electionAddr.addAddress(tempElectionAddress); + } catch(NumberFormatException e) { + throw new ConfigException("Address unresolved: " + serverParts[0] + ":" + serverParts[2]); + } + + if(tempAddress.getPort() == tempElectionAddress.getPort()) { + throw new ConfigException( + "Client and election port must be different! Please update the " + "configuration file on server." + sid); + } + + // TODO_MA 注释: serverParts[3] + if(serverParts.length == 4) { + LearnerType tempType = getType(serverParts[3]); + if(newType == null) { + newType = tempType; + } + + if(newType != tempType) { + throw new ConfigException( + "Multiple addresses should have similar roles: " + type + " vs " + tempType); + } + } + + // TODO_MA 注释: serverParts[0] + this.hostname = serverParts[0]; + } + + if(newType != null) { + type = newType; + } + + setMyAddrs(); + } + + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, LearnerType type) { + this(id, addr, electionAddr, null, type); + } + + public QuorumServer(long id, InetSocketAddress addr, InetSocketAddress electionAddr, InetSocketAddress clientAddr, + LearnerType type) { + this.id = id; + if(addr != null) { + this.addr.addAddress(addr); + } + if(electionAddr != null) { + this.electionAddr.addAddress(electionAddr); + } + this.type = type; + this.clientAddr = clientAddr; + + setMyAddrs(); + } + + private void setMyAddrs() { + this.myAddrs = new ArrayList<>(); + this.myAddrs.addAll(this.addr.getAllAddresses()); + this.myAddrs.add(this.clientAddr); + this.myAddrs.addAll(this.electionAddr.getAllAddresses()); + this.myAddrs = excludedSpecialAddresses(this.myAddrs); + } + + public static String delimitedHostString(InetSocketAddress addr) { + String host = addr.getHostString(); + if(host.contains(":")) { + return "[" + host + "]"; + } else { + return host; + } + } + + public String toString() { + StringWriter sw = new StringWriter(); + + List addrList = new LinkedList<>(addr.getAllAddresses()); + List electionAddrList = new LinkedList<>(electionAddr.getAllAddresses()); + + if(addrList.size() > 0 && electionAddrList.size() > 0) { + addrList.sort(Comparator.comparing(InetSocketAddress::getHostString)); + electionAddrList.sort(Comparator.comparing(InetSocketAddress::getHostString)); + sw.append(IntStream.range(0, addrList.size()).mapToObj(i -> String + .format("%s:%d:%d", delimitedHostString(addrList.get(i)), addrList.get(i).getPort(), + electionAddrList.get(i).getPort())).collect(Collectors.joining("|"))); + } + + if(type == LearnerType.OBSERVER) { + sw.append(":observer"); + } else if(type == LearnerType.PARTICIPANT) { + sw.append(":participant"); + } + + if(clientAddr != null && !isClientAddrFromStatic) { + sw.append(";"); + sw.append(delimitedHostString(clientAddr)); + sw.append(":"); + sw.append(String.valueOf(clientAddr.getPort())); + } + + return sw.toString(); + } + + public int hashCode() { + assert false : "hashCode not designed"; + return 42; // any arbitrary constant will do + } + + private boolean checkAddressesEqual(InetSocketAddress addr1, InetSocketAddress addr2) { + return (addr1 != null || addr2 == null) && (addr1 == null || addr2 != null) && (addr1 == null || addr2 == null || addr1 + .equals(addr2)); + } + + public boolean equals(Object o) { + if(!(o instanceof QuorumServer)) { + return false; + } + QuorumServer qs = (QuorumServer) o; + if((qs.id != id) || (qs.type != type)) { + return false; + } + if(!addr.equals(qs.addr)) { + return false; + } + if(!electionAddr.equals(qs.electionAddr)) { + return false; + } + return checkAddressesEqual(clientAddr, qs.clientAddr); + } + + public void checkAddressDuplicate(QuorumServer s) throws BadArgumentsException { + List otherAddrs = new ArrayList<>(s.addr.getAllAddresses()); + otherAddrs.add(s.clientAddr); + otherAddrs.addAll(s.electionAddr.getAllAddresses()); + otherAddrs = excludedSpecialAddresses(otherAddrs); + + for(InetSocketAddress my : this.myAddrs) { + + for(InetSocketAddress other : otherAddrs) { + if(my.equals(other)) { + String error = String + .format("%s of server.%d conflicts %s of server.%d", my, this.id, other, s.id); + throw new BadArgumentsException(error); + } + } + } + } + + private List excludedSpecialAddresses(List addrs) { + List included = new ArrayList<>(); + + for(InetSocketAddress addr : addrs) { + if(addr == null) { + continue; + } + InetAddress inetaddr = addr.getAddress(); + + if(inetaddr == null || inetaddr.isAnyLocalAddress() || // wildCard addresses (0.0.0.0 or [::]) + inetaddr.isLoopbackAddress()) { // loopback address(localhost/127.0.0.1) + continue; + } + included.add(addr); + } + return included; + } + + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、没有 leader 的时候,大家都是 LOOKING 装填 + * 2、如果有 leader 的话,大家根据选举结果,和 配置信息,来决定自己的状态 + * - + * 区分三对概念: + * 1、选举配置: observer participant + * 2、集群角色: observer leader follower + * 3、服务器状态: LOOKING, FOLLOWING, LEADING, OBSERVING + */ + public enum ServerState { + LOOKING, // TODO_MA 马中华 注释: 所有节点刚启动的时候的状态 + FOLLOWING, // TODO_MA 马中华 注释: 某个节点成为 follower 角色的时候的状态 + LEADING, + OBSERVING + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: ZabState 四种状态 + * 1、ELECTION 选举 + * 2、DISCOVERY 发现 + * 3、SYNCHRONIZATION 同步 + * 4、BROADCAST 广播 + */ + /** + * (Used for monitoring) shows the current phase of Zab protocol that peer is running. + * // TODO_MA 马中华 注释: 选举算法工作的结果,只是当前这个 节点 得到一个推举的结果 + * // TODO_MA 马中华 注释: 不同的节点得到的推举结果有可能不一样 + * // TODO_MA 马中华 注释: 需要确认:相互之间再次发送信息确认跟随的 leader 是否是同一个 + * // TODO_MA 马中华 注释: 如果确认了leader 是谁,则选举才真正结束 + * // TODO_MA 马中华 注释: 选举结束,进入 同步状态 + * // TODO_MA 马中华 注释: 同步状态结束之后,进入 BROADCAST, 只有进入 BROADCAST 状态,才能接收和处理外部客户端的请求 + */ + public enum ZabState { + ELECTION, // TODO_MA 马中华 注释: 表示正在选举 + DISCOVERY, // TODO_MA 马中华 注释: 选举算法工作完成,得到一个推举的结果,现在正在确认 + SYNCHRONIZATION, + BROADCAST + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + /** + * (Used for monitoring) When peer is in synchronization phase, this shows + * which synchronization mechanism is being used + */ + public enum SyncMode { + NONE, + DIFF, + SNAP, + TRUNC + } + + /* + * A peer can either be participating, which implies that it is willing to + * both vote in instances of consensus and to elect or become a Leader, or + * it may be observing in which case it isn't. + * + * We need this distinction to decide which ServerState to move to when + * conditions change (e.g. which state to become after LOOKING). + */ + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + public enum LearnerType { + + // TODO_MA 注释: 这个里面包含了 Leader 和 Follower + PARTICIPANT, + OBSERVER + } + + // TODO_MA 注释: Leader + Learner(Follower + Observer) + + /* + * To enable observers to have no identifier, we need a generic identifier + * at least for QuorumCnxManager. We use the following constant to as the + * value of such a generic identifier. + */ + + static final long OBSERVER_ID = Long.MAX_VALUE; + + /* + * Record leader election time + */ + public long start_fle, end_fle; // fle = fast leader election + public static final String FLE_TIME_UNIT = "MS"; + + /* + * Default value of peer is participant + */ + private LearnerType learnerType = LearnerType.PARTICIPANT; + + public LearnerType getLearnerType() { + return learnerType; + } + + /** + * Sets the LearnerType + */ + public void setLearnerType(LearnerType p) { + learnerType = p; + } + + protected synchronized void setConfigFileName(String s) { + configFilename = s; + } + + private String configFilename = null; + + public int getQuorumSize() { + return getVotingView().size(); + } + + public void setJvmPauseMonitor(JvmPauseMonitor jvmPauseMonitor) { + this.jvmPauseMonitor = jvmPauseMonitor; + } + + /** + * QuorumVerifier implementation; default (majority). + */ + + //last committed quorum verifier + private QuorumVerifier quorumVerifier; + + //last proposed quorum verifier + private QuorumVerifier lastSeenQuorumVerifier = null; + + // Lock object that guard access to quorumVerifier and lastSeenQuorumVerifier. + final Object QV_LOCK = new Object(); + + /** + * My id + */ + private long myid; + + /** + * get the id of this quorum peer. + */ + public long getId() { + return myid; + } + + // VisibleForTesting + void setId(long id) { + this.myid = id; + } + + private boolean sslQuorum; + private boolean shouldUsePortUnification; + + public boolean isSslQuorum() { + return sslQuorum; + } + + public boolean shouldUsePortUnification() { + return shouldUsePortUnification; + } + + private final QuorumX509Util x509Util; + + QuorumX509Util getX509Util() { + return x509Util; + } + + /** + * // TODO_MA 注释: 这个变量始终存储的是 当前节点认为的最合适当leader 的选票 + * // TODO_MA 注释: 所有的节点,一上来,都是不知道 leader, 都是推举自己当leader, 这个 currentVote 的信息都是自己的 + * // TODO_MA 注释: 当选举结束之后,选举算法会返回一个 Vote 对象, 这个 Vote 就是最终成功当选 leader 的选票 + * This is who I think the leader currently is. + */ + private volatile Vote currentVote; + + public synchronized Vote getCurrentVote() { + return currentVote; + } + + public synchronized void setCurrentVote(Vote v) { + currentVote = v; + } + + private volatile boolean running = true; + + private String initialConfig; + + /** + * The number of milliseconds of each tick + */ + protected int tickTime; + + /** + * Whether learners in this quorum should create new sessions as local. + * False by default to preserve existing behavior. + */ + protected boolean localSessionsEnabled = false; + + /** + * Whether learners in this quorum should upgrade local sessions to + * global. Only matters if local sessions are enabled. + */ + protected boolean localSessionsUpgradingEnabled = true; + + /** + * Minimum number of milliseconds to allow for session timeout. + * A value of -1 indicates unset, use default. + */ + protected int minSessionTimeout = -1; + + /** + * Maximum number of milliseconds to allow for session timeout. + * A value of -1 indicates unset, use default. + */ + protected int maxSessionTimeout = -1; + + /** + * The ZooKeeper server's socket backlog length. The number of connections + * that will be queued to be read before new connections are dropped. A + * value of one indicates the default backlog will be used. + */ + protected int clientPortListenBacklog = -1; + + /** + * The number of ticks that the initial synchronization phase can take + */ + protected volatile int initLimit; + + /** + * The number of ticks that can pass between sending a request and getting + * an acknowledgment + */ + protected volatile int syncLimit; + + /** + * The number of ticks that can pass before retrying to connect to learner master + */ + protected volatile int connectToLearnerMasterLimit; + + /** + * Enables/Disables sync request processor. This option is enabled + * by default and is to be used with observers. + */ + protected boolean syncEnabled = true; + + /** + * The current tick + */ + protected AtomicInteger tick = new AtomicInteger(); + + /** + * Whether or not to listen on all IPs for the two quorum ports + * (broadcast and fast leader election). + */ + protected boolean quorumListenOnAllIPs = false; + + /** + * Keeps time taken for leader election in milliseconds. Sets the value to + * this variable only after the completion of leader election. + */ + private long electionTimeTaken = -1; + + /** + * Enable/Disables quorum authentication using sasl. Defaulting to false. + */ + protected boolean quorumSaslEnableAuth; + + /** + * If this is false, quorum peer server will accept another quorum peer client + * connection even if the authentication did not succeed. This can be used while + * upgrading ZooKeeper server. Defaulting to false (required). + */ + protected boolean quorumServerSaslAuthRequired; + + /** + * If this is false, quorum peer learner will talk to quorum peer server + * without authentication. This can be used while upgrading ZooKeeper + * server. Defaulting to false (required). + */ + protected boolean quorumLearnerSaslAuthRequired; + + /** + * Kerberos quorum service principal. Defaulting to 'zkquorum/localhost'. + */ + protected String quorumServicePrincipal; + + /** + * Quorum learner login context name in jaas-conf file to read the kerberos + * security details. Defaulting to 'QuorumLearner'. + */ + protected String quorumLearnerLoginContext; + + /** + * Quorum server login context name in jaas-conf file to read the kerberos + * security details. Defaulting to 'QuorumServer'. + */ + protected String quorumServerLoginContext; + + // TODO: need to tune the default value of thread size + private static final int QUORUM_CNXN_THREADS_SIZE_DEFAULT_VALUE = 20; + /** + * The maximum number of threads to allow in the connectionExecutors thread + * pool which will be used to initiate quorum server connections. + */ + protected int quorumCnxnThreadsSize = QUORUM_CNXN_THREADS_SIZE_DEFAULT_VALUE; + + public static final String QUORUM_CNXN_TIMEOUT_MS = "zookeeper.quorumCnxnTimeoutMs"; + private static int quorumCnxnTimeoutMs; + + static { + quorumCnxnTimeoutMs = Integer.getInteger(QUORUM_CNXN_TIMEOUT_MS, -1); + LOG.info("{}={}", QUORUM_CNXN_TIMEOUT_MS, quorumCnxnTimeoutMs); + } + + /** + * @deprecated As of release 3.4.0, this class has been deprecated, since + * it is used with one of the udp-based versions of leader election, which + * we are also deprecating. + * + * This class simply responds to requests for the current leader of this + * node. + *

      + * The request contains just an xid generated by the requestor. + *

      + * The response has the xid, the id of this server, the id of the leader, + * and the zxid of the leader. + */ + @Deprecated + class ResponderThread extends ZooKeeperThread { + + ResponderThread() { + super("ResponderThread"); + } + + volatile boolean running = true; + + @Override + public void run() { + try { + byte[] b = new byte[36]; + ByteBuffer responseBuffer = ByteBuffer.wrap(b); + DatagramPacket packet = new DatagramPacket(b, b.length); + while(running) { + udpSocket.receive(packet); + if(packet.getLength() != 4) { + LOG.warn("Got more than just an xid! Len = {}", packet.getLength()); + } else { + responseBuffer.clear(); + responseBuffer.getInt(); // Skip the xid + responseBuffer.putLong(myid); + Vote current = getCurrentVote(); + switch(getPeerState()) { + case LOOKING: + responseBuffer.putLong(current.getId()); + responseBuffer.putLong(current.getZxid()); + break; + case LEADING: + responseBuffer.putLong(myid); + try { + long proposed; + synchronized(leader) { + proposed = leader.lastProposed; + } + responseBuffer.putLong(proposed); + } catch(NullPointerException npe) { + // This can happen in state transitions, + // just ignore the request + } + break; + case FOLLOWING: + responseBuffer.putLong(current.getId()); + try { + responseBuffer.putLong(follower.getZxid()); + } catch(NullPointerException npe) { + // This can happen in state transitions, + // just ignore the request + } + break; + case OBSERVING: + // Do nothing, Observers keep themselves to + // themselves. + break; + } + packet.setData(b); + udpSocket.send(packet); + } + packet.setLength(b.length); + } + } catch(RuntimeException e) { + LOG.warn("Unexpected runtime exception in ResponderThread", e); + } catch(IOException e) { + LOG.warn("Unexpected IO exception in ResponderThread", e); + } finally { + LOG.warn("QuorumPeer responder thread exited"); + } + } + + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + private ServerState state = ServerState.LOOKING; + + private AtomicReference zabState = new AtomicReference<>(ZabState.ELECTION); + private AtomicReference syncMode = new AtomicReference<>(SyncMode.NONE); + private AtomicReference leaderAddress = new AtomicReference(""); + private AtomicLong leaderId = new AtomicLong(-1); + + private boolean reconfigFlag = false; // indicates that a reconfig just committed + + public synchronized void setPeerState(ServerState newState) { + state = newState; + if(newState == ServerState.LOOKING) { + setLeaderAddressAndId(null, -1); + + // TODO_MA 注释: 切换到选举状态 + setZabState(ZabState.ELECTION); + } else { + LOG.info("Peer state changed: {}", getDetailedPeerState()); + } + } + + public void setZabState(ZabState zabState) { + this.zabState.set(zabState); + LOG.info("Peer state changed: {}", getDetailedPeerState()); + } + + public void setSyncMode(SyncMode syncMode) { + this.syncMode.set(syncMode); + LOG.info("Peer state changed: {}", getDetailedPeerState()); + } + + public ZabState getZabState() { + return zabState.get(); + } + + public SyncMode getSyncMode() { + return syncMode.get(); + } + + public void setLeaderAddressAndId(MultipleAddresses addr, long newId) { + if(addr != null) { + leaderAddress.set(String.join("|", addr.getAllHostStrings())); + } else { + leaderAddress.set(null); + } + leaderId.set(newId); + } + + public String getLeaderAddress() { + return leaderAddress.get(); + } + + public long getLeaderId() { + return leaderId.get(); + } + + public String getDetailedPeerState() { + final StringBuilder sb = new StringBuilder(getPeerState().toString().toLowerCase()); + final ZabState zabState = getZabState(); + if(!ZabState.ELECTION.equals(zabState)) { + sb.append(" - ").append(zabState.toString().toLowerCase()); + } + final SyncMode syncMode = getSyncMode(); + if(!SyncMode.NONE.equals(syncMode)) { + sb.append(" - ").append(syncMode.toString().toLowerCase()); + } + return sb.toString(); + } + + public synchronized void reconfigFlagSet() { + reconfigFlag = true; + } + + public synchronized void reconfigFlagClear() { + reconfigFlag = false; + } + + public synchronized boolean isReconfigStateChange() { + return reconfigFlag; + } + + public synchronized ServerState getPeerState() { + return state; + } + + DatagramSocket udpSocket; + + private final AtomicReference myAddrs = new AtomicReference<>(); + + /** + * Resolves hostname for a given server ID. + * + * This method resolves hostname for a given server ID in both quorumVerifer + * and lastSeenQuorumVerifier. If the server ID matches the local server ID, + * it also updates myAddrs. + */ + public void recreateSocketAddresses(long id) { + QuorumVerifier qv = getQuorumVerifier(); + if(qv != null) { + QuorumServer qs = qv.getAllMembers().get(id); + if(qs != null) { + qs.recreateSocketAddresses(); + if(id == getId()) { + setAddrs(qs.addr, qs.electionAddr, qs.clientAddr); + } + } + } + qv = getLastSeenQuorumVerifier(); + if(qv != null) { + QuorumServer qs = qv.getAllMembers().get(id); + if(qs != null) { + qs.recreateSocketAddresses(); + } + } + } + + private AddressTuple getAddrs() { + AddressTuple addrs = myAddrs.get(); + if(addrs != null) { + return addrs; + } + try { + synchronized(QV_LOCK) { + addrs = myAddrs.get(); + while(addrs == null) { + QV_LOCK.wait(); + addrs = myAddrs.get(); + } + return addrs; + } + } catch(InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); + } + } + + public MultipleAddresses getQuorumAddress() { + return getAddrs().quorumAddr; + } + + public MultipleAddresses getElectionAddress() { + return getAddrs().electionAddr; + } + + public InetSocketAddress getClientAddress() { + final AddressTuple addrs = myAddrs.get(); + return (addrs == null) ? null : addrs.clientAddr; + } + + private void setAddrs(MultipleAddresses quorumAddr, MultipleAddresses electionAddr, InetSocketAddress clientAddr) { + synchronized(QV_LOCK) { + myAddrs.set(new AddressTuple(quorumAddr, electionAddr, clientAddr)); + QV_LOCK.notifyAll(); + } + } + + private int electionType; + + Election electionAlg; + + ServerCnxnFactory cnxnFactory; + ServerCnxnFactory secureCnxnFactory; + + private FileTxnSnapLog logFactory = null; + + private final QuorumStats quorumStats; + + AdminServer adminServer; + + private final boolean reconfigEnabled; + + public static QuorumPeer testingQuorumPeer() throws SaslException { + return new QuorumPeer(); + } + + public QuorumPeer() throws SaslException { + super("QuorumPeer"); + quorumStats = new QuorumStats(this); + jmxRemotePeerBean = new HashMap(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从 zookeper-3.5.x 开始,内部提供了一个机遇 jetty 的 web 服务器 + * 其实就跟 hdfs 一样,提供了一个 web ui + */ + adminServer = AdminServerFactory.createAdminServer(); + + x509Util = createX509Util(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + initialize(); + reconfigEnabled = QuorumPeerConfig.isReconfigEnabled(); + } + + // VisibleForTesting + QuorumX509Util createX509Util() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: X.509是一种 数字证书 的 格式标准 。 应用很广泛,现在HTTPS依赖的SSL证书使用的就是使用的X.509格式。 + */ + return new QuorumX509Util(); + } + + /** + * For backward compatibility purposes, we instantiate QuorumMaj by default. + */ + + public QuorumPeer(Map quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, + int tickTime, int initLimit, int syncLimit, int connectToLearnerMasterLimit, + ServerCnxnFactory cnxnFactory) throws IOException { + this(quorumPeers, dataDir, dataLogDir, electionType, myid, tickTime, initLimit, syncLimit, + connectToLearnerMasterLimit, false, cnxnFactory, new QuorumMaj(quorumPeers)); + } + + public QuorumPeer(Map quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, + int tickTime, int initLimit, int syncLimit, int connectToLearnerMasterLimit, boolean quorumListenOnAllIPs, + ServerCnxnFactory cnxnFactory, QuorumVerifier quorumConfig) throws IOException { + this(); + this.cnxnFactory = cnxnFactory; + this.electionType = electionType; + this.myid = myid; + this.tickTime = tickTime; + this.initLimit = initLimit; + this.syncLimit = syncLimit; + this.connectToLearnerMasterLimit = connectToLearnerMasterLimit; + this.quorumListenOnAllIPs = quorumListenOnAllIPs; + this.logFactory = new FileTxnSnapLog(dataLogDir, dataDir); + this.zkDb = new ZKDatabase(this.logFactory); + if(quorumConfig == null) { + quorumConfig = new QuorumMaj(quorumPeers); + } + setQuorumVerifier(quorumConfig, false); + adminServer = AdminServerFactory.createAdminServer(); + } + + // TODO_MA 注释: 也都是为了 安全 做初始化的 + public void initialize() throws SaslException { + // init quorum auth server & learner + if(isQuorumSaslAuthEnabled()) { + Set authzHosts = new HashSet(); + for(QuorumServer qs : getView().values()) { + authzHosts.add(qs.hostname); + } + authServer = new SaslQuorumAuthServer(isQuorumServerSaslAuthRequired(), quorumServerLoginContext, authzHosts); + authLearner = new SaslQuorumAuthLearner(isQuorumLearnerSaslAuthRequired(), quorumServicePrincipal, + quorumLearnerLoginContext); + } else { + authServer = new NullQuorumAuthServer(); + authLearner = new NullQuorumAuthLearner(); + } + } + + QuorumStats quorumStats() { + return quorumStats; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 这个方法的目标是为了启动 QuorumPeer, 做了五件事: + * 1、 loadDataBase(); 冷启动数据恢复 + * 2、 startServerCnxnFactory(); 启动 NIO 服务端, 为发送读写请求的客户端提供服务的 + * 3、 adminServer.start(); 启动 AdminServer + * 4、 startLeaderElection(); 准备 选举环境 + * 5、 super.start(); 进入ZAB工作状态:首先执行选举 + * QuorumPeer 是一个线程,所以上述的 start() 其实就是让 QuorumPeer 进入线程的工作,也就是 run() + * run() 方法的逻辑就是 ZAB 的工作! + */ + @Override + public synchronized void start() { + if(!getView().containsKey(myid)) { + throw new RuntimeException("My id " + myid + " not in the peer list"); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 冷启动恢复 ZKDatabase (znode数据模型的数据) + * - + * 恢复的途径,有两: + * 1、最近的一个非损坏的快照文件,恢复绝大部分,比如 1-97 + * 2、从日志文件中恢复, 恢复少量新事务数据, 98-100 + * - + * 恢复的数据有两种: + * 1、session 信息 + * 2、datatree 数据 + * - + * 数据持久化机制中,会涉及类似于 LSMTree 操作方式 + * 其实在每个 zk 节点启动的时候, 节点和节点之间没有任何关系 + * 只有当客户端需要往 zk 中发事务请求的时候,节点与节点之间才有联系 + * 此时讲的是 启动呀, 启动的时候压根没有分布式事执行的,全都是单机事务 + * 快照文件 + 日志(才需要恢复事务的执行: 单机执行) + * - + * LSM Tree 专门有一篇论文讲这个事情! + * 1、先记录操作日志: 预记录日志 + * 2、做内存操作 + * 3、在记录日志: 提交事务操作的日志 + * hbase hdfs zookeeper 都是这样的 + */ + loadDataBase(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动服务端各种工作线程(一主多从的 reactor 模式的 NIO 服务端) + * 1、AcceptThread + * 2、SelectorThread + * 3、WorkerThread + * 4、会话超时处理线程 + */ + startServerCnxnFactory(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 WebServer + */ + try { + adminServer.start(); + } catch(AdminServerException e) { + LOG.warn("Problem starting AdminServer", e); + System.out.println(e); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 准备选举算法实例及相关环境 + * 1、准备选举环境 + * 启动了一个 BIO 服务端 + * 2、创建选举算法实例 + * 创建 FastLeaderElection 实例对象,极其复杂! + * 3、执行选举:FastLeaderElection.lookForLeader(); + */ + startLeaderElection(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动是一个 JVM 的监视器:gc + */ + startJvmPauseMonitor(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 一切准备就绪,开始进入 ZAB 工作状态 + * 1、崩溃恢复:选主: lookForLeader() + * 2、消息广播:读写请求处理 + */ + super.start(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + private void loadDataBase() { + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 加载 ZKDatabase + * QP 内部有一个成员变量: ZKDatabase + * - + * 内部实现细节: + * 1、从最新的不失效的 快照文件中,恢复绝大部分数据 + * 2、从日志文件中,恢复最新的少量数据(有可能没有) + */ + zkDb.loadDataBase(); + + // TODO_MA 注释: 上面这句代码恢复数据,最终,数据是存储在 zkDb.dataTree 成员变量里面 + + // TODO_MA 注释: 之后的逻辑,是关于 zxid 的处理 + + // TODO_MA 注释: 从 ZKDatabase 获取 lastProcessedZxid + // TODO_MA 注释: 这个 lastProcessedZxid = 100 + // load the epochs + long lastProcessedZxid = zkDb.getDataTree().lastProcessedZxid; + + // TODO_MA 注释: 从 lastProcessedZxid 中获取 epoch + long epochOfZxid = ZxidUtils.getEpochFromZxid(lastProcessedZxid); + + // TODO_MA 注释: 读取 currentEpoch + try { + currentEpoch = readLongFromFile(CURRENT_EPOCH_FILENAME); + } catch(FileNotFoundException e) { + // pick a reasonable epoch number + // this should only happen once when moving to a + // new code version + currentEpoch = epochOfZxid; + LOG.info( + "{} not found! Creating with a reasonable default of {}. " + "This should only happen when you are upgrading your installation", + CURRENT_EPOCH_FILENAME, currentEpoch); + + // TODO_MA 注释: 写入当前 epoch 到文件中 + writeLongToFile(CURRENT_EPOCH_FILENAME, currentEpoch); + } + + // TODO_MA 注释: epoch > currentEpoch 的话,然后写入 currentEpoch.tmp 文件 + if(epochOfZxid > currentEpoch) { + // acceptedEpoch.tmp file in snapshot directory + File currentTmp = new File(getTxnFactory().getSnapDir(), + CURRENT_EPOCH_FILENAME + AtomicFileOutputStream.TMP_EXTENSION); + if(currentTmp.exists()) { + long epochOfTmp = readLongFromFile(currentTmp.getName()); + LOG.info("{} found. Setting current epoch to {}.", currentTmp, epochOfTmp); + setCurrentEpoch(epochOfTmp); + } else { + throw new IOException("The current epoch, " + ZxidUtils + .zxidToString(currentEpoch) + ", is older than the last zxid, " + lastProcessedZxid); + } + } + + // TODO_MA 注释: 读取 acceptedEpoch + try { + acceptedEpoch = readLongFromFile(ACCEPTED_EPOCH_FILENAME); + } catch(FileNotFoundException e) { + // pick a reasonable epoch number + // this should only happen once when moving to a + // new code version + acceptedEpoch = epochOfZxid; + LOG.info( + "{} not found! Creating with a reasonable default of {}. " + "This should only happen when you are upgrading your installation", + ACCEPTED_EPOCH_FILENAME, acceptedEpoch); + writeLongToFile(ACCEPTED_EPOCH_FILENAME, acceptedEpoch); + } + + // TODO_MA 注释: acceptedEpoch 只能大于或者等于 currentEpoch + if(acceptedEpoch < currentEpoch) { + throw new IOException("The accepted epoch, " + ZxidUtils + .zxidToString(acceptedEpoch) + " is less than the current epoch, " + ZxidUtils + .zxidToString(currentEpoch)); + } + } catch(IOException ie) { + LOG.error("Unable to load database on disk", ie); + throw new RuntimeException("Unable to run quorum server ", ie); + } + } + + ResponderThread responder; + + public synchronized void stopLeaderElection() { + responder.running = false; + responder.interrupt(); + } + + public synchronized void startLeaderElection() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化推举选票 + */ + try { + if(getPeerState() == ServerState.LOOKING) { + currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch()); + } + // TODO_MA 注释: 当开始选举的时候,这个票的信息,时候自己的信息 + // TODO_MA 注释: 当选举结束的是偶,这个票的信息,就是被推举成为leader的节点信息 + } catch(IOException e) { + RuntimeException re = new RuntimeException(e.getMessage()); + re.setStackTrace(e.getStackTrace()); + throw re; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建选举算法实例 + * electionType = 默认3 = FastLeaderElection + */ + this.electionAlg = createElectionAlgorithm(electionType); + } + + private void startJvmPauseMonitor() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + if(this.jvmPauseMonitor != null) { + this.jvmPauseMonitor.serviceStart(); + } + } + + /** + * Count the number of nodes in the map that could be followers. + * + * @param peers + * @return The number of followers in the map + */ + protected static int countParticipants(Map peers) { + int count = 0; + for(QuorumServer q : peers.values()) { + if(q.type == LearnerType.PARTICIPANT) { + count++; + } + } + return count; + } + + /** + * This constructor is only used by the existing unit test code. + * It defaults to FileLogProvider persistence provider. + */ + public QuorumPeer(Map quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, + long myid, int tickTime, int initLimit, int syncLimit, int connectToLearnerMasterLimit) throws IOException { + this(quorumPeers, snapDir, logDir, electionAlg, myid, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, + false, ServerCnxnFactory.createFactory(getClientAddress(quorumPeers, myid, clientPort), -1), + new QuorumMaj(quorumPeers)); + } + + /** + * This constructor is only used by the existing unit test code. + * It defaults to FileLogProvider persistence provider. + */ + public QuorumPeer(Map quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, + long myid, int tickTime, int initLimit, int syncLimit, int connectToLearnerMasterLimit, + QuorumVerifier quorumConfig) throws IOException { + this(quorumPeers, snapDir, logDir, electionAlg, myid, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, + false, ServerCnxnFactory.createFactory(getClientAddress(quorumPeers, myid, clientPort), -1), + quorumConfig); + } + + private static InetSocketAddress getClientAddress(Map quorumPeers, long myid, + int clientPort) throws IOException { + QuorumServer quorumServer = quorumPeers.get(myid); + if(null == quorumServer) { + throw new IOException("No QuorumServer correspoding to myid " + myid); + } + if(null == quorumServer.clientAddr) { + return new InetSocketAddress(clientPort); + } + if(quorumServer.clientAddr.getPort() != clientPort) { + throw new IOException("QuorumServer port " + quorumServer.clientAddr + .getPort() + " does not match with given port " + clientPort); + } + return quorumServer.clientAddr; + } + + /** + * returns the highest zxid that this host has seen + * + * @return the highest zxid for this host + */ + public long getLastLoggedZxid() { + if(!zkDb.isInitialized()) { + loadDataBase(); + } + return zkDb.getDataTreeLastProcessedZxid(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 具体的当前服务器的 实例 + * 不是这三个变量都有值, 当选举结束之后,会自动的根据选举结果来创建对应的实例 + */ + public Follower follower; + public Leader leader; + public Observer observer; + + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + // TODO_MA 注释: + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.zkDb)); + } + + protected Leader makeLeader(FileTxnSnapLog logFactory) throws IOException, X509Exception { + // TODO_MA 注释: + return new Leader(this, new LeaderZooKeeperServer(logFactory, this, this.zkDb)); + } + + protected Observer makeObserver(FileTxnSnapLog logFactory) throws IOException { + // TODO_MA 注释: + return new Observer(this, new ObserverZooKeeperServer(logFactory, this, this.zkDb)); + } + + @SuppressWarnings("deprecation") + protected Election createElectionAlgorithm(int electionAlgorithm) { + Election le = null; + + //TODO: use a factory rather than a switch + switch(electionAlgorithm) { + case 1: + throw new UnsupportedOperationException("Election Algorithm 1 is not supported."); + case 2: + throw new UnsupportedOperationException("Election Algorithm 2 is not supported."); + case 3: + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 事实上是创建 QuorumCnxManager.Listener 这个对象 + */ + QuorumCnxManager qcm = createCnxnManager(); + QuorumCnxManager oldQcm = qcmRef.getAndSet(qcm); + if(oldQcm != null) { + LOG.warn("Clobbering already-set QuorumCnxManager (restarting leader election?)"); + oldQcm.halt(); + } + QuorumCnxManager.Listener listener = qcm.listener; + if(listener != null) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 其实就是启动了一个 BIO 的服务端,绑定了选举端口 + * 等待客户端发起链接请求过来 + */ + listener.start(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 初始化了两个线程,两个队列 + */ + FastLeaderElection fle = new FastLeaderElection(this, qcm); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动这两个线程 + */ + fle.start(); + + le = fle; + } else { + LOG.error("Null listener when initializing cnx manager"); + } + break; + default: + assert false; + } + return le; + } + + @SuppressWarnings("deprecation") + protected Election makeLEStrategy() { + LOG.debug("Initializing leader election protocol..."); + return electionAlg; + } + + protected synchronized void setLeader(Leader newLeader) { + leader = newLeader; + } + + protected synchronized void setFollower(Follower newFollower) { + follower = newFollower; + } + + protected synchronized void setObserver(Observer newObserver) { + observer = newObserver; + } + + public synchronized ZooKeeperServer getActiveServer() { + if(leader != null) { + return leader.zk; + } else if(follower != null) { + return follower.zk; + } else if(observer != null) { + return observer.zk; + } + return null; + } + + boolean shuttingDownLE = false; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * + */ + @Override + public void run() { + updateThreadName(); + + LOG.debug("Starting quorum peer"); + try { + jmxQuorumBean = new QuorumBean(this); + MBeanRegistry.getInstance().register(jmxQuorumBean, null); + for(QuorumServer s : getView().values()) { + ZKMBeanInfo p; + if(getId() == s.id) { + p = jmxLocalPeerBean = new LocalPeerBean(this); + try { + MBeanRegistry.getInstance().register(p, jmxQuorumBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxLocalPeerBean = null; + } + } else { + RemotePeerBean rBean = new RemotePeerBean(this, s); + try { + MBeanRegistry.getInstance().register(rBean, jmxQuorumBean); + jmxRemotePeerBean.put(s.id, rBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + } + } + } + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxQuorumBean = null; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + // 进入 ZAB 工作模式 + while(running) { + switch(getPeerState()) { + case LOOKING: + lookForLeader(); + case LEADING: + lead(); + case FOLLOWING: + followerLeader(); + case OBSERVING: + observerLeader(); + } + } + + + */ + + try { + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 进入 ZAB 工作模式 + */ + /* + * Main loop = 进入 ZAB 工作模式 + */ + while(running) { + + // TODO_MA 注释: getPeerState() 就是服务器此时的状态 + switch(getPeerState()) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + case LOOKING: + LOG.info("LOOKING"); + ServerMetrics.getMetrics().LOOKING_COUNT.add(1); + + if(Boolean.getBoolean("readonlymode.enabled")) { + LOG.info("Attempting to start ReadOnlyZooKeeperServer"); + + // Create read-only server but don't start it immediately + final ReadOnlyZooKeeperServer roZk = new ReadOnlyZooKeeperServer(logFactory, this, this.zkDb); + + // Instead of starting roZk immediately, wait some grace + // period before we decide we're partitioned. + // + // Thread is used here because otherwise it would require + // changes in each of election strategy classes which is + // unnecessary code coupling. + Thread roZkMgr = new Thread() { + public void run() { + try { + // lower-bound grace period to 2 secs + sleep(Math.max(2000, tickTime)); + if(ServerState.LOOKING.equals(getPeerState())) { + roZk.startup(); + } + } catch(InterruptedException e) { + LOG.info( + "Interrupted while attempting to start ReadOnlyZooKeeperServer, not started"); + } catch(Exception e) { + LOG.error("FAILED to start ReadOnlyZooKeeperServer", e); + } + } + }; + try { + roZkMgr.start(); + reconfigFlagClear(); + if(shuttingDownLE) { + shuttingDownLE = false; + startLeaderElection(); + } + setCurrentVote(makeLEStrategy().lookForLeader()); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + setPeerState(ServerState.LOOKING); + } finally { + // If the thread is in the the grace period, interrupt + // to come out of waiting. + roZkMgr.interrupt(); + roZk.shutdown(); + } + } else { + try { + reconfigFlagClear(); + if(shuttingDownLE) { + shuttingDownLE = false; + startLeaderElection(); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 开启选举 + * 1、makeLEStrategy() = 获取选举算法实例对象: FastLeaderElection + * 2、FastLeaderElection.lookForLeader() 执行选举 + * 3、Vote endVote = FastLeaderElection.lookForLeader() + * 选举结束之后: 这个 Vote 对象,会存储被推举为leader 的这个leader节点的信息 + * 选票PK 的规则: epoch大 ==> zxid大 ==> myid大 + */ + setCurrentVote(makeLEStrategy().lookForLeader()); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + setPeerState(ServerState.LOOKING); + } + } + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 当选举结束,能找到 leader 的时候,observer 节点,成为 Observer 角色 + */ + case OBSERVING: + try { + LOG.info("OBSERVING"); + setObserver(makeObserver(logFactory)); + observer.observeLeader(); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + } finally { + observer.shutdown(); + setObserver(null); + updateServerState(); + + // Add delay jitter before we switch to LOOKING + // state to reduce the load of ObserverMaster + if(isRunning()) { + Observer.waitForObserverElectionDelay(); + } + } + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 当选举结束,能找到 leader 的时候, 没有选举成功的节点,成为 follower 角色 + */ + case FOLLOWING: + try { + LOG.info("FOLLOWING"); + setFollower(makeFollower(logFactory)); + follower.followLeader(); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + } finally { + follower.shutdown(); + setFollower(null); + updateServerState(); + } + break; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 当选举结束, 有一个节点成为 leader 角色,进入领导状态,主持状态同步 + */ + case LEADING: + LOG.info("LEADING"); + try { + setLeader(makeLeader(logFactory)); + leader.lead(); + setLeader(null); + } catch(Exception e) { + LOG.warn("Unexpected exception", e); + } finally { + if(leader != null) { + leader.shutdown("Forcing shutdown"); + setLeader(null); + } + updateServerState(); + } + break; + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 如果退出了这个循环,意味着 QuorumPeer 要被关掉了 + */ + } finally { + LOG.warn("QuorumPeer main thread exited"); + MBeanRegistry instance = MBeanRegistry.getInstance(); + instance.unregister(jmxQuorumBean); + instance.unregister(jmxLocalPeerBean); + + for(RemotePeerBean remotePeerBean : jmxRemotePeerBean.values()) { + instance.unregister(remotePeerBean); + } + + jmxQuorumBean = null; + jmxLocalPeerBean = null; + jmxRemotePeerBean = null; + } + } + + private synchronized void updateServerState() { + if(!reconfigFlag) { + setPeerState(ServerState.LOOKING); + LOG.warn("PeerState set to LOOKING"); + return; + } + + if(getId() == getCurrentVote().getId()) { + setPeerState(ServerState.LEADING); + LOG.debug("PeerState set to LEADING"); + } else if(getLearnerType() == LearnerType.PARTICIPANT) { + setPeerState(ServerState.FOLLOWING); + LOG.debug("PeerState set to FOLLOWING"); + } else if(getLearnerType() == LearnerType.OBSERVER) { + setPeerState(ServerState.OBSERVING); + LOG.debug("PeerState set to OBSERVER"); + } else { // currently shouldn't happen since there are only 2 learner types + setPeerState(ServerState.LOOKING); + LOG.debug("Should not be here"); + } + reconfigFlag = false; + } + + public void shutdown() { + running = false; + x509Util.close(); + if(leader != null) { + leader.shutdown("quorum Peer shutdown"); + } + if(follower != null) { + follower.shutdown(); + } + shutdownServerCnxnFactory(); + if(udpSocket != null) { + udpSocket.close(); + } + if(jvmPauseMonitor != null) { + jvmPauseMonitor.serviceStop(); + } + + try { + adminServer.shutdown(); + } catch(AdminServerException e) { + LOG.warn("Problem stopping AdminServer", e); + } + + if(getElectionAlg() != null) { + this.interrupt(); + getElectionAlg().shutdown(); + } + try { + zkDb.close(); + } catch(IOException ie) { + LOG.warn("Error closing logs ", ie); + } + } + + /** + * A 'view' is a node's current opinion of the membership of the entire + * ensemble. + */ + public Map getView() { + + // TODO_MA 注释: QuorumVerifier = QuorumMaj + return Collections.unmodifiableMap(getQuorumVerifier().getAllMembers()); + } + + /** + * Observers are not contained in this view, only nodes with + * PeerType=PARTICIPANT. + */ + public Map getVotingView() { + return getQuorumVerifier().getVotingMembers(); + } + + /** + * Returns only observers, no followers. + */ + public Map getObservingView() { + return getQuorumVerifier().getObservingMembers(); + } + + public synchronized Set getCurrentAndNextConfigVoters() { + Set voterIds = new HashSet(getQuorumVerifier().getVotingMembers().keySet()); + if(getLastSeenQuorumVerifier() != null) { + voterIds.addAll(getLastSeenQuorumVerifier().getVotingMembers().keySet()); + } + return voterIds; + } + + /** + * Check if a node is in the current view. With static membership, the + * result of this check will never change; only when dynamic membership + * is introduced will this be more useful. + */ + public boolean viewContains(Long sid) { + return this.getView().containsKey(sid); + } + + /** + * Only used by QuorumStats at the moment + */ + public String[] getQuorumPeers() { + List l = new ArrayList(); + synchronized(this) { + if(leader != null) { + for(LearnerHandler fh : leader.getLearners()) { + if(fh.getSocket() != null) { + String s = formatInetAddr((InetSocketAddress) fh.getSocket().getRemoteSocketAddress()); + if(leader.isLearnerSynced(fh)) { + s += "*"; + } + l.add(s); + } + } + } else if(follower != null) { + l.add(formatInetAddr((InetSocketAddress) follower.sock.getRemoteSocketAddress())); + } + } + return l.toArray(new String[0]); + } + + public String getServerState() { + switch(getPeerState()) { + case LOOKING: + return QuorumStats.Provider.LOOKING_STATE; + case LEADING: + return QuorumStats.Provider.LEADING_STATE; + case FOLLOWING: + return QuorumStats.Provider.FOLLOWING_STATE; + case OBSERVING: + return QuorumStats.Provider.OBSERVING_STATE; + } + return QuorumStats.Provider.UNKNOWN_STATE; + } + + /** + * set the id of this quorum peer. + */ + public void setMyid(long myid) { + this.myid = myid; + } + + public void setInitialConfig(String initialConfig) { + this.initialConfig = initialConfig; + } + + public String getInitialConfig() { + return initialConfig; + } + + /** + * Get the number of milliseconds of each tick + */ + public int getTickTime() { + return tickTime; + } + + /** + * Set the number of milliseconds of each tick + */ + public void setTickTime(int tickTime) { + LOG.info("tickTime set to {}", tickTime); + this.tickTime = tickTime; + } + + /** + * Maximum number of connections allowed from particular host (ip) + */ + public int getMaxClientCnxnsPerHost() { + if(cnxnFactory != null) { + return cnxnFactory.getMaxClientCnxnsPerHost(); + } + if(secureCnxnFactory != null) { + return secureCnxnFactory.getMaxClientCnxnsPerHost(); + } + return -1; + } + + /** + * Whether local sessions are enabled + */ + public boolean areLocalSessionsEnabled() { + return localSessionsEnabled; + } + + /** + * Whether to enable local sessions + */ + public void enableLocalSessions(boolean flag) { + LOG.info("Local sessions {}", (flag ? "enabled" : "disabled")); + localSessionsEnabled = flag; + } + + /** + * Whether local sessions are allowed to upgrade to global sessions + */ + public boolean isLocalSessionsUpgradingEnabled() { + return localSessionsUpgradingEnabled; + } + + /** + * Whether to allow local sessions to upgrade to global sessions + */ + public void enableLocalSessionsUpgrading(boolean flag) { + LOG.info("Local session upgrading {}", (flag ? "enabled" : "disabled")); + localSessionsUpgradingEnabled = flag; + } + + /** + * minimum session timeout in milliseconds + */ + public int getMinSessionTimeout() { + return minSessionTimeout; + } + + /** + * minimum session timeout in milliseconds + */ + public void setMinSessionTimeout(int min) { + LOG.info("minSessionTimeout set to {}", min); + this.minSessionTimeout = min; + } + + /** + * maximum session timeout in milliseconds + */ + public int getMaxSessionTimeout() { + return maxSessionTimeout; + } + + /** + * maximum session timeout in milliseconds + */ + public void setMaxSessionTimeout(int max) { + LOG.info("maxSessionTimeout set to {}", max); + this.maxSessionTimeout = max; + } + + /** + * The server socket's listen backlog length + */ + public int getClientPortListenBacklog() { + return this.clientPortListenBacklog; + } + + /** + * Sets the server socket's listen backlog length. + */ + public void setClientPortListenBacklog(int backlog) { + this.clientPortListenBacklog = backlog; + } + + /** + * Get the number of ticks that the initial synchronization phase can take + */ + public int getInitLimit() { + return initLimit; + } + + /** + * Set the number of ticks that the initial synchronization phase can take + */ + public void setInitLimit(int initLimit) { + LOG.info("initLimit set to {}", initLimit); + this.initLimit = initLimit; + } + + /** + * Get the current tick + */ + public int getTick() { + return tick.get(); + } + + public QuorumVerifier configFromString(String s) throws IOException, ConfigException { + Properties props = new Properties(); + props.load(new StringReader(s)); + return QuorumPeerConfig.parseDynamicConfig(props, electionType, false, false); + } + + /** + * Return QuorumVerifier object for the last committed configuration. + */ + public QuorumVerifier getQuorumVerifier() { + synchronized(QV_LOCK) { + return quorumVerifier; + } + } + + /** + * Return QuorumVerifier object for the last proposed configuration. + */ + public QuorumVerifier getLastSeenQuorumVerifier() { + synchronized(QV_LOCK) { + return lastSeenQuorumVerifier; + } + } + + public synchronized void restartLeaderElection(QuorumVerifier qvOLD, QuorumVerifier qvNEW) { + if(qvOLD == null || !qvOLD.equals(qvNEW)) { + LOG.warn("Restarting Leader Election"); + getElectionAlg().shutdown(); + shuttingDownLE = false; + startLeaderElection(); + } + } + + public String getNextDynamicConfigFilename() { + if(configFilename == null) { + LOG.warn("configFilename is null! This should only happen in tests."); + return null; + } + return configFilename + QuorumPeerConfig.nextDynamicConfigFileSuffix; + } + + // On entry to this method, qcm must be non-null and the locks on both qcm and QV_LOCK + // must be held. We don't want quorumVerifier/lastSeenQuorumVerifier to change out from + // under us, so we have to hold QV_LOCK; and since the call to qcm.connectOne() will take + // the lock on qcm (and take QV_LOCK again inside that), the caller needs to have taken + // qcm outside QV_LOCK to avoid a deadlock against other callers of qcm.connectOne(). + private void connectNewPeers(QuorumCnxManager qcm) { + if(quorumVerifier != null && lastSeenQuorumVerifier != null) { + Map committedView = quorumVerifier.getAllMembers(); + for(Entry e : lastSeenQuorumVerifier.getAllMembers().entrySet()) { + if(e.getKey() != getId() && !committedView.containsKey(e.getKey())) { + qcm.connectOne(e.getKey()); + } + } + } + } + + public void setLastSeenQuorumVerifier(QuorumVerifier qv, boolean writeToDisk) { + if(!isReconfigEnabled()) { + LOG.info("Dynamic reconfig is disabled, we don't store the last seen config."); + return; + } + + // If qcm is non-null, we may call qcm.connectOne(), which will take the lock on qcm + // and then take QV_LOCK. Take the locks in the same order to ensure that we don't + // deadlock against other callers of connectOne(). If qcmRef gets set in another + // thread while we're inside the synchronized block, that does no harm; if we didn't + // take a lock on qcm (because it was null when we sampled it), we won't call + // connectOne() on it. (Use of an AtomicReference is enough to guarantee visibility + // of updates that provably happen in another thread before entering this method.) + QuorumCnxManager qcm = qcmRef.get(); + Object outerLockObject = (qcm != null) ? qcm : QV_LOCK; + synchronized(outerLockObject) { + synchronized(QV_LOCK) { + if(lastSeenQuorumVerifier != null && lastSeenQuorumVerifier.getVersion() > qv.getVersion()) { + LOG.error("setLastSeenQuorumVerifier called with stale config " + qv + .getVersion() + ". Current version: " + quorumVerifier.getVersion()); + } + // assuming that a version uniquely identifies a configuration, so if + // version is the same, nothing to do here. + if(lastSeenQuorumVerifier != null && lastSeenQuorumVerifier.getVersion() == qv.getVersion()) { + return; + } + lastSeenQuorumVerifier = qv; + if(qcm != null) { + connectNewPeers(qcm); + } + + if(writeToDisk) { + try { + String fileName = getNextDynamicConfigFilename(); + if(fileName != null) { + QuorumPeerConfig.writeDynamicConfig(fileName, qv, true); + } + } catch(IOException e) { + LOG.error("Error writing next dynamic config file to disk", e); + } + } + } + } + } + + public QuorumVerifier setQuorumVerifier(QuorumVerifier qv, boolean writeToDisk) { + synchronized(QV_LOCK) { + if((quorumVerifier != null) && (quorumVerifier.getVersion() >= qv.getVersion())) { + // this is normal. For example - server found out about new config through FastLeaderElection gossiping + // and then got the same config in UPTODATE message so its already known + LOG.debug("{} setQuorumVerifier called with known or old config {}. Current version: {}", getId(), + qv.getVersion(), quorumVerifier.getVersion()); + return quorumVerifier; + } + QuorumVerifier prevQV = quorumVerifier; + quorumVerifier = qv; + if(lastSeenQuorumVerifier == null || (qv.getVersion() > lastSeenQuorumVerifier.getVersion())) { + lastSeenQuorumVerifier = qv; + } + + if(writeToDisk) { + // some tests initialize QuorumPeer without a static config file + if(configFilename != null) { + try { + String dynamicConfigFilename = makeDynamicConfigFilename(qv.getVersion()); + QuorumPeerConfig.writeDynamicConfig(dynamicConfigFilename, qv, false); + QuorumPeerConfig.editStaticConfig(configFilename, dynamicConfigFilename, + needEraseClientInfoFromStaticConfig()); + } catch(IOException e) { + LOG.error("Error closing file", e); + } + } else { + LOG.info("writeToDisk == true but configFilename == null"); + } + } + + if(qv.getVersion() == lastSeenQuorumVerifier.getVersion()) { + QuorumPeerConfig.deleteFile(getNextDynamicConfigFilename()); + } + QuorumServer qs = qv.getAllMembers().get(getId()); + if(qs != null) { + setAddrs(qs.addr, qs.electionAddr, qs.clientAddr); + } + updateObserverMasterList(); + return prevQV; + } + } + + private String makeDynamicConfigFilename(long version) { + return configFilename + ".dynamic." + Long.toHexString(version); + } + + private boolean needEraseClientInfoFromStaticConfig() { + QuorumServer server = quorumVerifier.getAllMembers().get(getId()); + return (server != null && server.clientAddr != null && !server.isClientAddrFromStatic); + } + + /** + * Get an instance of LeaderElection + */ + public Election getElectionAlg() { + return electionAlg; + } + + /** + * Get the synclimit + */ + public int getSyncLimit() { + return syncLimit; + } + + /** + * Set the synclimit + */ + public void setSyncLimit(int syncLimit) { + LOG.info("syncLimit set to {}", syncLimit); + this.syncLimit = syncLimit; + } + + /** + * Get the connectToLearnerMasterLimit + */ + public int getConnectToLearnerMasterLimit() { + return connectToLearnerMasterLimit; + } + + /** + * Set the connectToLearnerMasterLimit + */ + public void setConnectToLearnerMasterLimit(int connectToLearnerMasterLimit) { + LOG.info("connectToLearnerMasterLimit set to {}", connectToLearnerMasterLimit); + this.connectToLearnerMasterLimit = connectToLearnerMasterLimit; + } + + /** + * The syncEnabled can also be set via a system property. + */ + public static final String SYNC_ENABLED = "zookeeper.observer.syncEnabled"; + + /** + * Return syncEnabled. + * + * @return + */ + public boolean getSyncEnabled() { + if(System.getProperty(SYNC_ENABLED) != null) { + LOG.info("{}={}", SYNC_ENABLED, Boolean.getBoolean(SYNC_ENABLED)); + return Boolean.getBoolean(SYNC_ENABLED); + } else { + return syncEnabled; + } + } + + /** + * Set syncEnabled. + * + * @param syncEnabled + */ + public void setSyncEnabled(boolean syncEnabled) { + this.syncEnabled = syncEnabled; + } + + /** + * Gets the election type + */ + public int getElectionType() { + return electionType; + } + + /** + * Sets the election type + */ + public void setElectionType(int electionType) { + this.electionType = electionType; + } + + public boolean getQuorumListenOnAllIPs() { + return quorumListenOnAllIPs; + } + + public void setQuorumListenOnAllIPs(boolean quorumListenOnAllIPs) { + this.quorumListenOnAllIPs = quorumListenOnAllIPs; + } + + public void setCnxnFactory(ServerCnxnFactory cnxnFactory) { + this.cnxnFactory = cnxnFactory; + } + + public void setSecureCnxnFactory(ServerCnxnFactory secureCnxnFactory) { + this.secureCnxnFactory = secureCnxnFactory; + } + + public void setSslQuorum(boolean sslQuorum) { + if(sslQuorum) { + LOG.info("Using TLS encrypted quorum communication"); + } else { + LOG.info("Using insecure (non-TLS) quorum communication"); + } + this.sslQuorum = sslQuorum; + } + + public void setUsePortUnification(boolean shouldUsePortUnification) { + LOG.info("Port unification {}", shouldUsePortUnification ? "enabled" : "disabled"); + this.shouldUsePortUnification = shouldUsePortUnification; + } + + private void startServerCnxnFactory() { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动 NIOServerCnxnFactory 中的所有线程 + */ + if(cnxnFactory != null) { + cnxnFactory.start(); + } + if(secureCnxnFactory != null) { + secureCnxnFactory.start(); + } + } + + private void shutdownServerCnxnFactory() { + if(cnxnFactory != null) { + cnxnFactory.shutdown(); + } + if(secureCnxnFactory != null) { + secureCnxnFactory.shutdown(); + } + } + + // Leader and learner will control the zookeeper server and pass it into QuorumPeer. + public void setZooKeeperServer(ZooKeeperServer zks) { + if(cnxnFactory != null) { + cnxnFactory.setZooKeeperServer(zks); + } + if(secureCnxnFactory != null) { + secureCnxnFactory.setZooKeeperServer(zks); + } + } + + public void closeAllConnections() { + if(cnxnFactory != null) { + cnxnFactory.closeAll(ServerCnxn.DisconnectReason.SERVER_SHUTDOWN); + } + if(secureCnxnFactory != null) { + secureCnxnFactory.closeAll(ServerCnxn.DisconnectReason.SERVER_SHUTDOWN); + } + } + + public int getClientPort() { + if(cnxnFactory != null) { + return cnxnFactory.getLocalPort(); + } + return -1; + } + + public void setTxnFactory(FileTxnSnapLog factory) { + this.logFactory = factory; + } + + public FileTxnSnapLog getTxnFactory() { + return this.logFactory; + } + + /** + * set zk database for this node + * + * @param database + */ + public void setZKDatabase(ZKDatabase database) { + this.zkDb = database; + } + + protected ZKDatabase getZkDb() { + return zkDb; + } + + public synchronized void initConfigInZKDatabase() { + if(zkDb != null) { + zkDb.initConfigInZKDatabase(getQuorumVerifier()); + } + } + + public boolean isRunning() { + return running; + } + + /** + * get reference to QuorumCnxManager + */ + public QuorumCnxManager getQuorumCnxManager() { + return qcmRef.get(); + } + + private long readLongFromFile(String name) throws IOException { + File file = new File(logFactory.getSnapDir(), name); + BufferedReader br = new BufferedReader(new FileReader(file)); + String line = ""; + try { + line = br.readLine(); + return Long.parseLong(line); + } catch(NumberFormatException e) { + throw new IOException("Found " + line + " in " + file); + } finally { + br.close(); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + private long acceptedEpoch = -1; + private long currentEpoch = -1; + + public static final String CURRENT_EPOCH_FILENAME = "currentEpoch"; + + public static final String ACCEPTED_EPOCH_FILENAME = "acceptedEpoch"; + + /** + * Write a long value to disk atomically. Either succeeds or an exception + * is thrown. + * + * @param name file name to write the long to + * @param value the long value to write to the named file + * @throws IOException if the file cannot be written atomically + */ + // visibleForTest + void writeLongToFile(String name, final long value) throws IOException { + File file = new File(logFactory.getSnapDir(), name); + new AtomicFileWritingIdiom(file, new WriterStatement() { + @Override + public void write(Writer bw) throws IOException { + bw.write(Long.toString(value)); + } + }); + } + + public long getCurrentEpoch() throws IOException { + if(currentEpoch == -1) { + currentEpoch = readLongFromFile(CURRENT_EPOCH_FILENAME); + } + return currentEpoch; + } + + public long getAcceptedEpoch() throws IOException { + if(acceptedEpoch == -1) { + acceptedEpoch = readLongFromFile(ACCEPTED_EPOCH_FILENAME); + } + return acceptedEpoch; + } + + public void setCurrentEpoch(long e) throws IOException { + writeLongToFile(CURRENT_EPOCH_FILENAME, e); + currentEpoch = e; + + } + + public void setAcceptedEpoch(long e) throws IOException { + writeLongToFile(ACCEPTED_EPOCH_FILENAME, e); + acceptedEpoch = e; + } + + public boolean processReconfig(QuorumVerifier qv, Long suggestedLeaderId, Long zxid, boolean restartLE) { + if(!isReconfigEnabled()) { + LOG.debug("Reconfig feature is disabled, skip reconfig processing."); + return false; + } + + InetSocketAddress oldClientAddr = getClientAddress(); + + // update last committed quorum verifier, write the new config to disk + // and restart leader election if config changed. + QuorumVerifier prevQV = setQuorumVerifier(qv, true); + + // There is no log record for the initial config, thus after syncing + // with leader + // /zookeeper/config is empty! it is also possible that last committed + // config is propagated during leader election + // without the propagation the corresponding log records. + // so we should explicitly do this (this is not necessary when we're + // already a Follower/Observer, only + // for Learner): + initConfigInZKDatabase(); + + if(prevQV.getVersion() < qv.getVersion() && !prevQV.equals(qv)) { + Map newMembers = qv.getAllMembers(); + updateRemotePeerMXBeans(newMembers); + if(restartLE) { + restartLeaderElection(prevQV, qv); + } + + QuorumServer myNewQS = newMembers.get(getId()); + if(myNewQS != null && myNewQS.clientAddr != null && !myNewQS.clientAddr.equals(oldClientAddr)) { + cnxnFactory.reconfigure(myNewQS.clientAddr); + updateThreadName(); + } + + boolean roleChange = updateLearnerType(qv); + boolean leaderChange = false; + if(suggestedLeaderId != null) { + // zxid should be non-null too + leaderChange = updateVote(suggestedLeaderId, zxid); + } else { + long currentLeaderId = getCurrentVote().getId(); + QuorumServer myleaderInCurQV = prevQV.getVotingMembers().get(currentLeaderId); + QuorumServer myleaderInNewQV = qv.getVotingMembers().get(currentLeaderId); + leaderChange = (myleaderInCurQV == null || myleaderInCurQV.addr == null || myleaderInNewQV == null || !myleaderInCurQV.addr + .equals(myleaderInNewQV.addr)); + // we don't have a designated leader - need to go into leader + // election + reconfigFlagClear(); + } + + return roleChange || leaderChange; + } + return false; + + } + + private void updateRemotePeerMXBeans(Map newMembers) { + Set existingMembers = new HashSet(newMembers.keySet()); + existingMembers.retainAll(jmxRemotePeerBean.keySet()); + for(Long id : existingMembers) { + RemotePeerBean rBean = jmxRemotePeerBean.get(id); + rBean.setQuorumServer(newMembers.get(id)); + } + + Set joiningMembers = new HashSet(newMembers.keySet()); + joiningMembers.removeAll(jmxRemotePeerBean.keySet()); + joiningMembers.remove(getId()); // remove self as it is local bean + for(Long id : joiningMembers) { + QuorumServer qs = newMembers.get(id); + RemotePeerBean rBean = new RemotePeerBean(this, qs); + try { + MBeanRegistry.getInstance().register(rBean, jmxQuorumBean); + jmxRemotePeerBean.put(qs.id, rBean); + } catch(Exception e) { + LOG.warn("Failed to register with JMX", e); + } + } + + Set leavingMembers = new HashSet(jmxRemotePeerBean.keySet()); + leavingMembers.removeAll(newMembers.keySet()); + for(Long id : leavingMembers) { + RemotePeerBean rBean = jmxRemotePeerBean.remove(id); + try { + MBeanRegistry.getInstance().unregister(rBean); + } catch(Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + } + } + + private ArrayList observerMasters = new ArrayList<>(); + + private void updateObserverMasterList() { + if(observerMasterPort <= 0) { + return; // observer masters not enabled + } + observerMasters.clear(); + StringBuilder sb = new StringBuilder(); + for(QuorumServer server : quorumVerifier.getVotingMembers().values()) { + InetAddress address = server.addr.getReachableOrOne().getAddress(); + InetSocketAddress addr = new InetSocketAddress(address, observerMasterPort); + observerMasters.add(new QuorumServer(server.id, addr)); + sb.append(addr).append(","); + } + LOG.info("Updated learner master list to be {}", sb.toString()); + Collections.shuffle(observerMasters); + // Reset the internal index of the observerMaster when + // the observerMaster List is refreshed + nextObserverMaster = 0; + } + + private boolean useObserverMasters() { + return getLearnerType() == LearnerType.OBSERVER && observerMasters.size() > 0; + } + + private int nextObserverMaster = 0; + + private QuorumServer nextObserverMaster() { + if(nextObserverMaster >= observerMasters.size()) { + nextObserverMaster = 0; + // Add a reconnect delay only after the observer + // has exhausted trying to connect to all the masters + // from the observerMasterList + if(isRunning()) { + Observer.waitForReconnectDelay(); + } + } + return observerMasters.get(nextObserverMaster++); + } + + QuorumServer findLearnerMaster(QuorumServer leader) { + if(useObserverMasters()) { + return nextObserverMaster(); + } else { + // Add delay jitter to reduce the load on the leader + if(isRunning()) { + Observer.waitForReconnectDelay(); + } + return leader; + } + } + + /** + * Vet a given learner master's information. + * Allows specification by server id, ip only, or ip and port + */ + QuorumServer validateLearnerMaster(String desiredMaster) { + if(useObserverMasters()) { + Long sid; + try { + sid = Long.parseLong(desiredMaster); + } catch(NumberFormatException e) { + sid = null; + } + for(QuorumServer server : observerMasters) { + if(sid == null) { + for(InetSocketAddress address : server.addr.getAllAddresses()) { + String serverAddr = address.getAddress().getHostAddress() + ':' + address.getPort(); + if(serverAddr.startsWith(desiredMaster)) { + return server; + } + } + } else { + if(sid.equals(server.id)) { + return server; + } + } + } + if(sid == null) { + LOG.info("could not find learner master address={}", desiredMaster); + } else { + LOG.warn("could not find learner master sid={}", sid); + } + } else { + LOG.info("cannot validate request, observer masters not enabled"); + } + return null; + } + + private boolean updateLearnerType(QuorumVerifier newQV) { + //check if I'm an observer in new config + if(newQV.getObservingMembers().containsKey(getId())) { + if(getLearnerType() != LearnerType.OBSERVER) { + setLearnerType(LearnerType.OBSERVER); + LOG.info("Becoming an observer"); + reconfigFlagSet(); + return true; + } else { + return false; + } + } else if(newQV.getVotingMembers().containsKey(getId())) { + if(getLearnerType() != LearnerType.PARTICIPANT) { + setLearnerType(LearnerType.PARTICIPANT); + LOG.info("Becoming a voting participant"); + reconfigFlagSet(); + return true; + } else { + return false; + } + } + // I'm not in the view + if(getLearnerType() != LearnerType.PARTICIPANT) { + setLearnerType(LearnerType.PARTICIPANT); + LOG.info("Becoming a non-voting participant"); + reconfigFlagSet(); + return true; + } + return false; + } + + private boolean updateVote(long designatedLeader, long zxid) { + Vote currentVote = getCurrentVote(); + if(currentVote != null && designatedLeader != currentVote.getId()) { + setCurrentVote(new Vote(designatedLeader, zxid)); + reconfigFlagSet(); + LOG.warn("Suggested leader: {}", designatedLeader); + return true; + } + return false; + } + + /** + * Updates leader election info to avoid inconsistencies when + * a new server tries to join the ensemble. + * + * Here is the inconsistency scenario we try to solve by updating the peer + * epoch after following leader: + * + * Let's say we have an ensemble with 3 servers z1, z2 and z3. + * + * 1. z1, z2 were following z3 with peerEpoch to be 0xb8, the new epoch is + * 0xb9, aka current accepted epoch on disk. + * 2. z2 get restarted, which will use 0xb9 as it's peer epoch when loading + * the current accept epoch from disk. + * 3. z2 received notification from z1 and z3, which is following z3 with + * epoch 0xb8, so it started following z3 again with peer epoch 0xb8. + * 4. before z2 successfully connected to z3, z3 get restarted with new + * epoch 0xb9. + * 5. z2 will retry around a few round (default 5s) before giving up, + * meanwhile it will report z3 as leader. + * 6. z1 restarted, and looking with peer epoch 0xb9. + * 7. z1 voted z3, and z3 was elected as leader again with peer epoch 0xb9. + * 8. z2 successfully connected to z3 before giving up, but with peer + * epoch 0xb8. + * 9. z1 get restarted, looking for leader with peer epoch 0xba, but cannot + * join, because z2 is reporting peer epoch 0xb8, while z3 is reporting + * 0xb9. + * + * By updating the election vote after actually following leader, we can + * avoid this kind of stuck happened. + * + * Btw, the zxid and electionEpoch could be inconsistent because of the same + * reason, it's better to update these as well after syncing with leader, but + * that required protocol change which is non trivial. This problem is worked + * around by skipping comparing the zxid and electionEpoch when counting for + * votes for out of election servers during looking for leader. + * + * See https://issues.apache.org/jira/browse/ZOOKEEPER-1732 + */ + protected void updateElectionVote(long newEpoch) { + Vote currentVote = getCurrentVote(); + if(currentVote != null) { + setCurrentVote(new Vote(currentVote.getId(), currentVote.getZxid(), currentVote.getElectionEpoch(), newEpoch, + currentVote.getState())); + } + } + + private void updateThreadName() { + String plain = cnxnFactory != null ? cnxnFactory.getLocalAddress() != null ? formatInetAddr( + cnxnFactory.getLocalAddress()) : "disabled" : "disabled"; + String secure = secureCnxnFactory != null ? formatInetAddr(secureCnxnFactory.getLocalAddress()) : "disabled"; + setName(String.format("QuorumPeer[myid=%d](plain=%s)(secure=%s)", getId(), plain, secure)); + } + + /** + * Sets the time taken for leader election in milliseconds. + * + * @param electionTimeTaken time taken for leader election + */ + void setElectionTimeTaken(long electionTimeTaken) { + this.electionTimeTaken = electionTimeTaken; + } + + /** + * @return the time taken for leader election in milliseconds. + */ + long getElectionTimeTaken() { + return electionTimeTaken; + } + + void setQuorumServerSaslRequired(boolean serverSaslRequired) { + quorumServerSaslAuthRequired = serverSaslRequired; + LOG.info("{} set to {}", QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, serverSaslRequired); + } + + void setQuorumLearnerSaslRequired(boolean learnerSaslRequired) { + quorumLearnerSaslAuthRequired = learnerSaslRequired; + LOG.info("{} set to {}", QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, learnerSaslRequired); + } + + void setQuorumSaslEnabled(boolean enableAuth) { + quorumSaslEnableAuth = enableAuth; + if(!quorumSaslEnableAuth) { + LOG.info("QuorumPeer communication is not secured! (SASL auth disabled)"); + } else { + LOG.info("{} set to {}", QuorumAuth.QUORUM_SASL_AUTH_ENABLED, enableAuth); + } + } + + void setQuorumServicePrincipal(String servicePrincipal) { + quorumServicePrincipal = servicePrincipal; + LOG.info("{} set to {}", QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, quorumServicePrincipal); + } + + void setQuorumLearnerLoginContext(String learnerContext) { + quorumLearnerLoginContext = learnerContext; + LOG.info("{} set to {}", QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, quorumLearnerLoginContext); + } + + void setQuorumServerLoginContext(String serverContext) { + quorumServerLoginContext = serverContext; + LOG.info("{} set to {}", QuorumAuth.QUORUM_SERVER_SASL_LOGIN_CONTEXT, quorumServerLoginContext); + } + + void setQuorumCnxnThreadsSize(int qCnxnThreadsSize) { + if(qCnxnThreadsSize > QUORUM_CNXN_THREADS_SIZE_DEFAULT_VALUE) { + quorumCnxnThreadsSize = qCnxnThreadsSize; + } + LOG.info("quorum.cnxn.threads.size set to {}", quorumCnxnThreadsSize); + } + + boolean isQuorumSaslAuthEnabled() { + return quorumSaslEnableAuth; + } + + private boolean isQuorumServerSaslAuthRequired() { + return quorumServerSaslAuthRequired; + } + + private boolean isQuorumLearnerSaslAuthRequired() { + return quorumLearnerSaslAuthRequired; + } + + public QuorumCnxManager createCnxnManager() { + int timeout = quorumCnxnTimeoutMs > 0 ? quorumCnxnTimeoutMs : this.tickTime * this.syncLimit; + LOG.info("Using {}ms as the quorum cnxn socket timeout", timeout); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return new QuorumCnxManager(this, this.getId(), this.getView(), this.authServer, this.authLearner, timeout, + this.getQuorumListenOnAllIPs(), this.quorumCnxnThreadsSize, this.isQuorumSaslAuthEnabled()); + } + + boolean isLeader(long id) { + Vote vote = getCurrentVote(); + return vote != null && id == vote.getId(); + } + + public boolean isReconfigEnabled() { + return reconfigEnabled; + } + + @InterfaceAudience.Private + /** + * This is a metric that depends on the status of the peer. + */ public Integer getSynced_observers_metric() { + if(leader != null) { + return leader.getObservingLearners().size(); + } else if(follower != null) { + return follower.getSyncedObserverSize(); + } else { + return null; + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java new file mode 100644 index 0000000..9e50d8f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java @@ -0,0 +1,1126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.common.NetUtils.formatInetAddr; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; +import java.io.Writer; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.common.AtomicFileWritingIdiom; +import org.apache.zookeeper.common.AtomicFileWritingIdiom.OutputStreamStatement; +import org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.common.StringUtils; +import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.auth.QuorumAuth; +import org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.JvmPauseMonitor; +import org.apache.zookeeper.server.util.VerifyingFileFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +@InterfaceAudience.Public +public class QuorumPeerConfig { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumPeerConfig.class); + private static final int UNSET_SERVERID = -1; + public static final String nextDynamicConfigFileSuffix = ".dynamic.next"; + + private static boolean standaloneEnabled = true; + private static boolean reconfigEnabled = false; + + // 基本上,这些成员变量都是从 zoo.cfg 中执行初始化的。 + protected InetSocketAddress clientPortAddress; + protected InetSocketAddress secureClientPortAddress; + protected boolean sslQuorum = false; + protected boolean shouldUsePortUnification = false; + protected int observerMasterPort; + protected boolean sslQuorumReloadCertFiles = false; + protected File dataDir; + protected File dataLogDir; + protected String dynamicConfigFileStr = null; + protected String configFileStr = null; + protected int tickTime = ZooKeeperServer.DEFAULT_TICK_TIME; + protected int maxClientCnxns = 60; + /** + * defaults to -1 if not set explicitly + */ + protected int minSessionTimeout = -1; + /** + * defaults to -1 if not set explicitly + */ + protected int maxSessionTimeout = -1; + protected String metricsProviderClassName = DefaultMetricsProvider.class.getName(); + protected Properties metricsProviderConfiguration = new Properties(); + protected boolean localSessionsEnabled = false; + protected boolean localSessionsUpgradingEnabled = false; + /** + * defaults to -1 if not set explicitly + */ + protected int clientPortListenBacklog = -1; + + protected int initLimit; + protected int syncLimit; + protected int connectToLearnerMasterLimit; + protected int electionAlg = 3; + protected int electionPort = 2182; + protected boolean quorumListenOnAllIPs = false; + + protected long serverId = UNSET_SERVERID; + + protected QuorumVerifier quorumVerifier = null, lastSeenQuorumVerifier = null; + protected int snapRetainCount = 3; + protected int purgeInterval = 0; + protected boolean syncEnabled = true; + + protected String initialConfig; + + protected LearnerType peerType = LearnerType.PARTICIPANT; + + /** + * Configurations for the quorumpeer-to-quorumpeer sasl authentication + */ + protected boolean quorumServerRequireSasl = false; + protected boolean quorumLearnerRequireSasl = false; + protected boolean quorumEnableSasl = false; + protected String quorumServicePrincipal = QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE; + protected String quorumLearnerLoginContext = QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT_DFAULT_VALUE; + protected String quorumServerLoginContext = QuorumAuth.QUORUM_SERVER_SASL_LOGIN_CONTEXT_DFAULT_VALUE; + protected int quorumCnxnThreadsSize; + + // multi address related configs + private boolean multiAddressEnabled = Boolean.parseBoolean( + System.getProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, + QuorumPeer.CONFIG_DEFAULT_MULTI_ADDRESS_ENABLED)); + private boolean multiAddressReachabilityCheckEnabled = Boolean + .parseBoolean(System.getProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_REACHABILITY_CHECK_ENABLED, "true")); + private int multiAddressReachabilityCheckTimeoutMs = Integer.parseInt( + System.getProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_REACHABILITY_CHECK_TIMEOUT_MS, + String.valueOf(MultipleAddresses.DEFAULT_TIMEOUT.toMillis()))); + + /** + * Minimum snapshot retain count. + * + * @see org.apache.zookeeper.server.PurgeTxnLog#purge(File, File, int) + */ + private final int MIN_SNAP_RETAIN_COUNT = 3; + + /** + * JVM Pause Monitor feature switch + */ + protected boolean jvmPauseMonitorToRun = false; + /** + * JVM Pause Monitor warn threshold in ms + */ + protected long jvmPauseWarnThresholdMs = JvmPauseMonitor.WARN_THRESHOLD_DEFAULT; + /** + * JVM Pause Monitor info threshold in ms + */ + protected long jvmPauseInfoThresholdMs = JvmPauseMonitor.INFO_THRESHOLD_DEFAULT; + /** + * JVM Pause Monitor sleep time in ms + */ + protected long jvmPauseSleepTimeMs = JvmPauseMonitor.SLEEP_TIME_MS_DEFAULT; + + @SuppressWarnings("serial") + public static class ConfigException extends Exception { + + public ConfigException(String msg) { + super(msg); + } + + public ConfigException(String msg, Exception e) { + super(msg, e); + } + + } + + /** + * Parse a ZooKeeper configuration file + * + * @param path the patch of the configuration file + * @throws ConfigException error processing configuration + */ + public void parse(String path) throws ConfigException { + LOG.info("Reading configuration from: " + path); + + try { + + // TODO_MA 注释: 把 zoo.cfg 构造一个 file 对象 + File configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath().failForNonExistingPath() + .build()).create(path); + + // TODO_MA 注释: 通过 Properties API 执行 zoo.cfg 的读取 + Properties cfg = new Properties(); + FileInputStream in = new FileInputStream(configFile); + try { + cfg.load(in); + configFileStr = path; + } finally { + in.close(); + } + + /* Read entire config file as initial configuration */ + initialConfig = new String(Files.readAllBytes(configFile.toPath())); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 真正执行解析 + * 1、QuorumPeer 对象中,有很多的成员变量,这些成员变量就是zoo.cfg 中的某些配置的值 + * 2、把 Properties 这个 Map 中的 key-value 解析出来,设置到 QuorumPeer + */ + parseProperties(cfg); + + } catch(IOException e) { + throw new ConfigException("Error processing " + path, e); + } catch(IllegalArgumentException e) { + throw new ConfigException("Error processing " + path, e); + } + + if(dynamicConfigFileStr != null) { + try { + Properties dynamicCfg = new Properties(); + FileInputStream inConfig = new FileInputStream(dynamicConfigFileStr); + try { + dynamicCfg.load(inConfig); + if(dynamicCfg.getProperty("version") != null) { + throw new ConfigException("dynamic file shouldn't have version inside"); + } + + String version = getVersionFromFilename(dynamicConfigFileStr); + // If there isn't any version associated with the filename, + // the default version is 0. + if(version != null) { + dynamicCfg.setProperty("version", version); + } + } finally { + inConfig.close(); + } + setupQuorumPeerConfig(dynamicCfg, false); + + } catch(IOException e) { + throw new ConfigException("Error processing " + dynamicConfigFileStr, e); + } catch(IllegalArgumentException e) { + throw new ConfigException("Error processing " + dynamicConfigFileStr, e); + } + File nextDynamicConfigFile = new File(configFileStr + nextDynamicConfigFileSuffix); + if(nextDynamicConfigFile.exists()) { + try { + Properties dynamicConfigNextCfg = new Properties(); + FileInputStream inConfigNext = new FileInputStream(nextDynamicConfigFile); + try { + dynamicConfigNextCfg.load(inConfigNext); + } finally { + inConfigNext.close(); + } + boolean isHierarchical = false; + for(Entry entry : dynamicConfigNextCfg.entrySet()) { + String key = entry.getKey().toString().trim(); + if(key.startsWith("group") || key.startsWith("weight")) { + isHierarchical = true; + break; + } + } + lastSeenQuorumVerifier = createQuorumVerifier(dynamicConfigNextCfg, isHierarchical); + } catch(IOException e) { + LOG.warn("NextQuorumVerifier is initiated to null"); + } + } + } + } + + // This method gets the version from the end of dynamic file name. + // For example, "zoo.cfg.dynamic.0" returns initial version "0". + // "zoo.cfg.dynamic.1001" returns version of hex number "0x1001". + // If a dynamic file name doesn't have any version at the end of file, + // e.g. "zoo.cfg.dynamic", it returns null. + public static String getVersionFromFilename(String filename) { + int i = filename.lastIndexOf('.'); + if(i < 0 || i >= filename.length()) { + return null; + } + + String hexVersion = filename.substring(i + 1); + try { + long version = Long.parseLong(hexVersion, 16); + return Long.toHexString(version); + } catch(NumberFormatException e) { + return null; + } + } + + /** + * // TODO_MA 注释: 该方法是用来解析 zoo.cfg 配置文件中的内容的。 + * // TODO_MA 注释: 各位可以思考,该方法存在的问题是什么?可以做如何改进? + * Parse config from a Properties. + * + * @param zkProp Properties to parse from. + * @throws IOException + * @throws ConfigException + * + * // TODO_MA 马中华 注释: 数据存储目录 和 端口的解析 + * // TODO_MA 马中华 注释: myid 的解析 + * + * // TODO_MA 马中华 注释: 这个方法的核心逻辑只有一个目标: + * // TODO_MA 马中华 注释: 吧 Properties 对象中的 key-value 解析设置成 QuorumPeerConfig 的成员变量来保存 + * // TODO_MA 马中华 注释: 之后呢,会把 QuorumPeerConfig 中的每个成员变量的值,然后设置到 QuorumPeer 类的成员变量 + * // TODO_MA 马中华 注释: QuorumPeerConfig 就是一个 容器 + */ + public void parseProperties(Properties zkProp) throws IOException, ConfigException { + int clientPort = 0; + int secureClientPort = 0; + int observerMasterPort = 0; + String clientPortAddress = null; + String secureClientPortAddress = null; + VerifyingFileFactory vff = new VerifyingFileFactory.Builder(LOG).warnForRelativePath().build(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、if else 写法 + * 代码可读性好,维护难度大,如果增加配置,或者减少配置,都需要进行维护 + * 2、使用类似于 Hadoop Spark 等的 key-value 方法来存入 map + * 更通用,更易维护;不好校验,不易读 + * - + * 当前这个 for 循环每次遍历到的就是一个配置项: key-value + * - + * 我发现的另外一个问题,:改了 + * 原来 解析 myid 配置文件的代码,就在当前这个方法的最后。! + * 现在改了: 封装了一下。 + */ + for(Entry entry : zkProp.entrySet()) { + + // TODO_MA 注释: 从 entry 中获取 key-value + String key = entry.getKey().toString().trim(); + String value = entry.getValue().toString().trim(); + + // TODO_MA 注释: 如果可以的话,其实可以使用一个 map 数据结构来存储所有的这些配置 + // TODO_MA 注释: map.put(key, value); + + // TODO_MA 注释: 如果此时遍历到的 key 是 dataDir, 那么 遍历到的 value ,就设置成为 config 对象的 dataDir 成员变量的值 + if(key.equals("dataDir")) { + dataDir = vff.create(value); + } + + else if(key.equals("dataLogDir")) { + dataLogDir = vff.create(value); + } else if(key.equals("clientPort")) { + clientPort = Integer.parseInt(value); + } else if(key.equals("localSessionsEnabled")) { + localSessionsEnabled = Boolean.parseBoolean(value); + } else if(key.equals("localSessionsUpgradingEnabled")) { + localSessionsUpgradingEnabled = Boolean.parseBoolean(value); + } else if(key.equals("clientPortAddress")) { + clientPortAddress = value.trim(); + } else if(key.equals("secureClientPort")) { + secureClientPort = Integer.parseInt(value); + } else if(key.equals("secureClientPortAddress")) { + secureClientPortAddress = value.trim(); + } else if(key.equals("observerMasterPort")) { + observerMasterPort = Integer.parseInt(value); + } else if(key.equals("clientPortListenBacklog")) { + clientPortListenBacklog = Integer.parseInt(value); + } else if(key.equals("tickTime")) { + tickTime = Integer.parseInt(value); + } else if(key.equals("maxClientCnxns")) { + maxClientCnxns = Integer.parseInt(value); + } else if(key.equals("minSessionTimeout")) { + minSessionTimeout = Integer.parseInt(value); + } else if(key.equals("maxSessionTimeout")) { + maxSessionTimeout = Integer.parseInt(value); + } else if(key.equals("initLimit")) { + initLimit = Integer.parseInt(value); + } else if(key.equals("syncLimit")) { + syncLimit = Integer.parseInt(value); + } else if(key.equals("connectToLearnerMasterLimit")) { + connectToLearnerMasterLimit = Integer.parseInt(value); + } + + // TODO_MA 注释: electionAlg = 3, 实现是 FastLeaderElection, 具体算法是 Fast Paxos 实现 + else if(key.equals("electionAlg")) { + electionAlg = Integer.parseInt(value); + if(electionAlg != 3) { + throw new ConfigException("Invalid electionAlg value. Only 3 is supported."); + } + } else if(key.equals("quorumListenOnAllIPs")) { + quorumListenOnAllIPs = Boolean.parseBoolean(value); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、observer + participant(有选举权和被选举权的节点:Leader 和 Follower)只能出现在配置中 + * 2、leader + learner(Follower 和 Observer ) + */ + else if(key.equals("peerType")) { + if(value.toLowerCase().equals("observer")) { + peerType = LearnerType.OBSERVER; + } else if(value.toLowerCase().equals("participant")) { + peerType = LearnerType.PARTICIPANT; + } else { + throw new ConfigException("Unrecognised peertype: " + value); + } + } else if(key.equals("syncEnabled")) { + syncEnabled = Boolean.parseBoolean(value); + } else if(key.equals("dynamicConfigFile")) { + dynamicConfigFileStr = value; + } else if(key.equals("autopurge.snapRetainCount")) { + snapRetainCount = Integer.parseInt(value); + } else if(key.equals("autopurge.purgeInterval")) { + purgeInterval = Integer.parseInt(value); + } else if(key.equals("standaloneEnabled")) { + if(value.toLowerCase().equals("true")) { + setStandaloneEnabled(true); + } else if(value.toLowerCase().equals("false")) { + setStandaloneEnabled(false); + } else { + throw new ConfigException( + "Invalid option " + value + " for standalone mode. Choose 'true' or 'false.'"); + } + } else if(key.equals("reconfigEnabled")) { + if(value.toLowerCase().equals("true")) { + setReconfigEnabled(true); + } else if(value.toLowerCase().equals("false")) { + setReconfigEnabled(false); + } else { + throw new ConfigException( + "Invalid option " + value + " for reconfigEnabled flag. Choose 'true' or 'false.'"); + } + } else if(key.equals("sslQuorum")) { + sslQuorum = Boolean.parseBoolean(value); + } else if(key.equals("portUnification")) { + shouldUsePortUnification = Boolean.parseBoolean(value); + } else if(key.equals("sslQuorumReloadCertFiles")) { + sslQuorumReloadCertFiles = Boolean.parseBoolean(value); + } else if((key.startsWith("server.") || key.startsWith("group") || key.startsWith("weight")) && zkProp + .containsKey("dynamicConfigFile")) { + throw new ConfigException("parameter: " + key + " must be in a separate dynamic config file"); + } else if(key.equals(QuorumAuth.QUORUM_SASL_AUTH_ENABLED)) { + quorumEnableSasl = Boolean.parseBoolean(value); + } else if(key.equals(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED)) { + quorumServerRequireSasl = Boolean.parseBoolean(value); + } else if(key.equals(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED)) { + quorumLearnerRequireSasl = Boolean.parseBoolean(value); + } else if(key.equals(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT)) { + quorumLearnerLoginContext = value; + } else if(key.equals(QuorumAuth.QUORUM_SERVER_SASL_LOGIN_CONTEXT)) { + quorumServerLoginContext = value; + } else if(key.equals(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL)) { + quorumServicePrincipal = value; + } else if(key.equals("quorum.cnxn.threads.size")) { + quorumCnxnThreadsSize = Integer.parseInt(value); + } else if(key.equals(JvmPauseMonitor.INFO_THRESHOLD_KEY)) { + jvmPauseInfoThresholdMs = Long.parseLong(value); + } else if(key.equals(JvmPauseMonitor.WARN_THRESHOLD_KEY)) { + jvmPauseWarnThresholdMs = Long.parseLong(value); + } else if(key.equals(JvmPauseMonitor.SLEEP_TIME_MS_KEY)) { + jvmPauseSleepTimeMs = Long.parseLong(value); + } else if(key.equals(JvmPauseMonitor.JVM_PAUSE_MONITOR_FEATURE_SWITCH_KEY)) { + jvmPauseMonitorToRun = Boolean.parseBoolean(value); + } else if(key.equals("metricsProvider.className")) { + metricsProviderClassName = value; + } else if(key.startsWith("metricsProvider.")) { + String keyForMetricsProvider = key.substring(16); + metricsProviderConfiguration.put(keyForMetricsProvider, value); + } else if(key.equals("multiAddress.enabled")) { + multiAddressEnabled = Boolean.parseBoolean(value); + } else if(key.equals("multiAddress.reachabilityCheckTimeoutMs")) { + multiAddressReachabilityCheckTimeoutMs = Integer.parseInt(value); + } else if(key.equals("multiAddress.reachabilityCheckEnabled")) { + multiAddressReachabilityCheckEnabled = Boolean.parseBoolean(value); + } else { + System.setProperty("zookeeper." + key, value); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 到此为止解析结束 + * 接下来,执行各种参数的校验! + */ + + if(!quorumEnableSasl && quorumServerRequireSasl) { + throw new IllegalArgumentException( + QuorumAuth.QUORUM_SASL_AUTH_ENABLED + " is disabled, so cannot enable " + QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED); + } + if(!quorumEnableSasl && quorumLearnerRequireSasl) { + throw new IllegalArgumentException( + QuorumAuth.QUORUM_SASL_AUTH_ENABLED + " is disabled, so cannot enable " + QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED); + } + // If quorumpeer learner is not auth enabled then self won't be able to + // join quorum. So this condition is ensuring that the quorumpeer learner + // is also auth enabled while enabling quorum server require sasl. + if(!quorumLearnerRequireSasl && quorumServerRequireSasl) { + throw new IllegalArgumentException( + QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED + " is disabled, so cannot enable " + QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED); + } + + // Reset to MIN_SNAP_RETAIN_COUNT if invalid (less than 3) + // PurgeTxnLog.purge(File, File, int) will not allow to purge less than 3. + if(snapRetainCount < MIN_SNAP_RETAIN_COUNT) { + LOG.warn( + "Invalid autopurge.snapRetainCount: " + snapRetainCount + ". Defaulting to " + MIN_SNAP_RETAIN_COUNT); + snapRetainCount = MIN_SNAP_RETAIN_COUNT; + } + + if(dataDir == null) { + throw new IllegalArgumentException("dataDir is not set"); + } + if(dataLogDir == null) { + dataLogDir = dataDir; + } + + if(clientPort == 0) { + LOG.info("clientPort is not set"); + if(clientPortAddress != null) { + throw new IllegalArgumentException("clientPortAddress is set but clientPort is not set"); + } + } else if(clientPortAddress != null) { + this.clientPortAddress = new InetSocketAddress(InetAddress.getByName(clientPortAddress), clientPort); + LOG.info("clientPortAddress is {}", formatInetAddr(this.clientPortAddress)); + } else { + this.clientPortAddress = new InetSocketAddress(clientPort); + LOG.info("clientPortAddress is {}", formatInetAddr(this.clientPortAddress)); + } + + if(secureClientPort == 0) { + LOG.info("secureClientPort is not set"); + if(secureClientPortAddress != null) { + throw new IllegalArgumentException("secureClientPortAddress is set but secureClientPort is not set"); + } + } else if(secureClientPortAddress != null) { + this.secureClientPortAddress = new InetSocketAddress(InetAddress.getByName(secureClientPortAddress), + secureClientPort); + LOG.info("secureClientPortAddress is {}", formatInetAddr(this.secureClientPortAddress)); + } else { + this.secureClientPortAddress = new InetSocketAddress(secureClientPort); + LOG.info("secureClientPortAddress is {}", formatInetAddr(this.secureClientPortAddress)); + } + if(this.secureClientPortAddress != null) { + configureSSLAuth(); + } + + if(observerMasterPort <= 0) { + LOG.info("observerMasterPort is not set"); + } else { + this.observerMasterPort = observerMasterPort; + LOG.info("observerMasterPort is {}", observerMasterPort); + } + + if(tickTime == 0) { + throw new IllegalArgumentException("tickTime is not set"); + } + + minSessionTimeout = minSessionTimeout == -1 ? tickTime * 2 : minSessionTimeout; + maxSessionTimeout = maxSessionTimeout == -1 ? tickTime * 20 : maxSessionTimeout; + + if(minSessionTimeout > maxSessionTimeout) { + throw new IllegalArgumentException("minSessionTimeout must not be larger than maxSessionTimeout"); + } + + LOG.info("metricsProvider.className is {}", metricsProviderClassName); + try { + Class.forName(metricsProviderClassName, false, Thread.currentThread().getContextClassLoader()); + } catch(ClassNotFoundException error) { + throw new IllegalArgumentException("metrics provider class was not found", error); + } + + // TODO_MA 注释: zookeeper-3.5x 往后提供了一个动态配置,为了避免一个 滚动重启 + // TODO_MA 注释: 滚动重启 有可能造成数据不一致 + // backward compatibility - dynamic configuration in the same file as + // static configuration params see writeDynamicConfig() + if(dynamicConfigFileStr == null) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 解析 myid + */ + setupQuorumPeerConfig(zkProp, true); + + if(isDistributed() && isReconfigEnabled()) { + // we don't backup static config for standalone mode. + // we also don't backup if reconfig feature is disabled. + backupOldConfig(); + } + } + } + + /** + * Configure SSL authentication only if it is not configured. + * + * @throws ConfigException If authentication scheme is configured but authentication + * provider is not configured. + */ + public static void configureSSLAuth() throws ConfigException { + try(ClientX509Util clientX509Util = new ClientX509Util()) { + String sslAuthProp = ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX + System + .getProperty(clientX509Util.getSslAuthProviderProperty(), "x509"); + if(System.getProperty(sslAuthProp) == null) { + if((ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX + "x509").equals(sslAuthProp)) { + System.setProperty(ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX + "x509", + "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); + } else { + throw new ConfigException("No auth provider configured for the SSL authentication scheme '" + System + .getProperty(clientX509Util.getSslAuthProviderProperty()) + "'."); + } + } + } + } + + /** + * Backward compatibility -- It would backup static config file on bootup + * if users write dynamic configuration in "zoo.cfg". + */ + private void backupOldConfig() throws IOException { + new AtomicFileWritingIdiom(new File(configFileStr + ".bak"), new OutputStreamStatement() { + @Override + public void write(OutputStream output) throws IOException { + InputStream input = null; + try { + input = new FileInputStream(new File(configFileStr)); + byte[] buf = new byte[1024]; + int bytesRead; + while((bytesRead = input.read(buf)) > 0) { + output.write(buf, 0, bytesRead); + } + } finally { + if(input != null) { + input.close(); + } + } + } + }); + } + + /** + * Writes dynamic configuration file + */ + public static void writeDynamicConfig(final String dynamicConfigFilename, final QuorumVerifier qv, + final boolean needKeepVersion) throws IOException { + + new AtomicFileWritingIdiom(new File(dynamicConfigFilename), new WriterStatement() { + @Override + public void write(Writer out) throws IOException { + Properties cfg = new Properties(); + cfg.load(new StringReader(qv.toString())); + + List servers = new ArrayList(); + for(Entry entry : cfg.entrySet()) { + String key = entry.getKey().toString().trim(); + if(!needKeepVersion && key.startsWith("version")) { + continue; + } + + String value = entry.getValue().toString().trim(); + servers.add(key.concat("=").concat(value)); + } + + Collections.sort(servers); + out.write(StringUtils.joinStrings(servers, "\n")); + } + }); + } + + /** + * Edit static config file. + * If there are quorum information in static file, e.g. "server.X", "group", + * it will remove them. + * If it needs to erase client port information left by the old config, + * "eraseClientPortAddress" should be set true. + * It should also updates dynamic file pointer on reconfig. + */ + public static void editStaticConfig(final String configFileStr, final String dynamicFileStr, + final boolean eraseClientPortAddress) throws IOException { + // Some tests may not have a static config file. + if(configFileStr == null) { + return; + } + + File configFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath().failForNonExistingPath().build()) + .create(configFileStr); + + final File dynamicFile = (new VerifyingFileFactory.Builder(LOG).warnForRelativePath().failForNonExistingPath() + .build()).create(dynamicFileStr); + + final Properties cfg = new Properties(); + FileInputStream in = new FileInputStream(configFile); + try { + cfg.load(in); + } finally { + in.close(); + } + + new AtomicFileWritingIdiom(new File(configFileStr), new WriterStatement() { + @Override + public void write(Writer out) throws IOException { + for(Entry entry : cfg.entrySet()) { + String key = entry.getKey().toString().trim(); + + if(key.startsWith("server.") || key.startsWith("group") || key.startsWith("weight") || key + .startsWith("dynamicConfigFile") || key + .startsWith("peerType") || (eraseClientPortAddress && (key.startsWith("clientPort") || key + .startsWith("clientPortAddress")))) { + // not writing them back to static file + continue; + } + + String value = entry.getValue().toString().trim(); + out.write(key.concat("=").concat(value).concat("\n")); + } + + // updates the dynamic file pointer + String dynamicConfigFilePath = PathUtils.normalizeFileSystemPath(dynamicFile.getCanonicalPath()); + out.write("dynamicConfigFile=".concat(dynamicConfigFilePath).concat("\n")); + } + }); + } + + public static void deleteFile(String filename) { + if(filename == null) { + return; + } + File f = new File(filename); + if(f.exists()) { + try { + f.delete(); + } catch(Exception e) { + LOG.warn("deleting {} failed", filename); + } + } + } + + private static QuorumVerifier createQuorumVerifier(Properties dynamicConfigProp, + boolean isHierarchical) throws ConfigException { + + if(isHierarchical) { + return new QuorumHierarchical(dynamicConfigProp); + } else { + /* + * The default QuorumVerifier is QuorumMaj + */ + //LOG.info("Defaulting to majority quorums"); + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: QuorumMaj 默认实现 + * 在构造 QuorumMaj 的时候,对 zoo.cfg 的服务器节点配置做了解析 + */ + return new QuorumMaj(dynamicConfigProp); + } + } + + void setupQuorumPeerConfig(Properties prop, + boolean configBackwardCompatibilityMode) throws IOException, ConfigException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: QuorumVerifier 这个对象定义了一个算法: 少数服从多数 + * 1、解析 + * server.2=bigdata02:2888 + * server.3=bigdata03:2888:3888 + * server.4=bigdata04:2888:3888:participant + * server.5=bigdata05:2888:3888:observer + */ + quorumVerifier = parseDynamicConfig(prop, electionAlg, true, configBackwardCompatibilityMode); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 解析 myid + */ + setupMyId(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 校验 2181 + */ + setupClientPort(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + setupPeerType(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 做一些参数的校验 + */ + checkValidity(); + } + + /** + * Parse dynamic configuration file and return + * quorumVerifier for new configuration. + * // TODO_MA 注释: 这个方法两个作用: + * // TODO_MA 注释: 1、初始化得到一个 少数服从多数/过半通过原则 的一个算法实例 + * // TODO_MA 注释: 2、其实是解析得到一个 all voting observer + * + * @param dynamicConfigProp Properties to parse from. + * @throws IOException + * @throws ConfigException + */ + public static QuorumVerifier parseDynamicConfig(Properties dynamicConfigProp, int eAlg, boolean warnings, + boolean configBackwardCompatibilityMode) throws IOException, ConfigException { + boolean isHierarchical = false; + + // TODO_MA 马中华 注释: 从 3.5 往后,可以给 zk 节点配置权重 + for(Entry entry : dynamicConfigProp.entrySet()) { + String key = entry.getKey().toString().trim(); + if(key.startsWith("group") || key.startsWith("weight")) { + isHierarchical = true; + } else if(!configBackwardCompatibilityMode && !key.startsWith("server.") && !key.equals("version")) { + LOG.info(dynamicConfigProp.toString()); + throw new ConfigException("Unrecognised parameter: " + key); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + QuorumVerifier qv = createQuorumVerifier(dynamicConfigProp, isHierarchical); + + int numParticipators = qv.getVotingMembers().size(); + int numObservers = qv.getObservingMembers().size(); + + if(numParticipators == 0) { + if(!standaloneEnabled) { + throw new IllegalArgumentException( + "standaloneEnabled = false then " + "number of participants should be >0"); + } + if(numObservers > 0) { + throw new IllegalArgumentException("Observers w/o participants is an invalid configuration"); + } + } else if(numParticipators == 1 && standaloneEnabled) { + // HBase currently adds a single server line to the config, for + // b/w compatibility reasons we need to keep this here. If standaloneEnabled + // is true, the QuorumPeerMain script will create a standalone server instead + // of a quorum configuration + LOG.error("Invalid configuration, only one server specified (ignoring)"); + if(numObservers > 0) { + throw new IllegalArgumentException("Observers w/o quorum is an invalid configuration"); + } + } else { + if(warnings) { + if(numParticipators <= 2) { + LOG.warn("No server failure will be tolerated. You need at least 3 servers."); + } else if(numParticipators % 2 == 0) { + LOG.warn("Non-optimial configuration, consider an odd number of servers."); + } + } + + for(QuorumServer s : qv.getVotingMembers().values()) { + if(s.electionAddr == null) { + throw new IllegalArgumentException("Missing election port for server: " + s.id); + } + } + } + return qv; + } + + private void setupMyId() throws IOException { + + // TODO_MA 注释: 搭建集群的时候,需要在 数据 datadir 目中中,给配置一个 叫做 myid 的文件 + // TODO_MA 注释: 只有一行,存储了当前 server 的 id 编号(1-255之间) + File myIdFile = new File(dataDir, "myid"); + + // TODO_MA 注释: + // standalone server doesn't need myid file. + if(!myIdFile.isFile()) { + return; + } + + // TODO_MA 注释: + BufferedReader br = new BufferedReader(new FileReader(myIdFile)); + String myIdString; + try { + myIdString = br.readLine(); + } finally { + br.close(); + } + + // TODO_MA 注释: 在 zookeeper 关于 myid 有三个类似的概念: myid serverid sid + // TODO_MA 注释: 执行类型转换 + try { + serverId = Long.parseLong(myIdString); + MDC.put("myid", myIdString); + } catch(NumberFormatException e) { + throw new IllegalArgumentException("serverid " + myIdString + " is not a number"); + } + } + + private void setupClientPort() throws ConfigException { + if(serverId == UNSET_SERVERID) { + return; + } + QuorumServer qs = quorumVerifier.getAllMembers().get(serverId); + + if(clientPortAddress != null && qs != null && qs.clientAddr != null) { + if((!clientPortAddress.getAddress().isAnyLocalAddress() && !clientPortAddress + .equals(qs.clientAddr)) || (clientPortAddress.getAddress().isAnyLocalAddress() && clientPortAddress + .getPort() != qs.clientAddr.getPort())) { + throw new ConfigException( + "client address for this server (id = " + serverId + ") in static config file is " + clientPortAddress + " is different from client address found in dynamic file: " + qs.clientAddr); + } + } + if(qs != null && qs.clientAddr != null) { + clientPortAddress = qs.clientAddr; + } + if(qs != null && qs.clientAddr == null) { + qs.clientAddr = clientPortAddress; + qs.isClientAddrFromStatic = true; + } + } + + // TODO_MA 注释: 根据 serverid 确认服务器的类别 + private void setupPeerType() { + + // TODO_MA 注释: 先拿到当前这个 server 的 peerType: OBSERVER PARTICIPANT + // Warn about inconsistent peer type + LearnerType roleByServersList = quorumVerifier.getObservingMembers() + .containsKey(serverId) ? LearnerType.OBSERVER : LearnerType.PARTICIPANT; + + if(roleByServersList != peerType) { + LOG.warn("Peer type from servers list ({}) doesn't match peerType ({}). Defaulting to servers list.", + roleByServersList, peerType); + peerType = roleByServersList; + } + } + + public void checkValidity() throws IOException, ConfigException { + if(isDistributed()) { + if(initLimit == 0) { + throw new IllegalArgumentException("initLimit is not set"); + } + if(syncLimit == 0) { + throw new IllegalArgumentException("syncLimit is not set"); + } + if(serverId == UNSET_SERVERID) { + throw new IllegalArgumentException("myid file is missing"); + } + } + } + + public InetSocketAddress getClientPortAddress() { + return clientPortAddress; + } + + public InetSocketAddress getSecureClientPortAddress() { + return secureClientPortAddress; + } + + public int getObserverMasterPort() { + return observerMasterPort; + } + + public File getDataDir() { + return dataDir; + } + + public File getDataLogDir() { + return dataLogDir; + } + + public String getInitialConfig() { + return initialConfig; + } + + public int getTickTime() { + return tickTime; + } + + public int getMaxClientCnxns() { + return maxClientCnxns; + } + + public int getMinSessionTimeout() { + return minSessionTimeout; + } + + public int getMaxSessionTimeout() { + return maxSessionTimeout; + } + + public String getMetricsProviderClassName() { + return metricsProviderClassName; + } + + public Properties getMetricsProviderConfiguration() { + return metricsProviderConfiguration; + } + + public boolean areLocalSessionsEnabled() { + return localSessionsEnabled; + } + + public boolean isLocalSessionsUpgradingEnabled() { + return localSessionsUpgradingEnabled; + } + + public boolean isSslQuorum() { + return sslQuorum; + } + + public boolean shouldUsePortUnification() { + return shouldUsePortUnification; + } + + public int getClientPortListenBacklog() { + return clientPortListenBacklog; + } + + public int getInitLimit() { + return initLimit; + } + + public int getSyncLimit() { + return syncLimit; + } + + public int getConnectToLearnerMasterLimit() { + return connectToLearnerMasterLimit; + } + + public int getElectionAlg() { + return electionAlg; + } + + public int getElectionPort() { + return electionPort; + } + + public int getSnapRetainCount() { + return snapRetainCount; + } + + public int getPurgeInterval() { + return purgeInterval; + } + + public boolean getSyncEnabled() { + return syncEnabled; + } + + public QuorumVerifier getQuorumVerifier() { + return quorumVerifier; + } + + public QuorumVerifier getLastSeenQuorumVerifier() { + return lastSeenQuorumVerifier; + } + + public Map getServers() { + // returns all configuration servers -- participants and observers + return Collections.unmodifiableMap(quorumVerifier.getAllMembers()); + } + + public long getJvmPauseInfoThresholdMs() { + return jvmPauseInfoThresholdMs; + } + + public long getJvmPauseWarnThresholdMs() { + return jvmPauseWarnThresholdMs; + } + + public long getJvmPauseSleepTimeMs() { + return jvmPauseSleepTimeMs; + } + + public boolean isJvmPauseMonitorToRun() { + return jvmPauseMonitorToRun; + } + + public long getServerId() { + return serverId; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + public boolean isDistributed() { + return quorumVerifier != null && (!standaloneEnabled || quorumVerifier.getVotingMembers().size() > 1); + } + + public LearnerType getPeerType() { + return peerType; + } + + public String getConfigFilename() { + return configFileStr; + } + + public Boolean getQuorumListenOnAllIPs() { + return quorumListenOnAllIPs; + } + + public boolean isMultiAddressEnabled() { + return multiAddressEnabled; + } + + public boolean isMultiAddressReachabilityCheckEnabled() { + return multiAddressReachabilityCheckEnabled; + } + + public int getMultiAddressReachabilityCheckTimeoutMs() { + return multiAddressReachabilityCheckTimeoutMs; + } + + public static boolean isStandaloneEnabled() { + return standaloneEnabled; + } + + public static void setStandaloneEnabled(boolean enabled) { + standaloneEnabled = enabled; + } + + public static boolean isReconfigEnabled() { + return reconfigEnabled; + } + + public static void setReconfigEnabled(boolean enabled) { + reconfigEnabled = enabled; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java new file mode 100644 index 0000000..36babdc --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumPeerMain.java @@ -0,0 +1,354 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import javax.management.JMException; +import javax.security.sasl.SaslException; + +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.audit.ZKAuditProvider; +import org.apache.zookeeper.jmx.ManagedUtil; +import org.apache.zookeeper.metrics.MetricsProvider; +import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; +import org.apache.zookeeper.metrics.impl.MetricsProviderBootstrap; +import org.apache.zookeeper.server.DatadirCleanupManager; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServerMain; +import org.apache.zookeeper.server.admin.AdminServer.AdminServerException; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog.DatadirException; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.util.JvmPauseMonitor; +import org.apache.zookeeper.util.ServiceUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + *

      Configuration file

      + * + * When the main() method of this class is used to start the program, the first + * argument is used as a path to the config file, which will be used to obtain + * configuration information. This file is a Properties file, so keys and + * values are separated by equals (=) and the key/value pairs are separated + * by new lines. The following is a general summary of keys used in the + * configuration file. For full details on this see the documentation in docs/index.html + *
        + *
      1. dataDir - The directory where the ZooKeeper data is stored.
      2. + *
      3. dataLogDir - The directory where the ZooKeeper transaction log is stored.
      4. + *
      5. clientPort - The port used to communicate with clients.
      6. + *
      7. tickTime - The duration of a tick in milliseconds. This is the basic + * unit of time in ZooKeeper.
      8. + *
      9. initLimit - The maximum number of ticks that a follower will wait to + * initially synchronize with a leader.
      10. + *
      11. syncLimit - The maximum number of ticks that a follower will wait for a + * message (including heartbeats) from the leader.
      12. + *
      13. server.id - This is the host:port[:port] that the server with the + * given id will use for the quorum protocol.
      14. + *
      + * In addition to the config file. There is a file in the data directory called + * "myid" that contains the server id as an ASCII decimal value. + */ +@InterfaceAudience.Public +public class QuorumPeerMain { + + // TODO_MA 注释: 一句话总结 QuorumPeerMain 的作用: 提供了饿一个 main 方法,用来创建和启动 QuorumPeer + + private static final Logger LOG = LoggerFactory.getLogger(QuorumPeerMain.class); + + private static final String USAGE = "Usage: QuorumPeerMain configfile"; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 一台物理 zookeeper 服务器 就是一个 QP 的抽象 + */ + protected QuorumPeer quorumPeer; + + /** + * To start the replicated server specify the configuration file name on the command line. + * + * @param args path to the configfile + */ + public static void main(String[] args) { + + // TODO_MA 马中华 注释: args【0】 = zoo.cfg 路径 + // TODO_MA 注释: 构建QuorumPeerMain 实例 + QuorumPeerMain main = new QuorumPeerMain(); + try { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + * 1、初始化 + * 2、启动 + */ + main.initializeAndRun(args); + + } catch(IllegalArgumentException e) { + LOG.error("Invalid arguments, exiting abnormally", e); + LOG.info(USAGE); + System.err.println(USAGE); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } catch(ConfigException e) { + LOG.error("Invalid config, exiting abnormally", e); + System.err.println("Invalid config, exiting abnormally"); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue()); + } catch(DatadirException e) { + LOG.error("Unable to access datadir, exiting abnormally", e); + System.err.println("Unable to access datadir, exiting abnormally"); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue()); + } catch(AdminServerException e) { + LOG.error("Unable to start AdminServer, exiting abnormally", e); + System.err.println("Unable to start AdminServer, exiting abnormally"); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.ERROR_STARTING_ADMIN_SERVER.getValue()); + } catch(Exception e) { + LOG.error("Unexpected exception, exiting abnormally", e); + ZKAuditProvider.addServerStartFailureAuditLog(); + ServiceUtils.requestSystemExit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + LOG.info("Exiting normally"); + ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue()); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 这个方法总共做了三件事: + * 1、解析 zoo.cfg 和 myid 配置 + * 2、启动一个关于 旧快照数据文件的 定期删除任务 + * 3、启动 QuorumPeer + * QuorumPeerMain 就是负责解析 配置文件之后,用来启动一个 QuorumPeer,QuorumPeer 代表了一台 ZooKeeperServer + */ + protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServerException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: args[0] = $ZOOKEEPER_HOME/conf/zoo.cfg + * 1、解析 zoo.cfg + * properties 文件,最终的解析,是通过 Properties API 来完成解析 + * 2、生成一个 QuorumMaj 的算法实例: 少数服从多数 + * 3、解析 myid + * 构建一个输入流读取这个文件,读取一行,得到一个数值字符串,转换数值 + * 4、在解析过程中,穿插了大量的参数的校验! + */ + QuorumPeerConfig config = new QuorumPeerConfig(); + if(args.length == 1) { + config.parse(args[0]); + } + + // TODO_MA 注释: zookeeper 的 解析 配置的管理代码的确不够优雅。参考 flink 的实现 + // TODO_MA 注释: config.xx1(key).xx2(value).xx3(jiaoyan).xx4(doc) + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 启动了一个定时任务,定时清理旧快照文件 + */ + // Start and schedule the the purge task + DatadirCleanupManager purgeMgr = new DatadirCleanupManager( + config.getDataDir(), // TODO_MA 马中华 注释: 数据存储目录 + config.getDataLogDir(), // TODO_MA 马中华 注释: 日志存储目录 + config.getSnapRetainCount(), // TODO_MA 马中华 注释: 至少要保留的快照文件个数 + config.getPurgeInterval()); // TODO_MA 马中华 注释: 定时任务的时间间隔 + purgeMgr.start(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 从配置中启动 QuorumPeer + * main.args[0] = zoo.cfg + * 最开始,解析 zoo.cfg 得到的配置信息,都被放置在了 QuorumPeerConfig 中 + * 在将来 QuorumPeer 启动的时候,会将 QuorumPeerConfig 中的各个成员变量复制到 QuorumPeer 中 + * 本身 QuorumPeer 就代表了一个 ZK JVM 进程,就是一个 ZooKeeperServer + * 最终实现的效果就是: 配置在 zoo.cfg 中的配置,最终,都被保存在 QuorumPeer 中了 + * 具体形式: 一个成员变量,一个配置值 + */ + if(args.length == 1 && config.isDistributed()) { + runFromConfig(config); + } else { + LOG.warn("Either no config or no quorum defined in config, running in standalone mode"); + // there is only server in the quorum -- run as standalone + ZooKeeperServerMain.main(args); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 三件事 + * 1、启动 真正意义上的 Server, 监听 2181 端口的 NIO 服务端 + * 2、获取 QuorumPeer 实例对象,并且把 QuorumPeerConfig 中的各种成员变量值,复制到 QuorumPeer 里面 + * 3、启动 QuorumPeer + */ + public void runFromConfig(QuorumPeerConfig config) throws IOException, AdminServerException { + try { + ManagedUtil.registerLog4jMBeans(); + } catch(JMException e) { + LOG.warn("Unable to register log4j JMX control", e); + } + + LOG.info("Starting quorum peer, myid=" + config.getServerId()); + MetricsProvider metricsProvider; + try { + metricsProvider = MetricsProviderBootstrap + .startMetricsProvider(config.getMetricsProviderClassName(), config.getMetricsProviderConfiguration()); + } catch(MetricsProviderLifeCycleException error) { + throw new IOException("Cannot boot MetricsProvider " + config.getMetricsProviderClassName(), error); + } + + // TODO_MA 注释: 从这儿开始: 准备 QP , 以及启动 QP + + try { + ServerMetrics.metricsProviderInitialized(metricsProvider); + ServerCnxnFactory cnxnFactory = null; + ServerCnxnFactory secureCnxnFactory = null; + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 创建 ServerCnxnFactory = 默认实现: NIOServerCnxnFactory + * 内初初始化了四种线程(只有三种, workerthread 是在调用 start() 方法的时初始化的) + * 此处创建的 NIOServerCnxnFactory 为客户额发送的读写请求提供处理服务的 + */ + if(config.getClientPortAddress() != null) { + // TODO_MA 注释: 创建 NIOServerCnxnFactory + cnxnFactory = ServerCnxnFactory.createFactory(); + // TODO_MA 注释: 进行配置启动 + cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns(), + config.getClientPortListenBacklog(), false); + } + + // TODO_MA 注释: + if(config.getSecureClientPortAddress() != null) { + secureCnxnFactory = ServerCnxnFactory.createFactory(); + secureCnxnFactory.configure(config.getSecureClientPortAddress(), config.getMaxClientCnxns(), + config.getClientPortListenBacklog(), true); + } + + // TODO_MA 注释: 构造了呀一个 QP 实例对象 + // TODO_MA 注释: 最开始在解析 配置的时候,吧各种配置,都存储在 QuorumPeerConfig 对象中 + // TODO_MA 注释: 现在需要从 QuorumPeerConfig 吧各种配置存储到 quorumPeer 中来 + quorumPeer = getQuorumPeer(); + + /** + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + quorumPeer.setTxnFactory(new FileTxnSnapLog(config.getDataLogDir(), config.getDataDir())); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 完成了参数的传递 + * zoo.cfg ===> QuorumPeerConfig ===> QuorumPeer + */ + quorumPeer.enableLocalSessions(config.areLocalSessionsEnabled()); + quorumPeer.enableLocalSessionsUpgrading(config.isLocalSessionsUpgradingEnabled()); + //quorumPeer.setQuorumPeers(config.getAllMembers()); + quorumPeer.setElectionType(config.getElectionAlg()); + quorumPeer.setMyid(config.getServerId()); + quorumPeer.setTickTime(config.getTickTime()); + quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout()); + quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout()); + quorumPeer.setInitLimit(config.getInitLimit()); + quorumPeer.setSyncLimit(config.getSyncLimit()); + quorumPeer.setConnectToLearnerMasterLimit(config.getConnectToLearnerMasterLimit()); + quorumPeer.setObserverMasterPort(config.getObserverMasterPort()); + quorumPeer.setConfigFileName(config.getConfigFilename()); + quorumPeer.setClientPortListenBacklog(config.getClientPortListenBacklog()); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); + + quorumPeer.setQuorumVerifier(config.getQuorumVerifier(), false); + if(config.getLastSeenQuorumVerifier() != null) { + quorumPeer.setLastSeenQuorumVerifier(config.getLastSeenQuorumVerifier(), false); + } + quorumPeer.initConfigInZKDatabase(); + quorumPeer.setCnxnFactory(cnxnFactory); + quorumPeer.setSecureCnxnFactory(secureCnxnFactory); + quorumPeer.setSslQuorum(config.isSslQuorum()); + quorumPeer.setUsePortUnification(config.shouldUsePortUnification()); + quorumPeer.setLearnerType(config.getPeerType()); + quorumPeer.setSyncEnabled(config.getSyncEnabled()); + quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs()); + if(config.sslQuorumReloadCertFiles) { + quorumPeer.getX509Util().enableCertFileReloading(); + } + quorumPeer.setMultiAddressEnabled(config.isMultiAddressEnabled()); + quorumPeer.setMultiAddressReachabilityCheckEnabled(config.isMultiAddressReachabilityCheckEnabled()); + quorumPeer.setMultiAddressReachabilityCheckTimeoutMs(config.getMultiAddressReachabilityCheckTimeoutMs()); + + // sets quorum sasl authentication configurations + quorumPeer.setQuorumSaslEnabled(config.quorumEnableSasl); + if(quorumPeer.isQuorumSaslAuthEnabled()) { + quorumPeer.setQuorumServerSaslRequired(config.quorumServerRequireSasl); + quorumPeer.setQuorumLearnerSaslRequired(config.quorumLearnerRequireSasl); + quorumPeer.setQuorumServicePrincipal(config.quorumServicePrincipal); + quorumPeer.setQuorumServerLoginContext(config.quorumServerLoginContext); + quorumPeer.setQuorumLearnerLoginContext(config.quorumLearnerLoginContext); + } + quorumPeer.setQuorumCnxnThreadsSize(config.quorumCnxnThreadsSize); + quorumPeer.initialize(); + + if(config.jvmPauseMonitorToRun) { + quorumPeer.setJvmPauseMonitor(new JvmPauseMonitor(config)); + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 做了五件大事: + * 1、冷启动数据恢复 + * 2、NIOServerCnxnFactory 的启动: 内部其实就是把 NIOServerCnxnFactory 内部的四种线程启动好 + * 3、启动 AdminServer(zk-3.4 没有的) + * 4、准备选举环境 + 创建选举算法实例 + * 5、super.start(); 进入 ZAB 工作模式, 进入 quorumPeer 的 run() + */ + quorumPeer.start(); + + ZKAuditProvider.addZKStartStopAuditLog(); + quorumPeer.join(); + } catch(InterruptedException e) { + // warn, but generally this is ok + LOG.warn("Quorum Peer interrupted", e); + } finally { + if(metricsProvider != null) { + try { + metricsProvider.stop(); + } catch(Throwable error) { + LOG.warn("Error while stopping metrics", error); + } + } + } + } + + // @VisibleForTesting + protected QuorumPeer getQuorumPeer() throws SaslException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return new QuorumPeer(); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumStats.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumStats.java new file mode 100644 index 0000000..698349f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumStats.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +public class QuorumStats { + + private final Provider provider; + + public interface Provider { + + String UNKNOWN_STATE = "unknown"; + String LOOKING_STATE = "leaderelection"; + String LEADING_STATE = "leading"; + String FOLLOWING_STATE = "following"; + String OBSERVING_STATE = "observing"; + String[] getQuorumPeers(); + String getServerState(); + + } + + protected QuorumStats(Provider provider) { + this.provider = provider; + } + + public String getServerState() { + return provider.getServerState(); + } + + public String[] getQuorumPeers() { + return provider.getQuorumPeers(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(super.toString()); + String state = getServerState(); + if (state.equals(Provider.LEADING_STATE)) { + sb.append("Followers:"); + for (String f : getQuorumPeers()) { + sb.append(" ").append(f); + } + sb.append("\n"); + } else if (state.equals(Provider.FOLLOWING_STATE) || state.equals(Provider.OBSERVING_STATE)) { + sb.append("Leader: "); + String[] ldr = getQuorumPeers(); + if (ldr.length > 0) { + sb.append(ldr[0]); + } else { + sb.append("not connected"); + } + sb.append("\n"); + } + return sb.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumZooKeeperServer.java new file mode 100644 index 0000000..6faef4a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/QuorumZooKeeperServer.java @@ -0,0 +1,222 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.util.Objects; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.MultiOperationRecord; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.metrics.MetricsContext; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; + +/** + * Abstract base class for all ZooKeeperServers that participate in + * a quorum. + */ +public abstract class QuorumZooKeeperServer extends ZooKeeperServer { + + public final QuorumPeer self; + protected UpgradeableSessionTracker upgradeableSessionTracker; + + protected QuorumZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, int minSessionTimeout, + int maxSessionTimeout, int listenBacklog, ZKDatabase zkDb, QuorumPeer self) { + super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, listenBacklog, zkDb, self.getInitialConfig(), + self.isReconfigEnabled()); + this.self = self; + } + + @Override + protected void startSessionTracker() { + upgradeableSessionTracker = (UpgradeableSessionTracker) sessionTracker; + upgradeableSessionTracker.start(); + } + + public Request checkUpgradeSession(Request request) throws IOException, KeeperException { + // TODO_MA 注释: 如果这是对本地会话的请求并且是创建临时节点,则升级会话并为领导者返回新的会话请求。 + // If this is a request for a local session and it is to + // create an ephemeral node, then upgrade the session and return + // a new session request for the leader. + // TODO_MA 注释: 这是由请求处理器线程(跟随者或观察者请求处理器)调用的,它是学习者独有的。所以不会被两个线程并发调用。 + // This is called by the request processor thread (either follower + // or observer request processor), which is unique to a learner. + // So will not be called concurrently by two threads. + if ((request.type != OpCode.create && request.type != OpCode.create2 && request.type != OpCode.multi) + || !upgradeableSessionTracker.isLocalSession(request.sessionId)) { + return null; + } + + if (OpCode.multi == request.type) { + MultiOperationRecord multiTransactionRecord = new MultiOperationRecord(); + request.request.rewind(); + ByteBufferInputStream.byteBuffer2Record(request.request, multiTransactionRecord); + request.request.rewind(); + boolean containsEphemeralCreate = false; + for (Op op : multiTransactionRecord) { + if (op.getType() == OpCode.create || op.getType() == OpCode.create2) { + CreateRequest createRequest = (CreateRequest) op.toRequestRecord(); + CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags()); + if (createMode.isEphemeral()) { + containsEphemeralCreate = true; + break; + } + } + } + if (!containsEphemeralCreate) { + return null; + } + } else { + CreateRequest createRequest = new CreateRequest(); + request.request.rewind(); + ByteBufferInputStream.byteBuffer2Record(request.request, createRequest); + request.request.rewind(); + CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags()); + if (!createMode.isEphemeral()) { + return null; + } + } + + // Uh oh. We need to upgrade before we can proceed. + if (!self.isLocalSessionsUpgradingEnabled()) { + throw new KeeperException.EphemeralOnLocalSessionException(); + } + + return makeUpgradeRequest(request.sessionId); + } + + private Request makeUpgradeRequest(long sessionId) { + // Make sure to atomically check local session status, upgrade + // session, and make the session creation request. This is to + // avoid another thread upgrading the session in parallel. + synchronized (upgradeableSessionTracker) { + if (upgradeableSessionTracker.isLocalSession(sessionId)) { + int timeout = upgradeableSessionTracker.upgradeSession(sessionId); + ByteBuffer to = ByteBuffer.allocate(4); + to.putInt(timeout); + return new Request(null, sessionId, 0, OpCode.createSession, to, null); + } + } + return null; + } + + /** + * Implements the SessionUpgrader interface, + * + * @param sessionId + */ + public void upgrade(long sessionId) { + Request request = makeUpgradeRequest(sessionId); + if (request != null) { + LOG.info("Upgrading session 0x{}", Long.toHexString(sessionId)); + // This must be a global request + submitRequest(request); + } + } + + @Override + protected void setLocalSessionFlag(Request si) { + // We need to set isLocalSession to tree for these type of request + // so that the request processor can process them correctly. + switch (si.type) { + case OpCode.createSession: + if (self.areLocalSessionsEnabled()) { + // All new sessions local by default. + si.setLocalSession(true); + } + break; + case OpCode.closeSession: + String reqType = "global"; + if (upgradeableSessionTracker.isLocalSession(si.sessionId)) { + si.setLocalSession(true); + reqType = "local"; + } + LOG.info("Submitting {} closeSession request for session 0x{}", reqType, Long.toHexString(si.sessionId)); + break; + default: + break; + } + } + + @Override + public void dumpConf(PrintWriter pwriter) { + super.dumpConf(pwriter); + + pwriter.print("initLimit="); + pwriter.println(self.getInitLimit()); + pwriter.print("syncLimit="); + pwriter.println(self.getSyncLimit()); + pwriter.print("electionAlg="); + pwriter.println(self.getElectionType()); + pwriter.print("electionPort="); + pwriter.println(self.getElectionAddress().getAllPorts() + .stream().map(Objects::toString).collect(Collectors.joining("|"))); + pwriter.print("quorumPort="); + pwriter.println(self.getQuorumAddress().getAllPorts() + .stream().map(Objects::toString).collect(Collectors.joining("|"))); + pwriter.print("peerType="); + pwriter.println(self.getLearnerType().ordinal()); + pwriter.println("membership: "); + pwriter.print(new String(self.getQuorumVerifier().toString().getBytes())); + } + + @Override + protected void setState(State state) { + this.state = state; + } + + @Override + protected void registerMetrics() { + super.registerMetrics(); + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + + rootContext.registerGauge("quorum_size", () -> { + return self.getQuorumSize(); + }); + } + + @Override + protected void unregisterMetrics() { + super.unregisterMetrics(); + + MetricsContext rootContext = ServerMetrics.getMetrics().getMetricsProvider().getRootContext(); + + rootContext.unregisterGauge("quorum_size"); + } + + @Override + public void dumpMonitorValues(BiConsumer response) { + super.dumpMonitorValues(response); + response.accept("peer_state", self.getDetailedPeerState()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyBean.java new file mode 100644 index 0000000..ff8a9d4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyBean.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperServerBean; + +/** + * ReadOnly MX Bean interface, implemented by ReadOnlyBean + * + */ +public class ReadOnlyBean extends ZooKeeperServerBean { + + public ReadOnlyBean(ZooKeeperServer zks) { + super(zks); + } + + public String getName() { + return "ReadOnlyServer"; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyRequestProcessor.java new file mode 100644 index 0000000..c50dd53 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyRequestProcessor.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ZooKeeperCriticalThread; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooTrace; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This processor is at the beginning of the ReadOnlyZooKeeperServer's + * processors chain. All it does is, it passes read-only operations (e.g. + * OpCode.getData, OpCode.exists) through to the next processor, but drops + * state-changing operations (e.g. OpCode.create, OpCode.setData). + */ +public class ReadOnlyRequestProcessor extends ZooKeeperCriticalThread implements RequestProcessor { + + private static final Logger LOG = LoggerFactory.getLogger(ReadOnlyRequestProcessor.class); + + private final LinkedBlockingQueue queuedRequests = new LinkedBlockingQueue(); + + private boolean finished = false; + + private final RequestProcessor nextProcessor; + + private final ZooKeeperServer zks; + + public ReadOnlyRequestProcessor(ZooKeeperServer zks, RequestProcessor nextProcessor) { + super("ReadOnlyRequestProcessor:" + zks.getServerId(), zks.getZooKeeperServerListener()); + this.zks = zks; + this.nextProcessor = nextProcessor; + } + + public void run() { + try { + while (!finished) { + Request request = queuedRequests.take(); + + // log request + long traceMask = ZooTrace.CLIENT_REQUEST_TRACE_MASK; + if (request.type == OpCode.ping) { + traceMask = ZooTrace.CLIENT_PING_TRACE_MASK; + } + if (LOG.isTraceEnabled()) { + ZooTrace.logRequest(LOG, traceMask, 'R', request, ""); + } + if (Request.requestOfDeath == request) { + break; + } + + // filter read requests + switch (request.type) { + case OpCode.sync: + case OpCode.create: + case OpCode.create2: + case OpCode.createTTL: + case OpCode.createContainer: + case OpCode.delete: + case OpCode.deleteContainer: + case OpCode.setData: + case OpCode.reconfig: + case OpCode.setACL: + case OpCode.multi: + case OpCode.check: + ReplyHeader hdr = new ReplyHeader( + request.cxid, + zks.getZKDatabase().getDataTreeLastProcessedZxid(), + Code.NOTREADONLY.intValue()); + try { + request.cnxn.sendResponse(hdr, null, null); + } catch (IOException e) { + LOG.error("IO exception while sending response", e); + } + continue; + } + + // proceed to the next processor + if (nextProcessor != null) { + nextProcessor.processRequest(request); + } + } + } catch (Exception e) { + handleException(this.getName(), e); + } + LOG.info("ReadOnlyRequestProcessor exited loop!"); + } + + @Override + public void processRequest(Request request) { + if (!finished) { + queuedRequests.add(request); + } + } + + @Override + public void shutdown() { + finished = true; + queuedRequests.clear(); + queuedRequests.add(Request.requestOfDeath); + nextProcessor.shutdown(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServer.java new file mode 100644 index 0000000..f8517eb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ReadOnlyZooKeeperServer.java @@ -0,0 +1,190 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.PrintWriter; +import java.util.Objects; +import java.util.stream.Collectors; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.DataTreeBean; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.PrepRequestProcessor; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooKeeperServerBean; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; + +/** + * A ZooKeeperServer which comes into play when peer is partitioned from the + * majority. Handles read-only clients, but drops connections from not-read-only + * ones. + *

      + * The very first processor in the chain of request processors is a + * ReadOnlyRequestProcessor which drops state-changing requests. + */ +public class ReadOnlyZooKeeperServer extends ZooKeeperServer { + + protected final QuorumPeer self; + private volatile boolean shutdown = false; + + ReadOnlyZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self, ZKDatabase zkDb) { + super( + logFactory, + self.tickTime, + self.minSessionTimeout, + self.maxSessionTimeout, + self.clientPortListenBacklog, + zkDb, + self.getInitialConfig(), + self.isReconfigEnabled()); + this.self = self; + } + + @Override + protected void setupRequestProcessors() { + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + RequestProcessor prepProcessor = new PrepRequestProcessor(this, finalProcessor); + ((PrepRequestProcessor) prepProcessor).start(); + firstProcessor = new ReadOnlyRequestProcessor(this, prepProcessor); + ((ReadOnlyRequestProcessor) firstProcessor).start(); + } + + @Override + public synchronized void startup() { + // check to avoid startup follows shutdown + if (shutdown) { + LOG.warn("Not starting Read-only server as startup follows shutdown!"); + return; + } + registerJMX(new ReadOnlyBean(this), self.jmxLocalPeerBean); + super.startup(); + self.setZooKeeperServer(this); + self.adminServer.setZooKeeperServer(this); + LOG.info("Read-only server started"); + } + + @Override + protected void registerJMX() { + // register with JMX + try { + jmxDataTreeBean = new DataTreeBean(getZKDatabase().getDataTree()); + MBeanRegistry.getInstance().register(jmxDataTreeBean, jmxServerBean); + } catch (Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxDataTreeBean = null; + } + } + + public void registerJMX(ZooKeeperServerBean serverBean, LocalPeerBean localPeerBean) { + // register with JMX + try { + jmxServerBean = serverBean; + MBeanRegistry.getInstance().register(serverBean, localPeerBean); + } catch (Exception e) { + LOG.warn("Failed to register with JMX", e); + jmxServerBean = null; + } + } + + @Override + protected void unregisterJMX() { + // unregister from JMX + try { + if (jmxDataTreeBean != null) { + MBeanRegistry.getInstance().unregister(jmxDataTreeBean); + } + } catch (Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxDataTreeBean = null; + } + + protected void unregisterJMX(ZooKeeperServer zks) { + // unregister from JMX + try { + if (jmxServerBean != null) { + MBeanRegistry.getInstance().unregister(jmxServerBean); + } + } catch (Exception e) { + LOG.warn("Failed to unregister with JMX", e); + } + jmxServerBean = null; + } + + @Override + public String getState() { + return "read-only"; + } + + /** + * Returns the id of the associated QuorumPeer, which will do for a unique + * id of this server. + */ + @Override + public long getServerId() { + return self.getId(); + } + + @Override + public synchronized void shutdown() { + if (!canShutdown()) { + LOG.debug("ZooKeeper server is not running, so not proceeding to shutdown!"); + return; + } + shutdown = true; + unregisterJMX(this); + + // set peer's server to null + self.setZooKeeperServer(null); + // clear all the connections + self.closeAllConnections(); + + self.adminServer.setZooKeeperServer(null); + + // shutdown the server itself + super.shutdown(); + } + + @Override + public void dumpConf(PrintWriter pwriter) { + super.dumpConf(pwriter); + + pwriter.print("initLimit="); + pwriter.println(self.getInitLimit()); + pwriter.print("syncLimit="); + pwriter.println(self.getSyncLimit()); + pwriter.print("electionAlg="); + pwriter.println(self.getElectionType()); + pwriter.print("electionPort="); + pwriter.println(self.getElectionAddress().getAllPorts() + .stream().map(Objects::toString).collect(Collectors.joining("|"))); + pwriter.print("quorumPort="); + pwriter.println(self.getQuorumAddress().getAllPorts() + .stream().map(Objects::toString).collect(Collectors.joining("|"))); + pwriter.print("peerType="); + pwriter.println(self.getLearnerType().ordinal()); + } + + @Override + protected void setState(State state) { + this.state = state; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java new file mode 100644 index 0000000..522ba3a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerBean.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.common.NetUtils.formatInetAddr; +import java.util.stream.Collectors; +import org.apache.zookeeper.common.NetUtils; +import org.apache.zookeeper.jmx.ZKMBeanInfo; + +/** + * A remote peer bean only provides limited information about the remote peer, + * and the peer cannot be managed remotely. + */ +public class RemotePeerBean implements RemotePeerMXBean, ZKMBeanInfo { + + private QuorumPeer.QuorumServer peer; + private final QuorumPeer localPeer; + + public RemotePeerBean(QuorumPeer localPeer, QuorumPeer.QuorumServer peer) { + this.peer = peer; + this.localPeer = localPeer; + } + + public void setQuorumServer(QuorumPeer.QuorumServer peer) { + this.peer = peer; + } + + public String getName() { + return "replica." + peer.id; + } + public boolean isHidden() { + return false; + } + + public String getQuorumAddress() { + return peer.addr.getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|")); + } + + public String getElectionAddress() { + return peer.electionAddr.getAllAddresses().stream().map(NetUtils::formatInetAddr) + .collect(Collectors.joining("|")); + } + + public String getClientAddress() { + if (null == peer.clientAddr) { + return ""; + } + return formatInetAddr(peer.clientAddr); + } + + public String getLearnerType() { + return peer.type.toString(); + } + + @Override + public boolean isLeader() { + return localPeer.isLeader(peer.getId()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerMXBean.java new file mode 100644 index 0000000..412d7e6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/RemotePeerMXBean.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * A proxy for a remote quorum peer. + */ +public interface RemotePeerMXBean { + + /** + * @return name of the peer + */ + String getName(); + /** + * @return IP address of the quorum peer + */ + String getQuorumAddress(); + + /** + * @return the election address + */ + String getElectionAddress(); + + /** + * @return the client address + */ + String getClientAddress(); + + /** + * @return the learner type + */ + String getLearnerType(); + + /** + * @return true if the peer is the current leader + */ + boolean isLeader(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SendAckRequestProcessor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SendAckRequestProcessor.java new file mode 100644 index 0000000..8218dda --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SendAckRequestProcessor.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.Flushable; +import java.io.IOException; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerMetrics; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SendAckRequestProcessor implements RequestProcessor, Flushable { + + private static final Logger LOG = LoggerFactory.getLogger(SendAckRequestProcessor.class); + + Learner learner; + + SendAckRequestProcessor(Learner peer) { + this.learner = peer; + } + + public void processRequest(Request si) { + if (si.type != OpCode.sync) { + QuorumPacket qp = new QuorumPacket(Leader.ACK, si.getHdr().getZxid(), null, null); + try { + si.logLatency(ServerMetrics.getMetrics().PROPOSAL_ACK_CREATION_LATENCY); + + learner.writePacket(qp, false); + } catch (IOException e) { + LOG.warn("Closing connection to leader, exception during packet send", e); + try { + if (!learner.sock.isClosed()) { + learner.sock.close(); + } + } catch (IOException e1) { + // Nothing to do, we are shutting things down, so an exception here is irrelevant + LOG.debug("Ignoring error closing the connection", e1); + } + } + } + } + + public void flush() throws IOException { + try { + learner.writePacket(null, true); + } catch (IOException e) { + LOG.warn("Closing connection to leader, exception during packet send", e); + try { + if (!learner.sock.isClosed()) { + learner.sock.close(); + } + } catch (IOException e1) { + // Nothing to do, we are shutting things down, so an exception here is irrelevant + LOG.debug("Ignoring error closing the connection", e1); + } + } + } + + public void shutdown() { + // Nothing needed + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ServerBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ServerBean.java new file mode 100644 index 0000000..746e0ce --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ServerBean.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.Date; +import org.apache.zookeeper.jmx.ZKMBeanInfo; + +/** + * An abstract base class for the leader and follower MBeans. + */ +public abstract class ServerBean implements ServerMXBean, ZKMBeanInfo { + + private final Date startTime = new Date(); + + public boolean isHidden() { + return false; + } + + public String getStartTime() { + return startTime.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ServerMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ServerMXBean.java new file mode 100644 index 0000000..96b91f6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/ServerMXBean.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * A quorum server MBean. + */ +public interface ServerMXBean { + + /** + * @return name of the server MBean + */ + String getName(); + /** + * @return the start time the server + */ + String getStartTime(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/StateSummary.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/StateSummary.java new file mode 100644 index 0000000..584abd0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/StateSummary.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * This class encapsulates the state comparison logic. Specifically, + * how two different states are compared. + */ +public class StateSummary { + + private long currentEpoch; + private long lastZxid; + + public StateSummary(long currentEpoch, long lastZxid) { + this.currentEpoch = currentEpoch; + this.lastZxid = lastZxid; + } + + public long getCurrentEpoch() { + return currentEpoch; + } + + public long getLastZxid() { + return lastZxid; + } + + public boolean isMoreRecentThan(StateSummary ss) { + return (currentEpoch > ss.currentEpoch) || (currentEpoch == ss.currentEpoch && lastZxid > ss.lastZxid); + } + + @Override + public boolean equals(Object obj) { + if(!(obj instanceof StateSummary)) { + return false; + } + StateSummary ss = (StateSummary) obj; + return currentEpoch == ss.currentEpoch && lastZxid == ss.lastZxid; + } + + @Override + public int hashCode() { + return (int) (currentEpoch ^ lastZxid); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SyncThrottleException.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SyncThrottleException.java new file mode 100644 index 0000000..06bd2ae --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SyncThrottleException.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +/** + * Thrown when a {@link Leader} has too many concurrent syncs being sent + * to observers. + * + * @see LearnerSyncThrottler + * + */ +public class SyncThrottleException extends Exception { + + private static final long serialVersionUID = 1L; + + public SyncThrottleException(int concurrentSyncNumber, int throttleThreshold, LearnerSyncThrottler.SyncType syncType) { + super(getMessage(concurrentSyncNumber, throttleThreshold, syncType)); + } + + private static String getMessage(int concurrentSyncNumber, int throttleThreshold, LearnerSyncThrottler.SyncType syncType) { + return String.format("new %s sync would make %d concurrently in progress; maximum is %d", + syncType.toString().toLowerCase(), + concurrentSyncNumber, + throttleThreshold); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SyncedLearnerTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SyncedLearnerTracker.java new file mode 100644 index 0000000..83ddcc6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/SyncedLearnerTracker.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.ArrayList; +import java.util.HashSet; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; + +/************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ +public class SyncedLearnerTracker { + + protected ArrayList qvAcksetPairs = new ArrayList(); + + public void addQuorumVerifier(QuorumVerifier qv) { + qvAcksetPairs.add(new QuorumVerifierAcksetPair(qv, new HashSet(qv.getVotingMembers().size()))); + } + + public boolean addAck(Long sid) { + boolean change = false; + for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) { + if (qvAckset.getQuorumVerifier().getVotingMembers().containsKey(sid)) { + qvAckset.getAckset().add(sid); + change = true; + } + } + return change; + } + + public boolean hasSid(long sid) { + for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) { + if (!qvAckset.getQuorumVerifier().getVotingMembers().containsKey(sid)) { + return false; + } + } + return true; + } + + public boolean hasAllQuorums() { + + // TODO_MA 注释: 把每个集合都拿出来计算一下,看是否有满足条件的 + for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 判断: 少数服从多数 + * 算法实现: qvAckset.getQuorumVerifier() = QuorumMaj + */ + if (!qvAckset.getQuorumVerifier().containsQuorum(qvAckset.getAckset())) { + return false; + } + } + return true; + } + + public String ackSetsToString() { + StringBuilder sb = new StringBuilder(); + + for (QuorumVerifierAcksetPair qvAckset : qvAcksetPairs) { + sb.append(qvAckset.getAckset().toString()).append(","); + } + + return sb.substring(0, sb.length() - 1); + } + + public static class QuorumVerifierAcksetPair { + + private final QuorumVerifier qv; + private final HashSet ackset; + + public QuorumVerifierAcksetPair(QuorumVerifier qv, HashSet ackset) { + this.qv = qv; + this.ackset = ackset; + } + + public QuorumVerifier getQuorumVerifier() { + return this.qv; + } + + public HashSet getAckset() { + return this.ackset; + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/UnifiedServerSocket.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/UnifiedServerSocket.java new file mode 100644 index 0000000..387a6c7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/UnifiedServerSocket.java @@ -0,0 +1,789 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import io.netty.buffer.Unpooled; +import io.netty.handler.ssl.SslHandler; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.nio.channels.SocketChannel; +import javax.net.ssl.SSLSocket; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.common.X509Util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A ServerSocket that can act either as a regular ServerSocket, as a SSLServerSocket, or as both, depending on + * the constructor parameters and on the type of client (TLS or plaintext) that connects to it. + * The constructors have the same signature as constructors of ServerSocket, with the addition of two parameters + * at the beginning: + *

        + *
      • X509Util - provides the SSL context to construct a secure socket when a client connects with TLS.
      • + *
      • boolean allowInsecureConnection - when true, acts as a hybrid server socket (plaintext / TLS). When + * false, acts as a SSLServerSocket (rejects plaintext connections).
      • + *
      + * The !allowInsecureConnection mode is needed so we can update the SSLContext (in particular, the + * key store and/or trust store) without having to re-create the server socket. By starting with a plaintext socket + * and delaying the upgrade to TLS until after a client has connected and begins a handshake, we can keep the same + * UnifiedServerSocket instance around, and replace the default SSLContext in the provided X509Util when the key store + * and/or trust store file changes on disk. + */ +public class UnifiedServerSocket extends ServerSocket { + + private static final Logger LOG = LoggerFactory.getLogger(UnifiedServerSocket.class); + + private X509Util x509Util; + private final boolean allowInsecureConnection; + + /** + * Creates an unbound unified server socket by calling {@link ServerSocket#ServerSocket()}. + * Secure client connections will be upgraded to TLS once this socket detects the ClientHello message (start of a + * TLS handshake). Plaintext client connections will either be accepted or rejected depending on the value of + * the allowInsecureConnection parameter. + * @param x509Util the X509Util that provides the SSLContext to use for secure connections. + * @param allowInsecureConnection if true, accept plaintext connections, otherwise close them. + * @throws IOException if {@link ServerSocket#ServerSocket()} throws. + */ + public UnifiedServerSocket(X509Util x509Util, boolean allowInsecureConnection) throws IOException { + super(); + this.x509Util = x509Util; + this.allowInsecureConnection = allowInsecureConnection; + } + + /** + * Creates a unified server socket bound to the specified port by calling {@link ServerSocket#ServerSocket(int)}. + * Secure client connections will be upgraded to TLS once this socket detects the ClientHello message (start of a + * TLS handshake). Plaintext client connections will either be accepted or rejected depending on the value of + * the allowInsecureConnection parameter. + * @param x509Util the X509Util that provides the SSLContext to use for secure connections. + * @param allowInsecureConnection if true, accept plaintext connections, otherwise close them. + * @param port the port number, or {@code 0} to use a port number that is automatically allocated. + * @throws IOException if {@link ServerSocket#ServerSocket(int)} throws. + */ + public UnifiedServerSocket(X509Util x509Util, boolean allowInsecureConnection, int port) throws IOException { + super(port); + this.x509Util = x509Util; + this.allowInsecureConnection = allowInsecureConnection; + } + + /** + * Creates a unified server socket bound to the specified port, with the specified backlog, by calling + * {@link ServerSocket#ServerSocket(int, int)}. + * Secure client connections will be upgraded to TLS once this socket detects the ClientHello message (start of a + * TLS handshake). Plaintext client connections will either be accepted or rejected depending on the value of + * the allowInsecureConnection parameter. + * @param x509Util the X509Util that provides the SSLContext to use for secure connections. + * @param allowInsecureConnection if true, accept plaintext connections, otherwise close them. + * @param port the port number, or {@code 0} to use a port number that is automatically allocated. + * @param backlog requested maximum length of the queue of incoming connections. + * @throws IOException if {@link ServerSocket#ServerSocket(int, int)} throws. + */ + public UnifiedServerSocket(X509Util x509Util, boolean allowInsecureConnection, int port, int backlog) throws IOException { + super(port, backlog); + this.x509Util = x509Util; + this.allowInsecureConnection = allowInsecureConnection; + } + + /** + * Creates a unified server socket bound to the specified port, with the specified backlog, and local IP address + * to bind to, by calling {@link ServerSocket#ServerSocket(int, int, InetAddress)}. + * Secure client connections will be upgraded to TLS once this socket detects the ClientHello message (start of a + * TLS handshake). Plaintext client connections will either be accepted or rejected depending on the value of + * the allowInsecureConnection parameter. + * @param x509Util the X509Util that provides the SSLContext to use for secure connections. + * @param allowInsecureConnection if true, accept plaintext connections, otherwise close them. + * @param port the port number, or {@code 0} to use a port number that is automatically allocated. + * @param backlog requested maximum length of the queue of incoming connections. + * @param bindAddr the local InetAddress the server will bind to. + * @throws IOException if {@link ServerSocket#ServerSocket(int, int, InetAddress)} throws. + */ + public UnifiedServerSocket(X509Util x509Util, boolean allowInsecureConnection, int port, int backlog, InetAddress bindAddr) throws IOException { + super(port, backlog, bindAddr); + this.x509Util = x509Util; + this.allowInsecureConnection = allowInsecureConnection; + } + + @Override + public Socket accept() throws IOException { + if (isClosed()) { + throw new SocketException("Socket is closed"); + } + if (!isBound()) { + throw new SocketException("Socket is not bound yet"); + } + final PrependableSocket prependableSocket = new PrependableSocket(null); + implAccept(prependableSocket); + return new UnifiedSocket(x509Util, allowInsecureConnection, prependableSocket); + } + + /** + * The result of calling accept() on a UnifiedServerSocket. This is a Socket that doesn't know if it's + * using plaintext or SSL/TLS at the time when it is created. Calling a method that indicates a desire to + * read or write from the socket will cause the socket to detect if the connected client is attempting + * to establish a TLS or plaintext connection. This is done by doing a blocking read of 5 bytes off the + * socket and checking if the bytes look like the start of a TLS ClientHello message. If it looks like + * the client is attempting to connect with TLS, the internal socket is upgraded to a SSLSocket. If not, + * any bytes read from the socket are pushed back to the input stream, and the socket continues + * to be treated as a plaintext socket. + * + * The methods that trigger this behavior are: + *
        + *
      • {@link UnifiedSocket#getInputStream()}
      • + *
      • {@link UnifiedSocket#getOutputStream()}
      • + *
      • {@link UnifiedSocket#sendUrgentData(int)}
      • + *
      + * + * Calling other socket methods (i.e option setters such as {@link Socket#setTcpNoDelay(boolean)}) does + * not trigger mode detection. + * + * Because detecting the mode is a potentially blocking operation, it should not be done in the + * accepting thread. Attempting to read from or write to the socket in the accepting thread opens the + * caller up to a denial-of-service attack, in which a client connects and then does nothing. This would + * prevent any other clients from connecting. Passing the socket returned by accept() to a separate + * thread which handles all read and write operations protects against this DoS attack. + * + * Callers can check if the socket has been upgraded to TLS by calling {@link UnifiedSocket#isSecureSocket()}, + * and can get the underlying SSLSocket by calling {@link UnifiedSocket#getSslSocket()}. + */ + public static class UnifiedSocket extends Socket { + + private enum Mode { + UNKNOWN, + PLAINTEXT, + TLS + } + + private final X509Util x509Util; + private final boolean allowInsecureConnection; + private PrependableSocket prependableSocket; + private SSLSocket sslSocket; + private Mode mode; + + /** + * Note: this constructor is intentionally private. The only intended caller is + * {@link UnifiedServerSocket#accept()}. + * + * @param x509Util + * @param allowInsecureConnection + * @param prependableSocket + */ + private UnifiedSocket(X509Util x509Util, boolean allowInsecureConnection, PrependableSocket prependableSocket) { + this.x509Util = x509Util; + this.allowInsecureConnection = allowInsecureConnection; + this.prependableSocket = prependableSocket; + this.sslSocket = null; + this.mode = Mode.UNKNOWN; + } + + /** + * Returns true if the socket mode has been determined to be TLS. + * @return true if the mode is TLS, false if it is UNKNOWN or PLAINTEXT. + */ + public boolean isSecureSocket() { + return mode == Mode.TLS; + } + + /** + * Returns true if the socket mode has been determined to be PLAINTEXT. + * @return true if the mode is PLAINTEXT, false if it is UNKNOWN or TLS. + */ + public boolean isPlaintextSocket() { + return mode == Mode.PLAINTEXT; + } + + /** + * Returns true if the socket mode is not yet known. + * @return true if the mode is UNKNOWN, false if it is PLAINTEXT or TLS. + */ + public boolean isModeKnown() { + return mode != Mode.UNKNOWN; + } + + /** + * Detects the socket mode, see comments at the top of the class for more details. This operation will block + * for up to {@link X509Util#getSslHandshakeTimeoutMillis()} milliseconds and should not be called in the + * accept() thread if possible. + * @throws IOException + */ + private void detectMode() throws IOException { + byte[] litmus = new byte[5]; + int oldTimeout = -1; + int bytesRead = 0; + int newTimeout = x509Util.getSslHandshakeTimeoutMillis(); + try { + oldTimeout = prependableSocket.getSoTimeout(); + prependableSocket.setSoTimeout(newTimeout); + bytesRead = prependableSocket.getInputStream().read(litmus, 0, litmus.length); + } catch (SocketTimeoutException e) { + // Didn't read anything within the timeout, fallthrough and assume the connection is plaintext. + LOG.warn("Socket mode detection timed out after {} ms, assuming PLAINTEXT", newTimeout); + } finally { + // restore socket timeout to the old value + try { + if (oldTimeout != -1) { + prependableSocket.setSoTimeout(oldTimeout); + } + } catch (Exception e) { + LOG.warn("Failed to restore old socket timeout value of {} ms", oldTimeout, e); + } + } + if (bytesRead < 0) { // Got a EOF right away, definitely not using TLS. Fallthrough. + bytesRead = 0; + } + + if (bytesRead == litmus.length && SslHandler.isEncrypted(Unpooled.wrappedBuffer(litmus))) { + try { + sslSocket = x509Util.createSSLSocket(prependableSocket, litmus); + } catch (X509Exception e) { + throw new IOException("failed to create SSL context", e); + } + prependableSocket = null; + mode = Mode.TLS; + LOG.info( + "Accepted TLS connection from {} - {} - {}", + sslSocket.getRemoteSocketAddress(), + sslSocket.getSession().getProtocol(), + sslSocket.getSession().getCipherSuite()); + } else if (allowInsecureConnection) { + prependableSocket.prependToInputStream(litmus, 0, bytesRead); + mode = Mode.PLAINTEXT; + LOG.info("Accepted plaintext connection from {}", prependableSocket.getRemoteSocketAddress()); + } else { + prependableSocket.close(); + mode = Mode.PLAINTEXT; + throw new IOException("Blocked insecure connection attempt"); + } + } + + private Socket getSocketAllowUnknownMode() { + if (isSecureSocket()) { + return sslSocket; + } else { // Note: mode is UNKNOWN or PLAINTEXT + return prependableSocket; + } + } + + /** + * Returns the underlying socket, detecting the socket mode if it is not yet known. This is a potentially + * blocking operation and should not be called in the accept() thread. + * @return the underlying socket, after the socket mode has been determined. + * @throws IOException + */ + private Socket getSocket() throws IOException { + if (!isModeKnown()) { + detectMode(); + } + if (mode == Mode.TLS) { + return sslSocket; + } else { + return prependableSocket; + } + } + + /** + * Returns the underlying SSLSocket if the mode is TLS. If the mode is UNKNOWN, causes mode detection which is a + * potentially blocking operation. If the mode ends up being PLAINTEXT, this will throw a SocketException, so + * callers are advised to only call this method after checking that {@link UnifiedSocket#isSecureSocket()} + * returned true. + * @return the underlying SSLSocket if the mode is known to be TLS. + * @throws IOException if detecting the socket mode fails + * @throws SocketException if the mode is PLAINTEXT. + */ + public SSLSocket getSslSocket() throws IOException { + if (!isModeKnown()) { + detectMode(); + } + if (!isSecureSocket()) { + throw new SocketException("Socket mode is not TLS"); + } + return sslSocket; + } + + /** + * See {@link Socket#connect(SocketAddress)}. Calling this method does not trigger mode detection. + */ + @Override + public void connect(SocketAddress endpoint) throws IOException { + getSocketAllowUnknownMode().connect(endpoint); + } + + /** + * See {@link Socket#connect(SocketAddress, int)}. Calling this method does not trigger mode detection. + */ + @Override + public void connect(SocketAddress endpoint, int timeout) throws IOException { + getSocketAllowUnknownMode().connect(endpoint, timeout); + } + + /** + * See {@link Socket#bind(SocketAddress)}. Calling this method does not trigger mode detection. + */ + @Override + public void bind(SocketAddress bindpoint) throws IOException { + getSocketAllowUnknownMode().bind(bindpoint); + } + + /** + * See {@link Socket#getInetAddress()}. Calling this method does not trigger mode detection. + */ + @Override + public InetAddress getInetAddress() { + return getSocketAllowUnknownMode().getInetAddress(); + } + + /** + * See {@link Socket#getLocalAddress()}. Calling this method does not trigger mode detection. + */ + @Override + public InetAddress getLocalAddress() { + return getSocketAllowUnknownMode().getLocalAddress(); + } + + /** + * See {@link Socket#getPort()}. Calling this method does not trigger mode detection. + */ + @Override + public int getPort() { + return getSocketAllowUnknownMode().getPort(); + } + + /** + * See {@link Socket#getLocalPort()}. Calling this method does not trigger mode detection. + */ + @Override + public int getLocalPort() { + return getSocketAllowUnknownMode().getLocalPort(); + } + + /** + * See {@link Socket#getRemoteSocketAddress()}. Calling this method does not trigger mode detection. + */ + @Override + public SocketAddress getRemoteSocketAddress() { + return getSocketAllowUnknownMode().getRemoteSocketAddress(); + } + + /** + * See {@link Socket#getLocalSocketAddress()}. Calling this method does not trigger mode detection. + */ + @Override + public SocketAddress getLocalSocketAddress() { + return getSocketAllowUnknownMode().getLocalSocketAddress(); + } + + /** + * See {@link Socket#getChannel()}. Calling this method does not trigger mode detection. + */ + @Override + public SocketChannel getChannel() { + return getSocketAllowUnknownMode().getChannel(); + } + + /** + * See {@link Socket#getInputStream()}. If the socket mode has not yet been detected, the first read from the + * returned input stream will trigger mode detection, which is a potentially blocking operation. This means + * the accept() thread should avoid reading from this input stream if possible. + */ + @Override + public InputStream getInputStream() throws IOException { + return new UnifiedInputStream(this); + } + + /** + * See {@link Socket#getOutputStream()}. If the socket mode has not yet been detected, the first read from the + * returned input stream will trigger mode detection, which is a potentially blocking operation. This means + * the accept() thread should avoid reading from this input stream if possible. + */ + @Override + public OutputStream getOutputStream() throws IOException { + return new UnifiedOutputStream(this); + } + + /** + * See {@link Socket#setTcpNoDelay(boolean)}. Calling this method does not trigger mode detection. + */ + @Override + public void setTcpNoDelay(boolean on) throws SocketException { + getSocketAllowUnknownMode().setTcpNoDelay(on); + } + + /** + * See {@link Socket#getTcpNoDelay()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean getTcpNoDelay() throws SocketException { + return getSocketAllowUnknownMode().getTcpNoDelay(); + } + + /** + * See {@link Socket#setSoLinger(boolean, int)}. Calling this method does not trigger mode detection. + */ + @Override + public void setSoLinger(boolean on, int linger) throws SocketException { + getSocketAllowUnknownMode().setSoLinger(on, linger); + } + + /** + * See {@link Socket#getSoLinger()}. Calling this method does not trigger mode detection. + */ + @Override + public int getSoLinger() throws SocketException { + return getSocketAllowUnknownMode().getSoLinger(); + } + + /** + * See {@link Socket#sendUrgentData(int)}. Calling this method triggers mode detection, which is a potentially + * blocking operation, so it should not be done in the accept() thread. + */ + @Override + public void sendUrgentData(int data) throws IOException { + getSocket().sendUrgentData(data); + } + + /** + * See {@link Socket#setOOBInline(boolean)}. Calling this method does not trigger mode detection. + */ + @Override + public void setOOBInline(boolean on) throws SocketException { + getSocketAllowUnknownMode().setOOBInline(on); + } + + /** + * See {@link Socket#getOOBInline()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean getOOBInline() throws SocketException { + return getSocketAllowUnknownMode().getOOBInline(); + } + + /** + * See {@link Socket#setSoTimeout(int)}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized void setSoTimeout(int timeout) throws SocketException { + getSocketAllowUnknownMode().setSoTimeout(timeout); + } + + /** + * See {@link Socket#getSoTimeout()}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized int getSoTimeout() throws SocketException { + return getSocketAllowUnknownMode().getSoTimeout(); + } + + /** + * See {@link Socket#setSendBufferSize(int)}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized void setSendBufferSize(int size) throws SocketException { + getSocketAllowUnknownMode().setSendBufferSize(size); + } + + /** + * See {@link Socket#getSendBufferSize()}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized int getSendBufferSize() throws SocketException { + return getSocketAllowUnknownMode().getSendBufferSize(); + } + + /** + * See {@link Socket#setReceiveBufferSize(int)}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized void setReceiveBufferSize(int size) throws SocketException { + getSocketAllowUnknownMode().setReceiveBufferSize(size); + } + + /** + * See {@link Socket#getReceiveBufferSize()}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized int getReceiveBufferSize() throws SocketException { + return getSocketAllowUnknownMode().getReceiveBufferSize(); + } + + /** + * See {@link Socket#setKeepAlive(boolean)}. Calling this method does not trigger mode detection. + */ + @Override + public void setKeepAlive(boolean on) throws SocketException { + getSocketAllowUnknownMode().setKeepAlive(on); + } + + /** + * See {@link Socket#getKeepAlive()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean getKeepAlive() throws SocketException { + return getSocketAllowUnknownMode().getKeepAlive(); + } + + /** + * See {@link Socket#setTrafficClass(int)}. Calling this method does not trigger mode detection. + */ + @Override + public void setTrafficClass(int tc) throws SocketException { + getSocketAllowUnknownMode().setTrafficClass(tc); + } + + /** + * See {@link Socket#getTrafficClass()}. Calling this method does not trigger mode detection. + */ + @Override + public int getTrafficClass() throws SocketException { + return getSocketAllowUnknownMode().getTrafficClass(); + } + + /** + * See {@link Socket#setReuseAddress(boolean)}. Calling this method does not trigger mode detection. + */ + @Override + public void setReuseAddress(boolean on) throws SocketException { + getSocketAllowUnknownMode().setReuseAddress(on); + } + + /** + * See {@link Socket#getReuseAddress()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean getReuseAddress() throws SocketException { + return getSocketAllowUnknownMode().getReuseAddress(); + } + + /** + * See {@link Socket#close()}. Calling this method does not trigger mode detection. + */ + @Override + public synchronized void close() throws IOException { + getSocketAllowUnknownMode().close(); + } + + /** + * See {@link Socket#shutdownInput()}. Calling this method does not trigger mode detection. + */ + @Override + public void shutdownInput() throws IOException { + getSocketAllowUnknownMode().shutdownInput(); + } + + /** + * See {@link Socket#shutdownOutput()}. Calling this method does not trigger mode detection. + */ + @Override + public void shutdownOutput() throws IOException { + getSocketAllowUnknownMode().shutdownOutput(); + } + + /** + * See {@link Socket#toString()}. Calling this method does not trigger mode detection. + */ + @Override + public String toString() { + return "UnifiedSocket[mode=" + mode.toString() + "socket=" + getSocketAllowUnknownMode().toString() + "]"; + } + + /** + * See {@link Socket#isConnected()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean isConnected() { + return getSocketAllowUnknownMode().isConnected(); + } + + /** + * See {@link Socket#isBound()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean isBound() { + return getSocketAllowUnknownMode().isBound(); + } + + /** + * See {@link Socket#isClosed()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean isClosed() { + return getSocketAllowUnknownMode().isClosed(); + } + + /** + * See {@link Socket#isInputShutdown()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean isInputShutdown() { + return getSocketAllowUnknownMode().isInputShutdown(); + } + + /** + * See {@link Socket#isOutputShutdown()}. Calling this method does not trigger mode detection. + */ + @Override + public boolean isOutputShutdown() { + return getSocketAllowUnknownMode().isOutputShutdown(); + } + + /** + * See {@link Socket#setPerformancePreferences(int, int, int)}. Calling this method does not trigger + * mode detection. + */ + @Override + public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) { + getSocketAllowUnknownMode().setPerformancePreferences(connectionTime, latency, bandwidth); + } + + } + + /** + * An input stream for a UnifiedSocket. The first read from this stream will trigger mode detection on the + * underlying UnifiedSocket. + */ + private static class UnifiedInputStream extends InputStream { + + private final UnifiedSocket unifiedSocket; + private InputStream realInputStream; + + private UnifiedInputStream(UnifiedSocket unifiedSocket) { + this.unifiedSocket = unifiedSocket; + this.realInputStream = null; + } + + @Override + public int read() throws IOException { + return getRealInputStream().read(); + } + + /** + * Note: SocketInputStream has optimized implementations of bulk-read operations, so we need to call them + * directly instead of relying on the base-class implementation which just calls the single-byte read() over + * and over. Not implementing these results in awful performance. + */ + @Override + public int read(byte[] b) throws IOException { + return getRealInputStream().read(b); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + return getRealInputStream().read(b, off, len); + } + + private InputStream getRealInputStream() throws IOException { + if (realInputStream == null) { + // Note: The first call to getSocket() triggers mode detection which can block + realInputStream = unifiedSocket.getSocket().getInputStream(); + } + return realInputStream; + } + + @Override + public long skip(long n) throws IOException { + return getRealInputStream().skip(n); + } + + @Override + public int available() throws IOException { + return getRealInputStream().available(); + } + + @Override + public void close() throws IOException { + getRealInputStream().close(); + } + + @Override + public synchronized void mark(int readlimit) { + try { + getRealInputStream().mark(readlimit); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public synchronized void reset() throws IOException { + getRealInputStream().reset(); + } + + @Override + public boolean markSupported() { + try { + return getRealInputStream().markSupported(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + } + + private static class UnifiedOutputStream extends OutputStream { + + private final UnifiedSocket unifiedSocket; + private OutputStream realOutputStream; + + private UnifiedOutputStream(UnifiedSocket unifiedSocket) { + this.unifiedSocket = unifiedSocket; + this.realOutputStream = null; + } + + @Override + public void write(int b) throws IOException { + getRealOutputStream().write(b); + } + + @Override + public void write(byte[] b) throws IOException { + getRealOutputStream().write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + getRealOutputStream().write(b, off, len); + } + + @Override + public void flush() throws IOException { + getRealOutputStream().flush(); + } + + @Override + public void close() throws IOException { + getRealOutputStream().close(); + } + + private OutputStream getRealOutputStream() throws IOException { + if (realOutputStream == null) { + // Note: The first call to getSocket() triggers mode detection which can block + realOutputStream = unifiedSocket.getSocket().getOutputStream(); + } + return realOutputStream; + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/UpgradeableSessionTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/UpgradeableSessionTracker.java new file mode 100644 index 0000000..a2ff8ca --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/UpgradeableSessionTracker.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.server.SessionTracker; +import org.apache.zookeeper.server.ZooKeeperServerListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A session tracker that supports upgradeable local sessions. + */ +public abstract class UpgradeableSessionTracker implements SessionTracker { + + private static final Logger LOG = LoggerFactory.getLogger(UpgradeableSessionTracker.class); + + private ConcurrentMap localSessionsWithTimeouts; + private ConcurrentMap upgradingSessions; + protected LocalSessionTracker localSessionTracker; + protected boolean localSessionsEnabled; + + public void start() { + } + + public void createLocalSessionTracker(SessionExpirer expirer, int tickTime, long id, + ZooKeeperServerListener listener) { + this.localSessionsWithTimeouts = new ConcurrentHashMap(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + this.localSessionTracker = new LocalSessionTracker(expirer, this.localSessionsWithTimeouts, tickTime, id, + listener); + this.upgradingSessions = new ConcurrentHashMap(); + } + + public boolean isTrackingSession(long sessionId) { + return isLocalSession(sessionId) || isGlobalSession(sessionId); + } + + public boolean isLocalSession(long sessionId) { + return localSessionTracker != null && localSessionTracker.isTrackingSession(sessionId); + } + + @Override + public boolean isLocalSessionsEnabled() { + return localSessionsEnabled; + } + + public boolean isUpgradingSession(long sessionId) { + return upgradingSessions != null && upgradingSessions.containsKey(sessionId); + } + + public void finishedUpgrading(long sessionId) { + if(upgradingSessions != null) { + upgradingSessions.remove(sessionId); + } + } + + public abstract boolean isGlobalSession(long sessionId); + + /** + * Upgrades the session to a global session. + * This simply removes the session from the local tracker and marks + * it as global. It is up to the caller to actually + * queue up a transaction for the session. + * + * @param sessionId + * @return session timeout (-1 if not a local session) + */ + public int upgradeSession(long sessionId) { + if(localSessionsWithTimeouts == null) { + return -1; + } + // We won't race another upgrade attempt because only one thread + // will get the timeout from the map + Integer timeout = localSessionsWithTimeouts.remove(sessionId); + if(timeout != null) { + LOG.info("Upgrading session 0x{}", Long.toHexString(sessionId)); + // Track global session, which will add to global session tracker + // on leader and do nothing on learner. Need to start track global + // session in leader now to update the session expire between + // LeaderRequestProcessor and PrepRequestProcessor. + trackSession(sessionId, timeout); + // Track ongoing upgrading sessions, learner will use it to find + // other sessions it has which are not in local and global sessions + upgradingSessions.put(sessionId, timeout); + localSessionTracker.removeSession(sessionId); + return timeout; + } + return -1; + } + + protected void removeLocalSession(long sessionId) { + if(localSessionTracker == null) { + return; + } + localSessionTracker.removeSession(sessionId); + } + + public void checkGlobalSession(long sessionId, + Object owner) throws KeeperException.SessionExpiredException, KeeperException.SessionMovedException { + throw new UnsupportedOperationException(); + } + + public long getLocalSessionCount() { + if(localSessionsWithTimeouts == null) { + return 0; + } + return localSessionsWithTimeouts.size(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Vote.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Vote.java new file mode 100644 index 0000000..a970584 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Vote.java @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; + +public class Vote { + + public Vote(long id, long zxid) { + this.version = 0x0; + this.id = id; + this.zxid = zxid; + this.electionEpoch = -1; + this.peerEpoch = -1; + this.state = ServerState.LOOKING; + } + + public Vote(long id, long zxid, long peerEpoch) { + this.version = 0x0; + this.id = id; + this.zxid = zxid; + this.electionEpoch = -1; + this.peerEpoch = peerEpoch; + this.state = ServerState.LOOKING; + } + + public Vote(long id, long zxid, long electionEpoch, long peerEpoch) { + this.version = 0x0; + this.id = id; + this.zxid = zxid; + this.electionEpoch = electionEpoch; + this.peerEpoch = peerEpoch; + this.state = ServerState.LOOKING; + } + + public Vote(int version, long id, long zxid, long electionEpoch, long peerEpoch, ServerState state) { + this.version = version; + this.id = id; // TODO_MA 注释: myid serverid sid + this.zxid = zxid; // TODO_MA 注释: zxid + this.electionEpoch = electionEpoch; + this.state = state; + this.peerEpoch = peerEpoch; + } + + public Vote(long id, long zxid, long electionEpoch, long peerEpoch, ServerState state) { + this.id = id; + this.zxid = zxid; + this.electionEpoch = electionEpoch; + this.state = state; + this.peerEpoch = peerEpoch; + this.version = 0x0; + } + + // TODO_MA 注释: 版本 + private final int version; + + // TODO_MA 注释: myid = serverid + private final long id; + + // TODO_MA 注释: zxid + private final long zxid; + + // TODO_MA 注释: 选举轮次 + private final long electionEpoch; + + // TODO_MA 注释: 选举轮次 + private final long peerEpoch; + + public int getVersion() { + return version; + } + + public long getId() { + return id; + } + + public long getZxid() { + return zxid; + } + + public long getElectionEpoch() { + return electionEpoch; + } + + public long getPeerEpoch() { + return peerEpoch; + } + + public ServerState getState() { + return state; + } + + private final ServerState state; + + @Override + public boolean equals(Object o) { + if(!(o instanceof Vote)) { + return false; + } + Vote other = (Vote) o; + + if((state == ServerState.LOOKING) || (other.state == ServerState.LOOKING)) { + return id == other.id && zxid == other.zxid && electionEpoch == other.electionEpoch && peerEpoch == other.peerEpoch; + } else { + /* + * There are two things going on in the logic below: + * + * 1. skip comparing the zxid and electionEpoch for votes for servers + * out of election. + * + * Need to skip those because they can be inconsistent due to + * scenarios described in QuorumPeer.updateElectionVote. + * + * And given that only one ensemble can be running at a single point + * in time and that each epoch is used only once, using only id and + * epoch to compare the votes is sufficient. + * + * {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1805} + * + * 2. skip comparing peerEpoch if if we're running with mixed ensemble + * with (version > 0x0) and without the change (version = 0x0) + * introduced in ZOOKEEPER-1732. + * + * {@see https://issues.apache.org/jira/browse/ZOOKEEPER-1732} + * + * The server running with and without ZOOKEEPER-1732 will return + * different peerEpoch. During rolling upgrades, it's possible + * that 2/5 servers are returning epoch 1, while the other 2/5 + * are returning epoch 2, the other server need to ignore the + * peerEpoch to be able to join it. + */ + if((version > 0x0) ^ (other.version > 0x0)) { + return id == other.id; + } else { + return (id == other.id && peerEpoch == other.peerEpoch); + } + } + } + + @Override + public int hashCode() { + return (int) (id & zxid); + } + + public String toString() { + return "(" + id + ", " + Long.toHexString(zxid) + ", " + Long.toHexString(peerEpoch) + ")"; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthLearner.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthLearner.java new file mode 100644 index 0000000..1b120b7 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthLearner.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.net.Socket; + +/** + * This class represents no authentication learner, it just return + * without performing any authentication. + */ +public class NullQuorumAuthLearner implements QuorumAuthLearner { + + @Override + public void authenticate(Socket sock, String hostname) { + return; // simply return don't require auth + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthServer.java new file mode 100644 index 0000000..96be1da --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/NullQuorumAuthServer.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.DataInputStream; +import java.net.Socket; + +/** + * This class represents no authentication server, it just return + * without performing any authentication. + */ +public class NullQuorumAuthServer implements QuorumAuthServer { + + @Override + public void authenticate(final Socket sock, final DataInputStream din) { + // simply return don't require auth + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuth.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuth.java new file mode 100644 index 0000000..9e5f914 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuth.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.DataInputStream; +import java.io.IOException; +import org.apache.jute.BinaryInputArchive; +import org.apache.zookeeper.server.quorum.QuorumAuthPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumAuth { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumAuth.class); + + public static final String QUORUM_SASL_AUTH_ENABLED = "quorum.auth.enableSasl"; + public static final String QUORUM_SERVER_SASL_AUTH_REQUIRED = "quorum.auth.serverRequireSasl"; + public static final String QUORUM_LEARNER_SASL_AUTH_REQUIRED = "quorum.auth.learnerRequireSasl"; + + public static final String QUORUM_KERBEROS_SERVICE_PRINCIPAL = "quorum.auth.kerberos.servicePrincipal"; + public static final String QUORUM_KERBEROS_SERVICE_PRINCIPAL_DEFAULT_VALUE = "zkquorum/localhost"; + + public static final String QUORUM_LEARNER_SASL_LOGIN_CONTEXT = "quorum.auth.learner.saslLoginContext"; + public static final String QUORUM_LEARNER_SASL_LOGIN_CONTEXT_DFAULT_VALUE = "QuorumLearner"; + + public static final String QUORUM_SERVER_SASL_LOGIN_CONTEXT = "quorum.auth.server.saslLoginContext"; + public static final String QUORUM_SERVER_SASL_LOGIN_CONTEXT_DFAULT_VALUE = "QuorumServer"; + + static final String QUORUM_SERVER_PROTOCOL_NAME = "zookeeper-quorum"; + static final String QUORUM_SERVER_SASL_DIGEST = "zk-quorum-sasl-md5"; + static final String QUORUM_AUTH_MESSAGE_TAG = "qpconnect"; + + // this is negative, so that if a learner that does auth, connects to a + // server, it'll think the received packet is an authentication packet + public static final long QUORUM_AUTH_MAGIC_NUMBER = -0xa0dbcafecafe1234L; + + public enum Status { + IN_PROGRESS(0), + SUCCESS(1), + ERROR(-1); + private int status; + + Status(int status) { + this.status = status; + } + + static Status getStatus(int status) { + switch (status) { + case 0: + return IN_PROGRESS; + case 1: + return SUCCESS; + case -1: + return ERROR; + default: + LOG.error("Unknown status:{}!", status); + assert false : "Unknown status!"; + return ERROR; + } + } + + int status() { + return status; + } + } + + public static QuorumAuthPacket createPacket(Status status, byte[] response) { + return new QuorumAuthPacket(QUORUM_AUTH_MAGIC_NUMBER, status.status(), response); + } + + public static boolean nextPacketIsAuth(DataInputStream din) throws IOException { + din.mark(32); + BinaryInputArchive bia = new BinaryInputArchive(din); + boolean firstIsAuth = (bia.readLong("NO_TAG") == QuorumAuth.QUORUM_AUTH_MAGIC_NUMBER); + din.reset(); + return firstIsAuth; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthLearner.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthLearner.java new file mode 100644 index 0000000..02ac326 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthLearner.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.IOException; +import java.net.Socket; + +/** + * Interface for quorum learner authentication mechanisms. + */ +public interface QuorumAuthLearner { + + /** + * Performs an authentication step for the given socket connection. + * + * @param sock + * socket connection to other quorum peer server + * @param hostname + * host name of other quorum peer server + * @throws IOException + * if there is an authentication failure + */ + void authenticate(Socket sock, String hostname) throws IOException; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthServer.java new file mode 100644 index 0000000..8f771d4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthServer.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.DataInputStream; +import java.io.IOException; +import java.net.Socket; + +/** + * Interface for quorum server authentication mechanisms. + */ +public interface QuorumAuthServer { + + /** + * Performs an authentication step for the given socket connection. + * + * @param sock + * socket connection to other quorum peer + * @param din + * stream used to read auth data send by the quorum learner + * @throws IOException if the server fails to authenticate connecting quorum learner + */ + void authenticate(Socket sock, DataInputStream din) throws IOException; + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthLearner.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthLearner.java new file mode 100644 index 0000000..12cec78 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthLearner.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import javax.security.auth.Subject; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginException; +import javax.security.sasl.SaslClient; +import javax.security.sasl.SaslException; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.Login; +import org.apache.zookeeper.SaslClientCallbackHandler; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.server.quorum.QuorumAuthPacket; +import org.apache.zookeeper.util.SecurityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SaslQuorumAuthLearner implements QuorumAuthLearner { + + private static final Logger LOG = LoggerFactory.getLogger(SaslQuorumAuthLearner.class); + + private final Login learnerLogin; + private final boolean quorumRequireSasl; + private final String quorumServicePrincipal; + + public SaslQuorumAuthLearner( + boolean quorumRequireSasl, + String quorumServicePrincipal, + String loginContext) throws SaslException { + this.quorumRequireSasl = quorumRequireSasl; + this.quorumServicePrincipal = quorumServicePrincipal; + try { + AppConfigurationEntry[] entries = Configuration.getConfiguration().getAppConfigurationEntry(loginContext); + if (entries == null || entries.length == 0) { + throw new LoginException(String.format( + "SASL-authentication failed because the specified JAAS configuration section '%s' could not be found.", + loginContext)); + } + this.learnerLogin = new Login( + loginContext, + new SaslClientCallbackHandler(null, "QuorumLearner"), + new ZKConfig()); + this.learnerLogin.startThreadIfNeeded(); + } catch (LoginException e) { + throw new SaslException("Failed to initialize authentication mechanism using SASL", e); + } + } + + @Override + public void authenticate(Socket sock, String hostName) throws IOException { + if (!quorumRequireSasl) { // let it through, we don't require auth + LOG.info( + "Skipping SASL authentication as {}={}", + QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, + quorumRequireSasl); + return; + } + SaslClient sc = null; + String principalConfig = SecurityUtils.getServerPrincipal(quorumServicePrincipal, hostName); + try { + DataOutputStream dout = new DataOutputStream(sock.getOutputStream()); + DataInputStream din = new DataInputStream(sock.getInputStream()); + byte[] responseToken = new byte[0]; + sc = SecurityUtils.createSaslClient( + learnerLogin.getSubject(), + principalConfig, + QuorumAuth.QUORUM_SERVER_PROTOCOL_NAME, + QuorumAuth.QUORUM_SERVER_SASL_DIGEST, + LOG, + "QuorumLearner"); + + if (sc.hasInitialResponse()) { + responseToken = createSaslToken(new byte[0], sc, learnerLogin); + } + send(dout, responseToken); + QuorumAuthPacket authPacket = receive(din); + QuorumAuth.Status qpStatus = QuorumAuth.Status.getStatus(authPacket.getStatus()); + while (!sc.isComplete()) { + switch (qpStatus) { + case SUCCESS: + responseToken = createSaslToken(authPacket.getToken(), sc, learnerLogin); + // we're done; don't expect to send another BIND + if (responseToken != null) { + throw new SaslException("Protocol error: attempting to send response after completion"); + } + break; + case IN_PROGRESS: + responseToken = createSaslToken(authPacket.getToken(), sc, learnerLogin); + send(dout, responseToken); + authPacket = receive(din); + qpStatus = QuorumAuth.Status.getStatus(authPacket.getStatus()); + break; + case ERROR: + throw new SaslException("Authentication failed against server addr: " + sock.getRemoteSocketAddress()); + default: + LOG.warn("Unknown status:{}!", qpStatus); + throw new SaslException("Authentication failed against server addr: " + sock.getRemoteSocketAddress()); + } + } + + // Validate status code at the end of authentication exchange. + checkAuthStatus(sock, qpStatus); + } finally { + if (sc != null) { + try { + sc.dispose(); + } catch (SaslException e) { + LOG.error("SaslClient dispose() failed", e); + } + } + } + } + + private void checkAuthStatus(Socket sock, QuorumAuth.Status qpStatus) throws SaslException { + if (qpStatus == QuorumAuth.Status.SUCCESS) { + LOG.info( + "Successfully completed the authentication using SASL. server addr: {}, status: {}", + sock.getRemoteSocketAddress(), + qpStatus); + } else { + throw new SaslException("Authentication failed against server addr: " + sock.getRemoteSocketAddress() + + ", qpStatus: " + qpStatus); + } + } + + private QuorumAuthPacket receive(DataInputStream din) throws IOException { + QuorumAuthPacket authPacket = new QuorumAuthPacket(); + BinaryInputArchive bia = BinaryInputArchive.getArchive(din); + authPacket.deserialize(bia, QuorumAuth.QUORUM_AUTH_MESSAGE_TAG); + return authPacket; + } + + private void send(DataOutputStream dout, byte[] response) throws IOException { + QuorumAuthPacket authPacket; + BufferedOutputStream bufferedOutput = new BufferedOutputStream(dout); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(bufferedOutput); + authPacket = QuorumAuth.createPacket(QuorumAuth.Status.IN_PROGRESS, response); + boa.writeRecord(authPacket, QuorumAuth.QUORUM_AUTH_MESSAGE_TAG); + bufferedOutput.flush(); + } + + // TODO: need to consolidate the #createSaslToken() implementation between ZooKeeperSaslClient#createSaslToken(). + private byte[] createSaslToken( + final byte[] saslToken, + final SaslClient saslClient, + final Login login) throws SaslException { + if (saslToken == null) { + throw new SaslException("Error in authenticating with a Zookeeper Quorum member: the quorum member's saslToken is null."); + } + if (login.getSubject() != null) { + synchronized (login) { + try { + final byte[] retval = Subject.doAs(login.getSubject(), new PrivilegedExceptionAction() { + public byte[] run() throws SaslException { + LOG.debug("saslClient.evaluateChallenge(len={})", saslToken.length); + return saslClient.evaluateChallenge(saslToken); + } + }); + return retval; + } catch (PrivilegedActionException e) { + String error = "An error: (" + e + ") occurred when evaluating Zookeeper Quorum Member's received SASL token."; + // Try to provide hints to use about what went wrong so they + // can fix their configuration. + // TODO: introspect about e: look for GSS information. + final String UNKNOWN_SERVER_ERROR_TEXT = "(Mechanism level: Server not found in Kerberos database (7) - UNKNOWN_SERVER)"; + if (e.toString().indexOf(UNKNOWN_SERVER_ERROR_TEXT) > -1) { + error += " This may be caused by Java's being unable to resolve the Zookeeper Quorum Member's" + + " hostname correctly. You may want to try to adding" + + " '-Dsun.net.spi.nameservice.provider.1=dns,sun' to your server's JVMFLAGS environment."; + } + LOG.error(error); + throw new SaslException(error, e); + } + } + } else { + throw new SaslException("Cannot make SASL token without subject defined. " + + "For diagnosis, please look for WARNs and ERRORs in your log related to the Login class."); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthServer.java new file mode 100644 index 0000000..9b5f48c --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumAuthServer.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.BufferedOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Set; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginException; +import javax.security.sasl.SaslException; +import javax.security.sasl.SaslServer; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.Login; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.server.quorum.QuorumAuthPacket; +import org.apache.zookeeper.util.SecurityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SaslQuorumAuthServer implements QuorumAuthServer { + + private static final Logger LOG = LoggerFactory.getLogger(SaslQuorumAuthServer.class); + + private static final int MAX_RETRIES = 5; + private final Login serverLogin; + private final boolean quorumRequireSasl; + + public SaslQuorumAuthServer(boolean quorumRequireSasl, String loginContext, Set authzHosts) throws SaslException { + this.quorumRequireSasl = quorumRequireSasl; + try { + AppConfigurationEntry[] entries = Configuration.getConfiguration().getAppConfigurationEntry(loginContext); + if (entries == null || entries.length == 0) { + throw new LoginException(String.format( + "SASL-authentication failed because the specified JAAS configuration section '%s' could not be found.", + loginContext)); + } + SaslQuorumServerCallbackHandler saslServerCallbackHandler = new SaslQuorumServerCallbackHandler( + Configuration.getConfiguration(), loginContext, authzHosts); + serverLogin = new Login(loginContext, saslServerCallbackHandler, new ZKConfig()); + serverLogin.startThreadIfNeeded(); + } catch (Throwable e) { + throw new SaslException("Failed to initialize authentication mechanism using SASL", e); + } + } + + @Override + public void authenticate(Socket sock, DataInputStream din) throws SaslException { + DataOutputStream dout = null; + SaslServer ss = null; + try { + if (!QuorumAuth.nextPacketIsAuth(din)) { + if (quorumRequireSasl) { + throw new SaslException("Learner not trying to authenticate" + " and authentication is required"); + } else { + // let it through, we don't require auth + return; + } + } + + byte[] token = receive(din); + int tries = 0; + dout = new DataOutputStream(sock.getOutputStream()); + byte[] challenge = null; + ss = SecurityUtils.createSaslServer( + serverLogin.getSubject(), + QuorumAuth.QUORUM_SERVER_PROTOCOL_NAME, + QuorumAuth.QUORUM_SERVER_SASL_DIGEST, + serverLogin.callbackHandler, + LOG); + while (!ss.isComplete()) { + challenge = ss.evaluateResponse(token); + if (!ss.isComplete()) { + // limited number of retries. + if (++tries > MAX_RETRIES) { + send(dout, challenge, QuorumAuth.Status.ERROR); + LOG.warn( + "Failed to authenticate using SASL, server addr: {}, retries={} exceeded.", + sock.getRemoteSocketAddress(), + tries); + break; + } + send(dout, challenge, QuorumAuth.Status.IN_PROGRESS); + token = receive(din); + } + } + // Authentication exchange has completed + if (ss.isComplete()) { + send(dout, challenge, QuorumAuth.Status.SUCCESS); + LOG.info( + "Successfully completed the authentication using SASL. learner addr: {}", + sock.getRemoteSocketAddress()); + } + } catch (Exception e) { + try { + if (dout != null) { + // send error message to the learner + send(dout, new byte[0], QuorumAuth.Status.ERROR); + } + } catch (IOException ioe) { + LOG.warn("Exception while sending failed status", ioe); + } + // If sasl is not required, when a server initializes a + // connection it will try to log in, but it will also + // accept connections that do not start with a sasl + // handshake. + if (quorumRequireSasl) { + LOG.error("Failed to authenticate using SASL", e); + throw new SaslException("Failed to authenticate using SASL: " + e.getMessage()); + } else { + LOG.warn("Failed to authenticate using SASL", e); + LOG.warn( + "Maintaining learner connection despite SASL authentication failure. server addr: {}, {}: {}", + sock.getRemoteSocketAddress(), + QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, + quorumRequireSasl); + // let it through, we don't require auth + } + } finally { + if (ss != null) { + try { + ss.dispose(); + } catch (SaslException e) { + LOG.error("SaslServer dispose() failed", e); + } + } + } + } + + private byte[] receive(DataInputStream din) throws IOException { + QuorumAuthPacket authPacket = new QuorumAuthPacket(); + BinaryInputArchive bia = BinaryInputArchive.getArchive(din); + authPacket.deserialize(bia, QuorumAuth.QUORUM_AUTH_MESSAGE_TAG); + return authPacket.getToken(); + } + + private void send(DataOutputStream dout, byte[] challenge, QuorumAuth.Status s) throws IOException { + BufferedOutputStream bufferedOutput = new BufferedOutputStream(dout); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(bufferedOutput); + QuorumAuthPacket authPacket; + if (challenge == null && s != QuorumAuth.Status.SUCCESS) { + authPacket = QuorumAuth.createPacket(QuorumAuth.Status.IN_PROGRESS, null); + } else { + authPacket = QuorumAuth.createPacket(s, challenge); + } + + boa.writeRecord(authPacket, QuorumAuth.QUORUM_AUTH_MESSAGE_TAG); + bufferedOutput.flush(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.java new file mode 100644 index 0000000..4b711a6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/auth/SaslQuorumServerCallbackHandler.java @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import javax.security.auth.callback.Callback; +import javax.security.auth.callback.CallbackHandler; +import javax.security.auth.callback.NameCallback; +import javax.security.auth.callback.PasswordCallback; +import javax.security.auth.callback.UnsupportedCallbackException; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.sasl.AuthorizeCallback; +import javax.security.sasl.RealmCallback; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This is used by the SASL mechanisms to get further information to complete + * the authentication. For example, a SASL mechanism might use this callback + * handler to do verification operation. This is used by the QuorumServer to + * perform the mutual quorum peer authentication. + */ +public class SaslQuorumServerCallbackHandler implements CallbackHandler { + + private static final String USER_PREFIX = "user_"; + private static final Logger LOG = LoggerFactory.getLogger(SaslQuorumServerCallbackHandler.class); + + private String userName; + private final Map credentials = new HashMap<>(); + private final Set authzHosts; + + public SaslQuorumServerCallbackHandler( + Configuration configuration, + String serverSection, + Set authzHosts) throws IOException { + AppConfigurationEntry[] configurationEntries = configuration.getAppConfigurationEntry(serverSection); + + if (configurationEntries == null) { + String errorMessage = "Could not find a '" + serverSection + "' entry in this configuration: Server cannot start."; + LOG.error(errorMessage); + throw new IOException(errorMessage); + } + credentials.clear(); + for (AppConfigurationEntry entry : configurationEntries) { + Map options = entry.getOptions(); + // Populate DIGEST-MD5 user -> password map with JAAS configuration entries from the "QuorumServer" section. + // Usernames are distinguished from other options by prefixing the username with a "user_" prefix. + for (Map.Entry pair : options.entrySet()) { + String key = pair.getKey(); + if (key.startsWith(USER_PREFIX)) { + String userName = key.substring(USER_PREFIX.length()); + credentials.put(userName, (String) pair.getValue()); + } + } + } + + // authorized host lists + this.authzHosts = authzHosts; + } + + public void handle(Callback[] callbacks) throws UnsupportedCallbackException { + for (Callback callback : callbacks) { + if (callback instanceof NameCallback) { + handleNameCallback((NameCallback) callback); + } else if (callback instanceof PasswordCallback) { + handlePasswordCallback((PasswordCallback) callback); + } else if (callback instanceof RealmCallback) { + handleRealmCallback((RealmCallback) callback); + } else if (callback instanceof AuthorizeCallback) { + handleAuthorizeCallback((AuthorizeCallback) callback); + } + } + } + + private void handleNameCallback(NameCallback nc) { + // check to see if this user is in the user password database. + if (credentials.get(nc.getDefaultName()) == null) { + LOG.warn("User '{}' not found in list of DIGEST-MD5 authenticateable users.", nc.getDefaultName()); + return; + } + nc.setName(nc.getDefaultName()); + userName = nc.getDefaultName(); + } + + private void handlePasswordCallback(PasswordCallback pc) { + if (credentials.containsKey(userName)) { + pc.setPassword(credentials.get(userName).toCharArray()); + } else { + LOG.warn("No password found for user: {}", userName); + } + } + + private void handleRealmCallback(RealmCallback rc) { + LOG.debug("QuorumLearner supplied realm: {}", rc.getDefaultText()); + rc.setText(rc.getDefaultText()); + } + + private void handleAuthorizeCallback(AuthorizeCallback ac) { + String authenticationID = ac.getAuthenticationID(); + String authorizationID = ac.getAuthorizationID(); + + boolean authzFlag = false; + // 1. Matches authenticationID and authorizationID + authzFlag = authenticationID.equals(authorizationID); + + // 2. Verify whether the connecting host is present in authorized hosts. + // If not exists, then connecting peer is not authorized to join the + // ensemble and will reject it. + if (authzFlag) { + String[] components = authorizationID.split("[/@]"); + if (components.length == 3) { + authzFlag = authzHosts.contains(components[1]); + } + if (!authzFlag) { + LOG.error("SASL authorization completed, {} is not authorized to connect", components[1]); + } + } + + // Sets authorization flag + ac.setAuthorized(authzFlag); + if (ac.isAuthorized()) { + ac.setAuthorizedID(authorizationID); + LOG.info("Successfully authenticated learner: authenticationID={}; authorizationID={}.", + authenticationID, authorizationID); + } + LOG.debug("SASL authorization completed, authorized flag set to {}", ac.isAuthorized()); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumHierarchical.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumHierarchical.java new file mode 100644 index 0000000..ced966f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumHierarchical.java @@ -0,0 +1,388 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.flexible; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements a validator for hierarchical quorums. With this + * construction, zookeeper servers are split into disjoint groups, and + * each server has a weight. We obtain a quorum if we get more than half + * of the total weight of a group for a majority of groups. + * + * The configuration of quorums uses two parameters: group and weight. + * Groups are sets of ZooKeeper servers, and we set a group by passing + * a colon-separated list of server ids. It is also necessary to assign + * weights to server. Here is an example of a configuration that creates + * three groups and assigns a weight of 1 to each server: + * + * group.1=1:2:3 + * group.2=4:5:6 + * group.3=7:8:9 + * + * weight.1=1 + * weight.2=1 + * weight.3=1 + * weight.4=1 + * weight.5=1 + * weight.6=1 + * weight.7=1 + * weight.8=1 + * weight.9=1 + * + * Note that it is still necessary to define peers using the server keyword. + */ + +public class QuorumHierarchical implements QuorumVerifier { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumHierarchical.class); + + private HashMap serverWeight = new HashMap(); + private HashMap serverGroup = new HashMap(); + private HashMap groupWeight = new HashMap(); + + private int numGroups = 0; + + private Map allMembers = new HashMap(); + private Map participatingMembers = new HashMap(); + private Map observingMembers = new HashMap(); + + private long version = 0; + + public int hashCode() { + assert false : "hashCode not designed"; + return 42; // any arbitrary constant will do + } + + public boolean equals(Object o) { + if (!(o instanceof QuorumHierarchical)) { + return false; + } + QuorumHierarchical qm = (QuorumHierarchical) o; + if (qm.getVersion() == version) { + return true; + } + if ((allMembers.size() != qm.getAllMembers().size()) + || (serverWeight.size() != qm.serverWeight.size()) + || (groupWeight.size() != qm.groupWeight.size()) + || (serverGroup.size() != qm.serverGroup.size())) { + return false; + } + for (QuorumServer qs : allMembers.values()) { + QuorumServer qso = qm.getAllMembers().get(qs.id); + if (qso == null || !qs.equals(qso)) { + return false; + } + } + for (Entry entry : serverWeight.entrySet()) { + if (!entry.getValue().equals(qm.serverWeight.get(entry.getKey()))) { + return false; + } + } + for (Entry entry : groupWeight.entrySet()) { + if (!entry.getValue().equals(qm.groupWeight.get(entry.getKey()))) { + return false; + } + } + for (Entry entry : serverGroup.entrySet()) { + if (!entry.getValue().equals(qm.serverGroup.get(entry.getKey()))) { + return false; + } + } + return true; + } + /** + * This constructor requires the quorum configuration + * to be declared in a separate file, and it takes the + * file as an input parameter. + */ + public QuorumHierarchical(String filename) throws ConfigException { + readConfigFile(filename); + } + + /** + * This constructor takes a set of properties. We use + * it in the unit test for this feature. + */ + + public QuorumHierarchical(Properties qp) throws ConfigException { + parse(qp); + LOG.info("{}, {}, {}", serverWeight.size(), serverGroup.size(), groupWeight.size()); + } + + /** + * Returns the weight of a server. + * + * @param id + */ + public long getWeight(long id) { + return serverWeight.get(id); + } + + /** + * Reads a configuration file. Called from the constructor + * that takes a file as an input. + */ + private void readConfigFile(String filename) throws ConfigException { + File configFile = new File(filename); + + LOG.info("Reading configuration from: {}", configFile); + + try { + if (!configFile.exists()) { + throw new IllegalArgumentException(configFile.toString() + " file is missing"); + } + + Properties cfg = new Properties(); + FileInputStream in = new FileInputStream(configFile); + try { + cfg.load(in); + } finally { + in.close(); + } + + parse(cfg); + } catch (IOException e) { + throw new ConfigException("Error processing " + filename, e); + } catch (IllegalArgumentException e) { + throw new ConfigException("Error processing " + filename, e); + } + + } + + /** + * Parse properties if configuration given in a separate file. + * Assumes that allMembers has been already assigned + * @throws ConfigException + */ + private void parse(Properties quorumProp) throws ConfigException { + for (Entry entry : quorumProp.entrySet()) { + String key = entry.getKey().toString(); + String value = entry.getValue().toString(); + + if (key.startsWith("server.")) { + int dot = key.indexOf('.'); + long sid = Long.parseLong(key.substring(dot + 1)); + QuorumServer qs = new QuorumServer(sid, value); + allMembers.put(Long.valueOf(sid), qs); + if (qs.type == LearnerType.PARTICIPANT) { + participatingMembers.put(Long.valueOf(sid), qs); + } else { + observingMembers.put(Long.valueOf(sid), qs); + } + } else if (key.startsWith("group")) { + int dot = key.indexOf('.'); + long gid = Long.parseLong(key.substring(dot + 1)); + + numGroups++; + + String[] parts = value.split(":"); + for (String s : parts) { + long sid = Long.parseLong(s); + if (serverGroup.containsKey(sid)) { + throw new ConfigException("Server " + sid + "is in multiple groups"); + } else { + serverGroup.put(sid, gid); + } + } + + } else if (key.startsWith("weight")) { + int dot = key.indexOf('.'); + long sid = Long.parseLong(key.substring(dot + 1)); + serverWeight.put(sid, Long.parseLong(value)); + } else if (key.equals("version")) { + version = Long.parseLong(value, 16); + } + } + + for (QuorumServer qs : allMembers.values()) { + Long id = qs.id; + if (qs.type == LearnerType.PARTICIPANT) { + if (!serverGroup.containsKey(id)) { + throw new ConfigException("Server " + id + "is not in a group"); + } + if (!serverWeight.containsKey(id)) { + serverWeight.put(id, (long) 1); + } + } + } + + computeGroupWeight(); + } + + public Map getAllMembers() { + return allMembers; + } + public String toString() { + StringWriter sw = new StringWriter(); + + for (QuorumServer member : getAllMembers().values()) { + String key = "server." + member.id; + String value = member.toString(); + sw.append(key); + sw.append('='); + sw.append(value); + sw.append('\n'); + } + + Map groups = new HashMap(); + for (Entry pair : serverGroup.entrySet()) { + Long sid = pair.getKey(); + Long gid = pair.getValue(); + String str = groups.get(gid); + if (str == null) { + str = sid.toString(); + } else { + str = str.concat(":").concat(sid.toString()); + } + groups.put(gid, str); + } + + for (Entry pair : groups.entrySet()) { + Long gid = pair.getKey(); + String key = "group." + gid.toString(); + String value = pair.getValue(); + sw.append(key); + sw.append('='); + sw.append(value); + sw.append('\n'); + } + + for (Entry pair : serverWeight.entrySet()) { + Long sid = pair.getKey(); + String key = "weight." + sid.toString(); + String value = pair.getValue().toString(); + sw.append(key); + sw.append('='); + sw.append(value); + sw.append('\n'); + } + + sw.append("version=" + Long.toHexString(version)); + + return sw.toString(); + } + + /** + * This method pre-computes the weights of groups to speed up processing + * when validating a given set. We compute the weights of groups in + * different places, so we have a separate method. + */ + private void computeGroupWeight() { + for (Entry entry : serverGroup.entrySet()) { + Long sid = entry.getKey(); + Long gid = entry.getValue(); + if (!groupWeight.containsKey(gid)) { + groupWeight.put(gid, serverWeight.get(sid)); + } else { + long totalWeight = serverWeight.get(sid) + groupWeight.get(gid); + groupWeight.put(gid, totalWeight); + } + } + + /* + * Do not consider groups with weight zero + */ + for (long weight : groupWeight.values()) { + LOG.debug("Group weight: {}", weight); + if (weight == ((long) 0)) { + numGroups--; + LOG.debug("One zero-weight group: 1, {}", numGroups); + } + } + } + + /** + * Verifies if a given set is a quorum. + */ + public boolean containsQuorum(Set set) { + HashMap expansion = new HashMap(); + + /* + * Adds up weights per group + */ + LOG.debug("Set size: {}", set.size()); + if (set.size() == 0) { + return false; + } + + for (long sid : set) { + Long gid = serverGroup.get(sid); + if (gid == null) { + continue; + } + if (!expansion.containsKey(gid)) { + expansion.put(gid, serverWeight.get(sid)); + } else { + long totalWeight = serverWeight.get(sid) + expansion.get(gid); + expansion.put(gid, totalWeight); + } + } + + /* + * Check if all groups have majority + */ + int majGroupCounter = 0; + for (Entry entry : expansion.entrySet()) { + Long gid = entry.getKey(); + LOG.debug("Group info: {}, {}, {}", entry.getValue(), gid, groupWeight.get(gid)); + if (entry.getValue() > (groupWeight.get(gid) / 2)) { + majGroupCounter++; + } + } + + LOG.debug("Majority group counter: {}, {}", majGroupCounter, numGroups); + if ((majGroupCounter > (numGroups / 2))) { + LOG.debug("Positive set size: {}", set.size()); + return true; + } else { + LOG.debug("Negative set size: {}", set.size()); + return false; + } + } + public Map getVotingMembers() { + return participatingMembers; + } + + public Map getObservingMembers() { + return observingMembers; + } + + public long getVersion() { + return version; + } + + public void setVersion(long ver) { + version = ver; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumMaj.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumMaj.java new file mode 100644 index 0000000..dfad89a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumMaj.java @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.flexible; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +/** + * This class implements a validator for majority quorums. The implementation is + * straightforward. + * + */ +public class QuorumMaj implements QuorumVerifier { + + // TODO_MA 注释: 里面包含的是 ZK 集群的所有节点 + private Map allMembers = new HashMap(); + + // TODO_MA 注释: 具有选举权的节点 = 除了 observer 以外的所有节点 + private Map votingMembers = new HashMap(); + + // TODO_MA 注释: observer 节点 + private Map observingMembers = new HashMap(); + private long version = 0; + private int half; + + public int hashCode() { + assert false : "hashCode not designed"; + return 42; // any arbitrary constant will do + } + + public boolean equals(Object o) { + if (!(o instanceof QuorumMaj)) { + return false; + } + QuorumMaj qm = (QuorumMaj) o; + if (qm.getVersion() == version) { + return true; + } + if (allMembers.size() != qm.getAllMembers().size()) { + return false; + } + for (QuorumServer qs : allMembers.values()) { + QuorumServer qso = qm.getAllMembers().get(qs.id); + if (qso == null || !qs.equals(qso)) { + return false; + } + } + return true; + } + + /** + * Defines a majority to avoid computing it every time. + * + */ + public QuorumMaj(Map allMembers) { + this.allMembers = allMembers; + for (QuorumServer qs : allMembers.values()) { + if (qs.type == LearnerType.PARTICIPANT) { + votingMembers.put(Long.valueOf(qs.id), qs); + } else { + observingMembers.put(Long.valueOf(qs.id), qs); + } + } + + // TODO_MA 马中华 注释: 一半 + // TODO_MA 马中华 注释: 4 = 2 + // TODO_MA 马中华 注释: 5 = 2 + half = votingMembers.size() / 2; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 在解析 server 的相关配置 + */ + public QuorumMaj(Properties props) throws ConfigException { + + // TODO_MA 注释: + for (Entry entry : props.entrySet()) { + + String key = entry.getKey().toString(); + String value = entry.getValue().toString(); + + // TODO_MA 注释: 解析 Server.myid = hostname:port:electionPort:peerType 的数据 + if (key.startsWith("server.")) { + int dot = key.indexOf('.'); + long sid = Long.parseLong(key.substring(dot + 1)); + + // TODO_MA 注释: 任何一台服务器都会被包装成一个 QuorumServer + QuorumServer qs = new QuorumServer(sid, value); + + // TODO_MA 注释: 所有服务节点 + allMembers.put(Long.valueOf(sid), qs); + + // TODO_MA 注释: 有选举权和被选举权的节点 + if (qs.type == LearnerType.PARTICIPANT) { + votingMembers.put(Long.valueOf(sid), qs); + } + + // TODO_MA 注释: observer节点 + else { + observingMembers.put(Long.valueOf(sid), qs); + } + } else if (key.equals("version")) { + version = Long.parseLong(value, 16); + } + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: votingMembers 中的成员就是 participant ,除了 observer 之外的 + * votingMembers + * observermembers + */ + half = votingMembers.size() / 2; + } + + /** + * Returns weight of 1 by default. + * + * @param id + */ + public long getWeight(long id) { + return 1; + } + + public String toString() { + StringBuilder sw = new StringBuilder(); + + for (QuorumServer member : getAllMembers().values()) { + String key = "server." + member.id; + String value = member.toString(); + sw.append(key); + sw.append('='); + sw.append(value); + sw.append('\n'); + } + String hexVersion = Long.toHexString(version); + sw.append("version="); + sw.append(hexVersion); + return sw.toString(); + } + + /** + * // TODO_MA 注释: 少数服从多数,是否 ack 集合大于 votingMembers 集合的半数 + * Verifies if a set is a majority. Assumes that ackSet contains acks only from votingMembers + * // TODO_MA 马中华 注释: 写数据 + * // TODO_MA 马中华 注释: 选举 + * // TODO_MA 马中华 注释: 选举确认 + */ + public boolean containsQuorum(Set ackSet) { + + // TODO_MA 马中华 注释: half 不会变! + return (ackSet.size() > half); + } + + public Map getAllMembers() { + return allMembers; + } + + public Map getVotingMembers() { + return votingMembers; + } + + public Map getObservingMembers() { + return observingMembers; + } + + public long getVersion() { + return version; + } + + public void setVersion(long ver) { + version = ver; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumVerifier.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumVerifier.java new file mode 100644 index 0000000..a024b76 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/flexible/QuorumVerifier.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.flexible; + +import java.util.Map; +import java.util.Set; + +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; + +/** + * All quorum validators have to implement a method called + * containsQuorum, which verifies if a HashSet of server + * identifiers constitutes a quorum. + */ + +public interface QuorumVerifier { + + long getWeight(long id); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 方法的意义:检查是否超过了半数 + * 1、Set set 已经收到的投票集合 a + * 2、getVotingMembers() 方法的结果 b + * a / b > 1/2 + */ + boolean containsQuorum(Set set); + + long getVersion(); + + void setVersion(long ver); + + // TODO_MA 注释: 所有的节点 + Map getAllMembers(); + + // TODO_MA 注释: 除了 Observer 之外的所有节点 + Map getVotingMembers(); + + // TODO_MA 注释: Observer 节点 + Map getObservingMembers(); + + boolean equals(Object o); + + String toString(); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/AdHash.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/AdHash.java new file mode 100644 index 0000000..f3de65e --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/AdHash.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +/** + * This incremental hash is used to keep track of the hash of + * the data tree to that we can quickly validate that things + * are in sync. + * + * See the excellent paper: A New Paradigm for collision-free hashing: + * Incrementality at reduced cost, M. Bellare and D. Micciancio + */ +public class AdHash { + + /* we use 64 bits so that we can be fast an efficient */ + private volatile long hash; + + /** + * Add new digest to the hash value maintained in this class. + * + * @param digest the value to add on + * @return the AdHash itself for chained operations + */ + public AdHash addDigest(long digest) { + hash += digest; + return this; + } + + /** + * Remove the digest from the hash value. + * + * @param digest the value to remove + * @return the AdHash itself for chained operations + */ + public AdHash removeDigest(long digest) { + hash -= digest; + return this; + } + + /** + * Return the long value of the hash. + */ + public long getHash() { + return hash; + } + + @Override + public boolean equals(Object other) { + return other instanceof AdHash && ((AdHash) other).hash == this.hash; + } + + @Override + public int hashCode() { + return Long.hashCode(hash); + } + + @Override + public String toString() { + return Long.toHexString(hash); + } + + public void clear() { + hash = 0; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/AuthUtil.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/AuthUtil.java new file mode 100644 index 0000000..3b0b7e4 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/AuthUtil.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.auth.AuthenticationProvider; +import org.apache.zookeeper.server.auth.ProviderRegistry; + +public final class AuthUtil { + private AuthUtil() { + //Utility classes should not have public constructors + } + /** + * Gives user name + * + * @param id contains scheme and authentication info + * @return returns null if authentication scheme does not exist or + * authentication provider returns null as user + */ + public static String getUser(Id id) { + AuthenticationProvider provider = ProviderRegistry.getProvider(id.getScheme()); + return provider == null ? null : provider.getUserName(id.getId()); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/BitHashSet.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/BitHashSet.java new file mode 100644 index 0000000..5788e6a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/BitHashSet.java @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.util.BitSet; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +/** + * Using BitSet to store all the elements, and use HashSet to cache limited + * number of elements to find a balance between memory and time complexity. + * + * Without HashSet, we need to use O(N) time to get the elements, N is + * the bit numbers in elementBits. But we need to keep the size small to make + * sure it doesn't cost too much in memory, there is a trade off between + * memory and time complexity. + * + * Previously, was deciding to dynamically switch between SparseBitSet and + * HashSet based on the memory consumption, but it will take time to copy + * data over and may have some herd effect of keep copying data from one + * data structure to anther. The current solution can do a very good job + * given most of the paths have limited number of elements. + */ +public class BitHashSet implements Iterable { + + /** + * Change to SparseBitSet if we we want to optimize more, the number of + * elements on a single server is usually limited, so BitSet should be + * fine. + */ + private final BitSet elementBits = new BitSet(); + + /** + * HashSet is used to optimize the iterating, if there is a single + * element in this BitHashSet, but the bit is very large, without + * HashSet we need to go through all the words before return that + * element, which is not efficient. + */ + private final Set cache = new HashSet(); + + private final int cacheSize; + + // To record how many elements in this set. + private int elementCount = 0; + + public BitHashSet() { + this(Integer.getInteger("zookeeper.bitHashCacheSize", 10)); + } + + public BitHashSet(int cacheSize) { + this.cacheSize = cacheSize; + } + + public synchronized boolean add(Integer elementBit) { + if (elementBit == null || elementBits.get(elementBit)) { + return false; + } + if (cache.size() < cacheSize) { + cache.add(elementBit); + } + elementBits.set(elementBit); + elementCount++; + return true; + } + + /** + * Remove the watches, and return the number of watches being removed. + */ + public synchronized int remove(Set bitSet, BitSet bits) { + cache.removeAll(bitSet); + elementBits.andNot(bits); + int elementCountBefore = elementCount; + elementCount = elementBits.cardinality(); + return elementCountBefore - elementCount; + } + + public synchronized boolean remove(Integer elementBit) { + if (elementBit == null || !elementBits.get(elementBit)) { + return false; + } + + cache.remove(elementBit); + elementBits.clear(elementBit); + elementCount--; + return true; + } + + public synchronized boolean contains(Integer elementBit) { + if (elementBit == null) { + return false; + } + return elementBits.get(elementBit); + } + + public synchronized int size() { + return elementCount; + } + + /** + * This function is not thread-safe, need to synchronized when + * iterate through this set. + */ + @Override + public Iterator iterator() { + // sample current size at the beginning + int currentSize = size(); + + if (cache.size() == currentSize) { + return cache.iterator(); + } + + return new Iterator() { + int returnedCount = 0; + int bitIndex = 0; + + @Override + public boolean hasNext() { + return returnedCount < currentSize; + } + + @Override + public Integer next() { + int bit = elementBits.nextSetBit(bitIndex); + bitIndex = bit + 1; + returnedCount++; + return bit; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + + // visible for test + public synchronized int cachedSize() { + return cache.size(); + } + + public synchronized boolean isEmpty() { + return elementCount == 0; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/BitMap.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/BitMap.java new file mode 100644 index 0000000..da76f46 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/BitMap.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.util.BitSet; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * This is a helper class to maintain the bit to specific value and the + * reversed value to bit mapping. + */ +public class BitMap { + + private final Map value2Bit = new HashMap(); + private final Map bit2Value = new HashMap(); + + private final BitSet freedBitSet = new BitSet(); + private Integer nextBit = Integer.valueOf(0); + + private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); + + @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "SpotBugs false positive") + public Integer add(T value) { + /* + * Optimized for code which will add the same value again and again, + * more specifically this is used to add new bit for watcher, and + * the same watcher may watching thousands or even millions of nodes, + * which will call add the same value of this function, check exist + * using read lock will optimize the performance here. + */ + Integer bit = getBit(value); + if (bit != null) { + return bit; + } + + rwLock.writeLock().lock(); + try { + bit = value2Bit.get(value); + if (bit != null) { + return bit; + } + bit = freedBitSet.nextSetBit(0); + if (bit > -1) { + freedBitSet.clear(bit); + } else { + bit = nextBit++; + } + + value2Bit.put(value, bit); + bit2Value.put(bit, value); + return bit; + } finally { + rwLock.writeLock().unlock(); + } + } + + public T get(int bit) { + rwLock.readLock().lock(); + try { + return bit2Value.get(bit); + } finally { + rwLock.readLock().unlock(); + } + } + + public Integer getBit(T value) { + rwLock.readLock().lock(); + try { + return value2Bit.get(value); + } finally { + rwLock.readLock().unlock(); + } + } + + public int remove(T value) { + /* + * remove only called once when the session is closed, so use write + * lock directly without checking read lock. + */ + rwLock.writeLock().lock(); + try { + Integer bit = value2Bit.get(value); + if (bit == null) { + return -1; + } + value2Bit.remove(value); + bit2Value.remove(bit); + freedBitSet.set(bit); + return bit; + } finally { + rwLock.writeLock().unlock(); + } + } + + public T remove(int bit) { + rwLock.writeLock().lock(); + try { + T value = bit2Value.get(bit); + if (value == null) { + return null; + } + value2Bit.remove(value); + bit2Value.remove(bit); + freedBitSet.set(bit); + return value; + } finally { + rwLock.writeLock().unlock(); + } + } + + public int size() { + rwLock.readLock().lock(); + try { + return value2Bit.size(); + } finally { + rwLock.readLock().unlock(); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/CircularBuffer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/CircularBuffer.java new file mode 100644 index 0000000..d338120 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/CircularBuffer.java @@ -0,0 +1,103 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.lang.reflect.Array; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Thread safe FIFO CircularBuffer implementation. + * When the buffer is full write operation overwrites the oldest element. + * + * Fun thing @todo, make this lock free as this is called on every quorum message + */ +public class CircularBuffer { + + private final T[] buffer; + private final int capacity; + private int oldest; + private AtomicInteger numberOfElements = new AtomicInteger(); + + @SuppressWarnings("unchecked") + public CircularBuffer(Class clazz, int capacity) { + if (capacity <= 0) { + throw new IllegalArgumentException("CircularBuffer capacity should be greater than 0"); + } + this.buffer = (T[]) Array.newInstance(clazz, capacity); + this.capacity = capacity; + } + + /** + * Puts elements in the next available index in the array. + * If the array is full the oldest element is replaced with + * the new value. + * @param element + */ + public synchronized void write(T element) { + int newSize = numberOfElements.incrementAndGet(); + if (newSize > capacity) { + buffer[oldest] = element; + oldest = ++oldest % capacity; + numberOfElements.decrementAndGet(); + } else { + int index = (oldest + numberOfElements.get() - 1) % capacity; + buffer[index] = element; + } + } + + /** + * Reads from the buffer in a FIFO manner. + * Returns the oldest element in the buffer if the buffer ie not empty + * Returns null if the buffer is empty + * @return + */ + public synchronized T take() { + int newSize = numberOfElements.decrementAndGet(); + if (newSize < 0) { + numberOfElements.incrementAndGet(); + return null; + } + T polled = buffer[oldest]; + oldest = ++oldest % capacity; + return polled; + } + + public synchronized T peek() { + if (numberOfElements.get() <= 0) { + return null; + } + return buffer[oldest]; + } + + public int size() { + return numberOfElements.get(); + } + + public boolean isEmpty() { + return numberOfElements.get() <= 0; + } + + public boolean isFull() { + return numberOfElements.get() >= capacity; + } + + public synchronized void reset() { + numberOfElements.set(0); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/ConfigUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/ConfigUtils.java new file mode 100644 index 0000000..d6f7572 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/ConfigUtils.java @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Map.Entry; +import java.util.Properties; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; + +public class ConfigUtils { + + public static String getClientConfigStr(String configData) { + Properties props = new Properties(); + try { + props.load(new StringReader(configData)); + } catch (IOException e) { + e.printStackTrace(); + return ""; + } + StringBuffer sb = new StringBuffer(); + boolean first = true; + String version = ""; + for (Entry entry : props.entrySet()) { + String key = entry.getKey().toString().trim(); + String value = entry.getValue().toString().trim(); + if (key.equals("version")) { + version = value; + } + if (!key.startsWith("server.")) { + continue; + } + QuorumPeer.QuorumServer qs; + try { + qs = new QuorumPeer.QuorumServer(-1, value); + } catch (ConfigException e) { + e.printStackTrace(); + continue; + } + if (!first) { + sb.append(","); + } else { + first = false; + } + if (null != qs.clientAddr) { + sb.append(qs.clientAddr.getHostString() + ":" + qs.clientAddr.getPort()); + } + } + return version + " " + sb.toString(); + } + + /** + * Gets host and port by splitting server config + * with support for IPv6 literals + * @return String[] first element being the + * IP address and the next being the port + * @param s server config, server:port + */ + public static String[] getHostAndPort(String s) throws ConfigException { + if (s.startsWith("[")) { + int i = s.indexOf("]"); + if (i < 0) { + throw new ConfigException(s + " starts with '[' but has no matching ']:'"); + } + if (i + 2 == s.length()) { + throw new ConfigException(s + " doesn't have a port after colon"); + } + if (i + 2 < s.length()) { + String[] sa = s.substring(i + 2).split(":"); + String[] nsa = new String[sa.length + 1]; + nsa[0] = s.substring(1, i); + System.arraycopy(sa, 0, nsa, 1, sa.length); + return nsa; + } + return new String[]{s.replaceAll("\\[|\\]", "")}; + } else { + return s.split(":"); + } + } + + /** + * Some old configuration properties are not configurable in zookeeper configuration file + * zoo.cfg. To make these properties configurable in zoo.cfg old properties are prepended + * with zookeeper. For example prop.x.y.z changed to zookeeper.prop.x.y.z. But for backward + * compatibility both prop.x.y.z and zookeeper.prop.x.y.z should be supported. + * This method first gets value from new property, if first property is not configured + * then gets value from old property + * + * @param newPropertyKey new property key which starts with zookeeper. + * @return either new or old system property value. Null if none of the properties are set. + */ + public static String getPropertyBackwardCompatibleWay(String newPropertyKey) { + String newKeyValue = System.getProperty(newPropertyKey); + if (newKeyValue != null) { + return newKeyValue.trim(); + } + String oldPropertyKey = newPropertyKey.replace("zookeeper.", ""); + String oldKeyValue = System.getProperty(oldPropertyKey); + + if (oldKeyValue != null) { + return oldKeyValue.trim(); + } + return null; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/JvmPauseMonitor.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/JvmPauseMonitor.java new file mode 100644 index 0000000..886f7f1 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/JvmPauseMonitor.java @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.lang.management.GarbageCollectorMXBean; +import java.lang.management.ManagementFactory; +import java.time.Instant; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.server.ServerConfig; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This code is originally from hadoop-common, see: + * https://github.com/apache/hadoop/blob/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/JvmPauseMonitor.java + * + * Class which sets up a simple thread which runs in a loop sleeping + * for a short interval of time. If the sleep takes significantly longer + * than its target time, it implies that the JVM or host machine has + * paused processing, which may cause other problems. If such a pause is + * detected, the thread logs a message. + */ +public class JvmPauseMonitor { + + private static final Logger LOG = LoggerFactory.getLogger(JvmPauseMonitor.class); + + public static final String JVM_PAUSE_MONITOR_FEATURE_SWITCH_KEY = "jvm.pause.monitor"; + + /** The target sleep time */ + protected long sleepTimeMs; + public static final String SLEEP_TIME_MS_KEY = "jvm.pause.sleep.time.ms"; + public static final long SLEEP_TIME_MS_DEFAULT = 500; + + /** log WARN if we detect a pause longer than this threshold */ + protected long warnThresholdMs; + public static final String WARN_THRESHOLD_KEY = "jvm.pause.warn-threshold.ms"; + public static final long WARN_THRESHOLD_DEFAULT = 10000; + + /** log INFO if we detect a pause longer than this threshold */ + protected long infoThresholdMs; + public static final String INFO_THRESHOLD_KEY = "jvm.pause.info-threshold.ms"; + public static final long INFO_THRESHOLD_DEFAULT = 1000; + + private long numGcWarnThresholdExceeded = 0; + private long numGcInfoThresholdExceeded = 0; + private long totalGcExtraSleepTime = 0; + + private Thread monitorThread; + private volatile boolean shouldRun = true; + + public JvmPauseMonitor(QuorumPeerConfig config) { + this.warnThresholdMs = config.getJvmPauseWarnThresholdMs(); + this.infoThresholdMs = config.getJvmPauseInfoThresholdMs(); + this.sleepTimeMs = config.getJvmPauseSleepTimeMs(); + } + + public JvmPauseMonitor(ServerConfig config) { + this.warnThresholdMs = config.getJvmPauseWarnThresholdMs(); + this.infoThresholdMs = config.getJvmPauseInfoThresholdMs(); + this.sleepTimeMs = config.getJvmPauseSleepTimeMs(); + } + + public void serviceStart() { + + // TODO_MA 注释: + monitorThread = new Thread(new JVMMonitor()); + monitorThread.setDaemon(true); + + // TODO_MA 注释: + monitorThread.start(); + } + + public void serviceStop() { + shouldRun = false; + if (monitorThread != null) { + monitorThread.interrupt(); + try { + monitorThread.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + + public boolean isStarted() { + return monitorThread != null; + } + + public long getNumGcWarnThresholdExceeded() { + return numGcWarnThresholdExceeded; + } + + public long getNumGcInfoThresholdExceeded() { + return numGcInfoThresholdExceeded; + } + + public long getTotalGcExtraSleepTime() { + return totalGcExtraSleepTime; + } + + private String formatMessage(long extraSleepTime, Map gcTimesAfterSleep, Map gcTimesBeforeSleep) { + + Set gcBeanNames = new HashSet<>(gcTimesAfterSleep.keySet()); + gcBeanNames.retainAll(gcTimesBeforeSleep.keySet()); + List gcDiffs = new ArrayList<>(); + + for (String name : gcBeanNames) { + GcTimes diff = gcTimesAfterSleep.get(name).subtract(gcTimesBeforeSleep.get(name)); + if (diff.gcCount != 0) { + gcDiffs.add("GC pool '" + name + "' had collection(s): " + diff.toString()); + } + } + + String ret = String.format("Detected pause in JVM or host machine (eg GC): pause of approximately %d ms, " + + "total pause: info level: %d, warn level: %d %n", + extraSleepTime, + numGcInfoThresholdExceeded, + numGcWarnThresholdExceeded); + if (gcDiffs.isEmpty()) { + ret += ("No GCs detected"); + } else { + ret += String.join("\n", gcDiffs); + } + return ret; + } + + private Map getGcTimes() { + Map map = new HashMap<>(); + List gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); + for (GarbageCollectorMXBean gcBean : gcBeans) { + map.put(gcBean.getName(), new GcTimes(gcBean)); + } + return map; + } + + private static class GcTimes { + + private long gcCount; + private long gcTimeMillis; + + private GcTimes(GarbageCollectorMXBean gcBean) { + gcCount = gcBean.getCollectionCount(); + gcTimeMillis = gcBean.getCollectionTime(); + } + + private GcTimes(long count, long time) { + this.gcCount = count; + this.gcTimeMillis = time; + } + + private GcTimes subtract(GcTimes other) { + return new GcTimes(this.gcCount - other.gcCount, this.gcTimeMillis - other.gcTimeMillis); + } + + public String toString() { + return "count=" + gcCount + " time=" + gcTimeMillis + "ms"; + } + + } + + private class JVMMonitor implements Runnable { + + @Override + public void run() { + Map gcTimesBeforeSleep = getGcTimes(); + LOG.info("Starting JVM Pause Monitor with infoThresholdMs:{} warnThresholdMs:{} and sleepTimeMs:{}", infoThresholdMs, warnThresholdMs, sleepTimeMs); + while (shouldRun) { + + // TODO_MA 注释: 睡一段时间 + long startTime = Instant.now().toEpochMilli(); + try { + Thread.sleep(sleepTimeMs); + } catch (InterruptedException ie) { + return; + } + long endTime = Instant.now().toEpochMilli(); + + long extraSleepTime = (endTime - startTime) - sleepTimeMs; + if (extraSleepTime >= 0) { + ServerMetrics.getMetrics().JVM_PAUSE_TIME.add(extraSleepTime); + } + Map gcTimesAfterSleep = getGcTimes(); + if (extraSleepTime > warnThresholdMs) { + ++numGcWarnThresholdExceeded; + LOG.warn(formatMessage(extraSleepTime, gcTimesAfterSleep, gcTimesBeforeSleep)); + } else if (extraSleepTime > infoThresholdMs) { + ++numGcInfoThresholdExceeded; + LOG.info(formatMessage(extraSleepTime, gcTimesAfterSleep, gcTimesBeforeSleep)); + } + totalGcExtraSleepTime += extraSleepTime; + gcTimesBeforeSleep = gcTimesAfterSleep; + } + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/KerberosUtil.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/KerberosUtil.java new file mode 100644 index 0000000..50e0d2f --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/KerberosUtil.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import javax.security.auth.kerberos.KerberosPrincipal; + +public class KerberosUtil { + + public static String getDefaultRealm() throws IllegalArgumentException { + return new KerberosPrincipal("tmp", 1).getRealm(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/LogChopper.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/LogChopper.java new file mode 100644 index 0000000..ccc8733 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/LogChopper.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.zip.Adler32; +import java.util.zip.Checksum; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.yetus.audience.InterfaceAudience; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; + +/** + * this class will chop the log at the specified zxid + */ +@InterfaceAudience.Public +public class LogChopper { + + public static void main(String[] args) { + ExitCode rc = ExitCode.INVALID_INVOCATION; + if (args.length != 3) { + System.out.println("Usage: LogChopper zxid_to_chop_to txn_log_to_chop chopped_filename"); + System.out.println(" this program will read the txn_log_to_chop file and copy all the transactions"); + System.out.println(" from it up to (and including) the given zxid into chopped_filename."); + ServiceUtils.requestSystemExit(rc.getValue()); + } + String txnLog = args[1]; + String choppedLog = args[2]; + + try (InputStream is = new BufferedInputStream(new FileInputStream(txnLog)); OutputStream os = new BufferedOutputStream(new FileOutputStream(choppedLog))) { + long zxid = Long.decode(args[0]); + + if (chop(is, os, zxid)) { + rc = ExitCode.EXECUTION_FINISHED; + } + } catch (Exception e) { + System.out.println("Got exception: " + e.getMessage()); + } + ServiceUtils.requestSystemExit(rc.getValue()); + } + + public static boolean chop(InputStream is, OutputStream os, long zxid) throws IOException { + BinaryInputArchive logStream = BinaryInputArchive.getArchive(is); + BinaryOutputArchive choppedStream = BinaryOutputArchive.getArchive(os); + FileHeader fhdr = new FileHeader(); + fhdr.deserialize(logStream, "fileheader"); + + if (fhdr.getMagic() != FileTxnLog.TXNLOG_MAGIC) { + System.err.println("Invalid magic number in txn log file"); + return false; + } + System.out.println("ZooKeeper Transactional Log File with dbid " + + fhdr.getDbid() + + " txnlog format version " + + fhdr.getVersion()); + + fhdr.serialize(choppedStream, "fileheader"); + int count = 0; + boolean hasZxid = false; + long previousZxid = -1; + while (true) { + long crcValue; + byte[] bytes; + try { + crcValue = logStream.readLong("crcvalue"); + + bytes = logStream.readBuffer("txnEntry"); + } catch (EOFException e) { + System.out.println("EOF reached after " + count + " txns."); + // returning false because nothing was chopped + return false; + } + if (bytes.length == 0) { + // Since we preallocate, we define EOF to be an + // empty transaction + System.out.println("EOF reached after " + count + " txns."); + // returning false because nothing was chopped + return false; + } + + Checksum crc = new Adler32(); + crc.update(bytes, 0, bytes.length); + if (crcValue != crc.getValue()) { + throw new IOException("CRC doesn't match " + crcValue + " vs " + crc.getValue()); + } + TxnLogEntry entry = SerializeUtils.deserializeTxn(bytes); + TxnHeader hdr = entry.getHeader(); + Record txn = entry.getTxn(); + if (logStream.readByte("EOR") != 'B') { + System.out.println("Last transaction was partial."); + throw new EOFException("Last transaction was partial."); + } + + final long txnZxid = hdr.getZxid(); + if (txnZxid == zxid) { + hasZxid = true; + } + + // logging the gap to make the inconsistency investigation easier + if (previousZxid != -1 && txnZxid != previousZxid + 1) { + long txnEpoch = ZxidUtils.getEpochFromZxid(txnZxid); + long txnCounter = ZxidUtils.getCounterFromZxid(txnZxid); + long previousEpoch = ZxidUtils.getEpochFromZxid(previousZxid); + if (txnEpoch == previousEpoch) { + System.out.println(String.format("There is intra-epoch gap between %x and %x", previousZxid, txnZxid)); + } else if (txnCounter != 1) { + System.out.println(String.format("There is inter-epoch gap between %x and %x", previousZxid, txnZxid)); + } + } + previousZxid = txnZxid; + + if (txnZxid > zxid) { + if (count == 0 || !hasZxid) { + System.out.println(String.format("This log does not contain zxid %x", zxid)); + return false; + } + System.out.println(String.format("Chopping at %x new log has %d records", zxid, count)); + return true; + } + + choppedStream.writeLong(crcValue, "crcvalue"); + choppedStream.writeBuffer(bytes, "txnEntry"); + choppedStream.writeByte((byte) 'B', "EOR"); + + count++; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/MessageTracker.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/MessageTracker.java new file mode 100644 index 0000000..a81a12b --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/MessageTracker.java @@ -0,0 +1,161 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.text.SimpleDateFormat; +import java.util.Date; +import org.apache.zookeeper.server.quorum.Leader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class provides a way of buffering sentBuffer and receivedBuffer messages in order. + * It uses EvictingQueue of size BUFFERED_MESSAGE_SIZE to store the messages. + * When the queue is full it overrides the oldest in a circular manner. + * This class does doe not provide thread safety. + */ +public class MessageTracker { + + private static final Logger LOG = LoggerFactory.getLogger(MessageTracker.class); + + private final CircularBuffer sentBuffer; + private final CircularBuffer receivedBuffer; + + public static final String MESSAGE_TRACKER_BUFFER_SIZE = "zookeeper.messageTracker.BufferSize"; + public static final String MESSAGE_TRACKER_ENABLED = "zookeeper.messageTracker.Enabled"; + public static final int BUFFERED_MESSAGE_SIZE; + private static final boolean enabled; + static { + BUFFERED_MESSAGE_SIZE = Integer.getInteger(MESSAGE_TRACKER_BUFFER_SIZE, 10); + enabled = Boolean.getBoolean(MESSAGE_TRACKER_ENABLED); + } + + public MessageTracker(int buffer_size) { + this.sentBuffer = new CircularBuffer<>(BufferedMessage.class, buffer_size); + this.receivedBuffer = new CircularBuffer<>(BufferedMessage.class, buffer_size); + } + + public void trackSent(long timestamp) { + if (enabled) { + sentBuffer.write(new BufferedMessage(timestamp)); + } + } + + public void trackSent(int packetType) { + if (enabled) { + sentBuffer.write(new BufferedMessage(packetType)); + } + } + + public void trackReceived(long timestamp) { + if (enabled) { + receivedBuffer.write(new BufferedMessage(timestamp)); + } + } + + public void trackReceived(int packetType) { + if (enabled) { + receivedBuffer.write(new BufferedMessage(packetType)); + } + } + + public final BufferedMessage peekSent() { + return sentBuffer.peek(); + } + + public final BufferedMessage peekReceived() { + return receivedBuffer.peek(); + } + + public final long peekSentTimestamp() { + return enabled ? sentBuffer.peek().getTimestamp() : 0; + } + + public final long peekReceivedTimestamp() { + return enabled ? receivedBuffer.peek().getTimestamp() : 0; + } + + public void dumpToLog(String serverAddress) { + if (!enabled) { + return; + } + logMessages(serverAddress, receivedBuffer, Direction.RECEIVED); + logMessages(serverAddress, sentBuffer, Direction.SENT); + } + + private static void logMessages( + String serverAddr, + CircularBuffer messages, + Direction direction) { + String sentOrReceivedText = direction == Direction.SENT ? "sentBuffer to" : "receivedBuffer from"; + + if (messages.isEmpty()) { + LOG.info("No buffered timestamps for messages {} {}", sentOrReceivedText, serverAddr); + } else { + LOG.warn("Last {} timestamps for messages {} {}:", messages.size(), sentOrReceivedText, serverAddr); + while (!messages.isEmpty()) { + LOG.warn("{} {} {}", sentOrReceivedText, serverAddr, messages.take().toString()); + } + } + } + + /** + * Direction for message track. + */ + private enum Direction { + SENT, RECEIVED + } + + private static class BufferedMessage { + + private long timestamp; + private int messageType; + + private long getTimestamp() { + return timestamp; + } + + BufferedMessage(int messageType) { + this.messageType = messageType; + this.timestamp = System.currentTimeMillis(); + } + + BufferedMessage(long timestamp) { + this.messageType = -1; + this.timestamp = timestamp; + } + + @Override + /** + * ToString examples are as follows: + * TimeStamp: 2016-06-06 11:07:58,594 Type: PROPOSAL + * TimeStamp: 2016-06-06 11:07:58,187 + */ + public String toString() { + if (messageType == -1) { + return "TimeStamp: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS") + .format(new Date(timestamp)); + } else { + return "TimeStamp: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS") + .format(new Date(timestamp)) + " Type: " + Leader.getPacketType(messageType); + } + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/OSMXBean.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/OSMXBean.java new file mode 100644 index 0000000..0ea4144 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/OSMXBean.java @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; +import java.lang.management.OperatingSystemMXBean; +import java.lang.management.RuntimeMXBean; +import java.lang.reflect.Method; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is a wrapper for the implementation of + * com.sun.management.UnixOperatingSystemMXBean + * It will decide to use the sun api or its own implementation + * depending on the runtime (vendor) used. + */ +public class OSMXBean { + + private static final Logger LOG = LoggerFactory.getLogger(OSMXBean.class); + + private OperatingSystemMXBean osMbean; + + private static final boolean ibmvendor = System.getProperty("java.vendor").contains("IBM"); + private static final boolean windows = System.getProperty("os.name").startsWith("Windows"); + private static final boolean linux = System.getProperty("os.name").startsWith("Linux"); + + /** + * Constructor. Get the running Operating System instance + */ + public OSMXBean() { + this.osMbean = ManagementFactory.getOperatingSystemMXBean(); + } + + /** + * Check if the OS is unix. If using the IBM java runtime, this + * will only work for linux. + * + * @return whether this is unix or not. + */ + public boolean getUnix() { + if (windows) { + return false; + } + return (!ibmvendor || linux); + } + + /** + * Load the implementation of UnixOperatingSystemMXBean for sun jvm + * and runs the desired method. + * @param mBeanMethodName : method to run from the interface UnixOperatingSystemMXBean + * @return the method result + */ + private Long getOSUnixMXBeanMethod(String mBeanMethodName) { + Object unixos; + Class classRef; + Method mBeanMethod; + + try { + classRef = Class.forName("com.sun.management.UnixOperatingSystemMXBean"); + if (classRef.isInstance(osMbean)) { + mBeanMethod = classRef.getDeclaredMethod(mBeanMethodName); + unixos = classRef.cast(osMbean); + return (Long) mBeanMethod.invoke(unixos); + } + } catch (Exception e) { + LOG.warn("Not able to load class or method for com.sun.managment.UnixOperatingSystemMXBean.", e); + } + return null; + } + + /** + * Get the number of opened filed descriptor for the runtime jvm. + * If sun java, it will use the com.sun.management interfaces. + * Otherwise, this methods implements it (linux only). + * @return number of open file descriptors for the jvm + */ + public long getOpenFileDescriptorCount() { + Long ofdc; + + if (!ibmvendor) { + ofdc = getOSUnixMXBeanMethod("getOpenFileDescriptorCount"); + return (ofdc != null ? ofdc.longValue() : -1); + } + + try { + //need to get the PID number of the process first + RuntimeMXBean rtmbean = ManagementFactory.getRuntimeMXBean(); + String rtname = rtmbean.getName(); + String[] pidhost = rtname.split("@"); + + //using linux bash commands to retrieve info + Process p = Runtime.getRuntime() + .exec(new String[]{"bash", "-c", "ls /proc/" + pidhost[0] + "/fdinfo | wc -l"}); + InputStream in = p.getInputStream(); + BufferedReader output = new BufferedReader(new InputStreamReader(in)); + + try { + String openFileDesCount; + if ((openFileDesCount = output.readLine()) != null) { + return Long.parseLong(openFileDesCount); + } + } finally { + if (output != null) { + output.close(); + } + } + } catch (IOException ie) { + LOG.warn("Not able to get the number of open file descriptors", ie); + } + return -1; + } + + /** + * Get the number of the maximum file descriptors the system can use. + * If sun java, it will use the com.sun.management interfaces. + * Otherwise, this methods implements it (linux only). + * @return max number of file descriptors the operating system can use. + */ + public long getMaxFileDescriptorCount() { + Long mfdc; + + if (!ibmvendor) { + mfdc = getOSUnixMXBeanMethod("getMaxFileDescriptorCount"); + return (mfdc != null ? mfdc.longValue() : -1); + } + + try { + //using linux bash commands to retrieve info + Process p = Runtime.getRuntime().exec(new String[]{"bash", "-c", "ulimit -n"}); + InputStream in = p.getInputStream(); + BufferedReader output = new BufferedReader(new InputStreamReader(in)); + + try { + String maxFileDesCount; + if ((maxFileDesCount = output.readLine()) != null) { + return Long.parseLong(maxFileDesCount); + } + } finally { + if (output != null) { + output.close(); + } + } + } catch (IOException ie) { + LOG.warn("Not able to get the max number of file descriptors", ie); + } + return -1; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/RequestPathMetricsCollector.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/RequestPathMetricsCollector.java new file mode 100644 index 0000000..db6f8c5 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/RequestPathMetricsCollector.java @@ -0,0 +1,390 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.apache.zookeeper.ZooDefs.OpCode.checkWatches; +import static org.apache.zookeeper.ZooDefs.OpCode.create; +import static org.apache.zookeeper.ZooDefs.OpCode.create2; +import static org.apache.zookeeper.ZooDefs.OpCode.createContainer; +import static org.apache.zookeeper.ZooDefs.OpCode.delete; +import static org.apache.zookeeper.ZooDefs.OpCode.deleteContainer; +import static org.apache.zookeeper.ZooDefs.OpCode.exists; +import static org.apache.zookeeper.ZooDefs.OpCode.getACL; +import static org.apache.zookeeper.ZooDefs.OpCode.getChildren; +import static org.apache.zookeeper.ZooDefs.OpCode.getChildren2; +import static org.apache.zookeeper.ZooDefs.OpCode.getData; +import static org.apache.zookeeper.ZooDefs.OpCode.removeWatches; +import static org.apache.zookeeper.ZooDefs.OpCode.setACL; +import static org.apache.zookeeper.ZooDefs.OpCode.setData; +import static org.apache.zookeeper.ZooDefs.OpCode.setWatches2; +import static org.apache.zookeeper.ZooDefs.OpCode.sync; +import java.io.PrintWriter; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; +import java.util.function.Predicate; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.Request; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class holds the requests path ( up till a certain depth) stats per request type + */ +public class RequestPathMetricsCollector { + + private static final Logger LOG = LoggerFactory.getLogger(RequestPathMetricsCollector.class); + // How many seconds does each slot represent, default is 15 seconds. + private final int REQUEST_STATS_SLOT_DURATION; + // How many slots we keep, default is 60 so it's 15 minutes total history. + private final int REQUEST_STATS_SLOT_CAPACITY; + // How far down the path we keep, default is 6. + private final int REQUEST_PREPROCESS_PATH_DEPTH; + // Sample rate, default is 0.1 (10%). + private final float REQUEST_PREPROCESS_SAMPLE_RATE; + private final long COLLECTOR_INITIAL_DELAY; + private final long COLLECTOR_DELAY; + private final int REQUEST_PREPROCESS_TOPPATH_MAX; + private final boolean enabled; + + public static final String PATH_STATS_SLOT_CAPACITY = "zookeeper.pathStats.slotCapacity"; + public static final String PATH_STATS_SLOT_DURATION = "zookeeper.pathStats.slotDuration"; + public static final String PATH_STATS_MAX_DEPTH = "zookeeper.pathStats.maxDepth"; + public static final String PATH_STATS_SAMPLE_RATE = "zookeeper.pathStats.sampleRate"; + public static final String PATH_STATS_COLLECTOR_INITIAL_DELAY = "zookeeper.pathStats.initialDelay"; + public static final String PATH_STATS_COLLECTOR_DELAY = "zookeeper.pathStats.delay"; + public static final String PATH_STATS_TOP_PATH_MAX = "zookeeper.pathStats.topPathMax"; + public static final String PATH_STATS_ENABLED = "zookeeper.pathStats.enabled"; + private static final String PATH_SEPERATOR = "/"; + + private final Map immutableRequestsMap; + private final ScheduledThreadPoolExecutor scheduledExecutor; + private final boolean accurateMode; + + public RequestPathMetricsCollector() { + this(false); + } + + public RequestPathMetricsCollector(boolean accurateMode) { + final Map requestsMap = new HashMap<>(); + this.accurateMode = accurateMode; + + REQUEST_PREPROCESS_TOPPATH_MAX = Integer.getInteger(PATH_STATS_TOP_PATH_MAX, 20); + REQUEST_STATS_SLOT_DURATION = Integer.getInteger(PATH_STATS_SLOT_DURATION, 15); + REQUEST_STATS_SLOT_CAPACITY = Integer.getInteger(PATH_STATS_SLOT_CAPACITY, 60); + REQUEST_PREPROCESS_PATH_DEPTH = Integer.getInteger(PATH_STATS_MAX_DEPTH, 6); + REQUEST_PREPROCESS_SAMPLE_RATE = Float.parseFloat(System.getProperty(PATH_STATS_SAMPLE_RATE, "0.1")); + COLLECTOR_INITIAL_DELAY = Long.getLong(PATH_STATS_COLLECTOR_INITIAL_DELAY, 5); + COLLECTOR_DELAY = Long.getLong(PATH_STATS_COLLECTOR_DELAY, 5); + enabled = Boolean.getBoolean(PATH_STATS_ENABLED); + + LOG.info("{} = {}", PATH_STATS_SLOT_CAPACITY, REQUEST_STATS_SLOT_CAPACITY); + LOG.info("{} = {}", PATH_STATS_SLOT_DURATION, REQUEST_STATS_SLOT_DURATION); + LOG.info("{} = {}", PATH_STATS_MAX_DEPTH, REQUEST_PREPROCESS_PATH_DEPTH); + LOG.info("{} = {}", PATH_STATS_COLLECTOR_INITIAL_DELAY, COLLECTOR_INITIAL_DELAY); + LOG.info("{} = {}", PATH_STATS_COLLECTOR_DELAY, COLLECTOR_DELAY); + LOG.info("{} = {}", PATH_STATS_ENABLED, enabled); + + this.scheduledExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); + scheduledExecutor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); + scheduledExecutor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); + requestsMap.put(Request.op2String(create), new PathStatsQueue(create)); + requestsMap.put(Request.op2String(create2), new PathStatsQueue(create2)); + requestsMap.put(Request.op2String(createContainer), new PathStatsQueue(createContainer)); + requestsMap.put(Request.op2String(deleteContainer), new PathStatsQueue(deleteContainer)); + requestsMap.put(Request.op2String(delete), new PathStatsQueue(delete)); + requestsMap.put(Request.op2String(exists), new PathStatsQueue(exists)); + requestsMap.put(Request.op2String(setData), new PathStatsQueue(setData)); + requestsMap.put(Request.op2String(getData), new PathStatsQueue(getData)); + requestsMap.put(Request.op2String(getACL), new PathStatsQueue(getACL)); + requestsMap.put(Request.op2String(setACL), new PathStatsQueue(setACL)); + requestsMap.put(Request.op2String(getChildren), new PathStatsQueue(getChildren)); + requestsMap.put(Request.op2String(getChildren2), new PathStatsQueue(getChildren2)); + requestsMap.put(Request.op2String(checkWatches), new PathStatsQueue(checkWatches)); + requestsMap.put(Request.op2String(removeWatches), new PathStatsQueue(removeWatches)); + requestsMap.put(Request.op2String(setWatches2), new PathStatsQueue(setWatches2)); + requestsMap.put(Request.op2String(sync), new PathStatsQueue(sync)); + this.immutableRequestsMap = java.util.Collections.unmodifiableMap(requestsMap); + } + + static boolean isWriteOp(int requestType) { + switch (requestType) { + case ZooDefs.OpCode.sync: + case ZooDefs.OpCode.create: + case ZooDefs.OpCode.create2: + case ZooDefs.OpCode.createContainer: + case ZooDefs.OpCode.delete: + case ZooDefs.OpCode.deleteContainer: + case ZooDefs.OpCode.setData: + case ZooDefs.OpCode.reconfig: + case ZooDefs.OpCode.setACL: + case ZooDefs.OpCode.multi: + case ZooDefs.OpCode.check: + return true; + } + return false; + } + + static String trimPathDepth(String path, int maxDepth) { + int count = 0; + StringBuilder sb = new StringBuilder(); + StringTokenizer pathTokenizer = new StringTokenizer(path, PATH_SEPERATOR); + while (pathTokenizer.hasMoreElements() && count++ < maxDepth) { + sb.append(PATH_SEPERATOR); + sb.append(pathTokenizer.nextToken()); + } + path = sb.toString(); + return path; + } + + public void shutdown() { + if (!enabled) { + return; + } + + LOG.info("shutdown scheduledExecutor"); + scheduledExecutor.shutdownNow(); + } + + public void start() { + if (!enabled) { + return; + } + + LOG.info("Start the RequestPath collector"); + immutableRequestsMap.forEach((opType, pathStatsQueue) -> pathStatsQueue.start()); + + // Schedule to log the top used read/write paths every 5 mins + scheduledExecutor.scheduleWithFixedDelay(() -> { + LOG.info("%nHere are the top Read paths:"); + logTopPaths(aggregatePaths(4, queue -> !queue.isWriteOperation()), + entry -> LOG.info("{} : {}", entry.getKey(), entry.getValue())); + LOG.info("%nHere are the top Write paths:"); + logTopPaths(aggregatePaths(4, queue -> queue.isWriteOperation()), + entry -> LOG.info("{} : {}", entry.getKey(), entry.getValue())); + }, COLLECTOR_INITIAL_DELAY, COLLECTOR_DELAY, TimeUnit.MINUTES); + } + + /** + * The public interface of the buffer. FinalRequestHandler will call into this for + * each request that has a path and this needs to be fast. we sample the path so that + * we don't have to store too many paths in memory + */ + public void registerRequest(int type, String path) { + if (!enabled) { + return; + } + if (ThreadLocalRandom.current().nextFloat() <= REQUEST_PREPROCESS_SAMPLE_RATE) { + PathStatsQueue pathStatsQueue = immutableRequestsMap.get(Request.op2String(type)); + if (pathStatsQueue != null) { + pathStatsQueue.registerRequest(path); + } else { + LOG.error("We should not handle {}", type); + } + } + } + + public void dumpTopRequestPath(PrintWriter pwriter, String requestTypeName, int queryMaxDepth) { + if (queryMaxDepth < 1) { + return; + } + PathStatsQueue pathStatsQueue = immutableRequestsMap.get(requestTypeName); + if (pathStatsQueue == null) { + pwriter.println("Can not find path stats for type: " + requestTypeName); + return; + } else { + pwriter.println("The top requests of type: " + requestTypeName); + } + Map combinedMap; + final int maxDepth = Math.min(queryMaxDepth, REQUEST_PREPROCESS_PATH_DEPTH); + combinedMap = pathStatsQueue.collectStats(maxDepth); + logTopPaths(combinedMap, entry -> pwriter.println(entry.getKey() + " : " + entry.getValue())); + } + + public void dumpTopReadPaths(PrintWriter pwriter, int queryMaxDepth) { + pwriter.println("The top read requests are"); + dumpTopAggregatedPaths(pwriter, queryMaxDepth, queue -> !queue.isWriteOperation); + } + + public void dumpTopWritePaths(PrintWriter pwriter, int queryMaxDepth) { + pwriter.println("The top write requests are"); + dumpTopAggregatedPaths(pwriter, queryMaxDepth, queue -> queue.isWriteOperation); + } + + public void dumpTopPaths(PrintWriter pwriter, int queryMaxDepth) { + pwriter.println("The top requests are"); + dumpTopAggregatedPaths(pwriter, queryMaxDepth, queue -> true); + } + + /** + * Combine all the path Stats Queue that matches the predicate together + * and then write to the pwriter + */ + private void dumpTopAggregatedPaths(PrintWriter pwriter, int queryMaxDepth, final Predicate predicate) { + if (!enabled) { + return; + } + final Map combinedMap = aggregatePaths(queryMaxDepth, predicate); + logTopPaths(combinedMap, entry -> pwriter.println(entry.getKey() + " : " + entry.getValue())); + } + + Map aggregatePaths(int queryMaxDepth, Predicate predicate) { + final Map combinedMap = new HashMap<>(REQUEST_PREPROCESS_TOPPATH_MAX); + final int maxDepth = Math.min(queryMaxDepth, REQUEST_PREPROCESS_PATH_DEPTH); + immutableRequestsMap.values() + .stream() + .filter(predicate) + .forEach(pathStatsQueue -> pathStatsQueue.collectStats(maxDepth).forEach( + (path, count) -> combinedMap.put(path, combinedMap.getOrDefault(path, 0) + count))); + return combinedMap; + } + + void logTopPaths(Map combinedMap, final Consumer> output) { + combinedMap.entrySet() + .stream() + // sort by path count + .sorted(Comparator.comparing(Map.Entry::getValue).reversed()) + .limit(REQUEST_PREPROCESS_TOPPATH_MAX).forEach(output); + } + + class PathStatsQueue { + + private final String requestTypeName; + private final AtomicReference> currentSlot; + private final LinkedBlockingQueue> requestPathStats; + private final boolean isWriteOperation; + + public PathStatsQueue(int requestType) { + this.requestTypeName = Request.op2String(requestType); + this.isWriteOperation = isWriteOp(requestType); + requestPathStats = new LinkedBlockingQueue<>(REQUEST_STATS_SLOT_CAPACITY); + currentSlot = new AtomicReference<>(new ConcurrentLinkedQueue<>()); + } + + /* + * The only write entry into this class, need to be fast. + * Just queue up the path to the current slot queue locking free. + */ + public void registerRequest(String path) { + if (!enabled) { + return; + } + currentSlot.get().offer(path); + } + + ConcurrentLinkedQueue getCurrentSlot() { + return currentSlot.get(); + } + + /** + * Helper function to MR the paths in the queue to map with count + * 1. cut each path up to max depth + * 2. aggregate the paths based on its count + * + * @param tobeProcessedSlot queue of paths called + * @return a map containing aggregated path in the queue + */ + Map mapReducePaths(int maxDepth, Collection tobeProcessedSlot) { + Map newSlot = new ConcurrentHashMap<>(); + tobeProcessedSlot.stream().filter(path -> path != null).forEach((path) -> { + path = trimPathDepth(path, maxDepth); + newSlot.put(path, newSlot.getOrDefault(path, 0) + 1); + }); + return newSlot; + } + + /** + * The only read point of this class + * + * @return the aggregated path to count map + */ + public Map collectStats(int maxDepth) { + Map combinedMap; + // Take a snapshot of the current slot and convert it to map. + // Set the initial size as 0 since we don't want it to padding nulls in the end. + Map snapShot = mapReducePaths( + maxDepth, + Arrays.asList(currentSlot.get().toArray(new String[0]))); + // Starting from the snapshot and go through the queue to reduce them into one map + // the iterator can run concurrently with write but we want to use a real lock in the test + synchronized (accurateMode ? requestPathStats : new Object()) { + combinedMap = requestPathStats.stream().reduce(snapShot, (firstMap, secondMap) -> { + secondMap.forEach((key, value) -> { + String trimmedPath = trimPathDepth(key, maxDepth); + firstMap.put(trimmedPath, firstMap.getOrDefault(trimmedPath, 0) + value); + }); + return firstMap; + }); + } + return combinedMap; + } + + /** + * Start to schedule the pre-processing of the current slot + */ + public void start() { + if (!enabled) { + return; + } + // Staggered start and then run every 15 seconds no matter what + int delay = ThreadLocalRandom.current().nextInt(REQUEST_STATS_SLOT_DURATION); + // We need to use fixed Delay as the fixed rate will start the next one right + // after the previous one finishes if it runs overtime instead of overlapping it. + scheduledExecutor.scheduleWithFixedDelay(() -> { + // Generate new slot so new requests will go here. + ConcurrentLinkedQueue tobeProcessedSlot = currentSlot.getAndSet(new ConcurrentLinkedQueue<>()); + try { + // pre process the last slot and queue it up, only one thread scheduled modified + // this but we can mess up the collect part so we put a lock in the test. + Map latestSlot = mapReducePaths(REQUEST_PREPROCESS_PATH_DEPTH, tobeProcessedSlot); + synchronized (accurateMode ? requestPathStats : new Object()) { + if (requestPathStats.remainingCapacity() <= 0) { + requestPathStats.poll(); + } + if (!requestPathStats.offer(latestSlot)) { + LOG.error("Failed to insert the new request path stats for {}", requestTypeName); + } + } + } catch (Exception e) { + LOG.error("Failed to insert the new request path stats for {} with exception {}", requestTypeName, e); + } + }, delay, REQUEST_STATS_SLOT_DURATION, TimeUnit.SECONDS); + } + + boolean isWriteOperation() { + return isWriteOperation; + } + + } + +} + + diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/SerializeUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/SerializeUtils.java new file mode 100644 index 0000000..14e1ccb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/SerializeUtils.java @@ -0,0 +1,207 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.io.ByteArrayInputStream; +import java.io.EOFException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.ZooTrace; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.txn.CloseSessionTxn; +import org.apache.zookeeper.txn.CreateContainerTxn; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.CreateTTLTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.CreateTxnV0; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.SetACLTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SerializeUtils { + + private static final Logger LOG = LoggerFactory.getLogger(SerializeUtils.class); + + public static TxnLogEntry deserializeTxn(byte[] txnBytes) throws IOException { + TxnHeader hdr = new TxnHeader(); + final ByteArrayInputStream bais = new ByteArrayInputStream(txnBytes); + InputArchive ia = BinaryInputArchive.getArchive(bais); + + hdr.deserialize(ia, "hdr"); + bais.mark(bais.available()); + Record txn = null; + switch (hdr.getType()) { + case OpCode.createSession: + // This isn't really an error txn; it just has the same + // format. The error represents the timeout + txn = new CreateSessionTxn(); + break; + case OpCode.closeSession: + txn = ZooKeeperServer.isCloseSessionTxnEnabled() + ? new CloseSessionTxn() : null; + break; + case OpCode.create: + case OpCode.create2: + txn = new CreateTxn(); + break; + case OpCode.createTTL: + txn = new CreateTTLTxn(); + break; + case OpCode.createContainer: + txn = new CreateContainerTxn(); + break; + case OpCode.delete: + case OpCode.deleteContainer: + txn = new DeleteTxn(); + break; + case OpCode.reconfig: + case OpCode.setData: + txn = new SetDataTxn(); + break; + case OpCode.setACL: + txn = new SetACLTxn(); + break; + case OpCode.error: + txn = new ErrorTxn(); + break; + case OpCode.multi: + txn = new MultiTxn(); + break; + default: + throw new IOException("Unsupported Txn with type=%d" + hdr.getType()); + } + if (txn != null) { + try { + txn.deserialize(ia, "txn"); + } catch (EOFException e) { + // perhaps this is a V0 Create + if (hdr.getType() == OpCode.create) { + CreateTxn create = (CreateTxn) txn; + bais.reset(); + CreateTxnV0 createv0 = new CreateTxnV0(); + createv0.deserialize(ia, "txn"); + // cool now make it V1. a -1 parentCVersion will + // trigger fixup processing in processTxn + create.setPath(createv0.getPath()); + create.setData(createv0.getData()); + create.setAcl(createv0.getAcl()); + create.setEphemeral(createv0.getEphemeral()); + create.setParentCVersion(-1); + } else if (hdr.getType() == OpCode.closeSession) { + // perhaps this is before CloseSessionTxn was added, + // ignore it and reset txn to null + txn = null; + } else { + throw e; + } + } + } + TxnDigest digest = null; + + if (ZooKeeperServer.isDigestEnabled()) { + digest = new TxnDigest(); + try { + digest.deserialize(ia, "digest"); + } catch (EOFException exception) { + // may not have digest in the txn + digest = null; + } + } + + return new TxnLogEntry(txn, hdr, digest); + } + + public static void deserializeSnapshot(DataTree dt, InputArchive ia, Map sessions) throws IOException { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 先恢复 session + */ + // TODO_MA 注释: 继续读取 count + int count = ia.readInt("count"); + while (count > 0) { + + // TODO_MA 注释: 读取 id + long id = ia.readLong("id"); + + // TODO_MA 注释: 读取 timeout + int to = ia.readInt("timeout"); + + // TODO_MA 注释: 恢复 session + sessions.put(id, to); + + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage( + LOG, + ZooTrace.SESSION_TRACE_MASK, + "loadData --- session in archive: " + id + " with timeout: " + to); + } + + // TODO_MA 注释: 已读取+1,待读取-1 + count--; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 恢复 DataTree + */ + dt.deserialize(ia, "tree"); + } + + public static void serializeSnapshot(DataTree dt, OutputArchive oa, Map sessions) throws IOException { + HashMap sessSnap = new HashMap(sessions); + oa.writeInt(sessSnap.size(), "count"); + for (Entry entry : sessSnap.entrySet()) { + oa.writeLong(entry.getKey().longValue(), "id"); + oa.writeInt(entry.getValue().intValue(), "timeout"); + } + dt.serialize(oa, "tree"); + } + + public static byte[] serializeRequest(Request request) { + if (request == null || request.getHdr() == null) { + return null; + } + byte[] data = new byte[32]; + try { + data = Util.marshallTxnEntry(request.getHdr(), request.getTxn(), request.getTxnDigest()); + } catch (IOException e) { + LOG.error("This really should be impossible", e); + } + return data; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/VerifyingFileFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/VerifyingFileFactory.java new file mode 100644 index 0000000..74f16fd --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/VerifyingFileFactory.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.io.File; +import org.slf4j.Logger; + +public final class VerifyingFileFactory { + + private final boolean warnForRelativePath; + private final boolean failForNonExistingPath; + private final Logger log; + + public VerifyingFileFactory(Builder builder) { + warnForRelativePath = builder.warnForRelativePathOption; + failForNonExistingPath = builder.failForNonExistingPathOption; + log = builder.log; + assert (log != null); + } + + public File create(String path) { + File file = new File(path); + return validate(file); + } + + public File validate(File file) { + if (warnForRelativePath) { + doWarnForRelativePath(file); + } + if (failForNonExistingPath) { + doFailForNonExistingPath(file); + } + return file; + } + + private void doFailForNonExistingPath(File file) { + if (!file.exists()) { + throw new IllegalArgumentException(file.toString() + " file is missing"); + } + } + + private void doWarnForRelativePath(File file) { + if (file.isAbsolute()) { + return; + } + if (file.getPath().substring(0, 2).equals("." + File.separator)) { + return; + } + log.warn(file.getPath() + " is relative. Prepend ." + File.separator + " to indicate that you're sure!"); + } + + public static class Builder { + + private boolean warnForRelativePathOption = false; + private boolean failForNonExistingPathOption = false; + private final Logger log; + + public Builder(Logger log) { + this.log = log; + } + + public Builder warnForRelativePath() { + warnForRelativePathOption = true; + return this; + } + + public Builder failForNonExistingPath() { + failForNonExistingPathOption = true; + return this; + } + + public VerifyingFileFactory build() { + return new VerifyingFileFactory(this); + } + + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/ZxidUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/ZxidUtils.java new file mode 100644 index 0000000..b3b6935 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/util/ZxidUtils.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +public class ZxidUtils { + + public static long getEpochFromZxid(long zxid) { + return zxid >> 32L; + } + public static long getCounterFromZxid(long zxid) { + return zxid & 0xffffffffL; + } + public static long makeZxid(long epoch, long counter) { + return (epoch << 32L) | (counter & 0xffffffffL); + } + public static String zxidToString(long zxid) { + return Long.toHexString(zxid); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/IDeadWatcherListener.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/IDeadWatcherListener.java new file mode 100644 index 0000000..0d05f35 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/IDeadWatcherListener.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.util.Set; + +/** + * Interface used to process the dead watchers related to closed cnxns. + */ +public interface IDeadWatcherListener { + + /** + * Process the given dead watchers. + * + * @param deadWatchers the watchers which have closed cnxn + */ + void processDeadWatchers(Set deadWatchers); + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/IWatchManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/IWatchManager.java new file mode 100644 index 0000000..1bc44c8 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/IWatchManager.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.io.PrintWriter; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; + +public interface IWatchManager { + + /** + * Add watch to specific path. + * + * @param path znode path + * @param watcher watcher object reference + * + * @return true if the watcher added is not already present + */ + boolean addWatch(String path, Watcher watcher); + + /** + * Add watch to specific path. + * + * @param path znode path + * @param watcher watcher object reference + * @param watcherMode the watcher mode to use + * + * @return true if the watcher added is not already present + */ + default boolean addWatch(String path, Watcher watcher, WatcherMode watcherMode) { + if (watcherMode == WatcherMode.DEFAULT_WATCHER_MODE) { + return addWatch(path, watcher); + } + throw new UnsupportedOperationException(); // custom implementations must defeat this + } + + /** + * Checks the specified watcher exists for the given path. + * + * @param path znode path + * @param watcher watcher object reference + * + * @return true if the watcher exists, false otherwise + */ + boolean containsWatcher(String path, Watcher watcher); + + /** + * Removes the specified watcher for the given path. + * + * @param path znode path + * @param watcher watcher object reference + * + * @return true if the watcher successfully removed, false otherwise + */ + boolean removeWatcher(String path, Watcher watcher); + + /** + * The entry to remove the watcher when the cnxn is closed. + * + * @param watcher watcher object reference + */ + void removeWatcher(Watcher watcher); + + /** + * Distribute the watch event for the given path. + * + * @param path znode path + * @param type the watch event type + * + * @return the watchers have been notified + */ + WatcherOrBitSet triggerWatch(String path, EventType type); + + /** + * Distribute the watch event for the given path, but ignore those + * suppressed ones. + * + * @param path znode path + * @param type the watch event type + * @param suppress the suppressed watcher set + * + * @return the watchers have been notified + */ + WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet suppress); + + /** + * Get the size of watchers. + * + * @return the watchers number managed in this class. + */ + int size(); + + /** + * Clean up the watch manager. + */ + void shutdown(); + + /** + * Returns a watch summary. + * + * @return watch summary + * @see WatchesSummary + */ + WatchesSummary getWatchesSummary(); + + /** + * Returns a watch report. + * + * @return watch report + * @see WatchesReport + */ + WatchesReport getWatches(); + + /** + * Returns a watch report by path. + * + * @return watch report + * @see WatchesPathReport + */ + WatchesPathReport getWatchesByPath(); + + /** + * String representation of watches. Warning, may be large! + * + * @param pwriter the writer to dump the watches + * @param byPath iff true output watches by paths, otw output + * watches by connection + * + */ + void dumpWatches(PrintWriter pwriter, boolean byPath); + + /** + * Return the current number of recursive watchers + * + * @return qty + */ + default int getRecursiveWatchQty() { + return 0; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/PathParentIterator.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/PathParentIterator.java new file mode 100644 index 0000000..a6aa8cd --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/PathParentIterator.java @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Iterates over a ZooKeeper path. Each iteration goes up one parent path. Thus, the + * effect of the iterator is to iterate over the initial path and then all of its parents. + */ +public class PathParentIterator implements Iterator { + private String path; + private final int maxLevel; + private int level = -1; + + /** + * Return a new PathParentIterator that iterates from the + * given path to all parents. + * + * @param path initial path + */ + public static PathParentIterator forAll(String path) { + return new PathParentIterator(path, Integer.MAX_VALUE); + } + + /** + * Return a new PathParentIterator that only returns the given path - i.e. + * does not iterate to parent paths. + * + * @param path initial path + */ + public static PathParentIterator forPathOnly(String path) { + return new PathParentIterator(path, 0); + } + + private PathParentIterator(String path, int maxLevel) { + // NOTE: asserts that the path has already been validated + this.path = path; + this.maxLevel = maxLevel; + } + + /** + * Return an Iterable view so that this Iterator can be used in for each + * statements. IMPORTANT: the returned Iterable is single use only + * @return Iterable + */ + public Iterable asIterable() { + return () -> PathParentIterator.this; + } + + @Override + public boolean hasNext() { + return !path.isEmpty() && (level < maxLevel); + } + + /** + * Returns true if this iterator is currently at a parent path as opposed + * to the initial path given to the constructor + * + * @return true/false + */ + public boolean atParentPath() { + return level > 0; + } + + @Override + public String next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + String localPath = path; + ++level; + if (path.equals("/")) { + path = ""; + } else { + path = path.substring(0, path.lastIndexOf('/')); + if (path.length() == 0) { + path = "/"; + } + } + return localPath; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManager.java new file mode 100644 index 0000000..82c79e0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManager.java @@ -0,0 +1,358 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZooTrace; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class manages watches. It allows watches to be associated with a string + * and removes watchers and their watches in addition to managing triggers. + */ +public class WatchManager implements IWatchManager { + + private static final Logger LOG = LoggerFactory.getLogger(WatchManager.class); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: ZNode path 对应的一堆 Watcher + */ + private final Map> watchTable = new HashMap<>(); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: Watcher 对应的一堆 ZNode Path + */ + private final Map> watch2Paths = new HashMap<>(); + + private final WatcherModeManager watcherModeManager = new WatcherModeManager(); + + @Override + public synchronized int size() { + int result = 0; + for (Set watches : watchTable.values()) { + result += watches.size(); + } + return result; + } + + private boolean isDeadWatcher(Watcher watcher) { + return watcher instanceof ServerCnxn && ((ServerCnxn) watcher).isStale(); + } + + @Override + public boolean addWatch(String path, Watcher watcher) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return addWatch(path, watcher, WatcherMode.DEFAULT_WATCHER_MODE); + } + + @Override + public synchronized boolean addWatch(String path, Watcher watcher, WatcherMode watcherMode) { + if (isDeadWatcher(watcher)) { + LOG.debug("Ignoring addWatch with closed cnxn"); + return false; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: watcher 注册 + */ + Set list = watchTable.get(path); + if (list == null) { + // don't waste memory if there are few watches on a node + // rehash when the 4th entry is added, doubling size thereafter + // seems like a good compromise + list = new HashSet<>(4); + watchTable.put(path, list); + } + list.add(watcher); + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 注册 watcher + */ + Set paths = watch2Paths.get(watcher); + if (paths == null) { + // cnxns typically have many watches, so use default cap here + paths = new HashSet<>(); + watch2Paths.put(watcher, paths); + } + watcherModeManager.setWatcherMode(watcher, path, watcherMode); + return paths.add(path); + } + + @Override + public synchronized void removeWatcher(Watcher watcher) { + Set paths = watch2Paths.remove(watcher); + if (paths == null) { + return; + } + for (String p : paths) { + Set list = watchTable.get(p); + if (list != null) { + list.remove(watcher); + if (list.isEmpty()) { + watchTable.remove(p); + } + } + watcherModeManager.removeWatcher(watcher, p); + } + } + + @Override + public WatcherOrBitSet triggerWatch(String path, EventType type) { + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: + */ + return triggerWatch(path, type, null); + } + + @Override + public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet supress) { + + // TODO_MA 马中华 注释: 封装一个 WatchedEvent + WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path); + + // TODO_MA 马中华 注释: 需要触发的 Watcher 集合 + Set watchers = new HashSet<>(); + PathParentIterator pathParentIterator = getPathParentIterator(path); + + synchronized (this) { + for (String localPath : pathParentIterator.asIterable()) { + + // TODO_MA 注释: 找到该 path 节点对应的所有 Watcher 集合 + Set thisWatchers = watchTable.get(localPath); + if (thisWatchers == null || thisWatchers.isEmpty()) { + continue; + } + + // TODO_MA 注释: 遍历每个 Watcher + Iterator iterator = thisWatchers.iterator(); + while (iterator.hasNext()) { + Watcher watcher = iterator.next(); + WatcherMode watcherMode = watcherModeManager.getWatcherMode(watcher, localPath); + if (watcherMode.isRecursive()) { + if (type != EventType.NodeChildrenChanged) { + watchers.add(watcher); + } + } else if (!pathParentIterator.atParentPath()) { + watchers.add(watcher); + if (!watcherMode.isPersistent()) { + iterator.remove(); + Set paths = watch2Paths.get(watcher); + if (paths != null) { + paths.remove(localPath); + } + } + } + } + if (thisWatchers.isEmpty()) { + watchTable.remove(localPath); + } + } + } + if (watchers.isEmpty()) { + if (LOG.isTraceEnabled()) { + ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "No watchers for " + path); + } + return null; + } + + /************************************************* + * TODO_MA 马中华 https://blog.csdn.net/zhongqi2513 + * 注释: 触发每一个 Watcher + */ + for (Watcher w : watchers) { + if (supress != null && supress.contains(w)) { + continue; + } + + // TODO_MA 马中华 注释: watcher 回调 + w.process(e); + } + + switch (type) { + case NodeCreated: + ServerMetrics.getMetrics().NODE_CREATED_WATCHER.add(watchers.size()); + break; + + case NodeDeleted: + ServerMetrics.getMetrics().NODE_DELETED_WATCHER.add(watchers.size()); + break; + + case NodeDataChanged: + ServerMetrics.getMetrics().NODE_CHANGED_WATCHER.add(watchers.size()); + break; + + case NodeChildrenChanged: + ServerMetrics.getMetrics().NODE_CHILDREN_WATCHER.add(watchers.size()); + break; + default: + // Other types not logged. + break; + } + + return new WatcherOrBitSet(watchers); + } + + @Override + public synchronized String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append(watch2Paths.size()).append(" connections watching ").append(watchTable.size()).append(" paths\n"); + + int total = 0; + for (Set paths : watch2Paths.values()) { + total += paths.size(); + } + sb.append("Total watches:").append(total); + + return sb.toString(); + } + + @Override + public synchronized void dumpWatches(PrintWriter pwriter, boolean byPath) { + if (byPath) { + for (Entry> e : watchTable.entrySet()) { + pwriter.println(e.getKey()); + for (Watcher w : e.getValue()) { + pwriter.print("\t0x"); + pwriter.print(Long.toHexString(((ServerCnxn) w).getSessionId())); + pwriter.print("\n"); + } + } + } else { + for (Entry> e : watch2Paths.entrySet()) { + pwriter.print("0x"); + pwriter.println(Long.toHexString(((ServerCnxn) e.getKey()).getSessionId())); + for (String path : e.getValue()) { + pwriter.print("\t"); + pwriter.println(path); + } + } + } + } + + @Override + public synchronized boolean containsWatcher(String path, Watcher watcher) { + WatcherMode watcherMode = watcherModeManager.getWatcherMode(watcher, path); + PathParentIterator pathParentIterator = getPathParentIterator(path); + for (String localPath : pathParentIterator.asIterable()) { + Set watchers = watchTable.get(localPath); + if (!pathParentIterator.atParentPath()) { + if (watchers != null) { + return true; // at the leaf node, all watcher types match + } + } + if (watcherMode.isRecursive()) { + return true; + } + } + return false; + } + + @Override + public synchronized boolean removeWatcher(String path, Watcher watcher) { + Set paths = watch2Paths.get(watcher); + if (paths == null || !paths.remove(path)) { + return false; + } + + Set list = watchTable.get(path); + if (list == null || !list.remove(watcher)) { + return false; + } + + if (list.isEmpty()) { + watchTable.remove(path); + } + + watcherModeManager.removeWatcher(watcher, path); + + return true; + } + + @Override + public synchronized WatchesReport getWatches() { + Map> id2paths = new HashMap<>(); + for (Entry> e : watch2Paths.entrySet()) { + Long id = ((ServerCnxn) e.getKey()).getSessionId(); + Set paths = new HashSet<>(e.getValue()); + id2paths.put(id, paths); + } + return new WatchesReport(id2paths); + } + + @Override + public synchronized WatchesPathReport getWatchesByPath() { + Map> path2ids = new HashMap<>(); + for (Entry> e : watchTable.entrySet()) { + Set ids = new HashSet<>(e.getValue().size()); + path2ids.put(e.getKey(), ids); + for (Watcher watcher : e.getValue()) { + ids.add(((ServerCnxn) watcher).getSessionId()); + } + } + return new WatchesPathReport(path2ids); + } + + @Override + public synchronized WatchesSummary getWatchesSummary() { + int totalWatches = 0; + for (Set paths : watch2Paths.values()) { + totalWatches += paths.size(); + } + return new WatchesSummary(watch2Paths.size(), watchTable.size(), totalWatches); + } + + @Override + public void shutdown() { /* do nothing */ } + + @Override + public int getRecursiveWatchQty() { + return watcherModeManager.getRecursiveQty(); + } + + private PathParentIterator getPathParentIterator(String path) { + if (watcherModeManager.getRecursiveQty() == 0) { + return PathParentIterator.forPathOnly(path); + } + return PathParentIterator.forAll(path); + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManagerFactory.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManagerFactory.java new file mode 100644 index 0000000..8a0b917 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManagerFactory.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.io.IOException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A factory used to produce the actual watch manager based on the + * zookeeper.watchManagerName option. + */ +public class WatchManagerFactory { + + private static final Logger LOG = LoggerFactory.getLogger(WatchManagerFactory.class); + + public static final String ZOOKEEPER_WATCH_MANAGER_NAME = "zookeeper.watchManagerName"; + + public static IWatchManager createWatchManager() throws IOException { + + // TODO_MA 注释: 默认就是 WatchManager + String watchManagerName = System.getProperty(ZOOKEEPER_WATCH_MANAGER_NAME); + if (watchManagerName == null) { + watchManagerName = WatchManager.class.getName(); + } + try { + IWatchManager watchManager = (IWatchManager) Class.forName(watchManagerName).getConstructor().newInstance(); + LOG.info("Using {} as watch manager", watchManagerName); + return watchManager; + } catch (Exception e) { + IOException ioe = new IOException("Couldn't instantiate " + watchManagerName, e); + throw ioe; + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManagerOptimized.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManagerOptimized.java new file mode 100644 index 0000000..1cc7deb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchManagerOptimized.java @@ -0,0 +1,405 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.io.PrintWriter; +import java.util.BitSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.util.BitHashSet; +import org.apache.zookeeper.server.util.BitMap; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Optimized in memory and time complexity, compared to WatchManager, both the + * memory consumption and time complexity improved a lot, but it cannot + * efficiently remove the watcher when the session or socket is closed, for + * majority use case this is not a problem. + * + * Changed made compared to WatchManager: + * + * - Use HashSet and BitSet to store the watchers to find a balance between + * memory usage and time complexity + * - Use ReadWriteLock instead of synchronized to reduce lock retention + * - Lazily clean up the closed watchers + */ +public class WatchManagerOptimized implements IWatchManager, IDeadWatcherListener { + + private static final Logger LOG = LoggerFactory.getLogger(WatchManagerOptimized.class); + + private final ConcurrentHashMap pathWatches = new ConcurrentHashMap(); + + // watcher to bit id mapping + private final BitMap watcherBitIdMap = new BitMap(); + + // used to lazily remove the dead watchers + private final WatcherCleaner watcherCleaner; + + private final ReentrantReadWriteLock addRemovePathRWLock = new ReentrantReadWriteLock(); + + public WatchManagerOptimized() { + watcherCleaner = new WatcherCleaner(this); + watcherCleaner.start(); + } + + @Override + public boolean addWatch(String path, Watcher watcher) { + boolean result = false; + // Need readLock to exclusively lock with removeWatcher, otherwise we + // may add a dead watch whose connection was just closed. + // + // Creating new watcher bit and adding it to the BitHashSet has it's + // own lock to minimize the write lock scope + addRemovePathRWLock.readLock().lock(); + try { + // avoid race condition of adding a on flying dead watcher + if (isDeadWatcher(watcher)) { + LOG.debug("Ignoring addWatch with closed cnxn"); + } else { + Integer bit = watcherBitIdMap.add(watcher); + BitHashSet watchers = pathWatches.get(path); + if (watchers == null) { + watchers = new BitHashSet(); + BitHashSet existingWatchers = pathWatches.putIfAbsent(path, watchers); + // it's possible multiple thread might add to pathWatches + // while we're holding read lock, so we need this check + // here + if (existingWatchers != null) { + watchers = existingWatchers; + } + } + result = watchers.add(bit); + } + } finally { + addRemovePathRWLock.readLock().unlock(); + } + return result; + } + + /** + * Used in the OpCode.checkWatches, which is a read operation, since read + * and write requests are exclusively processed, we don't need to hold + * lock here. + * + * Different from addWatch this method doesn't mutate any state, so we don't + * need to hold read lock to avoid dead watcher (cnxn closed) being added + * to the watcher manager. + * + * It's possible that before we lazily clean up the dead watcher, this will + * return true, but since the cnxn is closed, the response will dropped as + * well, so it doesn't matter. + */ + @Override + public boolean containsWatcher(String path, Watcher watcher) { + BitHashSet watchers = pathWatches.get(path); + return watchers != null && watchers.contains(watcherBitIdMap.getBit(watcher)); + } + + @Override + public boolean removeWatcher(String path, Watcher watcher) { + // Hold write lock directly because removeWatcher request is more + // likely to be invoked when the watcher is actually exist and + // haven't fired yet, so instead of having read lock to check existence + // before switching to write one, it's actually cheaper to hold write + // lock directly here. + addRemovePathRWLock.writeLock().lock(); + try { + BitHashSet list = pathWatches.get(path); + if (list == null || !list.remove(watcherBitIdMap.getBit(watcher))) { + return false; + } + if (list.isEmpty()) { + pathWatches.remove(path); + } + return true; + } finally { + addRemovePathRWLock.writeLock().unlock(); + } + } + + @Override + public void removeWatcher(Watcher watcher) { + Integer watcherBit; + // Use exclusive lock with addWatcher to guarantee that we won't add + // watch for a cnxn which is already closed. + addRemovePathRWLock.writeLock().lock(); + try { + // do nothing if the watcher is not tracked + watcherBit = watcherBitIdMap.getBit(watcher); + if (watcherBit == null) { + return; + } + } finally { + addRemovePathRWLock.writeLock().unlock(); + } + + // We can guarantee that when this line is executed, the cnxn of this + // watcher has already been marked as stale (this method is only called + // from ServerCnxn.close after we set stale), which means no watches + // will be added to the watcher manager with this watcher, so that we + // can safely clean up this dead watcher. + // + // So it's not necessary to have this line in the addRemovePathRWLock. + // And moving the addDeadWatcher out of the locking block to avoid + // holding the write lock while we're blocked on adding dead watchers + // into the watcherCleaner. + watcherCleaner.addDeadWatcher(watcherBit); + } + + /** + * Entry for WatcherCleaner to remove dead watchers + * + * @param deadWatchers the watchers need to be removed + */ + @Override + public void processDeadWatchers(Set deadWatchers) { + // All the watchers being processed here are guaranteed to be dead, + // no watches will be added for those dead watchers, that's why I + // don't need to have addRemovePathRWLock here. + BitSet bits = new BitSet(); + for (int dw : deadWatchers) { + bits.set(dw); + } + // The value iterator will reflect the state when it was + // created, don't need to synchronize. + for (BitHashSet watchers : pathWatches.values()) { + watchers.remove(deadWatchers, bits); + } + // Better to remove the empty path from pathWatches, but it will add + // lot of lock contention and affect the throughput of addWatch, + // let's rely on the triggerWatch to delete it. + for (Integer wbit : deadWatchers) { + watcherBitIdMap.remove(wbit); + } + } + + @Override + public WatcherOrBitSet triggerWatch(String path, EventType type) { + return triggerWatch(path, type, null); + } + + @Override + public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet suppress) { + WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path); + + BitHashSet watchers = remove(path); + if (watchers == null) { + return null; + } + + int triggeredWatches = 0; + + // Avoid race condition between dead watcher cleaner in + // WatcherCleaner and iterating here + synchronized (watchers) { + for (Integer wBit : watchers) { + if (suppress != null && suppress.contains(wBit)) { + continue; + } + + Watcher w = watcherBitIdMap.get(wBit); + + // skip dead watcher + if (w == null || isDeadWatcher(w)) { + continue; + } + + w.process(e); + triggeredWatches++; + } + } + + updateMetrics(type, triggeredWatches); + return new WatcherOrBitSet(watchers); + } + + @Override + public int size() { + int size = 0; + for (BitHashSet watches : pathWatches.values()) { + size += watches.size(); + } + return size; + } + + @Override + public void shutdown() { + if (watcherCleaner != null) { + watcherCleaner.shutdown(); + } + } + + private BitHashSet remove(String path) { + addRemovePathRWLock.writeLock().lock(); + try { + return pathWatches.remove(path); + } finally { + addRemovePathRWLock.writeLock().unlock(); + } + } + + void updateMetrics(final EventType type, int size) { + switch (type) { + case NodeCreated: + ServerMetrics.getMetrics().NODE_CREATED_WATCHER.add(size); + break; + + case NodeDeleted: + ServerMetrics.getMetrics().NODE_DELETED_WATCHER.add(size); + break; + + case NodeDataChanged: + ServerMetrics.getMetrics().NODE_CHANGED_WATCHER.add(size); + break; + + case NodeChildrenChanged: + ServerMetrics.getMetrics().NODE_CHILDREN_WATCHER.add(size); + break; + default: + // Other types not logged. + break; + } + } + + boolean isDeadWatcher(Watcher watcher) { + return watcher instanceof ServerCnxn && ((ServerCnxn) watcher).isStale(); + } + + int pathSize() { + return pathWatches.size(); + } + + @Override + public WatchesSummary getWatchesSummary() { + return new WatchesSummary(watcherBitIdMap.size(), pathSize(), size()); + } + + @Override + public WatchesReport getWatches() { + Map> id2paths = new HashMap>(); + for (Entry> e : getWatcher2PathesMap().entrySet()) { + Long id = ((ServerCnxn) e.getKey()).getSessionId(); + Set paths = new HashSet(e.getValue()); + id2paths.put(id, paths); + } + return new WatchesReport(id2paths); + } + + /** + * Iterate through ConcurrentHashMap is 'safe', it will reflect the state + * of the map at the time iteration began, may miss update while iterating, + * given this is used in the commands to get a general idea of the watches + * state, we don't care about missing some update. + */ + @Override + public WatchesPathReport getWatchesByPath() { + Map> path2ids = new HashMap>(); + for (Entry e : pathWatches.entrySet()) { + BitHashSet watchers = e.getValue(); + synchronized (watchers) { + Set ids = new HashSet(watchers.size()); + path2ids.put(e.getKey(), ids); + for (Integer wbit : watchers) { + Watcher watcher = watcherBitIdMap.get(wbit); + if (watcher instanceof ServerCnxn) { + ids.add(((ServerCnxn) watcher).getSessionId()); + } + } + } + } + return new WatchesPathReport(path2ids); + } + + /** + * May cause OOM if there are lots of watches, might better to forbid + * it in this class. + */ + public Map> getWatcher2PathesMap() { + Map> watcher2paths = new HashMap>(); + for (Entry e : pathWatches.entrySet()) { + String path = e.getKey(); + BitHashSet watchers = e.getValue(); + // avoid race condition with add/remove + synchronized (watchers) { + for (Integer wbit : watchers) { + Watcher w = watcherBitIdMap.get(wbit); + if (w == null) { + continue; + } + if (!watcher2paths.containsKey(w)) { + watcher2paths.put(w, new HashSet()); + } + watcher2paths.get(w).add(path); + } + } + } + return watcher2paths; + } + + @Override + public void dumpWatches(PrintWriter pwriter, boolean byPath) { + if (byPath) { + for (Entry e : pathWatches.entrySet()) { + pwriter.println(e.getKey()); + BitHashSet watchers = e.getValue(); + synchronized (watchers) { + for (Integer wbit : watchers) { + Watcher w = watcherBitIdMap.get(wbit); + if (!(w instanceof ServerCnxn)) { + continue; + } + pwriter.print("\t0x"); + pwriter.print(Long.toHexString(((ServerCnxn) w).getSessionId())); + pwriter.print("\n"); + } + } + } + } else { + for (Entry> e : getWatcher2PathesMap().entrySet()) { + pwriter.print("0x"); + pwriter.println(Long.toHexString(((ServerCnxn) e.getKey()).getSessionId())); + for (String path : e.getValue()) { + pwriter.print("\t"); + pwriter.println(path); + } + } + } + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(watcherBitIdMap.size()).append(" connections watching ").append(pathSize()).append(" paths\n"); + sb.append("Total watches:").append(size()); + return sb.toString(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherCleaner.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherCleaner.java new file mode 100644 index 0000000..be98f98 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherCleaner.java @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.RateLogger; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.WorkerService; +import org.apache.zookeeper.server.WorkerService.WorkRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Thread used to lazily clean up the closed watcher, it will trigger the + * clean up when the dead watchers get certain number or some number of + * seconds has elapsed since last clean up. + * + * Cost of running it: + * + * - need to go through all the paths even if the watcher may only + * watching a single path + * - block in the path BitHashSet when we try to check the dead watcher + * which won't block other stuff + */ +public class WatcherCleaner extends Thread { + + private static final Logger LOG = LoggerFactory.getLogger(WatcherCleaner.class); + private final RateLogger RATE_LOGGER = new RateLogger(LOG); + + private volatile boolean stopped = false; + private final Object cleanEvent = new Object(); + private final Object processingCompletedEvent = new Object(); + private final WorkerService cleaners; + + private final Set deadWatchers; + private final IDeadWatcherListener listener; + private final int watcherCleanThreshold; + private final int watcherCleanIntervalInSeconds; + private final int maxInProcessingDeadWatchers; + private final AtomicInteger totalDeadWatchers = new AtomicInteger(); + + public WatcherCleaner(IDeadWatcherListener listener) { + this( + listener, + Integer.getInteger("zookeeper.watcherCleanThreshold", 1000), + Integer.getInteger("zookeeper.watcherCleanIntervalInSeconds", 600), + Integer.getInteger("zookeeper.watcherCleanThreadsNum", 2), + Integer.getInteger("zookeeper.maxInProcessingDeadWatchers", -1)); + } + + public WatcherCleaner(IDeadWatcherListener listener, int watcherCleanThreshold, int watcherCleanIntervalInSeconds, int watcherCleanThreadsNum, int maxInProcessingDeadWatchers) { + this.listener = listener; + this.watcherCleanThreshold = watcherCleanThreshold; + this.watcherCleanIntervalInSeconds = watcherCleanIntervalInSeconds; + int suggestedMaxInProcessingThreshold = watcherCleanThreshold * watcherCleanThreadsNum; + if (maxInProcessingDeadWatchers > 0 && maxInProcessingDeadWatchers < suggestedMaxInProcessingThreshold) { + maxInProcessingDeadWatchers = suggestedMaxInProcessingThreshold; + LOG.info( + "The maxInProcessingDeadWatchers config is smaller than the suggested one, change it to use {}", + maxInProcessingDeadWatchers); + } + this.maxInProcessingDeadWatchers = maxInProcessingDeadWatchers; + this.deadWatchers = new HashSet(); + this.cleaners = new WorkerService("DeadWatcherCleanner", watcherCleanThreadsNum, false); + + LOG.info( + "watcherCleanThreshold={}, watcherCleanIntervalInSeconds={}" + + ", watcherCleanThreadsNum={}, maxInProcessingDeadWatchers={}", + watcherCleanThreshold, + watcherCleanIntervalInSeconds, + watcherCleanThreadsNum, + maxInProcessingDeadWatchers); + } + + public void addDeadWatcher(int watcherBit) { + // Wait if there are too many watchers waiting to be closed, + // this is will slow down the socket packet processing and + // the adding watches in the ZK pipeline. + while (maxInProcessingDeadWatchers > 0 && !stopped && totalDeadWatchers.get() >= maxInProcessingDeadWatchers) { + try { + RATE_LOGGER.rateLimitLog("Waiting for dead watchers cleaning"); + long startTime = Time.currentElapsedTime(); + synchronized (processingCompletedEvent) { + processingCompletedEvent.wait(100); + } + long latency = Time.currentElapsedTime() - startTime; + ServerMetrics.getMetrics().ADD_DEAD_WATCHER_STALL_TIME.add(latency); + } catch (InterruptedException e) { + LOG.info("Got interrupted while waiting for dead watches queue size"); + break; + } + } + synchronized (this) { + if (deadWatchers.add(watcherBit)) { + totalDeadWatchers.incrementAndGet(); + ServerMetrics.getMetrics().DEAD_WATCHERS_QUEUED.add(1); + if (deadWatchers.size() >= watcherCleanThreshold) { + synchronized (cleanEvent) { + cleanEvent.notifyAll(); + } + } + } + } + } + + @Override + public void run() { + while (!stopped) { + synchronized (cleanEvent) { + try { + // add some jitter to avoid cleaning dead watchers at the + // same time in the quorum + if (!stopped && deadWatchers.size() < watcherCleanThreshold) { + int maxWaitMs = (watcherCleanIntervalInSeconds + + ThreadLocalRandom.current().nextInt(watcherCleanIntervalInSeconds / 2 + 1)) * 1000; + cleanEvent.wait(maxWaitMs); + } + } catch (InterruptedException e) { + LOG.info("Received InterruptedException while waiting for cleanEvent"); + break; + } + } + + if (deadWatchers.isEmpty()) { + continue; + } + + synchronized (this) { + // Clean the dead watchers need to go through all the current + // watches, which is pretty heavy and may take a second if + // there are millions of watches, that's why we're doing lazily + // batch clean up in a separate thread with a snapshot of the + // current dead watchers. + final Set snapshot = new HashSet(deadWatchers); + deadWatchers.clear(); + int total = snapshot.size(); + LOG.info("Processing {} dead watchers", total); + cleaners.schedule(new WorkRequest() { + @Override + public void doWork() throws Exception { + long startTime = Time.currentElapsedTime(); + listener.processDeadWatchers(snapshot); + long latency = Time.currentElapsedTime() - startTime; + LOG.info("Takes {} to process {} watches", latency, total); + ServerMetrics.getMetrics().DEAD_WATCHERS_CLEANER_LATENCY.add(latency); + ServerMetrics.getMetrics().DEAD_WATCHERS_CLEARED.add(total); + totalDeadWatchers.addAndGet(-total); + synchronized (processingCompletedEvent) { + processingCompletedEvent.notifyAll(); + } + } + }); + } + } + LOG.info("WatcherCleaner thread exited"); + } + + public void shutdown() { + stopped = true; + deadWatchers.clear(); + cleaners.stop(); + this.interrupt(); + if (LOG.isInfoEnabled()) { + LOG.info("WatcherCleaner thread shutdown is initiated"); + } + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherMode.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherMode.java new file mode 100644 index 0000000..b8a1dda --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherMode.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import org.apache.zookeeper.ZooDefs; + +public enum WatcherMode { + STANDARD(false, false), + PERSISTENT(true, false), + PERSISTENT_RECURSIVE(true, true) + ; + + public static final WatcherMode DEFAULT_WATCHER_MODE = WatcherMode.STANDARD; + + public static WatcherMode fromZooDef(int mode) { + switch (mode) { + case ZooDefs.AddWatchModes.persistent: + return PERSISTENT; + case ZooDefs.AddWatchModes.persistentRecursive: + return PERSISTENT_RECURSIVE; + } + throw new IllegalArgumentException("Unsupported mode: " + mode); + } + + private final boolean isPersistent; + private final boolean isRecursive; + + WatcherMode(boolean isPersistent, boolean isRecursive) { + this.isPersistent = isPersistent; + this.isRecursive = isRecursive; + } + + public boolean isPersistent() { + return isPersistent; + } + + public boolean isRecursive() { + return isRecursive; + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherModeManager.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherModeManager.java new file mode 100644 index 0000000..c1a8225 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherModeManager.java @@ -0,0 +1,96 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.Watcher; + +class WatcherModeManager { + private final Map watcherModes = new ConcurrentHashMap<>(); + private final AtomicInteger recursiveQty = new AtomicInteger(0); + + private static class Key { + private final Watcher watcher; + private final String path; + + Key(Watcher watcher, String path) { + this.watcher = watcher; + this.path = path; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Key key = (Key) o; + return watcher.equals(key.watcher) && path.equals(key.path); + } + + @Override + public int hashCode() { + return Objects.hash(watcher, path); + } + } + + // VisibleForTesting + Map getWatcherModes() { + return watcherModes; + } + + void setWatcherMode(Watcher watcher, String path, WatcherMode mode) { + if (mode == WatcherMode.DEFAULT_WATCHER_MODE) { + removeWatcher(watcher, path); + } else { + adjustRecursiveQty(watcherModes.put(new Key(watcher, path), mode), mode); + } + } + + WatcherMode getWatcherMode(Watcher watcher, String path) { + return watcherModes.getOrDefault(new Key(watcher, path), WatcherMode.DEFAULT_WATCHER_MODE); + } + + void removeWatcher(Watcher watcher, String path) { + adjustRecursiveQty(watcherModes.remove(new Key(watcher, path)), WatcherMode.DEFAULT_WATCHER_MODE); + } + + int getRecursiveQty() { + return recursiveQty.get(); + } + + // recursiveQty is an optimization to avoid having to walk the map every time this value is needed + private void adjustRecursiveQty(WatcherMode oldMode, WatcherMode newMode) { + if (oldMode == null) { + oldMode = WatcherMode.DEFAULT_WATCHER_MODE; + } + if (oldMode.isRecursive() != newMode.isRecursive()) { + if (newMode.isRecursive()) { + recursiveQty.incrementAndGet(); + } else { + recursiveQty.decrementAndGet(); + } + } + } +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherOrBitSet.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherOrBitSet.java new file mode 100644 index 0000000..54fda5a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatcherOrBitSet.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import java.util.Set; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.server.util.BitHashSet; + +public class WatcherOrBitSet { + + private Set watchers; + private BitHashSet watcherBits; + + public WatcherOrBitSet(final Set watchers) { + this.watchers = watchers; + } + + public WatcherOrBitSet(final BitHashSet watcherBits) { + this.watcherBits = watcherBits; + } + + public boolean contains(Watcher watcher) { + if (watchers == null) { + return false; + } + return watchers.contains(watcher); + } + + public boolean contains(int watcherBit) { + if (watcherBits == null) { + return false; + } + return watcherBits.contains(watcherBit); + } + + public int size() { + if (watchers != null) { + return watchers.size(); + } + if (watcherBits != null) { + return watcherBits.size(); + } + return 0; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesPathReport.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesPathReport.java new file mode 100644 index 0000000..9d244b0 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesPathReport.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * A watch report, essentially a mapping of path to session IDs of sessions that + * have set a watch on that path. This class is immutable. + */ +public class WatchesPathReport { + + private final Map> path2Ids; + + /** + * Creates a new report. + * + * @param path2Ids map of paths to session IDs of sessions that have set a + * watch on that path + */ + WatchesPathReport(Map> path2Ids) { + this.path2Ids = Collections.unmodifiableMap(deepCopy(path2Ids)); + } + + private static Map> deepCopy(Map> m) { + Map> m2 = new HashMap>(); + for (Map.Entry> e : m.entrySet()) { + m2.put(e.getKey(), new HashSet(e.getValue())); + } + return m2; + } + + /** + * Checks if the given path has watches set. + * + * @param path path + * @return true if path has watch set + */ + public boolean hasSessions(String path) { + return path2Ids.containsKey(path); + } + /** + * Gets the session IDs of sessions that have set watches on the given path. + * The returned set is immutable. + * + * @param path session ID + * @return session IDs of sessions that have set watches on the path, or + * null if none + */ + public Set getSessions(String path) { + Set s = path2Ids.get(path); + return s != null ? Collections.unmodifiableSet(s) : null; + } + + /** + * Converts this report to a map. The returned map is mutable, and changes + * to it do not reflect back into this report. + * + * @return map representation of report + */ + public Map> toMap() { + return deepCopy(path2Ids); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesReport.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesReport.java new file mode 100644 index 0000000..e506143 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesReport.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * A watch report, essentially a mapping of session ID to paths that the session + * has set a watch on. This class is immutable. + */ +public class WatchesReport { + + private final Map> id2paths; + + /** + * Creates a new report. + * + * @param id2paths map of session IDs to paths that each session has set + * a watch on + */ + WatchesReport(Map> id2paths) { + this.id2paths = Collections.unmodifiableMap(deepCopy(id2paths)); + } + + private static Map> deepCopy(Map> m) { + Map> m2 = new HashMap>(); + for (Map.Entry> e : m.entrySet()) { + m2.put(e.getKey(), new HashSet(e.getValue())); + } + return m2; + } + + /** + * Checks if the given session has watches set. + * + * @param sessionId session ID + * @return true if session has paths with watches set + */ + public boolean hasPaths(long sessionId) { + return id2paths.containsKey(sessionId); + } + + /** + * Gets the paths that the given session has set watches on. The returned + * set is immutable. + * + * @param sessionId session ID + * @return paths that have watches set by the session, or null if none + */ + public Set getPaths(long sessionId) { + Set s = id2paths.get(sessionId); + return s != null ? Collections.unmodifiableSet(s) : null; + } + + /** + * Converts this report to a map. The returned map is mutable, and changes + * to it do not reflect back into this report. + * + * @return map representation of report + */ + public Map> toMap() { + return deepCopy(id2paths); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesSummary.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesSummary.java new file mode 100644 index 0000000..9cd36ca --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/watch/WatchesSummary.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * A summary of watch information. This class is immutable. + */ +public class WatchesSummary { + + /** + * The key in the map returned by {@link #toMap()} for the number of + * connections. + */ + public static final String KEY_NUM_CONNECTIONS = "num_connections"; + /** + * The key in the map returned by {@link #toMap()} for the number of paths. + */ + public static final String KEY_NUM_PATHS = "num_paths"; + /** + * The key in the map returned by {@link #toMap()} for the total number of + * watches. + */ + public static final String KEY_NUM_TOTAL_WATCHES = "num_total_watches"; + + private final int numConnections; + private final int numPaths; + private final int totalWatches; + + /** + * Creates a new summary. + * + * @param numConnections the number of sessions that have set watches + * @param numPaths the number of paths that have watches set on them + * @param totalWatches the total number of watches set + */ + WatchesSummary(int numConnections, int numPaths, int totalWatches) { + this.numConnections = numConnections; + this.numPaths = numPaths; + this.totalWatches = totalWatches; + } + + /** + * Gets the number of connections (sessions) that have set watches. + * + * @return number of connections + */ + public int getNumConnections() { + return numConnections; + } + /** + * Gets the number of paths that have watches set on them. + * + * @return number of paths + */ + public int getNumPaths() { + return numPaths; + } + /** + * Gets the total number of watches set. + * + * @return total watches + */ + public int getTotalWatches() { + return totalWatches; + } + + /** + * Converts this summary to a map. The returned map is mutable, and changes + * to it do not reflect back into this summary. + * + * @return map representation of summary + */ + public Map toMap() { + Map summary = new LinkedHashMap(); + summary.put(KEY_NUM_CONNECTIONS, numConnections); + summary.put(KEY_NUM_PATHS, numPaths); + summary.put(KEY_NUM_TOTAL_WATCHES, totalWatches); + return summary; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/util/CircularBlockingQueue.java b/zookeeper-server/src/main/java/org/apache/zookeeper/util/CircularBlockingQueue.java new file mode 100644 index 0000000..cbacb65 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/util/CircularBlockingQueue.java @@ -0,0 +1,277 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Iterator; +import java.util.Objects; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A bounded blocking queue backed by an array. This queue orders elements FIFO + * (first-in-first-out). The head of the queue is that element that has been on + * the queue the longest time. The tail of the queue is that element that has + * been on the queue the shortest time. New elements are inserted at the tail of + * the queue, and the queue retrieval operations obtain elements at the head of + * the queue. If the queue is full, the head of the queue (the oldest element) + * will be removed to make room for the newest element. + */ +public class CircularBlockingQueue implements BlockingQueue { + + private static final Logger LOG = LoggerFactory.getLogger(CircularBlockingQueue.class); + + /** Main lock guarding all access */ + private final ReentrantLock lock; + + /** Condition for waiting takes */ + private final Condition notEmpty; + + /** The array-backed queue */ + private final ArrayDeque queue; + + private final int maxSize; + + private long droppedCount; + + public CircularBlockingQueue(int queueSize) { + this.queue = new ArrayDeque<>(queueSize); + this.maxSize = queueSize; + + this.lock = new ReentrantLock(); + this.notEmpty = this.lock.newCondition(); + this.droppedCount = 0L; + } + + /** + * This method differs from {@link BlockingQueue#offer(Object)} in that it + * will remove the oldest queued element (the element at the front of the + * queue) in order to make room for any new elements if the queue is full. + * + * @param e the element to add + * @return true since it will make room for any new elements if required + */ + @Override + public boolean offer(E e) { + Objects.requireNonNull(e); + final ReentrantLock lock = this.lock; + lock.lock(); + try { + if (this.queue.size() == this.maxSize) { + final E discard = this.queue.remove(); + this.droppedCount++; + LOG.debug("Queue is full. Discarding oldest element [count={}]: {}", + this.droppedCount, discard); + } + this.queue.add(e); + this.notEmpty.signal(); + } finally { + lock.unlock(); + } + return true; + } + + @Override + public E poll(long timeout, TimeUnit unit) throws InterruptedException { + long nanos = unit.toNanos(timeout); + final ReentrantLock lock = this.lock; + lock.lockInterruptibly(); + try { + while (this.queue.isEmpty()) { + if (nanos <= 0) { + return null; + } + nanos = this.notEmpty.awaitNanos(nanos); + } + return this.queue.poll(); + } finally { + lock.unlock(); + } + } + + @Override + public E take() throws InterruptedException { + final ReentrantLock lock = this.lock; + lock.lockInterruptibly(); + try { + while (this.queue.isEmpty()) { + this.notEmpty.await(); + } + return this.queue.poll(); + } finally { + lock.unlock(); + } + } + + @Override + public boolean isEmpty() { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + return this.queue.isEmpty(); + } finally { + lock.unlock(); + } + } + + @Override + public int size() { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + return this.queue.size(); + } finally { + lock.unlock(); + } + } + + /** + * Returns the number of elements that were dropped from the queue because the + * queue was full when a new element was offered. + * + * @return The number of elements dropped (lost) from the queue + */ + public long getDroppedCount() { + return this.droppedCount; + } + + /** + * For testing purposes only. + * + * @return True if a thread is blocked waiting for a new element to be offered + * to the queue + */ + boolean isConsumerThreadBlocked() { + final ReentrantLock lock = this.lock; + lock.lock(); + try { + return lock.getWaitQueueLength(this.notEmpty) > 0; + } finally { + lock.unlock(); + } + } + + @Override + public int drainTo(Collection c) { + throw new UnsupportedOperationException(); + } + + + @Override + public E poll() { + throw new UnsupportedOperationException(); + } + + @Override + public E element() { + throw new UnsupportedOperationException(); + } + + @Override + public E peek() { + throw new UnsupportedOperationException(); + } + + @Override + public E remove() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean addAll(Collection arg0) { + throw new UnsupportedOperationException(); + } + + @Override + public void clear() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean containsAll(Collection arg0) { + throw new UnsupportedOperationException(); + } + + @Override + public Iterator iterator() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean removeAll(Collection arg0) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean retainAll(Collection arg0) { + throw new UnsupportedOperationException(); + } + + @Override + public Object[] toArray() { + throw new UnsupportedOperationException(); + } + + @Override + public T[] toArray(T[] arg0) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean add(E e) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean contains(Object o) { + throw new UnsupportedOperationException(); + } + + @Override + public int drainTo(Collection c, int maxElements) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean offer(E e, long timeout, TimeUnit unit) + throws InterruptedException { + throw new UnsupportedOperationException(); + } + + @Override + public void put(E e) throws InterruptedException { + throw new UnsupportedOperationException(); + } + + @Override + public int remainingCapacity() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean remove(Object o) { + throw new UnsupportedOperationException(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/util/PemReader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/util/PemReader.java new file mode 100644 index 0000000..9ab2241 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/util/PemReader.java @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import static java.nio.charset.StandardCharsets.US_ASCII; +import static java.util.Base64.getMimeDecoder; +import static java.util.regex.Pattern.CASE_INSENSITIVE; +import static javax.crypto.Cipher.DECRYPT_MODE; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.security.GeneralSecurityException; +import java.security.KeyFactory; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.crypto.Cipher; +import javax.crypto.EncryptedPrivateKeyInfo; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import javax.security.auth.x500.X500Principal; + +/** + * Note: this class is copied from io.airlift.security.pem.PemReader (see + * https://github.com/airlift/airlift/blob/master/security/src/main/java/io/airlift/security/pem/PemReader.java) with + * permission of the authors, to avoid adding an extra library dependency to Zookeeper. + * The file was copied from commit hash 86348546af43217f4d04a0cdad624b0ae4751c2c. + * + * The following modifications have been made to the original source code: + *

        + *
      • imports have been rearranged to match Zookeeper import order style.
      • + *
      • The dependency on com.google.common.io.Files.asCharSource has been removed.
      • + *
      • A dependency on java.nio.file.Files has been added.
      • + *
      + */ +public final class PemReader { + + private static final Pattern CERT_PATTERN = Pattern.compile( + "-+BEGIN\\s+.*CERTIFICATE[^-]*-+(?:\\s|\\r|\\n)+" + // Header + "([a-z0-9+/=\\r\\n]+)" + // Base64 text + "-+END\\s+.*CERTIFICATE[^-]*-+", // Footer + CASE_INSENSITIVE); + + private static final Pattern PRIVATE_KEY_PATTERN = Pattern.compile( + "-+BEGIN\\s+.*PRIVATE\\s+KEY[^-]*-+(?:\\s|\\r|\\n)+" + // Header + "([a-z0-9+/=\\r\\n]+)" + // Base64 text + "-+END\\s+.*PRIVATE\\s+KEY[^-]*-+", // Footer + CASE_INSENSITIVE); + + private static final Pattern PUBLIC_KEY_PATTERN = Pattern.compile( + "-+BEGIN\\s+.*PUBLIC\\s+KEY[^-]*-+(?:\\s|\\r|\\n)+" + // Header + "([a-z0-9+/=\\r\\n]+)" + // Base64 text + "-+END\\s+.*PUBLIC\\s+KEY[^-]*-+", // Footer + CASE_INSENSITIVE); + + private PemReader() { + } + + public static KeyStore loadTrustStore(File certificateChainFile) throws IOException, GeneralSecurityException { + KeyStore keyStore = KeyStore.getInstance("JKS"); + keyStore.load(null, null); + + List certificateChain = readCertificateChain(certificateChainFile); + for (X509Certificate certificate : certificateChain) { + X500Principal principal = certificate.getSubjectX500Principal(); + keyStore.setCertificateEntry(principal.getName("RFC2253"), certificate); + } + return keyStore; + } + + public static KeyStore loadKeyStore(File certificateChainFile, File privateKeyFile, Optional keyPassword) throws IOException, GeneralSecurityException { + PrivateKey key = loadPrivateKey(privateKeyFile, keyPassword); + + List certificateChain = readCertificateChain(certificateChainFile); + if (certificateChain.isEmpty()) { + throw new CertificateException("Certificate file does not contain any certificates: " + + certificateChainFile); + } + + KeyStore keyStore = KeyStore.getInstance("JKS"); + keyStore.load(null, null); + keyStore.setKeyEntry("key", + key, + keyPassword.orElse("").toCharArray(), + certificateChain.toArray(new Certificate[0])); + return keyStore; + } + + public static List readCertificateChain(File certificateChainFile) throws IOException, GeneralSecurityException { + String contents = new String(Files.readAllBytes(certificateChainFile.toPath()), US_ASCII); + return readCertificateChain(contents); + } + + public static List readCertificateChain(String certificateChain) throws CertificateException { + Matcher matcher = CERT_PATTERN.matcher(certificateChain); + CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); + List certificates = new ArrayList<>(); + + int start = 0; + while (matcher.find(start)) { + byte[] buffer = base64Decode(matcher.group(1)); + certificates.add((X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(buffer))); + start = matcher.end(); + } + + return certificates; + } + + public static PrivateKey loadPrivateKey(File privateKeyFile, Optional keyPassword) throws IOException, GeneralSecurityException { + String privateKey = new String(Files.readAllBytes(privateKeyFile.toPath()), US_ASCII); + return loadPrivateKey(privateKey, keyPassword); + } + + public static PrivateKey loadPrivateKey(String privateKey, Optional keyPassword) throws IOException, GeneralSecurityException { + Matcher matcher = PRIVATE_KEY_PATTERN.matcher(privateKey); + if (!matcher.find()) { + throw new KeyStoreException("did not find a private key"); + } + byte[] encodedKey = base64Decode(matcher.group(1)); + + PKCS8EncodedKeySpec encodedKeySpec; + if (keyPassword.isPresent()) { + EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(encodedKey); + SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName()); + SecretKey secretKey = keyFactory.generateSecret(new PBEKeySpec(keyPassword.get().toCharArray())); + + Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName()); + cipher.init(DECRYPT_MODE, secretKey, encryptedPrivateKeyInfo.getAlgParameters()); + + encodedKeySpec = encryptedPrivateKeyInfo.getKeySpec(cipher); + } else { + encodedKeySpec = new PKCS8EncodedKeySpec(encodedKey); + } + + // this code requires a key in PKCS8 format which is not the default openssl format + // to convert to the PKCS8 format you use : openssl pkcs8 -topk8 ... + try { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePrivate(encodedKeySpec); + } catch (InvalidKeySpecException ignore) { + } + + try { + KeyFactory keyFactory = KeyFactory.getInstance("EC"); + return keyFactory.generatePrivate(encodedKeySpec); + } catch (InvalidKeySpecException ignore) { + } + + KeyFactory keyFactory = KeyFactory.getInstance("DSA"); + return keyFactory.generatePrivate(encodedKeySpec); + } + + public static PublicKey loadPublicKey(File publicKeyFile) throws IOException, GeneralSecurityException { + String publicKey = new String(Files.readAllBytes(publicKeyFile.toPath()), US_ASCII); + return loadPublicKey(publicKey); + } + + public static PublicKey loadPublicKey(String publicKey) throws GeneralSecurityException { + Matcher matcher = PUBLIC_KEY_PATTERN.matcher(publicKey); + if (!matcher.find()) { + throw new KeyStoreException("did not find a public key"); + } + String data = matcher.group(1); + byte[] encodedKey = base64Decode(data); + + X509EncodedKeySpec encodedKeySpec = new X509EncodedKeySpec(encodedKey); + try { + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePublic(encodedKeySpec); + } catch (InvalidKeySpecException ignore) { + } + + try { + KeyFactory keyFactory = KeyFactory.getInstance("EC"); + return keyFactory.generatePublic(encodedKeySpec); + } catch (InvalidKeySpecException ignore) { + } + + KeyFactory keyFactory = KeyFactory.getInstance("DSA"); + return keyFactory.generatePublic(encodedKeySpec); + } + + private static byte[] base64Decode(String base64) { + return getMimeDecoder().decode(base64.getBytes(US_ASCII)); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/util/SecurityUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/util/SecurityUtils.java new file mode 100644 index 0000000..6ac3fff --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/util/SecurityUtils.java @@ -0,0 +1,288 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import java.security.Principal; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import javax.security.auth.Subject; +import javax.security.auth.callback.CallbackHandler; +import javax.security.sasl.Sasl; +import javax.security.sasl.SaslClient; +import javax.security.sasl.SaslException; +import javax.security.sasl.SaslServer; +import org.apache.zookeeper.SaslClientCallbackHandler; +import org.apache.zookeeper.server.auth.KerberosName; +import org.ietf.jgss.GSSContext; +import org.ietf.jgss.GSSCredential; +import org.ietf.jgss.GSSException; +import org.ietf.jgss.GSSManager; +import org.ietf.jgss.GSSName; +import org.ietf.jgss.Oid; +import org.slf4j.Logger; + +public final class SecurityUtils { + + public static final String QUORUM_HOSTNAME_PATTERN = "_HOST"; + + /** + * Create an instance of a SaslClient. It will return null if there is an exception. + * + * @param subject subject + * @param servicePrincipal principal + * @param protocol name of the protocol for which the authentication is being performed + * @param serverName name of the server to authenticate to + * @param LOG logger + * @param entity can be either zookeeper client or quorum learner + * + * @return saslclient object + * @throws SaslException + */ + public static SaslClient createSaslClient( + final Subject subject, + final String servicePrincipal, + final String protocol, + final String serverName, + final Logger LOG, + final String entity) throws SaslException { + SaslClient saslClient; + // Use subject.getPrincipals().isEmpty() as an indication of which SASL + // mechanism to use: if empty, use DIGEST-MD5; otherwise, use GSSAPI. + if (subject.getPrincipals().isEmpty()) { + // no principals: must not be GSSAPI: use DIGEST-MD5 mechanism + // instead. + LOG.info("{} will use DIGEST-MD5 as SASL mechanism.", entity); + String[] mechs = {"DIGEST-MD5"}; + String username = (String) (subject.getPublicCredentials().toArray()[0]); + String password = (String) (subject.getPrivateCredentials().toArray()[0]); + // 'domain' parameter is hard-wired between the server and client + saslClient = Sasl.createSaslClient(mechs, username, protocol, serverName, null, new SaslClientCallbackHandler(password, entity)); + return saslClient; + } else { // GSSAPI. + final Object[] principals = subject.getPrincipals().toArray(); + // determine client principal from subject. + final Principal clientPrincipal = (Principal) principals[0]; + boolean usingNativeJgss = Boolean.getBoolean("sun.security.jgss.native"); + if (usingNativeJgss) { + // http://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html + // """ + // In addition, when performing operations as a particular + // Subject, e.g. Subject.doAs(...) or + // Subject.doAsPrivileged(...), + // the to-be-used GSSCredential should be added to Subject's + // private credential set. Otherwise, the GSS operations will + // fail since no credential is found. + // """ + try { + GSSManager manager = GSSManager.getInstance(); + Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2"); + GSSCredential cred = manager.createCredential(null, GSSContext.DEFAULT_LIFETIME, krb5Mechanism, GSSCredential.INITIATE_ONLY); + subject.getPrivateCredentials().add(cred); + LOG.debug("Added private credential to {} principal name: '{}'", entity, clientPrincipal); + } catch (GSSException ex) { + LOG.warn("Cannot add private credential to subject; authentication at the server may fail", ex); + } + } + final KerberosName clientKerberosName = new KerberosName(clientPrincipal.getName()); + // assume that server and client are in the same realm (by default; + // unless the system property + // "zookeeper.server.realm" is set). + String serverRealm = System.getProperty("zookeeper.server.realm", clientKerberosName.getRealm()); + String modifiedServerPrincipal = servicePrincipal; + // If service principal does not contain realm, then add it + if (!modifiedServerPrincipal.contains("@")) { + modifiedServerPrincipal = modifiedServerPrincipal + "@" + serverRealm; + } + KerberosName serviceKerberosName = new KerberosName(modifiedServerPrincipal); + final String serviceName = serviceKerberosName.getServiceName(); + final String serviceHostname = serviceKerberosName.getHostName(); + final String clientPrincipalName = clientKerberosName.toString(); + try { + saslClient = Subject.doAs(subject, new PrivilegedExceptionAction() { + public SaslClient run() throws SaslException { + LOG.info("{} will use GSSAPI as SASL mechanism.", entity); + String[] mechs = {"GSSAPI"}; + LOG.debug( + "creating sasl client: {}={};service={};serviceHostname={}", + entity, + clientPrincipalName, + serviceName, + serviceHostname); + SaslClient saslClient = Sasl.createSaslClient( + mechs, + clientPrincipalName, + serviceName, + serviceHostname, + null, + new SaslClientCallbackHandler(null, entity)); + return saslClient; + } + }); + return saslClient; + } catch (Exception e) { + LOG.error("Exception while trying to create SASL client", e); + return null; + } + } + } + + /** + * Create an instance of a SaslServer. It will return null if there is an exception. + * + * @param subject subject + * @param protocol protocol + * @param serverName server name + * @param callbackHandler login callback handler + * @param LOG logger + * @return sasl server object + */ + public static SaslServer createSaslServer( + final Subject subject, + final String protocol, + final String serverName, + final CallbackHandler callbackHandler, + final Logger LOG) { + if (subject != null) { + // server is using a JAAS-authenticated subject: determine service + // principal name and hostname from zk server's subject. + if (subject.getPrincipals().size() > 0) { + try { + final Object[] principals = subject.getPrincipals().toArray(); + final Principal servicePrincipal = (Principal) principals[0]; + + // e.g. servicePrincipalNameAndHostname := + // "zookeeper/myhost.foo.com@FOO.COM" + final String servicePrincipalNameAndHostname = servicePrincipal.getName(); + + int indexOf = servicePrincipalNameAndHostname.indexOf("/"); + + // e.g. servicePrincipalName := "zookeeper" + final String servicePrincipalName = servicePrincipalNameAndHostname.substring(0, indexOf); + + // e.g. serviceHostnameAndKerbDomain := + // "myhost.foo.com@FOO.COM" + final String serviceHostnameAndKerbDomain = servicePrincipalNameAndHostname.substring(indexOf + 1); + + indexOf = serviceHostnameAndKerbDomain.indexOf("@"); + // e.g. serviceHostname := "myhost.foo.com" + final String serviceHostname = serviceHostnameAndKerbDomain.substring(0, indexOf); + + // TODO: should depend on zoo.cfg specified mechs, but if + // subject is non-null, it can be assumed to be GSSAPI. + final String mech = "GSSAPI"; + + LOG.debug("serviceHostname is '{}'", serviceHostname); + LOG.debug("servicePrincipalName is '{}'", servicePrincipalName); + LOG.debug("SASL mechanism(mech) is '{}'", mech); + + boolean usingNativeJgss = Boolean.getBoolean("sun.security.jgss.native"); + if (usingNativeJgss) { + // http://docs.oracle.com/javase/6/docs/technotes/guides/security/jgss/jgss-features.html + // """ + // In addition, when performing operations as a + // particular + // Subject, e.g. Subject.doAs(...) or + // Subject.doAsPrivileged(...), the to-be-used + // GSSCredential should be added to Subject's + // private credential set. Otherwise, the GSS operations + // will fail since no credential is found. + // """ + try { + GSSManager manager = GSSManager.getInstance(); + Oid krb5Mechanism = new Oid("1.2.840.113554.1.2.2"); + GSSName gssName = manager.createName( + servicePrincipalName + "@" + serviceHostname, + GSSName.NT_HOSTBASED_SERVICE); + GSSCredential cred = manager.createCredential(gssName, GSSContext.DEFAULT_LIFETIME, krb5Mechanism, GSSCredential.ACCEPT_ONLY); + subject.getPrivateCredentials().add(cred); + LOG.debug( + "Added private credential to service principal name: '{}', GSSCredential name: {}", + servicePrincipalName, + cred.getName()); + } catch (GSSException ex) { + LOG.warn("Cannot add private credential to subject; clients authentication may fail", ex); + } + } + try { + return Subject.doAs(subject, new PrivilegedExceptionAction() { + public SaslServer run() { + try { + SaslServer saslServer; + saslServer = Sasl.createSaslServer(mech, servicePrincipalName, serviceHostname, null, callbackHandler); + return saslServer; + } catch (SaslException e) { + LOG.error("Zookeeper Server failed to create a SaslServer to interact with a client during session initiation", e); + return null; + } + } + }); + } catch (PrivilegedActionException e) { + // TODO: exit server at this point(?) + LOG.error("Zookeeper Quorum member experienced a PrivilegedActionException exception while creating a SaslServer using a JAAS principal context", e); + } + } catch (IndexOutOfBoundsException e) { + LOG.error("server principal name/hostname determination error", e); + } + } else { + // JAAS non-GSSAPI authentication: assuming and supporting only + // DIGEST-MD5 mechanism for now. + // TODO: use 'authMech=' value in zoo.cfg. + try { + SaslServer saslServer = Sasl.createSaslServer("DIGEST-MD5", protocol, serverName, null, callbackHandler); + return saslServer; + } catch (SaslException e) { + LOG.error("Zookeeper Quorum member failed to create a SaslServer to interact with a client during session initiation", e); + } + } + } + return null; + } + + /** + * Convert Kerberos principal name pattern to valid Kerberos principal name. + * If the principal name contains hostname pattern "_HOST" then it replaces + * with the given hostname, which should be fully-qualified domain name. + * + * @param principalConfig + * the Kerberos principal name conf value to convert + * @param hostname + * the fully-qualified domain name used for substitution + * @return converted Kerberos principal name + */ + public static String getServerPrincipal(String principalConfig, String hostname) { + String[] components = getComponents(principalConfig); + if (components == null || components.length != 2 || !components[1].equals(QUORUM_HOSTNAME_PATTERN)) { + return principalConfig; + } else { + return replacePattern(components, hostname); + } + } + + private static String[] getComponents(String principalConfig) { + if (principalConfig == null) { + return null; + } + return principalConfig.split("[/]"); + } + + private static String replacePattern(String[] components, String hostname) { + return components[0] + "/" + hostname.toLowerCase(); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/util/ServiceUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/util/ServiceUtils.java new file mode 100644 index 0000000..68a25eb --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/util/ServiceUtils.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.util.Objects; +import java.util.function.Consumer; +import org.apache.zookeeper.server.ExitCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utilities for service management. + */ +public abstract class ServiceUtils { + + private static final Logger LOG = LoggerFactory.getLogger(ServiceUtils.class); + + private ServiceUtils() { + } + + /** + * Default strategy for shutting down the JVM. + */ + @SuppressFBWarnings("DM_EXIT") + public static final Consumer SYSTEM_EXIT = (code) -> { + LOG.error("Exiting JVM with code {}", code); + System.exit(code); + }; + + /** + * No-op strategy, useful for tests. + */ + public static final Consumer LOG_ONLY = (code) -> { + LOG.error("Fatal error, JVM should exit with code {}. " + + "Actually System.exit is disabled", code); + }; + + private static Consumer systemExitProcedure = SYSTEM_EXIT; + + /** + * Override system callback. Useful for preventing the JVM to exit in tests + * or in applications that are running an in-process ZooKeeper server. + * + * @param systemExitProcedure + */ + public static void setSystemExitProcedure(Consumer systemExitProcedure) { + Objects.requireNonNull(systemExitProcedure); + ServiceUtils.systemExitProcedure = systemExitProcedure; + } + + /** + * Force shutdown of the JVM using System.exit. + * + * @param code the exit code + * @see ExitCode + */ + public static void requestSystemExit(int code) { + systemExitProcedure.accept(code); + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java b/zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java new file mode 100644 index 0000000..9094a6a --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/version/util/VerGen.java @@ -0,0 +1,205 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.version.util; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.zookeeper.server.ExitCode; + +@SuppressFBWarnings("DM_EXIT") +public class VerGen { + + private static final String PACKAGE_NAME = "org.apache.zookeeper.version"; + private static final String VERSION_CLASS_NAME = "VersionInfoMain"; + private static final String VERSION_INTERFACE_NAME = "Info"; + + static void printUsage() { + System.out.print("Usage:\tjava -cp org.apache.zookeeper." + + "version.util.VerGen maj.min.micro[-qualifier] [rev] buildDate outputDirectory"); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + + public static void generateFile(File outputDir, Version version, String rev, String buildDate) { + String path = PACKAGE_NAME.replaceAll("\\.", "/"); + File pkgdir = new File(outputDir, path); + if (!pkgdir.exists()) { + // create the pkg directory + boolean ret = pkgdir.mkdirs(); + if (!ret) { + System.out.println("Cannnot create directory: " + path); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + } else if (!pkgdir.isDirectory()) { + // not a directory + System.out.println(path + " is not a directory."); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + + try (FileWriter w = new FileWriter(new File(pkgdir, VERSION_INTERFACE_NAME + ".java"))) { + w.write("// Do not edit!\n// File generated by org.apache.zookeeper" + ".version.util.VerGen.\n"); + w.write("/**\n"); + w.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + w.write("* or more contributor license agreements. See the NOTICE file\n"); + w.write("* distributed with this work for additional information\n"); + w.write("* regarding copyright ownership. The ASF licenses this file\n"); + w.write("* to you under the Apache License, Version 2.0 (the\n"); + w.write("* \"License\"); you may not use this file except in compliance\n"); + w.write("* with the License. You may obtain a copy of the License at\n"); + w.write("*\n"); + w.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + w.write("*\n"); + w.write("* Unless required by applicable law or agreed to in writing, software\n"); + w.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + w.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + w.write("* See the License for the specific language governing permissions and\n"); + w.write("* limitations under the License.\n"); + w.write("*/\n"); + w.write("\n"); + w.write("package " + PACKAGE_NAME + ";\n\n"); + w.write("public interface " + VERSION_INTERFACE_NAME + " {\n"); + w.write(" int MAJOR=" + version.maj + ";\n"); + w.write(" int MINOR=" + version.min + ";\n"); + w.write(" int MICRO=" + version.micro + ";\n"); + w.write(" String QUALIFIER=" + (version.qualifier == null ? "\"\"" : "\"" + version.qualifier + "\"") + ";\n"); + if (rev.equals("-1")) { + System.out.println("Unknown REVISION number, using " + rev); + } + w.write(" int REVISION=-1; //@deprecated, please use REVISION_HASH\n"); + w.write(" String REVISION_HASH=\"" + rev + "\";\n"); + w.write(" String BUILD_DATE=\"" + buildDate + "\";\n"); + w.write("}\n"); + } catch (IOException e) { + System.out.println("Unable to generate version.Info file: " + e.getMessage()); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + + // Generate a main class to display version data + // that can be exec'd in zkServer.sh + try (FileWriter w = new FileWriter(new File(pkgdir, VERSION_CLASS_NAME + ".java"))) { + w.write("// Do not edit!\n// File generated by org.apache.zookeeper" + ".version.util.VerGen.\n"); + w.write("/**\n"); + w.write("* Licensed to the Apache Software Foundation (ASF) under one\n"); + w.write("* or more contributor license agreements. See the NOTICE file\n"); + w.write("* distributed with this work for additional information\n"); + w.write("* regarding copyright ownership. The ASF licenses this file\n"); + w.write("* to you under the Apache License, Version 2.0 (the\n"); + w.write("* \"License\"); you may not use this file except in compliance\n"); + w.write("* with the License. You may obtain a copy of the License at\n"); + w.write("*\n"); + w.write("* http://www.apache.org/licenses/LICENSE-2.0\n"); + w.write("*\n"); + w.write("* Unless required by applicable law or agreed to in writing, software\n"); + w.write("* distributed under the License is distributed on an \"AS IS\" BASIS,\n"); + w.write("* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"); + w.write("* See the License for the specific language governing permissions and\n"); + w.write("* limitations under the License.\n"); + w.write("*/\n"); + w.write("\n"); + w.write("package " + PACKAGE_NAME + ";\n\n"); + w.write("public class " + VERSION_CLASS_NAME + " implements " + PACKAGE_NAME + ".Info {\n"); + w.write(" public static void main(String[] args) {\n"); + w.write(" final String VER_STRING = MAJOR + \".\" + MINOR + \".\" + MICRO +"); + w.write(" (QUALIFIER == null || QUALIFIER.isEmpty() ? \"\" : \"-\" + QUALIFIER) + \" \" +"); + w.write(" BUILD_DATE;" + "\n"); + w.write(" System.out.println(\"Apache ZooKeeper, version \" + VER_STRING);\n"); + w.write(" }\n"); + w.write("}\n"); + } catch (IOException e) { + System.out.println("Unable to generate version.VersionInfoMain file: " + e.getMessage()); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + } + + public static class Version { + + public int maj; + public int min; + public int micro; + public String qualifier; + + } + + public static Version parseVersionString(String input) { + Version result = new Version(); + + Pattern p = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)((\\.\\d+)*)(-(.+))?$"); + Matcher m = p.matcher(input); + + if (!m.matches()) { + return null; + } + result.maj = Integer.parseInt(m.group(1)); + result.min = Integer.parseInt(m.group(2)); + result.micro = Integer.parseInt(m.group(3)); + if (m.groupCount() == 7) { + result.qualifier = m.group(7); + } else { + result.qualifier = null; + } + return result; + } + + /** + * Emits a org.apache.zookeeper.version.Info interface file with version and + * revision information constants set to the values passed in as command + * line parameters. The file is created in the current directory.
      + * Usage: java org.apache.zookeeper.version.util.VerGen maj.min.micro[-qualifier] + * rev buildDate + * + * @param args + *
        + *
      • maj - major version number + *
      • min - minor version number + *
      • micro - minor minor version number + *
      • qualifier - optional qualifier (dash followed by qualifier text) + *
      • rev - current Git revision number + *
      • buildDate - date the build + *
      + */ + public static void main(String[] args) { + if (args.length != 3 && args.length != 4) { + printUsage(); + } + if (args.length == 3) { + args = new String[]{args[0], null, args[1], args[2]}; + } + try { + Version version = parseVersionString(args[0]); + if (version == null) { + System.err.println("Invalid version number format, must be \"x.y.z(-.*)?\""); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + String rev = args[1]; + if (rev == null || rev.trim().isEmpty()) { + rev = "-1"; + } else { + rev = rev.trim(); + } + generateFile(new File(args[3]), version, rev, args[2]); + } catch (NumberFormatException e) { + System.err.println("All version-related parameters must be valid integers!"); + throw e; + } + } + +} diff --git a/zookeeper-server/src/main/resources/LICENSE.txt b/zookeeper-server/src/main/resources/LICENSE.txt new file mode 100644 index 0000000..b1264c0 --- /dev/null +++ b/zookeeper-server/src/main/resources/LICENSE.txt @@ -0,0 +1,224 @@ + + 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. + + +This distribution bundles javacc, which is available under the +3-clause BSD License. For details, see a copy of the license in +lib/javacc.LICENSE.txt + +This distribution bundles jline 2.11, which is available under the +2-clause BSD License. For details, see a copy of the license in +lib/jline-2.11.LICENSE.txt + +This distribution bundles SLF4J 1.7.5, which is available under the MIT +License. For details, see a copy of the license in +lib/slf4j-1.7.5.LICENSE.txt + +This distribution bundles json-simple v1.1.1, which is available under the +Apache Software License, Version 2.0. For details, see a copy of the license in +lib/json-simple-1.1.1.LICENSE.txt + +This distribution bundles a modified version of 'JZLib' as part of +Netty-3.7.0, which is available under the 3-clause BSD licence. For +details, see a copy of the licence in META-INF/license/LICENSE-jzlib.txt +as part of the Netty jar in lib/netty-3.7.0.Final.jar. diff --git a/zookeeper-server/src/main/resources/NOTICE.txt b/zookeeper-server/src/main/resources/NOTICE.txt new file mode 100644 index 0000000..1b0fb95 --- /dev/null +++ b/zookeeper-server/src/main/resources/NOTICE.txt @@ -0,0 +1,139 @@ +Apache ZooKeeper +Copyright 2009-2021 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +This product includes software components originally +developed for Airlift (https://github.com/airlift/airlift), +licensed under the Apache 2.0 license. The licensing terms +for Airlift code can be found at: +https://github.com/airlift/airlift/blob/master/LICENSE + +This product includes software developed by +The Netty Project (http://netty.io/) +Copyright 2011 The Netty Project + +The Netty NOTICE file contains the following items: +This product contains the extensions to Java Collections Framework which has +been derived from the works by JSR-166 EG, Doug Lea, and Jason T. Greene: + + * LICENSE: + * license/LICENSE.jsr166y.txt (Public Domain) + * HOMEPAGE: + * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/ + * http://viewvc.jboss.org/cgi-bin/viewvc.cgi/jbosscache/experimental/jsr166/ + +This product contains a modified version of Robert Harder's Public Domain +Base64 Encoder and Decoder, which can be obtained at: + + * LICENSE: + * license/LICENSE.base64.txt (Public Domain) + * HOMEPAGE: + * http://iharder.sourceforge.net/current/java/base64/ + +This product contains a modified version of 'JZlib', a re-implementation of +zlib in pure Java, which can be obtained at: + + * LICENSE: + * license/LICENSE.jzlib.txt (BSD Style License) + * HOMEPAGE: + * http://www.jcraft.com/jzlib/ + +This product contains a modified version of 'Webbit', a Java event based +WebSocket and HTTP server: + + * LICENSE: + * license/LICENSE.webbit.txt (BSD License) + * HOMEPAGE: + * https://github.com/joewalnes/webbit + +This product optionally depends on 'Protocol Buffers', Google's data +interchange format, which can be obtained at: + + * LICENSE: + * license/LICENSE.protobuf.txt (New BSD License) + * HOMEPAGE: + * http://code.google.com/p/protobuf/ + +This product optionally depends on 'Bouncy Castle Crypto APIs' to generate +a temporary self-signed X.509 certificate when the JVM does not provide the +equivalent functionality. It can be obtained at: + + * LICENSE: + * license/LICENSE.bouncycastle.txt (MIT License) + * HOMEPAGE: + * http://www.bouncycastle.org/ + +This product optionally depends on 'SLF4J', a simple logging facade for Java, +which can be obtained at: + + * LICENSE: + * license/LICENSE.slf4j.txt (MIT License) + * HOMEPAGE: + * http://www.slf4j.org/ + +This product optionally depends on 'Apache Commons Logging', a logging +framework, which can be obtained at: + + * LICENSE: + * license/LICENSE.commons-logging.txt (Apache License 2.0) + * HOMEPAGE: + * http://commons.apache.org/logging/ + +This product optionally depends on 'Apache Log4J', a logging framework, +which can be obtained at: + + * LICENSE: + * license/LICENSE.log4j.txt (Apache License 2.0) + * HOMEPAGE: + * http://logging.apache.org/log4j/ + +This product optionally depends on 'JBoss Logging', a logging framework, +which can be obtained at: + + * LICENSE: + * license/LICENSE.jboss-logging.txt (GNU LGPL 2.1) + * HOMEPAGE: + * http://anonsvn.jboss.org/repos/common/common-logging-spi/ + +This product optionally depends on 'Apache Felix', an open source OSGi +framework implementation, which can be obtained at: + + * LICENSE: + * license/LICENSE.felix.txt (Apache License 2.0) + * HOMEPAGE: + * http://felix.apache.org/ + +The bundled library Metrics Core NOTICE file reports the following items + +Metrics +Copyright 2010-2013 Coda Hale and Yammer, Inc. + +This product includes software developed by Coda Hale and Yammer, Inc. + +This product includes code derived from the JSR-166 project (ThreadLocalRandom, Striped64, +LongAdder), which was released with the following comments: + + Written by Doug Lea with assistance from members of JCP JSR-166 + Expert Group and released to the public domain, as explained at + http://creativecommons.org/publicdomain/zero/1.0/ + +The Nappy Java NOTICE file reports the following items: + +This product includes software developed by Google + Snappy: http://code.google.com/p/snappy/ (New BSD License) + +This product includes software developed by Apache + PureJavaCrc32C from apache-hadoop-common http://hadoop.apache.org/ + (Apache 2.0 license) + +This library containd statically linked libstdc++. This inclusion is allowed by +"GCC RUntime Library Exception" +http://gcc.gnu.org/onlinedocs/libstdc++/manual/license.html + +== Contributors == + * Tatu Saloranta + * Providing benchmark suite + * Alec Wysoker + * Performance and memory usage improvement diff --git a/zookeeper-server/src/main/resources/lastRevision.bat b/zookeeper-server/src/main/resources/lastRevision.bat new file mode 100644 index 0000000..7814f7f --- /dev/null +++ b/zookeeper-server/src/main/resources/lastRevision.bat @@ -0,0 +1,22 @@ +echo off +rem Licensed to the Apache Software Foundation (ASF) under one +rem or more contributor license agreements. See the NOTICE file +rem distributed with this work for additional information +rem regarding copyright ownership. The ASF licenses this file +rem to you under the Apache License, Version 2.0 (the +rem "License"); you may not use this file except in compliance +rem with the License. You may obtain a copy of the License at +rem +rem http://www.apache.org/licenses/LICENSE-2.0 +rem +rem Unless required by applicable law or agreed to in writing, software +rem distributed under the License is distributed on an "AS IS" BASIS, +rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +rem See the License for the specific language governing permissions and +rem limitations under the License. + +rem Find the current revision, store it in a file, for DOS + +for /f "delims=" %%i in ('git rev-parse HEAD') do set rev=%%i + echo lastRevision=%rev% > %1 +) diff --git a/zookeeper-server/src/main/resources/lastRevision.sh b/zookeeper-server/src/main/resources/lastRevision.sh new file mode 100644 index 0000000..ec16784 --- /dev/null +++ b/zookeeper-server/src/main/resources/lastRevision.sh @@ -0,0 +1,22 @@ +#!/bin/sh +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Find the current revision, store it in a file +FILE=$1 +LASTREV=$(git rev-parse HEAD) + +echo "lastRevision=${LASTREV}" > "$FILE" diff --git a/zookeeper-server/src/main/resources/lib/jetty-http-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-http-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-http-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jetty-io-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-io-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-io-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jetty-security-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-security-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-security-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jetty-server-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-server-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-server-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jetty-servlet-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-servlet-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-servlet-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jetty-util-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-util-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-util-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jetty-util-ajax-9.4.39.v20210325.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jetty-util-ajax-9.4.39.v20210325.LICENSE.txt new file mode 100644 index 0000000..46f4f25 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jetty-util-ajax-9.4.39.v20210325.LICENSE.txt @@ -0,0 +1,414 @@ +This program and the accompanying materials are made available under the +terms of the Eclipse Public License 2.0 which is available at +http://www.eclipse.org/legal/epl-2.0, or the Apache Software License +2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0. + + + +Eclipse Public License - v 1.0 + +THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC +LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM +CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. + +1. DEFINITIONS + +"Contribution" means: + +a) in the case of the initial Contributor, the initial code and documentation + distributed under this Agreement, and +b) in the case of each subsequent Contributor: + i) changes to the Program, and + ii) additions to the Program; + + where such changes and/or additions to the Program originate from and are + distributed by that particular Contributor. A Contribution 'originates' + from a Contributor if it was added to the Program by such Contributor + itself or anyone acting on such Contributor's behalf. Contributions do not + include additions to the Program which: (i) are separate modules of + software distributed in conjunction with the Program under their own + license agreement, and (ii) are not derivative works of the Program. + +"Contributor" means any person or entity that distributes the Program. + +"Licensed Patents" mean patent claims licensable by a Contributor which are +necessarily infringed by the use or sale of its Contribution alone or when +combined with the Program. + +"Program" means the Contributions distributed in accordance with this +Agreement. + +"Recipient" means anyone who receives the Program under this Agreement, +including all Contributors. + +2. GRANT OF RIGHTS + a) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free copyright license to + reproduce, prepare derivative works of, publicly display, publicly + perform, distribute and sublicense the Contribution of such Contributor, + if any, and such derivative works, in source code and object code form. + b) Subject to the terms of this Agreement, each Contributor hereby grants + Recipient a non-exclusive, worldwide, royalty-free patent license under + Licensed Patents to make, use, sell, offer to sell, import and otherwise + transfer the Contribution of such Contributor, if any, in source code and + object code form. This patent license shall apply to the combination of + the Contribution and the Program if, at the time the Contribution is + added by the Contributor, such addition of the Contribution causes such + combination to be covered by the Licensed Patents. The patent license + shall not apply to any other combinations which include the Contribution. + No hardware per se is licensed hereunder. + c) Recipient understands that although each Contributor grants the licenses + to its Contributions set forth herein, no assurances are provided by any + Contributor that the Program does not infringe the patent or other + intellectual property rights of any other entity. Each Contributor + disclaims any liability to Recipient for claims brought by any other + entity based on infringement of intellectual property rights or + otherwise. As a condition to exercising the rights and licenses granted + hereunder, each Recipient hereby assumes sole responsibility to secure + any other intellectual property rights needed, if any. For example, if a + third party patent license is required to allow Recipient to distribute + the Program, it is Recipient's responsibility to acquire that license + before distributing the Program. + d) Each Contributor represents that to its knowledge it has sufficient + copyright rights in its Contribution, if any, to grant the copyright + license set forth in this Agreement. + +3. REQUIREMENTS + +A Contributor may choose to distribute the Program in object code form under +its own license agreement, provided that: + + a) it complies with the terms and conditions of this Agreement; and + b) its license agreement: + i) effectively disclaims on behalf of all Contributors all warranties + and conditions, express and implied, including warranties or + conditions of title and non-infringement, and implied warranties or + conditions of merchantability and fitness for a particular purpose; + ii) effectively excludes on behalf of all Contributors all liability for + damages, including direct, indirect, special, incidental and + consequential damages, such as lost profits; + iii) states that any provisions which differ from this Agreement are + offered by that Contributor alone and not by any other party; and + iv) states that source code for the Program is available from such + Contributor, and informs licensees how to obtain it in a reasonable + manner on or through a medium customarily used for software exchange. + +When the Program is made available in source code form: + + a) it must be made available under this Agreement; and + b) a copy of this Agreement must be included with each copy of the Program. + Contributors may not remove or alter any copyright notices contained + within the Program. + +Each Contributor must identify itself as the originator of its Contribution, +if +any, in a manner that reasonably allows subsequent Recipients to identify the +originator of the Contribution. + +4. COMMERCIAL DISTRIBUTION + +Commercial distributors of software may accept certain responsibilities with +respect to end users, business partners and the like. While this license is +intended to facilitate the commercial use of the Program, the Contributor who +includes the Program in a commercial product offering should do so in a manner +which does not create potential liability for other Contributors. Therefore, +if a Contributor includes the Program in a commercial product offering, such +Contributor ("Commercial Contributor") hereby agrees to defend and indemnify +every other Contributor ("Indemnified Contributor") against any losses, +damages and costs (collectively "Losses") arising from claims, lawsuits and +other legal actions brought by a third party against the Indemnified +Contributor to the extent caused by the acts or omissions of such Commercial +Contributor in connection with its distribution of the Program in a commercial +product offering. The obligations in this section do not apply to any claims +or Losses relating to any actual or alleged intellectual property +infringement. In order to qualify, an Indemnified Contributor must: +a) promptly notify the Commercial Contributor in writing of such claim, and +b) allow the Commercial Contributor to control, and cooperate with the +Commercial Contributor in, the defense and any related settlement +negotiations. The Indemnified Contributor may participate in any such claim at +its own expense. + +For example, a Contributor might include the Program in a commercial product +offering, Product X. That Contributor is then a Commercial Contributor. If +that Commercial Contributor then makes performance claims, or offers +warranties related to Product X, those performance claims and warranties are +such Commercial Contributor's responsibility alone. Under this section, the +Commercial Contributor would have to defend claims against the other +Contributors related to those performance claims and warranties, and if a +court requires any other Contributor to pay any damages as a result, the +Commercial Contributor must pay those damages. + +5. NO WARRANTY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED 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. Each +Recipient is solely responsible for determining the appropriateness of using +and distributing the Program and assumes all risks associated with its +exercise of rights under this Agreement , including but not limited to the +risks and costs of program errors, compliance with applicable laws, damage to +or loss of data, programs or equipment, and unavailability or interruption of +operations. + +6. DISCLAIMER OF LIABILITY + +EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY +CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION +LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE +EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGES. + +7. GENERAL + +If any provision of this Agreement is invalid or unenforceable under +applicable law, it shall not affect the validity or enforceability of the +remainder of the terms of this Agreement, and without further action by the +parties hereto, such provision shall be reformed to the minimum extent +necessary to make such provision valid and enforceable. + +If Recipient institutes patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Program itself +(excluding combinations of the Program with other software or hardware) +infringes such Recipient's patent(s), then such Recipient's rights granted +under Section 2(b) shall terminate as of the date such litigation is filed. + +All Recipient's rights under this Agreement shall terminate if it fails to +comply with any of the material terms or conditions of this Agreement and does +not cure such failure in a reasonable period of time after becoming aware of +such noncompliance. If all Recipient's rights under this Agreement terminate, +Recipient agrees to cease use and distribution of the Program as soon as +reasonably practicable. However, Recipient's obligations under this Agreement +and any licenses granted by Recipient relating to the Program shall continue +and survive. + +Everyone is permitted to copy and distribute copies of this Agreement, but in +order to avoid inconsistency the Agreement is copyrighted and may only be +modified in the following manner. The Agreement Steward reserves the right to +publish new versions (including revisions) of this Agreement from time to +time. No one other than the Agreement Steward has the right to modify this +Agreement. The Eclipse Foundation is the initial Agreement Steward. The +Eclipse Foundation may assign the responsibility to serve as the Agreement +Steward to a suitable separate entity. Each new version of the Agreement will +be given a distinguishing version number. The Program (including +Contributions) may always be distributed subject to the version of the +Agreement under which it was received. In addition, after a new version of the +Agreement is published, Contributor may elect to distribute the Program +(including its Contributions) under the new version. Except as expressly +stated in Sections 2(a) and 2(b) above, Recipient receives no rights or +licenses to the intellectual property of any Contributor under this Agreement, +whether expressly, by implication, estoppel or otherwise. All rights in the +Program not expressly granted under this Agreement are reserved. + +This Agreement is governed by the laws of the State of New York and the +intellectual property laws of the United States of America. No party to this +Agreement will bring a legal action under this Agreement more than one year +after the cause of action arose. Each party waives its rights to a jury trial in +any resulting litigation. + + + + 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/zookeeper-server/src/main/resources/lib/jline-2.14.6.LICENSE.txt b/zookeeper-server/src/main/resources/lib/jline-2.14.6.LICENSE.txt new file mode 100644 index 0000000..4ac9522 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/jline-2.14.6.LICENSE.txt @@ -0,0 +1,35 @@ +Copyright (c) 2002-2012, the original author or authors. +All rights reserved. + +http://www.opensource.org/licenses/bsd-license.php + +Redistribution and use in source and binary forms, with or +without modification, are permitted provided that the following +conditions are met: + +Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with +the distribution. + +Neither the name of JLine nor the names of its contributors +may be used to endorse or promote products derived from this +software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/zookeeper-server/src/main/resources/lib/json-simple-1.1.1.LICENSE.txt b/zookeeper-server/src/main/resources/lib/json-simple-1.1.1.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/json-simple-1.1.1.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/log4j-1.2.17.LICENSE.txt b/zookeeper-server/src/main/resources/lib/log4j-1.2.17.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/log4j-1.2.17.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/metrics-core-3.2.5.jar_LICENSE.txt b/zookeeper-server/src/main/resources/lib/metrics-core-3.2.5.jar_LICENSE.txt new file mode 100644 index 0000000..e4ba404 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/metrics-core-3.2.5.jar_LICENSE.txt @@ -0,0 +1,202 @@ + + 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 2010-2012 Coda Hale and Yammer, Inc. + + 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/zookeeper-server/src/main/resources/lib/netty-buffer-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-buffer-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-buffer-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-codec-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-codec-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-codec-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-common-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-common-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-common-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-handler-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-handler-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-handler-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-resolver-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-resolver-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-resolver-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-transport-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-transport-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-transport-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-transport-native-epoll-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-transport-native-epoll-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-transport-native-epoll-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/netty-transport-native-unix-common-4.1.63.Final.LICENSE.txt b/zookeeper-server/src/main/resources/lib/netty-transport-native-unix-common-4.1.63.Final.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/netty-transport-native-unix-common-4.1.63.Final.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/simpleclient-0.6.0.LICENSE.txt b/zookeeper-server/src/main/resources/lib/simpleclient-0.6.0.LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/simpleclient-0.6.0.LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/simpleclient_common-0.6.0_LICENSE.txt b/zookeeper-server/src/main/resources/lib/simpleclient_common-0.6.0_LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/simpleclient_common-0.6.0_LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/simpleclient_hotspot-0.6.0_LICENSE.txt b/zookeeper-server/src/main/resources/lib/simpleclient_hotspot-0.6.0_LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/simpleclient_hotspot-0.6.0_LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/simpleclient_servlet-0.6.0_LICENSE.txt b/zookeeper-server/src/main/resources/lib/simpleclient_servlet-0.6.0_LICENSE.txt new file mode 100644 index 0000000..6279e52 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/simpleclient_servlet-0.6.0_LICENSE.txt @@ -0,0 +1,202 @@ + + 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 1999-2005 The Apache Software Foundation + + 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/zookeeper-server/src/main/resources/lib/slf4j-1.7.25.LICENSE.txt b/zookeeper-server/src/main/resources/lib/slf4j-1.7.25.LICENSE.txt new file mode 100644 index 0000000..a502dd9 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/slf4j-1.7.25.LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2004-2017 QOS.ch +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/zookeeper-server/src/main/resources/lib/snappy-java-1.1.7.jar_LICENSE.txt b/zookeeper-server/src/main/resources/lib/snappy-java-1.1.7.jar_LICENSE.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/zookeeper-server/src/main/resources/lib/snappy-java-1.1.7.jar_LICENSE.txt @@ -0,0 +1,202 @@ + + 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/zookeeper-server/src/main/resources/overview.html b/zookeeper-server/src/main/resources/overview.html new file mode 100644 index 0000000..db9b16f --- /dev/null +++ b/zookeeper-server/src/main/resources/overview.html @@ -0,0 +1,26 @@ + + + + + + ZooKeeper + + +ZooKeeper is a service for coordinating processes of distributed applications. + + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCanonicalizeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCanonicalizeTest.java new file mode 100644 index 0000000..796cb6b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCanonicalizeTest.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.net.InetSocketAddress; +import org.apache.zookeeper.client.ZKClientConfig; +import org.junit.Test; + +public class ClientCanonicalizeTest extends ZKTestCase { + + @Test + public void testClientCanonicalization() throws IOException, InterruptedException { + SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); + SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); + + when(addr.getHostName()).thenReturn("zookeeper.apache.org"); + when(addr.getAddress()).thenReturn(ia); + when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); + when(ia.getHostAddress()).thenReturn("127.0.0.1"); + + ZKClientConfig conf = new ZKClientConfig(); + String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); + assertEquals("The computed principal does not appear to have been canonicalized", "zookeeper/zk1.apache.org", principal); + } + + @Test + public void testClientNoCanonicalization() throws IOException, InterruptedException { + SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); + SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); + + when(addr.getHostName()).thenReturn("zookeeper.apache.org"); + when(addr.getAddress()).thenReturn(ia); + when(ia.getCanonicalHostName()).thenReturn("zk1.apache.org"); + when(ia.getHostAddress()).thenReturn("127.0.0.1"); + + ZKClientConfig conf = new ZKClientConfig(); + conf.setProperty(ZKClientConfig.ZK_SASL_CLIENT_CANONICALIZE_HOSTNAME, "false"); + String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); + assertEquals("The computed principal does appears to have been canonicalized incorrectly", "zookeeper/zookeeper.apache.org", principal); + } + + @Test + public void testClientCanonicalizationToIp() throws IOException, InterruptedException { + SaslServerPrincipal.WrapperInetSocketAddress addr = mock(SaslServerPrincipal.WrapperInetSocketAddress.class); + SaslServerPrincipal.WrapperInetAddress ia = mock(SaslServerPrincipal.WrapperInetAddress.class); + + when(addr.getHostName()).thenReturn("zookeeper.apache.org"); + when(addr.getAddress()).thenReturn(ia); + when(ia.getCanonicalHostName()).thenReturn("127.0.0.1"); + when(ia.getHostAddress()).thenReturn("127.0.0.1"); + + ZKClientConfig conf = new ZKClientConfig(); + String principal = SaslServerPrincipal.getServerPrincipal(addr, conf); + assertEquals("The computed principal does appear to have falled back to the original host name", "zookeeper/zookeeper.apache.org", principal); + } + + @Test + public void testGetServerPrincipalReturnConfiguredPrincipalName() { + ZKClientConfig config = new ZKClientConfig(); + String configuredPrincipal = "zookeeper/zookeeper.apache.org@APACHE.ORG"; + config.setProperty(ZKClientConfig.ZOOKEEPER_SERVER_PRINCIPAL, configuredPrincipal); + + // Testing the case where server principal is configured, therefore InetSocketAddress is passed as null + String serverPrincipal = SaslServerPrincipal.getServerPrincipal((InetSocketAddress) null, config); + assertEquals(configuredPrincipal, serverPrincipal); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketFragilityTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketFragilityTest.java new file mode 100644 index 0000000..07b7d62 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketFragilityTest.java @@ -0,0 +1,362 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.Queue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ClientCnxn.Packet; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Assert; +import org.junit.Test; + +public class ClientCnxnSocketFragilityTest extends QuorumPeerTestBase { + + private static final int SERVER_COUNT = 3; + + private static final int SESSION_TIMEOUT = 40000; + + public static final int CONNECTION_TIMEOUT = 30000; + + private final UnsafeCoordinator unsafeCoordinator = new UnsafeCoordinator(); + + private volatile CustomZooKeeper zk = null; + + private volatile FragileClientCnxnSocketNIO socket = null; + + private volatile CustomClientCnxn cnxn = null; + + private String getCxnString(int[] clientPorts) { + StringBuffer hostPortBuffer = new StringBuffer(); + for (int i = 0; i < clientPorts.length; i++) { + hostPortBuffer.append("127.0.0.1:"); + hostPortBuffer.append(clientPorts[i]); + if (i != (clientPorts.length - 1)) { + hostPortBuffer.append(','); + } + } + return hostPortBuffer.toString(); + } + + private void closeZookeeper(ZooKeeper zk) { + Executors.newSingleThreadExecutor().submit(() -> { + try { + LOG.info("closeZookeeper is fired"); + zk.close(); + } catch (InterruptedException e) { + } + }); + } + + @Test + public void testClientCnxnSocketFragility() throws Exception { + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, + FragileClientCnxnSocketNIO.class.getName()); + System.setProperty(ZKClientConfig.ZOOKEEPER_REQUEST_TIMEOUT, "1000"); + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread[] mt = new MainThread[SERVER_COUNT]; + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // Ensure server started + for (int i = 0; i < SERVER_COUNT; i++) { + Assert.assertTrue("waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + String path = "/testClientCnxnSocketFragility"; + String data = "balabala"; + ClientWatcher watcher = new ClientWatcher(); + zk = new CustomZooKeeper(getCxnString(clientPorts), SESSION_TIMEOUT, watcher); + watcher.watchFor(zk); + + // Let's see some successful operations + zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Assert.assertEquals(new String(zk.getData(path, false, new Stat())), data); + Assert.assertTrue(!watcher.isSessionExpired()); + + // Let's make a broken operation + socket.mute(); + boolean catchKeeperException = false; + try { + zk.getData(path, false, new Stat()); + } catch (KeeperException e) { + catchKeeperException = true; + Assert.assertFalse(e instanceof KeeperException.SessionExpiredException); + } + socket.unmute(); + Assert.assertTrue(catchKeeperException); + Assert.assertTrue(!watcher.isSessionExpired()); + + GetDataRetryForeverBackgroundTask retryForeverGetData = + new GetDataRetryForeverBackgroundTask(zk, path); + retryForeverGetData.startTask(); + // Let's make a broken network + socket.mute(); + + // Let's attempt to close ZooKeeper + cnxn.attemptClose(); + + // Wait some time to expect continuous reconnecting. + // We try to make reconnecting hit the unsafe region. + cnxn.waitUntilHitUnsafeRegion(); + + // close zk with timeout 1000 milli seconds + closeZookeeper(zk); + TimeUnit.MILLISECONDS.sleep(3000); + + // Since we already close zookeeper, we expect that the zk should not be alive. + Assert.assertTrue(!zk.isAlive()); + Assert.assertTrue(!watcher.isSessionExpired()); + + retryForeverGetData.syncCloseTask(); + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + + class GetDataRetryForeverBackgroundTask extends Thread { + private volatile boolean alive; + private final CustomZooKeeper zk; + private final String path; + + GetDataRetryForeverBackgroundTask(CustomZooKeeper zk, String path) { + this.alive = false; + this.zk = zk; + this.path = path; + // marked as daemon to avoid exhausting CPU + setDaemon(true); + } + + void startTask() { + alive = true; + start(); + } + + void syncCloseTask() throws InterruptedException { + alive = false; + join(); + } + + @Override + public void run() { + while (alive) { + try { + zk.getData(path, false, new Stat()); + // sleep for a while to avoid exhausting CPU + TimeUnit.MILLISECONDS.sleep(500); + } catch (Exception e) { + LOG.info("zookeeper getData failed on path {}", path); + } + } + } + } + + public static class FragileClientCnxnSocketNIO extends ClientCnxnSocketNIO { + + private volatile boolean mute; + + public FragileClientCnxnSocketNIO(ZKClientConfig clientConfig) throws IOException { + super(clientConfig); + mute = false; + } + + synchronized void mute() { + if (!mute) { + LOG.info("Fire socket mute"); + mute = true; + } + } + + synchronized void unmute() { + if (mute) { + LOG.info("Fire socket unmute"); + mute = false; + } + } + + @Override + void doTransport(int waitTimeOut, Queue pendingQueue, ClientCnxn cnxn) + throws IOException, InterruptedException { + if (mute) { + throw new IOException("Socket is mute"); + } + super.doTransport(waitTimeOut, pendingQueue, cnxn); + } + + @Override + void connect(InetSocketAddress addr) throws IOException { + if (mute) { + throw new IOException("Socket is mute"); + } + super.connect(addr); + } + } + + class ClientWatcher implements Watcher { + + private ZooKeeper zk; + + private boolean sessionExpired = false; + + void watchFor(ZooKeeper zk) { + this.zk = zk; + } + + @Override + public void process(WatchedEvent event) { + LOG.info("Watcher got {}", event); + if (event.getState() == KeeperState.Expired) { + sessionExpired = true; + } + } + + boolean isSessionExpired() { + return sessionExpired; + } + } + + // Coordinate to construct the risky scenario. + class UnsafeCoordinator { + + private CountDownLatch syncLatch = new CountDownLatch(2); + + void sync(boolean closing) { + LOG.info("Attempt to sync with {}", closing); + if (closing) { + syncLatch.countDown(); + try { + syncLatch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + } + + class CustomClientCnxn extends ClientCnxn { + + private volatile boolean closing = false; + + private volatile boolean hitUnsafeRegion = false; + + public CustomClientCnxn( + String chrootPath, + HostProvider hostProvider, + int sessionTimeout, + ZooKeeper zooKeeper, + ClientWatchManager watcher, + ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + super(chrootPath, hostProvider, sessionTimeout, zooKeeper, watcher, clientCnxnSocket, canBeReadOnly); + } + + void attemptClose() { + closing = true; + } + + void waitUntilHitUnsafeRegion() { + while (!hitUnsafeRegion) { + try { + TimeUnit.MILLISECONDS.sleep(100); + } catch (InterruptedException e) { + } + } + } + + @Override + protected void onConnecting(InetSocketAddress addr) { + if (closing) { + LOG.info("Attempt to connnecting {} {} {}", addr, closing, state); + ///////// Unsafe Region //////// + // Slow down and zoom out the unsafe point to make risk + // The unsafe point is that startConnect happens after sendThread.close + hitUnsafeRegion = true; + unsafeCoordinator.sync(closing); + //////////////////////////////// + } + } + + @Override + public void disconnect() { + Assert.assertTrue(closing); + LOG.info("Attempt to disconnecting client for session: 0x{} {} {}", Long.toHexString(getSessionId()), closing, state); + sendThread.close(); + ///////// Unsafe Region //////// + unsafeCoordinator.sync(closing); + //////////////////////////////// + try { + sendThread.join(); + } catch (InterruptedException ex) { + LOG.warn("Got interrupted while waiting for the sender thread to close", ex); + } + eventThread.queueEventOfDeath(); + if (zooKeeperSaslClient != null) { + zooKeeperSaslClient.shutdown(); + } + } + } + + class CustomZooKeeper extends ZooKeeper { + + public CustomZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException { + super(connectString, sessionTimeout, watcher); + } + + public boolean isAlive() { + return cnxn.getState().isAlive(); + } + + @Override + protected ClientCnxn createConnection( + String chrootPath, + HostProvider hostProvider, + int sessionTimeout, + ZooKeeper zooKeeper, + ClientWatchManager watcher, + ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + Assert.assertTrue(clientCnxnSocket instanceof FragileClientCnxnSocketNIO); + socket = (FragileClientCnxnSocketNIO) clientCnxnSocket; + ClientCnxnSocketFragilityTest.this.cnxn = new CustomClientCnxn(chrootPath, hostProvider, sessionTimeout, zooKeeper, watcher, clientCnxnSocket, canBeReadOnly); + return ClientCnxnSocketFragilityTest.this.cnxn; + } + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java new file mode 100644 index 0000000..310d34f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientCnxnSocketTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.test.TestByteBufAllocator; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ClientCnxnSocketTest { + + @Before + public void setUp() { + ClientCnxnSocketNetty.setTestAllocator(TestByteBufAllocator.getInstance()); + } + + @After + public void tearDown() { + ClientCnxnSocketNetty.clearTestAllocator(); + TestByteBufAllocator.checkForLeaks(); + } + + @Test + public void testWhenInvalidJuteMaxBufferIsConfiguredIOExceptionIsThrown() { + ZKClientConfig clientConfig = new ZKClientConfig(); + String value = "SomeInvalidInt"; + clientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, value); + // verify ClientCnxnSocketNIO creation + try { + new ClientCnxnSocketNIO(clientConfig); + fail("IOException is expected."); + } catch (IOException e) { + assertTrue(e.getMessage().contains(value)); + } + // verify ClientCnxnSocketNetty creation + try { + new ClientCnxnSocketNetty(clientConfig); + fail("IOException is expected."); + } catch (IOException e) { + assertTrue(e.getMessage().contains(value)); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientReconnectTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientReconnectTest.java new file mode 100644 index 0000000..e77283c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientReconnectTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.channels.SocketChannel; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.ZKClientConfig; +import org.junit.Test; + +public class ClientReconnectTest extends ZKTestCase { + + private SocketChannel sc; + private CountDownLatch countDownLatch = new CountDownLatch(3); + + class MockCnxn extends ClientCnxnSocketNIO { + + MockCnxn() throws IOException { + super(new ZKClientConfig()); + } + + @Override + void registerAndConnect(SocketChannel sock, InetSocketAddress addr) throws IOException { + countDownLatch.countDown(); + throw new IOException("failed to register"); + } + + @Override + SocketChannel createSock() { + return sc; + } + + } + + @Test + public void testClientReconnect() throws IOException, InterruptedException { + HostProvider hostProvider = mock(HostProvider.class); + when(hostProvider.size()).thenReturn(1); + InetSocketAddress inaddr = new InetSocketAddress("127.0.0.1", 1111); + when(hostProvider.next(anyLong())).thenReturn(inaddr); + ZooKeeper zk = mock(ZooKeeper.class); + when(zk.getClientConfig()).thenReturn(new ZKClientConfig()); + sc = SocketChannel.open(); + + ClientCnxnSocketNIO nioCnxn = new MockCnxn(); + ClientWatchManager watcher = mock(ClientWatchManager.class); + ClientCnxn clientCnxn = new ClientCnxn("tmp", hostProvider, 5000, zk, watcher, nioCnxn, false); + clientCnxn.start(); + countDownLatch.await(5000, TimeUnit.MILLISECONDS); + assertTrue(countDownLatch.getCount() == 0); + clientCnxn.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ClientRequestTimeoutTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientRequestTimeoutTest.java new file mode 100644 index 0000000..7c3bf51 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ClientRequestTimeoutTest.java @@ -0,0 +1,156 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.Test; + +public class ClientRequestTimeoutTest extends QuorumPeerTestBase { + + private static final int SERVER_COUNT = 3; + private boolean dropPacket = false; + private int dropPacketType = ZooDefs.OpCode.create; + + @Test(timeout = 120000) + public void testClientRequestTimeout() throws Exception { + int requestTimeOut = 15000; + System.setProperty("zookeeper.request.timeout", Integer.toString(requestTimeOut)); + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread[] mt = new MainThread[SERVER_COUNT]; + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // ensure server started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + CountdownWatcher watch1 = new CountdownWatcher(); + CustomZooKeeper zk = new CustomZooKeeper(getCxnString(clientPorts), ClientBase.CONNECTION_TIMEOUT, watch1); + watch1.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + String data = "originalData"; + // lets see one successful operation + zk.create("/clientHang1", data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + + // now make environment for client hang + dropPacket = true; + dropPacketType = ZooDefs.OpCode.create; + + // Test synchronous API + try { + zk.create("/clientHang2", data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("KeeperException is expected."); + } catch (KeeperException exception) { + assertEquals(KeeperException.Code.REQUESTTIMEOUT.intValue(), exception.code().intValue()); + } + + // do cleanup + zk.close(); + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + + /** + * @return connection string in the form of + * 127.0.0.1:port1,127.0.0.1:port2,127.0.0.1:port3 + */ + private String getCxnString(int[] clientPorts) { + StringBuffer hostPortBuffer = new StringBuffer(); + for (int i = 0; i < clientPorts.length; i++) { + hostPortBuffer.append("127.0.0.1:"); + hostPortBuffer.append(clientPorts[i]); + if (i != (clientPorts.length - 1)) { + hostPortBuffer.append(','); + } + } + return hostPortBuffer.toString(); + } + + class CustomClientCnxn extends ClientCnxn { + + public CustomClientCnxn( + String chrootPath, + HostProvider hostProvider, + int sessionTimeout, + ZooKeeper zooKeeper, + ClientWatchManager watcher, + ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + super(chrootPath, hostProvider, sessionTimeout, zooKeeper, watcher, clientCnxnSocket, canBeReadOnly); + } + + @Override + public void finishPacket(Packet p) { + if (dropPacket && p.requestHeader.getType() == dropPacketType) { + // do nothing, just return, it is the same as packet is dropped + // by the network + return; + } + super.finishPacket(p); + } + + } + + class CustomZooKeeper extends ZooKeeper { + + public CustomZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException { + super(connectString, sessionTimeout, watcher); + } + + @Override + protected ClientCnxn createConnection( + String chrootPath, + HostProvider hostProvider, + int sessionTimeout, + ZooKeeper zooKeeper, + ClientWatchManager watcher, + ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + return new CustomClientCnxn(chrootPath, hostProvider, sessionTimeout, zooKeeper, watcher, clientCnxnSocket, canBeReadOnly); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/CustomHostProviderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/CustomHostProviderTest.java new file mode 100644 index 0000000..29ce9ef --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/CustomHostProviderTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.Collection; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class CustomHostProviderTest extends ZKTestCase { + + private AtomicInteger counter = new AtomicInteger(3); + + private class SpecialHostProvider implements HostProvider { + + // ignores its connectstring, and next() always returns localhost:2181 + // it will count down when updateServerList() is called + @Override + public int size() { + return 1; + } + @Override + public InetSocketAddress next(long spinDelay) { + return new InetSocketAddress("127.0.0.1", 2181); + } + @Override + public void onConnected() { + } + @Override + public boolean updateServerList(Collection serverAddresses, InetSocketAddress currentHost) { + counter.decrementAndGet(); + return false; + } + + } + + @Test + public void testZooKeeperWithCustomHostProvider() throws IOException, InterruptedException { + final int CLIENT_PORT = PortAssignment.unique(); + final HostProvider specialHostProvider = new SpecialHostProvider(); + int expectedCounter = 3; + counter.set(expectedCounter); + + ZooKeeper zkDefaults = new ZooKeeper( + "127.0.0.1:" + CLIENT_PORT, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE, + false); + + ZooKeeper zkSpecial = new ZooKeeper( + "127.0.0.1:" + CLIENT_PORT, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE, + false, + specialHostProvider); + + assertTrue(counter.get() == expectedCounter); + zkDefaults.updateServerList("127.0.0.1:" + PortAssignment.unique()); + assertTrue(counter.get() == expectedCounter); + + zkSpecial.updateServerList("127.0.0.1:" + PortAssignment.unique()); + expectedCounter--; + assertTrue(counter.get() == expectedCounter); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/DummyWatcher.java b/zookeeper-server/src/test/java/org/apache/zookeeper/DummyWatcher.java new file mode 100644 index 0000000..ff3bba3 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/DummyWatcher.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +/** + * A dummy implementation of {@link Watcher}. Used in tests. + */ +public class DummyWatcher implements Watcher { + + public static final DummyWatcher INSTANCE = new DummyWatcher(); + + @Override + public void process(WatchedEvent event) { + // no op + } + + private DummyWatcher() { + // singleton + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/GetAllChildrenNumberTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/GetAllChildrenNumberTest.java new file mode 100644 index 0000000..b13ceaf --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/GetAllChildrenNumberTest.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class GetAllChildrenNumberTest extends ClientBase { + + private static final String BASE = "/getAllChildrenNumberTest"; + private static final String BASE_EXT = BASE + "EXT"; + private static final int PERSISTENT_CNT = 2; + private static final int EPHEMERAL_CNT = 3; + + private ZooKeeper zk; + + @Override + public void setUp() throws Exception { + super.setUp(); + + zk = createClient(); + generatePaths(PERSISTENT_CNT, EPHEMERAL_CNT); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + + zk.close(); + } + + @Test + public void testGetAllChildrenNumberSync() throws KeeperException, InterruptedException { + //a bad case + try { + zk.getAllChildrenNumber(null); + fail("the path for getAllChildrenNumber must not be null."); + } catch (IllegalArgumentException e) { + //expected + } + + assertEquals(EPHEMERAL_CNT, zk.getAllChildrenNumber(BASE + "/0")); + assertEquals(0, zk.getAllChildrenNumber(BASE + "/0/ephem0")); + assertEquals(0, zk.getAllChildrenNumber(BASE_EXT)); + assertEquals(PERSISTENT_CNT + PERSISTENT_CNT * EPHEMERAL_CNT, zk.getAllChildrenNumber(BASE)); + // 6(EPHEMERAL) + 2(PERSISTENT) + 3("/zookeeper,/zookeeper/quota,/zookeeper/config") + 1(BASE_EXT) + 1(BASE) = 13 + assertEquals(13, zk.getAllChildrenNumber("/")); + } + + @Test + public void testGetAllChildrenNumberAsync() throws IOException, KeeperException, InterruptedException { + + final CountDownLatch doneProcessing = new CountDownLatch(1); + + zk.getAllChildrenNumber("/", new AsyncCallback.AllChildrenNumberCallback() { + @Override + public void processResult(int rc, String path, Object ctx, int number) { + if (path == null) { + fail((String.format("the path of getAllChildrenNumber was null."))); + } + assertEquals(13, number); + doneProcessing.countDown(); + } + }, null); + long waitForCallbackSecs = 2L; + if (!doneProcessing.await(waitForCallbackSecs, TimeUnit.SECONDS)) { + fail(String.format("getAllChildrenNumber didn't callback within %d seconds", waitForCallbackSecs)); + } + } + + private void generatePaths(int persistantCnt, int ephemeralCnt) throws KeeperException, InterruptedException { + + zk.create(BASE, BASE.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create(BASE_EXT, BASE_EXT.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + for (int p = 0; p < persistantCnt; p++) { + String base = BASE + "/" + p; + zk.create(base, base.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int e = 0; e < ephemeralCnt; e++) { + String ephem = base + "/ephem" + e; + zk.create(ephem, ephem.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/GetEphemeralsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/GetEphemeralsTest.java new file mode 100644 index 0000000..6d4dea1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/GetEphemeralsTest.java @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class GetEphemeralsTest extends ClientBase { + + private static final String BASE = "/base"; + private static final int PERSISTENT_CNT = 2; + private static final int EPHEMERAL_CNT = 2; + private static final String NEWLINE = System.getProperty("line.separator"); + private String[] expected; + private ZooKeeper zk; + + @Override + public void setUp() throws Exception { + super.setUp(); + + zk = createClient(); + expected = generatePaths(PERSISTENT_CNT, EPHEMERAL_CNT); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + + zk.close(); + } + + @Test + public void testGetEphemeralsSync() throws KeeperException, InterruptedException { + List actual = zk.getEphemerals(); + assertEquals("Expected ephemeral count for allPaths", actual.size(), expected.length); + for (int i = 0; i < expected.length; i++) { + String path = expected[i]; + assertTrue(String.format("Path=%s exists in get All Ephemerals list ", path), actual.contains(path)); + } + } + + @Test + public void testGetEphemeralsSyncByPath() throws KeeperException, InterruptedException { + final String prefixPath = BASE + 0; + List actual = zk.getEphemerals(prefixPath); + assertEquals("Expected ephemeral count for allPaths", actual.size(), EPHEMERAL_CNT); + for (int i = 0; i < EPHEMERAL_CNT; i++) { + String path = expected[i]; + assertTrue(String.format("Path=%s exists in getEphemerals(%s) list ", path, prefixPath), actual.contains(path)); + } + } + + @Test + public void testGetEphemerals() throws IOException, KeeperException, InterruptedException { + + final CountDownLatch doneProcessing = new CountDownLatch(1); + final List unexpectedBehavior = new ArrayList(); + zk.getEphemerals(new AsyncCallback.EphemeralsCallback() { + @Override + public void processResult(int rc, Object ctx, List paths) { + if (paths == null) { + unexpectedBehavior.add(String.format("Expected ephemeral count for" + + " allPaths to be %d but was null", expected.length)); + } else if (paths.size() != expected.length) { + unexpectedBehavior.add(String.format("Expected ephemeral count for allPaths to be %d but was %d", expected.length, paths.size())); + } + for (int i = 0; i < expected.length; i++) { + String path = expected[i]; + if (!paths.contains(path)) { + unexpectedBehavior.add(String.format("Path=%s exists in getEphemerals list ", path)); + } + } + doneProcessing.countDown(); + } + }, null); + long waitForCallbackSecs = 2L; + if (!doneProcessing.await(waitForCallbackSecs, TimeUnit.SECONDS)) { + fail(String.format("getEphemerals didn't callback within %d seconds", waitForCallbackSecs)); + } + checkForUnexpectedBehavior(unexpectedBehavior); + + } + + @Test + public void testGetEphemeralsByPath() throws IOException, KeeperException, InterruptedException { + + final CountDownLatch doneProcessing = new CountDownLatch(1); + final String checkPath = BASE + "0"; + final List unexpectedBehavior = new ArrayList(); + zk.getEphemerals(checkPath, new AsyncCallback.EphemeralsCallback() { + @Override + public void processResult(int rc, Object ctx, List paths) { + if (paths == null) { + unexpectedBehavior.add(String.format("Expected ephemeral count for %s to be %d but was null", checkPath, expected.length)); + } else if (paths.size() != EPHEMERAL_CNT) { + unexpectedBehavior.add(String.format("Expected ephemeral count for %s to be %d but was %d", checkPath, EPHEMERAL_CNT, paths.size())); + } + for (int i = 0; i < EPHEMERAL_CNT; i++) { + String path = expected[i]; + if (!paths.contains(path)) { + unexpectedBehavior.add(String.format("Expected path=%s didn't exist " + + "in getEphemerals list.", path)); + } + } + doneProcessing.countDown(); + } + }, null); + long waitForCallbackSecs = 2L; + if (!doneProcessing.await(waitForCallbackSecs, TimeUnit.SECONDS)) { + fail(String.format("getEphemerals(%s) didn't callback within %d seconds", checkPath, waitForCallbackSecs)); + } + checkForUnexpectedBehavior(unexpectedBehavior); + } + + @Test + public void testGetEphemeralsEmpty() throws IOException, KeeperException, InterruptedException { + + final CountDownLatch doneProcessing = new CountDownLatch(1); + final String checkPath = "/unknownPath"; + final int expectedSize = 0; + final List unexpectedBehavior = new ArrayList(); + zk.getEphemerals(checkPath, new AsyncCallback.EphemeralsCallback() { + @Override + public void processResult(int rc, Object ctx, List paths) { + if (paths == null) { + unexpectedBehavior.add(String.format("Expected ephemeral count for %s to be %d but was null", checkPath, expectedSize)); + } else if (paths.size() != expectedSize) { + unexpectedBehavior.add(String.format("Expected ephemeral count for %s to be %d but was %d", checkPath, expectedSize, paths.size())); + } + doneProcessing.countDown(); + } + }, null); + long waitForCallbackSecs = 2L; + if (!doneProcessing.await(waitForCallbackSecs, TimeUnit.SECONDS)) { + fail(String.format("getEphemerals(%s) didn't callback within %d seconds", checkPath, waitForCallbackSecs)); + } + checkForUnexpectedBehavior(unexpectedBehavior); + } + + @Test + public void testGetEphemeralsErrors() throws KeeperException { + try { + zk.getEphemerals(null, null, null); + fail("Should have thrown a IllegalArgumentException for a null prefixPath"); + } catch (IllegalArgumentException e) { + //pass + } + + try { + zk.getEphemerals("no leading slash", null, null); + fail("Should have thrown a IllegalArgumentException " + "for a prefix with no leading slash"); + } catch (IllegalArgumentException e) { + //pass + } + } + + private String[] generatePaths(int persistantCnt, int ephemeralCnt) throws KeeperException, InterruptedException { + + final String[] expected = new String[persistantCnt * ephemeralCnt]; + for (int p = 0; p < persistantCnt; p++) { + String base = BASE + p; + zk.create(base, base.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int e = 0; e < ephemeralCnt; e++) { + String ephem = base + "/ephem" + e; + zk.create(ephem, ephem.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + expected[p * ephemeralCnt + e] = ephem; + } + } + return expected; + } + + private void checkForUnexpectedBehavior(List unexpectedBehavior) { + if (unexpectedBehavior.size() > 0) { + StringBuilder b = new StringBuilder("The test failed for the following reasons:"); + b.append(NEWLINE); + for (String error : unexpectedBehavior) { + b.append("ERROR: ").append(error).append(NEWLINE); + } + fail(b.toString()); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/JUnit4ZKTestRunner.java b/zookeeper-server/src/test/java/org/apache/zookeeper/JUnit4ZKTestRunner.java new file mode 100644 index 0000000..700eadf --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/JUnit4ZKTestRunner.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.junit.internal.runners.statements.InvokeMethod; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The sole responsibility of this class is to print to the log when a test + * starts and when it finishes. + */ +public class JUnit4ZKTestRunner extends BlockJUnit4ClassRunner { + + private static final Logger LOG = LoggerFactory.getLogger(JUnit4ZKTestRunner.class); + + public JUnit4ZKTestRunner(Class klass) throws InitializationError { + super(klass); + } + + @SuppressWarnings("unchecked") + public static List computeTestMethodsForClass( + final Class klass, + final List defaultMethods) { + List list = defaultMethods; + String methodName = System.getProperty("test.method"); + if (methodName == null) { + LOG.info("No test.method specified. using default methods."); + } else { + LOG.info("Picked up test.method={}", methodName); + try { + list = Arrays.asList(new FrameworkMethod(klass.getMethod(methodName))); + } catch (NoSuchMethodException nsme) { + LOG.warn( + "{} does not have test.method={}. failing to default methods.", + klass.getName(), + methodName); + } + } + return list; + } + + @Override + protected List computeTestMethods() { + return computeTestMethodsForClass(getTestClass().getJavaClass(), super.computeTestMethods()); + } + + public static class LoggedInvokeMethod extends InvokeMethod { + + private final FrameworkMethod method; + private final String name; + + public LoggedInvokeMethod(FrameworkMethod method, Object target) { + super(method, target); + this.method = method; + name = method.getName(); + } + + @Override + public void evaluate() throws Throwable { + LOG.info("RUNNING TEST METHOD {}", name); + try { + super.evaluate(); + Runtime rt = Runtime.getRuntime(); + long usedKB = (rt.totalMemory() - rt.freeMemory()) / 1024; + LOG.info("Memory used {}", usedKB); + ThreadGroup tg = Thread.currentThread().getThreadGroup(); + while (tg.getParent() != null) { + tg = tg.getParent(); + } + LOG.info("Number of threads {}", tg.activeCount()); + } catch (Throwable t) { + // The test method threw an exception, but it might be an + // expected exception as defined in the @Test annotation. + // Check the annotation and log an appropriate message. + Test annotation = this.method.getAnnotation(Test.class); + if (annotation != null + && annotation.expected() != null + && annotation.expected().isAssignableFrom(t.getClass())) { + LOG.info("TEST METHOD {} THREW EXPECTED EXCEPTION {}", name, annotation.expected()); + } else { + LOG.warn("TEST METHOD FAILED {}", name, t); + } + throw t; + } + LOG.info("FINISHED TEST METHOD {}", name); + } + + } + + @Override + protected Statement methodInvoker(FrameworkMethod method, Object test) { + return new LoggedInvokeMethod(method, test); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/JaasConfiguration.java b/zookeeper-server/src/test/java/org/apache/zookeeper/JaasConfiguration.java new file mode 100644 index 0000000..aecc7ed --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/JaasConfiguration.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.HashMap; +import java.util.Map; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.AppConfigurationEntry.LoginModuleControlFlag; + +/** + * This helper class allows to programmatically create a JAAS configuration. + * Each section must have a name and a login module, and a set of key/values + * to describe login options. + * + * Example: + * jaas = new JaasConfiguration(); + * jaas.addSection("Server", "org.apache.zookeeper.server.auth.DigestLoginModule", + * "username", "passowrd"); + */ +public class JaasConfiguration extends javax.security.auth.login.Configuration { + + private final Map sections = new HashMap(); + + public JaasConfiguration() { + } + + /** + * Add a section to the jaas.conf + * @param name Section name + * @param loginModuleName Login module name + * @param args login key/value args + */ + public void addSection(String name, String loginModuleName, String... args) { + Map conf = new HashMap(); + // loop through the args (must be key/value sequence) + for (int i = 0; i < args.length - 1; i += 2) { + conf.put(args[i], args[i + 1]); + } + addSection(name, loginModuleName, conf); + } + + /** + * Add a section to the jaas.conf + * @param name Section name + * @param loginModuleName Login module name + * @param conf login key/value args + */ + public void addSection(String name, String loginModuleName, final Map conf) { + AppConfigurationEntry[] entries = new AppConfigurationEntry[1]; + entries[0] = new AppConfigurationEntry(loginModuleName, LoginModuleControlFlag.REQUIRED, conf); + this.sections.put(name, entries); + } + + @Override + public AppConfigurationEntry[] getAppConfigurationEntry(String appName) { + return sections.get(appName); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/MockPacket.java b/zookeeper-server/src/test/java/org/apache/zookeeper/MockPacket.java new file mode 100644 index 0000000..9d880fd --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/MockPacket.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.nio.ByteBuffer; +import org.apache.jute.Record; +import org.apache.zookeeper.ZooKeeper.WatchRegistration; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; + +public class MockPacket extends ClientCnxn.Packet { + + public MockPacket( + RequestHeader requestHeader, + ReplyHeader replyHeader, + Record request, + Record response, + WatchRegistration watchRegistration) { + super(requestHeader, replyHeader, request, response, watchRegistration); + } + + public MockPacket( + RequestHeader requestHeader, + ReplyHeader replyHeader, + Record request, + Record response, + WatchRegistration watchRegistration, + boolean readOnly) { + super(requestHeader, replyHeader, request, response, watchRegistration, readOnly); + } + + public ByteBuffer createAndReturnBB() { + createBB(); + return this.bb; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/MultiOperationRecordTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/MultiOperationRecordTest.java new file mode 100644 index 0000000..75da5df --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/MultiOperationRecordTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.junit.Test; + +public class MultiOperationRecordTest extends ZKTestCase { + + @Test + public void testRoundTrip() throws IOException { + MultiOperationRecord request = new MultiOperationRecord(); + request.add(Op.check("check", 1)); + request.add(Op.create("create", "create data".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, ZooDefs.Perms.ALL)); + request.add(Op.delete("delete", 17)); + request.add(Op.setData("setData", "set data".getBytes(), 19)); + + MultiOperationRecord decodedRequest = codeDecode(request); + + assertEquals(request, decodedRequest); + assertEquals(request.hashCode(), decodedRequest.hashCode()); + } + + @Test + public void testEmptyRoundTrip() throws IOException { + MultiOperationRecord request = new MultiOperationRecord(); + MultiOperationRecord decodedRequest = codeDecode(request); + + assertEquals(request, decodedRequest); + assertEquals(request.hashCode(), decodedRequest.hashCode()); + } + + private MultiOperationRecord codeDecode(MultiOperationRecord request) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + request.serialize(boa, "request"); + baos.close(); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + bb.rewind(); + + BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb)); + MultiOperationRecord decodedRequest = new MultiOperationRecord(); + decodedRequest.deserialize(bia, "request"); + return decodedRequest; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/MultiResponseTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/MultiResponseTest.java new file mode 100644 index 0000000..7993d04 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/MultiResponseTest.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.junit.Test; + +public class MultiResponseTest extends ZKTestCase { + + public void testRoundTrip() throws IOException { + MultiResponse response = new MultiResponse(); + + response.add(new OpResult.CheckResult()); + response.add(new OpResult.CreateResult("foo-bar")); + response.add(new OpResult.DeleteResult()); + + Stat s = new Stat(); + s.setCzxid(546); + response.add(new OpResult.SetDataResult(s)); + + MultiResponse decodedResponse = codeDecode(response); + + assertEquals(response, decodedResponse); + assertEquals(response.hashCode(), decodedResponse.hashCode()); + } + + @Test + public void testEmptyRoundTrip() throws IOException { + MultiResponse result = new MultiResponse(); + MultiResponse decodedResult = codeDecode(result); + + assertEquals(result, decodedResult); + assertEquals(result.hashCode(), decodedResult.hashCode()); + } + + private MultiResponse codeDecode(MultiResponse request) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + request.serialize(boa, "result"); + baos.close(); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + bb.rewind(); + + BinaryInputArchive bia = BinaryInputArchive.getArchive(new ByteBufferInputStream(bb)); + MultiResponse decodedRequest = new MultiResponse(); + decodedRequest.deserialize(bia, "result"); + return decodedRequest; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/PortAssignment.java b/zookeeper-server/src/test/java/org/apache/zookeeper/PortAssignment.java new file mode 100644 index 0000000..87c0562 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/PortAssignment.java @@ -0,0 +1,215 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.net.ServerSocket; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Assign ports to tests */ +public final class PortAssignment { + + private static final Logger LOG = LoggerFactory.getLogger(PortAssignment.class); + + // The available port range that we use stays away from the ephemeral port + // range, which the OS will assign to client socket connections. We can't + // coordinate with the OS on the assignment of those ports, so it's best to + // stay out of that range to avoid conflicts. Typical ranges for ephemeral + // ports are: + // - IANA suggests 49152 - 65535 + // - Linux typically uses 32768 - 61000 + // - FreeBSD modern versions typically use the IANA suggested range + // - Windows modern versions typically use the IANA suggested range + private static final int GLOBAL_BASE_PORT = 11221; + private static final int GLOBAL_MAX_PORT = 32767; + + private static PortRange portRange = null; + private static int nextPort; + + /** + * Assign a new, unique port to the test. This method works by assigning + * ports from a valid port range as identified by the total number of + * concurrent test processes and the ID of this test process. Each + * concurrent test process uses an isolated range, so it's not possible for + * multiple test processes to collide on the same port. Within the port + * range, ports are assigned in monotonic increasing order, wrapping around + * to the beginning of the range if needed. As an extra precaution, the + * method attempts to bind to the port and immediately close it before + * returning it to the caller. If the port cannot be bound, then it tries + * the next one in the range. This provides some resiliency in case the port + * is otherwise occupied, such as a developer running other servers on the + * machine running the tests. + * + * @return port + */ + public static synchronized int unique() { + if (portRange == null) { + Integer threadId = Integer.getInteger("zookeeper.junit.threadid"); + portRange = setupPortRange( + System.getProperty("test.junit.threads"), + threadId != null ? "threadid=" + threadId : System.getProperty("sun.java.command")); + nextPort = portRange.getMinimum(); + } + int candidatePort = nextPort; + for (; ; ) { + ++candidatePort; + if (candidatePort > portRange.getMaximum()) { + candidatePort = portRange.getMinimum(); + } + if (candidatePort == nextPort) { + throw new IllegalStateException(String.format( + "Could not assign port from range %s. The entire range has been exhausted.", + portRange)); + } + try { + ServerSocket s = new ServerSocket(candidatePort); + s.close(); + nextPort = candidatePort; + LOG.info("Assigned port {} from range {}.", nextPort, portRange); + return nextPort; + } catch (IOException e) { + LOG.debug( + "Could not bind to port {} from range {}. Attempting next port.", + candidatePort, + portRange, + e); + } + } + } + + /** + * Sets up the port range to be used. In typical usage, Ant invokes JUnit, + * possibly using multiple JUnit processes to execute multiple test suites + * concurrently. The count of JUnit processes is passed from Ant as a system + * property named "test.junit.threads". Ant's JUnit runner receives the + * thread ID as a command line argument of the form threadid=N, where N is an + * integer in the range [1, ${test.junit.threads}]. It's not otherwise + * accessible, so we need to parse it from the command line. This method + * uses these 2 pieces of information to split the available ports into + * disjoint ranges. Each JUnit process only assigns ports from its own range + * in order to prevent bind errors during concurrent test runs. If any of + * this information is unavailable or unparseable, then the default behavior + * is for this process to use the entire available port range. This is + * expected when running tests outside of Ant. + * + * @param strProcessCount string representation of integer process count, + * typically taken from system property test.junit.threads + * @param cmdLine command line containing threadid=N argument, typically + * taken from system property sun.java.command + * @return port range to use + */ + static PortRange setupPortRange(String strProcessCount, String cmdLine) { + Integer processCount = null; + if (strProcessCount != null && !strProcessCount.isEmpty()) { + try { + processCount = Integer.valueOf(strProcessCount); + } catch (NumberFormatException e) { + LOG.warn("Error parsing test.junit.threads = {}.", strProcessCount, e); + } + } + + Integer threadId = null; + if (processCount != null) { + if (cmdLine != null && !cmdLine.isEmpty()) { + Matcher m = Pattern.compile("threadid=(\\d+)").matcher(cmdLine); + if (m.find()) { + try { + threadId = Integer.valueOf(m.group(1)); + } catch (NumberFormatException e) { + LOG.warn("Error parsing threadid from {}.", cmdLine, e); + } + } + } + } + + final PortRange newPortRange; + if (processCount != null && processCount > 1 && threadId != null) { + // We know the total JUnit process count and this test process's ID. + // Use these values to calculate the valid range for port assignments + // within this test process. We lose a few possible ports to the + // remainder, but that's acceptable. + int portRangeSize = (GLOBAL_MAX_PORT - GLOBAL_BASE_PORT) / processCount; + int minPort = GLOBAL_BASE_PORT + ((threadId - 1) * portRangeSize); + int maxPort = minPort + portRangeSize - 1; + newPortRange = new PortRange(minPort, maxPort); + LOG.info("Test process {}/{} using ports from {}.", threadId, processCount, newPortRange); + } else { + // If running outside the context of Ant or Ant is using a single + // test process, then use all valid ports. + newPortRange = new PortRange(GLOBAL_BASE_PORT, GLOBAL_MAX_PORT); + LOG.info("Single test process using ports from {}.", newPortRange); + } + + return newPortRange; + } + + /** + * Contains the minimum and maximum (both inclusive) in a range of ports. + */ + static final class PortRange { + + private final int minimum; + private final int maximum; + + /** + * Creates a new PortRange. + * + * @param minimum lower bound port number + * @param maximum upper bound port number + */ + PortRange(int minimum, int maximum) { + this.minimum = minimum; + this.maximum = maximum; + } + + /** + * Returns maximum port in the range. + * + * @return maximum + */ + int getMaximum() { + return maximum; + } + + /** + * Returns minimum port in the range. + * + * @return minimum + */ + int getMinimum() { + return minimum; + } + + @Override + public String toString() { + return String.format("%d - %d", minimum, maximum); + } + + } + + /** + * There is no reason to instantiate this class. + */ + private PortAssignment() { + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/PortAssignmentTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/PortAssignmentTest.java new file mode 100644 index 0000000..234adb1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/PortAssignmentTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import java.util.Arrays; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class PortAssignmentTest { + + private final String strProcessCount; + private final String cmdLine; + private final int expectedMinimumPort; + private final int expectedMaximumPort; + + @Parameters + public static Collection data() { + return Arrays.asList( + new Object[]{"8", "threadid=1", 11221, 13913}, + new Object[]{"8", "threadid=2", 13914, 16606}, + new Object[]{"8", "threadid=3", 16607, 19299}, + new Object[]{"8", "threadid=4", 19300, 21992}, + new Object[]{"8", "threadid=5", 21993, 24685}, + new Object[]{"8", "threadid=6", 24686, 27378}, + new Object[]{"8", "threadid=7", 27379, 30071}, + new Object[]{"8", "threadid=8", 30072, 32764}, + new Object[]{"1", "threadid=1", 11221, 32767}, + new Object[]{"2", "threadid=1", 11221, 21993}, + new Object[]{"2", "threadid=2", 21994, 32766}, + new Object[]{null, null, 11221, 32767}, + new Object[]{"", "", 11221, 32767}); + } + + public PortAssignmentTest(String strProcessCount, String cmdLine, int expectedMinimumPort, int expectedMaximumPort) { + this.strProcessCount = strProcessCount; + this.cmdLine = cmdLine; + this.expectedMinimumPort = expectedMinimumPort; + this.expectedMaximumPort = expectedMaximumPort; + } + + @Test + public void testSetupPortRange() { + PortAssignment.PortRange portRange = PortAssignment.setupPortRange(strProcessCount, cmdLine); + assertEquals(buildAssertionMessage("minimum"), expectedMinimumPort, portRange.getMinimum()); + assertEquals(buildAssertionMessage("maximum"), expectedMaximumPort, portRange.getMaximum()); + } + + private String buildAssertionMessage(String checkType) { + return String.format("strProcessCount = %s, cmdLine = %s, checking %s", strProcessCount, cmdLine, checkType); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesCmdTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesCmdTest.java new file mode 100644 index 0000000..c92f72a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesCmdTest.java @@ -0,0 +1,302 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Testing remove watches using command line + */ +public class RemoveWatchesCmdTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(RemoveWatchesCmdTest.class); + private ZooKeeper zk; + private ZooKeeperMain zkMain; + + @Override + public void setUp() throws Exception { + super.setUp(); + zk = createClient(); + zkMain = new ZooKeeperMain(zk); + } + + @Override + public void tearDown() throws Exception { + if (zk != null) { + zk.close(); + } + super.tearDown(); + } + + /** + * Test verifies default options. When there is no passed options, + * removewatches command will use default options - WatcherType.ANY and + * local=false + */ + @Test(timeout = 30000) + public void testRemoveWatchesWithNoPassedOptions() throws Exception { + List expectedEvents = new ArrayList<>(); + expectedEvents.add(EventType.ChildWatchRemoved); + expectedEvents.add(EventType.DataWatchRemoved); + MyWatcher myWatcher = new MyWatcher("/testnode1", expectedEvents, 2); + + zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/testnode2", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + LOG.info("Adding childwatcher to /testnode1 and /testnode2"); + zk.getChildren("/testnode1", myWatcher); + zk.getChildren("/testnode2", myWatcher); + + LOG.info("Adding datawatcher to /testnode1 and /testnode2"); + zk.getData("/testnode1", myWatcher, null); + zk.getData("/testnode2", myWatcher, null); + + String cmdstring = "removewatches /testnode1"; + LOG.info("Remove watchers using shell command : {}", cmdstring); + zkMain.cl.parseCommand(cmdstring); + assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl)); + LOG.info("Waiting for the DataWatchRemoved event"); + myWatcher.matches(); + + // verifying that other path child watches are not affected + assertTrue("Failed to find child watches for the path testnode2", zk.getChildWatches().contains("/testnode2")); + assertTrue("Failed to find data watches for the path testnode2", zk.getDataWatches().contains("/testnode2")); + } + + /** + * Test verifies deletion of NodeDataChanged watches + */ + @Test(timeout = 30000) + public void testRemoveNodeDataChangedWatches() throws Exception { + LOG.info("Adding data watcher using getData()"); + List expectedEvents = new ArrayList<>(); + expectedEvents.add(EventType.DataWatchRemoved); + MyWatcher myWatcher = new MyWatcher("/testnode1", expectedEvents, 1); + + zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.getData("/testnode1", myWatcher, null); + + String cmdstring = "removewatches /testnode1 -d"; + LOG.info("Remove watchers using shell command : {}", cmdstring); + zkMain.cl.parseCommand(cmdstring); + assertTrue("Removewatches cmd fails to remove data watches", zkMain.processZKCmd(zkMain.cl)); + + LOG.info("Waiting for the DataWatchRemoved event"); + myWatcher.matches(); + + // verifying that other path data watches are removed + assertEquals("Data watches are not removed : " + zk.getDataWatches(), 0, zk.getDataWatches().size()); + } + + /** + * Test verifies deletion of NodeCreated data watches + */ + @Test(timeout = 30000) + public void testRemoveNodeCreatedWatches() throws Exception { + List expectedEvents = new ArrayList<>(); + expectedEvents.add(EventType.DataWatchRemoved); + MyWatcher myWatcher1 = new MyWatcher("/testnode1", expectedEvents, 1); + MyWatcher myWatcher2 = new MyWatcher("/testnode1/testnode2", expectedEvents, 1); + // Adding pre-created watcher + LOG.info("Adding NodeCreated watcher"); + zk.exists("/testnode1", myWatcher1); + zk.exists("/testnode1/testnode2", myWatcher2); + + String cmdstring1 = "removewatches /testnode1 -d"; + LOG.info("Remove watchers using shell command : {}", cmdstring1); + zkMain.cl.parseCommand(cmdstring1); + assertTrue("Removewatches cmd fails to remove pre-create watches", zkMain.processZKCmd(zkMain.cl)); + myWatcher1.matches(); + assertEquals("Failed to remove pre-create watches :" + + zk.getExistWatches(), 1, zk.getExistWatches().size()); + assertTrue("Failed to remove pre-create watches :" + + zk.getExistWatches(), zk.getExistWatches().contains("/testnode1/testnode2")); + + String cmdstring2 = "removewatches /testnode1/testnode2 -d"; + LOG.info("Remove watchers using shell command : {}", cmdstring2); + zkMain.cl.parseCommand(cmdstring2); + assertTrue("Removewatches cmd fails to remove data watches", zkMain.processZKCmd(zkMain.cl)); + + myWatcher2.matches(); + assertEquals("Failed to remove pre-create watches : " + + zk.getExistWatches(), 0, zk.getExistWatches().size()); + } + + /** + * Test verifies deletion of NodeChildrenChanged watches + */ + @Test(timeout = 30000) + public void testRemoveNodeChildrenChangedWatches() throws Exception { + List expectedEvents = new ArrayList<>(); + expectedEvents.add(EventType.ChildWatchRemoved); + MyWatcher myWatcher = new MyWatcher("/testnode1", expectedEvents, 1); + + zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("Adding child changed watcher"); + zk.getChildren("/testnode1", myWatcher); + + String cmdstring = "removewatches /testnode1 -c"; + LOG.info("Remove watchers using shell command : {}", cmdstring); + zkMain.cl.parseCommand(cmdstring); + assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl)); + myWatcher.matches(); + assertEquals("Failed to remove child watches : " + zk.getChildWatches(), 0, zk.getChildWatches().size()); + } + + /** + * Test verifies deletion of NodeDeleted watches + */ + @Test(timeout = 30000) + public void testRemoveNodeDeletedWatches() throws Exception { + LOG.info("Adding NodeDeleted watcher"); + List expectedEvents = new ArrayList<>(); + expectedEvents.add(EventType.ChildWatchRemoved); + expectedEvents.add(EventType.NodeDeleted); + MyWatcher myWatcher = new MyWatcher("/testnode1", expectedEvents, 1); + + zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/testnode1/testnode2", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.getChildren("/testnode1/testnode2", myWatcher); + zk.getChildren("/testnode1", myWatcher); + + String cmdstring = "removewatches /testnode1 -c"; + LOG.info("Remove watchers using shell command : {}", cmdstring); + zkMain.cl.parseCommand(cmdstring); + assertTrue("Removewatches cmd fails to remove child watches", zkMain.processZKCmd(zkMain.cl)); + LOG.info("Waiting for the ChildWatchRemoved event"); + myWatcher.matches(); + assertEquals("Failed to remove child watches : " + zk.getChildWatches(), 1, zk.getChildWatches().size()); + + assertTrue("Failed to remove child watches :" + + zk.getChildWatches(), zk.getChildWatches().contains("/testnode1/testnode2")); + + // verify node delete watcher + zk.delete("/testnode1/testnode2", -1); + myWatcher.matches(); + } + + /** + * Test verifies deletion of any watches + */ + @Test(timeout = 30000) + public void testRemoveAnyWatches() throws Exception { + verifyRemoveAnyWatches(false); + } + + /** + * Test verifies deletion of watches locally when there is no server + * connection + */ + @Test(timeout = 30000) + public void testRemoveWatchesLocallyWhenNoServerConnection() throws Exception { + verifyRemoveAnyWatches(true); + } + + private void verifyRemoveAnyWatches(boolean local) throws Exception { + final Map> pathVsEvent = new HashMap<>(); + LOG.info("Adding NodeChildrenChanged, NodeDataChanged watchers"); + final CountDownLatch watcherLatch = new CountDownLatch(2); + Watcher watcher = new Watcher() { + + @Override + public void process(WatchedEvent event) { + switch (event.getType()) { + case ChildWatchRemoved: + case DataWatchRemoved: + addWatchNotifications(pathVsEvent, event); + watcherLatch.countDown(); + break; + case NodeChildrenChanged: + case NodeDataChanged: + addWatchNotifications(pathVsEvent, event); + break; + } + } + + private void addWatchNotifications(Map> pathVsEvent, WatchedEvent event) { + pathVsEvent.computeIfAbsent(event.getPath(), k -> new ArrayList<>()) + .add(event.getType()); + } + }; + zk.create("/testnode1", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.getChildren("/testnode1", watcher); + zk.getData("/testnode1", watcher, null); + String cmdstring = "removewatches /testnode1 -a"; + if (local) { + LOG.info("Stopping ZK server to verify deletion of watches locally"); + stopServer(); + cmdstring = "removewatches /testnode1 -a -l"; + } + + LOG.info("Remove watchers using shell command : {}", cmdstring); + zkMain.cl.parseCommand(cmdstring); + assertTrue("Removewatches cmd fails to remove child/data watches", zkMain.processZKCmd(zkMain.cl)); + LOG.info("Waiting for the WatchRemoved events"); + watcherLatch.await(10, TimeUnit.SECONDS); + assertEquals("Didn't receives WatchRemoved events!", 1, pathVsEvent.size()); + assertTrue("Didn't receives DataWatchRemoved!", pathVsEvent.get("/testnode1").contains(EventType.DataWatchRemoved)); + assertTrue("Didn't receives ChildWatchRemoved!", pathVsEvent.get("/testnode1").contains(EventType.ChildWatchRemoved)); + } + + private static class MyWatcher implements Watcher { + + private final String path; + private String eventPath; + private final CountDownLatch latch; + private final List expectedEvents = new ArrayList<>(); + + MyWatcher(String path, List expectedEvents, int count) { + this.path = path; + this.latch = new CountDownLatch(count); + this.expectedEvents.addAll(expectedEvents); + } + + public void process(WatchedEvent event) { + LOG.debug("Event path : {}, eventPath : {}", path, event.getPath()); + this.eventPath = event.getPath(); + if (expectedEvents.contains(event.getType())) { + latch.countDown(); + } + } + + public boolean matches() throws InterruptedException { + if (!latch.await(CONNECTION_TIMEOUT / 3, TimeUnit.MILLISECONDS)) { + LOG.error("Failed to get watch notifications!"); + return false; + } + LOG.debug("Client path : {} eventPath : {}", path, eventPath); + return path.equals(eventPath); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesTest.java new file mode 100644 index 0000000..30f1558 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/RemoveWatchesTest.java @@ -0,0 +1,1075 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.commons.collections.CollectionUtils; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.NoWatcherException; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.WatcherType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Verifies removing watches using ZooKeeper client apis + */ +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class RemoveWatchesTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(RemoveWatchesTest.class); + private ZooKeeper zk1 = null; + private ZooKeeper zk2 = null; + + @Override + public void setUp() throws Exception { + super.setUp(); + zk1 = createClient(); + zk2 = createClient(); + } + + @Override + public void tearDown() throws Exception { + if (zk1 != null) { + zk1.close(); + } + if (zk2 != null) { + zk2.close(); + } + super.tearDown(); + } + + private final boolean useAsync; + + public RemoveWatchesTest(boolean useAsync) { + this.useAsync = useAsync; + } + + @Parameters + public static Collection configs() { + return Arrays.asList(new Object[][]{{false}, {true}}); + } + + private void removeWatches( + ZooKeeper zk, + String path, + Watcher watcher, + WatcherType watcherType, + boolean local, + KeeperException.Code rc) throws InterruptedException, KeeperException { + LOG.info("Sending removeWatches req using zk {} path: {} type: {} watcher: {} ", zk, path, watcherType, watcher); + if (useAsync) { + MyCallback c1 = new MyCallback(rc.intValue(), path); + zk.removeWatches(path, watcher, watcherType, local, c1, null); + assertTrue("Didn't succeeds removeWatch operation", c1.matches()); + if (KeeperException.Code.OK.intValue() != c1.rc) { + KeeperException ke = KeeperException.create(KeeperException.Code.get(c1.rc)); + throw ke; + } + } else { + zk.removeWatches(path, watcher, watcherType, local); + } + } + + private void removeAllWatches( + ZooKeeper zk, + String path, + WatcherType watcherType, + boolean local, + KeeperException.Code rc) throws InterruptedException, KeeperException { + LOG.info("Sending removeWatches req using zk {} path: {} type: {} ", zk, path, watcherType); + if (useAsync) { + MyCallback c1 = new MyCallback(rc.intValue(), path); + zk.removeAllWatches(path, watcherType, local, c1, null); + assertTrue("Didn't succeeds removeWatch operation", c1.matches()); + if (KeeperException.Code.OK.intValue() != c1.rc) { + KeeperException ke = KeeperException.create(KeeperException.Code.get(c1.rc)); + throw ke; + } + } else { + zk.removeAllWatches(path, watcherType, local); + } + } + + /** + * Test verifies removal of single watcher when there is server connection + */ + @Test(timeout = 90000) + public void testRemoveSingleWatcher() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + zk1.create("/node2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + MyWatcher w1 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + MyWatcher w2 = new MyWatcher("/node2", 1); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node2", w2)); + removeWatches(zk2, "/node1", w1, WatcherType.Data, false, Code.OK); + assertEquals("Didn't find data watcher", 1, zk2.getDataWatches().size()); + assertEquals("Didn't find data watcher", "/node2", zk2.getDataWatches().get(0)); + removeWatches(zk2, "/node2", w2, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove data watcher", w2.matches()); + // closing session should remove ephemeral nodes and trigger data + // watches if any + if (zk1 != null) { + zk1.close(); + zk1 = null; + } + + List events = w1.getEventsAfterWatchRemoval(); + assertFalse("Shouldn't get NodeDeletedEvent after watch removal", events.contains(EventType.NodeDeleted)); + assertEquals("Shouldn't get NodeDeletedEvent after watch removal", 0, events.size()); + } + + /** + * Test verifies removal of multiple data watchers when there is server + * connection + */ + @Test(timeout = 90000) + public void testMultipleDataWatchers() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + MyWatcher w1 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + removeWatches(zk2, "/node1", w2, WatcherType.Data, false, Code.OK); + assertEquals("Didn't find data watcher", 1, zk2.getDataWatches().size()); + assertEquals("Didn't find data watcher", "/node1", zk2.getDataWatches().get(0)); + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove data watcher", w2.matches()); + // closing session should remove ephemeral nodes and trigger data + // watches if any + if (zk1 != null) { + zk1.close(); + zk1 = null; + } + + List events = w2.getEventsAfterWatchRemoval(); + assertEquals("Shouldn't get NodeDeletedEvent after watch removal", 0, events.size()); + } + + /** + * Test verifies removal of multiple child watchers when there is server + * connection + */ + @Test(timeout = 90000) + public void testMultipleChildWatchers() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 1); + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w1); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w2); + removeWatches(zk2, "/node1", w2, WatcherType.Children, false, Code.OK); + assertTrue("Didn't remove child watcher", w2.matches()); + assertEquals("Didn't find child watcher", 1, zk2.getChildWatches().size()); + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove child watcher", w1.matches()); + // create child to see NodeChildren notification + zk1.create("/node1/node2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + // waiting for child watchers to be notified + int count = 30; + while (count > 0) { + if (w1.getEventsAfterWatchRemoval().size() > 0) { + break; + } + count--; + Thread.sleep(100); + } + // watcher2 + List events = w2.getEventsAfterWatchRemoval(); + assertEquals("Shouldn't get NodeChildrenChanged event", 0, events.size()); + } + + /** + * Test verifies null watcher with WatcherType.Any - remove all the watchers + * data, child, exists + */ + @Test(timeout = 90000) + public void testRemoveAllWatchers() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 2); + MyWatcher w2 = new MyWatcher("/node1", 2); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w1); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w2); + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK); + removeWatches(zk2, "/node1", w2, WatcherType.Any, false, Code.OK); + zk1.create("/node1/child", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertTrue("Didn't remove data watcher", w1.matches()); + assertTrue("Didn't remove child watcher", w2.matches()); + } + + /** + * Test verifies null watcher with WatcherType.Data - remove all data + * watchers. Child watchers shouldn't be removed + */ + @Test(timeout = 90000) + public void testRemoveAllDataWatchers() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 1); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w1); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w2); + removeWatches(zk2, "/node1", w1, WatcherType.Data, false, Code.OK); + removeWatches(zk2, "/node1", w2, WatcherType.Data, false, Code.OK); + zk1.create("/node1/child", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertTrue("Didn't remove data watcher", w1.matches()); + assertTrue("Didn't remove data watcher", w2.matches()); + // waiting for child watchers to be notified + int count = 10; + while (count > 0) { + if (w1.getEventsAfterWatchRemoval().size() > 0 && w2.getEventsAfterWatchRemoval().size() > 0) { + break; + } + count--; + Thread.sleep(1000); + } + // watcher1 + List events = w1.getEventsAfterWatchRemoval(); + assertEquals("Didn't get NodeChildrenChanged event", 1, events.size()); + assertTrue("Didn't get NodeChildrenChanged event", events.contains(EventType.NodeChildrenChanged)); + // watcher2 + events = w2.getEventsAfterWatchRemoval(); + assertEquals("Didn't get NodeChildrenChanged event", 1, events.size()); + assertTrue("Didn't get NodeChildrenChanged event", events.contains(EventType.NodeChildrenChanged)); + } + + /** + * Test verifies null watcher with WatcherType.Children - remove all child + * watchers. Data watchers shouldn't be removed + */ + @Test(timeout = 90000) + public void testRemoveAllChildWatchers() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 1); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w1); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w2); + removeWatches(zk2, "/node1", w1, WatcherType.Children, false, Code.OK); + removeWatches(zk2, "/node1", w2, WatcherType.Children, false, Code.OK); + zk1.setData("/node1", "test".getBytes(), -1); + assertTrue("Didn't remove child watcher", w1.matches()); + assertTrue("Didn't remove child watcher", w2.matches()); + // waiting for child watchers to be notified + int count = 10; + while (count > 0) { + if (w1.getEventsAfterWatchRemoval().size() > 0 && w2.getEventsAfterWatchRemoval().size() > 0) { + break; + } + count--; + Thread.sleep(1000); + } + // watcher1 + List events = w1.getEventsAfterWatchRemoval(); + assertEquals("Didn't get NodeDataChanged event", 1, events.size()); + assertTrue("Didn't get NodeDataChanged event", events.contains(EventType.NodeDataChanged)); + // watcher2 + events = w2.getEventsAfterWatchRemoval(); + assertEquals("Didn't get NodeDataChanged event", 1, events.size()); + assertTrue("Didn't get NodeDataChanged event", events.contains(EventType.NodeDataChanged)); + } + + /** + * Test verifies given watcher doesn't exists! + */ + @Test(timeout = 90000) + public void testNoWatcherException() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 2); + MyWatcher w2 = new MyWatcher("/node1", 2); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNull("Didn't set data watches", zk2.exists("/node2", w2)); + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w1); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w2); + + // New Watcher which will be used for removal + MyWatcher w3 = new MyWatcher("/node1", 2); + + try { + removeWatches(zk2, "/node1", w3, WatcherType.Any, false, Code.NOWATCHER); + fail("Should throw exception as given watcher doesn't exists"); + } catch (KeeperException.NoWatcherException nwe) { + // expected + } + try { + removeWatches(zk2, "/node1", w3, WatcherType.Children, false, Code.NOWATCHER); + fail("Should throw exception as given watcher doesn't exists"); + } catch (KeeperException.NoWatcherException nwe) { + // expected + } + try { + removeWatches(zk2, "/node1", w3, WatcherType.Data, false, Code.NOWATCHER); + fail("Should throw exception as given watcher doesn't exists"); + } catch (KeeperException.NoWatcherException nwe) { + // expected + } + try { + removeWatches(zk2, "/nonexists", w3, WatcherType.Data, false, Code.NOWATCHER); + fail("Should throw exception as given watcher doesn't exists"); + } catch (KeeperException.NoWatcherException nwe) { + // expected + } + } + + /** + * Test verifies WatcherType.Any - removes only the configured data watcher + * function + */ + @Test(timeout = 90000) + public void testRemoveAnyDataWatcher() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 1); + MyWatcher w2 = new MyWatcher("/node1", 2); + // Add multiple data watches + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + // Add child watch + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w2); + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove data watcher", w1.matches()); + assertEquals("Didn't find child watcher", 1, zk2.getChildWatches().size()); + assertEquals("Didn't find data watcher", 1, zk2.getDataWatches().size()); + removeWatches(zk2, "/node1", w2, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove child watcher", w2.matches()); + } + + /** + * Test verifies WatcherType.Any - removes only the configured child watcher + * function + */ + @Test(timeout = 90000) + public void testRemoveAnyChildWatcher() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 2); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + // Add multiple child watches + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w2); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w1); + removeWatches(zk2, "/node1", w2, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove child watcher", w2.matches()); + assertEquals("Didn't find child watcher", 1, zk2.getChildWatches().size()); + assertEquals("Didn't find data watcher", 1, zk2.getDataWatches().size()); + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove watchers", w1.matches()); + } + + /** + * Test verifies when there is no server connection. Remove watches when + * local=true, otw should retain it + */ + @Test(timeout = 90000) + public void testRemoveWatcherWhenNoConnection() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 2); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + // Add multiple child watches + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w1); + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w2); + stopServer(); + removeWatches(zk2, "/node1", w2, WatcherType.Any, true, Code.OK); + assertTrue("Didn't remove child watcher", w2.matches()); + assertFalse("Shouldn't remove data watcher", w1.matches()); + try { + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.CONNECTIONLOSS); + fail("Should throw exception as last watch removal requires server connection"); + } catch (KeeperException.ConnectionLossException nwe) { + // expected + } + assertFalse("Shouldn't remove data watcher", w1.matches()); + + // when local=true, here if connection not available, simply removes + // from local session + removeWatches(zk2, "/node1", w1, WatcherType.Any, true, Code.OK); + assertTrue("Didn't remove data watcher", w1.matches()); + } + + /** + * Test verifies many pre-node watchers. Also, verifies internal + * datastructure 'watchManager.existWatches' + */ + @Test(timeout = 90000) + public void testManyPreNodeWatchers() throws Exception { + int count = 50; + List wList = new ArrayList(count); + MyWatcher w; + String path = "/node"; + // Exists watcher + for (int i = 0; i < count; i++) { + final String nodePath = path + i; + w = new MyWatcher(nodePath, 1); + wList.add(w); + LOG.info("Adding pre node watcher {} on path {}", w, nodePath); + zk1.exists(nodePath, w); + } + assertEquals("Failed to add watchers!", count, zk1.getExistWatches().size()); + for (int i = 0; i < count; i++) { + final MyWatcher watcher = wList.get(i); + removeWatches(zk1, path + i, watcher, WatcherType.Data, false, Code.OK); + assertTrue("Didn't remove data watcher", watcher.matches()); + } + assertEquals("Didn't remove watch references!", 0, zk1.getExistWatches().size()); + } + + /** + * Test verifies many child watchers. Also, verifies internal datastructure + * 'watchManager.childWatches' + */ + @Test(timeout = 90000) + public void testManyChildWatchers() throws Exception { + int count = 50; + List wList = new ArrayList(count); + MyWatcher w; + String path = "/node"; + + // Child watcher + for (int i = 0; i < count; i++) { + String nodePath = path + i; + zk1.create(nodePath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + nodePath += "/"; + } + for (int i = 0; i < count; i++) { + String nodePath = path + i; + w = new MyWatcher(path + i, 1); + wList.add(w); + LOG.info("Adding child watcher {} on path {}", w, nodePath); + zk1.getChildren(nodePath, w); + nodePath += "/"; + } + assertEquals("Failed to add watchers!", count, zk1.getChildWatches().size()); + for (int i = 0; i < count; i++) { + final MyWatcher watcher = wList.get(i); + removeWatches(zk1, path + i, watcher, WatcherType.Children, false, Code.OK); + assertTrue("Didn't remove child watcher", watcher.matches()); + } + assertEquals("Didn't remove watch references!", 0, zk1.getChildWatches().size()); + } + + /** + * Test verifies many data watchers. Also, verifies internal datastructure + * 'watchManager.dataWatches' + */ + @Test(timeout = 90000) + public void testManyDataWatchers() throws Exception { + int count = 50; + List wList = new ArrayList(count); + MyWatcher w; + String path = "/node"; + + // Data watcher + for (int i = 0; i < count; i++) { + String nodePath = path + i; + w = new MyWatcher(path + i, 1); + wList.add(w); + zk1.create(nodePath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("Adding data watcher {} on path {}", w, nodePath); + zk1.getData(nodePath, w, null); + nodePath += "/"; + } + assertEquals("Failed to add watchers!", count, zk1.getDataWatches().size()); + for (int i = 0; i < count; i++) { + final MyWatcher watcher = wList.get(i); + removeWatches(zk1, path + i, watcher, WatcherType.Data, false, Code.OK); + assertTrue("Didn't remove data watcher", watcher.matches()); + } + assertEquals("Didn't remove watch references!", 0, zk1.getDataWatches().size()); + } + + /** + * Test verifies removal of many watchers locally when no connection and + * WatcherType#Any. Also, verifies internal watchManager datastructures + */ + @Test(timeout = 90000) + public void testManyWatchersWhenNoConnection() throws Exception { + int count = 3; + List wList = new ArrayList(count); + MyWatcher w; + String path = "/node"; + + // Child watcher + for (int i = 0; i < count; i++) { + String nodePath = path + i; + zk1.create(nodePath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + nodePath += "/"; + } + for (int i = 0; i < count; i++) { + String nodePath = path + i; + w = new MyWatcher(path + i, 2); + wList.add(w); + LOG.info("Adding child watcher {} on path {}", w, nodePath); + zk1.getChildren(nodePath, w); + nodePath += "/"; + } + assertEquals("Failed to add watchers!", count, zk1.getChildWatches().size()); + + // Data watcher + for (int i = 0; i < count; i++) { + String nodePath = path + i; + w = wList.get(i); + LOG.info("Adding data watcher {} on path {}", w, nodePath); + zk1.getData(nodePath, w, null); + nodePath += "/"; + } + assertEquals("Failed to add watchers!", count, zk1.getDataWatches().size()); + stopServer(); + for (int i = 0; i < count; i++) { + final MyWatcher watcher = wList.get(i); + removeWatches(zk1, path + i, watcher, WatcherType.Any, true, Code.OK); + assertTrue("Didn't remove watcher", watcher.matches()); + } + assertEquals("Didn't remove watch references!", 0, zk1.getChildWatches().size()); + assertEquals("Didn't remove watch references!", 0, zk1.getDataWatches().size()); + } + + /** + * Test verifies removing watcher having namespace + */ + @Test(timeout = 90000) + public void testChRootRemoveWatcher() throws Exception { + // creating the subtree for chRoot clients. + String chRoot = "/appsX"; + zk1.create("/appsX", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + if (zk1 != null) { + zk1.close(); + } + if (zk2 != null) { + zk2.close(); + } + // Creating chRoot client. + zk1 = createClient(this.hostPort + chRoot); + zk2 = createClient(this.hostPort + chRoot); + + LOG.info("Creating child znode /node1 using chRoot client"); + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + MyWatcher w1 = new MyWatcher("/node1", 2); + MyWatcher w2 = new MyWatcher("/node1", 1); + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + // Add multiple child watches + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + zk2.getChildren("/node1", w2); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + zk2.getChildren("/node1", w1); + removeWatches(zk2, "/node1", w1, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove child watcher", w1.matches()); + assertEquals("Didn't find child watcher", 1, zk2.getChildWatches().size()); + removeWatches(zk2, "/node1", w2, WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove child watcher", w2.matches()); + } + + /** + * Verify that if a given watcher doesn't exist, the server properly + * returns an error code for it. + * + * In our Java client implementation, we check that a given watch exists at + * two points: + * + * 1) before submitting the RemoveWatches request + * 2) after a successful server response, when the watcher needs to be + * removed + * + * Since this can be racy (i.e. a watch can fire while a RemoveWatches + * request is in-flight), we need to verify that the watch was actually + * removed (i.e. from ZKDatabase and DataTree) and return NOWATCHER if + * needed. + * + * Also, other implementations might not do a client side check before + * submitting a RemoveWatches request. If we don't do a server side check, + * we would just return ZOK even if no watch was removed. + * + */ + @Test(timeout = 90000) + public void testNoWatcherServerException() throws InterruptedException, IOException, TimeoutException { + CountdownWatcher watcher = new CountdownWatcher(); + MyZooKeeper zk = new MyZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + boolean nw = false; + + watcher.waitForConnected(CONNECTION_TIMEOUT); + + try { + zk.removeWatches("/nowatchhere", watcher, WatcherType.Data, false); + } catch (KeeperException nwe) { + if (nwe.code().intValue() == Code.NOWATCHER.intValue()) { + nw = true; + } + } + + assertTrue("Server didn't return NOWATCHER", zk.getRemoveWatchesRC() == Code.NOWATCHER.intValue()); + assertTrue("NoWatcherException didn't happen", nw); + } + + /** + * Test verifies given watcher doesn't exists! + */ + @Test(timeout = 90000) + public void testRemoveAllNoWatcherException() throws IOException, InterruptedException, KeeperException { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + try { + removeAllWatches(zk2, "/node1", WatcherType.Any, false, Code.NOWATCHER); + fail("Should throw exception as given watcher doesn't exists"); + } catch (KeeperException.NoWatcherException nwe) { + // expected + } + } + + /** + * Test verifies null watcher + */ + @Test(timeout = 30000) + public void testNullWatcherReference() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + try { + if (useAsync) { + zk1.removeWatches("/node1", null, WatcherType.Data, false, null, null); + } else { + zk1.removeWatches("/node1", null, WatcherType.Data, false); + } + fail("Must throw IllegalArgumentException as watcher is null!"); + } catch (IllegalArgumentException iae) { + // expected + } + } + + /** + * Test verifies WatcherType.Data - removes only the configured data watcher + * function + */ + @Test(timeout = 90000) + public void testRemoveWhenMultipleDataWatchesOnAPath() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final CountDownLatch dataWatchCount = new CountDownLatch(1); + final CountDownLatch rmWatchCount = new CountDownLatch(1); + Watcher w1 = event -> { + if (event.getType() == EventType.DataWatchRemoved) { + rmWatchCount.countDown(); + } + }; + Watcher w2 = event -> { + if (event.getType() == EventType.NodeDataChanged) { + dataWatchCount.countDown(); + } + }; + // Add multiple data watches + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + + removeWatches(zk2, "/node1", w1, WatcherType.Data, false, Code.OK); + assertTrue("Didn't remove data watcher", rmWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + zk1.setData("/node1", "test".getBytes(), -1); + LOG.info("Waiting for data watchers to be notified"); + assertTrue("Didn't get data watch notification!", dataWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + } + + /** + * Test verifies WatcherType.Children - removes only the configured child + * watcher function + */ + @Test(timeout = 90000) + public void testRemoveWhenMultipleChildWatchesOnAPath() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final CountDownLatch childWatchCount = new CountDownLatch(1); + final CountDownLatch rmWatchCount = new CountDownLatch(1); + Watcher w1 = event -> { + if (event.getType() == EventType.ChildWatchRemoved) { + rmWatchCount.countDown(); + } + }; + Watcher w2 = event -> { + if (event.getType() == EventType.NodeChildrenChanged) { + childWatchCount.countDown(); + } + }; + // Add multiple child watches + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + assertEquals("Didn't set child watches", 0, zk2.getChildren("/node1", w1).size()); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + assertEquals("Didn't set child watches", 0, zk2.getChildren("/node1", w2).size()); + + removeWatches(zk2, "/node1", w1, WatcherType.Children, false, Code.OK); + assertTrue("Didn't remove child watcher", rmWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + zk1.create("/node1/node2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("Waiting for child watchers to be notified"); + assertTrue("Didn't get child watch notification!", childWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + } + + /** + * Test verifies WatcherType.Data - removes only the configured data watcher + * function + */ + @Test(timeout = 90000) + public void testRemoveAllDataWatchesOnAPath() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final CountDownLatch dWatchCount = new CountDownLatch(2); + final CountDownLatch rmWatchCount = new CountDownLatch(2); + Watcher w1 = event -> { + switch (event.getType()) { + case DataWatchRemoved: + rmWatchCount.countDown(); + break; + case NodeDataChanged: + dWatchCount.countDown(); + break; + default: + break; + } + }; + Watcher w2 = event -> { + switch (event.getType()) { + case DataWatchRemoved: + rmWatchCount.countDown(); + break; + case NodeDataChanged: + dWatchCount.countDown(); + break; + default: + break; + } + }; + // Add multiple data watches + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + + assertTrue("Server session is not a watcher", isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Data)); + removeAllWatches(zk2, "/node1", WatcherType.Data, false, Code.OK); + assertTrue("Didn't remove data watcher", rmWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + assertFalse("Server session is still a watcher after removal", isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Data)); + } + + /** + * Test verifies WatcherType.Children - removes only the configured child + * watcher function + */ + @Test(timeout = 90000) + public void testRemoveAllChildWatchesOnAPath() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final CountDownLatch cWatchCount = new CountDownLatch(2); + final CountDownLatch rmWatchCount = new CountDownLatch(2); + Watcher w1 = event -> { + switch (event.getType()) { + case ChildWatchRemoved: + rmWatchCount.countDown(); + break; + case NodeChildrenChanged: + cWatchCount.countDown(); + break; + default: + break; + } + }; + Watcher w2 = event -> { + switch (event.getType()) { + case ChildWatchRemoved: + rmWatchCount.countDown(); + break; + case NodeChildrenChanged: + cWatchCount.countDown(); + break; + default: + break; + } + }; + // Add multiple child watches + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + assertEquals("Didn't set child watches", 0, zk2.getChildren("/node1", w1).size()); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + assertEquals("Didn't set child watches", 0, zk2.getChildren("/node1", w2).size()); + + assertTrue("Server session is not a watcher", isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Children)); + removeAllWatches(zk2, "/node1", WatcherType.Children, false, Code.OK); + assertTrue("Didn't remove child watcher", rmWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + assertFalse("Server session is still a watcher after removal", isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Children)); + } + + /** + * Test verifies WatcherType.Any - removes all the configured child,data + * watcher functions + */ + @Test(timeout = 90000) + public void testRemoveAllWatchesOnAPath() throws Exception { + zk1.create("/node1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final CountDownLatch watchCount = new CountDownLatch(2); + final CountDownLatch rmWatchCount = new CountDownLatch(4); + Watcher w1 = event -> { + switch (event.getType()) { + case ChildWatchRemoved: + case DataWatchRemoved: + rmWatchCount.countDown(); + break; + case NodeChildrenChanged: + case NodeDataChanged: + watchCount.countDown(); + break; + default: + break; + } + }; + Watcher w2 = event -> { + switch (event.getType()) { + case ChildWatchRemoved: + case DataWatchRemoved: + rmWatchCount.countDown(); + break; + case NodeChildrenChanged: + case NodeDataChanged: + watchCount.countDown(); + break; + default: + break; + } + }; + // Add multiple child watches + LOG.info("Adding child watcher {} on path {}", w1, "/node1"); + assertEquals("Didn't set child watches", 0, zk2.getChildren("/node1", w1).size()); + LOG.info("Adding child watcher {} on path {}", w2, "/node1"); + assertEquals("Didn't set child watches", 0, zk2.getChildren("/node1", w2).size()); + + // Add multiple data watches + LOG.info("Adding data watcher {} on path {}", w1, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w1)); + LOG.info("Adding data watcher {} on path {}", w2, "/node1"); + assertNotNull("Didn't set data watches", zk2.exists("/node1", w2)); + + assertTrue("Server session is not a watcher", isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Data)); + removeAllWatches(zk2, "/node1", WatcherType.Any, false, Code.OK); + assertTrue("Didn't remove data watcher", rmWatchCount.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + assertFalse("Server session is still a watcher after removal", isServerSessionWatcher(zk2.getSessionId(), "/node1", WatcherType.Data)); + assertEquals("Received watch notification after removal!", 2, watchCount.getCount()); + } + + /* a mocked ZK class that doesn't do client-side verification + * before/after calling removeWatches */ + private class MyZooKeeper extends ZooKeeper { + + class MyWatchManager extends ZKWatchManager { + + public MyWatchManager(boolean disableAutoWatchReset) { + super(disableAutoWatchReset); + } + + public int lastrc; + + /* Pretend that any watcher exists */ + void containsWatcher(String path, Watcher watcher, WatcherType watcherType) throws NoWatcherException { + } + + /* save the return error code by the server */ + protected boolean removeWatches( + Map> pathVsWatcher, + Watcher watcher, + String path, + boolean local, + int rc, + Set removedWatchers) throws KeeperException { + lastrc = rc; + return false; + } + + } + + public MyZooKeeper(String hp, int timeout, Watcher watcher) throws IOException { + super(hp, timeout, watcher, false); + } + + private MyWatchManager myWatchManager; + + protected ZKWatchManager defaultWatchManager() { + myWatchManager = new MyWatchManager(getClientConfig().getBoolean(ZKClientConfig.DISABLE_AUTO_WATCH_RESET)); + return myWatchManager; + } + + public int getRemoveWatchesRC() { + return myWatchManager.lastrc; + } + + } + + private class MyWatcher implements Watcher { + + private final String path; + private String eventPath; + private CountDownLatch latch; + private List eventsAfterWatchRemoval = new ArrayList(); + MyWatcher(String path, int count) { + this.path = path; + latch = new CountDownLatch(count); + } + + public void process(WatchedEvent event) { + LOG.debug("Event path : {}, eventPath : {}", path, event.getPath()); + this.eventPath = event.getPath(); + // notifies watcher removal + if (latch.getCount() == 0) { + if (event.getType() != EventType.None) { + eventsAfterWatchRemoval.add(event.getType()); + } + } + if (event.getType() == EventType.ChildWatchRemoved || event.getType() == EventType.DataWatchRemoved) { + latch.countDown(); + } + } + + /** + * Returns true if the watcher was triggered. Try to avoid using this + * method with assertFalse statements. A false return depends on a timed + * out wait on a latch, which makes tests run long. + * + * @return true if the watcher was triggered, false otherwise + * @throws InterruptedException if interrupted while waiting on latch + */ + public boolean matches() throws InterruptedException { + if (!latch.await(CONNECTION_TIMEOUT / 5, TimeUnit.MILLISECONDS)) { + LOG.error("Failed waiting to remove the watches"); + return false; + } + LOG.debug("Client path : {} eventPath : {}", path, eventPath); + return path.equals(eventPath); + } + + public List getEventsAfterWatchRemoval() { + return eventsAfterWatchRemoval; + } + + } + + private class MyCallback implements AsyncCallback.VoidCallback { + + private final String path; + private final int rc; + private String eventPath; + int eventRc; + private CountDownLatch latch = new CountDownLatch(1); + + public MyCallback(int rc, String path) { + this.rc = rc; + this.path = path; + } + + @Override + public void processResult(int rc, String eventPath, Object ctx) { + System.out.println("latch:" + path + " " + eventPath); + this.eventPath = eventPath; + this.eventRc = rc; + this.latch.countDown(); + } + + /** + * Returns true if the callback was triggered. Try to avoid using this + * method with assertFalse statements. A false return depends on a timed + * out wait on a latch, which makes tests run long. + * + * @return true if the watcher was triggered, false otherwise + * @throws InterruptedException if interrupted while waiting on latch + */ + public boolean matches() throws InterruptedException { + if (!latch.await(CONNECTION_TIMEOUT / 5, TimeUnit.MILLISECONDS)) { + return false; + } + return path.equals(eventPath) && rc == eventRc; + } + + } + + /** + * Checks if a session is registered with the server as a watcher. + * + * @param sessionId the session ID to check + * @param path the path to check for watchers + * @param type the type of watcher + * @return true if the client session is a watcher on path for the type + */ + private boolean isServerSessionWatcher(long sessionId, String path, WatcherType type) { + Set cnxns = new HashSet<>(); + CollectionUtils.addAll(cnxns, serverFactory.getConnections().iterator()); + for (ServerCnxn cnxn : cnxns) { + if (cnxn.getSessionId() == sessionId) { + return serverFactory.getZooKeeperServer().getZKDatabase().getDataTree().containsWatcher(path, type, cnxn); + } + } + return false; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/SaslAuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/SaslAuthTest.java new file mode 100644 index 0000000..a63ab10 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/SaslAuthTest.java @@ -0,0 +1,246 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ClientCnxn.EventThread; +import org.apache.zookeeper.ClientCnxn.SendThread; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.test.ClientBase; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SaslAuthTest extends ClientBase { + + @BeforeClass + public static void init() { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + try { + File tmpDir = createTmpDir(); + File saslConfFile = new File(tmpDir, "jaas.conf"); + String jaasContent = getJaasFileContent(); + FileWriter fwriter = new FileWriter(saslConfFile); + fwriter.write(jaasContent); + fwriter.close(); + System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + } catch (IOException e) { + // could not create tmp directory to hold JAAS conf file : test will + // fail now. + } + } + + private static String getJaasFileContent() { + StringBuilder jaasContent = new StringBuilder(); + String newLine = System.getProperty("line.separator"); + jaasContent.append("Server {"); + jaasContent.append(newLine); + jaasContent.append("org.apache.zookeeper.server.auth.DigestLoginModule required"); + jaasContent.append(newLine); + jaasContent.append("user_super=\"test\";"); + jaasContent.append(newLine); + jaasContent.append("};"); + jaasContent.append(newLine); + jaasContent.append("Client {"); + jaasContent.append(newLine); + jaasContent.append("org.apache.zookeeper.server.auth.DigestLoginModule required"); + jaasContent.append(newLine); + jaasContent.append("username=\"super\""); + jaasContent.append(newLine); + jaasContent.append("password=\"test\";"); + jaasContent.append(newLine); + jaasContent.append("};"); + jaasContent.append(newLine); + return jaasContent.toString(); + } + + @AfterClass + public static void clean() { + System.clearProperty("zookeeper.authProvider.1"); + System.clearProperty("java.security.auth.login.config"); + } + + private final CountDownLatch authFailed = new CountDownLatch(1); + + @Override + protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { + MyWatcher watcher = new MyWatcher(); + return createClient(watcher, hp); + } + + private class MyWatcher extends CountdownWatcher { + + @Override + public synchronized void process(WatchedEvent event) { + if (event.getState() == KeeperState.AuthFailed) { + authFailed.countDown(); + } else { + super.process(event); + } + } + + } + + @Test + public void testAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Thread.sleep(1000); + } finally { + zk.close(); + } + } + + @Test + public void testValidSaslIds() throws Exception { + ZooKeeper zk = createClient(); + + List validIds = new ArrayList(); + validIds.add("user"); + validIds.add("service/host.name.com"); + validIds.add("user@KERB.REALM"); + validIds.add("service/host.name.com@KERB.REALM"); + + int i = 0; + for (String validId : validIds) { + List aclList = new ArrayList(); + ACL acl = new ACL(0, new Id("sasl", validId)); + aclList.add(acl); + zk.create("/valid" + i, null, aclList, CreateMode.PERSISTENT); + i++; + } + } + + @Test + public void testInvalidSaslIds() throws Exception { + ZooKeeper zk = createClient(); + + List invalidIds = new ArrayList(); + invalidIds.add("user@KERB.REALM/server.com"); + invalidIds.add("user@KERB.REALM1@KERB.REALM2"); + + int i = 0; + for (String invalidId : invalidIds) { + List aclList = new ArrayList(); + try { + ACL acl = new ACL(0, new Id("sasl", invalidId)); + aclList.add(acl); + zk.create("/invalid" + i, null, aclList, CreateMode.PERSISTENT); + fail("SASLAuthenticationProvider.isValid() failed to catch invalid Id."); + } catch (KeeperException.InvalidACLException e) { + // ok. + } finally { + i++; + } + } + } + + @Test + public void testZKOperationsAfterClientSaslAuthFailure() throws Exception { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + try { + setSaslFailureFlag(zk); + + // try node creation for around 15 second, + int totalTry = 10; + int tryCount = 0; + + boolean success = false; + while (!success && tryCount++ <= totalTry) { + try { + zk.create("/saslAuthFail", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + success = true; + } catch (KeeperException.ConnectionLossException e) { + Thread.sleep(1000); + // do nothing + } + } + assertTrue("ZNode creation is failing continuously after Sasl auth failure.", success); + + } finally { + zk.close(); + } + } + + // set saslLoginFailed to true to simulate the LoginException + private void setSaslFailureFlag(ZooKeeper zk) throws Exception { + Field cnxnField = zk.getClass().getDeclaredField("cnxn"); + cnxnField.setAccessible(true); + ClientCnxn clientCnxn = (ClientCnxn) cnxnField.get(zk); + Field sendThreadField = clientCnxn.getClass().getDeclaredField("sendThread"); + sendThreadField.setAccessible(true); + SendThread sendThread = (SendThread) sendThreadField.get(clientCnxn); + Field saslLoginFailedField = sendThread.getClass().getDeclaredField("saslLoginFailed"); + saslLoginFailedField.setAccessible(true); + saslLoginFailedField.setBoolean(sendThread, true); + } + + @Test + public void testThreadsShutdownOnAuthFailed() throws Exception { + MyWatcher watcher = new MyWatcher(); + ZooKeeper zk = null; + try { + zk = new ZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + try { + zk.addAuthInfo("FOO", "BAR".getBytes()); + zk.getData("/path1", false, null); + fail("Should get auth state error"); + } catch (KeeperException.AuthFailedException e) { + if (!authFailed.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("Should have called my watcher"); + } + } + Field cnxnField = zk.getClass().getDeclaredField("cnxn"); + cnxnField.setAccessible(true); + ClientCnxn clientCnxn = (ClientCnxn) cnxnField.get(zk); + Field sendThreadField = clientCnxn.getClass().getDeclaredField("sendThread"); + sendThreadField.setAccessible(true); + SendThread sendThread = (SendThread) sendThreadField.get(clientCnxn); + Field eventThreadField = clientCnxn.getClass().getDeclaredField("eventThread"); + eventThreadField.setAccessible(true); + EventThread eventThread = (EventThread) eventThreadField.get(clientCnxn); + sendThread.join(CONNECTION_TIMEOUT); + eventThread.join(CONNECTION_TIMEOUT); + assertFalse("SendThread did not shutdown after authFail", sendThread.isAlive()); + assertFalse("EventThread did not shutdown after authFail", eventThread.isAlive()); + } finally { + if (zk != null) { + zk.close(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java new file mode 100644 index 0000000..a2b4371 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ServerConfigTest.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.File; +import org.apache.zookeeper.server.ServerConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.junit.Before; +import org.junit.Test; + +public class ServerConfigTest { + + private ServerConfig serverConfig; + + @Before + public void setUp() { + serverConfig = new ServerConfig(); + } + + @Test(expected = IllegalArgumentException.class) + public void testFewArguments() { + String[] args = {"2181"}; + serverConfig.parse(args); + } + + @Test + public void testValidArguments() { + String[] args = {"2181", "/data/dir", "60000", "10000"}; + serverConfig.parse(args); + + assertEquals(2181, serverConfig.getClientPortAddress().getPort()); + assertTrue(checkEquality("/data/dir", serverConfig.getDataDir())); + assertEquals(60000, serverConfig.getTickTime()); + assertEquals(10000, serverConfig.getMaxClientCnxns()); + } + + @Test(expected = IllegalArgumentException.class) + public void testTooManyArguments() { + String[] args = {"2181", "/data/dir", "60000", "10000", "9999"}; + serverConfig.parse(args); + } + + @Test + public void testJvmPauseMonitorConfigured() { + final Long sleepTime = 444L; + final Long warnTH = 5555L; + final Long infoTH = 555L; + + QuorumPeerConfig qpConfig = mock(QuorumPeerConfig.class); + when(qpConfig.isJvmPauseMonitorToRun()).thenReturn(true); + when(qpConfig.getJvmPauseSleepTimeMs()).thenReturn(sleepTime); + when(qpConfig.getJvmPauseWarnThresholdMs()).thenReturn(warnTH); + when(qpConfig.getJvmPauseInfoThresholdMs()).thenReturn(infoTH); + + serverConfig.readFrom(qpConfig); + + assertEquals(sleepTime, Long.valueOf(serverConfig.getJvmPauseSleepTimeMs())); + assertEquals(warnTH, Long.valueOf(serverConfig.getJvmPauseWarnThresholdMs())); + assertEquals(infoTH, Long.valueOf(serverConfig.getJvmPauseInfoThresholdMs())); + assertTrue(serverConfig.isJvmPauseMonitorToRun()); + } + + boolean checkEquality(String a, String b) { + assertNotNull(a); + assertNotNull(b); + return a.equals(b); + } + + boolean checkEquality(String a, File b) { + assertNotNull(a); + assertNotNull(b); + return new File(a).equals(b); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/TestableZooKeeper.java b/zookeeper-server/src/test/java/org/apache/zookeeper/TestableZooKeeper.java new file mode 100644 index 0000000..a02127a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/TestableZooKeeper.java @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.io.IOException; +import java.net.SocketAddress; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.jute.Record; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; + +public class TestableZooKeeper extends ZooKeeperAdmin { + + public TestableZooKeeper(String host, int sessionTimeout, Watcher watcher) throws IOException { + super(host, sessionTimeout, watcher); + } + + class TestableClientCnxn extends ClientCnxn { + + TestableClientCnxn( + String chrootPath, + HostProvider hostProvider, + int sessionTimeout, + ZooKeeper zooKeeper, + ClientWatchManager watcher, + ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + super(chrootPath, + hostProvider, + sessionTimeout, + zooKeeper, + watcher, + clientCnxnSocket, + 0, + new byte[16], + canBeReadOnly); + } + + void setXid(int newXid) { + xid = newXid; + } + + int checkXid() { + return xid; + } + + } + + protected ClientCnxn createConnection( + String chrootPath, + HostProvider hostProvider, + int sessionTimeout, + ZooKeeper zooKeeper, + ClientWatchManager watcher, + ClientCnxnSocket clientCnxnSocket, + boolean canBeReadOnly) throws IOException { + return new TestableClientCnxn( + chrootPath, + hostProvider, + sessionTimeout, + this, + watcher, + clientCnxnSocket, + canBeReadOnly); + } + + public void setXid(int xid) { + ((TestableClientCnxn) cnxn).setXid(xid); + } + + public int checkXid() { + return ((TestableClientCnxn) cnxn).checkXid(); + } + + @Override + public List getChildWatches() { + return super.getChildWatches(); + } + + @Override + public List getDataWatches() { + return super.getDataWatches(); + } + + @Override + public List getExistWatches() { + return super.getExistWatches(); + } + + /** + * Cause this ZooKeeper object to disconnect from the server. It will then + * later attempt to reconnect. + */ + public void testableConnloss() throws IOException { + synchronized (cnxn) { + cnxn.sendThread.testableCloseSocket(); + } + } + + /** + * Cause this ZooKeeper object to stop receiving from the ZooKeeperServer + * for the given number of milliseconds. + * @param ms the number of milliseconds to pause. + * @return true if the connection is paused, otherwise false + */ + public boolean pauseCnxn(final long ms) { + final CountDownLatch initiatedPause = new CountDownLatch(1); + new Thread() { + public void run() { + synchronized (cnxn) { + try { + try { + cnxn.sendThread.testableCloseSocket(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + initiatedPause.countDown(); + } + Thread.sleep(ms); + } catch (InterruptedException e) { + } + } + } + }.start(); + + try { + return initiatedPause.await(ms, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + e.printStackTrace(); + return false; + } + } + + public SocketAddress testableLocalSocketAddress() { + return super.testableLocalSocketAddress(); + } + + public SocketAddress testableRemoteSocketAddress() { + return super.testableRemoteSocketAddress(); + } + + /** + * @return the last zxid as seen by the client session + */ + public long testableLastZxid() { + return cnxn.getLastZxid(); + } + + public ReplyHeader submitRequest( + RequestHeader h, + Record request, + Record response, + WatchRegistration watchRegistration) throws InterruptedException { + return cnxn.submitRequest(h, request, response, watchRegistration); + } + + /** Testing only!!! Really!!!! This is only here to test when the client + * disconnects from the server w/o sending a session disconnect (ie + * ending the session cleanly). The server will eventually notice the + * client is no longer pinging and will timeout the session. + */ + public void disconnect() { + cnxn.disconnect(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java new file mode 100644 index 0000000..1542df0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/VerGenTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import java.io.File; +import java.util.Arrays; +import java.util.Collection; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.version.util.VerGen; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * Test VerGen, used during the build. + * + */ +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class VerGenTest extends ZKTestCase { + + @Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{{"1.2.3", new Object[]{1, 2, 3, null}}, {"1.2.3-dev", new Object[]{1, 2, 3, "dev"}}, {"1.2.3-SNAPSHOT", new Object[]{1, 2, 3, "SNAPSHOT"}}, {"1.2.3-SNAPSHOT", new Object[]{1, 2, 3, "SNAPSHOT"}}, {"1.2.3-foo-bar+123", new Object[]{1, 2, 3, "foo-bar+123"}}, {"1.2.3.4.5-SNAPSHOT", new Object[]{1, 2, 3, "SNAPSHOT"}}, {"1.2.3.4.5-foo-bar+123", new Object[]{1, 2, 3, "foo-bar+123"}}}); + } + + private String input; + + private Object[] expected; + + public VerGenTest(String input, Object[] expected) { + this.input = input; + this.expected = expected; + } + + @Test + public void testParser() { + VerGen.Version v = VerGen.parseVersionString(input); + assertEquals(expected[0], v.maj); + assertEquals(expected[1], v.min); + assertEquals(expected[2], v.micro); + assertEquals(expected[3], v.qualifier); + } + + @Test + public void testGenFile() throws Exception { + VerGen.Version v = VerGen.parseVersionString(input); + File outputDir = ClientBase.createTmpDir(); + VerGen.generateFile(outputDir, v, "1", "Nov1"); + ClientBase.recursiveDelete(outputDir); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZKParameterized.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKParameterized.java new file mode 100644 index 0000000..6b53590 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKParameterized.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import java.util.List; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; +import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters; +import org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory; +import org.junit.runners.parameterized.TestWithParameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZKParameterized { + + private static final Logger LOG = LoggerFactory.getLogger(ZKParameterized.class); + + public static class RunnerFactory extends BlockJUnit4ClassRunnerWithParametersFactory { + + @Override + public org.junit.runner.Runner createRunnerForTestWithParameters(TestWithParameters test) throws InitializationError { + return new ZKParameterized.Runner(test); + } + + } + + public static class Runner extends BlockJUnit4ClassRunnerWithParameters { + + public Runner(TestWithParameters test) throws InitializationError { + super(test); + } + + @Override + protected List computeTestMethods() { + return JUnit4ZKTestRunner.computeTestMethodsForClass(getTestClass().getJavaClass(), super.computeTestMethods()); + } + + @Override + protected Statement methodInvoker(FrameworkMethod method, Object test) { + return new JUnit4ZKTestRunner.LoggedInvokeMethod(method, test); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java new file mode 100644 index 0000000..78e3cd7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKTestCase.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.time.LocalDateTime; +import org.apache.zookeeper.util.ServiceUtils; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Base class for a non-parameterized ZK test. + * + * Basic utilities shared by all tests. Also logging of various events during + * the test execution (start/stop/success/failure/etc...) + */ +@RunWith(JUnit4ZKTestRunner.class) +public class ZKTestCase { + + protected static final File testBaseDir = new File(System.getProperty("build.test.dir", "build")); + private static final Logger LOG = LoggerFactory.getLogger(ZKTestCase.class); + + static { + // Disable System.exit in tests. + ServiceUtils.setSystemExitProcedure(ServiceUtils.LOG_ONLY); + } + + private String testName; + + protected String getTestName() { + return testName; + } + + @BeforeClass + public static void before() { + if (!testBaseDir.exists()) { + assertTrue( + "Cannot properly create test base directory " + testBaseDir.getAbsolutePath(), + testBaseDir.mkdirs()); + } else if (!testBaseDir.isDirectory()) { + assertTrue( + "Cannot properly delete file with duplicate name of test base directory " + testBaseDir.getAbsolutePath(), + testBaseDir.delete()); + assertTrue( + "Cannot properly create test base directory " + testBaseDir.getAbsolutePath(), + testBaseDir.mkdirs()); + } + } + + @Rule + public TestWatcher watchman = new TestWatcher() { + + @Override + public void starting(Description method) { + // By default, disable starting a JettyAdminServer in tests to avoid + // accidentally attempting to start multiple admin servers on the + // same port. + System.setProperty("zookeeper.admin.enableServer", "false"); + // ZOOKEEPER-2693 disables all 4lw by default. + // Here we enable the 4lw which ZooKeeper tests depends. + System.setProperty("zookeeper.4lw.commands.whitelist", "*"); + testName = method.getMethodName(); + LOG.info("STARTING {}", testName); + } + + @Override + public void finished(Description method) { + LOG.info("FINISHED {}", testName); + } + + @Override + public void succeeded(Description method) { + LOG.info("SUCCEEDED {}", testName); + } + + @Override + public void failed(Throwable e, Description method) { + LOG.error("FAILED {}", testName, e); + } + + }; + + public interface WaitForCondition { + + /** + * @return true when success + */ + boolean evaluate(); + + } + + /** + * Wait for condition to be true; otherwise fail the test if it exceed + * timeout + * @param msg error message to print when fail + * @param condition condition to evaluate + * @param timeout timeout in seconds + * @throws InterruptedException + */ + public void waitFor(String msg, WaitForCondition condition, int timeout) throws InterruptedException { + final LocalDateTime deadline = LocalDateTime.now().plusSeconds(timeout); + while (LocalDateTime.now().isBefore(deadline)) { + if (condition.evaluate()) { + return; + } + Thread.sleep(100); + } + fail(msg); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java new file mode 100644 index 0000000..43e9869 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZKUtilTest.java @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assume.assumeTrue; +import java.io.File; +import java.io.IOException; +import java.util.UUID; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ZKUtilTest { + + private static final File testData = new File(System.getProperty("test.data.dir", "build/test/data")); + + @BeforeClass + public static void init() { + testData.mkdirs(); + } + + @Test + public void testValidateFileInput() throws IOException { + File file = File.createTempFile("test", ".junit", testData); + file.deleteOnExit(); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNull(error); + } + + @Test + public void testValidateFileInputNotExist() { + String fileName = UUID.randomUUID().toString(); + File file = new File(testData, fileName); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNotNull(error); + String expectedMessage = "File '" + absolutePath + "' does not exist."; + assertEquals(expectedMessage, error); + } + + @Test + public void testValidateFileInputDirectory() throws Exception { + File file = File.createTempFile("test", ".junit", testData); + file.deleteOnExit(); + // delete file, as we need directory not file + file.delete(); + file.mkdir(); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNotNull(error); + String expectedMessage = "'" + absolutePath + "' is a direcory. it must be a file."; + assertEquals(expectedMessage, error); + } + + @Test + public void testUnreadableFileInput() throws Exception { + //skip this test on Windows, coverage on Linux + assumeTrue(!org.apache.zookeeper.Shell.WINDOWS); + File file = File.createTempFile("test", ".junit", testData); + file.setReadable(false, false); + file.deleteOnExit(); + String absolutePath = file.getAbsolutePath(); + String error = ZKUtil.validateFileInput(absolutePath); + assertNotNull(error); + String expectedMessage = "Read permission is denied on the file '" + absolutePath + "'"; + assertEquals(expectedMessage, error); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java new file mode 100644 index 0000000..8fc3df2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/ZooKeeperTest.java @@ -0,0 +1,729 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.cli.CliCommand; +import org.apache.zookeeper.cli.CliException; +import org.apache.zookeeper.cli.CliWrapperException; +import org.apache.zookeeper.cli.LsCommand; +import org.apache.zookeeper.cli.MalformedCommandException; +import org.apache.zookeeper.cli.MalformedPathException; +import org.apache.zookeeper.cli.SyncCommand; +import org.apache.zookeeper.client.ConnectStringParser; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.StaticHostProvider; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.StringUtils; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +/** + * + * Testing ZooKeeper public methods + * + */ +public class ZooKeeperTest extends ClientBase { + + private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n"); + + @Test + public void testDeleteRecursive() throws IOException, InterruptedException, KeeperException { + final ZooKeeper zk = createClient(); + setupDataTree(zk); + + assertTrue(ZKUtil.deleteRecursive(zk, "/a/c", 1000)); + List children = zk.getChildren("/a", false); + assertEquals("1 children - c should be deleted ", 1, children.size()); + assertTrue(children.contains("b")); + + assertTrue(ZKUtil.deleteRecursive(zk, "/a", 1000)); + assertNull(zk.exists("/a", null)); + } + + @Test + public void testDeleteRecursiveFail() throws IOException, InterruptedException, KeeperException { + final ZooKeeper zk = createClient(); + setupDataTree(zk); + + ACL deleteProtection = new ACL(ZooDefs.Perms.DELETE, new Id("digest", "user:tl+z3z0vO6PfPfEENfLF96E6pM0="/* password is test */)); + List acls = Arrays.asList(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE), deleteProtection); + + // poison the well + zk.create("/a/c/0/surprise", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(1, zk.getACL("/a/c/0", new Stat()).size()); + zk.setACL("/a/c/0", acls, -1); + assertEquals(2, zk.getACL("/a/c/0", new Stat()).size()); + + assertFalse(ZKUtil.deleteRecursive(zk, "/a/c", 1000)); + List children = zk.getChildren("/a", false); + assertEquals("2 children - c should fail to be deleted ", 2, children.size()); + assertTrue(children.contains("b")); + + assertTrue(ZKUtil.deleteRecursive(zk, "/a/b", 1000)); + children = zk.getChildren("/a", false); + assertEquals("1 children - b should be deleted ", 1, children.size()); + + // acquire immunity to poison + zk.addAuthInfo(deleteProtection.getId().getScheme(), "user:test".getBytes()); + + assertTrue(ZKUtil.deleteRecursive(zk, "/a", 1000)); + assertNull(zk.exists("/a", null)); + } + + private void setupDataTree(ZooKeeper zk) throws KeeperException, InterruptedException { + // making sure setdata works on / + zk.setData("/", "some".getBytes(), -1); + zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + for (int i = 1000; i < 3000; ++i) { + zk.create("/a/b/v/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + zk.create("/a/c", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/c/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + for (int i = 0; i < 500; ++i) { + zk.create("/a/c/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + List children = zk.getChildren("/a", false); + + assertEquals("2 children - b & c should be present ", 2, children.size()); + assertTrue(children.contains("b")); + assertTrue(children.contains("c")); + } + + @Test + public void testDeleteRecursiveCli() throws IOException, InterruptedException, CliException, KeeperException { + final ZooKeeper zk = createClient(); + // making sure setdata works on / + zk.setData("/", "some".getBytes(), -1); + zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v/1", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/c", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/c/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + List children = zk.getChildren("/a", false); + + assertEquals("2 children - b & c should be present ", children.size(), 2); + assertTrue(children.contains("b")); + assertTrue(children.contains("c")); + + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring1 = "deleteall /a"; + zkMain.cl.parseCommand(cmdstring1); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + assertNull(zk.exists("/a", null)); + } + + @Test + public void testDeleteRecursiveAsync() throws IOException, InterruptedException, KeeperException { + final ZooKeeper zk = createClient(); + // making sure setdata works on / + zk.setData("/", "some".getBytes(), -1); + zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v/1", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/c", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/c/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + for (int i = 0; i < 50; ++i) { + zk.create("/a/c/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + List children = zk.getChildren("/a", false); + + assertEquals("2 children - b & c should be present ", children.size(), 2); + assertTrue(children.contains("b")); + assertTrue(children.contains("c")); + + VoidCallback cb = new VoidCallback() { + + @Override + public void processResult(int rc, String path, Object ctx) { + synchronized (ctx) { + ((AtomicInteger) ctx).set(4); + ctx.notify(); + } + } + + }; + final AtomicInteger ctx = new AtomicInteger(3); + ZKUtil.deleteRecursive(zk, "/a", cb, ctx); + synchronized (ctx) { + ctx.wait(); + } + assertEquals(4, ctx.get()); + } + + @Test + public void testStatWhenPathDoesNotExist() throws IOException, InterruptedException, MalformedCommandException { + final ZooKeeper zk = createClient(); + ZooKeeperMain main = new ZooKeeperMain(zk); + String cmdstring = "stat /invalidPath"; + main.cl.parseCommand(cmdstring); + try { + main.processZKCmd(main.cl); + fail("As Node does not exist, command should fail by throwing No Node Exception."); + } catch (CliException e) { + assertEquals("Node does not exist: /invalidPath", e.getMessage()); + } + } + + @Test + public void testParseWithExtraSpaces() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring = " ls / "; + zkMain.cl.parseCommand(cmdstring); + assertEquals("Spaces also considered as characters", zkMain.cl.getNumArguments(), 2); + assertEquals("ls is not taken as first argument", zkMain.cl.getCmdArgument(0), "ls"); + assertEquals("/ is not taken as second argument", zkMain.cl.getCmdArgument(1), "/"); + } + + @Test + public void testParseWithQuotes() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + for (String quoteChar : new String[]{"'", "\""}) { + String cmdstring = String.format("create /node %1$squoted data%1$s", quoteChar); + zkMain.cl.parseCommand(cmdstring); + assertEquals("quotes combine arguments", zkMain.cl.getNumArguments(), 3); + assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + assertEquals("quoted data is not taken as third argument", zkMain.cl.getCmdArgument(2), "quoted data"); + } + } + + @Test + public void testParseWithMixedQuotes() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + for (String[] quoteChars : new String[][]{{"'", "\""}, {"\"", "'"}}) { + String outerQuotes = quoteChars[0]; + String innerQuotes = quoteChars[1]; + String cmdstring = String.format("create /node %1$s%2$squoted data%2$s%1$s", outerQuotes, innerQuotes); + zkMain.cl.parseCommand(cmdstring); + assertEquals("quotes combine arguments", zkMain.cl.getNumArguments(), 3); + assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + assertEquals( + "quoted data is not taken as third argument", + zkMain.cl.getCmdArgument(2), + innerQuotes + "quoted data" + innerQuotes); + } + } + + @Test + public void testParseWithEmptyQuotes() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring = "create /node ''"; + zkMain.cl.parseCommand(cmdstring); + assertEquals("empty quotes should produce arguments", zkMain.cl.getNumArguments(), 3); + assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + assertEquals("empty string is not taken as third argument", zkMain.cl.getCmdArgument(2), ""); + } + + @Test + public void testParseWithMultipleQuotes() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring = "create /node '' ''"; + zkMain.cl.parseCommand(cmdstring); + assertEquals("expected 5 arguments", zkMain.cl.getNumArguments(), 4); + assertEquals("create is not taken as first argument", zkMain.cl.getCmdArgument(0), "create"); + assertEquals("/node is not taken as second argument", zkMain.cl.getCmdArgument(1), "/node"); + assertEquals("empty string is not taken as third argument", zkMain.cl.getCmdArgument(2), ""); + assertEquals("empty string is not taken as fourth argument", zkMain.cl.getCmdArgument(3), ""); + } + + @Test + public void testNonexistantCommand() throws Exception { + testInvalidCommand("cret -s /node1", 127); + } + + @Test + public void testCreateCommandWithoutPath() throws Exception { + testInvalidCommand("create", 1); + } + + @Test + public void testCreateEphemeralCommandWithoutPath() throws Exception { + testInvalidCommand("create -e ", 1); + } + + @Test + public void testCreateSequentialCommandWithoutPath() throws Exception { + testInvalidCommand("create -s ", 1); + } + + @Test + public void testCreateEphemeralSequentialCommandWithoutPath() throws Exception { + testInvalidCommand("create -s -e ", 1); + } + + private void testInvalidCommand(String cmdString, int exitCode) throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + zkMain.cl.parseCommand(cmdString); + + // Verify that the exit code is set properly + zkMain.processCmd(zkMain.cl); + assertEquals(exitCode, zkMain.exitCode); + + // Verify that the correct exception is thrown + try { + zkMain.processZKCmd(zkMain.cl); + fail(); + } catch (CliException e) { + return; + } + fail("invalid command should throw CliException"); + } + + @Test + public void testCreateNodeWithoutData() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + // create persistent sequential node + String cmdstring = "create -s /node "; + zkMain.cl.parseCommand(cmdstring); + assertTrue("Doesn't create node without data", zkMain.processZKCmd(zkMain.cl)); + // create ephemeral node + cmdstring = "create -e /node "; + zkMain.cl.parseCommand(cmdstring); + assertTrue("Doesn't create node without data", zkMain.processZKCmd(zkMain.cl)); + // create ephemeral sequential node + cmdstring = "create -s -e /node "; + zkMain.cl.parseCommand(cmdstring); + assertTrue("Doesn't create node without data", zkMain.processZKCmd(zkMain.cl)); + // creating ephemeral with wrong option. + cmdstring = "create -s y /node"; + zkMain.cl.parseCommand(cmdstring); + try { + assertTrue("Created node with wrong option", zkMain.processZKCmd(zkMain.cl)); + fail("Created the node with wrong option should " + "throw Exception."); + } catch (MalformedPathException e) { + assertEquals("Path must start with / character", e.getMessage()); + } + } + + @Test + public void testACLWithExtraAgruments() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + // create persistent sequential node + String cmdstring = "create -s /l data ip:10.18.52.144:cdrwa f g h"; + zkMain.cl.parseCommand(cmdstring); + assertTrue("Not considering the extra arguments after the acls.", zkMain.processZKCmd(zkMain.cl)); + } + + @Test + public void testCreatePersistentNode() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring = "create /node2"; + zkMain.cl.parseCommand(cmdstring); + assertTrue("Not creating Persistent node.", zkMain.processZKCmd(zkMain.cl)); + } + + @Test + public void testDelete() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring1 = "create -e /node2 data"; + String cmdstring2 = "delete /node2"; + String cmdstring3 = "ls /node2"; + zkMain.cl.parseCommand(cmdstring1); + assertTrue(zkMain.processZKCmd(zkMain.cl)); + zkMain.cl.parseCommand(cmdstring2); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + zkMain.cl.parseCommand(cmdstring3); + assertFalse("", zkMain.processCmd(zkMain.cl)); + } + + @Test + public void testDeleteNonexistantNode() throws Exception { + testInvalidCommand("delete /blahblahblah", 1); + } + + @Test + public void testStatCommand() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring1 = "create -e /node3 data"; + String cmdstring2 = "stat /node3"; + String cmdstring3 = "delete /node3"; + zkMain.cl.parseCommand(cmdstring1); + assertTrue(zkMain.processZKCmd(zkMain.cl)); + zkMain.cl.parseCommand(cmdstring2); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + zkMain.cl.parseCommand(cmdstring3); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + } + + @Test + public void testInvalidStatCommand() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + // node doesn't exists + String cmdstring1 = "stat /node123"; + zkMain.cl.parseCommand(cmdstring1); + try { + assertFalse(zkMain.processZKCmd(zkMain.cl)); + fail("Path doesn't exists so, command should fail."); + } catch (CliWrapperException e) { + assertEquals(KeeperException.Code.NONODE, ((KeeperException) e.getCause()).code()); + } + } + + @Test + public void testSetData() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring1 = "create -e /node4 data"; + String cmdstring2 = "set /node4 " + "data"; + String cmdstring3 = "delete /node4"; + Stat stat = new Stat(); + int version = 0; + zkMain.cl.parseCommand(cmdstring1); + assertTrue(zkMain.processZKCmd(zkMain.cl)); + stat = zk.exists("/node4", true); + version = stat.getVersion(); + zkMain.cl.parseCommand(cmdstring2); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + stat = zk.exists("/node4", true); + assertEquals(version + 1, stat.getVersion()); + zkMain.cl.parseCommand(cmdstring3); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + } + + @Test + public void testCheckInvalidAcls() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring = "create -s -e /node data ip:scheme:gggsd"; //invalid acl's + + // For Invalid ACls should not throw exception + zkMain.executeLine(cmdstring); + } + + @Test + public void testDeleteWithInvalidVersionNo() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmdstring = "create -s -e /node1 data "; + String cmdstring1 = "delete /node1 2"; //invalid dataversion no + zkMain.executeLine(cmdstring); + + // For Invalid dataversion number should not throw exception + zkMain.executeLine(cmdstring1); + } + + @Test + public void testCliCommandsNotEchoingUsage() throws Exception { + // setup redirect out/err streams to get System.in/err, use this judiciously! + final PrintStream systemErr = System.err; // get current err + final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + System.setErr(new PrintStream(errContent)); + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmd1 = "printwatches"; + zkMain.executeLine(cmd1); + String cmd2 = "history"; + zkMain.executeLine(cmd2); + String cmd3 = "redo"; + zkMain.executeLine(cmd3); + // revert redirect of out/err streams - important step! + System.setErr(systemErr); + if (errContent.toString().contains("ZooKeeper -server host:port cmd args")) { + fail("CLI commands (history, redo, connect, printwatches) display usage info!"); + } + } + + // ZOOKEEPER-2467 : Testing negative number for redo command + @Test + public void testRedoWithNegativeCmdNumber() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String cmd1 = "redo -1"; + String result = executeLine(zkMain, cmd1); + assertEquals("Command index out of range", result); + } + + private String executeLine(ZooKeeperMain zkMain, String cmd) + throws InterruptedException, IOException { + // setup redirect out/err streams to get System.in/err, use this + // judiciously! + final PrintStream systemErr = System.err; // get current err + final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + System.setErr(new PrintStream(errContent)); + try { + zkMain.executeLine(cmd); + return errContent.toString().trim(); + } finally { + // revert redirect of out/err streams - important step! + System.setErr(systemErr); + } + } + + private static void runCommandExpect(CliCommand command, List expectedResults) throws Exception { + // call command and put result in byteStream + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + PrintStream out = new PrintStream(byteStream); + command.setOut(out); + command.exec(); + + String result = byteStream.toString(); + assertTrue(result, result.contains(StringUtils.joinStrings(expectedResults, LINE_SEPARATOR))); + } + + @Test + public void testSortedLs() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + + zkMain.executeLine("create /aa1"); + zkMain.executeLine("create /aa2"); + zkMain.executeLine("create /aa3"); + zkMain.executeLine("create /test1"); + zkMain.executeLine("create /zk1"); + + LsCommand cmd = new LsCommand(); + cmd.setZk(zk); + cmd.parse("ls /".split(" ")); + List expected = new ArrayList(); + expected.add("[aa1, aa2, aa3, test1, zk1, zookeeper]"); + runCommandExpect(cmd, expected); + } + + @Test + public void testLsrCommand() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + + zkMain.executeLine("create /a"); + zkMain.executeLine("create /a/b"); + zkMain.executeLine("create /a/c"); + zkMain.executeLine("create /a/b/d"); + zkMain.executeLine("create /a/c/e"); + zkMain.executeLine("create /a/f"); + + LsCommand cmd = new LsCommand(); + cmd.setZk(zk); + cmd.parse("ls -R /a".split(" ")); + + List expected = new ArrayList(); + expected.add("/a"); + expected.add("/a/b"); + expected.add("/a/c"); + expected.add("/a/f"); + expected.add("/a/b/d"); + expected.add("/a/c/e"); + runCommandExpect(cmd, expected); + } + + @Test + public void testLsrRootCommand() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + + LsCommand cmd = new LsCommand(); + cmd.setZk(zk); + cmd.parse("ls -R /".split(" ")); + + List expected = new ArrayList(); + expected.add("/"); + expected.add("/zookeeper"); + runCommandExpect(cmd, expected); + } + + @Test + public void testLsrLeafCommand() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + + zkMain.executeLine("create /b"); + zkMain.executeLine("create /b/c"); + + LsCommand cmd = new LsCommand(); + cmd.setZk(zk); + cmd.parse("ls -R /b/c".split(" ")); + + List expected = new ArrayList(); + expected.add("/b/c"); + runCommandExpect(cmd, expected); + } + + @Test + public void testLsrNonexistantZnodeCommand() throws Exception { + final ZooKeeper zk = createClient(); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + + zkMain.executeLine("create /b"); + zkMain.executeLine("create /b/c"); + + LsCommand cmd = new LsCommand(); + cmd.setZk(zk); + cmd.parse("ls -R /b/c/d".split(" ")); + + try { + runCommandExpect(cmd, new ArrayList()); + fail("Path doesn't exists so, command should fail."); + } catch (CliWrapperException e) { + assertEquals(KeeperException.Code.NONODE, ((KeeperException) e.getCause()).code()); + } + } + + @Test + public void testSetAclRecursive() throws Exception { + final ZooKeeper zk = createClient(); + final byte[] EMPTY = new byte[0]; + + zk.setData("/", EMPTY, -1); + zk.create("/a", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/d", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/e", EMPTY, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String setAclCommand = "setAcl -R /a world:anyone:r"; + zkMain.cl.parseCommand(setAclCommand); + assertFalse(zkMain.processZKCmd(zkMain.cl)); + + assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a", new Stat())); + assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a/b", new Stat())); + assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a/b/c", new Stat())); + assertEquals(Ids.READ_ACL_UNSAFE, zk.getACL("/a/d", new Stat())); + // /e is unset, its acl should remain the same. + assertEquals(Ids.OPEN_ACL_UNSAFE, zk.getACL("/e", new Stat())); + } + + @Test + public void testClientReconnectWithZKClientConfig() throws Exception { + ZooKeeper zk = null; + ZooKeeper newZKClient = null; + try { + zk = createClient(); + ZKClientConfig clientConfig = new ZKClientConfig(); + clientConfig.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + CountdownWatcher watcher = new CountdownWatcher(); + HostProvider aHostProvider = new StaticHostProvider(new ConnectStringParser(hostPort).getServerAddresses()); + newZKClient = new ZooKeeper( + hostPort, + zk.getSessionTimeout(), + watcher, + zk.getSessionId(), + zk.getSessionPasswd(), + false, + aHostProvider, + clientConfig); + watcher.waitForConnected(CONNECTION_TIMEOUT); + assertEquals("Old client session id and new clinet session id must be same", zk.getSessionId(), newZKClient.getSessionId()); + } finally { + zk.close(); + newZKClient.close(); + } + } + + @Test + public void testSyncCommand() throws Exception { + final ZooKeeper zk = createClient(); + SyncCommand cmd = new SyncCommand(); + cmd.setZk(zk); + cmd.parse("sync /".split(" ")); + List expected = new ArrayList(); + expected.add("Sync is OK"); + + runCommandExpect(cmd, expected); + } + + @Test + public void testInsufficientPermission() throws Exception { + final ZooKeeper zk = createClient(); + zk.create("/permZNode", "".getBytes(), Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT); + ZooKeeperMain zkMain = new ZooKeeperMain(zk); + String zNodeToBeCreated = "/permZNode/child1"; + String errorMessage = executeLine(zkMain, "create " + zNodeToBeCreated); + assertEquals("Insufficient permission : " + zNodeToBeCreated, errorMessage); + + // Test Get command error message when there is not read access + List writeAcl = Arrays.asList(new ACL(ZooDefs.Perms.WRITE, Ids.ANYONE_ID_UNSAFE)); + String noReadPermZNodePath = "/noReadPermZNode"; + zk.create(noReadPermZNodePath, "newData".getBytes(), writeAcl, CreateMode.PERSISTENT); + errorMessage = executeLine(zkMain, "get " + noReadPermZNodePath); + assertEquals("Insufficient permission : " + noReadPermZNodePath, errorMessage); + } + + @Test + public void testWaitForConnection() throws Exception { + // get a wrong port number + int invalidPort = PortAssignment.unique(); + long timeout = 3000L; // millisecond + String[] args1 = {"-server", "localhost:" + invalidPort, "-timeout", + Long.toString(timeout), "-waitforconnection", "ls", "/"}; + long startTime = System.currentTimeMillis(); + // try to connect to a non-existing server so as to wait until waitTimeout + try { + ZooKeeperMain zkMain = new ZooKeeperMain(args1); + fail("IOException was expected"); + } catch (IOException e) { + // do nothing + } + long endTime = System.currentTimeMillis(); + assertTrue("ZooKeeperMain does not wait until the specified timeout", + endTime - startTime >= timeout); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/audit/AuditEventTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/AuditEventTest.java new file mode 100644 index 0000000..02d9ac0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/AuditEventTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import static org.junit.Assert.assertEquals; +import org.apache.zookeeper.audit.AuditEvent.Result; +import org.junit.Test; + +public class AuditEventTest { + + @Test + public void testFormat() { + AuditEvent auditEvent = new AuditEvent(Result.SUCCESS); + auditEvent.addEntry(AuditEvent.FieldName.USER, "Value1"); + auditEvent.addEntry(AuditEvent.FieldName.OPERATION, "Value2"); + String actual = auditEvent.toString(); + String expected = "user=Value1\toperation=Value2\tresult=success"; + assertEquals(expected, actual); + } + + @Test + public void testFormatShouldIgnoreKeyIfValueIsNull() { + AuditEvent auditEvent = new AuditEvent(Result.SUCCESS); + auditEvent.addEntry(AuditEvent.FieldName.USER, null); + auditEvent.addEntry(AuditEvent.FieldName.OPERATION, "Value2"); + String actual = auditEvent.toString(); + String expected = "operation=Value2\tresult=success"; + assertEquals(expected, actual); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/audit/AuditLogPerfReading.java b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/AuditLogPerfReading.java new file mode 100644 index 0000000..38ef96c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/AuditLogPerfReading.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +/** + * Audit log performance reading + */ +public final class AuditLogPerfReading { + // time taken by create operations + private long create; + // time taken by setData operations + private long setData; + // time taken by delete operations + private long delete; + + public long getCreate() { + return create; + } + + public void setCreate(long create) { + this.create = create; + } + + public long getSetData() { + return setData; + } + + public void setSetData(long setData) { + this.setData = setData; + } + + public long getDelete() { + return delete; + } + + public void setDelete(long delete) { + this.delete = delete; + } + + public String report() { + StringBuilder builder = new StringBuilder(); + builder.append("create="); + builder.append(create); + builder.append(" ms\n"); + builder.append("setData="); + builder.append(setData); + builder.append(" ms\n"); + builder.append("delete="); + builder.append(delete); + builder.append(" ms\n"); + return builder.toString(); + } + + @Override + public String toString() { + return "create=" + create + ", setData=" + setData + ", delete=" + + delete; + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/audit/Log4jAuditLoggerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/Log4jAuditLoggerTest.java new file mode 100644 index 0000000..0c40811 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/Log4jAuditLoggerTest.java @@ -0,0 +1,442 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.List; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.apache.log4j.WriterAppender; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKUtil; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.audit.AuditEvent.Result; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + + +public class Log4jAuditLoggerTest extends QuorumPeerTestBase { + private static final Logger LOG = Logger.getLogger(Log4jAuditLoggerTest.class); + private static int SERVER_COUNT = 3; + private static MainThread[] mt; + private static ZooKeeper zk; + private static Logger zlogger; + private static WriterAppender appender; + private static ByteArrayOutputStream os; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.setProperty(ZKAuditProvider.AUDIT_ENABLE, "true"); + // setup the logger to capture all logs + Layout layout = new SimpleLayout(); + os = new ByteArrayOutputStream(); + appender = new WriterAppender(layout, os); + appender.setImmediateFlush(true); + appender.setThreshold(Level.INFO); + zlogger = Logger.getLogger(Log4jAuditLogger.class); + zlogger.addAppender(appender); + mt = startQuorum(); + zk = ClientBase.createZKClient("127.0.0.1:" + mt[0].getQuorumPeer().getClientPort()); + //Verify start audit log here itself + String expectedAuditLog = getStartLog(); + List logs = readAuditLog(os, SERVER_COUNT); + verifyLogs(expectedAuditLog, logs); + } + + @Before + public void setUp() { + os.reset(); + } + + @Test + public void testCreateAuditLogs() + throws KeeperException, InterruptedException, IOException { + String path = "/createPath"; + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + // success log + String createMode = CreateMode.PERSISTENT.toString().toLowerCase(); + verifyLog( + getAuditLog(AuditConstants.OP_CREATE, path, Result.SUCCESS, + null, createMode), readAuditLog(os)); + try { + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + } catch (KeeperException exception) { + Code code = exception.code(); + assertEquals(Code.NODEEXISTS, code); + } + // Verify create operation log + verifyLog( + getAuditLog(AuditConstants.OP_CREATE, path, Result.FAILURE, + null, createMode), readAuditLog(os)); + } + + @Test + public void testDeleteAuditLogs() + throws InterruptedException, IOException, KeeperException { + String path = "/deletePath"; + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + os.reset(); + try { + zk.delete(path, -100); + } catch (KeeperException exception) { + Code code = exception.code(); + assertEquals(Code.BADVERSION, code); + } + verifyLog(getAuditLog(AuditConstants.OP_DELETE, path, + Result.FAILURE), + readAuditLog(os)); + zk.delete(path, -1); + verifyLog(getAuditLog(AuditConstants.OP_DELETE, path), + readAuditLog(os)); + } + + @Test + public void testSetDataAuditLogs() + throws InterruptedException, IOException, KeeperException { + String path = "/setDataPath"; + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + os.reset(); + try { + zk.setData(path, "newData".getBytes(), -100); + } catch (KeeperException exception) { + Code code = exception.code(); + assertEquals(Code.BADVERSION, code); + } + verifyLog(getAuditLog(AuditConstants.OP_SETDATA, path, + Result.FAILURE), + readAuditLog(os)); + zk.setData(path, "newdata".getBytes(), -1); + verifyLog(getAuditLog(AuditConstants.OP_SETDATA, path), + readAuditLog(os)); + } + + @Test + public void testSetACLAuditLogs() + throws InterruptedException, IOException, KeeperException { + ArrayList openAclUnsafe = ZooDefs.Ids.OPEN_ACL_UNSAFE; + String path = "/aclPath"; + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + os.reset(); + try { + zk.setACL(path, openAclUnsafe, -100); + } catch (KeeperException exception) { + Code code = exception.code(); + assertEquals(Code.BADVERSION, code); + } + verifyLog( + getAuditLog(AuditConstants.OP_SETACL, path, Result.FAILURE, + ZKUtil.aclToString(openAclUnsafe), null), readAuditLog(os)); + zk.setACL(path, openAclUnsafe, -1); + verifyLog( + getAuditLog(AuditConstants.OP_SETACL, path, Result.SUCCESS, + ZKUtil.aclToString(openAclUnsafe), null), readAuditLog(os)); + } + + @Test + public void testMultiOperationAuditLogs() + throws InterruptedException, KeeperException, IOException { + List ops = new ArrayList<>(); + + String multiop = "/b"; + Op create = Op.create(multiop, "".getBytes(), + ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + Op setData = Op.setData(multiop, "newData".getBytes(), -1); + // check does nothing so it is audit logged + Op check = Op.check(multiop, -1); + Op delete = Op.delete(multiop, -1); + + String createMode = CreateMode.PERSISTENT.toString().toLowerCase(); + + ops.add(create); + ops.add(setData); + ops.add(check); + ops.add(delete); + + zk.multi(ops); + List multiOpLogs = readAuditLog(os, 3); + // verify that each multi operation success is logged + verifyLog(getAuditLog(AuditConstants.OP_CREATE, multiop, + Result.SUCCESS, null, createMode), + multiOpLogs.get(0)); + verifyLog(getAuditLog(AuditConstants.OP_SETDATA, multiop), + multiOpLogs.get(1)); + verifyLog(getAuditLog(AuditConstants.OP_DELETE, multiop), + multiOpLogs.get(2)); + + ops = new ArrayList<>(); + ops.add(create); + ops.add(create); + try { + zk.multi(ops); + } catch (KeeperException exception) { + Code code = exception.code(); + assertEquals(Code.NODEEXISTS, code); + } + + // Verify that multi operation failure is logged, and there is no path + // mentioned in the audit log + verifyLog(getAuditLog(AuditConstants.OP_MULTI_OP, null, + Result.FAILURE), + readAuditLog(os)); + } + + @Test + public void testEphemralZNodeAuditLogs() + throws Exception { + String ephemralPath = "/ephemral"; + CountdownWatcher watcher2 = new CountdownWatcher(); + ZooKeeper zk2 = new ZooKeeper( + "127.0.0.1:" + mt[0].getQuorumPeer().getClientPort(), + ClientBase.CONNECTION_TIMEOUT, watcher2); + watcher2.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk2.create(ephemralPath, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL); + String session2 = "0x" + Long.toHexString(zk2.getSessionId()); + verifyLog(getAuditLog(AuditConstants.OP_CREATE, ephemralPath, + Result.SUCCESS, null, + CreateMode.EPHEMERAL.toString().toLowerCase(), + session2), readAuditLog(os)); + zk2.close(); + waitForDeletion(zk, ephemralPath); + // verify that ephemeral node deletion on session close are captured + // in audit log + // Because these operations are done by ZooKeeper server itself, + // there are no IP user is zkServer user, not any client user + verifyLogs(getAuditLog(AuditConstants.OP_DEL_EZNODE_EXP, ephemralPath, + Result.SUCCESS, null, null, session2, + ZKAuditProvider.getZKUser(), null), readAuditLog(os, SERVER_COUNT)); + } + + + private static String getStartLog() { + // user=userName operation=ZooKeeperServer start result=success + AuditEvent logEvent = ZKAuditProvider.createLogEvent(ZKAuditProvider.getZKUser(), + AuditConstants.OP_START, Result.SUCCESS); + return logEvent.toString(); + } + + private String getAuditLog(String operation, String znode) { + return getAuditLog(operation, znode, Result.SUCCESS); + } + + private String getAuditLog(String operation, String znode, Result result) { + return getAuditLog(operation, znode, result, null, null); + } + + private String getAuditLog(String operation, String znode, Result result, + String acl, String createMode) { + String session = getSession(); + return getAuditLog(operation, znode, result, acl, createMode, session); + } + + private String getAuditLog(String operation, String znode, Result result, + String acl, String createMode, String session) { + String user = getUser(); + String ip = getIp(); + return getAuditLog(operation, znode, result, acl, createMode, session, + user, ip); + } + + private String getAuditLog(String operation, String znode, Result result, + String acl, String createMode, String session, String user, String ip) { + AuditEvent logEvent = ZKAuditProvider.createLogEvent(user, operation, znode, acl, createMode, session, ip, + result); + String auditLog = logEvent.toString(); + LOG.info("expected audit log for operation '" + operation + "' is '" + + auditLog + "'"); + return auditLog; + } + + private String getSession() { + return "0x" + Long.toHexString(zk.getSessionId()); + } + + private String getUser() { + ServerCnxn next = getServerCnxn(); + Request request = new Request(next, -1, -1, -1, null, + next.getAuthInfo()); + return request.getUsers(); + } + + private String getIp() { + ServerCnxn next = getServerCnxn(); + InetSocketAddress remoteSocketAddress = next.getRemoteSocketAddress(); + InetAddress address = remoteSocketAddress.getAddress(); + return address.getHostAddress(); + } + + private ServerCnxn getServerCnxn() { + Iterable connections = mt[0].getQuorumPeer() + .getActiveServer() + .getServerCnxnFactory().getConnections(); + return connections.iterator().next(); + } + + private static void verifyLog(String expectedLog, String log) { + String searchString = " - "; + int logStartIndex = log.indexOf(searchString); + String auditLog = log.substring(logStartIndex + searchString.length()); + assertEquals(expectedLog, auditLog); + + } + + private static void verifyLogs(String expectedLog, List logs) { + for (String log : logs) { + verifyLog(expectedLog, log); + } + } + + private String readAuditLog(ByteArrayOutputStream os) throws IOException { + return readAuditLog(os, 1).get(0); + } + + private static List readAuditLog(ByteArrayOutputStream os, + int numberOfLogEntry) + throws IOException { + return readAuditLog(os, numberOfLogEntry, false); + } + + private static List readAuditLog(ByteArrayOutputStream os, + int numberOfLogEntry, + boolean skipEphemralDeletion) throws IOException { + List logs = new ArrayList<>(); + LineNumberReader r = new LineNumberReader( + new StringReader(os.toString())); + String line; + while ((line = r.readLine()) != null) { + if (skipEphemralDeletion + && line.contains(AuditConstants.OP_DEL_EZNODE_EXP)) { + continue; + } + logs.add(line); + } + os.reset(); + assertEquals( + "Expected number of log entries are not generated. Logs are " + + logs, + numberOfLogEntry, logs.size()); + return logs; + + } + + private static MainThread[] startQuorum() throws IOException { + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + sb.append("4lw.commands.whitelist=*"); + sb.append("\n"); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + + ":" + + PortAssignment.unique() + ":participant;127.0.0.1:" + + clientPorts[i]; + sb.append(server); + sb.append("\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread[] mt = new MainThread[SERVER_COUNT]; + + // start all the servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, + false); + mt[i].start(); + } + + // ensure all servers started + for (int i = 0; i < SERVER_COUNT; i++) { + Assert.assertTrue("waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], + CONNECTION_TIMEOUT)); + } + return mt; + } + + private void waitForDeletion(ZooKeeper zooKeeper, String path) + throws Exception { + long elapsedTime = 0; + long waitInterval = 10; + int timeout = 100; + Stat exists = zooKeeper.exists(path, false); + while (exists != null && elapsedTime < timeout) { + try { + Thread.sleep(waitInterval); + } catch (InterruptedException e) { + Assert.fail("CurrentEpoch update failed"); + } + elapsedTime = elapsedTime + waitInterval; + exists = zooKeeper.exists(path, false); + } + Assert.assertNull("Node " + path + " not deleted in " + timeout + " ms", + exists); + } + + @AfterClass + public static void tearDownAfterClass() { + System.clearProperty(ZKAuditProvider.AUDIT_ENABLE); + for (int i = 0; i < SERVER_COUNT; i++) { + try { + if (mt[i] != null) { + mt[i].shutdown(); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + try { + zlogger.removeAppender(appender); + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/audit/StandaloneServerAuditTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/StandaloneServerAuditTest.java new file mode 100644 index 0000000..7aed4c6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/StandaloneServerAuditTest.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.SimpleLayout; +import org.apache.log4j.WriterAppender; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + + + +public class StandaloneServerAuditTest extends ClientBase { + private static ByteArrayOutputStream os; + + @BeforeClass + public static void setup() { + System.setProperty(ZKAuditProvider.AUDIT_ENABLE, "true"); + // setup the logger to capture all the logs + Layout layout = new SimpleLayout(); + os = new ByteArrayOutputStream(); + WriterAppender appender = new WriterAppender(layout, os); + appender.setImmediateFlush(true); + appender.setThreshold(Level.INFO); + Logger zLogger = Logger.getLogger(Log4jAuditLogger.class); + zLogger.addAppender(appender); + } + + @AfterClass + public static void teardown() { + System.clearProperty(ZKAuditProvider.AUDIT_ENABLE); + } + + @Test + public void testCreateAuditLog() throws KeeperException, InterruptedException, IOException { + final ZooKeeper zk = createClient(); + String path = "/createPath"; + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + List logs = readAuditLog(os); + assertEquals(1, logs.size()); + assertTrue(logs.get(0).endsWith("operation=create\tznode=/createPath\tznode_type=persistent\tresult=success")); + } + + private static List readAuditLog(ByteArrayOutputStream os) throws IOException { + List logs = new ArrayList<>(); + LineNumberReader r = new LineNumberReader( + new StringReader(os.toString())); + String line; + while ((line = r.readLine()) != null) { + logs.add(line); + } + os.reset(); + return logs; + } +} + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/audit/ZKAuditLoggerPerformance.java b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/ZKAuditLoggerPerformance.java new file mode 100644 index 0000000..da64197 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/audit/ZKAuditLoggerPerformance.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.audit; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZKAuditLoggerPerformance { + private static final Logger LOG = LoggerFactory + .getLogger(ZKAuditLoggerPerformance.class); + private ZooKeeper zkClient; + private String parentPath; + private int numberOfRecords; + + public ZKAuditLoggerPerformance(ZooKeeper zkClient, String parentPath, + int numberOfRecords) { + this.zkClient = zkClient; + this.parentPath = parentPath; + this.numberOfRecords = numberOfRecords; + } + + public void create() throws Exception { + for (int i = 0; i < numberOfRecords; i++) { + zkClient.create(getPath(i), "0123456789".getBytes(), + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + + } + } + + public void setData() throws Exception { + for (int i = 0; i < numberOfRecords; i++) { + zkClient.setData(getPath(i), "9876543210".getBytes(), -1); + } + } + + public void delete() throws Exception { + for (int i = 0; i < numberOfRecords; i++) { + zkClient.delete(getPath(i), -1); + } + } + + public AuditLogPerfReading doOperations() throws Exception { + AuditLogPerfReading perfReading = new AuditLogPerfReading(); + // create + long startTime = Time.currentElapsedTime(); + create(); + perfReading.setCreate(Time.currentElapsedTime() - startTime); + + // setData + startTime = Time.currentElapsedTime(); + setData(); + perfReading.setSetData(Time.currentElapsedTime() - startTime); + + // delete + startTime = Time.currentElapsedTime(); + delete(); + perfReading.setDelete(Time.currentElapsedTime() - startTime); + return perfReading; + } + + private String getPath(int i) { + return parentPath + "zNode" + i; + } + + public static void main(String[] args) { + if (args.length != 3) { + System.err.println( + "USAGE: ZKAuditLoggerPerformance connectionString parentPath numberOfRecords"); + System.exit(1); + } + String cxnString = args[0]; + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zkClient = null; + try { + zkClient = new ZooKeeper(cxnString, 60000, watcher); + watcher.waitForConnected(30000); + } catch (InterruptedException | TimeoutException | IOException e) { + String msg = "ZooKeeper client can not connect to " + cxnString; + logErrorAndExit(e, msg); + } + String parentPath = args[1]; + try { + Stat exists = zkClient.exists(parentPath, false); + if (exists == null) { + System.err.println( + "Parent path '" + parentPath + "' must exist."); + System.exit(1); + } + } catch (KeeperException | InterruptedException e1) { + String msg = "Error while checking the existence of parent path"; + logErrorAndExit(e1, msg); + } + int recordCount = 0; + try { + recordCount = Integer.parseInt(args[2]); + } catch (NumberFormatException e) { + String msg = "Failed to parse '" + args[2] + "' to integer"; + LOG.error(msg, e); + System.err.println(msg); + System.exit(1); + } + ZKAuditLoggerPerformance auditLoggingPerf = new ZKAuditLoggerPerformance( + zkClient, + parentPath, recordCount); + AuditLogPerfReading doOperations = null; + try { + doOperations = auditLoggingPerf.doOperations(); + } catch (Exception e) { + String msg = "Error while doing operations."; + LOG.error(msg, e); + System.err.println(msg); + System.exit(1); + } + System.out + .println("Time taken for " + recordCount + " operations are:"); + System.out.println(doOperations.report()); + System.exit(0); + } + + private static void logErrorAndExit(Exception e, String msg) { + LOG.error(msg, e); + System.err.println(msg + ", error=" + e.getMessage()); + System.exit(1); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/client/ZKClientConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/client/ZKClientConfigTest.java new file mode 100644 index 0000000..9a9b622 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/client/ZKClientConfigTest.java @@ -0,0 +1,213 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.client; + +import static org.apache.zookeeper.client.ZKClientConfig.DISABLE_AUTO_WATCH_RESET; +import static org.apache.zookeeper.client.ZKClientConfig.ENABLE_CLIENT_SASL_KEY; +import static org.apache.zookeeper.client.ZKClientConfig.LOGIN_CONTEXT_NAME_KEY; +import static org.apache.zookeeper.client.ZKClientConfig.SECURE_CLIENT; +import static org.apache.zookeeper.client.ZKClientConfig.ZK_SASL_CLIENT_USERNAME; +import static org.apache.zookeeper.client.ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET; +import static org.apache.zookeeper.client.ZKClientConfig.ZOOKEEPER_SERVER_REALM; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.common.ZKConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class ZKClientConfigTest { + + private static final File testData = new File(System.getProperty("test.data.dir", "src/test/resources/data")); + @Rule + public Timeout timeout = new Timeout(10, TimeUnit.SECONDS); + + @BeforeClass + public static void init() { + if (!testData.exists()) { + testData.mkdirs(); + } + } + + @Test + public void testDefaultConfiguration() { + Map properties = new HashMap<>(); + properties.put(ZK_SASL_CLIENT_USERNAME, "zookeeper1"); + properties.put(LOGIN_CONTEXT_NAME_KEY, "Client1"); + properties.put(ENABLE_CLIENT_SASL_KEY, "true"); + properties.put(ZOOKEEPER_SERVER_REALM, "zookeeper/hadoop.hadoop.com"); + properties.put(DISABLE_AUTO_WATCH_RESET, "true"); + properties.put(ZOOKEEPER_CLIENT_CNXN_SOCKET, "ClientCnxnSocketNetty"); + properties.put(SECURE_CLIENT, "true"); + + for (Map.Entry e : properties.entrySet()) { + System.setProperty(e.getKey(), e.getValue()); + } + /** + * ZKClientConfig should get initialized with system properties + */ + ZKClientConfig conf = new ZKClientConfig(); + for (Map.Entry e : properties.entrySet()) { + assertEquals(e.getValue(), conf.getProperty(e.getKey())); + } + /** + * clear properties + */ + for (Map.Entry e : properties.entrySet()) { + System.clearProperty(e.getKey()); + } + + conf = new ZKClientConfig(); + /** + * test that all the properties are null + */ + for (Map.Entry e : properties.entrySet()) { + String result = conf.getProperty(e.getKey()); + assertNull(result); + } + } + + @Test + public void testSystemPropertyValue() { + String clientName = "zookeeper1"; + System.setProperty(ZK_SASL_CLIENT_USERNAME, clientName); + + ZKClientConfig conf = new ZKClientConfig(); + assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), clientName); + + String newClientName = "zookeeper2"; + conf.setProperty(ZK_SASL_CLIENT_USERNAME, newClientName); + + assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), newClientName); + } + + @Test + public void testReadConfigurationFile() throws IOException, ConfigException { + File file = File.createTempFile("clientConfig", ".conf", testData); + file.deleteOnExit(); + Properties clientConfProp = new Properties(); + clientConfProp.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); + clientConfProp.setProperty(ZK_SASL_CLIENT_USERNAME, "ZK"); + clientConfProp.setProperty(LOGIN_CONTEXT_NAME_KEY, "MyClient"); + clientConfProp.setProperty(ZOOKEEPER_SERVER_REALM, "HADOOP.COM"); + clientConfProp.setProperty("dummyProperty", "dummyValue"); + OutputStream io = new FileOutputStream(file); + try { + clientConfProp.store(io, "Client Configurations"); + } finally { + io.close(); + } + + ZKClientConfig conf = new ZKClientConfig(); + conf.addConfiguration(file.getAbsolutePath()); + assertEquals(conf.getProperty(ENABLE_CLIENT_SASL_KEY), "true"); + assertEquals(conf.getProperty(ZK_SASL_CLIENT_USERNAME), "ZK"); + assertEquals(conf.getProperty(LOGIN_CONTEXT_NAME_KEY), "MyClient"); + assertEquals(conf.getProperty(ZOOKEEPER_SERVER_REALM), "HADOOP.COM"); + assertEquals(conf.getProperty("dummyProperty"), "dummyValue"); + + // try to delete it now as we have done with the created file, why to + // wait for deleteOnExit() deletion + file.delete(); + + } + + @Test + public void testSetConfiguration() { + ZKClientConfig conf = new ZKClientConfig(); + String defaultValue = conf.getProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, ZKClientConfig.ENABLE_CLIENT_SASL_DEFAULT); + if (defaultValue.equals("true")) { + conf.setProperty(ENABLE_CLIENT_SASL_KEY, "false"); + } else { + conf.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); + } + assertTrue(conf.getProperty(ENABLE_CLIENT_SASL_KEY) != defaultValue); + } + + @Test + public void testIntegerRetrievalFromProperty() { + ZKClientConfig conf = new ZKClientConfig(); + String prop = "UnSetProperty" + System.currentTimeMillis(); + int defaultValue = 100; + // property is not set we should get the default value + int result = conf.getInt(prop, defaultValue); + assertEquals(defaultValue, result); + + // property is set but can not be parsed to int, we should get the + // NumberFormatException + conf.setProperty(ZKConfig.JUTE_MAXBUFFER, "InvlaidIntValue123"); + try { + result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); + fail("NumberFormatException is expected"); + } catch (NumberFormatException exception) { + // do nothing + } + assertEquals(defaultValue, result); + + // property is set to an valid int, we should get the set value + int value = ZKClientConfig.CLIENT_MAX_PACKET_LENGTH_DEFAULT; + conf.setProperty(ZKConfig.JUTE_MAXBUFFER, Integer.toString(value)); + result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); + assertEquals(value, result); + + // property is set but with white spaces + value = 12345; + conf.setProperty(ZKConfig.JUTE_MAXBUFFER, " " + value + " "); + result = conf.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); + assertEquals(value, result); + } + + @Test + public void testIntegerRetrievalFromHexadecimalProperty() { + int hexaValue = 0x3000000; + String wrongValue = "0xwel"; + int defaultValue = 100; + // property is set in hexadecimal value + ZKClientConfig zkClientConfig = new ZKClientConfig(); + zkClientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, + Integer.toString(hexaValue)); + int result = zkClientConfig.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); + assertEquals(result, hexaValue); + zkClientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, + wrongValue); + try { + result = zkClientConfig.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); + fail("NumberFormatException is expected"); + } catch (NumberFormatException exception) { + // do nothing + } + zkClientConfig.setProperty(ZKConfig.JUTE_MAXBUFFER, + " " + hexaValue + " "); + result = zkClientConfig.getInt(ZKConfig.JUTE_MAXBUFFER, defaultValue); + assertEquals(result, hexaValue); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/AtomicFileWritingIdiomTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/AtomicFileWritingIdiomTest.java new file mode 100644 index 0000000..d128ecc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/AtomicFileWritingIdiomTest.java @@ -0,0 +1,360 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.AtomicFileWritingIdiom.OutputStreamStatement; +import org.apache.zookeeper.common.AtomicFileWritingIdiom.WriterStatement; +import org.junit.BeforeClass; +import org.junit.Test; + +public class AtomicFileWritingIdiomTest extends ZKTestCase { + + private static File tmpdir; + + @BeforeClass + public static void createTmpDir() { + tmpdir = new File("build/test/tmp"); + tmpdir.mkdirs(); + } + + @Test + public void testOutputStreamSuccess() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + new AtomicFileWritingIdiom(target, new OutputStreamStatement() { + @Override + public void write(OutputStream os) throws IOException { + os.write("after".getBytes(StandardCharsets.US_ASCII)); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + } + }); + assertFalse("tmp file should have been deleted", tmp.exists()); + // content changed + assertEquals("after", getContent(target)); + target.delete(); + } + + @Test + public void testWriterSuccess() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + new AtomicFileWritingIdiom(target, new WriterStatement() { + @Override + public void write(Writer os) throws IOException { + os.write("after"); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + } + }); + assertFalse("tmp file should have been deleted", tmp.exists()); + // content changed + assertEquals("after", getContent(target)); + target.delete(); + } + + @Test + public void testOutputStreamFailure() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new OutputStreamStatement() { + @Override + public void write(OutputStream os) throws IOException { + os.write("after".getBytes(StandardCharsets.US_ASCII)); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new RuntimeException(); + } + }); + } catch (RuntimeException ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // content preserved + assertEquals("before", getContent(target)); + target.delete(); + } + + @Test + public void testWriterFailure() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new WriterStatement() { + @Override + public void write(Writer os) throws IOException { + os.write("after"); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new RuntimeException(); + } + }); + } catch (RuntimeException ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // content preserved + assertEquals("before", getContent(target)); + target.delete(); + } + + @Test + public void testOutputStreamFailureIOException() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new OutputStreamStatement() { + @Override + public void write(OutputStream os) throws IOException { + os.write("after".getBytes(StandardCharsets.US_ASCII)); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new IOException(); + } + }); + } catch (IOException ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // content preserved + assertEquals("before", getContent(target)); + target.delete(); + } + + @Test + public void testWriterFailureIOException() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new WriterStatement() { + @Override + public void write(Writer os) throws IOException { + os.write("after"); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new IOException(); + } + }); + } catch (IOException ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // content preserved + assertEquals("before", getContent(target)); + target.delete(); + } + + @Test + public void testOutputStreamFailureError() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new OutputStreamStatement() { + @Override + public void write(OutputStream os) throws IOException { + os.write("after".getBytes(StandardCharsets.US_ASCII)); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new Error(); + } + }); + } catch (Error ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // content preserved + assertEquals("before", getContent(target)); + target.delete(); + } + + @Test + public void testWriterFailureError() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + createFile(target, "before"); + assertEquals("before", getContent(target)); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new WriterStatement() { + @Override + public void write(Writer os) throws IOException { + os.write("after"); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new Error(); + } + }); + } catch (Error ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // content preserved + assertEquals("before", getContent(target)); + target.delete(); + } + + // ************** target file does not exist + + @Test + public void testOutputStreamSuccessNE() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + target.delete(); + assertFalse("file should not exist", target.exists()); + new AtomicFileWritingIdiom(target, new OutputStreamStatement() { + @Override + public void write(OutputStream os) throws IOException { + os.write("after".getBytes(StandardCharsets.US_ASCII)); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + } + }); + // content changed + assertEquals("after", getContent(target)); + target.delete(); + } + + @Test + public void testWriterSuccessNE() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + target.delete(); + assertFalse("file should not exist", target.exists()); + new AtomicFileWritingIdiom(target, new WriterStatement() { + @Override + public void write(Writer os) throws IOException { + os.write("after"); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + } + }); + assertFalse("tmp file should have been deleted", tmp.exists()); + // content changed + assertEquals("after", getContent(target)); + target.delete(); + } + + @Test + public void testOutputStreamFailureNE() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + target.delete(); + assertFalse("file should not exist", target.exists()); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new OutputStreamStatement() { + @Override + public void write(OutputStream os) throws IOException { + os.write("after".getBytes(StandardCharsets.US_ASCII)); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new RuntimeException(); + } + }); + } catch (RuntimeException ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // file should not exist + assertFalse("file should not exist", target.exists()); + } + + @Test + public void testWriterFailureNE() throws IOException { + File target = new File(tmpdir, "target.txt"); + final File tmp = new File(tmpdir, "target.txt.tmp"); + target.delete(); + assertFalse("file should not exist", target.exists()); + boolean exception = false; + try { + new AtomicFileWritingIdiom(target, new WriterStatement() { + @Override + public void write(Writer os) throws IOException { + os.write("after"); + os.flush(); + assertTrue("implementation of AtomicFileOutputStream has changed, update the test", tmp.exists()); + throw new RuntimeException(); + } + }); + } catch (RuntimeException ex) { + exception = true; + } + assertFalse("tmp file should have been deleted", tmp.exists()); + assertTrue("should have raised an exception", exception); + // file should not exist + assertFalse("file should not exist", target.exists()); + } + + private String getContent(File file, String encoding) throws IOException { + StringBuilder result = new StringBuilder(); + FileInputStream fis = new FileInputStream(file); + byte[] b = new byte[20]; + int nb; + while ((nb = fis.read(b)) != -1) { + result.append(new String(b, 0, nb, encoding)); + } + fis.close(); + return result.toString(); + } + + private String getContent(File file) throws IOException { + return getContent(file, "ASCII"); + } + + private void createFile(File file, String content) throws IOException { + FileOutputStream fos = new FileOutputStream(file); + fos.write(content.getBytes(StandardCharsets.US_ASCII)); + fos.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/BCFKSFileLoaderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/BCFKSFileLoaderTest.java new file mode 100644 index 0000000..c3ee498 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/BCFKSFileLoaderTest.java @@ -0,0 +1,160 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.IOException; +import java.security.KeyStore; +import java.util.Collection; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + + +@RunWith(Parameterized.class) +public class BCFKSFileLoaderTest extends BaseX509ParameterizedTestCase { + + + @Parameterized.Parameters + public static Collection params() { + return BaseX509ParameterizedTestCase.defaultParams(); + } + + public BCFKSFileLoaderTest( + final X509KeyType caKeyType, + final X509KeyType certKeyType, + final String keyPassword, + final Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStorePassword(keyPassword) + .setKeyStoreKeyType(certKeyType) + .setTrustStorePassword(keyPassword) + .setTrustStoreKeyType(caKeyType) + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Test + public void testLoadKeyStore() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.BCFKS).getAbsolutePath(); + KeyStore ks = new BCFKSFileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + Assert.assertEquals(1, ks.size()); + } + + @Test(expected = Exception.class) + public void testLoadKeyStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.BCFKS).getAbsolutePath(); + new BCFKSFileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword("wrong password") + .build() + .loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.BCFKS).getAbsolutePath(); + new BCFKSFileLoader.Builder() + .setKeyStorePath(path + ".does_not_exist") + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadKeyStoreWithNullFilePath() throws Exception { + new BCFKSFileLoader.Builder() + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFileType() throws Exception { + // Trying to load a PEM file with BCFKS loader should fail + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new BCFKSFileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test + public void testLoadTrustStore() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.BCFKS).getAbsolutePath(); + KeyStore ts = new BCFKSFileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + Assert.assertEquals(1, ts.size()); + } + + @Test(expected = Exception.class) + public void testLoadTrustStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.BCFKS).getAbsolutePath(); + new BCFKSFileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword("wrong password") + .build() + .loadTrustStore(); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.BCFKS).getAbsolutePath(); + new BCFKSFileLoader.Builder() + .setTrustStorePath(path + ".does_not_exist") + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadTrustStoreWithNullFilePath() throws Exception { + new BCFKSFileLoader.Builder() + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFileType() throws Exception { + // Trying to load a PEM file with BCFKS loader should fail + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new BCFKSFileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/BaseX509ParameterizedTestCase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/BaseX509ParameterizedTestCase.java new file mode 100644 index 0000000..b4790cf --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/BaseX509ParameterizedTestCase.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.File; +import java.io.IOException; +import java.security.Security; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.test.ClientBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +/** + * Base class for parameterized unit tests that use X509TestContext for testing + * different X509 parameter combinations (CA key type, cert key type, with/without + * a password, with/without hostname verification, etc). + * + * This base class takes care of setting up / cleaning up the test environment, + * and caching the X509TestContext objects used by the tests. + */ +public abstract class BaseX509ParameterizedTestCase extends ZKTestCase { + + /** + * Default parameters suitable for most subclasses. See example usage + * in {@link X509UtilTest}. + * @return an array of parameter combinations to test with. + */ + public static Collection defaultParams() { + ArrayList result = new ArrayList<>(); + int paramIndex = 0; + for (X509KeyType caKeyType : X509KeyType.values()) { + for (X509KeyType certKeyType : X509KeyType.values()) { + for (String keyPassword : new String[]{"", "pa$$w0rd"}) { + result.add(new Object[]{caKeyType, certKeyType, keyPassword, paramIndex++}); + } + } + } + return result; + } + + /** + * Because key generation and writing / deleting files is kind of expensive, we cache the certs and on-disk files + * between test cases. None of the test cases modify any of this data so it's safe to reuse between tests. This + * caching makes all test cases after the first one for a given parameter combination complete almost instantly. + */ + protected static Map cachedTestContexts; + protected static File tempDir; + + protected X509TestContext x509TestContext; + + @BeforeClass + public static void setUpBaseClass() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + cachedTestContexts = new HashMap<>(); + tempDir = ClientBase.createEmptyTestDir(); + } + + @AfterClass + public static void cleanUpBaseClass() { + Security.removeProvider("BC"); + cachedTestContexts.clear(); + cachedTestContexts = null; + try { + FileUtils.deleteDirectory(tempDir); + } catch (IOException e) { + // ignore + } + } + + /** + * Constructor. See example usage in {@link X509UtilTest}. + * + * @param paramIndex the index under which the X509TestContext should be cached. + * @param contextSupplier a function that creates and returns the X509TestContext + * for the current index if one is not already cached. + */ + protected BaseX509ParameterizedTestCase( + Integer paramIndex, java.util.function.Supplier contextSupplier) { + if (cachedTestContexts.containsKey(paramIndex)) { + x509TestContext = cachedTestContexts.get(paramIndex); + } else { + x509TestContext = contextSupplier.get(); + cachedTestContexts.put(paramIndex, x509TestContext); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/CertificatesToPlayWith.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/CertificatesToPlayWith.java new file mode 100644 index 0000000..0cd2172 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/CertificatesToPlayWith.java @@ -0,0 +1,539 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +/** + * Some X509 certificates to test against. + *

      + * Note: some of these certificates have Japanese Kanji in the "subjectAlt" + * field (UTF8). Not sure how realistic that is since international characters + * in DNS names usually get translated into ASCII using "xn--" style DNS + * entries. "xn--i8s592g.co.jp" is what FireFox actually uses when trying to + * find 花子.co.jp. So would the CN in the certificate contain + * "xn--i8s592g.co.jp" in ASCII, or "花子.co.jp" in UTF8? (Both?) + *

      + * + * @since 11-Dec-2006 + */ +public class CertificatesToPlayWith { + + /** + * CN=foo.com + */ + public static final byte[] X509_FOO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIERjCCAy6gAwIBAgIJAIz+EYMBU6aQMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE1MzE0MVoXDTI4MTEwNTE1MzE0MVowgaQx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhNYXJ5bGFuZDEUMBIGA1UEBxMLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoTDmh0dHBjb21wb25lbnRzMRowGAYDVQQLExF0ZXN0IGNl\n" + + "cnRpZmljYXRlczEQMA4GA1UEAxMHZm9vLmNvbTElMCMGCSqGSIb3DQEJARYWanVs\n" + + "aXVzZGF2aWVzQGdtYWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\n" + + "ggEBAMhjr5aCPoyp0R1iroWAfnEyBMGYWoCidH96yGPFjYLowez5aYKY1IOKTY2B\n" + + "lYho4O84X244QrZTRl8kQbYtxnGh4gSCD+Z8gjZ/gMvLUlhqOb+WXPAUHMB39GRy\n" + + "zerA/ZtrlUqf+lKo0uWcocxeRc771KN8cPH3nHZ0rV0Hx4ZAZy6U4xxObe4rtSVY\n" + + "07hNKXAb2odnVqgzcYiDkLV8ilvEmoNWMWrp8UBqkTcpEhYhCYp3cTkgJwMSuqv8\n" + + "BqnGd87xQU3FVZI4tbtkB+KzjD9zz8QCDJAfDjZHR03KNQ5mxOgXwxwKw6lGMaiV\n" + + "JTxpTKqym93whYk93l3ocEe55c0CAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgB\n" + + "hvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYE\n" + + "FJ8Ud78/OrbKOIJCSBYs2tDLXofYMB8GA1UdIwQYMBaAFHua2o+QmU5S0qzbswNS\n" + + "yoemDT4NMA0GCSqGSIb3DQEBBQUAA4IBAQC3jRmEya6sQCkmieULcvx8zz1euCk9\n" + + "fSez7BEtki8+dmfMXe3K7sH0lI8f4jJR0rbSCjpmCQLYmzC3NxBKeJOW0RcjNBpO\n" + + "c2JlGO9auXv2GDP4IYiXElLJ6VSqc8WvDikv0JmCCWm0Zga+bZbR/EWN5DeEtFdF\n" + + "815CLpJZNcYwiYwGy/CVQ7w2TnXlG+mraZOz+owr+cL6J/ZesbdEWfjoS1+cUEhE\n" + + "HwlNrAu8jlZ2UqSgskSWlhYdMTAP9CPHiUv9N7FcT58Itv/I4fKREINQYjDpvQcx\n" + + "SaTYb9dr5sB4WLNglk7zxDtM80H518VvihTcP7FHL+Gn6g4j5fkI98+S\n" + + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * CN=花子.co.jp + */ + public static final byte[] X509_HANAKO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIESzCCAzOgAwIBAgIJAIz+EYMBU6aTMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE1NDIxNVoXDTI4MTEwNTE1NDIxNVowgakx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIDAhNYXJ5bGFuZDEUMBIGA1UEBwwLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoMDmh0dHBjb21wb25lbnRzMRowGAYDVQQLDBF0ZXN0IGNl\n" + + "cnRpZmljYXRlczEVMBMGA1UEAwwM6Iqx5a2QLmNvLmpwMSUwIwYJKoZIhvcNAQkB\n" + + "FhZqdWxpdXNkYXZpZXNAZ21haWwuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A\n" + + "MIIBCgKCAQEAyGOvloI+jKnRHWKuhYB+cTIEwZhagKJ0f3rIY8WNgujB7PlpgpjU\n" + + "g4pNjYGViGjg7zhfbjhCtlNGXyRBti3GcaHiBIIP5nyCNn+Ay8tSWGo5v5Zc8BQc\n" + + "wHf0ZHLN6sD9m2uVSp/6UqjS5ZyhzF5FzvvUo3xw8fecdnStXQfHhkBnLpTjHE5t\n" + + "7iu1JVjTuE0pcBvah2dWqDNxiIOQtXyKW8Sag1YxaunxQGqRNykSFiEJindxOSAn\n" + + "AxK6q/wGqcZ3zvFBTcVVkji1u2QH4rOMP3PPxAIMkB8ONkdHTco1DmbE6BfDHArD\n" + + "qUYxqJUlPGlMqrKb3fCFiT3eXehwR7nlzQIDAQABo3sweTAJBgNVHRMEAjAAMCwG\n" + + "CWCGSAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNV\n" + + "HQ4EFgQUnxR3vz86tso4gkJIFiza0Mteh9gwHwYDVR0jBBgwFoAUe5raj5CZTlLS\n" + + "rNuzA1LKh6YNPg0wDQYJKoZIhvcNAQEFBQADggEBALJ27i3okV/KvlDp6KMID3gd\n" + + "ITl68PyItzzx+SquF8gahMh016NX73z/oVZoVUNdftla8wPUB1GwIkAnGkhQ9LHK\n" + + "spBdbRiCj0gMmLCsX8SrjFvr7cYb2cK6J/fJe92l1tg/7Y4o7V/s4JBe/cy9U9w8\n" + + "a0ctuDmEBCgC784JMDtT67klRfr/2LlqWhlOEq7pUFxRLbhpquaAHSOjmIcWnVpw\n" + + "9BsO7qe46hidgn39hKh1WjKK2VcL/3YRsC4wUi0PBtFW6ScMCuMhgIRXSPU55Rae\n" + + "UIlOdPjjr1SUNWGId1rD7W16Scpwnknn310FNxFMHVI0GTGFkNdkilNCFJcIoRA=\n" + + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * CN=foo.com, subjectAlt=bar.com + */ + public static final byte[] X509_FOO_BAR = ("-----BEGIN CERTIFICATE-----\n" + + "MIIEXDCCA0SgAwIBAgIJAIz+EYMBU6aRMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE1MzYyOVoXDTI4MTEwNTE1MzYyOVowgaQx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhNYXJ5bGFuZDEUMBIGA1UEBxMLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoTDmh0dHBjb21wb25lbnRzMRowGAYDVQQLExF0ZXN0IGNl\n" + + "cnRpZmljYXRlczEQMA4GA1UEAxMHZm9vLmNvbTElMCMGCSqGSIb3DQEJARYWanVs\n" + + "aXVzZGF2aWVzQGdtYWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\n" + + "ggEBAMhjr5aCPoyp0R1iroWAfnEyBMGYWoCidH96yGPFjYLowez5aYKY1IOKTY2B\n" + + "lYho4O84X244QrZTRl8kQbYtxnGh4gSCD+Z8gjZ/gMvLUlhqOb+WXPAUHMB39GRy\n" + + "zerA/ZtrlUqf+lKo0uWcocxeRc771KN8cPH3nHZ0rV0Hx4ZAZy6U4xxObe4rtSVY\n" + + "07hNKXAb2odnVqgzcYiDkLV8ilvEmoNWMWrp8UBqkTcpEhYhCYp3cTkgJwMSuqv8\n" + + "BqnGd87xQU3FVZI4tbtkB+KzjD9zz8QCDJAfDjZHR03KNQ5mxOgXwxwKw6lGMaiV\n" + + "JTxpTKqym93whYk93l3ocEe55c0CAwEAAaOBkDCBjTAJBgNVHRMEAjAAMCwGCWCG\n" + + "SAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4E\n" + + "FgQUnxR3vz86tso4gkJIFiza0Mteh9gwHwYDVR0jBBgwFoAUe5raj5CZTlLSrNuz\n" + + "A1LKh6YNPg0wEgYDVR0RBAswCYIHYmFyLmNvbTANBgkqhkiG9w0BAQUFAAOCAQEA\n" + + "dQyprNZBmVnvuVWjV42sey/PTfkYShJwy1j0/jcFZR/ypZUovpiHGDO1DgL3Y3IP\n" + + "zVQ26uhUsSw6G0gGRiaBDe/0LUclXZoJzXX1qpS55OadxW73brziS0sxRgGrZE/d\n" + + "3g5kkio6IED47OP6wYnlmZ7EKP9cqjWwlnvHnnUcZ2SscoLNYs9rN9ccp8tuq2by\n" + + "88OyhKwGjJfhOudqfTNZcDzRHx4Fzm7UsVaycVw4uDmhEHJrAsmMPpj/+XRK9/42\n" + + "2xq+8bc6HojdtbCyug/fvBZvZqQXSmU8m8IVcMmWMz0ZQO8ee3QkBHMZfCy7P/kr\n" + + "VbWx/uETImUu+NZg22ewEw==\n" + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * CN=foo.com, subjectAlt=bar.com, subjectAlt=花子.co.jp + * (hanako.co.jp in kanji) + */ + public static final byte[] X509_FOO_BAR_HANAKO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIEajCCA1KgAwIBAgIJAIz+EYMBU6aSMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE1MzgxM1oXDTI4MTEwNTE1MzgxM1owgaQx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhNYXJ5bGFuZDEUMBIGA1UEBxMLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoTDmh0dHBjb21wb25lbnRzMRowGAYDVQQLExF0ZXN0IGNl\n" + + "cnRpZmljYXRlczEQMA4GA1UEAxMHZm9vLmNvbTElMCMGCSqGSIb3DQEJARYWanVs\n" + + "aXVzZGF2aWVzQGdtYWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\n" + + "ggEBAMhjr5aCPoyp0R1iroWAfnEyBMGYWoCidH96yGPFjYLowez5aYKY1IOKTY2B\n" + + "lYho4O84X244QrZTRl8kQbYtxnGh4gSCD+Z8gjZ/gMvLUlhqOb+WXPAUHMB39GRy\n" + + "zerA/ZtrlUqf+lKo0uWcocxeRc771KN8cPH3nHZ0rV0Hx4ZAZy6U4xxObe4rtSVY\n" + + "07hNKXAb2odnVqgzcYiDkLV8ilvEmoNWMWrp8UBqkTcpEhYhCYp3cTkgJwMSuqv8\n" + + "BqnGd87xQU3FVZI4tbtkB+KzjD9zz8QCDJAfDjZHR03KNQ5mxOgXwxwKw6lGMaiV\n" + + "JTxpTKqym93whYk93l3ocEe55c0CAwEAAaOBnjCBmzAJBgNVHRMEAjAAMCwGCWCG\n" + + "SAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4E\n" + + "FgQUnxR3vz86tso4gkJIFiza0Mteh9gwHwYDVR0jBBgwFoAUe5raj5CZTlLSrNuz\n" + + "A1LKh6YNPg0wIAYDVR0RBBkwF4IHYmFyLmNvbYIM6Iqx5a2QLmNvLmpwMA0GCSqG\n" + + "SIb3DQEBBQUAA4IBAQBeZs7ZIYyKtdnVxVvdLgwySEPOE4pBSXii7XYv0Q9QUvG/\n" + + "++gFGQh89HhABzA1mVUjH5dJTQqSLFvRfqTHqLpxSxSWqMHnvRM4cPBkIRp/XlMK\n" + + "PlXadYtJLPTgpbgvulA1ickC9EwlNYWnowZ4uxnfsMghW4HskBqaV+PnQ8Zvy3L0\n" + + "12c7Cg4mKKS5pb1HdRuiD2opZ+Hc77gRQLvtWNS8jQvd/iTbh6fuvTKfAOFoXw22\n" + + "sWIKHYrmhCIRshUNohGXv50m2o+1w9oWmQ6Dkq7lCjfXfUB4wIbggJjpyEtbNqBt\n" + + "j4MC2x5rfsLKKqToKmNE7pFEgqwe8//Aar1b+Qj+\n" + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * CN=*.foo.com + */ + public static final byte[] X509_WILD_FOO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIESDCCAzCgAwIBAgIJAIz+EYMBU6aUMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE2MTU1NVoXDTI4MTEwNTE2MTU1NVowgaYx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhNYXJ5bGFuZDEUMBIGA1UEBxMLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoTDmh0dHBjb21wb25lbnRzMRowGAYDVQQLExF0ZXN0IGNl\n" + + "cnRpZmljYXRlczESMBAGA1UEAxQJKi5mb28uY29tMSUwIwYJKoZIhvcNAQkBFhZq\n" + + "dWxpdXNkYXZpZXNAZ21haWwuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n" + + "CgKCAQEAyGOvloI+jKnRHWKuhYB+cTIEwZhagKJ0f3rIY8WNgujB7PlpgpjUg4pN\n" + + "jYGViGjg7zhfbjhCtlNGXyRBti3GcaHiBIIP5nyCNn+Ay8tSWGo5v5Zc8BQcwHf0\n" + + "ZHLN6sD9m2uVSp/6UqjS5ZyhzF5FzvvUo3xw8fecdnStXQfHhkBnLpTjHE5t7iu1\n" + + "JVjTuE0pcBvah2dWqDNxiIOQtXyKW8Sag1YxaunxQGqRNykSFiEJindxOSAnAxK6\n" + + "q/wGqcZ3zvFBTcVVkji1u2QH4rOMP3PPxAIMkB8ONkdHTco1DmbE6BfDHArDqUYx\n" + + "qJUlPGlMqrKb3fCFiT3eXehwR7nlzQIDAQABo3sweTAJBgNVHRMEAjAAMCwGCWCG\n" + + "SAGG+EIBDQQfFh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4E\n" + + "FgQUnxR3vz86tso4gkJIFiza0Mteh9gwHwYDVR0jBBgwFoAUe5raj5CZTlLSrNuz\n" + + "A1LKh6YNPg0wDQYJKoZIhvcNAQEFBQADggEBAH0ipG6J561UKUfgkeW7GvYwW98B\n" + + "N1ZooWX+JEEZK7+Pf/96d3Ij0rw9ACfN4bpfnCq0VUNZVSYB+GthQ2zYuz7tf/UY\n" + + "A6nxVgR/IjG69BmsBl92uFO7JTNtHztuiPqBn59pt+vNx4yPvno7zmxsfI7jv0ww\n" + + "yfs+0FNm7FwdsC1k47GBSOaGw38kuIVWqXSAbL4EX9GkryGGOKGNh0qvAENCdRSB\n" + + "G9Z6tyMbmfRY+dLSh3a9JwoEcBUso6EWYBakLbq4nG/nvYdYvG9ehrnLVwZFL82e\n" + + "l3Q/RK95bnA6cuRClGusLad0e6bjkBzx/VQ3VarDEpAkTLUGVAa0CLXtnyc=\n" + + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * CN=*.co.jp + */ + public static final byte[] X509_WILD_CO_JP = ("-----BEGIN CERTIFICATE-----\n" + + "MIIERjCCAy6gAwIBAgIJAIz+EYMBU6aVMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE2MTYzMFoXDTI4MTEwNTE2MTYzMFowgaQx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhNYXJ5bGFuZDEUMBIGA1UEBxMLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoTDmh0dHBjb21wb25lbnRzMRowGAYDVQQLExF0ZXN0IGNl\n" + + "cnRpZmljYXRlczEQMA4GA1UEAxQHKi5jby5qcDElMCMGCSqGSIb3DQEJARYWanVs\n" + + "aXVzZGF2aWVzQGdtYWlsLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\n" + + "ggEBAMhjr5aCPoyp0R1iroWAfnEyBMGYWoCidH96yGPFjYLowez5aYKY1IOKTY2B\n" + + "lYho4O84X244QrZTRl8kQbYtxnGh4gSCD+Z8gjZ/gMvLUlhqOb+WXPAUHMB39GRy\n" + + "zerA/ZtrlUqf+lKo0uWcocxeRc771KN8cPH3nHZ0rV0Hx4ZAZy6U4xxObe4rtSVY\n" + + "07hNKXAb2odnVqgzcYiDkLV8ilvEmoNWMWrp8UBqkTcpEhYhCYp3cTkgJwMSuqv8\n" + + "BqnGd87xQU3FVZI4tbtkB+KzjD9zz8QCDJAfDjZHR03KNQ5mxOgXwxwKw6lGMaiV\n" + + "JTxpTKqym93whYk93l3ocEe55c0CAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgB\n" + + "hvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYE\n" + + "FJ8Ud78/OrbKOIJCSBYs2tDLXofYMB8GA1UdIwQYMBaAFHua2o+QmU5S0qzbswNS\n" + + "yoemDT4NMA0GCSqGSIb3DQEBBQUAA4IBAQA0sWglVlMx2zNGvUqFC73XtREwii53\n" + + "CfMM6mtf2+f3k/d8KXhLNySrg8RRlN11zgmpPaLtbdTLrmG4UdAHHYr8O4y2BBmE\n" + + "1cxNfGxxechgF8HX10QV4dkyzp6Z1cfwvCeMrT5G/V1pejago0ayXx+GPLbWlNeZ\n" + + "S+Kl0m3p+QplXujtwG5fYcIpaGpiYraBLx3Tadih39QN65CnAh/zRDhLCUzKyt9l\n" + + "UGPLEUDzRHMPHLnSqT1n5UU5UDRytbjJPXzF+l/+WZIsanefWLsxnkgAuZe/oMMF\n" + + "EJMryEzOjg4Tfuc5qM0EXoPcQ/JlheaxZ40p2IyHqbsWV4MRYuFH4bkM\n" + + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * CN=*.foo.com, subjectAlt=*.bar.com, subjectAlt=*.花子.co.jp + * (*.hanako.co.jp in kanji) + */ + public static final byte[] X509_WILD_FOO_BAR_HANAKO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIEcDCCA1igAwIBAgIJAIz+EYMBU6aWMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE2MTczMVoXDTI4MTEwNTE2MTczMVowgaYx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhNYXJ5bGFuZDEUMBIGA1UEBxMLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoTDmh0dHBjb21wb25lbnRzMRowGAYDVQQLExF0ZXN0IGNl\n" + + "cnRpZmljYXRlczESMBAGA1UEAxQJKi5mb28uY29tMSUwIwYJKoZIhvcNAQkBFhZq\n" + + "dWxpdXNkYXZpZXNAZ21haWwuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB\n" + + "CgKCAQEAyGOvloI+jKnRHWKuhYB+cTIEwZhagKJ0f3rIY8WNgujB7PlpgpjUg4pN\n" + + "jYGViGjg7zhfbjhCtlNGXyRBti3GcaHiBIIP5nyCNn+Ay8tSWGo5v5Zc8BQcwHf0\n" + + "ZHLN6sD9m2uVSp/6UqjS5ZyhzF5FzvvUo3xw8fecdnStXQfHhkBnLpTjHE5t7iu1\n" + + "JVjTuE0pcBvah2dWqDNxiIOQtXyKW8Sag1YxaunxQGqRNykSFiEJindxOSAnAxK6\n" + + "q/wGqcZ3zvFBTcVVkji1u2QH4rOMP3PPxAIMkB8ONkdHTco1DmbE6BfDHArDqUYx\n" + + "qJUlPGlMqrKb3fCFiT3eXehwR7nlzQIDAQABo4GiMIGfMAkGA1UdEwQCMAAwLAYJ\n" + + "YIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRlMB0GA1Ud\n" + + "DgQWBBSfFHe/Pzq2yjiCQkgWLNrQy16H2DAfBgNVHSMEGDAWgBR7mtqPkJlOUtKs\n" + + "27MDUsqHpg0+DTAkBgNVHREEHTAbggkqLmJhci5jb22CDiou6Iqx5a2QLmNvLmpw\n" + + "MA0GCSqGSIb3DQEBBQUAA4IBAQBobWC+D5/lx6YhX64CwZ26XLjxaE0S415ajbBq\n" + + "DK7lz+Rg7zOE3GsTAMi+ldUYnhyz0wDiXB8UwKXl0SDToB2Z4GOgqQjAqoMmrP0u\n" + + "WB6Y6dpkfd1qDRUzI120zPYgSdsXjHW9q2H77iV238hqIU7qCvEz+lfqqWEY504z\n" + + "hYNlknbUnR525ItosEVwXFBJTkZ3Yw8gg02c19yi8TAh5Li3Ad8XQmmSJMWBV4XK\n" + + "qFr0AIZKBlg6NZZFf/0dP9zcKhzSriW27bY0XfzA6GSiRDXrDjgXq6baRT6YwgIg\n" + + "pgJsDbJtZfHnV1nd3M6zOtQPm1TIQpNmMMMd/DPrGcUQerD3\n" + "-----END CERTIFICATE-----\n") + .getBytes(); + + /** + * CN=foo.com, CN=bar.com, CN=花子.co.jp + */ + public static final byte[] X509_THREE_CNS_FOO_BAR_HANAKO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIEbzCCA1egAwIBAgIJAIz+EYMBU6aXMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE2MTk0NVoXDTI4MTEwNTE2MTk0NVowgc0x\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIDAhNYXJ5bGFuZDEUMBIGA1UEBwwLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoMDmh0dHBjb21wb25lbnRzMRowGAYDVQQLDBF0ZXN0IGNl\n" + + "cnRpZmljYXRlczEQMA4GA1UEAwwHZm9vLmNvbTEQMA4GA1UEAwwHYmFyLmNvbTEV\n" + + "MBMGA1UEAwwM6Iqx5a2QLmNvLmpwMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGOv\n" + + "loI+jKnRHWKuhYB+cTIEwZhagKJ0f3rIY8WNgujB7PlpgpjUg4pNjYGViGjg7zhf\n" + + "bjhCtlNGXyRBti3GcaHiBIIP5nyCNn+Ay8tSWGo5v5Zc8BQcwHf0ZHLN6sD9m2uV\n" + + "Sp/6UqjS5ZyhzF5FzvvUo3xw8fecdnStXQfHhkBnLpTjHE5t7iu1JVjTuE0pcBva\n" + + "h2dWqDNxiIOQtXyKW8Sag1YxaunxQGqRNykSFiEJindxOSAnAxK6q/wGqcZ3zvFB\n" + + "TcVVkji1u2QH4rOMP3PPxAIMkB8ONkdHTco1DmbE6BfDHArDqUYxqJUlPGlMqrKb\n" + + "3fCFiT3eXehwR7nlzQIDAQABo3sweTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQf\n" + + "Fh1PcGVuU1NMIEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUnxR3vz86\n" + + "tso4gkJIFiza0Mteh9gwHwYDVR0jBBgwFoAUe5raj5CZTlLSrNuzA1LKh6YNPg0w\n" + + "DQYJKoZIhvcNAQEFBQADggEBAGuZb8ai1NO2j4v3y9TLZvd5s0vh5/TE7n7RX+8U\n" + + "y37OL5k7x9nt0mM1TyAKxlCcY+9h6frue8MemZIILSIvMrtzccqNz0V1WKgA+Orf\n" + + "uUrabmn+CxHF5gpy6g1Qs2IjVYWA5f7FROn/J+Ad8gJYc1azOWCLQqSyfpNRLSvY\n" + + "EriQFEV63XvkJ8JrG62b+2OT2lqT4OO07gSPetppdlSa8NBSKP6Aro9RIX1ZjUZQ\n" + + "SpQFCfo02NO0uNRDPUdJx2huycdNb+AXHaO7eXevDLJ+QnqImIzxWiY6zLOdzjjI\n" + + "VBMkLHmnP7SjGSQ3XA4ByrQOxfOUTyLyE7NuemhHppuQPxE=\n" + "-----END CERTIFICATE-----\n") + .getBytes(); + + /** + * subjectAlt=foo.com + */ + public static final byte[] X509_NO_CNS_FOO = ("-----BEGIN CERTIFICATE-----\n" + + "MIIESjCCAzKgAwIBAgIJAIz+EYMBU6aYMA0GCSqGSIb3DQEBBQUAMIGiMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxEjAQBgNVBAcTCVZhbmNvdXZlcjEWMBQGA1UE\n" + + "ChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9uc19zc2wxHTAbBgNVBAMU\n" + + "FGRlbW9faW50ZXJtZWRpYXRlX2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZp\n" + + "ZXNAZ21haWwuY29tMB4XDTA2MTIxMTE2MjYxMFoXDTI4MTEwNTE2MjYxMFowgZIx\n" + + "CzAJBgNVBAYTAlVTMREwDwYDVQQIDAhNYXJ5bGFuZDEUMBIGA1UEBwwLRm9yZXN0\n" + + "IEhpbGwxFzAVBgNVBAoMDmh0dHBjb21wb25lbnRzMRowGAYDVQQLDBF0ZXN0IGNl\n" + + "cnRpZmljYXRlczElMCMGCSqGSIb3DQEJARYWanVsaXVzZGF2aWVzQGdtYWlsLmNv\n" + + "bTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMhjr5aCPoyp0R1iroWA\n" + + "fnEyBMGYWoCidH96yGPFjYLowez5aYKY1IOKTY2BlYho4O84X244QrZTRl8kQbYt\n" + + "xnGh4gSCD+Z8gjZ/gMvLUlhqOb+WXPAUHMB39GRyzerA/ZtrlUqf+lKo0uWcocxe\n" + + "Rc771KN8cPH3nHZ0rV0Hx4ZAZy6U4xxObe4rtSVY07hNKXAb2odnVqgzcYiDkLV8\n" + + "ilvEmoNWMWrp8UBqkTcpEhYhCYp3cTkgJwMSuqv8BqnGd87xQU3FVZI4tbtkB+Kz\n" + + "jD9zz8QCDJAfDjZHR03KNQ5mxOgXwxwKw6lGMaiVJTxpTKqym93whYk93l3ocEe5\n" + + "5c0CAwEAAaOBkDCBjTAJBgNVHRMEAjAAMCwGCWCGSAGG+EIBDQQfFh1PcGVuU1NM\n" + + "IEdlbmVyYXRlZCBDZXJ0aWZpY2F0ZTAdBgNVHQ4EFgQUnxR3vz86tso4gkJIFiza\n" + + "0Mteh9gwHwYDVR0jBBgwFoAUe5raj5CZTlLSrNuzA1LKh6YNPg0wEgYDVR0RBAsw\n" + + "CYIHZm9vLmNvbTANBgkqhkiG9w0BAQUFAAOCAQEAjl78oMjzFdsMy6F1sGg/IkO8\n" + + "tF5yUgPgFYrs41yzAca7IQu6G9qtFDJz/7ehh/9HoG+oqCCIHPuIOmS7Sd0wnkyJ\n" + + "Y7Y04jVXIb3a6f6AgBkEFP1nOT0z6kjT7vkA5LJ2y3MiDcXuRNMSta5PYVnrX8aZ\n" + + "yiqVUNi40peuZ2R8mAUSBvWgD7z2qWhF8YgDb7wWaFjg53I36vWKn90ZEti3wNCw\n" + + "qAVqixM+J0qJmQStgAc53i2aTMvAQu3A3snvH/PHTBo+5UL72n9S1kZyNCsVf1Qo\n" + + "n8jKTiRriEM+fMFlcgQP284EBFzYHyCXFb9O/hMjK2+6mY9euMB1U1aFFzM/Bg==\n" + + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * Intermediate CA for all of these. + */ + public static final byte[] X509_INTERMEDIATE_CA = ("-----BEGIN CERTIFICATE-----\n" + + "MIIEnDCCA4SgAwIBAgIJAJTNwZ6yNa5cMA0GCSqGSIb3DQEBBQUAMIGGMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxFjAUBgNVBAoTDXd3dy5jdWNiYy5jb20xFDAS\n" + + "BgNVBAsUC2NvbW1vbnNfc3NsMRUwEwYDVQQDFAxkZW1vX3Jvb3RfY2ExJTAjBgkq\n" + + "hkiG9w0BCQEWFmp1bGl1c2Rhdmllc0BnbWFpbC5jb20wHhcNMDYxMTA1MjE0OTMx\n" + + "WhcNMDcxMTA1MjE0OTMxWjCBojELMAkGA1UEBhMCQ0ExCzAJBgNVBAgTAkJDMRIw\n" + + "EAYDVQQHEwlWYW5jb3V2ZXIxFjAUBgNVBAoTDXd3dy5jdWNiYy5jb20xFDASBgNV\n" + + "BAsUC2NvbW1vbnNfc3NsMR0wGwYDVQQDFBRkZW1vX2ludGVybWVkaWF0ZV9jYTEl\n" + + "MCMGCSqGSIb3DQEJARYWanVsaXVzZGF2aWVzQGdtYWlsLmNvbTCCASIwDQYJKoZI\n" + + "hvcNAQEBBQADggEPADCCAQoCggEBAL0S4y3vUO0EM6lwqOEfK8fvrUprIbsikXaG\n" + + "XzejcZ+T3l2Dc7t8WtBfRf78i4JypMqJQSijrUicj3H6mOMIReKaXm6ls4hA5d8w\n" + + "Lhmgiqsz/kW+gA8SeWGWRN683BD/RbQmzOls6ynBvap9jZlthXWBrSIlPCQoBLXY\n" + + "KVaxGzbL4ezaq+XFMKMQSm2uKwVmHHQNbfmZlPsuendBVomb/ked53Ab9IH6dwwN\n" + + "qJH9WIrvIzIVEXWlpvQ5MCqozM7u1akU+G8cazr8theGPCaYkzoXnigWua4OjdpV\n" + + "9z5ZDknhfBzG1AjapdG07FIirwWWgIyZXqZSD96ikmLtwT29qnsCAwEAAaOB7jCB\n" + + "6zAdBgNVHQ4EFgQUe5raj5CZTlLSrNuzA1LKh6YNPg0wgbsGA1UdIwSBszCBsIAU\n" + + "rN8eFIvMiRFXXgDqKumS0/W2AhOhgYykgYkwgYYxCzAJBgNVBAYTAkNBMQswCQYD\n" + + "VQQIEwJCQzEWMBQGA1UEChMNd3d3LmN1Y2JjLmNvbTEUMBIGA1UECxQLY29tbW9u\n" + + "c19zc2wxFTATBgNVBAMUDGRlbW9fcm9vdF9jYTElMCMGCSqGSIb3DQEJARYWanVs\n" + + "aXVzZGF2aWVzQGdtYWlsLmNvbYIJAJTNwZ6yNa5bMAwGA1UdEwQFMAMBAf8wDQYJ\n" + + "KoZIhvcNAQEFBQADggEBAIB4KMZvHD20pdKajFtMBpL7X4W4soq6EeTtjml3NYa9\n" + + "Qc52bsQEGNccKY9afYSBIndaQvFdtmz6HdoN+B8TjYShw2KhyjtKimGLpWYoi1YF\n" + + "e4aHdmA/Gp5xk8pZzR18FmooxC9RqBux+NAM2iTFSLgDtGIIj4sg2rbn6Bb6ZlQT\n" + + "1rg6VucXCA1629lNfMeNcu7CBNmUKIdaxHR/YJQallE0KfGRiOIWPrPj/VNk0YA6\n" + + "XFg0ocjqXJ2/N0N9rWVshMUaXgOh7m4D/5zga5/nuxDU+PoToA6mQ4bV6eCYqZbh\n" + + "aa1kQYtR9B4ZiG6pB82qVc2dCqStOH2FAEWos2gAVkQ=\n" + "-----END CERTIFICATE-----\n") + .getBytes(); + + /** + * Root CA for all of these. + */ + public static final byte[] X509_ROOT_CA = ("-----BEGIN CERTIFICATE-----\n" + + "MIIEgDCCA2igAwIBAgIJAJTNwZ6yNa5bMA0GCSqGSIb3DQEBBQUAMIGGMQswCQYD\n" + + "VQQGEwJDQTELMAkGA1UECBMCQkMxFjAUBgNVBAoTDXd3dy5jdWNiYy5jb20xFDAS\n" + + "BgNVBAsUC2NvbW1vbnNfc3NsMRUwEwYDVQQDFAxkZW1vX3Jvb3RfY2ExJTAjBgkq\n" + + "hkiG9w0BCQEWFmp1bGl1c2Rhdmllc0BnbWFpbC5jb20wHhcNMDYxMTA1MjEzNjQz\n" + + "WhcNMjYxMTA1MjEzNjQzWjCBhjELMAkGA1UEBhMCQ0ExCzAJBgNVBAgTAkJDMRYw\n" + + "FAYDVQQKEw13d3cuY3VjYmMuY29tMRQwEgYDVQQLFAtjb21tb25zX3NzbDEVMBMG\n" + + "A1UEAxQMZGVtb19yb290X2NhMSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZpZXNA\n" + + "Z21haWwuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv+OnocmJ\n" + + "79UeO2hlCwK+Cle5uZWnU6uwJl+08z5cvebb5tT64WL9+psDbfgUH/Gm9JsuxKTg\n" + + "w1tZO/4duIgnaLNSx4HoqaTjwigd/hR3TsoGEPXTCkz1ikgTCOEDvl+iMid6aOrd\n" + + "mViE8HhscxKZ+h5FE7oHZyuT6gFoiaIXhFq+xK2w4ZwDz9L+paiwqywyUJJMnh9U\n" + + "jKorY+nua81N0oxpIhHPspCanDU4neMzCzYOZyLR/LqV5xORvHcFY84GWMz5hI25\n" + + "JbgaWJsYKuCAvNsnQwVoqKPGa7x1fn7x6oGsXJaCVt8weUwIj2xwg1lxMhrNaisH\n" + + "EvKpEAEnGGwWKQIDAQABo4HuMIHrMB0GA1UdDgQWBBSs3x4Ui8yJEVdeAOoq6ZLT\n" + + "9bYCEzCBuwYDVR0jBIGzMIGwgBSs3x4Ui8yJEVdeAOoq6ZLT9bYCE6GBjKSBiTCB\n" + + "hjELMAkGA1UEBhMCQ0ExCzAJBgNVBAgTAkJDMRYwFAYDVQQKEw13d3cuY3VjYmMu\n" + + "Y29tMRQwEgYDVQQLFAtjb21tb25zX3NzbDEVMBMGA1UEAxQMZGVtb19yb290X2Nh\n" + + "MSUwIwYJKoZIhvcNAQkBFhZqdWxpdXNkYXZpZXNAZ21haWwuY29tggkAlM3BnrI1\n" + + "rlswDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEAlPl3/8h1LttR1svC\n" + + "S8RXbHpAWIT2BEDhGHUNjSmgDQNkE/itf/FCEXh0tlU4bYdtBSOHzflbnzOyIPId\n" + + "VZeSWs33V38xDFy6KoVg1gT8JxkLmE5S1vWkpsHIlpw/U6r7KD0Kx9FYx5AiXjw0\n" + + "lzz/zlVNuO2U09KIDwDPVG1mBzQiMiSWj1U1pM4KxINkWQwDy/fvu/I983s8lW5z\n" + + "hf2WuFNzQN3fcMK5dpBE9NVIu27oYuGYh2sak34v+7T700W2ooBB71qFXtm9P5rl\n" + + "Yp9RCEsg3KEEPNTtCBs8fROeXvLDrP0cmBIqwGYDuRNCxFDTOdjv6YGdA8nLOjaH\n" + "2dDk0g==\n" + + "-----END CERTIFICATE-----\n").getBytes(); + + /** + * Below is the private key for all the server certificates above (but + * not the intermediate CA or the root CA). All of those server certs + * came from the same private key. + */ + public static final String RSA_PUBLIC_MODULUS = + "00c863af96823e8ca9d11d62ae85807e713204c1985a80a2747f7ac863c5" + + "8d82e8c1ecf9698298d4838a4d8d81958868e0ef385f6e3842b653465f24" + + "41b62dc671a1e204820fe67c82367f80cbcb52586a39bf965cf0141cc077" + + "f46472cdeac0fd9b6b954a9ffa52a8d2e59ca1cc5e45cefbd4a37c70f1f7" + + "9c7674ad5d07c78640672e94e31c4e6dee2bb52558d3b84d29701bda8767" + + "56a83371888390b57c8a5bc49a8356316ae9f1406a913729121621098a77" + + "713920270312baabfc06a9c677cef1414dc5559238b5bb6407e2b38c3f73" + + "cfc4020c901f0e3647474dca350e66c4e817c31c0ac3a94631a895253c69" + + "4caab29bddf085893dde5de87047b9e5cd"; + + public static final String RSA_PUBLIC_EXPONENT = "65537"; + + public static final String RSA_PRIVATE_EXPONENT = + "577abd3295553d0efd4d38c13b62a6d03fa7b7e40cce4f1d5071877d96c6" + + "7a39a63f0f7ab21a89db8acae45587b3ef251309a70f74dc1ac02bde68f3" + + "8ed658e54e685ed370a18c054449512ea66a2252ed36e82b565b5159ec83" + + "f23df40ae189550a183865b25fd77789e960f0d8cedcd72f32d7a66edb4b" + + "a0a2baf3fbeb6c7d75f56ef0af9a7cff1c8c7f297d72eae7982164e50a89" + + "d450698cf598d39343201094241d2d180a95882a7111e58f4a5bdbc5c125" + + "a967dd6ed9ec614c5853e88e4c71e8b682a7cf89cb1d82b6fe78cc865084" + + "c8c5dfbb50c939df2b839c977b0245bfa3615e0592b527b1013d5b675ecb" + + "44e6b355c1df581f50997175166eef39"; + + public static final String RSA_PRIME1 = + "00fe759c4f0ce8b763880215e82767e7a937297668f4e4b1e119c6b22a3c" + + "a2c7b06c547d88d0aa45f645d7d3aeadaf7f8bc594deae0978529592977c" + + "b1ff890f05033a9e9e15551cad9fbf9c41d12139ccd99c1c3ac7b2197eff" + + "350d236bb900c1440953b64956e0a058ef824a2e16894af175177c77dbe1" + "fef7d8b532608d2513"; + + public static final String RSA_PRIME2 = + "00c99a45878737a4cf73f9896680b75487f1b669b7686a6ba07103856f31" + + "db668c2c440c44cdd116f708f631c37a9adf119f5b5cb58ffe3dc62e20af" + + "af72693d936dc6bb3c5194996468389c1f094079b81522e94572b4ad7d39" + + "529178e9b8ebaeb1f0fdd83b8731c5223f1dea125341d1d64917f6b1a6ae" + "c18d320510d79f859f"; + + public static final String RSA_EXPONENT1 = + "029febf0d4cd41b7011c2465b4a259bd6118486464c247236f44a169d61e" + + "47b9062508f674508d5031003ceabc57e714e600d71b2c75d5443db2da52" + + "6bb45a374f0537c5a1aab3150764ce93cf386c84346a6bd01f6732e42075" + + "c7a0e9e78a9e73b934e7d871d0f75673820089e129a1604438edcbbeb4e2" + "106467da112ce389"; + + public static final String RSA_EXPONENT2 = + "00827e76650c946afcd170038d32e1f8386ab00d6be78d830efe382e45d4" + + "7ad4bd04e6231ee22e66740efbf52838134932c9f8c460cdccdec58a1424" + + "4427859192fd6ab6c58b74e97941b0eaf577f2a11713af5e5952af3ae124" + + "9a9a892e98410dfa2628d9af668a43b5302fb7d496c9b2fec69f595292b6" + "e997f079b0f6314eb7"; + + public static final String RSA_COEFFICIENT = + "00e6b62add350f1a2a8968903ff76c31cf703b0d7326c4a620aef01225b7" + + "1640b3f2ec375208c5f7299863f6005b7799b6e529bb1133c8435bf5fdb5" + + "a786f6cd8a19ee7094a384e6557c600a38845a0960ddbfd1df18d0af5740" + + "001853788f1b5ccbf9affb4c52c9d2efdb8aab0183d86735b32737fb4e79" + "2b8a9c7d91c7d175ae"; + + /** + * subjectAlt=IP Address:127.0.0.1, email:oleg@ural.ru, DNS:localhost.localdomain + */ + public static final byte[] X509_MULTIPLE_SUBJECT_ALT = ("-----BEGIN CERTIFICATE-----\n" + + "MIIDcTCCAtqgAwIBAgIBATANBgkqhkiG9w0BAQUFADBAMQswCQYDVQQGEwJDSDEL\n" + + "MAkGA1UECBMCWkgxDzANBgNVBAcTBlp1cmljaDETMBEGA1UEAxMKTXkgVGVzdCBD\n" + + "QTAeFw0wODEwMzExMTU3NDVaFw0wOTEwMzExMTU3NDVaMGkxCzAJBgNVBAYTAkNI\n" + + "MRAwDgYDVQQIEwdVbmtub3duMRAwDgYDVQQHEwdVbmtub3duMRAwDgYDVQQKEwdV\n" + + "bmtub3duMRAwDgYDVQQLEwdVbmtub3duMRIwEAYDVQQDEwlsb2NhbGhvc3QwggG4\n" + + "MIIBLAYHKoZIzjgEATCCAR8CgYEA/X9TgR11EilS30qcLuzk5/YRt1I870QAwx4/\n" + + "gLZRJmlFXUAiUftZPY1Y+r/F9bow9subVWzXgTuAHTRv8mZgt2uZUKWkn5/oBHsQ\n" + + "IsJPu6nX/rfGG/g7V+fGqKYVDwT7g/bTxR7DAjVUE1oWkTL2dfOuK2HXKu/yIgMZ\n" + + "ndFIAccCFQCXYFCPFSMLzLKSuYKi64QL8Fgc9QKBgQD34aCF1ps93su8q1w2uFe5\n" + + "eZSvu/o66oL5V0wLPQeCZ1FZV4661FlP5nEHEIGAtEkWcSPoTCgWE7fPCTKMyKbh\n" + + "PBZ6i1R8jSjgo64eK7OmdZFuo38L+iE1YvH7YnoBJDvMpPG+qFGQiaiD3+Fa5Z8G\n" + + "kotmXoB7VSVkAUw7/s9JKgOBhQACgYEA6ogAb/YLM1Rz9AoXKW4LA70VtFf7Mqqp\n" + + "divdu9f72WQc1vMKo1YMf3dQadkMfBYRvAAa1IXDnoiFCHhXnVRkWkoUBJyNebLB\n" + + "N92CZc0RVFZiMFgQMEh8UldnvAIi4cBk0/YuN3BGl4MzmquVIGrFovdWGqeaveOu\n" + + "Xcu4lKGJNiqjODA2MDQGA1UdEQQtMCuHBH8AAAGBDG9sZWdAdXJhbC5ydYIVbG9j\n" + + "YWxob3N0LmxvY2FsZG9tYWluMA0GCSqGSIb3DQEBBQUAA4GBAIgEwIoCSRkU3O7K\n" + + "USYaOYyfJB9hsvs6YpClvYXiQ/5kPGARP60pM62v4wC7wI9shEizokIAxY2+O3cC\n" + + "vwuJhNYaa2FJMELIwRN3XES8X8R6JHWbPaRjaAAPhczuEd8SZYy8yiVLmJTgw0gH\n" + + "BSW775NHlkjsscFVgXkNf0PobqJ9\n" + "-----END CERTIFICATE-----").getBytes(); + + /** + * subject CN=repository.infonotary.com (Multiple AVA in RDN). + */ + public static final byte[] X509_MULTIPLE_VALUE_AVA = ("-----BEGIN CERTIFICATE-----\n" + + "MIIFxzCCBK+gAwIBAgIIRO/2+/XA7z4wDQYJKoZIhvcNAQEFBQAwgZwxgZkwCQYD\n" + + "VQQGDAJCRzAVBgNVBAoMDkluZm9Ob3RhcnkgUExDMBcGCgmSJomT8ixkARkWCWRv\n" + + "bWFpbi1jYTAtBgNVBAMMJmktTm90YXJ5IFRydXN0UGF0aCBWYWxpZGF0ZWQgRG9t\n" + + "YWluIENBMC0GA1UECwwmaS1Ob3RhcnkgVHJ1c3RQYXRoIFZhbGlkYXRlZCBEb21h\n" + + "aW4gQ0EwHhcNMTIwNjE4MDg1MzIyWhcNMTMwNjE4MDg1MzIyWjCBxjGBwzAJBgNV\n" + + "BAYTAkJHMBUGA1UEChMOSW5mb05vdGFyeSBQTEMwFwYDVQQLExBGaWxlcyBSZXBv\n" + + "c2l0b3J5MBcGCgmSJomT8ixkARkWCWRvbWFpbi1jYTAgBgNVBAMTGXJlcG9zaXRv\n" + + "cnkuaW5mb25vdGFyeS5jb20wIwYJKoZIhvcNAQkBFhZzdXBwb3J0QGluZm9ub3Rh\n" + + "cnkuY29tMCYGCSqGSIb3DQEJAhMZcmVwb3NpdG9yeS5pbmZvbm90YXJ5LmNvbTCC\n" + + "ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALKWjGpgsuz103xVEW/GSg5I\n" + + "tBoLbXPxockabOTHnOh0VO2sImycyhBH78nMj+VMexn4y+kdCOuJqAA5LApxyhTA\n" + + "KgKlRN7TfoC90IYHjB1dqLMIseg4YM7Oe0e4Z2nL50bHoqXg7OUHaILUQn7ufpYp\n" + + "+VCWxyI43KvaR4+HnST3x47wqeArg/rULGV1a16X+46cxq2eoMAcDfostXHaemvz\n" + + "vg/Wd5xcWfPbF/oY1/sBXH+AK+peVBMen82+3GtAWtNWbyPE3bT4RG+WgKUyfLZ1\n" + + "7A67rX9DkUEVMPQpa50MpLnrRveiM9w6R3mrMHMHbNnwID0Tqfds5zzOi/7cLD0C\n" + + "AwEAAaOCAd8wggHbMA4GA1UdDwEB/wQEAwIDuDATBgNVHSUEDDAKBggrBgEFBQcD\n" + + "ATBEBggrBgEFBQcBAQQ4MDYwNAYIKwYBBQUHMAGGKGh0dHA6Ly9vY3NwLmluZm9u\n" + + "b3RhcnkuY29tL3Jlc3BvbmRlci5jZ2kwgZAGA1UdIASBiDCBhTCBggYMKwYBBAGB\n" + + "rQABAgMBMHIwOAYIKwYBBQUHAgEWLGh0dHA6Ly9yZXBvc2l0b3J5LmluZm9ub3Rh\n" + + "cnkuY29tL2RvbWFpbi5odG1sMDYGCCsGAQUFBwICMCoaKGktTm90YXJ5IFZhbGlk\n" + + "YXRlZCBEb21haW4gQ2VydGlmaWNhdGUgQ1AwgYkGA1UdHwSBgTB/MDWgL6Athito\n" + + "dHRwOi8vY3JsLmluZm9ub3RhcnkuY29tL2NybC9kb21haW4tY2EuY3JsgQIBVjBG\n" + + "oECgPoY8bGRhcDovL2xkYXAuaW5mb25vdGFyeS5jb20vZGM9ZG9tYWluLWNhLGRj\n" + + "PWluZm9ub3RhcnksZGM9Y29tgQIBVjAPBgNVHRMBAf8EBTADAQEAMB0GA1UdDgQW\n" + + "BBTImKJZrgV/8n7mHrA0U5EeGsBvbzAfBgNVHSMEGDAWgBTbkorEK+bPdVPpvyVI\n" + + "PTxGFnuOoDANBgkqhkiG9w0BAQUFAAOCAQEAhsMbqsqvkbfVaKZ+wDY9rX3EtuDS\n" + + "isdAo4AjmWgTtj/aBGiEiXcIGP312x+0JF+mEEQ75ZOKN+WsM8eLB0F4aqylklk7\n" + + "6yRYauRXp8dfbXrT3ozxekt0cpSMqbzze456krI12nL+C00V2Iwq96k5J/yZboNW\n" + + "Q+ibCaEAHNiL4tGVHSHm6znkWvIuUTbDgDEsm5RdafO27suz5H6zMnV+VE6onN1J\n" + + "I1mQmUs44cg2HZAqnFBpDyJQhNYy8M7yGVaRkbfuVaMqiPa+xDPR5v7NFB3kxRq2\n" + + "Za2Snopi52eUxDEhJ0MNqFi3Jfj/ZSmJ+XHra5lU4R8lijCAq8SVLZCmIQ==\n" + + "-----END CERTIFICATE-----").getBytes(); + + public static final byte[] S_GOOGLE_COM = ("-----BEGIN CERTIFICATE-----\n" + + "MIICpzCCAY+gAwIBAgIBATANBgkqhkiG9w0BAQUFADAXMRUwEwYDVQQDDAwqLmdv\n" + + "b2dsZS5jb20wHhcNMTcwMTEzMjI0OTAzWhcNMTgwMTEzMjI0OTAzWjAXMRUwEwYD\n" + + "VQQDDAwqLmdvb2dsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB\n" + + "AQDHuzznuHdJ5PH344xCyGYnUnIRhyLGBKN3WDLLrXWtr/5Sf3Q1qkiMiJ4BINsh\n" + + "3Xy0z7VvHmMFlntgHXtkofBUPvTihxsVIypRkCZb5hpsWLotR10AW2JpVl/oxLP2\n" + + "227/36X1zKh33fjImLJl9KzGWHLsbCBleQQJOn7YRsNR/QBZO0XGGkN/R2rRfLF3\n" + + "rseRfI5gJjZkO0WDxocnf/iieOe0XNR0NAZaY1aozzPmZ/pRrOKYB8OFH7F73WOC\n" + + "lPIUGai/byJ9SpbXdLUcMlGhml/4XzcnV/WVRD2P/mlY+xEFG3UEy3ufhNnKFJul\n" + + "yjZrOaKbagamqtOyktzkjnerAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBADaMcwVs\n" + + "w5kbnoDJzMBJ01H16T4u8k78i/ybwz7u7krgkU0tABXCRj7S/4Dt3jqQ/rV6evj4\n" + + "gIJ/2kZUp/PHKkV7CxWI48XBTAQUu9LEpxj0Hut3AtNMD9y/J6cFn2978tWsHFHI\n" + + "mYgvclKUDE4WFMvuxfQVuX3RcGQ5i8khEMczY/KVhZYDcLU1PU0GTTJqqrQm59Z4\n" + + "T4UyI3OPBR7Nb/kaU1fcgQ083uxRXcNYRMMZnU6c2oFnR+c6pO6aGoXo0C6rgC4R\n" + + "pOj4hPvHCfZO2xg6HAdQ7UPALLX8pu5KGot7GRc8yiJ/Q1nBEuiPKKu0MIwQoFgP\n" + + "WUux/APTsgLR7Vc=\n" + "-----END CERTIFICATE-----").getBytes(); + + public static final byte[] IP_1_1_1_1 = ("-----BEGIN CERTIFICATE-----\n" + + "MIICwjCCAaqgAwIBAgIBATANBgkqhkiG9w0BAQUFADAaMRgwFgYDVQQDEw9kdW1t\n" + + "eS12YWx1ZS5jb20wHhcNMTcwMTEzMjI1MTQ2WhcNMTgwMTEzMjI1MTQ2WjAaMRgw\n" + + "FgYDVQQDEw9kdW1teS12YWx1ZS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw\n" + + "ggEKAoIBAQDfrapp3jHLp1RlElzpR/4sF9AcTYwMF1N+adkHRoVtmTlJV2lTIAjn\n" + + "QLauy0Kkzv8uxmbID3uROgrFNDQ5RxTTCe+kW/vE6Pyzr5Z5ayjSTKeycTE7mAC4\n" + + "6ntoCeEWiD593zlfqVo5PuRSp9Kusd+kexNVjC/BETDPa3yXctcH1ouW9GyGItgQ\n" + + "u4GhCE8cipKMuTltgfK+Gh/5e9lFG9/F2fD+wHUVBULLR3JOQoqwgk2zAwKDwLuS\n" + + "sEd1CBi35+W3apCKN0SEdTKIAxc/R+O/1j2hpOl9yXCCYyveGwJdFXVZtDcx+9/H\n" + + "7NXhOdmw/mTXC5fOQGKciEo2SXt8Wp89AgMBAAGjEzARMA8GA1UdEQQIMAaHBAEB\n" + + "AQEwDQYJKoZIhvcNAQEFBQADggEBAEAO6CE8twpcfdjk9oMjI5nX9GdC5Wt6+ujd\n" + + "tLj0SbXvMKzCLLkveT0xTEzXfyEo8KW2qYYvPP1h83BIxsbR/J3Swt35UQVofv+4\n" + + "JgO0FIdgB+iLEcjUh5+60xslylqWE+9bSWm4f06OXuv78tq5NYPZKku/3i4tqLRp\n" + + "gH2rTtjX7Q4olSS7GdAgfiA2AnDZAbMtxtsnTt/QFpYQqhlkqHVDwgkGP7C8aMBD\n" + + "RH0UIQCPxUkhwhtNmVyHO42r6oHXselZoVU6XRHuhogrGxPf/pzDUvrKBiJhsZQQ\n" + + "oEu+pZCwkFLiNwUoq1G2oDpkkdBWB0JcBXB2Txa536ezFFWZYc0=\n" + "-----END CERTIFICATE-----") + .getBytes(); + + public static final byte[] EMAIL_ALT_SUBJECT_NAME = ("-----BEGIN CERTIFICATE-----\n" + + "MIIDpTCCAo2gAwIBAgIJANqkMEtlkelbMA0GCSqGSIb3DQEBCwUAMHAxCzAJBgNV\n" + + "BAYTAlVTMQswCQYDVQQIDAJWQTERMA8GA1UEBwwIU29tZUNpdHkxEjAQBgNVBAoM\n" + + "CU15Q29tcGFueTETMBEGA1UECwwKTXlEaXZpc2lvbjEYMBYGA1UEAwwPd3d3LmNv\n" + + "bXBhbnkuY29tMB4XDTE4MDIxNTA3MjkzMFoXDTIwMDIxNTA3MjkzMFowcDELMAkG\n" + + "A1UEBhMCVVMxCzAJBgNVBAgMAlZBMREwDwYDVQQHDAhTb21lQ2l0eTESMBAGA1UE\n" + + "CgwJTXlDb21wYW55MRMwEQYDVQQLDApNeURpdmlzaW9uMRgwFgYDVQQDDA93d3cu\n" + + "Y29tcGFueS5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC4v6Oq\n" + + "Ua0goRVn1cmT7MOpJhXFm3A70bTpvJIRpEjtGIz99hb34/9r5AYyf1VhKyWmBq24\n" + + "XNcOJ59XOlyjjbm2Tl811ufTOdcNbPadoVBmMt4039OSUFpVb4wAw2XPWLTCG2h1\n" + + "HNj9GuFHmwcDsg5EiIRrhDGQm2LLLAGoe5PdReoMZCeeWzNWvKTCV14pyRzwQhJL\n" + + "F1OmzLYzovbPfB8LZVhQgDbLsh034FScivf2oKDB+NEzAEagNpnrFR0MFLWGYsu1\n" + + "nWD5RiZi78HFGiibmhH7QrEPfGlo2eofuUga6naoBUROqkmMCIL8n1HZ/Ur0oGny\n" + + "vQCj1AyrfOhuVC53AgMBAAGjQjBAMAsGA1UdDwQEAwIEMDATBgNVHSUEDDAKBggr\n" + + "BgEFBQcDATAcBgNVHREEFTATgRFlbWFpbEBleGFtcGxlLmNvbTANBgkqhkiG9w0B\n" + + "AQsFAAOCAQEAZ0IsqRrsEmJ6Fa9Yo6PQtrKJrejN2TTDddVgyLQdokzWh/25JFad\n" + + "NCMYPH5KjTUyKf96hJDlDayjbKk1PMMhSZMU5OG9NOuGMH/dQttruG1ojse7KIKg\n" + + "yHDQrfq5Exxgfa7CMHRKAoTCY7JZhSLyVbTMVhmGfuUDad/RA86ZisXycp0ZmS97\n" + + "qDkAmzFL0sL0ZUWNNUh4ZUWvCUZwiuN08z70NjGqXMTDCf68p3SYxbII0xTfScgf\n" + + "aQ/A/hD7IbGGTexeoTwpEj01DNvefbQV6//neo32/R5XD0D5jn3TCgZcMThA6H3a\n" + + "VkEghVg+s7uMfL/UEebOBQWXQJ/uVoknMA==\n" + "-----END CERTIFICATE-----").getBytes(); + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/FileChangeWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/FileChangeWatcherTest.java new file mode 100644 index 0000000..619ab1d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/FileChangeWatcherTest.java @@ -0,0 +1,265 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.StandardWatchEventKinds; +import java.nio.file.WatchEvent; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.test.ClientBase; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileChangeWatcherTest extends ZKTestCase { + + private static File tempDir; + private static File tempFile; + + private static final Logger LOG = LoggerFactory.getLogger(FileChangeWatcherTest.class); + + private static final long FS_TIMEOUT = 30000L; + + @BeforeClass + public static void createTempFile() throws IOException { + tempDir = ClientBase.createEmptyTestDir(); + tempFile = File.createTempFile("zk_test_", "", tempDir); + tempFile.deleteOnExit(); + } + + @AfterClass + public static void cleanupTempDir() { + try { + FileUtils.deleteDirectory(tempDir); + } catch (IOException e) { + // ignore + } + } + + @Test + public void testCallbackWorksOnFileChanges() throws IOException, InterruptedException { + FileChangeWatcher watcher = null; + try { + final List> events = new ArrayList<>(); + watcher = new FileChangeWatcher(tempDir.toPath(), event -> { + LOG.info("Got an update: {} {}", event.kind(), event.context()); + // Filter out the extra ENTRY_CREATE events that are + // sometimes seen at the start. Even though we create the watcher + // after the file exists, sometimes we still get a create event. + if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) { + return; + } + synchronized (events) { + events.add(event); + events.notifyAll(); + } + }); + watcher.start(); + watcher.waitForState(FileChangeWatcher.State.RUNNING); + Thread.sleep(1000L); // TODO hack + for (int i = 0; i < 3; i++) { + LOG.info("Modifying file, attempt {}", (i + 1)); + FileUtils.writeStringToFile(tempFile, "Hello world " + i + "\n", StandardCharsets.UTF_8, true); + synchronized (events) { + if (events.size() < i + 1) { + events.wait(FS_TIMEOUT); + } + assertEquals("Wrong number of events", i + 1, events.size()); + WatchEvent event = events.get(i); + assertEquals(StandardWatchEventKinds.ENTRY_MODIFY, event.kind()); + assertEquals(tempFile.getName(), event.context().toString()); + } + } + } finally { + if (watcher != null) { + watcher.stop(); + watcher.waitForState(FileChangeWatcher.State.STOPPED); + } + } + } + + @Test + public void testCallbackWorksOnFileTouched() throws IOException, InterruptedException { + FileChangeWatcher watcher = null; + try { + final List> events = new ArrayList<>(); + watcher = new FileChangeWatcher(tempDir.toPath(), event -> { + LOG.info("Got an update: {} {}", event.kind(), event.context()); + // Filter out the extra ENTRY_CREATE events that are + // sometimes seen at the start. Even though we create the watcher + // after the file exists, sometimes we still get a create event. + if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) { + return; + } + synchronized (events) { + events.add(event); + events.notifyAll(); + } + }); + watcher.start(); + watcher.waitForState(FileChangeWatcher.State.RUNNING); + Thread.sleep(1000L); // TODO hack + LOG.info("Touching file"); + FileUtils.touch(tempFile); + synchronized (events) { + if (events.isEmpty()) { + events.wait(FS_TIMEOUT); + } + assertFalse(events.isEmpty()); + WatchEvent event = events.get(0); + assertEquals(StandardWatchEventKinds.ENTRY_MODIFY, event.kind()); + assertEquals(tempFile.getName(), event.context().toString()); + } + } finally { + if (watcher != null) { + watcher.stop(); + watcher.waitForState(FileChangeWatcher.State.STOPPED); + } + } + } + + @Test + public void testCallbackWorksOnFileAdded() throws IOException, InterruptedException { + FileChangeWatcher watcher = null; + try { + final List> events = new ArrayList<>(); + watcher = new FileChangeWatcher(tempDir.toPath(), event -> { + LOG.info("Got an update: {} {}", event.kind(), event.context()); + synchronized (events) { + events.add(event); + events.notifyAll(); + } + }); + watcher.start(); + watcher.waitForState(FileChangeWatcher.State.RUNNING); + Thread.sleep(1000L); // TODO hack + File tempFile2 = File.createTempFile("zk_test_", "", tempDir); + tempFile2.deleteOnExit(); + synchronized (events) { + if (events.isEmpty()) { + events.wait(FS_TIMEOUT); + } + assertFalse(events.isEmpty()); + WatchEvent event = events.get(0); + assertEquals(StandardWatchEventKinds.ENTRY_CREATE, event.kind()); + assertEquals(tempFile2.getName(), event.context().toString()); + } + } finally { + if (watcher != null) { + watcher.stop(); + watcher.waitForState(FileChangeWatcher.State.STOPPED); + } + } + } + + @Test + public void testCallbackWorksOnFileDeleted() throws IOException, InterruptedException { + FileChangeWatcher watcher = null; + try { + final List> events = new ArrayList<>(); + watcher = new FileChangeWatcher(tempDir.toPath(), event -> { + LOG.info("Got an update: {} {}", event.kind(), event.context()); + // Filter out the extra ENTRY_CREATE events that are + // sometimes seen at the start. Even though we create the watcher + // after the file exists, sometimes we still get a create event. + if (StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())) { + return; + } + synchronized (events) { + events.add(event); + events.notifyAll(); + } + }); + watcher.start(); + watcher.waitForState(FileChangeWatcher.State.RUNNING); + Thread.sleep(1000L); // TODO hack + tempFile.delete(); + synchronized (events) { + if (events.isEmpty()) { + events.wait(FS_TIMEOUT); + } + assertFalse(events.isEmpty()); + WatchEvent event = events.get(0); + assertEquals(StandardWatchEventKinds.ENTRY_DELETE, event.kind()); + assertEquals(tempFile.getName(), event.context().toString()); + } + } finally { + if (watcher != null) { + watcher.stop(); + watcher.waitForState(FileChangeWatcher.State.STOPPED); + } + } + } + + @Test + public void testCallbackErrorDoesNotCrashWatcherThread() throws IOException, InterruptedException { + FileChangeWatcher watcher = null; + try { + final AtomicInteger callCount = new AtomicInteger(0); + watcher = new FileChangeWatcher(tempDir.toPath(), event -> { + LOG.info("Got an update: {} {}", event.kind(), event.context()); + int oldValue; + synchronized (callCount) { + oldValue = callCount.getAndIncrement(); + callCount.notifyAll(); + } + if (oldValue == 0) { + throw new RuntimeException("This error should not crash the watcher thread"); + } + }); + watcher.start(); + watcher.waitForState(FileChangeWatcher.State.RUNNING); + Thread.sleep(1000L); // TODO hack + LOG.info("Modifying file"); + FileUtils.writeStringToFile(tempFile, "Hello world\n", StandardCharsets.UTF_8, true); + synchronized (callCount) { + while (callCount.get() == 0) { + callCount.wait(FS_TIMEOUT); + } + } + LOG.info("Modifying file again"); + FileUtils.writeStringToFile(tempFile, "Hello world again\n", StandardCharsets.UTF_8, true); + synchronized (callCount) { + if (callCount.get() == 1) { + callCount.wait(FS_TIMEOUT); + } + } + // The value of callCount can exceed 1 only if the callback thread + // survives the exception thrown by the first callback. + assertTrue(callCount.get() > 1); + } finally { + if (watcher != null) { + watcher.stop(); + watcher.waitForState(FileChangeWatcher.State.STOPPED); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProviderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProviderTest.java new file mode 100644 index 0000000..52640e5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/FileKeyStoreLoaderBuilderProviderTest.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertTrue; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class FileKeyStoreLoaderBuilderProviderTest extends ZKTestCase { + + @Test + public void testGetBuilderForJKSFileType() { + FileKeyStoreLoader.Builder builder = FileKeyStoreLoaderBuilderProvider.getBuilderForKeyStoreFileType(KeyStoreFileType.JKS); + assertTrue(builder instanceof JKSFileLoader.Builder); + } + + @Test + public void testGetBuilderForPEMFileType() { + FileKeyStoreLoader.Builder builder = FileKeyStoreLoaderBuilderProvider.getBuilderForKeyStoreFileType(KeyStoreFileType.PEM); + assertTrue(builder instanceof PEMFileLoader.Builder); + } + + @Test + public void testGetBuilderForPKCS12FileType() { + FileKeyStoreLoader.Builder builder = FileKeyStoreLoaderBuilderProvider.getBuilderForKeyStoreFileType(KeyStoreFileType.PKCS12); + assertTrue(builder instanceof PKCS12FileLoader.Builder); + } + + @Test(expected = NullPointerException.class) + public void testGetBuilderForNullFileType() { + FileKeyStoreLoaderBuilderProvider.getBuilderForKeyStoreFileType(null); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/JKSFileLoaderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/JKSFileLoaderTest.java new file mode 100644 index 0000000..7a349bc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/JKSFileLoaderTest.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.security.KeyStore; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class JKSFileLoaderTest extends BaseX509ParameterizedTestCase { + + @Parameterized.Parameters + public static Collection params() { + return BaseX509ParameterizedTestCase.defaultParams(); + } + + public JKSFileLoaderTest( + final X509KeyType caKeyType, final X509KeyType certKeyType, final String keyPassword, final Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder().setTempDir(tempDir).setKeyStorePassword(keyPassword).setKeyStoreKeyType(certKeyType).setTrustStorePassword(keyPassword).setTrustStoreKeyType(caKeyType).build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Test + public void testLoadKeyStore() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + KeyStore ks = new JKSFileLoader.Builder().setKeyStorePath(path).setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); + assertEquals(1, ks.size()); + } + + @Test(expected = Exception.class) + public void testLoadKeyStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + new JKSFileLoader.Builder().setKeyStorePath(path).setKeyStorePassword("wrong password").build().loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + new JKSFileLoader.Builder().setKeyStorePath(path + + ".does_not_exist").setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadKeyStoreWithNullFilePath() throws Exception { + new JKSFileLoader.Builder().setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFileType() throws Exception { + // Trying to load a PEM file with JKS loader should fail + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new JKSFileLoader.Builder().setKeyStorePath(path).setKeyStorePassword(x509TestContext.getKeyStorePassword()).build().loadKeyStore(); + } + + @Test + public void testLoadTrustStore() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + KeyStore ts = new JKSFileLoader.Builder().setTrustStorePath(path).setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); + assertEquals(1, ts.size()); + } + + @Test(expected = Exception.class) + public void testLoadTrustStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + new JKSFileLoader.Builder().setTrustStorePath(path).setTrustStorePassword("wrong password").build().loadTrustStore(); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + new JKSFileLoader.Builder().setTrustStorePath(path + + ".does_not_exist").setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadTrustStoreWithNullFilePath() throws Exception { + new JKSFileLoader.Builder().setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFileType() throws Exception { + // Trying to load a PEM file with JKS loader should fail + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new JKSFileLoader.Builder().setTrustStorePath(path).setTrustStorePassword(x509TestContext.getTrustStorePassword()).build().loadTrustStore(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/KeyStoreFileTypeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/KeyStoreFileTypeTest.java new file mode 100644 index 0000000..87c4c6d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/KeyStoreFileTypeTest.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class KeyStoreFileTypeTest extends ZKTestCase { + + @Test + public void testGetPropertyValue() { + assertEquals("PEM", KeyStoreFileType.PEM.getPropertyValue()); + assertEquals("JKS", KeyStoreFileType.JKS.getPropertyValue()); + assertEquals("PKCS12", KeyStoreFileType.PKCS12.getPropertyValue()); + assertEquals("BCFKS", KeyStoreFileType.BCFKS.getPropertyValue()); + } + + @Test + public void testFromPropertyValue() { + assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromPropertyValue("PEM")); + assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromPropertyValue("JKS")); + assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromPropertyValue("PKCS12")); + assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromPropertyValue("BCFKS")); + assertNull(KeyStoreFileType.fromPropertyValue("")); + assertNull(KeyStoreFileType.fromPropertyValue(null)); + } + + @Test + public void testFromPropertyValueIgnoresCase() { + assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromPropertyValue("pem")); + assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromPropertyValue("jks")); + assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromPropertyValue("pkcs12")); + assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromPropertyValue("bcfks")); + assertNull(KeyStoreFileType.fromPropertyValue("")); + assertNull(KeyStoreFileType.fromPropertyValue(null)); + } + + @Test(expected = IllegalArgumentException.class) + public void testFromPropertyValueThrowsOnBadPropertyValue() { + KeyStoreFileType.fromPropertyValue("foobar"); + } + + @Test + public void testFromFilename() { + assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromFilename("mykey.jks")); + assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.jks")); + assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromFilename("mykey.pem")); + assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.pem")); + assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromFilename("mykey.p12")); + assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.p12")); + assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromFilename("mykey.bcfks")); + assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromFilename("/path/to/key/dir/mykey.bcfks")); + } + + @Test(expected = IllegalArgumentException.class) + public void testFromFilenameThrowsOnBadFileExtension() { + KeyStoreFileType.fromFilename("prod.key"); + } + + @Test + public void testFromPropertyValueOrFileName() { + // Property value takes precedence if provided + assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromPropertyValueOrFileName("JKS", "prod.key")); + assertEquals(KeyStoreFileType.PEM, KeyStoreFileType.fromPropertyValueOrFileName("PEM", "prod.key")); + assertEquals(KeyStoreFileType.PKCS12, KeyStoreFileType.fromPropertyValueOrFileName("PKCS12", "prod.key")); + assertEquals(KeyStoreFileType.BCFKS, KeyStoreFileType.fromPropertyValueOrFileName("BCFKS", "prod.key")); + // Falls back to filename detection if no property value + assertEquals(KeyStoreFileType.JKS, KeyStoreFileType.fromPropertyValueOrFileName("", "prod.jks")); + } + + @Test(expected = IllegalArgumentException.class) + public void testFromPropertyValueOrFileNameThrowsOnBadPropertyValue() { + KeyStoreFileType.fromPropertyValueOrFileName("foobar", "prod.jks"); + } + + @Test(expected = IllegalArgumentException.class) + public void testFromPropertyValueOrFileNameThrowsOnBadFileExtension() { + KeyStoreFileType.fromPropertyValueOrFileName("", "prod.key"); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/NetUtilsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/NetUtilsTest.java new file mode 100644 index 0000000..e06b7d1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/NetUtilsTest.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import java.net.InetSocketAddress; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class NetUtilsTest extends ZKTestCase { + + private Integer port = 1234; + private String v4addr = "127.0.0.1"; + private String v6addr = "[0:0:0:0:0:0:0:1]"; + private String v6addr2 = "[2600:0:0:0:0:0:0:0]"; + private String v4local = v4addr + ":" + port.toString(); + private String v6local = v6addr + ":" + port.toString(); + private String v6ext = v6addr2 + ":" + port.toString(); + + @Test + public void testFormatInetAddrGoodIpv4() { + InetSocketAddress isa = new InetSocketAddress(v4addr, port); + assertEquals("127.0.0.1:1234", NetUtils.formatInetAddr(isa)); + } + + @Test + public void testFormatInetAddrGoodIpv6Local() { + // Have to use the expanded address here, hence not using v6addr in instantiation + InetSocketAddress isa = new InetSocketAddress("::1", port); + assertEquals(v6local, NetUtils.formatInetAddr(isa)); + } + + @Test + public void testFormatInetAddrGoodIpv6Ext() { + // Have to use the expanded address here, hence not using v6addr in instantiation + InetSocketAddress isa = new InetSocketAddress("2600::", port); + assertEquals(v6ext, NetUtils.formatInetAddr(isa)); + } + + @Test + public void testFormatInetAddrGoodHostname() { + InetSocketAddress isa = new InetSocketAddress("localhost", 1234); + + assertThat(NetUtils.formatInetAddr(isa), anyOf(equalTo(v4local), equalTo(v6local))); + } + + @Test + public void testFormatAddrUnresolved() { + InetSocketAddress isa = InetSocketAddress.createUnresolved("doesnt.exist.com", 1234); + assertEquals("doesnt.exist.com:1234", NetUtils.formatInetAddr(isa)); + } + + @Test(expected = IllegalArgumentException.class) + public void tetGetIPV6HostAndPort_WhenHostDoesNotEndWithBracket() { + NetUtils.getIPV6HostAndPort("[2001:0db8:85a3:0000:0000:8a2e:0370:7334:443"); + } + + @Test(expected = IllegalArgumentException.class) + public void tetGetIPV6HostAndPort_WhenNoPortAfterColon() { + NetUtils.getIPV6HostAndPort("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:"); + } + + @Test(expected = IllegalArgumentException.class) + public void tetGetIPV6HostAndPort_WhenPortIsNotSeparatedProperly() { + NetUtils.getIPV6HostAndPort("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]2181"); + } + + @Test(expected = IllegalArgumentException.class) + public void tetGetIPV6HostAndPort_WhenHostIsEmpty() { + NetUtils.getIPV6HostAndPort("[]:2181"); + } + + @Test + public void tetGetIPV6HostAndPort_EmptyStringArrayIfDoesNotStartWithBracket() { + String[] ipv6HostAndPort = + NetUtils.getIPV6HostAndPort("2001:0db8:85a3:0000:0000:8a2e:0370:7334]"); + assertEquals(0, ipv6HostAndPort.length); + } + + @Test + public void tetGetIPV6HostAndPort_ReturnHostPort() { + String[] ipv6HostAndPort = + NetUtils.getIPV6HostAndPort("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:2181"); + assertEquals(2, ipv6HostAndPort.length); + assertEquals("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ipv6HostAndPort[0]); + assertEquals("2181", ipv6HostAndPort[1]); + } + + @Test + public void tetGetIPV6HostAndPort_ReturnHostPortPort() { + String[] ipv6HostAndPort = + NetUtils.getIPV6HostAndPort("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:2181:3181"); + assertEquals(2, ipv6HostAndPort.length); + assertEquals("2001:0db8:85a3:0000:0000:8a2e:0370:7334", ipv6HostAndPort[0]); + assertEquals("2181:3181", ipv6HostAndPort[1]); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/PEMFileLoaderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PEMFileLoaderTest.java new file mode 100644 index 0000000..05ef12e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PEMFileLoaderTest.java @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class PEMFileLoaderTest extends BaseX509ParameterizedTestCase { + + @Parameterized.Parameters + public static Collection params() { + return BaseX509ParameterizedTestCase.defaultParams(); + } + + public PEMFileLoaderTest( + final X509KeyType caKeyType, + final X509KeyType certKeyType, + final String keyPassword, + final Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStorePassword(keyPassword) + .setKeyStoreKeyType(certKeyType) + .setTrustStorePassword(keyPassword) + .setTrustStoreKeyType(caKeyType) + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Test + public void testLoadKeyStore() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + KeyStore ks = new PEMFileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + assertEquals(1, ks.size()); + } + + @Test(expected = Exception.class) + public void testLoadKeyStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new PEMFileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword("wrong password") + .build() + .loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new PEMFileLoader.Builder() + .setKeyStorePath(path + ".does_not_exist") + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadKeyStoreWithNullFilePath() throws Exception { + new PEMFileLoader.Builder() + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test(expected = KeyStoreException.class) + public void testLoadKeyStoreWithWrongFileType() throws Exception { + // Trying to load a JKS file with PEM loader should fail + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + new PEMFileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test + public void testLoadTrustStore() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + KeyStore ts = new PEMFileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + assertEquals(1, ts.size()); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new PEMFileLoader.Builder() + .setTrustStorePath(path + ".does_not_exist") + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadTrustStoreWithNullFilePath() throws Exception { + new PEMFileLoader.Builder() + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + @Test + public void testLoadTrustStoreWithWrongFileType() throws Exception { + // Trying to load a JKS file with PEM loader should fail + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(); + KeyStore ts = new PEMFileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + assertEquals(0, ts.size()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/PKCS12FileLoaderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PKCS12FileLoaderTest.java new file mode 100644 index 0000000..63d6683 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PKCS12FileLoaderTest.java @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.security.KeyStore; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class PKCS12FileLoaderTest extends BaseX509ParameterizedTestCase { + + @Parameterized.Parameters + public static Collection params() { + return BaseX509ParameterizedTestCase.defaultParams(); + } + + public PKCS12FileLoaderTest( + final X509KeyType caKeyType, + final X509KeyType certKeyType, + final String keyPassword, + final Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStorePassword(keyPassword) + .setKeyStoreKeyType(certKeyType) + .setTrustStorePassword(keyPassword) + .setTrustStoreKeyType(caKeyType) + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Test + public void testLoadKeyStore() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(); + KeyStore ks = new PKCS12FileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + assertEquals(1, ks.size()); + } + + @Test(expected = Exception.class) + public void testLoadKeyStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(); + new PKCS12FileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword("wrong password") + .build() + .loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(); + new PKCS12FileLoader.Builder() + .setKeyStorePath(path + ".does_not_exist") + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadKeyStoreWithNullFilePath() throws Exception { + new PKCS12FileLoader.Builder() + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test(expected = IOException.class) + public void testLoadKeyStoreWithWrongFileType() throws Exception { + // Trying to load a PEM file with PKCS12 loader should fail + String path = x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new PKCS12FileLoader.Builder() + .setKeyStorePath(path) + .setKeyStorePassword(x509TestContext.getKeyStorePassword()) + .build() + .loadKeyStore(); + } + + @Test + public void testLoadTrustStore() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(); + KeyStore ts = new PKCS12FileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + assertEquals(1, ts.size()); + } + + @Test(expected = Exception.class) + public void testLoadTrustStoreWithWrongPassword() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(); + new PKCS12FileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword("wrong password") + .build() + .loadTrustStore(); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFilePath() throws Exception { + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(); + new PKCS12FileLoader.Builder() + .setTrustStorePath(path + ".does_not_exist") + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + @Test(expected = NullPointerException.class) + public void testLoadTrustStoreWithNullFilePath() throws Exception { + new PKCS12FileLoader.Builder() + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + + @Test(expected = IOException.class) + public void testLoadTrustStoreWithWrongFileType() throws Exception { + // Trying to load a PEM file with PKCS12 loader should fail + String path = x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(); + new PKCS12FileLoader.Builder() + .setTrustStorePath(path) + .setTrustStorePassword(x509TestContext.getTrustStorePassword()) + .build() + .loadTrustStore(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/PathTrieTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PathTrieTest.java new file mode 100644 index 0000000..c766164 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PathTrieTest.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.Before; +import org.junit.Test; + +public class PathTrieTest { + + private PathTrie pathTrie; + + @Before + public void before() { + this.pathTrie = new PathTrie(); + } + + @Test(expected = NullPointerException.class) + public void addNullPath() { + this.pathTrie.addPath(null); + } + + @Test(expected = IllegalArgumentException.class) + public void addIllegalPath() { + this.pathTrie.addPath(""); + } + + @Test + public void addPathToRoot() { + this.pathTrie.addPath("node1"); + assertTrue(this.pathTrie.existsNode("/node1")); + } + + @Test + public void addPathToRootLeaves() { + this.pathTrie.addPath("node1"); + this.pathTrie.addPath("node1/node2"); + this.pathTrie.addPath("node1/node3"); + assertTrue(this.pathTrie.existsNode("/node1")); + assertTrue(this.pathTrie.existsNode("/node1/node2")); + assertTrue(this.pathTrie.existsNode("/node1/node3")); + } + + @Test(expected = NullPointerException.class) + public void deleteNullPath() { + this.pathTrie.deletePath(null); + } + + @Test(expected = IllegalArgumentException.class) + public void deleteIllegalPath() { + this.pathTrie.deletePath(""); + } + + @Test + public void deletePathFromRoot() { + this.pathTrie.addPath("node1"); + this.pathTrie.deletePath("node1"); + assertFalse(this.pathTrie.existsNode("/node1")); + } + + @Test + public void deletePathFromRootLeaves() { + this.pathTrie.addPath("node1"); + this.pathTrie.addPath("node1/node2"); + this.pathTrie.addPath("node1/node3"); + + this.pathTrie.deletePath("node1/node3"); + + assertTrue(this.pathTrie.existsNode("/node1")); + assertTrue(this.pathTrie.existsNode("/node1/node2")); + assertFalse(this.pathTrie.existsNode("/node1/node3")); + + this.pathTrie.deletePath("node1/node2"); + + assertTrue(this.pathTrie.existsNode("/node1")); + assertFalse(this.pathTrie.existsNode("/node1/node2")); + + this.pathTrie.deletePath("node1"); + assertFalse(this.pathTrie.existsNode("/node1")); + } + + @Test + public void deletePathDoesNotExist() { + this.pathTrie.addPath("node1"); + this.pathTrie.addPath("node1/node2"); + + this.pathTrie.deletePath("node1/node3"); + + assertTrue(this.pathTrie.existsNode("/node1")); + assertTrue(this.pathTrie.existsNode("/node1/node2")); + } + + @Test + public void deleteRootPath() { + this.pathTrie.addPath("node1"); + this.pathTrie.addPath("node1/node2"); + this.pathTrie.addPath("node1/node3"); + + // Nodes are only removed from the trie if they are a leaf node + this.pathTrie.deletePath("node1"); + + assertTrue(this.pathTrie.existsNode("/node1")); + assertTrue(this.pathTrie.existsNode("/node1/node2")); + assertTrue(this.pathTrie.existsNode("/node1/node3")); + } + + @Test(expected = NullPointerException.class) + public void findMaxPrefixNullPath() { + this.pathTrie.findMaxPrefix(null); + } + + @Test + public void findMaxPrefixRootPath() { + assertEquals("/", this.pathTrie.findMaxPrefix("/")); + } + + @Test + public void findMaxPrefixChildren() { + this.pathTrie.addPath("node1"); + this.pathTrie.addPath("node1/node2"); + this.pathTrie.addPath("node1/node3"); + + assertEquals("/node1", this.pathTrie.findMaxPrefix("/node1")); + assertEquals("/node1/node2", this.pathTrie.findMaxPrefix("/node1/node2")); + assertEquals("/node1/node3", this.pathTrie.findMaxPrefix("/node1/node3")); + } + + @Test + public void findMaxPrefixChildrenPrefix() { + this.pathTrie.addPath("node1"); + + assertEquals("/node1", this.pathTrie.findMaxPrefix("/node1/node2")); + assertEquals("/node1", this.pathTrie.findMaxPrefix("/node1/node3")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/PathUtilsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PathUtilsTest.java new file mode 100644 index 0000000..0d08286 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/PathUtilsTest.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class PathUtilsTest extends ZKTestCase { + + @Test + public void testValidatePath_ValidPath() { + PathUtils.validatePath("/this is / a valid/path"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_Null() { + PathUtils.validatePath(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_EmptyString() { + PathUtils.validatePath(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_NotAbsolutePath() { + PathUtils.validatePath("not/valid"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_EndsWithSlash() { + PathUtils.validatePath("/ends/with/slash/"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_ContainsNullCharacter() { + PathUtils.validatePath("/test\u0000"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_DoubleSlash() { + PathUtils.validatePath("/double//slash"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_SinglePeriod() { + PathUtils.validatePath("/single/./period"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_DoublePeriod() { + PathUtils.validatePath("/double/../period"); + } + + @Test + public void testValidatePath_NameContainingPeriod() { + // A period that isn't on its own is ok + PathUtils.validatePath("/name/with.period."); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_0x01() { + PathUtils.validatePath("/test\u0001"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_0x1F() { + PathUtils.validatePath("/test\u001F"); + } + + @Test // The first allowable character + public void testValidatePath_0x20() { + PathUtils.validatePath("/test\u0020"); + } + + @Test + public void testValidatePath_0x7e() { + // The last valid ASCII character + PathUtils.validatePath("/test\u007e"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_0x7f() { + PathUtils.validatePath("/test\u007f"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_0x9f() { + PathUtils.validatePath("/test\u009f"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_ud800() { + PathUtils.validatePath("/test\ud800"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_uf8ff() { + PathUtils.validatePath("/test\uf8ff"); + } + + @Test + public void testValidatePath_HighestAllowableChar() { + PathUtils.validatePath("/test\uffef"); + } + + @Test(expected = IllegalArgumentException.class) + public void testValidatePath_SupplementaryChar() { + PathUtils.validatePath("/test\ufff0"); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java new file mode 100644 index 0000000..85077be --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/TimeTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertEquals; +import java.util.Calendar; +import java.util.Date; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +/** + * Command line program for demonstrating robustness to clock + * changes. + *

      + * How to run: + * ant clean compile-test + * echo build/test/lib/*.jar build/lib/*.jar build/classes build/test/classes | sed -e 's/ /:/g' > cp + * java -cp $(cat cp) org.apache.zookeeper.common.TimeTest | tee log-without-patch + *

      + * After test program starts, in another window, do commands: + * date -s '+1hour' + * date -s '-1hour' + *

      + * As long as there isn't any expired event, the experiment is successful. + */ +public class TimeTest extends ClientBase { + + private static final long mt0 = System.currentTimeMillis(); + private static final long nt0 = Time.currentElapsedTime(); + + private static AtomicInteger watchCount = new AtomicInteger(0); + + public static void main(String[] args) throws Exception { + System.out.print("Starting\n"); + final TimeTest test = new TimeTest(); + System.out.print("After construct\n"); + test.setUp(); + ZooKeeper zk = test.createClient(); + zk.create("/ephemeral", new byte[]{1, 2, 3}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + while (Time.currentElapsedTime() - nt0 < 100000) { + System.out.printf("%d\t%s\n", + discrepancy(), + zk.exists("/ephemeral", watchCount.get() == 0 ? createWatcher() : null) != null); + waitByYielding(500); + } + } + + private static Watcher createWatcher() { + watchCount.incrementAndGet(); + return event -> { + watchCount.decrementAndGet(); + System.out.printf("%d event = %s\n", discrepancy(), event); + }; + + } + + private static void waitByYielding(long delay) { + long t0 = Time.currentElapsedTime(); + while (Time.currentElapsedTime() < t0 + delay) { + Thread.yield(); + } + } + + private static long discrepancy() { + return (System.currentTimeMillis() - mt0) - (Time.currentElapsedTime() - nt0); + } + + @Test + public void testElapsedTimeToDate() throws Exception { + long walltime = Time.currentWallTime(); + long elapsedTime = Time.currentElapsedTime(); + Thread.sleep(200); + + Calendar cal = Calendar.getInstance(); + cal.setTime(Time.elapsedTimeToDate(elapsedTime)); + int calculatedDate = cal.get(Calendar.HOUR_OF_DAY); + cal.setTime(new Date(walltime)); + int realDate = cal.get(Calendar.HOUR_OF_DAY); + + assertEquals(calculatedDate, realDate); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509KeyType.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509KeyType.java new file mode 100644 index 0000000..9c97e96 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509KeyType.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +/** + * Represents a type of key pair used for X509 certs in tests. The two options are RSA or EC (elliptic curve). + */ +public enum X509KeyType { + RSA, + EC +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509TestContext.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509TestContext.java new file mode 100644 index 0000000..3238026 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509TestContext.java @@ -0,0 +1,525 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static java.util.Objects.requireNonNull; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.lang.invoke.MethodHandles; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.security.KeyPair; +import java.security.Security; +import java.security.cert.X509Certificate; +import java.util.Arrays; +import org.apache.commons.io.FileUtils; +import org.bouncycastle.asn1.x500.X500NameBuilder; +import org.bouncycastle.asn1.x500.style.BCStyle; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.operator.OperatorCreationException; + +/** + * This class simplifies the creation of certificates and private keys for SSL/TLS connections. + */ +public class X509TestContext { + + private static final String TRUST_STORE_PREFIX = "zk_test_ca"; + private static final String KEY_STORE_PREFIX = "zk_test_key"; + + private final File tempDir; + + private final X509KeyType trustStoreKeyType; + private final KeyPair trustStoreKeyPair; + private final long trustStoreCertExpirationMillis; + private final X509Certificate trustStoreCertificate; + private final String trustStorePassword; + private File trustStoreJksFile; + private File trustStorePemFile; + private File trustStorePkcs12File; + private File trustStoreBcfksFile; + + private final X509KeyType keyStoreKeyType; + private final KeyPair keyStoreKeyPair; + private final long keyStoreCertExpirationMillis; + private final X509Certificate keyStoreCertificate; + private final String keyStorePassword; + private File keyStoreJksFile; + private File keyStorePemFile; + private File keyStorePkcs12File; + private File keyStoreBcfksFile; + + private final Boolean hostnameVerification; + + /** + * Constructor is intentionally private, use the Builder class instead. + * @param tempDir the directory in which key store and trust store temp files will be written. + * @param trustStoreKeyPair the key pair for the trust store. + * @param trustStoreCertExpirationMillis the expiration of the trust store cert, in milliseconds from now. + * @param trustStorePassword the password to protect a JKS trust store (ignored for PEM trust stores). + * @param keyStoreKeyPair the key pair for the key store. + * @param keyStoreCertExpirationMillis the expiration of the key store cert, in milliseconds from now. + * @param keyStorePassword the password to protect the key store private key. + * @throws IOException + * @throws GeneralSecurityException + * @throws OperatorCreationException + */ + private X509TestContext(File tempDir, KeyPair trustStoreKeyPair, long trustStoreCertExpirationMillis, String trustStorePassword, KeyPair keyStoreKeyPair, long keyStoreCertExpirationMillis, String keyStorePassword, Boolean hostnameVerification) throws IOException, GeneralSecurityException, OperatorCreationException { + if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { + throw new IllegalStateException("BC Security provider was not found"); + } + this.tempDir = requireNonNull(tempDir); + if (!tempDir.isDirectory()) { + throw new IllegalArgumentException("Not a directory: " + tempDir); + } + this.trustStoreKeyPair = requireNonNull(trustStoreKeyPair); + this.trustStoreKeyType = keyPairToType(trustStoreKeyPair); + this.trustStoreCertExpirationMillis = trustStoreCertExpirationMillis; + this.trustStorePassword = requireNonNull(trustStorePassword); + this.keyStoreKeyPair = requireNonNull(keyStoreKeyPair); + this.keyStoreKeyType = keyPairToType(keyStoreKeyPair); + this.keyStoreCertExpirationMillis = keyStoreCertExpirationMillis; + this.keyStorePassword = requireNonNull(keyStorePassword); + + X500NameBuilder caNameBuilder = new X500NameBuilder(BCStyle.INSTANCE); + caNameBuilder.addRDN(BCStyle.CN, MethodHandles.lookup().lookupClass().getCanonicalName() + " Root CA"); + trustStoreCertificate = X509TestHelpers.newSelfSignedCACert(caNameBuilder.build(), trustStoreKeyPair, trustStoreCertExpirationMillis); + + X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); + nameBuilder.addRDN(BCStyle.CN, MethodHandles.lookup().lookupClass().getCanonicalName() + " Zookeeper Test"); + keyStoreCertificate = X509TestHelpers.newCert(trustStoreCertificate, trustStoreKeyPair, nameBuilder.build(), keyStoreKeyPair.getPublic(), keyStoreCertExpirationMillis); + trustStorePkcs12File = trustStorePemFile = trustStoreJksFile = null; + keyStorePkcs12File = keyStorePemFile = keyStoreJksFile = null; + + this.hostnameVerification = hostnameVerification; + } + + /** + * Returns the X509KeyType of the given key pair. + * @param keyPair the key pair. + * @return X509KeyType.RSA if given an RSA key pair, and X509KeyType.EC otherwise. + */ + private X509KeyType keyPairToType(KeyPair keyPair) { + if (keyPair.getPrivate().getAlgorithm().contains("RSA")) { + return X509KeyType.RSA; + } else { + return X509KeyType.EC; + } + } + + public File getTempDir() { + return tempDir; + } + + public X509KeyType getTrustStoreKeyType() { + return trustStoreKeyType; + } + + public KeyPair getTrustStoreKeyPair() { + return trustStoreKeyPair; + } + + public long getTrustStoreCertExpirationMillis() { + return trustStoreCertExpirationMillis; + } + + public X509Certificate getTrustStoreCertificate() { + return trustStoreCertificate; + } + + public String getTrustStorePassword() { + return trustStorePassword; + } + + /** + * Returns the path to the trust store file in the given format (JKS or PEM). Note that the file is created lazily, + * the first time this method is called. The trust store file is temporary and will be deleted on exit. + * @param storeFileType the store file type (JKS or PEM). + * @return the path to the trust store file. + * @throws IOException if there is an error creating the trust store file. + */ + public File getTrustStoreFile(KeyStoreFileType storeFileType) throws IOException { + switch (storeFileType) { + case JKS: + return getTrustStoreJksFile(); + case PEM: + return getTrustStorePemFile(); + case PKCS12: + return getTrustStorePkcs12File(); + case BCFKS: + return getTrustStoreBcfksFile(); + default: + throw new IllegalArgumentException("Invalid trust store type: " + + storeFileType + + ", must be one of: " + + Arrays.toString(KeyStoreFileType.values())); + } + } + + private File getTrustStoreJksFile() throws IOException { + if (trustStoreJksFile == null) { + File trustStoreJksFile = File.createTempFile(TRUST_STORE_PREFIX, KeyStoreFileType.JKS.getDefaultFileExtension(), tempDir); + trustStoreJksFile.deleteOnExit(); + try (final FileOutputStream trustStoreOutputStream = new FileOutputStream(trustStoreJksFile)) { + byte[] bytes = X509TestHelpers.certToJavaTrustStoreBytes(trustStoreCertificate, trustStorePassword); + trustStoreOutputStream.write(bytes); + trustStoreOutputStream.flush(); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.trustStoreJksFile = trustStoreJksFile; + } + return trustStoreJksFile; + } + + private File getTrustStorePemFile() throws IOException { + if (trustStorePemFile == null) { + File trustStorePemFile = File.createTempFile(TRUST_STORE_PREFIX, KeyStoreFileType.PEM.getDefaultFileExtension(), tempDir); + trustStorePemFile.deleteOnExit(); + FileUtils.writeStringToFile(trustStorePemFile, X509TestHelpers.pemEncodeX509Certificate(trustStoreCertificate), StandardCharsets.US_ASCII, false); + this.trustStorePemFile = trustStorePemFile; + } + return trustStorePemFile; + } + + private File getTrustStorePkcs12File() throws IOException { + if (trustStorePkcs12File == null) { + File trustStorePkcs12File = File.createTempFile(TRUST_STORE_PREFIX, KeyStoreFileType.PKCS12.getDefaultFileExtension(), tempDir); + trustStorePkcs12File.deleteOnExit(); + try (final FileOutputStream trustStoreOutputStream = new FileOutputStream(trustStorePkcs12File)) { + byte[] bytes = X509TestHelpers.certToPKCS12TrustStoreBytes(trustStoreCertificate, trustStorePassword); + trustStoreOutputStream.write(bytes); + trustStoreOutputStream.flush(); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.trustStorePkcs12File = trustStorePkcs12File; + } + return trustStorePkcs12File; + } + + private File getTrustStoreBcfksFile() throws IOException { + if (trustStoreBcfksFile == null) { + File trustStoreBcfksFile = File.createTempFile( + TRUST_STORE_PREFIX, KeyStoreFileType.BCFKS.getDefaultFileExtension(), tempDir); + trustStoreBcfksFile.deleteOnExit(); + try (final FileOutputStream trustStoreOutputStream = new FileOutputStream(trustStoreBcfksFile)) { + byte[] bytes = X509TestHelpers.certToBCFKSTrustStoreBytes(trustStoreCertificate, trustStorePassword); + trustStoreOutputStream.write(bytes); + trustStoreOutputStream.flush(); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.trustStoreBcfksFile = trustStoreBcfksFile; + } + return trustStoreBcfksFile; + } + + public X509KeyType getKeyStoreKeyType() { + return keyStoreKeyType; + } + + public KeyPair getKeyStoreKeyPair() { + return keyStoreKeyPair; + } + + public long getKeyStoreCertExpirationMillis() { + return keyStoreCertExpirationMillis; + } + + public X509Certificate getKeyStoreCertificate() { + return keyStoreCertificate; + } + + public String getKeyStorePassword() { + return keyStorePassword; + } + + public boolean isKeyStoreEncrypted() { + return keyStorePassword.length() > 0; + } + + /** + * Returns the path to the key store file in the given format (JKS, PEM, ...). Note that the file is created lazily, + * the first time this method is called. The key store file is temporary and will be deleted on exit. + * @param storeFileType the store file type (JKS, PEM, ...). + * @return the path to the key store file. + * @throws IOException if there is an error creating the key store file. + */ + public File getKeyStoreFile(KeyStoreFileType storeFileType) throws IOException { + switch (storeFileType) { + case JKS: + return getKeyStoreJksFile(); + case PEM: + return getKeyStorePemFile(); + case PKCS12: + return getKeyStorePkcs12File(); + case BCFKS: + return getKeyStoreBcfksFile(); + default: + throw new IllegalArgumentException("Invalid key store type: " + + storeFileType + + ", must be one of: " + + Arrays.toString(KeyStoreFileType.values())); + } + } + + private File getKeyStoreJksFile() throws IOException { + if (keyStoreJksFile == null) { + File keyStoreJksFile = File.createTempFile(KEY_STORE_PREFIX, KeyStoreFileType.JKS.getDefaultFileExtension(), tempDir); + keyStoreJksFile.deleteOnExit(); + try (final FileOutputStream keyStoreOutputStream = new FileOutputStream(keyStoreJksFile)) { + byte[] bytes = X509TestHelpers.certAndPrivateKeyToJavaKeyStoreBytes(keyStoreCertificate, keyStoreKeyPair.getPrivate(), keyStorePassword); + keyStoreOutputStream.write(bytes); + keyStoreOutputStream.flush(); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.keyStoreJksFile = keyStoreJksFile; + } + return keyStoreJksFile; + } + + private File getKeyStorePemFile() throws IOException { + if (keyStorePemFile == null) { + try { + File keyStorePemFile = File.createTempFile(KEY_STORE_PREFIX, KeyStoreFileType.PEM.getDefaultFileExtension(), tempDir); + keyStorePemFile.deleteOnExit(); + FileUtils.writeStringToFile(keyStorePemFile, X509TestHelpers.pemEncodeCertAndPrivateKey(keyStoreCertificate, keyStoreKeyPair.getPrivate(), keyStorePassword), StandardCharsets.US_ASCII, false); + this.keyStorePemFile = keyStorePemFile; + } catch (OperatorCreationException e) { + throw new IOException(e); + } + } + return keyStorePemFile; + } + + private File getKeyStorePkcs12File() throws IOException { + if (keyStorePkcs12File == null) { + File keyStorePkcs12File = File.createTempFile(KEY_STORE_PREFIX, KeyStoreFileType.PKCS12.getDefaultFileExtension(), tempDir); + keyStorePkcs12File.deleteOnExit(); + try (final FileOutputStream keyStoreOutputStream = new FileOutputStream(keyStorePkcs12File)) { + byte[] bytes = X509TestHelpers.certAndPrivateKeyToPKCS12Bytes(keyStoreCertificate, keyStoreKeyPair.getPrivate(), keyStorePassword); + keyStoreOutputStream.write(bytes); + keyStoreOutputStream.flush(); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.keyStorePkcs12File = keyStorePkcs12File; + } + return keyStorePkcs12File; + } + + private File getKeyStoreBcfksFile() throws IOException { + if (keyStoreBcfksFile == null) { + File keyStoreBcfksFile = File.createTempFile( + KEY_STORE_PREFIX, KeyStoreFileType.BCFKS.getDefaultFileExtension(), tempDir); + keyStoreBcfksFile.deleteOnExit(); + try (final FileOutputStream keyStoreOutputStream = new FileOutputStream(keyStoreBcfksFile)) { + byte[] bytes = X509TestHelpers.certAndPrivateKeyToBCFKSBytes( + keyStoreCertificate, keyStoreKeyPair.getPrivate(), keyStorePassword); + keyStoreOutputStream.write(bytes); + keyStoreOutputStream.flush(); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.keyStoreBcfksFile = keyStoreBcfksFile; + } + return keyStoreBcfksFile; + } + + /** + * Sets the SSL system properties such that the given X509Util object can be used to create SSL Contexts that + * will use the trust store and key store files created by this test context. Example usage: + *

      +     *     X509TestContext testContext = ...; // create the test context
      +     *     X509Util x509Util = new QuorumX509Util();
      +     *     testContext.setSystemProperties(x509Util, KeyStoreFileType.JKS, KeyStoreFileType.JKS);
      +     *     // The returned context will use the key store and trust store created by the test context.
      +     *     SSLContext ctx = x509Util.getDefaultSSLContext();
      +     * 
      + * @param x509Util the X509Util. + * @param keyStoreFileType the store file type to use for the key store (JKS, PEM, ...). + * @param trustStoreFileType the store file type to use for the trust store (JKS, PEM, ...). + * @throws IOException if there is an error creating the key store file or trust store file. + */ + public void setSystemProperties(X509Util x509Util, KeyStoreFileType keyStoreFileType, KeyStoreFileType trustStoreFileType) throws IOException { + System.setProperty(x509Util.getSslKeystoreLocationProperty(), this.getKeyStoreFile(keyStoreFileType).getAbsolutePath()); + System.setProperty(x509Util.getSslKeystorePasswdProperty(), this.getKeyStorePassword()); + System.setProperty(x509Util.getSslKeystoreTypeProperty(), keyStoreFileType.getPropertyValue()); + System.setProperty(x509Util.getSslTruststoreLocationProperty(), this.getTrustStoreFile(trustStoreFileType).getAbsolutePath()); + System.setProperty(x509Util.getSslTruststorePasswdProperty(), this.getTrustStorePassword()); + System.setProperty(x509Util.getSslTruststoreTypeProperty(), trustStoreFileType.getPropertyValue()); + if (hostnameVerification != null) { + System.setProperty(x509Util.getSslHostnameVerificationEnabledProperty(), hostnameVerification.toString()); + } else { + System.clearProperty(x509Util.getSslHostnameVerificationEnabledProperty()); + } + } + + /** + * Clears system properties set by + * {@link #setSystemProperties(X509Util, KeyStoreFileType, KeyStoreFileType)}. + * @param x509Util the X509Util to read property keys from. + */ + public void clearSystemProperties(X509Util x509Util) { + System.clearProperty(x509Util.getSslKeystoreLocationProperty()); + System.clearProperty(x509Util.getSslKeystorePasswdProperty()); + System.clearProperty(x509Util.getSslKeystoreTypeProperty()); + System.clearProperty(x509Util.getSslTruststoreLocationProperty()); + System.clearProperty(x509Util.getSslTruststorePasswdProperty()); + System.clearProperty(x509Util.getSslTruststoreTypeProperty()); + System.clearProperty(x509Util.getSslHostnameVerificationEnabledProperty()); + } + + /** + * Builder class, used for creating new instances of X509TestContext. + */ + public static class Builder { + + public static final long DEFAULT_CERT_EXPIRATION_MILLIS = 1000L * 60 * 60 * 24; // 1 day + private File tempDir; + private X509KeyType trustStoreKeyType; + private String trustStorePassword; + private long trustStoreCertExpirationMillis; + private X509KeyType keyStoreKeyType; + private String keyStorePassword; + private long keyStoreCertExpirationMillis; + private Boolean hostnameVerification; + + /** + * Creates an empty builder. + */ + public Builder() { + trustStoreKeyType = X509KeyType.EC; + trustStorePassword = ""; + trustStoreCertExpirationMillis = DEFAULT_CERT_EXPIRATION_MILLIS; + keyStoreKeyType = X509KeyType.EC; + keyStorePassword = ""; + keyStoreCertExpirationMillis = DEFAULT_CERT_EXPIRATION_MILLIS; + hostnameVerification = null; + } + + /** + * Builds a new X509TestContext from this builder. + * @return a new X509TestContext + * @throws IOException + * @throws GeneralSecurityException + * @throws OperatorCreationException + */ + public X509TestContext build() throws IOException, GeneralSecurityException, OperatorCreationException { + KeyPair trustStoreKeyPair = X509TestHelpers.generateKeyPair(trustStoreKeyType); + KeyPair keyStoreKeyPair = X509TestHelpers.generateKeyPair(keyStoreKeyType); + return new X509TestContext(tempDir, trustStoreKeyPair, trustStoreCertExpirationMillis, trustStorePassword, keyStoreKeyPair, keyStoreCertExpirationMillis, keyStorePassword, hostnameVerification); + } + + /** + * Sets the temporary directory. Certificate and private key files will be created in this directory. + * @param tempDir the temp directory. + * @return this Builder. + */ + public Builder setTempDir(File tempDir) { + this.tempDir = tempDir; + return this; + } + + /** + * Sets the trust store key type. The CA key generated for the test context will be of this type. + * @param keyType the key type. + * @return this Builder. + */ + public Builder setTrustStoreKeyType(X509KeyType keyType) { + trustStoreKeyType = keyType; + return this; + } + + /** + * Sets the trust store password. Ignored for PEM trust stores, JKS trust stores will be encrypted with this + * password. + * @param password the password. + * @return this Builder. + */ + public Builder setTrustStorePassword(String password) { + trustStorePassword = password; + return this; + } + + /** + * Sets the trust store certificate's expiration, in milliseconds from when build() is called. + * @param expirationMillis expiration in milliseconds. + * @return this Builder. + */ + public Builder setTrustStoreCertExpirationMillis(long expirationMillis) { + trustStoreCertExpirationMillis = expirationMillis; + return this; + } + + /** + * Sets the key store key type. The private key generated for the test context will be of this type. + * @param keyType the key type. + * @return this Builder. + */ + public Builder setKeyStoreKeyType(X509KeyType keyType) { + keyStoreKeyType = keyType; + return this; + } + + /** + * Sets the key store password. The private key (PEM, JKS) and certificate (JKS only) will be encrypted with + * this password. + * @param password the password. + * @return this Builder. + */ + public Builder setKeyStorePassword(String password) { + keyStorePassword = password; + return this; + } + + /** + * Sets the key store certificate's expiration, in milliseconds from when build() is called. + * @param expirationMillis expiration in milliseconds. + * @return this Builder. + */ + public Builder setKeyStoreCertExpirationMillis(long expirationMillis) { + keyStoreCertExpirationMillis = expirationMillis; + return this; + } + + /** + * Sets the hostname verification behavior. If null is provided, reverts the behavior to the default, otherwise + * explicitly sets hostname verification to true or false. + * @param hostnameVerification new value for the hostname verification setting. + * @return this Builder. + */ + public Builder setHostnameVerification(Boolean hostnameVerification) { + this.hostnameVerification = hostnameVerification; + return this; + } + + } + + /** + * Returns a new default-constructed Builder. + * @return a new Builder. + */ + public static Builder newBuilder() { + return new Builder(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509TestHelpers.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509TestHelpers.java new file mode 100644 index 0000000..fb1371a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509TestHelpers.java @@ -0,0 +1,434 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.StringWriter; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.security.GeneralSecurityException; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.KeyStore; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SecureRandom; +import java.security.cert.Certificate; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.security.spec.ECGenParameterSpec; +import java.security.spec.RSAKeyGenParameterSpec; +import java.util.Date; +import org.bouncycastle.asn1.DERIA5String; +import org.bouncycastle.asn1.DEROctetString; +import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; +import org.bouncycastle.asn1.x509.BasicConstraints; +import org.bouncycastle.asn1.x509.ExtendedKeyUsage; +import org.bouncycastle.asn1.x509.Extension; +import org.bouncycastle.asn1.x509.GeneralName; +import org.bouncycastle.asn1.x509.GeneralNames; +import org.bouncycastle.asn1.x509.KeyPurposeId; +import org.bouncycastle.asn1.x509.KeyUsage; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.cert.X509CertificateHolder; +import org.bouncycastle.cert.X509v3CertificateBuilder; +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; +import org.bouncycastle.crypto.util.PrivateKeyFactory; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.openssl.jcajce.JcaPEMWriter; +import org.bouncycastle.openssl.jcajce.JcaPKCS8Generator; +import org.bouncycastle.openssl.jcajce.JceOpenSSLPKCS8EncryptorBuilder; +import org.bouncycastle.operator.ContentSigner; +import org.bouncycastle.operator.DefaultDigestAlgorithmIdentifierFinder; +import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder; +import org.bouncycastle.operator.OperatorCreationException; +import org.bouncycastle.operator.OutputEncryptor; +import org.bouncycastle.operator.bc.BcContentSignerBuilder; +import org.bouncycastle.operator.bc.BcECContentSignerBuilder; +import org.bouncycastle.operator.bc.BcRSAContentSignerBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class contains helper methods for creating X509 certificates and key pairs, and for serializing them + * to JKS, PEM or other keystore type files. + */ +public class X509TestHelpers { + + private static final Logger LOG = LoggerFactory.getLogger(X509TestHelpers.class); + + private static final SecureRandom PRNG = new SecureRandom(); + private static final int DEFAULT_RSA_KEY_SIZE_BITS = 2048; + private static final BigInteger DEFAULT_RSA_PUB_EXPONENT = RSAKeyGenParameterSpec.F4; // 65537 + private static final String DEFAULT_ELLIPTIC_CURVE_NAME = "secp256r1"; + // Per RFC 5280 section 4.1.2.2, X509 certificates can use up to 20 bytes == 160 bits for serial numbers. + private static final int SERIAL_NUMBER_MAX_BITS = 20 * Byte.SIZE; + + /** + * Uses the private key of the given key pair to create a self-signed CA certificate with the public half of the + * key pair and the given subject and expiration. The issuer of the new cert will be equal to the subject. + * Returns the new certificate. + * The returned certificate should be used as the trust store. The private key of the input key pair should be + * used to sign certificates that are used by test peers to establish TLS connections to each other. + * @param subject the subject of the new certificate being created. + * @param keyPair the key pair to use. The public key will be embedded in the new certificate, and the private key + * will be used to self-sign the certificate. + * @param expirationMillis expiration of the new certificate, in milliseconds from now. + * @return a new self-signed CA certificate. + * @throws IOException + * @throws OperatorCreationException + * @throws GeneralSecurityException + */ + public static X509Certificate newSelfSignedCACert( + X500Name subject, KeyPair keyPair, long expirationMillis) throws IOException, OperatorCreationException, GeneralSecurityException { + Date now = new Date(); + X509v3CertificateBuilder builder = initCertBuilder(subject, // for self-signed certs, issuer == subject + now, new Date(now.getTime() + + expirationMillis), subject, keyPair.getPublic()); + builder.addExtension(Extension.basicConstraints, true, new BasicConstraints(true)); // is a CA + builder.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature + | KeyUsage.keyCertSign + | KeyUsage.cRLSign)); + return buildAndSignCertificate(keyPair.getPrivate(), builder); + } + + /** + * Using the private key of the given CA key pair and the Subject of the given CA cert as the Issuer, issues a + * new cert with the given subject and public key. The returned certificate, combined with the private key half + * of the certPublicKey, should be used as the key store. + * @param caCert the certificate of the CA that's doing the signing. + * @param caKeyPair the key pair of the CA. The private key will be used to sign. The public key must match the + * public key in the caCert. + * @param certSubject the subject field of the new cert being issued. + * @param certPublicKey the public key of the new cert being issued. + * @param expirationMillis the expiration of the cert being issued, in milliseconds from now. + * @return a new certificate signed by the CA's private key. + * @throws IOException + * @throws OperatorCreationException + * @throws GeneralSecurityException + */ + public static X509Certificate newCert( + X509Certificate caCert, KeyPair caKeyPair, X500Name certSubject, PublicKey certPublicKey, long expirationMillis) throws IOException, OperatorCreationException, GeneralSecurityException { + if (!caKeyPair.getPublic().equals(caCert.getPublicKey())) { + throw new IllegalArgumentException("CA private key does not match the public key in the CA cert"); + } + Date now = new Date(); + X509v3CertificateBuilder builder = initCertBuilder(new X500Name(caCert.getIssuerDN().getName()), now, new Date( + now.getTime() + + expirationMillis), certSubject, certPublicKey); + builder.addExtension(Extension.basicConstraints, true, new BasicConstraints(false)); // not a CA + builder.addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature + | KeyUsage.keyEncipherment)); + builder.addExtension(Extension.extendedKeyUsage, true, new ExtendedKeyUsage(new KeyPurposeId[]{KeyPurposeId.id_kp_serverAuth, KeyPurposeId.id_kp_clientAuth})); + + builder.addExtension(Extension.subjectAlternativeName, false, getLocalhostSubjectAltNames()); + return buildAndSignCertificate(caKeyPair.getPrivate(), builder); + } + + /** + * Returns subject alternative names for "localhost". + * @return the subject alternative names for "localhost". + */ + private static GeneralNames getLocalhostSubjectAltNames() throws UnknownHostException { + InetAddress[] localAddresses = InetAddress.getAllByName("localhost"); + GeneralName[] generalNames = new GeneralName[localAddresses.length + 1]; + for (int i = 0; i < localAddresses.length; i++) { + generalNames[i] = new GeneralName(GeneralName.iPAddress, new DEROctetString(localAddresses[i].getAddress())); + } + generalNames[generalNames.length - 1] = new GeneralName(GeneralName.dNSName, new DERIA5String("localhost")); + return new GeneralNames(generalNames); + } + + /** + * Helper method for newSelfSignedCACert() and newCert(). Initializes a X509v3CertificateBuilder with + * logic that's common to both methods. + * @param issuer Issuer field of the new cert. + * @param notBefore date before which the new cert is not valid. + * @param notAfter date after which the new cert is not valid. + * @param subject Subject field of the new cert. + * @param subjectPublicKey public key to store in the new cert. + * @return a X509v3CertificateBuilder that can be further customized to finish creating the new cert. + */ + private static X509v3CertificateBuilder initCertBuilder( + X500Name issuer, Date notBefore, Date notAfter, X500Name subject, PublicKey subjectPublicKey) { + return new X509v3CertificateBuilder(issuer, new BigInteger(SERIAL_NUMBER_MAX_BITS, PRNG), notBefore, notAfter, subject, SubjectPublicKeyInfo.getInstance(subjectPublicKey.getEncoded())); + } + + /** + * Signs the certificate being built by the given builder using the given private key and returns the certificate. + * @param privateKey the private key to sign the certificate with. + * @param builder the cert builder that contains the certificate data. + * @return the signed certificate. + * @throws IOException + * @throws OperatorCreationException + * @throws CertificateException + */ + private static X509Certificate buildAndSignCertificate( + PrivateKey privateKey, X509v3CertificateBuilder builder) throws IOException, OperatorCreationException, CertificateException { + BcContentSignerBuilder signerBuilder; + if (privateKey.getAlgorithm().contains("RSA")) { // a little hacky way to detect key type, but it works + AlgorithmIdentifier signatureAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WithRSAEncryption"); + AlgorithmIdentifier digestAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find(signatureAlgorithm); + signerBuilder = new BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm); + } else { // if not RSA, assume EC + AlgorithmIdentifier signatureAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256withECDSA"); + AlgorithmIdentifier digestAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find(signatureAlgorithm); + signerBuilder = new BcECContentSignerBuilder(signatureAlgorithm, digestAlgorithm); + } + AsymmetricKeyParameter privateKeyParam = PrivateKeyFactory.createKey(privateKey.getEncoded()); + ContentSigner signer = signerBuilder.build(privateKeyParam); + return toX509Cert(builder.build(signer)); + } + + /** + * Generates a new asymmetric key pair of the given type. + * @param keyType the type of key pair to generate. + * @return the new key pair. + * @throws GeneralSecurityException if your java crypto providers are messed up. + */ + public static KeyPair generateKeyPair(X509KeyType keyType) throws GeneralSecurityException { + switch (keyType) { + case RSA: + return generateRSAKeyPair(); + case EC: + return generateECKeyPair(); + default: + throw new IllegalArgumentException("Invalid X509KeyType"); + } + } + + /** + * Generates an RSA key pair with a 2048-bit private key and F4 (65537) as the public exponent. + * @return the key pair. + */ + public static KeyPair generateRSAKeyPair() throws GeneralSecurityException { + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); + RSAKeyGenParameterSpec keyGenSpec = new RSAKeyGenParameterSpec(DEFAULT_RSA_KEY_SIZE_BITS, DEFAULT_RSA_PUB_EXPONENT); + keyGen.initialize(keyGenSpec, PRNG); + return keyGen.generateKeyPair(); + } + + /** + * Generates an elliptic curve key pair using the "secp256r1" aka "prime256v1" aka "NIST P-256" curve. + * @return the key pair. + */ + public static KeyPair generateECKeyPair() throws GeneralSecurityException { + KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC"); + keyGen.initialize(new ECGenParameterSpec(DEFAULT_ELLIPTIC_CURVE_NAME), PRNG); + return keyGen.generateKeyPair(); + } + + /** + * PEM-encodes the given X509 certificate and private key (compatible with OpenSSL), optionally protecting the + * private key with a password. Concatenates them both and returns the result as a single string. + * This creates the PEM encoding of a key store. + * @param cert the X509 certificate to PEM-encode. + * @param privateKey the private key to PEM-encode. + * @param keyPassword an optional key password. If empty or null, the private key will not be encrypted. + * @return a String containing the PEM encodings of the certificate and private key. + * @throws IOException if converting the certificate or private key to PEM format fails. + * @throws OperatorCreationException if constructing the encryptor from the given password fails. + */ + public static String pemEncodeCertAndPrivateKey( + X509Certificate cert, PrivateKey privateKey, String keyPassword) throws IOException, OperatorCreationException { + return pemEncodeX509Certificate(cert) + "\n" + pemEncodePrivateKey(privateKey, keyPassword); + } + + /** + * PEM-encodes the given private key (compatible with OpenSSL), optionally protecting it with a password, and + * returns the result as a String. + * @param key the private key. + * @param password an optional key password. If empty or null, the private key will not be encrypted. + * @return a String containing the PEM encoding of the private key. + * @throws IOException if converting the key to PEM format fails. + * @throws OperatorCreationException if constructing the encryptor from the given password fails. + */ + public static String pemEncodePrivateKey( + PrivateKey key, String password) throws IOException, OperatorCreationException { + StringWriter stringWriter = new StringWriter(); + JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter); + OutputEncryptor encryptor = null; + if (password != null && password.length() > 0) { + encryptor = new JceOpenSSLPKCS8EncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd3_KeyTripleDES_CBC).setProvider(BouncyCastleProvider.PROVIDER_NAME).setRandom(PRNG).setPasssword(password.toCharArray()).build(); + } + pemWriter.writeObject(new JcaPKCS8Generator(key, encryptor)); + pemWriter.close(); + return stringWriter.toString(); + } + + /** + * PEM-encodes the given X509 certificate (compatible with OpenSSL) and returns the result as a String. + * @param cert the certificate. + * @return a String containing the PEM encoding of the certificate. + * @throws IOException if converting the certificate to PEM format fails. + */ + public static String pemEncodeX509Certificate(X509Certificate cert) throws IOException { + StringWriter stringWriter = new StringWriter(); + JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter); + pemWriter.writeObject(cert); + pemWriter.close(); + return stringWriter.toString(); + } + + /** + * Encodes the given X509Certificate as a JKS TrustStore, optionally protecting the cert with a password (though + * it's unclear why one would do this since certificates only contain public information and do not need to be + * kept secret). Returns the byte array encoding of the trust store, which may be written to a file and loaded to + * instantiate the trust store at a later point or in another process. + * @param cert the certificate to serialize. + * @param keyPassword an optional password to encrypt the trust store. If empty or null, the cert will not be encrypted. + * @return the serialized bytes of the JKS trust store. + * @throws IOException + * @throws GeneralSecurityException + */ + public static byte[] certToJavaTrustStoreBytes( + X509Certificate cert, String keyPassword) throws IOException, GeneralSecurityException { + KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); + return certToTrustStoreBytes(cert, keyPassword, trustStore); + } + + /** + * Encodes the given X509Certificate as a PKCS12 TrustStore, optionally protecting the cert with a password (though + * it's unclear why one would do this since certificates only contain public information and do not need to be + * kept secret). Returns the byte array encoding of the trust store, which may be written to a file and loaded to + * instantiate the trust store at a later point or in another process. + * @param cert the certificate to serialize. + * @param keyPassword an optional password to encrypt the trust store. If empty or null, the cert will not be encrypted. + * @return the serialized bytes of the PKCS12 trust store. + * @throws IOException + * @throws GeneralSecurityException + */ + public static byte[] certToPKCS12TrustStoreBytes( + X509Certificate cert, String keyPassword) throws IOException, GeneralSecurityException { + KeyStore trustStore = KeyStore.getInstance("PKCS12"); + return certToTrustStoreBytes(cert, keyPassword, trustStore); + } + + /** + * Encodes the given X509Certificate as a BCFKS TrustStore, optionally protecting the cert with a password (though + * it's unclear why one would do this since certificates only contain public information and do not need to be + * kept secret). Returns the byte array encoding of the trust store, which may be written to a file and loaded to + * instantiate the trust store at a later point or in another process. + * @param cert the certificate to serialize. + * @param keyPassword an optional password to encrypt the trust store. If empty or null, the cert will not be encrypted. + * @return the serialized bytes of the BCFKS trust store. + * @throws IOException + * @throws GeneralSecurityException + */ + public static byte[] certToBCFKSTrustStoreBytes( + X509Certificate cert, + String keyPassword) throws IOException, GeneralSecurityException { + KeyStore trustStore = KeyStore.getInstance("BCFKS"); + return certToTrustStoreBytes(cert, keyPassword, trustStore); + } + + private static byte[] certToTrustStoreBytes(X509Certificate cert, String keyPassword, KeyStore trustStore) throws IOException, GeneralSecurityException { + char[] keyPasswordChars = keyPassword == null ? new char[0] : keyPassword.toCharArray(); + trustStore.load(null, keyPasswordChars); + trustStore.setCertificateEntry(cert.getSubjectDN().toString(), cert); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + trustStore.store(outputStream, keyPasswordChars); + outputStream.flush(); + byte[] result = outputStream.toByteArray(); + outputStream.close(); + return result; + } + + /** + * Encodes the given X509Certificate and private key as a JKS KeyStore, optionally protecting the private key + * (and possibly the cert?) with a password. Returns the byte array encoding of the key store, which may be written + * to a file and loaded to instantiate the key store at a later point or in another process. + * @param cert the X509 certificate to serialize. + * @param privateKey the private key to serialize. + * @param keyPassword an optional key password. If empty or null, the private key will not be encrypted. + * @return the serialized bytes of the JKS key store. + * @throws IOException + * @throws GeneralSecurityException + */ + public static byte[] certAndPrivateKeyToJavaKeyStoreBytes( + X509Certificate cert, PrivateKey privateKey, String keyPassword) throws IOException, GeneralSecurityException { + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + return certAndPrivateKeyToBytes(cert, privateKey, keyPassword, keyStore); + } + + /** + * Encodes the given X509Certificate and private key as a PKCS12 KeyStore, optionally protecting the private key + * (and possibly the cert?) with a password. Returns the byte array encoding of the key store, which may be written + * to a file and loaded to instantiate the key store at a later point or in another process. + * @param cert the X509 certificate to serialize. + * @param privateKey the private key to serialize. + * @param keyPassword an optional key password. If empty or null, the private key will not be encrypted. + * @return the serialized bytes of the PKCS12 key store. + * @throws IOException + * @throws GeneralSecurityException + */ + public static byte[] certAndPrivateKeyToPKCS12Bytes( + X509Certificate cert, PrivateKey privateKey, String keyPassword) throws IOException, GeneralSecurityException { + KeyStore keyStore = KeyStore.getInstance("PKCS12"); + return certAndPrivateKeyToBytes(cert, privateKey, keyPassword, keyStore); + } + + /** + * Encodes the given X509Certificate and private key as a BCFKS KeyStore, optionally protecting the private key + * (and possibly the cert?) with a password. Returns the byte array encoding of the key store, which may be written + * to a file and loaded to instantiate the key store at a later point or in another process. + * @param cert the X509 certificate to serialize. + * @param privateKey the private key to serialize. + * @param keyPassword an optional key password. If empty or null, the private key will not be encrypted. + * @return the serialized bytes of the BCFKS key store. + * @throws IOException + * @throws GeneralSecurityException + */ + public static byte[] certAndPrivateKeyToBCFKSBytes( + X509Certificate cert, + PrivateKey privateKey, + String keyPassword) throws IOException, GeneralSecurityException { + KeyStore keyStore = KeyStore.getInstance("BCFKS"); + return certAndPrivateKeyToBytes(cert, privateKey, keyPassword, keyStore); + } + + private static byte[] certAndPrivateKeyToBytes( + X509Certificate cert, PrivateKey privateKey, String keyPassword, KeyStore keyStore) throws IOException, GeneralSecurityException { + char[] keyPasswordChars = keyPassword == null ? new char[0] : keyPassword.toCharArray(); + keyStore.load(null, keyPasswordChars); + keyStore.setKeyEntry("key", privateKey, keyPasswordChars, new Certificate[]{cert}); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + keyStore.store(outputStream, keyPasswordChars); + outputStream.flush(); + byte[] result = outputStream.toByteArray(); + outputStream.close(); + return result; + } + + /** + * Convenience method to convert a bouncycastle X509CertificateHolder to a java X509Certificate. + * @param certHolder a bouncycastle X509CertificateHolder. + * @return a java X509Certificate + * @throws CertificateException if the conversion fails. + */ + public static X509Certificate toX509Cert(X509CertificateHolder certHolder) throws CertificateException { + return new JcaX509CertificateConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME).getCertificate(certHolder); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509UtilTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509UtilTest.java new file mode 100644 index 0000000..05d216b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/X509UtilTest.java @@ -0,0 +1,654 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.security.NoSuchAlgorithmException; +import java.security.Security; +import java.util.Collection; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; +import javax.net.ssl.HandshakeCompletedEvent; +import javax.net.ssl.HandshakeCompletedListener; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.X509KeyManager; +import javax.net.ssl.X509TrustManager; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class X509UtilTest extends BaseX509ParameterizedTestCase { + + private X509Util x509Util; + private static final String[] customCipherSuites = new String[]{ + "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", + "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA" + }; + + @Parameterized.Parameters + public static Collection params() { + return BaseX509ParameterizedTestCase.defaultParams(); + } + + public X509UtilTest( + X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStorePassword(keyPassword) + .setKeyStoreKeyType(certKeyType) + .setTrustStorePassword(keyPassword) + .setTrustStoreKeyType(caKeyType) + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Before + public void setUp() throws Exception { + try (X509Util x509util = new ClientX509Util()) { + x509TestContext.setSystemProperties(x509util, KeyStoreFileType.JKS, KeyStoreFileType.JKS); + } + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + x509Util = new ClientX509Util(); + } + + @After + public void cleanUp() { + x509TestContext.clearSystemProperties(x509Util); + System.clearProperty(x509Util.getSslOcspEnabledProperty()); + System.clearProperty(x509Util.getSslCrlEnabledProperty()); + System.clearProperty(x509Util.getCipherSuitesProperty()); + System.clearProperty(x509Util.getSslProtocolProperty()); + System.clearProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty()); + System.clearProperty("com.sun.net.ssl.checkRevocation"); + System.clearProperty("com.sun.security.enableCRLDP"); + Security.setProperty("ocsp.enable", Boolean.FALSE.toString()); + Security.setProperty("com.sun.security.enableCRLDP", Boolean.FALSE.toString()); + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + x509Util.close(); + } + + @Test(timeout = 5000) + public void testCreateSSLContextWithoutCustomProtocol() throws Exception { + SSLContext sslContext = x509Util.getDefaultSSLContext(); + assertEquals(X509Util.DEFAULT_PROTOCOL, sslContext.getProtocol()); + } + + @Test(timeout = 5000) + public void testCreateSSLContextWithCustomProtocol() throws Exception { + final String protocol = "TLSv1.1"; + System.setProperty(x509Util.getSslProtocolProperty(), protocol); + SSLContext sslContext = x509Util.getDefaultSSLContext(); + assertEquals(protocol, sslContext.getProtocol()); + } + + @Test(timeout = 5000) + public void testCreateSSLContextWithoutKeyStoreLocation() throws Exception { + System.clearProperty(x509Util.getSslKeystoreLocationProperty()); + x509Util.getDefaultSSLContext(); + } + + @Test(timeout = 5000, expected = X509Exception.SSLContextException.class) + public void testCreateSSLContextWithoutKeyStorePassword() throws Exception { + if (!x509TestContext.isKeyStoreEncrypted()) { + throw new X509Exception.SSLContextException(""); + } + System.clearProperty(x509Util.getSslKeystorePasswdProperty()); + x509Util.getDefaultSSLContext(); + } + + @Test(timeout = 5000) + public void testCreateSSLContextWithCustomCipherSuites() throws Exception { + setCustomCipherSuites(); + SSLSocket sslSocket = x509Util.createSSLSocket(); + assertArrayEquals(customCipherSuites, sslSocket.getEnabledCipherSuites()); + } + + // It would be great to test the value of PKIXBuilderParameters#setRevocationEnabled but it does not appear to be + // possible + @Test(timeout = 5000) + public void testCRLEnabled() throws Exception { + System.setProperty(x509Util.getSslCrlEnabledProperty(), "true"); + x509Util.getDefaultSSLContext(); + assertTrue(Boolean.valueOf(System.getProperty("com.sun.net.ssl.checkRevocation"))); + assertTrue(Boolean.valueOf(System.getProperty("com.sun.security.enableCRLDP"))); + assertFalse(Boolean.valueOf(Security.getProperty("ocsp.enable"))); + } + + @Test(timeout = 5000) + public void testCRLDisabled() throws Exception { + x509Util.getDefaultSSLContext(); + assertFalse(Boolean.valueOf(System.getProperty("com.sun.net.ssl.checkRevocation"))); + assertFalse(Boolean.valueOf(System.getProperty("com.sun.security.enableCRLDP"))); + assertFalse(Boolean.valueOf(Security.getProperty("ocsp.enable"))); + } + + @Test(timeout = 5000) + public void testOCSPEnabled() throws Exception { + System.setProperty(x509Util.getSslOcspEnabledProperty(), "true"); + x509Util.getDefaultSSLContext(); + assertTrue(Boolean.valueOf(System.getProperty("com.sun.net.ssl.checkRevocation"))); + assertTrue(Boolean.valueOf(System.getProperty("com.sun.security.enableCRLDP"))); + assertTrue(Boolean.valueOf(Security.getProperty("ocsp.enable"))); + } + + @Test(timeout = 5000) + public void testCreateSSLSocket() throws Exception { + setCustomCipherSuites(); + SSLSocket sslSocket = x509Util.createSSLSocket(); + assertArrayEquals(customCipherSuites, sslSocket.getEnabledCipherSuites()); + } + + @Test(timeout = 5000) + public void testCreateSSLServerSocketWithoutPort() throws Exception { + setCustomCipherSuites(); + SSLServerSocket sslServerSocket = x509Util.createSSLServerSocket(); + assertArrayEquals(customCipherSuites, sslServerSocket.getEnabledCipherSuites()); + assertTrue(sslServerSocket.getNeedClientAuth()); + } + + @Test(timeout = 5000) + public void testCreateSSLServerSocketWithPort() throws Exception { + setCustomCipherSuites(); + int port = PortAssignment.unique(); + SSLServerSocket sslServerSocket = x509Util.createSSLServerSocket(port); + assertEquals(sslServerSocket.getLocalPort(), port); + assertArrayEquals(customCipherSuites, sslServerSocket.getEnabledCipherSuites()); + assertTrue(sslServerSocket.getNeedClientAuth()); + } + + @Test + public void testLoadPEMKeyStore() throws Exception { + // Make sure we can instantiate a key manager from the PEM file on disk + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + x509TestContext.getKeyStorePassword(), + KeyStoreFileType.PEM.getPropertyValue()); + } + + @Test + public void testLoadPEMKeyStoreNullPassword() throws Exception { + if (!x509TestContext.getKeyStorePassword().isEmpty()) { + return; + } + // Make sure that empty password and null password are treated the same + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + null, + KeyStoreFileType.PEM.getPropertyValue()); + } + + @Test + public void testLoadPEMKeyStoreAutodetectStoreFileType() throws Exception { + // Make sure we can instantiate a key manager from the PEM file on disk + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + x509TestContext.getKeyStorePassword(), + null /* null StoreFileType means 'autodetect from file extension' */); + } + + @Test(expected = X509Exception.KeyManagerException.class) + public void testLoadPEMKeyStoreWithWrongPassword() throws Exception { + // Attempting to load with the wrong key password should fail + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + "wrong password", // intentionally use the wrong password + KeyStoreFileType.PEM.getPropertyValue()); + } + + @Test + public void testLoadPEMTrustStore() throws Exception { + // Make sure we can instantiate a trust manager from the PEM file on disk + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + x509TestContext.getTrustStorePassword(), KeyStoreFileType.PEM.getPropertyValue(), + false, + false, + true, + true); + } + + @Test + public void testLoadPEMTrustStoreNullPassword() throws Exception { + if (!x509TestContext.getTrustStorePassword().isEmpty()) { + return; + } + // Make sure that empty password and null password are treated the same + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + null, + KeyStoreFileType.PEM.getPropertyValue(), + false, + false, + true, + true); + + } + + @Test + public void testLoadPEMTrustStoreAutodetectStoreFileType() throws Exception { + // Make sure we can instantiate a trust manager from the PEM file on disk + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath(), + x509TestContext.getTrustStorePassword(), + null, // null StoreFileType means 'autodetect from file extension' + false, + false, + true, + true); + } + + @Test + public void testLoadJKSKeyStore() throws Exception { + // Make sure we can instantiate a key manager from the JKS file on disk + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + x509TestContext.getKeyStorePassword(), + KeyStoreFileType.JKS.getPropertyValue()); + } + + @Test + public void testLoadJKSKeyStoreNullPassword() throws Exception { + if (!x509TestContext.getKeyStorePassword().isEmpty()) { + return; + } + // Make sure that empty password and null password are treated the same + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + null, + KeyStoreFileType.JKS.getPropertyValue()); + } + + @Test + public void testLoadJKSKeyStoreAutodetectStoreFileType() throws Exception { + // Make sure we can instantiate a key manager from the JKS file on disk + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + x509TestContext.getKeyStorePassword(), + null /* null StoreFileType means 'autodetect from file extension' */); + } + + @Test(expected = X509Exception.KeyManagerException.class) + public void testLoadJKSKeyStoreWithWrongPassword() throws Exception { + // Attempting to load with the wrong key password should fail + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + "wrong password", + KeyStoreFileType.JKS.getPropertyValue()); + } + + @Test + public void testLoadJKSTrustStore() throws Exception { + // Make sure we can instantiate a trust manager from the JKS file on disk + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + x509TestContext.getTrustStorePassword(), + KeyStoreFileType.JKS.getPropertyValue(), + true, + true, + true, + true); + } + + @Test + public void testLoadJKSTrustStoreNullPassword() throws Exception { + if (!x509TestContext.getTrustStorePassword().isEmpty()) { + return; + } + // Make sure that empty password and null password are treated the same + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + null, + KeyStoreFileType.JKS.getPropertyValue(), + false, + false, + true, + true); + } + + @Test + public void testLoadJKSTrustStoreAutodetectStoreFileType() throws Exception { + // Make sure we can instantiate a trust manager from the JKS file on disk + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + x509TestContext.getTrustStorePassword(), + null, // null StoreFileType means 'autodetect from file extension' + true, + true, + true, + true); + } + + @Test(expected = X509Exception.TrustManagerException.class) + public void testLoadJKSTrustStoreWithWrongPassword() throws Exception { + // Attempting to load with the wrong key password should fail + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.JKS).getAbsolutePath(), + "wrong password", + KeyStoreFileType.JKS.getPropertyValue(), + true, + true, + true, + true); + } + + @Test + public void testLoadPKCS12KeyStore() throws Exception { + // Make sure we can instantiate a key manager from the PKCS12 file on disk + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + x509TestContext.getKeyStorePassword(), + KeyStoreFileType.PKCS12.getPropertyValue()); + } + + @Test + public void testLoadPKCS12KeyStoreNullPassword() throws Exception { + if (!x509TestContext.getKeyStorePassword().isEmpty()) { + return; + } + // Make sure that empty password and null password are treated the same + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + null, + KeyStoreFileType.PKCS12.getPropertyValue()); + } + + @Test + public void testLoadPKCS12KeyStoreAutodetectStoreFileType() throws Exception { + // Make sure we can instantiate a key manager from the PKCS12 file on disk + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + x509TestContext.getKeyStorePassword(), + null /* null StoreFileType means 'autodetect from file extension' */); + } + + @Test(expected = X509Exception.KeyManagerException.class) + public void testLoadPKCS12KeyStoreWithWrongPassword() throws Exception { + // Attempting to load with the wrong key password should fail + X509KeyManager km = X509Util.createKeyManager( + x509TestContext.getKeyStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + "wrong password", + KeyStoreFileType.PKCS12.getPropertyValue()); + } + + @Test + public void testLoadPKCS12TrustStore() throws Exception { + // Make sure we can instantiate a trust manager from the PKCS12 file on disk + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + x509TestContext.getTrustStorePassword(), KeyStoreFileType.PKCS12.getPropertyValue(), + true, + true, + true, + true); + } + + @Test + public void testLoadPKCS12TrustStoreNullPassword() throws Exception { + if (!x509TestContext.getTrustStorePassword().isEmpty()) { + return; + } + // Make sure that empty password and null password are treated the same + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + null, + KeyStoreFileType.PKCS12.getPropertyValue(), + false, + false, + true, + true); + } + + @Test + public void testLoadPKCS12TrustStoreAutodetectStoreFileType() throws Exception { + // Make sure we can instantiate a trust manager from the PKCS12 file on disk + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + x509TestContext.getTrustStorePassword(), + null, // null StoreFileType means 'autodetect from file extension' + true, + true, + true, + true); + } + + @Test(expected = X509Exception.TrustManagerException.class) + public void testLoadPKCS12TrustStoreWithWrongPassword() throws Exception { + // Attempting to load with the wrong key password should fail + X509TrustManager tm = X509Util.createTrustManager( + x509TestContext.getTrustStoreFile(KeyStoreFileType.PKCS12).getAbsolutePath(), + "wrong password", + KeyStoreFileType.PKCS12.getPropertyValue(), + true, + true, + true, + true); + } + + @Test + public void testGetSslHandshakeDetectionTimeoutMillisProperty() { + assertEquals(X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS, x509Util.getSslHandshakeTimeoutMillis()); + // Note: need to create a new ClientX509Util each time to pick up modified property value + String newPropertyString = Integer.toString(X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS + 1); + System.setProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty(), newPropertyString); + try (X509Util tempX509Util = new ClientX509Util()) { + assertEquals(X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS + + 1, tempX509Util.getSslHandshakeTimeoutMillis()); + } + // 0 value not allowed, will return the default + System.setProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty(), "0"); + try (X509Util tempX509Util = new ClientX509Util()) { + assertEquals(X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS, tempX509Util.getSslHandshakeTimeoutMillis()); + } + // Negative value not allowed, will return the default + System.setProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty(), "-1"); + try (X509Util tempX509Util = new ClientX509Util()) { + assertEquals(X509Util.DEFAULT_HANDSHAKE_DETECTION_TIMEOUT_MILLIS, tempX509Util.getSslHandshakeTimeoutMillis()); + } + } + + @Test(expected = X509Exception.SSLContextException.class) + public void testCreateSSLContext_invalidCustomSSLContextClass() throws Exception { + ZKConfig zkConfig = new ZKConfig(); + ClientX509Util clientX509Util = new ClientX509Util(); + zkConfig.setProperty(clientX509Util.getSslContextSupplierClassProperty(), String.class.getCanonicalName()); + clientX509Util.createSSLContext(zkConfig); + } + + @Test + public void testCreateSSLContext_validCustomSSLContextClass() throws Exception { + ZKConfig zkConfig = new ZKConfig(); + ClientX509Util clientX509Util = new ClientX509Util(); + zkConfig.setProperty(clientX509Util.getSslContextSupplierClassProperty(), SslContextSupplier.class.getName()); + final SSLContext sslContext = clientX509Util.createSSLContext(zkConfig); + assertEquals(SSLContext.getDefault(), sslContext); + } + + private static void forceClose(Socket s) { + if (s == null || s.isClosed()) { + return; + } + try { + s.close(); + } catch (IOException e) { + } + } + + private static void forceClose(ServerSocket s) { + if (s == null || s.isClosed()) { + return; + } + try { + s.close(); + } catch (IOException e) { + } + } + + // This test makes sure that client-initiated TLS renegotiation does not + // succeed. We explicitly disable it at the top of X509Util.java. + @Test(expected = SSLHandshakeException.class) + public void testClientRenegotiationFails() throws Throwable { + int port = PortAssignment.unique(); + ExecutorService workerPool = Executors.newCachedThreadPool(); + final SSLServerSocket listeningSocket = x509Util.createSSLServerSocket(); + SSLSocket clientSocket = null; + SSLSocket serverSocket = null; + final AtomicInteger handshakesCompleted = new AtomicInteger(0); + final CountDownLatch handshakeCompleted = new CountDownLatch(1); + try { + InetSocketAddress localServerAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), port); + listeningSocket.bind(localServerAddress); + Future acceptFuture; + acceptFuture = workerPool.submit(new Callable() { + @Override + public SSLSocket call() throws Exception { + SSLSocket sslSocket = (SSLSocket) listeningSocket.accept(); + sslSocket.addHandshakeCompletedListener(new HandshakeCompletedListener() { + @Override + public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent) { + handshakesCompleted.getAndIncrement(); + handshakeCompleted.countDown(); + } + }); + assertEquals(1, sslSocket.getInputStream().read()); + try { + // 2nd read is after the renegotiation attempt and will fail + sslSocket.getInputStream().read(); + return sslSocket; + } catch (Exception e) { + forceClose(sslSocket); + throw e; + } + } + }); + clientSocket = x509Util.createSSLSocket(); + clientSocket.connect(localServerAddress); + clientSocket.getOutputStream().write(1); + // Attempt to renegotiate after establishing the connection + clientSocket.startHandshake(); + clientSocket.getOutputStream().write(1); + // The exception is thrown on the server side, we need to unwrap it + try { + serverSocket = acceptFuture.get(); + } catch (ExecutionException e) { + throw e.getCause(); + } + } finally { + forceClose(serverSocket); + forceClose(clientSocket); + forceClose(listeningSocket); + workerPool.shutdown(); + // Make sure the first handshake completed and only the second + // one failed. + handshakeCompleted.await(5, TimeUnit.SECONDS); + assertEquals(1, handshakesCompleted.get()); + } + } + + @Test + public void testGetDefaultCipherSuitesJava8() { + String[] cipherSuites = X509Util.getDefaultCipherSuitesForJavaVersion("1.8"); + // Java 8 default should have the CBC suites first + assertTrue(cipherSuites[0].contains("CBC")); + } + + @Test + public void testGetDefaultCipherSuitesJava9() { + String[] cipherSuites = X509Util.getDefaultCipherSuitesForJavaVersion("9"); + // Java 9+ default should have the GCM suites first + assertTrue(cipherSuites[0].contains("GCM")); + } + + @Test + public void testGetDefaultCipherSuitesJava10() { + String[] cipherSuites = X509Util.getDefaultCipherSuitesForJavaVersion("10"); + // Java 9+ default should have the GCM suites first + assertTrue(cipherSuites[0].contains("GCM")); + } + + @Test + public void testGetDefaultCipherSuitesJava11() { + String[] cipherSuites = X509Util.getDefaultCipherSuitesForJavaVersion("11"); + // Java 9+ default should have the GCM suites first + assertTrue(cipherSuites[0].contains("GCM")); + } + + @Test + public void testGetDefaultCipherSuitesUnknownVersion() { + String[] cipherSuites = X509Util.getDefaultCipherSuitesForJavaVersion("notaversion"); + // If version can't be parsed, use the more conservative Java 8 default + assertTrue(cipherSuites[0].contains("CBC")); + } + + @Test(expected = NullPointerException.class) + public void testGetDefaultCipherSuitesNullVersion() { + X509Util.getDefaultCipherSuitesForJavaVersion(null); + } + + // Warning: this will reset the x509Util + private void setCustomCipherSuites() { + System.setProperty(x509Util.getCipherSuitesProperty(), customCipherSuites[0] + "," + customCipherSuites[1]); + x509Util.close(); // remember to close old instance before replacing it + x509Util = new ClientX509Util(); + } + + public static class SslContextSupplier implements Supplier { + + @Override + public SSLContext get() { + try { + return SSLContext.getDefault(); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKConfigTest.java new file mode 100644 index 0000000..eff1bfb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKConfigTest.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + + +import static org.junit.Assert.assertEquals; +import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + + + +public class ZKConfigTest { + + X509Util x509Util = new ClientX509Util(); + + @Rule + public Timeout timeout = new Timeout(10, TimeUnit.SECONDS); + + @After + public void tearDown() throws Exception { + System.clearProperty(x509Util.getSslProtocolProperty()); + } + + // property is not set we should get the default value + @Test + public void testBooleanRetrievalFromPropertyDefault() { + ZKConfig conf = new ZKConfig(); + String prop = "UnSetProperty" + System.currentTimeMillis(); + boolean defaultValue = false; + boolean result = conf.getBoolean(prop, defaultValue); + assertEquals(defaultValue, result); + } + + // property is set to an valid boolean, we should get the set value + @Test + public void testBooleanRetrievalFromProperty() { + boolean value = true; + boolean defaultValue = false; + System.setProperty(x509Util.getSslProtocolProperty(), Boolean.toString(value)); + ZKConfig conf = new ZKConfig(); + boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue); + assertEquals(value, result); + } + + // property is set but with white spaces in the beginning + @Test + public void testBooleanRetrievalFromPropertyWithWhitespacesInBeginning() { + boolean value = true; + boolean defaultValue = false; + System.setProperty(x509Util.getSslProtocolProperty(), " " + value); + ZKConfig conf = new ZKConfig(); + boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue); + assertEquals(value, result); + } + + // property is set but with white spaces at the end + @Test + public void testBooleanRetrievalFromPropertyWithWhitespacesAtEnd() { + boolean value = true; + boolean defaultValue = false; + System.setProperty(x509Util.getSslProtocolProperty(), value + " "); + ZKConfig conf = new ZKConfig(); + boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue); + assertEquals(value, result); + } + + // property is set but with white spaces at the beginning and the end + @Test + public void testBooleanRetrievalFromPropertyWithWhitespacesAtBeginningAndEnd() { + boolean value = true; + boolean defaultValue = false; + System.setProperty(x509Util.getSslProtocolProperty(), " " + value + " "); + ZKConfig conf = new ZKConfig(); + boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue); + assertEquals(value, result); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKHostnameVerifierTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKHostnameVerifierTest.java new file mode 100644 index 0000000..13f7c76 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKHostnameVerifierTest.java @@ -0,0 +1,161 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import javax.net.ssl.SSLException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Note: These test cases (and resources) have been taken from the Apache HttpComponents project. + */ +public class ZKHostnameVerifierTest { + + private ZKHostnameVerifier impl; + + @Before + public void setup() { + impl = new ZKHostnameVerifier(); + } + + @Test + public void testVerify() throws Exception { + final CertificateFactory cf = CertificateFactory.getInstance("X.509"); + InputStream in; + X509Certificate x509; + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO); + x509 = (X509Certificate) cf.generateCertificate(in); + + impl.verify("foo.com", x509); + exceptionPlease(impl, "a.foo.com", x509); + exceptionPlease(impl, "bar.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_HANAKO); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("\u82b1\u5b50.co.jp", x509); + exceptionPlease(impl, "a.\u82b1\u5b50.co.jp", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO_BAR); + x509 = (X509Certificate) cf.generateCertificate(in); + exceptionPlease(impl, "foo.com", x509); + exceptionPlease(impl, "a.foo.com", x509); + impl.verify("bar.com", x509); + exceptionPlease(impl, "a.bar.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO_BAR_HANAKO); + x509 = (X509Certificate) cf.generateCertificate(in); + exceptionPlease(impl, "foo.com", x509); + exceptionPlease(impl, "a.foo.com", x509); + impl.verify("bar.com", x509); + exceptionPlease(impl, "a.bar.com", x509); + + /* + Java isn't extracting international subjectAlts properly. (Or + OpenSSL isn't storing them properly). + */ + // DEFAULT.verify("\u82b1\u5b50.co.jp", x509 ); + // impl.verify("\u82b1\u5b50.co.jp", x509 ); + exceptionPlease(impl, "a.\u82b1\u5b50.co.jp", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_NO_CNS_FOO); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("foo.com", x509); + exceptionPlease(impl, "a.foo.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_NO_CNS_FOO); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("foo.com", x509); + exceptionPlease(impl, "a.foo.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_THREE_CNS_FOO_BAR_HANAKO); + x509 = (X509Certificate) cf.generateCertificate(in); + exceptionPlease(impl, "foo.com", x509); + exceptionPlease(impl, "a.foo.com", x509); + exceptionPlease(impl, "bar.com", x509); + exceptionPlease(impl, "a.bar.com", x509); + impl.verify("\u82b1\u5b50.co.jp", x509); + exceptionPlease(impl, "a.\u82b1\u5b50.co.jp", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_WILD_FOO); + x509 = (X509Certificate) cf.generateCertificate(in); + exceptionPlease(impl, "foo.com", x509); + impl.verify("www.foo.com", x509); + impl.verify("\u82b1\u5b50.foo.com", x509); + exceptionPlease(impl, "a.b.foo.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_WILD_CO_JP); + x509 = (X509Certificate) cf.generateCertificate(in); + // Silly test because no-one would ever be able to lookup an IP address + // using "*.co.jp". + impl.verify("*.co.jp", x509); + impl.verify("foo.co.jp", x509); + impl.verify("\u82b1\u5b50.co.jp", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_WILD_FOO_BAR_HANAKO); + x509 = (X509Certificate) cf.generateCertificate(in); + // try the foo.com variations + exceptionPlease(impl, "foo.com", x509); + exceptionPlease(impl, "www.foo.com", x509); + exceptionPlease(impl, "\u82b1\u5b50.foo.com", x509); + exceptionPlease(impl, "a.b.foo.com", x509); + // try the bar.com variations + exceptionPlease(impl, "bar.com", x509); + impl.verify("www.bar.com", x509); + impl.verify("\u82b1\u5b50.bar.com", x509); + exceptionPlease(impl, "a.b.bar.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.X509_MULTIPLE_VALUE_AVA); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("repository.infonotary.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.S_GOOGLE_COM); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("*.google.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.S_GOOGLE_COM); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("*.Google.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.IP_1_1_1_1); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("1.1.1.1", x509); + + exceptionPlease(impl, "1.1.1.2", x509); + exceptionPlease(impl, "dummy-value.com", x509); + + in = new ByteArrayInputStream(CertificatesToPlayWith.EMAIL_ALT_SUBJECT_NAME); + x509 = (X509Certificate) cf.generateCertificate(in); + impl.verify("www.company.com", x509); + } + + private void exceptionPlease(final ZKHostnameVerifier hv, final String host, + final X509Certificate x509) { + try { + hv.verify(host, x509); + Assert.fail("HostnameVerifier shouldn't allow [" + host + "]"); + } catch (final SSLException e) { + // whew! we're okay! + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKTrustManagerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKTrustManagerTest.java new file mode 100644 index 0000000..66fabed --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/common/ZKTrustManagerTest.java @@ -0,0 +1,248 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.common; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.math.BigInteger; +import java.net.InetAddress; +import java.net.Socket; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.Security; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Random; +import javax.net.ssl.X509ExtendedTrustManager; +import org.apache.zookeeper.ZKTestCase; +import org.bouncycastle.asn1.x500.X500NameBuilder; +import org.bouncycastle.asn1.x500.style.BCStyle; +import org.bouncycastle.asn1.x509.BasicConstraints; +import org.bouncycastle.asn1.x509.Extension; +import org.bouncycastle.asn1.x509.GeneralName; +import org.bouncycastle.asn1.x509.GeneralNames; +import org.bouncycastle.asn1.x509.KeyUsage; +import org.bouncycastle.cert.X509v3CertificateBuilder; +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; +import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.operator.ContentSigner; +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +// We can only test calls to ZKTrustManager using Sockets (not SSLEngines). This can be fine since the logic is the same. +public class ZKTrustManagerTest extends ZKTestCase { + + private static KeyPair keyPair; + + private X509ExtendedTrustManager mockX509ExtendedTrustManager; + private static final String IP_ADDRESS = "127.0.0.1"; + private static final String HOSTNAME = "localhost"; + + private InetAddress mockInetAddress; + private Socket mockSocket; + + @BeforeClass + public static void createKeyPair() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); + keyPairGenerator.initialize(4096); + keyPair = keyPairGenerator.genKeyPair(); + } + + @AfterClass + public static void removeBouncyCastleProvider() throws Exception { + Security.removeProvider("BC"); + } + + @Before + public void setup() throws Exception { + mockX509ExtendedTrustManager = mock(X509ExtendedTrustManager.class); + + mockInetAddress = mock(InetAddress.class); + when(mockInetAddress.getHostAddress()).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + return IP_ADDRESS; + } + }); + + when(mockInetAddress.getHostName()).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + return HOSTNAME; + } + }); + + mockSocket = mock(Socket.class); + when(mockSocket.getInetAddress()).thenAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + return mockInetAddress; + } + }); + } + + private X509Certificate[] createSelfSignedCertifcateChain(String ipAddress, String hostname) throws Exception { + X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); + nameBuilder.addRDN(BCStyle.CN, "NOT_LOCALHOST"); + Date notBefore = new Date(); + Calendar cal = Calendar.getInstance(); + cal.setTime(notBefore); + cal.add(Calendar.YEAR, 1); + Date notAfter = cal.getTime(); + BigInteger serialNumber = new BigInteger(128, new Random()); + + X509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(nameBuilder.build(), serialNumber, notBefore, notAfter, nameBuilder.build(), keyPair.getPublic()).addExtension(Extension.basicConstraints, true, new BasicConstraints(0)).addExtension(Extension.keyUsage, true, new KeyUsage( + KeyUsage.digitalSignature + | KeyUsage.keyCertSign + | KeyUsage.cRLSign)); + + List generalNames = new ArrayList<>(); + if (ipAddress != null) { + generalNames.add(new GeneralName(GeneralName.iPAddress, ipAddress)); + } + if (hostname != null) { + generalNames.add(new GeneralName(GeneralName.dNSName, hostname)); + } + + if (!generalNames.isEmpty()) { + certificateBuilder.addExtension(Extension.subjectAlternativeName, true, new GeneralNames(generalNames.toArray(new GeneralName[]{}))); + } + + ContentSigner contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(keyPair.getPrivate()); + + return new X509Certificate[]{new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(contentSigner))}; + } + + @Test + public void testServerHostnameVerificationWithHostnameVerificationDisabled() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, false, false); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(IP_ADDRESS, HOSTNAME); + zkTrustManager.checkServerTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(0)).getHostAddress(); + verify(mockInetAddress, times(0)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkServerTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testServerHostnameVerificationWithHostnameVerificationDisabledAndClientHostnameVerificationEnabled() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, false, true); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(IP_ADDRESS, HOSTNAME); + zkTrustManager.checkServerTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(0)).getHostAddress(); + verify(mockInetAddress, times(0)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkServerTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testServerHostnameVerificationWithIPAddress() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, false); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(IP_ADDRESS, null); + zkTrustManager.checkServerTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(1)).getHostAddress(); + verify(mockInetAddress, times(0)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkServerTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testServerHostnameVerificationWithHostname() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, false); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); + zkTrustManager.checkServerTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(1)).getHostAddress(); + verify(mockInetAddress, times(1)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkServerTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testClientHostnameVerificationWithHostnameVerificationDisabled() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, false, true); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); + zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(1)).getHostAddress(); + verify(mockInetAddress, times(1)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testClientHostnameVerificationWithClientHostnameVerificationDisabled() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, false); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); + zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(0)).getHostAddress(); + verify(mockInetAddress, times(0)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testClientHostnameVerificationWithIPAddress() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, true); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(IP_ADDRESS, null); + zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(1)).getHostAddress(); + verify(mockInetAddress, times(0)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); + } + + @Test + public void testClientHostnameVerificationWithHostname() throws Exception { + ZKTrustManager zkTrustManager = new ZKTrustManager(mockX509ExtendedTrustManager, true, true); + + X509Certificate[] certificateChain = createSelfSignedCertifcateChain(null, HOSTNAME); + zkTrustManager.checkClientTrusted(certificateChain, null, mockSocket); + + verify(mockInetAddress, times(1)).getHostAddress(); + verify(mockInetAddress, times(1)).getHostName(); + + verify(mockX509ExtendedTrustManager, times(1)).checkClientTrusted(certificateChain, null, mockSocket); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/metrics/BaseTestMetricsProvider.java b/zookeeper-server/src/test/java/org/apache/zookeeper/metrics/BaseTestMetricsProvider.java new file mode 100644 index 0000000..7ce361c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/metrics/BaseTestMetricsProvider.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import org.apache.zookeeper.metrics.impl.NullMetricsProvider; + +/** + * Simple MetricsProvider for tests. + */ +public abstract class BaseTestMetricsProvider implements MetricsProvider { + + @Override + public void configure(Properties prprts) throws MetricsProviderLifeCycleException { + } + + @Override + public void start() throws MetricsProviderLifeCycleException { + } + + @Override + public MetricsContext getRootContext() { + return NullMetricsProvider.NullMetricsContext.INSTANCE; + } + + @Override + public void stop() { + } + + @Override + public void dump(BiConsumer sink) { + } + + @Override + public void resetAllValues() { + } + + public static final class MetricsProviderCapturingLifecycle extends BaseTestMetricsProvider { + + public static final AtomicBoolean configureCalled = new AtomicBoolean(); + public static final AtomicBoolean startCalled = new AtomicBoolean(); + public static final AtomicBoolean stopCalled = new AtomicBoolean(); + public static final AtomicBoolean getRootContextCalled = new AtomicBoolean(); + + public static void reset() { + configureCalled.set(false); + startCalled.set(false); + stopCalled.set(false); + getRootContextCalled.set(false); + } + + @Override + public void configure(Properties prprts) throws MetricsProviderLifeCycleException { + if (!configureCalled.compareAndSet(false, true)) { + // called twice + throw new IllegalStateException(); + } + } + + @Override + public void start() throws MetricsProviderLifeCycleException { + if (!startCalled.compareAndSet(false, true)) { + // called twice + throw new IllegalStateException(); + } + } + + @Override + public MetricsContext getRootContext() { + getRootContextCalled.set(true); + + return NullMetricsProvider.NullMetricsContext.INSTANCE; + } + + @Override + public void stop() { + if (!stopCalled.compareAndSet(false, true)) { + // called twice + throw new IllegalStateException(); + } + } + + } + + public static final class MetricsProviderWithErrorInStart extends BaseTestMetricsProvider { + + @Override + public void start() throws MetricsProviderLifeCycleException { + throw new MetricsProviderLifeCycleException(); + } + + } + + public static final class MetricsProviderWithErrorInConfigure extends BaseTestMetricsProvider { + + @Override + public void configure(Properties prprts) throws MetricsProviderLifeCycleException { + throw new MetricsProviderLifeCycleException(); + } + + } + + public static final class MetricsProviderWithConfiguration extends BaseTestMetricsProvider { + + public static final AtomicInteger httpPort = new AtomicInteger(); + + @Override + public void configure(Properties prprts) throws MetricsProviderLifeCycleException { + httpPort.set(Integer.parseInt(prprts.getProperty("httpPort"))); + } + + } + + public static final class MetricsProviderWithErrorInStop extends BaseTestMetricsProvider { + + public static final AtomicBoolean stopCalled = new AtomicBoolean(); + + @Override + public void stop() { + stopCalled.set(true); + throw new RuntimeException(); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/metrics/MetricsUtils.java b/zookeeper-server/src/test/java/org/apache/zookeeper/metrics/MetricsUtils.java new file mode 100644 index 0000000..25883aa --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/metrics/MetricsUtils.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics; + +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.server.ServerMetrics; + +/** + * Utility for Metrics in tests. + */ +public abstract class MetricsUtils { + + private MetricsUtils() { + } + + /** + * Collect all metrics from a {@link MetricsProvider}. A MetricsProvider + * provides a {@link MetricsProvider#dump(java.util.function.BiConsumer) + * } + * method, that method will in general be more efficient and it does not + * impose to the MetricsProvider to waste resources. + * + * @param metricsProvider + * @return a Map which collects one entry per each different key returned by + * {@link MetricsProvider#dump(java.util.function.BiConsumer) } + */ + public static Map collect(MetricsProvider metricsProvider) { + Map res = new HashMap<>(); + metricsProvider.dump(res::put); + return res; + } + + /** + * Collect current {@link ServerMetrics} as a Map. + * + * @return a flattened view of all metrics reported by the MetricsProvider + * in use by the current ServerMetrics static instance. + */ + public static Map currentServerMetrics() { + return collect(ServerMetrics.getMetrics().getMetricsProvider()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/BlueThrottleTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/BlueThrottleTest.java new file mode 100644 index 0000000..8b64c2b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/BlueThrottleTest.java @@ -0,0 +1,299 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.Random; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BlueThrottleTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(BlueThrottleTest.class); + private static final int RAPID_TIMEOUT = 10000; + + class MockRandom extends Random { + + int flag = 0; + BlueThrottle throttle; + + @Override + public double nextDouble() { + if (throttle.getDropChance() > 0) { + flag = 1 - flag; + return flag; + } else { + return 1; + } + } + + } + + class BlueThrottleWithMockRandom extends BlueThrottle { + + public BlueThrottleWithMockRandom(MockRandom random) { + super(); + this.rng = random; + random.throttle = this; + } + + } + + @Test + public void testThrottleDisabled() { + BlueThrottle throttler = new BlueThrottle(); + assertTrue("Throttle should be disabled by default", throttler.checkLimit(1)); + } + + @Test + public void testThrottleWithoutRefill() { + BlueThrottle throttler = new BlueThrottle(); + throttler.setMaxTokens(1); + throttler.setFillTime(2000); + assertTrue("First request should be allowed", throttler.checkLimit(1)); + assertFalse("Second request should be denied", throttler.checkLimit(1)); + } + + @Test + public void testThrottleWithRefill() throws InterruptedException { + BlueThrottle throttler = new BlueThrottle(); + throttler.setMaxTokens(1); + throttler.setFillTime(500); + assertTrue("First request should be allowed", throttler.checkLimit(1)); + assertFalse("Second request should be denied", throttler.checkLimit(1)); + + //wait for the bucket to be refilled + Thread.sleep(750); + assertTrue("Third request should be allowed since we've got a new token", throttler.checkLimit(1)); + } + + @Test + public void testThrottleWithoutRandomDropping() throws InterruptedException { + int maxTokens = 5; + BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom()); + throttler.setMaxTokens(maxTokens); + throttler.setFillCount(maxTokens); + throttler.setFillTime(1000); + + for (int i = 0; i < maxTokens; i++) { + throttler.checkLimit(1); + } + assertEquals("All tokens should be used up by now", throttler.getMaxTokens(), throttler.getDeficit()); + + Thread.sleep(110); + throttler.checkLimit(1); + assertFalse("Dropping probability should still be zero", throttler.getDropChance() > 0); + + //allow bucket to be refilled + Thread.sleep(1500); + + for (int i = 0; i < maxTokens; i++) { + assertTrue("The first " + maxTokens + " requests should be allowed", throttler.checkLimit(1)); + } + + for (int i = 0; i < maxTokens; i++) { + assertFalse("The latter " + maxTokens + " requests should be denied", throttler.checkLimit(1)); + } + } + + @Test + public void testThrottleWithRandomDropping() throws InterruptedException { + int maxTokens = 5; + BlueThrottle throttler = new BlueThrottleWithMockRandom(new MockRandom()); + throttler.setMaxTokens(maxTokens); + throttler.setFillCount(maxTokens); + throttler.setFillTime(1000); + throttler.setFreezeTime(100); + throttler.setDropIncrease(0.5); + + for (int i = 0; i < maxTokens; i++) { + throttler.checkLimit(1); + } + assertEquals("All tokens should be used up by now", throttler.getMaxTokens(), throttler.getDeficit()); + + Thread.sleep(120); + //this will trigger dropping probability being increased + throttler.checkLimit(1); + assertTrue("Dropping probability should be increased", throttler.getDropChance() > 0); + LOG.info("Dropping probability is {}", throttler.getDropChance()); + + //allow bucket to be refilled + Thread.sleep(1100); + LOG.info("Bucket is refilled with {} tokens.", maxTokens); + + int accepted = 0; + for (int i = 0; i < maxTokens; i++) { + if (throttler.checkLimit(1)) { + accepted++; + } + } + + LOG.info("Send {} requests, {} are accepted", maxTokens, accepted); + assertTrue("The dropping should be distributed", accepted < maxTokens); + + accepted = 0; + + for (int i = 0; i < maxTokens; i++) { + if (throttler.checkLimit(1)) { + accepted++; + } + } + + LOG.info("Send another {} requests, {} are accepted", maxTokens, accepted); + assertTrue("Later requests should have a chance", accepted > 0); + } + + private QuorumUtil quorumUtil = new QuorumUtil(1); + private ClientBase.CountdownWatcher[] watchers; + private ZooKeeper[] zks; + + private int connect(int n) throws Exception { + String connStr = quorumUtil.getConnectionStringForServer(1); + int connected = 0; + + zks = new ZooKeeper[n]; + watchers = new ClientBase.CountdownWatcher[n]; + for (int i = 0; i < n; i++){ + watchers[i] = new ClientBase.CountdownWatcher(); + zks[i] = new ZooKeeper(connStr, 3000, watchers[i]); + try { + watchers[i].waitForConnected(RAPID_TIMEOUT); + connected++; + } catch (TimeoutException e) { + LOG.info("Connection denied by the throttler due to insufficient tokens"); + break; + } + } + + return connected; + } + + private void shutdownQuorum() throws Exception{ + for (ZooKeeper zk : zks) { + if (zk != null) { + zk.close(); + } + } + + quorumUtil.shutdownAll(); + } + + @Test + public void testNoThrottling() throws Exception { + quorumUtil.startAll(); + + //disable throttling + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(0); + + int connected = connect(10); + + Assert.assertEquals(10, connected); + shutdownQuorum(); + } + + @Test + public void testThrottling() throws Exception { + quorumUtil.enableLocalSession(true); + quorumUtil.startAll(); + + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(2); + //no refill, makes testing easier + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setFillCount(0); + + + int connected = connect(3); + Assert.assertEquals(2, connected); + shutdownQuorum(); + + quorumUtil.enableLocalSession(false); + quorumUtil.startAll(); + + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(2); + //no refill, makes testing easier + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setFillCount(0); + + + connected = connect(3); + Assert.assertEquals(2, connected); + shutdownQuorum(); + } + + @Test + public void testWeighedThrottling() throws Exception { + // this test depends on the session weights set to the default values + // 3 for global session, 2 for renew sessions, 1 for local sessions + BlueThrottle.setConnectionWeightEnabled(true); + + quorumUtil.enableLocalSession(true); + quorumUtil.startAll(); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(10); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setFillCount(0); + + //try to create 11 local sessions, 10 created, because we have only 10 tokens + int connected = connect(11); + Assert.assertEquals(10, connected); + shutdownQuorum(); + + quorumUtil.enableLocalSession(false); + quorumUtil.startAll(); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(10); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setFillCount(0); + //tyr to create 11 global sessions, 3 created, because we have 10 tokens and each connection needs 3 + connected = connect(11); + Assert.assertEquals(3, connected); + shutdownQuorum(); + + quorumUtil.startAll(); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(10); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setFillCount(0); + connected = connect(2); + Assert.assertEquals(2, connected); + + quorumUtil.shutdown(1); + watchers[0].waitForDisconnected(RAPID_TIMEOUT); + watchers[1].waitForDisconnected(RAPID_TIMEOUT); + + quorumUtil.restart(1); + //client will try to reconnect + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setMaxTokens(3); + quorumUtil.getPeer(1).peer.getActiveServer().connThrottle().setFillCount(0); + int reconnected = 0; + for (int i = 0; i < 2; i++){ + try { + watchers[i].waitForConnected(RAPID_TIMEOUT); + reconnected++; + } catch (TimeoutException e) { + LOG.info("One reconnect fails due to insufficient tokens"); + } + } + //each reconnect takes two tokens, we have 3, so only one reconnects + LOG.info("reconnected {}", reconnected); + Assert.assertEquals(1, reconnected); + shutdownQuorum(); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ByteBufferInputStreamTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ByteBufferInputStreamTest.java new file mode 100644 index 0000000..7e8c387 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ByteBufferInputStreamTest.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ByteBufferInputStreamTest extends ZKTestCase { + + private static final byte[] DATA_BYTES_0 = "Apache ZooKeeper".getBytes(StandardCharsets.UTF_8); + + private static byte[] DATA_BYTES; + @BeforeClass + public static void setUpClass() { + int len = DATA_BYTES_0.length + 2; + DATA_BYTES = new byte[len]; + System.arraycopy(DATA_BYTES_0, 0, DATA_BYTES, 0, DATA_BYTES_0.length); + DATA_BYTES[len - 2] = (byte) 0x0; + DATA_BYTES[len - 1] = (byte) 0xff; + } + + private ByteBuffer bb; + private ByteBufferInputStream in; + private byte[] bs; + @Before + public void setUp() throws Exception { + bb = ByteBuffer.wrap(DATA_BYTES); + in = new ByteBufferInputStream(bb); + bs = new byte[]{(byte) 1, (byte) 2, (byte) 3, (byte) 4}; + } + + @Test + public void testRead() throws Exception { + for (int i = 0; i < DATA_BYTES.length; i++) { + int b = in.read(); + assertEquals(DATA_BYTES[i], (byte) b); + } + assertEquals(-1, in.read()); + } + @Test + public void testReadArrayOffsetLength() throws Exception { + assertEquals(1, in.read(bs, 2, 1)); + byte[] expected = new byte[]{(byte) 1, (byte) 2, DATA_BYTES[0], (byte) 4}; + assertArrayEquals(expected, bs); + } + @Test(expected = IndexOutOfBoundsException.class) + public void testReadArrayOffsetLength_LengthTooLarge() throws Exception { + in.read(bs, 2, 3); + } + @Test + public void testReadArrayOffsetLength_HitEndOfStream() throws Exception { + for (int i = 0; i < DATA_BYTES.length - 1; i++) { + in.read(); + } + assertEquals(1, in.read(bs, 2, 2)); + byte[] expected = new byte[]{(byte) 1, (byte) 2, DATA_BYTES[DATA_BYTES.length - 1], (byte) 4}; + assertArrayEquals(expected, bs); + } + @Test + public void testReadArrayOffsetLength_AtEndOfStream() throws Exception { + for (int i = 0; i < DATA_BYTES.length; i++) { + in.read(); + } + byte[] expected = Arrays.copyOf(bs, bs.length); + assertEquals(-1, in.read(bs, 2, 2)); + assertArrayEquals(expected, bs); + } + @Test + public void testReadArrayOffsetLength_0Length() throws Exception { + byte[] expected = Arrays.copyOf(bs, bs.length); + assertEquals(0, in.read(bs, 2, 0)); + assertArrayEquals(expected, bs); + } + @Test + public void testReadArray() throws Exception { + byte[] expected = Arrays.copyOf(DATA_BYTES, 4); + assertEquals(4, in.read(bs)); + assertArrayEquals(expected, bs); + } + + @Test + public void testSkip() throws Exception { + in.read(); + assertEquals(2L, in.skip(2L)); + assertEquals(DATA_BYTES[3], in.read()); + assertEquals(DATA_BYTES[4], in.read()); + } + @Test + public void testSkip2() throws Exception { + for (int i = 0; i < DATA_BYTES.length / 2; i++) { + in.read(); + } + long skipAmount = DATA_BYTES.length / 4; + assertEquals(skipAmount, in.skip(skipAmount)); + int idx = DATA_BYTES.length / 2 + (int) skipAmount; + assertEquals(DATA_BYTES[idx++], in.read()); + assertEquals(DATA_BYTES[idx++], in.read()); + } + @Test + public void testNegativeSkip() throws Exception { + in.read(); + assertEquals(0L, in.skip(-2L)); + assertEquals(DATA_BYTES[1], in.read()); + assertEquals(DATA_BYTES[2], in.read()); + } + @Test + public void testSkip_HitEnd() throws Exception { + for (int i = 0; i < DATA_BYTES.length - 1; i++) { + in.read(); + } + assertEquals(1L, in.skip(2L)); + assertEquals(-1, in.read()); + } + @Test + public void testSkip_AtEnd() throws Exception { + for (int i = 0; i < DATA_BYTES.length; i++) { + in.read(); + } + assertEquals(0L, in.skip(2L)); + assertEquals(-1, in.read()); + } + + @Test + public void testAvailable() throws Exception { + for (int i = DATA_BYTES.length; i > 0; i--) { + assertEquals(i, in.available()); + in.read(); + } + assertEquals(0, in.available()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/CRCTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/CRCTest.java new file mode 100644 index 0000000..c57a65f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/CRCTest.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.RandomAccessFile; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.zip.Adler32; +import java.util.zip.CheckedInputStream; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.InputArchive; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.persistence.FileSnap; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CRCTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(CRCTest.class); + + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + /** + * corrupt a file by writing m at 500 b + * offset + * @param file the file to be corrupted + * @throws IOException + */ + private void corruptFile(File file) throws IOException { + // corrupt the logfile + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + byte[] b = "mahadev".getBytes(); + long writeLen = 500L; + raf.seek(writeLen); + //corrupting the data + raf.write(b); + raf.close(); + } + + /** return if checksum matches for a snapshot **/ + private boolean getCheckSum(FileSnap snap, File snapFile) throws IOException { + DataTree dt = new DataTree(); + Map sessions = new ConcurrentHashMap(); + InputStream snapIS = new BufferedInputStream(new FileInputStream(snapFile)); + CheckedInputStream crcIn = new CheckedInputStream(snapIS, new Adler32()); + InputArchive ia = BinaryInputArchive.getArchive(crcIn); + try { + snap.deserialize(dt, sessions, ia); + } catch (IOException ie) { + // we failed on the most recent snapshot + // must be incomplete + // try reading the next one + // after corrupting + snapIS.close(); + crcIn.close(); + throw ie; + } + + long checksum = crcIn.getChecksum().getValue(); + long val = ia.readLong("val"); + snapIS.close(); + crcIn.close(); + return (val != checksum); + } + + /** test checksums for the logs and snapshots. + * the reader should fail on reading + * a corrupt snapshot and a corrupt log + * file + * @throws Exception + */ + @Test + public void testChecksums() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(150); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + for (int i = 0; i < 2000; i++) { + zk.create("/crctest- " + i, ("/crctest- " + i).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + File versionDir = new File(tmpDir, "version-2"); + File[] list = versionDir.listFiles(); + //there should be only two files + // one the snapshot and the other logFile + File snapFile = null; + File logFile = null; + for (File file : list) { + LOG.info("file is {}", file); + if (file.getName().startsWith("log")) { + logFile = file; + corruptFile(logFile); + } + } + FileTxnLog flog = new FileTxnLog(versionDir); + TxnIterator itr = flog.read(1); + //we will get a checksum failure + try { + while (itr.next()) { + // no op + } + fail(); + } catch (IOException ie) { + LOG.warn("crc corruption", ie); + } + itr.close(); + // find the last snapshot + FileSnap snap = new FileSnap(versionDir); + List snapFiles = snap.findNRecentSnapshots(2); + snapFile = snapFiles.get(0); + corruptFile(snapFile); + boolean cfile = false; + try { + cfile = getCheckSum(snap, snapFile); + } catch (IOException ie) { + //the last snapshot seems incompelte + // corrupt the last but one + // and use that + snapFile = snapFiles.get(1); + corruptFile(snapFile); + cfile = getCheckSum(snap, snapFile); + } + assertTrue(cfile); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ConnectionMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ConnectionMetricsTest.java new file mode 100644 index 0000000..2b10f9b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ConnectionMetricsTest.java @@ -0,0 +1,239 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.server.NIOServerCnxnFactory.ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import io.netty.channel.Channel; +import io.netty.channel.EventLoop; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ConnectionMetricsTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(ConnectionMetricsTest.class); + + @Test + public void testRevalidateCount() throws Exception { + ServerMetrics.getMetrics().resetAll(); + QuorumUtil util = new QuorumUtil(1); // create a quorum of 3 servers + // disable local session to make sure we create a global session + util.enableLocalSession(false); + util.startAll(); + + int follower1 = (int) util.getFollowerQuorumPeers().get(0).getId(); + int follower2 = (int) util.getFollowerQuorumPeers().get(1).getId(); + LOG.info("connecting to server: {}", follower1); + ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher(); + // create a connection to follower + ZooKeeper zk = new ZooKeeper(util.getConnectionStringForServer(follower1), ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + LOG.info("connected"); + + // update the connection to allow to connect to the other follower + zk.updateServerList(util.getConnectionStringForServer(follower2)); + + // follower is shut down and zk should be disconnected + util.shutdown(follower1); + watcher.waitForDisconnected(ClientBase.CONNECTION_TIMEOUT); + LOG.info("disconnected"); + // should reconnect to another follower, will ask leader to revalidate + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + LOG.info("reconnected"); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("connection_revalidate_count")); + assertEquals(1L, values.get("revalidate_count")); + + zk.close(); + util.shutdownAll(); + } + + private class MockNIOServerCnxn extends NIOServerCnxn { + + public MockNIOServerCnxn(ZooKeeperServer zk, SocketChannel sock, SelectionKey sk, NIOServerCnxnFactory factory, NIOServerCnxnFactory.SelectorThread selectorThread) throws IOException { + super(zk, sock, sk, factory, selectorThread); + } + + @Override + protected boolean isSocketOpen() { + return true; + } + + } + + private static class FakeSK extends SelectionKey { + + @Override + public SelectableChannel channel() { + return null; + } + + @Override + public Selector selector() { + return mock(Selector.class); + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public void cancel() { + } + + @Override + public int interestOps() { + return ops; + } + + private int ops = OP_WRITE + OP_READ; + + @Override + public SelectionKey interestOps(int ops) { + this.ops = ops; + return this; + } + + @Override + public int readyOps() { + return ops; + } + + } + + private NIOServerCnxn createMockNIOCnxn() throws IOException { + InetSocketAddress socketAddr = new InetSocketAddress(80); + Socket socket = mock(Socket.class); + when(socket.getRemoteSocketAddress()).thenReturn(socketAddr); + SocketChannel sock = mock(SocketChannel.class); + when(sock.socket()).thenReturn(socket); + when(sock.read(any(ByteBuffer.class))).thenReturn(-1); + + return new MockNIOServerCnxn(mock(ZooKeeperServer.class), sock, null, mock(NIOServerCnxnFactory.class), null); + } + + @Test + public void testNIOConnectionDropCount() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + NIOServerCnxn cnxn = createMockNIOCnxn(); + cnxn.doIO(new FakeSK()); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("connection_drop_count")); + } + + @Test + public void testNettyConnectionDropCount() throws Exception { + InetSocketAddress socketAddr = new InetSocketAddress(80); + Channel channel = mock(Channel.class); + when(channel.isOpen()).thenReturn(false); + when(channel.remoteAddress()).thenReturn(socketAddr); + EventLoop eventLoop = mock(EventLoop.class); + when(channel.eventLoop()).thenReturn(eventLoop); + + ServerMetrics.getMetrics().resetAll(); + + NettyServerCnxnFactory factory = new NettyServerCnxnFactory(); + NettyServerCnxn cnxn = new NettyServerCnxn(channel, mock(ZooKeeperServer.class), factory); + + // pretend it's connected + factory.cnxns.add(cnxn); + cnxn.close(); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("connection_drop_count")); + } + + @Test + public void testSessionlessConnectionsExpired() throws Exception { + ServerCnxnFactory factory = new NIOServerCnxnFactory(); + factory.configure(new InetSocketAddress(PortAssignment.unique()), 1000); + factory.start(); + int timeout = Integer.getInteger(ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT, 10000); + + ServerMetrics.getMetrics().resetAll(); + // add two connections w/o touching them so they will expire + ((NIOServerCnxnFactory) factory).touchCnxn(createMockNIOCnxn()); + ((NIOServerCnxnFactory) factory).touchCnxn(createMockNIOCnxn()); + + Map values = MetricsUtils.currentServerMetrics(); + int sleptTime = 0; + while (values.get("sessionless_connections_expired") == null || sleptTime < 2 * timeout) { + Thread.sleep(100); + sleptTime += 100; + values = MetricsUtils.currentServerMetrics(); + } + + assertEquals(2L, values.get("sessionless_connections_expired")); + + factory.shutdown(); + } + + @Test + public void testStaleSessionsExpired() throws Exception { + int tickTime = 1000; + SessionTrackerImpl tracker = new SessionTrackerImpl(mock(ZooKeeperServer.class), new ConcurrentHashMap<>(), tickTime, 1L, null); + + tracker.sessionsById.put(1L, mock(SessionTrackerImpl.SessionImpl.class)); + tracker.sessionsById.put(2L, mock(SessionTrackerImpl.SessionImpl.class)); + + tracker.touchSession(1L, tickTime); + tracker.touchSession(2L, tickTime); + + ServerMetrics.getMetrics().resetAll(); + + tracker.start(); + + Map values = MetricsUtils.currentServerMetrics(); + int sleptTime = 0; + while (values.get("stale_sessions_expired") == null || sleptTime < 2 * tickTime) { + Thread.sleep(100); + sleptTime += 100; + values = MetricsUtils.currentServerMetrics(); + } + + assertEquals(2L, values.get("stale_sessions_expired")); + + tracker.shutdown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/CreateContainerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/CreateContainerTest.java new file mode 100644 index 0000000..03f9bcc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/CreateContainerTest.java @@ -0,0 +1,331 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class CreateContainerTest extends ClientBase { + + private ZooKeeper zk; + private Semaphore completedContainerDeletions; + + @Override + public void setUp() throws Exception { + super.setUp(); + zk = createClient(); + + completedContainerDeletions = new Semaphore(0); + ZKDatabase testDatabase = new ZKDatabase(serverFactory.zkServer.getZKDatabase().snapLog) { + @Override + public void addCommittedProposal(Request request) { + super.addCommittedProposal(request); + if (request.type == ZooDefs.OpCode.deleteContainer) { + completedContainerDeletions.release(); + } + } + }; + serverFactory.zkServer.setZKDatabase(testDatabase); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + zk.close(); + } + + @Test(timeout = 30000) + public void testCreate() throws KeeperException, InterruptedException { + createNoStatVerifyResult("/foo"); + createNoStatVerifyResult("/foo/child"); + } + + @Test(timeout = 30000) + public void testCreateWithStat() throws KeeperException, InterruptedException { + Stat stat = createWithStatVerifyResult("/foo"); + Stat childStat = createWithStatVerifyResult("/foo/child"); + // Don't expect to get the same stats for different creates. + assertFalse(stat.equals(childStat)); + } + + @SuppressWarnings("ConstantConditions") + @Test(timeout = 30000) + public void testCreateWithNullStat() throws KeeperException, InterruptedException { + final String name = "/foo"; + assertNull(zk.exists(name, false)); + + Stat stat = null; + // If a null Stat object is passed the create should still + // succeed, but no Stat info will be returned. + zk.create(name, name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER, stat); + assertNull(stat); + assertNotNull(zk.exists(name, false)); + } + + @Test(timeout = 30000) + public void testSimpleDeletion() throws KeeperException, InterruptedException { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + zk.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/foo/bar", -1); // should cause "/foo" to get deleted when checkContainers() is called + + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100); + containerManager.checkContainers(); + + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + assertNull("Container should have been deleted", zk.exists("/foo", false)); + } + + @Test(timeout = 30000) + public void testMultiWithContainerSimple() throws KeeperException, InterruptedException { + Op createContainer = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + zk.multi(Collections.singletonList(createContainer)); + + DataTree dataTree = serverFactory.getZooKeeperServer().getZKDatabase().getDataTree(); + assertEquals(dataTree.getContainers().size(), 1); + } + + @Test(timeout = 30000) + public void testMultiWithContainer() throws KeeperException, InterruptedException { + Op createContainer = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + Op createChild = Op.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.multi(Arrays.asList(createContainer, createChild)); + + DataTree dataTree = serverFactory.getZooKeeperServer().getZKDatabase().getDataTree(); + assertEquals(dataTree.getContainers().size(), 1); + + zk.delete("/foo/bar", -1); // should cause "/foo" to get deleted when checkContainers() is called + + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100); + containerManager.checkContainers(); + + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + assertNull("Container should have been deleted", zk.exists("/foo", false)); + + createContainer = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + createChild = Op.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Op deleteChild = Op.delete("/foo/bar", -1); + zk.multi(Arrays.asList(createContainer, createChild, deleteChild)); + + containerManager.checkContainers(); + + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + assertNull("Container should have been deleted", zk.exists("/foo", false)); + } + + @Test(timeout = 30000) + public void testSimpleDeletionAsync() throws KeeperException, InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + AsyncCallback.Create2Callback cb = new AsyncCallback.Create2Callback() { + @Override + public void processResult(int rc, String path, Object ctx, String name, Stat stat) { + assertEquals(ctx, "context"); + latch.countDown(); + } + }; + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER, cb, "context"); + assertTrue(latch.await(5, TimeUnit.SECONDS)); + zk.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/foo/bar", -1); // should cause "/foo" to get deleted when checkContainers() is called + + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100); + containerManager.checkContainers(); + + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + assertNull("Container should have been deleted", zk.exists("/foo", false)); + } + + @Test(timeout = 30000) + public void testCascadingDeletion() throws KeeperException, InterruptedException { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + zk.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + zk.create("/foo/bar/one", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/foo/bar/one", -1); // should cause "/foo/bar" and "/foo" to get deleted when checkContainers() is called + + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100); + containerManager.checkContainers(); + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + containerManager.checkContainers(); + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + + assertNull("Container should have been deleted", zk.exists("/foo/bar", false)); + assertNull("Container should have been deleted", zk.exists("/foo", false)); + } + + @Test(timeout = 30000) + public void testFalseEmpty() throws KeeperException, InterruptedException { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + zk.create("/foo/bar", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100) { + @Override + protected Collection getCandidates() { + return Collections.singletonList("/foo"); + } + }; + containerManager.checkContainers(); + + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + assertNotNull("Container should have not been deleted", zk.exists("/foo", false)); + } + + @Test(timeout = 30000) + public void testMaxPerMinute() throws InterruptedException { + final BlockingQueue queue = new LinkedBlockingQueue(); + RequestProcessor processor = new RequestProcessor() { + @Override + public void processRequest(Request request) { + queue.add(new String(request.request.array())); + } + + @Override + public void shutdown() { + } + }; + final ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), processor, 1, 2) { + @Override + protected long getMinIntervalMs() { + return 1000; + } + + @Override + protected Collection getCandidates() { + return Arrays.asList("/one", "/two", "/three", "/four"); + } + }; + Executors.newSingleThreadExecutor().submit(new Callable() { + @Override + public Void call() throws Exception { + containerManager.checkContainers(); + return null; + } + }); + assertEquals(queue.poll(5, TimeUnit.SECONDS), "/one"); + assertEquals(queue.poll(5, TimeUnit.SECONDS), "/two"); + assertEquals(queue.size(), 0); + Thread.sleep(500); + assertEquals(queue.size(), 0); + + assertEquals(queue.poll(5, TimeUnit.SECONDS), "/three"); + assertEquals(queue.poll(5, TimeUnit.SECONDS), "/four"); + } + + @Test(timeout = 30000) + public void testMaxNeverUsedInterval() throws KeeperException, InterruptedException { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + AtomicLong elapsed = new AtomicLong(0); + AtomicInteger deletesQty = new AtomicInteger(0); + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100, 1000) { + @Override + protected void postDeleteRequest(Request request) throws RequestProcessor.RequestProcessorException { + deletesQty.incrementAndGet(); + super.postDeleteRequest(request); + } + + @Override + protected long getElapsed(DataNode node) { + return elapsed.get(); + } + }; + containerManager.checkContainers(); // elapsed time will appear to be 0 - container will not get deleted + assertEquals(deletesQty.get(), 0); + assertNotNull("Container should not have been deleted", zk.exists("/foo", false)); + + elapsed.set(10000); + containerManager.checkContainers(); // elapsed time will appear to be 10000 - container should get deleted + assertTrue(completedContainerDeletions.tryAcquire(1, TimeUnit.SECONDS)); + assertNull("Container should have been deleted", zk.exists("/foo", false)); + } + + @Test(timeout = 30000) + public void testZeroMaxNeverUsedInterval() throws KeeperException, InterruptedException { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + AtomicInteger deletesQty = new AtomicInteger(0); + ContainerManager containerManager = new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100, 0) { + @Override + protected void postDeleteRequest(Request request) throws RequestProcessor.RequestProcessorException { + deletesQty.incrementAndGet(); + super.postDeleteRequest(request); + } + + @Override + protected long getElapsed(DataNode node) { + return 10000; // some number greater than 0 + } + }; + containerManager.checkContainers(); // elapsed time will appear to be 0 - container will not get deleted + assertEquals(deletesQty.get(), 0); + assertNotNull("Container should not have been deleted", zk.exists("/foo", false)); + } + + private void createNoStatVerifyResult(String newName) throws KeeperException, InterruptedException { + assertNull("Node existed before created", zk.exists(newName, false)); + zk.create(newName, newName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER); + assertNotNull("Node was not created as expected", zk.exists(newName, false)); + } + + private Stat createWithStatVerifyResult(String newName) throws KeeperException, InterruptedException { + assertNull("Node existed before created", zk.exists(newName, false)); + Stat stat = new Stat(); + zk.create(newName, newName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER, stat); + validateCreateStat(stat, newName); + + Stat referenceStat = zk.exists(newName, false); + assertNotNull("Node was not created as expected", referenceStat); + assertEquals(referenceStat, stat); + + return stat; + } + + private void validateCreateStat(Stat stat, String name) { + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/CreateTTLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/CreateTTLTest.java new file mode 100644 index 0000000..b4c8fa0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/CreateTTLTest.java @@ -0,0 +1,260 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.CreateResponse; +import org.apache.zookeeper.proto.CreateTTLRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class CreateTTLTest extends ClientBase { + + private TestableZooKeeper zk; + + private static final Collection disabledTests = Collections.singleton("testDisabled"); + + @Override + public void setUp() throws Exception { + System.setProperty( + EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY, + disabledTests.contains(getTestName()) ? "false" : "true"); + super.setUpWithServerId(254); + zk = createClient(); + } + + @Override + public void tearDown() throws Exception { + System.clearProperty(EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY); + super.tearDown(); + zk.close(); + } + + @Test + public void testCreate() throws KeeperException, InterruptedException { + Stat stat = new Stat(); + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); + assertEquals(0, stat.getEphemeralOwner()); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); + + fakeElapsed.set(1000); + containerManager.checkContainers(); + assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); + } + + @Test + public void testBadTTLs() throws InterruptedException, KeeperException { + RequestHeader h = new RequestHeader(1, ZooDefs.OpCode.createTTL); + + String path = "/bad_ttl"; + CreateTTLRequest request = new CreateTTLRequest(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL.toFlag(), -100); + CreateResponse response = new CreateResponse(); + ReplyHeader r = zk.submitRequest(h, request, response, null); + assertEquals("An invalid CreateTTLRequest should throw BadArguments", r.getErr(), Code.BADARGUMENTS.intValue()); + assertNull("An invalid CreateTTLRequest should not result in znode creation", zk.exists(path, false)); + + request = new CreateTTLRequest(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL.toFlag(), + EphemeralType.TTL.maxValue() + + 1); + response = new CreateResponse(); + r = zk.submitRequest(h, request, response, null); + assertEquals("An invalid CreateTTLRequest should throw BadArguments", r.getErr(), Code.BADARGUMENTS.intValue()); + assertNull("An invalid CreateTTLRequest should not result in znode creation", zk.exists(path, false)); + } + + @Test + public void testMaxTTLs() throws InterruptedException, KeeperException { + RequestHeader h = new RequestHeader(1, ZooDefs.OpCode.createTTL); + + String path = "/bad_ttl"; + CreateTTLRequest request = new CreateTTLRequest(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL.toFlag(), EphemeralType.TTL.maxValue()); + CreateResponse response = new CreateResponse(); + ReplyHeader r = zk.submitRequest(h, request, response, null); + assertEquals("EphemeralType.getMaxTTL() should succeed", r.getErr(), Code.OK.intValue()); + assertNotNull("Node should exist", zk.exists(path, false)); + } + + @Test + public void testCreateSequential() throws KeeperException, InterruptedException { + Stat stat = new Stat(); + String path = zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, stat, 100); + assertEquals(0, stat.getEphemeralOwner()); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists(path, false)); + + fakeElapsed.set(1000); + containerManager.checkContainers(); + assertNull("Ttl node should have been deleted", zk.exists(path, false)); + } + + @Test + public void testCreateAsync() throws KeeperException, InterruptedException { + AsyncCallback.Create2Callback callback = new AsyncCallback.Create2Callback() { + @Override + public void processResult(int rc, String path, Object ctx, String name, Stat stat) { + // NOP + } + }; + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, callback, null, 100); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); + + fakeElapsed.set(1000); + containerManager.checkContainers(); + assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); + } + + @Test + public void testModifying() throws KeeperException, InterruptedException { + Stat stat = new Stat(); + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); + assertEquals(0, stat.getEphemeralOwner()); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); + + for (int i = 0; i < 10; ++i) { + fakeElapsed.set(50); + zk.setData("/foo", new byte[i + 1], -1); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); + } + + fakeElapsed.set(200); + containerManager.checkContainers(); + assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); + } + + @Test + public void testMulti() throws KeeperException, InterruptedException { + Op createTtl = Op.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, 100); + Op createTtlSequential = Op.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 200); + Op createNonTtl = Op.create("/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + List results = zk.multi(Arrays.asList(createTtl, createTtlSequential, createNonTtl)); + String sequentialPath = ((OpResult.CreateResult) results.get(1)).getPath(); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("node should not have been deleted yet", zk.exists("/a", false)); + assertNotNull("node should not have been deleted yet", zk.exists(sequentialPath, false)); + assertNotNull("node should never be deleted", zk.exists("/c", false)); + + fakeElapsed.set(110); + containerManager.checkContainers(); + assertNull("node should have been deleted", zk.exists("/a", false)); + assertNotNull("node should not have been deleted yet", zk.exists(sequentialPath, false)); + assertNotNull("node should never be deleted", zk.exists("/c", false)); + + fakeElapsed.set(210); + containerManager.checkContainers(); + assertNull("node should have been deleted", zk.exists("/a", false)); + assertNull("node should have been deleted", zk.exists(sequentialPath, false)); + assertNotNull("node should never be deleted", zk.exists("/c", false)); + } + + @Test + public void testBadUsage() throws KeeperException, InterruptedException { + for (CreateMode createMode : CreateMode.values()) { + try { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, new Stat(), createMode.isTTL() ? 0 : 100); + fail("should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException dummy) { + // correct + } + } + + for (CreateMode createMode : CreateMode.values()) { + AsyncCallback.Create2Callback callback = new AsyncCallback.Create2Callback() { + @Override + public void processResult(int rc, String path, Object ctx, String name, Stat stat) { + // NOP + } + }; + try { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode, callback, null, createMode.isTTL() ? 0 : 100); + fail("should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException dummy) { + // correct + } + } + + try { + Op op = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, 0); + zk.multi(Collections.singleton(op)); + fail("should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException dummy) { + // correct + } + try { + Op op = Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 0); + zk.multi(Collections.singleton(op)); + fail("should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException dummy) { + // correct + } + } + + @Test(expected = KeeperException.UnimplementedException.class) + public void testDisabled() throws KeeperException, InterruptedException { + // note, setUp() enables this test based on the test name + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, new Stat(), 100); + } + + private ContainerManager newContainerManager(final AtomicLong fakeElapsed) { + return new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100) { + @Override + protected long getElapsed(DataNode node) { + return fakeElapsed.get(); + } + }; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/DataNodeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DataNodeTest.java new file mode 100644 index 0000000..0fb6f68 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DataNodeTest.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import java.util.Set; +import org.junit.Test; + +public class DataNodeTest { + + @Test + public void testGetChildrenShouldReturnEmptySetWhenThereAreNoChidren() { + // create DataNode and call getChildren + DataNode dataNode = new DataNode(); + Set children = dataNode.getChildren(); + assertNotNull(children); + assertEquals(0, children.size()); + + // add child,remove child and then call getChildren + String child = "child"; + dataNode.addChild(child); + dataNode.removeChild(child); + children = dataNode.getChildren(); + assertNotNull(children); + assertEquals(0, children.size()); + + // Returned empty set must not be modifiable + children = dataNode.getChildren(); + try { + children.add("new child"); + fail("UnsupportedOperationException is expected"); + } catch (UnsupportedOperationException e) { + // do nothing + } + } + + @Test + public void testGetChildrenReturnsImmutableEmptySet() { + DataNode dataNode = new DataNode(); + Set children = dataNode.getChildren(); + try { + children.add("new child"); + fail("UnsupportedOperationException is expected"); + } catch (UnsupportedOperationException e) { + // do nothing + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/DataTreeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DataTreeTest.java new file mode 100644 index 0000000..da9a12a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DataTreeTest.java @@ -0,0 +1,616 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.KeeperException.NodeExistsException; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.PathTrie; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DataTreeTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(DataTreeTest.class); + + /** + * For ZOOKEEPER-1755 - Test race condition when taking dumpEphemerals and + * removing the session related ephemerals from DataTree structure + */ + @Test(timeout = 60000) + public void testDumpEphemerals() throws Exception { + int count = 1000; + long session = 1000; + long zxid = 2000; + final DataTree dataTree = new DataTree(); + LOG.info("Create {} zkclient sessions and its ephemeral nodes", count); + createEphemeralNode(session, dataTree, count); + final AtomicBoolean exceptionDuringDumpEphemerals = new AtomicBoolean(false); + final AtomicBoolean running = new AtomicBoolean(true); + Thread thread = new Thread() { + public void run() { + PrintWriter pwriter = new PrintWriter(new StringWriter()); + try { + while (running.get()) { + dataTree.dumpEphemerals(pwriter); + } + } catch (Exception e) { + LOG.error("Received exception while dumpEphemerals!", e); + exceptionDuringDumpEphemerals.set(true); + } + } + }; + thread.start(); + LOG.debug("Killing {} zkclient sessions and its ephemeral nodes", count); + killZkClientSession(session, zxid, dataTree, count); + running.set(false); + thread.join(); + assertFalse("Should have got exception while dumpEphemerals!", exceptionDuringDumpEphemerals.get()); + } + + private void killZkClientSession(long session, long zxid, final DataTree dataTree, int count) { + for (int i = 0; i < count; i++) { + dataTree.killSession(session + i, zxid); + } + } + + private void createEphemeralNode(long session, final DataTree dataTree, int count) throws NoNodeException, NodeExistsException { + for (int i = 0; i < count; i++) { + dataTree.createNode("/test" + i, new byte[0], null, session + i, dataTree.getNode("/").stat.getCversion() + + 1, 1, 1); + } + } + + @Test(timeout = 60000) + public void testRootWatchTriggered() throws Exception { + DataTree dt = new DataTree(); + + CompletableFuture fire = new CompletableFuture<>(); + // set a watch on the root node + dt.getChildren("/", new Stat(), event -> { + if (event.getPath().equals("/")) { + fire.complete(null); + } + }); + + // add a new node, should trigger a watch + dt.createNode("/xyz", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1); + + assertTrue("Root node watch not triggered", fire.isDone()); + } + + /** + * For ZOOKEEPER-1046 test if cversion is getting incremented correctly. + */ + @Test(timeout = 60000) + public void testIncrementCversion() throws Exception { + try { + // digestCalculator gets initialized for the new DataTree constructor based on the system property + ZooKeeperServer.setDigestEnabled(true); + DataTree dt = new DataTree(); + dt.createNode("/test", new byte[0], null, 0, dt.getNode("/").stat.getCversion() + 1, 1, 1); + DataNode zk = dt.getNode("/test"); + int prevCversion = zk.stat.getCversion(); + long prevPzxid = zk.stat.getPzxid(); + long digestBefore = dt.getTreeDigest(); + dt.setCversionPzxid("/test/", prevCversion + 1, prevPzxid + 1); + int newCversion = zk.stat.getCversion(); + long newPzxid = zk.stat.getPzxid(); + assertTrue(" verification failed. Expected: <" + + (prevCversion + 1) + + ", " + + (prevPzxid + 1) + + ">, found: <" + + newCversion + + ", " + + newPzxid + + ">", (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1)); + assertNotEquals(digestBefore, dt.getTreeDigest()); + } finally { + ZooKeeperServer.setDigestEnabled(false); + } + } + + @Test + public void testNoCversionRevert() throws Exception { + DataTree dt = new DataTree(); + DataNode parent = dt.getNode("/"); + dt.createNode("/test", new byte[0], null, 0, parent.stat.getCversion() + 1, 1, 1); + int currentCversion = parent.stat.getCversion(); + long currentPzxid = parent.stat.getPzxid(); + dt.createNode("/test1", new byte[0], null, 0, currentCversion - 1, 1, 1); + parent = dt.getNode("/"); + int newCversion = parent.stat.getCversion(); + long newPzxid = parent.stat.getPzxid(); + assertTrue(" verification failed. Expected: <" + + currentCversion + + ", " + + currentPzxid + + ">, found: <" + + newCversion + + ", " + + newPzxid + + ">", (newCversion >= currentCversion && newPzxid >= currentPzxid)); + } + + @Test + public void testPzxidUpdatedWhenDeletingNonExistNode() throws Exception { + DataTree dt = new DataTree(); + DataNode root = dt.getNode("/"); + long currentPzxid = root.stat.getPzxid(); + + // pzxid updated with deleteNode on higher zxid + long zxid = currentPzxid + 1; + try { + dt.deleteNode("/testPzxidUpdatedWhenDeletingNonExistNode", zxid); + } catch (NoNodeException e) { /* expected */ } + root = dt.getNode("/"); + currentPzxid = root.stat.getPzxid(); + assertEquals(currentPzxid, zxid); + + // pzxid not updated with smaller zxid + long prevPzxid = currentPzxid; + zxid = prevPzxid - 1; + try { + dt.deleteNode("/testPzxidUpdatedWhenDeletingNonExistNode", zxid); + } catch (NoNodeException e) { /* expected */ } + root = dt.getNode("/"); + currentPzxid = root.stat.getPzxid(); + assertEquals(currentPzxid, prevPzxid); + } + + @Test + public void testDigestUpdatedWhenReplayCreateTxnForExistNode() { + try { + // digestCalculator gets initialized for the new DataTree constructor based on the system property + ZooKeeperServer.setDigestEnabled(true); + DataTree dt = new DataTree(); + + dt.processTxn(new TxnHeader(13, 1000, 1, 30, ZooDefs.OpCode.create), new CreateTxn("/foo", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1)); + + // create the same node with a higher cversion to simulate the + // scenario when replaying a create txn for an existing node due + // to fuzzy snapshot + dt.processTxn(new TxnHeader(13, 1000, 1, 30, ZooDefs.OpCode.create), new CreateTxn("/foo", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 2)); + + // check the current digest value + assertEquals(dt.getTreeDigest(), dt.getLastProcessedZxidDigest().getDigest()); + } finally { + ZooKeeperServer.setDigestEnabled(false); + } + } + + @Test(timeout = 60000) + public void testPathTrieClearOnDeserialize() throws Exception { + + //Create a DataTree with quota nodes so PathTrie get updated + DataTree dserTree = new DataTree(); + + dserTree.createNode("/bug", new byte[20], null, -1, 1, 1, 1); + dserTree.createNode(Quotas.quotaZookeeper + "/bug", null, null, -1, 1, 1, 1); + dserTree.createNode(Quotas.quotaPath("/bug"), new byte[20], null, -1, 1, 1, 1); + dserTree.createNode(Quotas.statPath("/bug"), new byte[20], null, -1, 1, 1, 1); + + //deserialize a DataTree; this should clear the old /bug nodes and pathTrie + DataTree tree = new DataTree(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); + tree.serialize(oa, "test"); + baos.flush(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + BinaryInputArchive ia = BinaryInputArchive.getArchive(bais); + dserTree.deserialize(ia, "test"); + + Field pfield = DataTree.class.getDeclaredField("pTrie"); + pfield.setAccessible(true); + PathTrie pTrie = (PathTrie) pfield.get(dserTree); + + //Check that the node path is removed from pTrie + assertEquals("/bug is still in pTrie", "/", pTrie.findMaxPrefix("/bug")); + } + + + /* ZOOKEEPER-3531 - org.apache.zookeeper.server.DataTree#serialize calls the aclCache.serialize when doing + * dataree serialization, however, org.apache.zookeeper.server.ReferenceCountedACLCache#serialize + * could get stuck at OutputArchieve.writeInt due to potential network/disk issues. + * This can cause the system experiences hanging issues similar to ZooKeeper-2201. + * This test verifies the fix that we should not hold ACL cache during dumping aclcache to snapshots + */ + @Test(timeout = 60000) + public void testSerializeDoesntLockACLCacheWhileWriting() throws Exception { + DataTree tree = new DataTree(); + tree.createNode("/marker", new byte[]{42}, null, -1, 1, 1, 1); + final AtomicBoolean ranTestCase = new AtomicBoolean(); + DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream()); + BinaryOutputArchive oa = new BinaryOutputArchive(out) { + @Override + public void writeInt(int size, String tag) throws IOException { + final Semaphore semaphore = new Semaphore(0); + + new Thread(new Runnable() { + @Override + public void run() { + + synchronized (tree.getReferenceCountedAclCache()) { + //When we lock ACLCache, allow writeRecord to continue + semaphore.release(); + } + } + }).start(); + + try { + boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS); + //This is the real assertion - could another thread lock + //the ACLCache + assertTrue("Couldn't acquire a lock on the ACLCache while we were calling tree.serialize", acquired); + } catch (InterruptedException e1) { + throw new RuntimeException(e1); + } + ranTestCase.set(true); + + super.writeInt(size, tag); + } + }; + + tree.serialize(oa, "test"); + + //Let's make sure that we hit the code that ran the real assertion above + assertTrue("Didn't find the expected node", ranTestCase.get()); + } + + /* ZOOKEEPER-3531 - similarly for aclCache.deserialize, we should not hold lock either + */ + @Test(timeout = 60000) + public void testDeserializeDoesntLockACLCacheWhileReading() throws Exception { + DataTree tree = new DataTree(); + tree.createNode("/marker", new byte[]{42}, null, -1, 1, 1, 1); + final AtomicBoolean ranTestCase = new AtomicBoolean(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(baos); + BinaryOutputArchive oa = new BinaryOutputArchive(out); + + tree.serialize(oa, "test"); + + DataTree tree2 = new DataTree(); + DataInputStream in = new DataInputStream(new ByteArrayInputStream(baos.toByteArray())); + BinaryInputArchive ia = new BinaryInputArchive(in) { + @Override + public long readLong(String tag) throws IOException { + final Semaphore semaphore = new Semaphore(0); + + new Thread(new Runnable() { + @Override + public void run() { + + synchronized (tree2.getReferenceCountedAclCache()) { + //When we lock ACLCache, allow readLong to continue + semaphore.release(); + } + } + }).start(); + + try { + boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS); + //This is the real assertion - could another thread lock + //the ACLCache + assertTrue("Couldn't acquire a lock on the ACLCache while we were calling tree.deserialize", acquired); + } catch (InterruptedException e1) { + throw new RuntimeException(e1); + } + ranTestCase.set(true); + + return super.readLong(tag); + } + }; + + tree2.deserialize(ia, "test"); + + //Let's make sure that we hit the code that ran the real assertion above + assertTrue("Didn't find the expected node", ranTestCase.get()); + } + + /* + * ZOOKEEPER-2201 - OutputArchive.writeRecord can block for long periods of + * time, we must call it outside of the node lock. + * We call tree.serialize, which calls our modified writeRecord method that + * blocks until it can verify that a separate thread can lock the DataNode + * currently being written, i.e. that DataTree.serializeNode does not hold + * the DataNode lock while calling OutputArchive.writeRecord. + */ + @Test(timeout = 60000) + public void testSerializeDoesntLockDataNodeWhileWriting() throws Exception { + DataTree tree = new DataTree(); + tree.createNode("/marker", new byte[]{42}, null, -1, 1, 1, 1); + final DataNode markerNode = tree.getNode("/marker"); + final AtomicBoolean ranTestCase = new AtomicBoolean(); + DataOutputStream out = new DataOutputStream(new ByteArrayOutputStream()); + BinaryOutputArchive oa = new BinaryOutputArchive(out) { + @Override + public void writeRecord(Record r, String tag) throws IOException { + // Need check if the record is a DataNode instance because of changes in ZOOKEEPER-2014 + // which adds default ACL to config node. + if (r instanceof DataNode) { + DataNode node = (DataNode) r; + if (node.data.length == 1 && node.data[0] == 42) { + final Semaphore semaphore = new Semaphore(0); + new Thread(new Runnable() { + @Override + public void run() { + synchronized (markerNode) { + //When we lock markerNode, allow writeRecord to continue + semaphore.release(); + } + } + }).start(); + + try { + boolean acquired = semaphore.tryAcquire(30, TimeUnit.SECONDS); + //This is the real assertion - could another thread lock + //the DataNode we're currently writing + assertTrue("Couldn't acquire a lock on the DataNode while we were calling tree.serialize", acquired); + } catch (InterruptedException e1) { + throw new RuntimeException(e1); + } + ranTestCase.set(true); + } + } + + super.writeRecord(r, tag); + } + }; + + tree.serialize(oa, "test"); + + //Let's make sure that we hit the code that ran the real assertion above + assertTrue("Didn't find the expected node", ranTestCase.get()); + } + + @Test(timeout = 60000) + public void testReconfigACLClearOnDeserialize() throws Exception { + + DataTree tree = new DataTree(); + // simulate the upgrading scenario, where the reconfig znode + // doesn't exist and the acl cache is empty + tree.deleteNode(ZooDefs.CONFIG_NODE, 1); + tree.getReferenceCountedAclCache().aclIndex = 0; + + assertEquals("expected to have 1 acl in acl cache map", 0, tree.aclCacheSize()); + + // serialize the data with one znode with acl + tree.createNode("/bug", new byte[20], ZooDefs.Ids.OPEN_ACL_UNSAFE, -1, 1, 1, 1); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); + tree.serialize(oa, "test"); + baos.flush(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + BinaryInputArchive ia = BinaryInputArchive.getArchive(bais); + tree.deserialize(ia, "test"); + + assertEquals("expected to have 1 acl in acl cache map", 1, tree.aclCacheSize()); + assertEquals("expected to have the same acl", ZooDefs.Ids.OPEN_ACL_UNSAFE, tree.getACL("/bug", new Stat())); + + // simulate the upgrading case where the config node will be created + // again after leader election + tree.addConfigNode(); + + assertEquals("expected to have 2 acl in acl cache map", 2, tree.aclCacheSize()); + assertEquals("expected to have the same acl", ZooDefs.Ids.OPEN_ACL_UNSAFE, tree.getACL("/bug", new Stat())); + } + + @Test + public void testCachedApproximateDataSize() throws Exception { + DataTree dt = new DataTree(); + long initialSize = dt.approximateDataSize(); + assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); + + // create a node + dt.createNode("/testApproximateDataSize", new byte[20], null, -1, 1, 1, 1); + dt.createNode("/testApproximateDataSize1", new byte[20], null, -1, 1, 1, 1); + assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); + + // update data + dt.setData("/testApproximateDataSize1", new byte[32], -1, 1, 1); + assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); + + // delete a node + dt.deleteNode("/testApproximateDataSize", -1); + assertEquals(dt.cachedApproximateDataSize(), dt.approximateDataSize()); + } + + @Test + public void testGetAllChildrenNumber() throws Exception { + DataTree dt = new DataTree(); + // create a node + dt.createNode("/all_children_test", new byte[20], null, -1, 1, 1, 1); + dt.createNode("/all_children_test/nodes", new byte[20], null, -1, 1, 1, 1); + dt.createNode("/all_children_test/nodes/node1", new byte[20], null, -1, 1, 1, 1); + dt.createNode("/all_children_test/nodes/node2", new byte[20], null, -1, 1, 1, 1); + dt.createNode("/all_children_test/nodes/node3", new byte[20], null, -1, 1, 1, 1); + assertEquals(4, dt.getAllChildrenNumber("/all_children_test")); + assertEquals(3, dt.getAllChildrenNumber("/all_children_test/nodes")); + assertEquals(0, dt.getAllChildrenNumber("/all_children_test/nodes/node1")); + //add these three init nodes:/zookeeper,/zookeeper/quota,/zookeeper/config,so the number is 8. + assertEquals(8, dt.getAllChildrenNumber("/")); + } + + @Test + public void testDeserializeZxidDigest() throws Exception { + try { + ZooKeeperServer.setDigestEnabled(true); + DataTree dt = new DataTree(); + dt.processTxn(new TxnHeader(13, 1000, 1, 30, ZooDefs.OpCode.create), + new CreateTxn("/foo", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); + dt.serializeZxidDigest(oa); + baos.flush(); + + DataTree.ZxidDigest zd = dt.getLastProcessedZxidDigest(); + assertNotNull(zd); + + // deserialize data tree + InputArchive ia = BinaryInputArchive.getArchive( + new ByteArrayInputStream(baos.toByteArray())); + dt.deserializeZxidDigest(ia, zd.getZxid()); + assertNotNull(dt.getDigestFromLoadedSnapshot()); + + ia = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray())); + dt.deserializeZxidDigest(ia, zd.getZxid() + 1); + assertNull(dt.getDigestFromLoadedSnapshot()); + } finally { + ZooKeeperServer.setDigestEnabled(false); + } + } + + @Test + public void testDataTreeMetrics() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + long readBytes1 = 0; + long readBytes2 = 0; + long writeBytes1 = 0; + long writeBytes2 = 0; + + final String TOP1 = "top1"; + final String TOP2 = "ttop2"; + final String TOP1PATH = "/" + TOP1; + final String TOP2PATH = "/" + TOP2; + final String CHILD1 = "child1"; + final String CHILD2 = "springishere"; + final String CHILD1PATH = TOP1PATH + "/" + CHILD1; + final String CHILD2PATH = TOP1PATH + "/" + CHILD2; + + final int TOP2_LEN = 50; + final int CHILD1_LEN = 100; + final int CHILD2_LEN = 250; + + DataTree dt = new DataTree(); + dt.createNode(TOP1PATH, null, null, -1, 1, 1, 1); + writeBytes1 += TOP1PATH.length(); + dt.createNode(TOP2PATH, new byte[TOP2_LEN], null, -1, 1, 1, 1); + writeBytes2 += TOP2PATH.length() + TOP2_LEN; + dt.createNode(CHILD1PATH, null, null, -1, 1, 1, 1); + writeBytes1 += CHILD1PATH.length(); + dt.setData(CHILD1PATH, new byte[CHILD1_LEN], 1, -1, 1); + writeBytes1 += CHILD1PATH.length() + CHILD1_LEN; + dt.createNode(CHILD2PATH, new byte[CHILD2_LEN], null, -1, 1, 1, 1); + writeBytes1 += CHILD2PATH.length() + CHILD2_LEN; + dt.getData(TOP1PATH, new Stat(), null); + readBytes1 += TOP1PATH.length() + DataTree.STAT_OVERHEAD_BYTES; + dt.getData(TOP2PATH, new Stat(), null); + readBytes2 += TOP2PATH.length() + TOP2_LEN + DataTree.STAT_OVERHEAD_BYTES; + dt.statNode(CHILD2PATH, null); + readBytes1 += CHILD2PATH.length() + DataTree.STAT_OVERHEAD_BYTES; + dt.getChildren(TOP1PATH, new Stat(), null); + readBytes1 += TOP1PATH.length() + CHILD1.length() + CHILD2.length() + DataTree.STAT_OVERHEAD_BYTES; + dt.deleteNode(TOP1PATH, 1); + writeBytes1 += TOP1PATH.length(); + + Map values = MetricsUtils.currentServerMetrics(); + System.out.println("values:" + values); + assertEquals(writeBytes1, values.get("sum_" + TOP1 + "_write_per_namespace")); + assertEquals(5L, values.get("cnt_" + TOP1 + "_write_per_namespace")); + assertEquals(writeBytes2, values.get("sum_" + TOP2 + "_write_per_namespace")); + assertEquals(1L, values.get("cnt_" + TOP2 + "_write_per_namespace")); + + assertEquals(readBytes1, values.get("sum_" + TOP1 + "_read_per_namespace")); + assertEquals(3L, values.get("cnt_" + TOP1 + "_read_per_namespace")); + assertEquals(readBytes2, values.get("sum_" + TOP2 + "_read_per_namespace")); + assertEquals(1L, values.get("cnt_" + TOP2 + "_read_per_namespace")); + } + + /** + * Test digest with general ops in DataTree, check that digest are + * updated when call different ops. + */ + @Test + public void testDigest() throws Exception { + try { + // enable diegst check + ZooKeeperServer.setDigestEnabled(true); + + DataTree dt = new DataTree(); + + // create a node and check the digest is updated + long previousDigest = dt.getTreeDigest(); + dt.createNode("/digesttest", new byte[0], null, -1, 1, 1, 1); + assertNotEquals(dt.getTreeDigest(), previousDigest); + + // create a child and check the digest is updated + previousDigest = dt.getTreeDigest(); + dt.createNode("/digesttest/1", "1".getBytes(), null, -1, 2, 2, 2); + assertNotEquals(dt.getTreeDigest(), previousDigest); + + // check the digest is not chhanged when creating the same node + previousDigest = dt.getTreeDigest(); + try { + dt.createNode("/digesttest/1", "1".getBytes(), null, -1, 2, 2, 2); + } catch (NodeExistsException e) { /* ignore */ } + assertEquals(dt.getTreeDigest(), previousDigest); + + // check digest with updated data + previousDigest = dt.getTreeDigest(); + dt.setData("/digesttest/1", "2".getBytes(), 3, 3, 3); + assertNotEquals(dt.getTreeDigest(), previousDigest); + + // check digest with deleted node + previousDigest = dt.getTreeDigest(); + dt.deleteNode("/digesttest/1", 5); + assertNotEquals(dt.getTreeDigest(), previousDigest); + } finally { + ZooKeeperServer.setDigestEnabled(false); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/DatadirCleanupManagerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DatadirCleanupManagerTest.java new file mode 100644 index 0000000..d8b2185 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DatadirCleanupManagerTest.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.server.DatadirCleanupManager.PurgeTaskStatus.COMPLETED; +import static org.apache.zookeeper.server.DatadirCleanupManager.PurgeTaskStatus.NOT_STARTED; +import static org.apache.zookeeper.server.DatadirCleanupManager.PurgeTaskStatus.STARTED; +import static org.junit.Assert.assertEquals; +import java.io.File; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DatadirCleanupManagerTest extends ZKTestCase { + + private DatadirCleanupManager purgeMgr; + private File snapDir; + private File dataLogDir; + + @Before + public void setUp() throws Exception { + File dataDir = ClientBase.createTmpDir(); + snapDir = dataDir; + dataLogDir = dataDir; + } + + @Test + public void testPurgeTask() throws Exception { + purgeMgr = new DatadirCleanupManager(snapDir, dataLogDir, 3, 1); + purgeMgr.start(); + assertEquals("Data log directory is not set as configured", dataLogDir, purgeMgr.getDataLogDir()); + assertEquals("Snapshot directory is not set as configured", snapDir, purgeMgr.getSnapDir()); + assertEquals("Snapshot retain count is not set as configured", 3, purgeMgr.getSnapRetainCount()); + assertEquals("Purge task is not started", STARTED, purgeMgr.getPurgeTaskStatus()); + purgeMgr.shutdown(); + assertEquals("Purge task is still running after shutdown", COMPLETED, purgeMgr.getPurgeTaskStatus()); + } + + @Test + public void testWithZeroPurgeInterval() throws Exception { + purgeMgr = new DatadirCleanupManager(snapDir, dataLogDir, 3, 0); + purgeMgr.start(); + assertEquals("Purge task is scheduled with zero purge interval", NOT_STARTED, purgeMgr.getPurgeTaskStatus()); + purgeMgr.shutdown(); + assertEquals("Purge task is scheduled with zero purge interval", NOT_STARTED, purgeMgr.getPurgeTaskStatus()); + } + + @Test + public void testWithNegativePurgeInterval() throws Exception { + purgeMgr = new DatadirCleanupManager(snapDir, dataLogDir, 3, -1); + purgeMgr.start(); + assertEquals("Purge task is scheduled with negative purge interval", NOT_STARTED, purgeMgr.getPurgeTaskStatus()); + purgeMgr.shutdown(); + assertEquals("Purge task is scheduled with negative purge interval", NOT_STARTED, purgeMgr.getPurgeTaskStatus()); + } + + @After + public void tearDown() throws Exception { + if (purgeMgr != null) { + purgeMgr.shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/DeserializationPerfTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DeserializationPerfTest.java new file mode 100644 index 0000000..064b53e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/DeserializationPerfTest.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DeserializationPerfTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(DeserializationPerfTest.class); + + private static void deserializeTree(int depth, int width, int len) throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + BinaryInputArchive ia; + int count; + { + DataTree tree = new DataTree(); + SerializationPerfTest.createNodes(tree, "/", depth, width, tree.getNode("/").stat.getCversion(), new byte[len]); + count = tree.getNodeCount(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); + tree.serialize(oa, "test"); + baos.flush(); + + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); + ia = BinaryInputArchive.getArchive(bais); + } + + DataTree dserTree = new DataTree(); + + System.gc(); + long start = System.nanoTime(); + dserTree.deserialize(ia, "test"); + long end = System.nanoTime(); + long durationms = (end - start) / 1000000L; + long pernodeus = ((end - start) / 1000L) / count; + + assertEquals(count, dserTree.getNodeCount()); + + LOG.info( + "Deserialized {} nodes in {} ms ({}us/node), depth={} width={} datalen={}", + count, + durationms, + pernodeus, + depth, + width, + len); + } + + @Test + public void testSingleDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(1, 0, 20); + } + + @Test + public void testWideDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(2, 10000, 20); + } + + @Test + public void testDeepDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(400, 1, 20); + } + + @Test + public void test10Wide5DeepDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(5, 10, 20); + } + + @Test + public void test15Wide5DeepDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(5, 15, 20); + } + + @Test + public void test25Wide4DeepDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(4, 25, 20); + } + + @Test + public void test40Wide4DeepDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(4, 40, 20); + } + + @Test + public void test300Wide3DeepDeserialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + deserializeTree(3, 300, 20); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/Emulate353TTLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/Emulate353TTLTest.java new file mode 100644 index 0000000..7967beb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/Emulate353TTLTest.java @@ -0,0 +1,107 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class Emulate353TTLTest extends ClientBase { + + private TestableZooKeeper zk; + + @Override + public void setUp() throws Exception { + System.setProperty(EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY, "true"); + System.setProperty(EphemeralType.TTL_3_5_3_EMULATION_PROPERTY, "true"); + super.setUp(); + zk = createClient(); + } + + @Override + public void tearDown() throws Exception { + System.clearProperty(EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY); + System.clearProperty(EphemeralType.TTL_3_5_3_EMULATION_PROPERTY); + super.tearDown(); + zk.close(); + } + + @Test + public void testCreate() throws KeeperException, InterruptedException { + Stat stat = new Stat(); + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, stat, 100); + assertEquals(0, stat.getEphemeralOwner()); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); + + fakeElapsed.set(1000); + containerManager.checkContainers(); + assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); + } + + @Test + public void test353TTL() throws KeeperException, InterruptedException { + DataTree dataTree = serverFactory.zkServer.getZKDatabase().dataTree; + long ephemeralOwner = EphemeralTypeEmulate353.ttlToEphemeralOwner(100); + dataTree.createNode("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, ephemeralOwner, dataTree.getNode("/").stat.getCversion() + + 1, 1, 1); + + final AtomicLong fakeElapsed = new AtomicLong(0); + ContainerManager containerManager = newContainerManager(fakeElapsed); + containerManager.checkContainers(); + assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false)); + + fakeElapsed.set(1000); + containerManager.checkContainers(); + assertNull("Ttl node should have been deleted", zk.exists("/foo", false)); + } + + @Test + public void testEphemeralOwner_emulationTTL() { + assertThat(EphemeralType.get(-1), equalTo(EphemeralType.TTL)); + } + + @Test + public void testEphemeralOwner_emulationContainer() { + assertThat(EphemeralType.get(EphemeralType.CONTAINER_EPHEMERAL_OWNER), equalTo(EphemeralType.CONTAINER)); + } + + private ContainerManager newContainerManager(final AtomicLong fakeElapsed) { + return new ContainerManager(serverFactory.getZooKeeperServer().getZKDatabase(), serverFactory.getZooKeeperServer().firstProcessor, 1, 100) { + @Override + protected long getElapsed(DataNode node) { + return fakeElapsed.get(); + } + }; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/EphemeralTypeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/EphemeralTypeTest.java new file mode 100644 index 0000000..5021f0f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/EphemeralTypeTest.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class EphemeralTypeTest { + + @Before + public void setUp() { + System.setProperty(EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY, "true"); + } + + @After + public void tearDown() { + System.clearProperty(EphemeralType.EXTENDED_TYPES_ENABLED_PROPERTY); + } + + @Test + public void testTtls() { + long[] ttls = {100, 1, EphemeralType.TTL.maxValue()}; + for (long ttl : ttls) { + long ephemeralOwner = EphemeralType.TTL.toEphemeralOwner(ttl); + assertEquals(EphemeralType.TTL, EphemeralType.get(ephemeralOwner)); + assertEquals(ttl, EphemeralType.TTL.getValue(ephemeralOwner)); + } + + EphemeralType.validateTTL(CreateMode.PERSISTENT_WITH_TTL, 100); + EphemeralType.validateTTL(CreateMode.PERSISTENT_SEQUENTIAL_WITH_TTL, 100); + + try { + EphemeralType.validateTTL(CreateMode.EPHEMERAL, 100); + fail("Should have thrown IllegalArgumentException"); + } catch (IllegalArgumentException dummy) { + // expected + } + } + + @Test + public void testContainerValue() { + assertEquals(Long.MIN_VALUE, EphemeralType.CONTAINER_EPHEMERAL_OWNER); + assertEquals(EphemeralType.CONTAINER, EphemeralType.get(EphemeralType.CONTAINER_EPHEMERAL_OWNER)); + } + + @Test + public void testNonSpecial() { + assertEquals(EphemeralType.VOID, EphemeralType.get(0)); + assertEquals(EphemeralType.NORMAL, EphemeralType.get(1)); + assertEquals(EphemeralType.NORMAL, EphemeralType.get(Long.MAX_VALUE)); + } + + @Test + public void testServerIds() { + for (int i = 0; i <= EphemeralType.MAX_EXTENDED_SERVER_ID; ++i) { + EphemeralType.validateServerId(i); + } + try { + EphemeralType.validateServerId(EphemeralType.MAX_EXTENDED_SERVER_ID + 1); + fail("Should have thrown RuntimeException"); + } catch (RuntimeException e) { + // expected + } + } + + @Test + public void testEphemeralOwner_extendedFeature_TTL() { + // 0xff = Extended feature is ON + // 0x0000 = Extended type id TTL (0) + assertThat(EphemeralType.get(0xff00000000000000L), equalTo(EphemeralType.TTL)); + } + + @Test(expected = IllegalArgumentException.class) + public void testEphemeralOwner_extendedFeature_extendedTypeUnsupported() { + // 0xff = Extended feature is ON + // 0x0001 = Unsupported extended type id (1) + EphemeralType.get(0xff00010000000000L); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/FinalRequestProcessorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/FinalRequestProcessorTest.java new file mode 100644 index 0000000..1d4309f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/FinalRequestProcessorTest.java @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.GetACLRequest; +import org.apache.zookeeper.proto.GetACLResponse; +import org.apache.zookeeper.proto.ReplyHeader; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class FinalRequestProcessorTest { + + private List testACLs = new ArrayList(); + private final Record[] responseRecord = new Record[1]; + private final ReplyHeader[] replyHeaders = new ReplyHeader[1]; + + private ServerCnxn cnxn; + private ByteBuffer bb; + private FinalRequestProcessor processor; + + @Before + public void setUp() throws KeeperException.NoNodeException, IOException { + testACLs.clear(); + testACLs.addAll(Arrays.asList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "user:secrethash")), new ACL(ZooDefs.Perms.ADMIN, new Id("digest", "adminuser:adminsecret")), new ACL(ZooDefs.Perms.READ, new Id("world", "anyone")))); + + ZooKeeperServer zks = new ZooKeeperServer(); + ZKDatabase db = mock(ZKDatabase.class); + String testPath = "/testPath"; + when(db.getNode(eq(testPath))).thenReturn(new DataNode()); + when(db.getACL(eq(testPath), any(Stat.class))).thenReturn(testACLs); + when(db.aclForNode(any(DataNode.class))).thenReturn(testACLs); + zks.setZKDatabase(db); + processor = new FinalRequestProcessor(zks); + + cnxn = mock(ServerCnxn.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) { + replyHeaders[0] = invocationOnMock.getArgument(0); + responseRecord[0] = invocationOnMock.getArgument(1); + return null; + } + }).when(cnxn).sendResponse(any(), any(), anyString()); + + GetACLRequest getACLRequest = new GetACLRequest(); + getACLRequest.setPath(testPath); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + getACLRequest.serialize(boa, "request"); + baos.close(); + bb = ByteBuffer.wrap(baos.toByteArray()); + } + + @Test + public void testACLDigestHashHiding_NoAuth_WorldCanRead() { + // Arrange + + // Act + Request r = new Request(cnxn, 0, 0, ZooDefs.OpCode.getACL, bb, new ArrayList()); + processor.processRequest(r); + + // Assert + assertMasked(true); + } + + @Test + public void testACLDigestHashHiding_NoAuth_NoWorld() { + // Arrange + testACLs.remove(2); + + // Act + Request r = new Request(cnxn, 0, 0, ZooDefs.OpCode.getACL, bb, new ArrayList()); + processor.processRequest(r); + + // Assert + assertThat(KeeperException.Code.get(replyHeaders[0].getErr()), equalTo(KeeperException.Code.NOAUTH)); + } + + @Test + public void testACLDigestHashHiding_UserCanRead() { + // Arrange + List authInfo = new ArrayList(); + authInfo.add(new Id("digest", "otheruser:somesecrethash")); + + // Act + Request r = new Request(cnxn, 0, 0, ZooDefs.OpCode.getACL, bb, authInfo); + processor.processRequest(r); + + // Assert + assertMasked(true); + } + + @Test + public void testACLDigestHashHiding_UserCanAll() { + // Arrange + List authInfo = new ArrayList(); + authInfo.add(new Id("digest", "user:secrethash")); + + // Act + Request r = new Request(cnxn, 0, 0, ZooDefs.OpCode.getACL, bb, authInfo); + processor.processRequest(r); + + // Assert + assertMasked(false); + } + + @Test + public void testACLDigestHashHiding_AdminUser() { + // Arrange + List authInfo = new ArrayList(); + authInfo.add(new Id("digest", "adminuser:adminsecret")); + + // Act + Request r = new Request(cnxn, 0, 0, ZooDefs.OpCode.getACL, bb, authInfo); + processor.processRequest(r); + + // Assert + assertMasked(false); + } + + @Test + public void testACLDigestHashHiding_OnlyAdmin() { + // Arrange + testACLs.clear(); + testACLs.addAll(Arrays.asList(new ACL(ZooDefs.Perms.READ, new Id("digest", "user:secrethash")), new ACL(ZooDefs.Perms.ADMIN, new Id("digest", "adminuser:adminsecret")))); + List authInfo = new ArrayList(); + authInfo.add(new Id("digest", "adminuser:adminsecret")); + + // Act + Request r = new Request(cnxn, 0, 0, ZooDefs.OpCode.getACL, bb, authInfo); + processor.processRequest(r); + + // Assert + assertTrue("Not a GetACL response. Auth failed?", responseRecord[0] instanceof GetACLResponse); + GetACLResponse rsp = (GetACLResponse) responseRecord[0]; + assertThat("Number of ACLs in the response are different", rsp.getAcl().size(), equalTo(2)); + + // Verify ACLs in the response + assertThat("Password hash mismatch in the response", rsp.getAcl().get(0).getId().getId(), equalTo("user:secrethash")); + assertThat("Password hash mismatch in the response", rsp.getAcl().get(1).getId().getId(), equalTo("adminuser:adminsecret")); + } + + private void assertMasked(boolean masked) { + assertTrue("Not a GetACL response. Auth failed?", responseRecord[0] instanceof GetACLResponse); + GetACLResponse rsp = (GetACLResponse) responseRecord[0]; + assertThat("Number of ACLs in the response are different", rsp.getAcl().size(), equalTo(3)); + + // Verify ACLs in the response + assertThat("Invalid ACL list in the response", rsp.getAcl().get(0).getPerms(), equalTo(ZooDefs.Perms.ALL)); + assertThat("Invalid ACL list in the response", rsp.getAcl().get(0).getId().getScheme(), equalTo("digest")); + if (masked) { + assertThat("Password hash is not masked in the response", rsp.getAcl().get(0).getId().getId(), equalTo("user:x")); + } else { + assertThat("Password hash mismatch in the response", rsp.getAcl().get(0).getId().getId(), equalTo("user:secrethash")); + } + + assertThat("Invalid ACL list in the response", rsp.getAcl().get(1).getPerms(), equalTo(ZooDefs.Perms.ADMIN)); + assertThat("Invalid ACL list in the response", rsp.getAcl().get(1).getId().getScheme(), equalTo("digest")); + if (masked) { + assertThat("Password hash is not masked in the response", rsp.getAcl().get(1).getId().getId(), equalTo("adminuser:x")); + } else { + assertThat("Password hash mismatch in the response", rsp.getAcl().get(1).getId().getId(), equalTo("adminuser:adminsecret")); + } + + assertThat("Invalid ACL list in the response", rsp.getAcl().get(2).getPerms(), equalTo(ZooDefs.Perms.READ)); + assertThat("Invalid ACL list in the response", rsp.getAcl().get(2).getId().getScheme(), equalTo("world")); + assertThat("Invalid ACL list in the response", rsp.getAcl().get(2).getId().getId(), equalTo("anyone")); + + // Verify that FinalRequestProcessor hasn't changed the original ACL objects + assertThat("Original ACL list has been modified", testACLs.get(0).getPerms(), equalTo(ZooDefs.Perms.ALL)); + assertThat("Original ACL list has been modified", testACLs.get(0).getId().getScheme(), equalTo("digest")); + assertThat("Original ACL list has been modified", testACLs.get(0).getId().getId(), equalTo("user:secrethash")); + + assertThat("Original ACL list has been modified", testACLs.get(1).getPerms(), equalTo(ZooDefs.Perms.ADMIN)); + assertThat("Original ACL list has been modified", testACLs.get(1).getId().getScheme(), equalTo("digest")); + assertThat("Original ACL list has been modified", testACLs.get(1).getId().getId(), equalTo("adminuser:adminsecret")); + + assertThat("Original ACL list has been modified", testACLs.get(2).getPerms(), equalTo(ZooDefs.Perms.READ)); + assertThat("Original ACL list has been modified", testACLs.get(2).getId().getScheme(), equalTo("world")); + assertThat("Original ACL list has been modified", testACLs.get(2).getId().getId(), equalTo("anyone")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/InvalidSnapCountTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/InvalidSnapCountTest.java new file mode 100644 index 0000000..a7a4aea --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/InvalidSnapCountTest.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test stand-alone server. + * + */ +public class InvalidSnapCountTest extends ZKTestCase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(InvalidSnapCountTest.class); + + public static class MainThread extends Thread { + + final File confFile; + final TestMain main; + + public MainThread(int clientPort) throws IOException { + super("Standalone server with clientPort:" + clientPort); + File tmpDir = ClientBase.createTmpDir(); + confFile = new File(tmpDir, "zoo.cfg"); + + FileWriter fwriter = new FileWriter(confFile); + fwriter.write("tickTime=2000\n"); + fwriter.write("initLimit=10\n"); + fwriter.write("syncLimit=5\n"); + fwriter.write("snapCount=1\n"); + + File dataDir = new File(tmpDir, "data"); + if (!dataDir.mkdir()) { + throw new IOException("unable to mkdir " + dataDir); + } + + // Convert windows path to UNIX to avoid problems with "\" + String dir = PathUtils.normalizeFileSystemPath(dataDir.toString()); + fwriter.write("dataDir=" + dir + "\n"); + + fwriter.write("clientPort=" + clientPort + "\n"); + fwriter.flush(); + fwriter.close(); + + main = new TestMain(); + } + + public void run() { + String[] args = new String[1]; + args[0] = confFile.toString(); + try { + main.initializeAndRun(args); + } catch (Exception e) { + // test will still fail even though we just log/ignore + LOG.error("unexpected exception in run", e); + } + } + + public void shutdown() { + main.shutdown(); + } + + } + + public static class TestMain extends ZooKeeperServerMain { + + public void shutdown() { + super.shutdown(); + } + + } + + /** + * Verify the ability to start a standalone server instance. + */ + @Test + public void testInvalidSnapCount() throws Exception { + + final int CLIENT_PORT = 3181; + + MainThread main = new MainThread(CLIENT_PORT); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + + assertEquals(SyncRequestProcessor.getSnapCount(), 2); + + main.shutdown(); + + } + + public void process(WatchedEvent event) { + // ignore for this test + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/InvalidSnapshotTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/InvalidSnapshotTest.java new file mode 100644 index 0000000..f048c92 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/InvalidSnapshotTest.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.RandomAccessFile; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This test checks that the server works even if the last snapshot is + * invalidated by corruption or if the server crashes while generating the + * snapshot. + */ +public class InvalidSnapshotTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(InvalidSnapshotTest.class); + + public InvalidSnapshotTest() { + SyncRequestProcessor.setSnapCount(100); + } + + /** + * Validate that the server can come up on an invalid snapshot - by + * reverting to a prior snapshot + associated logs. + */ + @Test + public void testInvalidSnapshot() throws Exception { + ZooKeeper zk = createClient(); + try { + for (int i = 0; i < 2000; i++) { + zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + NIOServerCnxnFactory factory = (NIOServerCnxnFactory) serverFactory; + stopServer(); + + // now corrupt the snapshot + File snapFile = factory.zkServer.getTxnLogFactory().findMostRecentSnapshot(); + LOG.info("Corrupting {}", snapFile); + RandomAccessFile raf = new RandomAccessFile(snapFile, "rws"); + raf.setLength(3); + raf.close(); + + // now restart the server + startServer(); + + // verify that the expected data exists and wasn't lost + zk = createClient(); + try { + assertTrue("the node should exist", (zk.exists("/invalidsnap-1999", false) != null)); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockNIOServerCnxn.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockNIOServerCnxn.java new file mode 100644 index 0000000..7fc1a16 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockNIOServerCnxn.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.nio.channels.SelectionKey; +import java.nio.channels.SocketChannel; +import org.apache.zookeeper.server.NIOServerCnxnFactory.SelectorThread; + +public class MockNIOServerCnxn extends NIOServerCnxn { + + public MockNIOServerCnxn( + ZooKeeperServer zk, SocketChannel sock, SelectionKey sk, NIOServerCnxnFactory factory, SelectorThread selectorThread) throws IOException { + super(zk, sock, sk, factory, selectorThread); + } + + /** + * Handles read/write IO on connection. + */ + public void doIO(SelectionKey k) throws InterruptedException { + super.doIO(k); + } + + @Override + protected boolean isSocketOpen() { + return true; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockSelectorThread.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockSelectorThread.java new file mode 100644 index 0000000..2871932 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockSelectorThread.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.nio.channels.SelectionKey; + +public class MockSelectorThread extends NIOServerCnxnFactory.SelectorThread { + + public MockSelectorThread(NIOServerCnxnFactory fact) throws IOException { + fact.super(0); + } + + public boolean addInterestOpsUpdateRequest(SelectionKey sk) { + return super.addInterestOpsUpdateRequest(sk); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockServerCnxn.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockServerCnxn.java new file mode 100644 index 0000000..a82eab3 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MockServerCnxn.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.security.cert.Certificate; +import org.apache.jute.Record; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.ReplyHeader; + +public class MockServerCnxn extends ServerCnxn { + + public Certificate[] clientChain; + public boolean secure; + + public MockServerCnxn() { + super(null); + } + + @Override + int getSessionTimeout() { + return 0; + } + + @Override + public void close(DisconnectReason reason) { + } + + @Override + public void sendResponse(ReplyHeader h, Record r, String tag, + String cacheKey, Stat stat, int opCode) throws IOException { + } + + @Override + public void sendCloseSession() { + } + + @Override + public void process(WatchedEvent event) { + } + + @Override + public long getSessionId() { + return 0; + } + + @Override + void setSessionId(long sessionId) { + } + + @Override + public boolean isSecure() { + return secure; + } + + @Override + public Certificate[] getClientCertificateChain() { + return clientChain; + } + + @Override + public void setClientCertificateChain(Certificate[] chain) { + clientChain = chain; + } + + @Override + void sendBuffer(ByteBuffer... closeConn) { + } + + @Override + void enableRecv() { + } + + @Override + void disableRecv(boolean waitDisableRecv) { + } + + @Override + void setSessionTimeout(int sessionTimeout) { + } + + @Override + protected ServerStats serverStats() { + return null; + } + + @Override + public long getOutstandingRequests() { + return 0; + } + + @Override + public InetSocketAddress getRemoteSocketAddress() { + return null; + } + + @Override + public int getInterestOps() { + return 0; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/MultiOpSessionUpgradeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MultiOpSessionUpgradeTest.java new file mode 100644 index 0000000..6e35d2c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/MultiOpSessionUpgradeTest.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumZooKeeperServer; +import org.apache.zookeeper.server.quorum.UpgradeableSessionTracker; +import org.apache.zookeeper.test.QuorumBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MultiOpSessionUpgradeTest extends QuorumBase { + + protected static final Logger LOG = LoggerFactory.getLogger(MultiOpSessionUpgradeTest.class); + + @Override + public void setUp() throws Exception { + localSessionsEnabled = true; + localSessionsUpgradingEnabled = true; + super.setUp(); + } + + @Test + public void ephemeralCreateMultiOpTest() throws KeeperException, InterruptedException, IOException { + final ZooKeeper zk = createClient(); + + String data = "test"; + String path = "/ephemeralcreatemultiop"; + zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId()); + assertNotNull("unable to find server interlocutor", server); + UpgradeableSessionTracker sessionTracker = (UpgradeableSessionTracker) server.getSessionTracker(); + assertFalse("session already global", sessionTracker.isGlobalSession(zk.getSessionId())); + + List multi = null; + try { + multi = zk.multi(Arrays.asList( + Op.setData(path, data.getBytes(), 0), + Op.create(path + "/e", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL), + Op.create(path + "/p", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create(path + "/q", data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL))); + } catch (KeeperException.SessionExpiredException e) { + // the scenario that inspired this unit test + fail("received session expired for a session promotion in a multi-op"); + } + + assertNotNull(multi); + assertEquals(4, multi.size()); + assertEquals(data, new String(zk.getData(path + "/e", false, null))); + assertEquals(data, new String(zk.getData(path + "/p", false, null))); + assertEquals(data, new String(zk.getData(path + "/q", false, null))); + assertTrue("session not promoted", sessionTracker.isGlobalSession(zk.getSessionId())); + } + + @Test + public void directCheckUpgradeSessionTest() throws IOException, InterruptedException, KeeperException { + final ZooKeeper zk = createClient(); + + String path = "/directcheckupgradesession"; + zk.create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + QuorumZooKeeperServer server = getConnectedServer(zk.getSessionId()); + assertNotNull("unable to find server interlocutor", server); + + Request readRequest = makeGetDataRequest(path, zk.getSessionId()); + Request createRequest = makeCreateRequest(path + "/e", zk.getSessionId()); + assertNull("tried to upgrade on a read", server.checkUpgradeSession(readRequest)); + assertNotNull("failed to upgrade on a create", server.checkUpgradeSession(createRequest)); + assertNull("tried to upgrade after successful promotion", server.checkUpgradeSession(createRequest)); + } + + private Request makeGetDataRequest(String path, long sessionId) throws IOException { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); + GetDataRequest getDataRequest = new GetDataRequest(path, false); + getDataRequest.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); + return new Request(null, sessionId, 1, ZooDefs.OpCode.getData, bb, new ArrayList()); + } + + private Request makeCreateRequest(String path, long sessionId) throws IOException { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); + CreateRequest createRequest = new CreateRequest(path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag()); + createRequest.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); + return new Request(null, sessionId, 1, ZooDefs.OpCode.create2, bb, new ArrayList()); + } + + private QuorumZooKeeperServer getConnectedServer(long sessionId) { + for (QuorumPeer peer : getPeerList()) { + if (peer.getActiveServer().getSessionTracker().isTrackingSession(sessionId)) { + return (QuorumZooKeeperServer) peer.getActiveServer(); + } + } + return null; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/NIOServerCnxnFactoryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NIOServerCnxnFactoryTest.java new file mode 100644 index 0000000..715c15a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NIOServerCnxnFactoryTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.SocketException; +import org.apache.zookeeper.PortAssignment; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class NIOServerCnxnFactoryTest { + + private InetSocketAddress listenAddress; + private NIOServerCnxnFactory factory; + + @Before + public void setUp() throws IOException { + listenAddress = new InetSocketAddress(PortAssignment.unique()); + factory = new NIOServerCnxnFactory(); + factory.configure(listenAddress, 100); + } + + @After + public void tearDown() { + if (factory != null) { + factory.shutdown(); + } + } + + @Test(expected = SocketException.class) + public void testStartupWithoutStart_SocketAlreadyBound() throws IOException { + ServerSocket ss = new ServerSocket(listenAddress.getPort()); + } + + @Test(expected = SocketException.class) + public void testStartupWithStart_SocketAlreadyBound() throws IOException { + factory.start(); + ServerSocket ss = new ServerSocket(listenAddress.getPort()); + } + + @Test + public void testShutdownWithoutStart_SocketReleased() throws IOException { + factory.shutdown(); + factory = null; + + ServerSocket ss = new ServerSocket(listenAddress.getPort()); + ss.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/NIOServerCnxnTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NIOServerCnxnTest.java new file mode 100644 index 0000000..fc917e0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NIOServerCnxnTest.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.BufferStats; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NIOServerCnxnTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(NIOServerCnxnTest.class); + + /** + * Test operations on ServerCnxn after socket closure. + */ + @Test(timeout = 60000) + public void testOperationsAfterCnxnClose() throws IOException, InterruptedException, KeeperException { + final ZooKeeper zk = createClient(); + + final String path = "/a"; + try { + // make sure zkclient works + zk.create(path, "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull("Didn't create znode:" + path, zk.exists(path, false)); + // Defaults ServerCnxnFactory would be instantiated with + // NIOServerCnxnFactory + assertTrue("Didn't instantiate ServerCnxnFactory with NIOServerCnxnFactory!", serverFactory instanceof NIOServerCnxnFactory); + Iterable connections = serverFactory.getConnections(); + for (ServerCnxn serverCnxn : connections) { + serverCnxn.close(ServerCnxn.DisconnectReason.CHANNEL_CLOSED_EXCEPTION); + try { + serverCnxn.toString(); + } catch (Exception e) { + LOG.error("Exception while getting connection details!", e); + fail("Shouldn't throw exception while " + "getting connection details!"); + } + } + } finally { + zk.close(); + } + + } + + @Test + public void testClientResponseStatsUpdate() throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient()) { + BufferStats clientResponseStats = serverFactory.getZooKeeperServer().serverStats().getClientResponseStats(); + assertThat("Last client response size should be initialized with INIT_VALUE", clientResponseStats.getLastBufferSize(), equalTo(BufferStats.INIT_VALUE)); + + zk.create("/a", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertThat("Last client response size should be greater then zero after client request was performed", clientResponseStats.getLastBufferSize(), greaterThan(0)); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnFactoryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnFactoryTest.java new file mode 100644 index 0000000..76136c4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnFactoryTest.java @@ -0,0 +1,236 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.net.InetSocketAddress; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.server.metric.SimpleCounter; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.SSLAuthTest; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class NettyServerCnxnFactoryTest extends ClientBase { + + private static final Logger LOG = LoggerFactory + .getLogger(NettyServerCnxnFactoryTest.class); + + ClientX509Util x509Util; + final LinkedBlockingQueue zooKeeperClients = new LinkedBlockingQueue<>(); + + + @Override + public void setUp() throws Exception { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, + "org.apache.zookeeper.server.NettyServerCnxnFactory"); + + // by default, we don't start any ZooKeeper server, as not all the tests are needing it. + } + + @Override + public void tearDown() throws Exception { + + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + if (x509Util != null) { + SSLAuthTest.clearSecureSetting(x509Util); + } + for (ZooKeeper zk : zooKeeperClients) { + zk.close(); + } + + //stopping the server only if it was started + if (serverFactory != null) { + super.tearDown(); + } + } + + @Test + public void testRebind() throws Exception { + InetSocketAddress addr = new InetSocketAddress(PortAssignment.unique()); + NettyServerCnxnFactory factory = new NettyServerCnxnFactory(); + factory.configure(addr, 100, -1, false); + factory.start(); + Assert.assertTrue(factory.getParentChannel().isActive()); + + factory.reconfigure(addr); + + // wait the state change + Thread.sleep(100); + + Assert.assertTrue(factory.getParentChannel().isActive()); + } + + @Test + public void testRebindIPv4IPv6() throws Exception { + int randomPort = PortAssignment.unique(); + InetSocketAddress addr = new InetSocketAddress("0.0.0.0", randomPort); + NettyServerCnxnFactory factory = new NettyServerCnxnFactory(); + factory.configure(addr, 100, -1, false); + factory.start(); + Assert.assertTrue(factory.getParentChannel().isActive()); + + factory.reconfigure(new InetSocketAddress("[0:0:0:0:0:0:0:0]", randomPort)); + + // wait the state change + Thread.sleep(100); + + Assert.assertTrue(factory.getParentChannel().isActive()); + } + + /* + * In this test we are flooding the server with SSL connections, and expecting that not + * all the connection will succeed at once. Some of the connections should be closed, + * as there is a maximum number of parallel SSL handshake the server is willing to do + * for security reasons. + */ + @Test + public void testOutstandingHandshakeLimit() throws Exception { + + // setting up SSL params, but disable some debug logs + x509Util = SSLAuthTest.setUpSecure(); + System.clearProperty("javax.net.debug"); + + // starting a single server (it will be closed in the tearDown) + setUpWithServerId(1); + + // initializing the statistics + SimpleCounter tlsHandshakeExceeded = (SimpleCounter) ServerMetrics.getMetrics().TLS_HANDSHAKE_EXCEEDED; + tlsHandshakeExceeded.reset(); + Assert.assertEquals(tlsHandshakeExceeded.get(), 0); + + // setting the HandshakeLimit to 3, so only 3 SSL handshakes can happen in parallel + NettyServerCnxnFactory factory = (NettyServerCnxnFactory) serverFactory; + factory.setSecure(true); + factory.setOutstandingHandshakeLimit(3); + + // starting the threads that will try to connect to the server + // we will have 3 threads, each of them establishing 3 connections + int threadNum = 3; + int cnxnPerThread = 3; + int cnxnLimit = threadNum * cnxnPerThread; + AtomicInteger cnxnCreated = new AtomicInteger(0); + CountDownLatch latch = new CountDownLatch(1); + Thread[] cnxnWorker = new Thread[threadNum]; + for (int i = 0; i < cnxnWorker.length; i++) { + cnxnWorker[i] = new ClientConnectionGenerator(i, cnxnPerThread, cnxnCreated, cnxnLimit, latch, zooKeeperClients); + cnxnWorker[i].start(); + } + + // we might need to wait potentially for a longer time for all the connection to get established, + // as the ZooKeeper Server will close some of the connections and the clients will have to re-try + boolean allConnectionsCreatedInTime = latch.await(30, TimeUnit.SECONDS); + int actualConnections = cnxnCreated.get(); + LOG.info("created {} connections", actualConnections); + if (!allConnectionsCreatedInTime) { + Assert.fail(String.format("Only %d out of %d connections created!", actualConnections, cnxnLimit)); + } + + // Assert the server refused some of the connections because the handshake limit was reached + // (throttling should be greater than 0) + long handshakeThrottledNum = tlsHandshakeExceeded.get(); + LOG.info("TLS_HANDSHAKE_EXCEEDED: {}", handshakeThrottledNum); + Assert.assertThat("The number of handshake throttled should be " + + "greater than 0", handshakeThrottledNum, Matchers.greaterThan(0L)); + + // Assert there is no outstanding handshake anymore, all the clients connected in the end + int outstandingHandshakeNum = factory.getOutstandingHandshakeNum(); + LOG.info("outstanding handshake is {}", outstandingHandshakeNum); + Assert.assertThat("The outstanding handshake number should be 0 " + + "after all cnxns established", outstandingHandshakeNum, Matchers.is(0)); + } + + + private final class ClientConnectionWatcher implements Watcher { + + private final AtomicInteger cnxnCreated; + private final int cnxnLimit; + private final int cnxnThreadId; + private final int cnxnId; + private final CountDownLatch latch; + + public ClientConnectionWatcher(AtomicInteger cnxnCreated, int cnxnLimit, int cnxnThreadId, + int cnxnId, CountDownLatch latch) { + this.cnxnCreated = cnxnCreated; + this.cnxnLimit = cnxnLimit; + this.cnxnThreadId = cnxnThreadId; + this.cnxnId = cnxnId; + this.latch = latch; + } + + @Override + public void process(WatchedEvent event) { + LOG.info(String.format("WATCHER [thread: %d, cnx:%d] - new event: %s", cnxnThreadId, cnxnId, event.toString())); + if (event.getState() == Event.KeeperState.SyncConnected) { + int created = cnxnCreated.addAndGet(1); + if (created == cnxnLimit) { + latch.countDown(); + } + } + } + } + + + private final class ClientConnectionGenerator extends Thread { + + private final int cnxnThreadId; + private final int cnxnPerThread; + private final AtomicInteger cnxnCreated; + private final int cnxnLimit; + private final CountDownLatch latch; + private final LinkedBlockingQueue zks; + + private ClientConnectionGenerator(int cnxnThreadId, int cnxnPerThread, + AtomicInteger cnxnCreated, int cnxnLimit, + CountDownLatch latch, + LinkedBlockingQueue zks) { + this.cnxnThreadId = cnxnThreadId; + this.cnxnPerThread = cnxnPerThread; + this.cnxnCreated = cnxnCreated; + this.cnxnLimit = cnxnLimit; + this.latch = latch; + this.zks = zks; + } + + @Override + public void run() { + + for (int j = 0; j < cnxnPerThread; j++) { + try { + zks.add(new ZooKeeper(hostPort, 30000, + new ClientConnectionWatcher(cnxnCreated, cnxnLimit, cnxnThreadId, j, latch))); + } catch (Exception e) { + LOG.info("Error while creating zk client", e); + } + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnTest.java new file mode 100644 index 0000000..0d98e21 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnTest.java @@ -0,0 +1,387 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelId; +import io.netty.channel.ChannelPipeline; +import io.netty.util.Attribute; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.ProtocolException; +import java.nio.charset.StandardCharsets; +import java.util.Random; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.BufferStats; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.SSLAuthTest; +import org.apache.zookeeper.test.TestByteBufAllocator; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test verifies the behavior of NettyServerCnxn which represents a connection + * from a client to the server. + */ +public class NettyServerCnxnTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(NettyServerCnxnTest.class); + + @Override + public void setUp() throws Exception { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + NettyServerCnxnFactory.setTestAllocator(TestByteBufAllocator.getInstance()); + super.maxCnxns = 1; + super.exceptionOnFailedConnect = true; + super.setUp(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + NettyServerCnxnFactory.clearTestAllocator(); + TestByteBufAllocator.checkForLeaks(); + } + + /** + * Test verifies the channel closure - while closing the channel + * servercnxnfactory should remove all channel references to avoid + * duplicate channel closure. Duplicate closure may result in indefinite + * hanging due to netty open issue. + * + * @see NETTY-412 + */ + @Test(timeout = 40000) + public void testSendCloseSession() throws Exception { + assertTrue("Didn't instantiate ServerCnxnFactory with NettyServerCnxnFactory!", serverFactory instanceof NettyServerCnxnFactory); + + final ZooKeeper zk = createClient(); + final ZooKeeperServer zkServer = serverFactory.getZooKeeperServer(); + final String path = "/a"; + try { + // make sure zkclient works + zk.create(path, "test".getBytes(StandardCharsets.UTF_8), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + // set on watch + assertNotNull("Didn't create znode:" + path, zk.exists(path, true)); + assertEquals(1, zkServer.getZKDatabase().getDataTree().getWatchCount()); + Iterable connections = serverFactory.getConnections(); + assertEquals("Mismatch in number of live connections!", 1, serverFactory.getNumAliveConnections()); + for (ServerCnxn serverCnxn : connections) { + serverCnxn.sendCloseSession(); + } + LOG.info("Waiting for the channel disconnected event"); + int timeout = 0; + while (serverFactory.getNumAliveConnections() != 0) { + Thread.sleep(1000); + timeout += 1000; + if (timeout > CONNECTION_TIMEOUT) { + fail("The number of live connections should be 0"); + } + } + // make sure the watch is removed when the connection closed + assertEquals(0, zkServer.getZKDatabase().getDataTree().getWatchCount()); + } finally { + zk.close(); + } + } + + /** + * In the {@link #setUp()} routine, the maximum number of connections per IP + * is set to 1. This tests that if more than one connection is attempted, the + * connection fails. + */ + @Test(timeout = 40000, expected = ProtocolException.class) + public void testMaxConnectionPerIpSurpased() throws Exception { + assertTrue("Did not instantiate ServerCnxnFactory with NettyServerCnxnFactory!", serverFactory instanceof NettyServerCnxnFactory); + + try (final ZooKeeper zk1 = createClient(); final ZooKeeper zk2 = createClient()) { + } + } + + @Test + public void testClientResponseStatsUpdate() throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient()) { + BufferStats clientResponseStats = serverFactory.getZooKeeperServer().serverStats().getClientResponseStats(); + assertThat("Last client response size should be initialized with INIT_VALUE", clientResponseStats.getLastBufferSize(), equalTo(BufferStats.INIT_VALUE)); + + zk.create("/a", "test".getBytes(StandardCharsets.UTF_8), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertThat("Last client response size should be greater than 0 after client request was performed", clientResponseStats.getLastBufferSize(), greaterThan(0)); + + byte[] contents = zk.getData("/a", null, null); + assertArrayEquals("unexpected data", "test".getBytes(StandardCharsets.UTF_8), contents); + } + } + + @Test + public void testNonMTLSLocalConn() throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient()) { + ServerStats serverStats = serverFactory.getZooKeeperServer().serverStats(); + //2 for local stat connection and this client + assertEquals(2, serverStats.getNonMTLSLocalConnCount()); + assertEquals(0, serverStats.getNonMTLSRemoteConnCount()); + } + } + + @SuppressWarnings("unchecked") + @Test + public void testNonMTLSRemoteConn() throws Exception { + Channel channel = mock(Channel.class); + ChannelId id = mock(ChannelId.class); + ChannelFuture success = mock(ChannelFuture.class); + ChannelHandlerContext context = mock(ChannelHandlerContext.class); + ChannelPipeline channelPipeline = mock(ChannelPipeline.class); + + when(context.channel()).thenReturn(channel); + when(channel.pipeline()).thenReturn(channelPipeline); + when(success.channel()).thenReturn(channel); + when(channel.closeFuture()).thenReturn(success); + + InetSocketAddress address = new InetSocketAddress(0); + when(channel.remoteAddress()).thenReturn(address); + when(channel.id()).thenReturn(id); + NettyServerCnxnFactory factory = new NettyServerCnxnFactory(); + LeaderZooKeeperServer zks = mock(LeaderZooKeeperServer.class); + factory.setZooKeeperServer(zks); + Attribute atr = mock(Attribute.class); + Mockito.doReturn(atr).when(channel).attr( + Mockito.any() + ); + doNothing().when(atr).set(Mockito.any()); + + ServerStats.Provider providerMock = mock(ServerStats.Provider.class); + when(zks.serverStats()).thenReturn(new ServerStats(providerMock)); + + factory.channelHandler.channelActive(context); + + assertEquals(0, zks.serverStats().getNonMTLSLocalConnCount()); + assertEquals(1, zks.serverStats().getNonMTLSRemoteConnCount()); + } + + @Test + public void testServerSideThrottling() throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient()) { + BufferStats clientResponseStats = serverFactory.getZooKeeperServer().serverStats().getClientResponseStats(); + assertThat("Last client response size should be initialized with INIT_VALUE", clientResponseStats.getLastBufferSize(), equalTo(BufferStats.INIT_VALUE)); + + zk.create("/a", "test".getBytes(StandardCharsets.UTF_8), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertThat("Last client response size should be greater than 0 after client request was performed", clientResponseStats.getLastBufferSize(), greaterThan(0)); + + for (final ServerCnxn cnxn : serverFactory.cnxns) { + final NettyServerCnxn nettyCnxn = ((NettyServerCnxn) cnxn); + // Disable receiving data for all open connections ... + nettyCnxn.disableRecv(); + // ... then force a throttled read after 1 second (this puts the read into queuedBuffer) ... + nettyCnxn.getChannel().eventLoop().schedule(new Runnable() { + @Override + public void run() { + nettyCnxn.getChannel().read(); + } + }, 1, TimeUnit.SECONDS); + + // ... and finally disable throttling after 2 seconds. + nettyCnxn.getChannel().eventLoop().schedule(new Runnable() { + @Override + public void run() { + nettyCnxn.enableRecv(); + } + }, 2, TimeUnit.SECONDS); + } + + byte[] contents = zk.getData("/a", null, null); + assertArrayEquals("unexpected data", "test".getBytes(StandardCharsets.UTF_8), contents); + + // As above, but don't do the throttled read. Make the request bytes wait in the socket + // input buffer until after throttling is turned off. Need to make sure both modes work. + for (final ServerCnxn cnxn : serverFactory.cnxns) { + final NettyServerCnxn nettyCnxn = ((NettyServerCnxn) cnxn); + // Disable receiving data for all open connections ... + nettyCnxn.disableRecv(); + // ... then disable throttling after 2 seconds. + nettyCnxn.getChannel().eventLoop().schedule(new Runnable() { + @Override + public void run() { + nettyCnxn.enableRecv(); + } + }, 2, TimeUnit.SECONDS); + } + + contents = zk.getData("/a", null, null); + assertArrayEquals("unexpected data", "test".getBytes(StandardCharsets.UTF_8), contents); + } + } + + @Test + public void testEnableDisableThrottling_secure_random() throws Exception { + runEnableDisableThrottling(true, true); + } + + @Test + public void testEnableDisableThrottling_secure_sequentially() throws Exception { + runEnableDisableThrottling(true, false); + } + + @Test + public void testEnableDisableThrottling_nonSecure_random() throws Exception { + runEnableDisableThrottling(false, true); + } + + @Test + public void testEnableDisableThrottling_nonSecure_sequentially() throws Exception { + runEnableDisableThrottling(false, false); + } + + private void runEnableDisableThrottling(boolean secure, boolean randomDisableEnable) throws Exception { + ClientX509Util x509Util = null; + if (secure) { + x509Util = SSLAuthTest.setUpSecure(); + } + try { + NettyServerCnxnFactory factory = (NettyServerCnxnFactory) serverFactory; + factory.setAdvancedFlowControlEnabled(true); + if (secure) { + factory.setSecure(true); + } + + final String path = "/testEnableDisableThrottling"; + try (ZooKeeper zk = createClient()) { + zk.create(path, new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // meanwhile start another thread to enable and disable recv + AtomicBoolean stopped = new AtomicBoolean(false); + Random random = new Random(); + + Thread enableDisableThread = null; + if (randomDisableEnable) { + enableDisableThread = new Thread() { + @Override + public void run() { + while (!stopped.get()) { + for (final ServerCnxn cnxn : serverFactory.cnxns) { + boolean shouldDisableEnable = random.nextBoolean(); + if (shouldDisableEnable) { + cnxn.disableRecv(); + } else { + cnxn.enableRecv(); + } + } + try { + Thread.sleep(10); + } catch (InterruptedException e) { /* ignore */ } + } + // always enable the recv at end + for (final ServerCnxn cnxn : serverFactory.cnxns) { + cnxn.enableRecv(); + } + } + }; + } else { + enableDisableThread = new Thread() { + @Override + public void run() { + while (!stopped.get()) { + for (final ServerCnxn cnxn : serverFactory.cnxns) { + try { + cnxn.disableRecv(); + Thread.sleep(10); + cnxn.enableRecv(); + Thread.sleep(10); + } catch (InterruptedException e) { /* ignore */ } + } + } + } + }; + } + enableDisableThread.start(); + LOG.info("started thread to enable and disable recv"); + + // start a thread to keep sending requests + int totalRequestsNum = 100000; + AtomicInteger successResponse = new AtomicInteger(); + CountDownLatch responseReceivedLatch = new CountDownLatch(totalRequestsNum); + Thread clientThread = new Thread() { + @Override + public void run() { + int requestIssued = 0; + while (requestIssued++ < totalRequestsNum) { + zk.getData(path, null, new DataCallback() { + @Override + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + if (rc == 0) { + successResponse.addAndGet(1); + } else { + LOG.info("failed response is {}", rc); + } + responseReceivedLatch.countDown(); + } + }, null); + } + } + }; + clientThread.start(); + LOG.info("started thread to issue {} async requests", totalRequestsNum); + + // and verify the response received is same as what we issued + assertTrue(responseReceivedLatch.await(60, TimeUnit.SECONDS)); + LOG.info("received all {} responses", totalRequestsNum); + + stopped.set(true); + enableDisableThread.join(); + LOG.info("enable and disable recv thread exited"); + + // wait another second for the left requests to finish + LOG.info("waiting another 1s for the requests to go through"); + Thread.sleep(1000); + assertEquals(successResponse.get(), totalRequestsNum); + } + } finally { + if (secure) { + SSLAuthTest.clearSecureSetting(x509Util); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/NodeHashMapImplTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NodeHashMapImplTest.java new file mode 100644 index 0000000..b34dc90 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/NodeHashMapImplTest.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.data.StatPersisted; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class NodeHashMapImplTest extends ZKTestCase { + + @Before + public void setUp() { + ZooKeeperServer.setDigestEnabled(true); + } + + @After + public void tearDown() { + ZooKeeperServer.setDigestEnabled(false); + } + + /** + * Test all the operations supported in NodeHashMapImpl. + */ + @Test + public void testOperations() { + NodeHashMapImpl nodes = new NodeHashMapImpl(new DigestCalculator()); + + assertEquals(0, nodes.size()); + assertEquals(0L, nodes.getDigest()); + + // add a new node + String p1 = "p1"; + DataNode n1 = new DataNode(p1.getBytes(), 0L, new StatPersisted()); + nodes.put(p1, n1); + + assertEquals(n1, nodes.get(p1)); + assertNotEquals(0L, nodes.getDigest()); + assertEquals(1, nodes.size()); + + // put another node + String p2 = "p2"; + nodes.put(p2, new DataNode(p2.getBytes(), 0L, new StatPersisted())); + + Set> entries = nodes.entrySet(); + assertEquals(2, entries.size()); + + // remove a node + nodes.remove(p1); + assertEquals(1, nodes.size()); + + nodes.remove(p2); + assertEquals(0, nodes.size()); + assertEquals(0L, nodes.getDigest()); + + // test preChange and postChange + String p3 = "p3"; + DataNode n3 = new DataNode(p3.getBytes(), 0L, new StatPersisted()); + nodes.put(p3, n3); + long preChangeDigest = nodes.getDigest(); + assertNotEquals(0L, preChangeDigest); + + nodes.preChange(p3, n3); + assertEquals(0L, nodes.getDigest()); + + n3.stat.setMzxid(1); + n3.stat.setMtime(1); + n3.stat.setVersion(1); + nodes.postChange(p3, n3); + + long postChangeDigest = nodes.getDigest(); + assertNotEquals(0, postChangeDigest); + assertNotEquals(preChangeDigest, postChangeDigest); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/PrepRequestProcessorMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/PrepRequestProcessorMetricsTest.java new file mode 100644 index 0000000..77dc6c7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/PrepRequestProcessorMetricsTest.java @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.StatPersisted; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.proto.DeleteRequest; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PrepRequestProcessorMetricsTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(PrepRequestProcessorMetricsTest.class); + + ZooKeeperServer zks; + RequestProcessor nextProcessor; + + @Before + public void setup() { + System.setProperty(ZooKeeperServer.SKIP_ACL, "true"); + zks = spy(new ZooKeeperServer()); + zks.sessionTracker = mock(SessionTracker.class); + + ZKDatabase db = mock(ZKDatabase.class); + when(zks.getZKDatabase()).thenReturn(db); + + DataNode node = new DataNode(new byte[1], null, mock(StatPersisted.class)); + when(db.getNode(anyString())).thenReturn(node); + + DataTree dataTree = mock(DataTree.class); + when(db.getDataTree()).thenReturn(dataTree); + + Set ephemerals = new HashSet<>(); + ephemerals.add("/crystalmountain"); + ephemerals.add("/stevenspass"); + when(db.getEphemerals(anyLong())).thenReturn(ephemerals); + + nextProcessor = mock(RequestProcessor.class); + ServerMetrics.getMetrics().resetAll(); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(ZooKeeperServer.SKIP_ACL); + } + + private Request createRequest(Record record, int opCode) throws IOException { + // encoding + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + record.serialize(boa, "request"); + baos.close(); + return new Request(null, 1L, 0, opCode, ByteBuffer.wrap(baos.toByteArray()), null); + } + + private Request createRequest(String path, int opCode) throws IOException { + Record record; + switch (opCode) { + case ZooDefs.OpCode.setData: + record = new SetDataRequest(path, new byte[0], -1); + break; + case ZooDefs.OpCode.delete: + record = new DeleteRequest(path, -1); + break; + default: + record = new DeleteRequest(path, -1); + break; + } + + return createRequest(record, opCode); + } + + private Request createRequest(long sessionId, int opCode) { + return new Request(null, sessionId, 0, opCode, null, null); + } + + @Test + public void testPrepRequestProcessorMetrics() throws Exception { + CountDownLatch threeRequests = new CountDownLatch(3); + doAnswer(invocationOnMock -> { + threeRequests.countDown(); + return null; + }).when(nextProcessor).processRequest(any(Request.class)); + + PrepRequestProcessor prepRequestProcessor = new PrepRequestProcessor(zks, nextProcessor); + + //setData will generate one change + prepRequestProcessor.processRequest(createRequest("/foo", ZooDefs.OpCode.setData)); + //delete will generate two changes, one for itself, one for its parent + prepRequestProcessor.processRequest(createRequest("/foo/bar", ZooDefs.OpCode.delete)); + //mocking two ephemeral nodes exists for this session so two changes + prepRequestProcessor.processRequest(createRequest(2, ZooDefs.OpCode.closeSession)); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(3L, values.get("prep_processor_request_queued")); + + // the sleep is just to make sure the requests will stay in the queue for some time + Thread.sleep(20); + prepRequestProcessor.start(); + + threeRequests.await(500, TimeUnit.MILLISECONDS); + + values = MetricsUtils.currentServerMetrics(); + assertEquals(3L, values.get("max_prep_processor_queue_size")); + + assertThat((long) values.get("min_prep_processor_queue_time_ms"), greaterThan(20L)); + assertEquals(3L, values.get("cnt_prep_processor_queue_time_ms")); + + assertEquals(3L, values.get("cnt_prep_process_time")); + assertThat((long) values.get("max_prep_process_time"), greaterThan(0L)); + + assertEquals(1L, values.get("cnt_close_session_prep_time")); + assertThat((long) values.get("max_close_session_prep_time"), greaterThanOrEqualTo(0L)); + + // With digest feature, we have two more OUTSTANDING_CHANGES_QUEUED than w/o digest + // The expected should 5 in open source until we upstream the digest feature + assertEquals(7L, values.get("outstanding_changes_queued")); + } + + private class SimpleWatcher implements Watcher { + + CountDownLatch created; + public SimpleWatcher(CountDownLatch latch) { + this.created = latch; + } + @Override + public void process(WatchedEvent e) { + created.countDown(); + } + + } + + @Test + public void testOutstandingChangesRemoved() throws Exception { + // this metric is currently recorded in FinalRequestProcessor but it is tightly related to the Prep metrics + QuorumUtil util = new QuorumUtil(1); + util.startAll(); + + ServerMetrics.getMetrics().resetAll(); + + ZooKeeper zk = ClientBase.createZKClient(util.getConnString()); + zk.create("/test", new byte[50], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + CountDownLatch created = new CountDownLatch(1); + zk.exists("/test", new SimpleWatcher(created)); + created.await(200, TimeUnit.MILLISECONDS); + + Map values = MetricsUtils.currentServerMetrics(); + assertThat((long) values.get("outstanding_changes_removed"), greaterThan(0L)); + + util.shutdownAll(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/PrepRequestProcessorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/PrepRequestProcessorTest.java new file mode 100644 index 0000000..409e590 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/PrepRequestProcessorTest.java @@ -0,0 +1,399 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.KeeperException.SessionMovedException; +import org.apache.zookeeper.MultiOperationRecord; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.ReconfigRequest; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.server.ZooKeeperServer.ChangeRecord; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.LeaderBeanTest; +import org.apache.zookeeper.server.quorum.LeaderZooKeeperServer; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.ErrorTxn; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class PrepRequestProcessorTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(PrepRequestProcessorTest.class); + private static final int CONNECTION_TIMEOUT = 3000; + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private CountDownLatch pLatch; + + private ZooKeeperServer zks; + private ServerCnxnFactory servcnxnf; + private PrepRequestProcessor processor; + private Request outcome; + + private boolean isReconfigEnabledPreviously; + private boolean isStandaloneEnabledPreviously; + + @Before + public void setup() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(100); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + + servcnxnf = ServerCnxnFactory.createFactory(PORT, -1); + servcnxnf.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zks.sessionTracker = new MySessionTracker(); + + isReconfigEnabledPreviously = QuorumPeerConfig.isReconfigEnabled(); + isStandaloneEnabledPreviously = QuorumPeerConfig.isStandaloneEnabled(); + } + + @After + public void teardown() throws Exception { + if (servcnxnf != null) { + servcnxnf.shutdown(); + } + if (zks != null) { + zks.shutdown(); + } + + // reset the reconfig option + QuorumPeerConfig.setReconfigEnabled(isReconfigEnabledPreviously); + QuorumPeerConfig.setStandaloneEnabled(isStandaloneEnabledPreviously); + } + + @Test + public void testPRequest() throws Exception { + pLatch = new CountDownLatch(1); + processor = new PrepRequestProcessor(zks, new MyRequestProcessor()); + Request foo = new Request(null, 1L, 1, OpCode.create, ByteBuffer.allocate(3), null); + processor.pRequest(foo); + + assertEquals("Request should have marshalling error", new ErrorTxn(KeeperException.Code.MARSHALLINGERROR.intValue()), outcome.getTxn()); + assertTrue("request hasn't been processed in chain", pLatch.await(5, TimeUnit.SECONDS)); + } + + private Request createRequest(Record record, int opCode) throws IOException { + return createRequest(record, opCode, 1L); + } + + private Request createRequest(Record record, int opCode, long sessionId) throws IOException { + return createRequest(record, opCode, sessionId, false); + } + + private Request createRequest(Record record, int opCode, boolean admin) throws IOException { + return createRequest(record, opCode, 1L, admin); + } + + private Request createRequest(Record record, int opCode, long sessionId, boolean admin) throws IOException { + // encoding + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + record.serialize(boa, "request"); + baos.close(); + // Id + List ids = Arrays.asList(admin ? new Id("super", "super user") : Ids.ANYONE_ID_UNSAFE); + return new Request(null, sessionId, 0, opCode, ByteBuffer.wrap(baos.toByteArray()), ids); + } + + private void process(List ops) throws Exception { + pLatch = new CountDownLatch(1); + processor = new PrepRequestProcessor(zks, new MyRequestProcessor()); + + Record record = new MultiOperationRecord(ops); + Request req = createRequest(record, OpCode.multi, false); + + processor.pRequest(req); + assertTrue("request hasn't been processed in chain", pLatch.await(5, TimeUnit.SECONDS)); + } + + /** + * This test checks that a successful multi will change outstanding record + * and failed multi shouldn't change outstanding record. + */ + @Test + public void testMultiOutstandingChange() throws Exception { + zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0); + + assertNull(zks.outstandingChangesForPath.get("/foo")); + + process(Arrays.asList(Op.setData("/foo", new byte[0], -1))); + + ChangeRecord cr = zks.outstandingChangesForPath.get("/foo"); + assertNotNull("Change record wasn't set", cr); + assertEquals("Record zxid wasn't set correctly", 1, cr.zxid); + + process(Arrays.asList(Op.delete("/foo", -1))); + cr = zks.outstandingChangesForPath.get("/foo"); + assertEquals("Record zxid wasn't set correctly", 2, cr.zxid); + + // It should fail and shouldn't change outstanding record. + process(Arrays.asList(Op.delete("/foo", -1))); + cr = zks.outstandingChangesForPath.get("/foo"); + // zxid should still be previous result because record's not changed. + assertEquals("Record zxid wasn't set correctly", 2, cr.zxid); + } + + @Test + public void testReconfigWithAnotherOutstandingChange() throws Exception { + QuorumPeerConfig.setReconfigEnabled(true); + QuorumPeerConfig.setStandaloneEnabled(false); + + QuorumPeer qp = new QuorumPeer(); + QuorumVerifier quorumVerifierMock = mock(QuorumVerifier.class); + when(quorumVerifierMock.getAllMembers()).thenReturn(LeaderBeanTest.getMockedPeerViews(qp.getId())); + + qp.setQuorumVerifier(quorumVerifierMock, false); + FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir); + LeaderZooKeeperServer lzks = new LeaderZooKeeperServer(snapLog, qp, new ZKDatabase(snapLog)); + qp.leader = new Leader(qp, lzks); + lzks.sessionTracker = new MySessionTracker(); + ZooKeeperServer.setDigestEnabled(true); + processor = new PrepRequestProcessor(lzks, new MyRequestProcessor()); + + Record record = new CreateRequest("/foo", "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag()); + pLatch = new CountDownLatch(1); + processor.pRequest(createRequest(record, OpCode.create, false)); + assertTrue("request hasn't been processed in chain", pLatch.await(5, TimeUnit.SECONDS)); + + String newMember = "server.0=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant"; + record = new ReconfigRequest(null, null, newMember, 0); + pLatch = new CountDownLatch(1); + processor.pRequest(createRequest(record, OpCode.reconfig, true)); + assertTrue("request hasn't been processed in chain", pLatch.await(5, TimeUnit.SECONDS)); + assertEquals(outcome.getHdr().getType(), OpCode.reconfig); // Verifies that there was no error. + } + + /** + * ZOOKEEPER-2052: + * This test checks that if a multi operation aborted, and during the multi there is side effect + * that changed outstandingChangesForPath, after aborted the side effect should be removed and + * everything should be restored correctly. + */ + @Test + public void testMultiRollbackNoLastChange() throws Exception { + zks.getZKDatabase().dataTree.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0); + zks.getZKDatabase().dataTree.createNode("/foo/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, 0, 0, 0, 0); + + assertNull(zks.outstandingChangesForPath.get("/foo")); + + // multi record: + // set "/foo" => succeed, leave a outstanding change + // delete "/foo" => fail, roll back change + process(Arrays.asList(Op.setData("/foo", new byte[0], -1), Op.delete("/foo", -1))); + + // aborting multi shouldn't leave any record. + assertNull(zks.outstandingChangesForPath.get("/foo")); + } + + /** + * Test ephemerals are deleted when the session is closed with + * the newly added CloseSessionTxn in ZOOKEEPER-3145. + */ + @Test + public void testCloseSessionTxn() throws Exception { + boolean before = ZooKeeperServer.isCloseSessionTxnEnabled(); + + ZooKeeperServer.setCloseSessionTxnEnabled(true); + try { + // create a few ephemerals + long ephemeralOwner = 1; + DataTree dt = zks.getZKDatabase().dataTree; + dt.createNode("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, ephemeralOwner, 0, 0, 0); + dt.createNode("/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, ephemeralOwner, 0, 0, 0); + + // close session + RequestHeader header = new RequestHeader(); + header.setType(OpCode.closeSession); + + final FinalRequestProcessor frq = new FinalRequestProcessor(zks); + final CountDownLatch latch = new CountDownLatch(1); + processor = new PrepRequestProcessor(zks, new RequestProcessor() { + @Override + public void processRequest(Request request) { + frq.processRequest(request); + latch.countDown(); + } + + @Override + public void shutdown() { + // TODO Auto-generated method stub + } + }); + processor.pRequest(createRequest(header, OpCode.closeSession, ephemeralOwner)); + + assertTrue(latch.await(3, TimeUnit.SECONDS)); + + // assert ephemerals are deleted + assertEquals(null, dt.getNode("/foo")); + assertEquals(null, dt.getNode("/bar")); + } finally { + ZooKeeperServer.setCloseSessionTxnEnabled(before); + } + } + + /** + * It tests that PrepRequestProcessor will return BadArgument KeeperException + * if the request path (if it exists) is not valid, e.g. empty string. + */ + @Test + public void testInvalidPath() throws Exception { + pLatch = new CountDownLatch(1); + processor = new PrepRequestProcessor(zks, new MyRequestProcessor()); + + SetDataRequest record = new SetDataRequest("", new byte[0], -1); + Request req = createRequest(record, OpCode.setData, false); + processor.pRequest(req); + pLatch.await(); + assertEquals(outcome.getHdr().getType(), OpCode.error); + assertEquals(outcome.getException().code(), KeeperException.Code.BADARGUMENTS); + } + + private class MyRequestProcessor implements RequestProcessor { + + @Override + public void processRequest(Request request) { + // getting called by PrepRequestProcessor + outcome = request; + pLatch.countDown(); + } + @Override + public void shutdown() { + // TODO Auto-generated method stub + + } + + } + + private class MySessionTracker implements SessionTracker { + + @Override + public boolean trackSession(long id, int to) { + // TODO Auto-generated method stub + return false; + } + @Override + public boolean commitSession(long id, int to) { + // TODO Auto-generated method stub + return false; + } + @Override + public void checkSession(long sessionId, Object owner) throws SessionExpiredException, SessionMovedException { + // TODO Auto-generated method stub + } + @Override + public long createSession(int sessionTimeout) { + // TODO Auto-generated method stub + return 0; + } + @Override + public void dumpSessions(PrintWriter pwriter) { + // TODO Auto-generated method stub + + } + @Override + public void removeSession(long sessionId) { + // TODO Auto-generated method stub + + } + public int upgradeSession(long sessionId) { + // TODO Auto-generated method stub + return 0; + } + @Override + public void setOwner(long id, Object owner) throws SessionExpiredException { + // TODO Auto-generated method stub + + } + @Override + public void shutdown() { + // TODO Auto-generated method stub + + } + @Override + public boolean touchSession(long sessionId, int sessionTimeout) { + // TODO Auto-generated method stub + return false; + } + @Override + public void setSessionClosing(long sessionId) { + // TODO Auto-generated method stub + } + @Override + public boolean isTrackingSession(long sessionId) { + // TODO Auto-generated method stub + return false; + } + @Override + public void checkGlobalSession(long sessionId, Object owner) throws SessionExpiredException, SessionMovedException { + // TODO Auto-generated method stub + } + @Override + public Map> getSessionExpiryMap() { + return new HashMap>(); + } + @Override + public long getLocalSessionCount() { + return 0; + } + + @Override + public boolean isLocalSessionsEnabled() { + return false; + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/PurgeTxnTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/PurgeTxnTest.java new file mode 100644 index 0000000..804e237 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/PurgeTxnTest.java @@ -0,0 +1,626 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import java.util.zip.CheckedOutputStream; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileSnap; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.SnapStream; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PurgeTxnTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(PurgeTxnTest.class); + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + private static final long OP_TIMEOUT_IN_MILLIS = 120000; + private File tmpDir; + + @Before + public void setUp() throws Exception { + tmpDir = ClientBase.createTmpDir(); + } + + @After + public void teardown() { + if (null != tmpDir) { + ClientBase.recursiveDelete(tmpDir); + } + } + + /** + * test the purge + * @throws Exception an exception might be thrown here + */ + @Test + public void testPurge() throws Exception { + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(100); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + for (int i = 0; i < 2000; i++) { + zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + f.shutdown(); + zks.getTxnLogFactory().close(); + assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + // now corrupt the snapshot + PurgeTxnLog.purge(tmpDir, tmpDir, 3); + FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpDir, tmpDir); + List listLogs = snaplog.findNValidSnapshots(4); + int numSnaps = 0; + for (File ff : listLogs) { + if (ff.getName().startsWith("snapshot")) { + numSnaps++; + } + } + assertTrue("exactly 3 snapshots ", (numSnaps == 3)); + snaplog.close(); + zks.shutdown(); + } + + /** + * Tests purge when logs are rolling or a new snapshot is created, then + * these newer files should alse be excluded in the current cycle. + * + * For frequent snapshotting, configured SnapCount to 30. There are three + * threads which will create 1000 znodes each and simultaneously do purge + * call + */ + @Test + public void testPurgeWhenLogRollingInProgress() throws Exception { + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(30); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + final ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + final CountDownLatch doPurge = new CountDownLatch(1); + final CountDownLatch purgeFinished = new CountDownLatch(1); + final AtomicBoolean opFailed = new AtomicBoolean(false); + new Thread() { + public void run() { + try { + doPurge.await(OP_TIMEOUT_IN_MILLIS / 2, TimeUnit.MILLISECONDS); + PurgeTxnLog.purge(tmpDir, tmpDir, 3); + } catch (IOException ioe) { + LOG.error("Exception when purge", ioe); + opFailed.set(true); + } catch (InterruptedException ie) { + LOG.error("Exception when purge", ie); + opFailed.set(true); + } finally { + purgeFinished.countDown(); + } + } + }.start(); + final int thCount = 3; + List znodes = manyClientOps(zk, doPurge, thCount, "/invalidsnap"); + assertTrue("Purging is not finished!", purgeFinished.await(OP_TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS)); + assertFalse("Purging failed!", opFailed.get()); + for (String znode : znodes) { + try { + zk.getData(znode, false, null); + } catch (Exception ke) { + LOG.error("Unexpected exception when visiting znode!", ke); + fail("Unexpected exception when visiting znode!"); + } + } + zk.close(); + f.shutdown(); + zks.shutdown(); + zks.getTxnLogFactory().close(); + } + + /** + * Tests finding n recent valid snapshots from set of snapshots and data logs + */ + @Test + public void testFindNValidSnapshots() throws Exception { + int nRecentSnap = 4; // n recent snap shots + int nRecentCount = 30; + int offset = 0; + + File version2 = new File(tmpDir.toString(), "version-2"); + assertTrue("Failed to create version_2 dir:" + version2.toString(), version2.mkdir()); + + // Test that with no snaps, findNValidSnapshots returns empty list + FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir); + List foundSnaps = txnLog.findNValidSnapshots(1); + assertEquals(0, foundSnaps.size()); + + List expectedNRecentSnapFiles = new ArrayList(); + int counter = offset + (2 * nRecentCount); + for (int i = 0; i < nRecentCount; i++) { + // simulate log file + File logFile = new File(version2 + "/log." + Long.toHexString(--counter)); + assertTrue("Failed to create log File:" + logFile.toString(), logFile.createNewFile()); + // simulate snapshot file + File snapFile = new File(version2 + "/snapshot." + Long.toHexString(--counter)); + assertTrue("Failed to create snap File:" + snapFile.toString(), snapFile.createNewFile()); + makeValidSnapshot(snapFile); + // add the n recent snap files for assertion + if (i < nRecentSnap) { + expectedNRecentSnapFiles.add(snapFile); + } + } + + // Test that when we ask for recent snaps we get the number we asked for and + // the files we expected + List nRecentValidSnapFiles = txnLog.findNValidSnapshots(nRecentSnap); + assertEquals("exactly 4 snapshots ", 4, nRecentValidSnapFiles.size()); + expectedNRecentSnapFiles.removeAll(nRecentValidSnapFiles); + assertEquals("Didn't get the recent snap files", 0, expectedNRecentSnapFiles.size()); + + // Test that when asking for more snaps than we created, we still only get snaps + // not logs or anything else (per ZOOKEEPER-2420) + nRecentValidSnapFiles = txnLog.findNValidSnapshots(nRecentCount + 5); + assertEquals(nRecentCount, nRecentValidSnapFiles.size()); + for (File f : nRecentValidSnapFiles) { + assertTrue("findNValidSnapshots() returned a non-snapshot: " + + f.getPath(), (Util.getZxidFromName(f.getName(), "snapshot") != -1)); + } + + txnLog.close(); + } + + /** + * Tests purge where the data directory contains old snapshots and data + * logs, newest snapshots and data logs, (newest + n) snapshots and data + * logs + */ + @Test + public void testSnapFilesGreaterThanToRetain() throws Exception { + int nRecentCount = 4; + int fileAboveRecentCount = 4; + int fileToPurgeCount = 2; + AtomicInteger offset = new AtomicInteger(0); + File version2 = new File(tmpDir.toString(), "version-2"); + assertTrue("Failed to create version_2 dir:" + version2.toString(), version2.mkdir()); + List snapsToPurge = new ArrayList(); + List logsToPurge = new ArrayList(); + List snaps = new ArrayList(); + List logs = new ArrayList(); + List snapsAboveRecentFiles = new ArrayList(); + List logsAboveRecentFiles = new ArrayList(); + createDataDirFiles(offset, fileToPurgeCount, false, version2, snapsToPurge, logsToPurge); + createDataDirFiles(offset, nRecentCount, false, version2, snaps, logs); + logs.add(logsToPurge.remove(0)); // log that precedes first retained snapshot is also retained + createDataDirFiles(offset, fileAboveRecentCount, false, version2, snapsAboveRecentFiles, logsAboveRecentFiles); + + /** + * The newest log file preceding the oldest retained snapshot is not removed as it may + * contain transactions newer than the oldest snapshot. + */ + logsToPurge.remove(0); + + FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir); + PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1)); + txnLog.close(); + verifyFilesAfterPurge(snapsToPurge, false); + verifyFilesAfterPurge(logsToPurge, false); + verifyFilesAfterPurge(snaps, true); + verifyFilesAfterPurge(logs, true); + verifyFilesAfterPurge(snapsAboveRecentFiles, true); + verifyFilesAfterPurge(logsAboveRecentFiles, true); + } + + /** + * Tests purge where the data directory contains snap files and log files equals to the + * number of files to be retained + */ + @Test + public void testSnapFilesEqualsToRetain() throws Exception { + internalTestSnapFilesEqualsToRetain(false); + } + + /** + * Tests purge where the data directory contains snap files equals to the + * number of files to be retained, and a log file that precedes the earliest snapshot + */ + @Test + public void testSnapFilesEqualsToRetainWithPrecedingLog() throws Exception { + internalTestSnapFilesEqualsToRetain(true); + } + + public void internalTestSnapFilesEqualsToRetain(boolean testWithPrecedingLogFile) throws Exception { + int nRecentCount = 3; + AtomicInteger offset = new AtomicInteger(0); + File version2 = new File(tmpDir.toString(), "version-2"); + assertTrue("Failed to create version_2 dir:" + version2.toString(), version2.mkdir()); + List snaps = new ArrayList(); + List logs = new ArrayList(); + createDataDirFiles(offset, nRecentCount, testWithPrecedingLogFile, version2, snaps, logs); + + FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir); + PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1)); + txnLog.close(); + verifyFilesAfterPurge(snaps, true); + verifyFilesAfterPurge(logs, true); + } + + /** + * Tests purge where the data directory contains old snapshots and data + * logs, newest snapshots and data logs + */ + @Test + public void testSnapFilesLessThanToRetain() throws Exception { + int nRecentCount = 4; + int fileToPurgeCount = 2; + AtomicInteger offset = new AtomicInteger(0); + File version2 = new File(tmpDir.toString(), "version-2"); + assertTrue("Failed to create version_2 dir:" + version2.toString(), version2.mkdir()); + List snapsToPurge = new ArrayList(); + List logsToPurge = new ArrayList(); + List snaps = new ArrayList(); + List logs = new ArrayList(); + createDataDirFiles(offset, fileToPurgeCount, false, version2, snapsToPurge, logsToPurge); + createDataDirFiles(offset, nRecentCount, false, version2, snaps, logs); + logs.add(logsToPurge.remove(0)); // log that precedes first retained snapshot is also retained + + /** + * The newest log file preceding the oldest retained snapshot is not removed as it may + * contain transactions newer than the oldest snapshot. + */ + logsToPurge.remove(0); + + FileTxnSnapLog txnLog = new FileTxnSnapLog(tmpDir, tmpDir); + PurgeTxnLog.purgeOlderSnapshots(txnLog, snaps.get(snaps.size() - 1)); + txnLog.close(); + verifyFilesAfterPurge(snapsToPurge, false); + verifyFilesAfterPurge(logsToPurge, false); + verifyFilesAfterPurge(snaps, true); + verifyFilesAfterPurge(logs, true); + } + + /** + * PurgeTxnLog is called with dataLogDir snapDir -n count This test case + * verify these values are parsed properly and functionality works fine + */ + @Test + public void testPurgeTxnLogWithDataDir() throws Exception { + File dataDir = new File(tmpDir, "dataDir"); + File dataLogDir = new File(tmpDir, "dataLogDir"); + + File dataDirVersion2 = new File(dataDir, "version-2"); + dataDirVersion2.mkdirs(); + File dataLogDirVersion2 = new File(dataLogDir, "version-2"); + dataLogDirVersion2.mkdirs(); + + // create dummy log and transaction file + int totalFiles = 20; + + // create transaction and snapshot files in different-different + // directories + for (int i = 0; i < totalFiles; i++) { + // simulate log file + File logFile = new File(dataLogDirVersion2, "log." + Long.toHexString(i)); + logFile.createNewFile(); + // simulate snapshot file + File snapFile = new File(dataDirVersion2, "snapshot." + Long.toHexString(i)); + snapFile.createNewFile(); + makeValidSnapshot(snapFile); + } + + int numberOfSnapFilesToKeep = 10; + // scenario where four parameter are passed + String[] args = new String[]{dataLogDir.getAbsolutePath(), dataDir.getAbsolutePath(), "-n", Integer.toString(numberOfSnapFilesToKeep)}; + PurgeTxnLog.main(args); + + assertEquals(numberOfSnapFilesToKeep, dataDirVersion2.listFiles().length); + // Since for each snapshot we have a log file with same zxid, expect same # logs as snaps to be kept + assertEquals(numberOfSnapFilesToKeep, dataLogDirVersion2.listFiles().length); + } + + /** + * PurgeTxnLog is called with dataLogDir -n count This test case verify + * these values are parsed properly and functionality works fine + */ + @Test + public void testPurgeTxnLogWithoutDataDir() throws Exception { + File dataDir = new File(tmpDir, "dataDir"); + File dataLogDir = new File(tmpDir, "dataLogDir"); + + File dataDirVersion2 = new File(dataDir, "version-2"); + dataDirVersion2.mkdirs(); + File dataLogDirVersion2 = new File(dataLogDir, "version-2"); + dataLogDirVersion2.mkdirs(); + + // create dummy log and transaction file + int totalFiles = 20; + + // create transaction and snapshot files in data directory + for (int i = 0; i < totalFiles; i++) { + // simulate log file + File logFile = new File(dataLogDirVersion2, "log." + Long.toHexString(i)); + logFile.createNewFile(); + // simulate snapshot file + File snapFile = new File(dataLogDirVersion2, "snapshot." + Long.toHexString(i)); + snapFile.createNewFile(); + makeValidSnapshot(snapFile); + } + + int numberOfSnapFilesToKeep = 10; + // scenario where only three parameter are passed + String[] args = new String[]{dataLogDir.getAbsolutePath(), "-n", Integer.toString(numberOfSnapFilesToKeep)}; + PurgeTxnLog.main(args); + assertEquals( + numberOfSnapFilesToKeep + * 2, // Since for each snapshot we have a log file with same zxid, expect same # logs as snaps to be kept + dataLogDirVersion2.listFiles().length); + } + + /** + * Verifies that purge does not delete any log files which started before the oldest retained + * snapshot but which might extend beyond it. + * @throws Exception an exception might be thrown here + */ + @Test + public void testPurgeDoesNotDeleteOverlappingLogFile() throws Exception { + // Setting used for snapRetainCount in this test. + final int SNAP_RETAIN_COUNT = 3; + // Number of znodes this test creates in each snapshot. + final int NUM_ZNODES_PER_SNAPSHOT = 100; + /** + * Set a sufficiently high snapCount to ensure that we don't rollover the log. Normally, + * the default value (100K at time of this writing) would ensure this, but we make that + * dependence explicit here to make the test future-proof. Not rolling over the log is + * important for this test since we are testing retention of the one and only log file which + * predates each retained snapshot. + */ + SyncRequestProcessor.setSnapCount(SNAP_RETAIN_COUNT * NUM_ZNODES_PER_SNAPSHOT * 10); + + // Create Zookeeper and connect to it. + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + + // Unique identifier for each znode that we create. + int unique = 0; + try { + /** + * Create some znodes and take a snapshot. Repeat this until we have SNAP_RETAIN_COUNT + * snapshots. Do not rollover the log. + */ + for (int snapshotCount = 0; snapshotCount < SNAP_RETAIN_COUNT; snapshotCount++) { + for (int i = 0; i < 100; i++, unique++) { + zk.create("/snap-" + unique, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zks.takeSnapshot(); + } + // Create some additional znodes without taking a snapshot afterwards. + for (int i = 0; i < 100; i++, unique++) { + zk.create("/snap-" + unique, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + + // Shutdown Zookeeper. + f.shutdown(); + zks.getTxnLogFactory().close(); + zks.shutdown(); + assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + + // Purge snapshot and log files. + PurgeTxnLog.purge(tmpDir, tmpDir, SNAP_RETAIN_COUNT); + + // Initialize Zookeeper again from the same dataDir. + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + zk = ClientBase.createZKClient(HOSTPORT); + + /** + * Verify that the last znode that was created above exists. This znode's creation was + * captured by the transaction log which was created before any of the above + * SNAP_RETAIN_COUNT snapshots were created, but it's not captured in any of these + * snapshots. So for it it exist, the (only) existing log file should not have been purged. + */ + final String lastZnode = "/snap-" + (unique - 1); + final Stat stat = zk.exists(lastZnode, false); + assertNotNull("Last znode does not exist: " + lastZnode, stat); + + // Shutdown for the last time. + f.shutdown(); + zks.getTxnLogFactory().close(); + zks.shutdown(); + } + + @Test + public void testPurgeTxnLogWhenRecentSnapshotsAreAllInvalid() throws Exception { + File dataDir = new File(tmpDir, "dataDir"); + File dataLogDir = new File(tmpDir, "dataLogDir"); + + File dataDirVersion2 = new File(dataDir, "version-2"); + dataDirVersion2.mkdirs(); + File dataLogDirVersion2 = new File(dataLogDir, "version-2"); + dataLogDirVersion2.mkdirs(); + + // create dummy log and transaction file + int totalFiles = 10; + int numberOfSnapFilesToKeep = 3; + + // create transaction and snapshot files in different-different + // directories + for (int i = 0; i < totalFiles; i++) { + // simulate log file + File logFile = new File(dataLogDirVersion2, "log." + Long.toHexString(i)); + logFile.createNewFile(); + // simulate snapshot file + File snapFile = new File(dataDirVersion2, "snapshot." + Long.toHexString(i)); + snapFile.createNewFile(); + if (i < (totalFiles - numberOfSnapFilesToKeep)) { + makeValidSnapshot(snapFile); + } else { + makeInvalidSnapshot(snapFile); + } + } + + // scenario where four parameter are passed + String[] args = new String[]{dataLogDir.getAbsolutePath(), dataDir.getAbsolutePath(), "-n", Integer.toString(numberOfSnapFilesToKeep)}; + PurgeTxnLog.main(args); + //Since the recent 3 snapshots are all invalid,when purging, we can assert that 6 snapshot files are retained(3 invalid snapshots and 3 retained valid snapshots) + assertEquals(numberOfSnapFilesToKeep + numberOfSnapFilesToKeep, dataDirVersion2.listFiles().length); + // Since for each snapshot we have a log file with same zxid, expect same # logs as snaps to be kept + assertEquals(numberOfSnapFilesToKeep + numberOfSnapFilesToKeep, dataLogDirVersion2.listFiles().length); + } + + private File createDataDirLogFile(File version_2, int Zxid) throws IOException { + File logFile = new File(version_2 + "/log." + Long.toHexString(Zxid)); + assertTrue("Failed to create log File:" + logFile.toString(), logFile.createNewFile()); + return logFile; + } + + private void createDataDirFiles(AtomicInteger offset, int limit, boolean createPrecedingLogFile, File version_2, List snaps, List logs) throws IOException { + int counter = offset.get() + (2 * limit); + if (createPrecedingLogFile) { + counter++; + } + offset.set(counter); + for (int i = 0; i < limit; i++) { + // simulate log file + logs.add(createDataDirLogFile(version_2, --counter)); + // simulate snapshot file + File snapFile = new File(version_2 + "/snapshot." + Long.toHexString(--counter)); + assertTrue("Failed to create snap File:" + snapFile.toString(), snapFile.createNewFile()); + snaps.add(snapFile); + } + if (createPrecedingLogFile) { + logs.add(createDataDirLogFile(version_2, --counter)); + } + } + + private void verifyFilesAfterPurge(List logs, boolean exists) { + for (File file : logs) { + assertEquals("After purging, file " + file, exists, file.exists()); + } + } + + private List manyClientOps(final ZooKeeper zk, final CountDownLatch doPurge, int thCount, final String prefix) { + Thread[] ths = new Thread[thCount]; + final List znodes = Collections.synchronizedList(new ArrayList()); + final CountDownLatch finished = new CountDownLatch(thCount); + final AtomicReference exception = new AtomicReference<>(); + for (int indx = 0; indx < thCount; indx++) { + final String myprefix = prefix + "-" + indx; + Thread th = new Thread(() -> { + for (int i = 0; i < 750; i++) { + try { + String mynode = myprefix + "-" + i; + znodes.add(mynode); + zk.create(mynode, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (Exception e) { + LOG.error("Unexpected exception during ZkClient ops", e); + exception.set(e); + } + if (i == 200) { + doPurge.countDown(); + } + } + finished.countDown(); + }); + ths[indx] = th; + } + + for (Thread thread : ths) { + thread.start(); + } + try { + boolean operationsFinishedSuccessfully = finished.await(OP_TIMEOUT_IN_MILLIS, TimeUnit.MILLISECONDS); + if (exception.get() != null) { + LOG.error("unexpected exception during running ZkClient ops:", exception.get()); + fail("unexpected exception during running ZkClient ops, see in the logs above"); + } + assertTrue("ZkClient ops not finished in time!", operationsFinishedSuccessfully); + } catch (InterruptedException ie) { + LOG.error("Unexpected exception", ie); + fail("Unexpected exception occurred!"); + } + return znodes; + } + + private void makeValidSnapshot(File snapFile) throws IOException { + SnapStream.setStreamMode(SnapStream.StreamMode.CHECKED); + CheckedOutputStream os = SnapStream.getOutputStream(snapFile, true); + OutputArchive oa = BinaryOutputArchive.getArchive(os); + FileHeader header = new FileHeader(FileSnap.SNAP_MAGIC, 2, 1); + header.serialize(oa, "fileheader"); + SnapStream.sealStream(os, oa); + os.flush(); + os.close(); + + assertTrue(SnapStream.isValidSnapshot(snapFile)); + } + + private void makeInvalidSnapshot(File snapFile) throws IOException { + SnapStream.setStreamMode(SnapStream.StreamMode.CHECKED); + OutputStream os = SnapStream.getOutputStream(snapFile, true); + os.write(1); + os.flush(); + os.close(); + + assertFalse(SnapStream.isValidSnapshot(snapFile)); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ReferenceCountedACLCacheTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ReferenceCountedACLCacheTest.java new file mode 100644 index 0000000..6bb5d95 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ReferenceCountedACLCacheTest.java @@ -0,0 +1,287 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.junit.Test; + +public class ReferenceCountedACLCacheTest { + + @Test + public void testSameACLGivesSameID() { + List testACL = createACL("myid"); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + + List testACL2 = createACL("myid"); + + assertEquals(aclId, cache.convertAcls(testACL2)); + } + + @Test + public void testWhetherOrderingMatters() { + List testACL = new ArrayList(); + testACL.add(new ACL(ZooDefs.Perms.READ, new Id("scheme", "ro"))); + testACL.add(new ACL(ZooDefs.Perms.WRITE, new Id("scheme", "rw"))); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + + List testACL2 = new ArrayList(); + testACL2.add(new ACL(ZooDefs.Perms.WRITE, new Id("scheme", "rw"))); + testACL2.add(new ACL(ZooDefs.Perms.READ, new Id("scheme", "ro"))); + + assertFalse(aclId.equals(cache.convertAcls(testACL2))); + } + + @Test + public void testBidirectionality() { + List testACL = createACL("myid"); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + + assertEquals(testACL, cache.convertLong(aclId)); + } + + @Test + public void testCacheSize() { + List testACL = createACL("myid"); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + assertEquals(1, cache.size()); + + List testACL2 = createACL("myid"); + + assertEquals(aclId, cache.convertAcls(testACL2)); + assertEquals(1, cache.size()); + + List testACL3 = createACL("differentId"); + + Long aclId3 = cache.convertAcls(testACL3); + assertFalse(aclId3.equals(aclId)); + assertEquals(2, cache.size()); + } + + @Test + public void testAddThenRemove() { + List testACL = createACL("myid"); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + assertEquals(1, cache.size()); + + cache.removeUsage(aclId); + assertEquals(0, cache.size()); + } + + @Test + public void testMultipleAddsAndRemove() { + List testACL = createACL("myid"); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + assertEquals(1, cache.size()); + + cache.convertAcls(testACL); + assertEquals(1, cache.size()); + + List testACL2 = createACL("anotherId"); + cache.convertAcls(testACL2); + + cache.removeUsage(aclId); + assertEquals(2, cache.size()); + cache.removeUsage(aclId); + assertEquals(1, cache.size()); + + Long newId = cache.convertAcls(testACL); + assertFalse(aclId.equals(newId)); + } + + @Test + public void testAddUsage() { + List testACL = createACL("myid"); + + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + Long aclId = cache.convertAcls(testACL); + assertEquals(1, cache.size()); + + cache.addUsage(aclId); + assertEquals(1, cache.size()); + + cache.removeUsage(aclId); + assertEquals(1, cache.size()); + cache.removeUsage(aclId); + assertEquals(0, cache.size()); + } + + @Test + public void testAddNonExistentUsage() { + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + cache.addUsage(1L); + + assertEquals(0, cache.size()); + /* + On startup, it's possible that we'll try calling addUsage of an ID not in the cache. This is safe to ignore + as it'll be added later when we traverse the tranlog. See discussion here: + http://mail-archives.apache.org/mod_mbox/zookeeper-user/201507.mbox/%3CCAB5oV2_ujhvBA1sEkCG2WRakPjCy%2BNR10620WK2G1GGgmEO44g%40mail.gmail.com%3E + + This test makes sure that we don't add the ID to the cache in this case as that would result in dupes later + and consequently incorrect counts and entries that will never be cleaned out. + */ + } + + @Test + public void testSerializeDeserialize() throws IOException { + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + + List acl1 = createACL("one"); + List acl2 = createACL("two"); + List acl3 = createACL("three"); + List acl4 = createACL("four"); + List acl5 = createACL("five"); + + Long aclId1 = convertACLsNTimes(cache, acl1, 1); + Long aclId2 = convertACLsNTimes(cache, acl2, 2); + Long aclId3 = convertACLsNTimes(cache, acl3, 3); + Long aclId4 = convertACLsNTimes(cache, acl4, 4); + Long aclId5 = convertACLsNTimes(cache, acl5, 5); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos); + cache.serialize(archive); + + BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray())); + ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache(); + deserializedCache.deserialize(inArchive); + callAddUsageNTimes(deserializedCache, aclId1, 1); + callAddUsageNTimes(deserializedCache, aclId2, 2); + callAddUsageNTimes(deserializedCache, aclId3, 3); + callAddUsageNTimes(deserializedCache, aclId4, 4); + callAddUsageNTimes(deserializedCache, aclId5, 5); + assertCachesEqual(cache, deserializedCache); + } + + @Test + public void testNPEInDeserialize() throws IOException { + ReferenceCountedACLCache serializeCache = new ReferenceCountedACLCache() { + @Override + public synchronized void serialize(OutputArchive oa) throws IOException { + oa.writeInt(1, "map"); + oa.writeLong(1, "long"); + oa.startVector(null, "acls"); + oa.endVector(null, "acls"); + } + }; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos); + serializeCache.serialize(archive); + BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray())); + ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache(); + try { + deserializedCache.deserialize(inArchive); + } catch (NullPointerException e) { + fail("should not throw NPE while do deserialized"); + } catch (RuntimeException e) { + // do nothing. + } + } + + private void assertCachesEqual(ReferenceCountedACLCache expected, ReferenceCountedACLCache actual) { + assertEquals(expected.aclIndex, actual.aclIndex); + assertEquals(expected.aclKeyMap, actual.aclKeyMap); + assertEquals(expected.longKeyMap, actual.longKeyMap); + assertEquals(expected.referenceCounter, actual.referenceCounter); + } + + @Test + public void testPurgeUnused() throws IOException { + ReferenceCountedACLCache cache = new ReferenceCountedACLCache(); + + List acl1 = createACL("one"); + List acl2 = createACL("two"); + List acl3 = createACL("three"); + List acl4 = createACL("four"); + List acl5 = createACL("five"); + + Long aclId1 = convertACLsNTimes(cache, acl1, 1); + Long aclId2 = convertACLsNTimes(cache, acl2, 2); + Long aclId3 = convertACLsNTimes(cache, acl3, 3); + Long aclId4 = convertACLsNTimes(cache, acl4, 4); + Long aclId5 = convertACLsNTimes(cache, acl5, 5); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive archive = BinaryOutputArchive.getArchive(baos); + cache.serialize(archive); + + BinaryInputArchive inArchive = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray())); + ReferenceCountedACLCache deserializedCache = new ReferenceCountedACLCache(); + deserializedCache.deserialize(inArchive); + callAddUsageNTimes(deserializedCache, aclId1, 1); + callAddUsageNTimes(deserializedCache, aclId2, 2); + deserializedCache.purgeUnused(); + + assertEquals(2, deserializedCache.size()); + assertEquals(aclId1, deserializedCache.convertAcls(acl1)); + assertEquals(aclId2, deserializedCache.convertAcls(acl2)); + assertFalse(acl3.equals(deserializedCache.convertAcls(acl3))); + assertFalse(acl4.equals(deserializedCache.convertAcls(acl4))); + assertFalse(acl5.equals(deserializedCache.convertAcls(acl5))); + } + + private void callAddUsageNTimes(ReferenceCountedACLCache deserializedCache, Long aclId, int num) { + for (int i = 0; i < num; i++) { + deserializedCache.addUsage(aclId); + } + } + + private Long convertACLsNTimes(ReferenceCountedACLCache cache, List acl, int num) { + if (num <= 0) { + return -1L; + } + + for (int i = 0; i < num - 1; i++) { + cache.convertAcls(acl); + } + + return cache.convertAcls(acl); + } + + private List createACL(String id) { + List acl1 = new ArrayList(); + acl1.add(new ACL(ZooDefs.Perms.ADMIN, new Id("scheme", id))); + return acl1; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/RequestThrottlerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/RequestThrottlerTest.java new file mode 100644 index 0000000..3e1de55 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/RequestThrottlerTest.java @@ -0,0 +1,349 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RequestThrottlerTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(RequestThrottlerTest.class); + + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static String GLOBAL_OUTSTANDING_LIMIT = "1"; + private static final int TOTAL_REQUESTS = 5; + private static final int STALL_TIME = 5000; + + // latch to hold requests in the PrepRequestProcessor to + // keep them from going down the pipeline to reach the final + // request processor, where the number of in process requests + // will be decreased + CountDownLatch resumeProcess = null; + + // latch to make sure all requests are submitted + CountDownLatch submitted = null; + + // latch to make sure all requests entered the pipeline + CountDownLatch entered = null; + + // latch to make sure requests finished the pipeline + CountDownLatch finished = null; + + CountDownLatch disconnected = null; + + ZooKeeperServer zks = null; + ServerCnxnFactory f = null; + ZooKeeper zk = null; + int connectionLossCount = 0; + + + @Before + public void setup() throws Exception { + // start a server and create a client + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + zks = new TestZooKeeperServer(tmpDir, tmpDir, 3000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + + resumeProcess = null; + submitted = null; + + zk = ClientBase.createZKClient(HOSTPORT); + } + + @After + public void tearDown() throws Exception { + // shut down the server and the client + if (null != zk) { + zk.close(); + } + + if (null != f) { + f.shutdown(); + } + if (null != zks) { + zks.shutdown(); + } + } + + // TestZooKeeperServer + // 1. uses our version of PrepRequestProcessor, which can hold the request as long as we want + // 2. count the number of submitted requests + class TestZooKeeperServer extends ZooKeeperServer { + + public TestZooKeeperServer(File snapDir, File logDir, int tickTime) throws IOException { + super(snapDir, logDir, tickTime); + } + + @Override + protected void setupRequestProcessors() { + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + RequestProcessor syncProcessor = new SyncRequestProcessor(this, finalProcessor); + ((SyncRequestProcessor) syncProcessor).start(); + firstProcessor = new TestPrepRequestProcessor(this, syncProcessor); + ((TestPrepRequestProcessor) firstProcessor).start(); + } + + @Override + public void submitRequest(Request si) { + if (null != submitted) { + submitted.countDown(); + } + super.submitRequest(si); + } + + @Override + public void requestFinished(Request request) { + if (null != finished){ + finished.countDown(); + } + super.requestFinished(request); + } + } + + class TestPrepRequestProcessor extends PrepRequestProcessor { + + public TestPrepRequestProcessor(ZooKeeperServer zks, RequestProcessor syncProcessor) { + super(zks, syncProcessor); + } + + @Override + protected void pRequest(Request request) throws RequestProcessorException { + // keep the request in the processor as long as we want + if (resumeProcess != null) { + try { + resumeProcess.await(20, TimeUnit.SECONDS); + } catch (Exception e) { + + } + } + + if (entered != null) { + entered.countDown(); + } + + super.pRequest(request); + } + + } + + @Test + public void testRequestThrottler() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + // we only allow two requests in the pipeline + RequestThrottler.setMaxRequests(2); + + RequestThrottler.setStallTime(STALL_TIME); + RequestThrottler.setDropStaleRequests(false); + + // no requests can go through the pipeline unless we raise the latch + resumeProcess = new CountDownLatch(1); + submitted = new CountDownLatch(TOTAL_REQUESTS); + entered = new CountDownLatch(TOTAL_REQUESTS); + + // send 5 requests asynchronously + for (int i = 0; i < TOTAL_REQUESTS; i++) { + zk.create("/request_throttle_test- " + i, ("/request_throttle_test- " + + i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (rc, path, ctx, name) -> { + }, null); + } + + // make sure the server received all 5 requests + submitted.await(5, TimeUnit.SECONDS); + Map metrics = MetricsUtils.currentServerMetrics(); + + // but only two requests can get into the pipeline because of the throttler + assertEquals(2L, (long) metrics.get("prep_processor_request_queued")); + assertEquals(1L, (long) metrics.get("request_throttle_wait_count")); + + // let the requests go through the pipeline and the throttler will be waken up to allow more requests + // to enter the pipeline + resumeProcess.countDown(); + entered.await(STALL_TIME, TimeUnit.MILLISECONDS); + + metrics = MetricsUtils.currentServerMetrics(); + assertEquals(TOTAL_REQUESTS, (long) metrics.get("prep_processor_request_queued")); + } + + @Test + public void testDropStaleRequests() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + // we only allow two requests in the pipeline + RequestThrottler.setMaxRequests(2); + + RequestThrottler.setStallTime(STALL_TIME); + + RequestThrottler.setDropStaleRequests(true); + + // no requests can go through the pipeline unless we raise the latch + resumeProcess = new CountDownLatch(1); + submitted = new CountDownLatch(TOTAL_REQUESTS); + + // send 5 requests asynchronously + for (int i = 0; i < TOTAL_REQUESTS; i++) { + zk.create("/request_throttle_test- " + i, ("/request_throttle_test- " + + i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (rc, path, ctx, name) -> { + }, null); + } + + // make sure the server received all 5 requests + submitted.await(5, TimeUnit.SECONDS); + Map metrics = MetricsUtils.currentServerMetrics(); + + // but only two requests can get into the pipeline because of the throttler + assertEquals(2L, (long) metrics.get("prep_processor_request_queued")); + assertEquals(1L, (long) metrics.get("request_throttle_wait_count")); + + for (ServerCnxn cnxn : f.cnxns) { + cnxn.setStale(); + } + zk = null; + + resumeProcess.countDown(); + LOG.info("raise the latch"); + + while (zks.getInflight() > 0) { + Thread.sleep(50); + } + + // the rest of the 3 requests will be dropped + // but only the first one for a connection will be counted + metrics = MetricsUtils.currentServerMetrics(); + assertEquals(2L, (long) metrics.get("prep_processor_request_queued")); + assertEquals(1, (long) metrics.get("stale_requests_dropped")); + } + + @Test + public void testLargeRequestThrottling() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + AsyncCallback.StringCallback createCallback = (rc, path, ctx, name) -> { + if (KeeperException.Code.get(rc) == KeeperException.Code.CONNECTIONLOSS) { + disconnected.countDown(); + connectionLossCount++; + } + }; + + // we allow five requests in the pipeline + RequestThrottler.setMaxRequests(5); + + // enable large request throttling + zks.setLargeRequestThreshold(150); + zks.setLargeRequestMaxBytes(400); + + // no requests can go through the pipeline unless we raise the latch + resumeProcess = new CountDownLatch(1); + // the connection will be close when large requests exceed the limit + // we can't use the submitted latch because requests after close won't be submitted + disconnected = new CountDownLatch(TOTAL_REQUESTS); + + // the total length of the request is about 170-180 bytes, so only two requests are allowed + byte[] data = new byte[100]; + + // send 5 requests asynchronously + for (int i = 0; i < TOTAL_REQUESTS; i++) { + zk.create("/request_throttle_test- " + i , data, + ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, createCallback, null); + } + + // make sure the server received all 5 requests + disconnected.await(5, TimeUnit.SECONDS); + Map metrics = MetricsUtils.currentServerMetrics(); + + // but only two requests can get into the pipeline because they are large requests + // the connection will be closed + Assert.assertEquals(2L, (long) metrics.get("prep_processor_request_queued")); + Assert.assertEquals(1L, (long) metrics.get("large_requests_rejected")); + Assert.assertEquals(5, connectionLossCount); + + finished = new CountDownLatch(2); + // let the requests go through the pipeline + resumeProcess.countDown(); + finished.await(5, TimeUnit.SECONDS); + + // when the two requests finish, they are stale because the connection is closed already + metrics = MetricsUtils.currentServerMetrics(); + Assert.assertEquals(2, (long) metrics.get("stale_replies")); + } + + @Test + public void testGlobalOutstandingRequestThrottlingWithRequestThrottlerDisabled() throws Exception { + try { + System.setProperty(ZooKeeperServer.GLOBAL_OUTSTANDING_LIMIT, GLOBAL_OUTSTANDING_LIMIT); + + ServerMetrics.getMetrics().resetAll(); + + // Here we disable RequestThrottler and let incoming requests queued at first request processor. + RequestThrottler.setMaxRequests(0); + resumeProcess = new CountDownLatch(1); + int totalRequests = 10; + submitted = new CountDownLatch(totalRequests); + + for (int i = 0; i < totalRequests; i++) { + zk.create("/request_throttle_test- " + i, ("/request_throttle_test- " + + i).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (rc, path, ctx, name) -> { + }, null); + } + + submitted.await(5, TimeUnit.SECONDS); + + resumeProcess.countDown(); + + // We should start throttling instead of queuing more requests. + // + // We always allow up to GLOBAL_OUTSTANDING_LIMIT + 1 number of requests coming in request processing pipeline + // before throttling. For the next request, we will throttle by disabling receiving future requests but we still + // allow this single request coming in. So the total number of queued requests in processing pipeline would + // be GLOBAL_OUTSTANDING_LIMIT + 2. + assertEquals(Integer.parseInt(GLOBAL_OUTSTANDING_LIMIT) + 2, + (long) MetricsUtils.currentServerMetrics().get("prep_processor_request_queued")); + } catch (Exception e) { + throw e; + } finally { + System.clearProperty(ZooKeeperServer.GLOBAL_OUTSTANDING_LIMIT); + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/SerializationPerfTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/SerializationPerfTest.java new file mode 100644 index 0000000..1ff0a76 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/SerializationPerfTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.IOException; +import java.io.OutputStream; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SerializationPerfTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(SerializationPerfTest.class); + + private static class NullOutputStream extends OutputStream { + + public void write(int b) { + // do nothing - exclude persistence from perf + } + + } + + static int createNodes(DataTree tree, String path, int depth, int childcount, int parentCVersion, byte[] data) throws KeeperException.NodeExistsException, KeeperException.NoNodeException { + path += "node" + depth; + tree.createNode(path, data, null, -1, ++parentCVersion, 1, 1); + + if (--depth == 0) { + return 1; + } + + path += "/"; + + int count = 1; + for (int i = 0; i < childcount; i++) { + count += createNodes(tree, path + i, depth, childcount, 1, data); + } + + return count; + } + + private static void serializeTree(int depth, int width, int len) throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + DataTree tree = new DataTree(); + createNodes(tree, "/", depth, width, tree.getNode("/").stat.getCversion(), new byte[len]); + int count = tree.getNodeCount(); + + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(new NullOutputStream()); + System.gc(); + long start = System.nanoTime(); + tree.serialize(oa, "test"); + long end = System.nanoTime(); + long durationms = (end - start) / 1000000L; + long pernodeus = ((end - start) / 1000L) / count; + LOG.info( + "Serialized {} nodes in {} ms ({}us/node), depth={} width={} datalen={}", + count, + durationms, + pernodeus, + depth, + width, + len); + } + + @Test + public void testSingleSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(1, 0, 20); + } + + @Test + public void testWideSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(2, 10000, 20); + } + + @Test + public void testDeepSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(400, 1, 20); + } + + @Test + public void test10Wide5DeepSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(5, 10, 20); + } + + @Test + public void test15Wide5DeepSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(5, 15, 20); + } + + @Test + public void test25Wide4DeepSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(4, 25, 20); + } + + @Test + public void test40Wide4DeepSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(4, 40, 20); + } + + @Test + public void test300Wide3DeepSerialize() throws InterruptedException, IOException, KeeperException.NodeExistsException, KeeperException.NoNodeException { + serializeTree(3, 300, 20); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerIdTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerIdTest.java new file mode 100644 index 0000000..3f3f496 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerIdTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZKParameterized; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class ServerIdTest extends ClientBase { + + private final TestType testType; + + private static class TestType { + + final boolean ttlsEnabled; + final int serverId; + + TestType(boolean ttlsEnabled, int serverId) { + this.ttlsEnabled = ttlsEnabled; + this.serverId = serverId; + } + + } + + @Parameterized.Parameters + public static List data() { + List testTypes = new ArrayList<>(); + for (boolean ttlsEnabled : new boolean[]{true, false}) { + for (int serverId = 0; serverId <= 255; ++serverId) { + testTypes.add(new TestType(ttlsEnabled, serverId)); + } + } + return testTypes; + } + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + System.clearProperty("zookeeper.extendedTypesEnabled"); + } + + public ServerIdTest(TestType testType) { + this.testType = testType; + } + + @Before + @Override + public void setUp() throws Exception { + System.setProperty("zookeeper.extendedTypesEnabled", Boolean.toString(testType.ttlsEnabled)); + LOG.info("ttlsEnabled: {} - ServerId: {}", testType.ttlsEnabled, testType.serverId); + try { + super.setUpWithServerId(testType.serverId); + } catch (RuntimeException e) { + if (testType.ttlsEnabled && (testType.serverId >= EphemeralType.MAX_EXTENDED_SERVER_ID)) { + return; // expected + } + throw e; + } + } + + @Test + public void doTest() throws Exception { + if (testType.ttlsEnabled && (testType.serverId >= EphemeralType.MAX_EXTENDED_SERVER_ID)) { + return; + } + + TestableZooKeeper zk = null; + try { + zk = createClient(); + + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/foo", -1); + + if (testType.ttlsEnabled) { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, new Stat(), 1000); // should work + } else { + try { + zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, new Stat(), 1000); + fail("Should have thrown KeeperException.UnimplementedException"); + } catch (KeeperException.UnimplementedException e) { + // expected + } + } + } finally { + if (zk != null) { + zk.close(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerMetricsTest.java new file mode 100644 index 0000000..29a9fd9 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerMetricsTest.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import java.util.Arrays; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.metric.AvgMinMaxCounter; +import org.apache.zookeeper.server.metric.SimpleCounter; +import org.junit.Test; + +public class ServerMetricsTest extends ZKTestCase { + + private static final int RANDOM_TRIALS = 100; + private static final int RANDOM_SIZE = 100; + + private long[] generateRandomValues(int size) { + // Clamp range to prevent overflow in metric aggregation + final long[] values = new long[size]; + if (size == 0) { + return values; + } + final long rangeMin = Long.MIN_VALUE / size; + final long rangeMax = Long.MAX_VALUE / size; + for (int i = 0; i < size; ++i) { + values[i] = ThreadLocalRandom.current().nextLong(rangeMin, rangeMax); + } + return values; + } + + @Test + public void testAvgMinMaxCounter() { + final AvgMinMaxCounter metric = new AvgMinMaxCounter("test"); + testAvgMinMaxCounter(metric, 0); + testAvgMinMaxCounter(metric, 1); + for (int i = 0; i < RANDOM_TRIALS; ++i) { + testAvgMinMaxCounter(metric, RANDOM_SIZE); + } + } + + private void testAvgMinMaxCounter(AvgMinMaxCounter metric, int size) { + final long[] values = generateRandomValues(size); + for (long value : values) { + metric.add(value); + } + long expectedMin = Arrays.stream(values).min().orElse(0); + long expectedMax = Arrays.stream(values).max().orElse(0); + long expectedSum = Arrays.stream(values).sum(); + long expectedCnt = values.length; + double expectedAvg = expectedSum / Math.max(1, expectedCnt); + + assertEquals(expectedAvg, metric.getAvg(), 200); + assertEquals(expectedMin, metric.getMin()); + assertEquals(expectedMax, metric.getMax()); + assertEquals(expectedCnt, metric.getCount()); + assertEquals(expectedSum, metric.getTotal()); + + final Map results = metric.values(); + assertEquals(expectedMax, (long) results.get("max_test")); + assertEquals(expectedMin, (long) results.get("min_test")); + assertEquals(expectedCnt, (long) results.get("cnt_test")); + assertEquals(expectedAvg, (double) results.get("avg_test"), 200); + + metric.reset(); + } + + @Test + public void testSimpleCounter() { + SimpleCounter metric = new SimpleCounter("test"); + testSimpleCounter(metric, 0); + testSimpleCounter(metric, 1); + for (int i = 0; i < RANDOM_TRIALS; ++i) { + testSimpleCounter(metric, RANDOM_SIZE); + } + } + + private void testSimpleCounter(SimpleCounter metric, int size) { + final long[] values = generateRandomValues(size); + for (long value : values) { + metric.add(value); + } + + long expectedCount = Arrays.stream(values).sum(); + assertEquals(expectedCount, metric.get()); + + final Map results = metric.values(); + assertEquals(expectedCount, (long) results.get("test")); + + metric.reset(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerStatsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerStatsTest.java new file mode 100644 index 0000000..05a3505 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ServerStatsTest.java @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class ServerStatsTest extends ZKTestCase { + + private ServerStats.Provider providerMock; + + @Before + public void setUp() { + providerMock = mock(ServerStats.Provider.class); + } + + @Test + public void testPacketsMetrics() { + // Given ... + ServerStats serverStats = new ServerStats(providerMock); + int incrementCount = 20; + + // When increment ... + for (int i = 0; i < incrementCount; i++) { + serverStats.incrementPacketsSent(); + serverStats.incrementPacketsReceived(); + serverStats.incrementPacketsReceived(); + } + + // Then ... + assertEquals(incrementCount, serverStats.getPacketsSent()); + assertEquals(incrementCount * 2, serverStats.getPacketsReceived()); + + // When reset ... + serverStats.resetRequestCounters(); + + // Then ... + assertAllPacketsZero(serverStats); + + } + + @Test + public void testLatencyMetrics() { + // Given ... + ServerStats serverStats = new ServerStats(providerMock); + + // When incremented... + Request fakeRequest = new Request(0, 0, 0, null, null, 0); + serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 1000); + serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 2000); + + // Then ... + assertThat("Max latency check", 2000L, lessThanOrEqualTo(serverStats.getMaxLatency())); + assertThat("Min latency check", 1000L, lessThanOrEqualTo(serverStats.getMinLatency())); + assertEquals(1500, serverStats.getAvgLatency(), 200); + + // When reset... + serverStats.resetLatency(); + + // Then ... + assertAllLatencyZero(serverStats); + } + + @Test + public void testFsyncThresholdExceedMetrics() { + // Given ... + ServerStats serverStats = new ServerStats(providerMock); + int incrementCount = 30; + + // When increment ... + for (int i = 0; i < incrementCount; i++) { + serverStats.incrementFsyncThresholdExceedCount(); + } + + // Then ... + assertEquals(incrementCount, serverStats.getFsyncThresholdExceedCount()); + + // When reset ... + serverStats.resetFsyncThresholdExceedCount(); + + // Then ... + assertFsyncThresholdExceedCountZero(serverStats); + + } + + @Test + public void testReset() { + // Given ... + ServerStats serverStats = new ServerStats(providerMock); + + assertAllPacketsZero(serverStats); + assertAllLatencyZero(serverStats); + + // When ... + Request fakeRequest = new Request(0, 0, 0, null, null, 0); + serverStats.incrementPacketsSent(); + serverStats.incrementPacketsReceived(); + serverStats.updateLatency(fakeRequest, fakeRequest.createTime + 1000); + + serverStats.reset(); + + // Then ... + assertAllPacketsZero(serverStats); + assertAllLatencyZero(serverStats); + } + + private void assertAllPacketsZero(ServerStats serverStats) { + assertEquals(0L, serverStats.getPacketsSent()); + assertEquals(0L, serverStats.getPacketsReceived()); + } + + private void assertAllLatencyZero(ServerStats serverStats) { + assertEquals(0L, serverStats.getMaxLatency()); + assertEquals(0L, serverStats.getMinLatency()); + assertEquals(0, serverStats.getAvgLatency(), 0.00001); + } + + private void assertFsyncThresholdExceedCountZero(ServerStats serverStats) { + assertEquals(0L, serverStats.getFsyncThresholdExceedCount()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/SessionTrackerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/SessionTrackerTest.java new file mode 100644 index 0000000..c008550 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/SessionTrackerTest.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.server.SessionTrackerImpl.SessionImpl; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +/** + * Testing zk client session logic in sessiontracker + */ +public class SessionTrackerTest extends ZKTestCase { + + private final long sessionId = 339900; + private final int sessionTimeout = 3000; + private FirstProcessor firstProcessor; + private CountDownLatch latch; + + /** + * Verify the create session call in the Leader.FinalRequestProcessor after + * the session expiration. + */ + @Test(timeout = 20000) + public void testAddSessionAfterSessionExpiry() throws Exception { + RequestThrottler.setMaxRequests(0); + ZooKeeperServer zks = setupSessionTracker(); + + latch = new CountDownLatch(1); + zks.sessionTracker.trackSession(sessionId, sessionTimeout); + SessionTrackerImpl sessionTrackerImpl = (SessionTrackerImpl) zks.sessionTracker; + SessionImpl sessionImpl = sessionTrackerImpl.sessionsById.get(sessionId); + assertNotNull("Sessionid:" + sessionId + " doesn't exists in sessiontracker", sessionImpl); + + // verify the session existence + Object sessionOwner = new Object(); + sessionTrackerImpl.checkSession(sessionId, sessionOwner); + + // waiting for the session expiry + latch.await(sessionTimeout * 2, TimeUnit.MILLISECONDS); + + // Simulating FinalRequestProcessor logic: create session request has + // delayed and now reaches FinalRequestProcessor. Here the leader zk + // will do sessionTracker.addSession(id, timeout) + sessionTrackerImpl.trackSession(sessionId, sessionTimeout); + try { + sessionTrackerImpl.checkSession(sessionId, sessionOwner); + fail("Should throw session expiry exception " + "as the session has expired and closed"); + } catch (KeeperException.SessionExpiredException e) { + // expected behaviour + } + assertTrue("Session didn't expired", sessionImpl.isClosing()); + assertFalse("Session didn't expired", sessionTrackerImpl.touchSession(sessionId, sessionTimeout)); + assertEquals("Duplicate session expiry request has been generated", 1, firstProcessor.getCountOfCloseSessionReq()); + } + + /** + * Verify the session closure request has reached PrepRequestProcessor soon + * after session expiration by the session tracker + */ + @Test(timeout = 20000) + public void testCloseSessionRequestAfterSessionExpiry() throws Exception { + ZooKeeperServer zks = setupSessionTracker(); + + latch = new CountDownLatch(1); + zks.sessionTracker.trackSession(sessionId, sessionTimeout); + SessionTrackerImpl sessionTrackerImpl = (SessionTrackerImpl) zks.sessionTracker; + SessionImpl sessionImpl = sessionTrackerImpl.sessionsById.get(sessionId); + assertNotNull("Sessionid:" + sessionId + " doesn't exists in sessiontracker", sessionImpl); + + // verify the session existence + Object sessionOwner = new Object(); + sessionTrackerImpl.checkSession(sessionId, sessionOwner); + + // waiting for the session expiry + latch.await(sessionTimeout * 2, TimeUnit.MILLISECONDS); + + // Simulating close session request: removeSession() will be executed + // while OpCode.closeSession + sessionTrackerImpl.removeSession(sessionId); + SessionImpl actualSession = sessionTrackerImpl.sessionsById.get(sessionId); + assertNull("Session:" + sessionId + " still exists after removal", actualSession); + } + + private ZooKeeperServer setupSessionTracker() throws IOException { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + zks.setupRequestProcessors(); + firstProcessor = new FirstProcessor(zks, null); + zks.firstProcessor = firstProcessor; + + // setup session tracker + zks.createSessionTracker(); + zks.startSessionTracker(); + zks.startRequestThrottler(); + return zks; + } + + // Mock processor used in zookeeper server + private class FirstProcessor extends PrepRequestProcessor { + + private volatile int countOfCloseSessionReq = 0; + + public FirstProcessor(ZooKeeperServer zks, RequestProcessor nextProcessor) { + super(zks, nextProcessor); + } + + @Override + public void processRequest(Request request) { + // check session close request + if (request.type == OpCode.closeSession) { + countOfCloseSessionReq++; + latch.countDown(); + } + } + + // return number of session expiry calls + int getCountOfCloseSessionReq() { + return countOfCloseSessionReq; + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/SnapshotDigestTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/SnapshotDigestTest.java new file mode 100644 index 0000000..d40debf --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/SnapshotDigestTest.java @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.server.metric.SimpleCounter; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerMainTest; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SnapshotDigestTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(SnapshotDigestTest.class); + + private ZooKeeper zk; + private ZooKeeperServer server; + + @Before + public void setUp() throws Exception { + super.setUp(); + server = serverFactory.getZooKeeperServer(); + zk = createClient(); + } + + @After + public void tearDown() throws Exception { + // server will be closed in super.tearDown + super.tearDown(); + + if (zk != null) { + zk.close(); + } + } + + @Override + public void setupCustomizedEnv() { + ZooKeeperServer.setDigestEnabled(true); + System.setProperty(ZooKeeperServer.SNAP_COUNT, "100"); + } + + @Override + public void cleanUpCustomizedEnv() { + ZooKeeperServer.setDigestEnabled(false); + System.clearProperty(ZooKeeperServer.SNAP_COUNT); + } + + /** + * Check snapshot digests when loading a fuzzy or non-fuzzy snapshot. + */ + @Test + public void testSnapshotDigest() throws Exception { + // take a empty snapshot without creating any txn and make sure + // there is no digest mismatch issue + server.takeSnapshot(); + reloadSnapshotAndCheckDigest(); + + // trigger various write requests + String pathPrefix = "/testSnapshotDigest"; + for (int i = 0; i < 1000; i++) { + String path = pathPrefix + i; + zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + // update the data of first node + String firstNode = pathPrefix + 0; + zk.setData(firstNode, "new_setdata".getBytes(), -1); + + // delete the first node + zk.delete(firstNode, -1); + + // trigger multi op + List subTxns = new ArrayList(); + for (int i = 0; i < 3; i++) { + String path = pathPrefix + "-m" + i; + subTxns.add(Op.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + } + zk.multi(subTxns); + + reloadSnapshotAndCheckDigest(); + + // Take a snapshot and test the logic when loading a non-fuzzy snapshot + server = serverFactory.getZooKeeperServer(); + server.takeSnapshot(); + + reloadSnapshotAndCheckDigest(); + } + + /** + * Make sure the code will skip digest check when it's comparing + * digest with different version. + * + * This enables us to smoonthly add new fields into digest or using + * new digest calculation. + */ + @Test + public void testDifferentDigestVersion() throws Exception { + // check the current digest version + int currentVersion = new DigestCalculator().getDigestVersion(); + + // create a node + String path = "/testDifferentDigestVersion"; + zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // take a full snapshot + server.takeSnapshot(); + + //increment the digest version + int newVersion = currentVersion + 1; + DigestCalculator newVersionDigestCalculator = Mockito.spy(DigestCalculator.class); + Mockito.when(newVersionDigestCalculator.getDigestVersion()).thenReturn(newVersion); + assertEquals(newVersion, newVersionDigestCalculator.getDigestVersion()); + + // using mock to return different digest value when the way we + // calculate digest changed + FileTxnSnapLog txnSnapLog = new FileTxnSnapLog(tmpDir, tmpDir); + DataTree dataTree = Mockito.spy(new DataTree(newVersionDigestCalculator)); + Mockito.when(dataTree.getTreeDigest()).thenReturn(0L); + txnSnapLog.restore(dataTree, new ConcurrentHashMap<>(), Mockito.mock(FileTxnSnapLog.PlayBackListener.class)); + + // make sure the reportDigestMismatch function is never called + Mockito.verify(dataTree, Mockito.never()).reportDigestMismatch(Mockito.anyLong()); + } + + /** + * Make sure it's backward compatible, and also we can rollback this + * feature without corrupt the database. + */ + @Test + public void testBackwardCompatible() throws Exception { + testCompatibleHelper(false, true); + + testCompatibleHelper(true, false); + } + + private void testCompatibleHelper(Boolean enabledBefore, Boolean enabledAfter) throws Exception { + + ZooKeeperServer.setDigestEnabled(enabledBefore); + + + // restart the server to cache the option change + reloadSnapshotAndCheckDigest(); + + // create a node + String path = "/testCompatible" + "-" + enabledBefore + "-" + enabledAfter; + zk.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // take a full snapshot + server.takeSnapshot(); + + ZooKeeperServer.setDigestEnabled(enabledAfter); + + reloadSnapshotAndCheckDigest(); + + assertEquals(path, new String(zk.getData(path, false, null))); + } + + private void reloadSnapshotAndCheckDigest() throws Exception { + stopServer(); + QuorumPeerMainTest.waitForOne(zk, States.CONNECTING); + + ((SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT).reset(); + + startServer(); + QuorumPeerMainTest.waitForOne(zk, States.CONNECTED); + + server = serverFactory.getZooKeeperServer(); + + // Snapshot digests always match + assertEquals(0L, ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT.get()); + + // reset the digestFromLoadedSnapshot after comparing + assertNull(server.getZKDatabase().getDataTree().getDigestFromLoadedSnapshot()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ToStringTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ToStringTest.java new file mode 100644 index 0000000..e1349a3 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ToStringTest.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertNotSame; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.proto.SetDataRequest; +import org.junit.Test; + +/** + * A misc place to verify toString methods - mainly to make sure they don't + * fail. + */ +public class ToStringTest extends ZKTestCase { + + /** Verify jute - which we've had particular problems with in the past + * wrt null fields */ + @Test + public void testJuteToString() { + SetDataRequest req = new SetDataRequest(null, null, 0); + assertNotSame("ERROR", req.toString()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/TxnLogDigestTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/TxnLogDigestTest.java new file mode 100644 index 0000000..ae914af --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/TxnLogDigestTest.java @@ -0,0 +1,277 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertThat; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import mockit.Invocation; +import mockit.Mock; +import mockit.MockUp; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.server.metric.SimpleCounter; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.server.quorum.QuorumPeerMainTest; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TxnLogDigestTest extends ClientBase { + + private static final Logger LOG = + LoggerFactory.getLogger(TxnLogDigestTest.class); + + private ZooKeeper zk; + private ZooKeeperServer server; + + @Before + public void setUp() throws Exception { + super.setUp(); + server = serverFactory.getZooKeeperServer(); + zk = createClient(); + } + + @After + public void tearDown() throws Exception { + // server will be closed in super.tearDown + super.tearDown(); + + if (zk != null) { + zk.close(); + } + MockedFileTxnLog.reset(); + } + + @Override + public void setupCustomizedEnv() { + ZooKeeperServer.setDigestEnabled(true); + } + + @Override + public void cleanUpCustomizedEnv() { + ZooKeeperServer.setDigestEnabled(false); + } + + @BeforeClass + public static void applyMockUps() { + new MockedFileTxnLog(); + } + + /** + * Check that the digest stored in the txn matches the digest calculated + * from DataTree. + */ + @Test + public void digestFromTxnLogsMatchesTree() throws Exception { + // reset the mismatch metrics + SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT; + digestMistachesCount.reset(); + + // trigger some write ops + performOperations(createClient(), "/digestFromTxnLogsMatchesTree"); + + // make sure there is no digest mismatch + Assert.assertEquals(0, digestMistachesCount.get()); + + // verify that the digest is wrote to disk with txn + TxnDigest lastDigest = getLastTxnLogDigest(); + Assert.assertNotNull(lastDigest); + Assert.assertEquals(server.getZKDatabase().getDataTree().getTreeDigest(), + lastDigest.getTreeDigest()); + } + + /** + * Test the compatible when enable/disable digest: + * + * * check that txns which were written with digest can be read when + * digest is disabled + * * check that txns which were written without digest can be read + * when digest is enabled. + */ + @Test + public void checkTxnCompatibleWithAndWithoutDigest() throws Exception { + // 1. start server with digest disabled + restartServerWithDigestFlag(false); + + // trigger some write ops + Map expectedNodes = performOperations(createClient(), "/p1"); + + // reset the mismatch metrics + SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT; + digestMistachesCount.reset(); + + // 2. restart server with digest enabled + restartServerWithDigestFlag(true); + + // make sure the data wrote when digest was disabled can be + // successfully read + checkNodes(expectedNodes); + + Map expectedNodes1 = performOperations(createClient(), "/p2"); + + // make sure there is no digest mismatch + Assert.assertEquals(0, digestMistachesCount.get()); + + // 3. disable the digest again and make sure everything is fine + restartServerWithDigestFlag(false); + + checkNodes(expectedNodes); + checkNodes(expectedNodes1); + } + + /** + * Simulate the scenario where txn is missing, and make sure the + * digest code can catch this issue. + */ + @Test + public void testTxnMissing() throws Exception { + // updated MockedFileTxnLog to skip append txn on specific txn + MockedFileTxnLog.skipAppendZxid = 3; + + // trigger some write operations + performOperations(createClient(), "/testTxnMissing"); + + // restart server to load the corrupted txn file + SimpleCounter digestMistachesCount = (SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT; + digestMistachesCount.reset(); + + restartServerWithDigestFlag(true); + + // check that digest mismatch is reported + assertThat("mismtach should be reported", digestMistachesCount.get(), greaterThan(0L)); + + // restart server with digest disabled + digestMistachesCount.reset(); + restartServerWithDigestFlag(false); + + // check that no digest mismatch is reported + Assert.assertEquals(0, digestMistachesCount.get()); + } + + private void restartServerWithDigestFlag(boolean digestEnabled) + throws Exception { + stopServer(); + QuorumPeerMainTest.waitForOne(zk, States.CONNECTING); + + ZooKeeperServer.setDigestEnabled(digestEnabled); + + startServer(); + QuorumPeerMainTest.waitForOne(zk, States.CONNECTED); + } + + private TxnDigest getLastTxnLogDigest() throws IOException { + TxnIterator itr = new FileTxnLog(new File(tmpDir, "version-2")).read(1); + TxnDigest lastDigest = null; + while (itr.next()) { + lastDigest = itr.getDigest(); + } + return lastDigest; + } + + public static void create(ZooKeeper client, String path, CreateMode mode) + throws Exception { + client.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, mode); + } + + /** + * Helper method to trigger various write ops inside ZK. + */ + public static Map performOperations( + ZooKeeper client, String prefix) throws Exception { + Map nodes = new HashMap<>(); + + String path = prefix; + create(client, path, CreateMode.PERSISTENT); + nodes.put(path, path); + + path = prefix + "/child1"; + create(client, path, CreateMode.PERSISTENT); + nodes.put(path, path); + + path = prefix + "/child2"; + create(client, path, CreateMode.PERSISTENT); + client.delete(prefix + "/child2", -1); + + path = prefix + "/child1/leaf"; + create(client, path, CreateMode.PERSISTENT); + String updatedData = "updated data"; + client.setData(path, updatedData.getBytes(), -1); + nodes.put(path, updatedData); + + List subTxns = new ArrayList(); + for (int i = 0; i < 3; i++) { + path = prefix + "/m" + i; + subTxns.add(Op.create(path, path.getBytes(), + ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + nodes.put(path, path); + } + client.multi(subTxns); + client.close(); + + return nodes; + } + + private void checkNodes(Map expectedNodes) throws Exception { + ZooKeeper client = createClient(); + try { + for (Map.Entry entry: expectedNodes.entrySet()) { + Assert.assertEquals(entry.getValue(), + new String(client.getData(entry.getKey(), false, null))); + } + } finally { + client.close(); + } + } + + public static final class MockedFileTxnLog extends MockUp { + static long skipAppendZxid = -1; + + @Mock + public synchronized boolean append(Invocation invocation, TxnHeader hdr, + Record txn, TxnDigest digest) throws IOException { + if (hdr != null && hdr.getZxid() == skipAppendZxid) { + LOG.info("skipping txn {}", skipAppendZxid); + return true; + } + return invocation.proceed(hdr, txn, digest); + } + + public static void reset() { + skipAppendZxid = -1; + } + }; +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/X509AuthFailureTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/X509AuthFailureTest.java new file mode 100644 index 0000000..18147bb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/X509AuthFailureTest.java @@ -0,0 +1,99 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class X509AuthFailureTest extends ZKTestCase { + protected static final Logger LOG = LoggerFactory.getLogger(X509AuthFailureTest.class); + + private static ClientX509Util clientX509Util; + public static final int TIMEOUT = 5000; + public static int CONNECTION_TIMEOUT = 30000; + + @Before + public void setup() throws Exception{ + clientX509Util = new ClientX509Util(); + String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data"); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); + System.setProperty(clientX509Util.getSslKeystoreLocationProperty(), testDataPath + "/ssl/testKeyStore.jks"); + System.setProperty(clientX509Util.getSslKeystorePasswdProperty(), "testpass"); + System.setProperty("zookeeper.admin.serverPort", "" + PortAssignment.unique()); + } + + @After + public void teardown() throws Exception { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + System.clearProperty(ZKClientConfig.SECURE_CLIENT); + System.clearProperty(clientX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(clientX509Util.getSslKeystorePasswdProperty()); + System.clearProperty(clientX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(clientX509Util.getSslTruststorePasswdProperty()); + System.clearProperty("zookeeper.admin.serverPort"); + clientX509Util.close(); + } + + /** + * Developers might use standalone mode (which is the default for one server). + * This test checks metrics for authz failure in standalone server + */ + @Test + public void testSecureStandaloneServerAuthNFailure() throws Exception { + final Integer CLIENT_PORT = PortAssignment.unique(); + final Integer SECURE_CLIENT_PORT = PortAssignment.unique(); + + ZooKeeperServerMainTest.MainThread mt = new ZooKeeperServerMainTest.MainThread(CLIENT_PORT, SECURE_CLIENT_PORT, true, null); + mt.start(); + assertTrue( + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + + try { + ZooKeeper zk = createZKClnt("127.0.0.1:" + SECURE_CLIENT_PORT); + fail("should not be reached"); + } catch (Exception e){ + //Expected + } + ServerStats serverStats = mt.getSecureCnxnFactory().getZooKeeperServer().serverStats(); + assertTrue(serverStats.getAuthFailedCount() >= 1); + mt.shutdown(); + + } + + private ZooKeeper createZKClnt(String cxnString) throws Exception { + ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(cxnString, TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + return zk; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperCriticalThreadMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperCriticalThreadMetricsTest.java new file mode 100644 index 0000000..243e177 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperCriticalThreadMetricsTest.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import java.nio.ByteBuffer; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.junit.Test; + +public class ZooKeeperCriticalThreadMetricsTest extends ZKTestCase { + + CountDownLatch processed; + + private class MyRequestProcessor implements RequestProcessor { + + @Override + public void processRequest(Request request) throws RequestProcessorException { + // use this dummy request processor to trigger a unrecoverable ex + throw new RequestProcessorException("test", new Exception()); + } + + @Override + public void shutdown() { + } + + } + + private class MyPrepRequestProcessor extends PrepRequestProcessor { + + public MyPrepRequestProcessor() { + super(new ZooKeeperServer(), new MyRequestProcessor()); + } + + @Override + public void run() { + super.run(); + processed.countDown(); + } + + } + + @Test + public void testUnrecoverableErrorCountFromRequestProcessor() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + processed = new CountDownLatch(1); + PrepRequestProcessor processor = new MyPrepRequestProcessor(); + processor.start(); + + processor.processRequest(new Request(null, 1L, 1, ZooDefs.OpCode.setData, ByteBuffer.wrap(new byte[10]), null)); + processed.await(); + + processor.shutdown(); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("unrecoverable_error_count")); + } + + @Test + public void testUnrecoverableErrorCount() { + ServerMetrics.getMetrics().resetAll(); + + ZooKeeperServer zks = new ZooKeeperServer(); + ZooKeeperCriticalThread thread = new ZooKeeperCriticalThread("test", zks.getZooKeeperServerListener()); + + thread.handleException("test", new Exception()); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("unrecoverable_error_count")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerBeanTest.java new file mode 100644 index 0000000..a8db9e2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerBeanTest.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import org.apache.jute.Record; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ZooKeeperServerBeanTest { + + @Before + public void setup() { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + } + + @After + public void teardown() throws Exception { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } + + @Test + public void testTxnLogElapsedSyncTime() throws IOException { + + File tmpDir = ClientBase.createEmptyTestDir(); + FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog")); + + ZooKeeperServer zks = new ZooKeeperServer(); + zks.setTxnLogFactory(fileTxnSnapLog); + + ZooKeeperServerBean serverBean = new ZooKeeperServerBean(zks); + long elapsedTime = serverBean.getTxnLogElapsedSyncTime(); + assertEquals(-1, elapsedTime); + + TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData); + Record txn = new SetDataTxn("/foo", new byte[0], 1); + Request req = new Request(0, 0, 0, hdr, txn, 0); + + try { + + zks.getTxnLogFactory().append(req); + zks.getTxnLogFactory().commit(); + elapsedTime = serverBean.getTxnLogElapsedSyncTime(); + + assertNotEquals(-1, elapsedTime); + + assertEquals(elapsedTime, serverBean.getTxnLogElapsedSyncTime()); + + } finally { + fileTxnSnapLog.close(); + } + } + + @Test + public void testGetSecureClientPort() throws IOException { + ZooKeeperServer zks = new ZooKeeperServer(); + /** + * case 1: When secure client is not configured GetSecureClientPort + * should return empty string + */ + ZooKeeperServerBean serverBean = new ZooKeeperServerBean(zks); + String result = serverBean.getSecureClientPort(); + assertEquals("", result); + + /** + * case 2: When secure client is configured GetSecureClientPort should + * return configured port + */ + + ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); + int secureClientPort = 8443; + InetSocketAddress address = new InetSocketAddress(secureClientPort); + cnxnFactory.configure(address, 5, -1, true); + zks.setSecureServerCnxnFactory(cnxnFactory); + + result = serverBean.getSecureClientPort(); + assertEquals(Integer.toString(secureClientPort), result); + + // cleanup + cnxnFactory.shutdown(); + + } + + @Test + public void testGetSecureClientAddress() throws IOException { + ZooKeeperServer zks = new ZooKeeperServer(); + /** + * case 1: When secure client is not configured getSecureClientAddress + * should return empty string + */ + ZooKeeperServerBean serverBean = new ZooKeeperServerBean(zks); + String result = serverBean.getSecureClientPort(); + assertEquals("", result); + + /** + * case 2: When secure client is configured getSecureClientAddress + * should return configured SecureClientAddress + */ + + ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); + int secureClientPort = 8443; + InetSocketAddress address = new InetSocketAddress(secureClientPort); + cnxnFactory.configure(address, 5, -1, true); + zks.setSecureServerCnxnFactory(cnxnFactory); + + result = serverBean.getSecureClientAddress(); + String ipv4 = "0.0.0.0:" + secureClientPort; + String ipv6 = "0:0:0:0:0:0:0:0:" + secureClientPort; + assertTrue(result.equals(ipv4) || result.equals(ipv6)); + + // cleanup + cnxnFactory.shutdown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerConfTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerConfTest.java new file mode 100644 index 0000000..2f1c9e3 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerConfTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import java.util.Map; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class ZooKeeperServerConfTest extends ZKTestCase { + + private ZooKeeperServerConf c; + @Before + public void setUp() { + c = new ZooKeeperServerConf(1, "a", "b", 2, 3, 4, 5, 6L, 7); + } + @Test + public void testGetters() { + assertEquals(1, c.getClientPort()); + assertEquals("a", c.getDataDir()); + assertEquals("b", c.getDataLogDir()); + assertEquals(2, c.getTickTime()); + assertEquals(3, c.getMaxClientCnxnsPerHost()); + assertEquals(4, c.getMinSessionTimeout()); + assertEquals(5, c.getMaxSessionTimeout()); + assertEquals(6L, c.getServerId()); + assertEquals(7, c.getClientPortListenBacklog()); + } + @Test + public void testToMap() { + Map m = c.toMap(); + assertEquals(9, m.size()); + assertEquals(Integer.valueOf(1), m.get(ZooKeeperServerConf.KEY_CLIENT_PORT)); + assertEquals("a", m.get(ZooKeeperServerConf.KEY_DATA_DIR)); + assertEquals("b", m.get(ZooKeeperServerConf.KEY_DATA_LOG_DIR)); + assertEquals(Integer.valueOf(2), m.get(ZooKeeperServerConf.KEY_TICK_TIME)); + assertEquals(Integer.valueOf(3), m.get(ZooKeeperServerConf.KEY_MAX_CLIENT_CNXNS)); + assertEquals(Integer.valueOf(4), m.get(ZooKeeperServerConf.KEY_MIN_SESSION_TIMEOUT)); + assertEquals(Integer.valueOf(5), m.get(ZooKeeperServerConf.KEY_MAX_SESSION_TIMEOUT)); + assertEquals(Long.valueOf(6L), m.get(ZooKeeperServerConf.KEY_SERVER_ID)); + assertEquals(Integer.valueOf(7), m.get(ZooKeeperServerConf.KEY_CLIENT_PORT_LISTEN_BACKLOG)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerCreationTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerCreationTest.java new file mode 100644 index 0000000..670feaa --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerCreationTest.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.nio.ByteBuffer; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class ZooKeeperServerCreationTest { + + /** + * Test the default ZooKeeperServer and call processConnectRequest() to make sure + * that all needed fields are initialized properly, etc. + */ + @Test + public void testDefaultConstructor() throws Exception { + File tmpDir = ClientBase.createEmptyTestDir(); + FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog")); + + ZooKeeperServer zks = new ZooKeeperServer() { + @Override + public void submitRequest(Request si) { + // NOP + } + }; + zks.setTxnLogFactory(fileTxnSnapLog); + zks.setZKDatabase(new ZKDatabase(fileTxnSnapLog)); + zks.createSessionTracker(); + + ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); + ServerCnxn cnxn = new MockServerCnxn(); + + ConnectRequest connReq = new ConnectRequest(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + connReq.serialize(boa, "connect"); + zks.processConnectRequest(cnxn, ByteBuffer.wrap(baos.toByteArray())); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMainTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMainTest.java new file mode 100644 index 0000000..7def451 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMainTest.java @@ -0,0 +1,685 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider.MetricsProviderCapturingLifecycle; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider.MetricsProviderWithConfiguration; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider.MetricsProviderWithErrorInConfigure; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider.MetricsProviderWithErrorInStart; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider.MetricsProviderWithErrorInStop; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test stand-alone server. + * + */ +public class ZooKeeperServerMainTest extends ZKTestCase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(ZooKeeperServerMainTest.class); + + private CountDownLatch clientConnected = new CountDownLatch(1); + + public static class MainThread extends Thread { + + final File confFile; + final TestZKSMain main; + final File tmpDir; + final File dataDir; + final File logDir; + + public MainThread(int clientPort, boolean preCreateDirs, String configs) throws IOException { + this(clientPort, null, preCreateDirs, ClientBase.createTmpDir(), configs); + } + + public MainThread(int clientPort, Integer secureClientPort, boolean preCreateDirs, String configs) + throws IOException { + this(clientPort, secureClientPort, + preCreateDirs, ClientBase.createTmpDir(), configs); + } + + public MainThread(int clientPort, Integer secureClientPort, boolean preCreateDirs, File tmpDir, String configs) throws IOException { + super("Standalone server with clientPort:" + clientPort); + this.tmpDir = tmpDir; + confFile = new File(tmpDir, "zoo.cfg"); + + FileWriter fwriter = new FileWriter(confFile); + fwriter.write("tickTime=2000\n"); + fwriter.write("initLimit=10\n"); + fwriter.write("syncLimit=5\n"); + if (configs != null) { + fwriter.write(configs); + } + + dataDir = new File(this.tmpDir, "data"); + logDir = new File(dataDir.toString() + "_txnlog"); + if (preCreateDirs) { + if (!dataDir.mkdir()) { + throw new IOException("unable to mkdir " + dataDir); + } + if (!logDir.mkdir()) { + throw new IOException("unable to mkdir " + logDir); + } + ClientBase.createInitializeFile(logDir); + } + + String normalizedDataDir = PathUtils.normalizeFileSystemPath(dataDir.toString()); + String normalizedLogDir = PathUtils.normalizeFileSystemPath(logDir.toString()); + fwriter.write("dataDir=" + normalizedDataDir + "\n"); + fwriter.write("dataLogDir=" + normalizedLogDir + "\n"); + fwriter.write("clientPort=" + clientPort + "\n"); + + if (secureClientPort != null) { + fwriter.write("secureClientPort=" + secureClientPort + "\n"); + } + fwriter.flush(); + fwriter.close(); + + main = new TestZKSMain(); + } + + public void run() { + String[] args = new String[1]; + args[0] = confFile.toString(); + try { + main.initializeAndRun(args); + } catch (Exception e) { + // test will still fail even though we just log/ignore + LOG.error("unexpected exception in run", e); + } + } + + public void shutdown() throws IOException { + main.shutdown(); + } + + void deleteDirs() throws IOException { + delete(tmpDir); + } + + void delete(File f) throws IOException { + if (f.isDirectory()) { + for (File c : f.listFiles()) { + delete(c); + } + } + if (!f.delete()) { + // double check for the file existence + if (f.exists()) { + throw new IOException("Failed to delete file: " + f); + } + } + } + + ServerCnxnFactory getCnxnFactory() { + return main.getCnxnFactory(); + } + + public ServerCnxnFactory getSecureCnxnFactory(){ + return main.getSecureCnxnFactory(); + } + + } + + public static class TestZKSMain extends ZooKeeperServerMain { + + public void shutdown() { + super.shutdown(); + } + + } + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247. + * Test to verify that even after non recoverable error (error while + * writing transaction log), ZooKeeper is still available. + */ + @Test(timeout = 30000) + public void testNonRecoverableError() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + + MainThread main = new MainThread(CLIENT_PORT, true, null); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); + + zk.create("/foo1", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo1", null, null)), "foobar"); + + // inject problem in server + ZooKeeperServer zooKeeperServer = main.getCnxnFactory().getZooKeeperServer(); + FileTxnSnapLog snapLog = zooKeeperServer.getTxnLogFactory(); + FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) { + @Override + public void commit() throws IOException { + throw new IOException("Input/output error"); + } + }; + ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError); + zooKeeperServer.setZKDatabase(newDB); + + try { + // do create operation, so that injected IOException is thrown + zk.create("/foo2", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("IOException is expected as error is injected in transaction log commit funtionality"); + } catch (Exception e) { + // do nothing + } + zk.close(); + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + fileTxnSnapLogWithError.close(); + main.shutdown(); + main.deleteDirs(); + } + + /** + * Tests that the ZooKeeper server will fail to start if the + * snapshot directory is read only. + * + * This test will fail if it is executed as root user. + */ + @Test(timeout = 30000) + public void testReadOnlySnapshotDir() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT = PortAssignment.unique(); + + // Start up the ZK server to automatically create the necessary directories + // and capture the directory where data is stored + MainThread main = new MainThread(CLIENT_PORT, true, null); + File tmpDir = main.tmpDir; + main.start(); + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); + main.shutdown(); + + // Make the snapshot directory read only + File snapDir = new File(main.dataDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + snapDir.setWritable(false); + + // Restart ZK and observe a failure + main = new MainThread(CLIENT_PORT, null, false, tmpDir, null); + main.start(); + + assertFalse( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); + + main.shutdown(); + + snapDir.setWritable(true); + + main.deleteDirs(); + } + + /** + * Tests that the ZooKeeper server will fail to start if the + * transaction log directory is read only. + * + * This test will fail if it is executed as root user. + */ + @Test(timeout = 30000) + public void testReadOnlyTxnLogDir() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT = PortAssignment.unique(); + + // Start up the ZK server to automatically create the necessary directories + // and capture the directory where data is stored + MainThread main = new MainThread(CLIENT_PORT, true, null); + File tmpDir = main.tmpDir; + main.start(); + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); + main.shutdown(); + + // Make the transaction log directory read only + File logDir = new File(main.logDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + logDir.setWritable(false); + + // Restart ZK and observe a failure + main = new MainThread(CLIENT_PORT, null, false, tmpDir, null); + main.start(); + + assertFalse( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT / 2)); + + main.shutdown(); + + logDir.setWritable(true); + + main.deleteDirs(); + } + + /** + * Verify the ability to start a standalone server instance. + */ + @Test + public void testStandalone() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + + MainThread main = new MainThread(CLIENT_PORT, true, null); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + + clientConnected = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); + assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); + zk.close(); + + main.shutdown(); + main.join(); + main.deleteDirs(); + + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + } + + /** + * Test verifies that the server shouldn't allow minsessiontimeout greater than + * maxsessiontimeout + */ + @Test + public void testWithMinSessionTimeoutGreaterThanMaxSessionTimeout() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final int tickTime = 2000; + final int minSessionTimeout = 20 * tickTime + 1000; // min is higher + final int maxSessionTimeout = tickTime * 2 - 100; // max is lower + final String configs = "maxSessionTimeout=" + + maxSessionTimeout + + "\n" + + "minSessionTimeout=" + + minSessionTimeout + + "\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + String[] args = new String[1]; + args[0] = main.confFile.toString(); + try { + main.main.initializeAndRun(args); + fail("Must throw exception as " + "minsessiontimeout > maxsessiontimeout"); + } catch (ConfigException iae) { + // expected + } + } + + /** + * Test verifies that the server shouldn't boot with an invalid metrics provider + */ + @Test + public void testInvalidMetricsProvider() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final String configs = "metricsProvider.className=BadClass\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + String[] args = new String[1]; + args[0] = main.confFile.toString(); + try { + main.main.initializeAndRun(args); + fail("Must throw exception as metrics provider is not " + "well configured"); + } catch (ConfigException iae) { + // expected + } + } + + /** + * Test verifies that the server shouldn't boot with a faulty metrics provider + */ + @Test + public void testFaultyMetricsProviderOnStart() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final String configs = "metricsProvider.className=" + MetricsProviderWithErrorInStart.class.getName() + "\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + String[] args = new String[1]; + args[0] = main.confFile.toString(); + try { + main.main.initializeAndRun(args); + fail("Must throw exception as metrics provider cannot boot"); + } catch (IOException iae) { + // expected + } + } + + /** + * Test verifies that the server shouldn't boot with a faulty metrics provider + */ + @Test + public void testFaultyMetricsProviderOnConfigure() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final String configs = "metricsProvider.className=" + + MetricsProviderWithErrorInConfigure.class.getName() + + "\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + String[] args = new String[1]; + args[0] = main.confFile.toString(); + try { + main.main.initializeAndRun(args); + fail("Must throw exception as metrics provider is cannot boot"); + } catch (IOException iae) { + // expected + } + } + + /** + * Test verifies that the server shouldn't be affected but runtime errors on stop() + */ + @Test + public void testFaultyMetricsProviderOnStop() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + MetricsProviderWithErrorInStop.stopCalled.set(false); + final String configs = "metricsProvider.className=" + MetricsProviderWithErrorInStop.class.getName() + "\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + + clientConnected = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); + assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); + zk.close(); + + main.shutdown(); + main.join(); + main.deleteDirs(); + + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + assertTrue(MetricsProviderWithErrorInStop.stopCalled.get()); + } + + /** + * Test verifies that configuration is passed to the MetricsProvider. + */ + @Test + public void testMetricsProviderConfiguration() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + MetricsProviderWithConfiguration.httpPort.set(0); + final String configs = "metricsProvider.className=" + + MetricsProviderWithConfiguration.class.getName() + + "\n" + + "metricsProvider.httpPort=1234\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + + clientConnected = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); + assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); + zk.close(); + + main.shutdown(); + main.join(); + main.deleteDirs(); + + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + assertEquals(1234, MetricsProviderWithConfiguration.httpPort.get()); + } + + /** + * Test verifies that all of the lifecycle methods of the MetricsProvider are called. + */ + @Test + public void testMetricsProviderLifecycle() throws Exception { + ClientBase.setupTestEnv(); + MetricsProviderCapturingLifecycle.reset(); + + final int CLIENT_PORT = PortAssignment.unique(); + final String configs = "metricsProvider.className=" + + MetricsProviderCapturingLifecycle.class.getName() + + "\n" + + "metricsProvider.httpPort=1234\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + + clientConnected = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT, this); + assertTrue("Failed to establish zkclient connection!", clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + + zk.create("/foo", "foobar".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo", null, null)), "foobar"); + zk.close(); + + main.shutdown(); + main.join(); + main.deleteDirs(); + + assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.configureCalled.get()); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.startCalled.get()); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.getRootContextCalled.get()); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.stopCalled.get()); + } + + /** + * Test verifies that the server is able to redefine if user configured only + * minSessionTimeout limit + */ + @Test + public void testWithOnlyMinSessionTimeout() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final int tickTime = 2000; + final int minSessionTimeout = tickTime * 2 - 100; + int maxSessionTimeout = 20 * tickTime; + final String configs = "minSessionTimeout=" + minSessionTimeout + "\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + main.start(); + + String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + // create session with min value + verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT); + verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout, HOSTPORT); + // create session with max value + verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT); + verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout, HOSTPORT); + main.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + } + + /** + * Test verifies that the server is able to redefine the min/max session + * timeouts + */ + @Test + public void testMinMaxSessionTimeOut() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final int tickTime = 2000; + final int minSessionTimeout = tickTime * 2 - 100; + final int maxSessionTimeout = 20 * tickTime + 1000; + final String configs = "maxSessionTimeout=" + + maxSessionTimeout + + "\n" + + "minSessionTimeout=" + + minSessionTimeout + + "\n"; + MainThread main = new MainThread(CLIENT_PORT, true, configs); + main.start(); + + String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + // create session with min value + verifySessionTimeOut(minSessionTimeout, minSessionTimeout, HOSTPORT); + verifySessionTimeOut(minSessionTimeout - 2000, minSessionTimeout, HOSTPORT); + // create session with max value + verifySessionTimeOut(maxSessionTimeout, maxSessionTimeout, HOSTPORT); + verifySessionTimeOut(maxSessionTimeout + 2000, maxSessionTimeout, HOSTPORT); + main.shutdown(); + + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + } + + private void verifySessionTimeOut(int sessionTimeout, int expectedSessionTimeout, String HOSTPORT) throws IOException, KeeperException, InterruptedException { + clientConnected = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper(HOSTPORT, sessionTimeout, this); + assertTrue("Failed to establish zkclient connection!", clientConnected.await(sessionTimeout, TimeUnit.MILLISECONDS)); + assertEquals("Not able to configure the sessionTimeout values", expectedSessionTimeout, zk.getSessionTimeout()); + zk.close(); + } + + @Test + public void testJMXRegistrationWithNIO() throws Exception { + ClientBase.setupTestEnv(); + File tmpDir_1 = ClientBase.createTmpDir(); + ServerCnxnFactory server_1 = startServer(tmpDir_1); + File tmpDir_2 = ClientBase.createTmpDir(); + ServerCnxnFactory server_2 = startServer(tmpDir_2); + + server_1.shutdown(); + server_2.shutdown(); + + deleteFile(tmpDir_1); + deleteFile(tmpDir_2); + } + + @Test + public void testJMXRegistrationWithNetty() throws Exception { + String originalServerCnxnFactory = System.getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName()); + try { + ClientBase.setupTestEnv(); + File tmpDir_1 = ClientBase.createTmpDir(); + ServerCnxnFactory server_1 = startServer(tmpDir_1); + File tmpDir_2 = ClientBase.createTmpDir(); + ServerCnxnFactory server_2 = startServer(tmpDir_2); + + server_1.shutdown(); + server_2.shutdown(); + + deleteFile(tmpDir_1); + deleteFile(tmpDir_2); + } finally { + // setting back + if (originalServerCnxnFactory == null || originalServerCnxnFactory.isEmpty()) { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } else { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, originalServerCnxnFactory); + } + } + } + + private void deleteFile(File f) throws IOException { + if (f.isDirectory()) { + for (File c : f.listFiles()) { + deleteFile(c); + } + } + if (!f.delete()) { + // double check for the file existence + + if (f.exists()) { + throw new IOException("Failed to delete file: " + f); + } + } + } + + private ServerCnxnFactory startServer(File tmpDir) throws IOException, InterruptedException { + final int CLIENT_PORT = PortAssignment.unique(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1); + f.startup(zks); + assertNotNull("JMX initialization failed!", zks.jmxServerBean); + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, CONNECTION_TIMEOUT)); + return f; + } + + public void process(WatchedEvent event) { + if (event.getState() == KeeperState.SyncConnected) { + clientConnected.countDown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java new file mode 100644 index 0000000..48f5b99 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerMaxCnxnsTest.java @@ -0,0 +1,177 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZooKeeperServerMaxCnxnsTest extends QuorumPeerTestBase { + + protected static final Logger LOG = LoggerFactory.getLogger(ZooKeeperServerMaxCnxnsTest.class); + private static int SERVER_COUNT = 3; + private MainThread[] mt; + private ZooKeeper[] clients; + + /** + *

      +     * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2238.
      +     * Support limiting the maximum number of connections/clients to a ZooKeeper server.
      +     * 
      + */ + + @Test(timeout = 120000) + public void testMaxZooKeeperClientsWithNIOServerCnxnFactory() throws Exception { + String serverCnxnFactory = "org.apache.zookeeper.server.NIOServerCnxnFactory"; + testMaxZooKeeperClients(serverCnxnFactory); + } + + @Test(timeout = 120000) + public void testMaxZooKeeperClientsWithNettyServerCnxnFactory() throws Exception { + String serverCnxnFactory = "org.apache.zookeeper.server.NettyServerCnxnFactory"; + testMaxZooKeeperClients(serverCnxnFactory); + } + + private void testMaxZooKeeperClients(String serverCnxnFactory) throws Exception { + final int clientPorts[] = new int[SERVER_COUNT]; + int maxCnxns = 2; + StringBuilder sb = new StringBuilder(); + sb.append("maxCnxns=" + maxCnxns + "\n"); + sb.append("serverCnxnFactory=" + serverCnxnFactory + "\n"); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread mt[] = new MainThread[SERVER_COUNT]; + + // start 3 servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // ensure all servers started + for (int i = 0; i < SERVER_COUNT; i++) { + Assert.assertTrue("waiting for server " + i + " being up", ClientBase + .waitForServerUp("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT)); + } + + int maxAllowedConnection = maxCnxns * SERVER_COUNT; + String cxnString = getCxnString(clientPorts); + + final CountDownLatch countDownLatch = new CountDownLatch(maxAllowedConnection); + ZooKeeper[] clients = new ZooKeeper[maxAllowedConnection]; + Watcher watcher = new Watcher() { + + @Override + public void process(WatchedEvent event) { + if (event.getState() == Event.KeeperState.SyncConnected) { + countDownLatch.countDown(); + } + } + }; + for (int i = 0; i < maxAllowedConnection; i++) { + clients[i] = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, watcher); + Thread.sleep(100); + } + countDownLatch.await(); + // reaching this point indicates that all maxAllowedConnection connected + + // No more client to be allowed to connect now as we have reached the + // max connections + CountdownWatcher cdw = new CountdownWatcher(); + ZooKeeper extraClient = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, cdw); + try { + cdw.waitForConnected(ClientBase.CONNECTION_TIMEOUT / 2); + fail("Client is not supposed to get connected as max connection already reached."); + } catch (TimeoutException e) { + extraClient.close(); + } + + // lets close one already connected client + clients[0].close(); + + // Now extra client must automatically get connected + cdw = new CountdownWatcher(); + extraClient = new ZooKeeper(cxnString, ClientBase.CONNECTION_TIMEOUT, cdw); + cdw.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + // verify some basic operation + String create = extraClient.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT); + assertEquals("/test", create); + + // cleanup + extraClient.close(); + } + + private String getCxnString(int[] clientPorts) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < clientPorts.length; i++) { + builder.append("127.0.0.1:" + clientPorts[i]); + if (i != clientPorts.length - 1) { + builder.append(","); + } + } + return builder.toString(); + } + + @After + public void tearDown() { + // stop all clients + if (clients != null) { + for (ZooKeeper zooKeeper : clients) { + try { + zooKeeper.close(); + } catch (InterruptedException e) { + LOG.warn("ZooKeeper interrupted while closing it.", e); + } + } + } + // stop all severs + if (mt != null) { + for (int i = 0; i < SERVER_COUNT; i++) { + try { + mt[i].shutdown(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted while shutting it down", e); + } + } + } + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerStartupTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerStartupTest.java new file mode 100644 index 0000000..b9c21e4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerStartupTest.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import static org.apache.zookeeper.server.command.AbstractFourLetterCommand.ZK_NOT_SERVING; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class tests the startup behavior of ZooKeeper server. + */ +public class ZooKeeperServerStartupTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ZooKeeperServerStartupTest.class); + private static int PORT = PortAssignment.unique(); + private static String HOST = "127.0.0.1"; + private static String HOSTPORT = HOST + ":" + PORT; + + private ServerCnxnFactory servcnxnf; + private ZooKeeperServer zks; + private File tmpDir; + private CountDownLatch startupDelayLatch = new CountDownLatch(1); + + @After + public void teardown() throws Exception { + // count down to avoid infinite blocking call due to this latch, if + // any. + startupDelayLatch.countDown(); + + if (servcnxnf != null) { + servcnxnf.shutdown(); + } + if (zks != null) { + zks.shutdown(); + } + if (zks.getZKDatabase() != null) { + zks.getZKDatabase().close(); + } + ClientBase.recursiveDelete(tmpDir); + } + + /** + * Test case for + * https://issues.apache.org/jira/browse/ZOOKEEPER-2383 + */ + @Test(timeout = 30000) + public void testClientConnectionRequestDuringStartupWithNIOServerCnxn() throws Exception { + tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + + startSimpleZKServer(startupDelayLatch); + SimpleZooKeeperServer simplezks = (SimpleZooKeeperServer) zks; + assertTrue("Failed to invoke zks#startup() method during server startup", simplezks.waitForStartupInvocation(10)); + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zkClient = new ZooKeeper(HOSTPORT, ClientBase.CONNECTION_TIMEOUT, watcher); + + assertFalse("Since server is not fully started, zks#createSession() shouldn't be invoked", simplezks.waitForSessionCreation(5)); + + LOG.info("Decrements the count of the latch, so that server will proceed with startup"); + startupDelayLatch.countDown(); + + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + assertTrue("Failed to invoke zks#createSession() method during client session creation", simplezks.waitForSessionCreation(5)); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zkClient.close(); + } + + /** + * Test case for + * https://issues.apache.org/jira/browse/ZOOKEEPER-2383 + */ + @Test(timeout = 30000) + public void testClientConnectionRequestDuringStartupWithNettyServerCnxn() throws Exception { + tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + + String originalServerCnxnFactory = System.getProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + try { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName()); + startSimpleZKServer(startupDelayLatch); + SimpleZooKeeperServer simplezks = (SimpleZooKeeperServer) zks; + assertTrue("Failed to invoke zks#startup() method during server startup", simplezks.waitForStartupInvocation(10)); + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zkClient = new ZooKeeper(HOSTPORT, ClientBase.CONNECTION_TIMEOUT, watcher); + + assertFalse("Since server is not fully started, zks#createSession() shouldn't be invoked", simplezks.waitForSessionCreation(5)); + + LOG.info("Decrements the count of the latch, so that server will proceed with startup"); + startupDelayLatch.countDown(); + + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + assertTrue("Failed to invoke zks#createSession() method during client session creation", simplezks.waitForSessionCreation(5)); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zkClient.close(); + } finally { + // reset cnxn factory + if (originalServerCnxnFactory == null) { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + return; + } + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, originalServerCnxnFactory); + } + } + + /** + * Test case for + * https://issues.apache.org/jira/browse/ZOOKEEPER-2383 + */ + @Test(timeout = 30000) + public void testFourLetterWords() throws Exception { + startSimpleZKServer(startupDelayLatch); + verify("conf", ZK_NOT_SERVING); + verify("crst", ZK_NOT_SERVING); + verify("cons", ZK_NOT_SERVING); + verify("dirs", ZK_NOT_SERVING); + verify("dump", ZK_NOT_SERVING); + verify("mntr", ZK_NOT_SERVING); + verify("stat", ZK_NOT_SERVING); + verify("srst", ZK_NOT_SERVING); + verify("wchp", ZK_NOT_SERVING); + verify("wchc", ZK_NOT_SERVING); + verify("wchs", ZK_NOT_SERVING); + verify("isro", "null"); + } + + private void verify(String cmd, String expected) throws IOException, SSLContextException { + String resp = sendRequest(cmd); + LOG.info("cmd {} expected {} got {}", cmd, expected, resp); + assertTrue("Unexpected response", resp.contains(expected)); + } + + private String sendRequest(String cmd) throws IOException, SSLContextException { + return send4LetterWord(HOST, PORT, cmd); + } + + private void startSimpleZKServer(CountDownLatch startupDelayLatch) throws IOException { + zks = new SimpleZooKeeperServer(tmpDir, tmpDir, 3000, startupDelayLatch); + SyncRequestProcessor.setSnapCount(100); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + + servcnxnf = ServerCnxnFactory.createFactory(PORT, -1); + Thread startupThread = new Thread() { + public void run() { + try { + servcnxnf.startup(zks); + } catch (IOException e) { + LOG.error("Unexcepted exception during server startup", e); + // Ignoring exception. If there is an ioexception + // then one of the following assertion will fail + } catch (InterruptedException e) { + LOG.error("Unexcepted exception during server startup", e); + // Ignoring exception. If there is an interrupted exception + // then one of the following assertion will fail + } + } + }; + LOG.info("Starting zk server {}", HOSTPORT); + startupThread.start(); + } + + private static class SimpleZooKeeperServer extends ZooKeeperServer { + + private CountDownLatch startupDelayLatch; + private CountDownLatch startupInvokedLatch = new CountDownLatch(1); + private CountDownLatch createSessionInvokedLatch = new CountDownLatch(1); + + public SimpleZooKeeperServer(File snapDir, File logDir, int tickTime, CountDownLatch startupDelayLatch) throws IOException { + super(snapDir, logDir, tickTime); + this.startupDelayLatch = startupDelayLatch; + } + + @Override + public synchronized void startup() { + try { + startupInvokedLatch.countDown(); + // Delaying the zk server startup so that + // ZooKeeperServer#sessionTracker reference won't be + // initialized. In the defect scenario, while processing the + // connection request zkServer needs sessionTracker reference, + // but this is not yet initialized and the server is still in + // the startup phase, resulting in NPE. + startupDelayLatch.await(); + } catch (InterruptedException e) { + fail("Unexpected InterruptedException while startinng up!"); + } + super.startup(); + } + + @Override + long createSession(ServerCnxn cnxn, byte[] passwd, int timeout) { + createSessionInvokedLatch.countDown(); + return super.createSession(cnxn, passwd, timeout); + } + + boolean waitForStartupInvocation(long timeout) throws InterruptedException { + return startupInvokedLatch.await(timeout, TimeUnit.SECONDS); + } + + boolean waitForSessionCreation(long timeout) throws InterruptedException { + return createSessionInvokedLatch.await(timeout, TimeUnit.SECONDS); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java new file mode 100644 index 0000000..81469a8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.util.List; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.SnapStream; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class ZooKeeperServerTest extends ZKTestCase { + + @Test + public void testSortDataDirAscending() { + File[] files = new File[5]; + + files[0] = new File("foo.10027c6de"); + files[1] = new File("foo.10027c6df"); + files[2] = new File("bar.10027c6dd"); + files[3] = new File("foo.10027c6dc"); + files[4] = new File("foo.20027c6dc"); + + File[] orig = files.clone(); + + List filelist = Util.sortDataDir(files, "foo", true); + + assertEquals(orig[2], filelist.get(0)); + assertEquals(orig[3], filelist.get(1)); + assertEquals(orig[0], filelist.get(2)); + assertEquals(orig[1], filelist.get(3)); + assertEquals(orig[4], filelist.get(4)); + } + + @Test + public void testSortDataDirDescending() { + File[] files = new File[5]; + + files[0] = new File("foo.10027c6de"); + files[1] = new File("foo.10027c6df"); + files[2] = new File("bar.10027c6dd"); + files[3] = new File("foo.10027c6dc"); + files[4] = new File("foo.20027c6dc"); + + File[] orig = files.clone(); + + List filelist = Util.sortDataDir(files, "foo", false); + + assertEquals(orig[4], filelist.get(0)); + assertEquals(orig[1], filelist.get(1)); + assertEquals(orig[0], filelist.get(2)); + assertEquals(orig[3], filelist.get(3)); + assertEquals(orig[2], filelist.get(4)); + } + + @Test + public void testGetLogFiles() { + File[] files = new File[5]; + + files[0] = new File("log.10027c6de"); + files[1] = new File("log.10027c6df"); + files[2] = new File("snapshot.10027c6dd"); + files[3] = new File("log.10027c6dc"); + files[4] = new File("log.20027c6dc"); + + File[] orig = files.clone(); + + File[] filelist = FileTxnLog.getLogFiles(files, Long.parseLong("10027c6de", 16)); + + assertEquals(3, filelist.length); + assertEquals(orig[0], filelist[0]); + assertEquals(orig[1], filelist[1]); + assertEquals(orig[4], filelist[2]); + } + + @Test + public void testForceSyncDefaultEnabled() { + File file = new File("foo.10027c6de"); + FileTxnLog log = new FileTxnLog(file); + assertTrue(log.isForceSync()); + } + + @Test + public void testForceSyncDefaultDisabled() { + try { + File file = new File("foo.10027c6de"); + System.setProperty("zookeeper.forceSync", "no"); + FileTxnLog log = new FileTxnLog(file); + assertFalse(log.isForceSync()); + } finally { + //Reset back to default. + System.setProperty("zookeeper.forceSync", "yes"); + } + } + + @Test + public void testInvalidSnapshot() { + File f = null; + File tmpFileDir = null; + try { + tmpFileDir = ClientBase.createTmpDir(); + f = new File(tmpFileDir, "snapshot.0"); + if (!f.exists()) { + f.createNewFile(); + } + assertFalse("Snapshot file size is greater than 9 bytes", SnapStream.isValidSnapshot(f)); + assertTrue("Can't delete file", f.delete()); + } catch (IOException e) { + } finally { + if (null != tmpFileDir) { + ClientBase.recursiveDelete(tmpFileDir); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperThreadTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperThreadTest.java new file mode 100644 index 0000000..fe53f4d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperThreadTest.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertTrue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class ZooKeeperThreadTest extends ZKTestCase { + + private CountDownLatch runningLatch = new CountDownLatch(1); + + public class MyThread extends ZooKeeperThread { + + public MyThread(String threadName) { + super(threadName); + } + + public void run() { + throw new Error(); + } + + @Override + protected void handleException(String thName, Throwable e) { + runningLatch.countDown(); + } + + } + + public class MyCriticalThread extends ZooKeeperCriticalThread { + + public MyCriticalThread(String threadName) { + super(threadName, new ZooKeeperServerListener() { + + @Override + public void notifyStopping(String threadName, int erroCode) { + + } + }); + } + + public void run() { + throw new Error(); + } + + @Override + protected void handleException(String thName, Throwable e) { + runningLatch.countDown(); + } + + } + + /** + * Test verifies uncaught exception handling of ZooKeeperThread + */ + @Test(timeout = 30000) + public void testUncaughtException() throws Exception { + MyThread t1 = new MyThread("Test-Thread"); + t1.start(); + assertTrue("Uncaught exception is not properly handled.", runningLatch.await(10000, TimeUnit.MILLISECONDS)); + + runningLatch = new CountDownLatch(1); + MyCriticalThread t2 = new MyCriticalThread("Test-Critical-Thread"); + t2.start(); + assertTrue("Uncaught exception is not properly handled.", runningLatch.await(10000, TimeUnit.MILLISECONDS)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZxidRolloverTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZxidRolloverTest.java new file mode 100644 index 0000000..42e366e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/ZxidRolloverTest.java @@ -0,0 +1,451 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.ConnectionLossException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.apache.zookeeper.test.ClientTest; +import org.apache.zookeeper.test.QuorumUtil; +import org.apache.zookeeper.test.QuorumUtil.PeerStruct; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Verify ZOOKEEPER-1277 - ensure that we handle epoch rollover correctly. + */ +public class ZxidRolloverTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ZxidRolloverTest.class); + + private QuorumUtil qu; + private ZooKeeperServer zksLeader; + private ZooKeeper[] zkClients = new ZooKeeper[3]; + private CountdownWatcher[] zkClientWatchers = new CountdownWatcher[3]; + private int idxLeader; + private int idxFollower; + + private ZooKeeper getClient(int idx) { + return zkClients[idx - 1]; + } + + @Before + public void setUp() throws Exception { + System.setProperty("zookeeper.admin.enableServer", "false"); + + // set the snap count to something low so that we force log rollover + // and verify that is working as part of the epoch rollover. + SyncRequestProcessor.setSnapCount(7); + + qu = new QuorumUtil(1); + startAll(); + + for (int i = 0; i < zkClients.length; i++) { + zkClientWatchers[i] = new CountdownWatcher(); + PeerStruct peer = qu.getPeer(i + 1); + zkClients[i] = new ZooKeeper( + "127.0.0.1:" + peer.clientPort, + ClientTest.CONNECTION_TIMEOUT, + zkClientWatchers[i]); + } + waitForClientsConnected(); + } + + private void waitForClientsConnected() throws Exception { + for (int i = 0; i < zkClients.length; i++) { + zkClientWatchers[i].waitForConnected(ClientTest.CONNECTION_TIMEOUT); + zkClientWatchers[i].reset(); + } + } + + /** + * Ensure all clients are able to talk to the service. + */ + private void checkClientsConnected() throws Exception { + for (int i = 0; i < zkClients.length; i++) { + checkClientConnected(i + 1); + } + } + + /** + * Ensure the client is able to talk to the server. + * + * @param idx the idx of the server the client is talking to + */ + private void checkClientConnected(int idx) throws Exception { + ZooKeeper zk = getClient(idx); + if (zk == null) { + return; + } + try { + assertNull(zk.exists("/foofoofoo-connected", false)); + } catch (ConnectionLossException e) { + // second chance... + // in some cases, leader change in particular, the timing is + // very tricky to get right in order to assure that the client has + // disconnected and reconnected. In some cases the client will + // disconnect, then attempt to reconnect before the server is + // back, in which case we'll see another connloss on the operation + // in the try, this catches that case and waits for the server + // to come back + PeerStruct peer = qu.getPeer(idx); + assertTrue( + "Waiting for server down", + ClientBase.waitForServerUp("127.0.0.1:" + peer.clientPort, ClientBase.CONNECTION_TIMEOUT)); + + assertNull(zk.exists("/foofoofoo-connected", false)); + } + } + + /** + * Ensure all clients are disconnected from the service. + */ + private void checkClientsDisconnected() throws Exception { + for (int i = 0; i < zkClients.length; i++) { + checkClientDisconnected(i + 1); + } + } + + /** + * Ensure the client is able to talk to the server + * + * @param idx the idx of the server the client is talking to + */ + private void checkClientDisconnected(int idx) throws Exception { + ZooKeeper zk = getClient(idx); + if (zk == null) { + return; + } + try { + assertNull(zk.exists("/foofoofoo-disconnected", false)); + fail("expected client to be disconnected"); + } catch (KeeperException e) { + // success + } + } + + private void startAll() throws Exception { + qu.startAll(); + checkLeader(); + // all clients should be connected + checkClientsConnected(); + } + private void start(int idx) throws Exception { + qu.start(idx); + for (String hp : qu.getConnString().split(",")) { + assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, ClientTest.CONNECTION_TIMEOUT)); + } + + checkLeader(); + // all clients should be connected + checkClientsConnected(); + } + + private void checkLeader() { + idxLeader = 1; + while (qu.getPeer(idxLeader).peer.leader == null) { + idxLeader++; + } + idxFollower = (idxLeader == 1 ? 2 : 1); + + zksLeader = qu.getPeer(idxLeader).peer.getActiveServer(); + } + + private void shutdownAll() throws Exception { + qu.shutdownAll(); + // all clients should be disconnected + checkClientsDisconnected(); + } + + private void shutdown(int idx) throws Exception { + qu.shutdown(idx); + + // leader will shutdown, remaining followers will elect a new leader + PeerStruct peer = qu.getPeer(idx); + assertTrue( + "Waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + peer.clientPort, ClientBase.CONNECTION_TIMEOUT)); + + // if idx is the the leader then everyone will get disconnected, + // otherwise if idx is a follower then just that client will get + // disconnected + if (idx == idxLeader) { + checkClientDisconnected(idx); + try { + checkClientsDisconnected(); + } catch (AssertionError e) { + // the clients may or may not have already reconnected + // to the recovered cluster, force a check, but ignore + } + } else { + checkClientDisconnected(idx); + } + } + + /** Reset the next zxid to be near epoch end */ + private void adjustEpochNearEnd() { + zksLeader.setZxid((zksLeader.getZxid() & 0xffffffff00000000L) | 0xfffffffcL); + } + + @After + public void tearDown() throws Exception { + LOG.info("tearDown starting"); + for (int i = 0; i < zkClients.length; i++) { + zkClients[i].close(); + } + qu.shutdownAll(); + } + + /** + * Create the znodes, this may fail if the lower 32 roll over, if so + * wait for the clients to be re-connected after the re-election + */ + private int createNodes(ZooKeeper zk, int start, int count) throws Exception { + LOG.info("Creating nodes {} thru {}", start, (start + count)); + int j = 0; + try { + for (int i = start; i < start + count; i++) { + zk.create("/foo" + i, new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.EPHEMERAL); + j++; + } + } catch (ConnectionLossException e) { + // this is ok - the leader has dropped leadership + waitForClientsConnected(); + } + return j; + } + /** + * Verify the expected znodes were created and that the last znode, which + * caused the roll-over, did not. + */ + private void checkNodes(ZooKeeper zk, int start, int count) throws Exception { + LOG.info("Validating nodes {} thru {}", start, (start + count)); + for (int i = start; i < start + count; i++) { + assertNotNull(zk.exists("/foo" + i, false)); + LOG.error("Exists zxid:{}", Long.toHexString(zk.exists("/foo" + i, false).getCzxid())); + } + assertNull(zk.exists("/foo" + (start + count), false)); + } + + /** + * Prior to the fix this test would hang for a while, then fail with + * connection loss. + */ + @Test + public void testSimpleRolloverFollower() throws Exception { + adjustEpochNearEnd(); + + ZooKeeper zk = getClient((idxLeader == 1 ? 2 : 1)); + int countCreated = createNodes(zk, 0, 10); + + checkNodes(zk, 0, countCreated); + } + + /** + * Similar to testSimpleRollover, but ensure the cluster comes back, + * has the right data, and is able to serve new requests. + */ + @Test + public void testRolloverThenRestart() throws Exception { + ZooKeeper zk = getClient(idxFollower); + + int countCreated = createNodes(zk, 0, 10); + + adjustEpochNearEnd(); + + countCreated += createNodes(zk, countCreated, 10); + + shutdownAll(); + startAll(); + zk = getClient(idxLeader); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + adjustEpochNearEnd(); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdownAll(); + startAll(); + zk = getClient(idxFollower); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdownAll(); + startAll(); + zk = getClient(idxLeader); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + // sanity check + assertTrue(countCreated > 0); + assertTrue(countCreated < 60); + } + + /** + * Similar to testRolloverThenRestart, but ensure a follower comes back, + * has the right data, and is able to serve new requests. + */ + @Test + public void testRolloverThenFollowerRestart() throws Exception { + ZooKeeper zk = getClient(idxFollower); + + int countCreated = createNodes(zk, 0, 10); + + adjustEpochNearEnd(); + + countCreated += createNodes(zk, countCreated, 10); + + shutdown(idxFollower); + start(idxFollower); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + adjustEpochNearEnd(); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdown(idxFollower); + start(idxFollower); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdown(idxFollower); + start(idxFollower); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + // sanity check + assertTrue(countCreated > 0); + assertTrue(countCreated < 60); + } + + /** + * Similar to testRolloverThenRestart, but ensure leadership can change, + * comes back, has the right data, and is able to serve new requests. + */ + @Test + public void testRolloverThenLeaderRestart() throws Exception { + ZooKeeper zk = getClient(idxLeader); + + int countCreated = createNodes(zk, 0, 10); + + adjustEpochNearEnd(); + + checkNodes(zk, 0, countCreated); + + shutdown(idxLeader); + start(idxLeader); + zk = getClient(idxLeader); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + adjustEpochNearEnd(); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdown(idxLeader); + start(idxLeader); + zk = getClient(idxLeader); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdown(idxLeader); + start(idxLeader); + zk = getClient(idxFollower); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + // sanity check + assertTrue(countCreated > 0); + assertTrue(countCreated < 50); + } + + /** + * Similar to testRolloverThenRestart, but ensure we can survive multiple + * epoch rollovers between restarts. + */ + @Test + public void testMultipleRollover() throws Exception { + ZooKeeper zk = getClient(idxFollower); + + int countCreated = createNodes(zk, 0, 10); + + adjustEpochNearEnd(); + + countCreated += createNodes(zk, countCreated, 10); + + adjustEpochNearEnd(); + + countCreated += createNodes(zk, countCreated, 10); + + adjustEpochNearEnd(); + + countCreated += createNodes(zk, countCreated, 10); + + adjustEpochNearEnd(); + + countCreated += createNodes(zk, countCreated, 10); + + shutdownAll(); + startAll(); + zk = getClient(idxFollower); + + adjustEpochNearEnd(); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + shutdown(idxLeader); + start(idxLeader); + zk = getClient(idxFollower); + + checkNodes(zk, 0, countCreated); + countCreated += createNodes(zk, countCreated, 10); + + // sanity check + assertTrue(countCreated > 0); + assertTrue(countCreated < 70); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandResponseTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandResponseTest.java new file mode 100644 index 0000000..20dc407 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandResponseTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.admin; + +import static org.junit.Assert.assertEquals; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class CommandResponseTest extends ZKTestCase { + + private CommandResponse r; + + @Before + public void setUp() throws Exception { + r = new CommandResponse("makemeasandwich", "makeityourself"); + } + + @Test + public void testGetters() { + assertEquals("makemeasandwich", r.getCommand()); + assertEquals("makeityourself", r.getError()); + } + + @Test + public void testMap() { + r.put("missing", "sudo"); + Map m = new HashMap(); + m.put("origin", "xkcd"); + m.put("url", "http://xkcd.com/149/"); + r.putAll(m); + + Map rmap = r.toMap(); + assertEquals(5, rmap.size()); + assertEquals("makemeasandwich", rmap.get(CommandResponse.KEY_COMMAND)); + assertEquals("makeityourself", rmap.get(CommandResponse.KEY_ERROR)); + assertEquals("sudo", rmap.get("missing")); + assertEquals("xkcd", rmap.get("origin")); + assertEquals("http://xkcd.com/149/", rmap.get("url")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandsTest.java new file mode 100644 index 0000000..eb8c300 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/CommandsTest.java @@ -0,0 +1,301 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.quorum.BufferStats; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class CommandsTest extends ClientBase { + + /** + * Checks that running a given Command returns the expected Map. Asserts + * that all specified keys are present with values of the specified types + * and that there are no extra entries. + * + * @param cmdName + * - the primary name of the command + * @param kwargs + * - keyword arguments to the command + * @param fields + * - the fields that are expected in the returned Map + * @throws IOException + * @throws InterruptedException + */ + public void testCommand(String cmdName, Map kwargs, Field... fields) throws IOException, InterruptedException { + ZooKeeperServer zks = serverFactory.getZooKeeperServer(); + Map result = Commands.runCommand(cmdName, zks, kwargs).toMap(); + + assertTrue(result.containsKey("command")); + // This is only true because we're setting cmdName to the primary name + assertEquals(cmdName, result.remove("command")); + assertTrue(result.containsKey("error")); + assertNull("error: " + result.get("error"), result.remove("error")); + + for (Field field : fields) { + String k = field.key; + assertTrue("Result from command " + + cmdName + + " missing field \"" + + k + + "\"" + + "\n" + + result, result.containsKey(k)); + Class t = field.type; + Object v = result.remove(k); + assertTrue("\"" + + k + + "\" field from command " + + cmdName + + " should be of type " + + t + + ", is actually of type " + + v.getClass(), t.isAssignableFrom(v.getClass())); + } + + assertTrue("Result from command " + cmdName + " contains extra fields: " + result, result.isEmpty()); + } + + public void testCommand(String cmdName, Field... fields) throws IOException, InterruptedException { + testCommand(cmdName, new HashMap(), fields); + } + + private static class Field { + + String key; + Class type; + Field(String key, Class type) { + this.key = key; + this.type = type; + } + + } + + @Test + public void testConfiguration() throws IOException, InterruptedException { + testCommand("configuration", new Field("client_port", Integer.class), new Field("data_dir", String.class), new Field("data_log_dir", String.class), new Field("tick_time", Integer.class), new Field("max_client_cnxns", Integer.class), new Field("min_session_timeout", Integer.class), new Field("max_session_timeout", Integer.class), new Field("server_id", Long.class), new Field("client_port_listen_backlog", Integer.class)); + } + + @Test + public void testConnections() throws IOException, InterruptedException { + testCommand("connections", new Field("connections", Iterable.class), new Field("secure_connections", Iterable.class)); + } + + @Test + public void testObservers() throws IOException, InterruptedException { + testCommand("observers", new Field("synced_observers", Integer.class), new Field("observers", Iterable.class)); + } + + @Test + public void testObserverConnectionStatReset() throws IOException, InterruptedException { + testCommand("observer_connection_stat_reset"); + } + + @Test + public void testConnectionStatReset() throws IOException, InterruptedException { + testCommand("connection_stat_reset"); + } + + @Test + public void testDump() throws IOException, InterruptedException { + testCommand("dump", new Field("expiry_time_to_session_ids", Map.class), new Field("session_id_to_ephemeral_paths", Map.class)); + } + + @Test + public void testEnvironment() throws IOException, InterruptedException { + testCommand("environment", new Field("zookeeper.version", String.class), new Field("host.name", String.class), new Field("java.version", String.class), new Field("java.vendor", String.class), new Field("java.home", String.class), new Field("java.class.path", String.class), new Field("java.library.path", String.class), new Field("java.io.tmpdir", String.class), new Field("java.compiler", String.class), new Field("os.name", String.class), new Field("os.arch", String.class), new Field("os.version", String.class), new Field("user.name", String.class), new Field("user.home", String.class), new Field("user.dir", String.class), new Field("os.memory.free", String.class), new Field("os.memory.max", String.class), new Field("os.memory.total", String.class)); + } + + @Test + public void testGetTraceMask() throws IOException, InterruptedException { + testCommand("get_trace_mask", new Field("tracemask", Long.class)); + } + + @Test + public void testIsReadOnly() throws IOException, InterruptedException { + testCommand("is_read_only", new Field("read_only", Boolean.class)); + } + + @Test + public void testLastSnapshot() throws IOException, InterruptedException { + testCommand("last_snapshot", new Field("zxid", String.class), new Field("timestamp", Long.class)); + } + + @Test + public void testMonitor() throws IOException, InterruptedException { + ArrayList fields = new ArrayList<>(Arrays.asList( + new Field("version", String.class), + new Field("avg_latency", Double.class), + new Field("max_latency", Long.class), + new Field("min_latency", Long.class), + new Field("packets_received", Long.class), + new Field("packets_sent", Long.class), + new Field("num_alive_connections", Integer.class), + new Field("outstanding_requests", Long.class), + new Field("server_state", String.class), + new Field("znode_count", Integer.class), + new Field("watch_count", Integer.class), + new Field("ephemerals_count", Integer.class), + new Field("approximate_data_size", Long.class), + new Field("open_file_descriptor_count", Long.class), + new Field("max_file_descriptor_count", Long.class), + new Field("last_client_response_size", Integer.class), + new Field("max_client_response_size", Integer.class), + new Field("min_client_response_size", Integer.class), + new Field("auth_failed_count", Long.class), + new Field("non_mtls_remote_conn_count", Long.class), + new Field("non_mtls_local_conn_count", Long.class), + new Field("uptime", Long.class), + new Field("global_sessions", Long.class), + new Field("local_sessions", Long.class), + new Field("connection_drop_probability", Double.class), + new Field("outstanding_tls_handshake", Integer.class) + )); + Map metrics = MetricsUtils.currentServerMetrics(); + + for (String metric : metrics.keySet()) { + boolean alreadyDefined = fields.stream().anyMatch(f -> { + return f.key.equals(metric); + }); + if (alreadyDefined) { + // known metrics are defined statically in the block above + continue; + } + if (metric.startsWith("avg_")) { + fields.add(new Field(metric, Double.class)); + } else { + fields.add(new Field(metric, Long.class)); + } + } + Field[] fieldsArray = fields.toArray(new Field[0]); + testCommand("monitor", fieldsArray); + } + + @Test + public void testRuok() throws IOException, InterruptedException { + testCommand("ruok"); + } + + @Test + public void testServerStats() throws IOException, InterruptedException { + testCommand("server_stats", new Field("version", String.class), new Field("read_only", Boolean.class), new Field("server_stats", ServerStats.class), new Field("node_count", Integer.class), new Field("client_response", BufferStats.class)); + } + + @Test + public void testSetTraceMask() throws IOException, InterruptedException { + Map kwargs = new HashMap(); + kwargs.put("traceMask", "1"); + testCommand("set_trace_mask", kwargs, new Field("tracemask", Long.class)); + } + + @Test + public void testStat() throws IOException, InterruptedException { + testCommand("stats", + new Field("version", String.class), + new Field("read_only", Boolean.class), + new Field("server_stats", ServerStats.class), + new Field("node_count", Integer.class), + new Field("connections", Iterable.class), + new Field("secure_connections", Iterable.class), + new Field("client_response", BufferStats.class)); + } + + @Test + public void testStatReset() throws IOException, InterruptedException { + testCommand("stat_reset"); + } + + @Test + public void testWatches() throws IOException, InterruptedException { + testCommand("watches", new Field("session_id_to_watched_paths", Map.class)); + } + + @Test + public void testWatchesByPath() throws IOException, InterruptedException { + testCommand("watches_by_path", new Field("path_to_session_ids", Map.class)); + } + + @Test + public void testWatchSummary() throws IOException, InterruptedException { + testCommand("watch_summary", new Field("num_connections", Integer.class), new Field("num_paths", Integer.class), new Field("num_total_watches", Integer.class)); + } + + @Test + public void testVotingViewCommand() throws IOException, InterruptedException { + testCommand("voting_view", + new Field("current_config", Map.class)); + } + + @Test + public void testConsCommandSecureOnly() { + // Arrange + Commands.ConsCommand cmd = new Commands.ConsCommand(); + ZooKeeperServer zkServer = mock(ZooKeeperServer.class); + ServerCnxnFactory cnxnFactory = mock(ServerCnxnFactory.class); + when(zkServer.getSecureServerCnxnFactory()).thenReturn(cnxnFactory); + + // Act + CommandResponse response = cmd.run(zkServer, null); + + // Assert + assertThat(response.toMap().containsKey("connections"), is(true)); + assertThat(response.toMap().containsKey("secure_connections"), is(true)); + } + + /** + * testing Stat command, when only SecureClientPort is defined by the user and there is no + * regular (non-SSL port) open. In this case zkServer.getServerCnxnFactory === null + * see: ZOOKEEPER-3633 + */ + @Test + public void testStatCommandSecureOnly() { + Commands.StatCommand cmd = new Commands.StatCommand(); + ZooKeeperServer zkServer = mock(ZooKeeperServer.class); + ServerCnxnFactory cnxnFactory = mock(ServerCnxnFactory.class); + ServerStats serverStats = mock(ServerStats.class); + ZKDatabase zkDatabase = mock(ZKDatabase.class); + when(zkServer.getSecureServerCnxnFactory()).thenReturn(cnxnFactory); + when(zkServer.serverStats()).thenReturn(serverStats); + when(zkServer.getZKDatabase()).thenReturn(zkDatabase); + when(zkDatabase.getNodeCount()).thenReturn(0); + + CommandResponse response = cmd.run(zkServer, null); + + assertThat(response.toMap().containsKey("connections"), is(true)); + assertThat(response.toMap().containsKey("secure_connections"), is(true)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/JettyAdminServerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/JettyAdminServerTest.java new file mode 100644 index 0000000..2042034 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/admin/JettyAdminServerTest.java @@ -0,0 +1,318 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.admin; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.SocketException; +import java.net.URL; +import java.security.GeneralSecurityException; +import java.security.Security; +import java.security.cert.X509Certificate; +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.KeyStoreFileType; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.common.X509KeyType; +import org.apache.zookeeper.common.X509TestContext; +import org.apache.zookeeper.server.ZooKeeperServerMainTest; +import org.apache.zookeeper.server.admin.AdminServer.AdminServerException; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JettyAdminServerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(JettyAdminServerTest.class); + + private static final String URL_FORMAT = "http://localhost:%d/commands"; + private static final String HTTPS_URL_FORMAT = "https://localhost:%d/commands"; + private static final int jettyAdminPort = PortAssignment.unique(); + + @Before + public void enableServer() { + // Override setting in ZKTestCase + System.setProperty("zookeeper.admin.enableServer", "true"); + System.setProperty("zookeeper.admin.serverPort", "" + jettyAdminPort); + } + + @Before + public void setupEncryption() { + Security.addProvider(new BouncyCastleProvider()); + File tmpDir = null; + X509TestContext x509TestContext = null; + try { + tmpDir = ClientBase.createEmptyTestDir(); + x509TestContext = X509TestContext.newBuilder() + .setTempDir(tmpDir) + .setKeyStorePassword("") + .setKeyStoreKeyType(X509KeyType.EC) + .setTrustStorePassword("") + .setTrustStoreKeyType(X509KeyType.EC) + .build(); + System.setProperty( + "zookeeper.ssl.quorum.keyStore.location", + x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM).getAbsolutePath()); + System.setProperty( + "zookeeper.ssl.quorum.trustStore.location", + x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM).getAbsolutePath()); + } catch (Exception e) { + LOG.info("Problems encountered while setting up encryption for Jetty admin server test", e); + } + System.setProperty("zookeeper.ssl.quorum.keyStore.password", ""); + System.setProperty("zookeeper.ssl.quorum.keyStore.type", "PEM"); + System.setProperty("zookeeper.ssl.quorum.trustStore.password", ""); + System.setProperty("zookeeper.ssl.quorum.trustStore.type", "PEM"); + System.setProperty("zookeeper.admin.portUnification", "true"); + + // Create a trust manager that does not validate certificate chains + TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + }}; + + // Create all-trusting trust manager + SSLContext sc = null; + try { + sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + } catch (Exception e) { + LOG.error("Failed to customize encryption for HTTPS", e); + } + + // Create all-trusting hostname verifier + HostnameVerifier allValid = new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }; + + // This is a temporary fix while we do not yet have certificates set up to make + // HTTPS requests correctly. This is equivalent to the "-k" option in curl. + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + HttpsURLConnection.setDefaultHostnameVerifier(allValid); + } + + @After + public void cleanUp() { + Security.removeProvider("BC"); + + System.clearProperty("zookeeper.admin.enableServer"); + System.clearProperty("zookeeper.admin.serverPort"); + + System.clearProperty("zookeeper.ssl.quorum.keyStore.location"); + System.clearProperty("zookeeper.ssl.quorum.keyStore.password"); + System.clearProperty("zookeeper.ssl.quorum.keyStore.type"); + System.clearProperty("zookeeper.ssl.quorum.trustStore.location"); + System.clearProperty("zookeeper.ssl.quorum.trustStore.password"); + System.clearProperty("zookeeper.ssl.quorum.trustStore.type"); + System.clearProperty("zookeeper.admin.portUnification"); + System.clearProperty("zookeeper.admin.forceHttps"); + } + + /** + * Tests that we can start and query a JettyAdminServer. + */ + @Test + public void testJettyAdminServer() throws AdminServerException, IOException, SSLContextException, GeneralSecurityException { + JettyAdminServer server = new JettyAdminServer(); + try { + server.start(); + queryAdminServer(jettyAdminPort); + traceAdminServer(jettyAdminPort); + } finally { + server.shutdown(); + } + } + + /** + * Starts a standalone server and tests that we can query its AdminServer. + */ + @Test + public void testStandalone() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + + ZooKeeperServerMainTest.MainThread main = new ZooKeeperServerMainTest.MainThread(CLIENT_PORT, false, null); + main.start(); + + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + + queryAdminServer(jettyAdminPort); + + main.shutdown(); + + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT, ClientBase.CONNECTION_TIMEOUT)); + } + + /** + * Starts a quorum of two servers and tests that we can query both AdminServers. + */ + @Test + public void testQuorum() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + final int ADMIN_SERVER_PORT1 = PortAssignment.unique(); + final int ADMIN_SERVER_PORT2 = PortAssignment.unique(); + + String quorumCfgSection = String.format( + "server.1=127.0.0.1:%d:%d;%d\nserver.2=127.0.0.1:%d:%d;%d", + PortAssignment.unique(), + PortAssignment.unique(), + CLIENT_PORT_QP1, + PortAssignment.unique(), + PortAssignment.unique(), + CLIENT_PORT_QP2); + QuorumPeerTestBase.MainThread q1 = new QuorumPeerTestBase.MainThread(1, CLIENT_PORT_QP1, ADMIN_SERVER_PORT1, quorumCfgSection, null); + q1.start(); + + // Since JettyAdminServer reads a system property to determine its port, + // make sure it initializes itself before setting the system property + // again with the second port number + Thread.sleep(500); + + QuorumPeerTestBase.MainThread q2 = new QuorumPeerTestBase.MainThread(2, CLIENT_PORT_QP2, ADMIN_SERVER_PORT2, quorumCfgSection, null); + q2.start(); + + Thread.sleep(500); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + + queryAdminServer(ADMIN_SERVER_PORT1); + queryAdminServer(ADMIN_SERVER_PORT2); + + q1.shutdown(); + q2.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + } + + @Test + public void testForceHttpsPortUnificationEnabled() throws Exception { + testForceHttps(true); + } + + @Test + public void testForceHttpsPortUnificationDisabled() throws Exception { + testForceHttps(false); + } + + private void testForceHttps(boolean portUnification) throws Exception { + System.setProperty("zookeeper.admin.forceHttps", "true"); + System.setProperty("zookeeper.admin.portUnification", String.valueOf(portUnification)); + boolean httpsPassed = false; + + JettyAdminServer server = new JettyAdminServer(); + try { + server.start(); + queryAdminServer(String.format(HTTPS_URL_FORMAT, jettyAdminPort), true); + httpsPassed = true; + queryAdminServer(String.format(URL_FORMAT, jettyAdminPort), false); + fail("http call should have failed since forceHttps=true"); + } catch (SocketException se) { + //good + } finally { + server.shutdown(); + } + assertTrue(httpsPassed); + } + + /** + * Check that we can load the commands page of an AdminServer running at + * localhost:port. (Note that this should work even if no zk server is set.) + */ + private void queryAdminServer(int port) throws IOException, SSLContextException { + queryAdminServer(String.format(URL_FORMAT, port), false); + queryAdminServer(String.format(HTTPS_URL_FORMAT, port), true); + } + + /** + * Check that loading urlStr results in a non-zero length response. + */ + private void queryAdminServer(String urlStr, boolean encrypted) throws IOException, SSLContextException { + URL url = new URL(urlStr); + BufferedReader dis; + if (!encrypted) { + dis = new BufferedReader(new InputStreamReader((url.openStream()))); + } else { + HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); + dis = new BufferedReader(new InputStreamReader(conn.getInputStream())); + } + String line = dis.readLine(); + assertTrue(line.length() > 0); + } + + /** + * Using TRACE method to visit admin server + */ + private void traceAdminServer(int port) throws IOException { + traceAdminServer(String.format(URL_FORMAT, port)); + traceAdminServer(String.format(HTTPS_URL_FORMAT, port)); + } + + /** + * Using TRACE method to visit admin server, the response should be 403 forbidden + */ + private void traceAdminServer(String urlStr) throws IOException { + HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); + conn.setRequestMethod("TRACE"); + conn.connect(); + assertEquals(HttpURLConnection.HTTP_FORBIDDEN, conn.getResponseCode()); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxCounterSetTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxCounterSetTest.java new file mode 100644 index 0000000..50e3788 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxCounterSetTest.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import static org.junit.Assert.assertEquals; +import java.util.Map; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class AvgMinMaxCounterSetTest extends ZKTestCase { + + private AvgMinMaxCounterSet testCounterSet; + + @Before + public void initCounter() { + testCounterSet = new AvgMinMaxCounterSet("test"); + } + + private void addDataPoints() { + testCounterSet.add("key1", 0); + testCounterSet.add("key1", 1); + testCounterSet.add("key2", 2); + testCounterSet.add("key2", 3); + testCounterSet.add("key2", 4); + testCounterSet.add("key2", 5); + } + + @Test + public void testReset() { + addDataPoints(); + testCounterSet.reset(); + + Map values = testCounterSet.values(); + + assertEquals("There should be 10 values in the set", 10, values.size()); + + assertEquals("avg_key1_test should =0", 0D, values.get("avg_key1_test")); + assertEquals("min_key1_test should =0", 0L, values.get("min_key1_test")); + assertEquals("max_key1_test should =0", 0L, values.get("max_key1_test")); + assertEquals("cnt_key1_test should =0", 0L, values.get("cnt_key1_test")); + assertEquals("sum_key1_test should =0", 0L, values.get("sum_key1_test")); + + assertEquals("avg_key2_test should =0", 0D, values.get("avg_key2_test")); + assertEquals("min_key2_test should =0", 0L, values.get("min_key2_test")); + assertEquals("max_key2_test should =0", 0L, values.get("max_key2_test")); + assertEquals("cnt_key2_test should =0", 0L, values.get("cnt_key2_test")); + assertEquals("sum_key2_test should =0", 0L, values.get("sum_key2_test")); + + } + + @Test + public void testValues() { + addDataPoints(); + Map values = testCounterSet.values(); + + assertEquals("There should be 10 values in the set", 10, values.size()); + assertEquals("avg_key1_test should =0.5", 0.5D, values.get("avg_key1_test")); + assertEquals("min_key1_test should =0", 0L, values.get("min_key1_test")); + assertEquals("max_key1_test should =1", 1L, values.get("max_key1_test")); + assertEquals("cnt_key1_test should =2", 2L, values.get("cnt_key1_test")); + assertEquals("sum_key1_test should =1", 1L, values.get("sum_key1_test")); + + assertEquals("avg_key2_test should =3.5", 3.5, values.get("avg_key2_test")); + assertEquals("min_key2_test should =2", 2L, values.get("min_key2_test")); + assertEquals("max_key2_test should =5", 5L, values.get("max_key2_test")); + assertEquals("cnt_key2_test should =4", 4L, values.get("cnt_key2_test")); + assertEquals("sum_key2_test should =14", 14L, values.get("sum_key2_test")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterSetTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterSetTest.java new file mode 100644 index 0000000..e120ac5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterSetTest.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import static org.junit.Assert.assertEquals; +import java.util.Map; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class AvgMinMaxPercentileCounterSetTest extends ZKTestCase { + + private AvgMinMaxPercentileCounterSet testCounterSet; + + @Before + public void initCounter() { + testCounterSet = new AvgMinMaxPercentileCounterSet("test"); + } + + private void addDataPoints() { + for (int i = 0; i < 1000; i++) { + testCounterSet.add("key1", i); + } + + for (int i = 1000; i < 2000; i++) { + testCounterSet.add("key2", i); + } + } + + @Test + public void testReset() { + addDataPoints(); + testCounterSet.reset(); + + Map values = testCounterSet.values(); + + assertEquals("avg_key1_test should =0", 0D, values.get("avg_key1_test")); + assertEquals("min_key1_test should =0", 0L, values.get("min_key1_test")); + assertEquals("max_key1_test should =0", 0L, values.get("max_key1_test")); + assertEquals("cnt_key1_test should =0", 0L, values.get("cnt_key1_test")); + assertEquals("sum_key1_test should =0", 0L, values.get("sum_key1_test")); + assertEquals("p50_key1_test should have p50=0", 0L, values.get("p50_key1_test")); + assertEquals("p95_key1_test should have p95=0", 0L, values.get("p95_key1_test")); + assertEquals("p99_key1_test should have p99=0", 0L, values.get("p99_key1_test")); + assertEquals("p999_key1_test should have p999=0", 0L, values.get("p999_key1_test")); + + assertEquals("avg_key2_test should =0", 0D, values.get("avg_key2_test")); + assertEquals("min_key2_test should =0", 0L, values.get("min_key2_test")); + assertEquals("max_key2_test should =0", 0L, values.get("max_key2_test")); + assertEquals("cnt_key2_test should =0", 0L, values.get("cnt_key2_test")); + assertEquals("sum_key2_test should =0", 0L, values.get("sum_key2_test")); + assertEquals("p50_key2_test should have p50=0", 0L, values.get("p50_key2_test")); + assertEquals("p95_key2_test should have p95=0", 0L, values.get("p95_key2_test")); + assertEquals("p99_key2_test should have p99=0", 0L, values.get("p99_key2_test")); + assertEquals("p999_key2_test should have p999=0", 0L, values.get("p999_key2_test")); + } + + @Test + public void testValues() { + addDataPoints(); + Map values = testCounterSet.values(); + + assertEquals("There should be 18 values in the set", 18, values.size()); + + assertEquals("avg_key1_test should =499.5", 999D / 2, values.get("avg_key1_test")); + assertEquals("min_key1_test should =0", 0L, values.get("min_key1_test")); + assertEquals("max_key1_test should =999", 999L, values.get("max_key1_test")); + assertEquals("cnt_key1_test should =1000", 1000L, values.get("cnt_key1_test")); + assertEquals("sum_key1_test should =999*500", 999 * 500L, values.get("sum_key1_test")); + assertEquals("p50_key1_test should have p50=500", 500L, values.get("p50_key1_test")); + assertEquals("p95_key1_test should have p95=950", 950L, values.get("p95_key1_test")); + assertEquals("p99_key1_test should have p99=990", 990L, values.get("p99_key1_test")); + assertEquals("p999_key1_test should have p999=999", 999L, values.get("p999_key1_test")); + + assertEquals("avg_key2_test should =3.5", 1000 + 999D / 2, values.get("avg_key2_test")); + assertEquals("min_key2_test should =2", 1000L, values.get("min_key2_test")); + assertEquals("max_key2_test should =5", 1999L, values.get("max_key2_test")); + assertEquals("cnt_key2_test should =4", 1000L, values.get("cnt_key2_test")); + assertEquals("sum_key2_test should =14", 2999 * 500L, values.get("sum_key2_test")); + assertEquals("p50_key2_test should have p50=1500", 1500L, values.get("p50_key2_test")); + assertEquals("p95_key2_test should have p95=1950", 1950L, values.get("p95_key2_test")); + assertEquals("p99_key2_test should have p99=1990", 1990L, values.get("p99_key2_test")); + assertEquals("p999_key2_test should have p999=1999", 1999L, values.get("p999_key2_test")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterTest.java new file mode 100644 index 0000000..ecfdbbc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/AvgMinMaxPercentileCounterTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import static org.junit.Assert.assertEquals; +import java.util.Map; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class AvgMinMaxPercentileCounterTest extends ZKTestCase { + + private AvgMinMaxPercentileCounter testCounter; + + @Before + public void initCounter() { + testCounter = new AvgMinMaxPercentileCounter("test"); + } + + private void addDataPoints() { + for (int i = 0; i < 1000; i++) { + testCounter.add(i); + } + } + + @Test + public void testReset() { + addDataPoints(); + testCounter.reset(); + + Map values = testCounter.values(); + + assertEquals("There should be 9 values in the set", 9, values.size()); + + assertEquals("should avg=0", 0D, values.get("avg_test")); + assertEquals("should have min=0", 0L, values.get("min_test")); + assertEquals("should have max=0", 0L, values.get("max_test")); + assertEquals("should have cnt=0", 0L, values.get("cnt_test")); + assertEquals("should have sum=0", 0L, values.get("sum_test")); + assertEquals("should have p50=0", 0L, values.get("p50_test")); + assertEquals("should have p95=0", 0L, values.get("p95_test")); + assertEquals("should have p99=0", 0L, values.get("p99_test")); + assertEquals("should have p999=0", 0L, values.get("p999_test")); + } + + @Test + public void testValues() { + addDataPoints(); + Map values = testCounter.values(); + + assertEquals("There should be 9 values in the set", 9, values.size()); + + assertEquals("should avg=499.5", 999D / 2, values.get("avg_test")); + assertEquals("should have min=0", 0L, values.get("min_test")); + assertEquals("should have max=999", 999L, values.get("max_test")); + assertEquals("should have cnt=1000", 1000L, values.get("cnt_test")); + assertEquals("should have sum=999*500", 999 * 500L, values.get("sum_test")); + assertEquals("should have p50=500", 500L, values.get("p50_test")); + assertEquals("should have p95=950", 950L, values.get("p95_test")); + assertEquals("should have p99=990", 990L, values.get("p99_test")); + assertEquals("should have p999=999", 999L, values.get("p999_test")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/EmptySnapshotTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/EmptySnapshotTest.java new file mode 100644 index 0000000..7eb6f60 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/EmptySnapshotTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.server.DataTree; +import org.junit.Test; + +/** + * This test checks that the server does not create empty snapshot files if the + * disk is full. + */ +public class EmptySnapshotTest { + + static class MockFileSnap extends FileSnap { + + MockFileSnap(File snapDir) { + super(snapDir); + } + + public synchronized void serialize(DataTree dt, Map sessions, File snapShot, boolean fsync) throws IOException { + // Create empty new file. + assertTrue(snapShot.createNewFile()); + throw new IOException("Created empty snapshot file from " + "MockFileSnap::serialize()"); + } + + } + + @Test + public void testNoEmptySnapshot() throws Exception { + File tmpFile = File.createTempFile("empty-snapshot-test", ".junit", new File(System.getProperty("build.test.dir", "build"))); + File tmpDataDir = new File(tmpFile + ".dir"); + assertFalse(tmpDataDir.exists()); + assertTrue(tmpDataDir.mkdirs()); + + FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDataDir, tmpDataDir); + snapLog.snapLog = new MockFileSnap(snapLog.dataDir); + + assertEquals(0, ((FileSnap) snapLog.snapLog).findNRecentSnapshots(10).size()); + + DataTree tree = new DataTree(); + tree.createNode("/empty-snapshot-test-1", "data".getBytes(), null, -1, -1, 1, 1); + try { + snapLog.save(tree, new ConcurrentHashMap<>(), false); + fail("Should have thrown an IOException"); + } catch (IOException e) { + // no op + } + + assertEquals(0, ((FileSnap) snapLog.snapLog).findNRecentSnapshots(10).size()); + + snapLog.snapLog = new FileSnap(snapLog.dataDir); + snapLog.save(tree, new ConcurrentHashMap<>(), false); + assertEquals(1, ((FileSnap) snapLog.snapLog).findNRecentSnapshots(10).size()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnLogTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnLogTest.java new file mode 100644 index 0000000..52a734f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnLogTest.java @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Random; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileTxnLogTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FileTxnLogTest.class); + + private static final int KB = 1024; + + @Test + public void testInvalidPreallocSize() { + assertEquals( + "file should not be padded", + 10 * KB, + FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, 0)); + assertEquals( + "file should not be padded", + 10 * KB, + FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, -1)); + } + + @Test + public void testCalculateFileSizeWithPaddingWhenNotToCurrentSize() { + assertEquals( + "file should not be padded", + 10 * KB, + FilePadding.calculateFileSizeWithPadding(5 * KB, 10 * KB, 10 * KB)); + } + + @Test + public void testCalculateFileSizeWithPaddingWhenCloseToCurrentSize() { + assertEquals( + "file should be padded an additional 10 KB", + 20 * KB, + FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, 10 * KB)); + } + + @Test + public void testFileSizeGreaterThanPosition() { + assertEquals( + "file should be padded to 40 KB", + 40 * KB, + FilePadding.calculateFileSizeWithPadding(31 * KB, 10 * KB, 10 * KB)); + } + + @Test + public void testPreAllocSizeSmallerThanTxnData() throws IOException { + File logDir = ClientBase.createTmpDir(); + FileTxnLog fileTxnLog = new FileTxnLog(logDir); + + // Set a small preAllocSize (.5 MB) + final int preAllocSize = 500 * KB; + FilePadding.setPreallocSize(preAllocSize); + + // Create dummy txn larger than preAllocSize + // Since the file padding inserts a 0, we will fill the data with 0xff to ensure we corrupt the data if we put the 0 in the data + byte[] data = new byte[2 * preAllocSize]; + Arrays.fill(data, (byte) 0xff); + + // Append and commit 2 transactions to the log + // Prior to ZOOKEEPER-2249, attempting to pad in association with the second transaction will corrupt the first + fileTxnLog.append( + new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.create), + new CreateTxn("/testPreAllocSizeSmallerThanTxnData1", data, ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 0)); + fileTxnLog.commit(); + fileTxnLog.append( + new TxnHeader(1, 1, 2, 2, ZooDefs.OpCode.create), + new CreateTxn("/testPreAllocSizeSmallerThanTxnData2", new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 0)); + fileTxnLog.commit(); + fileTxnLog.close(); + + // Read the log back from disk, this will throw a java.io.IOException: CRC check failed prior to ZOOKEEPER-2249 + FileTxnLog.FileTxnIterator fileTxnIterator = new FileTxnLog.FileTxnIterator(logDir, 0); + + // Verify the data in the first transaction + CreateTxn createTxn = (CreateTxn) fileTxnIterator.getTxn(); + assertTrue(Arrays.equals(createTxn.getData(), data)); + + // Verify the data in the second transaction + fileTxnIterator.next(); + createTxn = (CreateTxn) fileTxnIterator.getTxn(); + assertTrue(Arrays.equals(createTxn.getData(), new byte[]{})); + } + + @Test + public void testSetPreallocSize() { + long customPreallocSize = 10101; + FileTxnLog.setPreallocSize(customPreallocSize); + assertThat(FilePadding.getPreAllocSize(), is(equalTo(customPreallocSize))); + } + + public void testSyncThresholdExceedCount() throws IOException { + // Given ... + + // Set threshold to -1, as after the first commit it takes 0ms to commit to disk. + java.lang.System.setProperty(FileTxnLog.ZOOKEEPER_FSYNC_WARNING_THRESHOLD_MS_PROPERTY, "-1"); + ServerStats.Provider providerMock = mock(ServerStats.Provider.class); + ServerStats serverStats = new ServerStats(providerMock); + + File logDir = ClientBase.createTmpDir(); + FileTxnLog fileTxnLog = new FileTxnLog(logDir); + fileTxnLog.setServerStats(serverStats); + + // Verify serverStats is 0 before any commit + assertEquals(0L, serverStats.getFsyncThresholdExceedCount()); + + // When ... + for (int i = 0; i < 50; i++) { + fileTxnLog.append( + new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.create), + new CreateTxn("/testFsyncThresholdCountIncreased", new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 0)); + fileTxnLog.commit(); // only 1 commit, otherwise it will be flaky + // Then ... verify serverStats is updated to the number of commits (as threshold is set to 0) + assertEquals((long) i + 1, serverStats.getFsyncThresholdExceedCount()); + } + } + + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + + // Overhead is about 150 bytes for txn created in this test + private static final int NODE_SIZE = 1024; + private final long PREALLOCATE = 512; + private final long LOG_SIZE_LIMIT = 1024 * 4; + + /** + * Test that log size get update correctly + */ + @Test + public void testGetCurrentLogSize() throws Exception { + FileTxnLog.setTxnLogSizeLimit(-1); + File tmpDir = ClientBase.createTmpDir(); + FileTxnLog log = new FileTxnLog(tmpDir); + FileTxnLog.setPreallocSize(PREALLOCATE); + CreateRequest record = new CreateRequest(null, new byte[NODE_SIZE], ZooDefs.Ids.OPEN_ACL_UNSAFE, 0); + int zxid = 1; + for (int i = 0; i < 4; i++) { + log.append(new TxnHeader(0, 0, zxid++, 0, 0), record); + LOG.debug("Current log size: {}", log.getCurrentLogSize()); + } + log.commit(); + LOG.info("Current log size: {}", log.getCurrentLogSize()); + assertTrue(log.getCurrentLogSize() > (zxid - 1) * NODE_SIZE); + for (int i = 0; i < 4; i++) { + log.append(new TxnHeader(0, 0, zxid++, 0, 0), record); + LOG.debug("Current log size: {}", log.getCurrentLogSize()); + } + log.commit(); + LOG.info("Current log size: " + log.getCurrentLogSize()); + assertTrue(log.getCurrentLogSize() > (zxid - 1) * NODE_SIZE); + } + + /** + * Test that the server can correctly load the data when there are multiple + * txnlogs per snapshot + */ + @Test + public void testLogSizeLimit() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + + // Need to override preallocate set by setupTestEnv() + // We don't need to unset these values since each unit test run in + // a separate JVM instance + FileTxnLog.setPreallocSize(PREALLOCATE); + FileTxnLog.setTxnLogSizeLimit(LOG_SIZE_LIMIT); + + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, DummyWatcher.INSTANCE); + + // Generate transactions + HashSet zxids = new HashSet<>(); + byte[] bytes = new byte[NODE_SIZE]; + Random random = new Random(); + random.nextBytes(bytes); + + // We will create enough txn to generate 3 logs + long txnCount = LOG_SIZE_LIMIT / NODE_SIZE / 2 * 5; + + LOG.info("Creating {} txns", txnCount); + + try { + for (long i = 0; i < txnCount; i++) { + Stat stat = new Stat(); + zk.create("/node-" + i, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.getData("/node-" + i, null, stat); + zxids.add(stat.getCzxid()); + } + + } finally { + zk.close(); + } + + // shutdown + f.shutdown(); + assertTrue( + "waiting for server to shutdown", + ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + + File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + File[] txnLogs = FileTxnLog.getLogFiles(logDir.listFiles(), 0); + + assertEquals("Unexpected number of logs", 3, txnLogs.length); + + // Log size should not exceed limit by more than one node size; + long threshold = LOG_SIZE_LIMIT + NODE_SIZE; + LOG.info(txnLogs[0].getAbsolutePath()); + assertTrue( + "Exceed log size limit: " + txnLogs[0].length(), + threshold > txnLogs[0].length()); + LOG.info(txnLogs[1].getAbsolutePath()); + assertTrue( + "Exceed log size limit " + txnLogs[1].length(), + threshold > txnLogs[1].length()); + + // Start database only + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + zks.startdata(); + + ZKDatabase db = zks.getZKDatabase(); + + for (long i = 0; i < txnCount; i++) { + Stat stat = new Stat(); + byte[] data = db.getData("/node-" + i, stat, null); + assertArrayEquals("Missmatch data", bytes, data); + assertTrue("Unknown zxid ", zxids.contains(stat.getMzxid())); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnSnapLogMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnSnapLogMetricsTest.java new file mode 100644 index 0000000..fe075fb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnSnapLogMetricsTest.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileTxnSnapLogMetricsTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(FileTxnSnapLogMetricsTest.class); + + CountDownLatch allCreatedLatch; + + private class MockWatcher implements Watcher { + + @Override + public void process(WatchedEvent e) { + LOG.info("all nodes created"); + allCreatedLatch.countDown(); + } + + } + + @Test + public void testFileTxnSnapLogMetrics() throws Exception { + SyncRequestProcessor.setSnapCount(100); + + QuorumUtil util = new QuorumUtil(1); + util.startAll(); + + allCreatedLatch = new CountDownLatch(1); + + byte[] data = new byte[500]; + // make sure a snapshot is taken and some txns are not in a snapshot + ZooKeeper zk = ClientBase.createZKClient(util.getConnString()); + for (int i = 0; i < 150; i++) { + zk.create("/path" + i, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + if (null == zk.exists("/path149", new MockWatcher())) { + allCreatedLatch.await(); + } + + ServerMetrics.getMetrics().resetAll(); + int leader = util.getLeaderServer(); + // restart a server so it will read the snapshot and the txn logs + util.shutdown(leader); + util.start(leader); + + Map values = MetricsUtils.currentServerMetrics(); + LOG.info("txn loaded during start up {}", values.get("max_startup_txns_loaded")); + assertEquals(1L, values.get("cnt_startup_txns_loaded")); + assertThat((long) values.get("max_startup_txns_loaded"), greaterThan(0L)); + assertEquals(1L, values.get("cnt_startup_txns_load_time")); + assertThat((long) values.get("max_startup_txns_load_time"), greaterThanOrEqualTo(0L)); + assertEquals(1L, values.get("cnt_startup_snap_load_time")); + assertThat((long) values.get("max_startup_snap_load_time"), greaterThan(0L)); + + util.shutdownAll(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnSnapLogTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnSnapLogTest.java new file mode 100644 index 0000000..34d57f4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/FileTxnSnapLogTest.java @@ -0,0 +1,400 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.DataNode; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.TestUtils; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class FileTxnSnapLogTest { + + private File tmpDir; + + private File logDir; + + private File snapDir; + + private File logVersionDir; + + private File snapVersionDir; + + @Before + public void setUp() throws Exception { + tmpDir = ClientBase.createEmptyTestDir(); + logDir = new File(tmpDir, "logdir"); + snapDir = new File(tmpDir, "snapdir"); + } + + @After + public void tearDown() throws Exception { + if (tmpDir != null) { + TestUtils.deleteFileRecursively(tmpDir); + } + this.tmpDir = null; + this.logDir = null; + this.snapDir = null; + this.logVersionDir = null; + this.snapVersionDir = null; + } + + private File createVersionDir(File parentDir) { + File versionDir = new File(parentDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + versionDir.mkdirs(); + return versionDir; + } + + private void createLogFile(File dir, long zxid) throws IOException { + File file = new File(dir.getPath() + File.separator + Util.makeLogName(zxid)); + file.createNewFile(); + } + + private void createSnapshotFile(File dir, long zxid) throws IOException { + File file = new File(dir.getPath() + File.separator + Util.makeSnapshotName(zxid)); + file.createNewFile(); + } + + private void twoDirSetupWithCorrectFiles() throws IOException { + logVersionDir = createVersionDir(logDir); + snapVersionDir = createVersionDir(snapDir); + + // transaction log files in log dir + createLogFile(logVersionDir, 1); + createLogFile(logVersionDir, 2); + + // snapshot files in snap dir + createSnapshotFile(snapVersionDir, 1); + createSnapshotFile(snapVersionDir, 2); + } + + private void singleDirSetupWithCorrectFiles() throws IOException { + logVersionDir = createVersionDir(logDir); + + // transaction log and snapshot files in the same dir + createLogFile(logVersionDir, 1); + createLogFile(logVersionDir, 2); + createSnapshotFile(logVersionDir, 1); + createSnapshotFile(logVersionDir, 2); + } + + private FileTxnSnapLog createFileTxnSnapLogWithNoAutoCreateDataDir(File logDir, File snapDir) throws IOException { + return createFileTxnSnapLogWithAutoCreateDataDir(logDir, snapDir, "false"); + } + + private FileTxnSnapLog createFileTxnSnapLogWithAutoCreateDataDir( + File logDir, + File snapDir, + String autoCreateValue) throws IOException { + String priorAutocreateDirValue = System.getProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE); + System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, autoCreateValue); + FileTxnSnapLog fileTxnSnapLog; + try { + fileTxnSnapLog = new FileTxnSnapLog(logDir, snapDir); + } finally { + if (priorAutocreateDirValue == null) { + System.clearProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE); + } else { + System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, priorAutocreateDirValue); + } + } + return fileTxnSnapLog; + } + + private FileTxnSnapLog createFileTxnSnapLogWithAutoCreateDB( + File logDir, + File snapDir, + String autoCreateValue) throws IOException { + String priorAutocreateDBValue = System.getProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE); + System.setProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE, autoCreateValue); + FileTxnSnapLog fileTxnSnapLog; + try { + fileTxnSnapLog = new FileTxnSnapLog(logDir, snapDir); + } finally { + if (priorAutocreateDBValue == null) { + System.clearProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE); + } else { + System.setProperty(FileTxnSnapLog.ZOOKEEPER_DB_AUTOCREATE, priorAutocreateDBValue); + } + } + return fileTxnSnapLog; + } + + /** + * Test verifies the auto creation of log dir and snap dir. + * Sets "zookeeper.datadir.autocreate" to true. + */ + @Test + public void testWithAutoCreateDataDir() throws IOException { + assertFalse("log directory already exists", logDir.exists()); + assertFalse("snapshot directory already exists", snapDir.exists()); + + FileTxnSnapLog fileTxnSnapLog = createFileTxnSnapLogWithAutoCreateDataDir(logDir, snapDir, "true"); + + assertTrue(logDir.exists()); + assertTrue(snapDir.exists()); + assertTrue(fileTxnSnapLog.getDataDir().exists()); + assertTrue(fileTxnSnapLog.getSnapDir().exists()); + } + + /** + * Test verifies server should fail when log dir or snap dir doesn't exist. + * Sets "zookeeper.datadir.autocreate" to false. + */ + @Test(expected = FileTxnSnapLog.DatadirException.class) + public void testWithoutAutoCreateDataDir() throws Exception { + assertFalse("log directory already exists", logDir.exists()); + assertFalse("snapshot directory already exists", snapDir.exists()); + + try { + createFileTxnSnapLogWithAutoCreateDataDir(logDir, snapDir, "false"); + } catch (FileTxnSnapLog.DatadirException e) { + assertFalse(logDir.exists()); + assertFalse(snapDir.exists()); + // rethrow exception + throw e; + } + fail("Expected exception from FileTxnSnapLog"); + } + + private void attemptAutoCreateDB( + File dataDir, + File snapDir, + Map sessions, + String autoCreateValue, + long expectedValue) throws IOException { + sessions.clear(); + + FileTxnSnapLog fileTxnSnapLog = createFileTxnSnapLogWithAutoCreateDB(dataDir, snapDir, autoCreateValue); + + long zxid = fileTxnSnapLog.restore(new DataTree(), sessions, new FileTxnSnapLog.PlayBackListener() { + @Override + public void onTxnLoaded(TxnHeader hdr, Record rec, TxnDigest digest) { + // empty by default + } + }); + assertEquals("unexpected zxid", expectedValue, zxid); + } + + @Test + public void testAutoCreateDB() throws IOException { + assertTrue("cannot create log directory", logDir.mkdir()); + assertTrue("cannot create snapshot directory", snapDir.mkdir()); + File initFile = new File(logDir, "initialize"); + assertFalse("initialize file already exists", initFile.exists()); + + Map sessions = new ConcurrentHashMap<>(); + + attemptAutoCreateDB(logDir, snapDir, sessions, "false", -1L); + attemptAutoCreateDB(logDir, snapDir, sessions, "true", 0L); + + assertTrue("cannot create initialize file", initFile.createNewFile()); + attemptAutoCreateDB(logDir, snapDir, sessions, "false", 0L); + } + + @Test + public void testGetTxnLogSyncElapsedTime() throws IOException { + FileTxnSnapLog fileTxnSnapLog = createFileTxnSnapLogWithAutoCreateDataDir(logDir, snapDir, "true"); + + TxnHeader hdr = new TxnHeader(1, 1, 1, 1, ZooDefs.OpCode.setData); + Record txn = new SetDataTxn("/foo", new byte[0], 1); + Request req = new Request(0, 0, 0, hdr, txn, 0); + + try { + fileTxnSnapLog.append(req); + fileTxnSnapLog.commit(); + long syncElapsedTime = fileTxnSnapLog.getTxnLogElapsedSyncTime(); + assertNotEquals("Did not update syncElapsedTime!", -1L, syncElapsedTime); + } finally { + fileTxnSnapLog.close(); + } + } + + @Test + public void testDirCheckWithCorrectFiles() throws IOException { + twoDirSetupWithCorrectFiles(); + + try { + createFileTxnSnapLogWithNoAutoCreateDataDir(logDir, snapDir); + } catch (FileTxnSnapLog.LogDirContentCheckException | FileTxnSnapLog.SnapDirContentCheckException e) { + fail("Should not throw ContentCheckException."); + } + } + + @Test + public void testDirCheckWithSingleDirSetup() throws IOException { + singleDirSetupWithCorrectFiles(); + + try { + createFileTxnSnapLogWithNoAutoCreateDataDir(logDir, logDir); + } catch (FileTxnSnapLog.LogDirContentCheckException | FileTxnSnapLog.SnapDirContentCheckException e) { + fail("Should not throw ContentCheckException."); + } + } + + @Test(expected = FileTxnSnapLog.LogDirContentCheckException.class) + public void testDirCheckWithSnapFilesInLogDir() throws IOException { + twoDirSetupWithCorrectFiles(); + + // add snapshot files to the log version dir + createSnapshotFile(logVersionDir, 3); + createSnapshotFile(logVersionDir, 4); + + createFileTxnSnapLogWithNoAutoCreateDataDir(logDir, snapDir); + } + + @Test(expected = FileTxnSnapLog.SnapDirContentCheckException.class) + public void testDirCheckWithLogFilesInSnapDir() throws IOException { + twoDirSetupWithCorrectFiles(); + + // add transaction log files to the snap version dir + createLogFile(snapVersionDir, 3); + createLogFile(snapVersionDir, 4); + + createFileTxnSnapLogWithNoAutoCreateDataDir(logDir, snapDir); + } + + /** + * Make sure the ACL is exist in the ACL map after SNAP syncing. + * + * ZooKeeper uses ACL reference id and count to save the space in snapshot. + * During fuzzy snapshot sync, the reference count may not be updated + * correctly in case like the znode is already exist. + * + * When ACL reference count reaches 0, it will be deleted from the cache, + * but actually there might be other nodes still using it. When visiting + * a node with the deleted ACL id, it will be rejected because it doesn't + * exist anymore. + * + * Here is the detailed flow for one of the scenario here: + * 1. Server A starts to have snap sync with leader + * 2. After serializing the ACL map to Server A, there is a txn T1 to + * create a node N1 with new ACL_1 which was not exist in ACL map + * 3. On leader, after this txn, the ACL map will be ID1 -> (ACL_1, COUNT: 1), + * and data tree N1 -> ID1 + * 4. On server A, it will be empty ACL map, and N1 -> ID1 in fuzzy snapshot + * 5. When replaying the txn T1, it will skip at the beginning since the + * node is already exist, which leaves an empty ACL map, and N1 is + * referencing to a non-exist ACL ID1 + * 6. Node N1 will be not accessible because the ACL not exist, and if it + * became leader later then all the write requests will be rejected as + * well with marshalling error. + */ + @Test + public void testACLCreatedDuringFuzzySnapshotSync() throws IOException { + DataTree leaderDataTree = new DataTree(); + + // Start the simulated snap-sync by serializing ACL cache. + File file = File.createTempFile("snapshot", "zk"); + FileOutputStream os = new FileOutputStream(file); + OutputArchive oa = BinaryOutputArchive.getArchive(os); + leaderDataTree.serializeAcls(oa); + + // Add couple of transaction in-between. + TxnHeader hdr1 = new TxnHeader(1, 2, 2, 2, ZooDefs.OpCode.create); + Record txn1 = new CreateTxn("/a1", "foo".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, false, -1); + leaderDataTree.processTxn(hdr1, txn1); + + // Finish the snapshot. + leaderDataTree.serializeNodes(oa); + os.close(); + + // Simulate restore on follower and replay. + FileInputStream is = new FileInputStream(file); + InputArchive ia = BinaryInputArchive.getArchive(is); + DataTree followerDataTree = new DataTree(); + followerDataTree.deserialize(ia, "tree"); + followerDataTree.processTxn(hdr1, txn1); + + DataNode a1 = leaderDataTree.getNode("/a1"); + assertNotNull(a1); + assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, leaderDataTree.getACL(a1)); + + assertEquals(ZooDefs.Ids.CREATOR_ALL_ACL, followerDataTree.getACL(a1)); + } + + @Test + public void testEmptySnapshotSerialization() throws IOException { + File dataDir = ClientBase.createEmptyTestDir(); + FileTxnSnapLog snaplog = new FileTxnSnapLog(dataDir, dataDir); + DataTree dataTree = new DataTree(); + ConcurrentHashMap sessions = new ConcurrentHashMap<>(); + + ZooKeeperServer.setDigestEnabled(true); + snaplog.save(dataTree, sessions, true); + snaplog.restore(dataTree, sessions, (hdr, rec, digest) -> { }); + + assertNull(dataTree.getDigestFromLoadedSnapshot()); + } + + @Test + public void testSnapshotSerializationCompatibility() throws IOException { + testSnapshotSerializationCompatibility(true, false); + testSnapshotSerializationCompatibility(false, false); + testSnapshotSerializationCompatibility(true, true); + testSnapshotSerializationCompatibility(false, true); + } + + void testSnapshotSerializationCompatibility(Boolean digestEnabled, Boolean snappyEnabled) throws IOException { + File dataDir = ClientBase.createEmptyTestDir(); + FileTxnSnapLog snaplog = new FileTxnSnapLog(dataDir, dataDir); + DataTree dataTree = new DataTree(); + ConcurrentHashMap sessions = new ConcurrentHashMap<>(); + SnapStream.setStreamMode(snappyEnabled ? SnapStream.StreamMode.SNAPPY : SnapStream.StreamMode.DEFAULT_MODE); + + ZooKeeperServer.setDigestEnabled(digestEnabled); + TxnHeader txnHeader = new TxnHeader(1, 1, 1, 1 + 1, ZooDefs.OpCode.create); + CreateTxn txn = new CreateTxn("/" + 1, "data".getBytes(), null, false, 1); + Request request = new Request(1, 1, 1, txnHeader, txn, 1); + dataTree.processTxn(request.getHdr(), request.getTxn()); + snaplog.save(dataTree, sessions, true); + + int expectedNodeCount = dataTree.getNodeCount(); + ZooKeeperServer.setDigestEnabled(!digestEnabled); + snaplog.restore(dataTree, sessions, (hdr, rec, digest) -> { }); + assertEquals(expectedNodeCount, dataTree.getNodeCount()); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/SnapStreamTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/SnapStreamTest.java new file mode 100644 index 0000000..6545780 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/SnapStreamTest.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.apache.zookeeper.test.ClientBase.createTmpDir; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.util.zip.CheckedInputStream; +import java.util.zip.CheckedOutputStream; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.server.persistence.SnapStream.StreamMode; +import org.junit.After; +import org.junit.Test; + +public class SnapStreamTest { + + @After + public void tearDown() { + System.clearProperty(SnapStream.ZOOKEEPER_SHAPSHOT_STREAM_MODE); + SnapStream.setStreamMode(StreamMode.DEFAULT_MODE); + } + + @Test + public void testStreamMode() { + assertEquals(StreamMode.CHECKED.getName(), ""); + assertEquals(StreamMode.CHECKED.getFileExtension(), ""); + assertEquals(StreamMode.CHECKED, StreamMode.fromString("name")); + assertEquals(StreamMode.GZIP.getName(), "gz"); + assertEquals(StreamMode.GZIP.getFileExtension(), ".gz"); + assertEquals(StreamMode.GZIP, StreamMode.fromString("gz")); + assertEquals(StreamMode.SNAPPY.getName(), "snappy"); + assertEquals(StreamMode.SNAPPY.getFileExtension(), ".snappy"); + assertEquals(StreamMode.SNAPPY, StreamMode.fromString("snappy")); + } + + @Test + public void testGetStreamMode() { + assertEquals("expected to return un-compressed stream", StreamMode.CHECKED, SnapStream.getStreamMode("snapshot.180000e3a2")); + assertEquals("expected to return snappy stream", StreamMode.SNAPPY, SnapStream.getStreamMode("snapshot.180000e3a2.snappy")); + assertEquals("expected to return gzip stream", StreamMode.GZIP, SnapStream.getStreamMode("snapshot.180000e3a2.gz")); + } + + @Test + public void testSerializeDeserializeWithChecked() throws IOException { + testSerializeDeserialize(StreamMode.CHECKED, ""); + } + + @Test + public void testSerializeDeserializeWithSNAPPY() throws IOException { + testSerializeDeserialize(StreamMode.SNAPPY, ".snappy"); + } + + @Test + public void testSerializeDeserializeWithGZIP() throws IOException { + testSerializeDeserialize(StreamMode.GZIP, ".gz"); + } + + private void testSerializeDeserialize(StreamMode mode, String fileSuffix) throws IOException { + testSerializeDeserialize(mode, fileSuffix, false); + testSerializeDeserialize(mode, fileSuffix, true); + } + + private void testSerializeDeserialize(StreamMode mode, String fileSuffix, boolean fsync) throws IOException { + SnapStream.setStreamMode(mode); + + // serialize with gzip stream + File tmpDir = createTmpDir(); + File file = new File(tmpDir, "snapshot.180000e3a2" + fileSuffix); + CheckedOutputStream os = SnapStream.getOutputStream(file, fsync); + OutputArchive oa = BinaryOutputArchive.getArchive(os); + FileHeader header = new FileHeader(FileSnap.SNAP_MAGIC, 2, 1); + header.serialize(oa, "fileheader"); + SnapStream.sealStream(os, oa); + os.flush(); + os.close(); + + assertTrue(SnapStream.isValidSnapshot(file)); + + // deserialize with gzip stream + CheckedInputStream is = SnapStream.getInputStream(file); + InputArchive ia = BinaryInputArchive.getArchive(is); + FileHeader restoredHeader = new FileHeader(); + restoredHeader.deserialize(ia, "fileheader"); + assertEquals("magic not the same", restoredHeader, header); + SnapStream.checkSealIntegrity(is, ia); + } + + private void checkInvalidSnapshot(String filename, boolean fsync) throws IOException { + // set the output stream mode to CHECKED + SnapStream.setStreamMode(StreamMode.CHECKED); + + // serialize to CHECKED file without magic header + File tmpDir = createTmpDir(); + File file = new File(tmpDir, filename); + OutputStream os = SnapStream.getOutputStream(file, fsync); + os.write(1); + os.flush(); + os.close(); + assertFalse(SnapStream.isValidSnapshot(file)); + } + + private void checkInvalidSnapshot(String filename) throws IOException { + checkInvalidSnapshot(filename, false); + checkInvalidSnapshot(filename, true); + } + + @Test + public void testInvalidSnapshot() throws IOException { + assertFalse(SnapStream.isValidSnapshot(null)); + + checkInvalidSnapshot("snapshot.180000e3a2"); + checkInvalidSnapshot("snapshot.180000e3a2.gz"); + checkInvalidSnapshot("snapshot.180000e3a2.snappy"); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java new file mode 100644 index 0000000..20d6fe8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.persistence; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintStream; +import java.util.Scanner; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TxnLogToolkitTest { + + private static final File testData = new File(System.getProperty("test.data.dir", "src/test/resources/data")); + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private File mySnapDir; + + @Before + public void setUp() throws IOException { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + File snapDir = new File(testData, "invalidsnap"); + mySnapDir = ClientBase.createTmpDir(); + FileUtils.copyDirectory(snapDir, mySnapDir); + } + + @After + public void tearDown() throws IOException { + System.setOut(System.out); + System.setErr(System.err); + mySnapDir.setWritable(true); + FileUtils.deleteDirectory(mySnapDir); + } + + @Test + public void testDumpMode() throws Exception { + // Arrange + File logfile = new File(new File(mySnapDir, "version-2"), "log.274"); + TxnLogToolkit lt = new TxnLogToolkit(false, false, logfile.toString(), true); + + // Act + lt.dump(null); + + // Assert + // no exception thrown + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitException.class) + public void testInitMissingFile() throws FileNotFoundException, TxnLogToolkit.TxnLogToolkitException { + // Arrange & Act + File logfile = new File("this_file_should_not_exists"); + TxnLogToolkit lt = new TxnLogToolkit(false, false, logfile.toString(), true); + } + + @Test(expected = TxnLogToolkit.TxnLogToolkitException.class) + public void testInitWithRecoveryFileExists() throws IOException, TxnLogToolkit.TxnLogToolkitException { + // Arrange & Act + File logfile = new File(new File(mySnapDir, "version-2"), "log.274"); + File recoveryFile = new File(new File(mySnapDir, "version-2"), "log.274.fixed"); + recoveryFile.createNewFile(); + TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), true); + } + + @Test + public void testDumpWithCrcError() throws Exception { + // Arrange + File logfile = new File(new File(mySnapDir, "version-2"), "log.42"); + TxnLogToolkit lt = new TxnLogToolkit(false, false, logfile.toString(), true); + + // Act + lt.dump(null); + + // Assert + String output = outContent.toString(); + Pattern p = Pattern.compile("^CRC ERROR.*session 0x8061fac5ddeb0000 cxid 0x0 zxid 0x8800000002 createSession 30000$", Pattern.MULTILINE); + Matcher m = p.matcher(output); + assertTrue("Output doesn't indicate CRC error for the broken session id: " + output, m.find()); + } + + @Test + public void testRecoveryFixBrokenFile() throws Exception { + // Arrange + File logfile = new File(new File(mySnapDir, "version-2"), "log.42"); + TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), true); + + // Act + lt.dump(null); + + // Assert + String output = outContent.toString(); + assertThat(output, containsString("CRC FIXED")); + + // Should be able to dump the recovered logfile with no CRC error + outContent.reset(); + logfile = new File(new File(mySnapDir, "version-2"), "log.42.fixed"); + lt = new TxnLogToolkit(false, false, logfile.toString(), true); + lt.dump(null); + output = outContent.toString(); + assertThat(output, not(containsString("CRC ERROR"))); + } + + @Test + public void testRecoveryInteractiveMode() throws Exception { + // Arrange + File logfile = new File(new File(mySnapDir, "version-2"), "log.42"); + TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), false); + + // Act + lt.dump(new Scanner("y\n")); + + // Assert + String output = outContent.toString(); + assertThat(output, containsString("CRC ERROR")); + + // Should be able to dump the recovered logfile with no CRC error + outContent.reset(); + logfile = new File(new File(mySnapDir, "version-2"), "log.42.fixed"); + lt = new TxnLogToolkit(false, false, logfile.toString(), true); + lt.dump(null); + output = outContent.toString(); + assertThat(output, not(containsString("CRC ERROR"))); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/BufferStatsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/BufferStatsTest.java new file mode 100644 index 0000000..9fb9cfb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/BufferStatsTest.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class BufferStatsTest { + + @Test + public void testSetProposalSizeSetMinMax() { + BufferStats stats = new BufferStats(); + assertEquals(-1, stats.getLastBufferSize()); + assertEquals(-1, stats.getMinBufferSize()); + assertEquals(-1, stats.getMaxBufferSize()); + stats.setLastBufferSize(10); + assertEquals(10, stats.getLastBufferSize()); + assertEquals(10, stats.getMinBufferSize()); + assertEquals(10, stats.getMaxBufferSize()); + stats.setLastBufferSize(20); + assertEquals(20, stats.getLastBufferSize()); + assertEquals(10, stats.getMinBufferSize()); + assertEquals(20, stats.getMaxBufferSize()); + stats.setLastBufferSize(5); + assertEquals(5, stats.getLastBufferSize()); + assertEquals(5, stats.getMinBufferSize()); + assertEquals(20, stats.getMaxBufferSize()); + } + + @Test + public void testReset() { + BufferStats stats = new BufferStats(); + stats.setLastBufferSize(10); + assertEquals(10, stats.getLastBufferSize()); + assertEquals(10, stats.getMinBufferSize()); + assertEquals(10, stats.getMaxBufferSize()); + stats.reset(); + assertEquals(-1, stats.getLastBufferSize()); + assertEquals(-1, stats.getMinBufferSize()); + assertEquals(-1, stats.getMaxBufferSize()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CloseSessionTxnTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CloseSessionTxnTest.java new file mode 100644 index 0000000..ac9665b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CloseSessionTxnTest.java @@ -0,0 +1,102 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Assert; +import org.junit.Test; + +public class CloseSessionTxnTest extends QuorumPeerTestBase { + + /** + * Test leader/leader compatibility with/without CloseSessionTxn, so that + * we can gradually rollout this code and rollback if there is problem. + */ + @Test + public void testCloseSessionTxnCompatile() throws Exception { + // Test 4 cases: + // 1. leader disabled, follower disabled + testCloseSessionWithDifferentConfig(false, false); + + // 2. leader disabled, follower enabled + testCloseSessionWithDifferentConfig(false, true); + + // 3. leader enabled, follower disabled + testCloseSessionWithDifferentConfig(true, false); + + // 4. leader enabled, follower enabled + testCloseSessionWithDifferentConfig(true, true); + } + + private void testCloseSessionWithDifferentConfig( + boolean closeSessionEnabledOnLeader, + boolean closeSessionEnabledOnFollower) throws Exception { + // 1. set up an ensemble with 3 servers + final int numServers = 3; + servers = LaunchServers(numServers); + int leaderId = servers.findLeader(); + ZooKeeperServer.setCloseSessionTxnEnabled(closeSessionEnabledOnLeader); + + // 2. shutdown one of the follower, start it later to pick up the + // CloseSessionTxnEnabled config change + // + // We cannot use different static config in the same JVM, so have to + // use this tricky + int followerA = (leaderId + 1) % numServers; + servers.mt[followerA].shutdown(); + waitForOne(servers.zk[followerA], States.CONNECTING); + + // 3. create an ephemeral node + String path = "/testCloseSessionTxnCompatile"; + servers.zk[leaderId].create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL); + + // 3. close the client + servers.restartClient(leaderId, this); + waitForOne(servers.zk[leaderId], States.CONNECTED); + + // 4. update the CloseSessionTxnEnabled config before follower A + // started + System.setProperty("zookeeper.retainZKDatabase", "true"); + ZooKeeperServer.setCloseSessionTxnEnabled(closeSessionEnabledOnFollower); + + // 5. restart follower A + servers.mt[followerA].start(); + waitForOne(servers.zk[followerA], States.CONNECTED); + + // 4. verify the ephemeral node is gone + for (int i = 0; i < numServers; i++) { + final CountDownLatch syncedLatch = new CountDownLatch(1); + servers.zk[i].sync(path, new AsyncCallback.VoidCallback() { + @Override + public void processResult(int rc, String path, Object ctx) { + syncedLatch.countDown(); + } + }, null); + Assert.assertTrue(syncedLatch.await(3, TimeUnit.SECONDS)); + Assert.assertNull(servers.zk[i].exists(path, false)); + } + } + } diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CnxManagerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CnxManagerTest.java new file mode 100644 index 0000000..a99e336 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CnxManagerTest.java @@ -0,0 +1,689 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.net.ssl.HandshakeCompletedListener; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.QuorumX509Util; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage; +import org.apache.zookeeper.server.quorum.QuorumCnxManager.Message; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.FLENewEpochTest; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class CnxManagerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLENewEpochTest.class); + protected static final int THRESHOLD = 4; + + int count; + Map peers; + File[] peerTmpdir; + int[] peerQuorumPort; + int[] peerClientPort; + @Before + public void setUp() throws Exception { + + this.count = 3; + this.peers = new HashMap(count); + peerTmpdir = new File[count]; + peerQuorumPort = new int[count]; + peerClientPort = new int[count]; + + for (int i = 0; i < count; i++) { + peerQuorumPort[i] = PortAssignment.unique(); + peerClientPort[i] = PortAssignment.unique(); + peers.put((long) i, new QuorumServer(i, new InetSocketAddress("127.0.0.1", peerQuorumPort[i]), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", peerClientPort[i]))); + peerTmpdir[i] = ClientBase.createTmpDir(); + } + } + + ByteBuffer createMsg(int state, long leader, long zxid, long epoch) { + byte[] requestBytes = new byte[28]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + + /* + * Building notification packet to send + */ + + requestBuffer.clear(); + requestBuffer.putInt(state); + requestBuffer.putLong(leader); + requestBuffer.putLong(zxid); + requestBuffer.putLong(epoch); + + return requestBuffer; + } + + class CnxManagerThread extends Thread { + + boolean failed; + CnxManagerThread() { + failed = false; + } + + public void run() { + try { + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[0], peerTmpdir[0], peerClientPort[0], 3, 0, 1000, 2, 2, 2); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if (listener != null) { + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + + long sid = 1; + cnxManager.toSend(sid, createMsg(ServerState.LOOKING.ordinal(), 0, -1, 1)); + + Message m = null; + int numRetries = 1; + while ((m == null) && (numRetries++ <= THRESHOLD)) { + m = cnxManager.pollRecvQueue(3000, TimeUnit.MILLISECONDS); + if (m == null) { + cnxManager.connectAll(); + } + } + + if (numRetries > THRESHOLD) { + failed = true; + return; + } + + cnxManager.testInitiateConnection(sid); + + m = cnxManager.pollRecvQueue(3000, TimeUnit.MILLISECONDS); + if (m == null) { + failed = true; + } + } catch (Exception e) { + LOG.error("Exception while running mock thread", e); + fail("Unexpected exception"); + } + } + + } + + @Test + public void testCnxManager() throws Exception { + CnxManagerThread thread = new CnxManagerThread(); + + thread.start(); + + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2, 2); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if (listener != null) { + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + + cnxManager.toSend(0L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1)); + + Message m = null; + int numRetries = 1; + while ((m == null) && (numRetries++ <= THRESHOLD)) { + m = cnxManager.pollRecvQueue(3000, TimeUnit.MILLISECONDS); + if (m == null) { + cnxManager.connectAll(); + } + } + + assertTrue("Exceeded number of retries", numRetries <= THRESHOLD); + + thread.join(5000); + if (thread.isAlive()) { + fail("Thread didn't join"); + } else { + if (thread.failed) { + fail("Did not receive expected message"); + } + } + cnxManager.halt(); + assertFalse(cnxManager.listener.isAlive()); + } + + @Test + public void testCnxManagerTimeout() throws Exception { + int address = ThreadLocalRandom.current().nextInt(1, 255); + int deadPort = PortAssignment.unique(); + String deadAddress = "10.1.1." + address; + + LOG.info("This is the dead address I'm trying: {}", deadAddress); + + peers.put(2L, + new QuorumServer(2, + new InetSocketAddress(deadAddress, deadPort), + new InetSocketAddress(deadAddress, PortAssignment.unique()), + new InetSocketAddress(deadAddress, PortAssignment.unique()))); + peerTmpdir[2] = ClientBase.createTmpDir(); + + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2, 2); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if (listener != null) { + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + + long begin = Time.currentElapsedTime(); + cnxManager.toSend(2L, createMsg(ServerState.LOOKING.ordinal(), 1, -1, 1)); + long end = Time.currentElapsedTime(); + + if ((end - begin) > 10_000) { + fail("Waited more than necessary"); + } + cnxManager.halt(); + assertFalse(cnxManager.listener.isAlive()); + } + + /** + * Tests a bug in QuorumCnxManager that causes a spin lock + * when a negative value is sent. This test checks if the + * connection is being closed upon a message with negative + * length. + * + * @throws Exception + */ + @Test + public void testCnxManagerSpinLock() throws Exception { + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2, 2); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if (listener != null) { + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + + InetSocketAddress address = peers.get(peer.getId()).electionAddr.getReachableOrOne(); + LOG.info("Election port: {}", address.getPort()); + + Thread.sleep(1000); + + SocketChannel sc = SocketChannel.open(); + sc.socket().connect(address, 5000); + + InetSocketAddress otherAddr = peers.get(2L).electionAddr.getReachableOrOne(); + DataOutputStream dout = new DataOutputStream(sc.socket().getOutputStream()); + dout.writeLong(QuorumCnxManager.PROTOCOL_VERSION_V1); + dout.writeLong(2); + String addr = otherAddr.getHostString() + ":" + otherAddr.getPort(); + byte[] addr_bytes = addr.getBytes(); + dout.writeInt(addr_bytes.length); + dout.write(addr_bytes); + dout.flush(); + + ByteBuffer msgBuffer = ByteBuffer.wrap(new byte[4]); + msgBuffer.putInt(-20); + msgBuffer.position(0); + sc.write(msgBuffer); + + Thread.sleep(1000); + + try { + /* + * Write a number of times until it + * detects that the socket is broken. + */ + for (int i = 0; i < 100; i++) { + msgBuffer.position(0); + sc.write(msgBuffer); + } + fail("Socket has not been closed"); + } catch (Exception e) { + LOG.info("Socket has been closed as expected"); + } + peer.shutdown(); + cnxManager.halt(); + assertFalse(cnxManager.listener.isAlive()); + } + + /** + * Test for bug described in https://issues.apache.org/jira/browse/ZOOKEEPER-3320. + * Test create peer with address which contains unresolvable DNS name, + * leader election listener thread should stop after N errors. + * + * @throws Exception + */ + @Test + public void testCnxManagerListenerThreadConfigurableRetry() throws Exception { + final Map unresolvablePeers = new HashMap<>(); + final long myid = 1L; + unresolvablePeers.put(myid, new QuorumServer(myid, "unresolvable-domain.org:2182:2183;2181")); + final QuorumPeer peer = new QuorumPeer(unresolvablePeers, ClientBase.createTmpDir(), ClientBase.createTmpDir(), 2181, 3, myid, 1000, 2, 2, 2); + final QuorumCnxManager cnxManager = peer.createCnxnManager(); + final QuorumCnxManager.Listener listener = cnxManager.listener; + final AtomicBoolean errorHappend = new AtomicBoolean(false); + listener.setSocketBindErrorHandler(() -> errorHappend.set(true)); + listener.start(); + // listener thread should stop and throws error which notify QuorumPeer about error. + // QuorumPeer should start shutdown process + listener.join(15000); // set wait time, if listener contains bug and thread not stops. + assertFalse(listener.isAlive()); + assertTrue(errorHappend.get()); + assertFalse(QuorumPeer.class.getSimpleName() + + " not stopped after " + + "listener thread death", listener.isAlive()); + } + + /** + * Tests a bug in QuorumCnxManager that causes a NPE when a 3.4.6 + * observer connects to a 3.5.0 server. + * see https://issues.apache.org/jira/browse/ZOOKEEPER-1789 + * + * @throws Exception + */ + @Test + public void testCnxManagerNPE() throws Exception { + // the connecting peer (id = 2) is a 3.4.6 observer + peers.get(2L).type = LearnerType.OBSERVER; + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 1000, 2, 2, 2); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if (listener != null) { + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + InetSocketAddress address = peers.get(peer.getId()).electionAddr.getReachableOrOne(); + LOG.info("Election port: {}", address.getPort()); + + Thread.sleep(1000); + + SocketChannel sc = SocketChannel.open(); + sc.socket().connect(address, 5000); + + /* + * Write id (3.4.6 protocol). This previously caused a NPE in + * QuorumCnxManager. + */ + byte[] msgBytes = new byte[8]; + ByteBuffer msgBuffer = ByteBuffer.wrap(msgBytes); + msgBuffer.putLong(2L); + msgBuffer.position(0); + sc.write(msgBuffer); + + msgBuffer = ByteBuffer.wrap(new byte[8]); + // write length of message + msgBuffer.putInt(4); + // write message + msgBuffer.putInt(5); + msgBuffer.position(0); + sc.write(msgBuffer); + + Message m = cnxManager.pollRecvQueue(1000, TimeUnit.MILLISECONDS); + assertNotNull(m); + + peer.shutdown(); + cnxManager.halt(); + assertFalse(cnxManager.listener.isAlive()); + } + + /* + * Test if a receiveConnection is able to timeout on socket errors + */ + @Test + public void testSocketTimeout() throws Exception { + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[1], peerTmpdir[1], peerClientPort[1], 3, 1, 2000, 2, 2, 2); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + QuorumCnxManager.Listener listener = cnxManager.listener; + if (listener != null) { + listener.start(); + } else { + LOG.error("Null listener when initializing cnx manager"); + } + InetSocketAddress address = peers.get(peer.getId()).electionAddr.getReachableOrOne(); + LOG.info("Election port: {}", address.getPort()); + Thread.sleep(1000); + + Socket sock = new Socket(); + sock.connect(address, 5000); + long begin = Time.currentElapsedTime(); + // Read without sending data. Verify timeout. + cnxManager.receiveConnection(sock); + long end = Time.currentElapsedTime(); + if ((end - begin) > ((peer.getSyncLimit() * peer.getTickTime()) + 500)) { + fail("Waited more than necessary"); + } + cnxManager.halt(); + assertFalse(cnxManager.listener.isAlive()); + } + + /** + * Test the SSLSocket is explicitly closed when there is IOException + * happened during connect. + */ + @Test + public void testSSLSocketClosedWhenHandshakeTimeout() throws Exception { + final CountDownLatch closeLatch = new CountDownLatch(1); + QuorumX509Util mockedX509Util = new QuorumX509Util() { + @Override + public SSLSocket createSSLSocket() { + return new SSLSocket() { + + @Override + public void connect(SocketAddress endpoint, int timeout) { + } + + @Override + public void startHandshake() throws IOException { + throw new IOException(); + } + + @Override + public void close() { + closeLatch.countDown(); + } + + public String[] getSupportedCipherSuites() { + throw new UnsupportedOperationException(); + } + + public String[] getEnabledCipherSuites() { + throw new UnsupportedOperationException(); + } + + public String[] getSupportedProtocols() { + throw new UnsupportedOperationException(); + } + + public String[] getEnabledProtocols() { + throw new UnsupportedOperationException(); + } + + public SSLSession getSession() { + throw new UnsupportedOperationException(); + } + + public void setEnabledCipherSuites(String[] suites) { + } + public void setEnabledProtocols(String[] protocols) { + } + public void addHandshakeCompletedListener(HandshakeCompletedListener listener) { + } + public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) { + } + public void setUseClientMode(boolean mode) { + } + public boolean getUseClientMode() { + return true; + } + public void setNeedClientAuth(boolean need) { + } + public boolean getNeedClientAuth() { + return true; + } + public void setWantClientAuth(boolean want) { + } + public boolean getWantClientAuth() { + return true; + } + public void setEnableSessionCreation(boolean flag) { + } + public boolean getEnableSessionCreation() { + return true; + } + }; + } + }; + + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[0], peerTmpdir[0], peerClientPort[0], 3, 0, 2000, 2, 2, 2) { + @Override + public QuorumX509Util createX509Util() { + return mockedX509Util; + } + }; + + peer.setSslQuorum(true); + QuorumCnxManager cnxManager = peer.createCnxnManager(); + cnxManager.connectOne(1, peers.get(1L).electionAddr); + assertTrue(closeLatch.await(1, TimeUnit.SECONDS)); + } + + /* + * Test if Worker threads are getting killed after connection loss + */ + @Test + public void testWorkerThreads() throws Exception { + ArrayList peerList = new ArrayList(); + try { + for (int sid = 0; sid < 3; sid++) { + QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[sid], peerTmpdir[sid], peerClientPort[sid], 3, sid, 1000, 2, 2, 2); + LOG.info("Starting peer {}", peer.getId()); + peer.start(); + peerList.add(sid, peer); + } + String failure = verifyThreadCount(peerList, 4); + assertNull(failure, failure); + for (int myid = 0; myid < 3; myid++) { + for (int i = 0; i < 5; i++) { + // halt one of the listeners and verify count + QuorumPeer peer = peerList.get(myid); + LOG.info("Round {}, halting peer {}", i, peer.getId()); + peer.shutdown(); + peerList.remove(myid); + failure = verifyThreadCount(peerList, 2); + assertNull(failure, failure); + // Restart halted node and verify count + peer = new QuorumPeer(peers, peerTmpdir[myid], peerTmpdir[myid], peerClientPort[myid], 3, myid, 1000, 2, 2, 2); + LOG.info("Round {}, restarting peer {}", i, peer.getId()); + peer.start(); + peerList.add(myid, peer); + failure = verifyThreadCount(peerList, 4); + assertNull(failure, failure); + } + } + } finally { + for (QuorumPeer quorumPeer : peerList) { + quorumPeer.shutdown(); + } + } + } + + /** + * Returns null on success, otw the message assoc with the failure + * @throws InterruptedException + */ + public String verifyThreadCount(ArrayList peerList, long ecnt) throws InterruptedException { + String failure = null; + for (int i = 0; i < 480; i++) { + Thread.sleep(500); + + failure = _verifyThreadCount(peerList, ecnt); + if (failure == null) { + return null; + } + } + return failure; + } + public String _verifyThreadCount(ArrayList peerList, long ecnt) { + for (int myid = 0; myid < peerList.size(); myid++) { + QuorumPeer peer = peerList.get(myid); + QuorumCnxManager cnxManager = peer.getQuorumCnxManager(); + long cnt = cnxManager.getThreadCount(); + if (cnt != ecnt) { + return new Date() + + " Incorrect number of Worker threads for sid=" + myid + + " expected " + ecnt + + " found " + cnt; + } + } + return null; + } + + @Test + public void testInitialMessage() throws Exception { + InitialMessage msg; + ByteArrayOutputStream bos; + DataInputStream din; + DataOutputStream dout; + String hostport; + + // message with bad protocol version + try { + + // the initial message (without the protocol version) + hostport = "10.0.0.2:3888"; + bos = new ByteArrayOutputStream(); + dout = new DataOutputStream(bos); + dout.writeLong(5L); // sid + dout.writeInt(hostport.getBytes().length); + dout.writeBytes(hostport); + + // now parse it + din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); + msg = InitialMessage.parse(-65530L, din); + fail("bad protocol version accepted"); + } catch (InitialMessage.InitialMessageException ex) { + } + + // message too long + try { + + hostport = createLongString(1048576); + bos = new ByteArrayOutputStream(); + dout = new DataOutputStream(bos); + dout.writeLong(5L); // sid + dout.writeInt(hostport.getBytes().length); + dout.writeBytes(hostport); + + din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); + msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION_V1, din); + fail("long message accepted"); + } catch (InitialMessage.InitialMessageException ex) { + } + + // bad hostport string + try { + + hostport = "what's going on here?"; + bos = new ByteArrayOutputStream(); + dout = new DataOutputStream(bos); + dout.writeLong(5L); // sid + dout.writeInt(hostport.getBytes().length); + dout.writeBytes(hostport); + + din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); + msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION_V1, din); + fail("bad hostport accepted"); + } catch (InitialMessage.InitialMessageException ex) { + } + + // good message, single election address + try { + + hostport = "10.0.0.2:3888"; + bos = new ByteArrayOutputStream(); + dout = new DataOutputStream(bos); + dout.writeLong(5L); // sid + dout.writeInt(hostport.getBytes().length); + dout.writeBytes(hostport); + + // now parse it + din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); + msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION_V1, din); + assertEquals(Long.valueOf(5), msg.sid); + assertEquals(Arrays.asList(new InetSocketAddress("10.0.0.2", 3888)), msg.electionAddr); + } catch (InitialMessage.InitialMessageException ex) { + fail(ex.toString()); + } + + // good message, multiple election addresses (ZOOKEEPER-3188) + try { + + hostport = "1.1.1.1:9999|2.2.2.2:8888|3.3.3.3:7777"; + bos = new ByteArrayOutputStream(); + dout = new DataOutputStream(bos); + dout.writeLong(5L); // sid + dout.writeInt(hostport.getBytes().length); + dout.writeBytes(hostport); + + // now parse it + din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); + msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION_V2, din); + assertEquals(Long.valueOf(5), msg.sid); + assertEquals(Arrays.asList(new InetSocketAddress("1.1.1.1", 9999), + new InetSocketAddress("2.2.2.2", 8888), + new InetSocketAddress("3.3.3.3", 7777)), + msg.electionAddr); + } catch (InitialMessage.InitialMessageException ex) { + fail(ex.toString()); + } + } + + @Test + public void testWildcardAddressRecognition() { + assertTrue(QuorumCnxManager.InitialMessage.isWildcardAddress("0.0.0.0")); + assertTrue(QuorumCnxManager.InitialMessage.isWildcardAddress("::")); + assertFalse(QuorumCnxManager.InitialMessage.isWildcardAddress("some.unresolvable.host.com")); + assertFalse(QuorumCnxManager.InitialMessage.isWildcardAddress("127.0.0.1")); + assertFalse(QuorumCnxManager.InitialMessage.isWildcardAddress("255.255.255.255")); + assertFalse(QuorumCnxManager.InitialMessage.isWildcardAddress("1.2.3.4")); + assertFalse(QuorumCnxManager.InitialMessage.isWildcardAddress("www.google.com")); + } + + private String createLongString(int size) { + StringBuilder sb = new StringBuilder(size); + for (int i = 0; i < size; i++) { + sb.append('x'); + } + return sb.toString(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorConcurrencyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorConcurrencyTest.java new file mode 100644 index 0000000..9cd4985 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorConcurrencyTest.java @@ -0,0 +1,654 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.proto.SetDataRequest; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.WorkerService; +import org.apache.zookeeper.server.ZooKeeperServerListener; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CommitProcessorConcurrencyTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(CommitProcessorConcurrencyTest.class); + + BlockingQueue processedRequests; + MockCommitProcessor processor; + int defaultSizeOfThreadPool = 16; + + @Before + public void setUp() throws Exception { + processedRequests = new LinkedBlockingQueue(); + processor = new MockCommitProcessor(); + CommitProcessor.setMaxReadBatchSize(-1); + CommitProcessor.setMaxCommitBatchSize(1); + } + + @After + public void tearDown() throws Exception { + processor.shutdown(); + } + + // This queue is infinite if we use "poll" to get requests, but returns a + // finite size when asked. + class MockRequestsQueue extends LinkedBlockingQueue { + + private static final long serialVersionUID = 1L; + int readReqId = 0; + + // Always have a request to return. + public Request poll() { + readReqId++; + try { + return newRequest(new GetDataRequest("/", false), OpCode.getData, readReqId % 50, readReqId); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + // Fixed queue size. + public int size() { + return 42; + } + + } + + class MockCommitProcessor extends CommitProcessor { + + MockCommitProcessor() { + super(new RequestProcessor() { + public void processRequest(Request request) throws RequestProcessorException { + processedRequests.offer(request); + } + + public void shutdown() { + } + }, "0", false, new ZooKeeperServerListener() { + + @Override + public void notifyStopping(String threadName, int errorCode) { + fail("Commit processor crashed " + errorCode); + } + }); + } + + public void initThreads(int poolSize) { + this.stopped = false; + this.workerPool = new WorkerService("CommitProcWork", poolSize, true); + } + + } + + private Request newRequest(Record rec, int type, int sessionId, int xid) throws IOException { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); + rec.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); + return new Request(null, sessionId, xid, type, bb, new ArrayList()); + } + + /** + * We place a read request followed by committed update request of the same + * session in queuedRequests. We verify that both requests are processed, + * according to the order of the session (first read, then the write). + */ + @Test + public void committedAndUncommittedOfTheSameSessionRaceTest() throws Exception { + final String path = "/testCvsUCRace"; + + Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x0, 0); + Request writeReq = newRequest(new SetDataRequest(path, new byte[16], -1), OpCode.setData, 0x0, 1); + + processor.committedRequests.add(writeReq); + processor.queuedRequests.add(readReq); + processor.queuedRequests.add(writeReq); + processor.queuedWriteRequests.add(writeReq); + processor.initThreads(1); + + processor.stoppedMainLoop = true; + processor.run(); + + assertTrue( + "Request was not processed " + readReq + " instead " + processedRequests.peek(), + processedRequests.peek() != null && processedRequests.peek().equals(readReq)); + processedRequests.poll(); + assertTrue( + "Request was not processed " + writeReq + " instead " + processedRequests.peek(), + processedRequests.peek() != null && processedRequests.peek().equals(writeReq)); + } + + /** + * Here we create the following requests queue structure: R1_1, W1_2, R1_3, + * R2_1, R2_2, W2_3, R2_4, R3_1, R3_2, R3_3, W3_4, R3_5, ... , W5_6, R5_7 + * i.e., 5 sessions, each has different amount or read requests, followed by + * single write and afterwards single read. The idea is to check that all of + * the reads that can be processed concurrently do so, and that none of the + * uncommited requests, followed by the reads are processed. + */ + @Test + public void processAsMuchUncommittedRequestsAsPossibleTest() throws Exception { + final String path = "/testAsMuchAsPossible"; + List shouldBeProcessed = new LinkedList(); + Set shouldNotBeProcessed = new HashSet(); + for (int sessionId = 1; sessionId <= 5; ++sessionId) { + for (int readReqId = 1; readReqId <= sessionId; ++readReqId) { + Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionId, readReqId); + shouldBeProcessed.add(readReq); + processor.queuedRequests.add(readReq); + } + Request writeReq = newRequest( + new CreateRequest( + path, + new byte[0], + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + sessionId, + sessionId + 1); + Request readReq = newRequest( + new GetDataRequest(path, false), + OpCode.getData, + sessionId, + sessionId + 2); + processor.queuedRequests.add(writeReq); + processor.queuedWriteRequests.add(writeReq); + processor.queuedRequests.add(readReq); + shouldNotBeProcessed.add(writeReq); + shouldNotBeProcessed.add(readReq); + } + processor.initThreads(defaultSizeOfThreadPool); + + processor.stoppedMainLoop = true; + processor.run(); + Thread.sleep(1000); + shouldBeProcessed.removeAll(processedRequests); + for (Request r : shouldBeProcessed) { + LOG.error("Did not process {}", r); + } + assertTrue("Not all requests were processed", shouldBeProcessed.isEmpty()); + assertFalse("Processed a wrong request", shouldNotBeProcessed.removeAll(processedRequests)); + } + + /** + * In the following test, we add a write request followed by several read + * requests of the same session, and we verify several things - 1. The write + * is not processed until commit arrives. 2. Once the write is processed, + * all the read requests are processed as well. 3. All read requests are + * executed after the write, before any other write, along with new reads. + */ + @Test + public void processAllFollowingUncommittedAfterFirstCommitTest() throws Exception { + final String path = "/testUncommittedFollowingCommited"; + Set shouldBeInPending = new HashSet(); + Set shouldBeProcessedAfterPending = new HashSet(); + + Request writeReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x1, + 1); + processor.queuedRequests.add(writeReq); + processor.queuedWriteRequests.add(writeReq); + shouldBeInPending.add(writeReq); + + for (int readReqId = 2; readReqId <= 5; ++readReqId) { + Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x1, readReqId); + processor.queuedRequests.add(readReq); + shouldBeInPending.add(readReq); + shouldBeProcessedAfterPending.add(readReq); + } + processor.initThreads(defaultSizeOfThreadPool); + + processor.stoppedMainLoop = true; + processor.run(); + assertTrue("Processed without waiting for commit", processedRequests.isEmpty()); + assertTrue("Did not handled all of queuedRequests' requests", processor.queuedRequests.isEmpty()); + assertTrue("Removed from blockedQueuedRequests before commit", !processor.queuedWriteRequests.isEmpty()); + + shouldBeInPending.removeAll(processor.pendingRequests.get(writeReq.sessionId)); + for (Request r : shouldBeInPending) { + LOG.error("Should be in pending {}", r); + } + assertTrue("Not all requests moved to pending from queuedRequests", shouldBeInPending.isEmpty()); + + processor.committedRequests.add(writeReq); + processor.stoppedMainLoop = true; + processor.run(); + processor.initThreads(defaultSizeOfThreadPool); + + Thread.sleep(500); + assertTrue("Did not process committed request", processedRequests.peek() == writeReq); + assertTrue("Did not process following read request", processedRequests.containsAll(shouldBeProcessedAfterPending)); + assertTrue("Did not process committed request", processor.committedRequests.isEmpty()); + assertTrue("Did not process committed request", processor.pendingRequests.isEmpty()); + assertTrue("Did not remove from blockedQueuedRequests", processor.queuedWriteRequests.isEmpty()); + } + + /** + * In the following test, we add a write request followed by several read + * requests of the same session. We will do this for 2 sessions. For the + * second session, we will queue up another write after the reads, and + * we verify several things - 1. The writes are not processed until + * the commits arrive. 2. Only 2 writes are processed, with maxCommitBatchSize + * of 3, due to the blocking reads. 3. Once the writes are processed, + * all the read requests are processed as well. 4. All read requests are + * executed after the write, before any other write for that session, + * along with new reads. 5. Then we add another read for session 1, and + * another write and commit for session 2. 6. Only the old write, and the read + * are processed, leaving the commit in the queue. 7. Last write is executed + * in the last iteration, and all lists are empty. + */ + @Test + public void processAllWritesMaxBatchSize() throws Exception { + final String path = "/processAllWritesMaxBatchSize"; + HashSet shouldBeProcessedAfterPending = new HashSet(); + + Request writeReq = newRequest( + new CreateRequest( + path + "_1", + new byte[0], + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x1, + 1); + processor.queuedRequests.add(writeReq); + processor.queuedWriteRequests.add(writeReq); + + Request writeReq2 = newRequest( + new CreateRequest( + path + "_2", + new byte[0], + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x2, + 1); + processor.queuedRequests.add(writeReq2); + processor.queuedWriteRequests.add(writeReq2); + + for (int readReqId = 2; readReqId <= 5; ++readReqId) { + Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x1, readReqId); + Request readReq2 = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x2, readReqId); + processor.queuedRequests.add(readReq); + shouldBeProcessedAfterPending.add(readReq); + processor.queuedRequests.add(readReq2); + shouldBeProcessedAfterPending.add(readReq2); + } + + Request writeReq3 = newRequest( + new CreateRequest( + path + "_3", + new byte[0], + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x2, + 6); + processor.queuedRequests.add(writeReq3); + processor.queuedWriteRequests.add(writeReq3); + + processor.initThreads(defaultSizeOfThreadPool); + + processor.stoppedMainLoop = true; + CommitProcessor.setMaxCommitBatchSize(2); + processor.run(); + assertTrue("Processed without waiting for commit", processedRequests.isEmpty()); + assertTrue("Did not handled all of queuedRequests' requests", processor.queuedRequests.isEmpty()); + assertTrue("Removed from blockedQueuedRequests before commit", !processor.queuedWriteRequests.isEmpty()); + assertTrue("Missing session 1 in pending queue", processor.pendingRequests.containsKey(writeReq.sessionId)); + assertTrue("Missing session 2 in pending queue", processor.pendingRequests.containsKey(writeReq2.sessionId)); + + processor.committedRequests.add(writeReq); + processor.committedRequests.add(writeReq2); + processor.committedRequests.add(writeReq3); + processor.stoppedMainLoop = true; + CommitProcessor.setMaxCommitBatchSize(3); + processor.run(); + processor.initThreads(defaultSizeOfThreadPool); + + Thread.sleep(500); + assertTrue("Did not process committed request", processedRequests.peek() == writeReq); + assertTrue("Did not process following read request", processedRequests.containsAll(shouldBeProcessedAfterPending)); + assertTrue("Processed committed request", !processor.committedRequests.isEmpty()); + assertTrue("Removed commit for write req 3", processor.committedRequests.peek() == writeReq3); + assertTrue("Processed committed request", !processor.pendingRequests.isEmpty()); + assertTrue("Missing session 2 in pending queue", processor.pendingRequests.containsKey(writeReq3.sessionId)); + assertTrue( + "Missing write 3 in pending queue", + processor.pendingRequests.get(writeReq3.sessionId).peek() == writeReq3); + assertTrue( + "Removed from blockedQueuedRequests", + !processor.queuedWriteRequests.isEmpty()); + assertTrue( + "Removed write req 3 from blockedQueuedRequests", + processor.queuedWriteRequests.peek() == writeReq3); + + Request readReq3 = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x1, 7); + processor.queuedRequests.add(readReq3); + shouldBeProcessedAfterPending.add(readReq3); + Request writeReq4 = newRequest( + new CreateRequest( + path + "_4", + new byte[0], + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x2, + 7); + + processor.queuedRequests.add(writeReq4); + processor.queuedWriteRequests.add(writeReq4); + processor.committedRequests.add(writeReq4); + + processor.stoppedMainLoop = true; + CommitProcessor.setMaxCommitBatchSize(3); + processor.run(); + processor.initThreads(defaultSizeOfThreadPool); + + Thread.sleep(500); + assertTrue("Did not process committed request", processedRequests.peek() == writeReq); + assertTrue("Did not process following read request", processedRequests.containsAll(shouldBeProcessedAfterPending)); + assertTrue("Processed unexpected committed request", !processor.committedRequests.isEmpty()); + assertTrue("Unexpected pending request", processor.pendingRequests.isEmpty()); + assertTrue("Removed from blockedQueuedRequests", !processor.queuedWriteRequests.isEmpty()); + assertTrue( + "Removed write req 4 from blockedQueuedRequests", + processor.queuedWriteRequests.peek() == writeReq4); + + processor.stoppedMainLoop = true; + CommitProcessor.setMaxCommitBatchSize(3); + processor.run(); + processor.initThreads(defaultSizeOfThreadPool); + + Thread.sleep(500); + assertTrue("Did not process committed request", processedRequests.peek() == writeReq); + assertTrue("Did not process following read request", processedRequests.containsAll(shouldBeProcessedAfterPending)); + assertTrue("Did not process committed request", processor.committedRequests.isEmpty()); + assertTrue("Did not process committed request", processor.pendingRequests.isEmpty()); + assertTrue("Did not remove from blockedQueuedRequests", processor.queuedWriteRequests.isEmpty()); + + } + + /** + * In the following test, we verify that committed requests are processed + * even when queuedRequests never gets empty. We add 10 committed request + * and use infinite queuedRequests. We verify that the committed request was + * processed. + */ + @Test(timeout = 1000) + public void noStarvationOfNonLocalCommittedRequestsTest() throws Exception { + final String path = "/noStarvationOfCommittedRequests"; + processor.queuedRequests = new MockRequestsQueue(); + Set nonLocalCommits = new HashSet(); + for (int i = 0; i < 10; i++) { + Request nonLocalCommitReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 51, + i + 1); + processor.committedRequests.add(nonLocalCommitReq); + nonLocalCommits.add(nonLocalCommitReq); + } + for (int i = 0; i < 10; i++) { + processor.initThreads(defaultSizeOfThreadPool); + processor.stoppedMainLoop = true; + processor.run(); + } + assertTrue("commit request was not processed", processedRequests.containsAll(nonLocalCommits)); + } + + /** + * In the following test, we verify that committed writes are not causing + * reads starvation. We populate the commit processor with the following + * order of requests: 1 committed local updated, 1 read request, 100 + * committed non-local updates. 50 read requests. We verify that after the + * first call to processor.run, only the first write is processed, then + * after the second call, all reads are processed along with the second + * write. + */ + @Test + public void noStarvationOfReadRequestsTest() throws Exception { + final String path = "/noStarvationOfReadRequests"; + + // +1 committed requests (also head of queuedRequests) + Request firstCommittedReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x3, + 1); + processor.queuedRequests.add(firstCommittedReq); + processor.queuedWriteRequests.add(firstCommittedReq); + processor.committedRequests.add(firstCommittedReq); + Set allReads = new HashSet(); + + // +1 read request to queuedRequests + Request firstRead = newRequest(new GetDataRequest(path, false), OpCode.getData, 0x1, 0); + allReads.add(firstRead); + processor.queuedRequests.add(firstRead); + + // +1 non local commit + Request secondCommittedReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x99, + 2); + processor.committedRequests.add(secondCommittedReq); + + Set waitingCommittedRequests = new HashSet(); + // +99 non local committed requests + for (int writeReqId = 3; writeReqId < 102; ++writeReqId) { + Request writeReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + 0x8, + writeReqId); + processor.committedRequests.add(writeReq); + waitingCommittedRequests.add(writeReq); + } + + // +50 read requests to queuedRequests + for (int readReqId = 1; readReqId <= 50; ++readReqId) { + Request readReq = newRequest( + new GetDataRequest(path, false), + OpCode.getData, + 0x5, + readReqId); + allReads.add(readReq); + processor.queuedRequests.add(readReq); + } + + processor.initThreads(defaultSizeOfThreadPool); + + processor.stoppedMainLoop = true; + processor.run(); + assertTrue("Did not process the first write request", processedRequests.contains(firstCommittedReq)); + for (Request r : allReads) { + assertTrue("Processed read request", !processedRequests.contains(r)); + } + processor.run(); + assertTrue("did not processed all reads", processedRequests.containsAll(allReads)); + assertTrue("Did not process the second write request", processedRequests.contains(secondCommittedReq)); + for (Request r : waitingCommittedRequests) { + assertTrue("Processed additional committed request", !processedRequests.contains(r)); + } + } + + /** + * In the following test, we verify that we can handle the case that we got a commit + * of a request we never seen since the session that we just established. This can happen + * when a session is just established and there is request waiting to be committed in the + * session queue but it sees a commit for a request that belongs to the previous connection. + */ + @Test(timeout = 5000) + public void noCrashOnCommittedRequestsOfUnseenRequestTest() throws Exception { + final String path = "/noCrash/OnCommittedRequests/OfUnseenRequestTest"; + final int numberofReads = 10; + final int sessionid = 0x123456; + final int firstCXid = 0x100; + int readReqId = firstCXid; + processor.stoppedMainLoop = true; + HashSet localRequests = new HashSet(); + // queue the blocking write request to queuedRequests + Request firstCommittedReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + sessionid, + readReqId++); + processor.queuedRequests.add(firstCommittedReq); + processor.queuedWriteRequests.add(firstCommittedReq); + localRequests.add(firstCommittedReq); + + // queue read requests to queuedRequests + for (; readReqId <= numberofReads + firstCXid; ++readReqId) { + Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionid, readReqId); + processor.queuedRequests.add(readReq); + localRequests.add(readReq); + } + + //run once + assertTrue(processor.queuedRequests.containsAll(localRequests)); + processor.initThreads(defaultSizeOfThreadPool); + processor.run(); + Thread.sleep(1000); + + //We verify that the processor is waiting for the commit + assertTrue(processedRequests.isEmpty()); + + // We add a commit that belongs to the same session but with smaller cxid, + // i.e., commit of an update from previous connection of this session. + Request preSessionCommittedReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + sessionid, + firstCXid - 2); + processor.committedRequests.add(preSessionCommittedReq); + processor.committedRequests.add(firstCommittedReq); + processor.run(); + Thread.sleep(1000); + + //We verify that the commit processor processed the old commit prior to the newer messages + assertTrue(processedRequests.peek() == preSessionCommittedReq); + + processor.run(); + Thread.sleep(1000); + + //We verify that the commit processor handle all messages. + assertTrue(processedRequests.containsAll(localRequests)); + } + + /** + * In the following test, we verify if we handle the case in which we get a commit + * for a request that has higher Cxid than the one we are waiting. This can happen + * when a session connection is lost but there is a request waiting to be committed in the + * session queue. However, since the session has moved, new requests can get to + * the leader out of order. Hence, the commits can also arrive "out of order" w.r.t. cxid. + * We should commit the requests according to the order we receive from the leader, i.e., wait for the relevant commit. + */ + @Test(timeout = 5000) + public void noCrashOnOutofOrderCommittedRequestTest() throws Exception { + final String path = "/noCrash/OnCommittedRequests/OfUnSeenRequestTest"; + final int sessionid = 0x123456; + final int lastCXid = 0x100; + final int numberofReads = 10; + int readReqId = lastCXid; + processor.stoppedMainLoop = true; + HashSet localRequests = new HashSet(); + + // queue the blocking write request to queuedRequests + Request orphanCommittedReq = newRequest( + new CreateRequest(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + sessionid, + lastCXid); + processor.queuedRequests.add(orphanCommittedReq); + processor.queuedWriteRequests.add(orphanCommittedReq); + localRequests.add(orphanCommittedReq); + + // queue read requests to queuedRequests + for (; readReqId <= numberofReads + lastCXid; ++readReqId) { + Request readReq = newRequest(new GetDataRequest(path, false), OpCode.getData, sessionid, readReqId); + processor.queuedRequests.add(readReq); + localRequests.add(readReq); + } + + //run once + processor.initThreads(defaultSizeOfThreadPool); + processor.run(); + Thread.sleep(1000); + + //We verify that the processor is waiting for the commit + assertTrue(processedRequests.isEmpty()); + + // We add a commit that belongs to the same session but with larger cxid, + // i.e., commit of an update from the next connection of this session. + Request otherSessionCommittedReq = newRequest( + new CreateRequest( + path, + new byte[0], + Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT_SEQUENTIAL.toFlag()), + OpCode.create, + sessionid, + lastCXid + 10); + processor.committedRequests.add(otherSessionCommittedReq); + processor.committedRequests.add(orphanCommittedReq); + processor.run(); + Thread.sleep(1000); + + //We verify that the commit processor processed the old commit prior to the newer messages + assertTrue(processedRequests.size() == 1); + assertTrue(processedRequests.contains(otherSessionCommittedReq)); + + processor.run(); + Thread.sleep(1000); + + //We verify that the commit processor handle all messages. + assertTrue(processedRequests.containsAll(localRequests)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorMetricsTest.java new file mode 100644 index 0000000..29fe94b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorMetricsTest.java @@ -0,0 +1,507 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.lessThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import java.nio.ByteBuffer; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.WorkerService; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CommitProcessorMetricsTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(CommitProcessorMetricsTest.class); + CommitProcessor commitProcessor; + DummyFinalProcessor finalProcessor; + + CountDownLatch requestScheduled = null; + CountDownLatch requestProcessed = null; + CountDownLatch commitSeen = null; + CountDownLatch poolEmpytied = null; + + @Before + public void setup() { + LOG.info("setup"); + ServerMetrics.getMetrics().resetAll(); + + // ensure no leaked parallelism properties + System.clearProperty("zookeeper.commitProcessor.maxReadBatchSize"); + System.clearProperty("zookeeper.commitProcessor.maxCommitBatchSize"); + } + + public void setupProcessors(int commitWorkers, int finalProcTime) { + finalProcessor = new DummyFinalProcessor(finalProcTime); + commitProcessor = new TestCommitProcessor(finalProcessor, commitWorkers); + commitProcessor.start(); + } + + @After + public void tearDown() throws Exception { + LOG.info("tearDown starting"); + + commitProcessor.shutdown(); + commitProcessor.join(); + } + + private class TestCommitProcessor extends CommitProcessor { + + int numWorkerThreads; + + public TestCommitProcessor(RequestProcessor finalProcessor, int numWorkerThreads) { + super(finalProcessor, "1", true, null); + this.numWorkerThreads = numWorkerThreads; + } + + @Override + public void start() { + super.workerPool = new TestWorkerService(numWorkerThreads); + super.start(); + // Since there are two threads--the test thread that puts requests into the queue and the processor + // thread (this thread) that removes requests from the queue--the execution order in general is + // indeterminate, making it hard to check the test results. + // + // In some tests, we really want the requests processed one by one. To achieve this, we make sure that + // things happen in this order: + // processor thread gets into WAITING -> test thread sets requestProcessed latch -> test thread puts + // a request into the queue (which wakes up the processor thread in the WAITING state) and waits for + // the requestProcessed latch -> the processor thread wakes up and removes the request from the queue and + // processes it and opens the requestProcessed latch -> the test thread continues onto the next request + + // So it is important for the processor thread to get into WAITING before any request is put into the queue. + // Otherwise, it would miss the wakeup signal and wouldn't process the request or open the latch and the + // test thread waiting on the latch would be stuck + Thread.State state = super.getState(); + while (state != State.WAITING) { + try { + Thread.sleep(50); + } catch (Exception e) { + + } + state = super.getState(); + } + LOG.info("numWorkerThreads in Test is {}", numWorkerThreads); + } + + @Override + protected void endOfIteration() { + if (requestProcessed != null) { + requestProcessed.countDown(); + } + } + + @Override + protected void waitForEmptyPool() throws InterruptedException { + if (commitSeen != null) { + commitSeen.countDown(); + } + super.waitForEmptyPool(); + if (poolEmpytied != null) { + poolEmpytied.countDown(); + } + } + + } + + private class TestWorkerService extends WorkerService { + + public TestWorkerService(int numWorkerThreads) { + super("CommitProcWork", numWorkerThreads, true); + } + + @Override + public void schedule(WorkRequest workRequest, long id) { + super.schedule(workRequest, id); + if (requestScheduled != null) { + requestScheduled.countDown(); + } + } + + } + + private class DummyFinalProcessor implements RequestProcessor { + + int processTime; + public DummyFinalProcessor(int processTime) { + this.processTime = processTime; + } + + @Override + public void processRequest(Request request) { + if (processTime > 0) { + try { + if (commitSeen != null) { + commitSeen.await(5, TimeUnit.SECONDS); + } + Thread.sleep(processTime); + } catch (Exception e) { + + } + } + } + + @Override + public void shutdown() { + } + + } + + private void checkMetrics(String metricName, long min, long max, double avg, long cnt, long sum) { + Map values = MetricsUtils.currentServerMetrics(); + + assertEquals("expected min is " + min, min, values.get("min_" + metricName)); + assertEquals("expected max is: " + max, max, values.get("max_" + metricName)); + assertEquals("expected avg is: " + avg, avg, (Double) values.get("avg_" + metricName), 0.001); + assertEquals("expected cnt is: " + cnt, cnt, values.get("cnt_" + metricName)); + assertEquals("expected sum is: " + sum, sum, values.get("sum_" + metricName)); + } + + private void checkTimeMetric(long actual, long lBoundrary, long hBoundrary) { + assertThat(actual, greaterThanOrEqualTo(lBoundrary)); + assertThat(actual, lessThanOrEqualTo(hBoundrary)); + } + + private Request createReadRequest(long sessionId, int xid) { + return new Request(null, sessionId, xid, ZooDefs.OpCode.getData, ByteBuffer.wrap(new byte[10]), null); + } + + private Request createWriteRequest(long sessionId, int xid) { + return new Request(null, sessionId, xid, ZooDefs.OpCode.setData, ByteBuffer.wrap(new byte[10]), null); + } + + private void processRequestWithWait(Request request) throws Exception { + requestProcessed = new CountDownLatch(1); + commitProcessor.processRequest(request); + requestProcessed.await(5, TimeUnit.SECONDS); + } + + private void commitWithWait(Request request) throws Exception { + requestProcessed = new CountDownLatch(1); + commitProcessor.commit(request); + requestProcessed.await(5, TimeUnit.SECONDS); + } + + @Test + public void testRequestsInSessionQueue() throws Exception { + setupProcessors(0, 0); + + Request req1 = createWriteRequest(1L, 1); + processRequestWithWait(req1); + + checkMetrics("requests_in_session_queue", 1L, 1L, 1D, 1L, 1L); + + //these two read requests will be stuck in the session queue because there is write in front of them + processRequestWithWait(createReadRequest(1L, 2)); + processRequestWithWait(createReadRequest(1L, 3)); + + checkMetrics("requests_in_session_queue", 1L, 3L, 2D, 3L, 6); + + commitWithWait(req1); + + checkMetrics("requests_in_session_queue", 1L, 3L, 2.25D, 4L, 9); + } + + @Test + public void testWriteFinalProcTime() throws Exception { + setupProcessors(0, 1000); + + Request req1 = createWriteRequest(1L, 2); + processRequestWithWait(req1); + + //no request sent to next processor yet + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(0L, values.get("cnt_write_final_proc_time_ms")); + + commitWithWait(req1); + + values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("cnt_write_final_proc_time_ms")); + checkTimeMetric((long) values.get("max_write_final_proc_time_ms"), 1000L, 2000L); + } + + @Test + public void testReadFinalProcTime() throws Exception { + setupProcessors(0, 1000); + + processRequestWithWait(createReadRequest(1L, 1)); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("cnt_read_final_proc_time_ms")); + checkTimeMetric((long) values.get("max_read_final_proc_time_ms"), 1000L, 2000L); + } + + @Test + public void testCommitProcessTime() throws Exception { + setupProcessors(0, 0); + processRequestWithWait(createReadRequest(1L, 1)); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("cnt_commit_process_time")); + checkTimeMetric((long) values.get("max_commit_process_time"), 0L, 1000L); + } + + @Test + public void testServerWriteCommittedTime() throws Exception { + setupProcessors(0, 0); + //a commit w/o pending request is a write from other servers + commitWithWait(createWriteRequest(1L, 1)); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(1L, values.get("cnt_server_write_committed_time_ms")); + checkTimeMetric((long) values.get("max_server_write_committed_time_ms"), 0L, 1000L); + } + + @Test + public void testLocalWriteCommittedTime() throws Exception { + setupProcessors(0, 0); + Request req1 = createWriteRequest(1L, 2); + processRequestWithWait(req1); + commitWithWait(req1); + + Map values = MetricsUtils.currentServerMetrics(); + + assertEquals(1L, values.get("cnt_local_write_committed_time_ms")); + checkTimeMetric((long) values.get("max_local_write_committed_time_ms"), 0L, 1000L); + + Request req2 = createWriteRequest(1L, 2); + processRequestWithWait(req2); + //the second write will be stuck in the session queue for at least one second + //but the LOCAL_WRITE_COMMITTED_TIME is from when the commit is received + Thread.sleep(1000); + + commitWithWait(req2); + + values = MetricsUtils.currentServerMetrics(); + assertEquals(2L, values.get("cnt_local_write_committed_time_ms")); + checkTimeMetric((long) values.get("max_local_write_committed_time_ms"), 0L, 1000L); + } + + @Test + public void testWriteCommitProcTime() throws Exception { + setupProcessors(0, 0); + Request req1 = createWriteRequest(1L, 2); + processRequestWithWait(req1); + commitWithWait(req1); + + Map values = MetricsUtils.currentServerMetrics(); + + assertEquals(1L, values.get("cnt_write_commitproc_time_ms")); + checkTimeMetric((long) values.get("max_write_commitproc_time_ms"), 0L, 1000L); + + Request req2 = createWriteRequest(1L, 2); + processRequestWithWait(req2); + //the second write will be stuck in the session queue for at least one second + Thread.sleep(1000); + + commitWithWait(req2); + + values = MetricsUtils.currentServerMetrics(); + assertEquals(2L, values.get("cnt_write_commitproc_time_ms")); + checkTimeMetric((long) values.get("max_write_commitproc_time_ms"), 1000L, 2000L); + } + + @Test + public void testReadCommitProcTime() throws Exception { + setupProcessors(0, 0); + processRequestWithWait(createReadRequest(1L, 1)); + + Map values = MetricsUtils.currentServerMetrics(); + + assertEquals(1L, values.get("cnt_read_commitproc_time_ms")); + checkTimeMetric((long) values.get("max_read_commitproc_time_ms"), 0L, 1000L); + + Request req1 = createWriteRequest(1L, 2); + processRequestWithWait(req1); + processRequestWithWait(createReadRequest(1L, 3)); + //the second read will be stuck in the session queue for at least one second + Thread.sleep(1000); + + commitWithWait(req1); + + values = MetricsUtils.currentServerMetrics(); + assertEquals(2L, values.get("cnt_read_commitproc_time_ms")); + checkTimeMetric((long) values.get("max_read_commitproc_time_ms"), 1000L, 2000L); + } + + @Test + public void testTimeWaitingEmptyPoolInCommitProcessorRead() throws Exception { + setupProcessors(1, 1000); + + //three read requests will be scheduled first + requestScheduled = new CountDownLatch(3); + commitProcessor.processRequest(createReadRequest(0L, 2)); + commitProcessor.processRequest(createReadRequest(1L, 3)); + commitProcessor.processRequest(createReadRequest(2L, 4)); + requestScheduled.await(5, TimeUnit.SECONDS); + + //add a commit request to trigger waitForEmptyPool + poolEmpytied = new CountDownLatch(1); + commitProcessor.commit(createWriteRequest(1L, 1)); + poolEmpytied.await(5, TimeUnit.SECONDS); + + long actual = (long) MetricsUtils.currentServerMetrics().get("max_time_waiting_empty_pool_in_commit_processor_read_ms"); + //since each request takes 1000ms to process, so the waiting shouldn't be more than three times of that + checkTimeMetric(actual, 2500L, 3500L); + } + + @Test + public void testConcurrentRequestProcessingInCommitProcessor() throws Exception { + setupProcessors(3, 1000); + + //three read requests will be processed in parallel + commitSeen = new CountDownLatch(1); + requestScheduled = new CountDownLatch(3); + commitProcessor.processRequest(createReadRequest(1L, 2)); + commitProcessor.processRequest(createReadRequest(1L, 3)); + commitProcessor.processRequest(createReadRequest(1L, 4)); + requestScheduled.await(5, TimeUnit.SECONDS); + + //add a commit request to trigger waitForEmptyPool, which will record number of requests being proccessed + poolEmpytied = new CountDownLatch(1); + commitProcessor.commit(createWriteRequest(1L, 1)); + poolEmpytied.await(5, TimeUnit.SECONDS); + + //this will change after we upstream batch write in CommitProcessor + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(3L, values.get("max_concurrent_request_processing_in_commit_processor")); + } + + @Test + public void testReadsAfterWriteInSessionQueue() throws Exception { + setupProcessors(0, 0); + //this read request is before write + processRequestWithWait(createReadRequest(1L, 1)); + + //one write request + Request req1 = createWriteRequest(1L, 1); + processRequestWithWait(req1); + + //three read requests after the write + processRequestWithWait(createReadRequest(1L, 2)); + processRequestWithWait(createReadRequest(1L, 3)); + processRequestWithWait(createReadRequest(1L, 4)); + + //commit the write + commitWithWait(req1); + + checkMetrics("reads_after_write_in_session_queue", 3L, 3L, 3d, 1, 3); + } + + @Test + public void testReadsQueuedInCommitProcessor() throws Exception { + setupProcessors(0, 0); + processRequestWithWait(createReadRequest(1L, 1)); + processRequestWithWait(createReadRequest(1L, 2)); + + //recorded reads in the queue are 1, 1 + checkMetrics("read_commit_proc_req_queued", 1L, 1L, 1d, 2, 2); + } + + @Test + public void testWritesQueuedInCommitProcessor() throws Exception { + setupProcessors(0, 0); + Request req1 = createWriteRequest(1L, 1); + processRequestWithWait(req1); + Request req2 = createWriteRequest(1L, 2); + processRequestWithWait(req2); + + //since we haven't got any commit request, the write request stays in the queue + //recorded writes in the queue are 1, 2 + checkMetrics("write_commit_proc_req_queued", 1L, 2L, 1.5d, 2, 3); + + commitWithWait(req1); + + //recording is done before commit request is processed, so writes in the queue are: 1, 2, 2 + checkMetrics("write_commit_proc_req_queued", 1L, 2L, 1.6667d, 3, 5); + + commitWithWait(req2); + //writes in the queue are 1, 2, 2, 1 + checkMetrics("write_commit_proc_req_queued", 1L, 2L, 1.5d, 4, 6); + + //send a read request to trigger the recording, this time the write queue should be empty + //writes in the queue are 1, 2, 2, 1, 0 + processRequestWithWait(createReadRequest(1L, 1)); + + checkMetrics("write_commit_proc_req_queued", 0L, 2L, 1.2d, 5, 6); + } + + @Test + public void testCommitsQueuedInCommitProcessor() throws Exception { + setupProcessors(0, 0); + + commitWithWait(createWriteRequest(1L, 1)); + commitWithWait(createWriteRequest(1L, 2)); + + //recorded commits in the queue are 1, 1 + checkMetrics("commit_commit_proc_req_queued", 1L, 1L, 1d, 2, 2); + } + + @Test + public void testCommitsQueued() throws Exception { + setupProcessors(0, 0); + + commitWithWait(createWriteRequest(1L, 1)); + commitWithWait(createWriteRequest(1L, 2)); + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(2L, (long) values.get("request_commit_queued")); + } + + @Test + public void testPendingSessionQueueSize() throws Exception { + setupProcessors(0, 0); + + //one write request for session 1 + Request req1 = createWriteRequest(1L, 1); + processRequestWithWait(req1); + + //two write requests for session 2 + Request req2 = createWriteRequest(2L, 2); + processRequestWithWait(req2); + Request req3 = createWriteRequest(2L, 3); + processRequestWithWait(req3); + + commitWithWait(req1); + //there are two sessions with pending requests + checkMetrics("pending_session_queue_size", 2L, 2L, 2d, 1, 2); + + commitWithWait(req2); + //there is on session with pending requests + checkMetrics("pending_session_queue_size", 1L, 2L, 1.5d, 2, 3); + + commitWithWait(req3); + //there is one session with pending requests + checkMetrics("pending_session_queue_size", 1L, 2L, 1.333d, 3, 4); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorTest.java new file mode 100644 index 0000000..d939dc0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CommitProcessorTest.java @@ -0,0 +1,511 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.GetDataRequest; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.PrepRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The following are invariant regardless of the particular implementation + * of the CommitProcessor, and are tested for: + * + * 1. For each session, requests are processed and the client sees its + * responses in order. + * 2. Write requests are processed in zxid order across all sessions. + * + * The following are also tested for here, but are specific to this + * particular implementation. The underlying issue is that watches can be + * reset while reading the data. For reads/writes on two different sessions + * on different nodes, or with reads that do not set watches, the reads can + * happen in any order relative to the writes. For a read in one session that + * resets a watch that is triggered by a write on another session, however, + * we need to ensure that there is no race condition + * + * 3. The pipeline needs to be drained before a write request can enter. + * 4. No in-flight write requests while processing a read request. + */ +public class CommitProcessorTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(CommitProcessorTest.class); + + // The amount of ms each test case should run + static final int TEST_RUN_TIME_IN_MS = 5000; + private AtomicInteger processedReadRequests = new AtomicInteger(0); + private AtomicInteger processedWriteRequests = new AtomicInteger(0); + + boolean stopped; + TestZooKeeperServer zks; + File tmpDir; + ArrayList testClients = new ArrayList(); + CommitProcessor commitProcessor; + + public void setUp(int numCommitThreads, int numClientThreads, int writePercent) throws Exception { + stopped = false; + System.setProperty(CommitProcessor.ZOOKEEPER_COMMIT_PROC_NUM_WORKER_THREADS, Integer.toString(numCommitThreads)); + tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + zks = new TestZooKeeperServer(tmpDir, tmpDir, 4000); + zks.startup(); + for (int i = 0; i < numClientThreads; ++i) { + TestClientThread client = new TestClientThread(writePercent); + testClients.add(client); + client.start(); + } + } + + public void setUp( + int numCommitThreads, + int numReadOnlyClientThreads, + int mixWorkloadClientThreads, + int writePercent) throws Exception { + stopped = false; + System.setProperty(CommitProcessor.ZOOKEEPER_COMMIT_PROC_NUM_WORKER_THREADS, Integer.toString(numCommitThreads)); + tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + zks = new TestZooKeeperServer(tmpDir, tmpDir, 4000); + zks.startup(); + for (int i = 0; i < mixWorkloadClientThreads; ++i) { + TestClientThread client = new TestClientThread(writePercent); + testClients.add(client); + client.start(); + } + for (int i = 0; i < numReadOnlyClientThreads; ++i) { + TestClientThread client = new TestClientThread(0); + testClients.add(client); + client.start(); + } + } + + @After + public void tearDown() throws Exception { + LOG.info("tearDown starting"); + stopped = true; + + zks.shutdown(); + for (TestClientThread client : testClients) { + client.interrupt(); + client.join(); + } + if (tmpDir != null) { + assertTrue("delete " + tmpDir.toString(), ClientBase.recursiveDelete(tmpDir)); + } + processedReadRequests.set(0); + processedWriteRequests.set(0); + testClients.clear(); + commitProcessor.join(); + } + + private class TestClientThread extends Thread { + + long sessionId; + int cxid; + int nodeId; + int writePercent; + + public TestClientThread(int writePercent) { + sessionId = zks.getSessionTracker().createSession(5000); + this.writePercent = writePercent; + } + + public void sendWriteRequest() throws Exception { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); + CreateRequest createReq = new CreateRequest("/session" + + Long.toHexString(sessionId) + + "-" + + (++nodeId), new byte[0], Ids.OPEN_ACL_UNSAFE, 1); + createReq.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); + Request req = new Request(null, sessionId, ++cxid, OpCode.create, bb, new ArrayList()); + zks.getFirstProcessor().processRequest(req); + + } + + public void sendReadRequest() throws Exception { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); + GetDataRequest getDataRequest = new GetDataRequest("/session" + + Long.toHexString(sessionId) + + "-" + + nodeId, false); + getDataRequest.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); + Request req = new Request(null, sessionId, ++cxid, OpCode.getData, bb, new ArrayList()); + zks.getFirstProcessor().processRequest(req); + } + + public void run() { + Random rand = new Random(Thread.currentThread().getId()); + try { + sendWriteRequest(); + while (!stopped) { + if (rand.nextInt(100) < writePercent) { + sendWriteRequest(); + } else { + sendReadRequest(); + } + Thread.sleep(5 + rand.nextInt(95)); + } + } catch (Exception e) { + LOG.error("Uncaught exception in test: ", e); + } + } + + } + + @Test + public void testNoCommitWorkersReadOnlyWorkload() throws Exception { + int numClients = 10; + LOG.info("testNoCommitWorkersReadOnlyWorkload"); + setUp(0, numClients, 0); + synchronized (this) { + wait(TEST_RUN_TIME_IN_MS); + } + assertFalse(fail); + assertTrue("No read requests processed", processedReadRequests.get() > 0); + // processedWriteRequests.get() == numClients since each client performs one write at the beginning (creates a znode) + assertTrue("Write requests processed", processedWriteRequests.get() == numClients); + } + + @Test + public void testNoCommitWorkersMixedWorkload() throws Exception { + int numClients = 10; + LOG.info("testNoCommitWorkersMixedWorkload 25w/75r workload test"); + setUp(0, numClients, 25); + synchronized (this) { + wait(TEST_RUN_TIME_IN_MS); + } + assertFalse(fail); + checkProcessedRequest(); + } + + @Test + public void testOneCommitWorkerReadOnlyWorkload() throws Exception { + int numClients = 10; + LOG.info("testOneCommitWorkerReadOnlyWorkload"); + setUp(1, numClients, 0); + synchronized (this) { + wait(TEST_RUN_TIME_IN_MS); + } + assertFalse(fail); + assertTrue("No read requests processed", processedReadRequests.get() > 0); + // processedWriteRequests.get() == numClients since each client performs one write at the beginning (creates a znode) + assertTrue("Write requests processed", processedWriteRequests.get() == numClients); + } + + @Test + public void testOneCommitWorkerMixedWorkload() throws Exception { + setUp(1, 10, 25); + LOG.info("testOneCommitWorkerMixedWorkload 25w/75r workload test"); + synchronized (this) { + wait(TEST_RUN_TIME_IN_MS); + } + assertFalse(fail); + checkProcessedRequest(); + } + + @Test + public void testManyCommitWorkersReadOnly() throws Exception { + int numClients = 10; + LOG.info("testManyCommitWorkersReadOnly"); + setUp(10, numClients, 0); + synchronized (this) { + wait(TEST_RUN_TIME_IN_MS); + } + assertFalse(fail); + assertTrue("No read requests processed", processedReadRequests.get() > 0); + // processedWriteRequests.get() == numClients since each client performs one write at the beginning (creates a znode) + assertTrue("Write requests processed", processedWriteRequests.get() == numClients); + } + + @Test + public void testManyCommitWorkersMixedWorkload() throws Exception { + setUp(16, 8, 8, 25); + LOG.info("testManyCommitWorkersMixedWorkload 8X0w/100r + 8X25w/75r workload test"); + synchronized (this) { + wait(TEST_RUN_TIME_IN_MS); + } + assertFalse(fail); + checkProcessedRequest(); + } + + private void checkProcessedRequest() { + assertTrue("No read requests processed", processedReadRequests.get() > 0); + assertTrue("No write requests processed", processedWriteRequests.get() > 0); + } + + volatile boolean fail = false; + private synchronized void failTest(String reason) { + fail = true; + notifyAll(); + fail(reason); + } + + private class TestZooKeeperServer extends ZooKeeperServer { + + public TestZooKeeperServer(File snapDir, File logDir, int tickTime) throws IOException { + super(snapDir, logDir, tickTime); + } + + public PrepRequestProcessor getFirstProcessor() { + return (PrepRequestProcessor) firstProcessor; + } + + // Leader mock: Prep -> MockProposal -> Commit -> validate -> Final + // Have side thread call commitProc.commit() + @Override + protected void setupRequestProcessors() { + RequestProcessor finalProcessor = new FinalRequestProcessor(zks); + // ValidateProcessor is set up in a similar fashion to ToBeApplied + // processor, so it can do pre/post validating of requests + ValidateProcessor validateProcessor = new ValidateProcessor(finalProcessor); + commitProcessor = new CommitProcessor(validateProcessor, "1", true, null); + validateProcessor.setCommitProcessor(commitProcessor); + commitProcessor.start(); + MockProposalRequestProcessor proposalProcessor = new MockProposalRequestProcessor(commitProcessor); + proposalProcessor.start(); + firstProcessor = new PrepRequestProcessor(zks, proposalProcessor); + getFirstProcessor().start(); + } + + } + + private class MockProposalRequestProcessor extends Thread implements RequestProcessor { + + private final CommitProcessor commitProcessor; + private final LinkedBlockingQueue proposals = new LinkedBlockingQueue(); + + public MockProposalRequestProcessor(CommitProcessor commitProcessor) { + this.commitProcessor = commitProcessor; + } + + @Override + public void run() { + Random rand = new Random(Thread.currentThread().getId()); + try { + while (true) { + // If it is a read-only test, there will be no proposals.. + if (!proposals.isEmpty()) { + Request request = proposals.take(); + Thread.sleep(5 + rand.nextInt(95)); + commitProcessor.commit(request); + } + } + } catch (InterruptedException e) { + // ignore + } + } + + @Override + public void processRequest(Request request) throws RequestProcessorException { + commitProcessor.processRequest(request); + if (request.getHdr() != null) { + // fake propose request + proposals.add(request); + } + } + + @Override + public void shutdown() { + LOG.info("shutdown MockProposalRequestProcessor"); + proposals.clear(); + if (commitProcessor != null) { + commitProcessor.shutdown(); + } + } + + } + + private class ValidateProcessor implements RequestProcessor { + + Random rand = new Random(Thread.currentThread().getId()); + RequestProcessor nextProcessor; + CommitProcessor commitProcessor; + AtomicLong expectedZxid = new AtomicLong(1); + ConcurrentHashMap cxidMap = new ConcurrentHashMap(); + + AtomicInteger outstandingReadRequests = new AtomicInteger(0); + AtomicInteger outstandingWriteRequests = new AtomicInteger(0); + + public ValidateProcessor(RequestProcessor nextProcessor) { + this.nextProcessor = nextProcessor; + } + + public void setCommitProcessor(CommitProcessor commitProcessor) { + this.commitProcessor = commitProcessor; + } + + @Override + public void processRequest(Request request) throws RequestProcessorException { + if (stopped) { + return; + } + if (request.type == OpCode.closeSession) { + LOG.debug("ValidateProcessor got closeSession request=" + request); + nextProcessor.processRequest(request); + return; + } + + boolean isWriteRequest = commitProcessor.needCommit(request); + if (isWriteRequest) { + outstandingWriteRequests.incrementAndGet(); + validateWriteRequestVariant(request); + LOG.debug("Starting write request zxid={}", request.zxid); + } else { + LOG.debug( + "Starting read request cxid={} for session 0x{}", + request.cxid, + Long.toHexString(request.sessionId)); + outstandingReadRequests.incrementAndGet(); + validateReadRequestVariant(request); + } + + // Insert random delay to test thread race conditions + try { + Thread.sleep(5 + rand.nextInt(25)); + } catch (InterruptedException e) { + // ignore + } + nextProcessor.processRequest(request); + /* + * The commit workers will have to execute this line before they + * wake up the commit processor. So this value is up-to-date when + * variant check is performed + */ + if (isWriteRequest) { + outstandingWriteRequests.decrementAndGet(); + LOG.debug("Done write request zxid={}", request.zxid); + processedWriteRequests.incrementAndGet(); + } else { + outstandingReadRequests.decrementAndGet(); + LOG.debug( + "Done read request cxid={} for session 0x{}", + request.cxid, + Long.toHexString(request.sessionId)); + processedReadRequests.incrementAndGet(); + } + validateRequest(request); + } + + /** + * Validate that this is the only request in the pipeline + */ + private void validateWriteRequestVariant(Request request) { + if (stopped) { + return; + } + long zxid = request.getHdr().getZxid(); + int readRequests = outstandingReadRequests.get(); + if (readRequests != 0) { + failTest("There are " + readRequests + " outstanding" + + " read requests while issuing a write request zxid=" + zxid); + } + int writeRequests = outstandingWriteRequests.get(); + if (writeRequests > 1) { + failTest("There are " + writeRequests + " outstanding" + + " write requests while issuing a write request zxid=" + zxid + + " (expected one)"); + } + } + + /** + * Validate that no write request is in the pipeline while working + * on a read request + */ + private void validateReadRequestVariant(Request request) { + int writeRequests = outstandingWriteRequests.get(); + if (writeRequests != 0) { + failTest("There are " + writeRequests + " outstanding" + + " write requests while issuing a read request cxid=" + request.cxid + + " for session 0x" + Long.toHexString(request.sessionId)); + } + } + + private void validateRequest(Request request) { + LOG.debug("Got request {}", request); + + // Zxids should always be in order for write requests + if (request.getHdr() != null) { + long zxid = request.getHdr().getZxid(); + if (!expectedZxid.compareAndSet(zxid, zxid + 1)) { + failTest("Write request, expected_zxid=" + expectedZxid.get() + "; req_zxid=" + zxid); + } + } + + // Each session should see its cxids in order + AtomicInteger sessionCxid = cxidMap.get(request.sessionId); + if (sessionCxid == null) { + sessionCxid = new AtomicInteger(request.cxid + 1); + AtomicInteger existingSessionCxid = cxidMap.putIfAbsent(request.sessionId, sessionCxid); + if (existingSessionCxid != null) { + failTest("Race condition adding cxid=" + + request.cxid + + " for session 0x" + + Long.toHexString(request.sessionId) + + " with other_cxid=" + + existingSessionCxid.get()); + } + } else { + if (!sessionCxid.compareAndSet(request.cxid, request.cxid + 1)) { + failTest("Expected_cxid=" + sessionCxid.get() + "; req_cxid=" + request.cxid); + } + } + } + + @Override + public void shutdown() { + LOG.info("shutdown validateReadRequestVariant"); + cxidMap.clear(); + expectedZxid = new AtomicLong(1); + if (nextProcessor != null) { + nextProcessor.shutdown(); + } + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CurrentEpochWriteFailureTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CurrentEpochWriteFailureTest.java new file mode 100644 index 0000000..9a172ee --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/CurrentEpochWriteFailureTest.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.AtomicFileOutputStream; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CurrentEpochWriteFailureTest extends QuorumPeerTestBase { + protected static final Logger LOG = LoggerFactory.getLogger(CurrentEpochWriteFailureTest.class); + private Servers servers; + private int clientPort; + + @After + public void tearDown() throws InterruptedException { + if (servers != null) { + servers.shutDownAllServers(); + } + } + + /* + * ZOOKEEPER-4269: + * accepted epoch is first written to temporary file acceptedEpoch.tmp then this file is + * renamed to acceptedEpoch. + * Failure, either because of exception or power-off, in renaming the acceptedEpoch.tmp file + * will cause server startup error with message "The current epoch, x, is older than the last + * zxid y" + * To handle this scenario we should read accepted epoch from this temp file as well. + */ + @Test + public void testReadCurrentEpochFromAcceptedEpochTmpFile() throws Exception { + startServers(); + writeSomeData(); + + restartServers(); + writeSomeData(); + + MainThread firstServer = servers.mt[0]; + + // As started servers two times, current epoch must be two + long currentEpoch = firstServer.getQuorumPeer().getCurrentEpoch(); + assertEquals(2, currentEpoch); + + // Initialize files for later use + File snapDir = firstServer.getQuorumPeer().getTxnFactory().getSnapDir(); + File currentEpochFile = new File(snapDir, QuorumPeer.CURRENT_EPOCH_FILENAME); + File currentEpochTempFile = new File(snapDir, + QuorumPeer.CURRENT_EPOCH_FILENAME + AtomicFileOutputStream.TMP_EXTENSION); + + // Shutdown servers + servers.shutDownAllServers(); + waitForAll(servers, ZooKeeper.States.CONNECTING); + + // Create scenario of file currentEpoch.tmp rename to currentEpoch failure. + // In this case currentEpoch file will have old epoch and currentEpoch.tmp will have the latest epoch + FileUtils.write(currentEpochFile, Long.toString(currentEpoch - 1), "UTF-8"); + FileUtils.write(currentEpochTempFile, Long.toString(currentEpoch), "UTF-8"); + + // Restart the serves, all serves should restart successfully. + servers.restartAllServersAndClients(this); + + // Check the first server where problem was injected. + assertTrue("server " + firstServer.getMyid() + + " is not up as file currentEpoch.tmp rename to currentEpoch file was failed" + + " which lead current epoch inconsistent state.", ClientBase + .waitForServerUp("127.0.0.1:" + firstServer.getClientPort(), CONNECTION_TIMEOUT)); + } + + private void restartServers() throws InterruptedException, IOException { + servers.shutDownAllServers(); + waitForAll(servers, ZooKeeper.States.CONNECTING); + servers.restartAllServersAndClients(this); + waitForAll(servers, ZooKeeper.States.CONNECTED); + } + + private void writeSomeData() throws Exception { + ZooKeeper client = ClientBase.createZKClient("127.0.0.1:" + clientPort); + String path = "/somePath" + System.currentTimeMillis(); + String data = "someData"; + client.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + byte[] data1 = client.getData(path, false, null); + assertEquals(data, new String(data1)); + client.close(); + } + + private void startServers() throws Exception { + servers = LaunchServers(3); + clientPort = servers.clientPorts[0]; + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/DIFFSyncConsistencyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/DIFFSyncConsistencyTest.java new file mode 100644 index 0000000..3523e0a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/DIFFSyncConsistencyTest.java @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.net.SocketTimeoutException; +import java.util.Map; +import javax.security.sasl.SaslException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Test; + +public class DIFFSyncConsistencyTest extends QuorumPeerTestBase { + + private static int SERVER_COUNT = 3; + private MainThread[] mt = new MainThread[SERVER_COUNT]; + + @Test(timeout = 120 * 1000) + public void testInconsistentDueToUncommittedLog() throws Exception { + final int LEADER_TIMEOUT_MS = 10_000; + final int[] clientPorts = new int[SERVER_COUNT]; + + StringBuilder sb = new StringBuilder(); + String server; + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new MockTestQPMain(); + } + }; + mt[i].start(); + } + + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue("waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + int leader = findLeader(mt); + CountdownWatcher watch = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[leader], ClientBase.CONNECTION_TIMEOUT, watch); + watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + Map outstanding = mt[leader].main.quorumPeer.leader.outstandingProposals; + // Increase the tick time to delay the leader going to looking to allow us proposal a transaction while other + // followers are offline. + int previousTick = mt[leader].main.quorumPeer.tickTime; + mt[leader].main.quorumPeer.tickTime = LEADER_TIMEOUT_MS; + // Let the previous tick on the leader exhaust itself so the new tick time takes effect + Thread.sleep(previousTick); + + LOG.info("LEADER ELECTED {}", leader); + + // Shutdown followers to make sure we don't accidentally send the proposal we are going to make to follower. + // In other words, we want to make sure the followers get the proposal later through DIFF sync. + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != leader) { + mt[i].shutdown(); + } + } + + // Send a create request to old leader and make sure it's synced to disk. + try { + zk.create("/zk" + leader, "zk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("create /zk" + leader + " should have failed"); + } catch (KeeperException e) { + } + + // Make sure that we actually did get it in process at the leader; there can be extra sessionClose proposals. + assertTrue(outstanding.size() > 0); + Proposal p = findProposalOfType(outstanding, OpCode.create); + LOG.info("Old leader id: {}. All proposals: {}", leader, outstanding); + assertNotNull("Old leader doesn't have 'create' proposal", p); + + // Make sure leader sync the proposal to disk. + int sleepTime = 0; + Long longLeader = (long) leader; + while (!p.qvAcksetPairs.get(0).getAckset().contains(longLeader)) { + if (sleepTime > 2000) { + fail("Transaction not synced to disk within 1 second " + p.qvAcksetPairs.get(0).getAckset() + " expected " + leader); + } + Thread.sleep(100); + sleepTime += 100; + } + + // Start controlled followers where we deliberately make the follower fail once follower receive the UPTODATE + // message from leader. Because followers only persist proposals from DIFF sync after UPTODATE, this can + // deterministically simulate the situation where followers ACK NEWLEADER (which makes leader think she has the + // quorum support, but actually not afterwards) but immediately fail afterwards without persisting the proposals + // from DIFF sync. + for (int i = 0; i < SERVER_COUNT; i++) { + if (i == leader) { + continue; + } + + mt[i].start(); + int sleepCount = 0; + while (mt[i].getQuorumPeer() == null) { + ++sleepCount; + if (sleepCount > 100) { + fail("Can't start follower " + i + " !"); + } + Thread.sleep(100); + } + + ((CustomQuorumPeer) mt[i].getQuorumPeer()).setInjectError(true); + LOG.info("Follower {} started.", i); + } + + // Verify leader can see it. The fact that leader can see it implies that + // leader should, at this point in time, get a quorum of ACK of NEWLEADER + // from two followers so leader can start serving requests; this also implies + // that DIFF sync from leader to followers are finished at this point in time. + // We then verify later that followers should have the same view after we shutdown + // this leader, otherwise it's a violation of ZAB / sequential consistency. + int c = 0; + while (c < 100) { + ++c; + try { + Stat stat = zk.exists("/zk" + leader, false); + assertNotNull("server " + leader + " should have /zk", stat); + break; + } catch (KeeperException.ConnectionLossException e) { + + } + Thread.sleep(100); + } + + // Shutdown all servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + waitForOne(zk, States.CONNECTING); + + // Now restart all servers except the old leader. Only old leader has the transaction sync to disk. + // The old followers only had in memory view of the transaction, and they didn't have a chance + // to sync to disk because we made them fail at UPTODATE. + for (int i = 0; i < SERVER_COUNT; i++) { + if (i == leader) { + continue; + } + mt[i].start(); + int sleepCount = 0; + while (mt[i].getQuorumPeer() == null) { + ++sleepCount; + if (sleepCount > 100) { + fail("Can't start follower " + i + " !"); + } + Thread.sleep(100); + } + + ((CustomQuorumPeer) mt[i].getQuorumPeer()).setInjectError(false); + LOG.info("Follower {} started again.", i); + } + + int newLeader = findLeader(mt); + assertNotEquals("new leader is still the old leader " + leader + " !!", newLeader, leader); + + // This simulates the case where clients connected to the old leader had a view of the data + // "/zkX", but clients connect to the new leader does not have the same view of data (missing "/zkX"). + // This inconsistent view of the quorum exposed from leaders is a violation of ZAB. + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != newLeader) { + continue; + } + zk.close(); + zk = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, watch); + watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + Stat val = zk.exists("/zk" + leader, false); + assertNotNull("Data inconsistency detected! Server " + i + " should have a view of /zk" + leader + "!", + val); + } + + zk.close(); + } + + @After + public void tearDown() { + for (int i = 0; i < mt.length; i++) { + try { + mt[i].shutdown(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted while shutting it down", e); + } + } + } + + static class CustomQuorumPeer extends QuorumPeer { + + private volatile boolean injectError = false; + + public CustomQuorumPeer() throws SaslException { + + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb())) { + + @Override + void readPacket(QuorumPacket pp) throws IOException { + /** + * In real scenario got SocketTimeoutException while reading + * the packet from leader because of network problem, but + * here throwing SocketTimeoutException based on whether + * error is injected or not + */ + super.readPacket(pp); + if (injectError && pp.getType() == Leader.UPTODATE) { + String type = LearnerHandler.packetToString(pp); + throw new SocketTimeoutException("Socket timeout while reading the packet for operation " + + type); + } + } + + }; + } + + public void setInjectError(boolean injectError) { + this.injectError = injectError; + } + + } + + static class MockTestQPMain extends TestQPMain { + + @Override + protected QuorumPeer getQuorumPeer() throws SaslException { + return new CustomQuorumPeer(); + } + + } + + private Proposal findProposalOfType(Map proposals, int type) { + for (Proposal proposal : proposals.values()) { + if (proposal.request.getHdr().getType() == type) { + return proposal; + } + } + return null; + } + + private int findLeader(MainThread[] mt) { + for (int i = 0; i < mt.length; i++) { + if (mt[i].main.quorumPeer.leader != null) { + return i; + } + } + return -1; + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/DelayRequestProcessor.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/DelayRequestProcessor.java new file mode 100644 index 0000000..c092c6c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/DelayRequestProcessor.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.concurrent.LinkedBlockingQueue; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; + +/** + * Allows the blocking of the request processor queue on a ZooKeeperServer. + * + * This is used to simulate arbitrary length delays or to produce delays + * in request processing that are maximally inconvenient for a given feature + * for the purposes of testing it. + */ +public class DelayRequestProcessor implements RequestProcessor { + + private boolean blocking; + RequestProcessor next; + + private LinkedBlockingQueue incomingRequests = new LinkedBlockingQueue<>(); + + private DelayRequestProcessor(RequestProcessor next) { + this.blocking = true; + this.next = next; + } + + @Override + public void processRequest(Request request) throws RequestProcessorException { + if (blocking) { + incomingRequests.add(request); + } else { + next.processRequest(request); + } + } + + public void submitRequest(Request request) throws RequestProcessorException { + next.processRequest(request); + } + + @Override + public void shutdown() { + } + + public void unblockQueue() throws RequestProcessorException { + if (blocking) { + for (Request request : incomingRequests) { + next.processRequest(request); + } + blocking = false; + } + } + + public static DelayRequestProcessor injectDelayRequestProcessor(FollowerZooKeeperServer zooKeeperServer) { + RequestProcessor finalRequestProcessor = zooKeeperServer.commitProcessor.nextProcessor; + DelayRequestProcessor delayRequestProcessor = new DelayRequestProcessor(finalRequestProcessor); + zooKeeperServer.commitProcessor.nextProcessor = delayRequestProcessor; + return delayRequestProcessor; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EagerACLFilterTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EagerACLFilterTest.java new file mode 100644 index 0000000..fa18bf0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EagerACLFilterTest.java @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.fail; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.QuorumBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class EagerACLFilterTest extends QuorumBase { + + protected boolean checkEnabled; + protected ServerState serverState; + protected final CountDownLatch callComplete = new CountDownLatch(1); + protected boolean complete = false; + protected static final String PARENT_PATH = "/foo"; + protected static final String CHILD_PATH = "/foo/bar"; + protected static final String AUTH_PROVIDER = "digest"; + protected static final byte[] AUTH = "hello".getBytes(); + protected static final byte[] AUTHB = "goodbye".getBytes(); + protected static final byte[] DATA = "Hint Water".getBytes(); + protected TestableZooKeeper zkClient; + protected TestableZooKeeper zkClientB; + protected QuorumPeer zkLeader; + protected ZooKeeperServer connectedServer; + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{{ServerState.LEADING, true}, {ServerState.LEADING, false}, {ServerState.FOLLOWING, true}, {ServerState.FOLLOWING, false}, {ServerState.OBSERVING, true}, {ServerState.OBSERVING, false}}); + } + + public EagerACLFilterTest(ServerState state, boolean checkEnabled) { + this.serverState = state; + this.checkEnabled = checkEnabled; + } + + @Before + public void setUp() throws Exception { + ensureCheck(checkEnabled); + CountdownWatcher clientWatch = new CountdownWatcher(); + CountdownWatcher clientWatchB = new CountdownWatcher(); + super.setUp(true); + + String hostPort = getPeersMatching(serverState).split(",")[0]; + int clientPort = Integer.parseInt(hostPort.split(":")[1]); + + zkLeader = getPeerList().get(getLeaderIndex()); + connectedServer = getPeerByClientPort(clientPort).getActiveServer(); + + zkClient = createClient(clientWatch, hostPort); + zkClientB = createClient(clientWatchB, hostPort); + zkClient.addAuthInfo(AUTH_PROVIDER, AUTH); + zkClientB.addAuthInfo(AUTH_PROVIDER, AUTHB); + clientWatch.waitForConnected(CONNECTION_TIMEOUT); + clientWatchB.waitForConnected(CONNECTION_TIMEOUT); + } + + @After + public void tearDown() throws Exception { + if (zkClient != null) { + zkClient.close(); + } + + if (zkClientB != null) { + zkClientB.close(); + } + + super.tearDown(); + } + + private void ensureCheck(boolean enabled) { + if (enabled) { + System.setProperty(ZooKeeperServer.ENABLE_EAGER_ACL_CHECK, "true"); + } else { + System.clearProperty(ZooKeeperServer.ENABLE_EAGER_ACL_CHECK); + } + } + + private void assertTransactionState(String condition, long lastxid) { + String assertion = String.format("Server State: %s Check Enabled: %s %s", serverState, checkEnabled, condition); + if (checkEnabled) { + assertEquals(assertion, lastxid, zkLeader.getLastLoggedZxid()); + } else { + assertNotSame(assertion, lastxid, zkLeader.getLastLoggedZxid()); + } + } + + @Test + public void testCreateOK() throws Exception { + ensureCheck(true); + zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zkClientB.create(CHILD_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + } + + @Test + public void testCreate2OK() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + zkClientB.create(CHILD_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + } + + @Test + public void testCreateFail() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + long lastxid = zkLeader.getLastLoggedZxid(); + try { + zkClientB.create(CHILD_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NoAuthException e) { + } + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + + assertTransactionState("Transaction state on Leader after failed create", lastxid); + } + + @Test + public void testCreate2Fail() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, null); + long lastxid = zkLeader.getLastLoggedZxid(); + try { + zkClientB.create(CHILD_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + } catch (KeeperException.NoAuthException e) { + } + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + + assertTransactionState("Transaction state on Leader after failed create2", lastxid); + } + + @Test + public void testDeleteOK() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zkClientB.delete(PARENT_PATH, -1); + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + } + + @Test + public void testDeleteFail() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, null); + zkClient.create(CHILD_PATH, DATA, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, null); + long lastxid = zkLeader.getLastLoggedZxid(); + try { + zkClientB.delete(CHILD_PATH, -1); + } catch (KeeperException.NoAuthException e) { + } + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + + assertTransactionState("Transaction state on Leader after failed delete", lastxid); + } + + @Test + public void testSetDataOK() throws Exception { + zkClient.create(PARENT_PATH, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + zkClientB.setData(PARENT_PATH, DATA, -1); + } + + @Test + public void testSetDataFail() throws Exception { + zkClient.create(PARENT_PATH, null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, null); + long lastxid = zkLeader.getLastLoggedZxid(); + try { + zkClientB.setData(PARENT_PATH, DATA, -1); + } catch (KeeperException.NoAuthException e) { + } + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + + assertTransactionState("Transaction state on Leader after failed setData", lastxid); + } + + @Test + public void testSetACLOK() throws Exception { + zkClient.create(PARENT_PATH, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + zkClientB.setACL(PARENT_PATH, Ids.READ_ACL_UNSAFE, -1); + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + } + + @Test + public void testSetACLFail() throws Exception { + zkClient.create(PARENT_PATH, null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, null); + long lastxid = zkLeader.getLastLoggedZxid(); + try { + zkClientB.setACL(PARENT_PATH, Ids.READ_ACL_UNSAFE, -1); + } catch (KeeperException.NoAuthException e) { + } + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + + assertTransactionState("Transaction state on Leader after failed setACL", lastxid); + } + + @Test + public void testBadACL() throws Exception { + CountdownWatcher cw = new CountdownWatcher(); + TestableZooKeeper zk = createClient(cw, getPeersMatching(serverState)); + long lastxid; + + cw.waitForConnected(CONNECTION_TIMEOUT); + + lastxid = zkLeader.getLastLoggedZxid(); + + try { + zk.create("/acltest", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Should have received an invalid acl error"); + } catch (KeeperException.InvalidACLException e) { + } + + assertEquals("OutstandingRequests not decremented", 0, connectedServer.getInProcess()); + + assertTransactionState("zxid after invalid ACL", lastxid); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EphemeralNodeDeletionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EphemeralNodeDeletionTest.java new file mode 100644 index 0000000..749eb80 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EphemeralNodeDeletionTest.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.net.SocketTimeoutException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javax.security.sasl.SaslException; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Test; + +public class EphemeralNodeDeletionTest extends QuorumPeerTestBase { + + private static int SERVER_COUNT = 3; + private MainThread[] mt = new MainThread[SERVER_COUNT]; + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2355. + * ZooKeeper ephemeral node is never deleted if follower fail while reading + * the proposal packet. + */ + + @Test(timeout = 120000) + public void testEphemeralNodeDeletion() throws Exception { + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + // start all the servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new MockTestQPMain(); + } + }; + mt[i].start(); + } + + // ensure all servers started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + CountdownWatcher watch = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[1], ClientBase.CONNECTION_TIMEOUT, watch); + watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + /** + * now the problem scenario starts + */ + + Stat firstEphemeralNode = new Stat(); + + // 1: create ephemeral node + String nodePath = "/e1"; + zk.create(nodePath, "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, firstEphemeralNode); + assertEquals( + "Current session and ephemeral owner should be same", + zk.getSessionId(), + firstEphemeralNode.getEphemeralOwner()); + + // 2: inject network problem in one of the follower + CustomQuorumPeer follower = (CustomQuorumPeer) getByServerState(mt, ServerState.FOLLOWING); + follower.setInjectError(true); + + // 3: close the session so that ephemeral node is deleted + zk.close(); + + // remove the error + follower.setInjectError(false); + + assertTrue( + "Faulted Follower should have joined quorum by now", + ClientBase.waitForServerUp("127.0.0.1:" + follower.getClientPort(), CONNECTION_TIMEOUT)); + + QuorumPeer leader = getByServerState(mt, ServerState.LEADING); + assertNotNull("Leader should not be null", leader); + assertTrue( + "Leader must be running", + ClientBase.waitForServerUp("127.0.0.1:" + leader.getClientPort(), CONNECTION_TIMEOUT)); + + watch = new CountdownWatcher(); + zk = new ZooKeeper("127.0.0.1:" + leader.getClientPort(), ClientBase.CONNECTION_TIMEOUT, watch); + watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + Stat exists = zk.exists(nodePath, false); + assertNull("Node must have been deleted from leader", exists); + + CountdownWatcher followerWatch = new CountdownWatcher(); + ZooKeeper followerZK = new ZooKeeper( + "127.0.0.1:" + follower.getClientPort(), + ClientBase.CONNECTION_TIMEOUT, + followerWatch); + followerWatch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + Stat nodeAtFollower = followerZK.exists(nodePath, false); + + // Problem 1: Follower had one extra ephemeral node /e1 + assertNull("ephemeral node must not exist", nodeAtFollower); + + // Create the node with another session + Stat currentEphemeralNode = new Stat(); + zk.create(nodePath, "2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, currentEphemeralNode); + + // close the session and newly created ephemeral node should be deleted + zk.close(); + + SyncCallback cb = new SyncCallback(); + followerZK.sync(nodePath, cb, null); + cb.sync.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + + nodeAtFollower = followerZK.exists(nodePath, false); + + // Problem 2: Before fix, after session close the ephemeral node + // was not getting deleted. But now after the fix after session close + // ephemeral node is getting deleted. + assertNull("After session close ephemeral node must be deleted", nodeAtFollower); + followerZK.close(); + } + + @After + public void tearDown() { + // stop all severs + for (int i = 0; i < mt.length; i++) { + try { + mt[i].shutdown(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted while shutting it down", e); + } + } + } + + private QuorumPeer getByServerState(MainThread[] mt, ServerState state) { + for (int i = mt.length - 1; i >= 0; i--) { + QuorumPeer quorumPeer = mt[i].getQuorumPeer(); + if (null != quorumPeer && state == quorumPeer.getPeerState()) { + return quorumPeer; + } + } + return null; + } + + static class CustomQuorumPeer extends QuorumPeer { + + private boolean injectError = false; + + public CustomQuorumPeer() throws SaslException { + + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb())) { + + @Override + void readPacket(QuorumPacket pp) throws IOException { + /** + * In real scenario got SocketTimeoutException while reading + * the packet from leader because of network problem, but + * here throwing SocketTimeoutException based on whether + * error is injected or not + */ + super.readPacket(pp); + if (injectError && pp.getType() == Leader.PROPOSAL) { + String type = LearnerHandler.packetToString(pp); + throw new SocketTimeoutException("Socket timeout while reading the packet for operation " + + type); + } + } + + }; + } + + public void setInjectError(boolean injectError) { + this.injectError = injectError; + } + + } + + static class MockTestQPMain extends TestQPMain { + + @Override + protected QuorumPeer getQuorumPeer() throws SaslException { + return new CustomQuorumPeer(); + } + + } + + private static class SyncCallback implements AsyncCallback.VoidCallback { + + private final CountDownLatch sync = new CountDownLatch(1); + + @Override + public void processResult(int rc, String path, Object ctx) { + sync.countDown(); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EpochWriteFailureTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EpochWriteFailureTest.java new file mode 100644 index 0000000..526dc0b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/EpochWriteFailureTest.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import java.io.File; +import java.io.IOException; +import java.util.Map; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Test; + +public class EpochWriteFailureTest extends QuorumPeerTestBase { + private static int SERVER_COUNT = 3; + private static int[] clientPorts = new int[SERVER_COUNT]; + private static MainThread[] mt = new MainThread[SERVER_COUNT]; + private static ZooKeeper zk; + + /* + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2307 + * Expectation: During leader election when accepted epoch write to file + * fails, it should not complete leader election, also it should not update + * run time values of acceptedEpoch, + */ + @Test(timeout = 120000) + public void testAcceptedEpochWriteFailure() throws Exception { + StringBuilder sb = new StringBuilder(); + sb.append("admin.enableServer=false"); + sb.append("\n"); + String server; + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server); + sb.append("\n"); + } + String currentQuorumCfgSection = sb.toString(); + for (int i = 0; i < SERVER_COUNT - 1; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // ensure two servers started + for (int i = 0; i < SERVER_COUNT - 1; i++) { + Assert.assertTrue("waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + CountdownWatcher watch1 = new CountdownWatcher(); + zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, + watch1); + watch1.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + String data = "originalData"; + zk.create("/epochIssue", data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + //initialize third server + mt[2] = new MainThread(2, clientPorts[2], currentQuorumCfgSection, false) { + + @Override + public TestQPMain getTestQPMain() { + return new MockTestQPMain(); + } + }; + + //This server has problem it fails while writing acceptedEpoch. + mt[2].start(); + + /* + * Verify that problematic server does not start as acceptedEpoch update + * failure is injected and it keeps on trying to join the quorum + */ + + Assert.assertFalse("verify server 2 not started", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[2], CONNECTION_TIMEOUT / 2)); + + QuorumPeer quorumPeer = mt[2].getQuorumPeer(); + + Assert.assertEquals("acceptedEpoch must not have changed", 0, + quorumPeer.getAcceptedEpoch()); + Assert.assertEquals("currentEpoch must not have changed", 0, + quorumPeer.getCurrentEpoch()); + } + + static class CustomQuorumPeer extends QuorumPeer { + CustomQuorumPeer(Map quorumPeers, File snapDir, File logDir, int clientPort, + int electionAlg, long myid, int tickTime, int initLimit, int syncLimit, + int connectToLearnerMasterLimit) throws IOException { + super(quorumPeers, snapDir, logDir, clientPort, electionAlg, myid, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + } + + @Override + protected void writeLongToFile(String name, long value) throws IOException { + // initial epoch writing should be successful + if (0 != value) { + throw new IOException("Input/output error"); + } + } + } + + private static class MockTestQPMain extends TestQPMain { + @Override + public void runFromConfig(QuorumPeerConfig config) + throws IOException { + quorumPeer = new CustomQuorumPeer(config.getQuorumVerifier().getAllMembers(), + config.getDataDir(), config.getDataLogDir(), + config.getClientPortAddress().getPort(), config.getElectionAlg(), + config.getServerId(), config.getTickTime(), config.getInitLimit(), + config.getSyncLimit(), config.getSyncLimit()); + quorumPeer.start(); + try { + quorumPeer.join(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted", e); + } + } + } + + @AfterClass + public static void tearDownAfterClass() throws InterruptedException { + for (int i = 0; i < SERVER_COUNT; i++) { + if (mt[i] != null) { + mt[i].shutdown(); + } + } + if (zk != null) { + zk.close(); + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEBackwardElectionRoundTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEBackwardElectionRoundTest.java new file mode 100644 index 0000000..84e2031 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEBackwardElectionRoundTest.java @@ -0,0 +1,150 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.fail; +import java.io.File; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLEBackwardElectionRoundTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLELostMessageTest.class); + + int count; + Map peers; + File[] tmpdir; + int[] port; + + QuorumCnxManager[] cnxManagers; + + @Before + public void setUp() throws Exception { + count = 3; + + peers = new HashMap(count); + tmpdir = new File[count]; + port = new int[count]; + cnxManagers = new QuorumCnxManager[count - 1]; + } + + @After + public void tearDown() throws Exception { + for (int i = 0; i < (count - 1); i++) { + if (cnxManagers[i] != null) { + cnxManagers[i].halt(); + } + } + } + + /** + * This test is checking the following case. A server S is + * currently LOOKING and it receives notifications from + * a quorum indicating they are following S. The election + * round E of S is higher than the election round E' in the + * notification messages, so S becomes the leader and sets + * its epoch back to E'. In the meanwhile, one or more + * followers turn to LOOKING and elect S in election round E. + * Having leader and followers with different election rounds + * might prevent other servers from electing a leader because + * they can't get a consistent set of notifications from a + * quorum. + * + * https://issues.apache.org/jira/browse/ZOOKEEPER-1514 + * + * + * @throws Exception + */ + + @Test + public void testBackwardElectionRound() throws Exception { + LOG.info("TestLE: {}, {}", getTestName(), count); + for (int i = 0; i < count; i++) { + int clientport = PortAssignment.unique(); + peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(clientport), new InetSocketAddress(PortAssignment.unique()))); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = clientport; + } + + ByteBuffer initialMsg0 = getMsg(); + ByteBuffer initialMsg1 = getMsg(); + + /* + * Start server 0 + */ + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2, 2); + peer.startLeaderElection(); + FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 0); + thread.start(); + + /* + * Start mock server 1 + */ + QuorumPeer mockPeer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2, 2); + cnxManagers[0] = mockPeer.createCnxnManager(); + cnxManagers[0].listener.start(); + + cnxManagers[0].toSend(0L, initialMsg0); + + /* + * Start mock server 2 + */ + mockPeer = new QuorumPeer(peers, tmpdir[2], tmpdir[2], port[2], 3, 2, 1000, 2, 2, 2); + cnxManagers[1] = mockPeer.createCnxnManager(); + cnxManagers[1].listener.start(); + + cnxManagers[1].toSend(0L, initialMsg1); + + /* + * Run another instance of leader election. + */ + thread.join(5000); + thread = new FLETestUtils.LEThread(peer, 0); + thread.start(); + + /* + * Send the same messages, this time should not make 0 the leader. + */ + cnxManagers[0].toSend(0L, initialMsg0); + cnxManagers[1].toSend(0L, initialMsg1); + + thread.join(5000); + + if (!thread.isAlive()) { + fail("Should not have joined"); + } + + } + + private ByteBuffer getMsg() { + return FLETestUtils.createMsg(ServerState.FOLLOWING.ordinal(), 0, 0, 1); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLELostMessageTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLELostMessageTest.java new file mode 100644 index 0000000..5c6ffb5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLELostMessageTest.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLELostMessageTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLELostMessageTest.class); + + int count; + HashMap peers; + File[] tmpdir; + int[] port; + + QuorumCnxManager cnxManager; + + @Before + public void setUp() throws Exception { + count = 3; + + peers = new HashMap(count); + tmpdir = new File[count]; + port = new int[count]; + } + + @After + public void tearDown() throws Exception { + cnxManager.halt(); + } + + @Test + public void testLostMessage() throws Exception { + LOG.info("TestLE: {}, {}", getTestName(), count); + for (int i = 0; i < count; i++) { + int clientport = PortAssignment.unique(); + peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(clientport), new InetSocketAddress(PortAssignment.unique()))); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = clientport; + } + + /* + * Start server 0 + */ + QuorumPeer peer = new QuorumPeer(peers, tmpdir[1], tmpdir[1], port[1], 3, 1, 1000, 2, 2, 2); + peer.startLeaderElection(); + FLETestUtils.LEThread thread = new FLETestUtils.LEThread(peer, 1); + thread.start(); + + /* + * Start mock server 1 + */ + mockServer(); + thread.join(5000); + if (thread.isAlive()) { + fail("Threads didn't join"); + } + } + + void mockServer() throws InterruptedException, IOException { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2, 2); + cnxManager = peer.createCnxnManager(); + cnxManager.listener.start(); + + cnxManager.toSend(1L, FLETestUtils.createMsg(ServerState.LOOKING.ordinal(), 0, 0, 0)); + cnxManager.recvQueue.take(); + cnxManager.toSend(1L, FLETestUtils.createMsg(ServerState.FOLLOWING.ordinal(), 1, 0, 0)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEMalformedNotificationMessageTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEMalformedNotificationMessageTest.java new file mode 100644 index 0000000..8465c9e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEMalformedNotificationMessageTest.java @@ -0,0 +1,249 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.util.HashMap; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class FLEMalformedNotificationMessageTest extends ZKTestCase { + private static final Logger LOG = LoggerFactory.getLogger(FLEMalformedNotificationMessageTest.class); + private static final byte[] CONFIG_BYTES = "my very invalid config string".getBytes(); + private static final int CONFIG_BYTES_LENGTH = CONFIG_BYTES.length; + + int count; + HashMap peers; + File tmpdir[]; + int port[]; + + QuorumCnxManager mockCnxManager; + FLETestUtils.LEThread leaderElectionThread; + QuorumPeer peerRunningLeaderElection; + + + @Before + public void setUp() throws Exception { + count = 3; + + peers = new HashMap<>(count); + tmpdir = new File[count]; + port = new int[count]; + + LOG.info("FLEMalformedNotificationMessageTest: {}, {}", getTestName(), count); + for (int i = 0; i < count; i++) { + int clientport = PortAssignment.unique(); + peers.put((long) i, + new QuorumServer(i, + new InetSocketAddress(clientport), + new InetSocketAddress(PortAssignment.unique()))); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = clientport; + } + + /* + * Start server 0 + */ + peerRunningLeaderElection = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2, 2); + peerRunningLeaderElection.startLeaderElection(); + leaderElectionThread = new FLETestUtils.LEThread(peerRunningLeaderElection, 0); + leaderElectionThread.start(); + } + + + @After + public void tearDown() throws Exception { + peerRunningLeaderElection.shutdown(); + mockCnxManager.halt(); + } + + + @Test + public void testTooShortPartialNotificationMessage() throws Exception { + + /* + * Start mock server 1, send a message too short to be compatible with any protocol version + * This simulates the case when only some parts of the whole message is received. + */ + startMockServer(1); + byte requestBytes[] = new byte[12]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + requestBuffer.clear(); + requestBuffer.putInt(ServerState.LOOKING.ordinal()); // state + requestBuffer.putLong(0); // leader + mockCnxManager.toSend(0L, requestBuffer); + + /* + * Assert that the message receiver thread in leader election is still healthy: + * we are sending valid votes and waiting for the leader election to be finished. + */ + sendValidNotifications(1, 0); + leaderElectionThread.join(5000); + if (leaderElectionThread.isAlive()) { + Assert.fail("Leader election thread didn't join, something went wrong."); + } + } + + + @Test + public void testNotificationMessageWithNegativeConfigLength() throws Exception { + + /* + * Start mock server 1, send a message with negative configLength field + */ + startMockServer(1); + byte requestBytes[] = new byte[48]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + requestBuffer.clear(); + requestBuffer.putInt(ServerState.LOOKING.ordinal()); // state + requestBuffer.putLong(0); // leader + requestBuffer.putLong(0); // zxid + requestBuffer.putLong(0); // electionEpoch + requestBuffer.putLong(0); // epoch + requestBuffer.putInt(FastLeaderElection.Notification.CURRENTVERSION); // version + requestBuffer.putInt(-123); // configData.length + mockCnxManager.toSend(0L, requestBuffer); + + /* + * Assert that the message receiver thread in leader election is still healthy: + * we are sending valid votes and waiting for the leader election to be finished. + */ + sendValidNotifications(1, 0); + leaderElectionThread.join(5000); + if (leaderElectionThread.isAlive()) { + Assert.fail("Leader election thread didn't join, something went wrong."); + } + } + + + @Test + public void testNotificationMessageWithInvalidConfigLength() throws Exception { + + /* + * Start mock server 1, send a message with an invalid configLength field + * (instead of sending CONFIG_BYTES_LENGTH, we send 10000) + */ + startMockServer(1); + byte requestBytes[] = new byte[48 + CONFIG_BYTES_LENGTH]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + requestBuffer.clear(); + requestBuffer.putInt(ServerState.LOOKING.ordinal()); // state + requestBuffer.putLong(0); // leader + requestBuffer.putLong(0); // zxid + requestBuffer.putLong(0); // electionEpoch + requestBuffer.putLong(0); // epoch + requestBuffer.putInt(FastLeaderElection.Notification.CURRENTVERSION); // version + requestBuffer.putInt(10000); // configData.length + requestBuffer.put(CONFIG_BYTES); // configData + mockCnxManager.toSend(0L, requestBuffer); + + /* + * Assert that the message receiver thread in leader election is still healthy: + * we are sending valid votes and waiting for the leader election to be finished. + */ + sendValidNotifications(1, 0); + leaderElectionThread.join(5000); + if (leaderElectionThread.isAlive()) { + Assert.fail("Leader election thread didn't join, something went wrong."); + } + } + + + @Test + public void testNotificationMessageWithInvalidConfig() throws Exception { + + /* + * Start mock server 1, send a message with an invalid config field + * (the receiver should not be able to parse the config part of the message) + */ + startMockServer(1); + ByteBuffer requestBuffer = FastLeaderElection.buildMsg(ServerState.LOOKING.ordinal(), 1, 0, 0, 0, CONFIG_BYTES); + mockCnxManager.toSend(0L, requestBuffer); + + /* + * Assert that the message receiver thread in leader election is still healthy: + * we are sending valid votes and waiting for the leader election to be finished. + */ + sendValidNotifications(1, 0); + leaderElectionThread.join(5000); + if (leaderElectionThread.isAlive()) { + Assert.fail("Leader election thread didn't join, something went wrong."); + } + } + + + @Test + public void testNotificationMessageWithBadProtocol() throws Exception { + + /* + * Start mock server 1, send an invalid 30 bytes long message + * (the receiver should not be able to parse the message and should skip it) + * This simulates the case when only some parts of the whole message is received. + */ + startMockServer(1); + byte requestBytes[] = new byte[30]; + ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); + requestBuffer.clear(); + requestBuffer.putInt(ServerState.LOOKING.ordinal()); // state + requestBuffer.putLong(1); // leader + requestBuffer.putLong(0); // zxid + requestBuffer.putLong(0); // electionEpoch + requestBuffer.putShort((short) 0); // this is the first two bytes of a proper + // 8 bytes Long we should send here + mockCnxManager.toSend(0L, requestBuffer); + + /* + * Assert that the message receiver thread in leader election is still healthy: + * we are sending valid votes and waiting for the leader election to be finished. + */ + sendValidNotifications(1, 0); + leaderElectionThread.join(5000); + if (leaderElectionThread.isAlive()) { + Assert.fail("Leader election thread didn't join, something went wrong."); + } + } + + + void startMockServer(int sid) throws IOException { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 1000, 2, 2, 2); + mockCnxManager = peer.createCnxnManager(); + mockCnxManager.listener.start(); + } + + + void sendValidNotifications(int fromSid, int toSid) throws InterruptedException { + mockCnxManager.toSend((long) toSid, FLETestUtils.createMsg(ServerState.LOOKING.ordinal(), fromSid, 0, 0)); + mockCnxManager.recvQueue.take(); + mockCnxManager.toSend((long) toSid, FLETestUtils.createMsg(ServerState.FOLLOWING.ordinal(), toSid, 0, 0)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEOutOfElectionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEOutOfElectionTest.java new file mode 100644 index 0000000..b1b6662 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLEOutOfElectionTest.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.server.quorum.FastLeaderElection.Notification; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Before; +import org.junit.Test; + +/** + * Test FastLeaderElection with out of election servers. + */ +public class FLEOutOfElectionTest { + + private FastLeaderElection fle; + + @Before + public void setUp() throws Exception { + File tmpdir = ClientBase.createTmpDir(); + Map peers = new HashMap(); + for (int i = 0; i < 5; i++) { + peers.put(Long.valueOf(i), new QuorumServer(Long.valueOf(i), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + } + QuorumPeer peer = new QuorumPeer(peers, tmpdir, tmpdir, PortAssignment.unique(), 3, 3, 1000, 2, 2, 2); + fle = new FastLeaderElection(peer, peer.createCnxnManager()); + } + + @Test + public void testIgnoringZxidElectionEpoch() { + Map votes = new HashMap(); + votes.put(0L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 1), 1, 2, ServerState.FOLLOWING)); + votes.put(1L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 2), 1, 2, ServerState.FOLLOWING)); + votes.put(3L, new Vote(0x1, 4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)); + votes.put(4L, new Vote(0x1, 4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LEADING)); + + assertTrue(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)).hasAllQuorums()); + } + + @Test + public void testElectionWIthDifferentVersion() { + Map votes = new HashMap(); + votes.put(0L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 1), 1, 1, ServerState.FOLLOWING)); + votes.put(1L, new Vote(0x1, 4L, ZxidUtils.makeZxid(1, 1), 1, 1, ServerState.FOLLOWING)); + votes.put(3L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)); + votes.put(4L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LEADING)); + + assertTrue(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.FOLLOWING)).hasAllQuorums()); + } + + @Test + public void testLookingNormal() { + Map votes = new HashMap(); + votes.put(0L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)); + votes.put(1L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)); + votes.put(3L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)); + votes.put(4L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LEADING)); + + assertTrue(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 1, 1, ServerState.LOOKING)).hasAllQuorums()); + } + + @Test + public void testLookingDiffRounds() { + HashMap votes = new HashMap(); + votes.put(0L, new Vote(4L, ZxidUtils.makeZxid(1, 1), 1, 1, ServerState.LOOKING)); + votes.put(1L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LOOKING)); + votes.put(3L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 3, 2, ServerState.LOOKING)); + votes.put(4L, new Vote(4L, ZxidUtils.makeZxid(2, 1), 3, 2, ServerState.LEADING)); + + assertFalse(fle.getVoteTracker(votes, new Vote(4L, ZxidUtils.makeZxid(2, 1), 2, 2, ServerState.LOOKING)).hasAllQuorums()); + } + + @Test + public void testOutofElection() { + HashMap outofelection = new HashMap(); + + outofelection.put(1L, new Vote(0x0, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x17, ServerState.FOLLOWING)); + outofelection.put(2L, new Vote(0x0, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x17, ServerState.FOLLOWING)); + outofelection.put(4L, new Vote(0x1, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x18, ServerState.FOLLOWING)); + Vote vote = new Vote(0x1, 5, ZxidUtils.makeZxid(15, 0), 0xa, 0x18, ServerState.LEADING); + outofelection.put(5L, vote); + + Notification n = new Notification(); + n.version = vote.getVersion(); + n.leader = vote.getId(); + n.zxid = vote.getZxid(); + n.electionEpoch = vote.getElectionEpoch(); + n.state = vote.getState(); + n.peerEpoch = vote.getPeerEpoch(); + n.sid = 5L; + + // Set the logical clock to 1 on fle instance of server 3. + fle.logicalclock.set(0x1); + + assertTrue("Quorum check failed", fle.getVoteTracker(outofelection, new Vote(n.version, n.leader, n.zxid, n.electionEpoch, n.peerEpoch, n.state)).hasAllQuorums()); + + assertTrue("Leader check failed", fle.checkLeader(outofelection, n.leader, n.electionEpoch)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLETestUtils.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLETestUtils.java new file mode 100644 index 0000000..45ce2ba --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FLETestUtils.java @@ -0,0 +1,83 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.nio.ByteBuffer; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLETestUtils extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLETestUtils.class); + + /* + * Thread to run an instance of leader election for + * a given quorum peer. + */ + static class LEThread extends Thread { + + private int i; + private QuorumPeer peer; + + LEThread(QuorumPeer peer, int i) { + this.i = i; + this.peer = peer; + LOG.info("Constructor: {}", getName()); + + } + + public void run() { + try { + Vote v = null; + peer.setPeerState(ServerState.LOOKING); + LOG.info("Going to call leader election: {}", i); + v = peer.getElectionAlg().lookForLeader(); + + if (v == null) { + fail("Thread " + i + " got a null vote"); + } + + /* + * A real zookeeper would take care of setting the current vote. Here + * we do it manually. + */ + peer.setCurrentVote(v); + + LOG.info("Finished election: {}, {}", i, v.getId()); + + assertTrue("State is not leading.", peer.getPeerState() == ServerState.LEADING); + } catch (Exception e) { + e.printStackTrace(); + } + LOG.info("Joining"); + } + + } + + /* + * Creates a leader election notification message. + */ + static ByteBuffer createMsg(int state, long leader, long zxid, long epoch) { + return FastLeaderElection.buildMsg(state, leader, zxid, 1, epoch); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FuzzySnapshotRelatedTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FuzzySnapshotRelatedTest.java new file mode 100644 index 0000000..fd3374d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/FuzzySnapshotRelatedTest.java @@ -0,0 +1,556 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.security.sasl.SaslException; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.KeeperException.NodeExistsException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.DataNode; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test cases used to catch corner cases due to fuzzy snapshot. + */ +public class FuzzySnapshotRelatedTest extends QuorumPeerTestBase { + + private static final Logger LOG = LoggerFactory.getLogger(FuzzySnapshotRelatedTest.class); + + MainThread[] mt = null; + ZooKeeper[] zk = null; + int[] clientPorts = null; + int leaderId; + int followerA; + + @Before + public void setup() throws Exception { + ZooKeeperServer.setDigestEnabled(true); + + LOG.info("Start up a 3 server quorum"); + final int ENSEMBLE_SERVERS = 3; + clientPorts = new int[ENSEMBLE_SERVERS]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + + // start servers + mt = new MainThread[ENSEMBLE_SERVERS]; + zk = new ZooKeeper[ENSEMBLE_SERVERS]; + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new CustomizedQPMain(); + } + }; + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + QuorumPeerMainTest.waitForAll(zk, States.CONNECTED); + LOG.info("all servers started"); + + leaderId = -1; + followerA = -1; + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + if (mt[i].main.quorumPeer.leader != null) { + leaderId = i; + } else if (followerA == -1) { + followerA = i; + } + } + } + + @After + public void tearDown() throws Exception { + ZooKeeperServer.setDigestEnabled(false); + + if (mt != null) { + for (MainThread t : mt) { + t.shutdown(); + } + } + + if (zk != null) { + for (ZooKeeper z : zk) { + z.close(); + } + } + } + + @Test + public void testMultiOpConsistency() throws Exception { + LOG.info("Create a parent node"); + final String path = "/testMultiOpConsistency"; + createEmptyNode(zk[followerA], path, CreateMode.PERSISTENT); + + LOG.info("Hook to catch the 2nd sub create node txn in multi-op"); + CustomDataTree dt = (CustomDataTree) mt[followerA].main.quorumPeer.getZkDb().getDataTree(); + + final ZooKeeperServer zkServer = mt[followerA].main.quorumPeer.getActiveServer(); + + String node1 = path + "/1"; + String node2 = path + "/2"; + + dt.addNodeCreateListener(node2, new NodeCreateListener() { + @Override + public void process(String path) { + LOG.info("Take a snapshot"); + zkServer.takeSnapshot(true); + } + }); + + LOG.info("Issue a multi op to create 2 nodes"); + zk[followerA].multi(Arrays.asList( + Op.create(node1, node1.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create(node2, node2.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT))); + + LOG.info("Restart the server"); + mt[followerA].shutdown(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTING); + + mt[followerA].start(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED); + + LOG.info("Make sure the node consistent with leader"); + assertEquals( + new String(zk[leaderId].getData(node2, null, null)), + new String(zk[followerA].getData(node2, null, null))); + } + + /** + * It's possibel during SNAP sync, the parent is serialized before the + * child get deleted during sending the snapshot over. + * + * In which case, we need to make sure the pzxid get correctly updated + * when applying the txns received. + */ + @Test + public void testPZxidUpdatedDuringSnapSyncing() throws Exception { + LOG.info("Enable force snapshot sync"); + System.setProperty(LearnerHandler.FORCE_SNAP_SYNC, "true"); + + final String parent = "/testPZxidUpdatedWhenDeletingNonExistNode"; + final String child = parent + "/child"; + createEmptyNode(zk[leaderId], parent, CreateMode.PERSISTENT); + createEmptyNode(zk[leaderId], child, CreateMode.EPHEMERAL); + // create another child to test closeSession + createEmptyNode(zk[leaderId], child + "1", CreateMode.EPHEMERAL); + + LOG.info("shutdown follower {}", followerA); + mt[followerA].shutdown(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTING); + + LOG.info("Set up ZKDatabase to catch the node serializing in DataTree"); + addSerializeListener(leaderId, parent, child); + + LOG.info("Restart follower A to trigger a SNAP sync with leader"); + mt[followerA].start(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED); + + LOG.info("Check and make sure the pzxid of the parent is the same on leader and follower A"); + compareStat(parent, leaderId, followerA); + } + + /** + * It's possible during taking fuzzy snapshot, the parent is serialized + * before the child get deleted in the fuzzy range. + * + * In which case, we need to make sure the pzxid get correctly updated + * when replaying the txns. + */ + @Test + public void testPZxidUpdatedWhenLoadingSnapshot() throws Exception { + + final String parent = "/testPZxidUpdatedDuringTakingSnapshot"; + final String child = parent + "/child"; + createEmptyNode(zk[followerA], parent, CreateMode.PERSISTENT); + createEmptyNode(zk[followerA], child, CreateMode.EPHEMERAL); + // create another child to test closeSession + createEmptyNode(zk[leaderId], child + "1", CreateMode.EPHEMERAL); + + LOG.info("Set up ZKDatabase to catch the node serializing in DataTree"); + addSerializeListener(followerA, parent, child); + + LOG.info("Take snapshot on follower A"); + ZooKeeperServer zkServer = mt[followerA].main.quorumPeer.getActiveServer(); + zkServer.takeSnapshot(true); + + LOG.info("Restarting follower A to load snapshot"); + mt[followerA].shutdown(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CLOSED); + mt[followerA].start(); + // zk[followerA] will be closed in addSerializeListener, re-create it + zk[followerA] = new ZooKeeper("127.0.0.1:" + clientPorts[followerA], + ClientBase.CONNECTION_TIMEOUT, this); + + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED); + + LOG.info("Check and make sure the pzxid of the parent is the same on leader and follower A"); + compareStat(parent, leaderId, followerA); + } + + @Test + public void testMultiOpDigestConsistentDuringSnapshot() throws Exception { + ServerMetrics.getMetrics().resetAll(); + + LOG.info("Create some txns"); + final String path = "/testMultiOpDigestConsistentDuringSnapshot"; + createEmptyNode(zk[followerA], path, CreateMode.PERSISTENT); + + CustomDataTree dt = + (CustomDataTree) mt[followerA].main.quorumPeer.getZkDb().getDataTree(); + final CountDownLatch setDataLatch = new CountDownLatch(1); + final CountDownLatch continueSetDataLatch = new CountDownLatch(1); + final ZooKeeper followerZk = zk[followerA]; + dt.setDigestSerializeListener(new DigestSerializeListener() { + @Override + public void process() { + LOG.info("Trigger a multi op in async"); + followerZk.multi(Arrays.asList( + Op.create("/multi0", "/multi0".getBytes(), + Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.setData(path, "new data".getBytes(), -1) + ), new MultiCallback() { + @Override + public void processResult(int rc, String path, Object ctx, + List opResults) {} + }, null); + + LOG.info("Wait for the signal to continue"); + try { + setDataLatch.await(3, TimeUnit.SECONDS); + } catch (Exception e) { + LOG.error("Error while waiting for set data txn, {}", e); + } + } + + @Override + public void finished() { + LOG.info("Finished writing digest out, continue"); + continueSetDataLatch.countDown(); + } + }); + + dt.setDataListener(new SetDataTxnListener() { + @Override + public void process() { + setDataLatch.countDown(); + try { + continueSetDataLatch.await(3, TimeUnit.SECONDS); + } catch (Exception e) { + LOG.error("Error while waiting for continue signal, {}", e); + } + } + }); + + LOG.info("Trigger a snapshot"); + ZooKeeperServer zkServer = mt[followerA].main.quorumPeer.getActiveServer(); + zkServer.takeSnapshot(true); + checkNoMismatchReported(); + + LOG.info("Restart the server to load the snapshot again"); + mt[followerA].shutdown(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTING); + mt[followerA].start(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED); + + LOG.info("Make sure there is nothing caught in the digest mismatch"); + checkNoMismatchReported(); + + } + + private void checkNoMismatchReported() { + long mismatch = (long) MetricsUtils.currentServerMetrics().get("digest_mismatches_count"); + + assertFalse("The mismatch count should be zero but is: " + mismatch, mismatch > 0); + } + + private void addSerializeListener(int sid, String parent, String child) { + final ZooKeeper zkClient = zk[sid]; + CustomDataTree dt = (CustomDataTree) mt[sid].main.quorumPeer.getZkDb().getDataTree(); + dt.addListener(parent, new NodeSerializeListener() { + @Override + public void nodeSerialized(String path) { + try { + zkClient.delete(child, -1); + zkClient.close(); + LOG.info("Deleted the child node after the parent is serialized"); + } catch (Exception e) { + LOG.error("Error when deleting node {}", e); + } + } + }); + } + + private void compareStat(String path, int sid, int compareWithSid) throws Exception { + ZooKeeper[] compareZk = new ZooKeeper[2]; + compareZk[0] = new ZooKeeper("127.0.0.1:" + clientPorts[sid], + ClientBase.CONNECTION_TIMEOUT, this); + compareZk[1] = new ZooKeeper("127.0.0.1:" + clientPorts[compareWithSid], + ClientBase.CONNECTION_TIMEOUT, this); + QuorumPeerMainTest.waitForAll(compareZk, States.CONNECTED); + + try { + Stat stat1 = new Stat(); + compareZk[0].getData(path, null, stat1); + + Stat stat2 = new Stat(); + compareZk[1].getData(path, null, stat2); + + assertEquals(stat1, stat2); + } finally { + for (ZooKeeper z: compareZk) { + z.close(); + } + } + } + + @Test + public void testGlobalSessionConsistency() throws Exception { + LOG.info("Hook to catch the commitSession event on followerA"); + CustomizedQPMain followerAMain = (CustomizedQPMain) mt[followerA].main; + final ZooKeeperServer zkServer = followerAMain.quorumPeer.getActiveServer(); + + // only take snapshot for the next global session we're going to create + final AtomicBoolean shouldTakeSnapshot = new AtomicBoolean(true); + followerAMain.setCommitSessionListener(new CommitSessionListener() { + @Override + public void process(long sessionId) { + LOG.info("Take snapshot"); + if (shouldTakeSnapshot.getAndSet(false)) { + zkServer.takeSnapshot(true); + } + } + }); + + LOG.info("Create a global session"); + ZooKeeper globalClient = new ZooKeeper( + "127.0.0.1:" + clientPorts[followerA], + ClientBase.CONNECTION_TIMEOUT, + this); + QuorumPeerMainTest.waitForOne(globalClient, States.CONNECTED); + + LOG.info("Restart followerA to load the data from disk"); + mt[followerA].shutdown(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTING); + + mt[followerA].start(); + QuorumPeerMainTest.waitForOne(zk[followerA], States.CONNECTED); + + LOG.info("Make sure the global sessions are consistent with leader"); + + Map globalSessionsOnLeader = mt[leaderId].main.quorumPeer.getZkDb().getSessionWithTimeOuts(); + Map globalSessionsOnFollowerA = mt[followerA].main.quorumPeer.getZkDb().getSessionWithTimeOuts(); + LOG.info("sessions are {}, {}", globalSessionsOnLeader.keySet(), globalSessionsOnFollowerA.keySet()); + assertTrue(globalSessionsOnFollowerA.keySet().containsAll(globalSessionsOnLeader.keySet())); + } + + private void createEmptyNode(ZooKeeper zk, String path, CreateMode mode) throws Exception { + zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, mode); + } + + interface NodeCreateListener { + + void process(String path); + + } + + interface DigestSerializeListener { + void process(); + + void finished(); + } + + interface SetDataTxnListener { + void process(); + } + + static class CustomDataTree extends DataTree { + + Map nodeCreateListeners = new HashMap(); + Map listeners = new HashMap(); + DigestSerializeListener digestListener; + SetDataTxnListener setListener; + + @Override + public void serializeNodeData(OutputArchive oa, String path, DataNode node) throws IOException { + super.serializeNodeData(oa, path, node); + NodeSerializeListener listener = listeners.get(path); + if (listener != null) { + listener.nodeSerialized(path); + } + } + + public void addListener(String path, NodeSerializeListener listener) { + listeners.put(path, listener); + } + + @Override + public void createNode( + final String path, + byte[] data, + List acl, + long ephemeralOwner, + int parentCVersion, + long zxid, + long time, + Stat outputStat) throws NoNodeException, NodeExistsException { + NodeCreateListener listener = nodeCreateListeners.get(path); + if (listener != null) { + listener.process(path); + } + super.createNode(path, data, acl, ephemeralOwner, parentCVersion, zxid, time, outputStat); + } + + public void addNodeCreateListener(String path, NodeCreateListener listener) { + nodeCreateListeners.put(path, listener); + } + + public void setDigestSerializeListener(DigestSerializeListener listener) { + this.digestListener = listener; + } + + public void setDataListener(SetDataTxnListener listener) { + this.setListener = listener; + } + + @Override + public boolean serializeZxidDigest(OutputArchive oa) throws IOException { + if (digestListener != null) { + digestListener.process(); + } + boolean result = super.serializeZxidDigest(oa); + if (digestListener != null) { + digestListener.finished(); + } + return result; + } + + public Stat setData(String path, byte data[], int version, long zxid, + long time) throws NoNodeException { + if (setListener != null) { + setListener.process(); + } + + return super.setData(path, data, version, zxid, time); + } + } + + interface NodeSerializeListener { + + void nodeSerialized(String path); + + } + + interface CommitSessionListener { + + void process(long sessionId); + + } + + static class CustomizedQPMain extends TestQPMain { + + CommitSessionListener commitSessionListener; + + public void setCommitSessionListener(CommitSessionListener listener) { + this.commitSessionListener = listener; + } + + @Override + protected QuorumPeer getQuorumPeer() throws SaslException { + return new QuorumPeer() { + @Override + public void setZKDatabase(ZKDatabase database) { + super.setZKDatabase(new ZKDatabase(this.getTxnFactory()) { + @Override + public DataTree createDataTree() { + return new CustomDataTree(); + } + }); + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb()) { + @Override + public void createSessionTracker() { + sessionTracker = new LearnerSessionTracker( + this, + getZKDatabase().getSessionWithTimeOuts(), + this.tickTime, + self.getId(), + self.areLocalSessionsEnabled(), + getZooKeeperServerListener()) { + + public synchronized boolean commitSession( + long sessionId, int sessionTimeout) { + if (commitSessionListener != null) { + commitSessionListener.process(sessionId); + } + return super.commitSession(sessionId, sessionTimeout); + } + }; + } + }); + } + }; + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderBeanTest.java new file mode 100644 index 0000000..0c6938b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderBeanTest.java @@ -0,0 +1,214 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class LeaderBeanTest { + + private Leader leader; + private LeaderBean leaderBean; + private FileTxnSnapLog fileTxnSnapLog; + private LeaderZooKeeperServer zks; + private QuorumPeer qp; + private QuorumVerifier quorumVerifierMock; + + public static Map getMockedPeerViews(long myId) { + int clientPort = PortAssignment.unique(); + Map peersView = new HashMap(); + InetAddress clientIP = InetAddress.getLoopbackAddress(); + + peersView.put(Long.valueOf(myId), + new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), + new InetSocketAddress(clientIP, PortAssignment.unique()), + new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT)); + return peersView; + } + + @Before + public void setUp() throws IOException, X509Exception { + qp = new QuorumPeer(); + quorumVerifierMock = mock(QuorumVerifier.class); + when(quorumVerifierMock.getAllMembers()).thenReturn(getMockedPeerViews(qp.getId())); + + qp.setQuorumVerifier(quorumVerifierMock, false); + File tmpDir = ClientBase.createEmptyTestDir(); + fileTxnSnapLog = new FileTxnSnapLog(new File(tmpDir, "data"), new File(tmpDir, "data_txnlog")); + ZKDatabase zkDb = new ZKDatabase(fileTxnSnapLog); + + zks = new LeaderZooKeeperServer(fileTxnSnapLog, qp, zkDb); + leader = new Leader(qp, zks); + leaderBean = new LeaderBean(leader, zks); + } + + @After + public void tearDown() throws IOException { + fileTxnSnapLog.close(); + } + + @Test + public void testGetName() { + assertEquals("Leader", leaderBean.getName()); + } + + @Test + public void testGetCurrentZxid() { + // Arrange + zks.setZxid(1); + + // Assert + assertEquals("0x1", leaderBean.getCurrentZxid()); + } + + @Test + public void testGetElectionTimeTaken() { + // Arrange + qp.setElectionTimeTaken(1); + + // Assert + assertEquals(1, leaderBean.getElectionTimeTaken()); + } + + @Test + public void testGetProposalSize() throws IOException, Leader.XidRolloverException { + // Arrange + Request req = createMockRequest(); + + // Act + leader.propose(req); + + // Assert + byte[] data = SerializeUtils.serializeRequest(req); + assertEquals(data.length, leaderBean.getLastProposalSize()); + assertEquals(data.length, leaderBean.getMinProposalSize()); + assertEquals(data.length, leaderBean.getMaxProposalSize()); + } + + @Test + public void testResetProposalStats() throws IOException, Leader.XidRolloverException { + // Arrange + int initialProposalSize = leaderBean.getLastProposalSize(); + Request req = createMockRequest(); + + // Act + leader.propose(req); + + // Assert + assertNotEquals(initialProposalSize, leaderBean.getLastProposalSize()); + leaderBean.resetProposalStatistics(); + assertEquals(initialProposalSize, leaderBean.getLastProposalSize()); + assertEquals(initialProposalSize, leaderBean.getMinProposalSize()); + assertEquals(initialProposalSize, leaderBean.getMaxProposalSize()); + } + + private Request createMockRequest() throws IOException { + TxnHeader header = mock(TxnHeader.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + OutputArchive oa = (OutputArchive) args[0]; + oa.writeString("header", "test"); + return null; + } + }).when(header).serialize(any(OutputArchive.class), anyString()); + Record txn = mock(Record.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + OutputArchive oa = (OutputArchive) args[0]; + oa.writeString("record", "test"); + return null; + } + }).when(txn).serialize(any(OutputArchive.class), anyString()); + return new Request(1, 2, 3, header, txn, 4); + } + + @Test + public void testFollowerInfo() throws IOException { + Map votingMembers = new HashMap(); + votingMembers.put(1L, null); + votingMembers.put(2L, null); + votingMembers.put(3L, null); + when(quorumVerifierMock.getVotingMembers()).thenReturn(votingMembers); + + LearnerHandler follower = mock(LearnerHandler.class); + when(follower.getLearnerType()).thenReturn(LearnerType.PARTICIPANT); + when(follower.toString()).thenReturn("1"); + when(follower.getSid()).thenReturn(1L); + leader.addLearnerHandler(follower); + leader.addForwardingFollower(follower); + + assertEquals("1\n", leaderBean.followerInfo()); + assertEquals("", leaderBean.nonVotingFollowerInfo()); + + LearnerHandler observer = mock(LearnerHandler.class); + when(observer.getLearnerType()).thenReturn(LearnerType.OBSERVER); + when(observer.toString()).thenReturn("2"); + leader.addLearnerHandler(observer); + + assertEquals("1\n", leaderBean.followerInfo()); + assertEquals("", leaderBean.nonVotingFollowerInfo()); + + LearnerHandler nonVotingFollower = mock(LearnerHandler.class); + when(nonVotingFollower.getLearnerType()).thenReturn(LearnerType.PARTICIPANT); + when(nonVotingFollower.toString()).thenReturn("5"); + when(nonVotingFollower.getSid()).thenReturn(5L); + leader.addLearnerHandler(nonVotingFollower); + leader.addForwardingFollower(nonVotingFollower); + + String followerInfo = leaderBean.followerInfo(); + assertTrue(followerInfo.contains("1")); + assertTrue(followerInfo.contains("5")); + assertEquals("5\n", leaderBean.nonVotingFollowerInfo()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderMetricsTest.java new file mode 100644 index 0000000..40ddfe6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderMetricsTest.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LeaderMetricsTest extends ZKTestCase { + + CountDownLatch createdLatch; + int oldLoggingFeq; + + private class MyWatcher implements Watcher { + + @Override + public void process(WatchedEvent e) { + createdLatch.countDown(); + } + + } + + @Before + public void setup() { + oldLoggingFeq = Leader.getAckLoggingFrequency(); + } + + @After + public void teardown() { + Leader.setAckLoggingFrequency(oldLoggingFeq); + } + + @Test + public void testLeaderMetrics() throws Exception { + // set the logging frequency to one so we log the ack latency for every ack + Leader.setAckLoggingFrequency(1); + + ServerMetrics.getMetrics().resetAll(); + + QuorumUtil util = new QuorumUtil(1); //creating a quorum of 3 servers + util.startAll(); + + ZooKeeper zk = ClientBase.createZKClient(util.getConnString()); + createdLatch = new CountDownLatch(1); + zk.exists("/test", new MyWatcher()); + zk.create("/test", new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + createdLatch.await(); + + Map values = MetricsUtils.currentServerMetrics(); + + assertEquals(2L, values.get("proposal_count")); + // Quorum ack latency is per txn + assertEquals(2L, values.get("cnt_quorum_ack_latency")); + assertThat((long) values.get("min_quorum_ack_latency"), greaterThan(0L)); + + int numberOfAckServers = 0; + // ack latency is per server + for (int sid = 1; sid <= 3; sid++) { + String metricName = "min_" + sid + "_ack_latency"; + if (values.get(metricName) != null) { + numberOfAckServers++; + assertThat((long) values.get("min_" + sid + "_ack_latency"), greaterThanOrEqualTo(0L)); + } + } + + // at least two servers should have send ACKs + assertThat(numberOfAckServers, greaterThanOrEqualTo(2)); + + zk.close(); + util.shutdownAll(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderWithObserverTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderWithObserverTest.java new file mode 100644 index 0000000..91292de --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LeaderWithObserverTest.java @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.server.quorum.ZabUtils.createLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class LeaderWithObserverTest { + + QuorumPeer peer; + Leader leader; + File tmpDir; + long participantId; + long observerId; + + @Before + public void setUp() throws Exception { + tmpDir = ClientBase.createTmpDir(); + peer = createQuorumPeer(tmpDir); + participantId = 1; + Map peers = peer.getQuorumVerifier().getAllMembers(); + observerId = peers.size(); + leader = createLeader(tmpDir, peer); + peer.leader = leader; + peers.put(observerId, new QuorumPeer.QuorumServer(observerId, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), QuorumPeer.LearnerType.OBSERVER)); + + // these tests are serial, we can speed up InterruptedException + peer.tickTime = 1; + } + + @After + public void tearDown() { + leader.shutdown("end of test"); + tmpDir.delete(); + } + + @Test + public void testGetEpochToPropose() throws Exception { + long lastAcceptedEpoch = 5; + peer.setAcceptedEpoch(5); + + assertEquals("Unexpected vote in connectingFollowers", 0, leader.connectingFollowers.size()); + assertTrue(leader.waitingForNewEpoch); + try { + // Leader asks for epoch (mocking Leader.lead behavior) + // First add to connectingFollowers + leader.getEpochToPropose(peer.getId(), lastAcceptedEpoch); + } catch (InterruptedException e) { + // ignore timeout + } + + assertEquals("Unexpected vote in connectingFollowers", 1, leader.connectingFollowers.size()); + assertEquals("Leader shouldn't set new epoch until quorum of participants is in connectingFollowers", lastAcceptedEpoch, peer.getAcceptedEpoch()); + assertTrue(leader.waitingForNewEpoch); + try { + // Observer asks for epoch (mocking LearnerHandler behavior) + leader.getEpochToPropose(observerId, lastAcceptedEpoch); + } catch (InterruptedException e) { + // ignore timeout + } + + assertEquals("Unexpected vote in connectingFollowers", 1, leader.connectingFollowers.size()); + assertEquals("Leader shouldn't set new epoch after observer asks for epoch", lastAcceptedEpoch, peer.getAcceptedEpoch()); + assertTrue(leader.waitingForNewEpoch); + try { + // Now participant asks for epoch (mocking LearnerHandler behavior). Second add to connectingFollowers. + // Triggers verifier.containsQuorum = true + leader.getEpochToPropose(participantId, lastAcceptedEpoch); + } catch (Exception e) { + fail("Timed out in getEpochToPropose"); + } + + assertEquals("Unexpected vote in connectingFollowers", 2, leader.connectingFollowers.size()); + assertEquals("Leader should record next epoch", lastAcceptedEpoch + 1, peer.getAcceptedEpoch()); + assertFalse(leader.waitingForNewEpoch); + } + + @Test + public void testWaitForEpochAck() throws Exception { + // things needed for waitForEpochAck to run (usually in leader.lead(), but we're not running leader here) + leader.leaderStateSummary = new StateSummary(leader.self.getCurrentEpoch(), leader.zk.getLastProcessedZxid()); + + assertEquals("Unexpected vote in electingFollowers", 0, leader.electingFollowers.size()); + assertFalse(leader.electionFinished); + try { + // leader calls waitForEpochAck, first add to electingFollowers + leader.waitForEpochAck(peer.getId(), new StateSummary(0, 0)); + } catch (InterruptedException e) { + // ignore timeout + } + + assertEquals("Unexpected vote in electingFollowers", 1, leader.electingFollowers.size()); + assertFalse(leader.electionFinished); + try { + // observer calls waitForEpochAck, should fail verifier.containsQuorum + leader.waitForEpochAck(observerId, new StateSummary(0, 0)); + } catch (InterruptedException e) { + // ignore timeout + } + + assertEquals("Unexpected vote in electingFollowers", 1, leader.electingFollowers.size()); + assertFalse(leader.electionFinished); + try { + // second add to electingFollowers, verifier.containsQuorum=true, waitForEpochAck returns without exceptions + leader.waitForEpochAck(participantId, new StateSummary(0, 0)); + assertEquals("Unexpected vote in electingFollowers", 2, leader.electingFollowers.size()); + assertTrue(leader.electionFinished); + } catch (Exception e) { + fail("Timed out in waitForEpochAck"); + } + } + + @Test + public void testWaitForNewLeaderAck() throws Exception { + long zxid = leader.zk.getZxid(); + + // things needed for waitForNewLeaderAck to run (usually in leader.lead(), but we're not running leader here) + leader.newLeaderProposal.packet = new QuorumPacket(0, zxid, null, null); + leader.newLeaderProposal.addQuorumVerifier(peer.getQuorumVerifier()); + + Set ackSet = leader.newLeaderProposal.qvAcksetPairs.get(0).getAckset(); + assertEquals("Unexpected vote in ackSet", 0, ackSet.size()); + assertFalse(leader.quorumFormed); + try { + // leader calls waitForNewLeaderAck, first add to ackSet + leader.waitForNewLeaderAck(peer.getId(), zxid); + } catch (InterruptedException e) { + // ignore timeout + } + + assertEquals("Unexpected vote in ackSet", 1, ackSet.size()); + assertFalse(leader.quorumFormed); + try { + // observer calls waitForNewLeaderAck, should fail verifier.containsQuorum + leader.waitForNewLeaderAck(observerId, zxid); + } catch (InterruptedException e) { + // ignore timeout + } + + assertEquals("Unexpected vote in ackSet", 1, ackSet.size()); + assertFalse(leader.quorumFormed); + try { + // second add to ackSet, verifier.containsQuorum=true, waitForNewLeaderAck returns without exceptions + leader.waitForNewLeaderAck(participantId, zxid); + assertEquals("Unexpected vote in ackSet", 2, ackSet.size()); + assertTrue(leader.quorumFormed); + } catch (Exception e) { + fail("Timed out in waitForEpochAck"); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerMetricsTest.java new file mode 100644 index 0000000..b6699d8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerMetricsTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerMetrics; +import org.junit.Before; +import org.junit.Test; + +public class LearnerHandlerMetricsTest { + + private MockLearnerHandler learnerHandler; + private long sid = 5; + private volatile CountDownLatch allSentLatch = null; + + class MockLearnerHandler extends LearnerHandler { + + MockLearnerHandler(Socket socket, Leader leader) throws IOException { + super(socket, null, leader); + } + + } + + @Before + public void setup() throws IOException { + Leader leader = mock(Leader.class); + when(leader.getQuorumAuthServer()).thenReturn(null); + + Socket socket = mock(Socket.class); + when(socket.getRemoteSocketAddress()).thenReturn(new InetSocketAddress(32)); + + //adding 5ms artificial delay when sending each packet + BinaryOutputArchive oa = mock(BinaryOutputArchive.class); + doAnswer(invocationOnMock -> { + Thread.sleep(5); + return null; + }).when(oa).writeRecord(any(QuorumPacket.class), anyString()); + + BufferedOutputStream bos = mock(BufferedOutputStream.class); + // flush is called when all packets are sent and the queue is empty + doAnswer(invocationOnMock -> { + if (allSentLatch != null) { + allSentLatch.countDown(); + } + return null; + }).when(bos).flush(); + + learnerHandler = new MockLearnerHandler(socket, leader); + learnerHandler.setOutputArchive(oa); + learnerHandler.setBufferedOutput(bos); + learnerHandler.sid = sid; + } + + @Test + public void testMetrics() throws InterruptedException { + ServerMetrics.getMetrics().resetAll(); + + //adding 1001 packets in the queue, two marker packets will be added since the interval is every 1000 packets + for (int i = 0; i < 1001; i++) { + learnerHandler.queuePacket(new QuorumPacket()); + } + + allSentLatch = new CountDownLatch(1); + + learnerHandler.startSendingPackets(); + + allSentLatch.await(8, TimeUnit.SECONDS); + + Map values = MetricsUtils.currentServerMetrics(); + String sidStr = Long.toString(sid); + + //we record time for each marker packet and we have two marker packets + assertEquals(2L, values.get("cnt_" + sidStr + "_learner_handler_qp_time_ms")); + + //the second marker has 1000 packets in front of it and each takes 5 ms to send so the time in queue should be + //longer than 5*1000 + assertThat((long) values.get("max_" + sidStr + "_learner_handler_qp_time_ms"), greaterThan(5000L)); + + //we send 1001 packets + 2 marker packets so the queue size is recorded 1003 times + assertEquals(1003L, values.get("cnt_" + sidStr + "_learner_handler_qp_size")); + + //the longest queue size is recorded when we are sending the first packet + assertEquals(1002L, values.get("max_" + sidStr + "_learner_handler_qp_size")); + + //this is when the queue is emptied + assertEquals(0L, values.get("min_" + sidStr + "_learner_handler_qp_size")); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerTest.java new file mode 100644 index 0000000..79b454a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerHandlerTest.java @@ -0,0 +1,562 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.TxnLogProposalIterator; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LearnerHandlerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(LearnerHandlerTest.class); + + class MockLearnerHandler extends LearnerHandler { + + boolean threadStarted = false; + + MockLearnerHandler(Socket sock, Leader leader) throws IOException { + super(sock, new BufferedInputStream(sock.getInputStream()), leader); + } + + protected void startSendingPackets() { + threadStarted = true; + } + + @Override + protected boolean shouldSendMarkerPacketForLogging() { + return false; + } + + } + + class MockZKDatabase extends ZKDatabase { + + long lastProcessedZxid; + ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + LinkedList committedLog = new LinkedList(); + LinkedList txnLog = new LinkedList(); + + public MockZKDatabase(FileTxnSnapLog snapLog) { + super(snapLog); + } + + public long getDataTreeLastProcessedZxid() { + return lastProcessedZxid; + } + + public long getmaxCommittedLog() { + if (!committedLog.isEmpty()) { + return committedLog.getLast().packet.getZxid(); + } + return 0; + } + + public long getminCommittedLog() { + if (!committedLog.isEmpty()) { + return committedLog.getFirst().packet.getZxid(); + } + return 0; + } + + public List getCommittedLog() { + return committedLog; + } + + public ReentrantReadWriteLock getLogLock() { + return lock; + } + + public Iterator getProposalsFromTxnLog(long peerZxid, long limit) { + if (peerZxid >= txnLog.peekFirst().packet.getZxid()) { + return txnLog.iterator(); + } else { + return Collections.emptyIterator(); + } + + } + + public long calculateTxnLogSizeLimit() { + return 1; + } + + } + + private MockLearnerHandler learnerHandler; + private Socket sock; + + // Member variables for mocking Leader + private Leader leader; + private long currentZxid; + + // Member variables for mocking ZkDatabase + private MockZKDatabase db; + + @Before + public void setUp() throws Exception { + db = new MockZKDatabase(null); + sock = mock(Socket.class); + + // Intercept when startForwarding is called + leader = mock(Leader.class); + when(leader.startForwarding(ArgumentMatchers.any(LearnerHandler.class), ArgumentMatchers.anyLong())).thenAnswer(new Answer() { + public Long answer(InvocationOnMock invocation) { + currentZxid = invocation.getArgument(1); + return 0L; + } + }); + when(leader.getZKDatabase()).thenReturn(db); + + learnerHandler = new MockLearnerHandler(sock, leader); + } + + Proposal createProposal(long zxid) { + Proposal p = new Proposal(); + p.packet = new QuorumPacket(); + p.packet.setZxid(zxid); + p.packet.setType(Leader.PROPOSAL); + return p; + } + + /** + * Validate that queued packets contains proposal in the following orders as + * a given array of zxids + * + * @param zxids + */ + public void queuedPacketMatches(long[] zxids) { + int index = 0; + for (QuorumPacket qp : learnerHandler.getQueuedPackets()) { + if (qp.getType() == Leader.PROPOSAL) { + assertZxidEquals(zxids[index++], qp.getZxid()); + } + } + } + + void reset() { + learnerHandler.getQueuedPackets().clear(); + learnerHandler.threadStarted = false; + learnerHandler.setFirstPacket(true); + } + + /** + * Check if op packet (first packet in the queue) match the expected value + * @param type - type of packet + * @param zxid - zxid in the op packet + * @param currentZxid - last packet queued by syncFollower, + * before invoking startForwarding() + */ + public void assertOpType(int type, long zxid, long currentZxid) { + Queue packets = learnerHandler.getQueuedPackets(); + assertTrue(packets.size() > 0); + assertEquals(type, packets.peek().getType()); + assertZxidEquals(zxid, packets.peek().getZxid()); + assertZxidEquals(currentZxid, this.currentZxid); + } + + void assertZxidEquals(long expected, long value) { + assertEquals("Expected 0x" + + Long.toHexString(expected) + + " but was 0x" + + Long.toHexString(value), expected, value); + } + + /** + * Test cases when leader has empty commitedLog + */ + @Test + public void testEmptyCommittedLog() throws Exception { + long peerZxid; + + // Peer has newer zxid + peerZxid = 3; + db.lastProcessedZxid = 1; + db.committedLog.clear(); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send TRUNC and forward any packet starting lastProcessedZxid + assertOpType(Leader.TRUNC, db.lastProcessedZxid, db.lastProcessedZxid); + reset(); + + // Peer is already sync + peerZxid = 1; + db.lastProcessedZxid = 1; + db.committedLog.clear(); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF and forward any packet starting lastProcessedZxid + assertOpType(Leader.DIFF, db.lastProcessedZxid, db.lastProcessedZxid); + assertEquals(1, learnerHandler.getQueuedPackets().size()); + reset(); + + // Peer has 0 zxid (new machine turn up), txnlog + // is disabled + peerZxid = 0; + db.setSnapshotSizeFactor(-1); + db.lastProcessedZxid = 1; + db.committedLog.clear(); + // We send SNAP + assertTrue(learnerHandler.syncFollower(peerZxid, leader)); + assertEquals(0, learnerHandler.getQueuedPackets().size()); + reset(); + + } + + /** + * Test cases when leader has committedLog + */ + @Test + public void testCommittedLog() throws Exception { + long peerZxid; + + // Commit proposal may lag behind data tree, but it shouldn't affect + // us in any case + db.lastProcessedZxid = 6; + db.committedLog.add(createProposal(2)); + db.committedLog.add(createProposal(3)); + db.committedLog.add(createProposal(5)); + + // Peer has zxid that we have never seen + peerZxid = 4; + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send TRUNC to 3 and forward any packet starting 5 + assertOpType(Leader.TRUNC, 3, 5); + // DIFF + 1 proposals + 1 commit + assertEquals(3, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{5}); + reset(); + + // Peer is within committedLog range + peerZxid = 2; + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF and forward any packet starting lastProcessedZxid + assertOpType(Leader.DIFF, db.getmaxCommittedLog(), db.getmaxCommittedLog()); + // DIFF + 2 proposals + 2 commit + assertEquals(5, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{3, 5}); + reset(); + + // Peer miss the committedLog and txnlog is disabled + peerZxid = 1; + db.setSnapshotSizeFactor(-1); + // We send SNAP + assertTrue(learnerHandler.syncFollower(peerZxid, leader)); + assertEquals(0, learnerHandler.getQueuedPackets().size()); + reset(); + } + + /** + * Test cases when txnlog is enabled + */ + @Test + public void testTxnLog() throws Exception { + long peerZxid; + db.txnLog.add(createProposal(2)); + db.txnLog.add(createProposal(3)); + db.txnLog.add(createProposal(5)); + db.txnLog.add(createProposal(6)); + db.txnLog.add(createProposal(7)); + db.txnLog.add(createProposal(8)); + db.txnLog.add(createProposal(9)); + + db.lastProcessedZxid = 9; + db.committedLog.add(createProposal(6)); + db.committedLog.add(createProposal(7)); + db.committedLog.add(createProposal(8)); + + // Peer has zxid that we have never seen + peerZxid = 4; + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send TRUNC to 3 and forward any packet starting at maxCommittedLog + assertOpType(Leader.TRUNC, 3, db.getmaxCommittedLog()); + // DIFF + 4 proposals + 4 commit + assertEquals(9, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{5, 6, 7, 8}); + reset(); + + // Peer zxid is in txnlog range + peerZxid = 3; + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF and forward any packet starting at maxCommittedLog + assertOpType(Leader.DIFF, db.getmaxCommittedLog(), db.getmaxCommittedLog()); + // DIFF + 4 proposals + 4 commit + assertEquals(9, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{5, 6, 7, 8}); + reset(); + + } + + /** + * Test case verifying TxnLogProposalIterator closure. + */ + @Test + public void testTxnLogProposalIteratorClosure() throws Exception { + long peerZxid; + + // CommmitedLog is empty, we will use txnlog up to lastProcessZxid + db = new MockZKDatabase(null) { + @Override + public Iterator getProposalsFromTxnLog(long peerZxid, long limit) { + return TxnLogProposalIterator.EMPTY_ITERATOR; + } + }; + db.lastProcessedZxid = 7; + db.txnLog.add(createProposal(2)); + db.txnLog.add(createProposal(3)); + when(leader.getZKDatabase()).thenReturn(db); + + // Peer zxid + peerZxid = 4; + assertTrue("Couldn't identify snapshot transfer!", learnerHandler.syncFollower(peerZxid, leader)); + reset(); + } + + /** + * Test cases when txnlog is enabled and commitedLog is empty + */ + @Test + public void testTxnLogOnly() throws Exception { + long peerZxid; + + // CommmitedLog is empty, we will use txnlog up to lastProcessZxid + db.lastProcessedZxid = 7; + db.txnLog.add(createProposal(2)); + db.txnLog.add(createProposal(3)); + db.txnLog.add(createProposal(5)); + db.txnLog.add(createProposal(6)); + db.txnLog.add(createProposal(7)); + db.txnLog.add(createProposal(8)); + + // Peer has zxid that we have never seen + peerZxid = 4; + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send TRUNC to 3 and forward any packet starting at + // lastProcessedZxid + assertOpType(Leader.TRUNC, 3, db.lastProcessedZxid); + // DIFF + 3 proposals + 3 commit + assertEquals(7, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{5, 6, 7}); + reset(); + + // Peer has zxid in txnlog range + peerZxid = 2; + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF and forward any packet starting at lastProcessedZxid + assertOpType(Leader.DIFF, db.lastProcessedZxid, db.lastProcessedZxid); + // DIFF + 4 proposals + 4 commit + assertEquals(9, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{3, 5, 6, 7}); + reset(); + + // Peer miss the txnlog + peerZxid = 1; + assertTrue(learnerHandler.syncFollower(peerZxid, leader)); + // We send snap + assertEquals(0, learnerHandler.getQueuedPackets().size()); + reset(); + } + + long getZxid(long epoch, long counter) { + return ZxidUtils.makeZxid(epoch, counter); + } + + /** + * Test cases with zxids that are negative long + */ + @Test + public void testTxnLogWithNegativeZxid() throws Exception { + long peerZxid; + db.txnLog.add(createProposal(getZxid(0xf, 2))); + db.txnLog.add(createProposal(getZxid(0xf, 3))); + db.txnLog.add(createProposal(getZxid(0xf, 5))); + db.txnLog.add(createProposal(getZxid(0xf, 6))); + db.txnLog.add(createProposal(getZxid(0xf, 7))); + db.txnLog.add(createProposal(getZxid(0xf, 8))); + db.txnLog.add(createProposal(getZxid(0xf, 9))); + + db.lastProcessedZxid = getZxid(0xf, 9); + db.committedLog.add(createProposal(getZxid(0xf, 6))); + db.committedLog.add(createProposal(getZxid(0xf, 7))); + db.committedLog.add(createProposal(getZxid(0xf, 8))); + + // Peer has zxid that we have never seen + peerZxid = getZxid(0xf, 4); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send TRUNC to 3 and forward any packet starting at maxCommittedLog + assertOpType(Leader.TRUNC, getZxid(0xf, 3), db.getmaxCommittedLog()); + // DIFF + 4 proposals + 4 commit + assertEquals(9, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{getZxid(0xf, 5), getZxid(0xf, 6), getZxid(0xf, 7), getZxid(0xf, 8)}); + reset(); + + // Peer zxid is in txnlog range + peerZxid = getZxid(0xf, 3); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF and forward any packet starting at maxCommittedLog + assertOpType(Leader.DIFF, db.getmaxCommittedLog(), db.getmaxCommittedLog()); + // DIFF + 4 proposals + 4 commit + assertEquals(9, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{getZxid(0xf, 5), getZxid(0xf, 6), getZxid(0xf, 7), getZxid(0xf, 8)}); + reset(); + } + + /** + * Test cases when peer has new-epoch zxid + */ + @Test + public void testNewEpochZxid() throws Exception { + long peerZxid; + db.txnLog.add(createProposal(getZxid(0, 1))); + db.txnLog.add(createProposal(getZxid(1, 1))); + db.txnLog.add(createProposal(getZxid(1, 2))); + + // After leader election, lastProcessedZxid will point to new epoch + db.lastProcessedZxid = getZxid(2, 0); + db.committedLog.add(createProposal(getZxid(1, 1))); + db.committedLog.add(createProposal(getZxid(1, 2))); + + // Peer has zxid of epoch 0 + peerZxid = getZxid(0, 0); + // We should get snap, we can do better here, but the main logic is + // that we should never send diff if we have never seen any txn older + // than peer zxid + assertTrue(learnerHandler.syncFollower(peerZxid, leader)); + assertEquals(0, learnerHandler.getQueuedPackets().size()); + reset(); + + // Peer has zxid of epoch 1 + peerZxid = getZxid(1, 0); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF to (1, 2) and forward any packet starting at (1, 2) + assertOpType(Leader.DIFF, getZxid(1, 2), getZxid(1, 2)); + // DIFF + 2 proposals + 2 commit + assertEquals(5, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{getZxid(1, 1), getZxid(1, 2)}); + reset(); + + // Peer has zxid of epoch 2, so it is already sync + peerZxid = getZxid(2, 0); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF to (2, 0) and forward any packet starting at (2, 0) + assertOpType(Leader.DIFF, getZxid(2, 0), getZxid(2, 0)); + // DIFF only + assertEquals(1, learnerHandler.getQueuedPackets().size()); + reset(); + + } + + /** + * Test cases when there is a duplicate txn in the committedLog. This + * should never happen unless there is a bug in initialization code + * but the learner should never see duplicate packets + */ + @Test + public void testDuplicatedTxn() throws Exception { + long peerZxid; + db.txnLog.add(createProposal(getZxid(0, 1))); + db.txnLog.add(createProposal(getZxid(1, 1))); + db.txnLog.add(createProposal(getZxid(1, 2))); + db.txnLog.add(createProposal(getZxid(1, 1))); + db.txnLog.add(createProposal(getZxid(1, 2))); + + // After leader election, lastProcessedZxid will point to new epoch + db.lastProcessedZxid = getZxid(2, 0); + db.committedLog.add(createProposal(getZxid(1, 1))); + db.committedLog.add(createProposal(getZxid(1, 2))); + db.committedLog.add(createProposal(getZxid(1, 1))); + db.committedLog.add(createProposal(getZxid(1, 2))); + + // Peer has zxid of epoch 1 + peerZxid = getZxid(1, 0); + assertFalse(learnerHandler.syncFollower(peerZxid, leader)); + // We send DIFF to (1, 2) and forward any packet starting at (1, 2) + assertOpType(Leader.DIFF, getZxid(1, 2), getZxid(1, 2)); + // DIFF + 2 proposals + 2 commit + assertEquals(5, learnerHandler.getQueuedPackets().size()); + queuedPacketMatches(new long[]{getZxid(1, 1), getZxid(1, 2)}); + reset(); + + } + + /** + * Test cases when we have to TRUNC learner, but it may cross epoch boundary + * so we need to send snap instead + */ + @Test + public void testCrossEpochTrunc() throws Exception { + long peerZxid; + db.txnLog.add(createProposal(getZxid(1, 1))); + db.txnLog.add(createProposal(getZxid(2, 1))); + db.txnLog.add(createProposal(getZxid(2, 2))); + db.txnLog.add(createProposal(getZxid(4, 1))); + + // After leader election, lastProcessedZxid will point to new epoch + db.lastProcessedZxid = getZxid(6, 0); + + // Peer has zxid (3, 1) + peerZxid = getZxid(3, 1); + assertTrue(learnerHandler.syncFollower(peerZxid, leader)); + assertEquals(0, learnerHandler.getQueuedPackets().size()); + reset(); + } + + /** + * Test cases when the leader's disk is slow. There can be a gap + * between the txnLog and the committedLog. Make sure we detect this + * and send a snap instead of a diff. + */ + @Test + public void testTxnLogGap() throws Exception { + long peerZxid; + db.txnLog.add(createProposal(2)); + db.txnLog.add(createProposal(3)); + db.txnLog.add(createProposal(4)); + + db.lastProcessedZxid = 8; + db.committedLog.add(createProposal(7)); + db.committedLog.add(createProposal(8)); + + // Peer zxid is in txnlog range + peerZxid = 3; + assertTrue(learnerHandler.syncFollower(peerZxid, leader)); + reset(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerMetricsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerMetricsTest.java new file mode 100644 index 0000000..659ba31 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerMetricsTest.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.test.ClientBase; +import org.hamcrest.Matcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class LearnerMetricsTest extends QuorumPeerTestBase { + + private static final int TIMEOUT_SECONDS = 30; + private static final int SERVER_COUNT = 4; // 1 observer, 3 participants + private final QuorumPeerTestBase.MainThread[] mt = new QuorumPeerTestBase.MainThread[SERVER_COUNT]; + private ZooKeeper zk_client; + private boolean asyncSending; + private static boolean bakAsyncSending; + + public LearnerMetricsTest(boolean asyncSending) { + this.asyncSending = asyncSending; + } + + @Parameterized.Parameters + public static Collection sendingModes() { + return Arrays.asList(new Object[][]{{true}, {false}}); + } + + @Before + public void setAsyncSendingFlag() { + Learner.setAsyncSending(asyncSending); + } + + @BeforeClass + public static void saveAsyncSendingFlag() { + bakAsyncSending = Learner.getAsyncSending(); + } + + @AfterClass + public static void resetAsyncSendingFlag() { + Learner.setAsyncSending(bakAsyncSending); + } + + @Test + public void testLearnerMetricsTest() throws Exception { + ServerMetrics.getMetrics().resetAll(); + ClientBase.setupTestEnv(); + + final String path = "/zk-testLeanerMetrics"; + final byte[] data = new byte[512]; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + int observer = 0; + clientPorts[observer] = PortAssignment.unique(); + sb.append("server." + observer + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":observer\n"); + for (int i = 1; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + sb.append("server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + "\n"); + } + + // start the three participants + String quorumCfgSection = sb.toString(); + for (int i = 1; i < SERVER_COUNT; i++) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts[i], quorumCfgSection); + mt[i].start(); + } + + // start the observer + Map observerConfig = new HashMap<>(); + observerConfig.put("peerType", "observer"); + mt[observer] = new QuorumPeerTestBase.MainThread(observer, clientPorts[observer], quorumCfgSection, observerConfig); + mt[observer].start(); + + // connect to the observer node and wait for CONNECTED state + // (this way we make sure to wait until the leader election finished and the observer node joined as well) + zk_client = new ZooKeeper("127.0.0.1:" + clientPorts[observer], ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk_client, ZooKeeper.States.CONNECTED); + + // creating a node + zk_client.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + + // there are two proposals by now, one for the global client session creation, one for the create request + + // there are two followers, each received two PROPOSALs + waitForMetric("learner_proposal_received_count", is(4L)); + waitForMetric("cnt_proposal_latency", is(4L)); + waitForMetric("min_proposal_latency", greaterThanOrEqualTo(0L)); + + // the two ACKs are processed by the leader and by each of the two followers + waitForMetric("cnt_proposal_ack_creation_latency", is(6L)); + waitForMetric("min_proposal_ack_creation_latency", greaterThanOrEqualTo(0L)); + + // two COMMITs are received by each of the two followers, and two INFORMs are received by the single observer + // (the INFORM message is also counted into the "commit_received" metrics) + waitForMetric("learner_commit_received_count", is(6L)); + waitForMetric("cnt_commit_propagation_latency", is(6L)); + waitForMetric("min_commit_propagation_latency", greaterThanOrEqualTo(0L)); + } + + private void waitForMetric(final String metricKey, final Matcher matcher) throws InterruptedException { + final String errorMessage = String.format("unable to match on metric: %s", metricKey); + waitFor(errorMessage, () -> { + long actual = (long) MetricsUtils.currentServerMetrics().get(metricKey); + if (!matcher.matches(actual)) { + LOG.info("match failed on {}, actual value: {}", metricKey, actual); + return false; + } + return true; + }, TIMEOUT_SECONDS); + } + + @After + public void tearDown() throws Exception { + zk_client.close(); + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerSyncThrottlerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerSyncThrottlerTest.java new file mode 100644 index 0000000..9c6385e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerSyncThrottlerTest.java @@ -0,0 +1,168 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(Parameterized.class) +public class LearnerSyncThrottlerTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(LearnerSyncThrottlerTest.class); + + private LearnerSyncThrottler.SyncType syncType; + public LearnerSyncThrottlerTest(LearnerSyncThrottler.SyncType syncType) { + this.syncType = syncType; + } + + @Parameterized.Parameters + public static Collection syncTypes() { + return Arrays.asList(new Object[][]{{LearnerSyncThrottler.SyncType.DIFF}, {LearnerSyncThrottler.SyncType.SNAP}}); + } + @Test(expected = SyncThrottleException.class) + public void testTooManySyncsNonessential() throws Exception { + LearnerSyncThrottler throttler = new LearnerSyncThrottler(5, syncType); + for (int i = 0; i < 6; i++) { + throttler.beginSync(false); + } + } + + @Test(expected = SyncThrottleException.class) + public void testTooManySyncsEssential() throws Exception { + LearnerSyncThrottler throttler = new LearnerSyncThrottler(5, syncType); + try { + for (int i = 0; i < 6; i++) { + throttler.beginSync(true); + } + } catch (SyncThrottleException ex) { + fail("essential syncs should not be throttled"); + } + throttler.endSync(); + throttler.beginSync(false); + } + + @Test + public void testNoThrottle() throws Exception { + LearnerSyncThrottler throttler = new LearnerSyncThrottler(5, syncType); + try { + for (int i = 0; i < 6; i++) { + throttler.beginSync(true); + } + } catch (SyncThrottleException ex) { + fail("essential syncs should not be throttled"); + } + throttler.endSync(); + for (int i = 0; i < 5; i++) { + throttler.endSync(); + throttler.beginSync(false); + } + assertTrue("should get here without exception", true); + } + + @Test + public void testTryWithResourceNoThrottle() throws Exception { + LearnerSyncThrottler throttler = new LearnerSyncThrottler(1, syncType); + for (int i = 0; i < 3; i++) { + throttler.beginSync(false); + try { + assertEquals(1, throttler.getSyncInProgress()); + } finally { + throttler.endSync(); + } + } + } + + @Test + public void testTryWithResourceThrottle() throws Exception { + LearnerSyncThrottler throttler = new LearnerSyncThrottler(1, syncType); + try { + throttler.beginSync(true); + try { + throttler.beginSync(false); + fail("shouldn't be able to have both syncs open"); + } catch (SyncThrottleException e) { + } + throttler.endSync(); + } catch (SyncThrottleException e) { + fail("First sync shouldn't be throttled"); + } + } + + @Test + public void testParallelNoThrottle() { + final int numThreads = 50; + + final LearnerSyncThrottler throttler = new LearnerSyncThrottler(numThreads, syncType); + ExecutorService threadPool = Executors.newFixedThreadPool(numThreads); + final CountDownLatch threadStartLatch = new CountDownLatch(numThreads); + final CountDownLatch syncProgressLatch = new CountDownLatch(numThreads); + + List> results = new ArrayList>(numThreads); + for (int i = 0; i < numThreads; i++) { + results.add(threadPool.submit(new Callable() { + + @Override + public Boolean call() { + threadStartLatch.countDown(); + try { + threadStartLatch.await(); + + throttler.beginSync(false); + + syncProgressLatch.countDown(); + syncProgressLatch.await(); + + throttler.endSync(); + } catch (Exception e) { + return false; + } + + return true; + } + })); + } + + try { + for (Future result : results) { + assertTrue(result.get()); + } + } catch (Exception e) { + + } finally { + threadPool.shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerTest.java new file mode 100644 index 0000000..97ab5a2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LearnerTest.java @@ -0,0 +1,364 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptySet; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import java.util.function.Consumer; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.server.ExitCode; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.test.TestUtils; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.apache.zookeeper.util.ServiceUtils; +import org.junit.After; +import org.junit.Test; + +public class LearnerTest extends ZKTestCase { + + private static final File testData = new File(System.getProperty("test.data.dir", "src/test/resources/data")); + + static class SimpleLearnerZooKeeperServer extends LearnerZooKeeperServer { + + Learner learner; + + public SimpleLearnerZooKeeperServer(FileTxnSnapLog ftsl, QuorumPeer self) throws IOException { + super(ftsl, 2000, 2000, 2000, -1, new ZKDatabase(ftsl), self); + } + + @Override + public Learner getLearner() { + return learner; + } + + } + + static class SimpleLearner extends Learner { + + SimpleLearner(FileTxnSnapLog ftsl) throws IOException { + self = new QuorumPeer(); + zk = new SimpleLearnerZooKeeperServer(ftsl, self); + ((SimpleLearnerZooKeeperServer) zk).learner = this; + } + + } + + static class TestLearner extends Learner { + + private int passSocketConnectOnAttempt = 10; + private int socketConnectAttempt = 0; + private long timeMultiplier = 0; + private Socket socketToBeCreated = null; + private Set unreachableAddresses = emptySet(); + + private void setTimeMultiplier(long multiplier) { + timeMultiplier = multiplier; + } + + private void setPassConnectAttempt(int num) { + passSocketConnectOnAttempt = num; + } + + protected long nanoTime() { + return socketConnectAttempt * timeMultiplier; + } + + private int getSockConnectAttempt() { + return socketConnectAttempt; + } + + private void setSocketToBeCreated(Socket socketToBeCreated) { + this.socketToBeCreated = socketToBeCreated; + } + + private void setUnreachableAddresses(Set unreachableAddresses) { + this.unreachableAddresses = unreachableAddresses; + } + + @Override + protected void sockConnect(Socket sock, InetSocketAddress addr, int timeout) throws IOException { + synchronized (this) { + if (++socketConnectAttempt < passSocketConnectOnAttempt || unreachableAddresses.contains(addr)) { + throw new IOException("Test injected Socket.connect() error."); + } + } + } + + @Override + protected Socket createSocket() throws X509Exception, IOException { + if (socketToBeCreated != null) { + return socketToBeCreated; + } + return super.createSocket(); + } + } + + @After + public void cleanup() { + System.clearProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED); + } + + @Test(expected = IOException.class) + public void connectionRetryTimeoutTest() throws Exception { + Learner learner = new TestLearner(); + learner.self = new QuorumPeer(); + learner.self.setTickTime(2000); + learner.self.setInitLimit(5); + learner.self.setSyncLimit(2); + + // this addr won't even be used since we fake the Socket.connect + InetSocketAddress addr = new InetSocketAddress(1111); + + // we expect this to throw an IOException since we're faking socket connect errors every time + learner.connectToLeader(new MultipleAddresses(addr), ""); + } + + @Test + public void connectionInitLimitTimeoutTest() throws Exception { + TestLearner learner = new TestLearner(); + learner.self = new QuorumPeer(); + learner.self.setTickTime(2000); + learner.self.setInitLimit(5); + learner.self.setSyncLimit(2); + + // this addr won't even be used since we fake the Socket.connect + InetSocketAddress addr = new InetSocketAddress(1111); + + // pretend each connect attempt takes 4000 milliseconds + learner.setTimeMultiplier((long) 4000 * 1000_000); + + learner.setPassConnectAttempt(5); + + // we expect this to throw an IOException since we're faking socket connect errors every time + try { + learner.connectToLeader(new MultipleAddresses(addr), ""); + fail("should have thrown IOException!"); + } catch (IOException e) { + //good, wanted to see that, let's make sure we ran out of time + assertTrue(learner.nanoTime() > 2000 * 5 * 1000_000); + assertEquals(3, learner.getSockConnectAttempt()); + } + } + + @Test + public void shouldTryMultipleAddresses() throws Exception { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + TestLearner learner = new TestLearner(); + learner.self = new QuorumPeer(); + learner.self.setTickTime(2000); + learner.self.setInitLimit(5); + learner.self.setSyncLimit(2); + + // this addr won't even be used since we fake the Socket.connect + InetSocketAddress addrA = new InetSocketAddress(1111); + InetSocketAddress addrB = new InetSocketAddress(2222); + InetSocketAddress addrC = new InetSocketAddress(3333); + InetSocketAddress addrD = new InetSocketAddress(4444); + + // we will never pass (don't allow successful socker.connect) during this test + learner.setPassConnectAttempt(100); + + // we expect this to throw an IOException since we're faking socket connect errors every time + try { + learner.connectToLeader(new MultipleAddresses(asList(addrA, addrB, addrC, addrD)), ""); + fail("should have thrown IOException!"); + } catch (IOException e) { + //good, wanted to see the IOException, let's make sure we tried each address 5 times + assertEquals(4 * 5, learner.getSockConnectAttempt()); + } + } + + @Test + public void multipleAddressesSomeAreFailing() throws Exception { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + TestLearner learner = new TestLearner(); + learner.self = new QuorumPeer(); + learner.self.setTickTime(2000); + learner.self.setInitLimit(5); + learner.self.setSyncLimit(2); + + // these addresses won't even be used since we fake the Socket.connect + InetSocketAddress addrWorking = new InetSocketAddress(1111); + InetSocketAddress addrBadA = new InetSocketAddress(2222); + InetSocketAddress addrBadB = new InetSocketAddress(3333); + InetSocketAddress addrBadC = new InetSocketAddress(4444); + + // we will emulate socket connection error for each 'bad' address + learner.setUnreachableAddresses(new HashSet<>(asList(addrBadA, addrBadB, addrBadC))); + + // all connection attempts should succeed (if it is not an unreachable address) + learner.setPassConnectAttempt(0); + + // initialize a mock socket, created by the Learner + Socket mockSocket = mock(Socket.class); + when(mockSocket.isConnected()).thenReturn(true); + learner.setSocketToBeCreated(mockSocket); + + + // we expect this to not throw an IOException since there is a single working address + learner.connectToLeader(new MultipleAddresses(asList(addrBadA, addrBadB, addrBadC, addrWorking)), ""); + + assertEquals("Learner connected to the wrong address", learner.getSocket(), mockSocket); + } + + @Test + public void connectToLearnerMasterLimitTest() throws Exception { + TestLearner learner = new TestLearner(); + learner.self = new QuorumPeer(); + learner.self.setTickTime(2000); + learner.self.setInitLimit(2); + learner.self.setSyncLimit(2); + learner.self.setConnectToLearnerMasterLimit(5); + + InetSocketAddress addr = new InetSocketAddress(1111); + learner.setTimeMultiplier((long) 4000 * 1000_000); + learner.setPassConnectAttempt(5); + + try { + learner.connectToLeader(new MultipleAddresses(addr), ""); + fail("should have thrown IOException!"); + } catch (IOException e) { + assertTrue(learner.nanoTime() > 2000 * 5 * 1000_000); + assertEquals(3, learner.getSockConnectAttempt()); + } + } + + @Test + public void syncTest() throws Exception { + File tmpFile = File.createTempFile("test", ".dir", testData); + tmpFile.delete(); + try { + FileTxnSnapLog ftsl = new FileTxnSnapLog(tmpFile, tmpFile); + SimpleLearner sl = new SimpleLearner(ftsl); + long startZxid = sl.zk.getLastProcessedZxid(); + + // Set up bogus streams + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); + sl.leaderOs = BinaryOutputArchive.getArchive(new ByteArrayOutputStream()); + + // make streams and socket do something innocuous + sl.bufferedOutput = new BufferedOutputStream(System.out); + sl.sock = new Socket(); + + // fake messages from the server + QuorumPacket qp = new QuorumPacket(Leader.SNAP, 0, null, null); + oa.writeRecord(qp, null); + sl.zk.getZKDatabase().serializeSnapshot(oa); + oa.writeString("BenWasHere", "signature"); + TxnHeader hdr = new TxnHeader(0, 0, 0, 0, ZooDefs.OpCode.create); + CreateTxn txn = new CreateTxn("/foo", new byte[0], new ArrayList(), false, sl.zk.getZKDatabase().getNode("/").stat.getCversion()); + ByteArrayOutputStream tbaos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(tbaos); + hdr.serialize(boa, "hdr"); + txn.serialize(boa, "txn"); + tbaos.close(); + qp = new QuorumPacket(Leader.PROPOSAL, 1, tbaos.toByteArray(), null); + oa.writeRecord(qp, null); + + // setup the messages to be streamed to follower + sl.leaderIs = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray())); + + try { + sl.syncWithLeader(3); + } catch (EOFException e) { + } + + sl.zk.shutdown(); + sl = new SimpleLearner(ftsl); + assertEquals(startZxid, sl.zk.getLastProcessedZxid()); + } finally { + TestUtils.deleteFileRecursively(tmpFile); + } + } + + @Test + public void truncFailTest() throws Exception { + final boolean[] exitProcCalled = {false}; + + ServiceUtils.setSystemExitProcedure(new Consumer() { + @Override + public void accept(Integer exitCode) { + exitProcCalled[0] = true; + assertThat("System.exit() was called with invalid exit code", exitCode, equalTo(ExitCode.QUORUM_PACKET_ERROR.getValue())); + } + }); + + File tmpFile = File.createTempFile("test", ".dir", testData); + tmpFile.delete(); + try { + FileTxnSnapLog txnSnapLog = new FileTxnSnapLog(tmpFile, tmpFile); + SimpleLearner sl = new SimpleLearner(txnSnapLog); + long startZxid = sl.zk.getLastProcessedZxid(); + + // Set up bogus streams + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive oa = BinaryOutputArchive.getArchive(baos); + sl.leaderOs = BinaryOutputArchive.getArchive(new ByteArrayOutputStream()); + + // make streams and socket do something innocuous + sl.bufferedOutput = new BufferedOutputStream(System.out); + sl.sock = new Socket(); + + // fake messages from the server + QuorumPacket qp = new QuorumPacket(Leader.TRUNC, 0, null, null); + oa.writeRecord(qp, null); + + // setup the messages to be streamed to follower + sl.leaderIs = BinaryInputArchive.getArchive(new ByteArrayInputStream(baos.toByteArray())); + + try { + sl.syncWithLeader(3); + } catch (EOFException e) { + } + + sl.zk.shutdown(); + + assertThat("System.exit() should have been called", exitProcCalled[0], is(true)); + } finally { + TestUtils.deleteFileRecursively(tmpFile); + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java new file mode 100644 index 0000000..c15fd5b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/LocalPeerBeanTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.Test; + +public class LocalPeerBeanTest { + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2299 + */ + @Test + public void testClientAddress() throws Exception { + QuorumPeer quorumPeer = new QuorumPeer(); + LocalPeerBean remotePeerBean = new LocalPeerBean(quorumPeer); + + /** + * Case 1: When cnxnFactory is null + */ + String result = remotePeerBean.getClientAddress(); + assertNotNull(result); + assertEquals(0, result.length()); + + /** + * Case 2: When only client port is configured + */ + ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); + int clientPort = PortAssignment.unique(); + InetSocketAddress address = new InetSocketAddress(clientPort); + cnxnFactory.configure(address, 5, -1, false); + quorumPeer.setCnxnFactory(cnxnFactory); + + result = remotePeerBean.getClientAddress(); + String ipv4 = "0.0.0.0:" + clientPort; + String ipv6 = "[0:0:0:0:0:0:0:0]:" + clientPort; + assertTrue(result.equals(ipv4) || result.equals(ipv6)); + // cleanup + cnxnFactory.shutdown(); + + /** + * Case 3: When both client port and client address is configured + */ + clientPort = PortAssignment.unique(); + InetAddress clientIP = InetAddress.getLoopbackAddress(); + address = new InetSocketAddress(clientIP, clientPort); + cnxnFactory = ServerCnxnFactory.createFactory(); + cnxnFactory.configure(address, 5, -1, false); + quorumPeer.setCnxnFactory(cnxnFactory); + + result = remotePeerBean.getClientAddress(); + String expectedResult = clientIP.getHostAddress() + ":" + clientPort; + assertEquals(expectedResult, result); + // cleanup + cnxnFactory.shutdown(); + } + + @Test + public void testLocalPeerIsLeader() throws Exception { + long localPeerId = 7; + QuorumPeer peer = mock(QuorumPeer.class); + when(peer.getId()).thenReturn(localPeerId); + when(peer.isLeader(eq(localPeerId))).thenReturn(true); + LocalPeerBean localPeerBean = new LocalPeerBean(peer); + assertTrue(localPeerBean.isLeader()); + } + + @Test + public void testLocalPeerIsNotLeader() throws Exception { + long localPeerId = 7; + QuorumPeer peer = mock(QuorumPeer.class); + when(peer.getId()).thenReturn(localPeerId); + when(peer.isLeader(eq(localPeerId))).thenReturn(false); + LocalPeerBean localPeerBean = new LocalPeerBean(peer); + assertFalse(localPeerBean.isLeader()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/MultipleAddressesTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/MultipleAddressesTest.java new file mode 100644 index 0000000..e060fa8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/MultipleAddressesTest.java @@ -0,0 +1,257 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NoRouteToHostException; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.commons.collections.CollectionUtils; +import org.apache.zookeeper.PortAssignment; +import org.junit.Assert; +import org.junit.Test; + +public class MultipleAddressesTest { + + public static final int PORTS_AMOUNT = 10; + + @Test + public void testIsEmpty() { + MultipleAddresses multipleAddresses = new MultipleAddresses(); + Assert.assertTrue(multipleAddresses.isEmpty()); + + multipleAddresses.addAddress(new InetSocketAddress(22)); + Assert.assertFalse(multipleAddresses.isEmpty()); + } + + @Test + public void testGetAllAddresses() { + List addresses = getAddressList(); + MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); + + Assert.assertTrue(CollectionUtils.isEqualCollection(addresses, multipleAddresses.getAllAddresses())); + + multipleAddresses.addAddress(addresses.get(1)); + Assert.assertTrue(CollectionUtils.isEqualCollection(addresses, multipleAddresses.getAllAddresses())); + } + + @Test + public void testGetAllHostStrings() { + List addresses = getAddressList(); + List hostStrings = getHostStrings(addresses); + MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); + + Assert.assertTrue(CollectionUtils.isEqualCollection(hostStrings, multipleAddresses.getAllHostStrings())); + + multipleAddresses.addAddress(addresses.get(addresses.size() - 1)); + Assert.assertTrue(CollectionUtils.isEqualCollection(hostStrings, multipleAddresses.getAllHostStrings())); + } + + @Test + public void testGetAllPorts() { + List ports = getPortList(); + MultipleAddresses multipleAddresses = new MultipleAddresses(getAddressList(ports)); + + Assert.assertTrue(CollectionUtils.isEqualCollection(ports, multipleAddresses.getAllPorts())); + + multipleAddresses.addAddress(new InetSocketAddress("localhost", ports.get(ports.size() - 1))); + Assert.assertTrue(CollectionUtils.isEqualCollection(ports, multipleAddresses.getAllPorts())); + } + + @Test + public void testGetWildcardAddresses() { + List ports = getPortList(); + List addresses = getAddressList(ports); + MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); + List allAddresses = ports.stream().map(InetSocketAddress::new).collect(Collectors.toList()); + + Assert.assertTrue(CollectionUtils.isEqualCollection(allAddresses, multipleAddresses.getWildcardAddresses())); + + multipleAddresses.addAddress(new InetSocketAddress("localhost", ports.get(ports.size() - 1))); + Assert.assertTrue(CollectionUtils.isEqualCollection(allAddresses, multipleAddresses.getWildcardAddresses())); + } + + @Test + public void testGetValidAddress() throws NoRouteToHostException { + List addresses = getAddressList(); + MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); + + Assert.assertTrue(addresses.contains(multipleAddresses.getReachableAddress())); + } + + @Test(expected = NoRouteToHostException.class) + public void testGetValidAddressWithNotValid() throws NoRouteToHostException { + // IP chosen because it is reserved for documentation/examples and should be unreachable (RFC 5737) + MultipleAddresses multipleAddresses = new MultipleAddresses(new InetSocketAddress("203.0.113.1", 22)); + multipleAddresses.getReachableAddress(); + } + + @Test + public void testGetReachableOrOneWithSingleReachableAddress() { + InetSocketAddress reachableAddress = new InetSocketAddress("127.0.0.1", PortAssignment.unique()); + + MultipleAddresses multipleAddresses = new MultipleAddresses(Collections.singletonList(reachableAddress)); + InetSocketAddress actualReturnedAddress = multipleAddresses.getReachableOrOne(); + + Assert.assertEquals(reachableAddress, actualReturnedAddress); + } + + @Test + public void testGetReachableOrOneWithSingleUnreachableAddress() { + InetSocketAddress unreachableAddress = new InetSocketAddress("unreachable.address.zookeeper.apache.com", 1234); + + MultipleAddresses multipleAddresses = new MultipleAddresses(Collections.singletonList(unreachableAddress)); + InetSocketAddress actualReturnedAddress = multipleAddresses.getReachableOrOne(); + + Assert.assertEquals(unreachableAddress, actualReturnedAddress); + } + + @Test + public void testRecreateSocketAddresses() throws UnknownHostException { + List searchedAddresses = Arrays.stream(InetAddress.getAllByName("google.com")) + .map(addr -> new InetSocketAddress(addr, 222)).collect(Collectors.toList()); + + MultipleAddresses multipleAddresses = new MultipleAddresses(searchedAddresses.get(searchedAddresses.size() - 1)); + List addresses = new ArrayList<>(multipleAddresses.getAllAddresses()); + + Assert.assertEquals(1, addresses.size()); + Assert.assertEquals(searchedAddresses.get(searchedAddresses.size() - 1), addresses.get(0)); + + multipleAddresses.recreateSocketAddresses(); + + addresses = new ArrayList<>(multipleAddresses.getAllAddresses()); + Assert.assertEquals(1, addresses.size()); + Assert.assertEquals(searchedAddresses.get(0), addresses.get(0)); + } + + @Test + public void testRecreateSocketAddressesWithWrongAddresses() { + InetSocketAddress address = new InetSocketAddress("locahost", 222); + MultipleAddresses multipleAddresses = new MultipleAddresses(address); + multipleAddresses.recreateSocketAddresses(); + + Assert.assertEquals(address, multipleAddresses.getOne()); + } + + @Test + public void testAlwaysGetReachableAddress() throws Exception{ + InetSocketAddress reachableHost = new InetSocketAddress("127.0.0.1", 1234); + InetSocketAddress unreachableHost1 = new InetSocketAddress("unreachable1.address.zookeeper.apache.com", 1234); + InetSocketAddress unreachableHost2 = new InetSocketAddress("unreachable2.address.zookeeper.apache.com", 1234); + InetSocketAddress unreachableHost3 = new InetSocketAddress("unreachable3.address.zookeeper.apache.com", 1234); + + MultipleAddresses multipleAddresses = new MultipleAddresses( + Arrays.asList(unreachableHost1, unreachableHost2, unreachableHost3, reachableHost)); + + // we call the getReachableAddress() function multiple times, to make sure we + // always got back a reachable address and not just a random one + for (int i = 0; i < 10; i++) { + Assert.assertEquals(reachableHost, multipleAddresses.getReachableAddress()); + } + } + + @Test + public void testGetAllReachableAddresses() throws Exception { + InetSocketAddress reachableHost1 = new InetSocketAddress("127.0.0.1", 1234); + InetSocketAddress reachableHost2 = new InetSocketAddress("127.0.0.1", 2345); + InetSocketAddress unreachableHost1 = new InetSocketAddress("unreachable1.address.zookeeper.apache.com", 1234); + InetSocketAddress unreachableHost2 = new InetSocketAddress("unreachable2.address.zookeeper.apache.com", 1234); + + MultipleAddresses multipleAddresses = new MultipleAddresses( + Arrays.asList(unreachableHost1, unreachableHost2, reachableHost1, reachableHost2)); + + Set reachableHosts = new HashSet<>(Arrays.asList(reachableHost1, reachableHost2)); + Assert.assertEquals(reachableHosts, multipleAddresses.getAllReachableAddresses()); + } + + @Test + public void testGetAllReachableAddressesOrAllWhenSomeReachable() throws Exception { + InetSocketAddress reachableHost1 = new InetSocketAddress("127.0.0.1", 1234); + InetSocketAddress reachableHost2 = new InetSocketAddress("127.0.0.1", 2345); + InetSocketAddress unreachableHost1 = new InetSocketAddress("unreachable1.address.zookeeper.apache.com", 1234); + InetSocketAddress unreachableHost2 = new InetSocketAddress("unreachable2.address.zookeeper.apache.com", 1234); + + MultipleAddresses multipleAddresses = new MultipleAddresses( + Arrays.asList(unreachableHost1, unreachableHost2, reachableHost1, reachableHost2)); + + Set reachableHosts = new HashSet<>(Arrays.asList(reachableHost1, reachableHost2)); + Assert.assertEquals(reachableHosts, multipleAddresses.getAllReachableAddressesOrAll()); + } + + @Test + public void testGetAllReachableAddressesOrAllWhenNoneReachable() throws Exception { + InetSocketAddress unreachableHost1 = new InetSocketAddress("unreachable1.address.zookeeper.apache.com", 1234); + InetSocketAddress unreachableHost2 = new InetSocketAddress("unreachable2.address.zookeeper.apache.com", 1234); + InetSocketAddress unreachableHost3 = new InetSocketAddress("unreachable3.address.zookeeper.apache.com", 1234); + List allUnreachableAddresses = Arrays.asList(unreachableHost1, unreachableHost2, unreachableHost3); + + MultipleAddresses multipleAddresses = new MultipleAddresses(allUnreachableAddresses); + + Assert.assertEquals(new HashSet<>(allUnreachableAddresses), multipleAddresses.getAllReachableAddressesOrAll()); + } + + @Test + public void testEquals() { + List addresses = getAddressList(); + + MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); + MultipleAddresses multipleAddressesEquals = new MultipleAddresses(addresses); + + Assert.assertEquals(multipleAddresses, multipleAddressesEquals); + + MultipleAddresses multipleAddressesNotEquals = new MultipleAddresses(getAddressList()); + + Assert.assertNotEquals(multipleAddresses, multipleAddressesNotEquals); + } + + @Test + public void testSize() { + List addresses = getAddressList(); + MultipleAddresses multipleAddresses = new MultipleAddresses(addresses); + + Assert.assertEquals(PORTS_AMOUNT, multipleAddresses.size()); + } + + public List getPortList() { + return IntStream.range(0, PORTS_AMOUNT).mapToObj(i -> PortAssignment.unique()).collect(Collectors.toList()); + } + + public List getAddressList() { + return getAddressList(getPortList()); + } + + public List getAddressList(List ports) { + return IntStream.range(0, ports.size()) + .mapToObj(i -> new InetSocketAddress("127.0.0." + i, ports.get(i))).collect(Collectors.toList()); + } + + private List getHostStrings(List addresses) { + return IntStream.range(0, addresses.size()) + .mapToObj(i -> "127.0.0." + i).collect(Collectors.toList()); + } + +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumBeanTest.java new file mode 100644 index 0000000..0f9a8ac --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumBeanTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.junit.Test; + +public class QuorumBeanTest { + + @Test + public void testGetNameProperty() { + QuorumPeer qpMock = mock(QuorumPeer.class); + when(qpMock.getId()).thenReturn(1L); + QuorumBean qb = new QuorumBean(qpMock); + + assertThat("getName property should return Bean name in the right format", qb.getName(), equalTo("ReplicatedServer_id1")); + } + + @Test + public void testIsHiddenProperty() { + QuorumPeer qpMock = mock(QuorumPeer.class); + QuorumBean qb = new QuorumBean(qpMock); + assertThat("isHidden should return false", qb.isHidden(), equalTo(false)); + } + + @Test + public void testGetQuorumSizeProperty() { + QuorumPeer qpMock = mock(QuorumPeer.class); + QuorumBean qb = new QuorumBean(qpMock); + + when(qpMock.getQuorumSize()).thenReturn(5); + assertThat("getQuorumSize property should return value of peet.getQuorumSize()", qb.getQuorumSize(), equalTo(5)); + } + + @Test + public void testSslQuorumProperty() { + QuorumPeer qpMock = mock(QuorumPeer.class); + QuorumBean qb = new QuorumBean(qpMock); + + when(qpMock.isSslQuorum()).thenReturn(true); + assertThat("isSslQuorum property should return value of peer.isSslQuorum()", qb.isSslQuorum(), equalTo(true)); + when(qpMock.isSslQuorum()).thenReturn(false); + assertThat("isSslQuorum property should return value of peer.isSslQuorum()", qb.isSslQuorum(), equalTo(false)); + } + + @Test + public void testPortUnificationProperty() { + QuorumPeer qpMock = mock(QuorumPeer.class); + QuorumBean qb = new QuorumBean(qpMock); + + when(qpMock.shouldUsePortUnification()).thenReturn(true); + assertThat("isPortUnification property should return value of peer.shouldUsePortUnification()", qb.isPortUnification(), equalTo(true)); + when(qpMock.shouldUsePortUnification()).thenReturn(false); + assertThat("isPortUnification property should return value of peer.shouldUsePortUnification()", qb.isPortUnification(), equalTo(false)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumCnxManagerSocketConnectionTimeoutTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumCnxManagerSocketConnectionTimeoutTest.java new file mode 100644 index 0000000..ab1c077 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumCnxManagerSocketConnectionTimeoutTest.java @@ -0,0 +1,112 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.SocketAddress; +import java.net.SocketTimeoutException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumCnxManagerSocketConnectionTimeoutTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumCnxManagerSocketConnectionTimeoutTest.class); + private QuorumUtil qu; + + @Before + public void setUp() throws Exception { + // starting a 3 node ensemble without observers + qu = new QuorumUtil(1, 2); + qu.startAll(); + } + + /** + * Testing an error case reported in ZOOKEEPER-3756: + * + * When a new leader election happens after a ZooKeeper server restarted, in Kubernetes + * the rest of the servers can not initiate connection to the restarted one. But they + * get SocketTimeoutException instead of immediate IOException. The Leader Election was + * time-outing quicker than the socket.connect call, so we ended up with cycles of broken + * leader elections. + * + * The fix was to make the connection initiation asynchronous, so one 'broken' connection + * doesn't make the whole leader election to be blocked, even in case of SocketTimeoutException. + * + * @throws Exception + */ + @Test + public void testSocketConnectionTimeoutDuringConnectingToElectionAddress() throws Exception { + + int leaderId = qu.getLeaderServer(); + + // use a custom socket factory that will cause timeout instead of connecting to the + // leader election port of the current leader + final InetSocketAddress leaderElectionAddress = + qu.getLeaderQuorumPeer().getElectionAddress().getOne(); + QuorumCnxManager.setSocketFactory(() -> new SocketStub(leaderElectionAddress)); + + qu.shutdown(leaderId); + + assertTrue("Timeout during waiting for current leader to go down", + ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(leaderId).clientPort, + ClientBase.CONNECTION_TIMEOUT)); + + String errorMessage = "No new leader was elected"; + waitFor(errorMessage, () -> qu.leaderExists() && qu.getLeaderServer() != leaderId, 15); + } + + final class SocketStub extends Socket { + + private final InetSocketAddress addressToTimeout; + + SocketStub(InetSocketAddress addressToTimeout) { + this.addressToTimeout = addressToTimeout; + } + + @Override + public void connect(SocketAddress endpoint, int timeout) throws IOException { + if (addressToTimeout.equals(endpoint)) { + try { + Thread.sleep(timeout); + } catch (InterruptedException e) { + LOG.warn("interrupted SocketStub.connect", e); + } + throw new SocketTimeoutException("timeout reached in SocketStub.connect()"); + } + + super.connect(endpoint, timeout); + } + } + + @After + public void tearDown() throws Exception { + qu.shutdownAll(); + QuorumCnxManager.setSocketFactory(QuorumCnxManager.DEFAULT_SOCKET_FACTORY); + } + +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java new file mode 100644 index 0000000..691b455 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumDigestTest.java @@ -0,0 +1,263 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import mockit.Invocation; +import mockit.Mock; +import mockit.MockUp; +import org.apache.jute.Record; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.DataTree.ProcessTxnResult; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.TxnLogDigestTest; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.metric.SimpleCounter; +import org.apache.zookeeper.txn.TxnDigest; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumDigestTest extends QuorumPeerTestBase { + + private static final Logger LOG = + LoggerFactory.getLogger(QuorumDigestTest.class); + + private Servers servers; + private String forceSnapSyncValue; + + @BeforeClass + public static void applyMockUps() { + new DataTreeMock(); + } + + @Before + public void setup() throws Exception { + forceSnapSyncValue = System.getProperty(LearnerHandler.FORCE_SNAP_SYNC); + ZooKeeperServer.setDigestEnabled(true); + ((SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT).reset(); + servers = LaunchServers(3, 1, null); + } + + @After + public void tearDown() throws Exception { + if (servers != null) { + servers.shutDownAllServers(); + } + ZooKeeperServer.setDigestEnabled(false); + System.clearProperty(LearnerHandler.FORCE_SNAP_SYNC); + DataTreeMock.reset(); + } + + /** + * Check positive case without digest mismatch during diff sync. + */ + @Test + public void testDigestMatchesDuringDiffSync() throws Exception { + triggerSync(false); + } + + /** + * Check positive case without digest mismatch during snap sync. + */ + @Test + public void testDigestMatchesDuringSnapSync() throws Exception { + triggerSync(true); + + // have some extra txns + int leader = servers.findLeader(); + TxnLogDigestTest.performOperations(servers.zk[leader], + "/testDigestMatchesDuringSnapSync"); + Assert.assertEquals(0L, getMismatchDigestCount()); + } + + @Test + public void testDigestMatchesWithAsyncRequests() throws Exception { + + int leader = servers.findLeader(); + + final ZooKeeper client = servers.zk[leader]; + final AtomicBoolean stopped = new AtomicBoolean(true); + final String prefix = "/testDigestMatchesWithAsyncRequests"; + + // start a thread to send requests asynchronously, + Thread createTrafficThread = new Thread () { + @Override + public void run() { + int i = 0; + while (!stopped.get()) { + String path = prefix + "-" + i; + client.create(path, path.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.PERSISTENT, new StringCallback() { + @Override + public void processResult(int rc, String path, + Object ctx, String name) { + // ignore the result + } + }, null); + try { + Thread.sleep(10); + } catch (InterruptedException e) { /* ignore */ } + } + } + }; + createTrafficThread.start(); + + // shutdown a follower and observer + List targets = Arrays.asList( + servers.findAnyFollower(), servers.findAnyObserver()); + stopServers(targets); + + // start the follower and observer to have a diff sync + startServers(targets); + + // make sure there is no digest mismatch + Assert.assertEquals(0L, getMismatchDigestCount()); + + // stop the leader + targets = Arrays.asList(leader); + stopServers(targets); + startServers(targets); + + // make sure there is no digest mismatch + Assert.assertEquals(0L, getMismatchDigestCount()); + + stopped.set(true); + } + + /** + * Check negative case by injecting txn miss during syncing. + */ + @Test + public void testDigestMismatchesWhenTxnLost() throws Exception { + // make sure there is no mismatch after all servers start up + Assert.assertEquals(0L, getMismatchDigestCount()); + + // shutdown a follower and observer + List targets = Arrays.asList( + servers.findAnyFollower(), servers.findAnyObserver()); + stopServers(targets); + + int leader = servers.findLeader(); + triggerOps(leader, "/p1"); + + Assert.assertEquals(0L, getMismatchDigestCount()); + + DataTreeMock.skipTxnZxid = "100000006"; + + // start the follower and observer to have a diff sync + startServers(targets); + + long misMatchCount = getMismatchDigestCount(); + Assert.assertNotEquals(0L, misMatchCount); + + triggerOps(leader, "/p2"); + Assert.assertNotEquals(misMatchCount, getMismatchDigestCount()); + } + + private void stopServers(List sids) throws InterruptedException { + for (int sid : sids) { + if (sid != -1) { + servers.mt[sid].shutdown(); + waitForOne(servers.zk[sid], States.CONNECTING); + } + } + } + + private void startServers(List sids) throws InterruptedException { + for (int sid : sids) { + servers.mt[sid].start(); + waitForOne(servers.zk[sid], States.CONNECTED); + } + } + + private void triggerOps(int sid, String prefix) throws Exception { + TxnLogDigestTest.performOperations(servers.zk[sid], prefix); + servers.restartClient(sid, null); + waitForOne(servers.zk[sid], States.CONNECTED); + } + + private void triggerSync(boolean snapSync) throws Exception { + if (snapSync) { + System.setProperty(LearnerHandler.FORCE_SNAP_SYNC, "true"); + } + + // make sure there is no mismatch after all servers start up + Assert.assertEquals(0L, getMismatchDigestCount()); + + int leader = servers.findLeader(); + triggerOps(leader, "/p1"); + + Assert.assertEquals(0L, getMismatchDigestCount()); + + // shutdown a follower and observer + List targets = Arrays.asList( + servers.findAnyFollower(), servers.findAnyObserver()); + stopServers(targets); + + // do some extra writes + triggerOps(leader, "/p2"); + + // start the follower and observer to have a diff sync + startServers(targets); + + Assert.assertEquals(0L, getMismatchDigestCount()); + } + + public static long getMismatchDigestCount() { + return ((SimpleCounter) ServerMetrics.getMetrics().DIGEST_MISMATCHES_COUNT).get(); + } + + public static final class DataTreeMock extends MockUp { + + static String skipTxnZxid = ""; + + @Mock + public ProcessTxnResult processTxn(Invocation invocation, + TxnHeader header, Record txn, TxnDigest digest) { + if (header != null && Long.toHexString(header.getZxid()).equals(skipTxnZxid)) { + LOG.info("skip process txn {}", header.getZxid()); + ProcessTxnResult rc = new ProcessTxnResult(); + rc.path = ""; + rc.stat = new Stat(); + rc.multiResult = new ArrayList(); + return rc; + } + return invocation.proceed(header, txn, digest); + } + + public static void reset() { + skipTxnZxid = ""; + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java new file mode 100644 index 0000000..0d7e7cc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerConfigTest.java @@ -0,0 +1,173 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.Properties; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.junit.Test; + +public class QuorumPeerConfigTest { + + /** + * test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2264 + */ + @Test + public void testErrorMessageWhensecureClientPortNotSetButsecureClientPortAddressSet() throws IOException, ConfigException { + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + try { + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("secureClientPortAddress", "localhost"); + quorumPeerConfig.parseProperties(zkProp); + fail("IllegalArgumentException is expected"); + } catch (IllegalArgumentException e) { + String expectedMessage = "secureClientPortAddress is set but secureClientPort is not set"; + assertEquals(expectedMessage, e.getMessage()); + } + } + + /** + * + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2264 + */ + @Test + public void testErrorMessageWhenclientPortNotSetButclientPortAddressSet() throws IOException, ConfigException { + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + try { + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("clientPortAddress", "localhost"); + quorumPeerConfig.parseProperties(zkProp); + fail("IllegalArgumentException is expected"); + } catch (IllegalArgumentException e) { + String expectedMessage = "clientPortAddress is set but clientPort is not set"; + assertEquals(expectedMessage, e.getMessage()); + } + } + + /** + * https://issues.apache.org/jira/browse/ZOOKEEPER-2297 + */ + @Test + public void testConfigureSSLAuthGetsConfiguredIfSecurePortConfigured() throws IOException, ConfigException { + String sslAuthProp = "zookeeper.authProvider.x509"; + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("secureClientPort", "12345"); + quorumPeerConfig.parseProperties(zkProp); + String expected = "org.apache.zookeeper.server.auth.X509AuthenticationProvider"; + String result = System.getProperty(sslAuthProp); + assertEquals(expected, result); + } + + /** + * https://issues.apache.org/jira/browse/ZOOKEEPER-2297 + */ + @Test + public void testCustomSSLAuth() throws IOException { + try (ClientX509Util x509Util = new ClientX509Util()) { + System.setProperty(x509Util.getSslAuthProviderProperty(), "y509"); + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + try { + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("secureClientPort", "12345"); + quorumPeerConfig.parseProperties(zkProp); + fail("ConfigException is expected"); + } catch (ConfigException e) { + assertNotNull(e.getMessage()); + } + } + } + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2873 + */ + @Test(expected = ConfigException.class) + public void testSamePortConfiguredForClientAndElection() throws IOException, ConfigException { + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("server.1", "localhost:2888:2888"); + quorumPeerConfig.parseProperties(zkProp); + } + + /** + * Extend the existing QuorumPeerConfig to set the server id. + */ + public static class MockQuorumPeerConfig extends QuorumPeerConfig { + + public MockQuorumPeerConfig(long serverId) { + this.serverId = serverId; + } + + } + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2847 + */ + @Test + public void testClientAddrFromClientPort() throws IOException, ConfigException { + long serverId = 1; + QuorumPeerConfig quorumPeerConfig = new MockQuorumPeerConfig(serverId); + Properties zkProp = getDefaultZKProperties(); + int clientPort = 12345; + zkProp.setProperty("clientPort", Integer.toString(clientPort)); + zkProp.setProperty("server.1", "127.0.0.1:2889:3889:participant"); + quorumPeerConfig.parseProperties(zkProp); + + QuorumServer qs = quorumPeerConfig.getQuorumVerifier().getAllMembers().get(serverId); + InetSocketAddress expectedAddress = new InetSocketAddress("0.0.0.0", clientPort); + assertEquals(expectedAddress, quorumPeerConfig.getClientPortAddress()); + assertEquals(quorumPeerConfig.getClientPortAddress(), qs.clientAddr); + } + + @Test + public void testJvmPauseMonitorConfigured() throws IOException, ConfigException { + final Long sleepTime = 444L; + final Long warnTH = 5555L; + final Long infoTH = 555L; + + QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig(); + Properties zkProp = getDefaultZKProperties(); + zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath()); + zkProp.setProperty("jvm.pause.monitor", "true"); + zkProp.setProperty("jvm.pause.sleep.time.ms", sleepTime.toString()); + zkProp.setProperty("jvm.pause.warn-threshold.ms", warnTH.toString()); + zkProp.setProperty("jvm.pause.info-threshold.ms", infoTH.toString()); + quorumPeerConfig.parseProperties(zkProp); + + assertEquals(sleepTime, Long.valueOf(quorumPeerConfig.getJvmPauseSleepTimeMs())); + assertEquals(warnTH, Long.valueOf(quorumPeerConfig.getJvmPauseWarnThresholdMs())); + assertEquals(infoTH, Long.valueOf(quorumPeerConfig.getJvmPauseInfoThresholdMs())); + assertTrue(quorumPeerConfig.isJvmPauseMonitorToRun()); + } + + private Properties getDefaultZKProperties() { + Properties zkProp = new Properties(); + zkProp.setProperty("dataDir", new File("myDataDir").getAbsolutePath()); + return zkProp; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainMultiAddressTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainMultiAddressTest.java new file mode 100644 index 0000000..9c2906c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainMultiAddressTest.java @@ -0,0 +1,334 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class QuorumPeerMainMultiAddressTest extends QuorumPeerTestBase { + + private static final int FIRST_SERVER = 0; + private static final int SECOND_SERVER = 1; + private static final int THIRD_SERVER = 2; + private static final int FIRST_ADDRESS = 0; + private static final int SECOND_ADDRESS = 1; + private static final String UNREACHABLE_HOST = "invalid.hostname.unreachable.com"; + private static final String IPV6_LOCALHOST = "[0:0:0:0:0:0:0:1]"; + + // IPv4 by default, change to IPV6_LOCALHOST to test with servers binding to IPv6 + private String hostName = "127.0.0.1"; + + private int zNodeId = 0; + + @Before + public void setUp() throws Exception { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + ClientBase.setupTestEnv(); + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + QuorumPeerConfig.setReconfigEnabled(true); + + // just to get rid of the unrelated 'InstanceAlreadyExistsException' in the logs + System.setProperty("zookeeper.jmx.log4j.disable", "true"); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + System.clearProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED); + System.clearProperty("zookeeper.jmx.log4j.disable"); + } + + + @Test + public void shouldStartClusterWithMultipleAddresses() throws Exception { + // we have three ZK servers, each server has two quorumPort and two electionPort registered + QuorumServerConfigBuilder quorumConfig = new QuorumServerConfigBuilder(hostName, 3, 2); + + // we launch the three servers, each server having the same configuration + QuorumServerConfigBuilder builderForServer1 = new QuorumServerConfigBuilder(quorumConfig); + QuorumServerConfigBuilder builderForServer2 = new QuorumServerConfigBuilder(quorumConfig); + QuorumServerConfigBuilder builderForServer3 = new QuorumServerConfigBuilder(quorumConfig); + launchServers(Arrays.asList(builderForServer1, builderForServer2, builderForServer3)); + + checkIfZooKeeperQuorumWorks(quorumConfig); + } + + + @Test + public void shouldStartClusterWithMultipleAddresses_IPv6() throws Exception { + hostName = IPV6_LOCALHOST; + + shouldStartClusterWithMultipleAddresses(); + } + + + @Test + public void shouldStartClusterWhenSomeAddressesAreUnreachable() throws Exception { + // we have three ZK servers, each server has two quorumPort and two electionPort registered + // in the config we misconfigure one of the addresses for each servers + QuorumServerConfigBuilder quorumConfig = new QuorumServerConfigBuilder(hostName, 3, 2) + .changeHostName(FIRST_SERVER, SECOND_ADDRESS, UNREACHABLE_HOST) + .changeHostName(SECOND_SERVER, SECOND_ADDRESS, UNREACHABLE_HOST) + .changeHostName(THIRD_SERVER, SECOND_ADDRESS, UNREACHABLE_HOST); + + // we prepare the same initial config for all the three servers + QuorumServerConfigBuilder builderForServer1 = new QuorumServerConfigBuilder(quorumConfig); + QuorumServerConfigBuilder builderForServer2 = new QuorumServerConfigBuilder(quorumConfig); + QuorumServerConfigBuilder builderForServer3 = new QuorumServerConfigBuilder(quorumConfig); + + // we test here: + // - if the Leader can bind to the correct address and not die with BindException or + // SocketException for trying to bind to a wrong address / port + // - if the ZK server can 'select' the correct address to connect when trying to form a quorum + // with the other servers + launchServers(Arrays.asList(builderForServer1, builderForServer2, builderForServer3)); + + checkIfZooKeeperQuorumWorks(quorumConfig); + } + + + @Test + public void shouldStartClusterWhenSomeAddressesAreUnreachable_IPv6() throws Exception { + hostName = IPV6_LOCALHOST; + + shouldStartClusterWhenSomeAddressesAreUnreachable(); + } + + + @Test + public void shouldReconfigIncrementallyByAddingMoreAddresses() throws Exception { + // we have three ZK servers, each server has two quorumPort and two electionPort registered + QuorumServerConfigBuilder initialQuorumConfig = new QuorumServerConfigBuilder(hostName, 3, 2); + + // we launch the three servers, each server should use the same initial config + launchServers(Arrays.asList(initialQuorumConfig, initialQuorumConfig, initialQuorumConfig)); + + checkIfZooKeeperQuorumWorks(initialQuorumConfig); + + // we create a new config where we add a new address to each server with random available ports + QuorumServerConfigBuilder newQuorumConfig = new QuorumServerConfigBuilder(initialQuorumConfig) + .addNewServerAddress(FIRST_SERVER); + + + ZooKeeperAdmin zkAdmin = newZooKeeperAdmin(initialQuorumConfig); + + // initiating a new incremental reconfig, by using the updated ports + ReconfigTest.reconfig(zkAdmin, newQuorumConfig.buildAsStringList(), null, null, -1); + + checkIfZooKeeperQuorumWorks(newQuorumConfig); + } + + + @Test + public void shouldReconfigIncrementallyByDeletingSomeAddresses() throws Exception { + // we have three ZK servers, each server has three quorumPort and three electionPort registered + QuorumServerConfigBuilder initialQuorumConfig = new QuorumServerConfigBuilder(hostName, 3, 3); + + // we launch the three servers, each server should use the same initial config + launchServers(Arrays.asList(initialQuorumConfig, initialQuorumConfig, initialQuorumConfig)); + + checkIfZooKeeperQuorumWorks(initialQuorumConfig); + + // we create a new config where we delete a few address from each server + QuorumServerConfigBuilder newQuorumConfig = new QuorumServerConfigBuilder(initialQuorumConfig) + .deleteLastServerAddress(FIRST_SERVER) + .deleteLastServerAddress(SECOND_SERVER) + .deleteLastServerAddress(SECOND_SERVER) + .deleteLastServerAddress(THIRD_SERVER); + + ZooKeeperAdmin zkAdmin = newZooKeeperAdmin(initialQuorumConfig); + + // initiating a new incremental reconfig, by using the updated ports + ReconfigTest.reconfig(zkAdmin, newQuorumConfig.buildAsStringList(), null, null, -1); + + checkIfZooKeeperQuorumWorks(newQuorumConfig); + } + + @Test + public void shouldReconfigNonIncrementally() throws Exception { + // we have three ZK servers, each server has two quorumPort and two electionPort registered + QuorumServerConfigBuilder initialQuorumConfig = new QuorumServerConfigBuilder(hostName, 3, 2); + + // we launch the three servers, each server should use the same initial config + launchServers(Arrays.asList(initialQuorumConfig, initialQuorumConfig, initialQuorumConfig)); + + checkIfZooKeeperQuorumWorks(initialQuorumConfig); + + // we create a new config where we delete and add a few address for each server + QuorumServerConfigBuilder newQuorumConfig = new QuorumServerConfigBuilder(initialQuorumConfig) + .deleteLastServerAddress(FIRST_SERVER) + .deleteLastServerAddress(SECOND_SERVER) + .deleteLastServerAddress(SECOND_SERVER) + .deleteLastServerAddress(THIRD_SERVER) + .addNewServerAddress(SECOND_SERVER) + .addNewServerAddress(THIRD_SERVER); + + ZooKeeperAdmin zkAdmin = newZooKeeperAdmin(initialQuorumConfig); + + // initiating a new non-incremental reconfig, by using the updated ports + ReconfigTest.reconfig(zkAdmin, null, null, newQuorumConfig.buildAsStringList(), -1); + + checkIfZooKeeperQuorumWorks(newQuorumConfig); + } + + + @Test + public void shouldReconfigIncrementally_IPv6() throws Exception { + + hostName = IPV6_LOCALHOST; + + // we have three ZK servers, each server has two quorumPort and two electionPort registered + QuorumServerConfigBuilder initialQuorumConfig = new QuorumServerConfigBuilder(hostName, 3, 2); + + // we launch the three servers, each server should use the same initial config + launchServers(Arrays.asList(initialQuorumConfig, initialQuorumConfig, initialQuorumConfig)); + + checkIfZooKeeperQuorumWorks(initialQuorumConfig); + + // we create a new config where we delete and add a few address for each server + QuorumServerConfigBuilder newQuorumConfig = new QuorumServerConfigBuilder(initialQuorumConfig) + .deleteLastServerAddress(FIRST_SERVER) + .deleteLastServerAddress(SECOND_SERVER) + .deleteLastServerAddress(SECOND_SERVER) + .deleteLastServerAddress(THIRD_SERVER) + .addNewServerAddress(SECOND_SERVER) + .addNewServerAddress(THIRD_SERVER); + + ZooKeeperAdmin zkAdmin = newZooKeeperAdmin(initialQuorumConfig); + + // initiating a new incremental reconfig, by using the updated ports + ReconfigTest.reconfig(zkAdmin, newQuorumConfig.buildAsStringList(), null, null, -1); + + checkIfZooKeeperQuorumWorks(newQuorumConfig); + } + + @Test + public void shouldFailToReconfigWithMultipleAddressesWhenFeatureIsDisabled() throws Exception { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "false"); + + // we have three ZK servers, each server has a single quorumPort and single electionPort registered + QuorumServerConfigBuilder initialQuorumConfig = new QuorumServerConfigBuilder(hostName, 3, 1); + + // we launch the three servers, each server should use the same initial config + launchServers(Arrays.asList(initialQuorumConfig, initialQuorumConfig, initialQuorumConfig)); + + checkIfZooKeeperQuorumWorks(initialQuorumConfig); + + // we create a new config where we add a new address to one of the servers with random available ports + QuorumServerConfigBuilder newQuorumConfig = new QuorumServerConfigBuilder(initialQuorumConfig) + .addNewServerAddress(FIRST_SERVER); + + ZooKeeperAdmin zkAdmin = newZooKeeperAdmin(initialQuorumConfig); + + // initiating a new incremental reconfig by using the updated ports, expecting exceptions here + try { + ReconfigTest.reconfig(zkAdmin, newQuorumConfig.buildAsStringList(), null, null, -1); + fail("Reconfig succeeded with multiple addresses without exception when the MultiAddress feature is disabled"); + } catch (KeeperException.BadArgumentsException e) { + // do nothing, this is what we expected + } catch (Exception e) { + fail("Reconfig failed in a wrong way. We expected KeeperException.BadArgumentsException."); + } + } + + private void launchServers(List builders) throws IOException, InterruptedException { + + numServers = builders.size(); + + servers = new Servers(); + servers.clientPorts = new int[numServers]; + servers.mt = new MainThread[numServers]; + servers.zk = new ZooKeeper[numServers]; + + for (int i = 0; i < numServers; i++) { + QuorumServerConfigBuilder quorumServerConfigBuilder = builders.get(i); + String quorumCfgSection = quorumServerConfigBuilder.build(); + LOG.info(String.format("starting server %d with quorum config:\n%s", i, quorumCfgSection)); + servers.clientPorts[i] = quorumServerConfigBuilder.getClientPort(i); + servers.mt[i] = new MainThread(i, servers.clientPorts[i], quorumCfgSection); + servers.mt[i].start(); + servers.restartClient(i, this); + } + + waitForAll(servers, ZooKeeper.States.CONNECTED); + + for (int i = 0; i < numServers; i++) { + servers.zk[i].close(5000); + } + } + + private void checkIfZooKeeperQuorumWorks(QuorumServerConfigBuilder builder) throws IOException, + InterruptedException, KeeperException { + + LOG.info("starting to verify if Quorum works"); + zNodeId += 1; + String zNodePath = "/foo_" + zNodeId; + ZooKeeper zk = connectToZkServer(builder, FIRST_SERVER); + zk.create(zNodePath, "foobar1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData(zNodePath, null, null)), "foobar1"); + zk.close(1000); + + + zk = connectToZkServer(builder, SECOND_SERVER); + assertEquals(new String(zk.getData(zNodePath, null, null)), "foobar1"); + zk.close(1000); + + zk = connectToZkServer(builder, THIRD_SERVER); + assertEquals(new String(zk.getData(zNodePath, null, null)), "foobar1"); + zk.close(1000); + + LOG.info("Quorum verification finished successfully"); + + } + + private ZooKeeper connectToZkServer(QuorumServerConfigBuilder builder, int serverId) throws IOException, InterruptedException { + QuorumServerConfigBuilder.ServerAddress server = builder.getServerAddress(serverId, FIRST_ADDRESS); + int clientPort = builder.getClientPort(serverId); + ZooKeeper zk = new ZooKeeper(server.getHost() + ":" + clientPort, ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk, ZooKeeper.States.CONNECTED); + return zk; + } + + private ZooKeeperAdmin newZooKeeperAdmin( + QuorumServerConfigBuilder quorumConfig) throws IOException { + ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin( + hostName + ":" + quorumConfig.getClientPort(FIRST_SERVER), + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + return zkAdmin; + } + + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java new file mode 100644 index 0000000..b32734a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerMainTest.java @@ -0,0 +1,1911 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.apache.zookeeper.test.ClientBase.createEmptyTestDir; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.nio.file.Paths; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.regex.Pattern; +import javax.security.sasl.SaslException; +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.PatternLayout; +import org.apache.log4j.WriterAppender; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.metrics.BaseTestMetricsProvider; +import org.apache.zookeeper.metrics.impl.NullMetricsProvider; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +/** + * Test stand-alone server. + * + */ +public class QuorumPeerMainTest extends QuorumPeerTestBase { + + /** + * Verify the ability to start a cluster. + */ + public void testQuorumInternal(String addr) throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String server1 = String.format("server.1=%1$s:%2$s:%3$s;%4$s", addr, PortAssignment.unique(), PortAssignment.unique(), CLIENT_PORT_QP1); + String server2 = String.format("server.2=%1$s:%2$s:%3$s;%4$s", addr, PortAssignment.unique(), PortAssignment.unique(), CLIENT_PORT_QP2); + + String quorumCfgSection = server1 + "\n" + server2; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + q1.start(); + q2.start(); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp(addr + ":" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp(addr + ":" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + QuorumPeer quorumPeer = q1.main.quorumPeer; + + int tickTime = quorumPeer.getTickTime(); + assertEquals( + "Default value of minimumSessionTimeOut is not considered", + tickTime * 2, + quorumPeer.getMinSessionTimeout()); + assertEquals( + "Default value of maximumSessionTimeOut is not considered", + tickTime * 20, + quorumPeer.getMaxSessionTimeout()); + + ZooKeeper zk = new ZooKeeper(addr + ":" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk, States.CONNECTED); + zk.create("/foo_q1", "foobar1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo_q1", null, null)), "foobar1"); + zk.close(); + + zk = new ZooKeeper(addr + ":" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk, States.CONNECTED); + zk.create("/foo_q2", "foobar2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo_q2", null, null)), "foobar2"); + zk.close(); + + q1.shutdown(); + q2.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown(addr + ":" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown(addr + ":" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + } + + /** + * Verify the ability to start a cluster. + */ + @Test + public void testQuorum() throws Exception { + testQuorumInternal("127.0.0.1"); + } + + /** + * Verify the ability to start a cluster. IN V6!!!! + */ + @Test + public void testQuorumV6() throws Exception { + testQuorumInternal("[::1]"); + } + + /** + * Test early leader abandonment. + */ + @Test + public void testEarlyLeaderAbandonment() throws Exception { + ClientBase.setupTestEnv(); + final int SERVER_COUNT = 3; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + sb.append("server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + clientPorts[i] + "\n"); + } + String quorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + waitForAll(zk, States.CONNECTED); + + // we need to shutdown and start back up to make sure that the create session isn't the first transaction since + // that is rather innocuous. + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + + waitForAll(zk, States.CONNECTING); + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].start(); + // Recreate a client session since the previous session was not persisted. + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + waitForAll(zk, States.CONNECTED); + + // ok lets find the leader and kill everything else, we have a few + // seconds, so it should be plenty of time + int leader = -1; + Map outstanding = null; + for (int i = 0; i < SERVER_COUNT; i++) { + if (mt[i].main.quorumPeer.leader == null) { + mt[i].shutdown(); + } else { + leader = i; + outstanding = mt[leader].main.quorumPeer.leader.outstandingProposals; + } + } + + try { + zk[leader].create("/zk" + leader, "zk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("create /zk" + leader + " should have failed"); + } catch (KeeperException e) { + } + + // just make sure that we actually did get it in process at the + // leader + assertTrue(outstanding.size() == 1); + assertTrue(outstanding.values().iterator().next().request.getHdr().getType() == OpCode.create); + // make sure it has a chance to write it to disk + Thread.sleep(1000); + mt[leader].shutdown(); + waitForAll(zk, States.CONNECTING); + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != leader) { + mt[i].start(); + } + } + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != leader) { + // Recreate a client session since the previous session was not persisted. + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk[i], States.CONNECTED); + zk[i].create("/zk" + i, "zk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } + + mt[leader].start(); + waitForAll(zk, States.CONNECTED); + // make sure everything is consistent + for (int i = 0; i < SERVER_COUNT; i++) { + for (int j = 0; j < SERVER_COUNT; j++) { + if (i == leader) { + assertTrue((j == leader ? ("Leader (" + leader + ")") : ("Follower " + j)) + + " should not have /zk" + + i, zk[j].exists("/zk" + i, false) == null); + } else { + assertTrue((j == leader ? ("Leader (" + leader + ")") : ("Follower " + j)) + + " does not have /zk" + + i, zk[j].exists("/zk" + i, false) != null); + } + } + } + for (int i = 0; i < SERVER_COUNT; i++) { + zk[i].close(); + } + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + + /** + * Test the case of server with highest zxid not present at leader election and joining later. + * This test case is for reproducing the issue and fixing the bug mentioned in ZOOKEEPER-1154 + * and ZOOKEEPER-1156. + */ + @Test + public void testHighestZxidJoinLate() throws Exception { + numServers = 3; + servers = LaunchServers(numServers); + String path = "/hzxidtest"; + int leader = servers.findLeader(); + + // make sure there is a leader + assertTrue("There should be a leader", leader >= 0); + + int nonleader = (leader + 1) % numServers; + + byte[] input = new byte[1]; + input[0] = 1; + byte[] output; + + // Create a couple of nodes + servers.zk[leader].create(path + leader, input, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + servers.zk[leader].create(path + nonleader, input, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // make sure the updates indeed committed. If it is not + // the following statement will throw. + output = servers.zk[leader].getData(path + nonleader, false, null); + + // Shutdown every one else but the leader + for (int i = 0; i < numServers; i++) { + if (i != leader) { + servers.mt[i].shutdown(); + } + } + + input[0] = 2; + + // Update the node on the leader + servers.zk[leader].setData(path + leader, input, -1, null, null); + + // wait some time to let this get written to disk + Thread.sleep(500); + + // shut the leader down + servers.mt[leader].shutdown(); + + System.gc(); + + waitForAll(servers.zk, States.CONNECTING); + + // Start everyone but the leader + for (int i = 0; i < numServers; i++) { + if (i != leader) { + servers.mt[i].start(); + } + } + + // wait to connect to one of these + waitForOne(servers.zk[nonleader], States.CONNECTED); + + // validate that the old value is there and not the new one + output = servers.zk[nonleader].getData(path + leader, false, null); + + assertEquals("Expecting old value 1 since 2 isn't committed yet", output[0], 1); + + // Do some other update, so we bump the maxCommttedZxid + // by setting the value to 2 + servers.zk[nonleader].setData(path + nonleader, input, -1); + + // start the old leader + servers.mt[leader].start(); + + // connect to it + waitForOne(servers.zk[leader], States.CONNECTED); + + // make sure it doesn't have the new value that it alone had logged + output = servers.zk[leader].getData(path + leader, false, null); + assertEquals("Validating that the deposed leader has rolled back that change it had written", output[0], 1); + + // make sure the leader has the subsequent changes that were made while it was offline + output = servers.zk[leader].getData(path + nonleader, false, null); + assertEquals("Validating that the deposed leader caught up on changes it missed", output[0], 2); + } + + /** + * This test validates that if a quorum member determines that it is leader without the support of the rest of the + * quorum (the other members do not believe it to be the leader) it will stop attempting to lead and become a follower. + * + * @throws IOException + * @throws InterruptedException + */ + @Test + public void testElectionFraud() throws IOException, InterruptedException { + // capture QuorumPeer logging + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.INFO); + Logger qlogger = Logger.getLogger(QuorumPeer.class); + qlogger.addAppender(appender); + + numServers = 3; + + // used for assertions later + boolean foundLeading = false; + boolean foundLooking = false; + boolean foundFollowing = false; + + try { + // spin up a quorum, we use a small ticktime to make the test run faster + servers = LaunchServers(numServers, 500); + + // find the leader + int trueLeader = servers.findLeader(); + assertTrue("There should be a leader", trueLeader >= 0); + + // find a follower + int falseLeader = (trueLeader + 1) % numServers; + assertTrue("All servers should join the quorum", servers.mt[falseLeader].main.quorumPeer.follower + != null); + + // to keep the quorum peer running and force it to go into the looking state, we kill leader election + servers.mt[falseLeader].main.quorumPeer.electionAlg.shutdown(); + servers.mt[falseLeader].main.quorumPeer.follower.getSocket().close(); + + // wait for the falseLeader to disconnect + waitForOne(servers.zk[falseLeader], States.CONNECTING); + + // convince falseLeader that it is the leader + servers.mt[falseLeader].main.quorumPeer.setPeerState(QuorumPeer.ServerState.LEADING); + + // provide time for the falseleader to realize no followers have connected + // (this is twice the timeout used in Leader#getEpochToPropose) + Thread.sleep(2 * servers.mt[falseLeader].main.quorumPeer.initLimit * servers.mt[falseLeader].main.quorumPeer.tickTime); + + // Restart leader election + servers.mt[falseLeader].main.quorumPeer.startLeaderElection(); + + // The previous client connection to falseLeader likely closed, create a new one + servers.zk[falseLeader] = new ZooKeeper( + "127.0.0.1:" + servers.mt[falseLeader].getClientPort(), + ClientBase.CONNECTION_TIMEOUT, + this); + + // Wait for falseLeader to rejoin the quorum + waitForOne(servers.zk[falseLeader], States.CONNECTED); + + // and ensure trueLeader is still the leader + assertTrue(servers.mt[trueLeader].main.quorumPeer.leader != null); + + // Look through the logs for output that indicates the falseLeader is LEADING, then LOOKING, then FOLLOWING + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + Pattern leading = Pattern.compile(".*myid=" + falseLeader + ".*LEADING.*"); + Pattern looking = Pattern.compile(".*myid=" + falseLeader + ".*LOOKING.*"); + Pattern following = Pattern.compile(".*myid=" + falseLeader + ".*FOLLOWING.*"); + + String line; + while ((line = r.readLine()) != null) { + if (!foundLeading) { + foundLeading = leading.matcher(line).matches(); + } else if (!foundLooking) { + foundLooking = looking.matcher(line).matches(); + } else if (following.matcher(line).matches()) { + foundFollowing = true; + break; + } + } + } finally { + qlogger.removeAppender(appender); + } + + assertTrue("falseLeader never attempts to become leader", foundLeading); + assertTrue("falseLeader never gives up on leadership", foundLooking); + assertTrue("falseLeader never rejoins the quorum", foundFollowing); + } + + /** + * Verify handling of bad quorum address + */ + @Test + public void testBadPeerAddressInQuorum() throws Exception { + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\nserver.2=fee.fii.foo.fum:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + + boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000); + + assertFalse("Server never came up", isup); + + q1.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = Pattern.compile(".*Cannot open channel to .* at election address .*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + assertTrue("complains about host", found); + } + + /** + * Verify handling of inconsistent peer type + */ + @Test + public void testInconsistentPeerType() throws Exception { + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.INFO); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + // test the most likely situation only: server is stated as observer in + // servers list, but there's no "peerType=observer" token in config + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + final int CLIENT_PORT_QP3 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2 + + "\nserver.3=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":observer" + ";" + CLIENT_PORT_QP3; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + MainThread q3 = new MainThread(3, CLIENT_PORT_QP3, quorumCfgSection); + q1.start(); + q2.start(); + q3.start(); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP3, CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 3 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP3, ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean warningPresent = false; + boolean defaultedToObserver = false; + Pattern pWarn = Pattern.compile(".*Peer type from servers list.* doesn't match peerType.*"); + Pattern pObserve = Pattern.compile(".*OBSERVING.*"); + while ((line = r.readLine()) != null) { + if (pWarn.matcher(line).matches()) { + warningPresent = true; + } + if (pObserve.matcher(line).matches()) { + defaultedToObserver = true; + } + if (warningPresent && defaultedToObserver) { + break; + } + } + assertTrue("Should warn about inconsistent peer type", warningPresent && defaultedToObserver); + } + + /** + * verify if bad packets are being handled properly + * at the quorum port + * @throws Exception + */ + @Test + public void testBadPackets() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + int electionPort1 = PortAssignment.unique(); + int electionPort2 = PortAssignment.unique(); + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort1 + ";" + CLIENT_PORT_QP1 + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort2 + ";" + CLIENT_PORT_QP2; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + q1.start(); + q2.start(); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + + byte[] b = new byte[4]; + int length = 1024 * 1024 * 1024; + ByteBuffer buff = ByteBuffer.wrap(b); + buff.putInt(length); + buff.position(0); + SocketChannel s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort1)); + s.write(buff); + s.close(); + buff.position(0); + s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort2)); + s.write(buff); + s.close(); + + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk, States.CONNECTED); + zk.create("/foo_q1", "foobar1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/foo_q1", null, null)), "foobar1"); + zk.close(); + q1.shutdown(); + q2.shutdown(); + } + + /** + * Verify handling of quorum defaults + * * default electionAlg is fast leader election + */ + @Test + public void testQuorumDefaults() throws Exception { + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.INFO); + appender.setImmediateFlush(true); + Logger zlogger = Logger.getLogger("org.apache.zookeeper"); + zlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection); + q1.start(); + q2.start(); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + + } finally { + zlogger.removeAppender(appender); + } + os.close(); + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = Pattern.compile(".*FastLeaderElection.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + assertTrue("fastleaderelection used", found); + } + + /** + * Verifies that QuorumPeer exits immediately + */ + @Test + public void testQuorumPeerExitTime() throws Exception { + long maxwait = 3000; + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + PortAssignment.unique(); + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + // Let the notifications timeout + Thread.sleep(30000); + long start = Time.currentElapsedTime(); + q1.shutdown(); + long end = Time.currentElapsedTime(); + if ((end - start) > maxwait) { + fail("QuorumPeer took " + (end - start) + " to shutdown, expected " + maxwait); + } + } + + /** + * Test verifies that the server is able to redefine the min/max session + * timeouts + */ + @Test + public void testMinMaxSessionTimeOut() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique(); + + final int minSessionTimeOut = 10000; + final int maxSessionTimeOut = 15000; + final String configs = "maxSessionTimeout=" + maxSessionTimeOut + "\n" + + "minSessionTimeout=" + minSessionTimeOut + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection, configs); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, configs); + q1.start(); + q2.start(); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + + QuorumPeer quorumPeer = q1.main.quorumPeer; + + assertEquals("minimumSessionTimeOut is not considered", minSessionTimeOut, quorumPeer.getMinSessionTimeout()); + assertEquals("maximumSessionTimeOut is not considered", maxSessionTimeOut, quorumPeer.getMaxSessionTimeout()); + } + + /** + * Test verifies that the server is able to redefine if user configured only + * minSessionTimeout limit + */ + @Test + public void testWithOnlyMinSessionTimeout() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique(); + + final int minSessionTimeOut = 15000; + final String configs = "minSessionTimeout=" + minSessionTimeOut + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection, configs); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, configs); + q1.start(); + q2.start(); + + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + + QuorumPeer quorumPeer = q1.main.quorumPeer; + final int maxSessionTimeOut = quorumPeer.tickTime * 20; + + assertEquals("minimumSessionTimeOut is not considered", minSessionTimeOut, quorumPeer.getMinSessionTimeout()); + assertEquals("maximumSessionTimeOut is wrong", maxSessionTimeOut, quorumPeer.getMaxSessionTimeout()); + } + + @Test + public void testFailedTxnAsPartOfQuorumLoss() throws Exception { + final int LEADER_TIMEOUT_MS = 10_000; + // 1. start up server and wait for leader election to finish + ClientBase.setupTestEnv(); + final int SERVER_COUNT = 3; + servers = LaunchServers(SERVER_COUNT); + + waitForAll(servers, States.CONNECTED); + + // we need to shutdown and start back up to make sure that the create session isn't the first transaction since + // that is rather innocuous. + servers.shutDownAllServers(); + waitForAll(servers, States.CONNECTING); + servers.restartAllServersAndClients(this); + waitForAll(servers, States.CONNECTED); + + // 2. kill all followers + int leader = servers.findLeader(); + Map outstanding = servers.mt[leader].main.quorumPeer.leader.outstandingProposals; + // increase the tick time to delay the leader going to looking + int previousTick = servers.mt[leader].main.quorumPeer.tickTime; + servers.mt[leader].main.quorumPeer.tickTime = LEADER_TIMEOUT_MS; + // let the previous tick on the leader exhaust itself so the new tick time takes effect + Thread.sleep(previousTick); + LOG.warn("LEADER {}", leader); + + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != leader) { + servers.mt[i].shutdown(); + } + } + + // 3. start up the followers to form a new quorum + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != leader) { + servers.mt[i].start(); + } + } + + // 4. wait one of the follower to be the new leader + for (int i = 0; i < SERVER_COUNT; i++) { + if (i != leader) { + // Recreate a client session since the previous session was not persisted. + servers.restartClient(i, this); + waitForOne(servers.zk[i], States.CONNECTED); + } + } + + // 5. send a create request to old leader and make sure it's synced to disk, + // which means it acked from itself + try { + servers.zk[leader].create("/zk" + leader, "zk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("create /zk" + leader + " should have failed"); + } catch (KeeperException e) { + } + + // just make sure that we actually did get it in process at the + // leader + // there can be extra sessionClose proposals + assertTrue(outstanding.size() > 0); + Proposal p = findProposalOfType(outstanding, OpCode.create); + LOG.info("Old leader id: {}. All proposals: {}", leader, outstanding); + assertNotNull("Old leader doesn't have 'create' proposal", p); + + // make sure it has a chance to write it to disk + int sleepTime = 0; + Long longLeader = (long) leader; + while (!p.qvAcksetPairs.get(0).getAckset().contains(longLeader)) { + if (sleepTime > 2000) { + fail("Transaction not synced to disk within 1 second " + p.qvAcksetPairs.get(0).getAckset() + " expected " + leader); + } + Thread.sleep(100); + sleepTime += 100; + } + + // 6. wait for the leader to quit due to not enough followers and come back up as a part of the new quorum + LOG.info("Waiting for leader {} to timeout followers", leader); + sleepTime = 0; + Follower f = servers.mt[leader].main.quorumPeer.follower; + while (f == null || !f.isRunning()) { + if (sleepTime > LEADER_TIMEOUT_MS * 2) { + fail("Took too long for old leader to time out " + + servers.mt[leader].main.quorumPeer.getPeerState()); + } + Thread.sleep(100); + sleepTime += 100; + f = servers.mt[leader].main.quorumPeer.follower; + } + + int newLeader = servers.findLeader(); + // make sure a different leader was elected + assertNotEquals(leader, newLeader); + + // 7. restart the previous leader to force it to replay the edits and possibly come up in a bad state + servers.mt[leader].shutdown(); + servers.mt[leader].start(); + // old client session can expire, restart it + servers.restartClient(leader, this); + waitForAll(servers, States.CONNECTED); + + // 8. check the node exist in previous leader but not others + // make sure everything is consistent + for (int i = 0; i < SERVER_COUNT; i++) { + assertNull( + "server " + i + " should not have /zk" + leader, + servers.zk[i].exists("/zk" + leader, false)); + } + } + + /** + * Verify that a node without the leader in its view will not attempt to connect to the leader. + */ + @Test + public void testLeaderOutOfView() throws Exception { + ClientBase.setupTestEnv(); + + int numServers = 3; + + // used for assertions later + boolean foundLeading = false; + boolean foundFollowing = false; + + // capture QuorumPeer logging + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.DEBUG); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + Servers svrs = new Servers(); + svrs.clientPorts = new int[numServers]; + for (int i = 0; i < numServers; i++) { + svrs.clientPorts[i] = PortAssignment.unique(); + } + + String quorumCfgIncomplete = getUniquePortCfgForId(1) + "\n" + getUniquePortCfgForId(2); + String quorumCfgComplete = quorumCfgIncomplete + "\n" + getUniquePortCfgForId(3); + svrs.mt = new MainThread[3]; + + // Node 1 is started without the leader (3) in its config view + svrs.mt[0] = new MainThread(1, svrs.clientPorts[0], quorumCfgIncomplete); + for (int i = 1; i < numServers; i++) { + svrs.mt[i] = new MainThread(i + 1, svrs.clientPorts[i], quorumCfgComplete); + } + + // Node 1 must be started first, before quorum is formed, to trigger the attempted invalid connection to 3 + svrs.mt[0].start(); + QuorumPeer quorumPeer1 = waitForQuorumPeer(svrs.mt[0], CONNECTION_TIMEOUT); + assertTrue(quorumPeer1.getPeerState() == QuorumPeer.ServerState.LOOKING); + + // Node 3 started second to avoid 1 and 2 forming a quorum before 3 starts up + int highestServerIndex = numServers - 1; + svrs.mt[highestServerIndex].start(); + QuorumPeer quorumPeer3 = waitForQuorumPeer(svrs.mt[highestServerIndex], CONNECTION_TIMEOUT); + assertTrue(quorumPeer3.getPeerState() == QuorumPeer.ServerState.LOOKING); + + // Node 2 started last, kicks off leader election + for (int i = 1; i < highestServerIndex; i++) { + svrs.mt[i].start(); + } + + // Nodes 2 and 3 now form quorum and fully start. 1 attempts to vote for 3, fails, returns to LOOKING state + for (int i = 1; i < numServers; i++) { + assertTrue( + "waiting for server to start", + ClientBase.waitForServerUp("127.0.0.1:" + svrs.clientPorts[i], CONNECTION_TIMEOUT)); + } + // Expecting that only 3 node will be leader is wrong, even 2 node can be leader, when cluster is formed with 1 node + boolean firstAndSecondNodeFormedCluster = false; + if (QuorumPeer.ServerState.LEADING == svrs.mt[1].getQuorumPeer().getPeerState()) { + assertEquals(QuorumPeer.ServerState.FOLLOWING, + svrs.mt[0].getQuorumPeer().getPeerState()); + assertEquals(QuorumPeer.ServerState.FOLLOWING, + svrs.mt[highestServerIndex].getQuorumPeer().getPeerState()); + firstAndSecondNodeFormedCluster = true; + } else { + // Verify leader out of view scenario + assertEquals(QuorumPeer.ServerState.LOOKING, + svrs.mt[0].getQuorumPeer().getPeerState()); + assertEquals(QuorumPeer.ServerState.LEADING, + svrs.mt[highestServerIndex].getQuorumPeer().getPeerState()); + } + + for (int i = 1; i < highestServerIndex; i++) { + assertTrue( + svrs.mt[i].getQuorumPeer().getPeerState() == QuorumPeer.ServerState.FOLLOWING + || svrs.mt[i].getQuorumPeer().getPeerState() + == QuorumPeer.ServerState.LEADING); + } + + // Look through the logs for output that indicates Node 1 is LEADING or FOLLOWING + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + Pattern leading = Pattern.compile(".*myid=1.*QuorumPeer.*LEADING.*"); + Pattern following = Pattern.compile(".*myid=1.*QuorumPeer.*FOLLOWING.*"); + + String line; + while ((line = r.readLine()) != null && !foundLeading && !foundFollowing) { + foundLeading = leading.matcher(line).matches(); + foundFollowing = following.matcher(line).matches(); + } + + if (firstAndSecondNodeFormedCluster) { + assertTrue( + "Corrupt peer should join quorum with servers having same server configuration", + foundFollowing); + } else { + assertFalse("Corrupt peer should never become leader", foundLeading); + assertFalse("Corrupt peer should not attempt connection to out of view leader", + foundFollowing); + } + } finally { + qlogger.removeAppender(appender); + } + + } + + @Test + public void testDataDirAndDataLogDir() throws Exception { + File dataDir = createEmptyTestDir(); + File dataLogDir = createEmptyTestDir(); + + // Arrange + try { + QuorumPeerConfig configMock = mock(QuorumPeerConfig.class); + when(configMock.getDataDir()).thenReturn(dataDir); + when(configMock.getDataLogDir()).thenReturn(dataLogDir); + when(configMock.getMetricsProviderClassName()).thenReturn(NullMetricsProvider.class.getName()); + + QuorumPeer qpMock = mock(QuorumPeer.class); + + doCallRealMethod().when(qpMock).setTxnFactory(any(FileTxnSnapLog.class)); + when(qpMock.getTxnFactory()).thenCallRealMethod(); + InjectableQuorumPeerMain qpMain = new InjectableQuorumPeerMain(qpMock); + + // Act + qpMain.runFromConfig(configMock); + + // Assert + FileTxnSnapLog txnFactory = qpMain.getQuorumPeer().getTxnFactory(); + assertEquals(Paths.get(dataLogDir.getAbsolutePath(), "version-2").toString(), txnFactory.getDataDir().getAbsolutePath()); + assertEquals(Paths.get(dataDir.getAbsolutePath(), "version-2").toString(), txnFactory.getSnapDir().getAbsolutePath()); + } finally { + FileUtils.deleteDirectory(dataDir); + FileUtils.deleteDirectory(dataLogDir); + } + } + + private class InjectableQuorumPeerMain extends QuorumPeerMain { + + QuorumPeer qp; + + InjectableQuorumPeerMain(QuorumPeer qp) { + this.qp = qp; + } + + @Override + protected QuorumPeer getQuorumPeer() { + return qp; + } + + } + + private WriterAppender getConsoleAppender(ByteArrayOutputStream os, Level level) { + String loggingPattern = ((PatternLayout) Logger.getRootLogger().getAppender("CONSOLE").getLayout()).getConversionPattern(); + WriterAppender appender = new WriterAppender(new PatternLayout(loggingPattern), os); + appender.setThreshold(level); + return appender; + } + + private String getUniquePortCfgForId(int id) { + return String.format("server.%d=127.0.0.1:%d:%d", id, PortAssignment.unique(), PortAssignment.unique()); + } + + private QuorumPeer waitForQuorumPeer(MainThread mainThread, int timeout) throws TimeoutException { + long start = Time.currentElapsedTime(); + while (true) { + QuorumPeer quorumPeer = mainThread.isAlive() ? mainThread.getQuorumPeer() : null; + if (quorumPeer != null) { + return quorumPeer; + } + + if (Time.currentElapsedTime() > start + timeout) { + LOG.error("Timed out while waiting for QuorumPeer"); + throw new TimeoutException(); + } + + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } + } + } + + private Proposal findProposalOfType(Map proposals, int type) { + for (Proposal proposal : proposals.values()) { + if (proposal.request.getHdr().getType() == type) { + return proposal; + } + } + return null; + } + + /** + * Currently, in SNAP sync, the leader will start queuing the + * proposal/commits and the NEWLEADER packet before sending + * over the snapshot over wire. So it's possible that the zxid + * associated with the snapshot might be higher than all the + * packets queued before NEWLEADER. + * + * When the follower received the snapshot, it will apply all + * the txns queued before NEWLEADER, which may not cover all + * the txns up to the zxid in the snapshot. After that, it + * will write the snapshot out to disk with the zxid associated + * with the snapshot. In case the server crashed after writing + * this out, when loading the data from disk, it will use zxid + * of the snapshot file to sync with leader, and it could cause + * data inconsistent, because we only replayed partial of the + * historical data during previous syncing. + * + * This test case is going to cover and simulate this scenario + * and make sure there is no data inconsistency issue after fix. + */ + @Test + public void testInconsistentDueToNewLeaderOrder() throws Exception { + + // 1. set up an ensemble with 3 servers + final int ENSEMBLE_SERVERS = 3; + final int[] clientPorts = new int[ENSEMBLE_SERVERS]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + + // start servers + MainThread[] mt = new MainThread[ENSEMBLE_SERVERS]; + ZooKeeper[] zk = new ZooKeeper[ENSEMBLE_SERVERS]; + Context[] contexts = new Context[ENSEMBLE_SERVERS]; + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + final Context context = new Context(); + contexts[i] = context; + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new CustomizedQPMain(context); + } + }; + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + waitForAll(zk, States.CONNECTED); + LOG.info("all servers started"); + + String nodePath = "/testInconsistentDueToNewLeader"; + + int leaderId = -1; + int followerA = -1; + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + if (mt[i].main.quorumPeer.leader != null) { + leaderId = i; + } else if (followerA == -1) { + followerA = i; + } + } + LOG.info("shutdown follower {}", followerA); + mt[followerA].shutdown(); + waitForOne(zk[followerA], States.CONNECTING); + + try { + // 2. set force snapshot to be true + LOG.info("force snapshot sync"); + System.setProperty(LearnerHandler.FORCE_SNAP_SYNC, "true"); + + // 3. create a node + String initialValue = "1"; + final ZooKeeper leaderZk = zk[leaderId]; + leaderZk.create(nodePath, initialValue.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("created node {} with value {}", nodePath, initialValue); + + CustomQuorumPeer leaderQuorumPeer = (CustomQuorumPeer) mt[leaderId].main.quorumPeer; + + // 4. on the customized leader catch the startForwarding call + // (without synchronized), set the node to value v1, then + // call the super.startForwarding to generate the ongoing + // txn proposal and commit for v1 value update + leaderQuorumPeer.setStartForwardingListener(new StartForwardingListener() { + @Override + public void start() { + if (!Boolean.getBoolean(LearnerHandler.FORCE_SNAP_SYNC)) { + return; + } + final String value = "2"; + LOG.info("start forwarding, set {} to {}", nodePath, value); + // use async, otherwise it will block the logLock in + // ZKDatabase and the setData request will timeout + try { + leaderZk.setData(nodePath, value.getBytes(), -1, new AsyncCallback.StatCallback() { + public void processResult(int rc, String path, Object ctx, Stat stat) { + } + }, null); + // wait for the setData txn being populated + Thread.sleep(1000); + } catch (Exception e) { + LOG.error("error when set {} to {}", nodePath, value, e); + } + } + }); + + // 5. on the customized leader catch the beginSnapshot call in + // LearnerSyncThrottler to set the node to value v2, + // wait it hit data tree + leaderQuorumPeer.setBeginSnapshotListener(new BeginSnapshotListener() { + @Override + public void start() { + String value = "3"; + LOG.info("before sending snapshot, set {} to {}", nodePath, value); + try { + leaderZk.setData(nodePath, value.getBytes(), -1); + LOG.info("successfully set {} to {}", nodePath, value); + } catch (Exception e) { + LOG.error("error when set {} to {}, {}", nodePath, value, e); + } + } + }); + + // 6. exit follower A after taking snapshot + CustomQuorumPeer followerAQuorumPeer = ((CustomQuorumPeer) mt[followerA].main.quorumPeer); + LOG.info("set exit when ack new leader packet on {}", followerA); + contexts[followerA].exitWhenAckNewLeader = true; + CountDownLatch latch = new CountDownLatch(1); + final MainThread followerAMT = mt[followerA]; + contexts[followerA].newLeaderAckCallback = new NewLeaderAckCallback() { + @Override + public void start() { + try { + latch.countDown(); + followerAMT.shutdown(); + } catch (Exception e) { + } + } + }; + + // 7. start follower A to do snapshot sync + LOG.info("starting follower {}", followerA); + mt[followerA].start(); + assertTrue(latch.await(30, TimeUnit.SECONDS)); + + // 8. now we have invalid data on disk, let's load it and verify + LOG.info("disable exit when ack new leader packet on {}", followerA); + System.setProperty(LearnerHandler.FORCE_SNAP_SYNC, "false"); + contexts[followerA].exitWhenAckNewLeader = true; + contexts[followerA].newLeaderAckCallback = null; + + LOG.info("restarting follower {}", followerA); + mt[followerA].start(); + zk[followerA].close(); + + zk[followerA] = new ZooKeeper("127.0.0.1:" + clientPorts[followerA], ClientBase.CONNECTION_TIMEOUT, this); + + // 9. start follower A, after it's in broadcast state, make sure + // the node value is same as what we have on leader + waitForOne(zk[followerA], States.CONNECTED); + assertEquals( + new String(zk[followerA].getData(nodePath, null, null)), + new String(zk[leaderId].getData(nodePath, null, null))); + } finally { + System.clearProperty(LearnerHandler.FORCE_SNAP_SYNC); + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + mt[i].shutdown(); + zk[i].close(); + } + } + } + + /** + * Test leader election finished with 1 disloyal voter and without + * majority followers, expecting to see the quorum stablized only + * after waiting for maxTimeToWaitForEpoch. + */ + @Test + public void testLeaderElectionWithDisloyalVoter() throws IOException { + testLeaderElection(5, 3, 1000, 10000); + } + + /** + * Test leader election finished with 1 disloyal voter and majority + * followers, expecting to see the quorum stablized immediately even + * there is 1 disloyal voter. + * + * Set the maxTimeToWaitForEpoch to 3s and maxTimeWaitForServerUp to + * 2s to confirm this. + */ + @Test + public void testLeaderElectionWithDisloyalVoter_stillHasMajority() throws IOException { + testLeaderElection(5, 5, 3000, 20000); + } + + void testLeaderElection(int totalServers, int serversToStart, int maxTimeToWaitForEpoch, int maxTimeWaitForServerUp) throws IOException { + Leader.setMaxTimeToWaitForEpoch(maxTimeToWaitForEpoch); + + // set up config for an ensemble with given number of servers + servers = new Servers(); + int ENSEMBLE_SERVERS = totalServers; + final int[] clientPorts = new int[ENSEMBLE_SERVERS]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + + // start servers + int SERVERS_TO_START = serversToStart; + MainThread[] mt = new MainThread[SERVERS_TO_START]; + Context[] contexts = new Context[SERVERS_TO_START]; + servers.mt = mt; + numServers = SERVERS_TO_START; + for (int i = 0; i < SERVERS_TO_START; i++) { + // hook the 1st follower to quit following after leader election + // simulate the behavior of changing voting during looking + final Context context = new Context(); + if (i == 0) { + context.quitFollowing = true; + } + contexts[i] = context; + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new CustomizedQPMain(context); + } + }; + mt[i].start(); + } + + // make sure the quorum can be formed within initLimit * tickTime + // the default setting is 10 * 4000 = 40000 ms + for (int i = 0; i < SERVERS_TO_START; i++) { + assertTrue( + "Server " + i + " should have joined quorum by now", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], maxTimeWaitForServerUp)); + } + } + + /** + * Verify boot works configuring a MetricsProvider + */ + @Test + public void testMetricsProviderLifecycle() throws Exception { + ClientBase.setupTestEnv(); + BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.reset(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSectionServer = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2 + "\n"; + + // server 1 boots with a MetricsProvider + String quorumCfgSectionServer1 = quorumCfgSectionServer + + "metricsProvider.className=" + BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.class.getName() + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSectionServer1); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSectionServer); + q1.start(); + q2.start(); + + boolean isup1 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000); + boolean isup2 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, 30000); + assertTrue("Server 1 never came up", isup1); + assertTrue("Server 2 never came up", isup2); + + q1.shutdown(); + q2.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + } finally { + qlogger.removeAppender(appender); + } + + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.configureCalled.get()); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.startCalled.get()); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.getRootContextCalled.get()); + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.stopCalled.get()); + } + + /** + * Test verifies that configuration is passed to the MetricsProvider. + */ + @Test + public void testMetricsProviderConfiguration() throws Exception { + ClientBase.setupTestEnv(); + BaseTestMetricsProvider.MetricsProviderWithConfiguration.httpPort.set(0); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSectionServer = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2 + "\n"; + + // server 1 boots with a MetricsProvider + String quorumCfgSectionServer1 = quorumCfgSectionServer + + "metricsProvider.className=" + + BaseTestMetricsProvider.MetricsProviderWithConfiguration.class.getName() + + "\n" + "metricsProvider.httpPort=1234"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSectionServer1); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSectionServer); + q1.start(); + q2.start(); + + boolean isup1 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000); + boolean isup2 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, 30000); + assertTrue("Server 1 never came up", isup1); + assertTrue("Server 2 never came up", isup2); + + q1.shutdown(); + q2.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + } finally { + qlogger.removeAppender(appender); + } + + assertEquals(1234, BaseTestMetricsProvider.MetricsProviderWithConfiguration.httpPort.get()); + } + + /** + * Test verifies that the server shouldn't be affected but runtime errors on stop() + */ + @Test + public void testFaultyMetricsProviderOnStop() throws Exception { + ClientBase.setupTestEnv(); + BaseTestMetricsProvider.MetricsProviderCapturingLifecycle.reset(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + final int CLIENT_PORT_QP2 = PortAssignment.unique(); + + String quorumCfgSectionServer = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + "\n" + + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP2 + "\n"; + + // server 1 boots with a MetricsProvider + String quorumCfgSectionServer1 = quorumCfgSectionServer + + "metricsProvider.className=" + + BaseTestMetricsProvider.MetricsProviderWithErrorInStop.class.getName() + + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSectionServer1); + MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSectionServer); + q1.start(); + q2.start(); + + boolean isup1 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 30000); + boolean isup2 = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, 30000); + assertTrue("Server 1 never came up", isup1); + assertTrue("Server 2 never came up", isup2); + + q1.shutdown(); + q2.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + assertTrue( + "waiting for server 2 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + } finally { + qlogger.removeAppender(appender); + } + + assertTrue("metrics provider lifecycle error", BaseTestMetricsProvider.MetricsProviderWithErrorInStop.stopCalled.get()); + + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = Pattern.compile(".*Error while stopping metrics.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + assertTrue("complains about metrics provider", found); + } + + /** + * Verify boot fails with a bad MetricsProvider + */ + @Test + public void testInvalidMetricsProvider() throws Exception { + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\n" + "metricsProvider.className=BadClass\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + + boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 5000); + + assertFalse("Server never came up", isup); + + q1.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = Pattern.compile(".*BadClass.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + assertTrue("complains about metrics provider", found); + } + + /** + * Verify boot fails with a MetricsProvider with fails to start + */ + @Test + public void testFaultyMetricsProviderOnStart() throws Exception { + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\n" + "metricsProvider.className=" + BaseTestMetricsProvider.MetricsProviderWithErrorInStart.class.getName() + + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + + boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 5000); + + assertFalse("Server never came up", isup); + + q1.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = Pattern.compile(".*MetricsProviderLifeCycleException.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + assertTrue("complains about metrics provider MetricsProviderLifeCycleException", found); + } + + /** + * Verify boot fails with a MetricsProvider with fails to start + */ + @Test + public void testFaultyMetricsProviderOnConfigure() throws Exception { + ClientBase.setupTestEnv(); + + // setup the logger to capture all logs + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = getConsoleAppender(os, Level.WARN); + Logger qlogger = Logger.getLogger("org.apache.zookeeper.server.quorum"); + qlogger.addAppender(appender); + + try { + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\n" + "server.2=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ";" + CLIENT_PORT_QP1 + + "\n" + "metricsProvider.className=" + BaseTestMetricsProvider.MetricsProviderWithErrorInConfigure.class.getName() + + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + + boolean isup = ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, 5000); + + assertFalse("Server never came up", isup); + + q1.shutdown(); + + assertTrue( + "waiting for server 1 down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + + } finally { + qlogger.removeAppender(appender); + } + + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + boolean found = false; + Pattern p = Pattern.compile(".*MetricsProviderLifeCycleException.*"); + while ((line = r.readLine()) != null) { + found = p.matcher(line).matches(); + if (found) { + break; + } + } + assertTrue("complains about metrics provider MetricsProviderLifeCycleException", found); + } + + /** + * If learner failed to do SNAP sync with leader before it's writing + * the snapshot to disk, it's possible that it might have DIFF sync + * with new leader or itself being elected as a leader. + * + * This test is trying to guarantee there is no data inconsistency for + * this case. + */ + @Test + public void testDiffSyncAfterSnap() throws Exception { + final int ENSEMBLE_SERVERS = 3; + MainThread[] mt = new MainThread[ENSEMBLE_SERVERS]; + ZooKeeper[] zk = new ZooKeeper[ENSEMBLE_SERVERS]; + + try { + // 1. start a quorum + final int[] clientPorts = new int[ENSEMBLE_SERVERS]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + + // start servers + Context[] contexts = new Context[ENSEMBLE_SERVERS]; + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + final Context context = new Context(); + contexts[i] = context; + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new CustomizedQPMain(context); + } + }; + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + waitForAll(zk, States.CONNECTED); + LOG.info("all servers started"); + + final String nodePath = "/testDiffSyncAfterSnap"; + + // 2. find leader and a follower + int leaderId = -1; + int followerA = -1; + for (int i = ENSEMBLE_SERVERS - 1; i >= 0; i--) { + if (mt[i].main.quorumPeer.leader != null) { + leaderId = i; + } else if (followerA == -1) { + followerA = i; + } + } + + // 3. stop follower A + LOG.info("shutdown follower {}", followerA); + mt[followerA].shutdown(); + waitForOne(zk[followerA], States.CONNECTING); + + // 4. issue some traffic + int index = 0; + int numOfRequests = 10; + for (int i = 0; i < numOfRequests; i++) { + zk[leaderId].create(nodePath + index++, + new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + CustomQuorumPeer leaderQuorumPeer = (CustomQuorumPeer) mt[leaderId].main.quorumPeer; + + // 5. inject fault to cause the follower exit when received NEWLEADER + contexts[followerA].newLeaderReceivedCallback = new NewLeaderReceivedCallback() { + boolean processed = false; + @Override + public void process() throws IOException { + if (processed) { + return; + } + processed = true; + System.setProperty(LearnerHandler.FORCE_SNAP_SYNC, "false"); + throw new IOException("read timedout"); + } + }; + + // 6. force snap sync once + LOG.info("force snapshot sync"); + System.setProperty(LearnerHandler.FORCE_SNAP_SYNC, "true"); + + // 7. start follower A + mt[followerA].start(); + waitForOne(zk[followerA], States.CONNECTED); + LOG.info("verify the nodes are exist in memory"); + for (int i = 0; i < index; i++) { + assertNotNull(zk[followerA].exists(nodePath + i, false)); + } + + // 8. issue another request which will be persisted on disk + zk[leaderId].create(nodePath + index++, + new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // wait some time to let this get written to disk + Thread.sleep(500); + + // 9. reload data from disk and make sure it's still consistent + LOG.info("restarting follower {}", followerA); + mt[followerA].shutdown(); + waitForOne(zk[followerA], States.CONNECTING); + mt[followerA].start(); + waitForOne(zk[followerA], States.CONNECTED); + + for (int i = 0; i < index; i++) { + assertNotNull("node " + i + " should exist", + zk[followerA].exists(nodePath + i, false)); + } + + } finally { + System.clearProperty(LearnerHandler.FORCE_SNAP_SYNC); + for (int i = 0; i < ENSEMBLE_SERVERS; i++) { + mt[i].shutdown(); + zk[i].close(); + } + } + } + + static class Context { + + boolean quitFollowing = false; + boolean exitWhenAckNewLeader = false; + NewLeaderAckCallback newLeaderAckCallback = null; + NewLeaderReceivedCallback newLeaderReceivedCallback = null; + + } + + interface NewLeaderAckCallback { + + void start(); + + } + + interface NewLeaderReceivedCallback { + void process() throws IOException; + } + + interface StartForwardingListener { + + void start(); + + } + + interface BeginSnapshotListener { + + void start(); + + } + + static class CustomizedQPMain extends TestQPMain { + + private Context context; + + public CustomizedQPMain(Context context) { + this.context = context; + } + + @Override + protected QuorumPeer getQuorumPeer() throws SaslException { + return new CustomQuorumPeer(context); + } + + } + + static class CustomQuorumPeer extends QuorumPeer { + + private Context context; + + private LearnerSyncThrottler throttler = null; + private StartForwardingListener startForwardingListener; + private BeginSnapshotListener beginSnapshotListener; + + public CustomQuorumPeer(Context context) throws SaslException { + this.context = context; + } + + public void setStartForwardingListener( + StartForwardingListener startForwardingListener) { + this.startForwardingListener = startForwardingListener; + } + + public void setBeginSnapshotListener( + BeginSnapshotListener beginSnapshotListener) { + this.beginSnapshotListener = beginSnapshotListener; + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb())) { + @Override + void followLeader() throws InterruptedException { + if (context.quitFollowing) { + // reset the flag + context.quitFollowing = false; + LOG.info("Quit following"); + return; + } else { + super.followLeader(); + } + } + + @Override + void writePacket(QuorumPacket pp, boolean flush) throws IOException { + if (pp != null && pp.getType() == Leader.ACK && context.exitWhenAckNewLeader) { + if (context.newLeaderAckCallback != null) { + context.newLeaderAckCallback.start(); + } + } + super.writePacket(pp, flush); + } + + @Override + void readPacket(QuorumPacket qp) throws IOException { + super.readPacket(qp); + if (qp.getType() == Leader.NEWLEADER && context.newLeaderReceivedCallback != null) { + context.newLeaderReceivedCallback.process(); + } + } + }; + } + + @Override + protected Leader makeLeader(FileTxnSnapLog logFactory) throws IOException, X509Exception { + return new Leader(this, new LeaderZooKeeperServer(logFactory, this, this.getZkDb())) { + @Override + public long startForwarding(LearnerHandler handler, long lastSeenZxid) { + if (startForwardingListener != null) { + startForwardingListener.start(); + } + return super.startForwarding(handler, lastSeenZxid); + } + + @Override + public LearnerSyncThrottler getLearnerSnapSyncThrottler() { + if (throttler == null) { + throttler = new LearnerSyncThrottler(getMaxConcurrentSnapSyncs(), LearnerSyncThrottler.SyncType.SNAP) { + @Override + public void beginSync(boolean essential) throws SyncThrottleException, InterruptedException { + if (beginSnapshotListener != null) { + beginSnapshotListener.start(); + } + super.beginSync(essential); + } + }; + } + return throttler; + } + }; + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTest.java new file mode 100644 index 0000000..03cb6f0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTest.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.test.ClientBase; +import org.junit.Test; + +public class QuorumPeerTest { + + private int electionAlg = 3; + private int tickTime = 2000; + private int initLimit = 3; + private int syncLimit = 3; + private int connectToLearnerMasterLimit = 3; + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2301 + */ + @Test + public void testQuorumPeerListendOnSpecifiedClientIP() throws IOException { + long myId = 1; + File dataDir = ClientBase.createTmpDir(); + int clientPort = PortAssignment.unique(); + Map peersView = new HashMap(); + InetAddress clientIP = InetAddress.getLoopbackAddress(); + + peersView.put(Long.valueOf(myId), new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT)); + + /** + * QuorumPeer constructor without QuorumVerifier + */ + QuorumPeer peer1 = new QuorumPeer(peersView, dataDir, dataDir, clientPort, electionAlg, myId, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + String hostString1 = peer1.cnxnFactory.getLocalAddress().getHostString(); + assertEquals(clientIP.getHostAddress(), hostString1); + + // cleanup + peer1.shutdown(); + + /** + * QuorumPeer constructor with QuorumVerifier + */ + peersView.clear(); + clientPort = PortAssignment.unique(); + peersView.put(Long.valueOf(myId), new QuorumServer(myId, new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, PortAssignment.unique()), new InetSocketAddress(clientIP, clientPort), LearnerType.PARTICIPANT)); + QuorumPeer peer2 = new QuorumPeer(peersView, dataDir, dataDir, clientPort, electionAlg, myId, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + String hostString2 = peer2.cnxnFactory.getLocalAddress().getHostString(); + assertEquals(clientIP.getHostAddress(), hostString2); + // cleanup + peer2.shutdown(); + } + + @Test + public void testLocalPeerIsLeader() throws Exception { + long localPeerId = 7; + QuorumPeer peer = new QuorumPeer(); + peer.setId(localPeerId); + Vote voteLocalPeerIsLeader = new Vote(localPeerId, 0); + peer.setCurrentVote(voteLocalPeerIsLeader); + assertTrue(peer.isLeader(localPeerId)); + } + + @Test + public void testLocalPeerIsNotLeader() throws Exception { + long localPeerId = 7; + long otherPeerId = 17; + QuorumPeer peer = new QuorumPeer(); + peer.setId(localPeerId); + Vote voteLocalPeerIsNotLeader = new Vote(otherPeerId, 0); + peer.setCurrentVote(voteLocalPeerIsNotLeader); + assertFalse(peer.isLeader(localPeerId)); + } + + @Test + public void testIsNotLeaderBecauseNoVote() throws Exception { + long localPeerId = 7; + QuorumPeer peer = new QuorumPeer(); + peer.setId(localPeerId); + peer.setCurrentVote(null); + assertFalse(peer.isLeader(localPeerId)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java new file mode 100644 index 0000000..aa75218 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumPeerTestBase.java @@ -0,0 +1,564 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Properties; +import java.util.Set; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.PathUtils; +import org.apache.zookeeper.server.admin.JettyAdminServer; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumBase; +import org.junit.After; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Has some common functionality for tests that work with QuorumPeers. Override + * process(WatchedEvent) to implement the Watcher interface + */ +public class QuorumPeerTestBase extends ZKTestCase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(QuorumPeerTestBase.class); + + public static final int TIMEOUT = 5000; + + protected Servers servers; + protected int numServers = 0; + + @After + public void tearDown() throws Exception { + if (servers == null || servers.mt == null) { + LOG.info("No servers to shutdown!"); + return; + } + for (int i = 0; i < numServers; i++) { + if (i < servers.mt.length) { + servers.mt[i].shutdown(); + } + } + } + + public void process(WatchedEvent event) { + // ignore for this test + } + + public static class TestQPMain extends QuorumPeerMain { + + public void shutdown() { + // ensure it closes - in particular wait for thread to exit + if (quorumPeer != null) { + QuorumBase.shutdown(quorumPeer); + } + } + + } + + public static class MainThread implements Runnable { + + final File confFile; + final File tmpDir; + + public static final int UNSET_STATIC_CLIENTPORT = -1; + // standalone mode doens't need myid + public static final int UNSET_MYID = -1; + + volatile TestQPMain main; + + File baseDir; + private int myid; + private int clientPort; + private String quorumCfgSection; + private Map otherConfigs; + + /** + * Create a MainThread + * + * @param myid + * @param clientPort + * @param quorumCfgSection + * @param otherConfigs + * @param tickTime initLimit will be 10 and syncLimit will be 5 + * @throws IOException + */ + public MainThread(int myid, int clientPort, String quorumCfgSection, Map otherConfigs, int tickTime) throws IOException { + baseDir = ClientBase.createTmpDir(); + this.myid = myid; + this.clientPort = clientPort; + this.quorumCfgSection = quorumCfgSection; + this.otherConfigs = otherConfigs; + LOG.info("id = {} tmpDir = {} clientPort = {}", myid, baseDir, clientPort); + confFile = new File(baseDir, "zoo.cfg"); + + FileWriter fwriter = new FileWriter(confFile); + fwriter.write("tickTime=" + tickTime + "\n"); + fwriter.write("initLimit=10\n"); + fwriter.write("syncLimit=5\n"); + fwriter.write("connectToLearnerMasterLimit=5\n"); + + tmpDir = new File(baseDir, "data"); + if (!tmpDir.mkdir()) { + throw new IOException("Unable to mkdir " + tmpDir); + } + + // Convert windows path to UNIX to avoid problems with "\" + String dir = tmpDir.toString(); + String osname = java.lang.System.getProperty("os.name"); + if (osname.toLowerCase().contains("windows")) { + dir = dir.replace('\\', '/'); + } + fwriter.write("dataDir=" + dir + "\n"); + + fwriter.write("clientPort=" + clientPort + "\n"); + + // write extra configurations + Set> entrySet = otherConfigs.entrySet(); + for (Entry entry : entrySet) { + fwriter.write(entry.getKey() + "=" + entry.getValue() + "\n"); + } + + fwriter.write(quorumCfgSection + "\n"); + fwriter.flush(); + fwriter.close(); + + File myidFile = new File(tmpDir, "myid"); + fwriter = new FileWriter(myidFile); + fwriter.write(Integer.toString(myid)); + fwriter.flush(); + fwriter.close(); + } + + public MainThread(int myid, String quorumCfgSection) throws IOException { + this(myid, quorumCfgSection, true); + } + + public MainThread(int myid, String quorumCfgSection, Integer secureClientPort, boolean writeDynamicConfigFile) throws IOException { + this(myid, UNSET_STATIC_CLIENTPORT, JettyAdminServer.DEFAULT_PORT, secureClientPort, quorumCfgSection, null, null, writeDynamicConfigFile, null); + } + + public MainThread(int myid, String quorumCfgSection, boolean writeDynamicConfigFile) throws IOException { + this(myid, UNSET_STATIC_CLIENTPORT, quorumCfgSection, writeDynamicConfigFile); + } + + public MainThread(int myid, int clientPort, String quorumCfgSection, boolean writeDynamicConfigFile) throws IOException { + this(myid, clientPort, JettyAdminServer.DEFAULT_PORT, quorumCfgSection, null, null, writeDynamicConfigFile); + } + + public MainThread(int myid, int clientPort, String quorumCfgSection, String peerType, boolean writeDynamicConfigFile) throws IOException { + this(myid, clientPort, JettyAdminServer.DEFAULT_PORT, quorumCfgSection, null, peerType, writeDynamicConfigFile); + } + + public MainThread(int myid, int clientPort, String quorumCfgSection, boolean writeDynamicConfigFile, String version) throws IOException { + this(myid, clientPort, JettyAdminServer.DEFAULT_PORT, quorumCfgSection, null, null, writeDynamicConfigFile, version); + } + + public MainThread(int myid, int clientPort, String quorumCfgSection, String configs) throws IOException { + this(myid, clientPort, JettyAdminServer.DEFAULT_PORT, quorumCfgSection, configs, null, true); + } + + public MainThread(int myid, int clientPort, int adminServerPort, String quorumCfgSection, String configs) throws IOException { + this(myid, clientPort, adminServerPort, quorumCfgSection, configs, null, true); + } + + public MainThread(int myid, int clientPort, int adminServerPort, String quorumCfgSection, String configs, String peerType, boolean writeDynamicConfigFile) throws IOException { + this(myid, clientPort, adminServerPort, quorumCfgSection, configs, peerType, writeDynamicConfigFile, null); + } + + public MainThread(int myid, int clientPort, int adminServerPort, String quorumCfgSection, String configs, String peerType, boolean writeDynamicConfigFile, String version) throws IOException { + this(myid, clientPort, adminServerPort, null, quorumCfgSection, configs, peerType, writeDynamicConfigFile, version); + } + + public MainThread(int myid, int clientPort, int adminServerPort, Integer secureClientPort, String quorumCfgSection, String configs, String peerType, boolean writeDynamicConfigFile, String version) throws IOException { + tmpDir = ClientBase.createTmpDir(); + LOG.info("id = {} tmpDir = {} clientPort = {} adminServerPort = {}", myid, tmpDir, clientPort, adminServerPort); + + File dataDir = new File(tmpDir, "data"); + if (!dataDir.mkdir()) { + throw new IOException("Unable to mkdir " + dataDir); + } + + confFile = new File(tmpDir, "zoo.cfg"); + + FileWriter fwriter = new FileWriter(confFile); + fwriter.write("tickTime=4000\n"); + fwriter.write("initLimit=10\n"); + fwriter.write("syncLimit=5\n"); + fwriter.write("connectToLearnerMasterLimit=5\n"); + if (configs != null) { + fwriter.write(configs); + } + + // Convert windows path to UNIX to avoid problems with "\" + String dir = PathUtils.normalizeFileSystemPath(dataDir.toString()); + + fwriter.write("dataDir=" + dir + "\n"); + fwriter.write("admin.serverPort=" + adminServerPort + "\n"); + + // For backward compatibility test, some tests create dynamic configuration + // without setting client port. + // This could happen both in static file or dynamic file. + if (clientPort != UNSET_STATIC_CLIENTPORT) { + fwriter.write("clientPort=" + clientPort + "\n"); + } + + if (secureClientPort != null) { + fwriter.write("secureClientPort=" + secureClientPort + "\n"); + } + + if (peerType != null) { + fwriter.write("peerType=" + peerType + "\n"); + } + + if (writeDynamicConfigFile) { + String dynamicConfigFilename = createDynamicFile(quorumCfgSection, version); + fwriter.write("dynamicConfigFile=" + dynamicConfigFilename + "\n"); + } else { + fwriter.write(quorumCfgSection); + } + fwriter.flush(); + fwriter.close(); + + File myidFile = new File(dataDir, "myid"); + fwriter = new FileWriter(myidFile); + fwriter.write(Integer.toString(myid)); + fwriter.flush(); + fwriter.close(); + + ClientBase.createInitializeFile(dataDir); + } + + private String createDynamicFile(String quorumCfgSection, String version) throws IOException { + String filename = "zoo.cfg.dynamic"; + if (version != null) { + filename = filename + "." + version; + } + + File dynamicConfigFile = new File(tmpDir, filename); + String dynamicConfigFilename = PathUtils.normalizeFileSystemPath(dynamicConfigFile.toString()); + + FileWriter fDynamicConfigWriter = new FileWriter(dynamicConfigFile); + fDynamicConfigWriter.write(quorumCfgSection); + fDynamicConfigWriter.flush(); + fDynamicConfigWriter.close(); + + return dynamicConfigFilename; + } + + public File[] getDynamicFiles() { + return getFilesWithPrefix("zoo.cfg.dynamic"); + } + + public File[] getFilesWithPrefix(final String prefix) { + return tmpDir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.startsWith(prefix); + } + }); + } + + public File getFileByName(String filename) { + File f = new File(tmpDir.getPath(), filename); + return f.isFile() ? f : null; + } + + public void writeTempDynamicConfigFile(String nextQuorumCfgSection, String version) throws IOException { + File nextDynamicConfigFile = new File(tmpDir, "zoo.cfg" + QuorumPeerConfig.nextDynamicConfigFileSuffix); + FileWriter fwriter = new FileWriter(nextDynamicConfigFile); + fwriter.write(nextQuorumCfgSection + "\n" + "version=" + version); + fwriter.flush(); + fwriter.close(); + } + + public MainThread(int myid, int clientPort, String quorumCfgSection) throws IOException { + this(myid, clientPort, quorumCfgSection, new HashMap()); + } + + public MainThread(int myid, int clientPort, String quorumCfgSection, Map otherConfigs) throws IOException { + this(myid, clientPort, quorumCfgSection, otherConfigs, 4000); + } + + Thread currentThread; + + public synchronized void start() { + main = getTestQPMain(); + currentThread = new Thread(this); + currentThread.start(); + } + + public TestQPMain getTestQPMain() { + return new TestQPMain(); + } + + public void run() { + String[] args = new String[1]; + args[0] = confFile.toString(); + try { + main.initializeAndRun(args); + } catch (Exception e) { + // test will still fail even though we just log/ignore + LOG.error("unexpected exception in run", e); + } finally { + currentThread = null; + } + } + + public void shutdown() throws InterruptedException { + Thread t = currentThread; + if (t != null && t.isAlive()) { + main.shutdown(); + t.join(500); + } + } + + public void join(long timeout) throws InterruptedException { + Thread t = currentThread; + if (t != null) { + t.join(timeout); + } + } + + public boolean isAlive() { + Thread t = currentThread; + return t != null && t.isAlive(); + } + + public void reinitialize() throws IOException { + File dataDir = main.quorumPeer.getTxnFactory().getDataDir(); + ClientBase.recursiveDelete(dataDir); + ClientBase.createInitializeFile(dataDir.getParentFile()); + } + + public boolean isQuorumPeerRunning() { + return main.quorumPeer != null; + } + + public String getPropFromStaticFile(String key) throws IOException { + Properties props = new Properties(); + props.load(new FileReader(confFile)); + return props.getProperty(key, ""); + } + + public QuorumPeer getQuorumPeer() { + return main.quorumPeer; + } + + public void deleteBaseDir() { + ClientBase.recursiveDelete(baseDir); + } + + public int getMyid() { + return myid; + } + + public int getClientPort() { + return clientPort; + } + + public String getQuorumCfgSection() { + return quorumCfgSection; + } + + public Map getOtherConfigs() { + return otherConfigs; + } + + public File getConfFile() { + return confFile; + } + + } + + // This class holds the servers and clients for those servers + protected static class Servers { + + MainThread[] mt; + ZooKeeper[] zk; + int[] clientPorts; + + public void shutDownAllServers() throws InterruptedException { + for (MainThread t : mt) { + t.shutdown(); + } + } + + public void restartAllServersAndClients(Watcher watcher) throws IOException, InterruptedException { + for (MainThread t : mt) { + if (!t.isAlive()) { + t.start(); + } + } + for (int i = 0; i < zk.length; i++) { + restartClient(i, watcher); + } + } + + public void restartClient(int clientIndex, Watcher watcher) throws IOException, InterruptedException { + if (zk[clientIndex] != null) { + zk[clientIndex].close(); + } + zk[clientIndex] = new ZooKeeper( + "127.0.0.1:" + clientPorts[clientIndex], + ClientBase.CONNECTION_TIMEOUT, + watcher); + } + + public int findLeader() { + for (int i = 0; i < mt.length; i++) { + if (mt[i].main.quorumPeer.leader != null) { + LOG.info("Leader is {}", i); + return i; + } + } + LOG.info("Cannot find Leader"); + return -1; + } + + public int findAnyFollower() { + for (int i = 0; i < mt.length; i++) { + if (mt[i].main.quorumPeer.follower != null) { + LOG.info("Follower is {}", i); + return i; + } + } + LOG.info("Cannot find any follower"); + return -1; + } + + public int findAnyObserver() { + for (int i = 0; i < mt.length; i++) { + if (mt[i].main.quorumPeer.observer != null) { + LOG.info("Observer is {}", i); + return i; + } + } + LOG.info("Cannot find any observer"); + return -1; + } + } + + protected Servers LaunchServers(int numServers) throws IOException, InterruptedException { + return LaunchServers(numServers, null); + } + + protected Servers LaunchServers(int numServers, Integer tickTime) throws IOException, InterruptedException { + return LaunchServers(numServers, 0, tickTime); + } + + /** * This is a helper function for launching a set of servers + * + * @param numServers the number of participant servers + * @param numObserver the number of observer servers + * @param tickTime A ticktime to pass to MainThread + * @return + * @throws IOException + * @throws InterruptedException + */ + protected Servers LaunchServers(int numServers, int numObservers, Integer tickTime) throws IOException, InterruptedException { + int SERVER_COUNT = numServers + numObservers; + QuorumPeerMainTest.Servers svrs = new QuorumPeerMainTest.Servers(); + svrs.clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < SERVER_COUNT; i++) { + svrs.clientPorts[i] = PortAssignment.unique(); + String role = i < numServers ? "participant" : "observer"; + sb.append(String.format("server.%d=127.0.0.1:%d:%d:%s;127.0.0.1:%d\n", + i, PortAssignment.unique(), PortAssignment.unique(), role, + svrs.clientPorts[i])); + } + String quorumCfgSection = sb.toString(); + + svrs.mt = new MainThread[SERVER_COUNT]; + svrs.zk = new ZooKeeper[SERVER_COUNT]; + for (int i = 0; i < SERVER_COUNT; i++) { + if (tickTime != null) { + svrs.mt[i] = new MainThread(i, svrs.clientPorts[i], quorumCfgSection, new HashMap(), tickTime); + } else { + svrs.mt[i] = new MainThread(i, svrs.clientPorts[i], quorumCfgSection); + } + svrs.mt[i].start(); + svrs.restartClient(i, this); + } + + waitForAll(svrs, ZooKeeper.States.CONNECTED); + + return svrs; + } + + public static void waitForOne(ZooKeeper zk, ZooKeeper.States state) throws InterruptedException { + int iterations = ClientBase.CONNECTION_TIMEOUT / 500; + while (zk.getState() != state) { + if (iterations-- == 0) { + throw new RuntimeException("Waiting too long " + zk.getState() + " != " + state); + } + Thread.sleep(500); + } + } + + protected void waitForAll(Servers servers, ZooKeeper.States state) throws InterruptedException { + waitForAll(servers.zk, state); + } + + public static void waitForAll(ZooKeeper[] zks, ZooKeeper.States state) throws InterruptedException { + int iterations = ClientBase.CONNECTION_TIMEOUT / 1000; + boolean someoneNotConnected = true; + while (someoneNotConnected) { + if (iterations-- == 0) { + logStates(zks); + ClientBase.logAllStackTraces(); + throw new RuntimeException("Waiting too long"); + } + + someoneNotConnected = false; + for (ZooKeeper zk : zks) { + if (zk.getState() != state) { + someoneNotConnected = true; + break; + } + } + Thread.sleep(1000); + } + } + + public static void logStates(ZooKeeper[] zks) { + StringBuilder sbBuilder = new StringBuilder("Connection States: {"); + for (int i = 0; i < zks.length; i++) { + sbBuilder.append(i + " : " + zks[i].getState() + ", "); + } + sbBuilder.append('}'); + LOG.error(sbBuilder.toString()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumRequestPipelineTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumRequestPipelineTest.java new file mode 100644 index 0000000..3565c56 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumRequestPipelineTest.java @@ -0,0 +1,164 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZKParameterized; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.QuorumBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class QuorumRequestPipelineTest extends QuorumBase { + + protected ServerState serverState; + protected final CountDownLatch callComplete = new CountDownLatch(1); + protected boolean complete = false; + protected static final String PARENT_PATH = "/foo"; + protected static final Set CHILDREN = new HashSet(Arrays.asList("1", "2", "3")); + protected static final String AUTH_PROVIDER = "digest"; + protected static final byte[] AUTH = "hello".getBytes(); + protected static final byte[] DATA = "Hint Water".getBytes(); + + protected TestableZooKeeper zkClient; + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][]{{ServerState.LEADING}, {ServerState.FOLLOWING}, {ServerState.OBSERVING}}); + } + + public QuorumRequestPipelineTest(ServerState state) { + this.serverState = state; + } + + @Before + public void setUp() throws Exception { + CountdownWatcher clientWatch = new CountdownWatcher(); + super.setUp(true); + zkClient = createClient(clientWatch, getPeersMatching(serverState)); + zkClient.addAuthInfo(AUTH_PROVIDER, AUTH); + clientWatch.waitForConnected(CONNECTION_TIMEOUT); + } + + @After + public void tearDown() throws Exception { + zkClient.close(); + super.tearDown(); + } + + private Stat create2EmptyNode(TestableZooKeeper zkClient, String path) throws Exception { + Stat stat = new Stat(); + zkClient.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat); + return stat; + } + + @Test + public void testCreate() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertArrayEquals(String.format("%s Node created (create) with expected value", serverState), DATA, zkClient.getData(PARENT_PATH, false, null)); + } + + @Test + public void testCreate2() throws Exception { + zkClient.create(PARENT_PATH, DATA, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + assertArrayEquals(String.format("%s Node created (create2) with expected value", serverState), DATA, zkClient.getData(PARENT_PATH, false, null)); + } + + @Test + public void testDelete() throws Exception { + create2EmptyNode(zkClient, PARENT_PATH); + zkClient.delete(PARENT_PATH, -1); + assertNull(String.format("%s Node no longer exists", serverState), zkClient.exists(PARENT_PATH, false)); + } + + @Test + public void testExists() throws Exception { + Stat stat = create2EmptyNode(zkClient, PARENT_PATH); + assertEquals(String.format("%s Exists returns correct node stat", serverState), stat, zkClient.exists(PARENT_PATH, false)); + } + + @Test + public void testSetAndGetData() throws Exception { + create2EmptyNode(zkClient, PARENT_PATH); + zkClient.setData(PARENT_PATH, DATA, -1); + assertArrayEquals(String.format("%s Node updated with expected value", serverState), DATA, zkClient.getData(PARENT_PATH, false, null)); + } + + @Test + public void testSetAndGetACL() throws Exception { + create2EmptyNode(zkClient, PARENT_PATH); + assertEquals(String.format("%s Node has open ACL", serverState), Ids.OPEN_ACL_UNSAFE, zkClient.getACL(PARENT_PATH, new Stat())); + zkClient.setACL(PARENT_PATH, Ids.READ_ACL_UNSAFE, -1); + assertEquals(String.format("%s Node has world read-only ACL", serverState), Ids.READ_ACL_UNSAFE, zkClient.getACL(PARENT_PATH, new Stat())); + } + + @Test + public void testSetAndGetChildren() throws Exception { + create2EmptyNode(zkClient, PARENT_PATH); + for (String child : CHILDREN) { + create2EmptyNode(zkClient, PARENT_PATH + "/" + child); + } + assertEquals(String.format("%s Parent has expected children", serverState), CHILDREN, new HashSet(zkClient.getChildren(PARENT_PATH, false))); + } + + @Test + public void testSetAndGetChildren2() throws Exception { + create2EmptyNode(zkClient, PARENT_PATH); + for (String child : CHILDREN) { + create2EmptyNode(zkClient, PARENT_PATH + "/" + child); + } + assertEquals(String.format("%s Parent has expected children", serverState), CHILDREN, new HashSet(zkClient.getChildren(PARENT_PATH, false, null))); + } + + @Test + public void testSync() throws Exception { + complete = false; + create2EmptyNode(zkClient, PARENT_PATH); + VoidCallback onSync = new VoidCallback() { + @Override + public void processResult(int rc, String path, Object ctx) { + complete = true; + callComplete.countDown(); + } + }; + zkClient.sync(PARENT_PATH, onSync, null); + callComplete.await(30, TimeUnit.SECONDS); + assertTrue(String.format("%s Sync completed", serverState), complete); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumSSLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumSSLTest.java new file mode 100644 index 0000000..593edbd --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumSSLTest.java @@ -0,0 +1,902 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.apache.zookeeper.test.ClientBase.createTmpDir; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.math.BigInteger; +import java.net.InetSocketAddress; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.KeyStore; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.PrivateKey; +import java.security.Security; +import java.security.cert.Certificate; +import java.security.cert.CertificateEncodingException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import javax.net.ssl.SSLServerSocketFactory; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.QuorumX509Util; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.test.ClientBase; +import org.bouncycastle.asn1.ocsp.OCSPResponse; +import org.bouncycastle.asn1.ocsp.OCSPResponseStatus; +import org.bouncycastle.asn1.x500.X500Name; +import org.bouncycastle.asn1.x500.X500NameBuilder; +import org.bouncycastle.asn1.x500.style.BCStyle; +import org.bouncycastle.asn1.x509.AuthorityInformationAccess; +import org.bouncycastle.asn1.x509.BasicConstraints; +import org.bouncycastle.asn1.x509.CRLDistPoint; +import org.bouncycastle.asn1.x509.CRLNumber; +import org.bouncycastle.asn1.x509.CRLReason; +import org.bouncycastle.asn1.x509.DistributionPoint; +import org.bouncycastle.asn1.x509.DistributionPointName; +import org.bouncycastle.asn1.x509.Extension; +import org.bouncycastle.asn1.x509.GeneralName; +import org.bouncycastle.asn1.x509.GeneralNames; +import org.bouncycastle.asn1.x509.KeyUsage; +import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; +import org.bouncycastle.asn1.x509.X509ObjectIdentifiers; +import org.bouncycastle.cert.X509CRLHolder; +import org.bouncycastle.cert.X509CertificateHolder; +import org.bouncycastle.cert.X509ExtensionUtils; +import org.bouncycastle.cert.X509v2CRLBuilder; +import org.bouncycastle.cert.X509v3CertificateBuilder; +import org.bouncycastle.cert.bc.BcX509ExtensionUtils; +import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; +import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder; +import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils; +import org.bouncycastle.cert.jcajce.JcaX509v2CRLBuilder; +import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; +import org.bouncycastle.cert.ocsp.BasicOCSPResp; +import org.bouncycastle.cert.ocsp.BasicOCSPRespBuilder; +import org.bouncycastle.cert.ocsp.CertificateID; +import org.bouncycastle.cert.ocsp.CertificateStatus; +import org.bouncycastle.cert.ocsp.OCSPException; +import org.bouncycastle.cert.ocsp.OCSPReq; +import org.bouncycastle.cert.ocsp.OCSPResp; +import org.bouncycastle.cert.ocsp.OCSPRespBuilder; +import org.bouncycastle.cert.ocsp.Req; +import org.bouncycastle.cert.ocsp.UnknownStatus; +import org.bouncycastle.cert.ocsp.jcajce.JcaBasicOCSPRespBuilder; +import org.bouncycastle.cert.ocsp.jcajce.JcaCertificateID; +import org.bouncycastle.crypto.util.PublicKeyFactory; +import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.openssl.MiscPEMGenerator; +import org.bouncycastle.operator.ContentSigner; +import org.bouncycastle.operator.DigestCalculator; +import org.bouncycastle.operator.OperatorException; +import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; +import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; +import org.bouncycastle.util.io.pem.PemWriter; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; + +public class QuorumSSLTest extends QuorumPeerTestBase { + + private static final String SSL_QUORUM_ENABLED = "sslQuorum=true\n"; + private static final String PORT_UNIFICATION_ENABLED = "portUnification=true\n"; + private static final String PORT_UNIFICATION_DISABLED = "portUnification=false\n"; + + private static final char[] PASSWORD = "testpass".toCharArray(); + private static final String HOSTNAME = "localhost"; + + private QuorumX509Util quorumX509Util; + + private MainThread q1; + private MainThread q2; + private MainThread q3; + + private int clientPortQp1; + private int clientPortQp2; + private int clientPortQp3; + + private String tmpDir; + + private String quorumConfiguration; + private String validKeystorePath; + private String truststorePath; + + private KeyPair rootKeyPair; + private X509Certificate rootCertificate; + + private KeyPair defaultKeyPair; + + private ContentSigner contentSigner; + + private Date certStartTime; + private Date certEndTime; + + @Rule + public Timeout timeout = Timeout.builder().withTimeout(5, TimeUnit.MINUTES).withLookingForStuckThread(true).build(); + + @Before + public void setup() throws Exception { + quorumX509Util = new QuorumX509Util(); + ClientBase.setupTestEnv(); + + tmpDir = createTmpDir().getAbsolutePath(); + + clientPortQp1 = PortAssignment.unique(); + clientPortQp2 = PortAssignment.unique(); + clientPortQp3 = PortAssignment.unique(); + + validKeystorePath = tmpDir + "/valid.jks"; + truststorePath = tmpDir + "/truststore.jks"; + + quorumConfiguration = generateQuorumConfiguration(); + + Security.addProvider(new BouncyCastleProvider()); + + certStartTime = new Date(); + Calendar cal = Calendar.getInstance(); + cal.setTime(certStartTime); + cal.add(Calendar.YEAR, 1); + certEndTime = cal.getTime(); + + rootKeyPair = createKeyPair(); + contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(rootKeyPair.getPrivate()); + rootCertificate = createSelfSignedCertifcate(rootKeyPair); + + // Write the truststore + KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); + trustStore.load(null, PASSWORD); + trustStore.setCertificateEntry(rootCertificate.getSubjectDN().toString(), rootCertificate); + FileOutputStream outputStream = new FileOutputStream(truststorePath); + trustStore.store(outputStream, PASSWORD); + outputStream.flush(); + outputStream.close(); + + defaultKeyPair = createKeyPair(); + X509Certificate validCertificate = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + HOSTNAME, + "127.0.0.1", + null, + null); + writeKeystore(validCertificate, defaultKeyPair, validKeystorePath); + + setSSLSystemProperties(); + } + + private void writeKeystore(X509Certificate certificate, KeyPair entityKeyPair, String path) throws Exception { + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + keyStore.load(null, PASSWORD); + keyStore.setKeyEntry("alias", entityKeyPair.getPrivate(), PASSWORD, new Certificate[]{certificate}); + FileOutputStream outputStream = new FileOutputStream(path); + keyStore.store(outputStream, PASSWORD); + outputStream.flush(); + outputStream.close(); + } + + private class OCSPHandler implements HttpHandler { + + private X509Certificate revokedCert; + + // Builds an OCSPHandler that responds with a good status for all certificates + // except revokedCert. + public OCSPHandler(X509Certificate revokedCert) { + this.revokedCert = revokedCert; + } + + @Override + public void handle(com.sun.net.httpserver.HttpExchange httpExchange) throws IOException { + byte[] responseBytes; + try { + InputStream request = httpExchange.getRequestBody(); + byte[] requestBytes = new byte[10000]; + request.read(requestBytes); + + OCSPReq ocspRequest = new OCSPReq(requestBytes); + Req[] requestList = ocspRequest.getRequestList(); + + DigestCalculator digestCalculator = new JcaDigestCalculatorProviderBuilder().build().get(CertificateID.HASH_SHA1); + + BasicOCSPRespBuilder responseBuilder = new JcaBasicOCSPRespBuilder(rootKeyPair.getPublic(), digestCalculator); + for (Req req : requestList) { + CertificateID certId = req.getCertID(); + CertificateID revokedCertId = new JcaCertificateID(digestCalculator, rootCertificate, revokedCert.getSerialNumber()); + CertificateStatus certificateStatus; + if (revokedCertId.equals(certId)) { + certificateStatus = new UnknownStatus(); + } else { + certificateStatus = CertificateStatus.GOOD; + } + + responseBuilder.addResponse(certId, certificateStatus, null); + } + + X509CertificateHolder[] chain = new X509CertificateHolder[]{new JcaX509CertificateHolder(rootCertificate)}; + ContentSigner signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(rootKeyPair.getPrivate()); + BasicOCSPResp ocspResponse = responseBuilder.build(signer, chain, Calendar.getInstance().getTime()); + + responseBytes = new OCSPRespBuilder().build(OCSPRespBuilder.SUCCESSFUL, ocspResponse).getEncoded(); + } catch (OperatorException | CertificateEncodingException | OCSPException exception) { + responseBytes = new OCSPResp(new OCSPResponse(new OCSPResponseStatus(OCSPRespBuilder.INTERNAL_ERROR), null)).getEncoded(); + } + + Headers rh = httpExchange.getResponseHeaders(); + rh.set("Content-Type", "application/ocsp-response"); + httpExchange.sendResponseHeaders(200, responseBytes.length); + + OutputStream os = httpExchange.getResponseBody(); + os.write(responseBytes); + os.close(); + } + + } + + private X509Certificate createSelfSignedCertifcate(KeyPair keyPair) throws Exception { + X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); + nameBuilder.addRDN(BCStyle.CN, HOSTNAME); + BigInteger serialNumber = new BigInteger(128, new Random()); + + JcaX509v3CertificateBuilder jcaX509v3CertificateBuilder = new JcaX509v3CertificateBuilder( + nameBuilder.build(), + serialNumber, + certStartTime, + certEndTime, + nameBuilder.build(), + keyPair.getPublic()); + X509v3CertificateBuilder certificateBuilder = jcaX509v3CertificateBuilder + .addExtension(Extension.basicConstraints, true, new BasicConstraints(0)) + .addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyCertSign | KeyUsage.cRLSign)); + + return new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(contentSigner)); + } + + private void buildCRL(X509Certificate x509Certificate, String crlPath) throws Exception { + X509v2CRLBuilder builder = new JcaX509v2CRLBuilder(x509Certificate.getIssuerX500Principal(), certStartTime); + builder.addCRLEntry(x509Certificate.getSerialNumber(), certStartTime, CRLReason.cACompromise); + builder.setNextUpdate(certEndTime); + builder.addExtension(Extension.authorityKeyIdentifier, false, new JcaX509ExtensionUtils().createAuthorityKeyIdentifier(rootCertificate)); + builder.addExtension(Extension.cRLNumber, false, new CRLNumber(new BigInteger("1000"))); + + X509CRLHolder cRLHolder = builder.build(contentSigner); + + PemWriter pemWriter = new PemWriter(new FileWriter(crlPath)); + pemWriter.writeObject(new MiscPEMGenerator(cRLHolder)); + pemWriter.flush(); + pemWriter.close(); + } + + public X509Certificate buildEndEntityCert( + KeyPair keyPair, + X509Certificate caCert, + PrivateKey caPrivateKey, + String hostname, + String ipAddress, + String crlPath, + Integer ocspPort) throws Exception { + X509CertificateHolder holder = new JcaX509CertificateHolder(caCert); + ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(caPrivateKey); + + List generalNames = new ArrayList<>(); + if (hostname != null) { + generalNames.add(new GeneralName(GeneralName.dNSName, hostname)); + } + + if (ipAddress != null) { + generalNames.add(new GeneralName(GeneralName.iPAddress, ipAddress)); + } + + SubjectPublicKeyInfo entityKeyInfo = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo( + PublicKeyFactory.createKey(keyPair.getPublic().getEncoded())); + X509ExtensionUtils extensionUtils = new BcX509ExtensionUtils(); + JcaX509v3CertificateBuilder jcaX509v3CertificateBuilder = new JcaX509v3CertificateBuilder( + holder.getSubject(), + new BigInteger(128, new Random()), + certStartTime, + certEndTime, + new X500Name("CN=Test End Entity Certificate"), + keyPair.getPublic()); + X509v3CertificateBuilder certificateBuilder = jcaX509v3CertificateBuilder + .addExtension(Extension.authorityKeyIdentifier, false, extensionUtils.createAuthorityKeyIdentifier(holder)) + .addExtension(Extension.subjectKeyIdentifier, false, extensionUtils.createSubjectKeyIdentifier(entityKeyInfo)) + .addExtension(Extension.basicConstraints, true, new BasicConstraints(false)) + .addExtension(Extension.keyUsage, true, new KeyUsage(KeyUsage.digitalSignature | KeyUsage.keyEncipherment)); + + if (!generalNames.isEmpty()) { + certificateBuilder.addExtension( + Extension.subjectAlternativeName, + true, + new GeneralNames(generalNames.toArray(new GeneralName[]{}))); + } + + if (crlPath != null) { + DistributionPointName distPointOne = new DistributionPointName( + new GeneralNames(new GeneralName(GeneralName.uniformResourceIdentifier, "file://" + crlPath))); + + certificateBuilder.addExtension( + Extension.cRLDistributionPoints, + false, + new CRLDistPoint(new DistributionPoint[]{new DistributionPoint(distPointOne, null, null)})); + } + + if (ocspPort != null) { + certificateBuilder.addExtension( + Extension.authorityInfoAccess, + false, + new AuthorityInformationAccess( + X509ObjectIdentifiers.ocspAccessMethod, + new GeneralName(GeneralName.uniformResourceIdentifier, "http://" + hostname + ":" + ocspPort))); + } + + return new JcaX509CertificateConverter().getCertificate(certificateBuilder.build(signer)); + } + + private KeyPair createKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); + keyPairGenerator.initialize(4096); + KeyPair keyPair = keyPairGenerator.genKeyPair(); + return keyPair; + } + + private String generateQuorumConfiguration() { + StringBuilder sb = new StringBuilder(); + + int portQp1 = PortAssignment.unique(); + int portQp2 = PortAssignment.unique(); + int portQp3 = PortAssignment.unique(); + + int portLe1 = PortAssignment.unique(); + int portLe2 = PortAssignment.unique(); + int portLe3 = PortAssignment.unique(); + + sb.append(String.format("server.1=127.0.0.1:%d:%d;%d\n", portQp1, portLe1, clientPortQp1)); + sb.append(String.format("server.2=127.0.0.1:%d:%d;%d\n", portQp2, portLe2, clientPortQp2)); + sb.append(String.format("server.3=127.0.0.1:%d:%d;%d\n", portQp3, portLe3, clientPortQp3)); + + return sb.toString(); + } + + private String generateMultiAddressQuorumConfiguration() { + StringBuilder sb = new StringBuilder(); + + int portQp1a = PortAssignment.unique(); + int portQp1b = PortAssignment.unique(); + int portQp2a = PortAssignment.unique(); + int portQp2b = PortAssignment.unique(); + int portQp3a = PortAssignment.unique(); + int portQp3b = PortAssignment.unique(); + + int portLe1a = PortAssignment.unique(); + int portLe1b = PortAssignment.unique(); + int portLe2a = PortAssignment.unique(); + int portLe2b = PortAssignment.unique(); + int portLe3a = PortAssignment.unique(); + int portLe3b = PortAssignment.unique(); + + sb.append(String.format("server.1=127.0.0.1:%d:%d|127.0.0.1:%d:%d;%d\n", portQp1a, portLe1a, portQp1b, portLe1b, clientPortQp1)); + sb.append(String.format("server.2=127.0.0.1:%d:%d|127.0.0.1:%d:%d;%d\n", portQp2a, portLe2a, portQp2b, portLe2b, clientPortQp2)); + sb.append(String.format("server.3=127.0.0.1:%d:%d|127.0.0.1:%d:%d;%d\n", portQp3a, portLe3a, portQp3b, portLe3b, clientPortQp3)); + + return sb.toString(); + } + + public void setSSLSystemProperties() { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), validKeystorePath); + System.setProperty(quorumX509Util.getSslKeystorePasswdProperty(), "testpass"); + System.setProperty(quorumX509Util.getSslTruststoreLocationProperty(), truststorePath); + System.setProperty(quorumX509Util.getSslTruststorePasswdProperty(), "testpass"); + } + + @After + public void cleanUp() throws Exception { + System.clearProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED); + clearSSLSystemProperties(); + if (q1 != null) { + q1.shutdown(); + } + if (q2 != null) { + q2.shutdown(); + } + if (q3 != null) { + q3.shutdown(); + } + + Security.removeProvider("BC"); + quorumX509Util.close(); + } + + private void clearSSLSystemProperties() { + System.clearProperty(quorumX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(quorumX509Util.getSslKeystorePasswdProperty()); + System.clearProperty(quorumX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(quorumX509Util.getSslTruststorePasswdProperty()); + System.clearProperty(quorumX509Util.getSslHostnameVerificationEnabledProperty()); + System.clearProperty(quorumX509Util.getSslOcspEnabledProperty()); + System.clearProperty(quorumX509Util.getSslCrlEnabledProperty()); + System.clearProperty(quorumX509Util.getCipherSuitesProperty()); + System.clearProperty(quorumX509Util.getSslProtocolProperty()); + } + + @Test + public void testQuorumSSL() throws Exception { + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + clearSSLSystemProperties(); + + // This server should fail to join the quorum as it is not using ssl. + q3 = new MainThread(3, clientPortQp3, quorumConfiguration); + q3.start(); + + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } + + + @Test + public void testQuorumSSLWithMultipleAddresses() throws Exception { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + quorumConfiguration = generateMultiAddressQuorumConfiguration(); + + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + clearSSLSystemProperties(); + + // This server should fail to join the quorum as it is not using ssl. + q3 = new MainThread(3, clientPortQp3, quorumConfiguration); + q3.start(); + + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } + + + @Test + public void testRollingUpgrade() throws Exception { + // Form a quorum without ssl + q1 = new MainThread(1, clientPortQp1, quorumConfiguration); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration); + q3 = new MainThread(3, clientPortQp3, quorumConfiguration); + + Map members = new HashMap<>(); + members.put(clientPortQp1, q1); + members.put(clientPortQp2, q2); + members.put(clientPortQp3, q3); + + for (MainThread member : members.values()) { + member.start(); + } + + for (int clientPort : members.keySet()) { + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); + } + + // Set SSL system properties and port unification, begin restarting servers + setSSLSystemProperties(); + + stopAppendConfigRestartAll(members, PORT_UNIFICATION_ENABLED); + stopAppendConfigRestartAll(members, SSL_QUORUM_ENABLED); + stopAppendConfigRestartAll(members, PORT_UNIFICATION_DISABLED); + } + + private void stopAppendConfigRestartAll(Map members, String config) throws Exception { + for (Map.Entry entry : members.entrySet()) { + int clientPort = entry.getKey(); + MainThread member = entry.getValue(); + + member.shutdown(); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); + + FileWriter fileWriter = new FileWriter(member.getConfFile(), true); + fileWriter.write(config); + fileWriter.flush(); + fileWriter.close(); + + member.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); + } + } + + @Test + public void testHostnameVerificationWithInvalidHostname() throws Exception { + String badhostnameKeystorePath = tmpDir + "/badhost.jks"; + X509Certificate badHostCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + "bleepbloop", + null, + null, + null); + writeKeystore(badHostCert, defaultKeyPair, badhostnameKeystorePath); + + testHostnameVerification(badhostnameKeystorePath, false); + } + + @Test + public void testHostnameVerificationWithInvalidIPAddress() throws Exception { + String badhostnameKeystorePath = tmpDir + "/badhost.jks"; + X509Certificate badHostCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + null, + "140.211.11.105", + null, + null); + writeKeystore(badHostCert, defaultKeyPair, badhostnameKeystorePath); + + testHostnameVerification(badhostnameKeystorePath, false); + } + + @Test + public void testHostnameVerificationWithInvalidIpAddressAndInvalidHostname() throws Exception { + String badhostnameKeystorePath = tmpDir + "/badhost.jks"; + X509Certificate badHostCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + "bleepbloop", + "140.211.11.105", + null, + null); + writeKeystore(badHostCert, defaultKeyPair, badhostnameKeystorePath); + + testHostnameVerification(badhostnameKeystorePath, false); + } + + @Test + public void testHostnameVerificationForInvalidMultiAddressServerConfig() throws Exception { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + quorumConfiguration = generateMultiAddressQuorumConfiguration(); + + String badhostnameKeystorePath = tmpDir + "/badhost.jks"; + X509Certificate badHostCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + "bleepbloop", + "140.211.11.105", + null, + null); + writeKeystore(badHostCert, defaultKeyPair, badhostnameKeystorePath); + + testHostnameVerification(badhostnameKeystorePath, false); + } + + @Test + public void testHostnameVerificationWithInvalidIpAddressAndValidHostname() throws Exception { + String badhostnameKeystorePath = tmpDir + "/badhost.jks"; + X509Certificate badHostCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + "localhost", + "140.211.11.105", + null, + null); + writeKeystore(badHostCert, defaultKeyPair, badhostnameKeystorePath); + + testHostnameVerification(badhostnameKeystorePath, true); + } + + @Test + public void testHostnameVerificationWithValidIpAddressAndInvalidHostname() throws Exception { + String badhostnameKeystorePath = tmpDir + "/badhost.jks"; + X509Certificate badHostCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + "bleepbloop", + "127.0.0.1", + null, + null); + writeKeystore(badHostCert, defaultKeyPair, badhostnameKeystorePath); + + testHostnameVerification(badhostnameKeystorePath, true); + } + + /** + * @param keystorePath The keystore to use + * @param expectSuccess True for expecting the keystore to pass hostname verification, false for expecting failure + * @throws Exception + */ + private void testHostnameVerification(String keystorePath, boolean expectSuccess) throws Exception { + System.setProperty(quorumX509Util.getSslHostnameVerificationEnabledProperty(), "false"); + + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), keystorePath); + + // This server should join successfully + q3 = new MainThread(3, clientPortQp3, quorumConfiguration, SSL_QUORUM_ENABLED); + q3.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + + setSSLSystemProperties(); + System.clearProperty(quorumX509Util.getSslHostnameVerificationEnabledProperty()); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), keystorePath); + q3.start(); + + assertEquals( + expectSuccess, + ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } + + @Test + public void testCertificateRevocationList() throws Exception { + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + String revokedInCRLKeystorePath = tmpDir + "/crl_revoked.jks"; + String crlPath = tmpDir + "/crl.pem"; + X509Certificate revokedInCRLCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + HOSTNAME, + null, + crlPath, + null); + writeKeystore(revokedInCRLCert, defaultKeyPair, revokedInCRLKeystorePath); + buildCRL(revokedInCRLCert, crlPath); + + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), revokedInCRLKeystorePath); + + // This server should join successfully + q3 = new MainThread(3, clientPortQp3, quorumConfiguration, SSL_QUORUM_ENABLED); + q3.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + + setSSLSystemProperties(); + System.setProperty(quorumX509Util.getSslCrlEnabledProperty(), "true"); + + X509Certificate validCertificate = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + HOSTNAME, + null, + crlPath, + null); + writeKeystore(validCertificate, defaultKeyPair, validKeystorePath); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), revokedInCRLKeystorePath); + q3.start(); + + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } + + @Test + public void testOCSP() throws Exception { + Integer ocspPort = PortAssignment.unique(); + + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + String revokedInOCSPKeystorePath = tmpDir + "/ocsp_revoked.jks"; + X509Certificate revokedInOCSPCert = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + HOSTNAME, + null, + null, + ocspPort); + writeKeystore(revokedInOCSPCert, defaultKeyPair, revokedInOCSPKeystorePath); + + HttpServer ocspServer = HttpServer.create(new InetSocketAddress(ocspPort), 0); + try { + ocspServer.createContext("/", new OCSPHandler(revokedInOCSPCert)); + ocspServer.start(); + + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), revokedInOCSPKeystorePath); + + // This server should join successfully + q3 = new MainThread(3, clientPortQp3, quorumConfiguration, SSL_QUORUM_ENABLED); + q3.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerDown("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + + setSSLSystemProperties(); + System.setProperty(quorumX509Util.getSslOcspEnabledProperty(), "true"); + + X509Certificate validCertificate = buildEndEntityCert( + defaultKeyPair, + rootCertificate, + rootKeyPair.getPrivate(), + HOSTNAME, + null, + null, + ocspPort); + writeKeystore(validCertificate, defaultKeyPair, validKeystorePath); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + System.setProperty(quorumX509Util.getSslKeystoreLocationProperty(), revokedInOCSPKeystorePath); + q3.start(); + + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } finally { + ocspServer.stop(0); + } + } + + @Test + public void testCipherSuites() throws Exception { + // Get default cipher suites from JDK + SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); + List defaultCiphers = new ArrayList(); + for (String cipher : ssf.getDefaultCipherSuites()) { + if (!cipher.matches(".*EMPTY.*") && cipher.startsWith("TLS") && cipher.contains("RSA")) { + defaultCiphers.add(cipher); + } + } + + if (defaultCiphers.size() < 2) { + fail("JDK has to support at least 2 valid (RSA) cipher suites for this test to run"); + } + + // Use them all except one to build the ensemble + String suitesOfEnsemble = String.join(",", defaultCiphers.subList(1, defaultCiphers.size())); + System.setProperty(quorumX509Util.getCipherSuitesProperty(), suitesOfEnsemble); + + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + // Use the odd one out for the client + String suiteOfClient = defaultCiphers.get(0); + System.setProperty(quorumX509Util.getCipherSuitesProperty(), suiteOfClient); + + // This server should fail to join the quorum as it is not using one of the supported suites from the other + // quorum members + q3 = new MainThread(3, clientPortQp3, quorumConfiguration, SSL_QUORUM_ENABLED); + q3.start(); + + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } + + @Test + public void testProtocolVersion() throws Exception { + System.setProperty(quorumX509Util.getSslProtocolProperty(), "TLSv1.2"); + + q1 = new MainThread(1, clientPortQp1, quorumConfiguration, SSL_QUORUM_ENABLED); + q2 = new MainThread(2, clientPortQp2, quorumConfiguration, SSL_QUORUM_ENABLED); + + q1.start(); + q2.start(); + + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp1, CONNECTION_TIMEOUT)); + assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp2, CONNECTION_TIMEOUT)); + + System.setProperty(quorumX509Util.getSslProtocolProperty(), "TLSv1.1"); + + // This server should fail to join the quorum as it is not using TLSv1.2 + q3 = new MainThread(3, clientPortQp3, quorumConfiguration, SSL_QUORUM_ENABLED); + q3.start(); + + assertFalse(ClientBase.waitForServerUp("127.0.0.1:" + clientPortQp3, CONNECTION_TIMEOUT)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerConfigBuilder.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerConfigBuilder.java new file mode 100644 index 0000000..6bbb6e4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerConfigBuilder.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import org.apache.zookeeper.PortAssignment; + + +/* + * Helper class to build / change Quorum Config String, like: + * server.1=127.0.0.1:11228:11231|127.0.0.1:11230:11229;11227 + * server.2=127.0.0.1:11338:11331|127.0.0.1:11330:11229;11337 + * + */ +public class QuorumServerConfigBuilder { + + // map of (serverId -> clientPort) + private final Map clientIds = new HashMap<>(); + + // map of (serverId -> (ServerAddress=host,quorumPort,electionPort) ) + private final Map> serverAddresses = new HashMap<>(); + private final String hostName; + private final int numberOfServers; + + public QuorumServerConfigBuilder(String hostName, int numberOfServers, int numberOfServerAddresses) { + this.numberOfServers = numberOfServers; + this.hostName = hostName; + for (int serverId = 0; serverId < numberOfServers; serverId++) { + clientIds.put(serverId, PortAssignment.unique()); + + List addresses = new ArrayList<>(); + serverAddresses.put(serverId, addresses); + + for (int serverAddressId = 0; serverAddressId < numberOfServerAddresses; serverAddressId++) { + addresses.add(new ServerAddress(hostName)); + } + + } + } + + public QuorumServerConfigBuilder(QuorumServerConfigBuilder otherBuilder) { + this.numberOfServers = otherBuilder.clientIds.size(); + this.clientIds.putAll(otherBuilder.clientIds); + this.hostName = otherBuilder.hostName; + for (int i : otherBuilder.serverAddresses.keySet()) { + List clonedServerAddresses = otherBuilder.serverAddresses.get(i).stream() + .map(ServerAddress::clone).collect(Collectors.toList()); + this.serverAddresses.put(i, clonedServerAddresses); + } + } + + public int getClientPort(int serverId) { + return clientIds.get(serverId); + } + + public ServerAddress getServerAddress(int serverId, int addressId) { + return serverAddresses.get(serverId).get(addressId); + } + + public QuorumServerConfigBuilder changeHostName(int serverId, int addressId, String hostName) { + serverAddresses.get(serverId).get(addressId).setHost(hostName); + return this; + } + + public QuorumServerConfigBuilder changeQuorumPort(int serverId, int addressId, int quorumPort) { + serverAddresses.get(serverId).get(addressId).setQuorumPort(quorumPort); + return this; + } + + public QuorumServerConfigBuilder changeElectionPort(int serverId, int addressId, int electionPort) { + serverAddresses.get(serverId).get(addressId).setElectionPort(electionPort); + return this; + } + + public QuorumServerConfigBuilder addNewServerAddress(int serverId) { + serverAddresses.get(serverId).add(new ServerAddress(hostName)); + return this; + } + + public QuorumServerConfigBuilder deleteLastServerAddress(int serverId) { + serverAddresses.get(serverId).remove(serverAddresses.get(serverId).size() - 1); + return this; + } + + public String build() { + return String.join("\n", buildAsStringList()); + } + + public List buildAsStringList() { + List result = new ArrayList<>(numberOfServers); + + for (int serverId = 0; serverId < numberOfServers; serverId++) { + String s = serverAddresses.get(serverId).stream() + .map(ServerAddress::toString) + .collect(Collectors.joining("|")); + + result.add(String.format("server.%d=%s;%d", serverId, s, clientIds.get(serverId))); + } + + return result; + } + + public static class ServerAddress { + private String host; + private int quorumPort; + private int electionPort; + + private ServerAddress(String host) { + this(host, PortAssignment.unique(), PortAssignment.unique()); + + } + + private ServerAddress(String host, int quorumPort, int electionPort) { + this.host = host; + this.quorumPort = quorumPort; + this.electionPort = electionPort; + } + + public String getHost() { + return host; + } + + private void setHost(String host) { + this.host = host; + } + + private void setQuorumPort(int quorumPort) { + this.quorumPort = quorumPort; + } + + private void setElectionPort(int electionPort) { + this.electionPort = electionPort; + } + + @Override + public ServerAddress clone() { + return new ServerAddress(host, quorumPort, electionPort); + } + + @Override + public String toString() { + return String.format("%s:%d:%d", host, quorumPort, electionPort); + } + } +} + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java new file mode 100644 index 0000000..6f5155b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/QuorumServerTest.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import java.net.InetSocketAddress; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.junit.After; +import org.junit.Test; + +public class QuorumServerTest extends ZKTestCase { + + private String ipv6n1 = "[2500:0:0:0:0:0:1:0]"; + private String ipv6n2 = "[2600:0:0:0:0:0:1:0]"; + private String ipv4config = "127.0.0.1:1234:1236"; + + @After + public void tearDown() { + System.clearProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED); + } + + @Test + public void testToString() throws ConfigException { + String provided = ipv4config + ":participant;0.0.0.0:1237"; + String expected = ipv4config + ":participant;0.0.0.0:1237"; + QuorumServer qs = new QuorumServer(0, provided); + assertEquals("Use IP address", expected, qs.toString()); + + provided = ipv4config + ";0.0.0.0:1237"; + expected = ipv4config + ":participant;0.0.0.0:1237"; + qs = new QuorumServer(0, provided); + assertEquals("Type unspecified", expected, qs.toString()); + + provided = ipv4config + ":observer;0.0.0.0:1237"; + expected = ipv4config + ":observer;0.0.0.0:1237"; + qs = new QuorumServer(0, provided); + assertEquals("Observer type", expected, qs.toString()); + + provided = ipv4config + ":participant;1237"; + expected = ipv4config + ":participant;0.0.0.0:1237"; + qs = new QuorumServer(0, provided); + assertEquals("Client address unspecified", expected, qs.toString()); + + provided = ipv4config + ":participant;1.2.3.4:1237"; + expected = ipv4config + ":participant;1.2.3.4:1237"; + qs = new QuorumServer(0, provided); + assertEquals("Client address specified", expected, qs.toString()); + + provided = "example.com:1234:1236:participant;1237"; + expected = "example.com:1234:1236:participant;0.0.0.0:1237"; + qs = new QuorumServer(0, provided); + assertEquals("Use hostname", expected, qs.toString()); + } + + @Test + public void constructionUnderstandsIpv6LiteralsInServerConfig() throws ConfigException { + String config = "[::1]:1234:1236:participant"; + QuorumServer qs = new QuorumServer(0, config); + assertEquals("[0:0:0:0:0:0:0:1]:1234:1236:participant", qs.toString()); + } + + @Test + public void constructionUnderstandsIpv6LiteralsInClientConfig() throws ConfigException { + String config = ipv4config + ":participant;[::1]:1237"; + QuorumServer qs = new QuorumServer(0, config); + assertEquals(ipv4config + ":participant;[0:0:0:0:0:0:0:1]:1237", qs.toString()); + } + + @Test(expected = ConfigException.class) + public void unbalancedIpv6LiteralsInServerConfigFailToBeParsed() throws ConfigException { + new QuorumServer(0, "[::1:1234:1236:participant"); + } + + @Test(expected = ConfigException.class) + public void unbalancedIpv6LiteralsInClientConfigFailToBeParsed() throws ConfigException { + new QuorumServer(0, ipv4config + ":participant;[::1:1237"); + } + + @Test(expected = ConfigException.class) + public void shouldNotAllowMultipleAddressesWhenMultiAddressFeatureIsDisabled() throws ConfigException { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "false"); + new QuorumServer(0, "127.0.0.1:1234:1236|127.0.0.1:2234:2236"); + } + + @Test + public void shouldAllowMultipleAddressesWhenMultiAddressFeatureIsEnabled() throws ConfigException { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + QuorumServer qs = new QuorumServer(0, "127.0.0.1:1234:1236|127.0.0.1:2234:2236"); + assertEquals("MultiAddress parse error", "127.0.0.1:1234:1236|127.0.0.1:2234:2236:participant", qs.toString()); + } + + @Test + public void testWildcard() throws KeeperException.BadArgumentsException { + String[] addrs = new String[]{"127.0.0.1", "[0:0:0:0:0:0:0:1]", "0.0.0.0", "[::]"}; + for (int i = 0; i < addrs.length; i++) { + for (int j = i; j < addrs.length; j++) { + QuorumPeer.QuorumServer server1 = new QuorumPeer.QuorumServer(1, new InetSocketAddress(ipv6n1, 1234), // peer + new InetSocketAddress(ipv6n1, 1236), // election + new InetSocketAddress(addrs[i], 1237) // client + ); + QuorumPeer.QuorumServer server2 = new QuorumPeer.QuorumServer(2, new InetSocketAddress(ipv6n2, 1234), // peer + new InetSocketAddress(ipv6n2, 1236), // election + new InetSocketAddress(addrs[j], 1237) // client + ); + server1.checkAddressDuplicate(server2); + } + } + } + + @Test(expected = KeeperException.BadArgumentsException.class) + public void testDuplicate() throws KeeperException.BadArgumentsException { + QuorumPeer.QuorumServer server1 = new QuorumPeer.QuorumServer(1, new InetSocketAddress(ipv6n1, 1234), // peer + new InetSocketAddress(ipv6n1, 1236), // election + new InetSocketAddress(ipv6n1, 1237) // client + ); + QuorumPeer.QuorumServer server2 = new QuorumPeer.QuorumServer(2, new InetSocketAddress(ipv6n2, 1234), // peer + new InetSocketAddress(ipv6n2, 1236), // election + new InetSocketAddress(ipv6n1, 1237) // client + ); + server1.checkAddressDuplicate(server2); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RaceConditionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RaceConditionTest.java new file mode 100644 index 0000000..38739a1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RaceConditionTest.java @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.net.SocketException; +import java.nio.ByteBuffer; +import javax.security.sasl.SaslException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.server.FinalRequestProcessor; +import org.apache.zookeeper.server.PrepRequestProcessor; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This test class contains test cases related to race condition in complete + * ZooKeeper + */ +public class RaceConditionTest extends QuorumPeerTestBase { + + protected static final Logger LOG = LoggerFactory.getLogger(RaceConditionTest.class); + private static int SERVER_COUNT = 3; + private MainThread[] mt; + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2380. + * Deadlock while shutting down the ZooKeeper + */ + + @Test(timeout = 30000) + public void testRaceConditionBetweenLeaderAndAckRequestProcessor() throws Exception { + mt = startQuorum(); + // get leader + QuorumPeer leader = getLeader(mt); + long oldLeaderCurrentEpoch = leader.getCurrentEpoch(); + assertNotNull("Leader should not be null", leader); + // shutdown 2 followers so that leader does not have majority and goes + // into looking state or following/leading state. + shutdownFollowers(mt); + /** + *

      +         * Verify that there is no deadlock in following ways:
      +         * 1) If leader is in LOOKING or FOLLOWING, we are sure there is no deadlock.
      +         * 2) If leader in in LEADING state then we have to check that this LEADING state is
      +         * after the leader election, not the old LEADING state.
      +         * 
      + */ + boolean leaderStateChanged = ClientBase.waitForServerState(leader, 15000, QuorumStats.Provider.LOOKING_STATE, QuorumStats.Provider.FOLLOWING_STATE); + // Wait for the old leader to start completely + assertTrue( + "Failed to bring up the old leader server", + ClientBase.waitForServerUp("127.0.0.1:" + leader.getClientPort(), CONNECTION_TIMEOUT)); + assertTrue( + "Leader failed to transition to new state. Current state is " + leader.getServerState(), + leaderStateChanged + || (leader.getCurrentEpoch() > oldLeaderCurrentEpoch)); + } + + @After + public void tearDown() { + // stop all severs + if (null != mt) { + for (int i = 0; i < SERVER_COUNT; i++) { + try { + // With the defect, leader hangs here also, but with fix + // it does not + mt[i].shutdown(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted while shutting it down", e); + } + } + } + } + + private MainThread[] startQuorum() throws IOException { + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread[] mt = new MainThread[SERVER_COUNT]; + + // start all the servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false) { + @Override + public TestQPMain getTestQPMain() { + return new MockTestQPMain(); + } + }; + mt[i].start(); + } + + // ensure all servers started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + return mt; + } + + private QuorumPeer getLeader(MainThread[] mt) { + for (int i = mt.length - 1; i >= 0; i--) { + QuorumPeer quorumPeer = mt[i].getQuorumPeer(); + if (quorumPeer != null && ServerState.LEADING == quorumPeer.getPeerState()) { + return quorumPeer; + } + } + return null; + } + + private void shutdownFollowers(MainThread[] mt) { + for (int i = 0; i < mt.length; i++) { + CustomQuorumPeer quorumPeer = (CustomQuorumPeer) mt[i].getQuorumPeer(); + if (quorumPeer != null && ServerState.FOLLOWING == quorumPeer.getPeerState()) { + quorumPeer.setStopPing(true); + } + } + } + + private static class CustomQuorumPeer extends QuorumPeer { + + private boolean stopPing; + + public CustomQuorumPeer() throws SaslException { + } + + public void setStopPing(boolean stopPing) { + this.stopPing = stopPing; + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb())) { + @Override + protected void processPacket(QuorumPacket qp) throws Exception { + if (stopPing && qp.getType() == Leader.PING) { + LOG.info("Follower skipped ping"); + throw new SocketException("Socket time out while sending the ping response"); + } else { + super.processPacket(qp); + } + } + }; + } + + @Override + protected Leader makeLeader(FileTxnSnapLog logFactory) throws IOException, X509Exception { + LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, this, this.getZkDb()) { + @Override + protected void setupRequestProcessors() { + /** + * This method is overridden to make a place to inject + * MockSyncRequestProcessor + */ + RequestProcessor finalProcessor = new FinalRequestProcessor(this); + RequestProcessor toBeAppliedProcessor = new Leader.ToBeAppliedRequestProcessor(finalProcessor, getLeader()); + commitProcessor = new CommitProcessor(toBeAppliedProcessor, Long.toString(getServerId()), false, getZooKeeperServerListener()); + commitProcessor.start(); + ProposalRequestProcessor proposalProcessor = new MockProposalRequestProcessor(this, commitProcessor); + proposalProcessor.initialize(); + prepRequestProcessor = new PrepRequestProcessor(this, proposalProcessor); + prepRequestProcessor.start(); + firstProcessor = new LeaderRequestProcessor(this, prepRequestProcessor); + } + + }; + return new Leader(this, zk); + } + + } + + private static class MockSyncRequestProcessor extends SyncRequestProcessor { + + public MockSyncRequestProcessor(ZooKeeperServer zks, RequestProcessor nextProcessor) { + super(zks, nextProcessor); + } + + @Override + public void shutdown() { + /** + * Add a request so that something is there for SyncRequestProcessor + * to process, while we are in shutdown flow + */ + Request request = new Request(null, 0, 0, ZooDefs.OpCode.delete, ByteBuffer.wrap("/deadLockIssue".getBytes()), null); + processRequest(request); + super.shutdown(); + } + + } + + private static class MockProposalRequestProcessor extends ProposalRequestProcessor { + + public MockProposalRequestProcessor(LeaderZooKeeperServer zks, RequestProcessor nextProcessor) { + super(zks, nextProcessor); + + /** + * The only purpose here is to inject the mocked + * SyncRequestProcessor + */ + AckRequestProcessor ackProcessor = new AckRequestProcessor(zks.getLeader()); + syncProcessor = new MockSyncRequestProcessor(zks, ackProcessor); + } + + } + + private static class MockTestQPMain extends TestQPMain { + + @Override + protected QuorumPeer getQuorumPeer() throws SaslException { + return new CustomQuorumPeer(); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigBackupTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigBackupTest.java new file mode 100644 index 0000000..8efb9d7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigBackupTest.java @@ -0,0 +1,339 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Properties; +import java.util.Scanner; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.common.StringUtils; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.Before; +import org.junit.Test; + +public class ReconfigBackupTest extends QuorumPeerTestBase { + + public static String getVersionFromConfigStr(String config) throws IOException { + Properties props = new Properties(); + props.load(new StringReader(config)); + return props.getProperty("version", ""); + } + + // upgrade this once we have Google-Guava or Java 7+ + public static String getFileContent(File file) throws FileNotFoundException { + Scanner sc = new Scanner(file); + StringBuilder sb = new StringBuilder(); + while (sc.hasNextLine()) { + sb.append(sc.nextLine() + "\n"); + } + return sb.toString(); + } + + @Before + public void setup() { + ClientBase.setupTestEnv(); + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + } + + /** + * This test checks that it will backup static file on bootup. + */ + @Test + public void testBackupStatic() throws Exception { + final int SERVER_COUNT = 3; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts[i]; + sb.append(server + "\n"); + } + + String currentQuorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + String[] staticFileContent = new String[SERVER_COUNT]; + String[] staticBackupContent = new String[SERVER_COUNT]; + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + // check that a dynamic configuration file doesn't exist + assertNull("static file backup shouldn't exist before bootup", mt[i].getFileByName("zoo.cfg.bak")); + staticFileContent[i] = getFileContent(mt[i].confFile); + mt[i].start(); + } + + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + File backupFile = mt[i].getFileByName("zoo.cfg.bak"); + assertNotNull("static file backup should exist", backupFile); + staticBackupContent[i] = getFileContent(backupFile); + assertEquals(staticFileContent[i], staticBackupContent[i]); + } + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + + /** + * This test checks that on reconfig, a new dynamic file will be created with + * current version appended to file name. Meanwhile, the dynamic file pointer + * in static config file should also be changed. + */ + @Test + public void testReconfigCreateNewVersionFile() throws Exception { + final int SERVER_COUNT = 3; + final int NEW_SERVER_COUNT = 5; + + final int[] clientPorts = new int[NEW_SERVER_COUNT]; + final int[] quorumPorts = new int[NEW_SERVER_COUNT]; + final int[] electionPorts = new int[NEW_SERVER_COUNT]; + final String[] servers = new String[NEW_SERVER_COUNT]; + + StringBuilder sb = new StringBuilder(); + ArrayList oldServers = new ArrayList(); + ArrayList newServers = new ArrayList(); + + for (int i = 0; i < NEW_SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + quorumPorts[i] = PortAssignment.unique(); + electionPorts[i] = PortAssignment.unique(); + servers[i] = "server." + i + "=localhost:" + quorumPorts[i] + ":" + electionPorts[i] + ":participant;localhost:" + clientPorts[i]; + + newServers.add(servers[i]); + + if (i >= SERVER_COUNT) { + continue; + } + oldServers.add(servers[i]); + sb.append(servers[i] + "\n"); + } + + String quorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[NEW_SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[NEW_SERVER_COUNT]; + ZooKeeperAdmin[] zkAdmin = new ZooKeeperAdmin[NEW_SERVER_COUNT]; + + // start old cluster + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection, "reconfigEnabled=true\n"); + mt[i].start(); + } + + String firstVersion = null, secondVersion = null; + + // test old cluster + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); + zkAdmin[i] = new ZooKeeperAdmin("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + zkAdmin[i].addAuthInfo("digest", "super:test".getBytes()); + + Properties cfg = ReconfigLegacyTest.readPropertiesFromFile(mt[i].confFile); + String filename = cfg.getProperty("dynamicConfigFile", ""); + + String version = QuorumPeerConfig.getVersionFromFilename(filename); + assertNotNull(version); + + String configStr = ReconfigTest.testServerHasConfig(zk[i], oldServers, null); + + String configVersion = getVersionFromConfigStr(configStr); + // the version appended to filename should be the same as + // the one of quorum verifier. + assertEquals(version, configVersion); + + if (i == 0) { + firstVersion = version; + } else { + assertEquals(firstVersion, version); + } + } + + ReconfigTest.reconfig(zkAdmin[1], null, null, newServers, -1); + + // start additional new servers + for (int i = SERVER_COUNT; i < NEW_SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection + servers[i]); + mt[i].start(); + } + + // wait for new servers to be up running + for (int i = SERVER_COUNT; i < NEW_SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); + } + + // test that all servers have: + // a different, larger version dynamic file + for (int i = 0; i < NEW_SERVER_COUNT; i++) { + Properties cfg = ReconfigLegacyTest.readPropertiesFromFile(mt[i].confFile); + String filename = cfg.getProperty("dynamicConfigFile", ""); + + String version = QuorumPeerConfig.getVersionFromFilename(filename); + assertNotNull(version); + + String configStr = ReconfigTest.testServerHasConfig(zk[i], newServers, null); + + String quorumVersion = getVersionFromConfigStr(configStr); + assertEquals(version, quorumVersion); + + if (i == 0) { + secondVersion = version; + assertTrue(Long.parseLong(secondVersion, 16) > Long.parseLong(firstVersion, 16)); + } else { + assertEquals(secondVersion, version); + } + } + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + zk[i].close(); + zkAdmin[i].close(); + } + } + + /** + * This test checks that if a version is appended to dynamic file, + * then peer should use that version as quorum config version. + *

      + * The scenario: one server has an older version of 3 servers, and + * four others have newer version of 5 servers. Finally, the lag-off one + * should have server config of 5 servers. + */ + @Test + public void testVersionOfDynamicFilename() throws Exception { + final int SERVER_COUNT = 5; + final int oldServerCount = 3; + final int lagOffServerId = 0; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + StringBuilder oldSb = new StringBuilder(); + ArrayList allServers = new ArrayList(); + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts[i]; + sb.append(server + "\n"); + allServers.add(server); + + if (i < oldServerCount) { + // only take in the first 3 servers as old quorum config. + oldSb.append(server + "\n"); + } + } + + String currentQuorumCfgSection = sb.toString(); + + String oldQuorumCfg = oldSb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + + for (int i = 0; i < SERVER_COUNT; i++) { + if (i == lagOffServerId) { + mt[i] = new MainThread(i, clientPorts[i], oldQuorumCfg, true, "100000000"); + } else { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, true, "200000000"); + } + + // before connecting to quorum, servers should have set up dynamic file + // version and pointer. And the lag-off server is using the older + // version dynamic file. + if (i == lagOffServerId) { + assertNotNull(mt[i].getFileByName("zoo.cfg.dynamic.100000000")); + assertNull(mt[i].getFileByName("zoo.cfg.dynamic.200000000")); + assertTrue(mt[i].getPropFromStaticFile("dynamicConfigFile").endsWith(".100000000")); + } else { + assertNotNull(mt[i].getFileByName("zoo.cfg.dynamic.200000000")); + assertTrue(mt[i].getPropFromStaticFile("dynamicConfigFile").endsWith(".200000000")); + } + + mt[i].start(); + } + + String dynamicFileContent = null; + + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); + + // we should see that now all servers have the same config of 5 servers + // including the lag-off server. + String configStr = ReconfigTest.testServerHasConfig(zk, allServers, null); + assertEquals("200000000", getVersionFromConfigStr(configStr)); + + List configLines = Arrays.asList(configStr.split("\n")); + Collections.sort(configLines); + String sortedConfigStr = StringUtils.joinStrings(configLines, "\n"); + + File dynamicConfigFile = mt[i].getFileByName("zoo.cfg.dynamic.200000000"); + assertNotNull(dynamicConfigFile); + + // All dynamic files created with the same version should have + // same configs, and they should be equal to the config we get from QuorumPeer. + if (i == 0) { + dynamicFileContent = getFileContent(dynamicConfigFile); + assertEquals(sortedConfigStr, dynamicFileContent + "version=200000000"); + } else { + String otherDynamicFileContent = getFileContent(dynamicConfigFile); + assertEquals(dynamicFileContent, otherDynamicFileContent); + } + + zk.close(); + } + + // finally, we should also check that the lag-off server has updated + // the dynamic file pointer. + assertTrue(mt[lagOffServerId].getPropFromStaticFile("dynamicConfigFile").endsWith(".200000000")); + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigDuringLeaderSyncTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigDuringLeaderSyncTest.java new file mode 100644 index 0000000..ee51baf --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigDuringLeaderSyncTest.java @@ -0,0 +1,303 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.Arrays; +import java.util.Collection; +import java.util.Map; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.admin.AdminServer.AdminServerException; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(Parameterized.class) +public class ReconfigDuringLeaderSyncTest extends QuorumPeerTestBase { + + private static final Logger LOG = LoggerFactory.getLogger(ReconfigDuringLeaderSyncTest.class); + private static int SERVER_COUNT = 3; + private MainThread[] mt; + private static boolean bakAsyncSending; + + private boolean asyncSending; + + public ReconfigDuringLeaderSyncTest(boolean asyncSending) { + this.asyncSending = asyncSending; + } + + @Parameterized.Parameters + public static Collection sendingModes() { + return Arrays.asList(new Object[][]{{true}, {false}}); + } + + @Before + public void setup() { + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + Learner.setAsyncSending(asyncSending); + QuorumPeerConfig.setReconfigEnabled(true); + } + + @BeforeClass + public static void saveAsyncSendingFlag() { + bakAsyncSending = Learner.getAsyncSending(); + } + + @AfterClass + public static void resetAsyncSendingFlag() { + Learner.setAsyncSending(bakAsyncSending); + } + + /** + *

      +     * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2172.
      +     * Cluster crashes when reconfig a new node as a participant.
      +     * 
      + * + * This issue occurs when reconfig's PROPOSAL and COMMITANDACTIVATE come in + * between the snapshot and the UPTODATE. In this case processReconfig was + * not invoked on the newly added node, and zoo.cfg.dynamic.next wasn't + * deleted. + */ + + @Test + public void testDuringLeaderSync() throws Exception { + final int[] clientPorts = new int[SERVER_COUNT + 1]; + StringBuilder sb = new StringBuilder(); + String[] serverConfig = new String[SERVER_COUNT + 1]; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + serverConfig[i] = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(serverConfig[i] + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + mt = new MainThread[SERVER_COUNT + 1]; + + // start 3 servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // ensure all servers started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + CountdownWatcher watch = new CountdownWatcher(); + ZooKeeperAdmin preReconfigClient = new ZooKeeperAdmin( + "127.0.0.1:" + clientPorts[0], + ClientBase.CONNECTION_TIMEOUT, + watch); + preReconfigClient.addAuthInfo("digest", "super:test".getBytes()); + watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + // new server joining + int joinerId = SERVER_COUNT; + clientPorts[joinerId] = PortAssignment.unique(); + serverConfig[joinerId] = "server." + joinerId + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[joinerId]; + + // Find leader id. + int leaderId = -1; + for (int i = 0; i < SERVER_COUNT; i++) { + if (mt[i].main.quorumPeer.leader != null) { + leaderId = i; + break; + } + } + assertFalse(leaderId == -1); + + // Joiner initial config consists of itself and the leader. + sb = new StringBuilder(); + sb.append(serverConfig[leaderId] + "\n").append(serverConfig[joinerId] + "\n"); + + /** + * This server will delay the response to a NEWLEADER message, and run + * reconfig command so that message at this processed in bellow order + * + *
      +         * NEWLEADER
      +         * reconfig's PROPOSAL
      +         * reconfig's COMMITANDACTIVATE
      +         * UPTODATE
      +         * 
      + */ + mt[joinerId] = new MainThread(joinerId, clientPorts[joinerId], sb.toString(), false) { + @Override + public TestQPMain getTestQPMain() { + return new MockTestQPMain(); + } + }; + mt[joinerId].start(); + CustomQuorumPeer qp = getCustomQuorumPeer(mt[joinerId]); + + // delete any already existing .next file + String nextDynamicConfigFilename = qp.getNextDynamicConfigFilename(); + File nextDynaFile = new File(nextDynamicConfigFilename); + nextDynaFile.delete(); + + // call reconfig API when the new server has received + // Leader.NEWLEADER + while (true) { + if (qp.isNewLeaderMessage()) { + preReconfigClient.reconfigure(serverConfig[joinerId], null, null, -1, null, null); + break; + } else { + // sleep for 10 millisecond and then again check + Thread.sleep(10); + } + } + watch = new CountdownWatcher(); + ZooKeeper postReconfigClient = new ZooKeeper( + "127.0.0.1:" + clientPorts[joinerId], + ClientBase.CONNECTION_TIMEOUT, + watch); + watch.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + // do one successful operation on the newly added node + postReconfigClient.create("/reconfigIssue", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertFalse("zoo.cfg.dynamic.next is not deleted.", nextDynaFile.exists()); + + // verify that joiner has up-to-date config, including all four servers. + for (long j = 0; j <= SERVER_COUNT; j++) { + assertNotNull( + "server " + j + " is not present in the new quorum", + qp.getQuorumVerifier().getVotingMembers().get(j)); + } + + // close clients + preReconfigClient.close(); + postReconfigClient.close(); + } + + private static CustomQuorumPeer getCustomQuorumPeer(MainThread mt) { + while (true) { + QuorumPeer quorumPeer = mt.getQuorumPeer(); + if (null != quorumPeer) { + return (CustomQuorumPeer) quorumPeer; + } else { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + @After + public void tearDown() { + // stop all severs + if (null != mt) { + for (int i = 0; i < mt.length; i++) { + try { + mt[i].shutdown(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted while shutting it down", e); + } + } + } + } + + private static class CustomQuorumPeer extends QuorumPeer { + + private boolean newLeaderMessage = false; + + public CustomQuorumPeer(Map quorumPeers, File snapDir, File logDir, int clientPort, int electionAlg, long myid, int tickTime, int initLimit, int syncLimit, int connectToLearnerMasterLimit) throws IOException { + super(quorumPeers, snapDir, logDir, electionAlg, myid, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, false, ServerCnxnFactory.createFactory(new InetSocketAddress(clientPort), -1), new QuorumMaj(quorumPeers)); + } + + /** + * If true, after 100 millisecond NEWLEADER response is send to leader + * + * @return + */ + public boolean isNewLeaderMessage() { + return newLeaderMessage; + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb())) { + + @Override + void writePacket(QuorumPacket pp, boolean flush) throws IOException { + if (pp != null && pp.getType() == Leader.ACK) { + newLeaderMessage = true; + try { + /** + * Delaying the ACK message, a follower sends as + * response to a NEWLEADER message, so that the + * leader has a chance to send the reconfig and only + * then the UPTODATE message. + */ + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + super.writePacket(pp, flush); + } + }; + } + + } + + private static class MockTestQPMain extends TestQPMain { + + @Override + public void runFromConfig(QuorumPeerConfig config) throws IOException, AdminServerException { + quorumPeer = new CustomQuorumPeer(config.getQuorumVerifier().getAllMembers(), config.getDataDir(), config.getDataLogDir(), config.getClientPortAddress().getPort(), config.getElectionAlg(), config.getServerId(), config.getTickTime(), config.getInitLimit(), config.getSyncLimit(), config.getConnectToLearnerMasterLimit()); + quorumPeer.setConfigFileName(config.getConfigFilename()); + quorumPeer.start(); + try { + quorumPeer.join(); + } catch (InterruptedException e) { + LOG.warn("Quorum Peer interrupted", e); + } + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigFailureCasesTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigFailureCasesTest.java new file mode 100644 index 0000000..a10e9c7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigFailureCasesTest.java @@ -0,0 +1,241 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.NewConfigNoQuorum; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.QuorumUtil; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ReconfigFailureCasesTest extends QuorumPeerTestBase { + + private QuorumUtil qu; + + @Before + public void setup() { + QuorumPeerConfig.setReconfigEnabled(true); + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + } + + @After + public void tearDown() throws Exception { + if (qu != null) { + qu.tearDown(); + } + } + + /* + * Tests that an incremental reconfig fails if the current config is hiearchical. + */ + @Test + public void testIncrementalReconfigInvokedOnHiearchicalQS() throws Exception { + qu = new QuorumUtil(2); // create 5 servers + qu.disableJMXTest = true; + qu.startAll(); + ZooKeeper[] zkArr = ReconfigTest.createHandles(qu); + ZooKeeperAdmin[] zkAdminArr = ReconfigTest.createAdminHandles(qu); + + ArrayList members = new ArrayList(); + members.add("group.1=3:4:5"); + members.add("group.2=1:2"); + members.add("weight.1=0"); + members.add("weight.2=0"); + members.add("weight.3=1"); + members.add("weight.4=1"); + members.add("weight.5=1"); + + for (int i = 1; i <= 5; i++) { + members.add("server." + i + "=127.0.0.1:" + + qu.getPeer(i).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + qu.getPeer(i).peer.getElectionAddress().getAllPorts().get(0) + + ";" + "127.0.0.1:" + + qu.getPeer(i).peer.getClientPort()); + } + + // Change the quorum system from majority to hierarchical. + ReconfigTest.reconfig(zkAdminArr[1], null, null, members, -1); + ReconfigTest.testNormalOperation(zkArr[1], zkArr[2]); + + // Attempt an incremental reconfig. + List leavingServers = new ArrayList(); + leavingServers.add("3"); + try { + zkAdminArr[1].reconfigure(null, leavingServers, null, -1, null); + fail("Reconfig should have failed since the current config isn't Majority QS"); + } catch (KeeperException.BadArgumentsException e) { + // We expect this to happen. + } catch (Exception e) { + fail("Should have been BadArgumentsException!"); + } + + ReconfigTest.closeAllHandles(zkArr, zkAdminArr); + } + + /* + * Test that a reconfiguration fails if the proposed change would leave the + * cluster with less than 2 participants (StandaloneEnabled = true). + * StandaloneDisabledTest.java (startSingleServerTest) checks that if + * StandaloneEnabled = false its legal to remove all but one remaining + * server. + */ + @Test + public void testTooFewRemainingPariticipants() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + ZooKeeper[] zkArr = ReconfigTest.createHandles(qu); + ZooKeeperAdmin[] zkAdminArr = ReconfigTest.createAdminHandles(qu); + + List leavingServers = new ArrayList(); + leavingServers.add("2"); + leavingServers.add("3"); + try { + zkAdminArr[1].reconfigure(null, leavingServers, null, -1, null); + fail("Reconfig should have failed since the current config version is not 8"); + } catch (KeeperException.BadArgumentsException e) { + // We expect this to happen. + } catch (Exception e) { + fail("Should have been BadArgumentsException!"); + } + + ReconfigTest.closeAllHandles(zkArr, zkAdminArr); + } + + /* + * Tests that a conditional reconfig fails if the specified version doesn't correspond + * to the version of the current config. + */ + @Test + public void testReconfigVersionConditionFails() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + ZooKeeper[] zkArr = ReconfigTest.createHandles(qu); + ZooKeeperAdmin[] zkAdminArr = ReconfigTest.createAdminHandles(qu); + + List leavingServers = new ArrayList(); + leavingServers.add("3"); + try { + zkAdminArr[1].reconfigure(null, leavingServers, null, 8, null); + fail("Reconfig should have failed since the current config version is not 8"); + } catch (KeeperException.BadVersionException e) { + // We expect this to happen. + } catch (Exception e) { + fail("Should have been BadVersionException!"); + } + + ReconfigTest.closeAllHandles(zkArr, zkAdminArr); + } + + /* + * Converting an observer into a participant may sometimes fail with a + * NewConfigNoQuorum exception. This test-case demonstrates the scenario. + * Current configuration is (A, B, C, D), where A, B and C are participant + * and D is an observer. Suppose that B has crashed (or never booted). If a + * reconfiguration is submitted where D is said to become a participant, it + * will fail with NewConfigNoQuorum since in this configuration, a majority + * of voters in the new configuration (any 3 voters), must be connected and + * up-to-date with the leader. An observer cannot acknowledge the history + * prefix sent during reconfiguration, and therefore it does not count towards + * these 3 required servers and the reconfiguration will be aborted. In case + * this happens, a client can achieve the same task by two reconfig commands: + * first invoke a reconfig to remove D from the configuration and then invoke a + * second command to add it back as a participant (follower). During the + * intermediate state D is a non-voting follower and can ACK the state + * transfer performed during the second reconfig command. + */ + @Test + public void testObserverToParticipantConversionFails() throws Exception { + ClientBase.setupTestEnv(); + + final int SERVER_COUNT = 4; + int[][] ports = ReconfigRecoveryTest.generatePorts(SERVER_COUNT); + + // generate old config string + Set observers = new HashSet(); + observers.add(3); + StringBuilder sb = ReconfigRecoveryTest.generateConfig(SERVER_COUNT, ports, observers); + String currentQuorumCfgSection = sb.toString(); + String nextQuorumCfgSection = currentQuorumCfgSection.replace("observer", "participant"); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + ZooKeeperAdmin[] zkAdmin = new ZooKeeperAdmin[SERVER_COUNT]; + + // Server 0 stays down + for (int i = 1; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, ports[i][2], currentQuorumCfgSection, true, "100000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + ports[i][2], ClientBase.CONNECTION_TIMEOUT, this); + zkAdmin[i] = new ZooKeeperAdmin("127.0.0.1:" + ports[i][2], ClientBase.CONNECTION_TIMEOUT, this); + zkAdmin[i].addAuthInfo("digest", "super:test".getBytes()); + } + + for (int i = 1; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + ports[i][2], CONNECTION_TIMEOUT * 2)); + } + + try { + zkAdmin[1].reconfigure("", "", nextQuorumCfgSection, -1, new Stat()); + fail("Reconfig should have failed with NewConfigNoQuorum"); + } catch (NewConfigNoQuorum e) { + // This is expected case since server 0 is down and 3 can't vote + // (observer in current role) and we need 3 votes from 0, 1, 2, 3, + } catch (Exception e) { + fail("Reconfig should have failed with NewConfigNoQuorum"); + } + // In this scenario to change 3's role to participant we need to remove it first + ArrayList leavingServers = new ArrayList(); + leavingServers.add("3"); + ReconfigTest.reconfig(zkAdmin[1], null, leavingServers, null, -1); + ReconfigTest.testNormalOperation(zk[2], zk[3]); + ReconfigTest.testServerHasConfig(zk[3], null, leavingServers); + + // Now we're adding it back as a participant and everything should work. + List newMembers = Arrays.asList(nextQuorumCfgSection.split("\n")); + ReconfigTest.reconfig(zkAdmin[1], null, null, newMembers, -1); + ReconfigTest.testNormalOperation(zk[2], zk[3]); + for (int i = 1; i < SERVER_COUNT; i++) { + ReconfigTest.testServerHasConfig(zk[i], newMembers, null); + } + for (int i = 1; i < SERVER_COUNT; i++) { + zk[i].close(); + zkAdmin[i].close(); + mt[i].shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java new file mode 100644 index 0000000..d6eba89 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigLegacyTest.java @@ -0,0 +1,305 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Properties; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.Before; +import org.junit.Test; + +public class ReconfigLegacyTest extends QuorumPeerTestBase { + + private static final int SERVER_COUNT = 3; + + @Before + public void setup() { + ClientBase.setupTestEnv(); + QuorumPeerConfig.setReconfigEnabled(true); + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + } + + /** + * This test checks that when started with a single static config file the + * servers will create a valid dynamic config file. Also checks that when + * the static config includes a clientPort but the dynamic definition also + * includes it, the static definition is erased. + */ + @Test + public void testConfigFileBackwardCompatibility() throws Exception { + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + ArrayList allServers = new ArrayList(); + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts[i]; + allServers.add(server); + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + + // Start the servers with a static config file, without a dynamic + // config file. + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, "participant", false); + // check that a dynamic configuration file doesn't exist + assertEquals(mt[i].getDynamicFiles().length, 0); + mt[i].start(); + } + // Check that the servers are up, have the right config and can process operations. + // Check that the static config was split into static and dynamic files correctly. + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); + File[] dynamicFiles = mt[i].getDynamicFiles(); + + assertTrue(dynamicFiles.length == 1); + ReconfigTest.testServerHasConfig(zk[i], allServers, null); + // check that static config file doesn't include membership info + // and has a pointer to the dynamic configuration file + // check that static config file doesn't include peerType info + Properties cfg = readPropertiesFromFile(mt[i].confFile); + for (int j = 0; j < SERVER_COUNT; j++) { + assertFalse(cfg.containsKey("server." + j)); + } + assertFalse(cfg.containsKey("peerType")); + assertTrue(cfg.containsKey("dynamicConfigFile")); + assertFalse(cfg.containsKey("clientPort")); + + // check that the dynamic configuration file contains the membership info + cfg = readPropertiesFromFile(dynamicFiles[0]); + for (int j = 0; j < SERVER_COUNT; j++) { + String serverLine = cfg.getProperty("server." + j, ""); + assertEquals(allServers.get(j), "server." + j + "=" + serverLine); + } + assertFalse(cfg.containsKey("dynamicConfigFile")); + } + ReconfigTest.testNormalOperation(zk[0], zk[1]); + + // now shut down the servers and restart them + for (int i = 0; i < SERVER_COUNT; i++) { + zk[i].close(); + mt[i].shutdown(); + } + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].start(); + } + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); + ReconfigTest.testServerHasConfig(zk[i], allServers, null); + } + ReconfigTest.testNormalOperation(zk[0], zk[1]); + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + zk[i].close(); + } + } + + /** + * https://issues.apache.org/jira/browse/ZOOKEEPER-1992 + * 1. When a server starts from old style static config, without a client port in the server + * specification, it should keep the client port in static config file. + * 2. After port reconfig, the old port should be removed from static file + * and new port added to dynamic file. + * @throws Exception + */ + @Test + public void testReconfigRemoveClientFromStatic() throws Exception { + final int[] clientPorts = new int[SERVER_COUNT]; + final int[] quorumPorts = new int[SERVER_COUNT]; + final int[] electionPorts = new int[SERVER_COUNT]; + + final int changedServerId = 0; + final int newClientPort = PortAssignment.unique(); + + StringBuilder sb = new StringBuilder(); + ArrayList allServers = new ArrayList(); + ArrayList newServers = new ArrayList(); + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + quorumPorts[i] = PortAssignment.unique(); + electionPorts[i] = PortAssignment.unique(); + + String server = "server." + i + "=localhost:" + quorumPorts[i] + ":" + electionPorts[i] + ":participant"; + allServers.add(server); + sb.append(server + "\n"); + + if (i == changedServerId) { + newServers.add(server + ";0.0.0.0:" + newClientPort); + } else { + newServers.add(server); + } + } + String quorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + ZooKeeperAdmin[] zkAdmin = new ZooKeeperAdmin[SERVER_COUNT]; + + // Start the servers with a static config file, without a dynamic config file. + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection, false); + mt[i].start(); + } + + // Check that when a server starts from old style config, it should keep the client + // port in static config file. + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]); + zkAdmin[i] = new ZooKeeperAdmin("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + zkAdmin[i].addAuthInfo("digest", "super:test".getBytes()); + + ReconfigTest.testServerHasConfig(zk[i], allServers, null); + Properties cfg = readPropertiesFromFile(mt[i].confFile); + + assertTrue(cfg.containsKey("dynamicConfigFile")); + assertTrue(cfg.containsKey("clientPort")); + } + ReconfigTest.testNormalOperation(zk[0], zk[1]); + + ReconfigTest.reconfig(zkAdmin[1], null, null, newServers, -1); + ReconfigTest.testNormalOperation(zk[0], zk[1]); + + // Sleep since writing the config files may take time. + Thread.sleep(1000); + + // Check that new dynamic config includes the updated client port. + // Check that server changedServerId erased clientPort from static config. + // Check that other servers still have clientPort in static config. + + for (int i = 0; i < SERVER_COUNT; i++) { + ReconfigTest.testServerHasConfig(zk[i], newServers, null); + Properties staticCfg = readPropertiesFromFile(mt[i].confFile); + if (i == changedServerId) { + assertFalse(staticCfg.containsKey("clientPort")); + } else { + assertTrue(staticCfg.containsKey("clientPort")); + } + } + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + zk[i].close(); + zkAdmin[i].close(); + } + } + + public static Properties readPropertiesFromFile(File file) throws IOException { + Properties cfg = new Properties(); + FileInputStream in = new FileInputStream(file); + try { + cfg.load(in); + } finally { + in.close(); + } + return cfg; + } + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2244 + * + * @throws Exception + */ + @Test(timeout = 120000) + public void testRestartZooKeeperServer() throws Exception { + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread[] mt = new MainThread[SERVER_COUNT]; + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // ensure server started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + clientPorts[0]); + + String zNodePath = "/serverRestartTest"; + String data = "originalData"; + zk.create(zNodePath, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + + /** + * stop two servers out of three and again start them + */ + mt[0].shutdown(); + mt[1].shutdown(); + mt[0].start(); + mt[1].start(); + // ensure server started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + zk = ClientBase.createZKClient("127.0.0.1:" + clientPorts[0]); + + byte[] dataBytes = zk.getData(zNodePath, null, null); + String receivedData = new String(dataBytes); + assertEquals(data, receivedData); + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigRecoveryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigRecoveryTest.java new file mode 100644 index 0000000..ee0fb4a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigRecoveryTest.java @@ -0,0 +1,564 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.Before; +import org.junit.Test; + +public class ReconfigRecoveryTest extends QuorumPeerTestBase { + + @Before + public void setup() { + QuorumPeerConfig.setReconfigEnabled(true); + } + + /** + * Reconfiguration recovery - test that a reconfiguration is completed if + * leader has .next file during startup and new config is not running yet + */ + @Test + public void testNextConfigCompletion() throws Exception { + ClientBase.setupTestEnv(); + + // 2 servers in current config, 3 in next config + final int SERVER_COUNT = 3; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + ArrayList allServers = new ArrayList(); + + String currentQuorumCfgSection = null, nextQuorumCfgSection; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts[i]; + allServers.add(server); + sb.append(server + "\n"); + if (i == 1) { + currentQuorumCfgSection = sb.toString(); + } + } + nextQuorumCfgSection = sb.toString(); + + // Both servers 0 and 1 will have the .next config file, which means + // for them that a reconfiguration was in progress when they failed + // and the leader will complete it + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + for (int i = 0; i < SERVER_COUNT - 1; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, true, "100000000"); + // note that we should run the server, shut it down and only then + // simulate a reconfig in progress by writing the temp file, but here no + // other server is competing with them in FLE, so we can skip this step + // (server 2 is booted after FLE ends) + mt[i].writeTempDynamicConfigFile(nextQuorumCfgSection, "200000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + assertTrue( + "waiting for server 0 being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[0], CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 1 being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[1], CONNECTION_TIMEOUT)); + + int leader = mt[0].main.quorumPeer.leader == null ? 1 : 0; + + // the new server's config is going to include itself and the current leader + sb = new StringBuilder(); + sb.append(allServers.get(leader) + "\n"); + sb.append(allServers.get(2) + "\n"); + + // suppose that this new server never heard about the reconfig proposal + String newServerInitialConfig = sb.toString(); + mt[2] = new MainThread(2, clientPorts[2], newServerInitialConfig); + mt[2].start(); + zk[2] = new ZooKeeper("127.0.0.1:" + clientPorts[2], ClientBase.CONNECTION_TIMEOUT, this); + + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[2], CONNECTION_TIMEOUT)); + + ReconfigTest.testServerHasConfig(zk[0], allServers, null); + ReconfigTest.testServerHasConfig(zk[1], allServers, null); + ReconfigTest.testServerHasConfig(zk[2], allServers, null); + + ReconfigTest.testNormalOperation(zk[0], zk[2]); + ReconfigTest.testNormalOperation(zk[2], zk[1]); + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + zk[i].close(); + } + } + + /** + * Reconfiguration recovery - current config servers discover .next file, + * but they're both observers and their ports change in next config. Suppose + * that next config wasn't activated yet. Should complete reconfiguration. + */ + @Test + public void testCurrentServersAreObserversInNextConfig() throws Exception { + ClientBase.setupTestEnv(); + + // 2 servers in current config, 5 in next config + final int SERVER_COUNT = 5; + final int[] clientPorts = new int[SERVER_COUNT]; + final int[] oldClientPorts = new int[2]; + StringBuilder sb = new StringBuilder(); + String server; + + String currentQuorumCfg, nextQuorumCfgSection; + + ArrayList allServersNext = new ArrayList(); + + for (int i = 0; i < 2; i++) { + oldClientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + oldClientPorts[i]; + sb.append(server + "\n"); + } + + currentQuorumCfg = sb.toString(); + + sb = new StringBuilder(); + String role; + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + if (i < 2) { + role = "observer"; + } else { + role = "participant"; + } + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":" + + role + ";localhost:" + clientPorts[i]; + allServersNext.add(server); + sb.append(server + "\n"); + } + nextQuorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + + // run servers 0 and 1 normally + for (int i = 0; i < 2; i++) { + mt[i] = new MainThread(i, oldClientPorts[i], currentQuorumCfg, true, "100000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + oldClientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + for (int i = 0; i < 2; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + oldClientPorts[i], CONNECTION_TIMEOUT + * 2)); + } + + ReconfigTest.testNormalOperation(zk[0], zk[1]); + + // shut them down and then simulate a reboot with a reconfig in progress + for (int i = 0; i < 2; i++) { + mt[i].shutdown(); + zk[i].close(); + } + + for (int i = 0; i < 2; i++) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerDown( + "127.0.0.1:" + + oldClientPorts[i], CONNECTION_TIMEOUT + * 2)); + } + + for (int i = 0; i < 2; i++) { + mt[i].writeTempDynamicConfigFile(nextQuorumCfgSection, "200000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + // new members are initialized with current config + the new server + for (int i = 2; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfg + allServersNext.get(i)); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT * 2)); + ReconfigTest.testServerHasConfig(zk[i], allServersNext, null); + } + + ReconfigTest.testNormalOperation(zk[0], zk[2]); + ReconfigTest.testNormalOperation(zk[4], zk[1]); + + for (int i = 0; i < SERVER_COUNT; i++) { + zk[i].close(); + mt[i].shutdown(); + } + } + + /** + * Reconfiguration recovery - test that if servers in old config have a + * .next file but no quorum of new config is up then no progress should be + * possible (no progress will happen to ensure safety as the new config + * might be actually up but partitioned from old config) + */ + @Test + public void testNextConfigUnreachable() throws Exception { + ClientBase.setupTestEnv(); + + // 2 servers in current config, 5 in next config + final int SERVER_COUNT = 5; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + String currentQuorumCfgSection = null, nextQuorumCfgSection; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts[i]; + sb.append(server + "\n"); + if (i == 1) { + currentQuorumCfgSection = sb.toString(); + } + } + nextQuorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + + // Both servers 0 and 1 will have the .next config file, which means + // for them that a reconfiguration was in progress when they failed + for (int i = 0; i < 2; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, true, "100000000"); + // note that we should run the server, shut it down and only then + // simulate a reconfig in progress by writing the temp file, but here no + // other server is competing with them in FLE, so we can skip this step + mt[i].writeTempDynamicConfigFile(nextQuorumCfgSection, "200000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + Thread.sleep(CONNECTION_TIMEOUT * 2); + + // make sure servers 0, 1 don't come online - this should be the case + // since they can't complete the reconfig + for (int i = 0; i < 2; i++) { + assertFalse( + "server " + i + " is up but shouldn't be", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT / 10)); + } + + for (int i = 0; i < 2; i++) { + zk[i].close(); + mt[i].shutdown(); + } + } + + /** + * Reconfiguration recovery - test that old config members will join the new + * config if its already active, and not try to complete the reconfiguration + */ + @Test + public void testNextConfigAlreadyActive() throws Exception { + ClientBase.setupTestEnv(); + + // 2 servers in current config, 5 in next config + final int SERVER_COUNT = 5; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + String currentQuorumCfgSection = null, nextQuorumCfgSection; + + ArrayList allServers = new ArrayList(); + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts[i]; + allServers.add(server); + sb.append(server + "\n"); + if (i == 1) { + currentQuorumCfgSection = sb.toString(); + } + } + nextQuorumCfgSection = sb.toString(); + + // lets start servers 2, 3, 4 with the new config + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + for (int i = 2; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], nextQuorumCfgSection, true, "200000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + for (int i = 2; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + ReconfigTest.testNormalOperation(zk[2], zk[3]); + + long epoch = mt[2].main.quorumPeer.getAcceptedEpoch(); + + // Both servers 0 and 1 will have the .next config file, which means + // for them that a reconfiguration was in progress when they failed + // and the leader will complete it. + for (int i = 0; i < 2; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, true, "100000000"); + mt[i].writeTempDynamicConfigFile(nextQuorumCfgSection, "200000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this); + } + + // servers 0 and 1 should connect to all servers, including the one in + // their .next file during startup, and will find the next config and join it + for (int i = 0; i < 2; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT * 2)); + } + + // make sure they joined the new config without any change to it + assertEquals(epoch, mt[0].main.quorumPeer.getAcceptedEpoch()); + assertEquals(epoch, mt[1].main.quorumPeer.getAcceptedEpoch()); + assertEquals(epoch, mt[2].main.quorumPeer.getAcceptedEpoch()); + + ReconfigTest.testServerHasConfig(zk[0], allServers, null); + ReconfigTest.testServerHasConfig(zk[1], allServers, null); + + ReconfigTest.testNormalOperation(zk[0], zk[2]); + ReconfigTest.testNormalOperation(zk[4], zk[1]); + + for (int i = 0; i < SERVER_COUNT; i++) { + zk[i].close(); + mt[i].shutdown(); + } + } + + /** + * Tests conversion of observer to participant AFTER new config was already + * committed. Old config: servers 0 (participant), 1 (participant), 2 + * (observer) New config: servers 2 (participant), 3 (participant) We start + * server 2 with old config and start server 3 with new config. All other + * servers are down. In order to terminate FLE, server 3 must 'convince' + * server 2 to adopt the new config and turn into a participant. + */ + @Test + public void testObserverConvertedToParticipantDuringFLE() throws Exception { + ClientBase.setupTestEnv(); + + final int SERVER_COUNT = 4; + int[][] ports = generatePorts(SERVER_COUNT); + String currentQuorumCfgSection, nextQuorumCfgSection; + + // generate old config string + Set observers = new HashSet(); + observers.add(2); + StringBuilder sb = generateConfig(3, ports, observers); + currentQuorumCfgSection = sb.toString(); + + // generate new config string + ArrayList allServersNext = new ArrayList(); + sb = new StringBuilder(); + for (int i = 2; i < SERVER_COUNT; i++) { + String server = "server." + i + "=localhost:" + ports[i][0] + ":" + ports[i][1] + + ":participant;localhost:" + ports[i][2]; + allServersNext.add(server); + sb.append(server + "\n"); + } + nextQuorumCfgSection = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + + // start server 2 with old config, where it is an observer + mt[2] = new MainThread(2, ports[2][2], currentQuorumCfgSection, true, "100000000"); + mt[2].start(); + zk[2] = new ZooKeeper("127.0.0.1:" + ports[2][2], ClientBase.CONNECTION_TIMEOUT, this); + + // start server 3 with new config + mt[3] = new MainThread(3, ports[3][2], nextQuorumCfgSection, true, "200000000"); + mt[3].start(); + zk[3] = new ZooKeeper("127.0.0.1:" + ports[3][2], ClientBase.CONNECTION_TIMEOUT, this); + + for (int i = 2; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + ports[i][2], CONNECTION_TIMEOUT * 2)); + ReconfigTest.testServerHasConfig(zk[i], allServersNext, null); + } + + assertEquals( + nextQuorumCfgSection + "version=200000000", + ReconfigTest.testServerHasConfig(zk[2], null, null)); + assertEquals( + nextQuorumCfgSection + "version=200000000", + ReconfigTest.testServerHasConfig(zk[3], null, null)); + ReconfigTest.testNormalOperation(zk[2], zk[2]); + ReconfigTest.testNormalOperation(zk[3], zk[2]); + + for (int i = 2; i < SERVER_COUNT; i++) { + zk[i].close(); + mt[i].shutdown(); + } + } + + /** + * Tests conversion of observer to participant during reconfig recovery, new + * config was not committed yet. Old config: servers 0 (participant), 1 + * (participant), 2 (observer) New config: servers 2 (participant), 3 + * (participant) We start server servers 0, 1, 2 with old config and a .next + * file indicating a reconfig in progress. We start server 3 with old config + * + itself in config file. In this scenario server 2 can't be converted to + * participant during reconfig since we don't gossip about proposed + * configurations, only about committed ones. This tests that new config can + * be completed, which requires server 2's ack for the newleader message, + * even though its an observer. + */ + @Test + public void testCurrentObserverIsParticipantInNewConfig() throws Exception { + ClientBase.setupTestEnv(); + + final int SERVER_COUNT = 4; + int[][] ports = generatePorts(SERVER_COUNT); + String currentQuorumCfg, nextQuorumCfgSection; + + // generate old config string + Set observers = new HashSet(); + observers.add(2); + + StringBuilder sb = generateConfig(3, ports, observers); + currentQuorumCfg = sb.toString(); + + // Run servers 0..2 for a while + MainThread[] mt = new MainThread[SERVER_COUNT]; + ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT]; + for (int i = 0; i <= 2; i++) { + mt[i] = new MainThread(i, ports[i][2], currentQuorumCfg, true, "100000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + ports[i][2], ClientBase.CONNECTION_TIMEOUT, this); + } + + ReconfigTest.testNormalOperation(zk[0], zk[2]); + + for (int i = 0; i <= 2; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + ports[i][2], CONNECTION_TIMEOUT * 2)); + } + + // shut servers 0..2 down + for (int i = 0; i <= 2; i++) { + mt[i].shutdown(); + zk[i].close(); + } + + // generate new config string + ArrayList allServersNext = new ArrayList(); + sb = new StringBuilder(); + for (int i = 2; i < SERVER_COUNT; i++) { + String server = "server." + i + "=localhost:" + ports[i][0] + ":" + ports[i][1] + + ":participant;localhost:" + ports[i][2]; + allServersNext.add(server); + sb.append(server + "\n"); + } + nextQuorumCfgSection = sb.toString(); + + // simulate reconfig in progress - servers 0..2 have a temp reconfig + // file when they boot + for (int i = 0; i <= 2; i++) { + mt[i].writeTempDynamicConfigFile(nextQuorumCfgSection, "200000000"); + mt[i].start(); + zk[i] = new ZooKeeper("127.0.0.1:" + ports[i][2], ClientBase.CONNECTION_TIMEOUT, this); + } + // new server 3 has still its invalid joiner config - everyone in old + // config + itself + mt[3] = new MainThread(3, ports[3][2], currentQuorumCfg + allServersNext.get(1)); + mt[3].start(); + zk[3] = new ZooKeeper("127.0.0.1:" + ports[3][2], ClientBase.CONNECTION_TIMEOUT, this); + + for (int i = 2; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + ports[i][2], CONNECTION_TIMEOUT * 3)); + ReconfigTest.testServerHasConfig(zk[i], allServersNext, null); + } + + ReconfigTest.testNormalOperation(zk[0], zk[2]); + ReconfigTest.testNormalOperation(zk[3], zk[1]); + assertEquals( + nextQuorumCfgSection + "version=200000000", + ReconfigTest.testServerHasConfig(zk[2], null, null)); + assertEquals( + nextQuorumCfgSection + "version=200000000", + ReconfigTest.testServerHasConfig(zk[3], null, null)); + + for (int i = 0; i < SERVER_COUNT; i++) { + zk[i].close(); + mt[i].shutdown(); + } + } + + /* + * Generates 3 ports per server + */ + public static int[][] generatePorts(int numServers) { + int[][] ports = new int[numServers][]; + for (int i = 0; i < numServers; i++) { + ports[i] = new int[3]; + for (int j = 0; j < 3; j++) { + ports[i][j] = PortAssignment.unique(); + } + } + return ports; + } + + /* + * Creates a configuration string for servers 0..numServers-1 Ids in + * observerIds correspond to observers, other ids are for participants. + */ + public static StringBuilder generateConfig(int numServers, int[][] ports, Set observerIds) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < numServers; i++) { + String server = "server." + i + "=localhost:" + ports[i][0] + ":" + ports[i][1] + ":" + + (observerIds.contains(i) ? "observer" : "participant") + + ";localhost:" + ports[i][2]; + sb.append(server + "\n"); + } + return sb; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigRollingRestartCompatibilityTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigRollingRestartCompatibilityTest.java new file mode 100644 index 0000000..9522da7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ReconfigRollingRestartCompatibilityTest.java @@ -0,0 +1,375 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.Test; + +/** + * ReconfigRollingRestartCompatibilityTest - we want to make sure that users + * can continue using the rolling restart approach when reconfig feature is disabled. + * It is important to stay compatible with rolling restart because dynamic reconfig + * has its limitation: it requires a quorum of server to work. When no quorum can be formed, + * rolling restart is the only approach to reconfigure the ensemble (e.g. removing bad nodes + * such that a new quorum with smaller number of nodes can be formed.). + * + * See ZOOKEEPER-2819 for more details. + */ +public class ReconfigRollingRestartCompatibilityTest extends QuorumPeerTestBase { + + private static final String ZOO_CFG_BAK_FILE = "zoo.cfg.bak"; + + Map clientPorts = new HashMap<>(5); + Map serverAddress = new HashMap<>(5); + + private String generateNewQuorumConfig(int serverCount) { + StringBuilder sb = new StringBuilder(); + String server; + for (int i = 0; i < serverCount; i++) { + clientPorts.put(i, PortAssignment.unique()); + server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts.get(i); + serverAddress.put(i, server); + sb.append(server + "\n"); + } + return sb.toString(); + } + + private String updateExistingQuorumConfig(List sidsToAdd, List sidsToRemove) { + StringBuilder sb = new StringBuilder(); + for (Integer sid : sidsToAdd) { + clientPorts.put(sid, PortAssignment.unique()); + serverAddress.put(sid, "server." + sid + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;localhost:" + clientPorts.get(sid)); + } + + for (Integer sid : sidsToRemove) { + clientPorts.remove(sid); + serverAddress.remove(sid); + } + + for (String server : serverAddress.values()) { + sb.append(server + "\n"); + } + + return sb.toString(); + } + + @Test(timeout = 60000) + // Verify no zoo.cfg.dynamic and zoo.cfg.bak files existing locally + // when reconfig feature flag is off by default. + public void testNoLocalDynamicConfigAndBackupFiles() throws InterruptedException, IOException { + int serverCount = 3; + String config = generateNewQuorumConfig(serverCount); + QuorumPeerTestBase.MainThread[] mt = new QuorumPeerTestBase.MainThread[serverCount]; + String[] staticFileContent = new String[serverCount]; + + for (int i = 0; i < serverCount; i++) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + } + + for (int i = 0; i < serverCount; i++) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + assertNull("static file backup (zoo.cfg.bak) shouldn't exist!", mt[i].getFileByName(ZOO_CFG_BAK_FILE)); + assertNull("dynamic configuration file (zoo.cfg.dynamic.*) shouldn't exist!", mt[i].getFileByName(mt[i].getQuorumPeer().getNextDynamicConfigFilename())); + staticFileContent[i] = Files.readAllLines(mt[i].confFile.toPath(), StandardCharsets.UTF_8).toString(); + assertTrue("static config file should contain server entry " + serverAddress.get(i), staticFileContent[i].contains(serverAddress.get(i))); + } + + for (int i = 0; i < serverCount; i++) { + mt[i].shutdown(); + } + } + + @Test(timeout = 60000) + // This test simulate the usual rolling restart with no membership change: + // 1. A node is shutdown first (e.g. to upgrade software, or hardware, or cleanup local data.). + // 2. After upgrade, start the node. + // 3. Do this for every node, one at a time. + public void testRollingRestartWithoutMembershipChange() throws Exception { + int serverCount = 3; + String config = generateNewQuorumConfig(serverCount); + List joiningServers = new ArrayList<>(); + QuorumPeerTestBase.MainThread[] mt = new QuorumPeerTestBase.MainThread[serverCount]; + for (int i = 0; i < serverCount; ++i) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + joiningServers.add(serverAddress.get(i)); + } + + for (int i = 0; i < serverCount; ++i) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + } + + for (int i = 0; i < serverCount; ++i) { + mt[i].shutdown(); + mt[i].start(); + verifyQuorumConfig(i, joiningServers, null); + verifyQuorumMembers(mt[i]); + } + + for (int i = 0; i < serverCount; i++) { + mt[i].shutdown(); + } + } + + @Test(timeout = 90000) + // This test simulate the use case of change of membership by starting new servers + // without dynamic reconfig. For a 3 node ensemble we expand it to a 5 node ensemble, verify + // during the process each node has the expected configuration setting pushed + // via updating local zoo.cfg file. + public void testExtendingQuorumWithNewMembers() throws Exception { + int serverCount = 3; + String config = generateNewQuorumConfig(serverCount); + QuorumPeerTestBase.MainThread[] mt = new QuorumPeerTestBase.MainThread[serverCount]; + List joiningServers = new ArrayList<>(); + for (int i = 0; i < serverCount; ++i) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + joiningServers.add(serverAddress.get(i)); + } + + for (int i = 0; i < serverCount; ++i) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + } + + for (int i = 0; i < serverCount; ++i) { + verifyQuorumConfig(i, joiningServers, null); + verifyQuorumMembers(mt[i]); + } + + Map oldServerAddress = new HashMap<>(serverAddress); + List newServers = new ArrayList<>(joiningServers); + config = updateExistingQuorumConfig(Arrays.asList(3, 4), new ArrayList<>()); + newServers.add(serverAddress.get(3)); + newServers.add(serverAddress.get(4)); + serverCount = serverAddress.size(); + assertEquals("Server count should be 5 after config update.", serverCount, 5); + + // We are adding two new servers to the ensemble. These two servers should have the config which includes + // all five servers (the old three servers, plus the two servers added). The old three servers should only + // have the old three server config, because disabling reconfig will prevent synchronizing configs between + // peers. + mt = Arrays.copyOf(mt, mt.length + 2); + for (int i = 3; i < 5; ++i) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + verifyQuorumConfig(i, newServers, null); + verifyQuorumMembers(mt[i]); + } + + Set expectedConfigs = new HashSet<>(); + for (String conf : oldServerAddress.values()) { + // Remove "server.x=" prefix which quorum peer does not include. + expectedConfigs.add(conf.substring(conf.indexOf('=') + 1)); + } + + for (int i = 0; i < 3; ++i) { + verifyQuorumConfig(i, joiningServers, null); + verifyQuorumMembers(mt[i], expectedConfigs); + } + + for (int i = 0; i < serverCount; ++i) { + mt[i].shutdown(); + } + } + + @Test + public void testRollingRestartWithExtendedMembershipConfig() throws Exception { + // in this test we are performing rolling restart with extended quorum config, see ZOOKEEPER-3829 + + // Start a quorum with 3 members + int serverCount = 3; + String config = generateNewQuorumConfig(serverCount); + QuorumPeerTestBase.MainThread[] mt = new QuorumPeerTestBase.MainThread[serverCount]; + List joiningServers = new ArrayList<>(); + for (int i = 0; i < serverCount; i++) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + joiningServers.add(serverAddress.get(i)); + } + for (int i = 0; i < serverCount; i++) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + } + for (int i = 0; i < serverCount; i++) { + verifyQuorumConfig(i, joiningServers, null); + verifyQuorumMembers(mt[i]); + } + + // Create updated config with 4 members + List newServers = new ArrayList<>(joiningServers); + config = updateExistingQuorumConfig(Arrays.asList(3), new ArrayList<>()); + newServers.add(serverAddress.get(3)); + serverCount = serverAddress.size(); + assertEquals("Server count should be 4 after config update.", serverCount, 4); + + // We are adding one new server to the ensemble. The new server should be started with the new config + mt = Arrays.copyOf(mt, mt.length + 1); + mt[3] = new QuorumPeerTestBase.MainThread(3, clientPorts.get(3), config, false); + mt[3].start(); + assertTrue("waiting for server 3 being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(3), CONNECTION_TIMEOUT)); + verifyQuorumConfig(3, newServers, null); + verifyQuorumMembers(mt[3]); + + // Now we restart the first 3 servers, one-by-one with the new config + for (int i = 0; i < 3; i++) { + mt[i].shutdown(); + + assertTrue(String.format("Timeout during waiting for server %d to go down", i), + ClientBase.waitForServerDown("127.0.0.1:" + clientPorts.get(i), ClientBase.CONNECTION_TIMEOUT)); + + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + verifyQuorumConfig(i, newServers, null); + verifyQuorumMembers(mt[i]); + } + + // now verify that all nodes can handle traffic + for (int i = 0; i < 4; ++i) { + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + clientPorts.get(i)); + ReconfigTest.testNormalOperation(zk, zk, false); + } + + for (int i = 0; i < 4; ++i) { + mt[i].shutdown(); + } + } + + @Test + public void testRollingRestartWithHostAddedAndRemoved() throws Exception { + // in this test we are performing rolling restart with a new quorum config, + // contains a deleted node and a new node + + // Start a quorum with 3 members + int serverCount = 3; + String config = generateNewQuorumConfig(serverCount); + QuorumPeerTestBase.MainThread[] mt = new QuorumPeerTestBase.MainThread[serverCount]; + List originalServers = new ArrayList<>(); + for (int i = 0; i < serverCount; i++) { + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + originalServers.add(serverAddress.get(i)); + } + for (int i = 0; i < serverCount; i++) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + } + for (int i = 0; i < serverCount; i++) { + verifyQuorumConfig(i, originalServers, null); + verifyQuorumMembers(mt[i]); + } + + // we are stopping the third server (myid=2) + mt[2].shutdown(); + assertTrue(String.format("Timeout during waiting for server %d to go down", 2), + ClientBase.waitForServerDown("127.0.0.1:" + clientPorts.get(2), ClientBase.CONNECTION_TIMEOUT)); + String leavingServer = originalServers.get(2); + + // Create updated config with the first 2 existing members, but we remove 3rd and add one with different myid + config = updateExistingQuorumConfig(Arrays.asList(3), Arrays.asList(2)); + List newServers = new ArrayList<>(serverAddress.values()); + serverCount = serverAddress.size(); + assertEquals("Server count should be 3 after config update.", serverCount, 3); + + + // We are adding one new server to the ensemble. The new server should be started with the new config + mt = Arrays.copyOf(mt, mt.length + 1); + mt[3] = new QuorumPeerTestBase.MainThread(3, clientPorts.get(3), config, false); + mt[3].start(); + assertTrue("waiting for server 3 being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(3), CONNECTION_TIMEOUT)); + verifyQuorumConfig(3, newServers, Arrays.asList(leavingServer)); + verifyQuorumMembers(mt[3]); + + // Now we restart the first 2 servers, one-by-one with the new config + for (int i = 0; i < 2; i++) { + mt[i].shutdown(); + + assertTrue(String.format("Timeout during waiting for server %d to go down", i), + ClientBase.waitForServerDown("127.0.0.1:" + clientPorts.get(i), ClientBase.CONNECTION_TIMEOUT)); + + mt[i] = new QuorumPeerTestBase.MainThread(i, clientPorts.get(i), config, false); + mt[i].start(); + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts.get(i), CONNECTION_TIMEOUT)); + verifyQuorumConfig(i, newServers, null); + verifyQuorumMembers(mt[i]); + } + + // now verify that all three nodes can handle traffic + for (int i : serverAddress.keySet()) { + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + clientPorts.get(i)); + ReconfigTest.testNormalOperation(zk, zk, false); + } + + for (int i : serverAddress.keySet()) { + mt[i].shutdown(); + } + } + + + // Verify each quorum peer has expected config in its config zNode. + private void verifyQuorumConfig(int sid, List joiningServers, List leavingServers) throws Exception { + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + clientPorts.get(sid)); + ReconfigTest.testNormalOperation(zk, zk); + ReconfigTest.testServerHasConfig(zk, joiningServers, leavingServers); + zk.close(); + } + + // Verify each quorum peer has expected quorum member view. + private void verifyQuorumMembers(QuorumPeerTestBase.MainThread mt) { + Set expectedConfigs = new HashSet<>(); + for (String config : serverAddress.values()) { + expectedConfigs.add(config.substring(config.indexOf('=') + 1)); + } + verifyQuorumMembers(mt, expectedConfigs); + } + + private void verifyQuorumMembers(QuorumPeerTestBase.MainThread mt, Set expectedConfigs) { + Map members = mt.getQuorumPeer().getQuorumVerifier().getAllMembers(); + + assertTrue("Quorum member should not change.", members.size() == expectedConfigs.size()); + + for (QuorumPeer.QuorumServer qs : members.values()) { + String actualConfig = qs.toString(); + assertTrue("Unexpected config " + actualConfig + " found!", expectedConfigs.contains(actualConfig)); + } + } + +} + + + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java new file mode 100644 index 0000000..57b164f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/RemotePeerBeanTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.net.InetSocketAddress; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.junit.Test; + +public class RemotePeerBeanTest { + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2269 + */ + @Test + public void testGetClientAddressShouldReturnEmptyStringWhenClientAddressIsNull() { + InetSocketAddress peerCommunicationAddress = null; + // Here peerCommunicationAddress is null, also clientAddr is null + QuorumServer peer = new QuorumServer(1, peerCommunicationAddress); + RemotePeerBean remotePeerBean = new RemotePeerBean(null, peer); + String clientAddress = remotePeerBean.getClientAddress(); + assertNotNull(clientAddress); + assertEquals(0, clientAddress.length()); + } + + @Test + @SuppressWarnings("unchecked") + public void testIsLeader() { + long peerId = 7; + QuorumPeer.QuorumServer quorumServerMock = mock(QuorumPeer.QuorumServer.class); + when(quorumServerMock.getId()).thenReturn(peerId); + QuorumPeer peerMock = mock(QuorumPeer.class); + RemotePeerBean remotePeerBean = new RemotePeerBean(peerMock, quorumServerMock); + when(peerMock.isLeader(eq(peerId))).thenReturn(true); + assertTrue(remotePeerBean.isLeader()); + when(peerMock.isLeader(eq(peerId))).thenReturn(false); + assertFalse(remotePeerBean.isLeader()); + } + + @Test + public void testHostPortReturnedWhenIPIsIPV6() { + QuorumPeer.QuorumServer quorumServerMock = mock(QuorumPeer.QuorumServer.class); + InetSocketAddress address = new InetSocketAddress("127::1", 2181); + MultipleAddresses multipleAddresses = new MultipleAddresses(address); + quorumServerMock.clientAddr = address; + quorumServerMock.electionAddr = multipleAddresses; + quorumServerMock.addr = multipleAddresses; + QuorumPeer peerMock = mock(QuorumPeer.class); + RemotePeerBean remotePeerBean = new RemotePeerBean(peerMock, quorumServerMock); + String expectedHostPort = "[127:0:0:0:0:0:0:1]:2181"; + assertEquals(expectedHostPort, remotePeerBean.getClientAddress()); + assertEquals(expectedHostPort, remotePeerBean.getElectionAddress()); + assertEquals(expectedHostPort, remotePeerBean.getQuorumAddress()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/SessionUpgradeQuorumTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/SessionUpgradeQuorumTest.java new file mode 100644 index 0000000..e89d5ae --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/SessionUpgradeQuorumTest.java @@ -0,0 +1,365 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.concurrent.ConcurrentHashMap; +import javax.security.sasl.SaslException; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SessionUpgradeQuorumTest extends QuorumPeerTestBase { + + protected static final Logger LOG = LoggerFactory.getLogger(SessionUpgradeQuorumTest.class); + public static final int CONNECTION_TIMEOUT = ClientBase.CONNECTION_TIMEOUT; + + public static final int SERVER_COUNT = 3; + private MainThread[] mt; + private int[] clientPorts; + private TestQPMainDropSessionUpgrading[] qpMain; + + @Before + public void setUp() throws Exception { + LOG.info("STARTING quorum {}", getClass().getName()); + // setup the env with RetainDB and local session upgrading + ClientBase.setupTestEnv(); + + mt = new MainThread[SERVER_COUNT]; + clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + sb.append("server.").append(i).append("=127.0.0.1:").append(PortAssignment.unique()).append(":").append(PortAssignment.unique()).append("\n"); + } + sb.append("localSessionsEnabled=true\n"); + sb.append("localSessionsUpgradingEnabled=true\n"); + String cfg = sb.toString(); + + // create a 3 server ensemble + qpMain = new TestQPMainDropSessionUpgrading[SERVER_COUNT]; + for (int i = 0; i < SERVER_COUNT; i++) { + final TestQPMainDropSessionUpgrading qp = new TestQPMainDropSessionUpgrading(); + qpMain[i] = qp; + mt[i] = new MainThread(i, clientPorts[i], cfg, false) { + @Override + public TestQPMain getTestQPMain() { + return qp; + } + }; + mt[i].start(); + } + + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + } + + @After + public void tearDown() throws Exception { + LOG.info("STOPPING quorum {}", getClass().getName()); + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + + @Test + public void testLocalSessionUpgradeSnapshot() throws IOException, InterruptedException { + // select the candidate of follower + int leader = -1; + int followerA = -1; + for (int i = SERVER_COUNT - 1; i >= 0; i--) { + if (mt[i].main.quorumPeer.leader != null) { + leader = i; + } else if (followerA == -1) { + followerA = i; + } + } + + LOG.info("follower A is {}", followerA); + qpMain[followerA].setDropCreateSession(true); + + // create a client, and create an ephemeral node to trigger the + // upgrading process + final String node = "/node-1"; + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[followerA], ClientBase.CONNECTION_TIMEOUT, this); + + waitForOne(zk, States.CONNECTED); + + // clone the session id and passwd for later usage + long sessionId = zk.getSessionId(); + + // should fail because of the injection + try { + zk.create(node, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + fail("expect to failed to upgrade session due to the " + + "TestQPMainDropSessionUpgrading is being used"); + } catch (KeeperException e) { + LOG.info("KeeperException when create ephemeral node.", e); + } + + // force to take snapshot + qpMain[followerA].quorumPeer.follower.zk.takeSnapshot(true); + + // wait snapshot finish + Thread.sleep(500); + + // shutdown all servers + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + + ArrayList waitStates = new ArrayList(); + waitStates.add(States.CONNECTING); + waitStates.add(States.CLOSED); + waitForOne(zk, waitStates); + + // start the servers again, start follower A last as we want to + // keep it running as follower + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].start(); + } + + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + // check global session not exist on follower A + for (int i = 0; i < SERVER_COUNT; i++) { + ConcurrentHashMap sessions = mt[i].main.quorumPeer.getZkDb().getSessionWithTimeOuts(); + assertFalse( + "server " + i + " should not have global " + "session " + sessionId, + sessions.containsKey(sessionId)); + } + + zk.close(); + } + + @Test + public void testOnlyUpgradeSessionOnce() throws IOException, InterruptedException, KeeperException { + // create a client, and create an ephemeral node to trigger the + // upgrading process + final String node = "/node-1"; + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, this); + + waitForOne(zk, States.CONNECTED); + long sessionId = zk.getSessionId(); + + QuorumZooKeeperServer server = (QuorumZooKeeperServer) mt[0].main.quorumPeer.getActiveServer(); + Request create1 = createEphemeralRequest("/data-1", sessionId); + Request create2 = createEphemeralRequest("/data-2", sessionId); + + assertNotNull("failed to upgrade on a ephemeral create", server.checkUpgradeSession(create1)); + assertNull("tried to upgrade again", server.checkUpgradeSession(create2)); + + // clean al the setups and close the zk + zk.close(); + } + + @Test + public void testCloseSessionWhileUpgradeOnLeader() + throws IOException, KeeperException, InterruptedException { + int leaderId = -1; + for (int i = SERVER_COUNT - 1; i >= 0; i--) { + if (mt[i].main.quorumPeer.leader != null) { + leaderId = i; + } + } + if (leaderId > 0) { + makeSureEphemeralIsGone(leaderId); + } + } + + @Test + public void testCloseSessionWhileUpgradeOnLearner() + throws IOException, KeeperException, InterruptedException { + int learnerId = -1; + for (int i = SERVER_COUNT - 1; i >= 0; i--) { + if (mt[i].main.quorumPeer.follower != null) { + learnerId = i; + } + } + if (learnerId > 0) { + makeSureEphemeralIsGone(learnerId); + } + } + + private void makeSureEphemeralIsGone(int sid) + throws IOException, KeeperException, InterruptedException { + // Delay submit request to simulate the request queued in + // RequestThrottler + qpMain[sid].setSubmitDelayMs(200); + + // Create a client and an ephemeral node + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[sid], + ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk, States.CONNECTED); + + final String node = "/node-1"; + zk.create(node, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, + CreateMode.EPHEMERAL, new StringCallback() { + @Override + public void processResult(int rc, String path, Object ctx, + String name) {} + }, null); + + // close the client + zk.close(); + + // make sure the ephemeral is gone + zk = new ZooKeeper("127.0.0.1:" + clientPorts[sid], + ClientBase.CONNECTION_TIMEOUT, this); + waitForOne(zk, States.CONNECTED); + assertNull(zk.exists(node, false)); + zk.close(); + } + + private static class TestQPMainDropSessionUpgrading extends TestQPMain { + + private volatile boolean shouldDrop = false; + private volatile int submitDelayMs = 0; + + public void setDropCreateSession(boolean dropCreateSession) { + shouldDrop = dropCreateSession; + } + + public void setSubmitDelayMs(int delay) { + this.submitDelayMs = delay; + } + + @Override + protected QuorumPeer getQuorumPeer() throws SaslException { + return new QuorumPeer() { + + @Override + protected Leader makeLeader(FileTxnSnapLog logFactory) throws IOException { + return new Leader(this, new LeaderZooKeeperServer( + logFactory, this, this.getZkDb()) { + + @Override + public void submitRequestNow(Request si) { + if (submitDelayMs > 0) { + try { + Thread.sleep(submitDelayMs); + } catch (Exception e) {} + } + super.submitRequestNow(si); + } + }); + } + + @Override + protected Follower makeFollower(FileTxnSnapLog logFactory) throws IOException { + + return new Follower(this, new FollowerZooKeeperServer(logFactory, this, this.getZkDb()) { + + @Override + public void submitRequestNow(Request si) { + if (submitDelayMs > 0) { + try { + Thread.sleep(submitDelayMs); + } catch (Exception e) {} + } + super.submitRequestNow(si); + } + + }) { + + @Override + protected void request(Request request) throws IOException { + if (!shouldDrop) { + super.request(request); + return; + } + LOG.info("request is {}, cnxn {}", request.type, request.cnxn); + + if (request.type == ZooDefs.OpCode.createSession) { + LOG.info("drop createSession request {}", request); + return; + } + + if (request.type == ZooDefs.OpCode.create && request.cnxn != null) { + CreateRequest createRequest = new CreateRequest(); + request.request.rewind(); + ByteBufferInputStream.byteBuffer2Record(request.request, createRequest); + request.request.rewind(); + try { + CreateMode createMode = CreateMode.fromFlag(createRequest.getFlags()); + if (createMode.isEphemeral()) { + request.cnxn.sendCloseSession(); + } + } catch (KeeperException e) { + } + return; + } + + super.request(request); + } + }; + } + }; + } + + } + + private void waitForOne(ZooKeeper zk, ArrayList states) throws InterruptedException { + int iterations = ClientBase.CONNECTION_TIMEOUT / 500; + while (!states.contains(zk.getState())) { + if (iterations-- == 0) { + LOG.info("state is {}", zk.getState()); + throw new RuntimeException("Waiting too long"); + } + Thread.sleep(500); + } + } + + private Request createEphemeralRequest(String path, long sessionId) throws IOException { + ByteArrayOutputStream boas = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(boas); + CreateRequest createRequest = new CreateRequest(path, "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag()); + createRequest.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(boas.toByteArray()); + return new Request(null, sessionId, 1, ZooDefs.OpCode.create2, bb, new ArrayList()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StandaloneDisabledTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StandaloneDisabledTest.java new file mode 100644 index 0000000..70fcab7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StandaloneDisabledTest.java @@ -0,0 +1,271 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.client.FourLetterWordMain; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ReconfigTest; +import org.junit.Test; + +public class StandaloneDisabledTest extends QuorumPeerTestBase { + + private final int NUM_SERVERS = 5; + private MainThread[] peers; + private ZooKeeper[] zkHandles; + private ZooKeeperAdmin[] zkAdminHandles; + private int[] clientPorts; + private final int leaderId = 0; + private final int follower1 = 1; + private final int follower2 = 2; + private final int observer1 = 3; + private final int observer2 = 4; + private ArrayList serverStrings; + private ArrayList reconfigServers; + + /** + * Test normal quorum operations work cleanly + * with just a single server. + */ + @Test(timeout = 600000) + public void startSingleServerTest() throws Exception { + setUpData(); + + //start one server + startServer(leaderId, serverStrings.get(leaderId) + "\n"); + ReconfigTest.testServerHasConfig(zkHandles[leaderId], null, null); + LOG.info( + "Initial Configuration:\n{}", + new String(zkHandles[leaderId].getConfig(this, new Stat()))); + + //start and add 2 followers + startFollowers(); + testReconfig(leaderId, true, reconfigServers); + LOG.info( + "Configuration after adding 2 followers:\n{}", + new String(zkHandles[leaderId].getConfig(this, new Stat()))); + + //shutdown leader- quorum should still exist + shutDownServer(leaderId); + ReconfigTest.testNormalOperation(zkHandles[follower1], zkHandles[follower2]); + + //should not be able to remove follower 2 + //No quorum in new config (1/2) + reconfigServers.clear(); + reconfigServers.add(Integer.toString(follower2)); + try { + ReconfigTest.reconfig(zkAdminHandles[follower1], null, reconfigServers, null, -1); + fail("reconfig completed successfully even though there is no quorum up in new config!"); + } catch (KeeperException.NewConfigNoQuorum e) { + } + + //reconfigure out leader and follower 1. Remaining follower + //2 should elect itself as leader and run by itself + reconfigServers.clear(); + reconfigServers.add(Integer.toString(leaderId)); + reconfigServers.add(Integer.toString(follower1)); + testReconfig(follower2, false, reconfigServers); + LOG.info( + "Configuration after removing leader and follower 1:\n{}", + new String(zkHandles[follower2].getConfig(this, new Stat()))); + + // Kill server 1 to avoid it interferences with FLE of the quorum {2, 3, 4}. + shutDownServer(follower1); + + // Try to remove follower2, which is the only remaining server. This should fail. + reconfigServers.clear(); + reconfigServers.add(Integer.toString(follower2)); + try { + zkAdminHandles[follower2].reconfigure(null, reconfigServers, null, -1, new Stat()); + fail("reconfig completed successfully even though there is no quorum up in new config!"); + } catch (KeeperException.BadArgumentsException e) { + // This is expected. + } catch (Exception e) { + fail("Should have been BadArgumentsException!"); + } + + //Add two participants and change them to observers to check + //that we can reconfigure down to one participant with observers. + ArrayList observerStrings = new ArrayList(); + startObservers(observerStrings); + testReconfig(follower2, true, reconfigServers); //add partcipants + testReconfig(follower2, true, observerStrings); //change to observers + LOG.info( + "Configuration after adding two observers:\n{}", + new String(zkHandles[follower2].getConfig(this, new Stat()))); + + shutDownData(); + } + + /** + * Initialize private data for test. + */ + private void setUpData() throws Exception { + ClientBase.setupTestEnv(); + QuorumPeerConfig.setStandaloneEnabled(false); + QuorumPeerConfig.setReconfigEnabled(true); + peers = new MainThread[NUM_SERVERS]; + zkHandles = new ZooKeeper[NUM_SERVERS]; + zkAdminHandles = new ZooKeeperAdmin[NUM_SERVERS]; + clientPorts = new int[NUM_SERVERS]; + serverStrings = buildServerStrings(); + reconfigServers = new ArrayList(); + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + } + + /** + * Stop server threads. + */ + private void shutDownData() throws Exception { + for (int i = 0; i < NUM_SERVERS; i++) { + zkHandles[i].close(); + zkAdminHandles[i].close(); + } + for (int i = 1; i < NUM_SERVERS; i++) { + peers[i].shutdown(); + } + } + + /** + * Create config strings that will be used for + * the test servers. + */ + private ArrayList buildServerStrings() { + ArrayList serverStrings = new ArrayList(); + + for (int i = 0; i < NUM_SERVERS; i++) { + clientPorts[i] = PortAssignment.unique(); + String server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;" + + "localhost:" + clientPorts[i]; + serverStrings.add(server); + } + return serverStrings; + } + + /** + * Starts a single server in replicated mode, + * initializes its client, and waits for it + * to be connected. + */ + private void startServer(int id, String config) throws Exception { + peers[id] = new MainThread(id, clientPorts[id], config); + peers[id].start(); + assertTrue( + "Server " + id + " is not up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[id], CONNECTION_TIMEOUT)); + assertTrue("Error- Server started in Standalone Mode!", peers[id].isQuorumPeerRunning()); + zkHandles[id] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[id]); + zkAdminHandles[id] = new ZooKeeperAdmin("127.0.0.1:" + clientPorts[id], CONNECTION_TIMEOUT, this); + zkAdminHandles[id].addAuthInfo("digest", "super:test".getBytes()); + String statCommandOut = FourLetterWordMain.send4LetterWord("127.0.0.1", clientPorts[id], "stat"); + LOG.info("Started server id {} with config:\n{}\nStat output:\n{}", id, config, statCommandOut); + } + + /** + * Shuts down a server, waits for it to disconnect, + * and gives enough time for the learner handler + * in its ensemble to realize it's been shut down. + */ + private void shutDownServer(int id) throws Exception { + peers[id].shutdown(); + ClientBase.waitForServerDown("127.0.0.1:" + clientPorts[id], CONNECTION_TIMEOUT); + TimeUnit.SECONDS.sleep(25); + } + + /** + * Starts servers 1 and 2 as participants and + * adds them to the list to be reconfigured + * into the ensemble. + */ + private void startFollowers() throws Exception { + reconfigServers.clear(); + for (int i = 1; i <= 2; i++) { + String config = serverStrings.get(leaderId) + + "\n" + + serverStrings.get(i) + + "\n" + + serverStrings.get(i % 2 + 1) + + "\n"; + startServer(i, config); + reconfigServers.add(serverStrings.get(i)); + } + } + /** + * Starts servers 1 and 2 as participants, + * adds them to the list to be reconfigured + * into the ensemble, and adds an observer + * version of their information to a list + * so they will be turned into observers later. + */ + private void startObservers(ArrayList observerStrings) throws Exception { + reconfigServers.clear(); + for (int i = observer1; i <= observer2; i++) { + String config = serverStrings.get(follower2) + "\n" + serverStrings.get(i) + "\n"; + startServer(i, config); + reconfigServers.add(serverStrings.get(i)); + observerStrings.add(serverStrings.get(i).replace("participant", "observer")); + } + } + + /** + * Calls reconfig on the client corresponding to id to add or remove + * the given servers. Tests appropriately to make sure the + * reconfig succeeded. + */ + private void testReconfig(int id, boolean adding, ArrayList servers) throws Exception { + if (adding) { + ReconfigTest.reconfig(zkAdminHandles[id], servers, null, null, -1); + for (String server : servers) { + int id2 = Integer.parseInt(server.substring(7, 8)); //server.# + ReconfigTest.testNormalOperation(zkHandles[id], zkHandles[id2]); + } + ReconfigTest.testServerHasConfig(zkHandles[id], servers, null); + } else { + ReconfigTest.reconfig(zkAdminHandles[id], null, servers, null, -1); + ReconfigTest.testServerHasConfig(zkHandles[id], null, servers); + } + + } + + /** + * Ensure observer cannot start by itself + **/ + @Test + public void startObserver() throws Exception { + int clientPort = PortAssignment.unique(); + String config = "server." + observer1 + "=localhost:" + PortAssignment.unique() + ":" + clientPort + + ":observer;" + "localhost:" + PortAssignment.unique(); + MainThread observer = new MainThread(observer1, clientPort, config); + observer.start(); + assertFalse( + "Observer was able to start by itself!", + ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StatCommandTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StatCommandTest.java new file mode 100644 index 0000000..7a46bac --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StatCommandTest.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.command.FourLetterCommands; +import org.apache.zookeeper.server.command.StatCommand; +import org.junit.Before; +import org.junit.Test; + +public class StatCommandTest { + + private StringWriter outputWriter; + private StatCommand statCommand; + private ServerStats.Provider providerMock; + + @Before + public void setUp() { + outputWriter = new StringWriter(); + ServerCnxn serverCnxnMock = mock(ServerCnxn.class); + + LeaderZooKeeperServer zks = mock(LeaderZooKeeperServer.class); + when(zks.isRunning()).thenReturn(true); + providerMock = mock(ServerStats.Provider.class); + when(zks.serverStats()).thenReturn(new ServerStats(providerMock)); + ZKDatabase zkDatabaseMock = mock(ZKDatabase.class); + when(zks.getZKDatabase()).thenReturn(zkDatabaseMock); + Leader leaderMock = mock(Leader.class); + when(leaderMock.getProposalStats()).thenReturn(new BufferStats()); + when(zks.getLeader()).thenReturn(leaderMock); + + ServerCnxnFactory serverCnxnFactory = mock(ServerCnxnFactory.class); + ServerCnxn serverCnxn = mock(ServerCnxn.class); + List connections = new ArrayList<>(); + connections.add(serverCnxn); + when(serverCnxnFactory.getConnections()).thenReturn(connections); + + statCommand = new StatCommand(new PrintWriter(outputWriter), serverCnxnMock, FourLetterCommands.statCmd); + statCommand.setZkServer(zks); + statCommand.setFactory(serverCnxnFactory); + } + + @Test + public void testLeaderStatCommand() { + // Arrange + when(providerMock.getState()).thenReturn("leader"); + + // Act + statCommand.commandRun(); + + // Assert + String output = outputWriter.toString(); + assertCommonStrings(output); + assertThat(output, containsString("Mode: leader")); + assertThat(output, containsString("Proposal sizes last/min/max:")); + } + + @Test + public void testFollowerStatCommand() { + // Arrange + when(providerMock.getState()).thenReturn("follower"); + + // Act + statCommand.commandRun(); + + // Assert + String output = outputWriter.toString(); + assertCommonStrings(output); + assertThat(output, containsString("Mode: follower")); + } + + private void assertCommonStrings(String output) { + assertThat(output, containsString("Clients:")); + assertThat(output, containsString("Zookeeper version:")); + assertThat(output, containsString("Node count:")); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StatResetCommandTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StatResetCommandTest.java new file mode 100644 index 0000000..850c4d8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/StatResetCommandTest.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.server.command.AbstractFourLetterCommand.ZK_NOT_SERVING; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.io.PrintWriter; +import java.io.StringWriter; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.command.StatResetCommand; +import org.junit.Before; +import org.junit.Test; + +public class StatResetCommandTest { + + private StatResetCommand statResetCommand; + private StringWriter outputWriter; + private ZooKeeperServer zks; + private ServerStats serverStats; + + @Before + public void setUp() { + outputWriter = new StringWriter(); + ServerCnxn serverCnxnMock = mock(ServerCnxn.class); + + zks = mock(ZooKeeperServer.class); + when(zks.isRunning()).thenReturn(true); + + serverStats = mock(ServerStats.class); + when(zks.serverStats()).thenReturn(serverStats); + + statResetCommand = new StatResetCommand(new PrintWriter(outputWriter), serverCnxnMock); + statResetCommand.setZkServer(zks); + } + + @Test + public void testStatResetWithZKNotRunning() { + // Arrange + when(zks.isRunning()).thenReturn(false); + + // Act + statResetCommand.commandRun(); + + // Assert + String output = outputWriter.toString(); + assertEquals(ZK_NOT_SERVING + "\n", output); + } + + @Test + public void testStatResetWithFollower() { + // Arrange + when(zks.isRunning()).thenReturn(true); + when(serverStats.getServerState()).thenReturn("follower"); + + // Act + statResetCommand.commandRun(); + + // Assert + String output = outputWriter.toString(); + assertEquals("Server stats reset.\n", output); + verify(serverStats, times(1)).reset(); + } + + @Test + public void testStatResetWithLeader() { + // Arrange + LeaderZooKeeperServer leaderZks = mock(LeaderZooKeeperServer.class); + when(leaderZks.isRunning()).thenReturn(true); + when(leaderZks.serverStats()).thenReturn(serverStats); + Leader leader = mock(Leader.class); + when(leaderZks.getLeader()).thenReturn(leader); + statResetCommand.setZkServer(leaderZks); + + when(serverStats.getServerState()).thenReturn("leader"); + + BufferStats bufferStats = mock(BufferStats.class); + when(leader.getProposalStats()).thenReturn(bufferStats); + + // Act + statResetCommand.commandRun(); + + // Assert + String output = outputWriter.toString(); + assertEquals("Server stats reset.\n", output); + verify(serverStats, times(1)).reset(); + verify(bufferStats, times(1)).reset(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/SyncRequestProcessorMetricTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/SyncRequestProcessorMetricTest.java new file mode 100644 index 0000000..c003622 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/SyncRequestProcessorMetricTest.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.nio.ByteBuffer; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.RequestProcessor; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Before; +import org.junit.Test; + +public class SyncRequestProcessorMetricTest { + + ZooKeeperServer zks; + RequestProcessor nextProcessor; + CountDownLatch allRequestsFlushed; + + @Before + public void setup() throws Exception { + ZKDatabase db = mock(ZKDatabase.class); + when(db.append(any(Request.class))).thenReturn(true); + doAnswer(invocation -> { + Thread.sleep(100); + return null; + }).when(db).commit(); + zks = mock(ZooKeeperServer.class); + when(zks.getZKDatabase()).thenReturn(db); + + nextProcessor = mock(RequestProcessor.class); + doAnswer(invocationOnMock -> { + allRequestsFlushed.countDown(); + return null; + }).when(nextProcessor).processRequest(any(Request.class)); + } + + private Request createRquest(long sessionId, int xid) { + return new Request(null, sessionId, xid, ZooDefs.OpCode.setData, ByteBuffer.wrap(new byte[10]), null); + } + + @Test + public void testSyncProcessorMetrics() throws Exception { + SyncRequestProcessor syncProcessor = new SyncRequestProcessor(zks, nextProcessor); + for (int i = 0; i < 500; i++) { + syncProcessor.processRequest(createRquest(1, i)); + } + + Map values = MetricsUtils.currentServerMetrics(); + assertEquals(500L, values.get("sync_processor_request_queued")); + + allRequestsFlushed = new CountDownLatch(500); + syncProcessor.start(); + + allRequestsFlushed.await(5000, TimeUnit.MILLISECONDS); + + values = MetricsUtils.currentServerMetrics(); + + assertEquals(501L, values.get("cnt_sync_processor_queue_size")); + assertEquals(500L, values.get("max_sync_processor_queue_size")); + assertEquals(0L, values.get("min_sync_processor_queue_size")); + + assertEquals(500L, values.get("cnt_sync_processor_queue_time_ms")); + assertThat((long) values.get("max_sync_processor_queue_time_ms"), greaterThan(0L)); + + assertEquals(500L, values.get("cnt_sync_processor_queue_and_flush_time_ms")); + assertThat((long) values.get("max_sync_processor_queue_and_flush_time_ms"), greaterThan(0L)); + + assertEquals(500L, values.get("cnt_sync_process_time")); + assertThat((long) values.get("max_sync_process_time"), greaterThan(0L)); + + assertEquals(500L, values.get("max_sync_processor_batch_size")); + assertEquals(1L, values.get("cnt_sync_processor_queue_flush_time_ms")); + assertThat((long) values.get("max_sync_processor_queue_flush_time_ms"), greaterThanOrEqualTo(100L)); + + syncProcessor.shutdown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/UnifiedServerSocketModeDetectionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/UnifiedServerSocketModeDetectionTest.java new file mode 100644 index 0000000..94b0e01 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/UnifiedServerSocketModeDetectionTest.java @@ -0,0 +1,404 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketOptions; +import java.security.Security; +import java.util.ArrayList; +import java.util.Collection; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.KeyStoreFileType; +import org.apache.zookeeper.common.X509KeyType; +import org.apache.zookeeper.common.X509TestContext; +import org.apache.zookeeper.common.X509Util; +import org.apache.zookeeper.test.ClientBase; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This test makes sure that certain operations on a UnifiedServerSocket do not + * trigger blocking mode detection. This is necessary to ensure that the + * Leader's accept() thread doesn't get blocked. + */ +@RunWith(Parameterized.class) +public class UnifiedServerSocketModeDetectionTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(UnifiedServerSocketModeDetectionTest.class); + + @Parameterized.Parameters + public static Collection params() { + ArrayList result = new ArrayList<>(); + result.add(new Object[]{true}); + result.add(new Object[]{false}); + return result; + } + + private static File tempDir; + private static X509TestContext x509TestContext; + + private boolean useSecureClient; + private X509Util x509Util; + private UnifiedServerSocket listeningSocket; + private UnifiedServerSocket.UnifiedSocket serverSideSocket; + private Socket clientSocket; + private ExecutorService workerPool; + private int port; + private InetSocketAddress localServerAddress; + + @BeforeClass + public static void setUpClass() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + tempDir = ClientBase.createEmptyTestDir(); + x509TestContext = X509TestContext.newBuilder().setTempDir(tempDir).setKeyStoreKeyType(X509KeyType.EC).setTrustStoreKeyType(X509KeyType.EC).build(); + } + + @AfterClass + public static void tearDownClass() { + try { + FileUtils.deleteDirectory(tempDir); + } catch (IOException e) { + // ignore + } + Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); + } + + private static void forceClose(Socket s) { + if (s == null || s.isClosed()) { + return; + } + try { + s.close(); + } catch (IOException e) { + } + } + + private static void forceClose(ServerSocket s) { + if (s == null || s.isClosed()) { + return; + } + try { + s.close(); + } catch (IOException e) { + } + } + + public UnifiedServerSocketModeDetectionTest(Boolean useSecureClient) { + this.useSecureClient = useSecureClient; + } + + @Before + public void setUp() throws Exception { + x509Util = new ClientX509Util(); + x509TestContext.setSystemProperties(x509Util, KeyStoreFileType.JKS, KeyStoreFileType.JKS); + System.setProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty(), "100"); + workerPool = Executors.newCachedThreadPool(); + port = PortAssignment.unique(); + localServerAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), port); + listeningSocket = new UnifiedServerSocket(x509Util, true); + listeningSocket.bind(localServerAddress); + Future acceptFuture; + acceptFuture = workerPool.submit(new Callable() { + @Override + public UnifiedServerSocket.UnifiedSocket call() throws Exception { + try { + return (UnifiedServerSocket.UnifiedSocket) listeningSocket.accept(); + } catch (IOException e) { + LOG.error("Error in accept()", e); + throw e; + } + } + }); + if (useSecureClient) { + clientSocket = x509Util.createSSLSocket(); + clientSocket.connect(localServerAddress); + } else { + clientSocket = new Socket(); + clientSocket.connect(localServerAddress); + clientSocket.getOutputStream().write(new byte[]{1, 2, 3, 4, 5}); + } + serverSideSocket = acceptFuture.get(); + } + + @After + public void tearDown() throws Exception { + x509TestContext.clearSystemProperties(x509Util); + System.clearProperty(x509Util.getSslHandshakeDetectionTimeoutMillisProperty()); + forceClose(listeningSocket); + forceClose(serverSideSocket); + forceClose(clientSocket); + workerPool.shutdown(); + workerPool.awaitTermination(1000, TimeUnit.MILLISECONDS); + x509Util.close(); + } + + @Test + public void testGetInetAddress() { + serverSideSocket.getInetAddress(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetLocalAddress() { + serverSideSocket.getLocalAddress(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetPort() { + serverSideSocket.getPort(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetLocalPort() { + serverSideSocket.getLocalPort(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetRemoteSocketAddress() { + serverSideSocket.getRemoteSocketAddress(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetLocalSocketAddress() { + serverSideSocket.getLocalSocketAddress(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetInputStream() throws IOException { + serverSideSocket.getInputStream(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetOutputStream() throws IOException { + serverSideSocket.getOutputStream(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testGetTcpNoDelay() throws IOException { + serverSideSocket.getTcpNoDelay(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetTcpNoDelay() throws IOException { + boolean tcpNoDelay = serverSideSocket.getTcpNoDelay(); + tcpNoDelay = !tcpNoDelay; + serverSideSocket.setTcpNoDelay(tcpNoDelay); + assertFalse(serverSideSocket.isModeKnown()); + assertEquals(tcpNoDelay, serverSideSocket.getTcpNoDelay()); + } + + @Test + public void testGetSoLinger() throws IOException { + serverSideSocket.getSoLinger(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetSoLinger() throws IOException { + int soLinger = serverSideSocket.getSoLinger(); + if (soLinger == -1) { + // enable it if disabled + serverSideSocket.setSoLinger(true, 1); + assertFalse(serverSideSocket.isModeKnown()); + assertEquals(1, serverSideSocket.getSoLinger()); + } else { + // disable it if enabled + serverSideSocket.setSoLinger(false, -1); + assertFalse(serverSideSocket.isModeKnown()); + assertEquals(-1, serverSideSocket.getSoLinger()); + } + } + + @Test + public void testGetSoTimeout() throws IOException { + serverSideSocket.getSoTimeout(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetSoTimeout() throws IOException { + int timeout = serverSideSocket.getSoTimeout(); + timeout = timeout + 10; + serverSideSocket.setSoTimeout(timeout); + assertFalse(serverSideSocket.isModeKnown()); + assertEquals(timeout, serverSideSocket.getSoTimeout()); + } + + @Test + public void testGetSendBufferSize() throws IOException { + serverSideSocket.getSendBufferSize(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetSendBufferSize() throws IOException { + serverSideSocket.setSendBufferSize(serverSideSocket.getSendBufferSize() + 1024); + assertFalse(serverSideSocket.isModeKnown()); + // Note: the new buffer size is a hint and socket implementation + // is free to ignore it, so we don't verify that we get back the + // same value. + + } + + @Test + public void testGetReceiveBufferSize() throws IOException { + serverSideSocket.getReceiveBufferSize(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetReceiveBufferSize() throws IOException { + serverSideSocket.setReceiveBufferSize(serverSideSocket.getReceiveBufferSize() + 1024); + assertFalse(serverSideSocket.isModeKnown()); + // Note: the new buffer size is a hint and socket implementation + // is free to ignore it, so we don't verify that we get back the + // same value. + + } + + @Test + public void testGetKeepAlive() throws IOException { + serverSideSocket.getKeepAlive(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetKeepAlive() throws IOException { + boolean keepAlive = serverSideSocket.getKeepAlive(); + keepAlive = !keepAlive; + serverSideSocket.setKeepAlive(keepAlive); + assertFalse(serverSideSocket.isModeKnown()); + assertEquals(keepAlive, serverSideSocket.getKeepAlive()); + } + + @Test + public void testGetTrafficClass() throws IOException { + serverSideSocket.getTrafficClass(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetTrafficClass() throws IOException { + serverSideSocket.setTrafficClass(SocketOptions.IP_TOS); + assertFalse(serverSideSocket.isModeKnown()); + // Note: according to the Socket javadocs, setTrafficClass() may be + // ignored by socket implementations, so we don't check that the value + // we set is returned. + } + + @Test + public void testGetReuseAddress() throws IOException { + serverSideSocket.getReuseAddress(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testSetReuseAddress() throws IOException { + boolean reuseAddress = serverSideSocket.getReuseAddress(); + reuseAddress = !reuseAddress; + serverSideSocket.setReuseAddress(reuseAddress); + assertFalse(serverSideSocket.isModeKnown()); + assertEquals(reuseAddress, serverSideSocket.getReuseAddress()); + } + + @Test + public void testClose() throws IOException { + serverSideSocket.close(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testShutdownInput() throws IOException { + serverSideSocket.shutdownInput(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testShutdownOutput() throws IOException { + serverSideSocket.shutdownOutput(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testIsConnected() { + serverSideSocket.isConnected(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testIsBound() { + serverSideSocket.isBound(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testIsClosed() { + serverSideSocket.isClosed(); + assertFalse(serverSideSocket.isModeKnown()); + } + + @Test + public void testIsInputShutdown() throws IOException { + serverSideSocket.isInputShutdown(); + assertFalse(serverSideSocket.isModeKnown()); + serverSideSocket.shutdownInput(); + assertTrue(serverSideSocket.isInputShutdown()); + } + + @Test + public void testIsOutputShutdown() throws IOException { + serverSideSocket.isOutputShutdown(); + assertFalse(serverSideSocket.isModeKnown()); + serverSideSocket.shutdownOutput(); + assertTrue(serverSideSocket.isOutputShutdown()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/UnifiedServerSocketTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/UnifiedServerSocketTest.java new file mode 100644 index 0000000..7e4fa69 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/UnifiedServerSocketTest.java @@ -0,0 +1,596 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.net.ConnectException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import javax.net.ssl.HandshakeCompletedEvent; +import javax.net.ssl.HandshakeCompletedListener; +import javax.net.ssl.SSLSocket; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.common.BaseX509ParameterizedTestCase; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.common.KeyStoreFileType; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.common.X509KeyType; +import org.apache.zookeeper.common.X509TestContext; +import org.apache.zookeeper.common.X509Util; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class UnifiedServerSocketTest extends BaseX509ParameterizedTestCase { + + @Parameterized.Parameters + public static Collection params() { + ArrayList result = new ArrayList<>(); + int paramIndex = 0; + for (X509KeyType caKeyType : X509KeyType.values()) { + for (X509KeyType certKeyType : X509KeyType.values()) { + for (Boolean hostnameVerification : new Boolean[]{true, false}) { + result.add(new Object[]{caKeyType, certKeyType, hostnameVerification, paramIndex++}); + } + } + } + return result; + } + + private static final int MAX_RETRIES = 5; + private static final int TIMEOUT = 1000; + private static final byte[] DATA_TO_CLIENT = "hello client".getBytes(); + private static final byte[] DATA_FROM_CLIENT = "hello server".getBytes(); + + private X509Util x509Util; + private InetSocketAddress localServerAddress; + private final Object handshakeCompletedLock = new Object(); + // access only inside synchronized(handshakeCompletedLock) { ... } blocks + private boolean handshakeCompleted = false; + + public UnifiedServerSocketTest( + final X509KeyType caKeyType, final X509KeyType certKeyType, final Boolean hostnameVerification, final Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder().setTempDir(tempDir).setKeyStoreKeyType(certKeyType).setTrustStoreKeyType(caKeyType).setHostnameVerification(hostnameVerification).build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Before + public void setUp() throws Exception { + localServerAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), PortAssignment.unique()); + x509Util = new ClientX509Util(); + x509TestContext.setSystemProperties(x509Util, KeyStoreFileType.JKS, KeyStoreFileType.JKS); + } + + @After + public void tearDown() throws Exception { + x509TestContext.clearSystemProperties(x509Util); + x509Util.close(); + } + + private static void forceClose(Socket s) { + if (s == null || s.isClosed()) { + return; + } + try { + s.close(); + } catch (IOException e) { + } + } + + private static void forceClose(ServerSocket s) { + if (s == null || s.isClosed()) { + return; + } + try { + s.close(); + } catch (IOException e) { + } + } + + private static final class UnifiedServerThread extends Thread { + + private final byte[] dataToClient; + private List dataFromClients; + private ExecutorService workerPool; + private UnifiedServerSocket serverSocket; + + UnifiedServerThread(X509Util x509Util, InetSocketAddress bindAddress, boolean allowInsecureConnection, byte[] dataToClient) throws IOException { + this.dataToClient = dataToClient; + dataFromClients = new ArrayList<>(); + workerPool = Executors.newCachedThreadPool(); + serverSocket = new UnifiedServerSocket(x509Util, allowInsecureConnection); + serverSocket.bind(bindAddress); + } + + @Override + public void run() { + try { + Random rnd = new Random(); + while (true) { + final Socket unifiedSocket = serverSocket.accept(); + final boolean tcpNoDelay = rnd.nextBoolean(); + unifiedSocket.setTcpNoDelay(tcpNoDelay); + unifiedSocket.setSoTimeout(TIMEOUT); + final boolean keepAlive = rnd.nextBoolean(); + unifiedSocket.setKeepAlive(keepAlive); + // Note: getting the input stream should not block the thread or trigger mode detection. + BufferedInputStream bis = new BufferedInputStream(unifiedSocket.getInputStream()); + workerPool.submit(new Runnable() { + @Override + public void run() { + try { + byte[] buf = new byte[1024]; + int bytesRead = unifiedSocket.getInputStream().read(buf, 0, 1024); + // Make sure the settings applied above before the socket was potentially upgraded to + // TLS still apply. + assertEquals(tcpNoDelay, unifiedSocket.getTcpNoDelay()); + assertEquals(TIMEOUT, unifiedSocket.getSoTimeout()); + assertEquals(keepAlive, unifiedSocket.getKeepAlive()); + if (bytesRead > 0) { + byte[] dataFromClient = new byte[bytesRead]; + System.arraycopy(buf, 0, dataFromClient, 0, bytesRead); + synchronized (dataFromClients) { + dataFromClients.add(dataFromClient); + } + } + unifiedSocket.getOutputStream().write(dataToClient); + unifiedSocket.getOutputStream().flush(); + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + forceClose(unifiedSocket); + } + } + }); + } + } catch (IOException e) { + throw new RuntimeException(e); + } finally { + forceClose(serverSocket); + workerPool.shutdown(); + } + } + + public void shutdown(long millis) throws InterruptedException { + forceClose(serverSocket); // this should break the run() loop + workerPool.awaitTermination(millis, TimeUnit.MILLISECONDS); + this.join(millis); + } + + synchronized byte[] getDataFromClient(int index) { + return dataFromClients.get(index); + } + + synchronized boolean receivedAnyDataFromClient() { + return !dataFromClients.isEmpty(); + } + + } + + private SSLSocket connectWithSSL() throws IOException, X509Exception, InterruptedException { + SSLSocket sslSocket = null; + int retries = 0; + while (retries < MAX_RETRIES) { + try { + sslSocket = x509Util.createSSLSocket(); + sslSocket.addHandshakeCompletedListener(new HandshakeCompletedListener() { + @Override + public void handshakeCompleted(HandshakeCompletedEvent handshakeCompletedEvent) { + synchronized (handshakeCompletedLock) { + handshakeCompleted = true; + handshakeCompletedLock.notifyAll(); + } + } + }); + sslSocket.setSoTimeout(TIMEOUT); + sslSocket.connect(localServerAddress, TIMEOUT); + break; + } catch (ConnectException connectException) { + connectException.printStackTrace(); + forceClose(sslSocket); + sslSocket = null; + Thread.sleep(TIMEOUT); + } + retries++; + } + + assertNotNull("Failed to connect to server with SSL", sslSocket); + return sslSocket; + } + + private Socket connectWithoutSSL() throws IOException, InterruptedException { + Socket socket = null; + int retries = 0; + while (retries < MAX_RETRIES) { + try { + socket = new Socket(); + socket.setSoTimeout(TIMEOUT); + socket.connect(localServerAddress, TIMEOUT); + break; + } catch (ConnectException connectException) { + connectException.printStackTrace(); + forceClose(socket); + socket = null; + Thread.sleep(TIMEOUT); + } + retries++; + } + assertNotNull("Failed to connect to server without SSL", socket); + return socket; + } + + // In the tests below, a "Strict" server means a UnifiedServerSocket that + // does not allow plaintext connections (in other words, it's SSL-only). + // A "Non Strict" server means a UnifiedServerSocket that allows both + // plaintext and SSL incoming connections. + + /** + * Attempting to connect to a SSL-or-plaintext server with SSL should work. + */ + @Test + public void testConnectWithSSLToNonStrictServer() throws Exception { + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, true, DATA_TO_CLIENT); + serverThread.start(); + + Socket sslSocket = connectWithSSL(); + try { + sslSocket.getOutputStream().write(DATA_FROM_CLIENT); + sslSocket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = sslSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + + synchronized (handshakeCompletedLock) { + if (!handshakeCompleted) { + handshakeCompletedLock.wait(TIMEOUT); + } + assertTrue(handshakeCompleted); + } + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + } finally { + forceClose(sslSocket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Attempting to connect to a SSL-only server with SSL should work. + */ + @Test + public void testConnectWithSSLToStrictServer() throws Exception { + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, false, DATA_TO_CLIENT); + serverThread.start(); + + Socket sslSocket = connectWithSSL(); + try { + sslSocket.getOutputStream().write(DATA_FROM_CLIENT); + sslSocket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = sslSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + + synchronized (handshakeCompletedLock) { + if (!handshakeCompleted) { + handshakeCompletedLock.wait(TIMEOUT); + } + assertTrue(handshakeCompleted); + } + + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + } finally { + forceClose(sslSocket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Attempting to connect to a SSL-or-plaintext server without SSL should work. + */ + @Test + public void testConnectWithoutSSLToNonStrictServer() throws Exception { + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, true, DATA_TO_CLIENT); + serverThread.start(); + + Socket socket = connectWithoutSSL(); + try { + socket.getOutputStream().write(DATA_FROM_CLIENT); + socket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = socket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + } finally { + forceClose(socket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Attempting to connect to a SSL-or-plaintext server without SSL with a + * small initial data write should work. This makes sure that sending + * less than 5 bytes does not break the logic in the server's initial 5 + * byte read. + */ + @Test + public void testConnectWithoutSSLToNonStrictServerPartialWrite() throws Exception { + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, true, DATA_TO_CLIENT); + serverThread.start(); + + Socket socket = connectWithoutSSL(); + try { + // Write only 2 bytes of the message, wait a bit, then write the rest. + // This makes sure that writes smaller than 5 bytes don't break the plaintext mode on the server + // once it decides that the input doesn't look like a TLS handshake. + socket.getOutputStream().write(DATA_FROM_CLIENT, 0, 2); + socket.getOutputStream().flush(); + Thread.sleep(TIMEOUT / 2); + socket.getOutputStream().write(DATA_FROM_CLIENT, 2, DATA_FROM_CLIENT.length - 2); + socket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = socket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + } finally { + forceClose(socket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Attempting to connect to a SSL-only server without SSL should fail. + */ + @Test + public void testConnectWithoutSSLToStrictServer() throws Exception { + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, false, DATA_TO_CLIENT); + serverThread.start(); + + Socket socket = connectWithoutSSL(); + socket.getOutputStream().write(DATA_FROM_CLIENT); + socket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + try { + int bytesRead = socket.getInputStream().read(buf, 0, buf.length); + if (bytesRead == -1) { + // Using the NioSocketImpl after JDK 13, the expected behaviour on the client side + // is to reach the end of the stream (bytesRead == -1), without a socket exception. + return; + } + } catch (SocketException e) { + // Using the old PlainSocketImpl (prior to JDK 13) we expect to get Socket Exception + return; + } finally { + forceClose(socket); + serverThread.shutdown(TIMEOUT); + + // independently of the client socket implementation details, we always make sure the + // server didn't receive any data during the test + assertFalse("The strict server accepted connection without SSL.", serverThread.receivedAnyDataFromClient()); + } + fail("Expected server to hang up the connection. Read from server succeeded unexpectedly."); + } + + /** + * This test makes sure that UnifiedServerSocket used properly (a single + * thread accept()-ing connections and handing the resulting sockets to + * other threads for processing) is not vulnerable to blocking the + * accept() thread while doing mode detection if a misbehaving client + * connects. A misbehaving client is one that either disconnects + * immediately, or connects but does not send any data. + * + * This version of the test uses a non-strict server socket (i.e. it + * accepts both TLS and plaintext connections). + */ + @Test + public void testTLSDetectionNonBlockingNonStrictServerIdleClient() throws Exception { + Socket badClientSocket = null; + Socket clientSocket = null; + Socket secureClientSocket = null; + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, true, DATA_TO_CLIENT); + serverThread.start(); + + try { + badClientSocket = connectWithoutSSL(); // Leave the bad client socket idle + + clientSocket = connectWithoutSSL(); + clientSocket.getOutputStream().write(DATA_FROM_CLIENT); + clientSocket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = clientSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + + synchronized (handshakeCompletedLock) { + assertFalse(handshakeCompleted); + } + + secureClientSocket = connectWithSSL(); + secureClientSocket.getOutputStream().write(DATA_FROM_CLIENT); + secureClientSocket.getOutputStream().flush(); + buf = new byte[DATA_TO_CLIENT.length]; + bytesRead = secureClientSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(1)); + + synchronized (handshakeCompletedLock) { + if (!handshakeCompleted) { + handshakeCompletedLock.wait(TIMEOUT); + } + assertTrue(handshakeCompleted); + } + } finally { + forceClose(badClientSocket); + forceClose(clientSocket); + forceClose(secureClientSocket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Like the above test, but with a strict server socket (closes non-TLS + * connections after seeing that there is no handshake). + */ + @Test + public void testTLSDetectionNonBlockingStrictServerIdleClient() throws Exception { + Socket badClientSocket = null; + Socket secureClientSocket = null; + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, false, DATA_TO_CLIENT); + serverThread.start(); + + try { + badClientSocket = connectWithoutSSL(); // Leave the bad client socket idle + + secureClientSocket = connectWithSSL(); + secureClientSocket.getOutputStream().write(DATA_FROM_CLIENT); + secureClientSocket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = secureClientSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + + synchronized (handshakeCompletedLock) { + if (!handshakeCompleted) { + handshakeCompletedLock.wait(TIMEOUT); + } + assertTrue(handshakeCompleted); + } + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + } finally { + forceClose(badClientSocket); + forceClose(secureClientSocket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Similar to the tests above, but the bad client disconnects immediately + * without sending any data. + */ + @Test + public void testTLSDetectionNonBlockingNonStrictServerDisconnectedClient() throws Exception { + Socket clientSocket = null; + Socket secureClientSocket = null; + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, true, DATA_TO_CLIENT); + serverThread.start(); + + try { + Socket badClientSocket = connectWithoutSSL(); + forceClose(badClientSocket); // close the bad client socket immediately + + clientSocket = connectWithoutSSL(); + clientSocket.getOutputStream().write(DATA_FROM_CLIENT); + clientSocket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = clientSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + + synchronized (handshakeCompletedLock) { + assertFalse(handshakeCompleted); + } + + secureClientSocket = connectWithSSL(); + secureClientSocket.getOutputStream().write(DATA_FROM_CLIENT); + secureClientSocket.getOutputStream().flush(); + buf = new byte[DATA_TO_CLIENT.length]; + bytesRead = secureClientSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(1)); + + synchronized (handshakeCompletedLock) { + if (!handshakeCompleted) { + handshakeCompletedLock.wait(TIMEOUT); + } + assertTrue(handshakeCompleted); + } + } finally { + forceClose(clientSocket); + forceClose(secureClientSocket); + serverThread.shutdown(TIMEOUT); + } + } + + /** + * Like the above test, but with a strict server socket (closes non-TLS + * connections after seeing that there is no handshake). + */ + @Test + public void testTLSDetectionNonBlockingStrictServerDisconnectedClient() throws Exception { + Socket secureClientSocket = null; + UnifiedServerThread serverThread = new UnifiedServerThread(x509Util, localServerAddress, false, DATA_TO_CLIENT); + serverThread.start(); + + try { + Socket badClientSocket = connectWithoutSSL(); + forceClose(badClientSocket); // close the bad client socket immediately + + secureClientSocket = connectWithSSL(); + secureClientSocket.getOutputStream().write(DATA_FROM_CLIENT); + secureClientSocket.getOutputStream().flush(); + byte[] buf = new byte[DATA_TO_CLIENT.length]; + int bytesRead = secureClientSocket.getInputStream().read(buf, 0, buf.length); + assertEquals(buf.length, bytesRead); + assertArrayEquals(DATA_TO_CLIENT, buf); + + synchronized (handshakeCompletedLock) { + if (!handshakeCompleted) { + handshakeCompletedLock.wait(TIMEOUT); + } + assertTrue(handshakeCompleted); + } + assertArrayEquals(DATA_FROM_CLIENT, serverThread.getDataFromClient(0)); + } finally { + forceClose(secureClientSocket); + serverThread.shutdown(TIMEOUT); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/WatchLeakTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/WatchLeakTest.java new file mode 100644 index 0000000..2455738 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/WatchLeakTest.java @@ -0,0 +1,357 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you 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 org.apache.zookeeper.server.quorum; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.nio.channels.SelectableChannel; +import java.nio.channels.SelectionKey; +import java.nio.channels.Selector; +import java.nio.channels.SocketChannel; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.ClientCnxn; +import org.apache.zookeeper.MockPacket; +import org.apache.zookeeper.ZKParameterized; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.proto.SetWatches; +import org.apache.zookeeper.server.MockNIOServerCnxn; +import org.apache.zookeeper.server.MockSelectorThread; +import org.apache.zookeeper.server.NIOServerCnxn; +import org.apache.zookeeper.server.NIOServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Demonstrate ZOOKEEPER-1382 : Watches leak on expired session + */ +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class WatchLeakTest { + + protected static final Logger LOG = LoggerFactory.getLogger(WatchLeakTest.class); + + final long SESSION_ID = 0xBABEL; + + private final boolean sessionTimedout; + + @Before + public void setUp() { + System.setProperty("zookeeper.admin.enableServer", "false"); + } + + public WatchLeakTest(boolean sessionTimedout) { + this.sessionTimedout = sessionTimedout; + } + + @Parameters + public static Collection configs() { + return Arrays.asList(new Object[][]{{false}, {true}}); + } + + /** + * Check that if session has expired then no watch can be set + */ + + @Test + public void testWatchesLeak() throws Exception { + + NIOServerCnxnFactory serverCnxnFactory = mock(NIOServerCnxnFactory.class); + final SelectionKey sk = new FakeSK(); + MockSelectorThread selectorThread = mock(MockSelectorThread.class); + when(selectorThread.addInterestOpsUpdateRequest(any(SelectionKey.class))).thenAnswer(new Answer() { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + SelectionKey sk = (SelectionKey) invocation.getArguments()[0]; + NIOServerCnxn nioSrvCnx = (NIOServerCnxn) sk.attachment(); + sk.interestOps(nioSrvCnx.getInterestOps()); + return true; + } + }); + + ZKDatabase database = new ZKDatabase(null); + database.setlastProcessedZxid(2L); + QuorumPeer quorumPeer = mock(QuorumPeer.class); + FileTxnSnapLog logfactory = mock(FileTxnSnapLog.class); + // Directories are not used but we need it to avoid NPE + when(logfactory.getDataDir()).thenReturn(new File("")); + when(logfactory.getSnapDir()).thenReturn(new File("")); + FollowerZooKeeperServer fzks = null; + + try { + // Create a new follower + fzks = new FollowerZooKeeperServer(logfactory, quorumPeer, database); + fzks.startup(); + fzks.setServerCnxnFactory(serverCnxnFactory); + quorumPeer.follower = new MyFollower(quorumPeer, fzks); + LOG.info("Follower created"); + // Simulate a socket channel between a client and a follower + final SocketChannel socketChannel = createClientSocketChannel(); + // Create the NIOServerCnxn that will handle the client requests + final MockNIOServerCnxn nioCnxn = new MockNIOServerCnxn(fzks, socketChannel, sk, serverCnxnFactory, selectorThread); + sk.attach(nioCnxn); + // Send the connection request as a client do + nioCnxn.doIO(sk); + LOG.info("Client connection sent"); + // Send the valid or invalid session packet to the follower + QuorumPacket qp = createValidateSessionPacketResponse(!sessionTimedout); + quorumPeer.follower.processPacket(qp); + LOG.info("Session validation sent"); + // OK, now the follower knows that the session is valid or invalid, let's try + // to send the watches + nioCnxn.doIO(sk); + // wait for the the request processor to do his job + Thread.sleep(1000L); + LOG.info("Watches processed"); + // If session has not been validated, there must be NO watches + int watchCount = database.getDataTree().getWatchCount(); + if (sessionTimedout) { + // Session has not been re-validated ! + LOG.info("session is not valid, watches = {}", watchCount); + assertEquals("Session is not valid so there should be no watches", 0, watchCount); + } else { + // Session has been re-validated + LOG.info("session is valid, watches = {}", watchCount); + assertEquals("Session is valid so the watch should be there", 1, watchCount); + } + } finally { + if (fzks != null) { + fzks.shutdown(); + } + } + } + + /** + * A follower with no real leader connection + */ + public static class MyFollower extends Follower { + + /** + * Create a follower with a mocked leader connection + * + * @param self + * @param zk + */ + MyFollower(QuorumPeer self, FollowerZooKeeperServer zk) { + super(self, zk); + leaderOs = mock(OutputArchive.class); + leaderIs = mock(InputArchive.class); + bufferedOutput = mock(BufferedOutputStream.class); + } + + } + + /** + * Simulate the behavior of a real selection key + */ + private static class FakeSK extends SelectionKey { + + @Override + public SelectableChannel channel() { + return null; + } + + @Override + public Selector selector() { + return mock(Selector.class); + } + + @Override + public boolean isValid() { + return true; + } + + @Override + public void cancel() { + } + + @Override + public int interestOps() { + return ops; + } + + private int ops = OP_WRITE + OP_READ; + + @Override + public SelectionKey interestOps(int ops) { + this.ops = ops; + return this; + } + + @Override + public int readyOps() { + boolean reading = (ops & OP_READ) != 0; + boolean writing = (ops & OP_WRITE) != 0; + if (reading && writing) { + LOG.info("Channel is ready for reading and writing"); + } else if (reading) { + LOG.info("Channel is ready for reading only"); + } else if (writing) { + LOG.info("Channel is ready for writing only"); + } + return ops; + } + + } + + /** + * Create a watches message with a single watch on / + * + * @return a message that attempts to set 1 watch on / + */ + private ByteBuffer createWatchesMessage() { + List dataWatches = new ArrayList(1); + dataWatches.add("/"); + List existWatches = Collections.emptyList(); + List childWatches = Collections.emptyList(); + SetWatches sw = new SetWatches(1L, dataWatches, existWatches, childWatches); + RequestHeader h = new RequestHeader(); + h.setType(ZooDefs.OpCode.setWatches); + h.setXid(ClientCnxn.SET_WATCHES_XID); + MockPacket p = new MockPacket(h, new ReplyHeader(), sw, null, null); + return p.createAndReturnBB(); + } + + /** + * This is the secret that we use to generate passwords, for the moment it + * is more of a sanity check. + */ + private static final long superSecret = 0XB3415C00L; + + /** + * Create a connection request + * + * @return a serialized connection request + */ + private ByteBuffer createConnRequest() { + Random r = new Random(SESSION_ID ^ superSecret); + byte[] p = new byte[16]; + r.nextBytes(p); + ConnectRequest conReq = new ConnectRequest(0, 1L, 30000, SESSION_ID, p); + MockPacket packet = new MockPacket(null, null, conReq, null, null, false); + return packet.createAndReturnBB(); + } + + /** + * Mock a client channel with a connection request and a watches message + * inside. + * + * @return a socket channel + * @throws IOException + */ + private SocketChannel createClientSocketChannel() throws IOException { + + SocketChannel socketChannel = mock(SocketChannel.class); + Socket socket = mock(Socket.class); + InetSocketAddress socketAddress = new InetSocketAddress(1234); + when(socket.getRemoteSocketAddress()).thenReturn(socketAddress); + when(socketChannel.socket()).thenReturn(socket); + + // Send watches packet to server connection + final ByteBuffer connRequest = createConnRequest(); + final ByteBuffer watchesMessage = createWatchesMessage(); + final ByteBuffer request = ByteBuffer.allocate(connRequest.limit() + watchesMessage.limit()); + request.put(connRequest); + request.put(watchesMessage); + + Answer answer = new Answer() { + int i = 0; + + @Override + public Integer answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + ByteBuffer bb = (ByteBuffer) args[0]; + for (int k = 0; k < bb.limit(); k++) { + bb.put(request.get(i)); + i = i + 1; + } + return bb.limit(); + } + }; + when(socketChannel.read(any(ByteBuffer.class))).thenAnswer(answer); + return socketChannel; + } + + /** + * Forge an invalid session packet as a LEADER do + * + * @param valid true to create a valid session message + * + * @throws Exception + */ + private QuorumPacket createValidateSessionPacketResponse(boolean valid) throws Exception { + QuorumPacket qp = createValidateSessionPacket(); + ByteArrayInputStream bis = new ByteArrayInputStream(qp.getData()); + DataInputStream dis = new DataInputStream(bis); + long id = dis.readLong(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + dos.writeLong(id); + // false means that the session has expired + dos.writeBoolean(valid); + qp.setData(bos.toByteArray()); + return qp; + } + + /** + * Forge an validate session packet as a LEARNER do + * + * @return + * @throws Exception + */ + private QuorumPacket createValidateSessionPacket() throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(baos); + dos.writeLong(SESSION_ID); + dos.writeInt(3000); + dos.close(); + QuorumPacket qp = new QuorumPacket(Leader.REVALIDATE, -1, baos.toByteArray(), null); + return qp; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/Zab1_0Test.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/Zab1_0Test.java new file mode 100644 index 0000000..b8630ae --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/Zab1_0Test.java @@ -0,0 +1,1234 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import static org.apache.zookeeper.server.quorum.ZabUtils.MockLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.createLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.createMockLeader; +import static org.apache.zookeeper.server.quorum.ZabUtils.createQuorumPeer; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.EOFException; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.InputArchive; +import org.apache.jute.OutputArchive; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ByteBufferInputStream; +import org.apache.zookeeper.server.ByteBufferOutputStream; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.util.ZxidUtils; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.TestUtils; +import org.apache.zookeeper.txn.CreateSessionTxn; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.ErrorTxn; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Zab1_0Test extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(Zab1_0Test.class); + + private static final File testData = new File(System.getProperty("test.data.dir", "src/test/resources/data")); + + @Before + public void setUp() { + System.setProperty("zookeeper.admin.enableServer", "false"); + } + + private static final class LeadThread extends Thread { + + private final Leader leader; + + private LeadThread(Leader leader) { + this.leader = leader; + } + + public void run() { + try { + leader.lead(); + } catch (InterruptedException e) { + LOG.info("Leader thread interrupted", e); + } catch (Exception e) { + LOG.warn("Unexpected exception in leader thread", e); + } finally { + leader.shutdown("lead ended"); + } + } + + } + + public static final class FollowerMockThread extends Thread { + + private final Leader leader; + private final long followerSid; + public long epoch = -1; + public String msg = null; + private boolean onlyGetEpochToPropose; + + private FollowerMockThread(long followerSid, Leader leader, boolean onlyGetEpochToPropose) { + this.leader = leader; + this.followerSid = followerSid; + this.onlyGetEpochToPropose = onlyGetEpochToPropose; + } + + public void run() { + if (onlyGetEpochToPropose) { + try { + epoch = leader.getEpochToPropose(followerSid, 0); + } catch (Exception e) { + } + } else { + try { + leader.waitForEpochAck(followerSid, new StateSummary(0, 0)); + msg = "FollowerMockThread (id = " + followerSid + ") returned from waitForEpochAck"; + } catch (Exception e) { + } + } + } + + } + @Test + public void testLeaderInConnectingFollowers() throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + try { + QuorumPeer peer = createQuorumPeer(tmpDir); + leader = createLeader(tmpDir, peer); + peer.leader = leader; + peer.setAcceptedEpoch(5); + + FollowerMockThread f1 = new FollowerMockThread(1, leader, true); + FollowerMockThread f2 = new FollowerMockThread(2, leader, true); + f1.start(); + f2.start(); + + // wait until followers time out in getEpochToPropose - they shouldn't return + // normally because the leader didn't execute getEpochToPropose and so its epoch was not + // accounted for + f1.join(leader.self.getInitLimit() * leader.self.getTickTime() + 5000); + f2.join(leader.self.getInitLimit() * leader.self.getTickTime() + 5000); + + // even though followers timed out, their ids are in connectingFollowers, and their + // epoch were accounted for, so the leader should not block and since it started with + // accepted epoch = 5 it should now have 6 + try { + long epoch = leader.getEpochToPropose(leader.self.getId(), leader.self.getAcceptedEpoch()); + assertEquals("leader got wrong epoch from getEpochToPropose", 6, epoch); + } catch (Exception e) { + fail("leader timed out in getEpochToPropose"); + } + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + /** + * In this test, the leader sets the last accepted epoch to 5. The call + * to getEpochToPropose should set epoch to 6 and wait until another + * follower executes it. If in getEpochToPropose we don't check if + * lastAcceptedEpoch == epoch, then the call from the subsequent + * follower with lastAcceptedEpoch = 6 doesn't change the value + * of epoch, and the test fails. It passes with the fix to predicate. + * + * https://issues.apache.org/jira/browse/ZOOKEEPER-1343 + * + * + * @throws Exception + */ + + @Test + public void testLastAcceptedEpoch() throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + LeadThread leadThread = null; + try { + QuorumPeer peer = createQuorumPeer(tmpDir); + leader = createMockLeader(tmpDir, peer); + peer.leader = leader; + peer.setAcceptedEpoch(5); + leadThread = new LeadThread(leader); + leadThread.start(); + + while (((MockLeader) leader).getCurrentEpochToPropose() != 6) { + Thread.sleep(20); + } + + try { + long epoch = leader.getEpochToPropose(1, 6); + assertEquals("New proposed epoch is wrong", 7, epoch); + } catch (Exception e) { + fail("Timed out in getEpochToPropose"); + } + + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + if (leadThread != null) { + leadThread.interrupt(); + leadThread.join(); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + @Test + public void testLeaderInElectingFollowers() throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + Leader leader = null; + try { + QuorumPeer peer = createQuorumPeer(tmpDir); + leader = createLeader(tmpDir, peer); + peer.leader = leader; + + FollowerMockThread f1 = new FollowerMockThread(1, leader, false); + FollowerMockThread f2 = new FollowerMockThread(2, leader, false); + + // things needed for waitForEpochAck to run (usually in leader.lead(), but we're not running leader here) + leader.leaderStateSummary = new StateSummary(leader.self.getCurrentEpoch(), leader.zk.getLastProcessedZxid()); + + f1.start(); + f2.start(); + + // wait until followers time out in waitForEpochAck - they shouldn't return + // normally because the leader didn't execute waitForEpochAck + f1.join(leader.self.getInitLimit() * leader.self.getTickTime() + 5000); + f2.join(leader.self.getInitLimit() * leader.self.getTickTime() + 5000); + + // make sure that they timed out and didn't return normally + assertTrue(f1.msg + " without waiting for leader", f1.msg == null); + assertTrue(f2.msg + " without waiting for leader", f2.msg == null); + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + static Socket[] getSocketPair() throws IOException { + ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1")); + InetSocketAddress endPoint = (InetSocketAddress) ss.getLocalSocketAddress(); + Socket s = new Socket(endPoint.getAddress(), endPoint.getPort()); + return new Socket[]{s, ss.accept()}; + } + static void readPacketSkippingPing(InputArchive ia, QuorumPacket qp) throws IOException { + while (true) { + ia.readRecord(qp, null); + if (qp.getType() != Leader.PING) { + return; + } + } + } + + public interface LeaderConversation { + + void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l) throws Exception; + + } + + public interface PopulatedLeaderConversation { + + void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l, long zxid) throws Exception; + + } + + public interface FollowerConversation { + + void converseWithFollower(InputArchive ia, OutputArchive oa, Follower f) throws Exception; + + } + + public interface ObserverConversation { + + void converseWithObserver(InputArchive ia, OutputArchive oa, Observer o) throws Exception; + + } + + public void testLeaderConversation(LeaderConversation conversation) throws Exception { + Socket[] pair = getSocketPair(); + Socket leaderSocket = pair[0]; + Socket followerSocket = pair[1]; + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + LeadThread leadThread = null; + Leader leader = null; + try { + QuorumPeer peer = createQuorumPeer(tmpDir); + leader = createLeader(tmpDir, peer); + peer.leader = leader; + leadThread = new LeadThread(leader); + leadThread.start(); + + while (leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) { + Thread.sleep(20); + } + + LearnerHandler lh = new LearnerHandler(leaderSocket, new BufferedInputStream(leaderSocket.getInputStream()), leader); + lh.start(); + leaderSocket.setSoTimeout(4000); + + InputArchive ia = BinaryInputArchive.getArchive(followerSocket.getInputStream()); + OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket.getOutputStream()); + + conversation.converseWithLeader(ia, oa, leader); + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + if (leadThread != null) { + leadThread.interrupt(); + leadThread.join(); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + public void testPopulatedLeaderConversation(PopulatedLeaderConversation conversation, int ops) throws Exception { + Socket[] pair = getSocketPair(); + Socket leaderSocket = pair[0]; + Socket followerSocket = pair[1]; + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + LeadThread leadThread = null; + Leader leader = null; + try { + // Setup a database with two znodes + FileTxnSnapLog snapLog = new FileTxnSnapLog(tmpDir, tmpDir); + ZKDatabase zkDb = new ZKDatabase(snapLog); + + assertTrue(ops >= 1); + long zxid = ZxidUtils.makeZxid(1, 0); + for (int i = 1; i <= ops; i++) { + zxid = ZxidUtils.makeZxid(1, i); + String path = "/foo-" + i; + zkDb.processTxn(new TxnHeader(13, 1000 + i, zxid, 30 + i, ZooDefs.OpCode.create), + new CreateTxn(path, "fpjwasalsohere".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null); + Stat stat = new Stat(); + assertEquals("fpjwasalsohere", new String(zkDb.getData(path, stat, null))); + } + assertTrue(zxid > ZxidUtils.makeZxid(1, 0)); + + // Generate snapshot and close files. + snapLog.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), false); + snapLog.close(); + + QuorumPeer peer = createQuorumPeer(tmpDir); + + leader = createLeader(tmpDir, peer); + peer.leader = leader; + + // Set the last accepted epoch and current epochs to be 1 + peer.setAcceptedEpoch(1); + peer.setCurrentEpoch(1); + + leadThread = new LeadThread(leader); + leadThread.start(); + + while (leader.cnxAcceptor == null || !leader.cnxAcceptor.isAlive()) { + Thread.sleep(20); + } + + LearnerHandler lh = new LearnerHandler(leaderSocket, new BufferedInputStream(leaderSocket.getInputStream()), leader); + lh.start(); + leaderSocket.setSoTimeout(4000); + + InputArchive ia = BinaryInputArchive.getArchive(followerSocket.getInputStream()); + OutputArchive oa = BinaryOutputArchive.getArchive(followerSocket.getOutputStream()); + + conversation.converseWithLeader(ia, oa, leader, zxid); + } finally { + if (leader != null) { + leader.shutdown("end of test"); + } + if (leadThread != null) { + leadThread.interrupt(); + leadThread.join(); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + public void testFollowerConversation(FollowerConversation conversation) throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + Thread followerThread = null; + ConversableFollower follower = null; + QuorumPeer peer = null; + try { + peer = createQuorumPeer(tmpDir); + follower = createFollower(tmpDir, peer); + peer.follower = follower; + + ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1")); + QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress()); + follower.setLeaderQuorumServer(leaderQS); + final Follower followerForThread = follower; + + followerThread = new Thread() { + public void run() { + try { + followerForThread.followLeader(); + } catch (InterruptedException e) { + LOG.info("Follower thread interrupted", e); + } catch (Exception e) { + LOG.warn("Unexpected exception in follower thread", e); + } + } + }; + followerThread.start(); + Socket leaderSocket = ss.accept(); + + InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream()); + OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream()); + + conversation.converseWithFollower(ia, oa, follower); + } finally { + if (follower != null) { + follower.shutdown(); + } + if (followerThread != null) { + followerThread.interrupt(); + followerThread.join(); + } + if (peer != null) { + peer.shutdown(); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + public void testObserverConversation(ObserverConversation conversation) throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + Thread observerThread = null; + ConversableObserver observer = null; + QuorumPeer peer = null; + try { + peer = createQuorumPeer(tmpDir); + peer.setSyncEnabled(true); + observer = createObserver(tmpDir, peer); + peer.observer = observer; + + ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1")); + QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress()); + observer.setLeaderQuorumServer(leaderQS); + final Observer observerForThread = observer; + + observerThread = new Thread() { + public void run() { + try { + observerForThread.observeLeader(); + } catch (Exception e) { + e.printStackTrace(); + } + } + }; + observerThread.start(); + Socket leaderSocket = ss.accept(); + + InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream()); + OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream()); + + conversation.converseWithObserver(ia, oa, observer); + } finally { + if (observer != null) { + observer.shutdown(); + } + if (observerThread != null) { + observerThread.interrupt(); + observerThread.join(); + } + if (peer != null) { + peer.shutdown(); + } + TestUtils.deleteFileRecursively(tmpDir); + } + } + + @Test + public void testUnnecessarySnap() throws Exception { + testPopulatedLeaderConversation(new PopulatedLeaderConversation() { + @Override + public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l, long zxid) throws Exception { + + assertEquals(1, l.self.getAcceptedEpoch()); + assertEquals(1, l.self.getCurrentEpoch()); + + /* we test a normal run. everything should work out well. */ + LearnerInfo li = new LearnerInfo(1, 0x10000, 0); + byte[] liBytes = new byte[20]; + ByteBufferOutputStream.record2ByteBuffer(li, ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 1, liBytes, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.LEADERINFO, qp.getType()); + assertEquals(ZxidUtils.makeZxid(2, 0), qp.getZxid()); + assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), 0x10000); + assertEquals(2, l.self.getAcceptedEpoch()); + assertEquals(1, l.self.getCurrentEpoch()); + + byte[] epochBytes = new byte[4]; + final ByteBuffer wrappedEpochBytes = ByteBuffer.wrap(epochBytes); + wrappedEpochBytes.putInt(1); + qp = new QuorumPacket(Leader.ACKEPOCH, zxid, epochBytes, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.DIFF, qp.getType()); + + } + }, 2); + } + + // We want to track the change with a callback rather than depending on timing + class TrackerWatcher implements Watcher { + + boolean changed; + synchronized void waitForChange() throws InterruptedException { + while (!changed) { + wait(); + } + } + @Override + public void process(WatchedEvent event) { + if (event.getType() == EventType.NodeDataChanged) { + synchronized (this) { + changed = true; + notifyAll(); + } + } + } + public synchronized boolean changed() { + return changed; + } + + } + + @Test + public void testNormalFollowerRun() throws Exception { + testFollowerConversation(new FollowerConversation() { + @Override + public void converseWithFollower(InputArchive ia, OutputArchive oa, Follower f) throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + File logDir = f.fzk.getTxnLogFactory().getDataDir().getParentFile(); + File snapDir = f.fzk.getTxnLogFactory().getSnapDir().getParentFile(); + //Spy on ZK so we can check if a snapshot happened or not. + f.zk = spy(f.zk); + try { + assertEquals(0, f.self.getAcceptedEpoch()); + assertEquals(0, f.self.getCurrentEpoch()); + + // Setup a database with a single /foo node + ZKDatabase zkDb = new ZKDatabase(new FileTxnSnapLog(tmpDir, tmpDir)); + final long firstZxid = ZxidUtils.makeZxid(1, 1); + zkDb.processTxn(new TxnHeader(13, 1313, firstZxid, 33, ZooDefs.OpCode.create), new CreateTxn("/foo", "data1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null); + Stat stat = new Stat(); + assertEquals("data1", new String(zkDb.getData("/foo", stat, null))); + + QuorumPacket qp = new QuorumPacket(); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.FOLLOWERINFO, qp.getType()); + assertEquals(qp.getZxid(), 0); + LearnerInfo learnInfo = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record(ByteBuffer.wrap(qp.getData()), learnInfo); + assertEquals(learnInfo.getProtocolVersion(), 0x10000); + assertEquals(learnInfo.getServerid(), 0); + + // We are simulating an established leader, so the epoch is 1 + qp.setType(Leader.LEADERINFO); + qp.setZxid(ZxidUtils.makeZxid(1, 0)); + byte[] protoBytes = new byte[4]; + ByteBuffer.wrap(protoBytes).putInt(0x10000); + qp.setData(protoBytes); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACKEPOCH, qp.getType()); + assertEquals(0, qp.getZxid()); + assertEquals(ZxidUtils.makeZxid(0, 0), ByteBuffer.wrap(qp.getData()).getInt()); + assertEquals(1, f.self.getAcceptedEpoch()); + assertEquals(0, f.self.getCurrentEpoch()); + + // Send the snapshot we created earlier + qp.setType(Leader.SNAP); + qp.setData(new byte[0]); + qp.setZxid(zkDb.getDataTreeLastProcessedZxid()); + oa.writeRecord(qp, null); + zkDb.serializeSnapshot(oa); + oa.writeString("BenWasHere", null); + Thread.sleep(10); //Give it some time to process the snap + //No Snapshot taken yet, the SNAP was applied in memory + verify(f.zk, never()).takeSnapshot(); + + qp.setType(Leader.NEWLEADER); + qp.setZxid(ZxidUtils.makeZxid(1, 0)); + oa.writeRecord(qp, null); + + // Get the ack of the new leader + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(1, f.self.getAcceptedEpoch()); + assertEquals(1, f.self.getCurrentEpoch()); + //Make sure that we did take the snapshot now + verify(f.zk).takeSnapshot(true); + assertEquals(firstZxid, f.fzk.getLastProcessedZxid()); + + // Make sure the data was recorded in the filesystem ok + ZKDatabase zkDb2 = new ZKDatabase(new FileTxnSnapLog(logDir, snapDir)); + long lastZxid = zkDb2.loadDataBase(); + assertEquals("data1", new String(zkDb2.getData("/foo", stat, null))); + assertEquals(firstZxid, lastZxid); + + // Propose an update + long proposalZxid = ZxidUtils.makeZxid(1, 1000); + proposeSetData(qp, proposalZxid, "data2", 2); + oa.writeRecord(qp, null); + + TrackerWatcher watcher = new TrackerWatcher(); + + // The change should not have happened yet, since we haven't committed + assertEquals("data1", new String(f.fzk.getZKDatabase().getData("/foo", stat, watcher))); + + // The change should happen now + qp.setType(Leader.COMMIT); + qp.setZxid(proposalZxid); + oa.writeRecord(qp, null); + + qp.setType(Leader.UPTODATE); + qp.setZxid(0); + oa.writeRecord(qp, null); + + // Read the uptodate ack + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(proposalZxid, qp.getZxid()); + + watcher.waitForChange(); + assertEquals("data2", new String(f.fzk.getZKDatabase().getData("/foo", stat, null))); + + // check and make sure the change is persisted + zkDb2 = new ZKDatabase(new FileTxnSnapLog(logDir, snapDir)); + lastZxid = zkDb2.loadDataBase(); + assertEquals("data2", new String(zkDb2.getData("/foo", stat, null))); + assertEquals(proposalZxid, lastZxid); + } finally { + TestUtils.deleteFileRecursively(tmpDir); + } + + } + + private void proposeSetData(QuorumPacket qp, long zxid, String data, int version) throws IOException { + qp.setType(Leader.PROPOSAL); + qp.setZxid(zxid); + TxnHeader hdr = new TxnHeader(4, 1414, qp.getZxid(), 55, ZooDefs.OpCode.setData); + SetDataTxn sdt = new SetDataTxn("/foo", data.getBytes(), version); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeRecord(hdr, null); + boa.writeRecord(sdt, null); + qp.setData(baos.toByteArray()); + } + }); + } + + @Test + public void testNormalFollowerRunWithDiff() throws Exception { + testFollowerConversation(new FollowerConversation() { + @Override + public void converseWithFollower(InputArchive ia, OutputArchive oa, Follower f) throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + File logDir = f.fzk.getTxnLogFactory().getDataDir().getParentFile(); + File snapDir = f.fzk.getTxnLogFactory().getSnapDir().getParentFile(); + //Spy on ZK so we can check if a snapshot happened or not. + f.zk = spy(f.zk); + try { + assertEquals(0, f.self.getAcceptedEpoch()); + assertEquals(0, f.self.getCurrentEpoch()); + + // Setup a database with a single /foo node + ZKDatabase zkDb = new ZKDatabase(new FileTxnSnapLog(tmpDir, tmpDir)); + final long firstZxid = ZxidUtils.makeZxid(1, 1); + zkDb.processTxn(new TxnHeader(13, 1313, firstZxid, 33, ZooDefs.OpCode.create), new CreateTxn("/foo", "data1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null); + Stat stat = new Stat(); + assertEquals("data1", new String(zkDb.getData("/foo", stat, null))); + + QuorumPacket qp = new QuorumPacket(); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.FOLLOWERINFO, qp.getType()); + assertEquals(qp.getZxid(), 0); + LearnerInfo learnInfo = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record(ByteBuffer.wrap(qp.getData()), learnInfo); + assertEquals(learnInfo.getProtocolVersion(), 0x10000); + assertEquals(learnInfo.getServerid(), 0); + + // We are simulating an established leader, so the epoch is 1 + qp.setType(Leader.LEADERINFO); + qp.setZxid(ZxidUtils.makeZxid(1, 0)); + byte[] protoBytes = new byte[4]; + ByteBuffer.wrap(protoBytes).putInt(0x10000); + qp.setData(protoBytes); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACKEPOCH, qp.getType()); + assertEquals(0, qp.getZxid()); + assertEquals(ZxidUtils.makeZxid(0, 0), ByteBuffer.wrap(qp.getData()).getInt()); + assertEquals(1, f.self.getAcceptedEpoch()); + assertEquals(0, f.self.getCurrentEpoch()); + + // Send a diff + qp.setType(Leader.DIFF); + qp.setData(new byte[0]); + qp.setZxid(zkDb.getDataTreeLastProcessedZxid()); + oa.writeRecord(qp, null); + final long createSessionZxid = ZxidUtils.makeZxid(1, 2); + proposeNewSession(qp, createSessionZxid, 0x333); + oa.writeRecord(qp, null); + qp.setType(Leader.COMMIT); + qp.setZxid(createSessionZxid); + oa.writeRecord(qp, null); + qp.setType(Leader.NEWLEADER); + qp.setZxid(ZxidUtils.makeZxid(1, 0)); + qp.setData(null); + oa.writeRecord(qp, null); + qp.setType(Leader.UPTODATE); + qp.setZxid(0); + oa.writeRecord(qp, null); + + // Read the uptodate ack + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + + // Get the ack of the new leader + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(1, f.self.getAcceptedEpoch()); + assertEquals(1, f.self.getCurrentEpoch()); + + //Wait for the transactions to be written out. The thread that writes them out + // does not send anything back when it is done. + long start = System.currentTimeMillis(); + while (createSessionZxid != f.fzk.getLastProcessedZxid() + && (System.currentTimeMillis() - start) < 50) { + Thread.sleep(1); + } + + assertEquals(createSessionZxid, f.fzk.getLastProcessedZxid()); + + // Make sure the data was recorded in the filesystem ok + ZKDatabase zkDb2 = new ZKDatabase(new FileTxnSnapLog(logDir, snapDir)); + start = System.currentTimeMillis(); + zkDb2.loadDataBase(); + while (zkDb2.getSessionWithTimeOuts().isEmpty() && (System.currentTimeMillis() - start) < 50) { + Thread.sleep(1); + zkDb2.loadDataBase(); + } + LOG.info("zkdb2 sessions:{}", zkDb2.getSessions()); + LOG.info("zkdb2 with timeouts:{}", zkDb2.getSessionWithTimeOuts()); + assertNotNull(zkDb2.getSessionWithTimeOuts().get(4L)); + //Snapshot was never taken during very simple sync + verify(f.zk, never()).takeSnapshot(); + } finally { + TestUtils.deleteFileRecursively(tmpDir); + } + + } + + private void proposeNewSession(QuorumPacket qp, long zxid, long sessionId) throws IOException { + qp.setType(Leader.PROPOSAL); + qp.setZxid(zxid); + TxnHeader hdr = new TxnHeader(4, 1414, qp.getZxid(), 55, ZooDefs.OpCode.createSession); + CreateSessionTxn cst = new CreateSessionTxn(30000); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeRecord(hdr, null); + boa.writeRecord(cst, null); + qp.setData(baos.toByteArray()); + } + }); + } + + @Test + public void testNormalRun() throws Exception { + testLeaderConversation(new LeaderConversation() { + public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l) throws IOException { + assertEquals(0, l.self.getAcceptedEpoch()); + assertEquals(0, l.self.getCurrentEpoch()); + + /* we test a normal run. everything should work out well. */ + LearnerInfo li = new LearnerInfo(1, 0x10000, 0); + byte[] liBytes = new byte[20]; + ByteBufferOutputStream.record2ByteBuffer(li, ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 0, liBytes, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.LEADERINFO, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), 0x10000); + assertEquals(1, l.self.getAcceptedEpoch()); + assertEquals(0, l.self.getCurrentEpoch()); + + qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.DIFF, qp.getType()); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.NEWLEADER, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(1, l.self.getAcceptedEpoch()); + assertCurrentEpochGotUpdated(1, l.self, ClientBase.CONNECTION_TIMEOUT); + + qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.UPTODATE, qp.getType()); + } + }); + } + + @Test + public void testTxnTimeout() throws Exception { + testLeaderConversation(new LeaderConversation() { + public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l) throws IOException, InterruptedException, org.apache.zookeeper.server.quorum.Leader.XidRolloverException { + assertEquals(0, l.self.getAcceptedEpoch()); + assertEquals(0, l.self.getCurrentEpoch()); + + LearnerInfo li = new LearnerInfo(1, 0x10000, 0); + byte[] liBytes = new byte[20]; + ByteBufferOutputStream.record2ByteBuffer(li, ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 0, liBytes, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.LEADERINFO, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), 0x10000); + assertEquals(1, l.self.getAcceptedEpoch()); + assertEquals(0, l.self.getCurrentEpoch()); + + qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.DIFF, qp.getType()); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.NEWLEADER, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(1, l.self.getAcceptedEpoch()); + assertCurrentEpochGotUpdated(1, l.self, ClientBase.CONNECTION_TIMEOUT); + + qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.UPTODATE, qp.getType()); + + long zxid = l.zk.getZxid(); + l.propose(new Request(1, 1, ZooDefs.OpCode.create, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.create), new CreateTxn("/test", "hola".getBytes(), null, true, 0), zxid)); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.PROPOSAL, qp.getType()); + + LOG.info("Proposal sent."); + + for (int i = 0; i < (2 * ZabUtils.SYNC_LIMIT) + 2; i++) { + try { + ia.readRecord(qp, null); + LOG.info("Ping received: {}", i); + qp = new QuorumPacket(Leader.PING, qp.getZxid(), "".getBytes(), null); + oa.writeRecord(qp, null); + } catch (EOFException e) { + return; + } + } + fail("Connection hasn't been closed by leader after transaction times out."); + } + }); + } + + private void deserializeSnapshot(InputArchive ia) throws IOException { + ZKDatabase zkdb = new ZKDatabase(null); + zkdb.deserializeSnapshot(ia); + String signature = ia.readString("signature"); + assertEquals("BenWasHere", signature); + } + + @Test + public void testNormalObserverRun() throws Exception { + testObserverConversation(new ObserverConversation() { + @Override + public void converseWithObserver(InputArchive ia, OutputArchive oa, Observer o) throws Exception { + File tmpDir = File.createTempFile("test", "dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + File logDir = o.zk.getTxnLogFactory().getDataDir().getParentFile(); + File snapDir = o.zk.getTxnLogFactory().getSnapDir().getParentFile(); + try { + assertEquals(0, o.self.getAcceptedEpoch()); + assertEquals(0, o.self.getCurrentEpoch()); + + // Setup a database with a single /foo node + ZKDatabase zkDb = new ZKDatabase(new FileTxnSnapLog(tmpDir, tmpDir)); + final long foo1Zxid = ZxidUtils.makeZxid(1, 1); + final long foo2Zxid = ZxidUtils.makeZxid(1, 2); + zkDb.processTxn(new TxnHeader(13, 1313, foo1Zxid, 33, ZooDefs.OpCode.create), new CreateTxn("/foo1", "data1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null); + zkDb.processTxn(new TxnHeader(13, 1313, foo2Zxid, 33, ZooDefs.OpCode.create), new CreateTxn("/foo2", "data1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, false, 1), null); + Stat stat = new Stat(); + assertEquals("data1", new String(zkDb.getData("/foo1", stat, null))); + assertEquals("data1", new String(zkDb.getData("/foo2", stat, null))); + + QuorumPacket qp = new QuorumPacket(); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.OBSERVERINFO, qp.getType()); + assertEquals(qp.getZxid(), 0); + LearnerInfo learnInfo = new LearnerInfo(); + ByteBufferInputStream.byteBuffer2Record(ByteBuffer.wrap(qp.getData()), learnInfo); + assertEquals(learnInfo.getProtocolVersion(), 0x10000); + assertEquals(learnInfo.getServerid(), 0); + + // We are simulating an established leader, so the epoch is 1 + qp.setType(Leader.LEADERINFO); + qp.setZxid(ZxidUtils.makeZxid(1, 0)); + byte[] protoBytes = new byte[4]; + ByteBuffer.wrap(protoBytes).putInt(0x10000); + qp.setData(protoBytes); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACKEPOCH, qp.getType()); + assertEquals(0, qp.getZxid()); + assertEquals(ZxidUtils.makeZxid(0, 0), ByteBuffer.wrap(qp.getData()).getInt()); + assertEquals(1, o.self.getAcceptedEpoch()); + assertEquals(0, o.self.getCurrentEpoch()); + + // Send the snapshot we created earlier + qp.setType(Leader.SNAP); + qp.setData(new byte[0]); + qp.setZxid(zkDb.getDataTreeLastProcessedZxid()); + oa.writeRecord(qp, null); + zkDb.serializeSnapshot(oa); + oa.writeString("BenWasHere", null); + qp.setType(Leader.NEWLEADER); + qp.setZxid(ZxidUtils.makeZxid(1, 0)); + oa.writeRecord(qp, null); + + // Get the ack of the new leader + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(1, o.self.getAcceptedEpoch()); + assertEquals(1, o.self.getCurrentEpoch()); + + assertEquals(foo2Zxid, o.zk.getLastProcessedZxid()); + + // Make sure the data was recorded in the filesystem ok + ZKDatabase zkDb2 = new ZKDatabase(new FileTxnSnapLog(logDir, snapDir)); + long lastZxid = zkDb2.loadDataBase(); + assertEquals("data1", new String(zkDb2.getData("/foo1", stat, null))); + assertEquals(foo2Zxid, lastZxid); + + // Register watch + TrackerWatcher watcher = new TrackerWatcher(); + assertEquals("data1", new String(o.zk.getZKDatabase().getData("/foo2", stat, watcher))); + + // Propose /foo1 update + long proposalZxid = ZxidUtils.makeZxid(1, 1000); + proposeSetData(qp, "/foo1", proposalZxid, "data2", 2); + oa.writeRecord(qp, null); + + // Commit /foo1 update + qp.setType(Leader.COMMIT); + qp.setZxid(proposalZxid); + oa.writeRecord(qp, null); + + // Inform /foo2 update + long informZxid = ZxidUtils.makeZxid(1, 1001); + proposeSetData(qp, "/foo2", informZxid, "data2", 2); + qp.setType(Leader.INFORM); + oa.writeRecord(qp, null); + + qp.setType(Leader.UPTODATE); + qp.setZxid(0); + oa.writeRecord(qp, null); + + // Read the uptodate ack + readPacketSkippingPing(ia, qp); + assertEquals(Leader.ACK, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + + // Data should get updated + watcher.waitForChange(); + assertEquals("data2", new String(o.zk.getZKDatabase().getData("/foo1", stat, null))); + assertEquals("data2", new String(o.zk.getZKDatabase().getData("/foo2", stat, null))); + + // Shutdown sequence guarantee that all pending requests + // in sync request processor get flush to disk + o.zk.shutdown(); + + zkDb2 = new ZKDatabase(new FileTxnSnapLog(logDir, snapDir)); + lastZxid = zkDb2.loadDataBase(); + assertEquals("data2", new String(zkDb2.getData("/foo1", stat, null))); + assertEquals("data2", new String(zkDb2.getData("/foo2", stat, null))); + assertEquals(informZxid, lastZxid); + } finally { + TestUtils.deleteFileRecursively(tmpDir); + } + + } + + private void proposeSetData(QuorumPacket qp, String path, long zxid, String data, int version) throws IOException { + qp.setType(Leader.PROPOSAL); + qp.setZxid(zxid); + TxnHeader hdr = new TxnHeader(4, 1414, qp.getZxid(), 55, ZooDefs.OpCode.setData); + SetDataTxn sdt = new SetDataTxn(path, data.getBytes(), version); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeRecord(hdr, null); + boa.writeRecord(sdt, null); + qp.setData(baos.toByteArray()); + } + }); + } + + @Test + public void testLeaderBehind() throws Exception { + testLeaderConversation(new LeaderConversation() { + public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l) throws IOException { + /* we test a normal run. everything should work out well. */ + LearnerInfo li = new LearnerInfo(1, 0x10000, 0); + byte[] liBytes = new byte[20]; + ByteBufferOutputStream.record2ByteBuffer(li, ByteBuffer.wrap(liBytes)); + /* we are going to say we last acked epoch 20 */ + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, ZxidUtils.makeZxid(20, 0), liBytes, null); + oa.writeRecord(qp, null); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.LEADERINFO, qp.getType()); + assertEquals(ZxidUtils.makeZxid(21, 0), qp.getZxid()); + assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), 0x10000); + qp = new QuorumPacket(Leader.ACKEPOCH, 0, new byte[4], null); + oa.writeRecord(qp, null); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.DIFF, qp.getType()); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.NEWLEADER, qp.getType()); + assertEquals(ZxidUtils.makeZxid(21, 0), qp.getZxid()); + + qp = new QuorumPacket(Leader.ACK, qp.getZxid(), null, null); + oa.writeRecord(qp, null); + + readPacketSkippingPing(ia, qp); + assertEquals(Leader.UPTODATE, qp.getType()); + } + }); + } + + /** + * Tests that when a quorum of followers send LearnerInfo but do not ack the epoch (which is sent + * by the leader upon receipt of LearnerInfo from a quorum), the leader does not start using this epoch + * as it would in the normal case (when a quorum do ack the epoch). This tests ZK-1192 + * @throws Exception + */ + @Test + public void testAbandonBeforeACKEpoch() throws Exception { + testLeaderConversation(new LeaderConversation() { + public void converseWithLeader(InputArchive ia, OutputArchive oa, Leader l) throws IOException, InterruptedException { + /* we test a normal run. everything should work out well. */ + LearnerInfo li = new LearnerInfo(1, 0x10000, 0); + byte[] liBytes = new byte[20]; + ByteBufferOutputStream.record2ByteBuffer(li, ByteBuffer.wrap(liBytes)); + QuorumPacket qp = new QuorumPacket(Leader.FOLLOWERINFO, 0, liBytes, null); + oa.writeRecord(qp, null); + readPacketSkippingPing(ia, qp); + assertEquals(Leader.LEADERINFO, qp.getType()); + assertEquals(ZxidUtils.makeZxid(1, 0), qp.getZxid()); + assertEquals(ByteBuffer.wrap(qp.getData()).getInt(), 0x10000); + Thread.sleep(l.self.getInitLimit() * l.self.getTickTime() + 5000); + + // The leader didn't get a quorum of acks - make sure that leader's current epoch is not advanced + assertEquals(0, l.self.getCurrentEpoch()); + } + }); + } + + static class ConversableFollower extends Follower { + + ConversableFollower(QuorumPeer self, FollowerZooKeeperServer zk) { + super(self, zk); + } + + QuorumServer leaderQuorumServer; + public void setLeaderQuorumServer(QuorumServer quorumServer) { + leaderQuorumServer = quorumServer; + } + + @Override + protected QuorumServer findLeader() { + return leaderQuorumServer; + } + + } + private ConversableFollower createFollower(File tmpDir, QuorumPeer peer) throws IOException { + FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); + peer.setTxnFactory(logFactory); + ZKDatabase zkDb = new ZKDatabase(logFactory); + FollowerZooKeeperServer zk = new FollowerZooKeeperServer(logFactory, peer, zkDb); + peer.setZKDatabase(zkDb); + return new ConversableFollower(peer, zk); + } + + static class ConversableObserver extends Observer { + + ConversableObserver(QuorumPeer self, ObserverZooKeeperServer zk) { + super(self, zk); + } + + QuorumServer leaderQuorumServer; + public void setLeaderQuorumServer(QuorumServer quorumServer) { + leaderQuorumServer = quorumServer; + } + + @Override + protected QuorumServer findLeader() { + return leaderQuorumServer; + } + + } + + private ConversableObserver createObserver(File tmpDir, QuorumPeer peer) throws IOException { + FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); + peer.setTxnFactory(logFactory); + ZKDatabase zkDb = new ZKDatabase(logFactory); + ObserverZooKeeperServer zk = new ObserverZooKeeperServer(logFactory, peer, zkDb); + peer.setZKDatabase(zkDb); + return new ConversableObserver(peer, zk); + } + + private String readContentsOfFile(File f) throws IOException { + return new BufferedReader(new FileReader(f)).readLine(); + } + + @Test + public void testInitialAcceptedCurrent() throws Exception { + File tmpDir = File.createTempFile("test", ".dir", testData); + tmpDir.delete(); + tmpDir.mkdir(); + try { + FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); + File version2 = new File(tmpDir, "version-2"); + version2.mkdir(); + logFactory.save(new DataTree(), new ConcurrentHashMap(), false); + long zxid = ZxidUtils.makeZxid(3, 3); + logFactory.append(new Request(1, 1, ZooDefs.OpCode.error, new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error), new ErrorTxn(1), zxid)); + logFactory.commit(); + ZKDatabase zkDb = new ZKDatabase(logFactory); + QuorumPeer peer = QuorumPeer.testingQuorumPeer(); + peer.setZKDatabase(zkDb); + peer.setTxnFactory(logFactory); + peer.getLastLoggedZxid(); + assertEquals(3, peer.getAcceptedEpoch()); + assertEquals(3, peer.getCurrentEpoch()); + assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME)))); + assertEquals(3, Integer.parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME)))); + } finally { + TestUtils.deleteFileRecursively(tmpDir); + } + } + + /* + * Epoch is first written to file then updated in memory. Give some time to + * write the epoch in file and then go for assert. + */ + private void assertCurrentEpochGotUpdated(int expected, QuorumPeer self, long timeout) + throws IOException { + long elapsedTime = 0; + long waitInterval = 10; + while (self.getCurrentEpoch() != expected && elapsedTime < timeout) { + try { + Thread.sleep(waitInterval); + } catch (InterruptedException e) { + fail("CurrentEpoch update failed"); + } + elapsedTime = elapsedTime + waitInterval; + } + assertEquals("CurrentEpoch update failed", expected, self.getCurrentEpoch()); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ZabUtils.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ZabUtils.java new file mode 100644 index 0000000..fff5e62 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/ZabUtils.java @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.test.ClientBase; + +public class ZabUtils { + + private ZabUtils() { + } + + public static final int SYNC_LIMIT = 2; + + public static QuorumPeer createQuorumPeer(File tmpDir) throws IOException { + HashMap peers = new HashMap(); + QuorumPeer peer = QuorumPeer.testingQuorumPeer(); + peer.syncLimit = SYNC_LIMIT; + peer.initLimit = 2; + peer.tickTime = 2000; + + peers.put(0L, new QuorumPeer.QuorumServer(0, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + peers.put(1L, new QuorumPeer.QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + peers.put(2L, new QuorumPeer.QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + + peer.setQuorumVerifier(new QuorumMaj(peers), false); + peer.setCnxnFactory(new NullServerCnxnFactory()); + File version2 = new File(tmpDir, "version-2"); + version2.mkdir(); + ClientBase.createInitializeFile(tmpDir); + FileOutputStream fos = new FileOutputStream(new File(version2, "currentEpoch")); + fos.write("0\n".getBytes()); + fos.close(); + fos = new FileOutputStream(new File(version2, "acceptedEpoch")); + fos.write("0\n".getBytes()); + fos.close(); + return peer; + } + + public static Leader createLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException, X509Exception { + LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer); + return new Leader(peer, zk); + } + + public static Leader createMockLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException, X509Exception { + LeaderZooKeeperServer zk = prepareLeader(tmpDir, peer); + return new MockLeader(peer, zk); + } + + private static LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException { + FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); + peer.setTxnFactory(logFactory); + ZKDatabase zkDb = new ZKDatabase(logFactory); + LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer, zkDb); + return zk; + } + + private static final class NullServerCnxnFactory extends ServerCnxnFactory { + + public void startup(ZooKeeperServer zkServer, boolean startServer) throws IOException, InterruptedException { + } + public void start() { + } + public void shutdown() { + } + public void setMaxClientCnxnsPerHost(int max) { + } + public void join() throws InterruptedException { + } + public int getMaxClientCnxnsPerHost() { + return 0; + } + public int getSocketListenBacklog() { + return -1; + } + public int getLocalPort() { + return 0; + } + public InetSocketAddress getLocalAddress() { + return null; + } + public Iterable getConnections() { + return null; + } + public void configure(InetSocketAddress addr, int maxcc, int listenBacklog, boolean secure) throws IOException { + } + + @Override + public boolean closeSession(long sessionId, ServerCnxn.DisconnectReason reason) { + return false; + } + @Override + public void closeAll(ServerCnxn.DisconnectReason reason) { + } + @Override + public int getNumAliveConnections() { + return 0; + } + @Override + public void reconfigure(InetSocketAddress addr) { + } + @Override + public void resetAllConnectionStats() { + } + @Override + public Iterable> getAllConnectionInfo(boolean brief) { + return null; + } + + } + + public static final class MockLeader extends Leader { + + MockLeader(QuorumPeer qp, LeaderZooKeeperServer zk) throws IOException, X509Exception { + super(qp, zk); + } + + /** + * This method returns the value of the variable that holds the epoch + * to be proposed and that has been proposed, depending on the point + * of the execution in which it is called. + * + * @return epoch + */ + public long getCurrentEpochToPropose() { + return epoch; + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java new file mode 100644 index 0000000..cc6af7b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/KerberosSecurityTestcase.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.util.Properties; +import org.apache.commons.io.FileUtils; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +/* + * This code is originally from HDFS, see the similarly named file there + * in case of bug fixing, history, etc. + * + * Branch : trunk + * Github Revision: 1d1ab587e4e92ce3aea4cb144811f69145cb3b33 + */ + +/** + * KerberosSecurityTestcase provides a base class for using MiniKdc with other + * test cases. KerberosSecurityTestcase starts the MiniKdc (@Before) before + * running tests, and stop the MiniKdc (@After) after the testcases, using + * default settings (working dir and kdc configurations). + *

      + * Users can directly inherit this class and implement their own test functions + * using the default settings, or override functions getTestDir() and + * createMiniKdcConf() to provide new settings. + */ +public class KerberosSecurityTestcase extends QuorumAuthTestBase { + + private static MiniKdc kdc; + private static File workDir; + private static Properties conf; + + @BeforeClass + public static void setUpSasl() throws Exception { + startMiniKdc(); + } + + @AfterClass + public static void tearDownSasl() throws Exception { + stopMiniKdc(); + FileUtils.deleteQuietly(workDir); + } + + public static void startMiniKdc() throws Exception { + createTestDir(); + createMiniKdcConf(); + + kdc = new MiniKdc(conf, workDir); + kdc.start(); + } + + /** + * Create a working directory, it should be the build directory. Under this + * directory an ApacheDS working directory will be created, this directory + * will be deleted when the MiniKdc stops. + * + * @throws IOException + */ + public static void createTestDir() throws IOException { + workDir = createTmpDir(new File(System.getProperty("build.test.dir", "build"))); + } + + static File createTmpDir(File parentDir) throws IOException { + File tmpFile = File.createTempFile("test", ".junit", parentDir); + // don't delete tmpFile - this ensures we don't attempt to create + // a tmpDir with a duplicate name + File tmpDir = new File(tmpFile + ".dir"); + // never true if tmpfile does it's job + assertFalse(tmpDir.exists()); + assertTrue(tmpDir.mkdirs()); + return tmpDir; + } + + /** + * Create a Kdc configuration + */ + public static void createMiniKdcConf() { + conf = MiniKdc.createConf(); + } + + public static void stopMiniKdc() { + if (kdc != null) { + kdc.stop(); + } + } + + public static MiniKdc getKdc() { + return kdc; + } + + public static File getWorkDir() { + return workDir; + } + + public static Properties getConf() { + return conf; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java new file mode 100644 index 0000000..755712b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/KerberosTestUtils.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.File; +import java.util.UUID; +import org.apache.zookeeper.util.SecurityUtils; + +public class KerberosTestUtils { + + private static String keytabFile = new File(System.getProperty("build.test.dir", "build"), UUID.randomUUID().toString()).getAbsolutePath(); + + public static String getRealm() { + return "EXAMPLE.COM"; + } + + public static String getLearnerPrincipal() { + return "learner@EXAMPLE.COM"; + } + + public static String getServerPrincipal() { + return "zkquorum/localhost@EXAMPLE.COM"; + } + + public static String getClientPrincipal() { + return getClientUsername() + "/localhost@EXAMPLE.COM"; + } + + public static String getClientUsername() { + return "zkclient"; + } + + public static String getHostLearnerPrincipal() { + return "learner/_HOST@EXAMPLE.COM"; + } + + public static String getHostServerPrincipal() { + return "zkquorum/_HOST@EXAMPLE.COM"; + } + + public static String getHostNamedLearnerPrincipal(String myHostname) { + return "learner/" + myHostname + "@EXAMPLE.COM"; + } + + public static String getKeytabFile() { + return keytabFile; + } + + public static String replaceHostPattern(String principal) { + String[] components = principal.split("[/@]"); + if (components == null + || components.length < 2 + || !components[1].equals(SecurityUtils.QUORUM_HOSTNAME_PATTERN)) { + return principal; + } else { + return replacePattern(components, "localhost"); + } + } + + public static String replacePattern(String[] components, String hostname) { + if (components.length == 3) { + return components[0] + "/" + hostname.toLowerCase() + "@" + components[2]; + } else { + return components[0] + "/" + hostname.toLowerCase(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/MiniKdc.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/MiniKdc.java new file mode 100644 index 0000000..4adcc0b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/MiniKdc.java @@ -0,0 +1,403 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import org.apache.kerby.kerberos.kerb.KrbException; +import org.apache.kerby.kerberos.kerb.server.KdcConfigKey; +import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; +import org.apache.kerby.util.IOUtil; +import org.apache.kerby.util.NetworkUtil; +import org.apache.zookeeper.server.ExitCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Mini KDC based on Apache Directory Server that can be embedded in testcases + * or used from command line as a standalone KDC. + *

      + * From within testcases: + *

      + * MiniKdc sets one System property when started and un-set when stopped: + *

        + *
      • sun.security.krb5.debug: set to the debug value provided in the + * configuration
      • + *
      + * Because of this, multiple MiniKdc instances cannot be started in parallel. + * For example, running testcases in parallel that start a KDC each. To + * accomplish this a single MiniKdc should be used for all testcases running + * in parallel. + *

      + * MiniKdc default configuration values are: + *

        + *
      • org.name=EXAMPLE (used to create the REALM)
      • + *
      • org.domain=COM (used to create the REALM)
      • + *
      • kdc.bind.address=localhost
      • + *
      • kdc.port=0 (ephemeral port)
      • + *
      • instance=DefaultKrbServer
      • + *
      • max.ticket.lifetime=86400000 (1 day)
      • + *
      • max.renewable.lifetime=604800000 (7 days)
      • + *
      • transport=TCP
      • + *
      • debug=false
      • + *
      + * The generated krb5.conf forces TCP connections. + */ +/* + * This code is originally from HDFS, see the file name MiniKdc there + * in case of bug fixing, history, etc. + * + * Branch : trunk + * Github Revision: 916140604ffef59466ba30832478311d3e6249bd + */ +public class MiniKdc { + + public static final String JAVA_SECURITY_KRB5_CONF = "java.security.krb5.conf"; + public static final String SUN_SECURITY_KRB5_DEBUG = "sun.security.krb5.debug"; + + public static void main(String[] args) throws Exception { + if (args.length < 4) { + System.out.println("Arguments: " + " []+"); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + File workDir = new File(args[0]); + if (!workDir.exists()) { + throw new RuntimeException("Specified work directory does not exists: " + workDir.getAbsolutePath()); + } + Properties conf = createConf(); + File file = new File(args[1]); + if (!file.exists()) { + throw new RuntimeException("Specified configuration does not exists: " + file.getAbsolutePath()); + } + Properties userConf = new Properties(); + InputStreamReader r = null; + try { + r = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); + userConf.load(r); + } finally { + if (r != null) { + r.close(); + } + } + for (Map.Entry entry : userConf.entrySet()) { + conf.put(entry.getKey(), entry.getValue()); + } + final MiniKdc miniKdc = new MiniKdc(conf, workDir); + miniKdc.start(); + File krb5conf = new File(workDir, "krb5.conf"); + if (miniKdc.getKrb5conf().renameTo(krb5conf)) { + File keytabFile = new File(args[2]).getAbsoluteFile(); + String[] principals = new String[args.length - 3]; + System.arraycopy(args, 3, principals, 0, args.length - 3); + miniKdc.createPrincipal(keytabFile, principals); + System.out.println(); + System.out.println("Standalone MiniKdc Running"); + System.out.println("---------------------------------------------------"); + System.out.println(" Realm : " + miniKdc.getRealm()); + System.out.println(" Running at : " + miniKdc.getHost() + ":" + miniKdc.getHost()); + System.out.println(" krb5conf : " + krb5conf); + System.out.println(); + System.out.println(" created keytab : " + keytabFile); + System.out.println(" with principals : " + Arrays.asList(principals)); + System.out.println(); + System.out.println(" Do or kill to stop it"); + System.out.println("---------------------------------------------------"); + System.out.println(); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + miniKdc.stop(); + } + }); + } else { + throw new RuntimeException("Cannot rename KDC's krb5conf to " + krb5conf.getAbsolutePath()); + } + } + + private static final Logger LOG = LoggerFactory.getLogger(MiniKdc.class); + + public static final String ORG_NAME = "org.name"; + public static final String ORG_DOMAIN = "org.domain"; + public static final String KDC_BIND_ADDRESS = "kdc.bind.address"; + public static final String KDC_PORT = "kdc.port"; + public static final String INSTANCE = "instance"; + public static final String MAX_TICKET_LIFETIME = "max.ticket.lifetime"; + public static final String MAX_RENEWABLE_LIFETIME = "max.renewable.lifetime"; + public static final String TRANSPORT = "transport"; + public static final String DEBUG = "debug"; + + private static final Set PROPERTIES = new HashSet(); + private static final Properties DEFAULT_CONFIG = new Properties(); + + static { + PROPERTIES.add(ORG_NAME); + PROPERTIES.add(ORG_DOMAIN); + PROPERTIES.add(KDC_BIND_ADDRESS); + PROPERTIES.add(KDC_BIND_ADDRESS); + PROPERTIES.add(KDC_PORT); + PROPERTIES.add(INSTANCE); + PROPERTIES.add(TRANSPORT); + PROPERTIES.add(MAX_TICKET_LIFETIME); + PROPERTIES.add(MAX_RENEWABLE_LIFETIME); + + DEFAULT_CONFIG.setProperty(KDC_BIND_ADDRESS, "localhost"); + DEFAULT_CONFIG.setProperty(KDC_PORT, "0"); + DEFAULT_CONFIG.setProperty(INSTANCE, "DefaultKrbServer"); + DEFAULT_CONFIG.setProperty(ORG_NAME, "EXAMPLE"); + DEFAULT_CONFIG.setProperty(ORG_DOMAIN, "COM"); + DEFAULT_CONFIG.setProperty(TRANSPORT, "TCP"); + DEFAULT_CONFIG.setProperty(MAX_TICKET_LIFETIME, "86400000"); + DEFAULT_CONFIG.setProperty(MAX_RENEWABLE_LIFETIME, "604800000"); + DEFAULT_CONFIG.setProperty(DEBUG, "false"); + } + + /** + * Convenience method that returns MiniKdc default configuration. + *

      + * The returned configuration is a copy, it can be customized before using + * it to create a MiniKdc. + * @return a MiniKdc default configuration. + */ + public static Properties createConf() { + return (Properties) DEFAULT_CONFIG.clone(); + } + + private Properties conf; + private SimpleKdcServer simpleKdc; + private int port; + private String realm; + private File workDir; + private File krb5conf; + private String transport; + private boolean krb5Debug; + + public void setTransport(String transport) { + this.transport = transport; + } + /** + * Creates a MiniKdc. + * + * @param conf MiniKdc configuration. + * @param workDir working directory, it should be the build directory. Under + * this directory an ApacheDS working directory will be created, this + * directory will be deleted when the MiniKdc stops. + * @throws Exception thrown if the MiniKdc could not be created. + */ + public MiniKdc(Properties conf, File workDir) throws Exception { + if (!conf.keySet().containsAll(PROPERTIES)) { + Set missingProperties = new HashSet(PROPERTIES); + missingProperties.removeAll(conf.keySet()); + throw new IllegalArgumentException("Missing configuration properties: " + missingProperties); + } + this.workDir = new File(workDir, Long.toString(System.currentTimeMillis())); + if (!this.workDir.exists() && !this.workDir.mkdirs()) { + throw new RuntimeException("Cannot create directory " + this.workDir); + } + LOG.info("Configuration:"); + LOG.info("---------------------------------------------------------------"); + for (Map.Entry entry : conf.entrySet()) { + LOG.info(" {}: {}", entry.getKey(), entry.getValue()); + } + LOG.info("---------------------------------------------------------------"); + this.conf = conf; + port = Integer.parseInt(conf.getProperty(KDC_PORT)); + String orgName = conf.getProperty(ORG_NAME); + String orgDomain = conf.getProperty(ORG_DOMAIN); + realm = orgName.toUpperCase(Locale.ENGLISH) + "." + orgDomain.toUpperCase(Locale.ENGLISH); + } + + /** + * Returns the port of the MiniKdc. + * + * @return the port of the MiniKdc. + */ + public int getPort() { + return port; + } + + /** + * Returns the host of the MiniKdc. + * + * @return the host of the MiniKdc. + */ + public String getHost() { + return conf.getProperty(KDC_BIND_ADDRESS); + } + + /** + * Returns the realm of the MiniKdc. + * + * @return the realm of the MiniKdc. + */ + public String getRealm() { + return realm; + } + + public File getKrb5conf() { + krb5conf = new File(System.getProperty(JAVA_SECURITY_KRB5_CONF)); + return krb5conf; + } + + /** + * Starts the MiniKdc. + * + * @throws Exception thrown if the MiniKdc could not be started. + */ + public synchronized void start() throws Exception { + if (simpleKdc != null) { + throw new RuntimeException("Already started"); + } + simpleKdc = new SimpleKdcServer(); + prepareKdcServer(); + simpleKdc.init(); + resetDefaultRealm(); + simpleKdc.start(); + LOG.info("MiniKdc stated."); + } + + private void resetDefaultRealm() throws IOException { + InputStream templateResource = new FileInputStream(getKrb5conf().getAbsolutePath()); + String content = IOUtil.readInput(templateResource); + content = content.replaceAll("default_realm = .*\n", "default_realm = " + getRealm() + "\n"); + IOUtil.writeFile(content, getKrb5conf()); + } + + private void prepareKdcServer() throws Exception { + // transport + simpleKdc.setWorkDir(workDir); + simpleKdc.setKdcHost(getHost()); + simpleKdc.setKdcRealm(realm); + if (transport == null) { + transport = conf.getProperty(TRANSPORT); + } + if (port == 0) { + port = NetworkUtil.getServerPort(); + } + if (transport != null) { + if (transport.trim().equals("TCP")) { + simpleKdc.setKdcTcpPort(port); + simpleKdc.setAllowUdp(false); + } else if (transport.trim().equals("UDP")) { + simpleKdc.setKdcUdpPort(port); + simpleKdc.setAllowTcp(false); + } else { + throw new IllegalArgumentException("Invalid transport: " + transport); + } + } else { + throw new IllegalArgumentException("Need to set transport!"); + } + simpleKdc.getKdcConfig().setString(KdcConfigKey.KDC_SERVICE_NAME, conf.getProperty(INSTANCE)); + if (conf.getProperty(DEBUG) != null) { + krb5Debug = getAndSet(SUN_SECURITY_KRB5_DEBUG, conf.getProperty(DEBUG)); + } + } + + /** + * Stops the MiniKdc + */ + public synchronized void stop() { + if (simpleKdc != null) { + try { + simpleKdc.stop(); + } catch (KrbException e) { + e.printStackTrace(); + } finally { + if (conf.getProperty(DEBUG) != null) { + System.setProperty(SUN_SECURITY_KRB5_DEBUG, Boolean.toString(krb5Debug)); + } + } + } + delete(workDir); + try { + // Will be fixed in next Kerby version. + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + LOG.info("MiniKdc stopped."); + } + + private void delete(File f) { + if (f.isFile()) { + if (!f.delete()) { + LOG.warn("WARNING: cannot delete file {}", f.getAbsolutePath()); + } + } else { + for (File c : f.listFiles()) { + delete(c); + } + if (!f.delete()) { + LOG.warn("WARNING: cannot delete directory {}", f.getAbsolutePath()); + } + } + } + + /** + * Creates a principal in the KDC with the specified user and password. + * + * @param principal principal name, do not include the domain. + * @param password password. + * @throws Exception thrown if the principal could not be created. + */ + public synchronized void createPrincipal(String principal, String password) throws Exception { + simpleKdc.createPrincipal(principal, password); + } + + /** + * Creates multiple principals in the KDC and adds them to a keytab file. + * + * @param keytabFile keytab file to add the created principals. + * @param principals principals to add to the KDC, do not include the domain. + * @throws Exception thrown if the principals or the keytab file could not be + * created. + */ + public synchronized void createPrincipal(File keytabFile, String... principals) throws Exception { + simpleKdc.createPrincipals(principals); + if (keytabFile.exists() && !keytabFile.delete()) { + LOG.error("Failed to delete keytab file: {}", keytabFile); + } + for (String principal : principals) { + simpleKdc.getKadmin().exportKeytab(keytabFile, principal); + } + } + + /** + * Set the System property; return the old value for caching. + * + * @param sysprop property + * @param debug true or false + * @return the previous value + */ + private boolean getAndSet(String sysprop, String debug) { + boolean old = Boolean.getBoolean(sysprop); + System.setProperty(sysprop, debug); + return old; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java new file mode 100644 index 0000000..6595515 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/MiniKdcTest.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import java.io.File; +import java.security.Principal; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import javax.security.auth.Subject; +import javax.security.auth.kerberos.KerberosPrincipal; +import javax.security.auth.login.AppConfigurationEntry; +import javax.security.auth.login.Configuration; +import javax.security.auth.login.LoginContext; +import org.apache.kerby.kerberos.kerb.keytab.Keytab; +import org.apache.kerby.kerberos.kerb.type.base.PrincipalName; +import org.junit.Test; + +/* + * This code is originally from HDFS, see the file name TestMiniKdc there + * in case of bug fixing, history, etc. + * + * Branch : trunk + * Github Revision: 916140604ffef59466ba30832478311d3e6249bd + */ +public class MiniKdcTest extends KerberosSecurityTestcase { + + private static final boolean IBM_JAVA = System.getProperty("java.vendor").contains("IBM"); + + @Test(timeout = 60000) + public void testMiniKdcStart() { + MiniKdc kdc = getKdc(); + assertNotSame(0, kdc.getPort()); + } + + @Test(timeout = 60000) + public void testKeytabGen() throws Exception { + MiniKdc kdc = getKdc(); + File workDir = getWorkDir(); + + kdc.createPrincipal(new File(workDir, "keytab"), "foo/bar", "bar/foo"); + List principalNameList = Keytab.loadKeytab(new File(workDir, "keytab")).getPrincipals(); + + Set principals = new HashSet(); + for (PrincipalName principalName : principalNameList) { + principals.add(principalName.getName()); + } + + assertEquals( + new HashSet<>(Arrays.asList("foo/bar@" + kdc.getRealm(), "bar/foo@" + kdc.getRealm())), + principals); + } + + private static class KerberosConfiguration extends Configuration { + + private String principal; + private String keytab; + private boolean isInitiator; + + private KerberosConfiguration(String principal, File keytab, boolean client) { + this.principal = principal; + this.keytab = keytab.getAbsolutePath(); + this.isInitiator = client; + } + + public static Configuration createClientConfig(String principal, File keytab) { + return new KerberosConfiguration(principal, keytab, true); + } + + public static Configuration createServerConfig(String principal, File keytab) { + return new KerberosConfiguration(principal, keytab, false); + } + + private static String getKrb5LoginModuleName() { + return System.getProperty("java.vendor").contains("IBM") + ? "com.ibm.security.auth.module.Krb5LoginModule" + : "com.sun.security.auth.module.Krb5LoginModule"; + } + + @Override + public AppConfigurationEntry[] getAppConfigurationEntry(String name) { + Map options = new HashMap(); + options.put("principal", principal); + options.put("refreshKrb5Config", "true"); + if (IBM_JAVA) { + options.put("useKeytab", keytab); + options.put("credsType", "both"); + } else { + options.put("keyTab", keytab); + options.put("useKeyTab", "true"); + options.put("storeKey", "true"); + options.put("doNotPrompt", "true"); + options.put("useTicketCache", "true"); + options.put("renewTGT", "true"); + options.put("isInitiator", Boolean.toString(isInitiator)); + } + String ticketCache = System.getenv("KRB5CCNAME"); + if (ticketCache != null) { + options.put("ticketCache", ticketCache); + } + options.put("debug", "true"); + + return new AppConfigurationEntry[]{new AppConfigurationEntry(getKrb5LoginModuleName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options)}; + } + + } + + @Test(timeout = 60000) + public void testKerberosLogin() throws Exception { + MiniKdc kdc = getKdc(); + File workDir = getWorkDir(); + LoginContext loginContext = null; + try { + String principal = "foo"; + File keytab = new File(workDir, "foo.keytab"); + kdc.createPrincipal(keytab, principal); + + Set principals = new HashSet(); + principals.add(new KerberosPrincipal(principal)); + + // client login + Subject subject = new Subject(false, principals, new HashSet(), new HashSet()); + loginContext = new LoginContext("", subject, null, KerberosConfiguration.createClientConfig(principal, keytab)); + loginContext.login(); + subject = loginContext.getSubject(); + assertEquals(1, subject.getPrincipals().size()); + assertEquals(KerberosPrincipal.class, subject.getPrincipals().iterator().next().getClass()); + assertEquals(principal + "@" + kdc.getRealm(), subject.getPrincipals().iterator().next().getName()); + loginContext.logout(); + + // server login + subject = new Subject(false, principals, new HashSet(), new HashSet()); + loginContext = new LoginContext("", subject, null, KerberosConfiguration.createServerConfig(principal, keytab)); + loginContext.login(); + subject = loginContext.getSubject(); + assertEquals(1, subject.getPrincipals().size()); + assertEquals(KerberosPrincipal.class, subject.getPrincipals().iterator().next().getClass()); + assertEquals(principal + "@" + kdc.getRealm(), subject.getPrincipals().iterator().next().getName()); + loginContext.logout(); + + } finally { + if (loginContext != null + && loginContext.getSubject() != null + && !loginContext.getSubject().getPrincipals().isEmpty()) { + loginContext.logout(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java new file mode 100644 index 0000000..d365c9a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthTestBase.java @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import javax.security.auth.login.Configuration; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread; +import org.apache.zookeeper.test.ClientBase; +import org.junit.After; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * QuorumAuthTestBase provides a base class for testing quorum peer mutual + * authentication using SASL mechanisms. + */ +public class QuorumAuthTestBase extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(QuorumAuthTestBase.class); + protected List mt = new ArrayList(); + protected static File jaasConfigDir; + + public static void setupJaasConfig(String jaasEntries) { + try { + jaasConfigDir = ClientBase.createTmpDir(); + File saslConfFile = new File(jaasConfigDir, "jaas.conf"); + FileWriter fwriter = new FileWriter(saslConfFile); + fwriter.write(jaasEntries); + fwriter.close(); + System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + } catch (IOException ioe) { + LOG.error("Failed to create tmp directory to hold JAAS conf file", ioe); + // could not create tmp directory to hold JAAS conf file : test will + // fail now. + } + + // refresh the SASL configuration in this JVM (making sure that we use the latest config + // even if other tests already have been executed and initialized the SASL configs before) + Configuration.getConfiguration().refresh(); + } + + public static void cleanupJaasConfig() { + if (jaasConfigDir != null) { + FileUtils.deleteQuietly(jaasConfigDir); + } + } + + @After + public void tearDown() throws Exception { + System.clearProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED); + } + + protected String startQuorum(final int serverCount, Map authConfigs, + int authServerCount) throws IOException { + return this.startQuorum(serverCount, authConfigs, authServerCount, false); + } + + protected String startMultiAddressQuorum(final int serverCount, Map authConfigs, + int authServerCount) throws IOException { + System.setProperty(QuorumPeer.CONFIG_KEY_MULTI_ADDRESS_ENABLED, "true"); + return this.startQuorum(serverCount, authConfigs, authServerCount, true); + } + + protected String startQuorum( + final int serverCount, + Map authConfigs, + int authServerCount, + boolean multiAddress) throws IOException { + StringBuilder connectStr = new StringBuilder(); + final int[] clientPorts = startQuorum(serverCount, connectStr, authConfigs, authServerCount, multiAddress); + for (int i = 0; i < serverCount; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT)); + } + return connectStr.toString(); + } + + protected int[] startQuorum(final int serverCount, StringBuilder connectStr, Map authConfigs, + int authServerCount, boolean multiAddress) throws IOException { + final int[] clientPorts = new int[serverCount]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < serverCount; i++) { + clientPorts[i] = PortAssignment.unique(); + String server = String.format("server.%d=localhost:%d:%d", i, PortAssignment.unique(), PortAssignment.unique()); + if (multiAddress) { + server = server + String.format("|localhost:%d:%d", PortAssignment.unique(), PortAssignment.unique()); + } + sb.append(server + ":participant\n"); + connectStr.append("127.0.0.1:" + clientPorts[i]); + if (i < serverCount - 1) { + connectStr.append(","); + } + } + String quorumCfg = sb.toString(); + // servers with authentication interfaces configured + int i = 0; + for (; i < authServerCount; i++) { + startServer(authConfigs, clientPorts, quorumCfg, i); + } + // servers without any authentication configured + for (int j = 0; j < serverCount - authServerCount; j++, i++) { + MainThread mthread = new MainThread(i, clientPorts[i], quorumCfg); + mt.add(mthread); + mthread.start(); + } + return clientPorts; + } + + private void startServer( + Map authConfigs, + final int[] clientPorts, + String quorumCfg, + int i) throws IOException { + MainThread mthread = new MainThread(i, clientPorts[i], quorumCfg, authConfigs); + mt.add(mthread); + mthread.start(); + } + + protected void startServer(MainThread restartPeer, Map authConfigs) throws IOException { + MainThread mthread = new MainThread(restartPeer.getMyid(), restartPeer.getClientPort(), restartPeer.getQuorumCfgSection(), authConfigs); + mt.add(mthread); + mthread.start(); + } + + void shutdownAll() { + for (int i = 0; i < mt.size(); i++) { + shutdown(i); + } + } + + MainThread shutdown(int index) { + MainThread mainThread = mt.get(index); + try { + mainThread.shutdown(); + } catch (InterruptedException e) { + // no op + } finally { + mt.remove(index); + } + mainThread.deleteBaseDir(); + return mainThread; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthUpgradeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthUpgradeTest.java new file mode 100644 index 0000000..ecdfaeb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumAuthUpgradeTest.java @@ -0,0 +1,237 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.apache.zookeeper.test.ClientTest; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Test; + +/** + * Rolling upgrade should do in three steps: + * + * step-1) Stop the server and set the flags and restart the server. + * quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=false and quorum.auth.serverRequireSasl=false + * Ensure that all the servers should complete this step. Now, move to next step. + * + * step-2) Stop the server one by one and change the flags and restart the server. + * quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true and quorum.auth.serverRequireSasl=false + * Ensure that all the servers should complete this step. Now, move to next step. + * + * step-3) Stop the server one by one and change the flags and restart the server. + * quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true and quorum.auth.serverRequireSasl=true + * Now, all the servers are fully upgraded and running in secured mode. + */ +public class QuorumAuthUpgradeTest extends QuorumAuthTestBase { + + static { + String jaasEntries = "QuorumServer {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_test=\"mypassword\";\n" + + "};\n" + + "QuorumLearner {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"test\"\n" + + " password=\"mypassword\";\n" + + "};\n"; + setupJaasConfig(jaasEntries); + } + + @After + @Override + public void tearDown() throws Exception { + shutdownAll(); + super.tearDown(); + } + + @AfterClass + public static void cleanup() { + cleanupJaasConfig(); + } + + /** + * Test to verify that servers are able to start without any authentication. + * peer0 -> quorum.auth.enableSasl=false + * peer1 -> quorum.auth.enableSasl=false + */ + @Test(timeout = 30000) + public void testNullAuthLearnerServer() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "false"); + + String connectStr = startQuorum(2, authConfigs, 0); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + } + + /** + * Test to verify that servers are able to form quorum. + * peer0 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false + * peer1 -> quorum.auth.enableSasl=false, quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false + */ + @Test(timeout = 30000) + public void testAuthLearnerAgainstNullAuthServer() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + + String connectStr = startQuorum(2, authConfigs, 1); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + } + + /** + * Test to verify that servers are able to form quorum. + * peer0 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false + * peer1 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false + */ + @Test(timeout = 30000) + public void testAuthLearnerAgainstNoAuthRequiredServer() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + + String connectStr = startQuorum(2, authConfigs, 2); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + } + + /** + * Test to verify that servers are able to form quorum. + * peer0 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true, quorum.auth.serverRequireSasl=true + * peer1 -> quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true, quorum.auth.serverRequireSasl=true + */ + @Test(timeout = 30000) + public void testAuthLearnerServer() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + + String connectStr = startQuorum(2, authConfigs, 2); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + } + + /** + * Rolling upgrade should do in three steps: + * + * step-1) Stop the server and set the flags and restart the server. + * quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=false and quorum.auth.serverRequireSasl=false + * Ensure that all the servers should complete this step. Now, move to next step. + * + * step-2) Stop the server one by one and change the flags and restart the server. + * quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true and quorum.auth.serverRequireSasl=false + * Ensure that all the servers should complete this step. Now, move to next step. + * + * step-3) Stop the server one by one and change the flags and restart the server. + * quorum.auth.enableSasl=true, quorum.auth.learnerRequireSasl=true and quorum.auth.serverRequireSasl=true + * Now, all the servers are fully upgraded and running in secured mode. + */ + @Test(timeout = 90000) + public void testRollingUpgrade() throws Exception { + // Start peer0,1,2 servers with quorum.auth.enableSasl=false and + // quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false + // Assume this is an existing cluster. + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "false"); + + String connectStr = startQuorum(3, authConfigs, 0); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + + //1. Upgrade peer0,1,2 with quorum.auth.enableSasl=true and + // quorum.auth.learnerRequireSasl=false, quorum.auth.serverRequireSasl=false + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "false"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "false"); + restartServer(authConfigs, 0, zk, watcher); + restartServer(authConfigs, 1, zk, watcher); + restartServer(authConfigs, 2, zk, watcher); + + //2. Upgrade peer0,1,2 with quorum.auth.enableSasl=true and + // quorum.auth.learnerRequireSasl=true, quorum.auth.serverRequireSasl=false + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "false"); + restartServer(authConfigs, 0, zk, watcher); + restartServer(authConfigs, 1, zk, watcher); + restartServer(authConfigs, 2, zk, watcher); + + //3. Upgrade peer0,1,2 with quorum.auth.enableSasl=true and + // quorum.auth.learnerRequireSasl=true, quorum.auth.serverRequireSasl=true + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + restartServer(authConfigs, 0, zk, watcher); + restartServer(authConfigs, 1, zk, watcher); + restartServer(authConfigs, 2, zk, watcher); + + //4. Restart peer2 with quorum.auth.learnerEnableSasl=false and + // quorum.auth.serverRequireSasl=false. It should fail to join the + // quorum as this needs auth. + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "false"); + MainThread m = shutdown(2); + startServer(m, authConfigs); + assertFalse( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + m.getClientPort(), 5000)); + } + + private void restartServer( + Map authConfigs, + int index, + ZooKeeper zk, + CountdownWatcher watcher) throws IOException, KeeperException, InterruptedException, TimeoutException { + LOG.info("Restarting server myid={}", index); + MainThread m = shutdown(index); + startServer(m, authConfigs); + assertTrue( + "waiting for server" + index + "being up", + ClientBase.waitForServerUp("127.0.0.1:" + m.getClientPort(), ClientBase.CONNECTION_TIMEOUT)); + watcher.waitForConnected(ClientTest.CONNECTION_TIMEOUT); + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumDigestAuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumDigestAuthTest.java new file mode 100644 index 0000000..cb28389 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumDigestAuthTest.java @@ -0,0 +1,232 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.admin.AdminServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.apache.zookeeper.server.quorum.QuorumPeerMain; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Test; + +public class QuorumDigestAuthTest extends QuorumAuthTestBase { + + static { + String jaasEntries = "QuorumServer {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_test=\"mypassword\";\n" + + "};\n" + + "QuorumLearner {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"test\"\n" + + " password=\"mypassword\";\n" + + "};\n" + + "QuorumLearnerInvalid {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"test\"\n" + + " password=\"invalid\";\n" + + "};" + + "\n"; + setupJaasConfig(jaasEntries); + } + + @After + @Override + public void tearDown() throws Exception { + for (MainThread mainThread : mt) { + mainThread.shutdown(); + mainThread.deleteBaseDir(); + } + super.tearDown(); + } + + @AfterClass + public static void cleanup() { + cleanupJaasConfig(); + } + + /** + * Test to verify that server is able to start with valid credentials + */ + @Test(timeout = 30000) + public void testValidCredentials() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + + String connectStr = startQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + /** + * Test to verify that server is able to start with valid credentials + * when using multiple Quorum / Election addresses + */ + @Test(timeout = 30000) + public void testValidCredentialsWithMultiAddresses() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + + String connectStr = startMultiAddressQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + /** + * Test to verify that server is able to start with invalid credentials if + * the configuration is set to quorum.auth.serverRequireSasl=false. + * Quorum will talk each other even if the authentication is not succeeded + */ + @Test(timeout = 30000) + public void testSaslNotRequiredWithInvalidCredentials() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerInvalid"); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "false"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "false"); + String connectStr = startQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + /** + * Test to verify that server shouldn't start with invalid credentials + * if the configuration is set to quorum.auth.serverRequireSasl=true, + * quorum.auth.learnerRequireSasl=true + */ + @Test(timeout = 30000) + public void testSaslRequiredInvalidCredentials() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerInvalid"); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + int serverCount = 2; + final int[] clientPorts = startQuorum(serverCount, new StringBuilder(), authConfigs, serverCount, false); + for (int i = 0; i < serverCount; i++) { + boolean waitForServerUp = ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], QuorumPeerTestBase.TIMEOUT); + assertFalse("Shouldn't start server with invalid credentials", waitForServerUp); + } + } + + /** + * If quorumpeer learner is not auth enabled then self won't be able to join + * quorum. So this test is ensuring that the quorumpeer learner is also auth + * enabled while enabling quorum server require sasl. + */ + @Test(timeout = 10000) + public void testEnableQuorumServerRequireSaslWithoutQuorumLearnerRequireSasl() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearner"); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "false"); + MainThread mthread = new MainThread(1, PortAssignment.unique(), "", authConfigs); + String[] args = new String[1]; + args[0] = mthread.getConfFile().toString(); + try { + new QuorumPeerMain() { + @Override + protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServer.AdminServerException { + super.initializeAndRun(args); + } + }.initializeAndRun(args); + fail("Must throw exception as quorumpeer learner is not enabled!"); + } catch (ConfigException e) { + // expected + } + } + + /** + * If quorumpeer learner is not auth enabled then self won't be able to join + * quorum. So this test is ensuring that the quorumpeer learner is also auth + * enabled while enabling quorum server require sasl. + */ + @Test(timeout = 10000) + public void testEnableQuorumAuthenticationConfigurations() throws Exception { + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearner"); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "false"); + + // case-1) 'quorum.auth.enableSasl' is off. Tries to enable server sasl. + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "false"); + MainThread mthread = new MainThread(1, PortAssignment.unique(), "", authConfigs); + String[] args = new String[1]; + args[0] = mthread.getConfFile().toString(); + try { + new QuorumPeerMain() { + @Override + protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServer.AdminServerException { + super.initializeAndRun(args); + } + }.initializeAndRun(args); + fail("Must throw exception as quorum sasl is not enabled!"); + } catch (ConfigException e) { + // expected + } + + // case-1) 'quorum.auth.enableSasl' is off. Tries to enable learner sasl. + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "false"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + try { + new QuorumPeerMain() { + @Override + protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServer.AdminServerException { + super.initializeAndRun(args); + } + }.initializeAndRun(args); + fail("Must throw exception as quorum sasl is not enabled!"); + } catch (ConfigException e) { + // expected + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.java new file mode 100644 index 0000000..28b93c7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosAuthTest.java @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class QuorumKerberosAuthTest extends KerberosSecurityTestcase { + + private static File keytabFile; + + static { + String keytabFilePath = FilenameUtils.normalize(KerberosTestUtils.getKeytabFile(), true); + + // note: we use "refreshKrb5Config=true" to refresh the kerberos config in the JVM, + // making sure that we use the latest config even if other tests already have been executed + // and initialized the kerberos client configs before) + String jaasEntries = "" + + "QuorumServer {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + + keytabFilePath + + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=false\n" + + " refreshKrb5Config=true\n" + + " principal=\"" + + KerberosTestUtils.getServerPrincipal() + + "\";\n" + + "};\n" + + "QuorumLearner {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + + keytabFilePath + + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=false\n" + + " refreshKrb5Config=true\n" + + " principal=\"" + + KerberosTestUtils.getLearnerPrincipal() + + "\";\n" + + "};\n"; + setupJaasConfig(jaasEntries); + } + + @BeforeClass + public static void setUp() throws Exception { + // create keytab + keytabFile = new File(KerberosTestUtils.getKeytabFile()); + String learnerPrincipal = KerberosTestUtils.getLearnerPrincipal(); + String serverPrincipal = KerberosTestUtils.getServerPrincipal(); + learnerPrincipal = learnerPrincipal.substring(0, learnerPrincipal.lastIndexOf("@")); + serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@")); + getKdc().createPrincipal(keytabFile, learnerPrincipal, serverPrincipal); + } + + @After + @Override + public void tearDown() throws Exception { + for (MainThread mainThread : mt) { + mainThread.shutdown(); + mainThread.deleteBaseDir(); + } + super.tearDown(); + } + + @AfterClass + public static void cleanup() { + if (keytabFile != null) { + FileUtils.deleteQuietly(keytabFile); + } + cleanupJaasConfig(); + } + + /** + * Test to verify that server is able to start with valid credentials + */ + @Test(timeout = 120000) + public void testValidCredentials() throws Exception { + String serverPrincipal = KerberosTestUtils.getServerPrincipal(); + serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@")); + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal); + String connectStr = startQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + /** + * Test to verify that server is able to start with valid credentials + * when using multiple Quorum / Election addresses + */ + @Test(timeout = 120000) + public void testValidCredentialsWithMultiAddresses() throws Exception { + String serverPrincipal = KerberosTestUtils.getServerPrincipal(); + serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@")); + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal); + String connectStr = startMultiAddressQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java new file mode 100644 index 0000000..c9b93c5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/quorum/auth/QuorumKerberosHostBasedAuthTest.java @@ -0,0 +1,223 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.quorum.auth; + +import static org.junit.Assert.fail; +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeoutException; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase.MainThread; +import org.apache.zookeeper.test.ClientBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class QuorumKerberosHostBasedAuthTest extends KerberosSecurityTestcase { + + private static File keytabFile; + private static String hostServerPrincipal = KerberosTestUtils.getHostServerPrincipal(); + private static String hostLearnerPrincipal = KerberosTestUtils.getHostLearnerPrincipal(); + private static String hostNamedLearnerPrincipal = KerberosTestUtils.getHostNamedLearnerPrincipal("myHost"); + + static { + setupJaasConfigEntries(hostServerPrincipal, hostLearnerPrincipal, hostNamedLearnerPrincipal); + } + + private static void setupJaasConfigEntries( + String hostServerPrincipal, + String hostLearnerPrincipal, + String hostNamedLearnerPrincipal) { + String keytabFilePath = FilenameUtils.normalize(KerberosTestUtils.getKeytabFile(), true); + + // note: we use "refreshKrb5Config=true" to refresh the kerberos config in the JVM, + // making sure that we use the latest config even if other tests already have been executed + // and initialized the kerberos client configs before) + String jaasEntries = "QuorumServer {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + keytabFilePath + + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=false\n" + + " refreshKrb5Config=true\n" + + " principal=\"" + KerberosTestUtils.replaceHostPattern(hostServerPrincipal) + + "\";\n" + + "};\n" + + "QuorumLearner {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + keytabFilePath + + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=false\n" + + " refreshKrb5Config=true\n" + + " principal=\"" + KerberosTestUtils.replaceHostPattern(hostLearnerPrincipal) + + "\";\n" + + "};\n" + + "QuorumLearnerMyHost {\n" + + " com.sun.security.auth.module.Krb5LoginModule required\n" + + " useKeyTab=true\n" + + " keyTab=\"" + keytabFilePath + + "\"\n" + + " storeKey=true\n" + + " useTicketCache=false\n" + + " debug=false\n" + + " refreshKrb5Config=true\n" + + " principal=\"" + hostNamedLearnerPrincipal + + "\";\n" + + "};\n"; + setupJaasConfig(jaasEntries); + } + + @BeforeClass + public static void setUp() throws Exception { + // create keytab + keytabFile = new File(KerberosTestUtils.getKeytabFile()); + + // Creates principals in the KDC and adds them to a keytab file. + String learnerPrincipal = hostLearnerPrincipal.substring(0, hostLearnerPrincipal.lastIndexOf("@")); + learnerPrincipal = KerberosTestUtils.replaceHostPattern(learnerPrincipal); + String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@")); + serverPrincipal = KerberosTestUtils.replaceHostPattern(serverPrincipal); + + // learner with ipaddress in principal + String learnerPrincipal2 = hostNamedLearnerPrincipal.substring(0, hostNamedLearnerPrincipal.lastIndexOf("@")); + getKdc().createPrincipal(keytabFile, learnerPrincipal, learnerPrincipal2, serverPrincipal); + } + + @After + @Override + public void tearDown() throws Exception { + for (MainThread mainThread : mt) { + mainThread.shutdown(); + mainThread.deleteBaseDir(); + } + super.tearDown(); + } + + @AfterClass + public static void cleanup() { + if (keytabFile != null) { + FileUtils.deleteQuietly(keytabFile); + } + cleanupJaasConfig(); + } + + /** + * Test to verify that server is able to start with valid credentials + */ + @Test(timeout = 120000) + public void testValidCredentials() throws Exception { + String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@")); + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal); + String connectStr = startQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + /** + * Test to verify that server is able to start with valid credentials + * when using multiple Quorum / Election addresses + */ + @Test(timeout = 120000) + public void testValidCredentialsWithMultiAddresses() throws Exception { + String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@")); + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal); + String connectStr = startMultiAddressQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + /** + * Test to verify that the bad server connection to the quorum should be rejected. + */ + @Test(timeout = 120000) + public void testConnectBadServer() throws Exception { + String serverPrincipal = hostServerPrincipal.substring(0, hostServerPrincipal.lastIndexOf("@")); + Map authConfigs = new HashMap(); + authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true"); + authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true"); + authConfigs.put(QuorumAuth.QUORUM_KERBEROS_SERVICE_PRINCIPAL, serverPrincipal); + String connectStr = startQuorum(3, authConfigs, 3); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + + String quorumCfgSection = mt.get(0).getQuorumCfgSection(); + StringBuilder sb = new StringBuilder(); + sb.append(quorumCfgSection); + + int myid = mt.size() + 1; + final int clientPort = PortAssignment.unique(); + String server = String.format("server.%d=localhost:%d:%d:participant", myid, PortAssignment.unique(), PortAssignment.unique()); + sb.append(server + "\n"); + quorumCfgSection = sb.toString(); + authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_LOGIN_CONTEXT, "QuorumLearnerMyHost"); + MainThread badServer = new MainThread(myid, clientPort, quorumCfgSection, authConfigs); + badServer.start(); + watcher = new CountdownWatcher(); + connectStr = "127.0.0.1:" + clientPort; + zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher); + try { + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT / 3); + fail("Must throw exception as the myHost is not an authorized one!"); + } catch (TimeoutException e) { + // expected + } finally { + zk.close(); + badServer.shutdown(); + badServer.deleteBaseDir(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/AdHashTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/AdHashTest.java new file mode 100644 index 0000000..671d484 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/AdHashTest.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class AdHashTest extends ZKTestCase { + + private static Random rand = new Random(); + + private static List generateRandomHashes(int count) { + ArrayList list = new ArrayList<>(count); + + for (int i = 0; i < count; i++) { + list.add(rand.nextLong()); + } + return list; + } + + private static void addListOfDigests(AdHash hash, List digests) { + for (long b : digests) { + hash.addDigest(b); + } + } + + private static void removeListOfDigests(AdHash hash, List digests) { + for (long b : digests) { + hash.removeDigest(b); + } + } + + /** + * Test thhe add and remove digest from AdHash is working as expected. + */ + @Test + public void testAdHash() throws Exception { + List bucket1 = generateRandomHashes(50); + List bucket2 = generateRandomHashes(3); + List bucket3 = generateRandomHashes(30); + List bucket4 = generateRandomHashes(10); + List bucket5 = generateRandomHashes(5); + + // adding out of order should result in the same hash + AdHash hash12 = new AdHash(); + addListOfDigests(hash12, bucket1); + addListOfDigests(hash12, bucket2); + + AdHash hash21 = new AdHash(); + addListOfDigests(hash21, bucket2); + addListOfDigests(hash21, bucket1); + assertEquals(hash12, hash21); + + AdHash hashall = new AdHash(); + addListOfDigests(hashall, bucket1); + addListOfDigests(hashall, bucket2); + addListOfDigests(hashall, bucket3); + addListOfDigests(hashall, bucket4); + addListOfDigests(hashall, bucket5); + assertFalse("digest of different set not different", hashall.equals(hash21)); + removeListOfDigests(hashall, bucket4); + removeListOfDigests(hashall, bucket5); + addListOfDigests(hash21, bucket3); + assertEquals("hashall with 4 & 5 removed should match hash21 with 3 added", hashall, hash21); + + removeListOfDigests(hashall, bucket3); + removeListOfDigests(hashall, bucket2); + removeListOfDigests(hashall, bucket1); + assertEquals("empty hashall's digest should be 0", hashall.toString(), "0"); + + AdHash hash45 = new AdHash(); + addListOfDigests(hash45, bucket4); + addListOfDigests(hash45, bucket5); + + addListOfDigests(hashall, bucket4); + addListOfDigests(hashall, bucket5); + assertEquals("empty hashall + 4&5 should equal hash45", hashall, hash45); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/AuthUtilTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/AuthUtilTest.java new file mode 100644 index 0000000..801a769 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/AuthUtilTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class AuthUtilTest { + + @BeforeClass + public static void beforeClassSetUp() { + ProviderRegistry.reset(); + System.setProperty("zookeeper.authProvider.sasl", + "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty("zookeeper.authProvider.x509", + "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); + } + + @AfterClass + public static void afterClassTearDown() { + System.clearProperty("zookeeper.authProvider.sasl"); + System.clearProperty("zookeeper.authProvider.x509"); + } + + @Test + public void testGetUserFromAllAuthenticationScheme() { + String user = "zkUser"; + Id id = new Id("digest", user + ":password"); + String result = AuthUtil.getUser(id); + assertEquals(user, result); + + String principal = "zkCli/hadoop.hadoop.com"; + id = new Id("sasl", principal); + assertEquals(principal, AuthUtil.getUser(id)); + + String ip = "192.168.1.2"; + id = new Id("ip", ip); + assertEquals(ip, AuthUtil.getUser(id)); + + String certificate = "CN=host-192.168.1.2,OU=OrganizationUnit,O=Organization,L=Location,ST=State,C=IN"; + id = new Id("x509", certificate); + assertEquals(certificate, AuthUtil.getUser(id)); + } + + @Test + public void testGetUserShouldReturnNullIfAuthenticationNotConfigured() { + Id id = new Id("invalid Authentication Scheme", "user"); + String result = AuthUtil.getUser(id); + assertNull(result); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/BitHashSetTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/BitHashSetTest.java new file mode 100644 index 0000000..ff67c4e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/BitHashSetTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class BitHashSetTest extends ZKTestCase { + + @Test + public void testAddWatchBit() { + int watcherCacheSize = 1; + BitHashSet ws = new BitHashSet(watcherCacheSize); + assertTrue(ws.add(1)); + assertEquals(1, ws.size()); + assertEquals(1, ws.cachedSize()); + + List actualBits = new ArrayList(); + + for (int bit : ws) { + actualBits.add(bit); + } + assertArrayEquals(new Integer[]{1}, actualBits.toArray(new Integer[actualBits.size()])); + + // add the same bit again + assertFalse(ws.add(1)); + assertEquals(1, ws.size()); + assertEquals(1, ws.cachedSize()); + + // add another bit, make sure there there is only 1 bit cached + assertTrue(ws.add(2)); + assertEquals(2, ws.size()); + assertEquals(1, ws.cachedSize()); + + assertTrue(ws.contains(1)); + + actualBits.clear(); + for (int bit : ws) { + actualBits.add(bit); + } + assertArrayEquals(new Integer[]{1, 2}, actualBits.toArray(new Integer[actualBits.size()])); + } + + @Test + public void testRemoveWatchBit() { + int watcherCacheSize = 1; + BitHashSet ws = new BitHashSet(watcherCacheSize); + ws.add(1); + ws.add(2); + + assertTrue(ws.contains(1)); + assertTrue(ws.contains(2)); + + ws.remove(1); + assertFalse(ws.contains(1)); + assertEquals(1, ws.size()); + assertEquals(0, ws.cachedSize()); + + List actualBits = new ArrayList(); + + for (int bit : ws) { + actualBits.add(bit); + } + assertArrayEquals(new Integer[]{2}, actualBits.toArray(new Integer[actualBits.size()])); + + ws.add(3); + assertEquals(2, ws.size()); + assertEquals(1, ws.cachedSize()); + + actualBits.clear(); + for (int bit : ws) { + actualBits.add(bit); + } + assertArrayEquals(new Integer[]{2, 3}, actualBits.toArray(new Integer[actualBits.size()])); + + ws.remove(2); + ws.remove(3); + + assertEquals(0, ws.size()); + assertEquals(0, ws.cachedSize()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/BitMapTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/BitMapTest.java new file mode 100644 index 0000000..799a786 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/BitMapTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class BitMapTest extends ZKTestCase { + + @Test + public void testAddAndRemove() { + BitMap bitMap = new BitMap(); + String v1 = "v1"; + Integer bit = bitMap.add(v1); + + assertEquals(1, bitMap.size()); + assertTrue(bit >= 0); + assertEquals(v1, bitMap.get(bit)); + assertEquals(bit, bitMap.getBit(v1)); + + // add the same value again + Integer newBit = bitMap.add(v1); + assertEquals(bit, newBit); + assertEquals(1, bitMap.size()); + + String v2 = "v2"; + Integer v2Bit = bitMap.add(v2); + assertEquals(2, bitMap.size()); + assertNotEquals(v2Bit, bit); + + // remove by value + bitMap.remove(v1); + assertEquals(1, bitMap.size()); + assertNull(bitMap.get(bit)); + assertNull(bitMap.getBit(v1)); + + // remove by bit + bitMap.remove(v2Bit); + assertEquals(0, bitMap.size()); + assertNull(bitMap.get(v2Bit)); + assertNull(bitMap.getBit(v2)); + } + + @Test + public void testBitReuse() { + BitMap bitMap = new BitMap(); + int v1Bit = bitMap.add("v1"); + int v2Bit = bitMap.add("v2"); + int v3Bit = bitMap.add("v3"); + bitMap.remove(v2Bit); + + int v4Bit = bitMap.add("v4"); + + assertEquals(v4Bit, v2Bit); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/CircularBufferTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/CircularBufferTest.java new file mode 100644 index 0000000..ff3c83c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/CircularBufferTest.java @@ -0,0 +1,198 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import org.junit.Assert; +import org.junit.Test; + +public class CircularBufferTest { + + @Test + public void testCircularBuffer() { + final int capacity = 3; + CircularBuffer buffer = new CircularBuffer<>(String.class, capacity); + + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + // write to the buffer + buffer.write("A"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + buffer.write("B"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + buffer.write("C"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + // Buffer is full. + // Read from buffer + Assert.assertEquals("A", buffer.take()); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + Assert.assertEquals("B", buffer.take()); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + Assert.assertEquals("C", buffer.take()); + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + // write to the buffer + buffer.write("1"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + buffer.write("2"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + buffer.write("3"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + buffer.write("4"); // 4 overwrites 1 + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + // Buffer if full + // Read from buffer + Assert.assertEquals("2", buffer.take()); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + Assert.assertEquals("3", buffer.take()); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + Assert.assertEquals("4", buffer.take()); + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + // write to the buffer + buffer.write("a"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + buffer.write("b"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + buffer.write("c"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + buffer.write("d"); // d overwrites a + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + buffer.write("e"); // e overwrites b + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + buffer.write("f"); // f overwrites c + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + buffer.write("g"); // g overwrites d + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + // Buffer is full. + // Read from buffer + Assert.assertEquals("e", buffer.take()); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + Assert.assertEquals("f", buffer.take()); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + Assert.assertEquals("g", buffer.take()); + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + } + + @Test + public void testCircularBufferWithCapacity1() { + final int capacity = 1; + CircularBuffer buffer = new CircularBuffer<>(String.class, capacity); + + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + // write to the buffer + buffer.write("A"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + buffer.write("B"); // B overwrite A + Assert.assertFalse(buffer.isEmpty()); + Assert.assertTrue(buffer.isFull()); + + // Buffer is full. + // Read from buffer + Assert.assertEquals("B", buffer.take()); + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + } + + @Test + public void testCircularBufferReset() { + final int capacity = 3; + CircularBuffer buffer = new CircularBuffer<>(String.class, capacity); + + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + + // write to the buffer + buffer.write("A"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + Assert.assertEquals(1, buffer.size()); + Assert.assertEquals("A", buffer.peek()); + + buffer.write("B"); + Assert.assertFalse(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + Assert.assertEquals(2, buffer.size()); + Assert.assertEquals("A", buffer.peek()); + + // reset + buffer.reset(); + Assert.assertNull(buffer.peek()); + Assert.assertTrue(buffer.isEmpty()); + Assert.assertFalse(buffer.isFull()); + Assert.assertEquals(0, buffer.size()); + } + + @Test + public void testCircularBufferIllegalCapacity() { + try { + CircularBuffer buffer = new CircularBuffer<>(String.class, 0); + Assert.fail(); + } catch (IllegalArgumentException e) { + Assert.assertEquals("CircularBuffer capacity should be greater than 0", e.getMessage()); + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/ConfigUtilsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/ConfigUtilsTest.java new file mode 100644 index 0000000..d259a5d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/ConfigUtilsTest.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; +import org.junit.Test; + +public class ConfigUtilsTest { + + @Test + public void testGetHostAndPortWithIPv6() throws ConfigException { + String[] nsa = ConfigUtils.getHostAndPort("[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443"); + assertEquals(nsa[0], "2001:db8:85a3:8d3:1319:8a2e:370:7348"); + assertEquals(nsa[1], "443"); + + nsa = ConfigUtils.getHostAndPort("[2001:db8:1::242:ac11:2]:2888:3888"); + assertEquals(nsa[0], "2001:db8:1::242:ac11:2"); + assertEquals(nsa[1], "2888"); + assertEquals(nsa[2], "3888"); + } + + @Test + public void testGetHostAndPortWithIPv4() throws ConfigException { + String[] nsa = ConfigUtils.getHostAndPort("127.0.0.1:443"); + assertEquals(nsa[0], "127.0.0.1"); + assertEquals(nsa[1], "443"); + + nsa = ConfigUtils.getHostAndPort("127.0.0.1:2888:3888"); + assertEquals(nsa[0], "127.0.0.1"); + assertEquals(nsa[1], "2888"); + assertEquals(nsa[2], "3888"); + } + + @Test(expected = ConfigException.class) + public void testGetHostAndPortWithoutBracket() throws ConfigException { + String[] nsa = ConfigUtils.getHostAndPort("[2001:db8:85a3:8d3:1319:8a2e:370:7348"); + } + + @Test(expected = ConfigException.class) + public void testGetHostAndPortWithoutPortAfterColon() throws ConfigException { + String[] nsa = ConfigUtils.getHostAndPort("[2001:db8:1::242:ac11:2]:"); + } + + @Test + public void testGetHostAndPortWithoutPort() throws ConfigException { + String[] nsa = ConfigUtils.getHostAndPort("127.0.0.1"); + assertEquals(nsa[0], "127.0.0.1"); + assertEquals(nsa.length, 1); + + nsa = ConfigUtils.getHostAndPort("[2001:db8:1::242:ac11:2]"); + assertEquals(nsa[0], "2001:db8:1::242:ac11:2"); + assertEquals(nsa.length, 1); + } + + @Test + public void testGetPropertyBackwardCompatibleWay() throws ConfigException { + String newProp = "zookeeper.prop.x.y.z"; + String oldProp = "prop.x.y.z"; + + // Null as both properties are not set + String result = ConfigUtils.getPropertyBackwardCompatibleWay(newProp); + assertNull(result); + + // Return old property value when only old property is set + String oldPropValue = "oldPropertyValue"; + System.setProperty(oldProp, oldPropValue); + result = ConfigUtils.getPropertyBackwardCompatibleWay(newProp); + assertEquals(oldPropValue, result); + + // Return new property value when both properties are set + String newPropValue = "newPropertyValue"; + System.setProperty(newProp, newPropValue); + result = ConfigUtils.getPropertyBackwardCompatibleWay(newProp); + assertEquals(newPropValue, result); + + // cleanUp + clearProp(newProp, oldProp); + + // Return trimmed value + System.setProperty(oldProp, oldPropValue + " "); + result = ConfigUtils.getPropertyBackwardCompatibleWay(newProp); + assertEquals(oldPropValue, result); + + System.setProperty(newProp, " " + newPropValue); + result = ConfigUtils.getPropertyBackwardCompatibleWay(newProp); + assertEquals(newPropValue, result); + + // cleanUp + clearProp(newProp, oldProp); + } + + private void clearProp(String newProp, String oldProp) { + System.clearProperty(newProp); + System.clearProperty(oldProp); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java new file mode 100644 index 0000000..58b1902 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/JvmPauseMonitorTest.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.junit.After; +import org.junit.Test; + +public class JvmPauseMonitorTest { + + private final Long sleepTime = 100L; + private final Long infoTH = -1L; + private final Long warnTH = -1L; + private JvmPauseMonitor pauseMonitor; + + @Test(timeout = 5000) + public void testJvmPauseMonitorExceedInfoThreshold() throws InterruptedException { + QuorumPeerConfig qpConfig = mock(QuorumPeerConfig.class); + when(qpConfig.getJvmPauseSleepTimeMs()).thenReturn(sleepTime); + when(qpConfig.getJvmPauseInfoThresholdMs()).thenReturn(infoTH); + + pauseMonitor = new JvmPauseMonitor(qpConfig); + pauseMonitor.serviceStart(); + + assertEquals(sleepTime, Long.valueOf(pauseMonitor.sleepTimeMs)); + assertEquals(infoTH, Long.valueOf(pauseMonitor.infoThresholdMs)); + + while (pauseMonitor.getNumGcInfoThresholdExceeded() == 0) { + Thread.sleep(200); + } + } + + @Test(timeout = 5000) + public void testJvmPauseMonitorExceedWarnThreshold() throws InterruptedException { + QuorumPeerConfig qpConfig = mock(QuorumPeerConfig.class); + when(qpConfig.getJvmPauseSleepTimeMs()).thenReturn(sleepTime); + when(qpConfig.getJvmPauseWarnThresholdMs()).thenReturn(warnTH); + + pauseMonitor = new JvmPauseMonitor(qpConfig); + pauseMonitor.serviceStart(); + + assertEquals(sleepTime, Long.valueOf(pauseMonitor.sleepTimeMs)); + assertEquals(warnTH, Long.valueOf(pauseMonitor.warnThresholdMs)); + + while (pauseMonitor.getNumGcWarnThresholdExceeded() == 0) { + Thread.sleep(200); + } + + } + + @After + public void teardown() { + pauseMonitor.serviceStop(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/MessageTrackerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/MessageTrackerTest.java new file mode 100644 index 0000000..f1cef10 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/MessageTrackerTest.java @@ -0,0 +1,129 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MessageTrackerTest { + private static final int BUFFERED_MESSAGE_SIZE = 5; + private static final Logger LOG = LoggerFactory.getLogger(MessageTrackerTest.class); + + @Before + public void setup() { + System.setProperty(MessageTracker.MESSAGE_TRACKER_ENABLED, "true"); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(MessageTracker.MESSAGE_TRACKER_ENABLED); + } + + @Test + public void testTrackSend() throws InterruptedException { + long timestamp1 = System.currentTimeMillis(); + MessageTracker messageTracker = new MessageTracker(BUFFERED_MESSAGE_SIZE); + + // First timestamp is added + messageTracker.trackSent(timestamp1); + Assert.assertEquals(messageTracker.peekSentTimestamp(), timestamp1); + + Thread.sleep(2); + + // Second timestamp is added + long timestamp2 = System.currentTimeMillis(); + messageTracker.trackSent(timestamp2); + Assert.assertEquals(messageTracker.peekSentTimestamp(), timestamp1); + } + + @Test + public void testTrackReceived() throws InterruptedException { + long timestamp1 = System.currentTimeMillis(); + MessageTracker messageTracker = new MessageTracker(BUFFERED_MESSAGE_SIZE); + + // First timestamp is added + messageTracker.trackReceived(timestamp1); + Assert.assertEquals(messageTracker.peekReceivedTimestamp(), timestamp1); + + Thread.sleep(2); + + // Second timestamp is added + long timestamp2 = System.currentTimeMillis(); + messageTracker.trackReceived(timestamp2); + Assert.assertEquals(messageTracker.peekReceivedTimestamp(), timestamp1); + } + + @Test + public void testMessageTrackerFull() throws InterruptedException { + MessageTracker messageTracker = new MessageTracker(BUFFERED_MESSAGE_SIZE); + + // Add up to capacity + 1 + long timestampSent = 0; + long timestampReceived = 0; + for (int i = 0; i <= BUFFERED_MESSAGE_SIZE; i++) { + if (i == 1) { + timestampSent = System.currentTimeMillis(); + messageTracker.trackSent(timestampSent); + Thread.sleep(2); + timestampReceived = System.currentTimeMillis(); + messageTracker.trackReceived(timestampReceived); + } else { + messageTracker.trackSent(System.currentTimeMillis()); + messageTracker.trackReceived(System.currentTimeMillis()); + } + Thread.sleep(1); + } + + Assert.assertEquals(messageTracker.peekSentTimestamp(), timestampSent); + Assert.assertEquals(messageTracker.peekReceivedTimestamp(), timestampReceived); + } + + @Test + public void testDumpToLog() { + long timestamp1 = System.currentTimeMillis(); + MessageTracker messageTracker = new MessageTracker(BUFFERED_MESSAGE_SIZE); + String sid = "127.0.0.1"; + + // MessageTracker is empty + messageTracker.dumpToLog(sid); + Assert.assertNull(messageTracker.peekSent()); + Assert.assertNull(messageTracker.peekReceived()); + + // There is 1 sent and 0 received + messageTracker.trackSent(timestamp1); + Assert.assertEquals(messageTracker.peekSentTimestamp(), timestamp1); + Assert.assertNull(messageTracker.peekReceived()); + messageTracker.dumpToLog(sid); + Assert.assertNull(messageTracker.peekSent()); + Assert.assertNull(messageTracker.peekReceived()); + + // There is 1 sent and 1 received + messageTracker.trackSent(timestamp1); + messageTracker.trackReceived(timestamp1); + Assert.assertEquals(messageTracker.peekSentTimestamp(), timestamp1); + Assert.assertEquals(messageTracker.peekReceivedTimestamp(), timestamp1); + messageTracker.dumpToLog(sid); + Assert.assertNull(messageTracker.peekSent()); + Assert.assertNull(messageTracker.peekReceived()); + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/PortForwarder.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/PortForwarder.java new file mode 100644 index 0000000..a6a5680 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/PortForwarder.java @@ -0,0 +1,265 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ConnectException; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A utility that does bi-directional forwarding between two ports. + * Useful, for example, to simulate network failures. + * Example: + * + * Server 1 config file: + * + * server.1=127.0.0.1:7301:7401;8201 + * server.2=127.0.0.1:7302:7402;8202 + * server.3=127.0.0.1:7303:7403;8203 + * + * Server 2 and 3 config files: + * + * server.1=127.0.0.1:8301:8401;8201 + * server.2=127.0.0.1:8302:8402;8202 + * server.3=127.0.0.1:8303:8403;8203 + * + * Initially forward traffic between 730x and 830x and between 740x and 830x + * This way server 1 can communicate with servers 2 and 3 + * .... + * + * List<PortForwarder> pfs = startForwarding(); + * .... + * // simulate a network interruption for server 1 + * stopForwarding(pfs); + * .... + * // restore connection + * pfs = startForwarding(); + * + * + * private List<PortForwarder> startForwarding() throws IOException { + * List<PortForwarder> res = new ArrayList<PortForwarder>(); + * res.add(new PortForwarder(8301, 7301)); + * res.add(new PortForwarder(8401, 7401)); + * res.add(new PortForwarder(7302, 8302)); + * res.add(new PortForwarder(7402, 8402)); + * res.add(new PortForwarder(7303, 8303)); + * res.add(new PortForwarder(7403, 8403)); + * return res; + * } + * + * private void stopForwarding(List<PortForwarder> pfs) throws Exception { + * for (PortForwarder pf : pfs) { + * pf.shutdown(); + * } + * } + * + * + */ +public class PortForwarder extends Thread { + + private static final Logger LOG = LoggerFactory.getLogger(PortForwarder.class); + + private static class PortForwardWorker implements Runnable { + + private final InputStream in; + private final OutputStream out; + private final Socket toClose; + private final Socket toClose2; + private boolean isFinished = false; + + PortForwardWorker(Socket toClose, Socket toClose2, InputStream in, OutputStream out) { + this.toClose = toClose; + this.toClose2 = toClose2; + this.in = in; + this.out = out; + // LOG.info("starting forward for "+toClose); + } + + public void run() { + Thread.currentThread().setName(toClose.toString() + "-->" + toClose2.toString()); + byte[] buf = new byte[1024]; + try { + while (true) { + try { + int read = this.in.read(buf); + if (read > 0) { + try { + this.out.write(buf, 0, read); + } catch (IOException e) { + LOG.warn("exception during write", e); + break; + } + } else if (read < 0) { + throw new IOException("read " + read); + } + } catch (SocketTimeoutException e) { + LOG.error("socket timeout", e); + } + } + Thread.sleep(1); + } catch (InterruptedException e) { + LOG.warn("Interrupted", e); + } catch (SocketException e) { + if (!"Socket closed".equals(e.getMessage())) { + LOG.error("Unexpected exception", e); + } + } catch (IOException e) { + LOG.error("Unexpected exception", e); + } finally { + shutdown(); + } + LOG.info("Shutting down forward for {}", toClose); + isFinished = true; + } + + boolean waitForShutdown(long timeoutMs) throws InterruptedException { + synchronized (this) { + if (!isFinished) { + this.wait(timeoutMs); + } + } + return isFinished; + } + + public void shutdown() { + try { + toClose.close(); + } catch (IOException ex) { + // ignore + } + try { + toClose2.close(); + } catch (IOException ex) { + // ignore silently + } + } + + } + + private volatile boolean stopped = false; + private ExecutorService workerExecutor = Executors.newCachedThreadPool(); + private List workers = new ArrayList<>(); + private ServerSocket serverSocket; + private final int to; + + public PortForwarder(int from, int to) throws IOException { + this.to = to; + serverSocket = new ServerSocket(from); + serverSocket.setSoTimeout(30000); + this.start(); + } + + @Override + public void run() { + try { + while (!stopped) { + Socket sock = null; + try { + LOG.info("accepting socket local:{} to:{}", serverSocket.getLocalPort(), to); + sock = serverSocket.accept(); + LOG.info("accepted: local:{} from:{} to:{}", sock.getLocalPort(), sock.getPort(), to); + Socket target = null; + int retry = 10; + while (sock.isConnected()) { + try { + target = new Socket("localhost", to); + break; + } catch (IOException e) { + if (retry == 0) { + throw e; + } + LOG.warn( + "connection failed, retrying({}): local:{} from:{} to:{}", + retry, + sock.getLocalPort(), + sock.getPort(), + to, + e); + } + Thread.sleep(TimeUnit.SECONDS.toMillis(1)); + retry--; + } + LOG.info("connected: local:{} from:{} to:{}", sock.getLocalPort(), sock.getPort(), to); + sock.setSoTimeout(30000); + target.setSoTimeout(30000); + + workers.add(new PortForwardWorker(sock, target, sock.getInputStream(), target.getOutputStream())); + workers.add(new PortForwardWorker(target, sock, target.getInputStream(), sock.getOutputStream())); + for (PortForwardWorker worker : workers) { + workerExecutor.submit(worker); + } + } catch (SocketTimeoutException e) { + LOG.warn("socket timed out", e); + } catch (ConnectException e) { + LOG.warn( + "connection exception local:{} from:{} to:{}", + sock.getLocalPort(), + sock.getPort(), + to, + e); + sock.close(); + } catch (IOException e) { + if (!"Socket closed".equals(e.getMessage())) { + LOG.warn( + "unexpected exception local:{} from:{} to:{}", + sock.getLocalPort(), + sock.getPort(), + to, + e); + throw e; + } + } + + } + } catch (IOException e) { + LOG.error("Unexpected exception to:{}", to, e); + } catch (InterruptedException e) { + LOG.error("Interrupted to:{}", to, e); + } + } + + public void shutdown() throws Exception { + this.stopped = true; + this.serverSocket.close(); + this.join(); + this.workerExecutor.shutdownNow(); + for (PortForwardWorker worker : workers) { + worker.shutdown(); + } + + for (PortForwardWorker worker : workers) { + if (!worker.waitForShutdown(5000)) { + throw new Exception("Failed to stop forwarding within 5 seconds"); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/RequestPathMetricsCollectorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/RequestPathMetricsCollectorTest.java new file mode 100644 index 0000000..ecfc92c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/RequestPathMetricsCollectorTest.java @@ -0,0 +1,451 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.apache.zookeeper.ZooDefs.OpCode.create; +import static org.apache.zookeeper.ZooDefs.OpCode.create2; +import static org.apache.zookeeper.ZooDefs.OpCode.delete; +import static org.apache.zookeeper.ZooDefs.OpCode.exists; +import static org.apache.zookeeper.ZooDefs.OpCode.getChildren; +import static org.apache.zookeeper.ZooDefs.OpCode.getChildren2; +import static org.apache.zookeeper.ZooDefs.OpCode.getData; +import static org.apache.zookeeper.ZooDefs.OpCode.setData; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class RequestPathMetricsCollectorTest { + + @Before + public void setUp() { + System.setProperty("zookeeper.pathStats.enabled", "true"); + System.setProperty("zookeeper.pathStats.slotCapacity", "60"); + System.setProperty("zookeeper.pathStats.slotDuration", "1"); + System.setProperty("zookeeper.pathStats.maxDepth", "6"); + System.setProperty("zookeeper.pathStats.sampleRate", "1.0"); + } + + @After + public void tearDown() { + System.clearProperty("zookeeper.pathStats.enabled"); + System.clearProperty("zookeeper.pathStats.slotCapacity"); + System.clearProperty("zookeeper.pathStats.slotDuration"); + System.clearProperty("zookeeper.pathStats.maxDepth"); + System.clearProperty("zookeeper.pathStats.sampleRate"); + } + + @Test + public void testTrimPath() { + //normal cases + String trimedPath = RequestPathMetricsCollector.trimPathDepth("/p1/p2/p3", 1); + assertTrue(trimedPath.equalsIgnoreCase("/p1")); + trimedPath = RequestPathMetricsCollector.trimPathDepth("/p1/p2/p3", 2); + assertTrue(trimedPath.equalsIgnoreCase("/p1/p2")); + trimedPath = RequestPathMetricsCollector.trimPathDepth("/p1/p2/p3", 3); + assertTrue(trimedPath.equalsIgnoreCase("/p1/p2/p3")); + trimedPath = RequestPathMetricsCollector.trimPathDepth("/p1/p2/p3", 4); + assertTrue(trimedPath.equalsIgnoreCase("/p1/p2/p3")); + //some extra symbols + trimedPath = RequestPathMetricsCollector.trimPathDepth("//p1 next/p2.index/p3:next", 3); + assertTrue(trimedPath.equalsIgnoreCase("/p1 next/p2.index/p3:next")); + trimedPath = RequestPathMetricsCollector.trimPathDepth("//p1 next/p2.index/p3:next", 2); + assertTrue(trimedPath.equalsIgnoreCase("/p1 next/p2.index")); + trimedPath = RequestPathMetricsCollector.trimPathDepth("//p1 next/p2.index/p3:next", 6); + assertTrue(trimedPath.equalsIgnoreCase("/p1 next/p2.index/p3:next")); + } + + @Test + public void testQueueMapReduce() throws InterruptedException { + RequestPathMetricsCollector requestPathMetricsCollector = new RequestPathMetricsCollector(); + RequestPathMetricsCollector.PathStatsQueue pathStatsQueue = requestPathMetricsCollector.new PathStatsQueue(create2); + Thread path7 = new Thread(() -> { + for (int i = 0; i < 1000000; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5/path6/path7" + "_" + i); + } + }); + path7.start(); + Thread path6 = new Thread(() -> { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5/path6"); + for (int i = 1; i < 100000; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5/path6" + "_" + i); + } + }); + path6.start(); + for (int i = 0; i < 1; i++) { + pathStatsQueue.registerRequest("/path1"); + } + for (int i = 0; i < 10; i++) { + pathStatsQueue.registerRequest("/path1/path2" + "_" + i); + } + for (int i = 0; i < 100; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3" + "_" + i); + } + for (int i = 0; i < 1000; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4" + "_" + i); + } + for (int i = 0; i < 10000; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5" + "_" + i); + } + path6.join(); + path7.join(); + Map newSlot = pathStatsQueue.mapReducePaths(1, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 1); + assertTrue(newSlot.get("/path1").compareTo(1111111) == 0); + //cut up to 2 + newSlot = pathStatsQueue.mapReducePaths(2, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 12); + assertTrue(newSlot.get("/path1").compareTo(1) == 0); + assertTrue(newSlot.get("/path1/path2").compareTo(1111100) == 0); + //cut up to 3 + newSlot = pathStatsQueue.mapReducePaths(3, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 112); + assertTrue(newSlot.get("/path1").compareTo(1) == 0); + assertTrue(newSlot.get("/path1/path2/path3").compareTo(1111000) == 0); + //cut up to 4 + newSlot = pathStatsQueue.mapReducePaths(4, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 1112); + assertTrue(newSlot.get("/path1/path2/path3/path4").compareTo(1110000) == 0); + //cut up to 5 + newSlot = pathStatsQueue.mapReducePaths(5, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 11112); + assertTrue(newSlot.get("/path1/path2/path3/path4/path5").compareTo(1100000) == 0); + //cut up to 6 + newSlot = pathStatsQueue.mapReducePaths(6, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 111111); + assertTrue(newSlot.get("/path1/path2/path3/path4/path5/path6").compareTo(1000001) == 0); + //cut up to 7 + newSlot = pathStatsQueue.mapReducePaths(7, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.size() == 1111111); + } + + @Test + public void testCollectEmptyStats() throws InterruptedException { + RequestPathMetricsCollector requestPathMetricsCollector = new RequestPathMetricsCollector(); + RequestPathMetricsCollector.PathStatsQueue pathStatsQueue = requestPathMetricsCollector.new PathStatsQueue(getChildren); + Thread.sleep(5000); + Map newSlot = pathStatsQueue.mapReducePaths(3, pathStatsQueue.getCurrentSlot()); + assertTrue(newSlot.isEmpty()); + pathStatsQueue.start(); + Thread.sleep(15000); + newSlot = pathStatsQueue.collectStats(1); + assertTrue(newSlot.size() == 0); + newSlot = pathStatsQueue.collectStats(2); + assertTrue(newSlot.size() == 0); + newSlot = pathStatsQueue.collectStats(5); + assertTrue(newSlot.size() == 0); + } + + @Test + @Ignore + public void testCollectStats() throws InterruptedException { + RequestPathMetricsCollector requestPathMetricsCollector = new RequestPathMetricsCollector(true); + RequestPathMetricsCollector.PathStatsQueue pathStatsQueue = requestPathMetricsCollector.new PathStatsQueue(getChildren); + pathStatsQueue.start(); + Thread path7 = new Thread(() -> { + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int j = 0; j < 100000; j++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5/path6/path7" + "_" + i + "_" + j); + } + } + }); + path7.start(); + Thread path6 = new Thread(() -> { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5/path6"); + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int j = 0; j < 10000; j++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5/path6" + "_" + i + "_" + j); + } + } + }); + path6.start(); + for (int i = 0; i < 1; i++) { + pathStatsQueue.registerRequest("/path1"); + } + for (int i = 0; i < 10; i++) { + pathStatsQueue.registerRequest("/path1/path2" + "_" + i); + } + for (int i = 0; i < 100; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3" + "_" + i); + } + for (int i = 0; i < 1000; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4" + "_" + i); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int i = 0; i < 10000; i++) { + pathStatsQueue.registerRequest("/path1/path2/path3/path4/path5" + "_" + i); + } + path6.join(); + path7.join(); + Map newSlot = pathStatsQueue.collectStats(1); + assertEquals(newSlot.size(), 1); + assertEquals(newSlot.get("/path1").intValue(), 1111112); + //cut up to 2 + newSlot = pathStatsQueue.collectStats(2); + assertEquals(newSlot.size(), 12); + assertEquals(newSlot.get("/path1").intValue(), 1); + assertEquals(newSlot.get("/path1/path2").intValue(), 1111101); + //cut up to 3 + newSlot = pathStatsQueue.collectStats(3); + assertEquals(newSlot.size(), 112); + assertEquals(newSlot.get("/path1").intValue(), 1); + assertEquals(newSlot.get("/path1/path2/path3").intValue(), 1111001); + //cut up to 4 + newSlot = pathStatsQueue.collectStats(4); + assertEquals(newSlot.size(), 1112); + assertEquals(newSlot.get("/path1/path2/path3/path4").intValue(), 1110001); + //cut up to 5 + newSlot = pathStatsQueue.collectStats(5); + assertEquals(newSlot.size(), 11112); + assertEquals(newSlot.get("/path1/path2/path3/path4/path5").intValue(), 1100001); + //cut up to 6 + newSlot = pathStatsQueue.collectStats(6); + assertEquals(newSlot.size(), 111112); + assertEquals(newSlot.get("/path1/path2/path3/path4/path5/path6").intValue(), 1000001); + } + + @Test + public void testAggregate() throws InterruptedException { + RequestPathMetricsCollector requestPathMetricsCollector = new RequestPathMetricsCollector(true); + Thread path7 = new Thread(() -> { + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int j = 0; j < 100000; j++) { + requestPathMetricsCollector.registerRequest(getData, "/path1/path2/path3/path4/path5/path6/path7" + + "_" + + i + + "_" + + j); + } + } + }); + path7.start(); + Thread path6 = new Thread(() -> { + requestPathMetricsCollector.registerRequest(getChildren2, "/path1/path2/path3/path4/path5/path6"); + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int j = 0; j < 10000; j++) { + requestPathMetricsCollector.registerRequest(getChildren, "/path1/path2/path3/path4/path5/path6" + + "_" + + i + + "_" + + j); + } + } + }); + path6.start(); + for (int i = 0; i < 1; i++) { + requestPathMetricsCollector.registerRequest(create2, "/path1"); + } + for (int i = 0; i < 10; i++) { + requestPathMetricsCollector.registerRequest(create, "/path1/path2" + "_" + i); + } + for (int i = 0; i < 100; i++) { + requestPathMetricsCollector.registerRequest(delete, "/path1/path2/path3" + "_" + i); + } + for (int i = 0; i < 1000; i++) { + requestPathMetricsCollector.registerRequest(setData, "/path1/path2/path3/path4" + "_" + i); + } + for (int i = 0; i < 10000; i++) { + requestPathMetricsCollector.registerRequest(exists, "/path1/path2/path3/path4/path5" + "_" + i); + } + path6.join(); + path7.join(); + Map newSlot = requestPathMetricsCollector.aggregatePaths(2, queue -> true); + assertEquals(newSlot.size(), 12); + assertEquals(newSlot.get("/path1").intValue(), 1); + assertEquals(newSlot.get("/path1/path2").intValue(), 1111101); + //cut up to 3 + newSlot = requestPathMetricsCollector.aggregatePaths(3, queue -> true); + assertEquals(newSlot.size(), 112); + assertEquals(newSlot.get("/path1").intValue(), 1); + assertEquals(newSlot.get("/path1/path2/path3").intValue(), 1111001); + //cut up to 4 + newSlot = requestPathMetricsCollector.aggregatePaths(4, queue -> true); + assertEquals(newSlot.size(), 1112); + assertEquals(newSlot.get("/path1/path2/path3/path4").intValue(), 1110001); + //cut up to 5 + newSlot = requestPathMetricsCollector.aggregatePaths(5, queue -> true); + assertEquals(newSlot.size(), 11112); + assertEquals(newSlot.get("/path1/path2/path3/path4/path5").intValue(), 1100001); + //cut up to 6 + newSlot = requestPathMetricsCollector.aggregatePaths(6, queue -> true); + assertEquals(newSlot.size(), 111112); + assertEquals(newSlot.get("/path1/path2/path3/path4/path5/path6").intValue(), 1000001); + //cut up to 7 but the initial mapReduce kept only 6 + newSlot = requestPathMetricsCollector.aggregatePaths(7, queue -> true); + assertEquals(newSlot.size(), 111112); + assertEquals(newSlot.get("/path1/path2/path3/path4/path5/path6").intValue(), 1000001); + //test predicate + //cut up to 4 for all the reads + newSlot = requestPathMetricsCollector.aggregatePaths(4, queue -> !queue.isWriteOperation()); + assertEquals(newSlot.size(), 1); + assertEquals(newSlot.get("/path1/path2/path3/path4").intValue(), 1110001); + //cut up to 4 for all the write + newSlot = requestPathMetricsCollector.aggregatePaths(4, queue -> queue.isWriteOperation()); + assertEquals(newSlot.size(), 1111); + //cut up to 3 for all the write + newSlot = requestPathMetricsCollector.aggregatePaths(3, queue -> queue.isWriteOperation()); + assertEquals(newSlot.size(), 112); + assertEquals(newSlot.get("/path1/path2/path3").intValue(), 1000); + } + + @Test + public void testTopPath() throws InterruptedException { + RequestPathMetricsCollector requestPathMetricsCollector = new RequestPathMetricsCollector(true); + Thread path7 = new Thread(() -> { + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int j = 0; j < 100000; j++) { + requestPathMetricsCollector.registerRequest(getData, "/path1/path2/path3/path4/path5/path6/path7" + + "_" + + i + + "_" + + j); + } + } + }); + path7.start(); + Thread path6 = new Thread(() -> { + requestPathMetricsCollector.registerRequest(getChildren2, "/path1/path2/path3/path4/path5/path6"); + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + for (int j = 0; j < 10000; j++) { + requestPathMetricsCollector.registerRequest(getChildren, "/path1/path2/path3/path4/path5/path6" + + "_" + + i + + "_" + + j); + } + } + }); + path6.start(); + for (int i = 0; i < 1; i++) { + requestPathMetricsCollector.registerRequest(create2, "/path1"); + } + for (int i = 0; i < 10; i++) { + requestPathMetricsCollector.registerRequest(create, "/path1/path2" + "_" + i); + } + for (int i = 0; i < 100; i++) { + requestPathMetricsCollector.registerRequest(delete, "/path1/path2/path3" + "_" + i); + } + for (int i = 0; i < 1000; i++) { + requestPathMetricsCollector.registerRequest(setData, "/path1/path2/path3/path4" + "_" + i); + } + for (int i = 0; i < 10000; i++) { + requestPathMetricsCollector.registerRequest(exists, "/path1/path2/path3/path4/path5" + "_" + i); + } + path6.join(); + path7.join(); + StringBuilder sb1 = new StringBuilder(); + Map newSlot = requestPathMetricsCollector.aggregatePaths(3, queue -> queue.isWriteOperation()); + requestPathMetricsCollector.logTopPaths(newSlot, entry -> sb1.append(entry.getKey() + + " : " + + entry.getValue() + + "\n")); + assertTrue(sb1.toString().startsWith("/path1/path2/path3 : 1000")); + StringBuilder sb2 = new StringBuilder(); + newSlot = requestPathMetricsCollector.aggregatePaths(3, queue -> !queue.isWriteOperation()); + requestPathMetricsCollector.logTopPaths(newSlot, entry -> sb2.append(entry.getKey() + + " : " + + entry.getValue() + + "\n")); + assertTrue(sb2.toString().startsWith("/path1/path2/path3 : 1110001")); + StringBuilder sb3 = new StringBuilder(); + newSlot = requestPathMetricsCollector.aggregatePaths(4, queue -> true); + requestPathMetricsCollector.logTopPaths(newSlot, entry -> sb3.append(entry.getKey() + + " : " + + entry.getValue() + + "\n")); + assertTrue(sb3.toString().startsWith("/path1/path2/path3/path4 : 1110001")); + } + + @Test + public void testMultiThreadPerf() throws InterruptedException { + RequestPathMetricsCollector requestPathMetricsCollector = new RequestPathMetricsCollector(); + Random rand = new Random(System.currentTimeMillis()); + Long startTime = System.currentTimeMillis(); + ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); + //call 100k get Data + for (int i = 0; i < 100000; i++) { + executor.submit(new Thread(() -> requestPathMetricsCollector.registerRequest(getData, "/path1/path2/path" + + rand.nextInt(10)))); + } + //5K create + for (int i = 0; i < 5000; i++) { + executor.submit(new Thread(() -> requestPathMetricsCollector.registerRequest(create2, "/path1/path2/path" + + rand.nextInt(10)))); + } + //5K delete + for (int i = 0; i < 5000; i++) { + executor.submit(new Thread(() -> requestPathMetricsCollector.registerRequest(delete, "/path1/path2/path" + + rand.nextInt(10)))); + } + //40K getChildren + for (int i = 0; i < 40000; i++) { + executor.submit(new Thread(() -> requestPathMetricsCollector.registerRequest(getChildren, "/path1/path2/path" + + rand.nextInt(10)))); + } + executor.shutdown(); + //wait for at most 10 mill seconds + executor.awaitTermination(10, TimeUnit.MILLISECONDS); + assertTrue(executor.isTerminated()); + Long endTime = System.currentTimeMillis(); + //less than 2 seconds total time + assertTrue(TimeUnit.MILLISECONDS.toSeconds(endTime - startTime) < 3); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/SerializeUtilsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/SerializeUtilsTest.java new file mode 100644 index 0000000..5caa718 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/SerializeUtilsTest.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.OutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class SerializeUtilsTest { + + @Test + public void testSerializeRequestRequestIsNull() { + byte[] data = SerializeUtils.serializeRequest(null); + assertNull(data); + } + + @Test + public void testSerializeRequestRequestHeaderIsNull() { + Request request = new Request(0, 0, 0, null, null, 0); + byte[] data = SerializeUtils.serializeRequest(request); + assertNull(data); + } + + @Test + public void testSerializeRequestWithoutTxn() throws IOException { + // Arrange + TxnHeader header = mock(TxnHeader.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + OutputArchive oa = (OutputArchive) args[0]; + oa.writeString("header", "test"); + return null; + } + }).when(header).serialize(any(OutputArchive.class), anyString()); + Request request = new Request(1, 2, 3, header, null, 4); + + // Act + byte[] data = SerializeUtils.serializeRequest(request); + + // Assert + assertNotNull(data); + verify(header).serialize(any(OutputArchive.class), eq("hdr")); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeString("header", "test"); + baos.close(); + assertArrayEquals(baos.toByteArray(), data); + } + + @Test + public void testSerializeRequestWithTxn() throws IOException { + // Arrange + TxnHeader header = mock(TxnHeader.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + OutputArchive oa = (OutputArchive) args[0]; + oa.writeString("header", "test"); + return null; + } + }).when(header).serialize(any(OutputArchive.class), anyString()); + Record txn = mock(Record.class); + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + Object[] args = invocation.getArguments(); + OutputArchive oa = (OutputArchive) args[0]; + oa.writeString("record", "test"); + return null; + } + }).when(txn).serialize(any(OutputArchive.class), anyString()); + Request request = new Request(1, 2, 3, header, txn, 4); + + // Act + byte[] data = SerializeUtils.serializeRequest(request); + + // Assert + assertNotNull(data); + InOrder inOrder = inOrder(header, txn); + inOrder.verify(header).serialize(any(OutputArchive.class), eq("hdr")); + inOrder.verify(txn).serialize(any(OutputArchive.class), eq("txn")); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeString("header", "test"); + boa.writeString("record", "test"); + baos.close(); + assertArrayEquals(baos.toByteArray(), data); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/VerifyingFileFactoryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/VerifyingFileFactoryTest.java new file mode 100644 index 0000000..925b01f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/util/VerifyingFileFactoryTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.util; + +import static org.junit.Assert.assertEquals; +import java.io.File; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class VerifyingFileFactoryTest extends ZKTestCase { + + private Logger log; + + @Before + public void setUp() { + log = LoggerFactory.getLogger("TODO: Mock Logging"); + } + + @Test + public void testForWarningOnRelativePath() { + VerifyingFileFactory vff = new VerifyingFileFactory.Builder(log).warnForRelativePath().build(); + vff.create("a/relative/path"); + // assertTrue(log.hasWarned); + } + + @Test + public void testForNoWarningOnIntendedRelativePath() { + VerifyingFileFactory vff = new VerifyingFileFactory.Builder(log).warnForRelativePath().build(); + vff.create("./an/intended/relative/path"); + // assertFalse(log.hasWarned); + } + + @Test(expected = IllegalArgumentException.class) + public void testForFailForNonExistingPath() { + VerifyingFileFactory vff = new VerifyingFileFactory.Builder(log).failForNonExistingPath().build(); + vff.create("/I/H0p3/this/path/d035/n0t/ex15t"); + } + + @Test + public void testFileHasCorrectPath() { + File file = new File("/some/path"); + VerifyingFileFactory vff = new VerifyingFileFactory.Builder(log).build(); + assertEquals(file, vff.create(file.getPath())); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/PathParentIteratorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/PathParentIteratorTest.java new file mode 100644 index 0000000..59bb17a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/PathParentIteratorTest.java @@ -0,0 +1,84 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import org.junit.Assert; +import org.junit.Test; + +public class PathParentIteratorTest { + @Test + public void testRoot() { + PathParentIterator pathParentIterator = PathParentIterator.forAll("/"); + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertFalse(pathParentIterator.atParentPath()); + Assert.assertEquals(pathParentIterator.next(), "/"); + Assert.assertFalse(pathParentIterator.hasNext()); + } + + @Test + public void test1Level() { + PathParentIterator pathParentIterator = PathParentIterator.forAll("/a"); + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertFalse(pathParentIterator.atParentPath()); + Assert.assertEquals(pathParentIterator.next(), "/a"); + + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/"); + Assert.assertTrue(pathParentIterator.atParentPath()); + + Assert.assertFalse(pathParentIterator.hasNext()); + } + + @Test + public void testLong() { + PathParentIterator pathParentIterator = PathParentIterator.forAll("/a/b/c/d"); + + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/a/b/c/d"); + Assert.assertFalse(pathParentIterator.atParentPath()); + + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/a/b/c"); + Assert.assertTrue(pathParentIterator.atParentPath()); + + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/a/b"); + Assert.assertTrue(pathParentIterator.atParentPath()); + + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/a"); + Assert.assertTrue(pathParentIterator.atParentPath()); + + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/"); + Assert.assertTrue(pathParentIterator.atParentPath()); + + Assert.assertFalse(pathParentIterator.hasNext()); + } + + @Test + public void testForPathOnly() { + PathParentIterator pathParentIterator = PathParentIterator.forPathOnly("/a/b/c/d"); + Assert.assertTrue(pathParentIterator.hasNext()); + Assert.assertEquals(pathParentIterator.next(), "/a/b/c/d"); + Assert.assertFalse(pathParentIterator.atParentPath()); + + Assert.assertFalse(pathParentIterator.hasNext()); + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/RecursiveWatchQtyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/RecursiveWatchQtyTest.java new file mode 100644 index 0000000..067cb2a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/RecursiveWatchQtyTest.java @@ -0,0 +1,197 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.watch; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.junit.Before; +import org.junit.Test; + +public class RecursiveWatchQtyTest { + private WatchManager watchManager; + + private static final int clientQty = 25; + private static final int iterations = 1000; + + private static class DummyWatcher implements Watcher { + @Override + public void process(WatchedEvent event) { + // NOP + } + } + + @Before + public void setup() { + watchManager = new WatchManager(); + } + + @Test + public void testRecursiveQty() { + WatcherModeManager manager = new WatcherModeManager(); + DummyWatcher watcher = new DummyWatcher(); + manager.setWatcherMode(watcher, "/a", WatcherMode.DEFAULT_WATCHER_MODE); + assertEquals(0, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a", WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(1, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a/b", WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(2, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a", WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(2, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a/b", WatcherMode.PERSISTENT); + assertEquals(1, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a/b", WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(2, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a/b", WatcherMode.DEFAULT_WATCHER_MODE); + assertEquals(1, manager.getRecursiveQty()); + manager.setWatcherMode(watcher, "/a", WatcherMode.PERSISTENT); + assertEquals(0, manager.getRecursiveQty()); + } + + @Test + public void testAddRemove() { + Watcher watcher1 = new DummyWatcher(); + Watcher watcher2 = new DummyWatcher(); + + watchManager.addWatch("/a", watcher1, WatcherMode.PERSISTENT_RECURSIVE); + watchManager.addWatch("/b", watcher2, WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(2, watchManager.getRecursiveWatchQty()); + assertTrue(watchManager.removeWatcher("/a", watcher1)); + assertTrue(watchManager.removeWatcher("/b", watcher2)); + assertEquals(0, watchManager.getRecursiveWatchQty()); + } + + @Test + public void testAddRemoveAlt() { + Watcher watcher1 = new DummyWatcher(); + Watcher watcher2 = new DummyWatcher(); + + watchManager.addWatch("/a", watcher1, WatcherMode.PERSISTENT_RECURSIVE); + watchManager.addWatch("/b", watcher2, WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(2, watchManager.getRecursiveWatchQty()); + watchManager.removeWatcher(watcher1); + watchManager.removeWatcher(watcher2); + assertEquals(0, watchManager.getRecursiveWatchQty()); + } + + @Test + public void testDoubleAdd() { + Watcher watcher = new DummyWatcher(); + + watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE); + watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(1, watchManager.getRecursiveWatchQty()); + watchManager.removeWatcher(watcher); + assertEquals(0, watchManager.getRecursiveWatchQty()); + } + + @Test + public void testSameWatcherMultiPath() { + Watcher watcher = new DummyWatcher(); + + watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE); + watchManager.addWatch("/a/b", watcher, WatcherMode.PERSISTENT_RECURSIVE); + watchManager.addWatch("/a/b/c", watcher, WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(3, watchManager.getRecursiveWatchQty()); + assertTrue(watchManager.removeWatcher("/a/b", watcher)); + assertEquals(2, watchManager.getRecursiveWatchQty()); + watchManager.removeWatcher(watcher); + assertEquals(0, watchManager.getRecursiveWatchQty()); + } + + @Test + public void testChangeType() { + Watcher watcher = new DummyWatcher(); + + watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT); + assertEquals(0, watchManager.getRecursiveWatchQty()); + watchManager.addWatch("/a", watcher, WatcherMode.PERSISTENT_RECURSIVE); + assertEquals(1, watchManager.getRecursiveWatchQty()); + watchManager.addWatch("/a", watcher, WatcherMode.STANDARD); + assertEquals(0, watchManager.getRecursiveWatchQty()); + assertTrue(watchManager.removeWatcher("/a", watcher)); + assertEquals(0, watchManager.getRecursiveWatchQty()); + } + + @Test + public void testRecursiveQtyConcurrency() { + ThreadLocalRandom random = ThreadLocalRandom.current(); + WatcherModeManager manager = new WatcherModeManager(); + ExecutorService threadPool = Executors.newFixedThreadPool(clientQty); + List> tasks = null; + CountDownLatch completedLatch = new CountDownLatch(clientQty); + try { + tasks = IntStream.range(0, clientQty) + .mapToObj(__ -> threadPool.submit(() -> iterate(manager, completedLatch))) + .collect(Collectors.toList()); + try { + completedLatch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } finally { + if (tasks != null) { + tasks.forEach(t -> t.cancel(true)); + } + threadPool.shutdownNow(); + } + + int expectedRecursiveQty = (int) manager.getWatcherModes().values() + .stream() + .filter(mode -> mode == WatcherMode.PERSISTENT_RECURSIVE) + .count(); + assertEquals(expectedRecursiveQty, manager.getRecursiveQty()); + } + + private void iterate(WatcherModeManager manager, CountDownLatch completedLatch) { + ThreadLocalRandom random = ThreadLocalRandom.current(); + try { + for (int i = 0; i < iterations; ++i) { + String path = "/" + random.nextInt(clientQty); + boolean doSet = random.nextInt(100) > 33; // 2/3 will be sets + if (doSet) { + WatcherMode mode = WatcherMode.values()[random.nextInt(WatcherMode.values().length)]; + manager.setWatcherMode(new DummyWatcher(), path, mode); + } else { + manager.removeWatcher(new DummyWatcher(), path); + } + + int sleepMillis = random.nextInt(2); + if (sleepMillis > 0) { + try { + Thread.sleep(sleepMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + } + } finally { + completedLatch.countDown(); + } + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java new file mode 100644 index 0000000..e29dab9 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchManagerTest.java @@ -0,0 +1,470 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.DumbWatcher; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerMetrics; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(Parameterized.class) +public class WatchManagerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(WatchManagerTest.class); + + private static final String PATH_PREFIX = "/path"; + + private ConcurrentHashMap watchers; + private Random r; + private String className; + + public WatchManagerTest(String className) { + this.className = className; + } + + @Parameterized.Parameters + public static List data() { + return Arrays.asList(new Object[][]{{WatchManager.class.getName()}, {WatchManagerOptimized.class.getName()}}); + } + + @Before + public void setUp() { + ServerMetrics.getMetrics().resetAll(); + watchers = new ConcurrentHashMap(); + r = new Random(System.nanoTime()); + } + + public IWatchManager getWatchManager() throws IOException { + System.setProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME, className); + return WatchManagerFactory.createWatchManager(); + } + + public DumbWatcher createOrGetWatcher(int watcherId) { + if (!watchers.containsKey(watcherId)) { + DumbWatcher watcher = new DumbWatcher(watcherId); + watchers.putIfAbsent(watcherId, watcher); + } + return watchers.get(watcherId); + } + + public class AddWatcherWorker extends Thread { + + private final IWatchManager manager; + private final int paths; + private final int watchers; + private final AtomicInteger watchesAdded; + private volatile boolean stopped = false; + + public AddWatcherWorker( + IWatchManager manager, int paths, int watchers, AtomicInteger watchesAdded) { + this.manager = manager; + this.paths = paths; + this.watchers = watchers; + this.watchesAdded = watchesAdded; + } + + @Override + public void run() { + while (!stopped) { + String path = PATH_PREFIX + r.nextInt(paths); + Watcher watcher = createOrGetWatcher(r.nextInt(watchers)); + if (manager.addWatch(path, watcher)) { + watchesAdded.addAndGet(1); + } + } + } + + public void shutdown() { + stopped = true; + } + + } + + public class WatcherTriggerWorker extends Thread { + + private final IWatchManager manager; + private final int paths; + private final AtomicInteger triggeredCount; + private volatile boolean stopped = false; + + public WatcherTriggerWorker( + IWatchManager manager, int paths, AtomicInteger triggeredCount) { + this.manager = manager; + this.paths = paths; + this.triggeredCount = triggeredCount; + } + + @Override + public void run() { + while (!stopped) { + String path = PATH_PREFIX + r.nextInt(paths); + WatcherOrBitSet s = manager.triggerWatch(path, EventType.NodeDeleted); + if (s != null) { + triggeredCount.addAndGet(s.size()); + } + try { + Thread.sleep(r.nextInt(10)); + } catch (InterruptedException e) { + } + } + } + + public void shutdown() { + stopped = true; + } + + } + + public class RemoveWatcherWorker extends Thread { + + private final IWatchManager manager; + private final int paths; + private final int watchers; + private final AtomicInteger watchesRemoved; + private volatile boolean stopped = false; + + public RemoveWatcherWorker( + IWatchManager manager, int paths, int watchers, AtomicInteger watchesRemoved) { + this.manager = manager; + this.paths = paths; + this.watchers = watchers; + this.watchesRemoved = watchesRemoved; + } + + @Override + public void run() { + while (!stopped) { + String path = PATH_PREFIX + r.nextInt(paths); + Watcher watcher = createOrGetWatcher(r.nextInt(watchers)); + if (manager.removeWatcher(path, watcher)) { + watchesRemoved.addAndGet(1); + } + try { + Thread.sleep(r.nextInt(10)); + } catch (InterruptedException e) { + } + } + } + + public void shutdown() { + stopped = true; + } + + } + + public class CreateDeadWatchersWorker extends Thread { + + private final IWatchManager manager; + private final int watchers; + private final Set removedWatchers; + private volatile boolean stopped = false; + + public CreateDeadWatchersWorker( + IWatchManager manager, int watchers, Set removedWatchers) { + this.manager = manager; + this.watchers = watchers; + this.removedWatchers = removedWatchers; + } + + @Override + public void run() { + while (!stopped) { + DumbWatcher watcher = createOrGetWatcher(r.nextInt(watchers)); + watcher.setStale(); + manager.removeWatcher(watcher); + synchronized (removedWatchers) { + removedWatchers.add(watcher); + } + try { + Thread.sleep(r.nextInt(10)); + } catch (InterruptedException e) { + } + } + } + + public void shutdown() { + stopped = true; + } + + } + + /** + * Concurrently add and trigger watch, make sure the watches triggered + * are the same as the number added. + */ + @Test(timeout = 90000) + public void testAddAndTriggerWatcher() throws IOException { + IWatchManager manager = getWatchManager(); + int paths = 1; + int watchers = 10000; + + // 1. start 5 workers to trigger watchers on that path + // count all the watchers have been fired + AtomicInteger watchTriggered = new AtomicInteger(); + List triggerWorkers = new ArrayList(); + for (int i = 0; i < 5; i++) { + WatcherTriggerWorker worker = new WatcherTriggerWorker(manager, paths, watchTriggered); + triggerWorkers.add(worker); + worker.start(); + } + + // 2. start 5 workers to add different watchers on the same path + // count all the watchers being added + AtomicInteger watchesAdded = new AtomicInteger(); + List addWorkers = new ArrayList(); + for (int i = 0; i < 5; i++) { + AddWatcherWorker worker = new AddWatcherWorker(manager, paths, watchers, watchesAdded); + addWorkers.add(worker); + worker.start(); + } + + while (watchesAdded.get() < 100000) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + + // 3. stop all the addWorkers + for (AddWatcherWorker worker : addWorkers) { + worker.shutdown(); + } + + // 4. running the trigger worker a bit longer to make sure + // all watchers added are fired + try { + Thread.sleep(500); + } catch (InterruptedException e) { + } + + // 5. stop all triggerWorkers + for (WatcherTriggerWorker worker : triggerWorkers) { + worker.shutdown(); + } + + // 6. make sure the total watch triggered is same as added + assertTrue(watchesAdded.get() > 0); + assertEquals(watchesAdded.get(), watchTriggered.get()); + } + + /** + * Concurrently add and remove watch, make sure the watches left + + * the watches removed are equal to the total added watches. + */ + @Test(timeout = 90000) + public void testRemoveWatcherOnPath() throws IOException { + IWatchManager manager = getWatchManager(); + int paths = 10; + int watchers = 10000; + + // 1. start 5 workers to remove watchers on those path + // record the watchers have been removed + AtomicInteger watchesRemoved = new AtomicInteger(); + List removeWorkers = new ArrayList(); + for (int i = 0; i < 5; i++) { + RemoveWatcherWorker worker = new RemoveWatcherWorker(manager, paths, watchers, watchesRemoved); + removeWorkers.add(worker); + worker.start(); + } + + // 2. start 5 workers to add different watchers on different path + // record the watchers have been added + AtomicInteger watchesAdded = new AtomicInteger(); + List addWorkers = new ArrayList(); + for (int i = 0; i < 5; i++) { + AddWatcherWorker worker = new AddWatcherWorker(manager, paths, watchers, watchesAdded); + addWorkers.add(worker); + worker.start(); + } + + while (watchesAdded.get() < 100000) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + + // 3. stop all workers + for (RemoveWatcherWorker worker : removeWorkers) { + worker.shutdown(); + } + for (AddWatcherWorker worker : addWorkers) { + worker.shutdown(); + } + + // 4. sleep for a while to make sure all the thread exited + try { + Thread.sleep(500); + } catch (InterruptedException e) { + } + + // 5. make sure left watches + removed watches = added watches + assertTrue(watchesAdded.get() > 0); + assertTrue(watchesRemoved.get() > 0); + assertTrue(manager.size() > 0); + assertEquals(watchesAdded.get(), watchesRemoved.get() + manager.size()); + } + + /** + * Concurrently add watch while close the watcher to simulate the + * client connections closed on prod. + */ + @Test(timeout = 90000) + public void testDeadWatchers() throws IOException { + System.setProperty("zookeeper.watcherCleanThreshold", "10"); + System.setProperty("zookeeper.watcherCleanIntervalInSeconds", "1"); + + IWatchManager manager = getWatchManager(); + int paths = 1; + int watchers = 100000; + + // 1. start 5 workers to randomly mark those watcher as dead + // and remove them from watch manager + Set deadWatchers = new HashSet(); + List deadWorkers = new ArrayList(); + for (int i = 0; i < 5; i++) { + CreateDeadWatchersWorker worker = new CreateDeadWatchersWorker(manager, watchers, deadWatchers); + deadWorkers.add(worker); + worker.start(); + } + + // 2. start 5 workers to add different watchers on the same path + AtomicInteger watchesAdded = new AtomicInteger(); + List addWorkers = new ArrayList(); + for (int i = 0; i < 5; i++) { + AddWatcherWorker worker = new AddWatcherWorker(manager, paths, watchers, watchesAdded); + addWorkers.add(worker); + worker.start(); + } + + while (watchesAdded.get() < 50000) { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + + // 3. stop all workers + for (CreateDeadWatchersWorker worker : deadWorkers) { + worker.shutdown(); + } + for (AddWatcherWorker worker : addWorkers) { + worker.shutdown(); + } + + // 4. sleep for a while to make sure all the thread exited + // the cleaner may wait as long as CleanerInterval+CleanerInterval/2+1 + // So need to sleep as least that long + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + } + + // 5. make sure the dead watchers are not in the existing watchers + WatchesReport existingWatchers = manager.getWatches(); + for (Watcher w : deadWatchers) { + assertFalse(existingWatchers.hasPaths(((ServerCnxn) w).getSessionId())); + } + } + + private void checkMetrics(String metricName, long min, long max, double avg, long cnt, long sum) { + Map values = MetricsUtils.currentServerMetrics(); + + assertEquals(min, values.get("min_" + metricName)); + assertEquals(max, values.get("max_" + metricName)); + assertEquals(avg, (Double) values.get("avg_" + metricName), 0.000001); + assertEquals(cnt, values.get("cnt_" + metricName)); + assertEquals(sum, values.get("sum_" + metricName)); + } + + @Test + public void testWatcherMetrics() throws IOException { + IWatchManager manager = getWatchManager(); + ServerMetrics.getMetrics().resetAll(); + + DumbWatcher watcher1 = new DumbWatcher(1); + DumbWatcher watcher2 = new DumbWatcher(2); + + final String path1 = "/path1"; + final String path2 = "/path2"; + + final String path3 = "/path3"; + + //both wather1 and wather2 are watching path1 + manager.addWatch(path1, watcher1); + manager.addWatch(path1, watcher2); + + //path2 is watched by watcher1 + manager.addWatch(path2, watcher1); + + manager.triggerWatch(path3, EventType.NodeCreated); + //path3 is not being watched so metric is 0 + checkMetrics("node_created_watch_count", 0L, 0L, 0D, 0L, 0L); + + //path1 is watched by two watchers so two fired + manager.triggerWatch(path1, EventType.NodeCreated); + checkMetrics("node_created_watch_count", 2L, 2L, 2D, 1L, 2L); + + //path2 is watched by one watcher so one fired now total is 3 + manager.triggerWatch(path2, EventType.NodeCreated); + checkMetrics("node_created_watch_count", 1L, 2L, 1.5D, 2L, 3L); + + //watches on path1 are no longer there so zero fired + manager.triggerWatch(path1, EventType.NodeDataChanged); + checkMetrics("node_changed_watch_count", 0L, 0L, 0D, 0L, 0L); + + //both wather1 and wather2 are watching path1 + manager.addWatch(path1, watcher1); + manager.addWatch(path1, watcher2); + + //path2 is watched by watcher1 + manager.addWatch(path2, watcher1); + + manager.triggerWatch(path1, EventType.NodeDataChanged); + checkMetrics("node_changed_watch_count", 2L, 2L, 2D, 1L, 2L); + + manager.triggerWatch(path2, EventType.NodeDeleted); + checkMetrics("node_deleted_watch_count", 1L, 1L, 1D, 1L, 1L); + + //make sure that node created watch count is not impacted by the fire of other event types + checkMetrics("node_created_watch_count", 1L, 2L, 1.5D, 2L, 3L); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatcherCleanerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatcherCleanerTest.java new file mode 100644 index 0000000..5152b5e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatcherCleanerTest.java @@ -0,0 +1,174 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerMetrics; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WatcherCleanerTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(WatcherCleanerTest.class); + + public static class MyDeadWatcherListener implements IDeadWatcherListener { + + private CountDownLatch latch; + private int delayMs; + private Set deadWatchers = new HashSet(); + + public void setCountDownLatch(CountDownLatch latch) { + this.latch = latch; + } + + public void setDelayMs(int delayMs) { + this.delayMs = delayMs; + } + + @Override + public void processDeadWatchers(Set deadWatchers) { + if (delayMs > 0) { + try { + Thread.sleep(delayMs); + } catch (InterruptedException e) { + } + } + this.deadWatchers.clear(); + this.deadWatchers.addAll(deadWatchers); + latch.countDown(); + } + + public Set getDeadWatchers() { + return deadWatchers; + } + + public boolean wait(int maxWaitMs) { + try { + return latch.await(maxWaitMs, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + } + return false; + } + + } + + @Test + public void testProcessDeadWatchersBasedOnThreshold() { + MyDeadWatcherListener listener = new MyDeadWatcherListener(); + int threshold = 3; + WatcherCleaner cleaner = new WatcherCleaner(listener, threshold, 60, 1, 10); + cleaner.start(); + + int i = 0; + while (i++ < threshold - 1) { + cleaner.addDeadWatcher(i); + } + // not trigger processDeadWatchers yet + assertEquals(0, listener.getDeadWatchers().size()); + + listener.setCountDownLatch(new CountDownLatch(1)); + // add another dead watcher to trigger the process + cleaner.addDeadWatcher(i); + assertTrue(listener.wait(1000)); + assertEquals(threshold, listener.getDeadWatchers().size()); + } + + @Test + public void testProcessDeadWatchersBasedOnTime() { + MyDeadWatcherListener listener = new MyDeadWatcherListener(); + WatcherCleaner cleaner = new WatcherCleaner(listener, 10, 1, 1, 10); + cleaner.start(); + + cleaner.addDeadWatcher(1); + // not trigger processDeadWatchers yet + assertEquals(0, listener.getDeadWatchers().size()); + + listener.setCountDownLatch(new CountDownLatch(1)); + assertTrue(listener.wait(2000)); + assertEquals(1, listener.getDeadWatchers().size()); + + // won't trigger event if there is no dead watchers + listener.setCountDownLatch(new CountDownLatch(1)); + assertFalse(listener.wait(2000)); + } + + @Test + public void testMaxInProcessingDeadWatchers() { + MyDeadWatcherListener listener = new MyDeadWatcherListener(); + int delayMs = 1000; + listener.setDelayMs(delayMs); + WatcherCleaner cleaner = new WatcherCleaner(listener, 1, 60, 1, 1); + cleaner.start(); + + listener.setCountDownLatch(new CountDownLatch(2)); + + long startTime = Time.currentElapsedTime(); + cleaner.addDeadWatcher(1); + cleaner.addDeadWatcher(2); + long time = Time.currentElapsedTime() - startTime; + System.out.println("time used " + time); + assertTrue(Time.currentElapsedTime() - startTime >= delayMs); + assertTrue(listener.wait(5000)); + } + + @Test + public void testDeadWatcherMetrics() { + ServerMetrics.getMetrics().resetAll(); + MyDeadWatcherListener listener = new MyDeadWatcherListener(); + WatcherCleaner cleaner = new WatcherCleaner(listener, 1, 1, 1, 1); + listener.setDelayMs(20); + cleaner.start(); + listener.setCountDownLatch(new CountDownLatch(3)); + //the dead watchers will be added one by one and cleared one by one because we set both watchCleanThreshold and + //maxInProcessingDeadWatchers to 1 + cleaner.addDeadWatcher(1); + cleaner.addDeadWatcher(2); + cleaner.addDeadWatcher(3); + + assertTrue(listener.wait(5000)); + + Map values = MetricsUtils.currentServerMetrics(); + assertThat("Adding dead watcher should be stalled twice", (Long) values.get("add_dead_watcher_stall_time"), greaterThan(0L)); + assertEquals("Total dead watchers added to the queue should be 3", 3L, values.get("dead_watchers_queued")); + assertEquals("Total dead watchers cleared should be 3", 3L, values.get("dead_watchers_cleared")); + + assertEquals(3L, values.get("cnt_dead_watchers_cleaner_latency")); + + //Each latency should be a little over 20 ms, allow 5 ms deviation + assertEquals(20D, (Double) values.get("avg_dead_watchers_cleaner_latency"), 5); + assertEquals(20D, ((Long) values.get("min_dead_watchers_cleaner_latency")).doubleValue(), 5); + assertEquals(20D, ((Long) values.get("max_dead_watchers_cleaner_latency")).doubleValue(), 5); + assertEquals(20D, ((Long) values.get("p50_dead_watchers_cleaner_latency")).doubleValue(), 5); + assertEquals(20D, ((Long) values.get("p95_dead_watchers_cleaner_latency")).doubleValue(), 5); + assertEquals(20D, ((Long) values.get("p99_dead_watchers_cleaner_latency")).doubleValue(), 5); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatcherOrBitSetTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatcherOrBitSetTest.java new file mode 100644 index 0000000..ee105df --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatcherOrBitSetTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.HashSet; +import java.util.Set; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.DumbWatcher; +import org.apache.zookeeper.server.util.BitHashSet; +import org.junit.Test; + +public class WatcherOrBitSetTest extends ZKTestCase { + + @Test + public void testWatcherSet() { + Set wset = new HashSet(); + WatcherOrBitSet hashSet = new WatcherOrBitSet(wset); + assertEquals(0, hashSet.size()); + + DumbWatcher w1 = new DumbWatcher(); + assertFalse(hashSet.contains(w1)); + wset.add(w1); + assertTrue(hashSet.contains(w1)); + assertEquals(1, hashSet.size()); + assertFalse(hashSet.contains(1)); + } + + @Test + public void testBitSet() { + BitHashSet bset = new BitHashSet(0); + WatcherOrBitSet bitSet = new WatcherOrBitSet(bset); + assertEquals(0, bitSet.size()); + + Integer bit = 1; + assertFalse(bitSet.contains(1)); + assertFalse(bitSet.contains(bit)); + + bset.add(bit); + assertTrue(bitSet.contains(1)); + assertTrue(bitSet.contains(bit)); + assertEquals(1, bitSet.size()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesPathReportTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesPathReportTest.java new file mode 100644 index 0000000..123b49b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesPathReportTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class WatchesPathReportTest extends ZKTestCase { + + private Map> m; + private WatchesPathReport r; + @Before + public void setUp() { + m = new HashMap>(); + Set s = new HashSet(); + s.add(101L); + s.add(102L); + m.put("path1", s); + s = new HashSet(); + s.add(201L); + m.put("path2", s); + r = new WatchesPathReport(m); + } + @Test + public void testHasSessions() { + assertTrue(r.hasSessions("path1")); + assertTrue(r.hasSessions("path2")); + assertFalse(r.hasSessions("path3")); + } + @Test + public void testGetSessions() { + Set s = r.getSessions("path1"); + assertEquals(2, s.size()); + assertTrue(s.contains(101L)); + assertTrue(s.contains(102L)); + s = r.getSessions("path2"); + assertEquals(1, s.size()); + assertTrue(s.contains(201L)); + assertNull(r.getSessions("path3")); + } + @Test + public void testToMap() { + assertEquals(m, r.toMap()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesReportTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesReportTest.java new file mode 100644 index 0000000..05c0547 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesReportTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class WatchesReportTest extends ZKTestCase { + + private Map> m; + private WatchesReport r; + @Before + public void setUp() { + m = new HashMap>(); + Set s = new HashSet(); + s.add("path1a"); + s.add("path1b"); + m.put(1L, s); + s = new HashSet(); + s.add("path2a"); + m.put(2L, s); + r = new WatchesReport(m); + } + @Test + public void testHasPaths() { + assertTrue(r.hasPaths(1L)); + assertTrue(r.hasPaths(2L)); + assertFalse(r.hasPaths(3L)); + } + @Test + public void testGetPaths() { + Set s = r.getPaths(1L); + assertEquals(2, s.size()); + assertTrue(s.contains("path1a")); + assertTrue(s.contains("path1b")); + s = r.getPaths(2L); + assertEquals(1, s.size()); + assertTrue(s.contains("path2a")); + assertNull(r.getPaths(3L)); + } + @Test + public void testToMap() { + assertEquals(m, r.toMap()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesSummaryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesSummaryTest.java new file mode 100644 index 0000000..9d53281 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/watch/WatchesSummaryTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.server.watch; + +import static org.junit.Assert.assertEquals; +import java.util.Map; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Before; +import org.junit.Test; + +public class WatchesSummaryTest extends ZKTestCase { + + private WatchesSummary s; + @Before + public void setUp() { + s = new WatchesSummary(1, 2, 3); + } + @Test + public void testGetters() { + assertEquals(1, s.getNumConnections()); + assertEquals(2, s.getNumPaths()); + assertEquals(3, s.getTotalWatches()); + } + @Test + public void testToMap() { + Map m = s.toMap(); + assertEquals(3, m.size()); + assertEquals(Integer.valueOf(1), m.get(WatchesSummary.KEY_NUM_CONNECTIONS)); + assertEquals(Integer.valueOf(2), m.get(WatchesSummary.KEY_NUM_PATHS)); + assertEquals(Integer.valueOf(3), m.get(WatchesSummary.KEY_NUM_TOTAL_WATCHES)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLCountTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLCountTest.java new file mode 100644 index 0000000..6b8f9ff --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLCountTest.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ACLCountTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ACLCountTest.class); + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + + /** + * + * Create a node and add 4 ACL values to it, but there are only 2 unique ACL values, + * and each is repeated once: + * + * ACL(ZooDefs.Perms.READ,ZooDefs.Ids.ANYONE_ID_UNSAFE); + * ACL(ZooDefs.Perms.ALL,ZooDefs.Ids.AUTH_IDS); + * ACL(ZooDefs.Perms.READ,ZooDefs.Ids.ANYONE_ID_UNSAFE); + * ACL(ZooDefs.Perms.ALL,ZooDefs.Ids.AUTH_IDS); + * + * Even though we've added 4 ACL values, there should only be 2 ACLs for that node, + * since there are only 2 *unique* ACL values. + */ + @Test + public void testAclCount() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(1000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + ZooKeeper zk; + + final ArrayList CREATOR_ALL_AND_WORLD_READABLE = new ArrayList() { + { + add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE)); + add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS)); + add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE)); + add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.AUTH_IDS)); + } + }; + + try { + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zk = ClientBase.createZKClient(HOSTPORT); + + zk.addAuthInfo("digest", "pat:test".getBytes()); + zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); + + String path = "/path"; + + try { + assertEquals(4, CREATOR_ALL_AND_WORLD_READABLE.size()); + } catch (Exception e) { + LOG.error("Something is fundamentally wrong with ArrayList's add() method. add()ing four times to an empty ArrayList should result in an ArrayList with 4 members."); + throw e; + } + + zk.create(path, path.getBytes(), CREATOR_ALL_AND_WORLD_READABLE, CreateMode.PERSISTENT); + List acls = zk.getACL("/path", new Stat()); + assertEquals(2, acls.size()); + } catch (Exception e) { + // test failed somehow. + assertTrue(false); + } + + f.shutdown(); + zks.shutdown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLRootTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLRootTest.java new file mode 100644 index 0000000..3f4b9d4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLRootTest.java @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Test; + +public class ACLRootTest extends ClientBase { + + @Test + public void testRootAcl() throws Exception { + ZooKeeper zk = createClient(); + try { + // set auth using digest + zk.addAuthInfo("digest", "pat:test".getBytes()); + zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); + zk.getData("/", false, null); + zk.close(); + // verify no access + zk = createClient(); + try { + zk.getData("/", false, null); + fail("validate auth"); + } catch (KeeperException.NoAuthException e) { + // expected + } + try { + zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("validate auth"); + } catch (KeeperException.InvalidACLException e) { + // expected + } + zk.addAuthInfo("digest", "world:anyone".getBytes()); + try { + zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("validate auth"); + } catch (KeeperException.NoAuthException e) { + // expected + } + zk.close(); + // verify access using original auth + zk = createClient(); + zk.addAuthInfo("digest", "pat:test".getBytes()); + zk.getData("/", false, null); + zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.delete("/apps", -1); + // reset acl (back to open) and verify accessible again + zk.setACL("/", Ids.OPEN_ACL_UNSAFE, -1); + zk.close(); + zk = createClient(); + zk.getData("/", false, null); + zk.create("/apps", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + try { + zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("validate auth"); + } catch (KeeperException.InvalidACLException e) { + // expected + } + zk.delete("/apps", -1); + zk.addAuthInfo("digest", "world:anyone".getBytes()); + zk.create("/apps", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.close(); + zk = createClient(); + zk.delete("/apps", -1); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLTest.java new file mode 100644 index 0000000..df94e3a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ACLTest.java @@ -0,0 +1,305 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException.InvalidACLException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.auth.IPAuthenticationProvider; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ACLTest extends ZKTestCase implements Watcher { + + private static final Logger LOG = LoggerFactory.getLogger(ACLTest.class); + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private volatile CountDownLatch startSignal; + + @Test + public void testIPAuthenticationIsValidCIDR() throws Exception { + IPAuthenticationProvider prov = new IPAuthenticationProvider(); + assertTrue("testing no netmask", prov.isValid("127.0.0.1")); + assertTrue("testing single ip netmask", prov.isValid("127.0.0.1/32")); + assertTrue("testing lowest netmask possible", prov.isValid("127.0.0.1/0")); + assertFalse("testing netmask too high", prov.isValid("127.0.0.1/33")); + assertFalse("testing netmask too low", prov.isValid("10.0.0.1/-1")); + } + + @Test + public void testNettyIpAuthDefault() throws Exception { + String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + ClientBase.setupTestEnv(); + File tmpDir = ClientBase.createTmpDir(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(1000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + try { + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ClientBase.createZKClient(HOSTPORT); + for (ServerCnxn cnxn : f.getConnections()) { + boolean foundID = false; + for (Id id : cnxn.getAuthInfo()) { + if (id.getScheme().equals("ip")) { + foundID = true; + break; + } + } + assertTrue(foundID); + } + } finally { + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } + } + + @Test + public void testDisconnectedAddAuth() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(1000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + try { + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + zk.addAuthInfo("digest", "pat:test".getBytes()); + zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); + } finally { + zk.close(); + } + } finally { + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + } + } + + /** + * Verify that acl optimization of storing just + * a few acls and there references in the data + * node is actually working. + */ + @Test + public void testAcls() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(1000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + ZooKeeper zk; + String path; + try { + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zk = ClientBase.createZKClient(HOSTPORT); + LOG.info("starting creating acls"); + for (int i = 0; i < 100; i++) { + path = "/" + i; + zk.create(path, path.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + int size = zks.getZKDatabase().getAclSize(); + assertTrue("size of the acl map ", (2 == zks.getZKDatabase().getAclSize())); + for (int j = 100; j < 200; j++) { + path = "/" + j; + ACL acl = new ACL(); + acl.setPerms(0); + Id id = new Id(); + id.setId("1.1.1." + j); + id.setScheme("ip"); + acl.setId(id); + List list = new ArrayList(); + list.add(acl); + zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT); + } + assertTrue("size of the acl map ", (102 == zks.getZKDatabase().getAclSize())); + } finally { + // now shutdown the server and restart it + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + } + + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + f = ServerCnxnFactory.createFactory(PORT, -1); + + f.startup(zks); + try { + assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + zk = ClientBase.createZKClient(HOSTPORT); + assertTrue("acl map ", (102 == zks.getZKDatabase().getAclSize())); + for (int j = 200; j < 205; j++) { + path = "/" + j; + ACL acl = new ACL(); + acl.setPerms(0); + Id id = new Id(); + id.setId("1.1.1." + j); + id.setScheme("ip"); + acl.setId(id); + ArrayList list = new ArrayList(); + list.add(acl); + zk.create(path, path.getBytes(), list, CreateMode.PERSISTENT); + } + assertTrue("acl map ", (107 == zks.getZKDatabase().getAclSize())); + + zk.close(); + } finally { + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + } + + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatcherEvent) + */ + public void process(WatchedEvent event) { + LOG.info("Event:{} {} {}", event.getState(), event.getType(), event.getPath()); + if (event.getState() == KeeperState.SyncConnected) { + if (startSignal != null && startSignal.getCount() > 0) { + LOG.info("startsignal.countDown()"); + startSignal.countDown(); + } else { + LOG.warn("startsignal {}", startSignal); + } + } + } + + @Test + public void testNullACL() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + // case 1 : null ACL with create + try { + zk.create("/foo", "foo".getBytes(), null, CreateMode.PERSISTENT); + fail("Expected InvalidACLException for null ACL parameter"); + } catch (InvalidACLException e) { + // Expected. Do nothing + } + + // case 2 : null ACL with other create API + try { + zk.create("/foo", "foo".getBytes(), null, CreateMode.PERSISTENT, null); + fail("Expected InvalidACLException for null ACL parameter"); + } catch (InvalidACLException e) { + // Expected. Do nothing + } + + // case 3 : null ACL with setACL + try { + zk.setACL("/foo", null, 0); + fail("Expected InvalidACLException for null ACL parameter"); + } catch (InvalidACLException e) { + // Expected. Do nothing + } + } finally { + zk.close(); + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + } + } + + @Test + public void testNullValueACL() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + + List acls = new ArrayList(); + acls.add(null); + + // case 1 : null value in ACL list with create + try { + zk.create("/foo", "foo".getBytes(), acls, CreateMode.PERSISTENT); + fail("Expected InvalidACLException for null value in ACL List"); + } catch (InvalidACLException e) { + // Expected. Do nothing + } + + // case 2 : null value in ACL list with other create API + try { + zk.create("/foo", "foo".getBytes(), acls, CreateMode.PERSISTENT, null); + fail("Expected InvalidACLException for null value in ACL List"); + } catch (InvalidACLException e) { + // Expected. Do nothing + } + + // case 3 : null value in ACL list with setACL + try { + zk.setACL("/foo", acls, -1); + fail("Expected InvalidACLException for null value in ACL List"); + } catch (InvalidACLException e) { + // Expected. Do nothing + } + } finally { + zk.close(); + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncHammerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncHammerTest.java new file mode 100644 index 0000000..0112ba2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncHammerTest.java @@ -0,0 +1,246 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.apache.zookeeper.test.ClientBase.verifyThreadTerminated; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.LinkedList; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncHammerTest extends ZKTestCase implements StringCallback, VoidCallback, DataCallback { + + private static final Logger LOG = LoggerFactory.getLogger(AsyncHammerTest.class); + + private QuorumBase qb = new QuorumBase(); + + private volatile boolean bang; + + public void setUp(boolean withObservers) throws Exception { + qb.setUp(withObservers); + } + + protected void restart() throws Exception { + LOG.info("RESTARTING {}", getTestName()); + qb.tearDown(); + + // don't call setup - we don't want to reassign ports/dirs, etc... + JMXEnv.setUp(); + qb.startServers(); + } + + public void tearDown() throws Exception { + LOG.info("Test clients shutting down"); + qb.tearDown(); + } + + /** + * Create /test- sequence nodes asynchronously, max 30 outstanding + */ + class HammerThread extends Thread implements StringCallback, VoidCallback { + + private static final int MAX_OUTSTANDING = 30; + + private TestableZooKeeper zk; + private int outstanding; + + private volatile boolean failed = false; + + public HammerThread(String name) { + super(name); + } + + public void run() { + try { + CountdownWatcher watcher = new CountdownWatcher(); + zk = new TestableZooKeeper(qb.hostPort, CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + while (bang) { + incOutstanding(); // before create otw race + zk.create("/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL, this, null); + } + } catch (InterruptedException e) { + if (bang) { + LOG.error("sanity check failed!!!"); // sanity check + return; + } + } catch (Exception e) { + LOG.error("Client create operation failed", e); + return; + } finally { + if (zk != null) { + try { + if (!zk.close(CONNECTION_TIMEOUT)) { + failed = true; + LOG.error("Client did not shutdown"); + } + } catch (InterruptedException e) { + LOG.info("Interrupted", e); + } + } + } + } + + private synchronized void incOutstanding() throws InterruptedException { + outstanding++; + while (outstanding > MAX_OUTSTANDING) { + wait(); + } + } + + private synchronized void decOutstanding() { + outstanding--; + assertTrue("outstanding >= 0", outstanding >= 0); + notifyAll(); + } + + public void process(WatchedEvent event) { + // ignore for purposes of this test + } + + public void processResult(int rc, String path, Object ctx, String name) { + if (rc != KeeperException.Code.OK.intValue()) { + if (bang) { + failed = true; + LOG.error( + "Create failed for 0x{} with rc:{} path:{}", + Long.toHexString(zk.getSessionId()), + rc, + path); + } + decOutstanding(); + return; + } + try { + decOutstanding(); + zk.delete(name, -1, this, null); + } catch (Exception e) { + if (bang) { + failed = true; + LOG.error("Client delete failed", e); + } + } + } + + public void processResult(int rc, String path, Object ctx) { + if (rc != KeeperException.Code.OK.intValue()) { + if (bang) { + failed = true; + LOG.error( + "Delete failed for 0x{} with rc:{} path:{}", + Long.toHexString(zk.getSessionId()), + rc, + path); + } + } + } + + } + + @Test + public void testHammer() throws Exception { + setUp(false); + bang = true; + LOG.info("Starting hammers"); + HammerThread[] hammers = new HammerThread[100]; + for (int i = 0; i < hammers.length; i++) { + hammers[i] = new HammerThread("HammerThread-" + i); + hammers[i].start(); + } + LOG.info("Started hammers"); + Thread.sleep(5000); // allow the clients to run for max 5sec + bang = false; + LOG.info("Stopping hammers"); + for (int i = 0; i < hammers.length; i++) { + hammers[i].interrupt(); + verifyThreadTerminated(hammers[i], 60000); + assertFalse(hammers[i].failed); + } + + // before restart + LOG.info("Hammers stopped, verifying consistency"); + qb.verifyRootOfAllServersMatch(qb.hostPort); + + restart(); + + // after restart + LOG.info("Verifying hammers 2"); + qb.verifyRootOfAllServersMatch(qb.hostPort); + tearDown(); + } + + @Test + public void testObserversHammer() throws Exception { + setUp(true); + bang = true; + Thread[] hammers = new Thread[100]; + for (int i = 0; i < hammers.length; i++) { + hammers[i] = new HammerThread("HammerThread-" + i); + hammers[i].start(); + } + Thread.sleep(5000); // allow the clients to run for max 5sec + bang = false; + for (int i = 0; i < hammers.length; i++) { + hammers[i].interrupt(); + verifyThreadTerminated(hammers[i], 60000); + } + // before restart + qb.verifyRootOfAllServersMatch(qb.hostPort); + tearDown(); + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, String name) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncOps.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncOps.java new file mode 100644 index 0000000..d5f2310 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncOps.java @@ -0,0 +1,842 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback.ACLCallback; +import org.apache.zookeeper.AsyncCallback.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +import org.apache.zookeeper.AsyncCallback.Create2Callback; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; + +public class AsyncOps { + + /** + * This is the base class for all of the async callback classes. It will + * verify the expected value against the actual value. + * + * Basic operation is that the subclasses will generate an "expected" value + * which is defined by the "toString" method of the subclass. This is + * passed through to the verify clause by specifying it as the ctx object + * of each async call (processResult methods get the ctx as part of + * the callback). Additionally the callback will also overwrite any + * instance fields with matching parameter arguments to the processResult + * method. The cb instance can then compare the expected to the + * actual value by again calling toString and comparing the two. + * + * The format of each expected value differs (is defined) by subclass. + * Generally the expected value starts with the result code (rc) and path + * of the node being operated on, followed by the fields specific to + * each operation type (cb subclass). For example ChildrenCB specifies + * a list of the expected children suffixed onto the rc and path. See + * the toString() method of each subclass for details of it's format. + */ + public abstract static class AsyncCB { + + protected final ZooKeeper zk; + protected long defaultTimeoutMillis = 30000; + + /** the latch is used to await the results from the server */ + CountDownLatch latch; + + Code rc = Code.OK; + String path = "/foo"; + String expected; + + public AsyncCB(ZooKeeper zk, CountDownLatch latch) { + this.zk = zk; + this.latch = latch; + } + + public void setRC(Code rc) { + this.rc = rc; + } + + public void setPath(String path) { + this.path = path; + } + + public void processResult(Code rc, String path, Object ctx) { + this.rc = rc; + this.path = path; + this.expected = (String) ctx; + latch.countDown(); + } + + /** String format is rc:path:<suffix> where <suffix> is defined by each + * subclass individually. */ + @Override + public String toString() { + return rc + ":" + path + ":"; + } + + protected void verify() { + try { + latch.await(defaultTimeoutMillis, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + fail("unexpected interrupt"); + } + // on the lookout for timeout + assertSame(0L, latch.getCount()); + + String actual = toString(); + + assertEquals(expected, actual); + } + + } + + public static class StringCB extends AsyncCB implements StringCallback { + + byte[] data = new byte[10]; + List acl = Ids.CREATOR_ALL_ACL; + CreateMode flags = CreateMode.PERSISTENT; + String name = path; + + StringCB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + StringCB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + } + + public void setPath(String path) { + super.setPath(path); + this.name = path; + } + + public String nodeName() { + return path.substring(path.lastIndexOf('/') + 1); + } + + public void processResult(int rc, String path, Object ctx, String name) { + this.name = name; + super.processResult(Code.get(rc), path, ctx); + } + + public AsyncCB create() { + zk.create(path, data, acl, flags, this, toString()); + return this; + } + + public AsyncCB createEphemeral() { + zk.create(path, data, acl, CreateMode.EPHEMERAL, this, toString()); + return this; + } + + public void verifyCreate() { + create(); + verify(); + } + + public void verifyCreateEphemeral() { + createEphemeral(); + verify(); + } + + public void verifyCreateFailure_NodeExists() { + new StringCB(zk).verifyCreate(); + + rc = Code.NODEEXISTS; + name = null; + zk.create(path, data, acl, flags, this, toString()); + verify(); + } + + public void verifyCreateFailure_NoNode() { + + rc = Code.NONODE; + name = null; + path = path + "/bar"; + zk.create(path, data, acl, flags, this, toString()); + + verify(); + } + + public void verifyCreateFailure_NoChildForEphemeral() { + new StringCB(zk).verifyCreateEphemeral(); + + rc = Code.NOCHILDRENFOREPHEMERALS; + name = null; + path = path + "/bar"; + zk.create(path, data, acl, flags, this, toString()); + + verify(); + } + + @Override + public String toString() { + return super.toString() + name; + } + + } + + public static class ACLCB extends AsyncCB implements ACLCallback { + + List acl = Ids.CREATOR_ALL_ACL; + int version = 0; + Stat stat = new Stat(); + byte[] data = "testing".getBytes(); + + ACLCB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + ACLCB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + stat.setAversion(0); + stat.setCversion(0); + stat.setEphemeralOwner(0); + stat.setVersion(0); + } + + public void processResult(int rc, String path, Object ctx, List acl, Stat stat) { + this.acl = acl; + this.stat = stat; + super.processResult(Code.get(rc), path, ctx); + } + + public void verifyGetACL() { + new StringCB(zk).verifyCreate(); + + zk.getACL(path, stat, this, toString()); + verify(); + } + + public void verifyGetACLFailure_NoNode() { + rc = Code.NONODE; + stat = null; + acl = null; + zk.getACL(path, stat, this, toString()); + + verify(); + } + + public String toString(List acls) { + if (acls == null) { + return ""; + } + + StringBuilder result = new StringBuilder(); + for (ACL acl : acls) { + result.append(acl.getPerms()).append("::"); + } + return result.toString(); + } + + @Override + public String toString() { + return super.toString() + + toString(acl) + ":" + + ":" + version + + ":" + new String(data) + + ":" + (stat == null ? "null" : stat.getAversion() + + ":" + stat.getCversion() + + ":" + stat.getEphemeralOwner() + + ":" + stat.getVersion()); + } + + } + + public static class ChildrenCB extends AsyncCB implements ChildrenCallback { + + List children = new ArrayList(); + + ChildrenCB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + ChildrenCB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + } + + public void processResult(int rc, String path, Object ctx, List children) { + this.children = (children == null ? new ArrayList() : children); + Collections.sort(this.children); + super.processResult(Code.get(rc), path, ctx); + } + + public StringCB createNode() { + StringCB parent = new StringCB(zk); + parent.verifyCreate(); + + return parent; + } + + public StringCB createNode(StringCB parent) { + String childName = "bar"; + + return createNode(parent, childName); + } + + public StringCB createNode(StringCB parent, String childName) { + StringCB child = new StringCB(zk); + child.setPath(parent.path + "/" + childName); + child.verifyCreate(); + + return child; + } + + public void verifyGetChildrenEmpty() { + StringCB parent = createNode(); + path = parent.path; + verify(); + } + + public void verifyGetChildrenSingle() { + StringCB parent = createNode(); + StringCB child = createNode(parent); + + path = parent.path; + children.add(child.nodeName()); + + verify(); + } + + public void verifyGetChildrenTwo() { + StringCB parent = createNode(); + StringCB child1 = createNode(parent, "child1"); + StringCB child2 = createNode(parent, "child2"); + + path = parent.path; + children.add(child1.nodeName()); + children.add(child2.nodeName()); + + verify(); + } + + public void verifyGetChildrenFailure_NoNode() { + rc = KeeperException.Code.NONODE; + verify(); + } + + @Override + public void verify() { + zk.getChildren(path, false, this, toString()); + super.verify(); + } + + @Override + public String toString() { + return super.toString() + children.toString(); + } + + } + + public static class Children2CB extends AsyncCB implements Children2Callback { + + List children = new ArrayList(); + + Children2CB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + Children2CB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + } + + public void processResult(int rc, String path, Object ctx, List children, Stat stat) { + this.children = (children == null ? new ArrayList() : children); + Collections.sort(this.children); + super.processResult(Code.get(rc), path, ctx); + } + + public StringCB createNode() { + StringCB parent = new StringCB(zk); + parent.verifyCreate(); + + return parent; + } + + public StringCB createNode(StringCB parent) { + String childName = "bar"; + + return createNode(parent, childName); + } + + public StringCB createNode(StringCB parent, String childName) { + StringCB child = new StringCB(zk); + child.setPath(parent.path + "/" + childName); + child.verifyCreate(); + + return child; + } + + public void verifyGetChildrenEmpty() { + StringCB parent = createNode(); + path = parent.path; + verify(); + } + + public void verifyGetChildrenSingle() { + StringCB parent = createNode(); + StringCB child = createNode(parent); + + path = parent.path; + children.add(child.nodeName()); + + verify(); + } + + public void verifyGetChildrenTwo() { + StringCB parent = createNode(); + StringCB child1 = createNode(parent, "child1"); + StringCB child2 = createNode(parent, "child2"); + + path = parent.path; + children.add(child1.nodeName()); + children.add(child2.nodeName()); + + verify(); + } + + public void verifyGetChildrenFailure_NoNode() { + rc = KeeperException.Code.NONODE; + verify(); + } + + @Override + public void verify() { + zk.getChildren(path, false, this, toString()); + super.verify(); + } + + @Override + public String toString() { + return super.toString() + children.toString(); + } + + } + + public static class Create2CB extends AsyncCB implements Create2Callback { + + byte[] data = new byte[10]; + List acl = Ids.CREATOR_ALL_ACL; + CreateMode flags = CreateMode.PERSISTENT; + String name = path; + Stat stat = new Stat(); + + Create2CB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + Create2CB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + } + + public void setPath(String path) { + super.setPath(path); + this.name = path; + } + + public String nodeName() { + return path.substring(path.lastIndexOf('/') + 1); + } + + public void processResult(int rc, String path, Object ctx, String name, Stat stat) { + this.name = name; + this.stat = stat; + super.processResult(Code.get(rc), path, ctx); + } + + public AsyncCB create() { + zk.create(path, data, acl, flags, this, toString()); + return this; + } + + public void verifyCreate() { + create(); + verify(); + } + + public void verifyCreateFailure_NodeExists() { + new Create2CB(zk).verifyCreate(); + rc = Code.NODEEXISTS; + name = null; + stat = null; + zk.create(path, data, acl, flags, this, toString()); + verify(); + } + + public void verifyCreateFailure_NoNode() { + rc = Code.NONODE; + name = null; + stat = null; + path = path + "/bar"; + zk.create(path, data, acl, flags, this, toString()); + + verify(); + } + + public void verifyCreateFailure_NoChildForEphemeral() { + new StringCB(zk).verifyCreateEphemeral(); + + rc = Code.NOCHILDRENFOREPHEMERALS; + name = null; + stat = null; + path = path + "/bar"; + zk.create(path, data, acl, flags, this, toString()); + + verify(); + } + + @Override + public String toString() { + return super.toString() + + name + ":" + + (stat == null + ? "null" + : stat.getAversion() + + ":" + stat.getCversion() + + ":" + stat.getEphemeralOwner() + + ":" + stat.getVersion()); + } + + } + + public static class DataCB extends AsyncCB implements DataCallback { + + byte[] data = new byte[10]; + Stat stat = new Stat(); + + DataCB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + DataCB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + stat.setAversion(0); + stat.setCversion(0); + stat.setEphemeralOwner(0); + stat.setVersion(0); + } + + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + this.data = data; + this.stat = stat; + super.processResult(Code.get(rc), path, ctx); + } + + public void verifyGetData() { + new StringCB(zk).verifyCreate(); + + zk.getData(path, false, this, toString()); + verify(); + } + + public void verifyGetDataFailure_NoNode() { + rc = KeeperException.Code.NONODE; + data = null; + stat = null; + zk.getData(path, false, this, toString()); + verify(); + } + + @Override + public String toString() { + return super.toString() + + ":" + (data == null ? "null" : new String(data)) + + ":" + (stat == null ? "null" : stat.getAversion() + + ":" + stat.getCversion() + + ":" + stat.getEphemeralOwner() + + ":" + stat.getVersion()); + } + + } + + public static class StatCB extends AsyncCB implements StatCallback { + + List acl = Ids.CREATOR_ALL_ACL; + int version = 0; + Stat stat = new Stat(); + byte[] data = "testing".getBytes(); + + StatCB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + StatCB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + stat.setAversion(0); + stat.setCversion(0); + stat.setEphemeralOwner(0); + stat.setVersion(0); + } + + public void processResult(int rc, String path, Object ctx, Stat stat) { + this.stat = stat; + super.processResult(Code.get(rc), path, ctx); + } + + public void verifySetACL() { + stat.setAversion(1); + new StringCB(zk).verifyCreate(); + + zk.setACL(path, acl, version, this, toString()); + verify(); + } + + public void verifySetACLFailure_NoNode() { + rc = KeeperException.Code.NONODE; + stat = null; + zk.setACL(path, acl, version, this, toString()); + verify(); + } + + public void verifySetACLFailure_BadVersion() { + new StringCB(zk).verifyCreate(); + + rc = Code.BADVERSION; + stat = null; + zk.setACL(path, acl, version + 1, this, toString()); + + verify(); + } + + public void setData() { + zk.setData(path, data, version, this, toString()); + } + + public void verifySetData() { + stat.setVersion(1); + new StringCB(zk).verifyCreate(); + + setData(); + verify(); + } + + public void verifySetDataFailure_NoNode() { + rc = KeeperException.Code.NONODE; + stat = null; + zk.setData(path, data, version, this, toString()); + verify(); + } + + public void verifySetDataFailure_BadVersion() { + new StringCB(zk).verifyCreate(); + + rc = Code.BADVERSION; + stat = null; + zk.setData(path, data, version + 1, this, toString()); + + verify(); + } + + public void verifyExists() { + new StringCB(zk).verifyCreate(); + + zk.exists(path, false, this, toString()); + verify(); + } + + public void verifyExistsFailure_NoNode() { + rc = KeeperException.Code.NONODE; + stat = null; + zk.exists(path, false, this, toString()); + verify(); + } + + @Override + public String toString() { + return super.toString() + version + + ":" + new String(data) + + ":" + (stat == null ? "null" : stat.getAversion() + + ":" + stat.getCversion() + + ":" + stat.getEphemeralOwner() + + ":" + stat.getVersion()); + } + + } + + public static class VoidCB extends AsyncCB implements VoidCallback { + + int version = 0; + + VoidCB(ZooKeeper zk) { + this(zk, new CountDownLatch(1)); + } + + VoidCB(ZooKeeper zk, CountDownLatch latch) { + super(zk, latch); + } + + public void processResult(int rc, String path, Object ctx) { + super.processResult(Code.get(rc), path, ctx); + } + + public void delete() { + zk.delete(path, version, this, toString()); + } + + public void verifyDelete() { + new StringCB(zk).verifyCreate(); + + delete(); + verify(); + } + + public void verifyDeleteFailure_NoNode() { + rc = Code.NONODE; + zk.delete(path, version, this, toString()); + verify(); + } + + public void verifyDeleteFailure_BadVersion() { + new StringCB(zk).verifyCreate(); + rc = Code.BADVERSION; + zk.delete(path, version + 1, this, toString()); + verify(); + } + + public void verifyDeleteFailure_NotEmpty() { + StringCB scb = new StringCB(zk); + scb.create(); + scb.setPath(path + "/bar"); + scb.create(); + + rc = Code.NOTEMPTY; + zk.delete(path, version, this, toString()); + verify(); + } + + public void sync() { + zk.sync(path, this, toString()); + } + + public void verifySync() { + sync(); + verify(); + } + + @Override + public String toString() { + return super.toString() + version; + } + + } + + public static class MultiCB implements MultiCallback { + + ZooKeeper zk; + int rc; + List opResults; + final CountDownLatch latch = new CountDownLatch(1); + + MultiCB(ZooKeeper zk) { + this.zk = zk; + } + + public void processResult(int rc, String path, Object ctx, List opResults) { + this.rc = rc; + this.opResults = opResults; + latch.countDown(); + } + + void latch_await() { + try { + latch.await(10000, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + fail("unexpected interrupt"); + } + assertSame(0L, latch.getCount()); + } + + public void verifyMulti() { + List ops = Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/multi", -1)); + zk.multi(ops, this, null); + latch_await(); + + assertEquals(this.rc, KeeperException.Code.OK.intValue()); + assertTrue(this.opResults.get(0) instanceof OpResult.CreateResult); + assertTrue(this.opResults.get(1) instanceof OpResult.DeleteResult); + } + + public void verifyMultiFailure_AllErrorResult() { + List ops = Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/nonexist1", -1), Op.setData("/multi", "test".getBytes(), -1)); + zk.multi(ops, this, null); + latch_await(); + + assertTrue(this.opResults.get(0) instanceof OpResult.ErrorResult); + assertTrue(this.opResults.get(1) instanceof OpResult.ErrorResult); + assertTrue(this.opResults.get(2) instanceof OpResult.ErrorResult); + } + + public void verifyMultiFailure_NoSideEffect() throws KeeperException, InterruptedException { + List ops = Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/nonexist1", -1)); + zk.multi(ops, this, null); + latch_await(); + + assertTrue(this.opResults.get(0) instanceof OpResult.ErrorResult); + assertNull(zk.exists("/multi", false)); + } + + public void verifyMultiSequential_NoSideEffect() throws Exception { + StringCB scb = new StringCB(zk); + scb.verifyCreate(); + String path = scb.path + "-"; + String seqPath = path + "0000000002"; + + zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + assertNotNull(zk.exists(path + "0000000001", false)); + + List ops = Arrays.asList( + Op.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL), + Op.delete("/nonexist", -1)); + zk.multi(ops, this, null); + latch_await(); + + assertNull(zk.exists(seqPath, false)); + zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + assertNotNull(zk.exists(seqPath, false)); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncOpsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncOpsTest.java new file mode 100644 index 0000000..9a9fe35 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncOpsTest.java @@ -0,0 +1,270 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.AsyncOps.ACLCB; +import org.apache.zookeeper.test.AsyncOps.Children2CB; +import org.apache.zookeeper.test.AsyncOps.ChildrenCB; +import org.apache.zookeeper.test.AsyncOps.Create2CB; +import org.apache.zookeeper.test.AsyncOps.DataCB; +import org.apache.zookeeper.test.AsyncOps.MultiCB; +import org.apache.zookeeper.test.AsyncOps.StatCB; +import org.apache.zookeeper.test.AsyncOps.StringCB; +import org.apache.zookeeper.test.AsyncOps.VoidCB; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncOpsTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(AsyncOpsTest.class); + + private ZooKeeper zk; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + + LOG.info("Creating client {}", getTestName()); + + zk = createClient(); + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + } + + @After + @Override + public void tearDown() throws Exception { + zk.close(); + + super.tearDown(); + + LOG.info("Test clients shutting down"); + } + + @Test + public void testAsyncCreate() { + new StringCB(zk).verifyCreate(); + } + + @Test + public void testAsyncCreate2() { + new Create2CB(zk).verifyCreate(); + } + + @Test + public void testAsyncCreateThree() { + CountDownLatch latch = new CountDownLatch(3); + + StringCB op1 = new StringCB(zk, latch); + op1.setPath("/op1"); + StringCB op2 = new StringCB(zk, latch); + op2.setPath("/op2"); + StringCB op3 = new StringCB(zk, latch); + op3.setPath("/op3"); + + op1.create(); + op2.create(); + op3.create(); + + op1.verify(); + op2.verify(); + op3.verify(); + } + + @Test + public void testAsyncCreateFailure_NodeExists() { + new StringCB(zk).verifyCreateFailure_NodeExists(); + } + + @Test + public void testAsyncCreateFailure_NoNode() { + new StringCB(zk).verifyCreateFailure_NoNode(); + } + + @Test + public void testAsyncCreateFailure_NoChildForEphemeral() { + new StringCB(zk).verifyCreateFailure_NoChildForEphemeral(); + } + + @Test + public void testAsyncCreate2Failure_NodeExists() { + new Create2CB(zk).verifyCreateFailure_NodeExists(); + } + + @Test + public void testAsyncCreate2Failure_NoNode() { + new Create2CB(zk).verifyCreateFailure_NoNode(); + } + + @Test + public void testAsyncCreate2Failure_NoChildForEphemeral() { + new Create2CB(zk).verifyCreateFailure_NoChildForEphemeral(); + } + + @Test + public void testAsyncDelete() { + new VoidCB(zk).verifyDelete(); + } + + @Test + public void testAsyncDeleteFailure_NoNode() { + new VoidCB(zk).verifyDeleteFailure_NoNode(); + } + + @Test + public void testAsyncDeleteFailure_BadVersion() { + new VoidCB(zk).verifyDeleteFailure_BadVersion(); + } + + @Test + public void testAsyncDeleteFailure_NotEmpty() { + new VoidCB(zk).verifyDeleteFailure_NotEmpty(); + } + + @Test + public void testAsyncSync() { + new VoidCB(zk).verifySync(); + } + + @Test + public void testAsyncSetACL() { + new StatCB(zk).verifySetACL(); + } + + @Test + public void testAsyncSetACLFailure_NoNode() { + new StatCB(zk).verifySetACLFailure_NoNode(); + } + + @Test + public void testAsyncSetACLFailure_BadVersion() { + new StatCB(zk).verifySetACLFailure_BadVersion(); + } + + @Test + public void testAsyncSetData() { + new StatCB(zk).verifySetData(); + } + + @Test + public void testAsyncSetDataFailure_NoNode() { + new StatCB(zk).verifySetDataFailure_NoNode(); + } + + @Test + public void testAsyncSetDataFailure_BadVersion() { + new StatCB(zk).verifySetDataFailure_BadVersion(); + } + + @Test + public void testAsyncExists() { + new StatCB(zk).verifyExists(); + } + + @Test + public void testAsyncExistsFailure_NoNode() { + new StatCB(zk).verifyExistsFailure_NoNode(); + } + + @Test + public void testAsyncGetACL() { + new ACLCB(zk).verifyGetACL(); + } + + @Test + public void testAsyncGetACLFailure_NoNode() { + new ACLCB(zk).verifyGetACLFailure_NoNode(); + } + + @Test + public void testAsyncGetChildrenEmpty() { + new ChildrenCB(zk).verifyGetChildrenEmpty(); + } + + @Test + public void testAsyncGetChildrenSingle() { + new ChildrenCB(zk).verifyGetChildrenSingle(); + } + + @Test + public void testAsyncGetChildrenTwo() { + new ChildrenCB(zk).verifyGetChildrenTwo(); + } + + @Test + public void testAsyncGetChildrenFailure_NoNode() { + new ChildrenCB(zk).verifyGetChildrenFailure_NoNode(); + } + + @Test + public void testAsyncGetChildren2Empty() { + new Children2CB(zk).verifyGetChildrenEmpty(); + } + + @Test + public void testAsyncGetChildren2Single() { + new Children2CB(zk).verifyGetChildrenSingle(); + } + + @Test + public void testAsyncGetChildren2Two() { + new Children2CB(zk).verifyGetChildrenTwo(); + } + + @Test + public void testAsyncGetChildren2Failure_NoNode() { + new Children2CB(zk).verifyGetChildrenFailure_NoNode(); + } + + @Test + public void testAsyncGetData() { + new DataCB(zk).verifyGetData(); + } + + @Test + public void testAsyncGetDataFailure_NoNode() { + new DataCB(zk).verifyGetDataFailure_NoNode(); + } + + @Test + public void testAsyncMulti() { + new MultiCB(zk).verifyMulti(); + } + + @Test + public void testAsyncMultiFailure_AllErrorResult() { + new MultiCB(zk).verifyMultiFailure_AllErrorResult(); + } + + @Test + public void testAsyncMultiFailure_NoSideEffect() throws Exception { + new MultiCB(zk).verifyMultiFailure_NoSideEffect(); + } + + @Test + public void testAsyncMultiSequential_NoSideEffect() throws Exception { + new MultiCB(zk).verifyMultiSequential_NoSideEffect(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncTest.java new file mode 100644 index 0000000..26db270 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AsyncTest.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.util.LinkedList; +import java.util.List; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AsyncTest extends ZKTestCase implements StringCallback, VoidCallback, DataCallback { + + private static final Logger LOG = LoggerFactory.getLogger(AsyncTest.class); + + private QuorumBase qb = new QuorumBase(); + + @Before + public void setUp() throws Exception { + qb.setUp(); + } + + @After + public void tearDown() throws Exception { + LOG.info("Test clients shutting down"); + qb.tearDown(); + } + + private ZooKeeper createClient() throws Exception { + return createClient(qb.hostPort); + } + + private ZooKeeper createClient(String hp) throws Exception { + ZooKeeper zk = ClientBase.createZKClient(hp); + return zk; + } + + List results = new LinkedList(); + + @Test + public void testAsync() throws Exception { + ZooKeeper zk = null; + zk = createClient(); + try { + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + zk.create("/ben", new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT, this, results); + zk.create("/ben/2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results); + zk.delete("/ben", -1, this, results); + zk.create("/ben2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results); + zk.getData("/ben2", false, this, results); + synchronized (results) { + while (results.size() < 5) { + results.wait(); + } + } + assertEquals(0, (int) results.get(0)); + assertEquals(Code.NOAUTH, Code.get(results.get(1))); + assertEquals(0, (int) results.get(2)); + assertEquals(0, (int) results.get(3)); + assertEquals(0, (int) results.get(4)); + } finally { + zk.close(); + } + + zk = createClient(); + try { + zk.addAuthInfo("digest", "ben:passwd2".getBytes()); + try { + zk.getData("/ben2", false, new Stat()); + fail("Should have received a permission error"); + } catch (KeeperException e) { + assertEquals(Code.NOAUTH, e.code()); + } + } finally { + zk.close(); + } + + zk = createClient(); + try { + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + zk.getData("/ben2", false, new Stat()); + } finally { + zk.close(); + } + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, String name) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + + @SuppressWarnings("unchecked") + public void processResult( + int rc, String path, Object ctx, byte[] data, Stat stat) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AtomicFileOutputStreamTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AtomicFileOutputStreamTest.java new file mode 100644 index 0000000..a91bf35 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AtomicFileOutputStreamTest.java @@ -0,0 +1,200 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.AtomicFileOutputStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class AtomicFileOutputStreamTest extends ZKTestCase { + + private static final String TEST_STRING = "hello world"; + private static final String TEST_STRING_2 = "goodbye world"; + + private File testDir; + private File dstFile; + + @Before + public void setupTestDir() throws IOException { + testDir = ClientBase.createEmptyTestDir(); + dstFile = new File(testDir, "test.txt"); + } + @After + public void cleanupTestDir() throws IOException { + ClientBase.recursiveDelete(testDir); + } + + /** + * Test case where there is no existing file + */ + @Test + public void testWriteNewFile() throws IOException { + OutputStream fos = new AtomicFileOutputStream(dstFile); + assertFalse(dstFile.exists()); + fos.write(TEST_STRING.getBytes()); + fos.flush(); + assertFalse(dstFile.exists()); + fos.close(); + assertTrue(dstFile.exists()); + + String readBackData = ClientBase.readFile(dstFile); + assertEquals(TEST_STRING, readBackData); + } + + /** + * Test case where there is no existing file + */ + @Test + public void testOverwriteFile() throws IOException { + assertTrue("Creating empty dst file", dstFile.createNewFile()); + + OutputStream fos = new AtomicFileOutputStream(dstFile); + + assertTrue("Empty file still exists", dstFile.exists()); + fos.write(TEST_STRING.getBytes()); + fos.flush(); + + // Original contents still in place + assertEquals("", ClientBase.readFile(dstFile)); + + fos.close(); + + // New contents replace original file + String readBackData = ClientBase.readFile(dstFile); + assertEquals(TEST_STRING, readBackData); + } + + /** + * Test case where the flush() fails at close time - make sure that we clean + * up after ourselves and don't touch any existing file at the destination + */ + @Test + public void testFailToFlush() throws IOException { + // Create a file at destination + FileOutputStream fos = new FileOutputStream(dstFile); + fos.write(TEST_STRING_2.getBytes()); + fos.close(); + + OutputStream failingStream = createFailingStream(); + failingStream.write(TEST_STRING.getBytes()); + try { + failingStream.close(); + fail("Close didn't throw exception"); + } catch (IOException ioe) { + // expected + } + + // Should not have touched original file + assertEquals(TEST_STRING_2, ClientBase.readFile(dstFile)); + + assertEquals("Temporary file should have been cleaned up", dstFile.getName(), ClientBase.join(",", testDir.list())); + } + + /** + * Create a stream that fails to flush at close time + */ + private OutputStream createFailingStream() throws FileNotFoundException { + return new AtomicFileOutputStream(dstFile) { + @Override + public void flush() throws IOException { + throw new IOException("injected failure"); + } + }; + } + + /** + * Ensure the tmp file is cleaned up and dstFile is not created when + * aborting a new file. + */ + @Test + public void testAbortNewFile() throws IOException { + AtomicFileOutputStream fos = new AtomicFileOutputStream(dstFile); + + fos.abort(); + + assertEquals(0, testDir.list().length); + } + + /** + * Ensure the tmp file is cleaned up and dstFile is not created when + * aborting a new file. + */ + @Test + public void testAbortNewFileAfterFlush() throws IOException { + AtomicFileOutputStream fos = new AtomicFileOutputStream(dstFile); + fos.write(TEST_STRING.getBytes()); + fos.flush(); + + fos.abort(); + + assertEquals(0, testDir.list().length); + } + + /** + * Ensure the tmp file is cleaned up and dstFile is untouched when + * aborting an existing file overwrite. + */ + @Test + public void testAbortExistingFile() throws IOException { + FileOutputStream fos1 = new FileOutputStream(dstFile); + fos1.write(TEST_STRING.getBytes()); + fos1.close(); + + AtomicFileOutputStream fos2 = new AtomicFileOutputStream(dstFile); + + fos2.abort(); + + // Should not have touched original file + assertEquals(TEST_STRING, ClientBase.readFile(dstFile)); + assertEquals(1, testDir.list().length); + } + + /** + * Ensure the tmp file is cleaned up and dstFile is untouched when + * aborting an existing file overwrite. + */ + @Test + public void testAbortExistingFileAfterFlush() throws IOException { + FileOutputStream fos1 = new FileOutputStream(dstFile); + fos1.write(TEST_STRING.getBytes()); + fos1.close(); + + AtomicFileOutputStream fos2 = new AtomicFileOutputStream(dstFile); + fos2.write(TEST_STRING_2.getBytes()); + fos2.flush(); + + fos2.abort(); + + // Should not have touched original file + assertEquals(TEST_STRING, ClientBase.readFile(dstFile)); + assertEquals(1, testDir.list().length); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthFailX509AuthenticationProvider.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthFailX509AuthenticationProvider.java new file mode 100644 index 0000000..32121e6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthFailX509AuthenticationProvider.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import javax.net.ssl.X509KeyManager; +import javax.net.ssl.X509TrustManager; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.common.X509Exception; +import org.apache.zookeeper.server.ServerCnxn; +import org.apache.zookeeper.server.auth.X509AuthenticationProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AuthFailX509AuthenticationProvider extends X509AuthenticationProvider { + private static final Logger LOG = LoggerFactory.getLogger(AuthFailX509AuthenticationProvider.class); + + public AuthFailX509AuthenticationProvider() throws X509Exception { + super(); + } + + public AuthFailX509AuthenticationProvider(X509TrustManager trustManager, X509KeyManager keyManager) { + super(trustManager, keyManager); + } + + @Override + public KeeperException.Code handleAuthentication(ServerCnxn cnxn, byte[] authData) { + LOG.info("Authentication failed"); + return KeeperException.Code.AUTHFAILED; + } + + @Override + public String getScheme() { + return "authfail"; + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthTest.java new file mode 100644 index 0000000..c78f620 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/AuthTest.java @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Test; + +public class AuthTest extends ClientBase { + + static { + // password is test + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="); + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.test.InvalidAuthProvider"); + } + + private final CountDownLatch authFailed = new CountDownLatch(1); + + @Override + protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { + MyWatcher watcher = new MyWatcher(); + return createClient(watcher, hp); + } + + private class MyWatcher extends CountdownWatcher { + + @Override + public synchronized void process(WatchedEvent event) { + if (event.getState() == KeeperState.AuthFailed) { + authFailed.countDown(); + } else { + super.process(event); + } + } + + } + + @Test + public void testBadAuthNotifiesWatch() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.addAuthInfo("FOO", "BAR".getBytes()); + zk.getData("/path1", false, null); + fail("Should get auth state error"); + } catch (KeeperException.AuthFailedException e) { + if (!authFailed.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("Should have called my watcher"); + } + } finally { + zk.close(); + } + } + + @Test + public void testBadAuthThenSendOtherCommands() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.addAuthInfo("INVALID", "BAR".getBytes()); + zk.exists("/foobar", false); + zk.getData("/path1", false, null); + fail("Should get auth state error"); + } catch (KeeperException.AuthFailedException e) { + if (!authFailed.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("Should have called my watcher"); + } + } finally { + zk.close(); + } + } + + @Test + public void testSuper() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.addAuthInfo("digest", "pat:pass".getBytes()); + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.close(); + // verify no auth + zk = createClient(); + try { + zk.getData("/path1", false, null); + fail("auth verification"); + } catch (KeeperException.NoAuthException e) { + // expected + } + zk.close(); + // verify bad pass fails + zk = createClient(); + zk.addAuthInfo("digest", "pat:pass2".getBytes()); + try { + zk.getData("/path1", false, null); + fail("auth verification"); + } catch (KeeperException.NoAuthException e) { + // expected + } + zk.close(); + // verify super with bad pass fails + zk = createClient(); + zk.addAuthInfo("digest", "super:test2".getBytes()); + try { + zk.getData("/path1", false, null); + fail("auth verification"); + } catch (KeeperException.NoAuthException e) { + // expected + } + zk.close(); + // verify super with correct pass success + zk = createClient(); + zk.addAuthInfo("digest", "super:test".getBytes()); + zk.getData("/path1", false, null); + } finally { + zk.close(); + } + } + + @Test + public void testSuperACL() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.addAuthInfo("digest", "pat:pass".getBytes()); + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.close(); + // verify super can do anything and ignores ACLs + zk = createClient(); + zk.addAuthInfo("digest", "super:test".getBytes()); + zk.getData("/path1", false, null); + + zk.setACL("/path1", Ids.READ_ACL_UNSAFE, -1); + zk.create("/path1/foo", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + + zk.setACL("/path1", Ids.OPEN_ACL_UNSAFE, -1); + + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/BufferSizeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/BufferSizeTest.java new file mode 100644 index 0000000..8a4dd54 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/BufferSizeTest.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import org.apache.jute.BinaryInputArchive; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Before; +import org.junit.Test; + +public class BufferSizeTest extends ClientBase { + + public static final int TEST_MAXBUFFER = 100; + private static final File TEST_DATA = new File(System.getProperty("test.data.dir", "src/test/resources/data"), "buffersize"); + + private ZooKeeper zk; + + @Before + public void setMaxBuffer() throws IOException, InterruptedException { + System.setProperty("jute.maxbuffer", "" + TEST_MAXBUFFER); + assertEquals("Can't set jute.maxbuffer!", TEST_MAXBUFFER, BinaryInputArchive.maxBuffer); + zk = createClient(); + } + + @Test + public void testCreatesReqs() throws Exception { + testRequests(new ClientOp() { + @Override + public void execute(byte[] data) throws Exception { + zk.create("/create_test", data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } + }); + } + + @Test + public void testSetReqs() throws Exception { + final String path = "/set_test"; + zk.create(path, new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + testRequests(new ClientOp() { + @Override + public void execute(byte[] data) throws Exception { + zk.setData(path, data, -1); + } + }); + } + + /** Issues requests containing data smaller, equal, and greater than TEST_MAXBUFFER. */ + private void testRequests(ClientOp clientOp) throws Exception { + clientOp.execute(new byte[TEST_MAXBUFFER - 60]); + try { + // This should fail since the buffer size > the data size due to extra fields + clientOp.execute(new byte[TEST_MAXBUFFER]); + fail("Request exceeding jute.maxbuffer succeeded!"); + } catch (KeeperException.ConnectionLossException e) { + } + try { + clientOp.execute(new byte[TEST_MAXBUFFER + 10]); + fail("Request exceeding jute.maxbuffer succeeded!"); + } catch (KeeperException.ConnectionLossException e) { + } + } + + private interface ClientOp { + + void execute(byte[] data) throws Exception; + + } + + @Test + public void testStartup() throws Exception { + final String path = "/test_node"; + zk.create(path, new byte[TEST_MAXBUFFER - 60], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.setData(path, new byte[TEST_MAXBUFFER - 50], -1); + + stopServer(); + startServer(); + } + + @Test + public void testStartupFailureCreate() throws Exception { + // Empty snapshot and logfile containing a 5000-byte create + testStartupFailure(new File(TEST_DATA, "create"), "Server started despite create exceeding jute.maxbuffer!"); + } + + @Test + public void testStartupFailureSet() throws Exception { + // Empty snapshot and logfile containing a 1-byte create and 5000-byte set + testStartupFailure(new File(TEST_DATA, "set"), "Server started despite set exceeding jute.maxbuffer!"); + } + + @Test + public void testStartupFailureSnapshot() throws Exception { + // Snapshot containing 5000-byte znode and logfile containing create txn + testStartupFailure(new File(TEST_DATA, "snapshot"), "Server started despite znode exceeding jute.maxbuffer!"); + } + + private void testStartupFailure(File testDir, String failureMsg) throws Exception { + stopServer(); + // Point server at testDir + File oldTmpDir = tmpDir; + tmpDir = testDir; + try { + startServer(); + fail(failureMsg); + } catch (IOException e) { + LOG.debug("Successfully caught IOException", e); + } finally { + tmpDir = oldTmpDir; + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootAsyncTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootAsyncTest.java new file mode 100644 index 0000000..bb421b3 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootAsyncTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ChrootAsyncTest extends AsyncOpsTest { + + private static final Logger LOG = LoggerFactory.getLogger(ChrootAsyncTest.class); + + @Override + public void setUp() throws Exception { + String hp = hostPort; + hostPort = hostPort + "/chrootasynctest"; + + super.setUp(); + + LOG.info("Creating client {}", getTestName()); + + ZooKeeper zk = createClient(hp); + try { + zk.create("/chrootasynctest", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootClientTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootClientTest.java new file mode 100644 index 0000000..70cd2c6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootClientTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ChrootClientTest extends ClientTest { + + private static final Logger LOG = LoggerFactory.getLogger(ChrootClientTest.class); + + @Override + public void setUp() throws Exception { + String hp = hostPort; + hostPort = hostPort + "/chrootclienttest"; + + System.out.println(hostPort); + super.setUp(); + + LOG.info("STARTING {}", getTestName()); + + ZooKeeper zk = createClient(hp); + try { + zk.create("/chrootclienttest", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } finally { + zk.close(); + } + } + + @Test + public void testPing() throws Exception { + // not necessary to repeat this, expensive and not chroot related + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootTest.java new file mode 100644 index 0000000..ddf2063 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ChrootTest.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Test; + +public class ChrootTest extends ClientBase { + + private static class MyWatcher implements Watcher { + + private final String path; + private String eventPath; + private CountDownLatch latch = new CountDownLatch(1); + + public MyWatcher(String path) { + this.path = path; + } + public void process(WatchedEvent event) { + System.out.println("latch:" + path + " " + event.getPath()); + this.eventPath = event.getPath(); + latch.countDown(); + } + public boolean matches() throws InterruptedException { + if (!latch.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("No watch received within timeout period " + path); + } + return path.equals(eventPath); + } + + } + + @Test + public void testChrootSynchronous() throws IOException, InterruptedException, KeeperException { + ZooKeeper zk1 = createClient(); + try { + zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } finally { + if (zk1 != null) { + zk1.close(); + } + } + ZooKeeper zk2 = createClient(hostPort + "/ch1"); + try { + assertEquals("/ch2", zk2.create("/ch2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + } finally { + if (zk2 != null) { + zk2.close(); + } + } + + zk1 = createClient(); + zk2 = createClient(hostPort + "/ch1"); + try { + // check get + MyWatcher w1 = new MyWatcher("/ch1"); + assertNotNull(zk1.exists("/ch1", w1)); + MyWatcher w2 = new MyWatcher("/ch1/ch2"); + assertNotNull(zk1.exists("/ch1/ch2", w2)); + + MyWatcher w3 = new MyWatcher("/ch2"); + assertNotNull(zk2.exists("/ch2", w3)); + + // set watches on child + MyWatcher w4 = new MyWatcher("/ch1"); + zk1.getChildren("/ch1", w4); + MyWatcher w5 = new MyWatcher("/"); + zk2.getChildren("/", w5); + + // check set + zk1.setData("/ch1", "1".getBytes(), -1); + zk2.setData("/ch2", "2".getBytes(), -1); + + // check watches + assertTrue(w1.matches()); + assertTrue(w2.matches()); + assertTrue(w3.matches()); + + // check exceptions + try { + zk2.setData("/ch3", "3".getBytes(), -1); + } catch (KeeperException.NoNodeException e) { + assertEquals("/ch3", e.getPath()); + } + + assertTrue(Arrays.equals("1".getBytes(), zk1.getData("/ch1", false, null))); + assertTrue(Arrays.equals("2".getBytes(), zk1.getData("/ch1/ch2", false, null))); + assertTrue(Arrays.equals("2".getBytes(), zk2.getData("/ch2", false, null))); + + // check delete + zk2.delete("/ch2", -1); + assertTrue(w4.matches()); + assertTrue(w5.matches()); + + zk1.delete("/ch1", -1); + assertNull(zk1.exists("/ch1", false)); + assertNull(zk1.exists("/ch1/ch2", false)); + assertNull(zk2.exists("/ch2", false)); + } finally { + if (zk1 != null) { + zk1.close(); + } + if (zk2 != null) { + zk2.close(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java new file mode 100644 index 0000000..bb3b7be --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientBase.java @@ -0,0 +1,752 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.ConnectException; +import java.net.ProtocolException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.IOUtils; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FilePadding; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.util.OSMXBean; +import org.junit.After; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class ClientBase extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(ClientBase.class); + + public static int CONNECTION_TIMEOUT = 30000; + + protected String hostPort = "127.0.0.1:" + PortAssignment.unique(); + protected int maxCnxns = 0; + protected ServerCnxnFactory serverFactory = null; + protected File tmpDir = null; + protected boolean exceptionOnFailedConnect = false; + + long initialFdCount; + + public ClientBase() { + super(); + } + + public static class CountdownWatcher implements Watcher { + + // TODO this doesn't need to be volatile! (Should probably be final) + volatile CountDownLatch clientConnected; + // Set to true when connected to a read-only server, or a read-write (quorum) server. + volatile boolean connected; + // Set to true when connected to a quorum server. + volatile boolean syncConnected; + // Set to true when connected to a quorum server in read-only mode + volatile boolean readOnlyConnected; + + public CountdownWatcher() { + reset(); + } + public synchronized void reset() { + clientConnected = new CountDownLatch(1); + connected = false; + syncConnected = false; + readOnlyConnected = false; + } + public synchronized void process(WatchedEvent event) { + KeeperState state = event.getState(); + if (state == KeeperState.SyncConnected) { + connected = true; + syncConnected = true; + readOnlyConnected = false; + } else if (state == KeeperState.ConnectedReadOnly) { + connected = true; + syncConnected = false; + readOnlyConnected = true; + } else { + connected = false; + syncConnected = false; + readOnlyConnected = false; + } + + notifyAll(); + if (connected) { + clientConnected.countDown(); + } + } + public synchronized boolean isConnected() { + return connected; + } + public synchronized void waitForConnected(long timeout) throws InterruptedException, TimeoutException { + long expire = Time.currentElapsedTime() + timeout; + long left = timeout; + while (!connected && left > 0) { + wait(left); + left = expire - Time.currentElapsedTime(); + } + if (!connected) { + throw new TimeoutException("Failed to connect to ZooKeeper server."); + + } + } + public synchronized void waitForSyncConnected(long timeout) throws InterruptedException, TimeoutException { + long expire = Time.currentElapsedTime() + timeout; + long left = timeout; + while (!syncConnected && left > 0) { + wait(left); + left = expire - Time.currentElapsedTime(); + } + if (!syncConnected) { + throw new TimeoutException("Failed to connect to read-write ZooKeeper server."); + } + } + public synchronized void waitForReadOnlyConnected(long timeout) throws InterruptedException, TimeoutException { + long expire = System.currentTimeMillis() + timeout; + long left = timeout; + while (!readOnlyConnected && left > 0) { + wait(left); + left = expire - System.currentTimeMillis(); + } + if (!readOnlyConnected) { + throw new TimeoutException("Failed to connect in read-only mode to ZooKeeper server."); + } + } + public synchronized void waitForDisconnected(long timeout) throws InterruptedException, TimeoutException { + long expire = Time.currentElapsedTime() + timeout; + long left = timeout; + while (connected && left > 0) { + wait(left); + left = expire - Time.currentElapsedTime(); + } + if (connected) { + throw new TimeoutException("Did not disconnect"); + + } + } + + } + + protected TestableZooKeeper createClient() throws IOException, InterruptedException { + return createClient(hostPort); + } + + protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createClient(watcher, hp); + } + + protected TestableZooKeeper createClient(CountdownWatcher watcher) throws IOException, InterruptedException { + return createClient(watcher, hostPort); + } + + private List allClients; + private boolean allClientsSetup = false; + + protected TestableZooKeeper createClient(CountdownWatcher watcher, String hp) throws IOException, InterruptedException { + return createClient(watcher, hp, CONNECTION_TIMEOUT); + } + + protected TestableZooKeeper createClient(CountdownWatcher watcher, String hp, int timeout) throws IOException, InterruptedException { + watcher.reset(); + TestableZooKeeper zk = new TestableZooKeeper(hp, timeout, watcher); + if (!watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS)) { + if (exceptionOnFailedConnect) { + throw new ProtocolException("Unable to connect to server"); + } + fail("Unable to connect to server"); + } + synchronized (this) { + if (!allClientsSetup) { + LOG.error("allClients never setup"); + fail("allClients never setup"); + } + if (allClients != null) { + allClients.add(zk); + JMXEnv.ensureAll(getHexSessionId(zk.getSessionId())); + } else { + // test done - close the zk, not needed + zk.close(); + } + } + + return zk; + } + + public static class HostPort { + + String host; + int port; + public HostPort(String host, int port) { + this.host = host; + this.port = port; + } + + } + public static List parseHostPortList(String hplist) { + ArrayList alist = new ArrayList(); + for (String hp : hplist.split(",")) { + int idx = hp.lastIndexOf(':'); + String host = hp.substring(0, idx); + int port; + try { + port = Integer.parseInt(hp.substring(idx + 1)); + } catch (RuntimeException e) { + throw new RuntimeException("Problem parsing " + hp + e.toString()); + } + alist.add(new HostPort(host, port)); + } + return alist; + } + + public static boolean waitForServerUp(String hp, long timeout) { + return waitForServerUp(hp, timeout, false); + } + + public static boolean waitForServerUp(String hp, long timeout, boolean secure) { + long start = Time.currentElapsedTime(); + while (true) { + try { + // if there are multiple hostports, just take the first one + HostPort hpobj = parseHostPortList(hp).get(0); + String result = send4LetterWord(hpobj.host, hpobj.port, "stat", secure); + if (result.startsWith("Zookeeper version:") && !result.contains("READ-ONLY")) { + return true; + } + } catch (ConnectException e) { + // ignore as this is expected, do not log stacktrace + LOG.info("server {} not up: {}", hp, e.toString()); + } catch (IOException e) { + // ignore as this is expected + LOG.info("server {} not up", hp, e); + } catch (SSLContextException e) { + LOG.error("server {} not up", hp, e); + } + + if (Time.currentElapsedTime() > start + timeout) { + break; + } + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } + } + return false; + } + + public static boolean waitForServerDown(String hp, long timeout) { + return waitForServerDown(hp, timeout, false); + } + + public static boolean waitForServerDown(String hp, long timeout, boolean secure) { + long start = Time.currentElapsedTime(); + while (true) { + try { + HostPort hpobj = parseHostPortList(hp).get(0); + send4LetterWord(hpobj.host, hpobj.port, "stat", secure); + } catch (IOException e) { + return true; + } catch (SSLContextException e) { + return true; + } + + if (Time.currentElapsedTime() > start + timeout) { + break; + } + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } + } + return false; + } + + /** + * Return true if any of the states is achieved + */ + public static boolean waitForServerState(QuorumPeer qp, int timeout, String... serverStates) { + long start = Time.currentElapsedTime(); + while (true) { + try { + Thread.sleep(250); + } catch (InterruptedException e) { + // ignore + } + for (String state : serverStates) { + if (qp.getServerState().equals(state)) { + return true; + } + } + if (Time.currentElapsedTime() > start + timeout) { + return false; + } + } + } + + static void verifyThreadTerminated(Thread thread, long millis) throws InterruptedException { + thread.join(millis); + if (thread.isAlive()) { + LOG.error("Thread {} : {}", thread.getName(), Arrays.toString(thread.getStackTrace())); + assertFalse("thread " + thread.getName() + " still alive after join", true); + } + } + + public static File createEmptyTestDir() throws IOException { + return createTmpDir(testBaseDir, false); + } + + public static File createTmpDir() throws IOException { + return createTmpDir(testBaseDir, true); + } + + static File createTmpDir(File parentDir, boolean createInitFile) throws IOException { + File tmpFile = File.createTempFile("test", ".junit", parentDir); + // don't delete tmpFile - this ensures we don't attempt to create + // a tmpDir with a duplicate name + File tmpDir = new File(tmpFile + ".dir"); + assertFalse(tmpDir.exists()); // never true if tmpfile does it's job + assertTrue(tmpDir.mkdirs()); + + // todo not every tmp directory needs this file + if (createInitFile) { + createInitializeFile(tmpDir); + } + + return tmpDir; + } + + public static void createInitializeFile(File dir) throws IOException { + File initFile = new File(dir, "initialize"); + if (!initFile.exists()) { + assertTrue(initFile.createNewFile()); + } + } + + private static int getPort(String hostPort) { + String[] split = hostPort.split(":"); + String portstr = split[split.length - 1]; + String[] pc = portstr.split("/"); + if (pc.length > 1) { + portstr = pc[0]; + } + return Integer.parseInt(portstr); + } + + /** + * Starting the given server instance + */ + public static void startServerInstance( + File dataDir, + ServerCnxnFactory factory, + String hostPort, + int serverId) throws IOException, InterruptedException { + final int port = getPort(hostPort); + LOG.info("STARTING server instance 127.0.0.1:{}", port); + ZooKeeperServer zks = new ZooKeeperServer(dataDir, dataDir, 3000); + zks.setCreateSessionTrackerServerId(serverId); + factory.startup(zks); + assertTrue( + "waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + port, CONNECTION_TIMEOUT, factory.isSecure())); + } + + /** + * This method instantiates a new server. Starting of the server + * instance has been moved to a separate method + * {@link ClientBase#startServerInstance(File, ServerCnxnFactory, String, int)}. + * Because any exception on starting the server would leave the server + * running and the caller would not be able to shutdown the instance. This + * may affect other test cases. + * + * @return newly created server instance + * + * @see ZOOKEEPER-1852 + * for more information. + */ + public static ServerCnxnFactory createNewServerInstance( + ServerCnxnFactory factory, + String hostPort, + int maxCnxns) throws IOException, InterruptedException { + final int port = getPort(hostPort); + LOG.info("CREATING server instance 127.0.0.1:{}", port); + if (factory == null) { + factory = ServerCnxnFactory.createFactory(port, maxCnxns); + } + return factory; + } + + static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) { + if (factory != null) { + ZKDatabase zkDb = null; + { + ZooKeeperServer zs = factory.getZooKeeperServer(); + if (zs != null) { + zkDb = zs.getZKDatabase(); + } + } + factory.shutdown(); + try { + if (zkDb != null) { + zkDb.close(); + } + } catch (IOException ie) { + LOG.warn("Error closing logs ", ie); + } + final int PORT = getPort(hostPort); + + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT, factory.isSecure())); + } + } + + /** + * Test specific setup + */ + public static void setupTestEnv() { + // during the tests we run with 100K prealloc in the logs. + // on windows systems prealloc of 64M was seen to take ~15seconds + // resulting in test failure (client timeout on first session). + // set env and directly in order to handle static init/gc issues + System.setProperty("zookeeper.preAllocSize", "100"); + FilePadding.setPreallocSize(100 * 1024); + } + + protected void setUpAll() throws Exception { + allClients = new LinkedList(); + allClientsSetup = true; + } + + @Before + public void setUp() throws Exception { + setUpWithServerId(1); + } + + protected void setUpWithServerId(int serverId) throws Exception { + /* some useful information - log the number of fds used before + * and after a test is run. Helps to verify we are freeing resources + * correctly. Unfortunately this only works on unix systems (the + * only place sun has implemented as part of the mgmt bean api. + */ + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix()) { + initialFdCount = osMbean.getOpenFileDescriptorCount(); + LOG.info("Initial fdcount is: {}", initialFdCount); + } + + setupTestEnv(); + + setupCustomizedEnv(); + + JMXEnv.setUp(); + + setUpAll(); + + tmpDir = createTmpDir(testBaseDir, true); + + startServer(serverId); + + LOG.info("Client test setup finished"); + } + + protected void startServer() throws Exception { + startServer(1); + } + + /** + * Give it a chance to set up customized env before starting the server. + */ + public void setupCustomizedEnv() { /* do nothing by default */ } + + private void startServer(int serverId) throws Exception { + LOG.info("STARTING server"); + serverFactory = createNewServerInstance(serverFactory, hostPort, maxCnxns); + startServerInstance(tmpDir, serverFactory, hostPort, serverId); + // ensure that server and data bean are registered + Set children = JMXEnv.ensureParent("InMemoryDataTree", "StandaloneServer_port"); + // Remove beans which are related to zk client sessions. Strong + // assertions cannot be done for these client sessions because + // registeration of these beans with server will happen only on their + // respective reconnection interval + verifyUnexpectedBeans(children); + } + + private void verifyUnexpectedBeans(Set children) { + if (allClients != null) { + for (ZooKeeper zkc : allClients) { + Iterator childItr = children.iterator(); + while (childItr.hasNext()) { + ObjectName clientBean = childItr.next(); + if (clientBean.toString().contains(getHexSessionId(zkc.getSessionId()))) { + LOG.info("found name:{} client bean:{}", zkc.getSessionId(), clientBean.toString()); + childItr.remove(); + } + } + } + } + for (ObjectName bean : children) { + LOG.info("unexpected:{}", bean.toString()); + } + assertEquals("Unexpected bean exists!", 0, children.size()); + } + + /** + * Returns a string representation of the given long value session id + * + * @param sessionId + * long value of session id + * @return string representation of session id + */ + protected static String getHexSessionId(long sessionId) { + return "0x" + Long.toHexString(sessionId); + } + + protected void stopServer() throws Exception { + LOG.info("STOPPING server"); + shutdownServerInstance(serverFactory, hostPort); + serverFactory = null; + // ensure no beans are leftover + JMXEnv.ensureOnly(); + } + + protected void tearDownAll() throws Exception { + synchronized (this) { + if (allClients != null) { + for (ZooKeeper zk : allClients) { + try { + if (zk != null) { + zk.close(); + } + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt", e); + } + } + } + allClients = null; + } + } + + @After + public void tearDown() throws Exception { + LOG.info("tearDown starting"); + + tearDownAll(); + + stopServer(); + + if (tmpDir != null) { + assertTrue("delete " + tmpDir.toString(), recursiveDelete(tmpDir)); + } + + // This has to be set to null when the same instance of this class is reused between test cases + serverFactory = null; + + JMXEnv.tearDown(); + + /* some useful information - log the number of fds used before + * and after a test is run. Helps to verify we are freeing resources + * correctly. Unfortunately this only works on unix systems (the + * only place sun has implemented as part of the mgmt bean api. + */ + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix()) { + long fdCount = osMbean.getOpenFileDescriptorCount(); + String message = "fdcount after test is: " + fdCount + " at start it was " + initialFdCount; + LOG.info(message); + if (fdCount > initialFdCount) { + LOG.info("sleeping for 20 secs"); + //Thread.sleep(60000); + //assertTrue(message, fdCount <= initialFdCount); + } + } + + cleanUpCustomizedEnv(); + } + + public void cleanUpCustomizedEnv() { /* do nothing by default */ } + + public static MBeanServerConnection jmxConn() throws IOException { + return JMXEnv.conn(); + } + + public static boolean recursiveDelete(File d) { + return TestUtils.deleteFileRecursively(d, true); + } + + public static void logAllStackTraces() { + StringBuilder sb = new StringBuilder(); + sb.append("Starting logAllStackTraces()\n"); + Map threads = Thread.getAllStackTraces(); + for (Entry e : threads.entrySet()) { + sb.append("Thread " + e.getKey().getName() + "\n"); + for (StackTraceElement elem : e.getValue()) { + sb.append("\tat " + elem + "\n"); + } + } + sb.append("Ending logAllStackTraces()\n"); + LOG.error(sb.toString()); + } + + /* + * Verify that all of the servers see the same number of nodes + * at the root + */ + void verifyRootOfAllServersMatch(String hostPort) throws InterruptedException, KeeperException, IOException { + String[] parts = hostPort.split(","); + + // run through till the counts no longer change on each server + // max 15 tries, with 2 second sleeps, so approx 30 seconds + int[] counts = new int[parts.length]; + int failed = 0; + for (int j = 0; j < 100; j++) { + int[] newcounts = new int[parts.length]; + int i = 0; + for (String hp : parts) { + try { + ZooKeeper zk = createClient(hp); + + try { + newcounts[i++] = zk.getChildren("/", false).size(); + } finally { + zk.close(); + } + } catch (Throwable t) { + failed++; + // if session creation fails dump the thread stack + // and try the next server + logAllStackTraces(); + } + } + + if (Arrays.equals(newcounts, counts)) { + LOG.info("Found match with array:{}", Arrays.toString(newcounts)); + counts = newcounts; + break; + } else { + counts = newcounts; + Thread.sleep(10000); + } + + // don't keep this up too long, will assert false below + if (failed > 10) { + break; + } + } + + // verify all the servers reporting same number of nodes + String logmsg = "node count not consistent{} {}"; + for (int i = 1; i < parts.length; i++) { + if (counts[i - 1] != counts[i]) { + LOG.error(logmsg, counts[i - 1], counts[i]); + } else { + LOG.info(logmsg, counts[i - 1], counts[i]); + } + } + } + + public static String readFile(File file) throws IOException { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); + try { + IOUtils.copyBytes(is, os, 1024, true); + } finally { + is.close(); + } + return os.toString(); + } + + public static String join(String separator, Object[] parts) { + StringBuilder sb = new StringBuilder(); + boolean first = true; + for (Object part : parts) { + if (!first) { + sb.append(separator); + first = false; + } + sb.append(part); + } + return sb.toString(); + } + + public static ZooKeeper createZKClient(String cxnString) throws Exception { + return createZKClient(cxnString, CONNECTION_TIMEOUT); + } + + /** + * Returns ZooKeeper client after connecting to ZooKeeper Server. Session + * timeout is {@link #CONNECTION_TIMEOUT} + * + * @param cxnString + * connection string in the form of host:port + * @param sessionTimeout + * @throws IOException + * in cases of network failure + */ + public static ZooKeeper createZKClient(String cxnString, int sessionTimeout) throws IOException { + return createZKClient(cxnString, sessionTimeout, CONNECTION_TIMEOUT); + } + + public static ZooKeeper createZKClient(String cxnString, int sessionTimeout, long connectionTimeout) throws IOException { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(cxnString, sessionTimeout, watcher); + try { + watcher.waitForConnected(connectionTimeout); + } catch (InterruptedException | TimeoutException e) { + fail("ZooKeeper client can not connect to " + cxnString); + } + return zk; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientHammerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientHammerTest.java new file mode 100644 index 0000000..14e6ba5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientHammerTest.java @@ -0,0 +1,240 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.util.Date; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.Time; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientHammerTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(ClientHammerTest.class); + + private static final long HAMMERTHREAD_LATENCY = 5; + + private abstract static class HammerThread extends Thread { + + protected final int count; + protected volatile int current = 0; + + HammerThread(String name, int count) { + super(name); + this.count = count; + } + + } + + private static class BasicHammerThread extends HammerThread { + + private final ZooKeeper zk; + private final String prefix; + + BasicHammerThread(String name, ZooKeeper zk, String prefix, int count) { + super(name, count); + this.zk = zk; + this.prefix = prefix; + } + + public void run() { + byte[] b = new byte[256]; + try { + for (; current < count; current++) { + // Simulate a bit of network latency... + Thread.sleep(HAMMERTHREAD_LATENCY); + zk.create(prefix + current, b, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } catch (Throwable t) { + LOG.error("Client create operation failed", t); + } finally { + try { + zk.close(); + } catch (InterruptedException e) { + LOG.warn("Unexpected", e); + } + } + } + + } + + private static class SuperHammerThread extends HammerThread { + + private final ClientHammerTest parent; + private final String prefix; + + SuperHammerThread(String name, ClientHammerTest parent, String prefix, int count) { + super(name, count); + this.parent = parent; + this.prefix = prefix; + } + + public void run() { + byte[] b = new byte[256]; + try { + for (; current < count; current++) { + ZooKeeper zk = parent.createClient(); + try { + zk.create(prefix + current, b, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } finally { + try { + zk.close(); + } catch (InterruptedException e) { + LOG.warn("Unexpected", e); + } + } + } + } catch (Throwable t) { + LOG.error("Client create operation failed", t); + } + } + + } + + /** + * Separate threads each creating a number of nodes. Each thread + * is using a non-shared (owned by thread) client for all node creations. + * @throws Throwable + */ + @Test + public void testHammerBasic() throws Throwable { + runHammer(10, 1000); + } + + public void runHammer(final int threadCount, final int childCount) throws Throwable { + try { + HammerThread[] threads = new HammerThread[threadCount]; + long start = Time.currentElapsedTime(); + for (int i = 0; i < threads.length; i++) { + ZooKeeper zk = createClient(); + String prefix = "/test-" + i; + zk.create(prefix, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + prefix += "/"; + HammerThread thread = new BasicHammerThread("BasicHammerThread-" + i, zk, prefix, childCount); + thread.start(); + + threads[i] = thread; + } + + verifyHammer(start, threads, childCount); + } catch (Throwable t) { + LOG.error("test failed", t); + throw t; + } + } + + /** + * Separate threads each creating a number of nodes. Each thread + * is creating a new client for each node creation. + * @throws Throwable + */ + @Test + public void testHammerSuper() throws Throwable { + try { + final int threadCount = 5; + final int childCount = 10; + + HammerThread[] threads = new HammerThread[threadCount]; + long start = Time.currentElapsedTime(); + for (int i = 0; i < threads.length; i++) { + String prefix = "/test-" + i; + { + ZooKeeper zk = createClient(); + try { + zk.create(prefix, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } finally { + zk.close(); + } + } + prefix += "/"; + HammerThread thread = new SuperHammerThread("SuperHammerThread-" + i, this, prefix, childCount); + thread.start(); + + threads[i] = thread; + } + + verifyHammer(start, threads, childCount); + } catch (Throwable t) { + LOG.error("test failed", t); + throw t; + } + } + + public void verifyHammer(long start, HammerThread[] threads, int childCount) throws IOException, InterruptedException, KeeperException { + // look for the clients to finish their create operations + LOG.info("Starting check for completed hammers"); + int workingCount = threads.length; + for (int i = 0; i < 120; i++) { + Thread.sleep(10000); + for (HammerThread h : threads) { + if (!h.isAlive() || h.current == h.count) { + workingCount--; + } + } + if (workingCount == 0) { + break; + } + workingCount = threads.length; + } + if (workingCount > 0) { + for (HammerThread h : threads) { + LOG.warn("{} never finished creation, current:{}", h.getName(), h.current); + } + } else { + LOG.info("Hammer threads completed creation operations"); + } + + for (HammerThread h : threads) { + final int safetyFactor = 3; + verifyThreadTerminated(h, (long) threads.length + * (long) childCount + * HAMMERTHREAD_LATENCY + * (long) safetyFactor); + } + LOG.info("{} Total time {}", new Date(), (Time.currentElapsedTime() - start)); + + ZooKeeper zk = createClient(); + try { + LOG.info("******************* Connected to ZooKeeper{}", new Date()); + for (int i = 0; i < threads.length; i++) { + LOG.info("Doing thread: {} {}", i, new Date()); + List children = zk.getChildren("/test-" + i, false); + assertEquals(childCount, children.size()); + children = zk.getChildren("/test-" + i, false, null); + assertEquals(childCount, children.size()); + } + for (int i = 0; i < threads.length; i++) { + List children = zk.getChildren("/test-" + i, false); + assertEquals(childCount, children.size()); + children = zk.getChildren("/test-" + i, false, null); + assertEquals(childCount, children.size()); + } + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientPortBindTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientPortBindTest.java new file mode 100644 index 0000000..2daf6f3 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientPortBindTest.java @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientPortBindTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(ClientPortBindTest.class); + + /** + * Verify that the server binds to the specified address + */ + @Test + public void testBindByAddress() throws Exception { + String bindAddress = null; + Enumeration intfs = NetworkInterface.getNetworkInterfaces(); + // if we have a loopback and it has an address use it + while (intfs.hasMoreElements()) { + NetworkInterface i = intfs.nextElement(); + try { + if (i.isLoopback()) { + Enumeration addrs = i.getInetAddresses(); + while (addrs.hasMoreElements()) { + InetAddress a = addrs.nextElement(); + if (a.isLoopbackAddress()) { + bindAddress = a.getHostAddress(); + if (a instanceof Inet6Address) { + bindAddress = "[" + bindAddress + "]"; + } + break; + } + } + } + } catch (SocketException se) { + LOG.warn("Couldn't find loopback interface", se); + } + } + if (bindAddress == null) { + LOG.warn("Unable to determine loop back address, skipping test"); + return; + } + final int PORT = PortAssignment.unique(); + + LOG.info("Using {} as the bind address", bindAddress); + final String HOSTPORT = bindAddress + ":" + PORT; + LOG.info("Using {} as the host/port", HOSTPORT); + + File tmpDir = ClientBase.createTmpDir(); + + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + + ServerCnxnFactory f = ServerCnxnFactory.createFactory(new InetSocketAddress(bindAddress, PORT), -1); + f.startup(zks); + LOG.info("starting up the the server, waiting"); + + assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + zk.close(); + } finally { + f.shutdown(); + zks.shutdown(); + + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientRetryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientRetryTest.java new file mode 100644 index 0000000..48c0182 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientRetryTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertSame; +import java.io.IOException; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.junit.Test; + +public class ClientRetryTest extends ClientBase { + + @Override + public void setUp() throws Exception { + maxCnxns = 1; + super.setUp(); + } + /* + * This is a simple test - try to connect two clients to a server + * accepting a maximum of one connection from each address. Check that + * only one is accepted. Close that connection, and check that the other + * eventually connects. + * + * There is a possibility of a false positive here, as when zk2 is tested + * for having connected it might not have been given enough time, and finish + * connecting after the test is done. Since the + * server doesn't tell the client why it hasn't connected, there's no + * obvious way to detect the difference. + */ + @Test + public void testClientRetry() throws IOException, InterruptedException, TimeoutException { + CountdownWatcher cdw1 = new CountdownWatcher(); + CountdownWatcher cdw2 = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(hostPort, 10000, cdw1); + try { + cdw1.waitForConnected(CONNECTION_TIMEOUT); + ZooKeeper zk2 = new ZooKeeper(hostPort, 10000, cdw2); + try { + States s1 = zk.getState(); + States s2 = zk2.getState(); + assertSame(s1, States.CONNECTED); + assertSame(s2, States.CONNECTING); + cdw1.reset(); + zk.close(); + cdw1.waitForDisconnected(CONNECTION_TIMEOUT); + cdw2.waitForConnected(CONNECTION_TIMEOUT); + assertSame(zk2.getState(), States.CONNECTED); + } finally { + zk2.close(); + } + } finally { + zk.close(); + } + } + +} + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java new file mode 100644 index 0000000..12838a5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSSLTest.java @@ -0,0 +1,188 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.server.NettyServerCnxnFactory; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class ClientSSLTest extends QuorumPeerTestBase { + + private ClientX509Util clientX509Util; + + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + + @Before + public void setup() { + System.setProperty(NettyServerCnxnFactory.PORT_UNIFICATION_KEY, Boolean.TRUE.toString()); + clientX509Util = new ClientX509Util(); + String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data"); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); + System.setProperty(clientX509Util.getSslKeystoreLocationProperty(), testDataPath + "/ssl/testKeyStore.jks"); + System.setProperty(clientX509Util.getSslKeystorePasswdProperty(), "testpass"); + System.setProperty(clientX509Util.getSslTruststoreLocationProperty(), testDataPath + "/ssl/testTrustStore.jks"); + System.setProperty(clientX509Util.getSslTruststorePasswdProperty(), "testpass"); + } + + @After + public void teardown() { + System.clearProperty(NettyServerCnxnFactory.PORT_UNIFICATION_KEY); + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + System.clearProperty(ZKClientConfig.SECURE_CLIENT); + System.clearProperty(clientX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(clientX509Util.getSslKeystorePasswdProperty()); + System.clearProperty(clientX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(clientX509Util.getSslTruststorePasswdProperty()); + clientX509Util.close(); + } + + /** + * This test checks that client SSL connections work in the absence of a + * secure port when port unification is set up for the plaintext port. + * + * This single client port will be tested for handling both plaintext + * and SSL traffic. + */ + @Test + public void testClientServerUnifiedPort() throws Exception { + testClientServerSSL(false); + } + + /** + * This test checks that client - server SSL works in cluster setup of ZK servers, which includes: + * 1. setting "secureClientPort" in "zoo.cfg" file. + * 2. setting jvm flags for serverCnxn, keystore, truststore. + * Finally, a zookeeper client should be able to connect to the secure port and + * communicate with server via secure connection. + *

      + * Note that in this test a ZK server has two ports -- clientPort and secureClientPort. + */ + @Test + public void testClientServerSSL() throws Exception { + testClientServerSSL(true); + } + + public void testClientServerSSL(boolean useSecurePort) throws Exception { + final int SERVER_COUNT = 3; + final int[] clientPorts = new int[SERVER_COUNT]; + final Integer[] secureClientPorts = new Integer[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + secureClientPorts[i] = PortAssignment.unique(); + String server = String.format("server.%d=127.0.0.1:%d:%d:participant;127.0.0.1:%d%n", i, PortAssignment.unique(), PortAssignment.unique(), clientPorts[i]); + sb.append(server); + } + String quorumCfg = sb.toString(); + + MainThread[] mt = new MainThread[SERVER_COUNT]; + for (int i = 0; i < SERVER_COUNT; i++) { + if (useSecurePort) { + mt[i] = new MainThread(i, quorumCfg, secureClientPorts[i], true); + } else { + mt[i] = new MainThread(i, quorumCfg, true); + } + mt[i].start(); + } + + // Add some timing margin for the quorum to elect a leader + // (without this margin, timeouts have been observed in parallel test runs) + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[0], 2 * TIMEOUT); + + // Servers have been set up. Now go test if secure connection is successful. + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], TIMEOUT)); + final int port = useSecurePort ? secureClientPorts[i] : clientPorts[i]; + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + port, TIMEOUT); + // Do a simple operation to make sure the connection is fine. + zk.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/test", -1); + zk.close(); + } + + for (int i = 0; i < mt.length; i++) { + mt[i].shutdown(); + } + } + + /** + * Developers might use standalone mode (which is the default for one server). + * This test checks SSL works in standalone mode of ZK server. + *

      + * Note that in this test the Zk server has only secureClientPort + */ + @Test + public void testSecureStandaloneServer() throws Exception { + Integer secureClientPort = PortAssignment.unique(); + MainThread mt = new MainThread(MainThread.UNSET_MYID, "", secureClientPort, false); + mt.start(); + + ZooKeeper zk = ClientBase.createZKClient("127.0.0.1:" + secureClientPort, TIMEOUT); + zk.create("/test", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/test", -1); + zk.close(); + mt.shutdown(); + } + + @Test + public void testSecureStandaloneServerAuthFail() throws IOException { + exceptionRule.expect(AssertionError.class); + exceptionRule.expectMessage("ZooKeeper client can not connect"); + try { + System.setProperty(ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX + "authfail", + "org.apache.zookeeper.test.AuthFailX509AuthenticationProvider"); + System.setProperty(clientX509Util.getSslAuthProviderProperty(), "authfail"); + + Integer secureClientPort = PortAssignment.unique(); + MainThread mt = new MainThread(MainThread.UNSET_MYID, "", secureClientPort, false); + mt.start(); + + ClientBase.createZKClient("localhost:" + secureClientPort, TIMEOUT, 3000); + fail("Client should not able to connect to this server, because auth failed"); + } finally { + System.clearProperty(ProviderRegistry.AUTHPROVIDER_PROPERTY_PREFIX + "authfail"); + System.clearProperty(clientX509Util.getSslAuthProviderProperty()); + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSkipACLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSkipACLTest.java new file mode 100644 index 0000000..d9b8cdd --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientSkipACLTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.junit.AfterClass; +import org.junit.BeforeClass; + +public class ClientSkipACLTest extends ClientTest { + + @BeforeClass + public static void setup() { + System.setProperty("zookeeper.skipACL", "yes"); + } + + @AfterClass + public static void teardown() { + System.clearProperty("zookeeper.skipACL"); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientTest.java new file mode 100644 index 0000000..43cc5b0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ClientTest.java @@ -0,0 +1,854 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.KeeperException.InvalidACLException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.Perms; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.ExistsRequest; +import org.apache.zookeeper.proto.ExistsResponse; +import org.apache.zookeeper.proto.ReplyHeader; +import org.apache.zookeeper.proto.RequestHeader; +import org.apache.zookeeper.server.PrepRequestProcessor; +import org.apache.zookeeper.server.util.OSMXBean; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClientTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(ClientTest.class); + private boolean skipACL = System.getProperty("zookeeper.skipACL", "no").equals("yes"); + + /** Verify that pings are sent, keeping the "idle" client alive */ + @Test + public void testPing() throws Exception { + ZooKeeper zkIdle = null; + ZooKeeper zkWatchCreator = null; + try { + CountdownWatcher watcher = new CountdownWatcher(); + zkIdle = createClient(watcher, hostPort, 10000); + + zkWatchCreator = createClient(); + + for (int i = 0; i < 10; i++) { + zkWatchCreator.create("/" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + for (int i = 0; i < 10; i++) { + zkIdle.exists("/" + i, true); + } + for (int i = 0; i < 10; i++) { + Thread.sleep(1000); + zkWatchCreator.delete("/" + i, -1); + } + // The bug will manifest itself here because zkIdle will expire + zkIdle.exists("/0", false); + } finally { + if (zkIdle != null) { + zkIdle.close(); + } + if (zkWatchCreator != null) { + zkWatchCreator.close(); + } + } + } + + @Test + public void testClientwithoutWatcherObj() throws IOException, InterruptedException, KeeperException { + performClientTest(false); + } + + @Test + public void testClientWithWatcherObj() throws IOException, InterruptedException, KeeperException { + performClientTest(true); + } + + /** Exercise the testable functions, verify tostring, etc... */ + @Test + public void testTestability() throws Exception { + TestableZooKeeper zk = createClient(); + try { + LOG.info("{}", zk.testableLocalSocketAddress()); + LOG.info("{}", zk.testableRemoteSocketAddress()); + LOG.info("{}", zk.toString()); + } finally { + zk.close(CONNECTION_TIMEOUT); + LOG.info("{}", zk.testableLocalSocketAddress()); + LOG.info("{}", zk.testableRemoteSocketAddress()); + LOG.info("{}", zk.toString()); + } + } + + @Test + public void testACLs() throws Exception { + ZooKeeper zk = null; + try { + zk = createClient(); + + try { + zk.create("/acltest", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Should have received an invalid acl error"); + } catch (InvalidACLException e) { + LOG.info("Test successful, invalid acl received : {}", e.getMessage()); + } + + try { + ArrayList testACL = new ArrayList(); + testACL.add(new ACL(Perms.ALL | Perms.ADMIN, Ids.AUTH_IDS)); + testACL.add(new ACL(Perms.ALL | Perms.ADMIN, new Id("ip", "127.0.0.1/8"))); + zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT); + fail("Should have received an invalid acl error"); + } catch (InvalidACLException e) { + LOG.info("Test successful, invalid acl received : {}", e.getMessage()); + } + + try { + ArrayList testACL = new ArrayList(); + testACL.add(new ACL(Perms.ALL | Perms.ADMIN, new Id())); + zk.create("/nullidtest", new byte[0], testACL, CreateMode.PERSISTENT); + fail("Should have received an invalid acl error"); + } catch (InvalidACLException e) { + LOG.info("Test successful, invalid acl received : {}", e.getMessage()); + } + + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + ArrayList testACL = new ArrayList(); + testACL.add(new ACL(Perms.ALL, new Id("auth", ""))); + testACL.add(new ACL(Perms.WRITE, new Id("ip", "127.0.0.1"))); + zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT); + zk.close(); + zk = createClient(); + zk.addAuthInfo("digest", "ben:passwd2".getBytes()); + if (skipACL) { + try { + zk.getData("/acltest", false, null); + } catch (KeeperException e) { + fail("Badauth reads should succeed with skipACL."); + } + } else { + try { + zk.getData("/acltest", false, null); + fail("Should have received a permission error"); + } catch (KeeperException e) { + assertEquals(Code.NOAUTH, e.code()); + } + } + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + zk.getData("/acltest", false, null); + zk.setACL("/acltest", Ids.OPEN_ACL_UNSAFE, -1); + zk.close(); + zk = createClient(); + zk.getData("/acltest", false, null); + List acls = zk.getACL("/acltest", new Stat()); + assertEquals(1, acls.size()); + assertEquals(Ids.OPEN_ACL_UNSAFE, acls); + + // The stat parameter should be optional. + acls = zk.getACL("/acltest", null); + assertEquals(1, acls.size()); + assertEquals(Ids.OPEN_ACL_UNSAFE, acls); + + zk.close(); + } finally { + if (zk != null) { + zk.close(); + } + } + } + + @Test + public void testNullAuthId() throws Exception { + ZooKeeper zk = null; + try { + zk = createClient(); + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + ArrayList testACL = new ArrayList(); + testACL.add(new ACL(Perms.ALL, new Id("auth", null))); + zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT); + zk.close(); + zk = createClient(); + zk.addAuthInfo("digest", "ben:passwd2".getBytes()); + if (skipACL) { + try { + zk.getData("/acltest", false, null); + } catch (KeeperException e) { + fail("Badauth reads should succeed with skipACL."); + } + } else { + try { + zk.getData("/acltest", false, null); + fail("Should have received a permission error"); + } catch (KeeperException e) { + assertEquals(Code.NOAUTH, e.code()); + } + } + zk.addAuthInfo("digest", "ben:passwd".getBytes()); + zk.getData("/acltest", false, null); + zk.setACL("/acltest", Ids.OPEN_ACL_UNSAFE, -1); + zk.close(); + zk = createClient(); + zk.getData("/acltest", false, null); + List acls = zk.getACL("/acltest", new Stat()); + assertEquals(1, acls.size()); + assertEquals(Ids.OPEN_ACL_UNSAFE, acls); + } finally { + if (zk != null) { + zk.close(); + } + } + } + + private class MyWatcher extends CountdownWatcher { + + LinkedBlockingQueue events = new LinkedBlockingQueue(); + + public void process(WatchedEvent event) { + super.process(event); + if (event.getType() != EventType.None) { + try { + events.put(event); + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt during event.put"); + } + } + } + + } + + /** + * Register multiple watchers and verify that they all get notified and + * in the right order. + */ + @Test + public void testMutipleWatcherObjs() throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = createClient(new CountdownWatcher(), hostPort); + try { + MyWatcher[] watchers = new MyWatcher[100]; + MyWatcher[] watchers2 = new MyWatcher[watchers.length]; + for (int i = 0; i < watchers.length; i++) { + watchers[i] = new MyWatcher(); + watchers2[i] = new MyWatcher(); + zk.create("/foo-" + i, ("foodata" + i).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + Stat stat = new Stat(); + + // + // test get/exists with single set of watchers + // get all, then exists all + // + for (int i = 0; i < watchers.length; i++) { + assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); + } + for (int i = 0; i < watchers.length; i++) { + assertNotNull(zk.exists("/foo-" + i, watchers[i])); + } + // trigger the watches + for (int i = 0; i < watchers.length; i++) { + zk.setData("/foo-" + i, ("foodata2-" + i).getBytes(), -1); + zk.setData("/foo-" + i, ("foodata3-" + i).getBytes(), -1); + } + for (int i = 0; i < watchers.length; i++) { + WatchedEvent event = watchers[i].events.poll(10, TimeUnit.SECONDS); + assertEquals("/foo-" + i, event.getPath()); + assertEquals(EventType.NodeDataChanged, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + + // small chance that an unexpected message was delivered + // after this check, but we would catch that next time + // we check events + assertEquals(0, watchers[i].events.size()); + } + + // + // test get/exists with single set of watchers + // get/exists together + // + for (int i = 0; i < watchers.length; i++) { + assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); + assertNotNull(zk.exists("/foo-" + i, watchers[i])); + } + // trigger the watches + for (int i = 0; i < watchers.length; i++) { + zk.setData("/foo-" + i, ("foodata4-" + i).getBytes(), -1); + zk.setData("/foo-" + i, ("foodata5-" + i).getBytes(), -1); + } + for (int i = 0; i < watchers.length; i++) { + WatchedEvent event = watchers[i].events.poll(10, TimeUnit.SECONDS); + assertEquals("/foo-" + i, event.getPath()); + assertEquals(EventType.NodeDataChanged, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + + // small chance that an unexpected message was delivered + // after this check, but we would catch that next time + // we check events + assertEquals(0, watchers[i].events.size()); + } + + // + // test get/exists with two sets of watchers + // + for (int i = 0; i < watchers.length; i++) { + assertNotNull(zk.getData("/foo-" + i, watchers[i], stat)); + assertNotNull(zk.exists("/foo-" + i, watchers2[i])); + } + // trigger the watches + for (int i = 0; i < watchers.length; i++) { + zk.setData("/foo-" + i, ("foodata6-" + i).getBytes(), -1); + zk.setData("/foo-" + i, ("foodata7-" + i).getBytes(), -1); + } + for (int i = 0; i < watchers.length; i++) { + WatchedEvent event = watchers[i].events.poll(10, TimeUnit.SECONDS); + assertEquals("/foo-" + i, event.getPath()); + assertEquals(EventType.NodeDataChanged, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + + // small chance that an unexpected message was delivered + // after this check, but we would catch that next time + // we check events + assertEquals(0, watchers[i].events.size()); + + // watchers2 + WatchedEvent event2 = watchers2[i].events.poll(10, TimeUnit.SECONDS); + assertEquals("/foo-" + i, event2.getPath()); + assertEquals(EventType.NodeDataChanged, event2.getType()); + assertEquals(KeeperState.SyncConnected, event2.getState()); + + // small chance that an unexpected message was delivered + // after this check, but we would catch that next time + // we check events + assertEquals(0, watchers2[i].events.size()); + } + + } finally { + if (zk != null) { + zk.close(); + } + } + } + + private void performClientTest(boolean withWatcherObj) throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = null; + try { + MyWatcher watcher = new MyWatcher(); + zk = createClient(watcher, hostPort); + LOG.info("Before create /benwashere"); + zk.create("/benwashere", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("After create /benwashere"); + try { + zk.setData("/benwashere", "hi".getBytes(), 57); + fail("Should have gotten BadVersion exception"); + } catch (KeeperException.BadVersionException e) { + // expected that + } catch (KeeperException e) { + fail("Should have gotten BadVersion exception"); + } + LOG.info("Before delete /benwashere"); + zk.delete("/benwashere", 0); + LOG.info("After delete /benwashere"); + zk.close(); + + Thread.sleep(2000); + + zk = createClient(watcher, hostPort); + + LOG.info("Before delete /"); + + try { + zk.delete("/", -1); + fail("deleted root!"); + } catch (KeeperException.BadArgumentsException e) { + // good, expected that + } + Stat stat = new Stat(); + // Test basic create, ls, and getData + zk.create("/pat", "Pat was here".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("Before create /ben"); + zk.create("/pat/ben", "Ben was here".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + LOG.info("Before getChildren /pat"); + List children = zk.getChildren("/pat", false); + assertEquals(1, children.size()); + assertEquals("ben", children.get(0)); + List children2 = zk.getChildren("/pat", false, null); + assertEquals(children, children2); + String value = new String(zk.getData("/pat/ben", false, stat)); + assertEquals("Ben was here", value); + // Test stat and watch of non existent node + + try { + if (withWatcherObj) { + assertEquals(null, zk.exists("/frog", watcher)); + } else { + assertEquals(null, zk.exists("/frog", true)); + } + LOG.info("Comment: asseting passed for frog setting /"); + } catch (KeeperException.NoNodeException e) { + // OK, expected that + } + zk.create("/frog", "hi".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + // the first poll is just a session delivery + LOG.info("Comment: checking for events length {}", watcher.events.size()); + WatchedEvent event = watcher.events.poll(10, TimeUnit.SECONDS); + assertEquals("/frog", event.getPath()); + assertEquals(EventType.NodeCreated, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + // Test child watch and create with sequence + zk.getChildren("/pat/ben", true); + for (int i = 0; i < 10; i++) { + zk.create("/pat/ben/" + + i + + "-", Integer.toString(i).getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } + children = zk.getChildren("/pat/ben", false); + Collections.sort(children); + assertEquals(10, children.size()); + for (int i = 0; i < 10; i++) { + final String name = children.get(i); + assertTrue("starts with -", name.startsWith(i + "-")); + byte[] b; + if (withWatcherObj) { + b = zk.getData("/pat/ben/" + name, watcher, stat); + } else { + b = zk.getData("/pat/ben/" + name, true, stat); + } + assertEquals(Integer.toString(i), new String(b)); + zk.setData("/pat/ben/" + name, "new".getBytes(), stat.getVersion()); + if (withWatcherObj) { + stat = zk.exists("/pat/ben/" + name, watcher); + } else { + stat = zk.exists("/pat/ben/" + name, true); + } + zk.delete("/pat/ben/" + name, stat.getVersion()); + } + event = watcher.events.poll(10, TimeUnit.SECONDS); + assertEquals("/pat/ben", event.getPath()); + assertEquals(EventType.NodeChildrenChanged, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + for (int i = 0; i < 10; i++) { + event = watcher.events.poll(10, TimeUnit.SECONDS); + final String name = children.get(i); + assertEquals("/pat/ben/" + name, event.getPath()); + assertEquals(EventType.NodeDataChanged, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + event = watcher.events.poll(10, TimeUnit.SECONDS); + assertEquals("/pat/ben/" + name, event.getPath()); + assertEquals(EventType.NodeDeleted, event.getType()); + assertEquals(KeeperState.SyncConnected, event.getState()); + } + zk.create("/good\u0040path", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/duplicate", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + try { + zk.create("/duplicate", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("duplicate create allowed"); + } catch (KeeperException.NodeExistsException e) { + // OK, expected that + } + } finally { + if (zk != null) { + zk.close(); + } + } + } + + // Test that sequential filenames are being created correctly, + // with 0-padding in the filename + @Test + public void testSequentialNodeNames() throws IOException, InterruptedException, KeeperException { + String path = "/SEQUENCE"; + String file = "TEST"; + String filepath = path + "/" + file; + + ZooKeeper zk = null; + try { + zk = createClient(); + zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + List children = zk.getChildren(path, false); + assertEquals(1, children.size()); + assertEquals(file + "0000000000", children.get(0)); + + zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + children = zk.getChildren(path, false); + assertEquals(2, children.size()); + assertTrue("contains child 1", children.contains(file + "0000000001")); + + zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + children = zk.getChildren(path, false); + assertEquals(3, children.size()); + assertTrue("contains child 2", children.contains(file + "0000000002")); + + // The pattern is holding so far. Let's run the counter a bit + // to be sure it continues to spit out the correct answer + for (int i = children.size(); i < 105; i++) { + zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } + + children = zk.getChildren(path, false); + assertTrue("contains child 104", children.contains(file + "0000000104")); + + } finally { + if (zk != null) { + zk.close(); + } + } + } + + // Test that data provided when + // creating sequential nodes is stored properly + @Test + public void testSequentialNodeData() throws Exception { + ZooKeeper zk = null; + String queue_handle = "/queue"; + try { + zk = createClient(); + + zk.create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create(queue_handle + "/element", "0".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + zk.create(queue_handle + "/element", "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + List children = zk.getChildren(queue_handle, true); + assertEquals(children.size(), 2); + String child1 = children.get(0); + String child2 = children.get(1); + int compareResult = child1.compareTo(child2); + assertNotSame(compareResult, 0); + if (compareResult < 0) { + } else { + String temp = child1; + child1 = child2; + child2 = temp; + } + String child1data = new String(zk.getData(queue_handle + "/" + child1, false, null)); + String child2data = new String(zk.getData(queue_handle + "/" + child2, false, null)); + assertEquals(child1data, "0"); + assertEquals(child2data, "1"); + } finally { + if (zk != null) { + zk.close(); + } + } + + } + + @Test + public void testLargeNodeData() throws Exception { + ZooKeeper zk = null; + String queue_handle = "/large"; + try { + zk = createClient(); + + zk.create(queue_handle, new byte[500000], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } finally { + if (zk != null) { + zk.close(); + } + } + + } + + private void verifyCreateFails(String path, ZooKeeper zk) throws Exception { + try { + zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (IllegalArgumentException e) { + // this is good + return; + } + fail("bad path \"" + path + "\" not caught"); + } + + // Test that the path string is validated + @Test + public void testPathValidation() throws Exception { + ZooKeeper zk = createClient(); + + verifyCreateFails(null, zk); + verifyCreateFails("", zk); + verifyCreateFails("//", zk); + verifyCreateFails("///", zk); + verifyCreateFails("////", zk); + verifyCreateFails("/.", zk); + verifyCreateFails("/..", zk); + verifyCreateFails("/./", zk); + verifyCreateFails("/../", zk); + verifyCreateFails("/foo/./", zk); + verifyCreateFails("/foo/../", zk); + verifyCreateFails("/foo/.", zk); + verifyCreateFails("/foo/..", zk); + verifyCreateFails("/./.", zk); + verifyCreateFails("/../..", zk); + verifyCreateFails("/\u0001foo", zk); + verifyCreateFails("/foo/bar/", zk); + verifyCreateFails("/foo//bar", zk); + verifyCreateFails("/foo/bar//", zk); + + verifyCreateFails("foo", zk); + verifyCreateFails("a", zk); + + zk.create("/createseqpar", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + // next two steps - related to sequential processing + // 1) verify that empty child name fails if not sequential + try { + zk.create("/createseqpar/", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertTrue(false); + } catch (IllegalArgumentException be) { + // catch this. + } + + // 2) verify that empty child name success if sequential + zk.create("/createseqpar/", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + zk.create("/createseqpar/.", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + zk.create("/createseqpar/..", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + try { + zk.create("/createseqpar//", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + assertTrue(false); + } catch (IllegalArgumentException be) { + // catch this. + } + try { + zk.create("/createseqpar/./", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + assertTrue(false); + } catch (IllegalArgumentException be) { + // catch this. + } + try { + zk.create("/createseqpar/../", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + assertTrue(false); + } catch (IllegalArgumentException be) { + // catch this. + } + + //check for the code path that throws at server + PrepRequestProcessor.setFailCreate(true); + try { + zk.create("/m", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertTrue(false); + } catch (KeeperException.BadArgumentsException be) { + // catch this. + } + PrepRequestProcessor.setFailCreate(false); + zk.create("/.foo", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/.f.", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/..f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/..f..", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f.c", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f\u0040f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f/.f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f/f.", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f/..f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f/f..", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f/.f/f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/f/f./f", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + @Test + public void testDeleteWithChildren() throws Exception { + ZooKeeper zk = createClient(); + zk.create("/parent", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/parent/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + try { + zk.delete("/parent", -1); + fail("Should have received a not equals message"); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NOTEMPTY, e.code()); + } + zk.delete("/parent/child", -1); + zk.delete("/parent", -1); + zk.close(); + } + + private class VerifyClientCleanup extends Thread { + + int count; + int current = 0; + + VerifyClientCleanup(String name, int count) { + super(name); + this.count = count; + } + + public void run() { + try { + for (; current < count; current++) { + TestableZooKeeper zk = createClient(); + // we've asked to close, wait for it to finish closing + // all the sub-threads otw the selector may not be + // closed when we check (false positive on test failure + zk.close(CONNECTION_TIMEOUT); + } + } catch (Throwable t) { + LOG.error("test failed", t); + } + } + + } + + /** + * Verify that the client is cleaning up properly. Open/close a large + * number of sessions. Essentially looking to see if sockets/selectors + * are being cleaned up properly during close. + * + * @throws Throwable + */ + @Test + public void testClientCleanup() throws Throwable { + OSMXBean osMbean = new OSMXBean(); + if (!osMbean.getUnix()) { + LOG.warn("skipping testClientCleanup, only available on Unix"); + return; + } + + final int threadCount = 3; + final int clientCount = 10; + + /* Log the number of fds used before and after a test is run. Verifies + * we are freeing resources correctly. Unfortunately this only works + * on unix systems (the only place sun has implemented as part of the + * mgmt bean api). + */ + long initialFdCount = osMbean.getOpenFileDescriptorCount(); + + VerifyClientCleanup[] threads = new VerifyClientCleanup[threadCount]; + + for (int i = 0; i < threads.length; i++) { + threads[i] = new VerifyClientCleanup("VCC" + i, clientCount); + threads[i].start(); + } + + for (int i = 0; i < threads.length; i++) { + threads[i].join(CONNECTION_TIMEOUT); + assertTrue(threads[i].current == threads[i].count); + } + + // if this fails it means we are not cleaning up after the closed + // sessions. + long currentCount = osMbean.getOpenFileDescriptorCount(); + final String logmsg = "open fds after test ({}) are not significantly higher than before ({})"; + + if (currentCount > initialFdCount + 10) { + // consider as error + LOG.error(logmsg, currentCount, initialFdCount); + } else { + LOG.info(logmsg, currentCount, initialFdCount); + } + } + + /** + * We create a perfectly valid 'exists' request, except that the opcode is wrong. + * @throws Exception + */ + @Test + public void testNonExistingOpCode() throws Exception { + final CountDownLatch clientDisconnected = new CountDownLatch(1); + Watcher watcher = new Watcher() { + @Override + public synchronized void process(WatchedEvent event) { + if (event.getState() == KeeperState.Disconnected) { + clientDisconnected.countDown(); + } + } + }; + TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + + final String path = "/m1"; + + RequestHeader h = new RequestHeader(); + h.setType(888); // This code does not exists + ExistsRequest request = new ExistsRequest(); + request.setPath(path); + request.setWatch(false); + ExistsResponse response = new ExistsResponse(); + + ReplyHeader r = zk.submitRequest(h, request, response, null); + + assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue()); + + // Sending a nonexisting opcode should cause the server to disconnect + assertTrue("failed to disconnect", clientDisconnected.await(5000, TimeUnit.MILLISECONDS)); + zk.close(); + } + + @Test + public void testTryWithResources() throws Exception { + ZooKeeper zooKeeper; + try (ZooKeeper zk = createClient()) { + zooKeeper = zk; + assertTrue(zooKeeper.getState().isAlive()); + } + + assertFalse(zooKeeper.getState().isAlive()); + } + + @Test + public void testCXidRollover() throws Exception { + TestableZooKeeper zk = null; + try { + zk = createClient(); + zk.setXid(Integer.MAX_VALUE - 10); + + zk.create("/testnode", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int i = 0; i < 20; ++i) { + final CountDownLatch latch = new CountDownLatch(1); + final AtomicInteger rc = new AtomicInteger(0); + zk.setData("/testnode", "".getBytes(), -1, new AsyncCallback.StatCallback() { + @Override + public void processResult(int retcode, String path, Object ctx, Stat stat) { + rc.set(retcode); + latch.countDown(); + } + }, null); + assertTrue("setData should complete within 5s", latch.await(zk.getSessionTimeout(), TimeUnit.MILLISECONDS)); + assertEquals("setData should have succeeded", Code.OK.intValue(), rc.get()); + } + zk.delete("/testnode", -1); + assertTrue("xid should be positive", zk.checkXid() > 0); + } finally { + if (zk != null) { + zk.close(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ConnectStringParserTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ConnectStringParserTest.java new file mode 100644 index 0000000..e1048f2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ConnectStringParserTest.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.client.ConnectStringParser; +import org.junit.Assert; +import org.junit.Test; + +public class ConnectStringParserTest extends ZKTestCase { + + private static final int DEFAULT_PORT = 2181; + + @Test + public void testSingleServerChrootPath() { + String chrootPath = "/hallo/welt"; + String servers = "10.10.10.1"; + assertChrootPath(chrootPath, new ConnectStringParser(servers + chrootPath)); + + servers = "[2001:db8:1::242:ac11:2]"; + assertChrootPath(chrootPath, new ConnectStringParser(servers + chrootPath)); + } + + @Test + public void testMultipleServersChrootPath() { + String chrootPath = "/hallo/welt"; + String servers = "10.10.10.1,10.10.10.2"; + assertChrootPath(chrootPath, new ConnectStringParser(servers + chrootPath)); + + servers = "[2001:db8:1::242:ac11:2]:2181,[2001:db8:85a3:8d3:1319:8a2e:370:7348]:5678"; + assertChrootPath(chrootPath, new ConnectStringParser(servers + chrootPath)); + } + + @Test + public void testParseServersWithoutPort() { + String servers = "10.10.10.1,10.10.10.2"; + ConnectStringParser parser = new ConnectStringParser(servers); + assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostString()); + assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(0).getPort()); + assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostString()); + assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(1).getPort()); + + servers = "[2001:db8:1::242:ac11:2],[2001:db8:85a3:8d3:1319:8a2e:370:7348]"; + parser = new ConnectStringParser(servers); + assertEquals("2001:db8:1::242:ac11:2", parser.getServerAddresses().get(0).getHostString()); + assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(0).getPort()); + assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", parser.getServerAddresses().get(1).getHostString()); + assertEquals(DEFAULT_PORT, parser.getServerAddresses().get(1).getPort()); + } + + @Test + public void testParseServersWithPort() { + String servers = "10.10.10.1:112,10.10.10.2:110"; + ConnectStringParser parser = new ConnectStringParser(servers); + assertEquals("10.10.10.1", parser.getServerAddresses().get(0).getHostString()); + assertEquals("10.10.10.2", parser.getServerAddresses().get(1).getHostString()); + assertEquals(112, parser.getServerAddresses().get(0).getPort()); + assertEquals(110, parser.getServerAddresses().get(1).getPort()); + + servers = "[2001:db8:1::242:ac11:2]:1234,[2001:db8:85a3:8d3:1319:8a2e:370:7348]:5678"; + parser = new ConnectStringParser(servers); + assertEquals("2001:db8:1::242:ac11:2", parser.getServerAddresses().get(0).getHostString()); + assertEquals("2001:db8:85a3:8d3:1319:8a2e:370:7348", parser.getServerAddresses().get(1).getHostString()); + assertEquals(1234, parser.getServerAddresses().get(0).getPort()); + assertEquals(5678, parser.getServerAddresses().get(1).getPort()); + } + + private void assertChrootPath(String expected, ConnectStringParser parser) { + assertEquals(expected, parser.getChrootPath()); + } + + @Test + public void testParseIPV6ConnectionString() { + String servers = "[127::1],127.0.10.2"; + ConnectStringParser parser = new ConnectStringParser(servers); + + Assert.assertEquals("127::1", parser.getServerAddresses().get(0).getHostString()); + Assert.assertEquals("127.0.10.2", parser.getServerAddresses().get(1).getHostString()); + Assert.assertEquals(2181, parser.getServerAddresses().get(0).getPort()); + Assert.assertEquals(2181, parser.getServerAddresses().get(1).getPort()); + + servers = "[127::1]:2181,[127::2]:2182,[127::3]:2183"; + parser = new ConnectStringParser(servers); + + Assert.assertEquals("127::1", parser.getServerAddresses().get(0).getHostString()); + Assert.assertEquals("127::2", parser.getServerAddresses().get(1).getHostString()); + Assert.assertEquals("127::3", parser.getServerAddresses().get(2).getHostString()); + Assert.assertEquals(2181, parser.getServerAddresses().get(0).getPort()); + Assert.assertEquals(2182, parser.getServerAddresses().get(1).getPort()); + Assert.assertEquals(2183, parser.getServerAddresses().get(2).getPort()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/CreateModeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/CreateModeTest.java new file mode 100644 index 0000000..d2e908b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/CreateModeTest.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.EnumSet; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class CreateModeTest extends ZKTestCase { + + @Test + public void testBasicCreateMode() { + CreateMode cm = CreateMode.PERSISTENT; + assertEquals(cm.toFlag(), 0); + assertFalse(cm.isEphemeral()); + assertFalse(cm.isSequential()); + assertFalse(cm.isContainer()); + + cm = CreateMode.EPHEMERAL; + assertEquals(cm.toFlag(), 1); + assertTrue(cm.isEphemeral()); + assertFalse(cm.isSequential()); + assertFalse(cm.isContainer()); + + cm = CreateMode.PERSISTENT_SEQUENTIAL; + assertEquals(cm.toFlag(), 2); + assertFalse(cm.isEphemeral()); + assertTrue(cm.isSequential()); + assertFalse(cm.isContainer()); + + cm = CreateMode.EPHEMERAL_SEQUENTIAL; + assertEquals(cm.toFlag(), 3); + assertTrue(cm.isEphemeral()); + assertTrue(cm.isSequential()); + assertFalse(cm.isContainer()); + + cm = CreateMode.CONTAINER; + assertEquals(cm.toFlag(), 4); + assertFalse(cm.isEphemeral()); + assertFalse(cm.isSequential()); + assertTrue(cm.isContainer()); + } + + @Test + public void testFlagConversion() throws KeeperException { + // Ensure we get the same value back after round trip conversion + EnumSet allModes = EnumSet.allOf(CreateMode.class); + + for (CreateMode cm : allModes) { + assertEquals(cm, CreateMode.fromFlag(cm.toFlag())); + } + } + + @Test + public void testInvalidFlagConversion() throws KeeperException { + try { + CreateMode.fromFlag(99); + fail("Shouldn't be able to convert 99 to a CreateMode."); + } catch (KeeperException ke) { + assertEquals(Code.BADARGUMENTS, ke.code()); + } + + try { + CreateMode.fromFlag(-1); + fail("Shouldn't be able to convert -1 to a CreateMode."); + } catch (KeeperException ke) { + assertEquals(Code.BADARGUMENTS, ke.code()); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/CreateTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/CreateTest.java new file mode 100644 index 0000000..a3d5ccf --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/CreateTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.Test; + +/** + * Test suite for validating the Create API. + */ +public class CreateTest extends ClientBase { + + private ZooKeeper zk; + + @Override + public void setUp() throws Exception { + super.setUp(); + zk = createClient(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + zk.close(); + } + + @Test + public void testCreate() throws IOException, KeeperException, InterruptedException { + createNoStatVerifyResult("/foo"); + createNoStatVerifyResult("/foo/child"); + } + + @Test + public void testCreateWithStat() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + Stat stat = createWithStatVerifyResult("/foo"); + Stat childStat = createWithStatVerifyResult("/foo/child"); + // Don't expect to get the same stats for different creates. + assertFalse(stat.equals(childStat)); + } + + @Test + public void testCreateWithNullStat() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + assertNull(zk.exists(name, false)); + + Stat stat = null; + // If a null Stat object is passed the create should still + // succeed, but no Stat info will be returned. + String path = zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat); + assertNull(stat); + assertNotNull(zk.exists(name, false)); + } + + private void createNoStatVerifyResult(String newName) throws KeeperException, InterruptedException { + assertNull("Node existed before created", zk.exists(newName, false)); + String path = zk.create(newName, newName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(path, newName); + assertNotNull("Node was not created as expected", zk.exists(newName, false)); + } + + private Stat createWithStatVerifyResult(String newName) throws KeeperException, InterruptedException { + assertNull("Node existed before created", zk.exists(newName, false)); + Stat stat = new Stat(); + String path = zk.create(newName, newName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat); + assertEquals(path, newName); + validateCreateStat(stat, newName); + + Stat referenceStat = zk.exists(newName, false); + assertNotNull("Node was not created as expected", referenceStat); + assertEquals(referenceStat, stat); + + return stat; + } + + private void validateCreateStat(Stat stat, String name) { + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/DisconnectableZooKeeper.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/DisconnectableZooKeeper.java new file mode 100644 index 0000000..e429f2a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/DisconnectableZooKeeper.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import java.io.IOException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; + +public class DisconnectableZooKeeper extends ZooKeeper { + + public DisconnectableZooKeeper(String host, int sessionTimeout, Watcher watcher) throws IOException { + super(host, sessionTimeout, watcher); + } + + public DisconnectableZooKeeper( + String host, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) throws IOException { + super(host, sessionTimeout, watcher, sessionId, sessionPasswd); + } + + /** Testing only!!! Really!!!! This is only here to test when the client + * disconnects from the server w/o sending a session disconnect (ie + * ending the session cleanly). The server will eventually notice the + * client is no longer pinging and will timeout the session. + */ + public void disconnect() throws IOException { + cnxn.disconnect(); + } + + /** + * Prevent the client from automatically reconnecting if the connection to the + * server is lost + */ + public void dontReconnect() throws Exception { + java.lang.reflect.Field f = cnxn.getClass().getDeclaredField("closing"); + f.setAccessible(true); + f.setBoolean(cnxn, true); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/DisconnectedWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/DisconnectedWatcherTest.java new file mode 100644 index 0000000..dcff76c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/DisconnectedWatcherTest.java @@ -0,0 +1,260 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DisconnectedWatcherTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(DisconnectedWatcherTest.class); + final int TIMEOUT = 5000; + + private class MyWatcher extends CountdownWatcher { + + LinkedBlockingQueue events = new LinkedBlockingQueue(); + + public void process(WatchedEvent event) { + super.process(event); + if (event.getType() != Event.EventType.None) { + try { + events.put(event); + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt during event.put"); + } + } + } + + } + + private CountdownWatcher watcher1; + private ZooKeeper zk1; + private MyWatcher watcher2; + private ZooKeeper zk2; + + @Before + public void setUp() throws Exception { + super.setUp(); + watcher1 = new CountdownWatcher(); + zk1 = createClient(watcher1); + watcher2 = new MyWatcher(); + } + + @After + public void tearDown() throws Exception { + if (zk2 != null) { + zk2.close(); + } + if (zk1 != null) { + zk1.close(); + } + super.tearDown(); + } + + // @see jira issue ZOOKEEPER-961 + + @Test + public void testChildWatcherAutoResetWithChroot() throws Exception { + zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk2 = createClient(watcher2, hostPort + "/ch1"); + zk2.getChildren("/", true); + + // this call shouldn't trigger any error or watch + zk1.create("/youdontmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // this should trigger the watch + zk1.create("/ch1/youshouldmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + WatchedEvent e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/", e.getPath()); + + MyWatcher childWatcher = new MyWatcher(); + zk2.getChildren("/", childWatcher); + + stopServer(); + watcher2.waitForDisconnected(3000); + startServer(); + watcher2.waitForConnected(3000); + watcher1.waitForConnected(3000); + + // this should trigger the watch + zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + e = childWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/", e.getPath()); + } + + @Test + public void testDefaultWatcherAutoResetWithChroot() throws Exception { + zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk2 = createClient(watcher2, hostPort + "/ch1"); + zk2.getChildren("/", true); + + // this call shouldn't trigger any error or watch + zk1.create("/youdontmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // this should trigger the watch + zk1.create("/ch1/youshouldmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + WatchedEvent e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/", e.getPath()); + + zk2.getChildren("/", true); + + stopServer(); + watcher2.waitForDisconnected(3000); + startServer(); + watcher2.waitForConnected(3000); + watcher1.waitForConnected(3000); + + // this should trigger the watch + zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/", e.getPath()); + } + + @Test + public void testDeepChildWatcherAutoResetWithChroot() throws Exception { + zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.create("/ch1/here", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.create("/ch1/here/we", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.create("/ch1/here/we/are", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk2 = createClient(watcher2, hostPort + "/ch1/here/we"); + zk2.getChildren("/are", true); + + // this should trigger the watch + zk1.create("/ch1/here/we/are/now", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + WatchedEvent e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/are", e.getPath()); + + MyWatcher childWatcher = new MyWatcher(); + zk2.getChildren("/are", childWatcher); + + stopServer(); + watcher2.waitForDisconnected(3000); + startServer(); + watcher2.waitForConnected(3000); + watcher1.waitForConnected(3000); + + // this should trigger the watch + zk1.create("/ch1/here/we/are/again", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + e = childWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/are", e.getPath()); + } + + // @see jira issue ZOOKEEPER-706. Test auto reset of a large number of + // watches which require multiple SetWatches calls. + @Test(timeout = 840000) + public void testManyChildWatchersAutoReset() throws Exception { + zk2 = createClient(watcher2); + + // 110 character base path + String pathBase = "/long-path-000000000-111111111-222222222-333333333-444444444-" + + "555555555-666666666-777777777-888888888-999999999"; + + zk1.create(pathBase, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // Create 10,000 nodes. This should ensure the length of our + // watches set below exceeds 1MB. + List paths = new ArrayList(); + for (int i = 0; i < 10000; i++) { + String path = zk1.create(pathBase + "/ch-", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + paths.add(path); + } + LOG.info("Created 10,000 nodes."); + + MyWatcher childWatcher = new MyWatcher(); + + // Set a combination of child/exists/data watches + int i = 0; + for (String path : paths) { + if (i % 3 == 0) { + zk2.getChildren(path, childWatcher); + } else if (i % 3 == 1) { + zk2.exists(path + "/foo", childWatcher); + } else if (i % 3 == 2) { + zk2.getData(path, childWatcher, null); + } + + i++; + } + + stopServer(); + watcher2.waitForDisconnected(30000); + startServer(); + watcher2.waitForConnected(30000); + watcher1.waitForConnected(30000); + + // Trigger the watches and ensure they properly propagate to the client + i = 0; + for (String path : paths) { + if (i % 3 == 0) { + zk1.create(path + "/ch", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + WatchedEvent e = childWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals(path, e.getPath()); + } else if (i % 3 == 1) { + zk1.create(path + "/foo", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + WatchedEvent e = childWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeCreated, e.getType()); + assertEquals(path + "/foo", e.getPath()); + } else if (i % 3 == 2) { + zk1.setData(path, new byte[]{1, 2, 3}, -1); + + WatchedEvent e = childWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertNotNull(e); + assertEquals(EventType.NodeDataChanged, e.getType()); + assertEquals(path, e.getPath()); + } + + i++; + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/DuplicateLocalSessionUpgradeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/DuplicateLocalSessionUpgradeTest.java new file mode 100644 index 0000000..8e2b530 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/DuplicateLocalSessionUpgradeTest.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * When request are route incorrectly, both follower and the leader will perform + * local session upgrade. So we saw CreateSession twice in txnlog This doesn't + * affect the correctness but cause the ensemble to see more load than + * necessary. + */ +public class DuplicateLocalSessionUpgradeTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(DuplicateLocalSessionUpgradeTest.class); + + private final QuorumBase qb = new QuorumBase(); + + private static final int CONNECTION_TIMEOUT = ClientBase.CONNECTION_TIMEOUT; + + @Before + public void setUp() throws Exception { + LOG.info("STARTING quorum {}", getClass().getName()); + qb.localSessionsEnabled = true; + qb.localSessionsUpgradingEnabled = true; + qb.setUp(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + } + + @After + public void tearDown() throws Exception { + LOG.info("STOPPING quorum {}", getClass().getName()); + qb.tearDown(); + } + + @Test + public void testLocalSessionUpgradeOnFollower() throws Exception { + testLocalSessionUpgrade(false); + } + + @Test + public void testLocalSessionUpgradeOnLeader() throws Exception { + testLocalSessionUpgrade(true); + } + + private void testLocalSessionUpgrade(boolean testLeader) throws Exception { + + int leaderIdx = qb.getLeaderIndex(); + assertFalse("No leader in quorum?", leaderIdx == -1); + int followerIdx = (leaderIdx + 1) % 5; + int testPeerIdx = testLeader ? leaderIdx : followerIdx; + String[] hostPorts = qb.hostPort.split(","); + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx], CONNECTION_TIMEOUT); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + final String firstPath = "/first"; + final String secondPath = "/ephemeral"; + + // Just create some node so that we know the current zxid + zk.create(firstPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // Now, try an ephemeral node. This will trigger session upgrade + // so there will be createSession request inject into the pipeline + // prior to this request + zk.create(secondPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + Stat firstStat = zk.exists(firstPath, null); + assertNotNull(firstStat); + + Stat secondStat = zk.exists(secondPath, null); + assertNotNull(secondStat); + + long zxidDiff = secondStat.getCzxid() - firstStat.getCzxid(); + + // If there is only one createSession request in between, zxid diff + // will be exactly 2. The alternative way of checking is to actually + // read txnlog but this should be sufficient + assertEquals(2L, zxidDiff); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/EmptiedSnapshotRecoveryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/EmptiedSnapshotRecoveryTest.java new file mode 100644 index 0000000..da50a9c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/EmptiedSnapshotRecoveryTest.java @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** If snapshots are corrupted to the empty file or deleted, Zookeeper should + * not proceed to read its transaction log files + * Test that zxid == -1 in the presence of emptied/deleted snapshots + */ +public class EmptiedSnapshotRecoveryTest extends ZKTestCase implements Watcher { + + private static final Logger LOG = LoggerFactory.getLogger(RestoreCommittedLogTest.class); + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + private static final int N_TRANSACTIONS = 150; + private static final int SNAP_COUNT = 100; + + public void runTest(boolean leaveEmptyFile, boolean trustEmptySnap) throws Exception { + File tmpSnapDir = ClientBase.createTmpDir(); + File tmpLogDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000); + SyncRequestProcessor.setSnapCount(SNAP_COUNT); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + try { + for (int i = 0; i < N_TRANSACTIONS; i++) { + zk.create("/node-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + + // start server again with intact database + zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000); + zks.startdata(); + long zxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + LOG.info("After clean restart, zxid = {}", zxid); + assertTrue("zxid > 0", zxid > 0); + zks.shutdown(); + + // Make all snapshots empty + FileTxnSnapLog txnLogFactory = zks.getTxnLogFactory(); + List snapshots = txnLogFactory.findNRecentSnapshots(10); + assertTrue("We have a snapshot to corrupt", snapshots.size() > 0); + for (File file : snapshots) { + if (leaveEmptyFile) { + new PrintWriter(file).close(); + } else { + file.delete(); + } + } + + if (trustEmptySnap) { + System.setProperty(FileTxnSnapLog.ZOOKEEPER_SNAPSHOT_TRUST_EMPTY, "true"); + } + // start server again with corrupted database + zks = new ZooKeeperServer(tmpSnapDir, tmpLogDir, 3000); + try { + zks.startdata(); + long currentZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + if (!trustEmptySnap) { + fail("Should have gotten exception for corrupted database"); + } + assertEquals("zxid mismatch after restoring database", currentZxid, zxid); + } catch (IOException e) { + // expected behavior + if (trustEmptySnap) { + fail("Should not get exception for empty database"); + } + } finally { + if (trustEmptySnap) { + System.clearProperty(FileTxnSnapLog.ZOOKEEPER_SNAPSHOT_TRUST_EMPTY); + } + } + + zks.shutdown(); + } + + /** + * Test resilience to empty Snapshots + * @throws Exception an exception might be thrown here + */ + @Test + public void testRestoreWithEmptySnapFiles() throws Exception { + runTest(true, false); + } + + /** + * Test resilience to deletion of Snapshots + * @throws Exception an exception might be thrown here + */ + @Test + public void testRestoreWithNoSnapFiles() throws Exception { + runTest(false, false); + } + + @Test + public void testRestoreWithTrustedEmptySnapFiles() throws Exception { + runTest(false, true); + } + + public void process(WatchedEvent event) { + // do nothing + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/EnsembleAuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/EnsembleAuthTest.java new file mode 100644 index 0000000..dcd7ee6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/EnsembleAuthTest.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import java.io.IOException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.auth.EnsembleAuthenticationProvider; +import org.apache.zookeeper.server.auth.ProviderRegistry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class EnsembleAuthTest extends ClientBase { + + @Before + public void setUp() throws Exception { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.EnsembleAuthenticationProvider"); + super.setUp(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + System.clearProperty("zookeeper.authProvider.1"); + System.clearProperty(EnsembleAuthenticationProvider.ENSEMBLE_PROPERTY); + ProviderRegistry.removeProvider("ensemble"); + } + + @Test + public void noAuth() throws Exception { + resetEnsembleAuth(null, false); + connectToEnsemble(null); + } + + @Test + public void emptyAuth() throws Exception { + resetEnsembleAuth(null, true); + connectToEnsemble("foo"); + } + + @Test + public void skipAuth() throws Exception { + resetEnsembleAuth("woo", true); + connectToEnsemble(null); + } + + @Test + public void passAuth() throws Exception { + resetEnsembleAuth("woo", true); + connectToEnsemble("woo"); + } + + @Test + public void passAuthCSV() throws Exception { + resetEnsembleAuth(" foo,bar, baz ", true); + + connectToEnsemble("foo"); + connectToEnsemble("bar"); + connectToEnsemble("baz"); + } + + @Test(expected = KeeperException.ConnectionLossException.class) + public void failAuth() throws Exception { + resetEnsembleAuth("woo", true); + connectToEnsemble("goo"); + } + + @Test(expected = KeeperException.AuthFailedException.class) + public void removeEnsembleAuthProvider() throws Exception { + resetEnsembleAuth(null, false); + connectToEnsemble("goo"); + } + + private void connectToEnsemble(final String auth) throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient()) { + // pass auth check + if (auth != null) { + zk.addAuthInfo("ensemble", auth.getBytes()); + } + zk.getData("/", false, null); + } + } + + private void resetEnsembleAuth(final String auth, final boolean useAuth) throws Exception { + stopServer(); + if (auth == null) { + System.clearProperty(EnsembleAuthenticationProvider.ENSEMBLE_PROPERTY); + } else { + System.setProperty(EnsembleAuthenticationProvider.ENSEMBLE_PROPERTY, auth); + } + if (useAuth) { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.EnsembleAuthenticationProvider"); + } else { + System.clearProperty("zookeeper.authProvider.1"); + } + ProviderRegistry.removeProvider("ensemble"); + ProviderRegistry.initialize(); + startServer(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/EventTypeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/EventTypeTest.java new file mode 100644 index 0000000..cf9807d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/EventTypeTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.util.EnumSet; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class EventTypeTest extends ZKTestCase { + + @Test + public void testIntConversion() { + // Ensure that we can convert all valid integers to EventTypes + EnumSet allTypes = EnumSet.allOf(EventType.class); + + for (EventType et : allTypes) { + assertEquals(et, EventType.fromInt(et.getIntValue())); + } + } + + @Test + public void testInvalidIntConversion() { + try { + EventType.fromInt(324242); + fail("Was able to create an invalid EventType via an integer"); + } catch (RuntimeException re) { + // we're good. + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLENewEpochTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLENewEpochTest.java new file mode 100644 index 0000000..64af397 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLENewEpochTest.java @@ -0,0 +1,189 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.concurrent.Semaphore; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.Vote; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLENewEpochTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLENewEpochTest.class); + + int count; + HashMap peers; + ArrayList threads; + File[] tmpdir; + int[] port; + volatile int[] round; + + Semaphore start0; + Semaphore finish3, finish0; + + @Before + public void setUp() throws Exception { + count = 3; + + peers = new HashMap(count); + threads = new ArrayList(count); + tmpdir = new File[count]; + port = new int[count]; + + round = new int[3]; + round[0] = 0; + round[1] = 0; + round[2] = 0; + + start0 = new Semaphore(0); + finish0 = new Semaphore(0); + finish3 = new Semaphore(0); + } + + @After + public void tearDown() throws Exception { + for (int i = 0; i < threads.size(); i++) { + threads.get(i).peer.getElectionAlg().shutdown(); + } + } + + class LEThread extends Thread { + + int i; + QuorumPeer peer; + + LEThread(QuorumPeer peer, int i) { + this.i = i; + this.peer = peer; + LOG.info("Constructor: {}", getName()); + + } + + public void run() { + boolean flag = true; + try { + while (flag) { + Vote v = null; + peer.setPeerState(ServerState.LOOKING); + LOG.info("Going to call leader election again: {}", i); + v = peer.getElectionAlg().lookForLeader(); + + if (v == null) { + fail("Thread " + i + " got a null vote"); + } + + /* + * A real zookeeper would take care of setting the current vote. Here + * we do it manually. + */ + peer.setCurrentVote(v); + + LOG.info("Finished election: {}, {}", i, v.getId()); + //votes[i] = v; + + switch (i) { + case 0: + LOG.info("First peer, do nothing, just join"); + if (finish0.tryAcquire(1000, java.util.concurrent.TimeUnit.MILLISECONDS)) { + //if(threads.get(0).peer.getPeerState() == ServerState.LEADING ){ + LOG.info("Setting flag to false"); + flag = false; + } + break; + case 1: + LOG.info("Second entering case"); + if (round[1] != 0) { + finish0.release(); + flag = false; + } else { + finish3.acquire(); + start0.release(); + } + LOG.info("Second is going to start second round"); + round[1]++; + break; + case 2: + LOG.info("Third peer, shutting it down"); + QuorumBase.shutdown(peer); + flag = false; + round[2] = 1; + finish3.release(); + LOG.info("Third leaving"); + break; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + + @Test + public void testLENewEpoch() throws Exception { + + LOG.info("TestLE: {}, {}", getTestName(), count); + for (int i = 0; i < count; i++) { + peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + + for (int i = 1; i < count; i++) { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2, 2); + peer.startLeaderElection(); + LEThread thread = new LEThread(peer, i); + thread.start(); + threads.add(thread); + } + if (!start0.tryAcquire(4000, java.util.concurrent.TimeUnit.MILLISECONDS)) { + fail("First leader election failed"); + } + + QuorumPeer peer = new QuorumPeer(peers, tmpdir[0], tmpdir[0], port[0], 3, 0, 1000, 2, 2, 2); + peer.startLeaderElection(); + LEThread thread = new LEThread(peer, 0); + thread.start(); + threads.add(thread); + + LOG.info("Started threads {}", getTestName()); + + for (int i = 0; i < threads.size(); i++) { + threads.get(i).join(10000); + if (threads.get(i).isAlive()) { + fail("Threads didn't join"); + } + + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEPredicateTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEPredicateTest.java new file mode 100644 index 0000000..3fcb83b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEPredicateTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLEPredicateTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLEPredicateTest.class); + + class MockFLE extends FastLeaderElection { + + MockFLE(QuorumPeer peer) { + super(peer, peer.createCnxnManager()); + } + + boolean predicate(long newId, long newZxid, long newEpoch, long curId, long curZxid, long curEpoch) { + return this.totalOrderPredicate(newId, newZxid, newEpoch, curId, curZxid, curEpoch); + } + + } + + HashMap peers; + + @Test + public void testPredicate() throws IOException { + + peers = new HashMap(3); + + /* + * Creates list of peers. + */ + for (int i = 0; i < 3; i++) { + peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + } + + /* + * Creating peer. + */ + try { + File tmpDir = ClientBase.createTmpDir(); + QuorumPeer peer = new QuorumPeer(peers, tmpDir, tmpDir, PortAssignment.unique(), 3, 0, 1000, 2, 2, 2); + + MockFLE mock = new MockFLE(peer); + mock.start(); + + /* + * Lower epoch must return false + */ + + assertFalse(mock.predicate(4L, 0L, 0L, 3L, 0L, 2L)); + + /* + * Later epoch + */ + assertTrue(mock.predicate(0L, 0L, 1L, 1L, 0L, 0L)); + + /* + * Higher zxid + */ + assertTrue(mock.predicate(0L, 1L, 0L, 1L, 0L, 0L)); + + /* + * Higher id + */ + assertTrue(mock.predicate(1L, 1L, 0L, 0L, 1L, 0L)); + } catch (IOException e) { + LOG.error("Exception while creating quorum peer", e); + fail("Exception while creating quorum peer"); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLERestartTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLERestartTest.java new file mode 100644 index 0000000..3563019 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLERestartTest.java @@ -0,0 +1,189 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Semaphore; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.Vote; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLERestartTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLETest.class); + + private int count; + private Map peers; + private List restartThreads; + private File[] tmpdir; + private int[] port; + private Semaphore finish; + + static class TestVote { + + long leader; + + TestVote(int id, long leader) { + this.leader = leader; + } + + } + + int countVotes(HashSet hs, long id) { + int counter = 0; + for (TestVote v : hs) { + if (v.leader == id) { + counter++; + } + } + + return counter; + } + + @Before + public void setUp() throws Exception { + count = 3; + peers = new HashMap(count); + restartThreads = new ArrayList(count); + tmpdir = new File[count]; + port = new int[count]; + finish = new Semaphore(0); + } + + @After + public void tearDown() throws Exception { + for (int i = 0; i < restartThreads.size(); i++) { + restartThreads.get(i).peer.getElectionAlg().shutdown(); + } + } + + class FLERestartThread extends Thread { + + int i; + QuorumPeer peer; + int peerRound = 0; + + FLERestartThread(QuorumPeer peer, int i) { + this.i = i; + this.peer = peer; + LOG.info("Constructor: {}", getName()); + } + public void run() { + try { + Vote v = null; + while (true) { + peer.setPeerState(ServerState.LOOKING); + LOG.info("Going to call leader election again."); + v = peer.getElectionAlg().lookForLeader(); + if (v == null) { + LOG.info("Thread {} got a null vote", i); + break; + } + + /* + * A real zookeeper would take care of setting the current vote. Here + * we do it manually. + */ + peer.setCurrentVote(v); + + LOG.info("Finished election: {}, {}", i, v.getId()); + //votes[i] = v; + + switch (i) { + case 0: + if (peerRound == 0) { + LOG.info("First peer, shutting it down"); + QuorumBase.shutdown(peer); + restartThreads.get(i).peer.getElectionAlg().shutdown(); + + peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2, 2); + peer.startLeaderElection(); + peerRound++; + } else { + finish.release(2); + return; + } + + break; + case 1: + LOG.info("Second entering case"); + finish.acquire(); + //if(threads.get(0).peer.getPeerState() == ServerState.LEADING ){ + LOG.info("Release"); + + return; + case 2: + LOG.info("First peer, do nothing, just join"); + finish.acquire(); + //if(threads.get(0).peer.getPeerState() == ServerState.LEADING ){ + LOG.info("Release"); + + return; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + + @Test + public void testLERestart() throws Exception { + + LOG.info("TestLE: {}, {}", getTestName(), count); + for (int i = 0; i < count; i++) { + peers.put((long) i, new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + tmpdir[i] = ClientBase.createTmpDir(); + port[i] = PortAssignment.unique(); + } + + for (int i = 0; i < count; i++) { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2, 2); + peer.startLeaderElection(); + FLERestartThread thread = new FLERestartThread(peer, i); + thread.start(); + restartThreads.add(thread); + } + LOG.info("Started threads {}", getTestName()); + for (int i = 0; i < restartThreads.size(); i++) { + restartThreads.get(i).join(10000); + if (restartThreads.get(i).isAlive()) { + fail("Threads didn't join"); + } + + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLETest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLETest.java new file mode 100644 index 0000000..8583f64 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLETest.java @@ -0,0 +1,506 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.FastLeaderElection; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.Vote; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLETest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(FLETest.class); + private final int MAX_LOOP_COUNTER = 300; + private FLETest.LEThread leThread; + + static class TestVote { + + TestVote(int id, long leader) { + this.leader = leader; + } + + long leader; + + } + + int countVotes(HashSet hs, long id) { + int counter = 0; + for (TestVote v : hs) { + if (v.leader == id) { + counter++; + } + } + + return counter; + } + + int count; + Map peers; + ArrayList threads; + Map> voteMap; + Map quora; + File[] tmpdir; + int[] port; + int successCount; + + volatile Vote[] votes; + volatile long leader = -1; + //volatile int round = 1; + Random rand = new Random(); + Set joinedThreads; + + @Before + public void setUp() throws Exception { + count = 7; + + peers = new HashMap(count); + threads = new ArrayList(count); + voteMap = new HashMap>(); + votes = new Vote[count]; + tmpdir = new File[count]; + port = new int[count]; + successCount = 0; + joinedThreads = new HashSet(); + } + + @After + public void tearDown() throws Exception { + for (int i = 0; i < threads.size(); i++) { + leThread = threads.get(i); + QuorumBase.shutdown(leThread.peer); + } + } + + /** + * Implements the behavior of a peer during the leader election rounds + * of tests. + */ + class LEThread extends Thread { + + FLETest self; + int i; + QuorumPeer peer; + int totalRounds; + ConcurrentHashMap> quora; + + LEThread(FLETest self, QuorumPeer peer, int i, int rounds, ConcurrentHashMap> quora) { + this.self = self; + this.i = i; + this.peer = peer; + this.totalRounds = rounds; + this.quora = quora; + + LOG.info("Constructor: {}", getName()); + } + + public void run() { + try { + Vote v = null; + while (true) { + + /* + * Set the state of the peer to LOOKING and look for leader + */ + peer.setPeerState(ServerState.LOOKING); + LOG.info("Going to call leader election again."); + v = peer.getElectionAlg().lookForLeader(); + if (v == null) { + LOG.info("Thread {} got a null vote", i); + break; + } + + /* + * Done with the election round, so now we set the vote in + * the peer. A real zookeeper would take care of setting the + * current vote. Here we do it manually. + */ + peer.setCurrentVote(v); + + LOG.info("Finished election: {}, {}", i, v.getId()); + votes[i] = v; + + /* + * Get the current value of the logical clock for this peer + * so that we know in which round this peer has executed. + */ + int lc = (int) ((FastLeaderElection) peer.getElectionAlg()).getLogicalClock(); + + /* + * The leader executes the following block, which essentially shuts down + * the peer if it is not the last round. + */ + if (v.getId() == i) { + LOG.info("I'm the leader: {}", i); + if (lc < this.totalRounds) { + LOG.info("Leader {} dying", i); + FastLeaderElection election = (FastLeaderElection) peer.getElectionAlg(); + election.shutdown(); + // Make sure the vote is reset to -1 after shutdown. + assertEquals(-1, election.getVote().getId()); + LOG.info("Leader {} dead", i); + + break; + } + } + + /* + * If the peer has done enough rounds, then consider joining. The thread + * will only join if it is part of a quorum supporting the current + * leader. Otherwise it will try again. + */ + if (lc >= this.totalRounds) { + /* + * quora keeps the supporters of a given leader, so + * we first update it with the vote of this peer. + */ + if (quora.get(v.getId()) == null) { + quora.put(v.getId(), new HashSet()); + } + quora.get(v.getId()).add(i); + + /* + * we now wait until a quorum supports the same leader. + */ + if (waitForQuorum(v.getId())) { + synchronized (self) { + + /* + * Assert that the state of the thread is the one expected. + */ + if (v.getId() == i) { + assertTrue("Wrong state" + peer.getPeerState(), peer.getPeerState() + == ServerState.LEADING); + leader = i; + } else { + assertTrue("Wrong state" + peer.getPeerState(), peer.getPeerState() + == ServerState.FOLLOWING); + } + + /* + * Global variable keeping track of + * how many peers have successfully + * joined. + */ + successCount++; + joinedThreads.add((long) i); + self.notify(); + } + + /* + * I'm done so joining. + */ + break; + } else { + quora.get(v.getId()).remove(i); + } + } + + /* + * This sleep time represents the time a follower + * would take to declare the leader dead and start + * a new leader election. + */ + Thread.sleep(100); + + } + LOG.debug("Thread {} votes {}", i, v); + } catch (InterruptedException e) { + fail(e.toString()); + } + } + + /** + * Auxiliary method to make sure that enough followers terminated. + * + * @return boolean followers successfully joined. + */ + boolean waitForQuorum(long id) throws InterruptedException { + int loopCounter = 0; + while ((quora.get(id).size() <= count / 2) && (loopCounter < MAX_LOOP_COUNTER)) { + Thread.sleep(100); + loopCounter++; + } + + return (loopCounter < MAX_LOOP_COUNTER) || (quora.get(id).size() > count / 2); + } + + } + + @Test + public void testSingleElection() throws Exception { + try { + runElection(1); + } catch (Exception e) { + fail(e.toString()); + } + } + + @Test + public void testDoubleElection() throws Exception { + try { + runElection(2); + } catch (Exception e) { + fail(e.toString()); + } + } + + @Test + public void testTripleElection() throws Exception { + try { + runElection(3); + } catch (Exception e) { + fail(e.toString()); + } + } + + /** + * Test leader election for a number of rounds. In all rounds but the last one + * we kill the leader. + * + * @param rounds + * @throws Exception + */ + private void runElection(int rounds) throws Exception { + ConcurrentHashMap> quora = new ConcurrentHashMap>(); + + LOG.info("TestLE: {}, {}", getTestName(), count); + + /* + * Creates list of peers. + */ + for (int i = 0; i < count; i++) { + port[i] = PortAssignment.unique(); + peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port[i]))); + tmpdir[i] = ClientBase.createTmpDir(); + } + + /* + * Start one LEThread for each peer we want to run. + */ + for (int i = 0; i < count; i++) { + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2, 2); + peer.startLeaderElection(); + LEThread thread = new LEThread(this, peer, i, rounds, quora); + thread.start(); + threads.add(thread); + } + LOG.info("Started threads {}", getTestName()); + + int waitCounter = 0; + synchronized (this) { + while (((successCount <= count / 2) || (leader == -1)) && (waitCounter < MAX_LOOP_COUNTER)) { + this.wait(200); + waitCounter++; + } + } + LOG.info("Success count: {}", successCount); + + /* + * Lists what threads haven't joined. A thread doesn't join if + * it hasn't decided upon a leader yet. It can happen that a + * peer is slow or disconnected, and it can take longer to + * nominate and connect to the current leader. + */ + for (int i = 0; i < threads.size(); i++) { + if (threads.get(i).isAlive()) { + LOG.info("Threads didn't join: {}", i); + } + } + + /* + * If we have a majority, then we are good to go. + */ + if (successCount <= count / 2) { + fail("Fewer than a a majority has joined"); + } + + /* + * I'm done so joining. + */ + if (!joinedThreads.contains(leader)) { + fail("Leader hasn't joined: " + leader); + } + } + + /* + * Class to verify of the thread has become a follower + */ + static class VerifyState extends Thread { + + private volatile boolean success = false; + private QuorumPeer peer; + public VerifyState(QuorumPeer peer) { + this.peer = peer; + } + public void run() { + setName("VerifyState-" + peer.getId()); + while (true) { + if (peer.getPeerState() == ServerState.FOLLOWING) { + LOG.info("I am following"); + success = true; + break; + } else if (peer.getPeerState() == ServerState.LEADING) { + LOG.info("I am leading"); + success = false; + break; + } + try { + Thread.sleep(250); + } catch (Exception e) { + LOG.warn("Sleep failed ", e); + } + } + } + public boolean isSuccess() { + return success; + } + + } + + /* + * For ZOOKEEPER-975 verify that a peer joining an established cluster + * does not go in LEADING state. + */ + @Test + public void testJoin() throws Exception { + int sid; + QuorumPeer peer; + int waitTime = 10 * 1000; + ArrayList peerList = new ArrayList(); + for (sid = 0; sid < 3; sid++) { + port[sid] = PortAssignment.unique(); + peers.put(Long.valueOf(sid), new QuorumServer(sid, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port[sid]))); + tmpdir[sid] = ClientBase.createTmpDir(); + } + // start 2 peers and verify if they form the cluster + for (sid = 0; sid < 2; sid++) { + peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 2000, 2, 2, 2); + LOG.info("Starting peer {}", peer.getId()); + peer.start(); + peerList.add(sid, peer); + } + peer = peerList.get(0); + VerifyState v1 = new VerifyState(peerList.get(0)); + v1.start(); + v1.join(waitTime); + assertFalse("Unable to form cluster in " + waitTime + " ms", !v1.isSuccess()); + // Start 3rd peer and check if it goes in LEADING state + peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 2000, 2, 2, 2); + LOG.info("Starting peer {}", peer.getId()); + peer.start(); + peerList.add(sid, peer); + v1 = new VerifyState(peer); + v1.start(); + v1.join(waitTime); + if (v1.isAlive()) { + fail("Peer " + peer.getId() + " failed to join the cluster " + "within " + waitTime + " ms"); + } else if (!v1.isSuccess()) { + fail("Incorrect LEADING state for peer " + peer.getId()); + } + // cleanup + for (int id = 0; id < 3; id++) { + peer = peerList.get(id); + if (peer != null) { + peer.shutdown(); + } + } + } + + /* + * For ZOOKEEPER-1732 verify that it is possible to join an ensemble with + * inconsistent election round information. + */ + @Test + public void testJoinInconsistentEnsemble() throws Exception { + int sid; + QuorumPeer peer; + int waitTime = 10 * 1000; + ArrayList peerList = new ArrayList(); + for (sid = 0; sid < 3; sid++) { + peers.put(Long.valueOf(sid), new QuorumServer(sid, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()))); + tmpdir[sid] = ClientBase.createTmpDir(); + port[sid] = PortAssignment.unique(); + } + // start 2 peers and verify if they form the cluster + for (sid = 0; sid < 2; sid++) { + peer = new QuorumPeer(peers, tmpdir[sid], tmpdir[sid], port[sid], 3, sid, 2000, 2, 2, 2); + LOG.info("Starting peer {}", peer.getId()); + peer.start(); + peerList.add(sid, peer); + } + peer = peerList.get(0); + VerifyState v1 = new VerifyState(peerList.get(0)); + v1.start(); + v1.join(waitTime); + assertFalse("Unable to form cluster in " + waitTime + " ms", !v1.isSuccess()); + // Change the election round for one of the members of the ensemble + long leaderSid = peer.getCurrentVote().getId(); + long zxid = peer.getCurrentVote().getZxid(); + long electionEpoch = peer.getCurrentVote().getElectionEpoch(); + ServerState state = peer.getCurrentVote().getState(); + long peerEpoch = peer.getCurrentVote().getPeerEpoch(); + Vote newVote = new Vote(leaderSid, zxid + 100, electionEpoch + 100, peerEpoch, state); + peer.setCurrentVote(newVote); + // Start 3rd peer and check if it joins the quorum + peer = new QuorumPeer(peers, tmpdir[2], tmpdir[2], port[2], 3, 2, 2000, 2, 2, 2); + LOG.info("Starting peer {}", peer.getId()); + peer.start(); + peerList.add(sid, peer); + v1 = new VerifyState(peer); + v1.start(); + v1.join(waitTime); + if (v1.isAlive()) { + fail("Peer " + peer.getId() + " failed to join the cluster " + "within " + waitTime + " ms"); + } + // cleanup + for (int id = 0; id < 3; id++) { + peer = peerList.get(id); + if (peer != null) { + peer.shutdown(); + } + } + } + + @Test + public void testElectionTimeUnit() throws Exception { + assertEquals("MS", QuorumPeer.FLE_TIME_UNIT); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEZeroWeightTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEZeroWeightTest.java new file mode 100644 index 0000000..29f93a6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FLEZeroWeightTest.java @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.Vote; +import org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FLEZeroWeightTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(HierarchicalQuorumTest.class); + + Properties qp; + + int count; + Map peers; + ArrayList threads; + File[] tmpdir; + int[] port; + + volatile Vote[] votes; + + @Before + public void setUp() throws Exception { + count = 9; + + peers = new HashMap(count); + threads = new ArrayList(count); + votes = new Vote[count]; + tmpdir = new File[count]; + port = new int[count]; + + String config = "group.1=0:1:2\n" + + "group.2=3:4:5\n" + + "group.3=6:7:8\n" + + "weight.0=1\n" + + "weight.1=1\n" + + "weight.2=1\n" + + "weight.3=0\n" + + "weight.4=0\n" + + "weight.5=0\n" + + "weight.6=0\n" + + "weight.7=0\n" + + "weight.8=0"; + + ByteArrayInputStream is = new ByteArrayInputStream(config.getBytes()); + this.qp = new Properties(); + qp.load(is); + } + + @After + public void tearDown() throws Exception { + for (int i = 0; i < threads.size(); i++) { + LEThread leThread = threads.get(i); + // shutdown() has to be explicitly called for every thread to + // make sure that resources are freed properly and all fixed network ports + // are available for other test cases + QuorumBase.shutdown(leThread.peer); + } + } + + class LEThread extends Thread { + + int i; + QuorumPeer peer; + boolean fail; + + LEThread(QuorumPeer peer, int i) { + this.i = i; + this.peer = peer; + LOG.info("Constructor: {}", getName()); + } + + public void run() { + try { + Vote v = null; + fail = false; + while (true) { + + //while(true) { + peer.setPeerState(ServerState.LOOKING); + LOG.info("Going to call leader election."); + v = peer.getElectionAlg().lookForLeader(); + if (v == null) { + LOG.info("Thread {} got a null vote", i); + return; + } + + /* + * A real zookeeper would take care of setting the current vote. Here + * we do it manually. + */ + peer.setCurrentVote(v); + + LOG.info("Finished election: {}, {}", i, v.getId()); + votes[i] = v; + + if ((peer.getPeerState() == ServerState.LEADING) && (peer.getId() > 2)) { + fail = true; + } + + if ((peer.getPeerState() == ServerState.FOLLOWING) || (peer.getPeerState() + == ServerState.LEADING)) { + break; + } + } + LOG.debug("Thread {} votes {}", i, v); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } + + @Test + public void testZeroWeightQuorum() throws Exception { + LOG.info("TestZeroWeightQuorum: {}, {}", getTestName(), count); + for (int i = 0; i < count; i++) { + InetSocketAddress addr1 = new InetSocketAddress("127.0.0.1", PortAssignment.unique()); + InetSocketAddress addr2 = new InetSocketAddress("127.0.0.1", PortAssignment.unique()); + InetSocketAddress addr3 = new InetSocketAddress("127.0.0.1", PortAssignment.unique()); + port[i] = addr3.getPort(); + qp.setProperty("server." + i, "127.0.0.1:" + addr1.getPort() + ":" + addr2.getPort() + ";" + port[i]); + peers.put(Long.valueOf(i), new QuorumServer(i, addr1, addr2, addr3)); + tmpdir[i] = ClientBase.createTmpDir(); + } + + for (int i = 0; i < count; i++) { + QuorumHierarchical hq = new QuorumHierarchical(qp); + QuorumPeer peer = new QuorumPeer(peers, tmpdir[i], tmpdir[i], port[i], 3, i, 1000, 2, 2, 2, hq); + peer.startLeaderElection(); + LEThread thread = new LEThread(peer, i); + thread.start(); + threads.add(thread); + } + LOG.info("Started threads {}", getTestName()); + + for (int i = 0; i < threads.size(); i++) { + threads.get(i).join(15000); + if (threads.get(i).isAlive()) { + fail("Threads didn't join"); + } else { + if (threads.get(i).fail) { + fail("Elected zero-weight server"); + } + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FollowerResyncConcurrencyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FollowerResyncConcurrencyTest.java new file mode 100644 index 0000000..9463b22 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FollowerResyncConcurrencyTest.java @@ -0,0 +1,724 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.Collection; +import java.util.Set; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FollowerResyncConcurrencyTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(FollowerResyncConcurrencyTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + private AtomicInteger counter = new AtomicInteger(0); + private AtomicInteger errors = new AtomicInteger(0); + /** + * Keep track of pending async operations, we shouldn't start verifying + * the state until pending operation is 0 + */ + private AtomicInteger pending = new AtomicInteger(0); + + @Before + public void setUp() throws Exception { + pending.set(0); + errors.set(0); + counter.set(0); + } + + @After + public void tearDown() throws Exception { + LOG.info("Error count {}", errors.get()); + } + + /** + * See ZOOKEEPER-1319 - verify that a lagging follwer resyncs correctly + * + * 1) start with down quorum + * 2) start leader/follower1, add some data + * 3) restart leader/follower1 + * 4) start follower2 + * 5) verify data consistency across the ensemble + * + * @throws Exception + */ + @Test + public void testLaggingFollowerResyncsUnderNewEpoch() throws Exception { + CountdownWatcher watcher1 = new CountdownWatcher(); + CountdownWatcher watcher2 = new CountdownWatcher(); + CountdownWatcher watcher3 = new CountdownWatcher(); + + QuorumUtil qu = new QuorumUtil(1); + qu.shutdownAll(); + + qu.start(1); + qu.start(2); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + ZooKeeper zk1 = createClient(qu.getPeer(1).peer.getClientPort(), watcher1); + LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId())); + + final String resyncPath = "/resyncundernewepoch"; + zk1.create(resyncPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.close(); + + qu.shutdown(1); + qu.shutdown(2); + assertTrue( + "Waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "Waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + qu.start(1); + qu.start(2); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(1).clientPort, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + qu.start(3); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(3).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + zk1 = createClient(qu.getPeer(1).peer.getClientPort(), watcher1); + LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId())); + + assertNotNull("zk1 has data", zk1.exists(resyncPath, false)); + + final ZooKeeper zk2 = createClient(qu.getPeer(2).peer.getClientPort(), watcher2); + LOG.info("zk2 has session id 0x{}", Long.toHexString(zk2.getSessionId())); + + assertNotNull("zk2 has data", zk2.exists(resyncPath, false)); + + final ZooKeeper zk3 = createClient(qu.getPeer(3).peer.getClientPort(), watcher3); + LOG.info("zk3 has session id 0x{}", Long.toHexString(zk3.getSessionId())); + + assertNotNull("zk3 has data", zk3.exists(resyncPath, false)); + + zk1.close(); + zk2.close(); + zk3.close(); + + qu.shutdownAll(); + } + + /** + * See ZOOKEEPER-962. This tests for one of the bugs hit while fixing this, + * setting the ZXID of the SNAP packet + * Starts up 3 ZKs. Shut down F1, write a node, restart the one that was shut down + * The non-leader ZKs are writing to cluster + * Shut down F1 again + * Restart after sessions are expired, expect to get a snap file + * Shut down, run some transactions through. + * Restart to a diff while transactions are running in leader + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + */ + @Test + public void testResyncBySnapThenDiffAfterFollowerCrashes() throws Throwable { + followerResyncCrashTest(false); + } + + /** + * Same as testResyncBySnapThenDiffAfterFollowerCrashes() but we resync + * follower using txnlog + * + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + */ + @Test + public void testResyncByTxnlogThenDiffAfterFollowerCrashes() throws Throwable { + followerResyncCrashTest(true); + } + + public void followerResyncCrashTest(boolean useTxnLogResync) throws Throwable { + final Semaphore sem = new Semaphore(0); + + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + CountdownWatcher watcher1 = new CountdownWatcher(); + CountdownWatcher watcher2 = new CountdownWatcher(); + CountdownWatcher watcher3 = new CountdownWatcher(); + + int index = 1; + while (qu.getPeer(index).peer.leader == null) { + index++; + } + + Leader leader = qu.getPeer(index).peer.leader; + assertNotNull(leader); + + if (useTxnLogResync) { + // Set the factor to high value so that this test case always + // resync using txnlog + qu.getPeer(index).peer.getActiveServer().getZKDatabase().setSnapshotSizeFactor(1000); + } else { + // Disable sending DIFF using txnlog, so that this test still + // testing the ZOOKEEPER-962 bug + qu.getPeer(index).peer.getActiveServer().getZKDatabase().setSnapshotSizeFactor(-1); + } + + /* Reusing the index variable to select a follower to connect to */ + index = (index == 1) ? 2 : 1; + LOG.info("Connecting to follower: {}", index); + + qu.shutdown(index); + + final ZooKeeper zk3 = createClient(qu.getPeer(3).peer.getClientPort(), watcher3); + LOG.info("zk3 has session id 0x{}", Long.toHexString(zk3.getSessionId())); + + zk3.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); + + qu.restart(index); + + final ZooKeeper zk1 = createClient(qu.getPeer(index).peer.getClientPort(), watcher1); + LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId())); + + final ZooKeeper zk2 = createClient(qu.getPeer(index).peer.getClientPort(), watcher2); + LOG.info("zk2 has session id 0x{}", Long.toHexString(zk2.getSessionId())); + + zk1.create("/first", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // Prepare a thread that will create znodes. + Thread mytestfooThread = new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < 3000; i++) { + // Here we create 3000 znodes + zk3.create("/mytestfoo", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new AsyncCallback.StringCallback() { + + @Override + public void processResult(int rc, String path, Object ctx, String name) { + pending.decrementAndGet(); + counter.incrementAndGet(); + if (rc != 0) { + errors.incrementAndGet(); + } + if (counter.get() == 16200) { + sem.release(); + } + } + }, null); + pending.incrementAndGet(); + if (i % 10 == 0) { + try { + Thread.sleep(100); + } catch (Exception e) { + + } + } + } + + } + }); + + // Here we start populating the server and shutdown the follower after + // initial data is written. + for (int i = 0; i < 13000; i++) { + // Here we create 13000 znodes + zk3.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new AsyncCallback.StringCallback() { + + @Override + public void processResult(int rc, String path, Object ctx, String name) { + pending.decrementAndGet(); + counter.incrementAndGet(); + if (rc != 0) { + errors.incrementAndGet(); + } + if (counter.get() == 16200) { + sem.release(); + } + } + }, null); + pending.incrementAndGet(); + + if (i == 5000) { + qu.shutdown(index); + LOG.info("Shutting down s1"); + } + if (i == 12000) { + // Start the prepared thread so that it is writing znodes while + // the follower is restarting. On the first restart, the follow + // should use txnlog to catchup. For subsequent restart, the + // follower should use a diff to catchup. + mytestfooThread.start(); + LOG.info("Restarting follower: {}", index); + qu.restart(index); + Thread.sleep(300); + LOG.info("Shutdown follower: {}", index); + qu.shutdown(index); + Thread.sleep(300); + LOG.info("Restarting follower: {}", index); + qu.restart(index); + LOG.info("Setting up server: {}", index); + } + if ((i % 1000) == 0) { + Thread.sleep(1000); + } + + if (i % 50 == 0) { + zk2.create("/newbaz", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new AsyncCallback.StringCallback() { + @Override + public void processResult(int rc, String path, Object ctx, String name) { + pending.decrementAndGet(); + counter.incrementAndGet(); + if (rc != 0) { + errors.incrementAndGet(); + } + if (counter.get() == 16200) { + sem.release(); + } + } + }, null); + pending.incrementAndGet(); + } + } + + // Wait until all updates return + if (!sem.tryAcquire(ClientBase.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + LOG.warn("Did not aquire semaphore fast enough"); + } + mytestfooThread.join(ClientBase.CONNECTION_TIMEOUT); + if (mytestfooThread.isAlive()) { + LOG.error("mytestfooThread is still alive"); + } + assertTrue(waitForPendingRequests(60)); + assertTrue(waitForSync(qu, index, 10)); + + verifyState(qu, index, leader); + + zk1.close(); + zk2.close(); + zk3.close(); + + qu.shutdownAll(); + } + + /** + * This test: + * Starts up 3 ZKs. The non-leader ZKs are writing to cluster + * Shut down one of the non-leader ZKs. + * Restart after sessions have expired but less than 500 txns have taken place (get a diff) + * Shut down immediately after restarting, start running separate thread with other transactions + * Restart to a diff while transactions are running in leader + * + * + * Before fixes for ZOOKEEPER-962, restarting off of diff could get an inconsistent view of data missing transactions that + * completed during diff syncing. Follower would also be considered "restarted" before all forwarded transactions + * were completely processed, so restarting would cause a snap file with a too-high zxid to be written, and transactions + * would be missed + * + * This test should pretty reliably catch the failure of restarting the server before all diff messages have been processed, + * however, due to the transient nature of the system it may not catch failures due to concurrent processing of transactions + * during the leader's diff forwarding. + * + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + * @throws Throwable + */ + + @Test + public void testResyncByDiffAfterFollowerCrashes() throws IOException, InterruptedException, KeeperException, Throwable { + final Semaphore sem = new Semaphore(0); + + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + CountdownWatcher watcher1 = new CountdownWatcher(); + CountdownWatcher watcher2 = new CountdownWatcher(); + CountdownWatcher watcher3 = new CountdownWatcher(); + + int index = 1; + while (qu.getPeer(index).peer.leader == null) { + index++; + } + + Leader leader = qu.getPeer(index).peer.leader; + assertNotNull(leader); + + /* Reusing the index variable to select a follower to connect to */ + index = (index == 1) ? 2 : 1; + LOG.info("Connecting to follower: {}", index); + + final ZooKeeper zk1 = createClient(qu.getPeer(index).peer.getClientPort(), watcher1); + LOG.info("zk1 has session id 0x{}", Long.toHexString(zk1.getSessionId())); + + final ZooKeeper zk2 = createClient(qu.getPeer(index).peer.getClientPort(), watcher2); + LOG.info("zk2 has session id 0x{}", Long.toHexString(zk2.getSessionId())); + + final ZooKeeper zk3 = createClient(qu.getPeer(3).peer.getClientPort(), watcher3); + LOG.info("zk3 has session id 0x{}", Long.toHexString(zk3.getSessionId())); + + zk1.create("/first", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk2.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); + + final AtomicBoolean runNow = new AtomicBoolean(false); + Thread mytestfooThread = new Thread(new Runnable() { + + @Override + public void run() { + int inSyncCounter = 0; + while (inSyncCounter < 400) { + if (runNow.get()) { + zk3.create("/mytestfoo", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new AsyncCallback.StringCallback() { + + @Override + public void processResult(int rc, String path, Object ctx, String name) { + pending.decrementAndGet(); + counter.incrementAndGet(); + if (rc != 0) { + errors.incrementAndGet(); + } + if (counter.get() > 7300) { + sem.release(); + } + } + }, null); + pending.incrementAndGet(); + try { + Thread.sleep(10); + } catch (Exception e) { + } + inSyncCounter++; + } else { + Thread.yield(); + } + } + + } + }); + + mytestfooThread.start(); + for (int i = 0; i < 5000; i++) { + zk2.create("/mybar", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new AsyncCallback.StringCallback() { + + @Override + public void processResult(int rc, String path, Object ctx, String name) { + pending.decrementAndGet(); + counter.incrementAndGet(); + if (rc != 0) { + errors.incrementAndGet(); + } + if (counter.get() > 7300) { + sem.release(); + } + } + }, null); + pending.incrementAndGet(); + if (i == 1000) { + qu.shutdown(index); + Thread.sleep(1100); + LOG.info("Shutting down s1"); + } + if (i == 1100 || i == 1150 || i == 1200) { + Thread.sleep(1000); + } + + if (i == 1200) { + qu.startThenShutdown(index); + runNow.set(true); + qu.restart(index); + LOG.info("Setting up server: {}", index); + } + + if (i >= 1000 && i % 2 == 0) { + zk3.create("/newbaz", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL, new AsyncCallback.StringCallback() { + + @Override + public void processResult(int rc, String path, Object ctx, String name) { + pending.decrementAndGet(); + counter.incrementAndGet(); + if (rc != 0) { + errors.incrementAndGet(); + } + if (counter.get() > 7300) { + sem.release(); + } + } + }, null); + pending.incrementAndGet(); + } + if (i == 1050 || i == 1100 || i == 1150) { + Thread.sleep(1000); + } + } + + // Wait until all updates return + if (!sem.tryAcquire(ClientBase.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + LOG.warn("Did not aquire semaphore fast enough"); + } + mytestfooThread.join(ClientBase.CONNECTION_TIMEOUT); + if (mytestfooThread.isAlive()) { + LOG.error("mytestfooThread is still alive"); + } + + assertTrue(waitForPendingRequests(60)); + assertTrue(waitForSync(qu, index, 10)); + // Verify that server is following and has the same epoch as the leader + + verifyState(qu, index, leader); + + zk1.close(); + zk2.close(); + zk3.close(); + + qu.shutdownAll(); + } + + private static DisconnectableZooKeeper createClient(int port, CountdownWatcher watcher) throws IOException, TimeoutException, InterruptedException { + DisconnectableZooKeeper zk = new DisconnectableZooKeeper( + "127.0.0.1:" + port, + ClientBase.CONNECTION_TIMEOUT, + watcher); + + watcher.waitForConnected(CONNECTION_TIMEOUT); + return zk; + } + + /** + * Wait for all async operation to return. So we know that we can start + * verifying the state + */ + private boolean waitForPendingRequests(int timeout) throws InterruptedException { + LOG.info("Wait for pending requests: {}", pending.get()); + for (int i = 0; i < timeout; ++i) { + Thread.sleep(1000); + if (pending.get() == 0) { + return true; + } + } + LOG.info("Timeout waiting for pending requests: {}", pending.get()); + return false; + } + + /** + * Wait for all server to have the same lastProccessedZxid. Timeout in seconds + */ + private boolean waitForSync(QuorumUtil qu, int index, int timeout) throws InterruptedException { + LOG.info("Wait for server to sync"); + int leaderIndex = (index == 1) ? 2 : 1; + ZKDatabase restartedDb = qu.getPeer(index).peer.getActiveServer().getZKDatabase(); + ZKDatabase cleanDb = qu.getPeer(3).peer.getActiveServer().getZKDatabase(); + ZKDatabase leadDb = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase(); + long leadZxid = 0; + long cleanZxid = 0; + long restartedZxid = 0; + for (int i = 0; i < timeout; ++i) { + leadZxid = leadDb.getDataTreeLastProcessedZxid(); + cleanZxid = cleanDb.getDataTreeLastProcessedZxid(); + restartedZxid = restartedDb.getDataTreeLastProcessedZxid(); + if (leadZxid == cleanZxid && leadZxid == restartedZxid) { + return true; + } + Thread.sleep(1000); + } + LOG.info( + "Timeout waiting for zxid to sync: leader 0x{} clean 0x{} restarted 0x{}", + Long.toHexString(leadZxid), + Long.toHexString(cleanZxid), + Long.toHexString(restartedZxid)); + return false; + } + + private static TestableZooKeeper createTestableClient(String hp) throws IOException, TimeoutException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createTestableClient(watcher, hp); + } + + private static TestableZooKeeper createTestableClient( + CountdownWatcher watcher, String hp) throws IOException, TimeoutException, InterruptedException { + TestableZooKeeper zk = new TestableZooKeeper(hp, ClientBase.CONNECTION_TIMEOUT, watcher); + + watcher.waitForConnected(CONNECTION_TIMEOUT); + return zk; + } + + private void verifyState(QuorumUtil qu, int index, Leader leader) { + LOG.info("Verifying state"); + assertTrue("Not following", qu.getPeer(index).peer.follower != null); + long epochF = (qu.getPeer(index).peer.getActiveServer().getZxid() >> 32L); + long epochL = (leader.getEpoch() >> 32L); + assertTrue("Zxid: " + + qu.getPeer(index).peer.getActiveServer().getZKDatabase().getDataTreeLastProcessedZxid() + + "Current epoch: " + + epochF, epochF == epochL); + int leaderIndex = (index == 1) ? 2 : 1; + Collection sessionsRestarted = qu.getPeer(index).peer.getActiveServer().getZKDatabase().getSessions(); + Collection sessionsNotRestarted = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase().getSessions(); + + for (Long l : sessionsRestarted) { + assertTrue("Should have same set of sessions in both servers, did not expect: " + + l, sessionsNotRestarted.contains(l)); + } + assertEquals("Should have same number of sessions", sessionsNotRestarted.size(), sessionsRestarted.size()); + ZKDatabase restarted = qu.getPeer(index).peer.getActiveServer().getZKDatabase(); + ZKDatabase clean = qu.getPeer(3).peer.getActiveServer().getZKDatabase(); + ZKDatabase lead = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase(); + for (Long l : sessionsRestarted) { + LOG.info("Validating ephemeral for session id 0x{}", Long.toHexString(l)); + assertTrue("Should have same set of sessions in both servers, did not expect: " + + l, sessionsNotRestarted.contains(l)); + Set ephemerals = restarted.getEphemerals(l); + Set cleanEphemerals = clean.getEphemerals(l); + for (String o : cleanEphemerals) { + if (!ephemerals.contains(o)) { + LOG.info("Restarted follower doesn't contain ephemeral {} zxid 0x{}", o, Long.toHexString(clean.getDataTree().getNode(o).stat.getMzxid())); + } + } + for (String o : ephemerals) { + if (!cleanEphemerals.contains(o)) { + LOG.info("Restarted follower has extra ephemeral {} zxid 0x{}", o, Long.toHexString(restarted.getDataTree().getNode(o).stat.getMzxid())); + } + } + Set leadEphemerals = lead.getEphemerals(l); + for (String o : leadEphemerals) { + if (!cleanEphemerals.contains(o)) { + LOG.info("Follower doesn't contain ephemeral from leader {} zxid 0x{}", o, Long.toHexString(lead.getDataTree().getNode(o).stat.getMzxid())); + } + } + for (String o : cleanEphemerals) { + if (!leadEphemerals.contains(o)) { + LOG.info("Leader doesn't contain ephemeral from follower {} zxid 0x{}", o, Long.toHexString(clean.getDataTree().getNode(o).stat.getMzxid())); + } + } + assertEquals("Should have same number of ephemerals in both followers", ephemerals.size(), cleanEphemerals.size()); + assertEquals("Leader should equal follower", lead.getEphemerals(l).size(), cleanEphemerals.size()); + } + } + + /** + * Verify that the server is sending the proper zxid. See ZOOKEEPER-1412. + */ + @Test + public void testFollowerSendsLastZxid() throws Exception { + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + + int index = 1; + while (qu.getPeer(index).peer.follower == null) { + index++; + } + LOG.info("Connecting to follower: {}", index); + + TestableZooKeeper zk = createTestableClient("localhost:" + qu.getPeer(index).peer.getClientPort()); + + assertEquals(0L, zk.testableLastZxid()); + zk.exists("/", false); + long lzxid = zk.testableLastZxid(); + assertTrue("lzxid:" + lzxid + " > 0", lzxid > 0); + zk.close(); + qu.shutdownAll(); + } + + private class MyWatcher extends CountdownWatcher { + + LinkedBlockingQueue events = new LinkedBlockingQueue(); + + public void process(WatchedEvent event) { + super.process(event); + if (event.getType() != Event.EventType.None) { + try { + events.put(event); + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt during event.put"); + } + } + } + + } + + /** + * Verify that the server is sending the proper zxid, and as a result + * the watch doesn't fire. See ZOOKEEPER-1412. + */ + @Test + public void testFollowerWatcherResync() throws Exception { + QuorumUtil qu = new QuorumUtil(1); + qu.startAll(); + + int index = 1; + while (qu.getPeer(index).peer.follower == null) { + index++; + } + LOG.info("Connecting to follower: {}", index); + + TestableZooKeeper zk1 = createTestableClient("localhost:" + qu.getPeer(index).peer.getClientPort()); + zk1.create("/foo", "foo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + MyWatcher watcher = new MyWatcher(); + TestableZooKeeper zk2 = createTestableClient(watcher, "localhost:" + qu.getPeer(index).peer.getClientPort()); + + zk2.exists("/foo", true); + + watcher.reset(); + zk2.testableConnloss(); + if (!watcher.clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("Unable to connect to server"); + } + assertArrayEquals("foo".getBytes(), zk2.getData("/foo", false, null)); + + assertNull(watcher.events.poll(5, TimeUnit.SECONDS)); + + zk1.close(); + zk2.close(); + qu.shutdownAll(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsQuorumTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsQuorumTest.java new file mode 100644 index 0000000..747cb1c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsQuorumTest.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FourLetterWordsQuorumTest extends QuorumBase { + + protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordsQuorumTest.class); + + /** Test the various four letter words */ + @Test + public void testFourLetterWords() throws Exception { + String[] servers = hostPort.split(","); + for (String hp : servers) { + verify(hp, "ruok", "imok"); + verify(hp, "envi", "java.version"); + verify(hp, "conf", "clientPort"); + verify(hp, "stat", "Outstanding"); + verify(hp, "srvr", "Outstanding"); + verify(hp, "cons", "queued"); + verify(hp, "dump", "Session"); + verify(hp, "wchs", "watches"); + verify(hp, "wchp", ""); + verify(hp, "wchc", ""); + + verify(hp, "srst", "reset"); + verify(hp, "crst", "reset"); + + verify(hp, "stat", "Outstanding"); + verify(hp, "srvr", "Outstanding"); + verify(hp, "cons", "queued"); + + TestableZooKeeper zk = createClient(hp); + String sid = getHexSessionId(zk.getSessionId()); + + verify(hp, "stat", "queued"); + verify(hp, "srvr", "Outstanding"); + verify(hp, "cons", sid); + verify(hp, "dump", sid); + verify(hp, "dirs", "size"); + + zk.getData("/", true, null); + + verify(hp, "stat", "queued"); + verify(hp, "srvr", "Outstanding"); + verify(hp, "cons", sid); + verify(hp, "dump", sid); + verify(hp, "wchs", "watching 1"); + verify(hp, "wchp", sid); + verify(hp, "wchc", sid); + verify(hp, "dirs", "size"); + + zk.close(); + + verify(hp, "ruok", "imok"); + verify(hp, "envi", "java.version"); + verify(hp, "conf", "clientPort"); + verify(hp, "stat", "Outstanding"); + verify(hp, "srvr", "Outstanding"); + verify(hp, "cons", "queued"); + verify(hp, "dump", "Session"); + verify(hp, "wchs", "watch"); + verify(hp, "wchp", ""); + verify(hp, "wchc", ""); + verify(hp, "dirs", "size"); + + verify(hp, "srst", "reset"); + verify(hp, "crst", "reset"); + + verify(hp, "stat", "Outstanding"); + verify(hp, "srvr", "Outstanding"); + verify(hp, "cons", "queued"); + + verify(hp, "mntr", "zk_version\t"); + } + } + + private void verify(String hp, String cmd, String expected) throws IOException, SSLContextException { + for (HostPort hpobj : parseHostPortList(hp)) { + String resp = send4LetterWord(hpobj.host, hpobj.port, cmd); + LOG.info("cmd {} expected {} got {}", cmd, expected, resp); + if (cmd.equals("dump")) { + assertTrue(resp.contains(expected) || resp.contains("Sessions with Ephemerals")); + } else { + assertTrue(resp.contains(expected)); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsTest.java new file mode 100644 index 0000000..a48f4bc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsTest.java @@ -0,0 +1,245 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.StringReader; +import java.util.regex.Pattern; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.IOUtils; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.Timeout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FourLetterWordsTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordsTest.class); + + @Rule + public Timeout timeout = Timeout.millis(30000); + + /** Test the various four letter words */ + @Test + public void testFourLetterWords() throws Exception { + verify("ruok", "imok"); + verify("envi", "java.version"); + verify("conf", "clientPort"); + verify("stat", "Outstanding"); + verify("srvr", "Outstanding"); + verify("cons", "queued"); + verify("dump", "Session"); + verify("wchs", "watches"); + verify("wchp", ""); + verify("wchc", ""); + + verify("srst", "reset"); + verify("crst", "reset"); + + verify("stat", "Outstanding"); + verify("srvr", "Outstanding"); + verify("cons", "queued"); + verify("gtmk", "306"); + verify("isro", "rw"); + + TestableZooKeeper zk = createClient(); + String sid = getHexSessionId(zk.getSessionId()); + + verify("stat", "queued"); + verify("srvr", "Outstanding"); + verify("cons", sid); + verify("dump", sid); + verify("dirs", "size"); + + zk.getData("/", true, null); + + verify("stat", "queued"); + verify("srvr", "Outstanding"); + verify("cons", sid); + verify("dump", sid); + + verify("wchs", "watching 1"); + verify("wchp", sid); + verify("wchc", sid); + verify("dirs", "size"); + zk.close(); + + verify("ruok", "imok"); + verify("envi", "java.version"); + verify("conf", "clientPort"); + verify("stat", "Outstanding"); + verify("srvr", "Outstanding"); + verify("cons", "queued"); + verify("dump", "Session"); + verify("wchs", "watch"); + verify("wchp", ""); + verify("wchc", ""); + + verify("srst", "reset"); + verify("crst", "reset"); + + verify("stat", "Outstanding"); + verify("srvr", "Outstanding"); + verify("cons", "queued"); + verify("mntr", "zk_server_state\tstandalone"); + verify("mntr", "num_alive_connections"); + verify("stat", "Connections"); + verify("srvr", "Connections"); + verify("dirs", "size"); + } + + private String sendRequest(String cmd) throws IOException, SSLContextException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd); + } + private String sendRequest(String cmd, int timeout) throws IOException, SSLContextException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd, false, timeout); + } + + private void verify(String cmd, String expected) throws IOException, SSLContextException { + String resp = sendRequest(cmd); + LOG.info("cmd {} expected {} got {}", cmd, expected, resp); + assertTrue(resp.contains(expected)); + } + + @Test + public void testValidateStatOutput() throws Exception { + ZooKeeper zk1 = createClient(); + ZooKeeper zk2 = createClient(); + + String resp = sendRequest("stat"); + BufferedReader in = new BufferedReader(new StringReader(resp)); + + String line; + // first line should be version info + line = in.readLine(); + assertTrue(Pattern.matches("^.*\\s\\d+\\.\\d+\\.\\d+-.*$", line)); + assertTrue(Pattern.matches("^Clients:$", in.readLine())); + + int count = 0; + while ((line = in.readLine()).length() > 0) { + count++; + assertTrue(Pattern.matches("^ /.*:\\d+\\[\\d+\\]\\(queued=\\d+,recved=\\d+,sent=\\d+\\)$", line)); + } + // ensure at least the two clients we created are accounted for + assertTrue(count >= 2); + + line = in.readLine(); + assertTrue(Pattern.matches("^Latency min/avg/max: \\d+/-?[0-9]*.?[0-9]*/\\d+$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Received: \\d+$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Sent: \\d+$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Connections: \\d+$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Outstanding: \\d+$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Zxid: 0x[\\da-fA-F]+$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Mode: .*$", line)); + line = in.readLine(); + assertTrue(Pattern.matches("^Node count: \\d+$", line)); + + zk1.close(); + zk2.close(); + } + + @Test + public void testValidateConsOutput() throws Exception { + ZooKeeper zk1 = createClient(); + ZooKeeper zk2 = createClient(); + + String resp = sendRequest("cons"); + BufferedReader in = new BufferedReader(new StringReader(resp)); + + String line; + int count = 0; + while ((line = in.readLine()) != null && line.length() > 0) { + count++; + assertTrue(line, Pattern.matches("^ /.*:\\d+\\[\\d+\\]\\(queued=\\d+,recved=\\d+,sent=\\d+.*\\)$", line)); + } + // ensure at least the two clients we created are accounted for + assertTrue(count >= 2); + + zk1.close(); + zk2.close(); + } + + @Test(timeout = 60000) + public void testValidateSocketTimeout() throws Exception { + /** + * testing positive scenario that even with timeout parameter the + * functionality works fine + */ + String resp = sendRequest("isro", 2000); + assertTrue(resp.contains("rw")); + } + + @Test + public void testSetTraceMask() throws Exception { + String gtmkResp = sendRequest("gtmk"); + assertNotNull(gtmkResp); + gtmkResp = gtmkResp.trim(); + assertFalse(gtmkResp.isEmpty()); + long formerMask = Long.valueOf(gtmkResp); + try { + verify(buildSetTraceMaskRequest(0), "0"); + verify("gtmk", "0"); + } finally { + // Restore former value. + sendRequest(buildSetTraceMaskRequest(formerMask)); + } + } + + /** + * Builds a SetTraceMask request to be sent to the server, consisting of + * "stmk" followed by the 8-byte long representation of the trace mask. + * + * @param mask trace mask to set + * @return built request + * @throws IOException if there is an I/O error + */ + private String buildSetTraceMaskRequest(long mask) throws IOException { + ByteArrayOutputStream baos = null; + DataOutputStream dos = null; + try { + baos = new ByteArrayOutputStream(); + dos = new DataOutputStream(baos); + dos.writeBytes("stmk"); + dos.writeLong(mask); + } finally { + IOUtils.closeStream(dos); + IOUtils.closeStream(baos); + } + return new String(baos.toByteArray()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsWhiteListTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsWhiteListTest.java new file mode 100644 index 0000000..8fb0a72 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/FourLetterWordsWhiteListTest.java @@ -0,0 +1,247 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.FourLetterWordMain.send4LetterWord; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.common.X509Exception.SSLContextException; +import org.apache.zookeeper.server.command.FourLetterCommands; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FourLetterWordsWhiteListTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(FourLetterWordsWhiteListTest.class); + + /* + * ZOOKEEPER-2693: test white list of four letter words. + * For 3.5.x default white list is empty. Verify that is + * the case (except 'stat' command which is enabled in ClientBase + * which other tests depend on.). + */ + @Test(timeout = 30000) + public void testFourLetterWordsAllDisabledByDefault() throws Exception { + stopServer(); + FourLetterCommands.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "stat"); + startServer(); + + // Default white list for 3.5.x is empty, so all command should fail. + verifyAllCommandsFail(); + + TestableZooKeeper zk = createClient(); + + verifyAllCommandsFail(); + + zk.getData("/", true, null); + + verifyAllCommandsFail(); + + zk.close(); + + verifyFuzzyMatch("stat", "Outstanding"); + verifyAllCommandsFail(); + } + + @Test(timeout = 30000) + public void testFourLetterWordsEnableSomeCommands() throws Exception { + stopServer(); + FourLetterCommands.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "stat, ruok, isro"); + startServer(); + // stat, ruok and isro are white listed. + verifyFuzzyMatch("stat", "Outstanding"); + verifyExactMatch("ruok", "imok"); + verifyExactMatch("isro", "rw"); + + // Rest of commands fail. + verifyExactMatch("conf", generateExpectedMessage("conf")); + verifyExactMatch("cons", generateExpectedMessage("cons")); + verifyExactMatch("crst", generateExpectedMessage("crst")); + verifyExactMatch("dirs", generateExpectedMessage("dirs")); + verifyExactMatch("dump", generateExpectedMessage("dump")); + verifyExactMatch("envi", generateExpectedMessage("envi")); + verifyExactMatch("gtmk", generateExpectedMessage("gtmk")); + verifyExactMatch("stmk", generateExpectedMessage("stmk")); + verifyExactMatch("srst", generateExpectedMessage("srst")); + verifyExactMatch("wchc", generateExpectedMessage("wchc")); + verifyExactMatch("wchp", generateExpectedMessage("wchp")); + verifyExactMatch("wchs", generateExpectedMessage("wchs")); + verifyExactMatch("mntr", generateExpectedMessage("mntr")); + } + + @Test(timeout = 30000) + public void testISROEnabledWhenReadOnlyModeEnabled() throws Exception { + stopServer(); + FourLetterCommands.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "stat"); + System.setProperty("readonlymode.enabled", "true"); + startServer(); + verifyExactMatch("isro", "rw"); + System.clearProperty("readonlymode.enabled"); + } + + @Test(timeout = 30000) + public void testFourLetterWordsInvalidConfiguration() throws Exception { + stopServer(); + FourLetterCommands.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "foo bar" + + " foo,,, " + + "bar :.,@#$%^&*() , , , , bar, bar, stat, "); + startServer(); + + // Just make sure we are good when admin made some mistakes in config file. + verifyAllCommandsFail(); + // But still, what's valid in white list will get through. + verifyFuzzyMatch("stat", "Outstanding"); + } + + @Test(timeout = 30000) + public void testFourLetterWordsEnableAllCommandsThroughAsterisk() throws Exception { + stopServer(); + FourLetterCommands.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "*"); + startServer(); + verifyAllCommandsSuccess(); + } + + @Test(timeout = 30000) + public void testFourLetterWordsEnableAllCommandsThroughExplicitList() throws Exception { + stopServer(); + FourLetterCommands.resetWhiteList(); + System.setProperty("zookeeper.4lw.commands.whitelist", "ruok, envi, conf, stat, srvr, cons, dump," + + "wchs, wchp, wchc, srst, crst, " + + "dirs, mntr, gtmk, isro, stmk"); + startServer(); + verifyAllCommandsSuccess(); + } + + private void verifyAllCommandsSuccess() throws Exception { + verifyExactMatch("ruok", "imok"); + verifyFuzzyMatch("envi", "java.version"); + verifyFuzzyMatch("conf", "clientPort"); + verifyFuzzyMatch("stat", "Outstanding"); + verifyFuzzyMatch("srvr", "Outstanding"); + verifyFuzzyMatch("cons", "queued"); + verifyFuzzyMatch("dump", "Session"); + verifyFuzzyMatch("wchs", "watches"); + verifyFuzzyMatch("wchp", ""); + verifyFuzzyMatch("wchc", ""); + + verifyFuzzyMatch("srst", "reset"); + verifyFuzzyMatch("crst", "reset"); + + verifyFuzzyMatch("stat", "Outstanding"); + verifyFuzzyMatch("srvr", "Outstanding"); + verifyFuzzyMatch("cons", "queued"); + verifyFuzzyMatch("gtmk", "306"); + verifyFuzzyMatch("isro", "rw"); + + TestableZooKeeper zk = createClient(); + String sid = getHexSessionId(zk.getSessionId()); + + verifyFuzzyMatch("stat", "queued"); + verifyFuzzyMatch("srvr", "Outstanding"); + verifyFuzzyMatch("cons", sid); + verifyFuzzyMatch("dump", sid); + verifyFuzzyMatch("dirs", "size"); + + zk.getData("/", true, null); + + verifyFuzzyMatch("stat", "queued"); + verifyFuzzyMatch("srvr", "Outstanding"); + verifyFuzzyMatch("cons", sid); + verifyFuzzyMatch("dump", sid); + + verifyFuzzyMatch("wchs", "watching 1"); + verifyFuzzyMatch("wchp", sid); + verifyFuzzyMatch("wchc", sid); + verifyFuzzyMatch("dirs", "size"); + zk.close(); + + verifyExactMatch("ruok", "imok"); + verifyFuzzyMatch("envi", "java.version"); + verifyFuzzyMatch("conf", "clientPort"); + verifyFuzzyMatch("stat", "Outstanding"); + verifyFuzzyMatch("srvr", "Outstanding"); + verifyFuzzyMatch("cons", "queued"); + verifyFuzzyMatch("dump", "Session"); + verifyFuzzyMatch("wchs", "watch"); + verifyFuzzyMatch("wchp", ""); + verifyFuzzyMatch("wchc", ""); + + verifyFuzzyMatch("srst", "reset"); + verifyFuzzyMatch("crst", "reset"); + + verifyFuzzyMatch("stat", "Outstanding"); + verifyFuzzyMatch("srvr", "Outstanding"); + verifyFuzzyMatch("cons", "queued"); + verifyFuzzyMatch("mntr", "zk_server_state\tstandalone"); + verifyFuzzyMatch("mntr", "num_alive_connections"); + verifyFuzzyMatch("stat", "Connections"); + verifyFuzzyMatch("srvr", "Connections"); + verifyFuzzyMatch("dirs", "size"); + } + + private void verifyAllCommandsFail() throws Exception { + verifyExactMatch("ruok", generateExpectedMessage("ruok")); + verifyExactMatch("conf", generateExpectedMessage("conf")); + verifyExactMatch("cons", generateExpectedMessage("cons")); + verifyExactMatch("crst", generateExpectedMessage("crst")); + verifyExactMatch("dirs", generateExpectedMessage("dirs")); + verifyExactMatch("dump", generateExpectedMessage("dump")); + verifyExactMatch("envi", generateExpectedMessage("envi")); + verifyExactMatch("gtmk", generateExpectedMessage("gtmk")); + verifyExactMatch("stmk", generateExpectedMessage("stmk")); + verifyExactMatch("srst", generateExpectedMessage("srst")); + verifyExactMatch("wchc", generateExpectedMessage("wchc")); + verifyExactMatch("wchp", generateExpectedMessage("wchp")); + verifyExactMatch("wchs", generateExpectedMessage("wchs")); + verifyExactMatch("mntr", generateExpectedMessage("mntr")); + verifyExactMatch("isro", generateExpectedMessage("isro")); + + // srvr is enabled by default due to the sad fact zkServer.sh uses it. + verifyFuzzyMatch("srvr", "Outstanding"); + } + + private String sendRequest(String cmd) throws IOException, SSLContextException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd); + } + + private void verifyFuzzyMatch(String cmd, String expected) throws IOException, SSLContextException { + String resp = sendRequest(cmd); + LOG.info("cmd {} expected {} got {}", cmd, expected, resp); + assertTrue(resp.contains(expected)); + } + + private String generateExpectedMessage(String command) { + return command + " is not executed because it is not in the whitelist."; + } + + private void verifyExactMatch(String cmd, String expected) throws IOException, SSLContextException { + String resp = sendRequest(cmd); + LOG.info("cmd {} expected an exact match of {}; got {}", cmd, expected, resp); + assertTrue(resp.trim().equals(expected)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/GetChildren2Test.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/GetChildren2Test.java new file mode 100644 index 0000000..1d8c0fd --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/GetChildren2Test.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.Test; + +public class GetChildren2Test extends ClientBase { + + private ZooKeeper zk; + + @Override + public void setUp() throws Exception { + super.setUp(); + + zk = createClient(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + + zk.close(); + } + + @Test + public void testChild() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + String childname = name + "/bar"; + zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + Stat stat = new Stat(); + List s = zk.getChildren(name, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid() + 1, stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(1, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(1, stat.getNumChildren()); + assertEquals(s.size(), stat.getNumChildren()); + + s = zk.getChildren(childname, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); + assertEquals(childname.length(), stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + assertEquals(s.size(), stat.getNumChildren()); + } + + @Test + public void testChildren() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + List children = new ArrayList(); + List children_s = new ArrayList(); + + for (int i = 0; i < 10; i++) { + String childname = name + "/bar" + i; + String childname_s = "bar" + i; + children.add(childname); + children_s.add(childname_s); + } + + for (int i = 0; i < children.size(); i++) { + String childname = children.get(i); + zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + Stat stat = new Stat(); + List s = zk.getChildren(name, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid() + i + 1, stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(i + 1, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(i + 1, stat.getNumChildren()); + assertEquals(s.size(), stat.getNumChildren()); + } + List p = zk.getChildren(name, false, null); + List c_a = children_s; + List c_b = p; + Collections.sort(c_a); + Collections.sort(c_b); + assertEquals(c_a.size(), 10); + assertEquals(c_a, c_b); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/GetProposalFromTxnTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/GetProposalFromTxnTest.java new file mode 100644 index 0000000..207caf5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/GetProposalFromTxnTest.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.TxnLogEntry; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.util.SerializeUtils; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; + +/** + * Test loading committed proposal from txnlog. Learner uses these proposals to + * catch-up with leader + */ +public class GetProposalFromTxnTest extends ZKTestCase { + + private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + + private static final int MSG_COUNT = 2000; + + /** + * Test loading proposal from txnlog + * + * @throws Exception + * an exception might be thrown here + */ + @Test + public void testGetProposalFromTxn() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + SyncRequestProcessor.setSnapCount(100); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + + // Generate transaction so we will have some txnlog + Long[] zxids = new Long[MSG_COUNT]; + try { + String data = "data"; + byte[] bytes = data.getBytes(); + for (int i = 0; i < MSG_COUNT; i++) { + Stat stat = new Stat(); + zk.create("/invalidsnap-" + i, bytes, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.getData("/invalidsnap-" + i, null, stat); + zxids[i] = stat.getCzxid(); + } + + } finally { + zk.close(); + } + + // shutdown and start zookeeper again + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + zks.startdata(); + + ZKDatabase db = zks.getZKDatabase(); + + // Set sizeLimit to be very high number, so we can pull all transactions + // from txnlog + Iterator itr = db.getProposalsFromTxnLog(zxids[0], 10000000); + + int createCount = 0; + ArrayList retrievedZxids = new ArrayList(MSG_COUNT); + + // Get zxid of create requests + while (itr.hasNext()) { + Proposal proposal = itr.next(); + TxnLogEntry logEntry = SerializeUtils.deserializeTxn( + proposal.packet.getData()); + TxnHeader hdr = logEntry.getHeader(); + Record rec = logEntry.getTxn(); + if (hdr.getType() == OpCode.create) { + retrievedZxids.add(hdr.getZxid()); + createCount++; + } + } + + // All zxid should match what we created + assertTrue("Zxids missmatches", Arrays.equals(zxids, retrievedZxids.toArray(new Long[0]))); + + // There should be 2000 create requests + assertTrue("create proposal count == " + MSG_COUNT, (createCount == MSG_COUNT)); + + // We are requesting half the number of transaction from the snapshot + // this should exceed threshold (ZKDatabase.snapshotSizeFactor) + db.setSnapshotSizeFactor(0.33); + long sizeLimit = db.calculateTxnLogSizeLimit(); + + itr = db.getProposalsFromTxnLog(zxids[MSG_COUNT / 2], sizeLimit); + assertFalse("Expect empty proposal", (itr.hasNext())); + f.shutdown(); + zks.shutdown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/HierarchicalQuorumTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/HierarchicalQuorumTest.java new file mode 100644 index 0000000..26a963f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/HierarchicalQuorumTest.java @@ -0,0 +1,299 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Properties; +import java.util.Set; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +public class HierarchicalQuorumTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumBase.class); + + File s1dir, s2dir, s3dir, s4dir, s5dir; + QuorumPeer s1, s2, s3, s4, s5; + protected int port1; + protected int port2; + protected int port3; + protected int port4; + protected int port5; + + protected int leport1; + protected int leport2; + protected int leport3; + protected int leport4; + protected int leport5; + + protected int clientport1; + protected int clientport2; + protected int clientport3; + protected int clientport4; + protected int clientport5; + + Properties qp; + protected final ClientHammerTest cht = new ClientHammerTest(); + + @Override + public void setUp() throws Exception { + setupTestEnv(); + + JMXEnv.setUp(); + + setUpAll(); + + port1 = PortAssignment.unique(); + port2 = PortAssignment.unique(); + port3 = PortAssignment.unique(); + port4 = PortAssignment.unique(); + port5 = PortAssignment.unique(); + leport1 = PortAssignment.unique(); + leport2 = PortAssignment.unique(); + leport3 = PortAssignment.unique(); + leport4 = PortAssignment.unique(); + leport5 = PortAssignment.unique(); + clientport1 = PortAssignment.unique(); + clientport2 = PortAssignment.unique(); + clientport3 = PortAssignment.unique(); + clientport4 = PortAssignment.unique(); + clientport5 = PortAssignment.unique(); + + hostPort = "127.0.0.1:" + clientport1 + + ",127.0.0.1:" + clientport2 + + ",127.0.0.1:" + clientport3 + + ",127.0.0.1:" + clientport4 + + ",127.0.0.1:" + clientport5; + LOG.info("Ports are: {}", hostPort); + + s1dir = ClientBase.createTmpDir(); + s2dir = ClientBase.createTmpDir(); + s3dir = ClientBase.createTmpDir(); + s4dir = ClientBase.createTmpDir(); + s5dir = ClientBase.createTmpDir(); + + String config = "group.1=1:2:3\n" + + "group.2=4:5\n" + + "weight.1=1\n" + + "weight.2=1\n" + + "weight.3=1\n" + + "weight.4=0\n" + + "weight.5=0\n" + + "server.1=127.0.0.1:" + port1 + ":" + leport1 + ";" + clientport1 + + "\n" + "server.2=127.0.0.1:" + port2 + ":" + leport2 + ";" + clientport2 + + "\n" + "server.3=127.0.0.1:" + port3 + ":" + leport3 + ";" + clientport3 + + "\n" + "server.4=127.0.0.1:" + port4 + ":" + leport4 + ";" + clientport4 + + "\n" + "server.5=127.0.0.1:" + port5 + ":" + leport5 + ";" + clientport5 + + "\n"; + + ByteArrayInputStream is = new ByteArrayInputStream(config.getBytes()); + this.qp = new Properties(); + + qp.load(is); + startServers(); + + cht.hostPort = hostPort; + cht.setUpAll(); + + LOG.info("Setup finished"); + } + + /** + * This method is here to keep backwards compatibility with the test code + * written before observers. + * @throws Exception + */ + void startServers() throws Exception { + startServers(false); + } + + /** + * Starts 5 Learners. When withObservers == false, all 5 are Followers. + * When withObservers == true, 3 are Followers and 2 Observers. + * @param withObservers + * @throws Exception + */ + void startServers(boolean withObservers) throws Exception { + int tickTime = 2000; + int initLimit = 3; + int syncLimit = 3; + int connectToLearnerMasterLimit = 3; + HashMap peers = new HashMap<>(); + + peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", port1), new InetSocketAddress("127.0.0.1", leport1), new InetSocketAddress("127.0.0.1", clientport1))); + peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", port2), new InetSocketAddress("127.0.0.1", leport2), new InetSocketAddress("127.0.0.1", clientport2))); + peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", port3), new InetSocketAddress("127.0.0.1", leport3), new InetSocketAddress("127.0.0.1", clientport3))); + peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress("127.0.0.1", port4), new InetSocketAddress("127.0.0.1", leport4), new InetSocketAddress("127.0.0.1", clientport4), withObservers ? QuorumPeer.LearnerType.OBSERVER : QuorumPeer.LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress("127.0.0.1", port5), new InetSocketAddress("127.0.0.1", leport5), new InetSocketAddress("127.0.0.1", clientport5), withObservers ? QuorumPeer.LearnerType.OBSERVER : QuorumPeer.LearnerType.PARTICIPANT)); + + LOG.info("creating QuorumPeer 1 port {}", clientport1); + + if (withObservers) { + qp.setProperty("server.4", "127.0.0.1:" + port4 + ":" + leport4 + ":observer" + ";" + clientport4); + qp.setProperty("server.5", "127.0.0.1:" + port5 + ":" + leport5 + ":observer" + ";" + clientport5); + } + QuorumHierarchical hq1 = new QuorumHierarchical(qp); + s1 = new QuorumPeer(peers, s1dir, s1dir, clientport1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, hq1); + assertEquals(clientport1, s1.getClientPort()); + + LOG.info("creating QuorumPeer 2 port {}", clientport2); + QuorumHierarchical hq2 = new QuorumHierarchical(qp); + s2 = new QuorumPeer(peers, s2dir, s2dir, clientport2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, hq2); + assertEquals(clientport2, s2.getClientPort()); + + LOG.info("creating QuorumPeer 3 port {}", clientport3); + QuorumHierarchical hq3 = new QuorumHierarchical(qp); + s3 = new QuorumPeer(peers, s3dir, s3dir, clientport3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, hq3); + assertEquals(clientport3, s3.getClientPort()); + + LOG.info("creating QuorumPeer 4 port {}", clientport4); + QuorumHierarchical hq4 = new QuorumHierarchical(qp); + s4 = new QuorumPeer(peers, s4dir, s4dir, clientport4, 3, 4, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, hq4); + if (withObservers) { + s4.setLearnerType(QuorumPeer.LearnerType.OBSERVER); + } + assertEquals(clientport4, s4.getClientPort()); + + LOG.info("creating QuorumPeer 5 port {}", clientport5); + QuorumHierarchical hq5 = new QuorumHierarchical(qp); + s5 = new QuorumPeer(peers, s5dir, s5dir, clientport5, 3, 5, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit, hq5); + if (withObservers) { + s5.setLearnerType(QuorumPeer.LearnerType.OBSERVER); + } + assertEquals(clientport5, s5.getClientPort()); + + LOG.info("start QuorumPeer 1"); + s1.start(); + LOG.info("start QuorumPeer 2"); + s2.start(); + LOG.info("start QuorumPeer 3"); + s3.start(); + LOG.info("start QuorumPeer 4{}", (withObservers ? "(observer)" : "")); + s4.start(); + LOG.info("start QuorumPeer 5{}", (withObservers ? "(observer)" : "")); + s5.start(); + LOG.info("started QuorumPeer 5"); + + LOG.info("Closing ports {}", hostPort); + for (String hp : hostPort.split(",")) { + assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT)); + LOG.info("{} is accepting client connections", hp); + } + final int numberOfPeers = 5; + // interesting to see what's there... + JMXEnv.dump(); + // make sure we have these 5 servers listed + Set ensureNames = new LinkedHashSet(); + for (int i = 1; i <= numberOfPeers; i++) { + ensureNames.add("InMemoryDataTree"); + } + for (int i = 1; i <= numberOfPeers; i++) { + ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + i + ",name2="); + } + for (int i = 1; i <= numberOfPeers; i++) { + for (int j = 1; j <= numberOfPeers; j++) { + ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + j); + } + } + for (int i = 1; i <= numberOfPeers; i++) { + ensureNames.add("name0=ReplicatedServer_id" + i); + } + JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()])); + for (int i = 1; i <= numberOfPeers; i++) { + // LocalPeerBean + String bean = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + i + ",name1=replica." + i; + JMXEnv.ensureBeanAttribute(bean, "ConfigVersion"); + JMXEnv.ensureBeanAttribute(bean, "LearnerType"); + JMXEnv.ensureBeanAttribute(bean, "ClientAddress"); + JMXEnv.ensureBeanAttribute(bean, "ElectionAddress"); + JMXEnv.ensureBeanAttribute(bean, "QuorumSystemInfo"); + JMXEnv.ensureBeanAttribute(bean, "Leader"); + } + + for (int i = 1; i <= numberOfPeers; i++) { + for (int j = 1; j <= numberOfPeers; j++) { + if (j != i) { + // RemotePeerBean + String bean = MBeanRegistry.DOMAIN + ":name0=ReplicatedServer_id" + i + ",name1=replica." + j; + JMXEnv.ensureBeanAttribute(bean, "Name"); + JMXEnv.ensureBeanAttribute(bean, "LearnerType"); + JMXEnv.ensureBeanAttribute(bean, "ClientAddress"); + JMXEnv.ensureBeanAttribute(bean, "ElectionAddress"); + JMXEnv.ensureBeanAttribute(bean, "QuorumAddress"); + JMXEnv.ensureBeanAttribute(bean, "Leader"); + } + } + } + } + + @Override + public void tearDown() throws Exception { + LOG.info("TearDown started"); + cht.tearDownAll(); + + LOG.info("Shutting down server 1"); + shutdown(s1); + LOG.info("Shutting down server 2"); + shutdown(s2); + LOG.info("Shutting down server 3"); + shutdown(s3); + LOG.info("Shutting down server 4"); + shutdown(s4); + LOG.info("Shutting down server 5"); + shutdown(s5); + + for (String hp : hostPort.split(",")) { + assertTrue("waiting for server down", ClientBase.waitForServerDown(hp, ClientBase.CONNECTION_TIMEOUT)); + LOG.info("{} is no longer accepting client connections", hp); + } + + JMXEnv.tearDown(); + } + + protected void shutdown(QuorumPeer qp) { + QuorumBase.shutdown(qp); + } + + protected TestableZooKeeper createClient() throws IOException, InterruptedException { + return createClient(hostPort); + } + + protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createClient(watcher, hp); + } + + @Test + public void testHierarchicalQuorum() throws Throwable { + cht.runHammer(5, 10); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/IntegrityCheck.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/IntegrityCheck.java new file mode 100644 index 0000000..1a238df --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/IntegrityCheck.java @@ -0,0 +1,221 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +/** + * This is a simple test to check the integrity of ZooKeeper servers. The client + * simply cycles through blasting changes to ZooKeeper and the checking what it + * gets back. + * + * The check is very simple. The value of the last successful read or write is + * stored in lastValue. When we issue a request, that value becomes a possible + * value. The difficulty is that when a communication error happens, the client + * doesn't know if the set actually went through. So, our invariant that we + * check for is that we always read a value that is greater than or equal to + * a value that we have previously read or set. (Each time we set a value, the + * value will be one more than the previous set.) + */ + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ExitCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IntegrityCheck implements StatCallback, DataCallback { + + private static final Logger LOG = LoggerFactory.getLogger(IntegrityCheck.class); + + ZooKeeper zk; + + Map lastValue = new HashMap(); + + int count; + + String path; + + int iteration; + + int outstanding; + + int errorCount; + + synchronized void incOutstanding() { + outstanding++; + } + + synchronized void decOutstanding() { + outstanding--; + notifyAll(); + } + + synchronized void waitOutstanding() throws InterruptedException { + while (outstanding > 0) { + wait(); + } + } + + IntegrityCheck(String hostPort, String path, int count) throws Exception { + zk = ClientBase.createZKClient(hostPort); + this.path = path; + this.count = count; + } + + public void run() throws InterruptedException, KeeperException { + try { + LOG.warn("Creating znodes for {}", path); + doCreate(); + LOG.warn("Staring the test loop for {}", path); + while (true) { + LOG.warn("Staring write cycle for {}", path); + doPopulate(); + waitOutstanding(); + LOG.warn("Staring read cycle for {}", path); + readAll(); + waitOutstanding(); + } + } finally { + LOG.warn("Test loop terminated for {}", path); + } + } + + void readAll() { + for (int i = 0; i < count; i++) { + String cpath = path + "/" + i; + zk.getData(cpath, false, this, null); + incOutstanding(); + } + + } + + void doCreate() throws InterruptedException, KeeperException { + // create top level znode + try { + zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException e) { + // ignore duplicate create + } + iteration++; + byte[] v = ("" + iteration).getBytes(); + // create child znodes + for (int i = 0; i < count; i++) { + String cpath = path + "/" + i; + try { + if (i % 10 == 0) { + LOG.warn("Creating znode {}", cpath); + } + zk.create(cpath, v, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException e) { + // ignore duplicate create + } + lastValue.put(cpath, v); + } + } + + void doPopulate() { + iteration++; + byte[] v = ("" + iteration).getBytes(); + for (int i = 0; i < count; i++) { + String cpath = path + "/" + i; + zk.setData(cpath, v, -1, this, v); + incOutstanding(); + } + } + + synchronized void ensureConnected() { + while (zk.getState() != ZooKeeper.States.CONNECTED) { + try { + wait(); + } catch (InterruptedException e) { + return; + } + } + } + + /** + * @param args + */ + public static void main(String[] args) { + if (args.length < 3) { + System.err.println("USAGE: IntegrityCheck zookeeperHostPort znode #children"); + return; + } + int childrenCount = 0; + try { + childrenCount = Integer.parseInt(args[2]); + } catch (NumberFormatException e) { + e.printStackTrace(); + System.exit(ExitCode.UNEXPECTED_ERROR.getValue()); + } + + try { + final IntegrityCheck ctest = new IntegrityCheck(args[0], args[1], childrenCount); + Runtime.getRuntime().addShutdownHook(new Thread() { + public void run() { + System.out.println(new Date().toString() + ": Error count = " + ctest.errorCount); + } + }); + while (true) { + try { + ctest.ensureConnected(); + ctest.run(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(ExitCode.INVALID_INVOCATION.getValue()); + } + } + + public void processResult(int rc, String path, Object ctx, Stat stat) { + if (rc == 0) { + lastValue.put(path, (byte[]) ctx); + } + decOutstanding(); + } + + public void processResult( + int rc, String path, Object ctx, byte[] data, Stat stat) { + if (rc == 0) { + String string = new String(data); + String lastString = null; + byte[] v = lastValue.get(path); + if (v != null) { + lastString = new String(v); + } + if (lastString != null && Integer.parseInt(string) < Integer.parseInt(lastString)) { + LOG.error("ERROR: Got {} expected >= {}", string, lastString); + errorCount++; + } + lastValue.put(path, (byte[]) ctx); + } + decOutstanding(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java new file mode 100644 index 0000000..01ccefd --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/InvalidSnapshotTest.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SnapshotFormatter; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class InvalidSnapshotTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(InvalidSnapshotTest.class); + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + + private static final File testData = new File(System.getProperty("test.data.dir", "src/test/resources/data")); + + /** + * Verify the LogFormatter by running it on a known file. + */ + @SuppressWarnings("deprecation") + @Test + public void testLogFormatter() throws Exception { + File snapDir = new File(testData, "invalidsnap"); + File logfile = new File(new File(snapDir, "version-2"), "log.274"); + String[] args = {logfile.getCanonicalFile().toString()}; + org.apache.zookeeper.server.LogFormatter.main(args); + } + + /** + * Verify the SnapshotFormatter by running it on a known file. + */ + @Test + public void testSnapshotFormatter() throws Exception { + File snapDir = new File(testData, "invalidsnap"); + File snapfile = new File(new File(snapDir, "version-2"), "snapshot.272"); + String[] args = {snapfile.getCanonicalFile().toString()}; + SnapshotFormatter.main(args); + } + + /** + * Verify the SnapshotFormatter by running it on a known file with one null data. + */ + @Test + public void testSnapshotFormatterWithNull() throws Exception { + File snapDir = new File(testData, "invalidsnap"); + File snapfile = new File(new File(snapDir, "version-2"), "snapshot.273"); + String[] args = {snapfile.getCanonicalFile().toString()}; + SnapshotFormatter.main(args); + } + + /** + * Verify the SnapshotFormatter fails as expected on corrupted snapshot. + */ + @Test + public void testSnapshotFormatterWithInvalidSnap() throws Exception { + File snapDir = new File(testData, "invalidsnap"); + // Broken snapshot introduced by ZOOKEEPER-367, and used to + // demonstrate recovery in testSnapshot below. + File snapfile = new File(new File(snapDir, "version-2"), "snapshot.83f"); + String[] args = {snapfile.getCanonicalFile().toString()}; + try { + SnapshotFormatter.main(args); + fail("Snapshot '" + snapfile + "' unexpectedly parsed without error."); + } catch (IOException e) { + assertTrue(e.getMessage().contains("Unreasonable length = 977468229")); + } + } + + /** + * test the snapshot + * @throws Exception an exception could be expected + */ + @Test + public void testSnapshot() throws Exception { + File origSnapDir = new File(testData, "invalidsnap"); + + // This test otherwise updates the resources directory. + File snapDir = ClientBase.createTmpDir(); + FileUtils.copyDirectory(origSnapDir, snapDir); + + ZooKeeperServer zks = new ZooKeeperServer(snapDir, snapDir, 3000); + SyncRequestProcessor.setSnapCount(1000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + LOG.info("starting up the zookeeper server .. waiting"); + assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + // we know this from the data files + // this node is the last node in the snapshot + + assertTrue(zk.exists("/9/9/8", false) != null); + } finally { + zk.close(); + } + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/JMXEnv.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/JMXEnv.java new file mode 100644 index 0000000..76845ed --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/JMXEnv.java @@ -0,0 +1,340 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; +import javax.management.MBeanServer; +import javax.management.MBeanServerConnection; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXConnectorServer; +import javax.management.remote.JMXConnectorServerFactory; +import javax.management.remote.JMXServiceURL; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JMXEnv { + + protected static final Logger LOG = LoggerFactory.getLogger(JMXEnv.class); + + private static JMXConnectorServer cs; + private static JMXConnector cc; + + public static void setUp() throws IOException { + MBeanServer mbs = MBeanRegistry.getInstance().getPlatformMBeanServer(); + + JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://127.0.0.1"); + cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); + cs.start(); + + JMXServiceURL addr = cs.getAddress(); + LOG.info("connecting to addr {}", addr); + + cc = JMXConnectorFactory.connect(addr); + } + + public static void tearDown() { + try { + if (cc != null) { + cc.close(); + } + } catch (IOException e) { + LOG.warn("Unexpected, ignoring", e); + + } + cc = null; + try { + if (cs != null) { + cs.stop(); + } + } catch (IOException e) { + LOG.warn("Unexpected, ignoring", e); + + } + cs = null; + } + + public static MBeanServerConnection conn() throws IOException { + return cc.getMBeanServerConnection(); + } + + /** + * Ensure that all of the specified names are registered. + * Note that these are components of the name, and in particular + * order matters - you want the more specific name (leafs) specified + * before their parent(s) (since names are hierarchical) + * It waits in a loop up to 60 seconds before failing if there is a + * mismatch. + * @param expectedNames + * @return + * @throws IOException + * @throws InterruptedException + */ + public static Set ensureAll(String... expectedNames) throws IOException, InterruptedException { + Set beans; + Set found; + int nTry = 0; + do { + if (nTry++ > 0) { + Thread.sleep(100); + } + try { + beans = conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null); + } catch (MalformedObjectNameException e) { + throw new RuntimeException(e); + } + + found = new HashSet(); + for (String name : expectedNames) { + LOG.info("expect:{}", name); + for (ObjectName bean : beans) { + if (bean.toString().contains(name)) { + LOG.info("found:{} {}", name, bean); + found.add(bean); + break; + } + } + beans.removeAll(found); + } + } while ((expectedNames.length != found.size()) && (nTry < 600)); + assertEquals("expected " + Arrays.toString(expectedNames), expectedNames.length, found.size()); + return beans; + } + + /** + * Ensure that only the specified names are registered. + * Note that these are components of the name, and in particular + * order matters - you want the more specific name (leafs) specified + * before their parent(s) (since names are hierarchical) + * @param expectedNames + * @return + * @throws IOException + * @throws InterruptedException + */ + public static Set ensureOnly(String... expectedNames) throws IOException, InterruptedException { + LOG.info("ensureOnly:{}", Arrays.toString(expectedNames)); + Set beans = ensureAll(expectedNames); + for (ObjectName bean : beans) { + LOG.info("unexpected:{}", bean.toString()); + } + assertEquals(0, beans.size()); + return beans; + } + + public static void ensureNone(String... expectedNames) throws IOException, InterruptedException { + Set beans; + int nTry = 0; + boolean foundUnexpected = false; + String unexpectedName = ""; + do { + if (nTry++ > 0) { + Thread.sleep(100); + } + try { + beans = conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null); + } catch (MalformedObjectNameException e) { + throw new RuntimeException(e); + } + + foundUnexpected = false; + for (String name : expectedNames) { + for (ObjectName bean : beans) { + if (bean.toString().contains(name)) { + LOG.info("didntexpect:{}", name); + foundUnexpected = true; + unexpectedName = name + " " + bean.toString(); + break; + } + } + if (foundUnexpected) { + break; + } + } + } while ((foundUnexpected) && (nTry < 600)); + if (foundUnexpected) { + LOG.info("List of all beans follows:"); + for (ObjectName bean : beans) { + LOG.info("bean:{}", bean.toString()); + } + fail(unexpectedName); + } + } + + public static void dump() throws IOException { + LOG.info("JMXEnv.dump() follows"); + Set beans; + try { + beans = conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null); + } catch (MalformedObjectNameException e) { + throw new RuntimeException(e); + } + for (ObjectName bean : beans) { + LOG.info("bean:{}", bean.toString()); + } + } + + /** + * Ensure that the specified parent names are registered. Note that these + * are components of the name. It waits in a loop up to 60 seconds before + * failing if there is a mismatch. This will return the beans which are not + * matched. + * + * https://issues.apache.org/jira/browse/ZOOKEEPER-1858 + * + * @param expectedNames + * - expected beans + * @return the beans which are not matched with the given expected names + * + * @throws IOException + * @throws InterruptedException + */ + public static Set ensureParent(String... expectedNames) throws IOException, InterruptedException { + LOG.info("ensureParent:{}", Arrays.toString(expectedNames)); + + Set beans; + int nTry = 0; + Set found = new HashSet(); + do { + if (nTry++ > 0) { + Thread.sleep(500); + } + try { + beans = conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null); + } catch (MalformedObjectNameException e) { + throw new RuntimeException(e); + } + found.clear(); + for (String name : expectedNames) { + LOG.info("expect:{}", name); + for (ObjectName bean : beans) { + // check the existence of name in bean + if (compare(bean.toString(), name)) { + LOG.info("found:{} {}", name, bean); + found.add(bean); + break; + } + } + beans.removeAll(found); + } + } while (expectedNames.length != found.size() && nTry < 120); + assertEquals("expected " + Arrays.toString(expectedNames), expectedNames.length, found.size()); + return beans; + } + + /** + * Ensure that the specified bean name and its attribute is registered. Note + * that these are components of the name. It waits in a loop up to 60 + * seconds before failing if there is a mismatch. This will return the beans + * which are not matched. + * + * @param expectedName + * - expected bean + * @param expectedAttribute + * - expected attribute + * @return the value of the attribute + * + * @throws Exception + */ + public static Object ensureBeanAttribute(String expectedName, String expectedAttribute) throws Exception { + String value = ""; + LOG.info("ensure bean:{}, attribute:{}", expectedName, expectedAttribute); + + Set beans; + int nTry = 0; + do { + if (nTry++ > 0) { + Thread.sleep(500); + } + try { + beans = conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null); + } catch (MalformedObjectNameException e) { + throw new RuntimeException(e); + } + LOG.info("expect:{}", expectedName); + for (ObjectName bean : beans) { + // check the existence of name in bean + if (bean.toString().equals(expectedName)) { + LOG.info("found:{} {}", expectedName, bean); + return conn().getAttribute(bean, expectedAttribute); + } + } + } while (nTry < 120); + fail("Failed to find bean:" + expectedName + ", attribute:" + expectedAttribute); + return value; + } + + /** + * Comparing that the given name exists in the bean. For component beans, + * the component name will be present at the end of the bean name + * + * For example 'StandaloneServer' will present in the bean name like + * 'org.apache.ZooKeeperService:name0=StandaloneServer_port-1' + */ + private static boolean compare(String bean, String name) { + String[] names = bean.split("="); + return names.length > 0 && names[names.length - 1].contains(name); + } + + static Pattern standaloneRegEx = Pattern.compile("^org.apache.ZooKeeperService:name0=StandaloneServer_port-?\\d+$"); + static Pattern instanceRegEx = Pattern.compile("^org.apache.ZooKeeperService:name0=ReplicatedServer_id(\\d+)" + + ",name1=replica.(\\d+),name2=(Follower|Leader)$"); + static Pattern observerRegEx = Pattern.compile("^org.apache.ZooKeeperService:name0=ReplicatedServer_id(-?\\d+)" + + ",name1=replica.(-?\\d+),name2=(StandaloneServer_port-?\\d+)$"); + static List beanPatterns = Arrays.asList(standaloneRegEx, instanceRegEx, observerRegEx); + + public static List getServerBeans() throws IOException { + ArrayList serverBeans = new ArrayList<>(); + Set beans; + try { + beans = conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null); + } catch (MalformedObjectNameException e) { + throw new RuntimeException(e); + } + for (ObjectName bean : beans) { + String name = bean.toString(); + LOG.info("bean:{}", name); + for (Pattern pattern : beanPatterns) { + if (pattern.matcher(name).find()) { + serverBeans.add(bean); + } + } + } + return serverBeans; + } + + public static ObjectName getServerBean() throws Exception { + List serverBeans = getServerBeans(); + if (serverBeans.size() != 1) { + throw new RuntimeException("Unable to find one and only one server bean"); + } + return serverBeans.get(0); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/KeeperStateTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/KeeperStateTest.java new file mode 100644 index 0000000..15b9455 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/KeeperStateTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.EnumSet; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.junit.Test; + +public class KeeperStateTest extends ZKTestCase { + + @Test + public void testIntConversion() { + // Ensure that we can convert all valid integers to KeeperStates + EnumSet allStates = EnumSet.allOf(KeeperState.class); + + for (KeeperState as : allStates) { + assertEquals(as, KeeperState.fromInt(as.getIntValue())); + } + } + + @Test + public void testInvalidIntConversion() { + try { + KeeperState.fromInt(324142); + fail("Was able to create an invalid KeeperState via an integer"); + } catch (RuntimeException re) { + // we're good. + } + + } + + /** Validate that the deprecated constant still works. There were issues + * found with switch statements - which need compile time constants. + */ + @Test + @SuppressWarnings("deprecation") + public void testDeprecatedCodeOkInSwitch() { + int test = 1; + switch (test) { + case Code.Ok: + assertTrue(true); + break; + } + } + + /** Verify the enum works (paranoid) */ + @Test + public void testCodeOKInSwitch() { + Code test = Code.OK; + switch (test) { + case OK: + assertTrue(true); + break; + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/KeyAuthClientTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/KeyAuthClientTest.java new file mode 100644 index 0000000..ffa90e1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/KeyAuthClientTest.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class KeyAuthClientTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(KeyAuthClientTest.class); + + static { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.KeyAuthenticationProvider"); + } + + public void createNodePrintAcl(ZooKeeper zk, String path, String testName) { + try { + LOG.debug("KeyAuthenticationProvider Creating Test Node:{}\n", path); + zk.create(path, null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + List acls = zk.getACL(path, null); + LOG.debug("Node:{} Test:{} ACLs:", path, testName); + for (ACL acl : acls) { + LOG.debug(" {}", acl.toString()); + } + } catch (Exception e) { + LOG.debug(" EXCEPTION THROWN", e); + } + } + + public void preAuth() throws Exception { + ZooKeeper zk = createClient(); + zk.addAuthInfo("key", "25".getBytes()); + try { + createNodePrintAcl(zk, "/pre", "testPreAuth"); + zk.setACL("/", Ids.CREATOR_ALL_ACL, -1); + zk.getChildren("/", false); + zk.create("/abc", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.setData("/abc", "testData1".getBytes(), -1); + zk.create("/key", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + zk.setData("/key", "5".getBytes(), -1); + Thread.sleep(1000); + } catch (KeeperException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + public void missingAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.getData("/abc", false, null); + fail("Should not be able to get data"); + } catch (KeeperException correct) { + // correct + } + try { + zk.setData("/abc", "testData2".getBytes(), -1); + fail("Should not be able to set data"); + } catch (KeeperException correct) { + // correct + } finally { + zk.close(); + } + } + + public void validAuth() throws Exception { + ZooKeeper zk = createClient(); + // any multiple of 5 will do... + zk.addAuthInfo("key", "25".getBytes()); + try { + createNodePrintAcl(zk, "/valid", "testValidAuth"); + zk.getData("/abc", false, null); + zk.setData("/abc", "testData3".getBytes(), -1); + } catch (KeeperException.AuthFailedException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + public void validAuth2() throws Exception { + ZooKeeper zk = createClient(); + // any multiple of 5 will do... + zk.addAuthInfo("key", "125".getBytes()); + try { + createNodePrintAcl(zk, "/valid2", "testValidAuth2"); + zk.getData("/abc", false, null); + zk.setData("/abc", "testData3".getBytes(), -1); + } catch (KeeperException.AuthFailedException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + @Test + public void testAuth() throws Exception { + // NOTE: the tests need to run in-order, and older versions of + // junit don't provide any way to order tests + preAuth(); + missingAuth(); + validAuth(); + validAuth2(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/LeaderSessionTrackerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LeaderSessionTrackerTest.java new file mode 100644 index 0000000..dd6e4cd --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LeaderSessionTrackerTest.java @@ -0,0 +1,154 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Due to race condition or bad client code, the leader may get request from + * expired session. We need to make sure that we never allow ephmeral node + * to be created in those case, but we do allow normal node to be created. + */ +public class LeaderSessionTrackerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(LeaderSessionTrackerTest.class); + + QuorumUtil qu; + + @Before + public void setUp() throws Exception { + qu = new QuorumUtil(1); + } + + @After + public void tearDown() throws Exception { + qu.shutdownAll(); + } + + @Test + public void testExpiredSessionWithLocalSession() throws Exception { + testCreateEphemeral(true); + } + + @Test + public void testExpiredSessionWithoutLocalSession() throws Exception { + testCreateEphemeral(false); + } + + /** + * When we create ephemeral node, we need to check against global + * session, so the leader never accept request from an expired session + * (that we no longer track) + * + * This is not the same as SessionInvalidationTest since session + * is not in closing state + */ + public void testCreateEphemeral(boolean localSessionEnabled) throws Exception { + if (localSessionEnabled) { + qu.enableLocalSession(true); + } + qu.startAll(); + QuorumPeer leader = qu.getLeaderQuorumPeer(); + + ZooKeeper zk = ClientBase.createZKClient(qu.getConnectString(leader)); + + CreateRequest createRequest = new CreateRequest("/impossible", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL.toFlag()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + createRequest.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + + // Mimic sessionId generated by follower's local session tracker + long sid = qu.getFollowerQuorumPeers().get(0).getActiveServer().getServerId(); + long fakeSessionId = (sid << 56) + 1; + + LOG.info("Fake session Id: {}", Long.toHexString(fakeSessionId)); + + Request request = new Request(null, fakeSessionId, 0, OpCode.create, bb, new ArrayList()); + + // Submit request directly to leader + leader.getActiveServer().submitRequest(request); + + // Make sure that previous request is finished + zk.create("/ok", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + Stat stat = zk.exists("/impossible", null); + assertEquals("Node from fake session get created", null, stat); + + } + + /** + * When local session is enabled, leader will allow persistent node + * to be create for unknown session + */ + @Test + public void testCreatePersistent() throws Exception { + qu.enableLocalSession(true); + qu.startAll(); + + QuorumPeer leader = qu.getLeaderQuorumPeer(); + + ZooKeeper zk = ClientBase.createZKClient(qu.getConnectString(leader)); + + CreateRequest createRequest = new CreateRequest("/success", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT.toFlag()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + createRequest.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + + // Mimic sessionId generated by follower's local session tracker + long sid = qu.getFollowerQuorumPeers().get(0).getActiveServer().getServerId(); + long locallSession = (sid << 56) + 1; + + LOG.info("Local session Id: {}", Long.toHexString(locallSession)); + + Request request = new Request(null, locallSession, 0, OpCode.create, bb, new ArrayList()); + + // Submit request directly to leader + leader.getActiveServer().submitRequest(request); + + // Make sure that previous request is finished + zk.create("/ok", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + Stat stat = zk.exists("/success", null); + assertTrue("Request from local sesson failed", stat != null); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogNoServerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogNoServerTest.java new file mode 100644 index 0000000..a48ce08 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogNoServerTest.java @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import org.apache.jute.BinaryInputArchive; +import org.apache.jute.BinaryOutputArchive; +import org.apache.jute.Record; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.server.DataNode; +import org.apache.zookeeper.server.DataTree; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileHeader; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.txn.CreateTxn; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.MultiTxn; +import org.apache.zookeeper.txn.Txn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LoadFromLogNoServerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(LoadFromLogNoServerTest.class); + + /** + * For ZOOKEEPER-1046. Verify if cversion and pzxid if incremented + * after create/delete failure during restore. + */ + @Test + public void testTxnFailure() throws Exception { + try { + ZooKeeperServer.setDigestEnabled(true); + + long count = 1; + File tmpDir = ClientBase.createTmpDir(); + FileTxnSnapLog logFile = new FileTxnSnapLog(tmpDir, tmpDir); + DataTree dt = new DataTree(); + dt.createNode("/test", new byte[0], null, 0, -1, 1, 1); + for (count = 1; count <= 3; count++) { + dt.createNode("/test/" + count, new byte[0], null, 0, -1, count, Time.currentElapsedTime()); + } + long digestBefore = dt.getTreeDigest(); + + DataNode zk = dt.getNode("/test"); + + // Make create to fail, then verify cversion. + LOG.info("Attempting to create /test/{}", (count - 1)); + doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, -1); + assertNotEquals(digestBefore, dt.getTreeDigest()); + + LOG.info("Attempting to create /test/{}", (count - 1)); + digestBefore = dt.getTreeDigest(); + doOp(logFile, ZooDefs.OpCode.create, "/test/" + (count - 1), dt, zk, zk.stat.getCversion() + 1); + assertNotEquals(digestBefore, dt.getTreeDigest()); + + LOG.info("Attempting to create /test/{}", (count - 1)); + digestBefore = dt.getTreeDigest(); + doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, zk.stat.getCversion() + 1); + assertNotEquals(digestBefore, dt.getTreeDigest()); + + LOG.info("Attempting to create /test/{}", (count - 1)); + digestBefore = dt.getTreeDigest(); + doOp(logFile, ZooDefs.OpCode.multi, "/test/" + (count - 1), dt, zk, -1); + assertNotEquals(digestBefore, dt.getTreeDigest()); + + // Make delete fo fail, then verify cversion. + // this doesn't happen anymore, we only set the cversion on create + // LOG.info("Attempting to delete " + "/test/" + (count + 1)); + // doOp(logFile, OpCode.delete, "/test/" + (count + 1), dt, zk); + } finally { + ZooKeeperServer.setDigestEnabled(false); + } + } + + /* + * Does create/delete depending on the type and verifies + * if cversion before the operation is 1 less than cversion afer. + */ + private void doOp(FileTxnSnapLog logFile, int type, String path, DataTree dt, DataNode parent, int cversion) throws Exception { + int lastSlash = path.lastIndexOf('/'); + String parentName = path.substring(0, lastSlash); + + int prevCversion = parent.stat.getCversion(); + long prevPzxid = parent.stat.getPzxid(); + List child = dt.getChildren(parentName, null, null); + StringBuilder childStr = new StringBuilder(); + for (String s : child) { + childStr.append(s).append(" "); + } + LOG.info("Children: {} for {}", childStr, parentName); + LOG.info("(cverions, pzxid): {}, {}", prevCversion, prevPzxid); + + Record txn = null; + TxnHeader txnHeader = null; + if (type == ZooDefs.OpCode.delete) { + txn = new DeleteTxn(path); + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), ZooDefs.OpCode.delete); + } else if (type == ZooDefs.OpCode.create) { + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), ZooDefs.OpCode.create); + txn = new CreateTxn(path, new byte[0], null, false, cversion); + } else if (type == ZooDefs.OpCode.multi) { + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), ZooDefs.OpCode.create); + txn = new CreateTxn(path, new byte[0], null, false, cversion); + List txnList = new ArrayList(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + txn.serialize(boa, "request"); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + Txn txact = new Txn(ZooDefs.OpCode.create, bb.array()); + txnList.add(txact); + txn = new MultiTxn(txnList); + txnHeader = new TxnHeader(0xabcd, 0x123, prevPzxid + 1, Time.currentElapsedTime(), ZooDefs.OpCode.multi); + } + logFile.processTransaction(txnHeader, dt, null, txn); + + int newCversion = parent.stat.getCversion(); + long newPzxid = parent.stat.getPzxid(); + child = dt.getChildren(parentName, null, null); + childStr = new StringBuilder(); + for (String s : child) { + childStr.append(s).append(" "); + } + LOG.info("Children: {} for {}", childStr, parentName); + LOG.info("(cverions, pzxid): {}, {}", newCversion, newPzxid); + assertTrue(type + + " verification failed. Expected: <" + + (prevCversion + 1) + + ", " + + (prevPzxid + + 1) + + ">, found: <" + + newCversion + + ", " + + newPzxid + + ">", (newCversion == prevCversion + 1 && newPzxid == prevPzxid + 1)); + } + + /** + * Simulates ZOOKEEPER-1069 and verifies that flush() before padLogFile + * fixes it. + */ + @Test + public void testPad() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + FileTxnLog txnLog = new FileTxnLog(tmpDir); + TxnHeader txnHeader = new TxnHeader(0xabcd, 0x123, 0x123, Time.currentElapsedTime(), ZooDefs.OpCode.create); + Record txn = new CreateTxn("/Test", new byte[0], null, false, 1); + txnLog.append(txnHeader, txn); + FileInputStream in = new FileInputStream(tmpDir.getPath() + "/log." + Long.toHexString(txnHeader.getZxid())); + BinaryInputArchive ia = BinaryInputArchive.getArchive(in); + FileHeader header = new FileHeader(); + header.deserialize(ia, "fileheader"); + LOG.info("Received magic : {} Expected : {}", header.getMagic(), FileTxnLog.TXNLOG_MAGIC); + assertTrue("Missing magic number ", header.getMagic() == FileTxnLog.TXNLOG_MAGIC); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogTest.java new file mode 100644 index 0000000..949cb0f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LoadFromLogTest.java @@ -0,0 +1,302 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException.NoNodeException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.FileTxnLog.FileTxnIterator; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.server.persistence.Util; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LoadFromLogTest extends ClientBase { + + private static final int NUM_MESSAGES = 300; + protected static final Logger LOG = LoggerFactory.getLogger(LoadFromLogTest.class); + + // setting up the quorum has a transaction overhead for creating and closing the session + private static final int TRANSACTION_OVERHEAD = 2; + private static final int TOTAL_TRANSACTIONS = NUM_MESSAGES + TRANSACTION_OVERHEAD; + + @Before + public void setUp() throws Exception { + SyncRequestProcessor.setSnapCount(50); + super.setUp(); + } + + /** + * test that all transactions from the Log are loaded, and only once + * @throws Exception an exception might be thrown here + */ + @Test + public void testLoad() throws Exception { + // generate some transactions that will get logged + ZooKeeper zk = createZKClient(hostPort); + try { + for (int i = 0; i < NUM_MESSAGES; i++) { + zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + stopServer(); + + // now verify that the FileTxnLog reads every transaction only once + File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + FileTxnLog txnLog = new FileTxnLog(logDir); + TxnIterator itr = txnLog.read(0); + + // Check that storage space return some value + FileTxnIterator fileItr = (FileTxnIterator) itr; + long storageSize = fileItr.getStorageSize(); + LOG.info("Txnlog size: {} bytes", storageSize); + assertTrue("Storage size is greater than zero ", (storageSize > 0)); + + long expectedZxid = 0; + long lastZxid = 0; + TxnHeader hdr; + do { + hdr = itr.getHeader(); + expectedZxid++; + assertTrue("not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid(), lastZxid + != hdr.getZxid()); + assertTrue("excepting next transaction. expected=" + expectedZxid + ", retrieved=" + hdr.getZxid(), ( + hdr.getZxid() + == expectedZxid)); + lastZxid = hdr.getZxid(); + } while (itr.next()); + + assertTrue("processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS, (expectedZxid + == TOTAL_TRANSACTIONS)); + } + + /** + * test that we fail to load txnlog of a request zxid that is older + * than what exist on disk + * @throws Exception an exception might be thrown here + */ + @Test + public void testLoadFailure() throws Exception { + // generate some transactions that will get logged + ZooKeeper zk = createZKClient(hostPort); + try { + for (int i = 0; i < NUM_MESSAGES; i++) { + zk.create("/data-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } + } finally { + zk.close(); + } + stopServer(); + + File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION); + File[] logFiles = FileTxnLog.getLogFiles(logDir.listFiles(), 0); + // Verify that we have at least NUM_MESSAGES / SNAPCOUNT txnlog + assertTrue(logFiles.length > NUM_MESSAGES / 100); + // Delete the first log file, so we will fail to read it back from disk + assertTrue("delete the first log file", logFiles[0].delete()); + + // Find zxid for the second log + long secondStartZxid = Util.getZxidFromName(logFiles[1].getName(), "log"); + + FileTxnLog txnLog = new FileTxnLog(logDir); + TxnIterator itr = txnLog.read(1, false); + + // Oldest log is already remove, so this should point to the start of + // of zxid on the second log + assertEquals(secondStartZxid, itr.getHeader().getZxid()); + + itr = txnLog.read(secondStartZxid, false); + assertEquals(secondStartZxid, itr.getHeader().getZxid()); + assertTrue(itr.next()); + + // Trying to get a second txn on second txnlog give us the + // the start of second log, since the first one is removed + long nextZxid = itr.getHeader().getZxid(); + + itr = txnLog.read(nextZxid, false); + assertEquals(secondStartZxid, itr.getHeader().getZxid()); + + // Trying to get a first txn on the third give us the + // the start of second log, since the first one is removed + long thirdStartZxid = Util.getZxidFromName(logFiles[2].getName(), "log"); + itr = txnLog.read(thirdStartZxid, false); + assertEquals(secondStartZxid, itr.getHeader().getZxid()); + assertTrue(itr.next()); + + nextZxid = itr.getHeader().getZxid(); + itr = txnLog.read(nextZxid, false); + assertEquals(secondStartZxid, itr.getHeader().getZxid()); + } + + /** + * Test we can restore the snapshot that has data ahead of the zxid + * of the snapshot file. + */ + @Test + public void testRestore() throws Exception { + // generate some transactions + ZooKeeper zk = createZKClient(hostPort); + String lastPath = null; + try { + zk.create("/invalidsnap", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int i = 0; i < NUM_MESSAGES; i++) { + lastPath = zk.create("/invalidsnap/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } + } finally { + zk.close(); + } + String[] tokens = lastPath.split("-"); + String expectedPath = "/invalidsnap/test-" + String.format("%010d", (Integer.parseInt(tokens[1])) + 1); + ZooKeeperServer zks = serverFactory.getZooKeeperServer(); + long eZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + // force the zxid to be behind the content + zks.getZKDatabase().setlastProcessedZxid(zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10); + LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase().getDataTreeLastProcessedZxid()); + // Force snapshot and restore + zks.takeSnapshot(); + zks.shutdown(); + stopServer(); + + startServer(); + zks = serverFactory.getZooKeeperServer(); + long fZxid = zks.getZKDatabase().getDataTreeLastProcessedZxid(); + + // Verify lastProcessedZxid is set correctly + assertTrue("Restore failed expected zxid=" + eZxid + " found=" + fZxid, fZxid == eZxid); + zk = createZKClient(hostPort); + + // Verify correctness of data and whether sequential znode creation + // proceeds correctly after this point + String[] children; + String path; + try { + children = zk.getChildren("/invalidsnap", false).toArray(new String[0]); + path = zk.create("/invalidsnap/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } finally { + zk.close(); + } + LOG.info("Expected {} found {}", expectedPath, path); + assertTrue("Error in sequential znode creation expected " + + expectedPath + + " found " + + path, path.equals(expectedPath)); + assertTrue("Unexpected number of children " + + children.length + + " expected " + + NUM_MESSAGES, (children.length + == NUM_MESSAGES)); + } + + /** + * Test we can restore a snapshot that has errors and data ahead of the zxid + * of the snapshot file. + */ + @Test + public void testRestoreWithTransactionErrors() throws Exception { + // generate some transactions + ZooKeeper zk = createZKClient(hostPort); + try { + for (int i = 0; i < NUM_MESSAGES; i++) { + try { + zk.create("/invaliddir/test-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + } catch (NoNodeException e) { + //Expected + } + } + } finally { + zk.close(); + } + + // force the zxid to be behind the content + ZooKeeperServer zks = serverFactory.getZooKeeperServer(); + zks.getZKDatabase().setlastProcessedZxid(zks.getZKDatabase().getDataTreeLastProcessedZxid() - 10); + LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase().getDataTreeLastProcessedZxid()); + + // Force snapshot and restore + zks.takeSnapshot(); + zks.shutdown(); + stopServer(); + + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + startServer(); + } + + /** + * Verify snap/log dir create with/without autocreate enabled. + */ + @Test + public void testDatadirAutocreate() throws Exception { + stopServer(); + + try { + // now verify autocreate off works + System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, "false"); + tmpDir = createTmpDir(); + startServer(); + fail("Server should not have started without datadir"); + } catch (IOException e) { + LOG.debug("Server failed to start - correct behavior", e); + } finally { + System.setProperty(FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE, FileTxnSnapLog.ZOOKEEPER_DATADIR_AUTOCREATE_DEFAULT); + } + } + + /** + * ZOOKEEPER-1573: test restoring a snapshot with deleted txns ahead of the + * snapshot file's zxid. + */ + @Test + public void testReloadSnapshotWithMissingParent() throws Exception { + // create transactions to create the snapshot with create/delete pattern + ZooKeeper zk = createZKClient(hostPort); + zk.create("/a", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Stat stat = zk.exists("/a", false); + long createZxId = stat.getMzxid(); + zk.create("/a/b", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.delete("/a/b", -1); + zk.delete("/a", -1); + // force the zxid to be behind the content + ZooKeeperServer zks = serverFactory.getZooKeeperServer(); + zks.getZKDatabase().setlastProcessedZxid(createZxId); + LOG.info("Set lastProcessedZxid to {}", zks.getZKDatabase().getDataTreeLastProcessedZxid()); + // Force snapshot and restore + zks.takeSnapshot(); + zks.shutdown(); + stopServer(); + + startServer(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/LocalSessionRequestTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LocalSessionRequestTest.java new file mode 100644 index 0000000..d24faa6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LocalSessionRequestTest.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Validate that open/close session request of a local session to not propagate + * to other machines in the quorum. We verify this by checking that + * these request doesn't show up in committedLog on other machines. + */ +public class LocalSessionRequestTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(LocalSessionRequestTest.class); + // Need to be short since we need to wait for session to expire + public static final int CONNECTION_TIMEOUT = 4000; + + private final QuorumBase qb = new QuorumBase(); + + @Before + public void setUp() throws Exception { + LOG.info("STARTING quorum {}", getClass().getName()); + qb.localSessionsEnabled = true; + qb.localSessionsUpgradingEnabled = true; + qb.setUp(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + } + + @After + public void tearDown() throws Exception { + LOG.info("STOPPING quorum {}", getClass().getName()); + qb.tearDown(); + } + + @Test + public void testLocalSessionsOnFollower() throws Exception { + testOpenCloseSession(false); + } + + @Test + public void testLocalSessionsOnLeader() throws Exception { + testOpenCloseSession(true); + } + + /** + * Walk through the target peer commmittedLog. + * @param sessionId + * @param peerId + */ + private void validateRequestLog(long sessionId, int peerId) { + String session = Long.toHexString(sessionId); + LOG.info("Searching for txn of session 0x " + session + " on peer " + peerId); + String peerType = peerId == qb.getLeaderIndex() ? "leader" : "follower"; + QuorumPeer peer = qb.getPeerList().get(peerId); + ZKDatabase db = peer.getActiveServer().getZKDatabase(); + for (Proposal p : db.getCommittedLog()) { + assertFalse("Should not see " + + Request.op2String(p.request.type) + + " request from local session 0x" + + session + + " on the " + + peerType, p.request.sessionId == sessionId); + } + } + + /** + * Test that a CloseSession request generated by both the server (client + * disconnect) or by the client (client explicitly issue close()) doesn't + * get committed by the ensemble + */ + public void testOpenCloseSession(boolean onLeader) throws Exception { + int leaderIdx = qb.getLeaderIndex(); + assertFalse("No leader in quorum?", leaderIdx == -1); + int followerIdx = (leaderIdx + 1) % 5; + int testPeerIdx = onLeader ? leaderIdx : followerIdx; + int verifyPeerIdx = onLeader ? followerIdx : leaderIdx; + + String[] hostPorts = qb.hostPort.split(","); + + CountdownWatcher watcher = new CountdownWatcher(); + DisconnectableZooKeeper client = new DisconnectableZooKeeper(hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + long localSessionId1 = client.getSessionId(); + + // Cut the connection, so the server will create closeSession as part + // of expiring the session. + client.dontReconnect(); + client.disconnect(); + watcher.reset(); + + // We don't validate right away, will do another session create first + + ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx], CONNECTION_TIMEOUT); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + long localSessionId2 = zk.getSessionId(); + + // Send closeSession request. + zk.close(); + watcher.reset(); + + // This should be enough time for the first session to expire and for + // the closeSession request to propagate to other machines (if there is a bug) + // Since it is time sensitive, we have false negative when test + // machine is under load + Thread.sleep(CONNECTION_TIMEOUT * 2); + + // Validate that we don't see any txn from the first session + validateRequestLog(localSessionId1, verifyPeerIdx); + + // Validate that we don't see any txn from the second session + validateRequestLog(localSessionId2, verifyPeerIdx); + + qb.shutdownServers(); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/LocalSessionsOnlyTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LocalSessionsOnlyTest.java new file mode 100644 index 0000000..2bd00d8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LocalSessionsOnlyTest.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Tests learners configured to use local sessions only. Expected + * behavior is that sessions created on the learner will never be + * made global. Operations requiring a global session (e.g. + * creation of ephemeral nodes) will fail with an error. + */ +public class LocalSessionsOnlyTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(LocalSessionsOnlyTest.class); + public static final int CONNECTION_TIMEOUT = ClientBase.CONNECTION_TIMEOUT; + + private final QuorumBase qb = new QuorumBase(); + + @Before + public void setUp() throws Exception { + LOG.info("STARTING quorum {}", getClass().getName()); + qb.localSessionsEnabled = true; + qb.localSessionsUpgradingEnabled = false; + qb.setUp(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + } + + @After + public void tearDown() throws Exception { + LOG.info("STOPPING quorum {}", getClass().getName()); + qb.tearDown(); + } + + @Test + public void testLocalSessionsOnFollower() throws Exception { + testLocalSessions(false); + } + + @Test + public void testLocalSessionsOnLeader() throws Exception { + testLocalSessions(true); + } + + private void testLocalSessions(boolean testLeader) throws Exception { + String nodePrefix = "/testLocalSessions-" + (testLeader ? "leaderTest-" : "followerTest-"); + int leaderIdx = qb.getLeaderIndex(); + assertFalse("No leader in quorum?", leaderIdx == -1); + int followerIdx = (leaderIdx + 1) % 5; + int testPeerIdx = testLeader ? leaderIdx : followerIdx; + String[] hostPorts = qb.hostPort.split(","); + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx], CONNECTION_TIMEOUT); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + long localSessionId = zk.getSessionId(); + + // Try creating some data. + for (int i = 0; i < 5; i++) { + zk.create(nodePrefix + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + // Now, try an ephemeral node. This should fail since we + // cannot create ephemeral nodes on a local session. + try { + zk.create(nodePrefix + "ephemeral", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + fail("Ephemeral node creation should fail."); + } catch (KeeperException.EphemeralOnLocalSessionException e) { + } + + // Close the session. + zk.close(); + + // Validate data on both follower and leader + Map peers = new HashMap(); + peers.put("leader", leaderIdx); + peers.put("follower", followerIdx); + for (Entry entry : peers.entrySet()) { + watcher.reset(); + // Try reconnecting with a new session. + // The data should be persisted, even though the session was not. + zk = qb.createClient(watcher, hostPorts[entry.getValue()], CONNECTION_TIMEOUT); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + long newSessionId = zk.getSessionId(); + assertFalse(newSessionId == localSessionId); + + for (int i = 0; i < 5; i++) { + assertNotNull("Data not exists in " + entry.getKey(), zk.exists(nodePrefix + i, null)); + } + + // We may get the correct exception but the txn may go through + assertNull("Data exists in " + entry.getKey(), zk.exists(nodePrefix + "ephemeral", null)); + + zk.close(); + } + qb.shutdownServers(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/LogChopperTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LogChopperTest.java new file mode 100644 index 0000000..c836e83 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/LogChopperTest.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.TxnLog; +import org.apache.zookeeper.server.util.LogChopper; +import org.apache.zookeeper.txn.DeleteTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.Test; + +class Pair { + + private V1 v1; + private V2 v2; + Pair(V1 v1, V2 v2) { + this.v1 = v1; + this.v2 = v2; + } + public V1 getFirst() { + return v1; + } + public V2 getSecond() { + return v2; + } + +} + +public class LogChopperTest extends ClientBase { + + void rmr(File dir) throws IOException { + Files.walkFileTree(dir.toPath(), new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes a) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } + }); + } + + Pair getFirstLastZxid(File logFile) throws IOException { + File tmp = createTmpDir(); + Files.copy(logFile.toPath(), new File(tmp, "log.0").toPath()); + FileTxnLog txnLog = new FileTxnLog(tmp); + TxnLog.TxnIterator it = txnLog.read(0); + long firstZxid = it.getHeader().getZxid(); + long lastZxid = firstZxid; + while (it.next()) { + lastZxid = it.getHeader().getZxid(); + } + txnLog.close(); + rmr(tmp); + return new Pair(firstZxid, lastZxid); + } + + @Test + public void testChopper() throws IOException { + long clientId = 17; + int cxid = 77; + long zxid = 1000; + long time = 1; + int type = ZooDefs.OpCode.delete; + DeleteTxn txn = new DeleteTxn("/foo"); + File tmpDir = createTmpDir(); + FileTxnLog txnLog = new FileTxnLog(tmpDir); + + for (int i = 0; i < 100; i++) { + TxnHeader hdr = new TxnHeader(clientId, cxid, ++zxid, ++time, type); + txnLog.append(hdr, txn); + } + + // append a txn with gap + TxnHeader hdr = new TxnHeader(clientId, cxid, zxid + 10, ++time, type); + txnLog.append(hdr, txn); + + txnLog.commit(); + + // now find the log we just created. + final File logFile = new File(tmpDir, "log." + Integer.toHexString(1001)); + Pair firstLast = getFirstLastZxid(logFile); + assertEquals(1001, (long) firstLast.getFirst()); + assertEquals(1110, (long) firstLast.getSecond()); + + File choppedFile = new File(tmpDir, "chopped_failed"); + assertFalse(LogChopper.chop(new FileInputStream(logFile), new FileOutputStream(choppedFile), 1107)); + + choppedFile = new File(tmpDir, "chopped"); + assertTrue(LogChopper.chop(new FileInputStream(logFile), new FileOutputStream(choppedFile), 1017)); + + firstLast = getFirstLastZxid(choppedFile); + assertEquals(1001, (long) firstLast.getFirst()); + assertEquals(1017, (long) firstLast.getSecond()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/MaxCnxnsTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/MaxCnxnsTest.java new file mode 100644 index 0000000..a927e3e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/MaxCnxnsTest.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertSame; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.proto.ConnectRequest; +import org.junit.Test; + +public class MaxCnxnsTest extends ClientBase { + + private static final int numCnxns = 30; + AtomicInteger numConnected = new AtomicInteger(0); + String host; + int port; + + @Override + public void setUp() throws Exception { + maxCnxns = numCnxns; + super.setUp(); + } + + class CnxnThread extends Thread { + + public CnxnThread(int i) { + super("CnxnThread-" + i); + } + + public void run() { + SocketChannel sChannel = null; + try { + /* + * For future unwary socket programmers: although connect 'blocks' it + * does not require an accept on the server side to return. Therefore + * you can not assume that all the sockets are connected at the end of + * this for loop. + */ + sChannel = SocketChannel.open(); + sChannel.connect(new InetSocketAddress(host, port)); + // Construct a connection request + ConnectRequest conReq = new ConnectRequest(0, 0, 10000, 0, "password".getBytes()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + boa.writeInt(-1, "len"); + conReq.serialize(boa, "connect"); + baos.close(); + ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray()); + bb.putInt(bb.capacity() - 4); + bb.rewind(); + + /* Send a connect request. Any socket that has been closed (or at least + * not added to the cnxn list on the server) will not have any bytes to + * read and get an eof. + * + * The trick here was finding a call that caused the server to put + * bytes in the input stream without closing the cnxn. None of + * the four letter commands do that, so we actually try to create + * a session which should send us something back, while maintaining + * the connection. + */ + + int eof = sChannel.write(bb); + // If the socket times out, we count that as failed - + // the server should respond within 10s + sChannel.socket().setSoTimeout(10000); + if (!sChannel.socket().isClosed()) { + eof = sChannel.socket().getInputStream().read(); + if (eof != -1) { + numConnected.incrementAndGet(); + } + } + } catch (IOException io) { + // "Connection reset by peer" + } finally { + if (sChannel != null) { + try { + sChannel.close(); + } catch (Exception e) { + // Do nothing + } + } + } + } + + } + + /** + * Verify the ability to limit the number of concurrent connections. + * @throws IOException + * @throws InterruptedException + */ + @Test + public void testMaxCnxns() throws IOException, InterruptedException { + String[] split = hostPort.split(":"); + host = split[0]; + port = Integer.parseInt(split[1]); + int numThreads = numCnxns + 5; + CnxnThread[] threads = new CnxnThread[numThreads]; + + for (int i = 0; i < numCnxns; ++i) { + threads[i] = new CnxnThread(i); + } + + for (int i = 0; i < numCnxns; ++i) { + threads[i].start(); + } + + for (int i = 0; i < numCnxns; ++i) { + threads[i].join(); + } + assertSame(numCnxns, numConnected.get()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/MultiAsyncTransactionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/MultiAsyncTransactionTest.java new file mode 100644 index 0000000..5a94649 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/MultiAsyncTransactionTest.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.OpResult.CreateResult; +import org.apache.zookeeper.OpResult.ErrorResult; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Before; +import org.junit.Test; + +public class MultiAsyncTransactionTest extends ClientBase { + + private ZooKeeper zk; + private final AtomicInteger pendingOps = new AtomicInteger(0); + + @Before + public void setUp() throws Exception { + super.setUp(); + zk = createClient(); + pendingOps.set(0); + } + + private static class MultiResult { + + int rc; + List results; + + } + + private void finishPendingOps() { + if (pendingOps.decrementAndGet() == 0) { + synchronized (pendingOps) { + pendingOps.notifyAll(); + } + } + } + + private void waitForPendingOps(int timeout) throws Exception { + synchronized (pendingOps) { + while (pendingOps.get() > 0) { + pendingOps.wait(timeout); + } + } + } + + /** + * ZOOKEEPER-1624: PendingChanges of create sequential node request didn't + * get rollbacked correctly when multi-op failed. This cause + * create sequential node request in subsequent multi-op to failed because + * sequential node name generation is incorrect. + * + * The check is to make sure that each request in multi-op failed with + * the correct reason. + */ + @Test + public void testSequentialNodeCreateInAsyncMulti() throws Exception { + final int iteration = 4; + final List results = new ArrayList(); + + pendingOps.set(iteration); + + List ops = Arrays.asList(Op.create("/node-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL), Op.create("/dup", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + + for (int i = 0; i < iteration; ++i) { + zk.multi(ops, new MultiCallback() { + @Override + public void processResult(int rc, String path, Object ctx, List opResults) { + MultiResult result = new MultiResult(); + result.results = opResults; + result.rc = rc; + results.add(result); + finishPendingOps(); + } + }, null); + } + + waitForPendingOps(CONNECTION_TIMEOUT); + + // Check that return code of all request are correct + assertEquals(KeeperException.Code.OK.intValue(), results.get(0).rc); + assertEquals(KeeperException.Code.NODEEXISTS.intValue(), results.get(1).rc); + assertEquals(KeeperException.Code.NODEEXISTS.intValue(), results.get(2).rc); + assertEquals(KeeperException.Code.NODEEXISTS.intValue(), results.get(3).rc); + + // Check that the first operation is successful in all request + assertTrue(results.get(0).results.get(0) instanceof CreateResult); + assertEquals(KeeperException.Code.OK.intValue(), ((ErrorResult) results.get(1).results.get(0)).getErr()); + assertEquals(KeeperException.Code.OK.intValue(), ((ErrorResult) results.get(2).results.get(0)).getErr()); + assertEquals(KeeperException.Code.OK.intValue(), ((ErrorResult) results.get(3).results.get(0)).getErr()); + + // Check that the second operation failed after the first request + assertEquals(KeeperException.Code.NODEEXISTS.intValue(), ((ErrorResult) results.get(1).results.get(1)).getErr()); + assertEquals(KeeperException.Code.NODEEXISTS.intValue(), ((ErrorResult) results.get(2).results.get(1)).getErr()); + assertEquals(KeeperException.Code.NODEEXISTS.intValue(), ((ErrorResult) results.get(3).results.get(1)).getErr()); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/MultiOperationTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/MultiOperationTest.java new file mode 100644 index 0000000..5dfd73e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/MultiOperationTest.java @@ -0,0 +1,942 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TreeSet; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.AsyncCallback.MultiCallback; +import org.apache.zookeeper.ClientCnxn; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.OpResult; +import org.apache.zookeeper.OpResult.CheckResult; +import org.apache.zookeeper.OpResult.CreateResult; +import org.apache.zookeeper.OpResult.DeleteResult; +import org.apache.zookeeper.OpResult.ErrorResult; +import org.apache.zookeeper.OpResult.SetDataResult; +import org.apache.zookeeper.Transaction; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKParameterized; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(Parameterized.class) +@Parameterized.UseParametersRunnerFactory(ZKParameterized.RunnerFactory.class) +public class MultiOperationTest extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(MultiOperationTest.class); + private ZooKeeper zk; + private ZooKeeper zk_chroot; + + private final boolean useAsync; + + public MultiOperationTest(boolean useAsync) { + this.useAsync = useAsync; + } + + @Parameters + public static Collection configs() { + return Arrays.asList(new Object[][]{{false}, {true}}); + } + + @Before + public void setUp() throws Exception { + SyncRequestProcessor.setSnapCount(150); + super.setUp(); + zk = createClient(); + } + + static class MultiResult { + + int rc; + List results; + boolean finished = false; + + } + + private List multi(ZooKeeper zk, Iterable ops) throws KeeperException, InterruptedException { + if (useAsync) { + final MultiResult res = new MultiResult(); + zk.multi(ops, new MultiCallback() { + @Override + public void processResult(int rc, String path, Object ctx, List opResults) { + if (!ClientCnxn.isInEventThread()) { + throw new RuntimeException("not in event thread"); + } + synchronized (res) { + res.rc = rc; + res.results = opResults; + res.finished = true; + res.notifyAll(); + } + } + }, null); + synchronized (res) { + while (!res.finished) { + res.wait(); + } + } + // In case of only OpKind.READ operations, no exception is thrown. Errors only marked in form of ErrorResults. + if (KeeperException.Code.OK.intValue() != res.rc && ops.iterator().next().getKind() != Op.OpKind.READ) { + KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc)); + throw ke; + } + return res.results; + } else { + return zk.multi(ops); + } + } + + private void multiHavingErrors(ZooKeeper zk, Iterable ops, List expectedResultCodes, String expectedErr) throws KeeperException, InterruptedException { + if (useAsync) { + final MultiResult res = new MultiResult(); + zk.multi(ops, new MultiCallback() { + @Override + public void processResult(int rc, String path, Object ctx, List opResults) { + synchronized (res) { + res.rc = rc; + res.results = opResults; + res.finished = true; + res.notifyAll(); + } + } + }, null); + synchronized (res) { + while (!res.finished) { + res.wait(); + } + } + for (int i = 0; i < res.results.size(); i++) { + OpResult opResult = res.results.get(i); + assertTrue("Did't receive proper error response", opResult instanceof ErrorResult); + ErrorResult errRes = (ErrorResult) opResult; + assertEquals("Did't receive proper error code", expectedResultCodes.get(i).intValue(), errRes.getErr()); + } + } else { + try { + zk.multi(ops); + fail("Shouldn't have validated in ZooKeeper client!"); + } catch (KeeperException e) { + assertEquals("Wrong exception", expectedErr, e.code().name()); + } catch (IllegalArgumentException e) { + assertEquals("Wrong exception", expectedErr, e.getMessage()); + } + } + } + + private List commit(Transaction txn) throws KeeperException, InterruptedException { + if (useAsync) { + final MultiResult res = new MultiResult(); + txn.commit(new MultiCallback() { + @Override + public void processResult(int rc, String path, Object ctx, List opResults) { + synchronized (res) { + res.rc = rc; + res.results = opResults; + res.finished = true; + res.notifyAll(); + } + } + }, null); + synchronized (res) { + while (!res.finished) { + res.wait(); + } + } + if (KeeperException.Code.OK.intValue() != res.rc) { + KeeperException ke = KeeperException.create(KeeperException.Code.get(res.rc)); + throw ke; + } + return res.results; + } else { + return txn.commit(); + } + } + + /** + * Test verifies the multi calls with invalid znode path + */ + @Test(timeout = 90000) + public void testInvalidPath() throws Exception { + List expectedResultCodes = new ArrayList(); + expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue()); + expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + // create with CreateMode + List opList = Arrays.asList( + Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi1/", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + String expectedErr = "Path must not end with / character"; + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + + // create with valid sequential flag + opList = Arrays.asList( + Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("multi1/", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL.toFlag()), + Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + expectedErr = "Path must start with / character"; + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + + // check + opList = Arrays.asList( + Op.check("/multi0", -1), Op.check("/multi1/", 100), + Op.check("/multi2", 5)); + expectedErr = "Path must not end with / character"; + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + + // delete + opList = Arrays.asList( + Op.delete("/multi0", -1), + Op.delete("/multi1/", 100), + Op.delete("/multi2", 5)); + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + + // Multiple bad arguments + expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue()); + + // setdata + opList = Arrays.asList( + Op.setData("/multi0", new byte[0], -1), + Op.setData("/multi1/", new byte[0], -1), + Op.setData("/multi2", new byte[0], -1), + Op.setData("multi3", new byte[0], -1)); + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + } + + /** + * ZOOKEEPER-2052: + * Multi abort shouldn't have any side effect. + * We fix a bug in rollback and the following scenario should work: + * 1. multi delete abort because of not empty directory + * 2. ephemeral nodes under that directory are deleted + * 3. multi delete should succeed. + */ + @Test + public void testMultiRollback() throws Exception { + zk.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + ZooKeeper epheZk = createClient(); + epheZk.create("/foo/bar", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + List opList = Arrays.asList(Op.delete("/foo", -1)); + try { + zk.multi(opList); + fail("multi delete should failed for not empty directory"); + } catch (KeeperException.NotEmptyException e) { + } + + final CountDownLatch latch = new CountDownLatch(1); + + zk.exists("/foo/bar", event -> { + if (event.getType() == Watcher.Event.EventType.NodeDeleted) { + latch.countDown(); + } + }); + + epheZk.close(); + + latch.await(); + + try { + zk.getData("/foo/bar", false, null); + fail("ephemeral node should have been deleted"); + } catch (KeeperException.NoNodeException e) { + } + + zk.multi(opList); + + try { + zk.getData("/foo", false, null); + fail("persistent node should have been deleted after multi"); + } catch (KeeperException.NoNodeException e) { + } + } + + /** + * Test verifies the multi calls with blank znode path + */ + @Test(timeout = 90000) + public void testBlankPath() throws Exception { + List expectedResultCodes = new ArrayList(); + expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue()); + expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue()); + + // delete + String expectedErr = "Path cannot be null"; + List opList = Arrays.asList( + Op.delete("/multi0", -1), + Op.delete(null, 100), + Op.delete("/multi2", 5), + Op.delete("", -1)); + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + } + + /** + * Test verifies the multi.create with invalid createModeFlag + */ + @Test(timeout = 90000) + public void testInvalidCreateModeFlag() throws Exception { + List expectedResultCodes = new ArrayList(); + expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue()); + expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); + + int createModeFlag = 6789; + List opList = Arrays.asList( + Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, createModeFlag), + Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + String expectedErr = KeeperException.Code.BADARGUMENTS.name(); + multiHavingErrors(zk, opList, expectedResultCodes, expectedErr); + } + + @Test + public void testChRootCreateDelete() throws Exception { + // creating the subtree for chRoot clients. + String chRoot = createNameSpace(); + // Creating child using chRoot client. + zk_chroot = createClient(this.hostPort + chRoot); + Op createChild = Op.create("/myid", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + multi(zk_chroot, Arrays.asList(createChild)); + + assertNotNull("zNode is not created under chroot:" + chRoot, zk.exists(chRoot + "/myid", false)); + assertNotNull("zNode is not created under chroot:" + chRoot, zk_chroot.exists("/myid", false)); + assertNull("zNode is created directly under '/', ignored configured chroot", zk.exists("/myid", false)); + + // Deleting child using chRoot client. + Op deleteChild = Op.delete("/myid", 0); + multi(zk_chroot, Arrays.asList(deleteChild)); + assertNull("zNode exists under chroot:" + chRoot, zk.exists(chRoot + "/myid", false)); + assertNull("zNode exists under chroot:" + chRoot, zk_chroot.exists("/myid", false)); + } + + @Test + public void testChRootSetData() throws Exception { + // creating the subtree for chRoot clients. + String chRoot = createNameSpace(); + // setData using chRoot client. + zk_chroot = createClient(this.hostPort + chRoot); + String[] names = {"/multi0", "/multi1", "/multi2"}; + List ops = new ArrayList(); + + for (int i = 0; i < names.length; i++) { + ops.add(Op.create(names[i], new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + ops.add(Op.setData(names[i], names[i].getBytes(), 0)); + } + + multi(zk_chroot, ops); + + for (int i = 0; i < names.length; i++) { + assertArrayEquals("zNode data not matching", names[i].getBytes(), zk_chroot.getData(names[i], false, null)); + } + } + + @Test + public void testChRootCheck() throws Exception { + // creating the subtree for chRoot clients. + String chRoot = createNameSpace(); + // checking the child version using chRoot client. + zk_chroot = createClient(this.hostPort + chRoot); + String[] names = {"/multi0", "/multi1", "/multi2"}; + List ops = new ArrayList(); + + for (int i = 0; i < names.length; i++) { + zk.create(chRoot + names[i], new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + for (int i = 0; i < names.length; i++) { + ops.add(Op.check(names[i], 0)); + } + multi(zk_chroot, ops); + } + + @Test + public void testChRootTransaction() throws Exception { + // creating the subtree for chRoot clients. + String chRoot = createNameSpace(); + // checking the child version using chRoot client. + zk_chroot = createClient(this.hostPort + chRoot); + String childPath = "/myid"; + Transaction transaction = zk_chroot.transaction(); + transaction.create(childPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + transaction.check(childPath, 0); + transaction.setData(childPath, childPath.getBytes(), 0); + commit(transaction); + + assertNotNull("zNode is not created under chroot:" + chRoot, zk.exists(chRoot + childPath, false)); + assertNotNull("zNode is not created under chroot:" + chRoot, zk_chroot.exists(childPath, false)); + assertNull("zNode is created directly under '/', ignored configured chroot", zk.exists(childPath, false)); + assertArrayEquals("zNode data not matching", childPath.getBytes(), zk_chroot.getData(childPath, false, null)); + + transaction = zk_chroot.transaction(); + // Deleting child using chRoot client. + transaction.delete(childPath, 1); + commit(transaction); + + assertNull("chroot:" + chRoot + " exists after delete", zk.exists(chRoot + "/myid", false)); + assertNull("chroot:" + chRoot + " exists after delete", zk_chroot.exists("/myid", false)); + } + + private String createNameSpace() throws InterruptedException, KeeperException { + // creating the subtree for chRoot clients. + String chRoot = "/appsX"; + Op createChRoot = Op.create(chRoot, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + multi(zk, Arrays.asList(createChRoot)); + return chRoot; + } + + @Test + public void testCreate() throws Exception { + multi(zk, Arrays.asList( + Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT))); + zk.getData("/multi0", false, null); + zk.getData("/multi1", false, null); + zk.getData("/multi2", false, null); + } + + @Test + public void testEmpty() throws Exception { + multi(zk, Arrays.asList()); + } + + @Test + public void testCreateDelete() throws Exception { + + multi(zk, Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/multi", 0))); + + // '/multi' should have been deleted + assertNull(zk.exists("/multi", null)); + } + + @Test + public void testInvalidVersion() throws Exception { + + try { + multi(zk, Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/multi", 1))); + fail("delete /multi should have failed"); + } catch (KeeperException e) { + /* PASS */ + } + } + + @Test + public void testNestedCreate() throws Exception { + + multi(zk, Arrays.asList( + /* Create */ + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.create("/multi/a", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.create("/multi/a/1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + + /* Delete */ + Op.delete("/multi/a/1", 0), Op.delete("/multi/a", 0), Op.delete("/multi", 0))); + + //Verify tree deleted + assertNull(zk.exists("/multi/a/1", null)); + assertNull(zk.exists("/multi/a", null)); + assertNull(zk.exists("/multi", null)); + } + + @Test + public void testSetData() throws Exception { + + String[] names = {"/multi0", "/multi1", "/multi2"}; + List ops = new ArrayList(); + + for (int i = 0; i < names.length; i++) { + ops.add(Op.create(names[i], new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + ops.add(Op.setData(names[i], names[i].getBytes(), 0)); + } + + multi(zk, ops); + + for (int i = 0; i < names.length; i++) { + assertArrayEquals(names[i].getBytes(), zk.getData(names[i], false, null)); + } + } + + @Test + public void testUpdateConflict() throws Exception { + + assertNull(zk.exists("/multi", null)); + + try { + multi(zk, Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.setData("/multi", "X".getBytes(), 0), + Op.setData("/multi", "Y".getBytes(), 0))); + fail("Should have thrown a KeeperException for invalid version"); + } catch (KeeperException e) { + //PASS + LOG.error("STACKTRACE: ", e); + } + + assertNull(zk.exists("/multi", null)); + + //Updating version solves conflict -- order matters + multi(zk, Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.setData("/multi", "X".getBytes(), 0), + Op.setData("/multi", "Y".getBytes(), 1))); + + assertArrayEquals(zk.getData("/multi", false, null), "Y".getBytes()); + } + + @Test + public void testDeleteUpdateConflict() throws Exception { + + /* Delete of a node folowed by an update of the (now) deleted node */ + try { + multi(zk, Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/multi", 0), + Op.setData("/multi", "Y".getBytes(), 0))); + fail("/multi should have been deleted so setData should have failed"); + } catch (KeeperException e) { + /* PASS */ + } + + // '/multi' should never have been created as entire op should fail + assertNull(zk.exists("/multi", null)); + } + + @Test + public void testGetResults() throws Exception { + /* Delete of a node folowed by an update of the (now) deleted node */ + Iterable ops = Arrays.asList( + Op.create("/multi", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/multi", 0), + Op.setData("/multi", "Y".getBytes(), 0), + Op.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)); + List results = null; + if (useAsync) { + final MultiResult res = new MultiResult(); + zk.multi(ops, new MultiCallback() { + @Override + public void processResult(int rc, String path, Object ctx, List opResults) { + synchronized (res) { + res.rc = rc; + res.results = opResults; + res.finished = true; + res.notifyAll(); + } + } + }, null); + synchronized (res) { + while (!res.finished) { + res.wait(); + } + } + assertFalse("/multi should have been deleted so setData should have failed", KeeperException.Code.OK.intValue() + == res.rc); + assertNull(zk.exists("/multi", null)); + results = res.results; + } else { + try { + zk.multi(ops); + fail("/multi should have been deleted so setData should have failed"); + } catch (KeeperException e) { + // '/multi' should never have been created as entire op should fail + assertNull(zk.exists("/multi", null)); + results = e.getResults(); + } + } + + assertNotNull(results); + for (OpResult r : results) { + LOG.info("RESULT==> {}", r); + if (r instanceof ErrorResult) { + ErrorResult er = (ErrorResult) r; + LOG.info("ERROR RESULT: {} ERR=>{}", er, KeeperException.Code.get(er.getErr())); + } + } + } + + /** + * Exercise the equals methods of OpResult classes. + */ + @Test + public void testOpResultEquals() { + opEquals(new CreateResult("/foo"), new CreateResult("/foo"), new CreateResult("nope")); + + opEquals(new CreateResult("/foo"), new CreateResult("/foo"), new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11))); + + opEquals(new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("nope", new Stat(11, 12, 13, 14, 15, 16, 17, 18, 19, 110, 111))); + + opEquals(new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("/foo", new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new CreateResult("/foo")); + + opEquals(new CheckResult(), new CheckResult(), null); + + opEquals(new SetDataResult(new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new SetDataResult(new Stat(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)), new SetDataResult(new Stat(11, 12, 13, 14, 15, 16, 17, 18, 19, 110, 111))); + + opEquals(new ErrorResult(1), new ErrorResult(1), new ErrorResult(2)); + + opEquals(new DeleteResult(), new DeleteResult(), null); + + opEquals(new ErrorResult(1), new ErrorResult(1), new ErrorResult(2)); + } + + private void opEquals(OpResult expected, OpResult value, OpResult near) { + assertEquals(value, value); + assertFalse(value.equals(new Object())); + assertFalse(value.equals(near)); + assertFalse(value.equals(value instanceof CreateResult ? new ErrorResult(1) : new CreateResult("nope2"))); + assertTrue(value.equals(expected)); + } + + @Test + public void testWatchesTriggered() throws KeeperException, InterruptedException { + HasTriggeredWatcher watcher = new HasTriggeredWatcher(); + zk.getChildren("/", watcher); + multi(zk, Arrays.asList( + Op.create("/t", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/t", -1))); + assertTrue(watcher.triggered.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + } + + @Test + public void testNoWatchesTriggeredForFailedMultiRequest() throws InterruptedException, KeeperException { + HasTriggeredWatcher watcher = new HasTriggeredWatcher(); + zk.getChildren("/", watcher); + try { + multi(zk, Arrays.asList( + Op.create("/t", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.delete("/nonexisting", -1))); + fail("expected previous multi op to fail!"); + } catch (KeeperException.NoNodeException e) { + // expected + } + SyncCallback cb = new SyncCallback(); + zk.sync("/", cb, null); + + // by waiting for the callback we're assured that the event queue is flushed + cb.done.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(1, watcher.triggered.getCount()); + } + + @Test + public void testTransactionBuilder() throws Exception { + List results = commit(zk.transaction().create("/t1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).create("/t1/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT).create("/t2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL)); + assertEquals(3, results.size()); + for (OpResult r : results) { + CreateResult c = (CreateResult) r; + assertTrue(c.getPath().startsWith("/t")); + assertNotNull(c.toString()); + } + assertNotNull(zk.exists("/t1", false)); + assertNotNull(zk.exists("/t1/child", false)); + assertNotNull(zk.exists("/t2", false)); + + results = commit(zk.transaction().check("/t1", 0).check("/t1/child", 0).check("/t2", 0)); + assertEquals(3, results.size()); + for (OpResult r : results) { + CheckResult c = (CheckResult) r; + assertNotNull(c.toString()); + } + + try { + results = commit(zk.transaction().check("/t1", 0).check("/t1/child", 0).check("/t2", 1)); + fail(); + } catch (KeeperException.BadVersionException e) { + // expected + } + + results = commit(zk.transaction().check("/t1", 0).setData("/t1", new byte[0], 0)); + assertEquals(2, results.size()); + for (OpResult r : results) { + assertNotNull(r.toString()); + } + + try { + results = commit(zk.transaction().check("/t1", 1).setData("/t1", new byte[0], 2)); + fail(); + } catch (KeeperException.BadVersionException e) { + // expected + } + + results = commit(zk.transaction().check("/t1", 1).check("/t1/child", 0).check("/t2", 0)); + assertEquals(3, results.size()); + + results = commit(zk.transaction().delete("/t2", -1).delete("/t1/child", -1)); + assertEquals(2, results.size()); + for (OpResult r : results) { + DeleteResult d = (DeleteResult) r; + assertNotNull(d.toString()); + } + assertNotNull(zk.exists("/t1", false)); + assertNull(zk.exists("/t1/child", false)); + assertNull(zk.exists("/t2", false)); + } + + @Test + public void testMultiGetChildren() throws Exception { + List topLevelNodes = new ArrayList(); + Map> childrenNodes = new HashMap>(); + // Creating a database where '/fooX' nodes has 'barXY' named children. + for (int i = 0; i < 10; i++) { + String name = "/foo" + i; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + topLevelNodes.add(name); + childrenNodes.put(name, new ArrayList<>()); + for (int j = 0; j < 10; j++) { + String childname = name + "/bar" + i + j; + String childname_s = "bar" + i + j; + zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + childrenNodes.get(name).add(childname_s); + } + } + // Create a multi operation, which queries the children of the nodes in topLevelNodes. + List multiChildrenList = multi(zk, topLevelNodes.stream().map(Op::getChildren).collect(Collectors.toList())); + for (int i = 0; i < topLevelNodes.size(); i++) { + String nodeName = topLevelNodes.get(i); + assertTrue(multiChildrenList.get(i) instanceof OpResult.GetChildrenResult); + List childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(i)).getChildren(); + // In general, we do not demand an order from the children list but to contain every child. + assertEquals(new TreeSet(childrenList), new TreeSet(childrenNodes.get(nodeName))); + + List children = zk.getChildren(nodeName, false); + assertEquals(childrenList, children); + } + } + + @Test + public void testMultiGetChildrenSameNode() throws Exception { + List childrenNodes = new ArrayList(); + // Creating a database where '/foo' node has 'barX' named children. + String topLevelNode = "/foo"; + zk.create(topLevelNode, topLevelNode.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int i = 0; i < 10; i++) { + String childname = topLevelNode + "/bar" + i; + String childname_s = "bar" + i; + zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + childrenNodes.add(childname_s); + } + + // Check for getting the children of the same node twice. + List sameChildrenList = multi(zk, Arrays.asList( + Op.getChildren(topLevelNode), + Op.getChildren(topLevelNode))); + // The response should contain two elements which are the same. + assertEquals(sameChildrenList.size(), 2); + assertEquals(sameChildrenList.get(0), sameChildrenList.get(1)); + // Check the actual result. + assertTrue(sameChildrenList.get(0) instanceof OpResult.GetChildrenResult); + OpResult.GetChildrenResult gcr = (OpResult.GetChildrenResult) sameChildrenList.get(0); + // In general, we do not demand an order from the children list but to contain every child. + assertEquals(new TreeSet(gcr.getChildren()), new TreeSet(childrenNodes)); + } + + @Test + public void testMultiGetChildrenAuthentication() throws KeeperException, InterruptedException { + List writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone"))); + zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT); + + // Check for normal behaviour. + List multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_auth"))); + assertEquals(multiChildrenList.size(), 1); + assertTrue(multiChildrenList.get(0) instanceof OpResult.GetChildrenResult); + List childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(0)).getChildren(); + assertEquals(childrenList.size(), 1); + assertEquals(childrenList.get(0), "bar"); + + // Check for authentication violation. + multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_no_auth"))); + + assertEquals(multiChildrenList.size(), 1); + assertTrue(multiChildrenList.get(0) instanceof OpResult.ErrorResult); + assertEquals("Expected NoAuthException for getting the children of a write only node", ((OpResult.ErrorResult) multiChildrenList.get(0)).getErr(), KeeperException.Code.NOAUTH.intValue()); + + } + + @Test + public void testMultiGetChildrenMixedAuthenticationErrorFirst() throws KeeperException, InterruptedException { + List writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone"))); + zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT); + List multiChildrenList; + + // Mixed nodes, the operation after the error should return RuntimeInconsistency error. + multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_no_auth"), Op.getChildren("/foo_auth"))); + + assertEquals(multiChildrenList.size(), 2); + assertTrue(multiChildrenList.get(0) instanceof OpResult.ErrorResult); + assertEquals("Expected NoAuthException for getting the children of a write only node", ((OpResult.ErrorResult) multiChildrenList.get(0)).getErr(), KeeperException.Code.NOAUTH.intValue()); + + assertTrue(multiChildrenList.get(1) instanceof OpResult.GetChildrenResult); + List childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(1)).getChildren(); + assertEquals(childrenList.size(), 1); + assertEquals(childrenList.get(0), "bar"); + } + + @Test + public void testMultiGetChildrenMixedAuthenticationCorrectFirst() throws KeeperException, InterruptedException { + List writeOnly = Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("world", "anyone"))); + zk.create("/foo_auth", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/foo_auth/bar", null, Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/foo_no_auth", null, writeOnly, CreateMode.PERSISTENT); + + // Check for getting the children of the nodes with mixed authentication. + // The getChildren operation returns GetChildrenResult if it happened before the error. + List multiChildrenList; + multiChildrenList = multi(zk, Arrays.asList(Op.getChildren("/foo_auth"), Op.getChildren("/foo_no_auth"))); + assertSame(multiChildrenList.size(), 2); + + assertTrue(multiChildrenList.get(0) instanceof OpResult.GetChildrenResult); + List childrenList = ((OpResult.GetChildrenResult) multiChildrenList.get(0)).getChildren(); + assertEquals(childrenList.size(), 1); + assertEquals(childrenList.get(0), "bar"); + + assertTrue(multiChildrenList.get(1) instanceof OpResult.ErrorResult); + assertEquals("Expected NoAuthException for getting the children of a write only node", ((OpResult.ErrorResult) multiChildrenList.get(1)).getErr(), KeeperException.Code.NOAUTH.intValue()); + } + + @Test + public void testMultiGetData() throws Exception { + zk.create("/node1", "data1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/node2", "data2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + List multiData = multi(zk, Arrays.asList(Op.getData("/node1"), Op.getData("/node2"))); + assertEquals(multiData.size(), 2); + assertArrayEquals(((OpResult.GetDataResult) multiData.get(0)).getData(), "data1".getBytes()); + assertArrayEquals(((OpResult.GetDataResult) multiData.get(1)).getData(), "data2".getBytes()); + } + + @Test + public void testMultiRead() throws Exception { + zk.create("/node1", "data1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/node2", "data2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + zk.create("/node1/node1", "data11".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/node1/node2", "data12".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + List multiRead = multi(zk, Arrays.asList( + Op.getChildren("/node1"), + Op.getData("/node1"), + Op.getChildren("/node2"), + Op.getData("/node2"))); + assertEquals(multiRead.size(), 4); + assertTrue(multiRead.get(0) instanceof OpResult.GetChildrenResult); + List childrenList = ((OpResult.GetChildrenResult) multiRead.get(0)).getChildren(); + assertEquals(childrenList.size(), 2); + assertEquals(new TreeSet(childrenList), new TreeSet(Arrays.asList("node1", "node2"))); + + assertArrayEquals(((OpResult.GetDataResult) multiRead.get(1)).getData(), "data1".getBytes()); + Stat stat = ((OpResult.GetDataResult) multiRead.get(1)).getStat(); + assertEquals(stat.getMzxid(), stat.getCzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(2, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(5, stat.getDataLength()); + assertEquals(2, stat.getNumChildren()); + + assertTrue(multiRead.get(2) instanceof OpResult.GetChildrenResult); + childrenList = ((OpResult.GetChildrenResult) multiRead.get(2)).getChildren(); + assertTrue(childrenList.isEmpty()); + + assertArrayEquals(((OpResult.GetDataResult) multiRead.get(3)).getData(), "data2".getBytes()); + stat = ((OpResult.GetDataResult) multiRead.get(3)).getStat(); + assertEquals(stat.getMzxid(), stat.getCzxid()); + assertEquals(stat.getMzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); + assertEquals(5, stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + } + + @Test + public void testMixedReadAndTransaction() throws Exception { + zk.create("/node", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + try { + List multiRead = multi(zk, Arrays.asList( + Op.setData("/node1", "data1".getBytes(), -1), + Op.getData("/node1"))); + fail("Mixed kind of operations are not allowed"); + } catch (IllegalArgumentException e) { + // expected + } + } + + private static class HasTriggeredWatcher implements Watcher { + + private final CountDownLatch triggered = new CountDownLatch(1); + + @Override + public void process(WatchedEvent event) { + triggered.countDown(); + } + + } + + private static class SyncCallback implements AsyncCallback.VoidCallback { + + private final CountDownLatch done = new CountDownLatch(1); + + @Override + public void processResult(int rc, String path, Object ctx) { + done.countDown(); + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NIOConnectionFactoryFdLeakTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NIOConnectionFactoryFdLeakTest.java new file mode 100644 index 0000000..7ea295a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NIOConnectionFactoryFdLeakTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import java.net.InetSocketAddress; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.NIOServerCnxnFactory; +import org.apache.zookeeper.server.util.OSMXBean; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * ZOOKEEPER-1620 - Acceptor and Selector thread don't call selector.close() + * causing fd leakage + */ +public class NIOConnectionFactoryFdLeakTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(NIOConnectionFactoryFdLeakTest.class); + + @Test + public void testFileDescriptorLeak() throws Exception { + + OSMXBean osMbean = new OSMXBean(); + if (!osMbean.getUnix()) { + LOG.info("Unable to run test on non-unix system"); + return; + } + + long startFdCount = osMbean.getOpenFileDescriptorCount(); + LOG.info("Start fdcount is: {}", startFdCount); + + for (int i = 0; i < 50; ++i) { + NIOServerCnxnFactory factory = new NIOServerCnxnFactory(); + factory.configure(new InetSocketAddress("127.0.0.1", PortAssignment.unique()), 10); + factory.start(); + Thread.sleep(100); + factory.shutdown(); + } + + long endFdCount = osMbean.getOpenFileDescriptorCount(); + LOG.info("End fdcount is: {}", endFdCount); + + // On my box, if selector.close() is not called fd diff is > 700. + assertTrue("Possible fd leakage", ((endFdCount - startFdCount) < 50)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteBase.java new file mode 100644 index 0000000..ffc2b76 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteBase.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.ClientCnxnSocketNetty; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.server.NettyServerCnxnFactory; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Run tests with: Netty Client against Netty server + */ +@RunWith(Suite.class) +public class NettyNettySuiteBase { + + @BeforeClass + public static void setUp() { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName()); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, ClientCnxnSocketNetty.class.getName()); + System.setProperty("zookeeper.admin.enableServer", "false"); + } + + @AfterClass + public static void tearDown() { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + } + + @Before + public void setUpTest() throws Exception { + TestByteBufAllocatorTestHelper.setTestAllocator(TestByteBufAllocator.getInstance()); + } + + @After + public void tearDownTest() throws Exception { + TestByteBufAllocatorTestHelper.clearTestAllocator(); + TestByteBufAllocator.checkForLeaks(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteHammerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteHammerTest.java new file mode 100644 index 0000000..97f4ee1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteHammerTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.junit.runners.Suite; + +/** + * Run tests with: Netty Client against Netty server + */ +@Suite.SuiteClasses({AsyncHammerTest.class}) +public class NettyNettySuiteHammerTest extends NettyNettySuiteBase { + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteTest.java new file mode 100644 index 0000000..5fde744 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NettyNettySuiteTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.junit.runners.Suite; + +/** + * Run tests with: Netty Client against Netty server + */ +@Suite.SuiteClasses({ACLTest.class, AsyncOpsTest.class, ChrootClientTest.class, ClientTest.class, FourLetterWordsTest.class, NullDataTest.class, SessionTest.class, WatcherTest.class, ReconfigTest.class}) +public class NettyNettySuiteTest extends NettyNettySuiteBase { + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteBase.java new file mode 100644 index 0000000..1d6b821 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteBase.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.server.NettyServerCnxnFactory; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * Run tests with: Nio Client against Netty server + */ +@RunWith(Suite.class) +public class NioNettySuiteBase { + + @BeforeClass + public static void setUp() { + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, NettyServerCnxnFactory.class.getName()); + System.setProperty("zookeeper.admin.enableServer", "false"); + } + + @AfterClass + public static void tearDown() { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } + + @Before + public void setUpTest() throws Exception { + TestByteBufAllocatorTestHelper.setTestAllocator(TestByteBufAllocator.getInstance()); + } + + @After + public void tearDownTest() throws Exception { + TestByteBufAllocatorTestHelper.clearTestAllocator(); + TestByteBufAllocator.checkForLeaks(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteHammerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteHammerTest.java new file mode 100644 index 0000000..1c1a6cc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteHammerTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.junit.runners.Suite; + +/** + * Run tests with: Nio Client against Netty server + */ +@Suite.SuiteClasses({AsyncHammerTest.class}) +public class NioNettySuiteHammerTest extends NioNettySuiteBase { + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteTest.java new file mode 100644 index 0000000..24fc60a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NioNettySuiteTest.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.junit.runners.Suite; + +/** + * Run tests with: Nio Client against Netty server + */ +@Suite.SuiteClasses({ACLTest.class, AsyncOpsTest.class, ChrootClientTest.class, ClientTest.class, FourLetterWordsTest.class, NullDataTest.class, SessionTest.class, WatcherTest.class, ReconfigTest.class}) +public class NioNettySuiteTest extends NioNettySuiteBase { + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NonRecoverableErrorTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NonRecoverableErrorTest.java new file mode 100644 index 0000000..f729920 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NonRecoverableErrorTest.java @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.UUID; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.Test; + +/** + * This class tests the non-recoverable error behavior of quorum server. + */ +public class NonRecoverableErrorTest extends QuorumPeerTestBase { + + private static final String NODE_PATH = "/noLeaderIssue"; + + /** + * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247. + * Test to verify that even after non recoverable error (error while + * writing transaction log), ZooKeeper is still available. + */ + @Test(timeout = 30000) + public void testZooKeeperServiceAvailableOnLeader() throws Exception { + int SERVER_COUNT = 3; + final int[] clientPorts = new int[SERVER_COUNT]; + StringBuilder sb = new StringBuilder(); + String server; + + for (int i = 0; i < SERVER_COUNT; i++) { + clientPorts[i] = PortAssignment.unique(); + server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + + ":participant;127.0.0.1:" + clientPorts[i]; + sb.append(server + "\n"); + } + String currentQuorumCfgSection = sb.toString(); + MainThread[] mt = new MainThread[SERVER_COUNT]; + + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false); + mt[i].start(); + } + + // ensure server started + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + + String data = "originalData"; + zk.create(NODE_PATH, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // get information of current leader + QuorumPeer leader = getLeaderQuorumPeer(mt); + assertNotNull("Leader must have been elected by now", leader); + + // inject problem in leader + FileTxnSnapLog snapLog = leader.getActiveServer().getTxnLogFactory(); + FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) { + @Override + public void commit() throws IOException { + throw new IOException("Input/output error"); + } + }; + ZKDatabase originalZKDatabase = leader.getActiveServer().getZKDatabase(); + long leaderCurrentEpoch = leader.getCurrentEpoch(); + + ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError); + leader.getActiveServer().setZKDatabase(newDB); + + try { + // do create operation, so that injected IOException is thrown + zk.create(uniqueZnode(), data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("IOException is expected due to error injected to transaction log commit"); + } catch (Exception e) { + // do nothing + } + + // resetting watcher so that this watcher can be again used to ensure + // that the zkClient is able to re-establish connection with the + // newly elected zookeeper quorum. + watcher.reset(); + waitForNewLeaderElection(leader, leaderCurrentEpoch); + + // ensure server started, give enough time, so that new leader election + // takes place + for (int i = 0; i < SERVER_COUNT; i++) { + assertTrue( + "waiting for server " + i + " being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT)); + } + + // revert back the error + leader.getActiveServer().setZKDatabase(originalZKDatabase); + + // verify that now ZooKeeper service is up and running + leader = getLeaderQuorumPeer(mt); + assertNotNull("New leader must have been elected by now", leader); + + String uniqueNode = uniqueZnode(); + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + String createNode = zk.create(uniqueNode, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + // if node is created successfully then it means that ZooKeeper service + // is available + assertEquals("Failed to create znode", uniqueNode, createNode); + zk.close(); + // stop all severs + for (int i = 0; i < SERVER_COUNT; i++) { + mt[i].shutdown(); + } + } + + private void waitForNewLeaderElection(QuorumPeer peer, long leaderCurrentEpoch) throws IOException, InterruptedException { + LOG.info("Waiting for new LE cycle.."); + int count = 100; // giving a grace period of 10seconds + while (count > 0) { + if (leaderCurrentEpoch == peer.getCurrentEpoch()) { + Thread.sleep(100); + } + count--; + } + assertNotEquals("New LE cycle must have triggered", leaderCurrentEpoch, peer.getCurrentEpoch()); + } + + private QuorumPeer getLeaderQuorumPeer(MainThread[] mt) { + for (int i = mt.length - 1; i >= 0; i--) { + QuorumPeer quorumPeer = mt[i].getQuorumPeer(); + if (null != quorumPeer && ServerState.LEADING == quorumPeer.getPeerState()) { + return quorumPeer; + } + } + return null; + } + + private String uniqueZnode() { + UUID randomUUID = UUID.randomUUID(); + String node = NODE_PATH + "/" + randomUUID.toString(); + return node; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/NullDataTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NullDataTest.java new file mode 100644 index 0000000..9356ee4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/NullDataTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertSame; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.Test; + +public class NullDataTest extends ClientBase implements StatCallback { + + String snapCount; + CountDownLatch cn = new CountDownLatch(1); + + @Override + public void setUp() throws Exception { + // Change the snapcount to happen more often + snapCount = System.getProperty("zookeeper.snapCount", "1024"); + System.setProperty("zookeeper.snapCount", "10"); + super.setUp(); + } + + @Override + public void tearDown() throws Exception { + System.setProperty("zookeeper.snapCount", snapCount); + super.tearDown(); + } + + @Test + public void testNullData() throws IOException, InterruptedException, KeeperException { + String path = "/SIZE"; + ZooKeeper zk = null; + zk = createClient(); + try { + zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + // try sync zk exists + zk.exists(path, false); + zk.exists(path, false, this, null); + cn.await(10, TimeUnit.SECONDS); + assertSame(0L, cn.getCount()); + } finally { + if (zk != null) { + zk.close(); + } + } + + } + + public void processResult(int rc, String path, Object ctx, Stat stat) { + cn.countDown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/OOMTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/OOMTest.java new file mode 100644 index 0000000..05d02ae --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/OOMTest.java @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Ignore; +import org.junit.Test; + +public class OOMTest extends ZKTestCase { + + private static final Watcher TEST_WATCHER = event -> System.err.println("Got event: " + event); + + @Test + @Ignore + public void testOOM() throws IOException, InterruptedException, KeeperException { + File tmpDir = ClientBase.createTmpDir(); + // Grab some memory so that it is easier to cause an + // OOM condition; + List hog = new ArrayList<>(); + while (true) { + try { + hog.add(new byte[1024 * 1024 * 2]); + } catch (OutOfMemoryError e) { + hog.remove(0); + break; + } + } + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + + final int PORT = PortAssignment.unique(); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); + + System.err.println("OOM Stage 0"); + utestPrep(PORT); + System.out.println("Free = " + Runtime.getRuntime().freeMemory() + + " total = " + Runtime.getRuntime().totalMemory() + + " max = " + Runtime.getRuntime().maxMemory()); + System.err.println("OOM Stage 1"); + for (int i = 0; i < 1000; i++) { + System.out.println(i); + utestExists(PORT); + } + System.out.println("Free = " + Runtime.getRuntime().freeMemory() + + " total = " + Runtime.getRuntime().totalMemory() + + " max = " + Runtime.getRuntime().maxMemory()); + System.err.println("OOM Stage 2"); + for (int i = 0; i < 1000; i++) { + System.out.println(i); + utestGet(PORT); + } + System.out.println("Free = " + Runtime.getRuntime().freeMemory() + + " total = " + Runtime.getRuntime().totalMemory() + + " max = " + Runtime.getRuntime().maxMemory()); + System.err.println("OOM Stage 3"); + for (int i = 0; i < 1000; i++) { + System.out.println(i); + utestChildren(PORT); + } + System.out.println("Free = " + Runtime.getRuntime().freeMemory() + + " total = " + Runtime.getRuntime().totalMemory() + + " max = " + Runtime.getRuntime().maxMemory()); + hog.get(0)[0] = (byte) 1; + + f.shutdown(); + zks.shutdown(); + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); + } + + private void utestExists(int port) throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, TEST_WATCHER); + for (int i = 0; i < 10000; i++) { + zk.exists("/this/path/doesnt_exist!", true); + } + zk.close(); + } + + private void utestPrep(int port) throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, TEST_WATCHER); + for (int i = 0; i < 10000; i++) { + zk.create("/" + i, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + } + + private void utestGet(int port) throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, TEST_WATCHER); + for (int i = 0; i < 10000; i++) { + Stat stat = new Stat(); + zk.getData("/" + i, true, stat); + } + zk.close(); + } + + private void utestChildren(int port) throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, TEST_WATCHER); + for (int i = 0; i < 10000; i++) { + zk.getChildren("/" + i, true); + } + zk.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/OSMXBeanTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/OSMXBeanTest.java new file mode 100644 index 0000000..edc90fe --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/OSMXBeanTest.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.util.OSMXBean; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OSMXBeanTest extends ZKTestCase { + + private OSMXBean osMbean; + private Long ofdc = 0L; + private Long mfdc = 0L; + protected static final Logger LOG = LoggerFactory.getLogger(OSMXBeanTest.class); + + @Before + public void initialize() { + this.osMbean = new OSMXBean(); + assertNotNull("Could not initialize OSMXBean object!", osMbean); + } + + @Test + public final void testGetUnix() { + boolean isUnix = osMbean.getUnix(); + if (!isUnix) { + LOG.info("Running in a Windows system! Output won't be printed!"); + } else { + LOG.info("Running in a Unix or Linux system!"); + } + } + + @Test + public final void testGetOpenFileDescriptorCount() { + if (osMbean != null && osMbean.getUnix()) { + ofdc = osMbean.getOpenFileDescriptorCount(); + LOG.info("open fdcount is: {}", ofdc); + } + assertFalse("The number of open file descriptor is negative", (ofdc < 0)); + } + + @Test + public final void testGetMaxFileDescriptorCount() { + if (osMbean != null && osMbean.getUnix()) { + mfdc = osMbean.getMaxFileDescriptorCount(); + LOG.info("max fdcount is: {}", mfdc); + } + assertFalse("The max file descriptor number is negative", (mfdc < 0)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverHierarchicalQuorumTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverHierarchicalQuorumTest.java new file mode 100644 index 0000000..fd16091 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverHierarchicalQuorumTest.java @@ -0,0 +1,41 @@ +/* Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.junit.Test; + +public class ObserverHierarchicalQuorumTest extends HierarchicalQuorumTest { + + /** + * startServers(true) puts two observers into a 5 peer ensemble + */ + void startServers() throws Exception { + startServers(true); + } + + protected void shutdown(QuorumPeer qp) { + QuorumBase.shutdown(qp); + } + + @Test + public void testHierarchicalQuorum() throws Throwable { + cht.runHammer(5, 10); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverLETest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverLETest.java new file mode 100644 index 0000000..273b05e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverLETest.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumStats; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ObserverLETest extends ZKTestCase { + + final QuorumBase qb = new QuorumBase(); + final ClientTest ct = new ClientTest(); + + @Before + public void establishThreeParticipantOneObserverEnsemble() throws Exception { + qb.setUp(true); + ct.hostPort = qb.hostPort; + ct.setUpAll(); + qb.s5.shutdown(); + } + + @After + public void shutdownQuorum() throws Exception { + ct.tearDownAll(); + qb.tearDown(); + } + + /** + * See ZOOKEEPER-1294. Confirms that an observer will not support the quorum + * of a leader by forming a 5-node, 2-observer ensemble (so quorum size is 2). + * When all but the leader and one observer are shut down, the leader should + * enter the 'looking' state, not stay in the 'leading' state. + */ + @Test + public void testLEWithObserver() throws Exception { + QuorumPeer leader = null; + for (QuorumPeer server : Arrays.asList(qb.s1, qb.s2, qb.s3)) { + if (server.getServerState().equals(QuorumStats.Provider.FOLLOWING_STATE)) { + server.shutdown(); + assertTrue( + "Waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + server.getClientPort(), ClientBase.CONNECTION_TIMEOUT)); + } else { + assertNull("More than one leader found", leader); + leader = server; + } + } + assertTrue( + "Leader is not in Looking state", + ClientBase.waitForServerState(leader, ClientBase.CONNECTION_TIMEOUT, QuorumStats.Provider.LOOKING_STATE)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java new file mode 100644 index 0000000..ee54a7a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverMasterTest.java @@ -0,0 +1,744 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import javax.management.Attribute; +import javax.management.AttributeNotFoundException; +import javax.management.InstanceNotFoundException; +import javax.management.InvalidAttributeValueException; +import javax.management.MBeanException; +import javax.management.MalformedObjectNameException; +import javax.management.ObjectName; +import javax.management.ReflectionException; +import javax.management.RuntimeMBeanException; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.ConnectionLossException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.admin.Commands; +import org.apache.zookeeper.server.quorum.DelayRequestProcessor; +import org.apache.zookeeper.server.quorum.FollowerZooKeeperServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.server.util.PortForwarder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(Parameterized.class) +public class ObserverMasterTest extends QuorumPeerTestBase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(ObserverMasterTest.class); + + public ObserverMasterTest(Boolean testObserverMaster) { + this.testObserverMaster = testObserverMaster; + } + + @Parameterized.Parameters + public static List data() { + return Arrays.asList(new Object[][]{{Boolean.TRUE}, {Boolean.FALSE}}); + } + + private Boolean testObserverMaster; + + private CountDownLatch latch; + ZooKeeper zk; + private WatchedEvent lastEvent = null; + + private int CLIENT_PORT_QP1; + private int CLIENT_PORT_QP2; + private int CLIENT_PORT_OBS; + private int OM_PORT; + private MainThread q1; + private MainThread q2; + private MainThread q3; + + private PortForwarder setUp(final int omProxyPort) throws IOException { + ClientBase.setupTestEnv(); + + final int PORT_QP1 = PortAssignment.unique(); + final int PORT_QP2 = PortAssignment.unique(); + final int PORT_OBS = PortAssignment.unique(); + final int PORT_QP_LE1 = PortAssignment.unique(); + final int PORT_QP_LE2 = PortAssignment.unique(); + final int PORT_OBS_LE = PortAssignment.unique(); + + CLIENT_PORT_QP1 = PortAssignment.unique(); + CLIENT_PORT_QP2 = PortAssignment.unique(); + CLIENT_PORT_OBS = PortAssignment.unique(); + + OM_PORT = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + (PORT_QP1) + ":" + (PORT_QP_LE1) + ";" + CLIENT_PORT_QP1 + + "\nserver.2=127.0.0.1:" + (PORT_QP2) + ":" + (PORT_QP_LE2) + ";" + CLIENT_PORT_QP2 + + "\nserver.3=127.0.0.1:" + (PORT_OBS) + ":" + (PORT_OBS_LE) + ":observer" + ";" + CLIENT_PORT_OBS; + String extraCfgs = testObserverMaster + ? String.format("observerMasterPort=%d%n", OM_PORT) + : ""; + String extraCfgsObs = testObserverMaster + ? String.format("observerMasterPort=%d%n", omProxyPort <= 0 ? OM_PORT : omProxyPort) + : ""; + + PortForwarder forwarder = null; + if (testObserverMaster && omProxyPort >= 0) { + forwarder = new PortForwarder(omProxyPort, OM_PORT); + } + + q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection, extraCfgs); + q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, extraCfgs); + q3 = new MainThread(3, CLIENT_PORT_OBS, quorumCfgSection, extraCfgsObs); + q1.start(); + q2.start(); + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT)); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + return forwarder; + } + + private void shutdown() throws InterruptedException { + LOG.info("Shutting down all servers"); + zk.close(); + + q1.shutdown(); + q2.shutdown(); + q3.shutdown(); + + assertTrue( + "Waiting for server 1 to shut down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "Waiting for server 2 to shut down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + assertTrue( + "Waiting for server 3 to shut down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT)); + } + + @Test + public void testLaggingObserverMaster() throws Exception { + final int OM_PROXY_PORT = PortAssignment.unique(); + PortForwarder forwarder = setUp(OM_PROXY_PORT); + + // find the leader and observer master + int leaderPort; + MainThread leader; + MainThread follower; + if (q1.getQuorumPeer().leader != null) { + leaderPort = CLIENT_PORT_QP1; + leader = q1; + follower = q2; + } else if (q2.getQuorumPeer().leader != null) { + leaderPort = CLIENT_PORT_QP2; + leader = q2; + follower = q1; + } else { + throw new RuntimeException("No leader"); + } + + // ensure the observer master has commits in the queue before observer sync + zk = new ZooKeeper("127.0.0.1:" + leaderPort, ClientBase.CONNECTION_TIMEOUT, this); + for (int i = 0; i < 10; i++) { + zk.create("/bulk" + i, ("initial data of some size").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + + q3.start(); + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + + latch = new CountDownLatch(1); + zk = new ZooKeeper("127.0.0.1:" + leaderPort, ClientBase.CONNECTION_TIMEOUT, this); + latch.await(); + assertEquals(zk.getState(), States.CONNECTED); + + zk.create("/init", "first".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final long lastLoggedZxid = leader.getQuorumPeer().getLastLoggedZxid(); + + // wait for change to propagate + waitFor("Timeout waiting for observer sync", new WaitForCondition() { + public boolean evaluate() { + return lastLoggedZxid == q3.getQuorumPeer().getLastLoggedZxid(); + } + }, 30); + + // simulate network fault + if (forwarder != null) { + forwarder.shutdown(); + } + + for (int i = 0; i < 10; i++) { + zk.create("/basic" + i, "second".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + DelayRequestProcessor delayRequestProcessor = null; + if (testObserverMaster) { + FollowerZooKeeperServer followerZooKeeperServer = (FollowerZooKeeperServer) follower.getQuorumPeer().getActiveServer(); + delayRequestProcessor = DelayRequestProcessor.injectDelayRequestProcessor(followerZooKeeperServer); + } + + zk.create("/target1", "third".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/target2", "third".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + LOG.info( + "observer zxid {}{} leader zxid {}", + Long.toHexString(q3.getQuorumPeer().getLastLoggedZxid()), + (testObserverMaster ? "" : " observer master zxid " + Long.toHexString(follower.getQuorumPeer().getLastLoggedZxid())), + Long.toHexString(leader.getQuorumPeer().getLastLoggedZxid())); + + // restore network + forwarder = testObserverMaster ? new PortForwarder(OM_PROXY_PORT, OM_PORT) : null; + + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + assertNotNull("Leader switched", leader.getQuorumPeer().leader); + + if (delayRequestProcessor != null) { + delayRequestProcessor.unblockQueue(); + } + + latch = new CountDownLatch(1); + ZooKeeper obsZk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT, this); + latch.await(); + zk.create("/finalop", "fourth".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertEquals("first", new String(obsZk.getData("/init", null, null))); + assertEquals("third", new String(obsZk.getData("/target1", null, null))); + + obsZk.close(); + shutdown(); + + try { + if (forwarder != null) { + forwarder.shutdown(); + } + } catch (Exception e) { + // ignore + } + } + + /** + * This test ensures two things: + * 1. That Observers can successfully proxy requests to the ensemble. + * 2. That Observers don't participate in leader elections. + * The second is tested by constructing an ensemble where a leader would + * be elected if and only if an Observer voted. + */ + @Test + public void testObserver() throws Exception { + // We expect two notifications before we want to continue + latch = new CountDownLatch(2); + setUp(-1); + q3.start(); + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + + if (testObserverMaster) { + int masterPort = q3.getQuorumPeer().observer.getSocket().getPort(); + LOG.info("port {} {}", masterPort, OM_PORT); + assertEquals("observer failed to connect to observer master", masterPort, OM_PORT); + } + + zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT, this); + zk.create("/obstest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // Assert that commands are getting forwarded correctly + assertEquals(new String(zk.getData("/obstest", null, null)), "test"); + + // Now check that other commands don't blow everything up + zk.sync("/", null, null); + zk.setData("/obstest", "test2".getBytes(), -1); + zk.getChildren("/", false); + + assertEquals(zk.getState(), States.CONNECTED); + + LOG.info("Shutting down server 2"); + // Now kill one of the other real servers + q2.shutdown(); + + assertTrue( + "Waiting for server 2 to shut down", + ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT)); + + LOG.info("Server 2 down"); + + // Now the resulting ensemble shouldn't be quorate + latch.await(); + assertNotSame("Client is still connected to non-quorate cluster", KeeperState.SyncConnected, lastEvent.getState()); + + LOG.info("Latch returned"); + + try { + assertNotEquals("Shouldn't get a response when cluster not quorate!", "test", new String(zk.getData("/obstest", null, null))); + } catch (ConnectionLossException c) { + LOG.info("Connection loss exception caught - ensemble not quorate (this is expected)"); + } + + latch = new CountDownLatch(1); + + LOG.info("Restarting server 2"); + + // Bring it back + //q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection, extraCfgs); + q2.start(); + + LOG.info("Waiting for server 2 to come up"); + assertTrue( + "waiting for server 2 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT)); + + LOG.info("Server 2 started, waiting for latch"); + + latch.await(); + // It's possible our session expired - but this is ok, shows we + // were able to talk to the ensemble + assertTrue("Client didn't reconnect to quorate ensemble (state was" + + lastEvent.getState() + + ")", (KeeperState.SyncConnected == lastEvent.getState() + || KeeperState.Expired == lastEvent.getState())); + + LOG.info("perform a revalidation test"); + int leaderProxyPort = PortAssignment.unique(); + int obsProxyPort = PortAssignment.unique(); + int leaderPort = q1.getQuorumPeer().leader == null ? CLIENT_PORT_QP2 : CLIENT_PORT_QP1; + PortForwarder leaderPF = new PortForwarder(leaderProxyPort, leaderPort); + + latch = new CountDownLatch(1); + ZooKeeper client = new ZooKeeper(String.format("127.0.0.1:%d,127.0.0.1:%d", leaderProxyPort, obsProxyPort), ClientBase.CONNECTION_TIMEOUT, this); + latch.await(); + client.create("/revalidtest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertNotNull("Read-after write failed", client.exists("/revalidtest", null)); + + latch = new CountDownLatch(2); + PortForwarder obsPF = new PortForwarder(obsProxyPort, CLIENT_PORT_OBS); + try { + leaderPF.shutdown(); + } catch (Exception e) { + // ignore? + } + latch.await(); + assertEquals(new String(client.getData("/revalidtest", null, null)), "test"); + client.close(); + obsPF.shutdown(); + + shutdown(); + } + + @Test + public void testRevalidation() throws Exception { + setUp(-1); + q3.start(); + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + final int leaderProxyPort = PortAssignment.unique(); + final int obsProxyPort = PortAssignment.unique(); + + int leaderPort = q1.getQuorumPeer().leader == null ? CLIENT_PORT_QP2 : CLIENT_PORT_QP1; + PortForwarder leaderPF = new PortForwarder(leaderProxyPort, leaderPort); + + latch = new CountDownLatch(1); + zk = new ZooKeeper(String.format("127.0.0.1:%d,127.0.0.1:%d", leaderProxyPort, obsProxyPort), ClientBase.CONNECTION_TIMEOUT, this); + latch.await(); + zk.create("/revalidtest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertNotNull("Read-after write failed", zk.exists("/revalidtest", null)); + + latch = new CountDownLatch(2); + PortForwarder obsPF = new PortForwarder(obsProxyPort, CLIENT_PORT_OBS); + try { + leaderPF.shutdown(); + } catch (Exception e) { + // ignore? + } + latch.await(); + assertEquals(new String(zk.getData("/revalidtest", null, null)), "test"); + obsPF.shutdown(); + + shutdown(); + } + + @Test + public void testInOrderCommits() throws Exception { + setUp(-1); + + zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, null); + for (int i = 0; i < 10; i++) { + zk.create("/bulk" + + i, ("Initial data of some size").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + + q3.start(); + assertTrue( + "waiting for observer to be up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + + latch = new CountDownLatch(1); + zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, this); + latch.await(); + assertEquals(zk.getState(), States.CONNECTED); + + zk.create("/init", "first".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + final long zxid = q1.getQuorumPeer().getLastLoggedZxid(); + + // wait for change to propagate + waitFor("Timeout waiting for observer sync", new WaitForCondition() { + public boolean evaluate() { + return zxid == q3.getQuorumPeer().getLastLoggedZxid(); + } + }, 30); + + ZooKeeper obsZk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT, this); + int followerPort = q1.getQuorumPeer().leader == null ? CLIENT_PORT_QP1 : CLIENT_PORT_QP2; + ZooKeeper fZk = new ZooKeeper("127.0.0.1:" + followerPort, ClientBase.CONNECTION_TIMEOUT, this); + final int numTransactions = 10001; + CountDownLatch gate = new CountDownLatch(1); + CountDownLatch oAsyncLatch = new CountDownLatch(numTransactions); + Thread oAsyncWriteThread = new Thread(new AsyncWriter(obsZk, numTransactions, true, oAsyncLatch, "/obs", gate)); + CountDownLatch fAsyncLatch = new CountDownLatch(numTransactions); + Thread fAsyncWriteThread = new Thread(new AsyncWriter(fZk, numTransactions, true, fAsyncLatch, "/follower", gate)); + + LOG.info("ASYNC WRITES"); + oAsyncWriteThread.start(); + fAsyncWriteThread.start(); + gate.countDown(); + + oAsyncLatch.await(); + fAsyncLatch.await(); + + oAsyncWriteThread.join(ClientBase.CONNECTION_TIMEOUT); + if (oAsyncWriteThread.isAlive()) { + LOG.error("asyncWriteThread is still alive"); + } + fAsyncWriteThread.join(ClientBase.CONNECTION_TIMEOUT); + if (fAsyncWriteThread.isAlive()) { + LOG.error("asyncWriteThread is still alive"); + } + + obsZk.close(); + fZk.close(); + + shutdown(); + } + + @Test + public void testAdminCommands() throws IOException, MBeanException, InstanceNotFoundException, ReflectionException, InterruptedException, MalformedObjectNameException, AttributeNotFoundException, InvalidAttributeValueException, KeeperException { + // flush all beans, then start + for (ZKMBeanInfo beanInfo : MBeanRegistry.getInstance().getRegisteredBeans()) { + MBeanRegistry.getInstance().unregister(beanInfo); + } + + JMXEnv.setUp(); + setUp(-1); + q3.start(); + assertTrue( + "waiting for observer to be up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + + // Assert that commands are getting forwarded correctly + zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT, this); + zk.create("/obstest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEquals(new String(zk.getData("/obstest", null, null)), "test"); + + // test stats collection + final Map emptyMap = Collections.emptyMap(); + Map stats = Commands.runCommand("mntr", q3.getQuorumPeer().getActiveServer(), emptyMap).toMap(); + assertTrue("observer not emitting observer_master_id", stats.containsKey("observer_master_id")); + + // check the stats for the first peer + if (testObserverMaster) { + if (q1.getQuorumPeer().leader == null) { + assertEquals(Integer.valueOf(1), q1.getQuorumPeer().getSynced_observers_metric()); + } else { + assertEquals(Integer.valueOf(0), q1.getQuorumPeer().getSynced_observers_metric()); + } + } else { + if (q1.getQuorumPeer().leader == null) { + assertNull(q1.getQuorumPeer().getSynced_observers_metric()); + } else { + assertEquals(Integer.valueOf(1), q1.getQuorumPeer().getSynced_observers_metric()); + } + } + + // check the stats for the second peer + if (testObserverMaster) { + if (q2.getQuorumPeer().leader == null) { + assertEquals(Integer.valueOf(1), q2.getQuorumPeer().getSynced_observers_metric()); + } else { + assertEquals(Integer.valueOf(0), q2.getQuorumPeer().getSynced_observers_metric()); + } + } else { + if (q2.getQuorumPeer().leader == null) { + assertNull(q2.getQuorumPeer().getSynced_observers_metric()); + } else { + assertEquals(Integer.valueOf(1), q2.getQuorumPeer().getSynced_observers_metric()); + } + } + + // test admin commands for disconnection + ObjectName connBean = null; + for (ObjectName bean : JMXEnv.conn().queryNames(new ObjectName(MBeanRegistry.DOMAIN + ":*"), null)) { + if (bean.getCanonicalName().contains("Learner_Connections") && bean.getCanonicalName().contains("id:" + + q3.getQuorumPeer().getId())) { + connBean = bean; + break; + } + } + assertNotNull("could not find connection bean", connBean); + + latch = new CountDownLatch(1); + JMXEnv.conn().invoke(connBean, "terminateConnection", new Object[0], null); + assertTrue("server failed to disconnect on terminate", latch.await(CONNECTION_TIMEOUT + / 2, TimeUnit.MILLISECONDS)); + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + + final String obsBeanName = String.format("org.apache.ZooKeeperService:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Observer", q3.getQuorumPeer().getId(), q3.getQuorumPeer().getId()); + Set names = JMXEnv.conn().queryNames(new ObjectName(obsBeanName), null); + assertEquals("expecting singular observer bean", 1, names.size()); + ObjectName obsBean = names.iterator().next(); + + if (testObserverMaster) { + // show we can move the observer using the id + long observerMasterId = q3.getQuorumPeer().observer.getLearnerMasterId(); + latch = new CountDownLatch(1); + JMXEnv.conn().setAttribute(obsBean, new Attribute("LearnerMaster", Long.toString(3 - observerMasterId))); + assertTrue("server failed to disconnect on terminate", latch.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)); + assertTrue( + "waiting for server 3 being up", + ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT)); + } else { + // show we get an error + final long leaderId = q1.getQuorumPeer().leader == null ? 2 : 1; + try { + JMXEnv.conn().setAttribute(obsBean, new Attribute("LearnerMaster", Long.toString(3 - leaderId))); + fail("should have seen an exception on previous command"); + } catch (RuntimeMBeanException e) { + assertEquals("mbean failed for the wrong reason", IllegalArgumentException.class, e.getCause().getClass()); + } + } + + shutdown(); + JMXEnv.tearDown(); + } + + private String createServerString(String type, long serverId, int clientPort) { + return "server." + serverId + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":" + type + ";" + clientPort; + } + + private void waitServerUp(int clientPort) { + assertTrue( + "waiting for server being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); + } + + private ZooKeeperAdmin createAdmin(int clientPort) throws IOException { + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + QuorumPeerConfig.setReconfigEnabled(true); + ZooKeeperAdmin admin = new ZooKeeperAdmin( + "127.0.0.1:" + clientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + admin.addAuthInfo("digest", "super:test".getBytes()); + return admin; + } + + // This test is known to be flaky and fail due to "reconfig already in progress". + // TODO: Investigate intermittent testDynamicReconfig failures. + // @Test + public void testDynamicReconfig() throws InterruptedException, IOException, KeeperException { + if (!testObserverMaster) { + return; + } + + ClientBase.setupTestEnv(); + + // create a quorum running with different observer master port + // to make it easier to choose which server the observer is + // following with + // + // we have setObserverMaster function but it's broken, use this + // solution before we fixed that + int clientPort1 = PortAssignment.unique(); + int clientPort2 = PortAssignment.unique(); + int omPort1 = PortAssignment.unique(); + int omPort2 = PortAssignment.unique(); + String quorumCfgSection = createServerString("participant", 1, clientPort1) + + "\n" + + createServerString("participant", 2, clientPort2); + + MainThread s1 = new MainThread(1, clientPort1, quorumCfgSection, String.format("observerMasterPort=%d%n", omPort1)); + MainThread s2 = new MainThread(2, clientPort2, quorumCfgSection, String.format("observerMasterPort=%d%n", omPort2)); + s1.start(); + s2.start(); + waitServerUp(clientPort1); + waitServerUp(clientPort2); + + // create observer to follow non-leader observer master + long nonLeaderOMPort = s1.getQuorumPeer().leader == null ? omPort1 : omPort2; + int observerClientPort = PortAssignment.unique(); + int observerId = 10; + MainThread observer = new MainThread( + observerId, + observerClientPort, + quorumCfgSection + "\n" + createServerString("observer", observerId, observerClientPort), + String.format("observerMasterPort=%d%n", nonLeaderOMPort)); + LOG.info("starting observer"); + observer.start(); + waitServerUp(observerClientPort); + + // create a client to the observer + final LinkedBlockingQueue states = new LinkedBlockingQueue(); + ZooKeeper observerClient = new ZooKeeper( + "127.0.0.1:" + observerClientPort, + ClientBase.CONNECTION_TIMEOUT, + event -> { + try { + states.put(event.getState()); + } catch (InterruptedException ignore) { + + } + }); + + // wait for connected + KeeperState state = states.poll(1000, TimeUnit.MILLISECONDS); + assertEquals(KeeperState.SyncConnected, state); + + // issue reconfig command + ArrayList newServers = new ArrayList(); + String server = "server.3=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;localhost:" + PortAssignment.unique(); + newServers.add(server); + ZooKeeperAdmin admin = createAdmin(clientPort1); + ReconfigTest.reconfig(admin, newServers, null, null, -1); + + // make sure the observer has the new config + ReconfigTest.testServerHasConfig(observerClient, newServers, null); + + // shouldn't be disconnected during reconfig, so expect to not + // receive any new event + state = states.poll(1000, TimeUnit.MILLISECONDS); + assertNull(state); + + admin.close(); + observerClient.close(); + observer.shutdown(); + s2.shutdown(); + s1.shutdown(); + } + + /** + * Implementation of watcher interface. + */ + public void process(WatchedEvent event) { + lastEvent = event; + if (latch != null) { + latch.countDown(); + } + LOG.info("Latch got event :: {}", event); + } + + class AsyncWriter implements Runnable { + + private final ZooKeeper client; + private final int numTransactions; + private final boolean issueSync; + private final CountDownLatch writerLatch; + private final String root; + private final CountDownLatch gate; + + AsyncWriter(ZooKeeper client, int numTransactions, boolean issueSync, CountDownLatch writerLatch, String root, CountDownLatch gate) { + this.client = client; + this.numTransactions = numTransactions; + this.issueSync = issueSync; + this.writerLatch = writerLatch; + this.root = root; + this.gate = gate; + } + + @Override + public void run() { + if (gate != null) { + try { + gate.await(); + } catch (InterruptedException e) { + LOG.error("Gate interrupted"); + return; + } + } + for (int i = 0; i < numTransactions; i++) { + final boolean pleaseLog = i % 100 == 0; + client.create(root + + i, "inner thread".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() { + @Override + public void processResult(int rc, String path, Object ctx, String name) { + writerLatch.countDown(); + if (pleaseLog) { + LOG.info("wrote {}", path); + } + } + }, null); + if (pleaseLog) { + LOG.info("async wrote {}{}", root, i); + if (issueSync) { + client.sync(root + "0", null, null); + } + } + } + } + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverQuorumHammerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverQuorumHammerTest.java new file mode 100644 index 0000000..7f81e48 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverQuorumHammerTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.junit.Before; +import org.junit.Test; + +public class ObserverQuorumHammerTest extends QuorumHammerTest { + + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + @Before + @Override + public void setUp() throws Exception { + qb.setUp(true); + cht.hostPort = qb.hostPort; + cht.setUpAll(); + } + + @Test + public void testHammerBasic() throws Throwable { + cht.testHammerBasic(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverTest.java new file mode 100644 index 0000000..a083c0d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ObserverTest.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ObserverTest extends QuorumPeerTestBase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(ObserverTest.class); + + ZooKeeper zk; + + /** + * This test ensures that an Observer does not elect itself as a leader, or + * indeed come up properly, if it is the lone member of an ensemble. + * @throws Exception + */ + @Test + public void testObserverOnly() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer;" + CLIENT_PORT_QP1 + "\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + q1.join(ClientBase.CONNECTION_TIMEOUT); + assertFalse(q1.isAlive()); + } + + /** + * Ensure that observer only comes up when a proper ensemble is configured. + * (and will not come up with standalone server). + */ + @Test + public void testObserverWithStandlone() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT_QP1 = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":observer\n" + + "server.2=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + "\npeerType=observer\n"; + + MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection); + q1.start(); + q1.join(ClientBase.CONNECTION_TIMEOUT); + assertFalse(q1.isAlive()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java new file mode 100644 index 0000000..67f19dc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java @@ -0,0 +1,174 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.AddWatchMode.PERSISTENT_RECURSIVE; +import java.io.IOException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PersistentRecursiveWatcherTest extends ClientBase { + private static final Logger LOG = LoggerFactory.getLogger(PersistentRecursiveWatcherTest.class); + private BlockingQueue events; + private Watcher persistentWatcher; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + + events = new LinkedBlockingQueue<>(); + persistentWatcher = event -> events.add(event); + } + + @Test + public void testBasic() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE); + internalTestBasic(zk); + } + } + + @Test + public void testBasicAsync() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + final CountDownLatch latch = new CountDownLatch(1); + AsyncCallback.VoidCallback cb = (rc, path, ctx) -> { + if (rc == 0) { + latch.countDown(); + } + }; + zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE, cb, null); + Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); + internalTestBasic(zk); + } + } + + private void internalTestBasic(ZooKeeper zk) throws KeeperException, InterruptedException { + zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c/d", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c/d/e", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.setData("/a/b/c/d/e", new byte[0], -1); + zk.delete("/a/b/c/d/e", -1); + zk.create("/a/b/c/d/e", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c/d"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c/d/e"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c/d/e"); + assertEvent(events, Watcher.Event.EventType.NodeDeleted, "/a/b/c/d/e"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c/d/e"); + } + + @Test + public void testRemoval() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE); + zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b/c"); + + zk.removeWatches("/a/b", persistentWatcher, Watcher.WatcherType.Any, false); + zk.create("/a/b/c/d", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEvent(events, Watcher.Event.EventType.PersistentWatchRemoved, "/a/b"); + } + } + + @Test + public void testDisconnect() throws Exception { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE); + stopServer(); + assertEvent(events, Watcher.Event.EventType.None, null); + startServer(); + assertEvent(events, Watcher.Event.EventType.None, null); + internalTestBasic(zk); + } + } + + @Test + public void testMultiClient() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk1 = createClient(new CountdownWatcher(), hostPort); ZooKeeper zk2 = createClient(new CountdownWatcher(), hostPort)) { + + zk1.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk1.addWatch("/a/b", persistentWatcher, PERSISTENT_RECURSIVE); + zk1.setData("/a/b/c", "one".getBytes(), -1); + Thread.sleep(1000); // give some time for the event to arrive + + zk2.setData("/a/b/c", "two".getBytes(), -1); + zk2.setData("/a/b/c", "three".getBytes(), -1); + zk2.setData("/a/b/c", "four".getBytes(), -1); + + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b/c"); + } + } + + @Test + public void testRootWatcher() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/", persistentWatcher, PERSISTENT_RECURSIVE); + zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/b"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/b/c"); + } + } + + private void assertEvent(BlockingQueue events, Watcher.Event.EventType eventType, String path) + throws InterruptedException { + WatchedEvent event = events.poll(5, TimeUnit.SECONDS); + Assert.assertNotNull(event); + Assert.assertEquals(eventType, event.getType()); + Assert.assertEquals(path, event.getPath()); + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java new file mode 100644 index 0000000..8220074 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java @@ -0,0 +1,233 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.AddWatchMode.PERSISTENT; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PersistentWatcherTest extends ClientBase { + private static final Logger LOG = LoggerFactory.getLogger(PersistentWatcherTest.class); + private BlockingQueue events; + private Watcher persistentWatcher; + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + + events = new LinkedBlockingQueue<>(); + persistentWatcher = event -> events.add(event); + } + + @Test + public void testBasic() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/a/b", persistentWatcher, PERSISTENT); + internalTestBasic(zk); + } + } + + @Test + public void testNullWatch() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + try { + zk.addWatch("/a/b", null, PERSISTENT); + fail("IllegalArgumentException was not thrown."); + } catch (IllegalArgumentException e) { + // Ignore expected exception + } + + try { + AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {}; + zk.addWatch("/a/b", null, PERSISTENT, cb, null); + fail("IllegalArgumentException was not thrown."); + } catch (IllegalArgumentException e) { + // Ignore expected exception + } + } + } + + @Test + public void testDefaultWatcher() + throws IOException, InterruptedException, KeeperException { + CountdownWatcher watcher = new CountdownWatcher() { + @Override + public synchronized void process(WatchedEvent event) { + super.process(event); + events.add(event); + } + }; + try (ZooKeeper zk = createClient(watcher, hostPort)) { + zk.addWatch("/a/b", PERSISTENT); + events.clear(); // clear any events added during client connection + internalTestBasic(zk); + } + } + + @Test + public void testBasicAsync() + throws IOException, InterruptedException, KeeperException { + CountdownWatcher watcher = new CountdownWatcher() { + @Override + public synchronized void process(WatchedEvent event) { + super.process(event); + events.add(event); + } + }; + try (ZooKeeper zk = createClient(watcher, hostPort)) { + final CountDownLatch latch = new CountDownLatch(1); + AsyncCallback.VoidCallback cb = (rc, path, ctx) -> { + if (rc == 0) { + latch.countDown(); + } + }; + zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null); + Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); + events.clear(); // clear any events added during client connection + internalTestBasic(zk); + } + } + + @Test + public void testAsyncDefaultWatcher() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + final CountDownLatch latch = new CountDownLatch(1); + AsyncCallback.VoidCallback cb = (rc, path, ctx) -> { + if (rc == 0) { + latch.countDown(); + } + }; + zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null); + Assert.assertTrue(latch.await(5, TimeUnit.SECONDS)); + internalTestBasic(zk); + } + } + + private void internalTestBasic(ZooKeeper zk) throws KeeperException, InterruptedException { + zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.setData("/a/b", new byte[0], -1); + zk.delete("/a/b/c", -1); + zk.delete("/a/b", -1); + zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeDeleted, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b"); + } + + @Test + public void testRemoval() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/a/b", persistentWatcher, PERSISTENT); + zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/a/b/c", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEvent(events, Watcher.Event.EventType.NodeCreated, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/a/b"); + + zk.removeWatches("/a/b", persistentWatcher, Watcher.WatcherType.Any, false); + zk.delete("/a/b/c", -1); + zk.delete("/a/b", -1); + assertEvent(events, Watcher.Event.EventType.PersistentWatchRemoved, "/a/b"); + } + } + + @Test + public void testDisconnect() throws Exception { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/a/b", persistentWatcher, PERSISTENT); + stopServer(); + assertEvent(events, Watcher.Event.EventType.None, null); + startServer(); + assertEvent(events, Watcher.Event.EventType.None, null); + internalTestBasic(zk); + } + } + + @Test + public void testMultiClient() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk1 = createClient(new CountdownWatcher(), hostPort); + ZooKeeper zk2 = createClient(new CountdownWatcher(), hostPort)) { + + zk1.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.create("/a/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk1.addWatch("/a/b", persistentWatcher, PERSISTENT); + zk1.setData("/a/b", "one".getBytes(), -1); + Thread.sleep(1000); // give some time for the event to arrive + + zk2.setData("/a/b", "two".getBytes(), -1); + zk2.setData("/a/b", "three".getBytes(), -1); + zk2.setData("/a/b", "four".getBytes(), -1); + + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b"); + assertEvent(events, Watcher.Event.EventType.NodeDataChanged, "/a/b"); + } + } + + @Test + public void testRootWatcher() + throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) { + zk.addWatch("/", persistentWatcher, PERSISTENT); + zk.create("/a", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.setData("/a", new byte[0], -1); + zk.create("/b", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/"); + assertEvent(events, Watcher.Event.EventType.NodeChildrenChanged, "/"); + } + } + + private void assertEvent(BlockingQueue events, Watcher.Event.EventType eventType, String path) + throws InterruptedException { + WatchedEvent event = events.poll(5, TimeUnit.SECONDS); + Assert.assertNotNull(event); + Assert.assertEquals(eventType, event.getType()); + Assert.assertEquals(path, event.getPath()); + } +} \ No newline at end of file diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumBase.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumBase.java new file mode 100644 index 0000000..b91aa19 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumBase.java @@ -0,0 +1,400 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.server.quorum.Election; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.util.OSMXBean; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumBase extends ClientBase { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumBase.class); + + private static final String LOCALADDR = "127.0.0.1"; + + File s1dir, s2dir, s3dir, s4dir, s5dir; + QuorumPeer s1, s2, s3, s4, s5; + protected int port1; + protected int port2; + protected int port3; + protected int port4; + protected int port5; + + protected int portLE1; + protected int portLE2; + protected int portLE3; + protected int portLE4; + protected int portLE5; + + protected int portClient1; + protected int portClient2; + protected int portClient3; + protected int portClient4; + protected int portClient5; + + protected boolean localSessionsEnabled = false; + protected boolean localSessionsUpgradingEnabled = false; + + @Test + // This just avoids complaints by junit + public void testNull() { + } + + @Override + public void setUp() throws Exception { + setUp(false); + } + + protected void setUp(boolean withObservers) throws Exception { + LOG.info("QuorumBase.setup {}", getTestName()); + setupTestEnv(); + + JMXEnv.setUp(); + + setUpAll(); + + port1 = PortAssignment.unique(); + port2 = PortAssignment.unique(); + port3 = PortAssignment.unique(); + port4 = PortAssignment.unique(); + port5 = PortAssignment.unique(); + + portLE1 = PortAssignment.unique(); + portLE2 = PortAssignment.unique(); + portLE3 = PortAssignment.unique(); + portLE4 = PortAssignment.unique(); + portLE5 = PortAssignment.unique(); + + portClient1 = PortAssignment.unique(); + portClient2 = PortAssignment.unique(); + portClient3 = PortAssignment.unique(); + portClient4 = PortAssignment.unique(); + portClient5 = PortAssignment.unique(); + + hostPort = "127.0.0.1:" + + portClient1 + + ",127.0.0.1:" + + portClient2 + + ",127.0.0.1:" + + portClient3 + + ",127.0.0.1:" + + portClient4 + + ",127.0.0.1:" + + portClient5; + LOG.info("Ports are: {}", hostPort); + + s1dir = ClientBase.createTmpDir(); + s2dir = ClientBase.createTmpDir(); + s3dir = ClientBase.createTmpDir(); + s4dir = ClientBase.createTmpDir(); + s5dir = ClientBase.createTmpDir(); + + startServers(withObservers); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix()) { + LOG.info("Initial fdcount is: {}", osMbean.getOpenFileDescriptorCount()); + } + + LOG.info("Setup finished"); + } + + void startServers() throws Exception { + startServers(false); + } + + void startServers(boolean withObservers) throws Exception { + int tickTime = 2000; + int initLimit = 3; + int syncLimit = 3; + int connectToLearnerMasterLimit = 3; + Map peers = new HashMap(); + peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress(LOCALADDR, port1), new InetSocketAddress(LOCALADDR, portLE1), new InetSocketAddress(LOCALADDR, portClient1), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress(LOCALADDR, port2), new InetSocketAddress(LOCALADDR, portLE2), new InetSocketAddress(LOCALADDR, portClient2), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress(LOCALADDR, port3), new InetSocketAddress(LOCALADDR, portLE3), new InetSocketAddress(LOCALADDR, portClient3), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress(LOCALADDR, port4), new InetSocketAddress(LOCALADDR, portLE4), new InetSocketAddress(LOCALADDR, portClient4), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress(LOCALADDR, port5), new InetSocketAddress(LOCALADDR, portLE5), new InetSocketAddress(LOCALADDR, portClient5), LearnerType.PARTICIPANT)); + + if (withObservers) { + peers.get(Long.valueOf(4)).type = LearnerType.OBSERVER; + peers.get(Long.valueOf(5)).type = LearnerType.OBSERVER; + } + + LOG.info("creating QuorumPeer 1 port {}", portClient1); + s1 = new QuorumPeer(peers, s1dir, s1dir, portClient1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient1, s1.getClientPort()); + LOG.info("creating QuorumPeer 2 port {}", portClient2); + s2 = new QuorumPeer(peers, s2dir, s2dir, portClient2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient2, s2.getClientPort()); + LOG.info("creating QuorumPeer 3 port {}", portClient3); + s3 = new QuorumPeer(peers, s3dir, s3dir, portClient3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient3, s3.getClientPort()); + LOG.info("creating QuorumPeer 4 port {}", portClient4); + s4 = new QuorumPeer(peers, s4dir, s4dir, portClient4, 3, 4, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient4, s4.getClientPort()); + LOG.info("creating QuorumPeer 5 port {}", portClient5); + s5 = new QuorumPeer(peers, s5dir, s5dir, portClient5, 3, 5, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient5, s5.getClientPort()); + + if (withObservers) { + s4.setLearnerType(LearnerType.OBSERVER); + s5.setLearnerType(LearnerType.OBSERVER); + } + + LOG.info("QuorumPeer 1 voting view: {}", s1.getVotingView()); + LOG.info("QuorumPeer 2 voting view: {}", s2.getVotingView()); + LOG.info("QuorumPeer 3 voting view: {}", s3.getVotingView()); + LOG.info("QuorumPeer 4 voting view: {}", s4.getVotingView()); + LOG.info("QuorumPeer 5 voting view: {}", s5.getVotingView()); + + s1.enableLocalSessions(localSessionsEnabled); + s2.enableLocalSessions(localSessionsEnabled); + s3.enableLocalSessions(localSessionsEnabled); + s4.enableLocalSessions(localSessionsEnabled); + s5.enableLocalSessions(localSessionsEnabled); + s1.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled); + s2.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled); + s3.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled); + s4.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled); + s5.enableLocalSessionsUpgrading(localSessionsUpgradingEnabled); + + LOG.info("start QuorumPeer 1"); + s1.start(); + LOG.info("start QuorumPeer 2"); + s2.start(); + LOG.info("start QuorumPeer 3"); + s3.start(); + LOG.info("start QuorumPeer 4"); + s4.start(); + LOG.info("start QuorumPeer 5"); + s5.start(); + LOG.info("started QuorumPeer 5"); + + LOG.info("Checking ports {}", hostPort); + for (String hp : hostPort.split(",")) { + assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT)); + LOG.info("{} is accepting client connections", hp); + } + + // interesting to see what's there... + JMXEnv.dump(); + // make sure we have these 5 servers listed + Set ensureNames = new LinkedHashSet(); + for (int i = 1; i <= 5; i++) { + ensureNames.add("InMemoryDataTree"); + } + for (int i = 1; i <= 5; i++) { + ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + i + ",name2="); + } + for (int i = 1; i <= 5; i++) { + for (int j = 1; j <= 5; j++) { + ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + j); + } + } + for (int i = 1; i <= 5; i++) { + ensureNames.add("name0=ReplicatedServer_id" + i); + } + JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()])); + } + + public int getLeaderIndex() { + if (s1.getPeerState() == ServerState.LEADING) { + return 0; + } else if (s2.getPeerState() == ServerState.LEADING) { + return 1; + } else if (s3.getPeerState() == ServerState.LEADING) { + return 2; + } else if (s4.getPeerState() == ServerState.LEADING) { + return 3; + } else if (s5.getPeerState() == ServerState.LEADING) { + return 4; + } + return -1; + } + + public String getPeersMatching(ServerState state) { + StringBuilder hosts = new StringBuilder(); + for (QuorumPeer p : getPeerList()) { + if (p.getPeerState() == state) { + hosts.append(String.format("%s:%d,", LOCALADDR, p.getClientAddress().getPort())); + } + } + LOG.info("getPeersMatching ports are {}", hosts); + return hosts.toString(); + } + + public ArrayList getPeerList() { + ArrayList peers = new ArrayList(); + peers.add(s1); + peers.add(s2); + peers.add(s3); + peers.add(s4); + peers.add(s5); + return peers; + } + + public QuorumPeer getPeerByClientPort(int clientPort) { + for (QuorumPeer p : getPeerList()) { + if (p.getClientAddress().getPort() == clientPort) { + return p; + } + } + return null; + } + + public void setupServers() throws IOException { + setupServer(1); + setupServer(2); + setupServer(3); + setupServer(4); + setupServer(5); + } + + Map peers = null; + public void setupServer(int i) throws IOException { + int tickTime = 2000; + int initLimit = 3; + int syncLimit = 3; + int connectToLearnerMasterLimit = 3; + + if (peers == null) { + peers = new HashMap(); + + peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress(LOCALADDR, port1), new InetSocketAddress(LOCALADDR, portLE1), new InetSocketAddress(LOCALADDR, portClient1), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress(LOCALADDR, port2), new InetSocketAddress(LOCALADDR, portLE2), new InetSocketAddress(LOCALADDR, portClient2), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress(LOCALADDR, port3), new InetSocketAddress(LOCALADDR, portLE3), new InetSocketAddress(LOCALADDR, portClient3), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(4), new QuorumServer(4, new InetSocketAddress(LOCALADDR, port4), new InetSocketAddress(LOCALADDR, portLE4), new InetSocketAddress(LOCALADDR, portClient4), LearnerType.PARTICIPANT)); + peers.put(Long.valueOf(5), new QuorumServer(5, new InetSocketAddress(LOCALADDR, port5), new InetSocketAddress(LOCALADDR, portLE5), new InetSocketAddress(LOCALADDR, portClient5), LearnerType.PARTICIPANT)); + } + + switch (i) { + case 1: + LOG.info("creating QuorumPeer 1 port {}", portClient1); + s1 = new QuorumPeer(peers, s1dir, s1dir, portClient1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient1, s1.getClientPort()); + break; + case 2: + LOG.info("creating QuorumPeer 2 port {}", portClient2); + s2 = new QuorumPeer(peers, s2dir, s2dir, portClient2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient2, s2.getClientPort()); + break; + case 3: + LOG.info("creating QuorumPeer 3 port {}", portClient3); + s3 = new QuorumPeer(peers, s3dir, s3dir, portClient3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient3, s3.getClientPort()); + break; + case 4: + LOG.info("creating QuorumPeer 4 port {}", portClient4); + s4 = new QuorumPeer(peers, s4dir, s4dir, portClient4, 3, 4, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient4, s4.getClientPort()); + break; + case 5: + LOG.info("creating QuorumPeer 5 port {}", portClient5); + s5 = new QuorumPeer(peers, s5dir, s5dir, portClient5, 3, 5, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(portClient5, s5.getClientPort()); + } + } + + @Override + public void tearDown() throws Exception { + LOG.info("TearDown started"); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix()) { + LOG.info("fdcount after test is: {}", osMbean.getOpenFileDescriptorCount()); + } + + shutdownServers(); + + for (String hp : hostPort.split(",")) { + assertTrue("waiting for server down", ClientBase.waitForServerDown(hp, ClientBase.CONNECTION_TIMEOUT)); + LOG.info("{} is no longer accepting client connections", hp); + } + + JMXEnv.tearDown(); + } + public void shutdownServers() { + shutdown(s1); + shutdown(s2); + shutdown(s3); + shutdown(s4); + shutdown(s5); + } + + public static void shutdown(QuorumPeer qp) { + if (qp == null) { + return; + } + try { + LOG.info("Shutting down quorum peer {}", qp.getName()); + qp.shutdown(); + Election e = qp.getElectionAlg(); + if (e != null) { + LOG.info("Shutting down leader election {}", qp.getName()); + e.shutdown(); + } else { + LOG.info("No election available to shutdown {}", qp.getName()); + } + LOG.info("Waiting for {} to exit thread", qp.getName()); + long readTimeout = qp.getTickTime() * qp.getInitLimit(); + long connectTimeout = qp.getTickTime() * qp.getSyncLimit(); + long maxTimeout = Math.max(readTimeout, connectTimeout); + maxTimeout = Math.max(maxTimeout, ClientBase.CONNECTION_TIMEOUT); + qp.join(maxTimeout * 2); + if (qp.isAlive()) { + fail("QP failed to shutdown in " + (maxTimeout * 2) + " seconds: " + qp.getName()); + } + } catch (InterruptedException e) { + LOG.debug("QP interrupted: {}", qp.getName(), e); + } + } + + protected TestableZooKeeper createClient() throws IOException, InterruptedException { + return createClient(hostPort); + } + + protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createClient(watcher, hp); + } + + protected TestableZooKeeper createClient(CountdownWatcher watcher, ServerState state) throws IOException, InterruptedException { + return createClient(watcher, getPeersMatching(state)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumHammerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumHammerTest.java new file mode 100644 index 0000000..57446ee --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumHammerTest.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.ZKTestCase; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumHammerTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(QuorumHammerTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + protected final QuorumBase qb = new QuorumBase(); + protected final ClientHammerTest cht = new ClientHammerTest(); + + @Before + public void setUp() throws Exception { + qb.setUp(); + cht.hostPort = qb.hostPort; + cht.setUpAll(); + } + + @After + public void tearDown() throws Exception { + cht.tearDownAll(); + qb.tearDown(); + } + + @Test + public void testHammerBasic() throws Throwable { + cht.testHammerBasic(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumMajorityTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumMajorityTest.java new file mode 100644 index 0000000..9516d01 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumMajorityTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.util.ArrayList; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumMajorityTest extends QuorumBase { + + protected static final Logger LOG = LoggerFactory.getLogger(QuorumMajorityTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + /***************************************************************/ + /* Test that the majority quorum verifier only counts votes from */ + /* followers in its view */ + /***************************************************************/ + @Test + public void testMajQuorums() throws Throwable { + LOG.info("Verify QuorumPeer#electionTimeTaken jmx bean attribute"); + + ArrayList peers = getPeerList(); + for (int i = 1; i <= peers.size(); i++) { + QuorumPeer qp = peers.get(i - 1); + Long electionTimeTaken = -1L; + String bean = ""; + if (qp.getPeerState() == ServerState.FOLLOWING) { + bean = String.format("%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Follower", MBeanRegistry.DOMAIN, i, i); + } else if (qp.getPeerState() == ServerState.LEADING) { + bean = String.format("%s:name0=ReplicatedServer_id%d,name1=replica.%d,name2=Leader", MBeanRegistry.DOMAIN, i, i); + } + electionTimeTaken = (Long) JMXEnv.ensureBeanAttribute(bean, "ElectionTimeTaken"); + assertTrue("Wrong electionTimeTaken value!", electionTimeTaken >= 0); + } + + //setup servers 1-5 to be followers + setUp(false); + + Proposal p = new Proposal(); + + p.addQuorumVerifier(s1.getQuorumVerifier()); + + // 2 followers out of 5 is not a majority + p.addAck(Long.valueOf(1)); + p.addAck(Long.valueOf(2)); + assertEquals(false, p.hasAllQuorums()); + + // 6 is not in the view - its vote shouldn't count + p.addAck(Long.valueOf(6)); + assertEquals(false, p.hasAllQuorums()); + + // 3 followers out of 5 are a majority of the voting view + p.addAck(Long.valueOf(3)); + assertEquals(true, p.hasAllQuorums()); + + //setup servers 1-3 to be followers and 4 and 5 to be observers + setUp(true); + + p = new Proposal(); + p.addQuorumVerifier(s1.getQuorumVerifier()); + + // 1 follower out of 3 is not a majority + p.addAck(Long.valueOf(1)); + assertEquals(false, p.hasAllQuorums()); + + // 4 and 5 are observers, their vote shouldn't count + p.addAck(Long.valueOf(4)); + p.addAck(Long.valueOf(5)); + assertEquals(false, p.hasAllQuorums()); + + // 6 is not in the view - its vote shouldn't count + p.addAck(Long.valueOf(6)); + assertEquals(false, p.hasAllQuorums()); + + // 2 followers out of 3 are a majority of the voting view + p.addAck(Long.valueOf(2)); + assertEquals(true, p.hasAllQuorums()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumQuotaTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumQuotaTest.java new file mode 100644 index 0000000..6025a15 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumQuotaTest.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.cli.SetQuotaCommand; +import org.apache.zookeeper.data.Stat; +import org.junit.Test; + +public class QuorumQuotaTest extends QuorumBase { + + @Test + public void testQuotaWithQuorum() throws Exception { + ZooKeeper zk = createClient(); + zk.setData("/", "some".getBytes(), -1); + zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + int i = 0; + for (i = 0; i < 300; i++) { + zk.create("/a/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + SetQuotaCommand.createQuota(zk, "/a", 1000L, 5000); + String statPath = Quotas.quotaZookeeper + "/a" + "/" + Quotas.statNode; + byte[] data = zk.getData(statPath, false, new Stat()); + StatsTrack st = new StatsTrack(new String(data)); + assertTrue("bytes are set", st.getBytes() == 1204L); + assertTrue("num count is set", st.getCount() == 301); + for (i = 300; i < 600; i++) { + zk.create("/a/" + i, "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + data = zk.getData(statPath, false, new Stat()); + st = new StatsTrack(new String(data)); + assertTrue("bytes are set", st.getBytes() == 2404L); + assertTrue("num count is set", st.getCount() == 601); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumRestartTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumRestartTest.java new file mode 100644 index 0000000..afdafb7 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumRestartTest.java @@ -0,0 +1,133 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET; +import static org.junit.Assert.assertTrue; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumRestartTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumRestartTest.class); + private QuorumUtil qu; + + @Before + public void setUp() throws Exception { + System.setProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + + // starting a 3 node ensemble without observers + qu = new QuorumUtil(1, 2); + qu.startAll(); + } + + + /** + * A basic test for rolling restart. We are restarting the ZooKeeper servers one by one, + * starting from the first server. We always make sure that all the nodes joined to the + * Quorum before moving forward. + * @throws Exception + */ + @Test + public void testRollingRestart() throws Exception { + for (int serverToRestart = 1; serverToRestart <= 3; serverToRestart++) { + LOG.info("***** restarting: " + serverToRestart); + qu.shutdown(serverToRestart); + + assertTrue(String.format("Timeout during waiting for server %d to go down", serverToRestart), + ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(serverToRestart).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + qu.restart(serverToRestart); + + final String errorMessage = "Not all the quorum members are connected after restarting server " + serverToRestart; + waitFor(errorMessage, () -> qu.allPeersAreConnected(), 30); + + LOG.info("***** Restart {} succeeded", serverToRestart); + } + } + + /** + * Testing one of the errors reported in ZOOKEEPER-2164, when some servers can not + * rejoin to the Quorum after restarting the servers backwards + * @throws Exception + */ + @Test + public void testRollingRestartBackwards() throws Exception { + for (int serverToRestart = 3; serverToRestart >= 1; serverToRestart--) { + LOG.info("***** restarting: " + serverToRestart); + qu.shutdown(serverToRestart); + + assertTrue(String.format("Timeout during waiting for server %d to go down", serverToRestart), + ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(serverToRestart).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + qu.restart(serverToRestart); + + final String errorMessage = "Not all the quorum members are connected after restarting server " + serverToRestart; + waitFor(errorMessage, () -> qu.allPeersAreConnected(), 30); + + LOG.info("***** Restart {} succeeded", serverToRestart); + } + } + + + /** + * Testing one of the errors reported in ZOOKEEPER-2164, when some servers can not + * rejoin to the Quorum after restarting the current leader multiple times + * @throws Exception + */ + @Test + public void testRestartingLeaderMultipleTimes() throws Exception { + for (int restartCount = 1; restartCount <= 3; restartCount++) { + int leaderId = qu.getLeaderServer(); + LOG.info("***** new leader: " + leaderId); + qu.shutdown(leaderId); + + assertTrue("Timeout during waiting for current leader to go down", + ClientBase.waitForServerDown("127.0.0.1:" + qu.getPeer(leaderId).clientPort, ClientBase.CONNECTION_TIMEOUT)); + + String errorMessage = "No new leader was elected"; + waitFor(errorMessage, () -> qu.leaderExists() && qu.getLeaderServer() != leaderId, 30); + + qu.restart(leaderId); + + errorMessage = "Not all the quorum members are connected after restarting the old leader"; + waitFor(errorMessage, () -> qu.allPeersAreConnected(), 30); + + LOG.info("***** Leader Restart {} succeeded", restartCount); + } + } + + + @After + public void tearDown() throws Exception { + qu.shutdownAll(); + System.clearProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET); + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + } + + + + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumTest.java new file mode 100644 index 0000000..7a3a5f0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumTest.java @@ -0,0 +1,415 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Op; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.Leader; +import org.apache.zookeeper.server.quorum.LearnerHandler; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class QuorumTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + private final QuorumBase qb = new QuorumBase(); + private final ClientTest ct = new ClientTest(); + private QuorumUtil qu; + + @Before + public void setUp() throws Exception { + qb.setUp(); + ct.hostPort = qb.hostPort; + ct.setUpAll(); + } + + @After + public void tearDown() throws Exception { + ct.tearDownAll(); + qb.tearDown(); + if (qu != null) { + qu.tearDown(); + } + } + + @Test + public void testDeleteWithChildren() throws Exception { + ct.testDeleteWithChildren(); + } + + @Test + public void testPing() throws Exception { + ct.testPing(); + } + + @Test + public void testSequentialNodeNames() throws IOException, InterruptedException, KeeperException { + ct.testSequentialNodeNames(); + } + + @Test + public void testACLs() throws Exception { + ct.testACLs(); + } + + @Test + public void testClientwithoutWatcherObj() throws IOException, InterruptedException, KeeperException { + ct.testClientwithoutWatcherObj(); + } + + @Test + public void testClientWithWatcherObj() throws IOException, InterruptedException, KeeperException { + ct.testClientWithWatcherObj(); + } + + @Test + public void testGetView() { + assertEquals(5, qb.s1.getView().size()); + assertEquals(5, qb.s2.getView().size()); + assertEquals(5, qb.s3.getView().size()); + assertEquals(5, qb.s4.getView().size()); + assertEquals(5, qb.s5.getView().size()); + } + + @Test + public void testViewContains() { + // Test view contains self + assertTrue(qb.s1.viewContains(qb.s1.getId())); + + // Test view contains other servers + assertTrue(qb.s1.viewContains(qb.s2.getId())); + + // Test view does not contain non-existant servers + assertFalse(qb.s1.viewContains(-1L)); + } + + volatile int counter = 0; + volatile int errors = 0; + @Test + public void testLeaderShutdown() throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = new DisconnectableZooKeeper( + qb.hostPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zk.create("/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/blah/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Leader leader = qb.s1.leader; + if (leader == null) { + leader = qb.s2.leader; + } + if (leader == null) { + leader = qb.s3.leader; + } + if (leader == null) { + leader = qb.s4.leader; + } + if (leader == null) { + leader = qb.s5.leader; + } + assertNotNull(leader); + for (int i = 0; i < 5000; i++) { + zk.setData("/blah/blah", new byte[0], -1, new AsyncCallback.StatCallback() { + public void processResult(int rc, String path, Object ctx, Stat stat) { + counter++; + if (rc != 0) { + errors++; + } + } + }, null); + } + for (LearnerHandler f : leader.getForwardingFollowers()) { + f.getSocket().shutdownInput(); + } + for (int i = 0; i < 5000; i++) { + zk.setData("/blah/blah", new byte[0], -1, new AsyncCallback.StatCallback() { + public void processResult(int rc, String path, Object ctx, Stat stat) { + counter++; + if (rc != 0) { + errors++; + } + } + }, null); + } + // check if all the followers are alive + assertTrue(qb.s1.isAlive()); + assertTrue(qb.s2.isAlive()); + assertTrue(qb.s3.isAlive()); + assertTrue(qb.s4.isAlive()); + assertTrue(qb.s5.isAlive()); + zk.close(); + } + + @Test + public void testMultipleWatcherObjs() throws IOException, InterruptedException, KeeperException { + ct.testMutipleWatcherObjs(); + } + + /** + * Make sure that we can change sessions + * from follower to leader. + * + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + */ + @Test + public void testSessionMoved() throws Exception { + String[] hostPorts = qb.hostPort.split(","); + DisconnectableZooKeeper zk = new DisconnectableZooKeeper( + hostPorts[0], + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zk.create("/sessionMoveTest", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + // we want to loop through the list twice + for (int i = 0; i < hostPorts.length * 2; i++) { + zk.dontReconnect(); + // This should stomp the zk handle + DisconnectableZooKeeper zknew = new DisconnectableZooKeeper( + hostPorts[(i + 1) % hostPorts.length], + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE, + zk.getSessionId(), + zk.getSessionPasswd()); + zknew.setData("/", new byte[1], -1); + final int[] result = new int[1]; + result[0] = Integer.MAX_VALUE; + zknew.sync("/", new AsyncCallback.VoidCallback() { + public void processResult(int rc, String path, Object ctx) { + synchronized (result) { + result[0] = rc; + result.notify(); + } + } + }, null); + synchronized (result) { + if (result[0] == Integer.MAX_VALUE) { + result.wait(5000); + } + } + LOG.info("{} Sync returned {}", hostPorts[(i + 1) % hostPorts.length], result[0]); + assertTrue(result[0] == KeeperException.Code.OK.intValue()); + try { + zk.setData("/", new byte[1], -1); + fail("Should have lost the connection"); + } catch (KeeperException.ConnectionLossException e) { + } + zk = zknew; + } + zk.close(); + } + + private static class DiscoWatcher implements Watcher { + + volatile boolean zkDisco = false; + public void process(WatchedEvent event) { + if (event.getState() == KeeperState.Disconnected) { + zkDisco = true; + } + } + + } + + /** + * Make sure the previous connection closed after session move within + * multiop. + * + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + */ + @Test + public void testSessionMovedWithMultiOp() throws Exception { + String[] hostPorts = qb.hostPort.split(","); + DisconnectableZooKeeper zk = new DisconnectableZooKeeper( + hostPorts[0], + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zk.multi(Arrays.asList(Op.create("/testSessionMovedWithMultiOp", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL))); + + // session moved to the next server + ZooKeeper zknew = new ZooKeeper( + hostPorts[1], + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE, + zk.getSessionId(), + zk.getSessionPasswd()); + + zknew.multi(Arrays.asList(Op.create("/testSessionMovedWithMultiOp-1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL))); + + // try to issue the multi op again from the old connection + // expect to have ConnectionLossException instead of keep + // getting SessionMovedException + try { + zk.multi(Arrays.asList(Op.create("/testSessionMovedWithMultiOp-Failed", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL))); + fail("Should have lost the connection"); + } catch (KeeperException.ConnectionLossException e) { + } + + zk.close(); + zknew.close(); + } + + /** + * Connect to two different servers with two different handles using the same session and + * make sure we cannot do any changes. + */ + @Test + @Ignore + public void testSessionMove() throws Exception { + String[] hps = qb.hostPort.split(","); + DiscoWatcher oldWatcher = new DiscoWatcher(); + DisconnectableZooKeeper zk = new DisconnectableZooKeeper(hps[0], ClientBase.CONNECTION_TIMEOUT, oldWatcher); + zk.create("/t1", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + zk.dontReconnect(); + // This should stomp the zk handle + DiscoWatcher watcher = new DiscoWatcher(); + DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(hps[1], ClientBase.CONNECTION_TIMEOUT, watcher, zk.getSessionId(), zk.getSessionPasswd()); + zknew.create("/t2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + try { + zk.create("/t3", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + fail("Should have lost the connection"); + } catch (KeeperException.ConnectionLossException e) { + // wait up to 30 seconds for the disco to be delivered + for (int i = 0; i < 30; i++) { + if (oldWatcher.zkDisco) { + break; + } + Thread.sleep(1000); + } + assertTrue(oldWatcher.zkDisco); + } + + ArrayList toClose = new ArrayList(); + toClose.add(zknew); + // Let's just make sure it can still move + for (int i = 0; i < 10; i++) { + zknew.dontReconnect(); + zknew = new DisconnectableZooKeeper(hps[1], ClientBase.CONNECTION_TIMEOUT, new DiscoWatcher(), zk.getSessionId(), zk.getSessionPasswd()); + toClose.add(zknew); + zknew.create("/t-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + } + for (ZooKeeper z : toClose) { + z.close(); + } + zk.close(); + } + + /** + * See ZOOKEEPER-790 for details + * */ + @Test + public void testFollowersStartAfterLeader() throws Exception { + qu = new QuorumUtil(1); + CountdownWatcher watcher = new CountdownWatcher(); + qu.startQuorum(); + + int index = 1; + while (qu.getPeer(index).peer.leader == null) { + index++; + } + + // break the quorum + qu.shutdown(index); + + // try to reestablish the quorum + qu.start(index); + + // Connect the client after services are restarted (otherwise we would get + // SessionExpiredException as the previous local session was not persisted). + ZooKeeper zk = new ZooKeeper( + "127.0.0.1:" + qu.getPeer((index == 1) ? 2 : 1).peer.getClientPort(), + ClientBase.CONNECTION_TIMEOUT, + watcher); + + try { + watcher.waitForConnected(CONNECTION_TIMEOUT); + } catch (TimeoutException e) { + fail("client could not connect to reestablished quorum: giving up after 30+ seconds."); + } + + zk.close(); + } + + // skip superhammer and clientcleanup as they are too expensive for quorum + + /** + * Tests if a multiop submitted to a non-leader propagates to the leader properly + * (see ZOOKEEPER-1124). + * + * The test works as follows. It has a client connect to a follower and submit a multiop + * to the follower. It then verifies that the multiop successfully gets committed by the leader. + * + * Without the fix in ZOOKEEPER-1124, this fails with a ConnectionLoss KeeperException. + */ + @Test + public void testMultiToFollower() throws Exception { + qu = new QuorumUtil(1); + CountdownWatcher watcher = new CountdownWatcher(); + qu.startQuorum(); + + int index = 1; + while (qu.getPeer(index).peer.leader == null) { + index++; + } + + ZooKeeper zk = new ZooKeeper( + "127.0.0.1:" + qu.getPeer((index == 1) ? 2 : 1).peer.getClientPort(), + ClientBase.CONNECTION_TIMEOUT, + watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + zk.multi(Arrays.asList( + Op.create("/multi0", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi1", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), + Op.create("/multi2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT))); + zk.getData("/multi0", false, null); + zk.getData("/multi1", false, null); + zk.getData("/multi2", false, null); + + zk.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumUtil.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumUtil.java new file mode 100644 index 0000000..f793585 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumUtil.java @@ -0,0 +1,346 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.server.quorum.Election; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.LearnerType; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.util.OSMXBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility for quorum testing. Setups 2n+1 peers and allows to start/stop all + * peers, particular peer, n peers etc. + */ +public class QuorumUtil { + + // TODO partitioning of peers and clients + + // TODO refactor QuorumBase to be special case of this + + private static final Logger LOG = LoggerFactory.getLogger(QuorumUtil.class); + private static final Set CONNECTED_STATES = new TreeSet<>( + Arrays.asList(QuorumPeer.ServerState.LEADING, QuorumPeer.ServerState.FOLLOWING, QuorumPeer.ServerState.OBSERVING)); + + public static class PeerStruct { + + public int id; + public QuorumPeer peer; + public File dataDir; + public int clientPort; + + } + + private final Map peersView = new HashMap(); + + private final Map peers = new HashMap(); + + public final int N; + + public final int ALL; + + private String hostPort; + + private int tickTime; + + private int initLimit; + + private int syncLimit; + + private int connectToLearnerMasterLimit; + + private int electionAlg; + + private boolean localSessionEnabled; + + /** + * Initializes 2n+1 quorum peers which will form a ZooKeeper ensemble. + * + * @param n + * number of peers in the ensemble will be 2n+1 + */ + public QuorumUtil(int n, int syncLimit) throws RuntimeException { + try { + ClientBase.setupTestEnv(); + JMXEnv.setUp(); + + N = n; + ALL = 2 * N + 1; + tickTime = 2000; + initLimit = 3; + this.syncLimit = syncLimit; + connectToLearnerMasterLimit = 3; + electionAlg = 3; + hostPort = ""; + + for (int i = 1; i <= ALL; ++i) { + PeerStruct ps = new PeerStruct(); + ps.id = i; + ps.dataDir = ClientBase.createTmpDir(); + ps.clientPort = PortAssignment.unique(); + peers.put(i, ps); + + peersView.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", ps.clientPort), LearnerType.PARTICIPANT)); + hostPort += "127.0.0.1:" + ps.clientPort + ((i == ALL) ? "" : ","); + } + for (int i = 1; i <= ALL; ++i) { + PeerStruct ps = peers.get(i); + LOG.info("Creating QuorumPeer {}; public port {}", i, ps.clientPort); + ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, ps.id, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + assertEquals(ps.clientPort, ps.peer.getClientPort()); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public QuorumUtil(int n) throws RuntimeException { + this(n, 3); + } + + public PeerStruct getPeer(int id) { + return peers.get(id); + } + + // This was added to avoid running into the problem of ZOOKEEPER-1539 + public boolean disableJMXTest = false; + + public void enableLocalSession(boolean localSessionEnabled) { + this.localSessionEnabled = localSessionEnabled; + } + + public void startAll() throws IOException { + shutdownAll(); + for (int i = 1; i <= ALL; ++i) { + start(i); + LOG.info("Started QuorumPeer {}", i); + } + + LOG.info("Checking ports {}", hostPort); + for (String hp : hostPort.split(",")) { + assertTrue("waiting for server " + hp + " up", ClientBase.waitForServerUp(hp, ClientBase.CONNECTION_TIMEOUT)); + LOG.info("{} is accepting client connections", hp); + } + + // This was added to avoid running into the problem of ZOOKEEPER-1539 + if (disableJMXTest) { + return; + } + + // interesting to see what's there... + try { + JMXEnv.dump(); + // make sure we have all servers listed + Set ensureNames = new LinkedHashSet(); + for (int i = 1; i <= ALL; ++i) { + ensureNames.add("InMemoryDataTree"); + } + for (int i = 1; i <= ALL; ++i) { + ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + i + ",name2="); + } + for (int i = 1; i <= ALL; ++i) { + for (int j = 1; j <= ALL; ++j) { + ensureNames.add("name0=ReplicatedServer_id" + i + ",name1=replica." + j); + } + } + for (int i = 1; i <= ALL; ++i) { + ensureNames.add("name0=ReplicatedServer_id" + i); + } + JMXEnv.ensureAll(ensureNames.toArray(new String[ensureNames.size()])); + } catch (IOException e) { + LOG.warn("IOException during JMXEnv operation", e); + } catch (InterruptedException e) { + LOG.warn("InterruptedException during JMXEnv operation", e); + } + } + + /** + * Start first N+1 peers. + */ + public void startQuorum() throws IOException { + shutdownAll(); + for (int i = 1; i <= N + 1; ++i) { + start(i); + } + for (int i = 1; i <= N + 1; ++i) { + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + getPeer(i).clientPort, ClientBase.CONNECTION_TIMEOUT)); + } + } + + public void start(int id) throws IOException { + PeerStruct ps = getPeer(id); + LOG.info("Creating QuorumPeer {}; public port {}", ps.id, ps.clientPort); + ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, ps.id, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + if (localSessionEnabled) { + ps.peer.enableLocalSessions(true); + } + assertEquals(ps.clientPort, ps.peer.getClientPort()); + + ps.peer.start(); + } + + public void restart(int id) throws IOException { + start(id); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + getPeer(id).clientPort, ClientBase.CONNECTION_TIMEOUT)); + } + + public void startThenShutdown(int id) throws IOException { + PeerStruct ps = getPeer(id); + LOG.info("Creating QuorumPeer {}; public port {}", ps.id, ps.clientPort); + ps.peer = new QuorumPeer(peersView, ps.dataDir, ps.dataDir, ps.clientPort, electionAlg, ps.id, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + if (localSessionEnabled) { + ps.peer.enableLocalSessions(true); + } + assertEquals(ps.clientPort, ps.peer.getClientPort()); + + ps.peer.start(); + assertTrue( + "Waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + getPeer(id).clientPort, ClientBase.CONNECTION_TIMEOUT)); + shutdown(id); + } + + public void shutdownAll() { + for (int i = 1; i <= ALL; ++i) { + shutdown(i); + } + for (String hp : hostPort.split(",")) { + assertTrue("Waiting for server down", ClientBase.waitForServerDown(hp, ClientBase.CONNECTION_TIMEOUT)); + LOG.info("{} is no longer accepting client connections", hp); + } + } + + public void shutdown(int id) { + QuorumPeer qp = getPeer(id).peer; + try { + LOG.info("Shutting down quorum peer {}", qp.getName()); + qp.shutdown(); + Election e = qp.getElectionAlg(); + if (e != null) { + LOG.info("Shutting down leader election {}", qp.getName()); + e.shutdown(); + } else { + LOG.info("No election available to shutdown {}", qp.getName()); + } + LOG.info("Waiting for {} to exit thread", qp.getName()); + qp.join(30000); + if (qp.isAlive()) { + fail("QP failed to shutdown in 30 seconds: " + qp.getName()); + } + } catch (InterruptedException e) { + LOG.debug("QP interrupted: {}", qp.getName(), e); + } + } + + public String getConnString() { + return hostPort; + } + + public String getConnectString(QuorumPeer peer) { + return "127.0.0.1:" + peer.getClientPort(); + } + + public boolean allPeersAreConnected() { + return peers.values().stream() + .map(ps -> ps.peer) + .allMatch(peer -> CONNECTED_STATES.contains(peer.getPeerState())); + } + + public QuorumPeer getLeaderQuorumPeer() { + for (PeerStruct ps : peers.values()) { + if (ps.peer.leader != null) { + return ps.peer; + } + } + throw new RuntimeException("Unable to find a leader peer"); + } + + public List getFollowerQuorumPeers() { + List peerList = new ArrayList(ALL - 1); + + for (PeerStruct ps : peers.values()) { + if (ps.peer.leader == null) { + peerList.add(ps.peer); + } + } + + return Collections.unmodifiableList(peerList); + } + + public void tearDown() throws Exception { + LOG.info("TearDown started"); + + OSMXBean osMbean = new OSMXBean(); + if (osMbean.getUnix()) { + LOG.info("fdcount after test is: {}", osMbean.getOpenFileDescriptorCount()); + } + + shutdownAll(); + JMXEnv.tearDown(); + } + + public int getLeaderServer() { + int index = 0; + for (int i = 1; i <= ALL; i++) { + if (getPeer(i).peer.leader != null) { + index = i; + break; + } + } + + assertTrue("Leader server not found.", index > 0); + return index; + } + + public boolean leaderExists() { + for (int i = 1; i <= ALL; i++) { + if (getPeer(i).peer.leader != null) { + return true; + } + } + return false; + } + + public String getConnectionStringForServer(final int index) { + return "127.0.0.1:" + getPeer(index).clientPort; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumUtilTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumUtilTest.java new file mode 100644 index 0000000..d02fbd6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumUtilTest.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or morecontributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.Set; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.jmx.ZKMBeanInfo; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is intented to ensure the correct functionality of + * {@link QuorumUtil} helper. + */ +public class QuorumUtilTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(QuorumUtilTest.class); + + /** + *

      + * This test ensures that all JXM beans associated to a {@link QuorumPeer} + * are unregistered when shuted down ({@link QuorumUtil#shutdown(int)}). It + * allows a successfull restarting of several zookeeper servers ( + * {@link QuorumPeer}) running on the same JVM. + *

      + * See ZOOKEEPER-1214 for details. + */ + @Test + public void validateAllMXBeanAreUnregistered() throws IOException { + QuorumUtil qU = new QuorumUtil(1); + LOG.info(">-->> Starting up all servers..."); + qU.startAll(); + LOG.info(">-->> Servers up and running..."); + + int leaderIndex = qU.getLeaderServer(); + int firstFollowerIndex = 0; + int secondFollowerIndex = 0; + + switch (leaderIndex) { + case 1: + firstFollowerIndex = 2; + secondFollowerIndex = 3; + break; + case 2: + firstFollowerIndex = 1; + secondFollowerIndex = 3; + break; + case 3: + firstFollowerIndex = 1; + secondFollowerIndex = 2; + break; + + default: + fail("Unexpected leaderIndex value: " + leaderIndex); + break; + } + + LOG.info(">-->> Shuting down server [{}]", firstFollowerIndex); + qU.shutdown(firstFollowerIndex); + LOG.info(">-->> Shuting down server [{}]", secondFollowerIndex); + qU.shutdown(secondFollowerIndex); + LOG.info(">-->> Restarting server [{}]", firstFollowerIndex); + qU.restart(firstFollowerIndex); + LOG.info(">-->> Restarting server [{}]", secondFollowerIndex); + qU.restart(secondFollowerIndex); + + qU.shutdownAll(); + Set pending = MBeanRegistry.getInstance().getRegisteredBeans(); + assertTrue("The following beans should have been unregistered: " + pending, pending.isEmpty()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumZxidSyncTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumZxidSyncTest.java new file mode 100644 index 0000000..80d2b9c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/QuorumZxidSyncTest.java @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class QuorumZxidSyncTest extends ZKTestCase { + + QuorumBase qb = new QuorumBase(); + + @Before + public void setUp() throws Exception { + qb.setUp(); + } + + /** + * find out what happens when a follower connects to leader that is behind + */ + @Test + public void testBehindLeader() throws Exception { + // crank up the epoch numbers + ClientBase.waitForServerUp(qb.hostPort, 10000); + ClientBase.waitForServerUp(qb.hostPort, 10000); + ZooKeeper zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + zk.create("/0", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + zk.create("/1", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + zk.create("/2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + cleanAndInitializeDataDir(qb.s1dir); + cleanAndInitializeDataDir(qb.s2dir); + cleanAndInitializeDataDir(qb.s3dir); + cleanAndInitializeDataDir(qb.s4dir); + qb.setupServers(); + qb.s1.start(); + qb.s2.start(); + qb.s3.start(); + qb.s4.start(); + assertTrue("Servers didn't come up", ClientBase.waitForServerUp(qb.hostPort, 10000)); + qb.s5.start(); + String hostPort = "127.0.0.1:" + qb.s5.getClientPort(); + assertFalse("Servers came up, but shouldn't have since it's ahead of leader", ClientBase.waitForServerUp(hostPort, 10000)); + } + + private void cleanAndInitializeDataDir(File f) throws IOException { + File v = new File(f, "version-2"); + for (File c : v.listFiles()) { + c.delete(); + } + ClientBase.createInitializeFile(f); + } + + /** + * find out what happens when the latest state is in the snapshots not + * the logs. + */ + @Test + public void testLateLogs() throws Exception { + // crank up the epoch numbers + ClientBase.waitForServerUp(qb.hostPort, 10000); + ClientBase.waitForServerUp(qb.hostPort, 10000); + ZooKeeper zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + zk.create("/0", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + zk.create("/1", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + qb.shutdownServers(); + deleteLogs(qb.s1dir); + deleteLogs(qb.s2dir); + deleteLogs(qb.s3dir); + deleteLogs(qb.s4dir); + deleteLogs(qb.s5dir); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + zk.create("/2", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + qb.shutdownServers(); + qb.startServers(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + zk = new ZooKeeper(qb.hostPort, 10000, DummyWatcher.INSTANCE); + boolean saw2 = false; + for (String child : zk.getChildren("/", false)) { + if (child.equals("2")) { + saw2 = true; + } + } + zk.close(); + assertTrue("Didn't see /2 (went back in time)", saw2); + } + + private void deleteLogs(File f) { + File v = new File(f, "version-2"); + for (File c : v.listFiles()) { + if (c.getName().startsWith("log")) { + c.delete(); + } + } + } + + @After + public void tearDown() throws Exception { + qb.tearDown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReadOnlyModeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReadOnlyModeTest.java new file mode 100644 index 0000000..0bef4c1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReadOnlyModeTest.java @@ -0,0 +1,284 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.ByteArrayOutputStream; +import java.io.LineNumberReader; +import java.io.StringReader; +import java.util.regex.Pattern; +import org.apache.log4j.Layout; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.apache.log4j.WriterAppender; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.NotReadOnlyException; +import org.apache.zookeeper.Transaction; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.ZooKeeper.States; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.LoggerFactory; + +public class ReadOnlyModeTest extends ZKTestCase { + + private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(ReadOnlyModeTest.class); + private static int CONNECTION_TIMEOUT = QuorumBase.CONNECTION_TIMEOUT; + private QuorumUtil qu = new QuorumUtil(1); + + @Before + public void setUp() throws Exception { + System.setProperty("readonlymode.enabled", "true"); + qu.startQuorum(); + } + + @After + public void tearDown() throws Exception { + System.setProperty("readonlymode.enabled", "false"); + qu.tearDown(); + } + + /** + * Test write operations using multi request. + */ + @Test(timeout = 90000) + public void testMultiTransaction() throws Exception { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected + + final String data = "Data to be read in RO mode"; + final String node1 = "/tnode1"; + final String node2 = "/tnode2"; + zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + watcher.reset(); + qu.shutdown(2); + watcher.waitForConnected(CONNECTION_TIMEOUT); + assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY, zk.getState()); + + // read operation during r/o mode + String remoteData = new String(zk.getData(node1, false, null)); + assertEquals("Failed to read data in r-o mode", data, remoteData); + + try { + Transaction transaction = zk.transaction(); + transaction.setData(node1, "no way".getBytes(), -1); + transaction.create(node2, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + transaction.commit(); + fail("Write operation using multi-transaction" + " api has succeeded during RO mode"); + } catch (NotReadOnlyException e) { + // ok + } + + assertNull("Should have created the znode:" + node2, zk.exists(node2, false)); + } + + /** + * Basic test of read-only client functionality. Tries to read and write + * during read-only mode, then regains a quorum and tries to write again. + */ + @Test(timeout = 90000) + public void testReadOnlyClient() throws Exception { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected + + final String data = "Data to be read in RO mode"; + final String node = "/tnode"; + zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + watcher.reset(); + qu.shutdown(2); + zk.close(); + + // Re-connect the client (in case we were connected to the shut down + // server and the local session was not persisted). + zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + // read operation during r/o mode + String remoteData = new String(zk.getData(node, false, null)); + assertEquals(data, remoteData); + + try { + zk.setData(node, "no way".getBytes(), -1); + fail("Write operation has succeeded during RO mode"); + } catch (NotReadOnlyException e) { + // ok + } + + watcher.reset(); + qu.start(2); + assertTrue( + "waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); + zk.close(); + watcher.reset(); + + // Re-connect the client (in case we were connected to the shut down + // server and the local session was not persisted). + zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); + zk.setData(node, "We're in the quorum now".getBytes(), -1); + + zk.close(); + } + + /** + * Ensures that upon connection to a read-only server client receives + * ConnectedReadOnly state notification. + */ + @Test(timeout = 90000) + public void testConnectionEvents() throws Exception { + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + boolean success = false; + for (int i = 0; i < 30; i++) { + try { + zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + success = true; + break; + } catch (KeeperException.ConnectionLossException e) { + Thread.sleep(1000); + } + } + assertTrue("Did not succeed in connecting in 30s", success); + assertFalse("The connection should not be read-only yet", watcher.readOnlyConnected); + + // kill peer and wait no more than 5 seconds for read-only server + // to be started (which should take one tickTime (2 seconds)) + qu.shutdown(2); + + // Re-connect the client (in case we were connected to the shut down + // server and the local session was not persisted). + zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + long start = Time.currentElapsedTime(); + while (!(zk.getState() == States.CONNECTEDREADONLY)) { + Thread.sleep(200); + // TODO this was originally 5 seconds, but realistically, on random/slow/virt hosts, there is no way to guarantee this + assertTrue("Can't connect to the server", Time.currentElapsedTime() - start < 30000); + } + + watcher.waitForReadOnlyConnected(5000); + zk.close(); + } + + /** + * Tests a situation when client firstly connects to a read-only server and + * then connects to a majority server. Transition should be transparent for + * the user. + */ + @Test(timeout = 90000) + public void testSessionEstablishment() throws Exception { + qu.shutdown(2); + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); + assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk.getState()); + long fakeId = zk.getSessionId(); + LOG.info("Connected as r/o mode with state {} and session id {}", zk.getState(), fakeId); + + watcher.reset(); + qu.start(2); + assertTrue( + "waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT)); + LOG.info("Server 127.0.0.1:{} is up", qu.getPeer(2).clientPort); + // ZOOKEEPER-2722: wait until we can connect to a read-write server after the quorum + // is formed. Otherwise, it is possible that client first connects to a read-only server, + // then drops the connection because of shutting down of the read-only server caused + // by leader election / quorum forming between the read-only server and the newly started + // server. If we happen to execute the zk.create after the read-only server is shutdown and + // before the quorum is formed, we will get a ConnectLossException. + watcher.waitForSyncConnected(CONNECTION_TIMEOUT); + assertEquals("Should be in read-write mode", States.CONNECTED, zk.getState()); + LOG.info("Connected as rw mode with state {} and session id {}", zk.getState(), zk.getSessionId()); + zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertFalse("fake session and real session have same id", zk.getSessionId() == fakeId); + zk.close(); + } + + /** + * Ensures that client seeks for r/w servers while it's connected to r/o + * server. + */ + @SuppressWarnings("deprecation") + @Test(timeout = 90000) + public void testSeekForRwServer() throws Exception { + // setup the logger to capture all logs + Layout layout = Logger.getRootLogger().getAppender("CONSOLE").getLayout(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + WriterAppender appender = new WriterAppender(layout, os); + appender.setImmediateFlush(true); + appender.setThreshold(Level.INFO); + Logger zlogger = Logger.getLogger("org.apache.zookeeper"); + zlogger.addAppender(appender); + + try { + qu.shutdown(2); + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + // if we don't suspend a peer it will rejoin a quorum + qu.getPeer(1).peer.suspend(); + + // start two servers to form a quorum; client should detect this and + // connect to one of them + watcher.reset(); + qu.start(2); + qu.start(3); + ClientBase.waitForServerUp(qu.getConnString(), 2000); + watcher.waitForConnected(CONNECTION_TIMEOUT); + zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // resume poor fellow + qu.getPeer(1).peer.resume(); + } finally { + zlogger.removeAppender(appender); + } + + os.close(); + LineNumberReader r = new LineNumberReader(new StringReader(os.toString())); + String line; + Pattern p = Pattern.compile(".*Majority server found.*"); + boolean found = false; + while ((line = r.readLine()) != null) { + if (p.matcher(line).matches()) { + found = true; + break; + } + } + assertTrue("Majority server wasn't found while connected to r/o server", found); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigExceptionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigExceptionTest.java new file mode 100644 index 0000000..daa471f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigExceptionTest.java @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReconfigExceptionTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ReconfigExceptionTest.class); + private static String authProvider = "zookeeper.DigestAuthenticationProvider.superDigest"; + // Use DigestAuthenticationProvider.base64Encode or + // run ZooKeeper jar with org.apache.zookeeper.server.auth.DigestAuthenticationProvider to generate password. + // An example: + // java -cp zookeeper-3.6.0-SNAPSHOT.jar:lib/log4j-1.2.17.jar:lib/slf4j-log4j12-1.7.5.jar: + // lib/slf4j-api-1.7.5.jar org.apache.zookeeper.server.auth.DigestAuthenticationProvider super:test + // The password here is 'test'. + private static String superDigest = "super:D/InIHSb7yEEbrWz8b9l71RjZJU="; + private QuorumUtil qu; + private ZooKeeperAdmin zkAdmin; + + @Before + public void setup() throws InterruptedException { + System.setProperty(authProvider, superDigest); + QuorumPeerConfig.setReconfigEnabled(true); + + // Get a three server quorum. + qu = new QuorumUtil(1); + qu.disableJMXTest = true; + + try { + qu.startAll(); + } catch (IOException e) { + fail("Fail to start quorum servers."); + } + + resetZKAdmin(); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(authProvider); + try { + if (qu != null) { + qu.tearDown(); + } + if (zkAdmin != null) { + zkAdmin.close(); + } + } catch (Exception e) { + // Ignore. + } + } + + @Test(timeout = 10000) + public void testReconfigDisabled() throws InterruptedException { + QuorumPeerConfig.setReconfigEnabled(false); + + // for this test we need to restart the quorum peers to get the config change, + // as in the setup() we started the quorum with reconfigEnabled=true + qu.shutdownAll(); + try { + qu.startAll(); + } catch (IOException e) { + fail("Fail to start quorum servers."); + } + + try { + reconfigPort(); + fail("Reconfig should be disabled."); + } catch (KeeperException e) { + assertTrue(e.code() == KeeperException.Code.RECONFIGDISABLED); + } + } + + @Test(timeout = 10000) + public void testReconfigFailWithoutAuth() throws InterruptedException { + try { + reconfigPort(); + fail("Reconfig should fail without auth."); + } catch (KeeperException e) { + // However a failure is still expected as user is not authenticated, so ACL check will fail. + assertTrue(e.code() == KeeperException.Code.NOAUTH); + } + } + + @Test(timeout = 10000) + public void testReconfigEnabledWithSuperUser() throws InterruptedException { + try { + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + assertTrue(reconfigPort()); + } catch (KeeperException e) { + fail("Reconfig should not fail, but failed with exception : " + e.getMessage()); + } + } + + @Test(timeout = 10000) + public void testReconfigFailWithAuthWithNoACL() throws InterruptedException { + resetZKAdmin(); + + try { + zkAdmin.addAuthInfo("digest", "user:test".getBytes()); + reconfigPort(); + fail("Reconfig should fail without a valid ACL associated with user."); + } catch (KeeperException e) { + // Again failure is expected because no ACL is associated with this user. + assertTrue(e.code() == KeeperException.Code.NOAUTH); + } + } + + @Test(timeout = 10000) + public void testReconfigEnabledWithAuthAndWrongACL() throws InterruptedException { + resetZKAdmin(); + + try { + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + // There is ACL however the permission is wrong - need WRITE permission at leaste. + ArrayList acls = new ArrayList(Collections.singletonList(new ACL(ZooDefs.Perms.READ, new Id("digest", "user:tl+z3z0vO6PfPfEENfLF96E6pM0="/* password is test */)))); + zkAdmin.setACL(ZooDefs.CONFIG_NODE, acls, -1); + resetZKAdmin(); + zkAdmin.addAuthInfo("digest", "user:test".getBytes()); + reconfigPort(); + fail("Reconfig should fail with an ACL that is read only!"); + } catch (KeeperException e) { + assertTrue(e.code() == KeeperException.Code.NOAUTH); + } + } + + @Test(timeout = 10000) + public void testReconfigEnabledWithAuthAndACL() throws InterruptedException { + resetZKAdmin(); + + try { + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + ArrayList acls = new ArrayList(Collections.singletonList(new ACL(ZooDefs.Perms.WRITE, new Id("digest", "user:tl+z3z0vO6PfPfEENfLF96E6pM0="/* password is test */)))); + zkAdmin.setACL(ZooDefs.CONFIG_NODE, acls, -1); + resetZKAdmin(); + zkAdmin.addAuthInfo("digest", "user:test".getBytes()); + assertTrue(reconfigPort()); + } catch (KeeperException e) { + fail("Reconfig should not fail, but failed with exception : " + e.getMessage()); + } + } + + // Utility method that recreates a new ZooKeeperAdmin handle, and wait for the handle to connect to + // quorum servers. + private void resetZKAdmin() throws InterruptedException { + String cnxString; + ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher(); + try { + cnxString = "127.0.0.1:" + qu.getPeer(1).peer.getClientPort(); + if (zkAdmin != null) { + zkAdmin.close(); + } + zkAdmin = new ZooKeeperAdmin(cnxString, ClientBase.CONNECTION_TIMEOUT, watcher); + } catch (IOException e) { + fail("Fail to create ZooKeeperAdmin handle."); + return; + } + + try { + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + } catch (InterruptedException | TimeoutException e) { + fail("ZooKeeper admin client can not connect to " + cnxString); + } + } + + private boolean reconfigPort() throws KeeperException, InterruptedException { + List joiningServers = new ArrayList(); + int leaderId = 1; + while (qu.getPeer(leaderId).peer.leader == null) { + leaderId++; + } + int followerId = leaderId == 1 ? 2 : 1; + joiningServers.add("server." + followerId + "=localhost:" + + qu.getPeer(followerId).peer.getQuorumAddress().getAllPorts().get(0) /*quorum port*/ + + ":" + + qu.getPeer(followerId).peer.getElectionAddress().getAllPorts().get(0) /*election port*/ + + ":participant;localhost:" + + PortAssignment.unique()/* new client port */); + zkAdmin.reconfigure(joiningServers, null, null, -1, new Stat()); + return true; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigMisconfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigMisconfigTest.java new file mode 100644 index 0000000..7fb675a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigMisconfigTest.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReconfigMisconfigTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ReconfigMisconfigTest.class); + private QuorumUtil qu; + private ZooKeeperAdmin zkAdmin; + private static String errorMsg = "Reconfig should fail without configuring the super " + + "user's password on server side first."; + + @Before + public void setup() throws InterruptedException { + QuorumPeerConfig.setReconfigEnabled(true); + // Get a three server quorum. + qu = new QuorumUtil(1); + qu.disableJMXTest = true; + try { + qu.startAll(); + } catch (IOException e) { + fail("Fail to start quorum servers."); + } + + instantiateZKAdmin(); + } + + @After + public void tearDown() throws Exception { + try { + if (qu != null) { + qu.tearDown(); + } + if (zkAdmin != null) { + zkAdmin.close(); + } + } catch (Exception e) { + // Ignore. + } + } + + @Test(timeout = 10000) + public void testReconfigFailWithoutSuperuserPasswordConfiguredOnServer() throws InterruptedException { + // This tests the case where ZK ensemble does not have the super user's password configured. + // Reconfig should fail as the super user has to be explicitly configured via + // zookeeper.DigestAuthenticationProvider.superDigest. + try { + reconfigPort(); + fail(errorMsg); + } catch (KeeperException e) { + assertTrue(e.code() == KeeperException.Code.NOAUTH); + } + + try { + zkAdmin.addAuthInfo("digest", "super:".getBytes()); + reconfigPort(); + fail(errorMsg); + } catch (KeeperException e) { + assertTrue(e.code() == KeeperException.Code.NOAUTH); + } + } + + private void instantiateZKAdmin() throws InterruptedException { + String cnxString; + ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher(); + try { + cnxString = "127.0.0.1:" + qu.getPeer(1).peer.getClientPort(); + zkAdmin = new ZooKeeperAdmin(cnxString, ClientBase.CONNECTION_TIMEOUT, watcher); + } catch (IOException e) { + fail("Fail to create ZooKeeperAdmin handle."); + return; + } + + try { + watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT); + } catch (InterruptedException | TimeoutException e) { + fail("ZooKeeper admin client can not connect to " + cnxString); + } + } + + private boolean reconfigPort() throws KeeperException, InterruptedException { + List joiningServers = new ArrayList(); + int leaderId = 1; + while (qu.getPeer(leaderId).peer.leader == null) { + leaderId++; + } + int followerId = leaderId == 1 ? 2 : 1; + joiningServers.add("server." + followerId + + "=localhost:" + + qu.getPeer(followerId).peer.getQuorumAddress().getAllPorts().get(0) /*quorum port*/ + + ":" + + qu.getPeer(followerId).peer.getElectionAddress().getAllPorts().get(0) /*election port*/ + + ":participant;localhost:" + + PortAssignment.unique()/* new client port */); + zkAdmin.reconfigure(joiningServers, null, null, -1, new Stat()); + return true; + } + +} + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java new file mode 100644 index 0000000..810cc2a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ReconfigTest.java @@ -0,0 +1,1299 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static java.lang.Integer.parseInt; +import static java.lang.String.format; +import static java.net.InetAddress.getLoopbackAddress; +import static java.util.stream.Collectors.toList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.ServerSocket; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import org.apache.zookeeper.AsyncCallback.DataCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.jmx.MBeanRegistry; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.flexible.QuorumHierarchical; +import org.apache.zookeeper.server.quorum.flexible.QuorumMaj; +import org.apache.zookeeper.server.quorum.flexible.QuorumVerifier; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ReconfigTest extends ZKTestCase implements DataCallback { + + private static final Logger LOG = LoggerFactory.getLogger(ReconfigTest.class); + + private QuorumUtil qu; + private ZooKeeper[] zkArr; + private ZooKeeperAdmin[] zkAdminArr; + + @Before + public void setup() { + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + QuorumPeerConfig.setReconfigEnabled(true); + } + + @After + public void tearDown() throws Exception { + closeAllHandles(zkArr, zkAdminArr); + if (qu != null) { + qu.tearDown(); + } + } + + public static String reconfig( + ZooKeeperAdmin zkAdmin, + List joiningServers, + List leavingServers, + List newMembers, + long fromConfig) throws KeeperException, InterruptedException { + byte[] config = null; + String failure = null; + LOG.info("reconfig initiated by the test"); + for (int j = 0; j < 30; j++) { + try { + config = zkAdmin.reconfigure(joiningServers, leavingServers, newMembers, fromConfig, new Stat()); + failure = null; + break; + } catch (KeeperException.ConnectionLossException e) { + failure = "client could not connect to reestablished quorum: giving up after 30+ seconds."; + } catch (KeeperException.ReconfigInProgress e) { + failure = "reconfig still in progress: giving up after 30+ seconds."; + } + Thread.sleep(1000); + } + if (failure != null) { + fail(failure); + } + + String configStr = new String(config); + List currentServerConfigs = Arrays.stream(configStr.split("\n")) + .map(String::trim) + .filter(s->s.startsWith("server")) + .map(ServerConfigLine::new) + .collect(toList()); + + if (joiningServers != null) { + for (String joiner : joiningServers) { + ServerConfigLine joinerServerConfigLine = new ServerConfigLine(joiner); + + String errorMessage = format("expected joiner config \"%s\" not found in current config:\n%s", joiner, configStr); + assertTrue(errorMessage, currentServerConfigs.stream().anyMatch(c -> c.equals(joinerServerConfigLine))); + } + } + if (leavingServers != null) { + for (String leaving : leavingServers) { + String errorMessage = format("leaving server \"%s\" not removed from config: \n%s", leaving, configStr); + assertFalse(errorMessage, configStr.contains(format("server.%s=", leaving))); + } + } + + return configStr; + } + + public static String testServerHasConfig( + ZooKeeper zk, + List joiningServers, + List leavingServers) throws KeeperException, InterruptedException { + boolean testNodeExists = false; + byte[] config = null; + for (int j = 0; j < 30; j++) { + try { + if (!testNodeExists) { + createZNode(zk, "/dummy", "dummy"); + testNodeExists = true; + } + // Use setData instead of sync API to force a view update. + // Check ZOOKEEPER-2137 for details. + zk.setData("/dummy", "dummy".getBytes(), -1); + config = zk.getConfig(false, new Stat()); + break; + } catch (KeeperException.ConnectionLossException e) { + if (j < 29) { + Thread.sleep(1000); + } else { + // test fails if we still can't connect to the quorum after + // 30 seconds. + fail("client could not connect to reestablished quorum: giving up after 30+ seconds."); + } + } + } + + String configStr = new String(config); + if (joiningServers != null) { + for (String joiner : joiningServers) { + assertTrue("Config:<" + configStr + ">\n" + joiner, configStr.contains(joiner)); + } + } + if (leavingServers != null) { + for (String leaving : leavingServers) { + assertFalse("Config:<" + configStr + ">\n" + leaving, configStr.contains("server.".concat(leaving))); + } + } + + return configStr; + } + + public static void testNormalOperation(ZooKeeper writer, ZooKeeper reader) throws KeeperException, InterruptedException { + testNormalOperation(writer, reader, true); + } + + public static void testNormalOperation(ZooKeeper writer, ZooKeeper reader, boolean initTestNodes) throws KeeperException, InterruptedException { + boolean createNodes = initTestNodes; + for (int j = 0; j < 30; j++) { + try { + if (createNodes) { + createZNode(writer, "/test", "test"); + createZNode(reader, "/dummy", "dummy"); + createNodes = false; + } + + String data = "test" + j; + writer.setData("/test", data.getBytes(), -1); + // Use setData instead of sync API to force a view update. + // Check ZOOKEEPER-2137 for details. + reader.setData("/dummy", "dummy".getBytes(), -1); + byte[] res = reader.getData("/test", null, new Stat()); + assertEquals(data, new String(res)); + break; + } catch (KeeperException.ConnectionLossException e) { + if (j < 29) { + Thread.sleep(1000); + } else { + // test fails if we still can't connect to the quorum after + // 30 seconds. + fail("client could not connect to reestablished quorum: giving up after 30+ seconds."); + } + } + } + } + + private static void createZNode( + ZooKeeper zk, + String path, + String data) throws KeeperException, InterruptedException { + try { + zk.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException e) { + } + } + + private int getLeaderId(QuorumUtil qu) { + int leaderId = 1; + while (qu.getPeer(leaderId).peer.leader == null) { + leaderId++; + } + return leaderId; + } + + public static ZooKeeper[] createHandles(QuorumUtil qu) throws IOException { + // create an extra handle, so we can index the handles from 1 to qu.ALL + // using the server id. + ZooKeeper[] zkArr = new ZooKeeper[qu.ALL + 1]; + zkArr[0] = null; // not used. + for (int i = 1; i <= qu.ALL; i++) { + // server ids are 1, 2 and 3 + zkArr[i] = new ZooKeeper( + "127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + } + return zkArr; + } + + public static ZooKeeperAdmin[] createAdminHandles(QuorumUtil qu) throws IOException { + // create an extra handle, so we can index the handles from 1 to qu.ALL + // using the server id. + ZooKeeperAdmin[] zkAdminArr = new ZooKeeperAdmin[qu.ALL + 1]; + zkAdminArr[0] = null; // not used. + for (int i = 1; i <= qu.ALL; i++) { + // server ids are 1, 2 and 3 + zkAdminArr[i] = new ZooKeeperAdmin( + "127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zkAdminArr[i].addAuthInfo("digest", "super:test".getBytes()); + } + + return zkAdminArr; + } + + public static void closeAllHandles(ZooKeeper[] zkArr, ZooKeeperAdmin[] zkAdminArr) throws InterruptedException { + if (zkArr != null) { + for (ZooKeeper zk : zkArr) { + if (zk != null) { + zk.close(); + } + } + } + if (zkAdminArr != null) { + for (ZooKeeperAdmin zkAdmin : zkAdminArr) { + if (zkAdmin != null) { + zkAdmin.close(); + } + } + } + } + + @Test + public void testRemoveAddOne() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + List leavingServers = new ArrayList(); + List joiningServers = new ArrayList(); + + int leaderIndex = getLeaderId(qu); + + // during first iteration, leavingIndex will correspond to a follower + // during second iteration leavingIndex will be the index of the leader + int leavingIndex = (leaderIndex == 1) ? 2 : 1; + + for (int i = 0; i < 2; i++) { + // some of the operations will be executed by a client connected to + // the removed server + // while others are invoked by a client connected to some other + // server. + // when we're removing the leader, zk1 will be the client connected + // to removed server + ZooKeeper zk1 = (leavingIndex == leaderIndex) ? zkArr[leaderIndex] : zkArr[(leaderIndex % qu.ALL) + 1]; + ZooKeeper zk2 = (leavingIndex == leaderIndex) ? zkArr[(leaderIndex % qu.ALL) + 1] : zkArr[leaderIndex]; + ZooKeeperAdmin zkAdmin1 = (leavingIndex == leaderIndex) + ? zkAdminArr[leaderIndex] + : zkAdminArr[(leaderIndex % qu.ALL) + 1]; + ZooKeeperAdmin zkAdmin2 = (leavingIndex == leaderIndex) + ? zkAdminArr[(leaderIndex % qu.ALL) + 1] + : zkAdminArr[leaderIndex]; + + leavingServers.add(Integer.toString(leavingIndex)); + + // remember this server so we can add it back later + joiningServers.add("server." + leavingIndex + + "=localhost:" + + qu.getPeer(leavingIndex).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(leavingIndex).peer.getElectionAddress().getAllPorts().get(0) + + ":participant;localhost:" + + qu.getPeer(leavingIndex).peer.getClientPort()); + + String configStr = reconfig(zkAdmin1, null, leavingServers, null, -1); + testServerHasConfig(zk2, null, leavingServers); + testNormalOperation(zk2, zk1); + + QuorumVerifier qv = qu.getPeer(1).peer.configFromString(configStr); + long version = qv.getVersion(); + + // checks that conditioning on version works properly + try { + reconfig(zkAdmin2, joiningServers, null, null, version + 1); + fail("reconfig succeeded even though version condition was incorrect!"); + } catch (KeeperException.BadVersionException e) { + + } + + reconfig(zkAdmin2, joiningServers, null, null, version); + + testNormalOperation(zk1, zk2); + testServerHasConfig(zk1, joiningServers, null); + + // second iteration of the loop will remove the leader + // and add it back (as follower) + leavingIndex = leaderIndex = getLeaderId(qu); + leavingServers.clear(); + joiningServers.clear(); + } + } + + /** + * 1. removes and adds back two servers (incl leader). One of the servers is added back as observer + * 2. tests that reconfig fails if quorum of new config is not up + * 3. tests that a server that's not up during reconfig learns the new config when it comes up + * @throws Exception + */ + @Test + public void testRemoveAddTwo() throws Exception { + qu = new QuorumUtil(2); // create 5 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + List leavingServers = new ArrayList(); + List joiningServers = new ArrayList(); + + int leaderIndex = getLeaderId(qu); + + // lets remove the leader and some other server + int leavingIndex1 = leaderIndex; + int leavingIndex2 = (leaderIndex == 1) ? 2 : 1; + + // find some server that's staying + int stayingIndex1 = 1, stayingIndex2 = 1, stayingIndex3 = 1; + while (stayingIndex1 == leavingIndex1 || stayingIndex1 == leavingIndex2) { + stayingIndex1++; + } + + while (stayingIndex2 == leavingIndex1 || stayingIndex2 == leavingIndex2 || stayingIndex2 == stayingIndex1) { + stayingIndex2++; + } + + while (stayingIndex3 == leavingIndex1 + || stayingIndex3 == leavingIndex2 + || stayingIndex3 == stayingIndex1 + || stayingIndex3 == stayingIndex2) { + stayingIndex3++; + } + + leavingServers.add(Integer.toString(leavingIndex1)); + leavingServers.add(Integer.toString(leavingIndex2)); + + // remember these servers so we can add them back later + joiningServers.add("server." + + leavingIndex1 + + "=localhost:" + + qu.getPeer(leavingIndex1).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(leavingIndex1).peer.getElectionAddress().getAllPorts().get(0) + + ":participant;localhost:" + + qu.getPeer(leavingIndex1).peer.getClientPort()); + + // this server will be added back as an observer + joiningServers.add("server." + + leavingIndex2 + + "=localhost:" + + qu.getPeer(leavingIndex2).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(leavingIndex2).peer.getElectionAddress().getAllPorts().get(0) + + ":observer;localhost:" + + qu.getPeer(leavingIndex2).peer.getClientPort()); + + qu.shutdown(leavingIndex1); + qu.shutdown(leavingIndex2); + + // 3 servers still up so this should work + reconfig(zkAdminArr[stayingIndex2], null, leavingServers, null, -1); + + qu.shutdown(stayingIndex2); + + // the following commands would not work in the original + // cluster of 5, but now that we've removed 2 servers + // we have a cluster of 3 servers and one of them is allowed to fail + + testServerHasConfig(zkArr[stayingIndex1], null, leavingServers); + testServerHasConfig(zkArr[stayingIndex3], null, leavingServers); + testNormalOperation(zkArr[stayingIndex1], zkArr[stayingIndex3]); + + // this is a test that a reconfig will only succeed + // if there is a quorum up in new config. Below there is no + // quorum so it should fail + + // the sleep is necessary so that the leader figures out + // that the switched off servers are down + Thread.sleep(10000); + + try { + reconfig(zkAdminArr[stayingIndex1], joiningServers, null, null, -1); + fail("reconfig completed successfully even though there is no quorum up in new config!"); + } catch (KeeperException.NewConfigNoQuorum e) { + + } + + // now start the third server so that new config has quorum + qu.restart(stayingIndex2); + + reconfig(zkAdminArr[stayingIndex1], joiningServers, null, null, -1); + testNormalOperation(zkArr[stayingIndex2], zkArr[stayingIndex3]); + testServerHasConfig(zkArr[stayingIndex2], joiningServers, null); + + // this server wasn't around during the configuration change + // we should check that it is able to connect, finds out + // about the change and becomes an observer. + + qu.restart(leavingIndex2); + assertTrue(qu.getPeer(leavingIndex2).peer.getPeerState() == ServerState.OBSERVING); + testNormalOperation(zkArr[stayingIndex2], zkArr[leavingIndex2]); + testServerHasConfig(zkArr[leavingIndex2], joiningServers, null); + } + + @Test + public void testBulkReconfig() throws Exception { + qu = new QuorumUtil(3); // create 7 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + // new config will have three of the servers as followers + // two of the servers as observers, and all ports different + ArrayList newServers = new ArrayList(); + for (int i = 1; i <= 5; i++) { + String server = "server." + i + "=localhost:" + PortAssignment.unique() + + ":" + PortAssignment.unique() + ":" + ((i == 4 || i == 5) ? "observer" : "participant") + + ";localhost:" + qu.getPeer(i).peer.getClientPort(); + newServers.add(server); + } + + qu.shutdown(3); + qu.shutdown(6); + qu.shutdown(7); + + reconfig(zkAdminArr[1], null, null, newServers, -1); + testNormalOperation(zkArr[1], zkArr[2]); + + testServerHasConfig(zkArr[1], newServers, null); + testServerHasConfig(zkArr[2], newServers, null); + testServerHasConfig(zkArr[4], newServers, null); + testServerHasConfig(zkArr[5], newServers, null); + + qu.shutdown(5); + qu.shutdown(4); + + testNormalOperation(zkArr[1], zkArr[2]); + } + + @Test + public void testRemoveOneAsynchronous() throws Exception { + qu = new QuorumUtil(2); + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + List leavingServers = new ArrayList(); + + // lets remove someone who's not the leader + leavingServers.add(getLeaderId(qu) == 5 ? "4" : "5"); + + List results = new LinkedList(); + + zkAdminArr[1].reconfigure(null, leavingServers, null, -1, this, results); + + synchronized (results) { + while (results.size() < 1) { + results.wait(); + } + } + assertEquals(0, (int) results.get(0)); + + testNormalOperation(zkArr[1], zkArr[2]); + for (int i = 1; i <= 5; i++) { + testServerHasConfig(zkArr[i], null, leavingServers); + } + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) { + synchronized (ctx) { + ((LinkedList) ctx).add(rc); + ctx.notifyAll(); + } + } + + @Test + public void testRoleChange() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + // changing a server's role / port is done by "adding" it with the same + // id but different role / port + List joiningServers = new ArrayList(); + + int leaderIndex = getLeaderId(qu); + + // during first and second iteration, leavingIndex will correspond to a + // follower + // during third and fouth iteration leavingIndex will be the index of + // the leader + int changingIndex = (leaderIndex == 1) ? 2 : 1; + + // first convert participant to observer, then observer to participant, + // and so on + String newRole = "observer"; + + for (int i = 0; i < 4; i++) { + // some of the operations will be executed by a client connected to + // the removed server + // while others are invoked by a client connected to some other + // server. + // when we're removing the leader, zk1 will be the client connected + // to removed server + ZooKeeper zk1 = (changingIndex == leaderIndex) ? zkArr[leaderIndex] : zkArr[(leaderIndex % qu.ALL) + 1]; + ZooKeeperAdmin zkAdmin1 = (changingIndex == leaderIndex) + ? zkAdminArr[leaderIndex] + : zkAdminArr[(leaderIndex % qu.ALL) + 1]; + + // exactly as it is now, except for role change + joiningServers.add("server." + + changingIndex + + "=localhost:" + + qu.getPeer(changingIndex).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(changingIndex).peer.getElectionAddress().getAllPorts().get(0) + + ":" + + newRole + + ";localhost:" + + qu.getPeer(changingIndex).peer.getClientPort()); + + reconfig(zkAdmin1, joiningServers, null, null, -1); + testNormalOperation(zkArr[changingIndex], zk1); + + if (newRole.equals("observer")) { + assertTrue(qu.getPeer(changingIndex).peer.observer != null + && qu.getPeer(changingIndex).peer.follower == null + && qu.getPeer(changingIndex).peer.leader == null); + assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.OBSERVING); + } else { + assertTrue(qu.getPeer(changingIndex).peer.observer == null + && (qu.getPeer(changingIndex).peer.follower != null + || qu.getPeer(changingIndex).peer.leader != null)); + assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.FOLLOWING + || qu.getPeer(changingIndex).peer.getPeerState() == ServerState.LEADING); + } + + joiningServers.clear(); + + if (newRole.equals("observer")) { + newRole = "participant"; + } else { + // lets change leader to observer + newRole = "observer"; + leaderIndex = getLeaderId(qu); + changingIndex = leaderIndex; + } + } + } + + @Test + public void testPortChange() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + List joiningServers = new ArrayList(); + + int leaderIndex = getLeaderId(qu); + int followerIndex = leaderIndex == 1 ? 2 : 1; + + // modify follower's client port + + int quorumPort = qu.getPeer(followerIndex).peer.getQuorumAddress().getAllPorts().get(0); + int electionPort = qu.getPeer(followerIndex).peer.getElectionAddress().getAllPorts().get(0); + int oldClientPort = qu.getPeer(followerIndex).peer.getClientPort(); + int newClientPort = PortAssignment.unique(); + joiningServers.add("server." + + followerIndex + + "=localhost:" + + quorumPort + + ":" + + electionPort + + ":participant;localhost:" + + newClientPort); + + // create a /test znode and check that read/write works before + // any reconfig is invoked + testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]); + + reconfig(zkAdminArr[followerIndex], joiningServers, null, null, -1); + + try { + for (int i = 0; i < 20; i++) { + Thread.sleep(1000); + zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1); + } + } catch (KeeperException.ConnectionLossException e) { + fail("Existing client disconnected when client port changed!"); + } + + zkArr[followerIndex].close(); + zkArr[followerIndex] = new ZooKeeper( + "127.0.0.1:" + oldClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + + zkAdminArr[followerIndex].close(); + zkAdminArr[followerIndex] = new ZooKeeperAdmin( + "127.0.0.1:" + oldClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes()); + + for (int i = 0; i < 10; i++) { + try { + Thread.sleep(1000); + zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1); + fail("New client connected to old client port!"); + } catch (KeeperException.ConnectionLossException e) { + } + } + + zkArr[followerIndex].close(); + zkArr[followerIndex] = new ZooKeeper( + "127.0.0.1:" + newClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + + zkAdminArr[followerIndex].close(); + zkAdminArr[followerIndex] = new ZooKeeperAdmin( + "127.0.0.1:" + newClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes()); + + testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]); + testServerHasConfig(zkArr[followerIndex], joiningServers, null); + assertEquals(newClientPort, qu.getPeer(followerIndex).peer.getClientPort()); + + joiningServers.clear(); + + // change leader's leading port - should renounce leadership + + int newQuorumPort = PortAssignment.unique(); + joiningServers.add("server." + leaderIndex + "=localhost:" + + newQuorumPort + + ":" + + qu.getPeer(leaderIndex).peer.getElectionAddress().getAllPorts().get(0) + + ":participant;localhost:" + + qu.getPeer(leaderIndex).peer.getClientPort()); + + reconfig(zkAdminArr[leaderIndex], joiningServers, null, null, -1); + + testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]); + + assertEquals((int) qu.getPeer(leaderIndex).peer.getQuorumAddress().getAllPorts().get(0), newQuorumPort); + + joiningServers.clear(); + + // change everyone's leader election port + + for (int i = 1; i <= 3; i++) { + joiningServers.add("server." + i + "=localhost:" + + qu.getPeer(i).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + PortAssignment.unique() + + ":participant;localhost:" + + qu.getPeer(i).peer.getClientPort()); + } + + reconfig(zkAdminArr[1], joiningServers, null, null, -1); + + leaderIndex = getLeaderId(qu); + int follower1 = leaderIndex == 1 ? 2 : 1; + int follower2 = 1; + while (follower2 == leaderIndex || follower2 == follower1) { + follower2++; + } + + // lets kill the leader and see if a new one is elected + + qu.shutdown(getLeaderId(qu)); + + testNormalOperation(zkArr[follower2], zkArr[follower1]); + testServerHasConfig(zkArr[follower1], joiningServers, null); + testServerHasConfig(zkArr[follower2], joiningServers, null); + } + + @Test + public void testPortChangeToBlockedPortFollower() throws Exception { + testPortChangeToBlockedPort(false); + } + @Test + public void testPortChangeToBlockedPortLeader() throws Exception { + testPortChangeToBlockedPort(true); + } + + private void testPortChangeToBlockedPort(boolean testLeader) throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + List joiningServers = new ArrayList(); + + int leaderIndex = getLeaderId(qu); + int followerIndex = leaderIndex == 1 ? 2 : 1; + int serverIndex = testLeader ? leaderIndex : followerIndex; + int reconfigIndex = testLeader ? followerIndex : leaderIndex; + + // modify server's client port + int quorumPort = qu.getPeer(serverIndex).peer.getQuorumAddress().getAllPorts().get(0); + int electionPort = qu.getPeer(serverIndex).peer.getElectionAddress().getAllPorts().get(0); + int oldClientPort = qu.getPeer(serverIndex).peer.getClientPort(); + int newClientPort = PortAssignment.unique(); + + try (ServerSocket ss = new ServerSocket()) { + ss.bind(new InetSocketAddress(getLoopbackAddress(), newClientPort)); + + joiningServers.add("server." + serverIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + newClientPort); + + // create a /test znode and check that read/write works before + // any reconfig is invoked + testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]); + + // Reconfigure + reconfig(zkAdminArr[reconfigIndex], joiningServers, null, null, -1); + Thread.sleep(1000); + + // The follower reconfiguration will have failed + zkArr[serverIndex].close(); + zkArr[serverIndex] = new ZooKeeper( + "127.0.0.1:" + newClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + + zkAdminArr[serverIndex].close(); + zkAdminArr[serverIndex] = new ZooKeeperAdmin( + "127.0.0.1:" + newClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + + try { + Thread.sleep(1000); + zkArr[serverIndex].setData("/test", "teststr".getBytes(), -1); + fail("New client connected to new client port!"); + } catch (KeeperException.ConnectionLossException e) { + // Exception is expected + } + + //The old port should be clear at this stage + + try (ServerSocket ss2 = new ServerSocket()) { + ss2.bind(new InetSocketAddress(getLoopbackAddress(), oldClientPort)); + } + + // Move back to the old port + joiningServers.clear(); + joiningServers.add("server." + serverIndex + "=localhost:" + quorumPort + ":" + electionPort + + ":participant;localhost:" + oldClientPort); + + reconfig(zkAdminArr[reconfigIndex], joiningServers, null, null, -1); + + zkArr[serverIndex].close(); + zkArr[serverIndex] = new ZooKeeper( + "127.0.0.1:" + oldClientPort, + ClientBase.CONNECTION_TIMEOUT, + DummyWatcher.INSTANCE); + + testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]); + testServerHasConfig(zkArr[serverIndex], joiningServers, null); + assertEquals(oldClientPort, qu.getPeer(serverIndex).peer.getClientPort()); + } + } + + @Test + public void testUnspecifiedClientAddress() throws Exception { + int[] ports = {PortAssignment.unique(), PortAssignment.unique(), PortAssignment.unique()}; + + String server = "server.0=localhost:" + ports[0] + ":" + ports[1] + ";" + ports[2]; + QuorumServer qs = new QuorumServer(0, server); + assertEquals(qs.clientAddr.getHostString(), "0.0.0.0"); + assertEquals(qs.clientAddr.getPort(), ports[2]); + } + + @Test + public void testQuorumSystemChange() throws Exception { + qu = new QuorumUtil(3); // create 7 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + ArrayList members = new ArrayList<>(); + members.add("group.1=3:4:5"); + members.add("group.2=1:2"); + members.add("weight.1=0"); + members.add("weight.2=0"); + members.add("weight.3=1"); + members.add("weight.4=1"); + members.add("weight.5=1"); + + for (int i = 1; i <= 5; i++) { + members.add("server." + i + "=127.0.0.1:" + + qu.getPeer(i).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(i).peer.getElectionAddress().getAllPorts().get(0) + + ";" + + "127.0.0.1:" + + qu.getPeer(i).peer.getClientPort()); + } + + reconfig(zkAdminArr[1], null, null, members, -1); + + // this should flush the config to servers 2, 3, 4 and 5 + testNormalOperation(zkArr[2], zkArr[3]); + testNormalOperation(zkArr[4], zkArr[5]); + + for (int i = 1; i <= 5; i++) { + if (!(qu.getPeer(i).peer.getQuorumVerifier() instanceof QuorumHierarchical)) { + fail("peer " + i + " doesn't think the quorum system is Hieararchical!"); + } + } + + qu.shutdown(1); + qu.shutdown(2); + qu.shutdown(3); + qu.shutdown(7); + qu.shutdown(6); + + // servers 4 and 5 should be able to work independently + testNormalOperation(zkArr[4], zkArr[5]); + + qu.restart(1); + qu.restart(2); + + members.clear(); + for (int i = 1; i <= 3; i++) { + members.add("server." + i + "=127.0.0.1:" + + qu.getPeer(i).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(i).peer.getElectionAddress().getAllPorts().get(0) + + ";" + + "127.0.0.1:" + + qu.getPeer(i).peer.getClientPort()); + } + + reconfig(zkAdminArr[1], null, null, members, -1); + + // flush the config to server 2 + testNormalOperation(zkArr[1], zkArr[2]); + + qu.shutdown(4); + qu.shutdown(5); + + // servers 1 and 2 should be able to work independently + testNormalOperation(zkArr[1], zkArr[2]); + + for (int i = 1; i <= 2; i++) { + if (!(qu.getPeer(i).peer.getQuorumVerifier() instanceof QuorumMaj)) { + fail("peer " + i + " doesn't think the quorum system is a majority quorum system!"); + } + } + } + + @Test + public void testInitialConfigHasPositiveVersion() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + testNormalOperation(zkArr[1], zkArr[2]); + for (int i = 1; i < 4; i++) { + String configStr = testServerHasConfig(zkArr[i], null, null); + QuorumVerifier qv = qu.getPeer(i).peer.configFromString(configStr); + long version = qv.getVersion(); + assertTrue(version == 0x100000000L); + } + } + + /** + * Tests verifies the jmx attributes of local and remote peer bean - remove + * one quorum peer and again adding it back + */ + @Test + public void testJMXBeanAfterRemoveAddOne() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + List leavingServers = new ArrayList(); + List joiningServers = new ArrayList(); + + // assert remotePeerBean.1 of ReplicatedServer_2 + int leavingIndex = 1; + int replica2 = 2; + QuorumPeer peer2 = qu.getPeer(replica2).peer; + QuorumServer leavingQS2 = peer2.getView().get(Long.valueOf(leavingIndex)); + String remotePeerBean2 = MBeanRegistry.DOMAIN + + ":name0=ReplicatedServer_id" + + replica2 + + ",name1=replica." + + leavingIndex; + assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2); + + // assert remotePeerBean.1 of ReplicatedServer_3 + int replica3 = 3; + QuorumPeer peer3 = qu.getPeer(replica3).peer; + QuorumServer leavingQS3 = peer3.getView().get(Long.valueOf(leavingIndex)); + String remotePeerBean3 = MBeanRegistry.DOMAIN + + ":name0=ReplicatedServer_id" + + replica3 + + ",name1=replica." + + leavingIndex; + assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3); + + ZooKeeper zk = zkArr[leavingIndex]; + ZooKeeperAdmin zkAdmin = zkAdminArr[leavingIndex]; + + leavingServers.add(Integer.toString(leavingIndex)); + + // remember this server so we can add it back later + joiningServers.add("server." + leavingIndex + "=127.0.0.1:" + + qu.getPeer(leavingIndex).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(leavingIndex).peer.getElectionAddress().getAllPorts().get(0) + + ":participant;127.0.0.1:" + + qu.getPeer(leavingIndex).peer.getClientPort()); + + // Remove ReplicatedServer_1 from the ensemble + reconfig(zkAdmin, null, leavingServers, null, -1); + + // localPeerBean.1 of ReplicatedServer_1 + QuorumPeer removedPeer = qu.getPeer(leavingIndex).peer; + String localPeerBean = MBeanRegistry.DOMAIN + + ":name0=ReplicatedServer_id" + + leavingIndex + + ",name1=replica." + + leavingIndex; + assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, false); + + // remotePeerBean.1 shouldn't exists in ReplicatedServer_2 + JMXEnv.ensureNone(remotePeerBean2); + // remotePeerBean.1 shouldn't exists in ReplicatedServer_3 + JMXEnv.ensureNone(remotePeerBean3); + + // Add ReplicatedServer_1 back to the ensemble + reconfig(zkAdmin, joiningServers, null, null, -1); + + // localPeerBean.1 of ReplicatedServer_1 + assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, true); + + // assert remotePeerBean.1 of ReplicatedServer_2 + leavingQS2 = peer2.getView().get(Long.valueOf(leavingIndex)); + assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2); + + // assert remotePeerBean.1 of ReplicatedServer_3 + leavingQS3 = peer3.getView().get(Long.valueOf(leavingIndex)); + assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3); + } + + /** + * Tests verifies the jmx attributes of local and remote peer bean - change + * participant to observer role + */ + @Test + public void testJMXBeanAfterRoleChange() throws Exception { + qu = new QuorumUtil(1); // create 3 servers + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + zkAdminArr = createAdminHandles(qu); + + // changing a server's role / port is done by "adding" it with the same + // id but different role / port + List joiningServers = new ArrayList(); + + // assert remotePeerBean.1 of ReplicatedServer_2 + int changingIndex = 1; + int replica2 = 2; + QuorumPeer peer2 = qu.getPeer(replica2).peer; + QuorumServer changingQS2 = peer2.getView().get(Long.valueOf(changingIndex)); + String remotePeerBean2 = MBeanRegistry.DOMAIN + + ":name0=ReplicatedServer_id" + + replica2 + + ",name1=replica." + + changingIndex; + assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2); + + // assert remotePeerBean.1 of ReplicatedServer_3 + int replica3 = 3; + QuorumPeer peer3 = qu.getPeer(replica3).peer; + QuorumServer changingQS3 = peer3.getView().get(Long.valueOf(changingIndex)); + String remotePeerBean3 = MBeanRegistry.DOMAIN + + ":name0=ReplicatedServer_id" + + replica3 + + ",name1=replica." + + changingIndex; + assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3); + + String newRole = "observer"; + + ZooKeeper zk = zkArr[changingIndex]; + ZooKeeperAdmin zkAdmin = zkAdminArr[changingIndex]; + + // exactly as it is now, except for role change + joiningServers.add("server." + changingIndex + "=127.0.0.1:" + + qu.getPeer(changingIndex).peer.getQuorumAddress().getAllPorts().get(0) + + ":" + + qu.getPeer(changingIndex).peer.getElectionAddress().getAllPorts().get(0) + + ":" + + newRole + + ";127.0.0.1:" + + qu.getPeer(changingIndex).peer.getClientPort()); + + reconfig(zkAdmin, joiningServers, null, null, -1); + testNormalOperation(zkArr[changingIndex], zk); + + assertTrue(qu.getPeer(changingIndex).peer.observer != null + && qu.getPeer(changingIndex).peer.follower == null + && qu.getPeer(changingIndex).peer.leader == null); + assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.OBSERVING); + + QuorumPeer qp = qu.getPeer(changingIndex).peer; + String localPeerBeanName = MBeanRegistry.DOMAIN + + ":name0=ReplicatedServer_id" + + changingIndex + + ",name1=replica." + + changingIndex; + + // localPeerBean.1 of ReplicatedServer_1 + assertLocalPeerMXBeanAttributes(qp, localPeerBeanName, true); + + // assert remotePeerBean.1 of ReplicatedServer_2 + changingQS2 = peer2.getView().get(Long.valueOf(changingIndex)); + assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2); + + // assert remotePeerBean.1 of ReplicatedServer_3 + changingQS3 = peer3.getView().get(Long.valueOf(changingIndex)); + assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3); + } + + + @Test + public void testReconfigEnablemntWithRollingRestart() throws Exception { + + // make sure dynamic reconfig is disabled + QuorumPeerConfig.setReconfigEnabled(false); + + // start a 3 node cluster + qu = new QuorumUtil(1); + qu.disableJMXTest = true; + qu.startAll(); + zkArr = createHandles(qu); + testNormalOperation(zkArr[1], zkArr[1], true); + + + // enable dynamic reconfig (new servers created after this time will be initialized with reconfigEnabled=true) + QuorumPeerConfig.setReconfigEnabled(true); + + // restart the three servers, one-by-one, now with reconfig enabled + // test if we can write / read in the cluster after each rolling restart step + for (int i = 1; i < 4; i++) { + assertFalse("dynamic reconfig was not disabled before stopping server " + i, qu.getPeer(i).peer.isReconfigEnabled()); + qu.shutdown(i); + qu.restart(i); + assertTrue("dynamic reconfig is not enabled for the restarted server " + i, qu.getPeer(i).peer.isReconfigEnabled()); + testNormalOperation(zkArr[i], zkArr[(i % 3) + 1], false); + } + + // now we will test dynamic reconfig by remove server 2, then add it back later + List leavingServers = new ArrayList<>(); + List joiningServers = new ArrayList<>(); + leavingServers.add("2"); + + // remember this server so we can add it back later + joiningServers.add(String.format("server.2=localhost:%d:%d:participant;localhost:%d", + qu.getPeer(2).peer.getQuorumAddress().getAllPorts().get(0), + qu.getPeer(2).peer.getElectionAddress().getAllPorts().get(0), + qu.getPeer(2).peer.getClientPort())); + + // here we remove server 2 + zkAdminArr = createAdminHandles(qu); + String configStr = reconfig(zkAdminArr[1], null, leavingServers, null, -1); + testServerHasConfig(zkArr[3], null, leavingServers); + testNormalOperation(zkArr[1], zkArr[3], false); + + + // here we add back server 2 + QuorumVerifier qv = qu.getPeer(1).peer.configFromString(configStr); + long version = qv.getVersion(); + reconfig(zkAdminArr[3], joiningServers, null, null, version); + + testServerHasConfig(zkArr[1], joiningServers, null); + testServerHasConfig(zkArr[2], joiningServers, null); + testServerHasConfig(zkArr[3], joiningServers, null); + testNormalOperation(zkArr[3], zkArr[1], false); + } + + + private void assertLocalPeerMXBeanAttributes( + QuorumPeer qp, + String beanName, + Boolean isPartOfEnsemble) throws Exception { + assertEquals( + "Mismatches LearnerType!", + qp.getLearnerType().name(), + JMXEnv.ensureBeanAttribute(beanName, "LearnerType")); + assertEquals( + "Mismatches ClientAddress!", + qp.getClientAddress().getHostString() + ":" + qp.getClientAddress().getPort(), + JMXEnv.ensureBeanAttribute(beanName, "ClientAddress")); + assertEquals( + "Mismatches LearnerType!", + qp.getElectionAddress().getOne().getHostString() + ":" + qp.getElectionAddress().getOne().getPort(), + JMXEnv.ensureBeanAttribute(beanName, "ElectionAddress")); + assertEquals( + "Mismatches PartOfEnsemble!", + isPartOfEnsemble, + JMXEnv.ensureBeanAttribute(beanName, "PartOfEnsemble")); + assertEquals( + "Mismatches ConfigVersion!", + qp.getQuorumVerifier().getVersion(), + JMXEnv.ensureBeanAttribute(beanName, "ConfigVersion")); + assertEquals( + "Mismatches QuorumSystemInfo!", + qp.getQuorumVerifier().toString(), + JMXEnv.ensureBeanAttribute(beanName, "QuorumSystemInfo")); + } + + String getAddrPortFromBean(String beanName, String attribute) throws Exception { + String name = (String) JMXEnv.ensureBeanAttribute(beanName, attribute); + + if (!name.contains(":")) { + return name; + } + + return getNumericalAddrPort(name); + } + + String getNumericalAddrPort(String name) throws UnknownHostException { + String port = name.split(":")[1]; + String addr = name.split(":")[0]; + addr = InetAddress.getByName(addr).getHostAddress(); + return addr + ":" + port; + } + + private void assertRemotePeerMXBeanAttributes(QuorumServer qs, String beanName) throws Exception { + assertEquals( + "Mismatches LearnerType!", + qs.type.name(), + JMXEnv.ensureBeanAttribute(beanName, "LearnerType")); + assertEquals( + "Mismatches ClientAddress!", + getNumericalAddrPort(qs.clientAddr.getHostString() + ":" + qs.clientAddr.getPort()), + getAddrPortFromBean(beanName, "ClientAddress")); + assertEquals( + "Mismatches ElectionAddress!", + getNumericalAddrPort(qs.electionAddr.getOne().getHostString() + ":" + qs.electionAddr.getOne().getPort()), + getAddrPortFromBean(beanName, "ElectionAddress")); + assertEquals( + "Mismatches QuorumAddress!", + getNumericalAddrPort(qs.addr.getOne().getHostString() + ":" + qs.addr.getOne().getPort()), + getAddrPortFromBean(beanName, "QuorumAddress")); + } + + + /* + * A helper class to parse / compare server address config lines. + * Example: server.1=127.0.0.1:11228:11231|127.0.0.1:11230:11229:participant;0.0.0.0:11227 + */ + private static class ServerConfigLine { + private final int serverId; + private Integer clientPort; + + // hostName -> + private final Map> quorumPorts = new HashMap<>(); + + // hostName -> + private final Map> electionPorts = new HashMap<>(); + + private ServerConfigLine(String configLine) { + String[] parts = configLine.trim().split("="); + serverId = parseInt(parts[0].split("\\.")[1]); + String[] serverConfig = parts[1].split(";"); + String[] serverAddresses = serverConfig[0].split("\\|"); + if (serverConfig.length > 1) { + String[] clientParts = serverConfig[1].split(":"); + if (clientParts.length > 1) { + clientPort = parseInt(clientParts[1]); + } else { + clientPort = parseInt(clientParts[0]); + } + } + + for (String addr : serverAddresses) { + // addr like: 127.0.0.1:11230:11229:participant or [0:0:0:0:0:0:0:1]:11346:11347 + String serverHost; + String[] ports; + if (addr.contains("[")) { + serverHost = addr.substring(1, addr.indexOf("]")); + ports = addr.substring(addr.indexOf("]") + 2).split(":"); + } else { + serverHost = addr.substring(0, addr.indexOf(":")); + ports = addr.substring(addr.indexOf(":") + 1).split(":"); + } + + quorumPorts.computeIfAbsent(serverHost, k -> new HashSet<>()).add(parseInt(ports[0])); + if (ports.length > 1) { + electionPorts.computeIfAbsent(serverHost, k -> new HashSet<>()).add(parseInt(ports[1])); + } + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ServerConfigLine that = (ServerConfigLine) o; + return serverId == that.serverId + && Objects.equals(clientPort, that.clientPort) + && quorumPorts.equals(that.quorumPorts) + && electionPorts.equals(that.electionPorts); + } + + @Override + public int hashCode() { + return Objects.hash(serverId, clientPort, quorumPorts, electionPorts); + } + } + + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/RecoveryTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/RecoveryTest.java new file mode 100644 index 0000000..4f4d12d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/RecoveryTest.java @@ -0,0 +1,184 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RecoveryTest extends ZKTestCase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(RecoveryTest.class); + + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + + private volatile CountDownLatch startSignal; + + /** + * Verify that if a server goes down that clients will reconnect + * automatically after the server is restarted. Note that this requires the + * server to restart within the connection timeout period. + * + * Also note that the client latches are used to eliminate any chance + * of spurrious connectionloss exceptions on the read ops. Specifically + * a sync operation will throw this exception if the server goes down + * (as recognized by the client) during the operation. If the operation + * occurs after the server is down, but before the client recognizes + * that the server is down (ping) then the op will throw connectionloss. + */ + @Test + public void testRecovery() throws Exception { + File tmpDir = ClientBase.createTmpDir(); + + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + + int oldSnapCount = SyncRequestProcessor.getSnapCount(); + SyncRequestProcessor.setSnapCount(1000); + try { + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + LOG.info("starting up the the server, waiting"); + + assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + + startSignal = new CountDownLatch(1); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this); + startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + assertTrue("count == 0", startSignal.getCount() == 0); + String path; + LOG.info("starting creating nodes"); + for (int i = 0; i < 10; i++) { + path = "/" + i; + zk.create(path, (path + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int j = 0; j < 10; j++) { + String subpath = path + "/" + j; + zk.create(subpath, (subpath + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + for (int k = 0; k < 20; k++) { + String subsubpath = subpath + "/" + k; + zk.create(subsubpath, (subsubpath + + "!").getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } + } + + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + f = ServerCnxnFactory.createFactory(PORT, -1); + + startSignal = new CountDownLatch(1); + + f.startup(zks); + + assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + + startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + assertTrue("count == 0", startSignal.getCount() == 0); + + Stat stat = new Stat(); + for (int i = 0; i < 10; i++) { + path = "/" + i; + LOG.info("Checking " + path); + assertEquals(new String(zk.getData(path, false, stat)), path + "!"); + for (int j = 0; j < 10; j++) { + String subpath = path + "/" + j; + assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!"); + for (int k = 0; k < 20; k++) { + String subsubpath = subpath + "/" + k; + assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!"); + } + } + } + f.shutdown(); + zks.shutdown(); + + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT)); + + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + f = ServerCnxnFactory.createFactory(PORT, -1); + + startSignal = new CountDownLatch(1); + + f.startup(zks); + + assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + + startSignal.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); + assertTrue("count == 0", startSignal.getCount() == 0); + + stat = new Stat(); + LOG.info("Check 2"); + for (int i = 0; i < 10; i++) { + path = "/" + i; + assertEquals(new String(zk.getData(path, false, stat)), path + "!"); + for (int j = 0; j < 10; j++) { + String subpath = path + "/" + j; + assertEquals(new String(zk.getData(subpath, false, stat)), subpath + "!"); + for (int k = 0; k < 20; k++) { + String subsubpath = subpath + "/" + k; + assertEquals(new String(zk.getData(subsubpath, false, stat)), subsubpath + "!"); + } + } + } + zk.close(); + + f.shutdown(); + zks.shutdown(); + + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + } finally { + SyncRequestProcessor.setSnapCount(oldSnapCount); + } + } + + /* + * (non-Javadoc) + * + * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatcherEvent) + */ + public void process(WatchedEvent event) { + LOG.info("Event:{} {} {}", event.getState(), event.getType(), event.getPath()); + if (event.getState() == KeeperState.SyncConnected && startSignal != null && startSignal.getCount() > 0) { + startSignal.countDown(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/RepeatStartupTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/RepeatStartupTest.java new file mode 100644 index 0000000..27fa4a9 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/RepeatStartupTest.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Test; + +public class RepeatStartupTest extends ZKTestCase { + + /** bring up 5 quorum peers and then shut them down + * and then bring one of the nodes as server + * + * @throws Exception might be thrown here + */ + @Test + public void testFail() throws Exception { + QuorumBase qb = new QuorumBase(); + qb.setUp(); + + System.out.println("Comment: the servers are at " + qb.hostPort); + ZooKeeper zk = qb.createClient(); + zk.create("/test", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.close(); + QuorumBase.shutdown(qb.s1); + QuorumBase.shutdown(qb.s2); + QuorumBase.shutdown(qb.s3); + QuorumBase.shutdown(qb.s4); + QuorumBase.shutdown(qb.s5); + String hp = qb.hostPort.split(",")[0]; + ZooKeeperServer zks = new ZooKeeperServer(qb.s1.getTxnFactory().getSnapDir(), qb.s1.getTxnFactory().getDataDir(), 3000); + final int PORT = Integer.parseInt(hp.split(":")[1]); + ServerCnxnFactory factory = ServerCnxnFactory.createFactory(PORT, -1); + + factory.startup(zks); + System.out.println("Comment: starting factory"); + assertTrue( + "waiting for server up", + ClientBase.waitForServerUp("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT)); + factory.shutdown(); + zks.shutdown(); + assertTrue( + "waiting for server down", + ClientBase.waitForServerDown("127.0.0.1:" + PORT, QuorumTest.CONNECTION_TIMEOUT)); + System.out.println("Comment: shutting down standalone"); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ResponseCacheTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ResponseCacheTest.java new file mode 100644 index 0000000..0b27ff5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ResponseCacheTest.java @@ -0,0 +1,196 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.fail; +import java.util.List; +import java.util.Map; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.metrics.MetricsUtils; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ResponseCacheTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(ResponseCacheTest.class); + + @Before + public void setup() throws Exception { + System.setProperty(ZooKeeperServer.GET_DATA_RESPONSE_CACHE_SIZE, "32"); + System.setProperty(ZooKeeperServer.GET_CHILDREN_RESPONSE_CACHE_SIZE, "64"); + super.setUp(); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(ZooKeeperServer.GET_DATA_RESPONSE_CACHE_SIZE); + System.clearProperty(ZooKeeperServer.GET_CHILDREN_RESPONSE_CACHE_SIZE); + } + + @Test + public void testResponseCache() throws Exception { + ZooKeeper zk = createClient(); + + try { + performCacheTest(zk, "/cache", true); + performCacheTest(zk, "/nocache", false); + } finally { + zk.close(); + } + } + + private void checkCacheStatus(long expectedHits, long expectedMisses, + String cacheHitMetricsName, String cacheMissMetricsName) { + + Map metrics = MetricsUtils.currentServerMetrics(); + assertEquals(expectedHits, metrics.get(cacheHitMetricsName)); + assertEquals(expectedMisses, metrics.get(cacheMissMetricsName)); + } + + public void performCacheTest(ZooKeeper zk, String path, boolean useCache) throws Exception { + ServerMetrics.getMetrics().resetAll(); + Stat writeStat = new Stat(); + Stat readStat = new Stat(); + byte[] readData = null; + int reads = 10; + long expectedHits = 0; + long expectedMisses = 0; + + ZooKeeperServer zks = serverFactory.getZooKeeperServer(); + zks.setResponseCachingEnabled(useCache); + LOG.info("caching: {}", useCache); + + if (useCache) { + assertEquals(zks.getReadResponseCache().getCacheSize(), 32); + assertEquals(zks.getGetChildrenResponseCache().getCacheSize(), 64); + } + + byte[] writeData = "test1".getBytes(); + zk.create(path, writeData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, writeStat); + for (int i = 0; i < reads; ++i) { + readData = zk.getData(path, false, readStat); + assertArrayEquals(writeData, readData); + assertEquals(writeStat, readStat); + } + if (useCache) { + expectedMisses += 1; + expectedHits += reads - 1; + } + checkCacheStatus(expectedHits, expectedMisses, "response_packet_cache_hits", + "response_packet_cache_misses"); + + writeData = "test2".getBytes(); + writeStat = zk.setData(path, writeData, -1); + for (int i = 0; i < 10; ++i) { + readData = zk.getData(path, false, readStat); + assertArrayEquals(writeData, readData); + assertEquals(writeStat, readStat); + } + if (useCache) { + expectedMisses += 1; + expectedHits += reads - 1; + } + checkCacheStatus(expectedHits, expectedMisses, "response_packet_cache_hits", + "response_packet_cache_misses"); + + // Create a child beneath the tested node. This won't change the data of + // the tested node, but will change it's pzxid. The next read of the tested + // node should miss in the cache. The data should still match what was written + // before, but the stat information should not. + zk.create(path + "/child", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + readData = zk.getData(path, false, readStat); + if (useCache) { + expectedMisses++; + } + assertArrayEquals(writeData, readData); + assertNotSame(writeStat, readStat); + checkCacheStatus(expectedHits, expectedMisses, "response_packet_cache_hits", + "response_packet_cache_misses"); + + ServerMetrics.getMetrics().resetAll(); + expectedHits = 0; + expectedMisses = 0; + createPath(path + "/a", zk); + createPath(path + "/a/b", zk); + createPath(path + "/a/c", zk); + createPath(path + "/a/b/d", zk); + createPath(path + "/a/b/e", zk); + createPath(path + "/a/b/e/f", zk); + createPath(path + "/a/b/e/g", zk); + createPath(path + "/a/b/e/h", zk); + + checkPath(path + "/a", zk, 2); + checkPath(path + "/a/b", zk, 2); + checkPath(path + "/a/c", zk, 0); + checkPath(path + "/a/b/d", zk, 0); + checkPath(path + "/a/b/e", zk, 3); + checkPath(path + "/a/b/e/h", zk, 0); + + if (useCache) { + expectedMisses += 6; + } + + checkCacheStatus(expectedHits, expectedMisses, "response_packet_get_children_cache_hits", + "response_packet_get_children_cache_misses"); + + checkPath(path + "/a", zk, 2); + checkPath(path + "/a/b", zk, 2); + checkPath(path + "/a/c", zk, 0); + + if (useCache) { + expectedHits += 3; + } + + checkCacheStatus(expectedHits, expectedMisses, "response_packet_get_children_cache_hits", + "response_packet_get_children_cache_misses"); + } + + private void createPath(String path, ZooKeeper zk) throws Exception { + zk.create(path, "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, null); + } + + private void checkPath(String path, ZooKeeper zk, int expectedNumberOfChildren) throws Exception { + Stat stat = zk.exists(path, false); + + List c1 = zk.getChildren(path, false); + List c2 = zk.getChildren(path, false, stat); + + if (!c1.equals(c2)) { + fail("children lists from getChildren()/getChildren2() do not match"); + } + + assertEquals(c1.size(), expectedNumberOfChildren); + + if (!stat.equals(stat)) { + fail("stats from exists()/getChildren2() do not match"); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/RestoreCommittedLogTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/RestoreCommittedLogTest.java new file mode 100644 index 0000000..188396f --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/RestoreCommittedLogTest.java @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.util.Collection; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.quorum.Leader.Proposal; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** After a replica starts, it should load commits in its committedLog list. + * This test checks if committedLog != 0 after replica restarted. + */ +public class RestoreCommittedLogTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(RestoreCommittedLogTest.class); + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + private static final int CONNECTION_TIMEOUT = 3000; + + /** + * Verify the logs can be used to restore when they are rolled + * based on the size of the transactions received + * + * @throws Exception + */ + @Test + public void testRestoreCommittedLogWithSnapSize() throws Exception { + final int minExpectedSnapshots = 5; + final int minTxnsToSnap = 256; + final int numTransactions = minExpectedSnapshots * minTxnsToSnap; + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 4 * 1024; i++) { + sb.append("0"); + } + final byte[] data = sb.toString().getBytes(); + + SyncRequestProcessor.setSnapCount(numTransactions * 1000 /* just some high number */); + // The test breaks if this number is less than the smallest size file + // created on the system, as revealed through File::length. + // Setting to about 1 Mb. + SyncRequestProcessor.setSnapSizeInBytes(minTxnsToSnap * data.length); + + testRestoreCommittedLog(numTransactions, data, minExpectedSnapshots); + + } + + /** + * Verify the logs can be used to restore when they are rolled + * based on the number of transactions received + * + * @throws Exception + */ + @Test + public void testRestoreCommittedLogWithSnapCount() throws Exception { + final int minExpectedSnapshots = 30; + final int snapCount = 100; + + SyncRequestProcessor.setSnapCount(snapCount); + SyncRequestProcessor.setSnapSizeInBytes(4294967296L); + + testRestoreCommittedLog(minExpectedSnapshots * snapCount, new byte[0], minExpectedSnapshots); + } + + /** + * test the purge + * @throws Exception an exception might be thrown here + */ + private void testRestoreCommittedLog(int totalTransactions, byte[] data, int minExpectedSnapshots) throws Exception { + File tmpDir = ClientBase.createTmpDir(); + ClientBase.setupTestEnv(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + ZooKeeper zk = ClientBase.createZKClient(HOSTPORT); + try { + for (int i = 0; i < totalTransactions; i++) { + zk.create("/invalidsnap-" + i, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + } finally { + zk.close(); + } + final int numSnaps = zks.getTxnLogFactory().findNRecentSnapshots(10 * minExpectedSnapshots).size(); + LOG.info("number of snapshots taken {}", numSnaps); + + f.shutdown(); + zks.shutdown(); + assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + + assertTrue("too few snapshot files", numSnaps > minExpectedSnapshots); + assertTrue("too many snapshot files", numSnaps <= minExpectedSnapshots * 2); + + // start server again + zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + zks.startdata(); + Collection committedLog = zks.getZKDatabase().getCommittedLog(); + int logsize = committedLog.size(); + LOG.info("committedLog size = {}", logsize); + assertTrue("log size != 0", (logsize != 0)); + zks.shutdown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SSLAuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SSLAuthTest.java new file mode 100644 index 0000000..ddfa48d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SSLAuthTest.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class SSLAuthTest extends ClientBase { + + private ClientX509Util clientX509Util; + + public static ClientX509Util setUpSecure() throws Exception { + ClientX509Util x509Util = new ClientX509Util(); + String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data"); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); + System.setProperty(x509Util.getSslAuthProviderProperty(), "x509"); + System.setProperty(x509Util.getSslKeystoreLocationProperty(), testDataPath + "/ssl/testKeyStore.jks"); + System.setProperty(x509Util.getSslKeystorePasswdProperty(), "testpass"); + System.setProperty(x509Util.getSslTruststoreLocationProperty(), testDataPath + "/ssl/testTrustStore.jks"); + System.setProperty(x509Util.getSslTruststorePasswdProperty(), "testpass"); + System.setProperty("javax.net.debug", "ssl"); + System.setProperty("zookeeper.authProvider.x509", "org.apache.zookeeper.server.auth.X509AuthenticationProvider"); + return x509Util; + } + + public static void clearSecureSetting(ClientX509Util clientX509Util) { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + System.clearProperty(ZKClientConfig.SECURE_CLIENT); + System.clearProperty(clientX509Util.getSslAuthProviderProperty()); + System.clearProperty(clientX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(clientX509Util.getSslKeystorePasswdProperty()); + System.clearProperty(clientX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(clientX509Util.getSslTruststorePasswdProperty()); + System.clearProperty("javax.net.debug"); + System.clearProperty("zookeeper.authProvider.x509"); + clientX509Util.close(); + } + + @Before + public void setUp() throws Exception { + clientX509Util = setUpSecure(); + + String host = "localhost"; + int port = PortAssignment.unique(); + hostPort = host + ":" + port; + + serverFactory = ServerCnxnFactory.createFactory(); + serverFactory.configure(new InetSocketAddress(host, port), maxCnxns, -1, true); + + super.setUp(); + } + + @After + public void teardown() throws Exception { + clearSecureSetting(clientX509Util); + } + + @Test + public void testRejection() throws Exception { + String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data"); + + // Replace trusted keys with a valid key that is not trusted by the server + System.setProperty(clientX509Util.getSslKeystoreLocationProperty(), testDataPath + + "/ssl/testUntrustedKeyStore.jks"); + System.setProperty(clientX509Util.getSslKeystorePasswdProperty(), "testpass"); + + CountdownWatcher watcher = new CountdownWatcher(); + + // Handshake will take place, and then X509AuthenticationProvider should reject the untrusted cert + new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + assertFalse("Untrusted certificate should not result in successful connection", watcher.clientConnected.await(1000, TimeUnit.MILLISECONDS)); + } + + @Test + public void testMisconfiguration() throws Exception { + System.clearProperty(clientX509Util.getSslAuthProviderProperty()); + System.clearProperty(clientX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(clientX509Util.getSslKeystorePasswdProperty()); + System.clearProperty(clientX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(clientX509Util.getSslTruststorePasswdProperty()); + + CountdownWatcher watcher = new CountdownWatcher(); + new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + assertFalse("Missing SSL configuration should not result in successful connection", watcher.clientConnected.await(1000, TimeUnit.MILLISECONDS)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.java new file mode 100644 index 0000000..95db150 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthDesignatedClientTest.java @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.Perms; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.client.ZooKeeperSaslClient; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.junit.Test; + +public class SaslAuthDesignatedClientTest extends ClientBase { + + static { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, "MyZookeeperClient"); + + try { + File tmpDir = createTmpDir(); + File saslConfFile = new File(tmpDir, "jaas.conf"); + FileWriter fwriter = new FileWriter(saslConfFile); + + fwriter.write("" + + "Server {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_myuser=\"mypassword\";\n" + + "};\n" + + "Client {\n" + + /* this 'Client' section has an incorrect password, but we're not configured + to use it (we're configured by the above System.setProperty(...LOGIN_CONTEXT_NAME_KEY...) to + use the 'MyZookeeperClient' section below, which has the correct password).*/ + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"myuser\"\n" + + " password=\"wrongpassword\";\n" + + "};" + + "MyZookeeperClient {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"myuser\"\n" + + " password=\"mypassword\";\n" + + "};" + + "\n"); + fwriter.close(); + System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + } catch (IOException e) { + // could not create tmp directory to hold JAAS conf file : test will fail now. + } + } + + @Test + public void testAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Thread.sleep(1000); + } catch (KeeperException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + @Test + public void testSaslConfig() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.getChildren("/", false); + assertFalse(zk.getSaslClient(). + clientTunneledAuthenticationInProgress()); + assertEquals(zk.getSaslClient().getSaslState(), ZooKeeperSaslClient.SaslState.COMPLETE); + assertNotNull(javax.security.auth.login.Configuration.getConfiguration(). + getAppConfigurationEntry("MyZookeeperClient")); + assertSame(zk.getSaslClient().getLoginContext(), "MyZookeeperClient"); + } catch (KeeperException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + @Test + public void testReadAccessUser() throws Exception { + System.setProperty("zookeeper.letAnySaslUserDoX", "anyone"); + ZooKeeper zk = createClient(); + List aclList = new ArrayList(); + ACL acl = new ACL(Perms.ADMIN | Perms.CREATE | Perms.WRITE | Perms.DELETE, new Id("sasl", "fakeuser")); + ACL acl1 = new ACL(Perms.READ, new Id("sasl", "anyone")); + aclList.add(acl); + aclList.add(acl1); + try { + zk.create("/abc", "testData".getBytes(), aclList, CreateMode.PERSISTENT); + } catch (KeeperException e) { + fail("Unable to create znode"); + } + zk.close(); + Thread.sleep(100); + + // try to access it with different user (myuser) + zk = createClient(); + + try { + zk.setData("/abc", "testData1".getBytes(), -1); + fail("Should not be able to set data"); + } catch (KeeperException.NoAuthException e) { + // success + } + + try { + byte[] bytedata = zk.getData("/abc", null, null); + String data = new String(bytedata); + assertTrue("testData".equals(data)); + } catch (KeeperException e) { + fail("failed to get data"); + } + + zk.close(); + Thread.sleep(100); + + // disable Client Sasl + System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, "false"); + + try { + zk = createClient(); + try { + zk.getData("/abc", null, null); + fail("Should not be able to read data when not authenticated"); + } catch (KeeperException.NoAuthException e) { + // success + } + zk.close(); + } finally { + // enable Client Sasl + System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, "true"); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthDesignatedServerTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthDesignatedServerTest.java new file mode 100644 index 0000000..bbe12c9 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthDesignatedServerTest.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.JaasConfiguration; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.ZooKeeperSaslServer; +import org.junit.Test; + +public class SaslAuthDesignatedServerTest extends ClientBase { + + public static int AUTHENTICATION_TIMEOUT = 30000; + + static { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(ZooKeeperSaslServer.LOGIN_CONTEXT_NAME_KEY, "MyZookeeperServer"); + + JaasConfiguration conf = new JaasConfiguration(); + + /* this 'Server' section has an incorrect password, but we're not configured + * to use it (we're configured by the above System.setProperty(...LOGIN_CONTEXT_NAME_KEY...) + * to use the 'MyZookeeperServer' section below, which has the correct password). + */ + conf.addSection("Server", "org.apache.zookeeper.server.auth.DigestLoginModule", "user_myuser", "wrongpassword"); + + conf.addSection("MyZookeeperServer", "org.apache.zookeeper.server.auth.DigestLoginModule", "user_myuser", "mypassword"); + + conf.addSection("Client", "org.apache.zookeeper.server.auth.DigestLoginModule", "username", "myuser", "password", "mypassword"); + + javax.security.auth.login.Configuration.setConfiguration(conf); + } + + private AtomicInteger authFailed = new AtomicInteger(0); + + private class MyWatcher extends CountdownWatcher { + + volatile CountDownLatch authCompleted; + + @Override + public synchronized void reset() { + authCompleted = new CountDownLatch(1); + super.reset(); + } + + @Override + public synchronized void process(WatchedEvent event) { + if (event.getState() == KeeperState.AuthFailed) { + authFailed.incrementAndGet(); + authCompleted.countDown(); + } else if (event.getState() == KeeperState.SaslAuthenticated) { + authCompleted.countDown(); + } else { + super.process(event); + } + } + + } + + @Test + public void testAuth() throws Exception { + MyWatcher watcher = new MyWatcher(); + ZooKeeper zk = createClient(watcher); + watcher.authCompleted.await(AUTHENTICATION_TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(authFailed.get(), 0); + + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + } catch (KeeperException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthFailDesignatedClientTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthFailDesignatedClientTest.java new file mode 100644 index 0000000..f0a85c2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthFailDesignatedClientTest.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.client.ZKClientConfig; +import org.junit.Test; + +public class SaslAuthFailDesignatedClientTest extends ClientBase { + + static { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, "MyZookeeperClient"); + + try { + File tmpDir = createTmpDir(); + File saslConfFile = new File(tmpDir, "jaas.conf"); + FileWriter fwriter = new FileWriter(saslConfFile); + + fwriter.write("Server {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_myuser=\"mypassword\";\n" + + "};\n" + + "Client {\n" + + /* this 'Client' section has the correct password, but we're not configured + to use it (we're configured by the above System.setProperty(...LOGIN_CONTEXT_NAME_KEY...) to + use the 'MyZookeeperClient' section, which has an incorrect password).*/ + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"myuser\"\n" + + " password=\"mypassword\";\n" + + "};" + + "MyZookeeperClient {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"myuser\"\n" + + " password=\"wrongpassword\";\n" + + "};" + + "\n"); + fwriter.close(); + System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + } catch (IOException e) { + // could not create tmp directory to hold JAAS conf file : test will fail now. + } + } + + @Test + public void testAuth() throws Exception { + // Cannot use createClient here because server may close session before + // JMXEnv.ensureAll is called which will fail the test case + CountdownWatcher watcher = new CountdownWatcher(); + TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + if (!watcher.clientConnected.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("Unable to connect to server"); + } + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Should have gotten exception."); + } catch (KeeperException e) { + // ok, exception as expected. + LOG.debug("Got exception as expected", e); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthFailTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthFailTest.java new file mode 100644 index 0000000..e5df31e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthFailTest.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Test; + +public class SaslAuthFailTest extends ClientBase { + + static { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty("zookeeper.allowSaslFailedClients", "true"); + + try { + File tmpDir = createTmpDir(); + File saslConfFile = new File(tmpDir, "jaas.conf"); + FileWriter fwriter = new FileWriter(saslConfFile); + + fwriter.write("Server {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_super=\"test\";\n" + + "};\n" + + "Client {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"super\"\n" + + " password=\"test1\";\n" + + + // NOTE: wrong password ('test' != 'test1') : this is to test SASL authentication failure. + "};" + + "\n"); + fwriter.close(); + System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + } catch (IOException e) { + // could not create tmp directory to hold JAAS conf file. + } + } + + private CountDownLatch authFailed = new CountDownLatch(1); + + private class MyWatcher extends CountdownWatcher { + + @Override + public synchronized void process(WatchedEvent event) { + if (event.getState() == KeeperState.AuthFailed) { + authFailed.countDown(); + } else { + super.process(event); + } + } + + } + + @Test + public void testAuthFail() { + try (ZooKeeper zk = createClient()) { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Should have gotten exception."); + } catch (Exception e) { + // ok, exception as expected. + LOG.debug("Got exception as expected", e); + } + } + + @Test + public void testBadSaslAuthNotifiesWatch() throws Exception { + try (ZooKeeper ignored = createClient(new MyWatcher(), hostPort)) { + // wait for authFailed event from client's EventThread. + authFailed.await(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthMissingClientConfigTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthMissingClientConfigTest.java new file mode 100644 index 0000000..51a6d03 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthMissingClientConfigTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.junit.Test; + +public class SaslAuthMissingClientConfigTest extends ClientBase { + + static { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + // This configuration section 'MyZookeeperClient', is missing from the JAAS configuration. + // As a result, SASL authentication should fail, which is tested by this test (testAuth()). + System.setProperty(ZKClientConfig.LOGIN_CONTEXT_NAME_KEY, "MyZookeeperClient"); + + try { + File tmpDir = createTmpDir(); + File saslConfFile = new File(tmpDir, "jaas.conf"); + FileWriter fwriter = new FileWriter(saslConfFile); + + fwriter.write("" + + "Server {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_myuser=\"mypassword\";\n" + + "};\n" + + "Client {\n" + + /* this 'Client' section has the correct password, but we're not configured + to use it - we're configured instead by the above + System.setProperty(...LOGIN_CONTEXT_NAME_KEY...) to + use the (nonexistent) 'MyZookeeperClient' section. */ + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"myuser\"\n" + + " password=\"mypassword\";\n" + + "};\n"); + fwriter.close(); + System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + } catch (IOException e) { + // could not create tmp directory to hold JAAS conf file : test will fail now. + } + } + + @Test + public void testAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Should have gotten exception."); + } catch (KeeperException e) { + // ok, exception as expected. + LOG.debug("Got exception as expected", e); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredFailNoSASLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredFailNoSASLTest.java new file mode 100644 index 0000000..e60fcc4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredFailNoSASLTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class SaslAuthRequiredFailNoSASLTest extends ClientBase { + + @Before + public void setup() { + System.setProperty(SaslTestUtil.requireSASLAuthProperty, "true"); + } + + @After + public void tearDown() throws Exception { + System.clearProperty(SaslTestUtil.requireSASLAuthProperty); + } + + @Test + public void testClientOpWithoutSASLConfigured() throws Exception { + ZooKeeper zk = null; + CountdownWatcher watcher = new CountdownWatcher(); + try { + zk = createClient(watcher); + zk.create("/foo", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Client is not configured with SASL authentication, so zk.create operation should fail."); + } catch (KeeperException e) { + assertTrue(e.code() == KeeperException.Code.SESSIONCLOSEDREQUIRESASLAUTH); + // Verify that "eventually" (within the bound of timeouts) + // this client closes the connection between itself and the server. + watcher.waitForDisconnected(SaslTestUtil.CLIENT_DISCONNECT_TIMEOUT); + } finally { + if (zk != null) { + zk.close(); + } + } + } + +} + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredFailWrongSASLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredFailWrongSASLTest.java new file mode 100644 index 0000000..1fed4d9 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredFailWrongSASLTest.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SaslAuthRequiredFailWrongSASLTest extends ClientBase { + + @BeforeClass + public static void setUpBeforeClass() { + System.setProperty(SaslTestUtil.requireSASLAuthProperty, "true"); + System.setProperty(SaslTestUtil.authProviderProperty, SaslTestUtil.authProvider); + System.setProperty(SaslTestUtil.jaasConfig, SaslTestUtil.createJAASConfigFile("jaas_wrong.conf", "test1")); + } + + @AfterClass + public static void tearDownAfterClass() { + System.clearProperty(SaslTestUtil.requireSASLAuthProperty); + System.clearProperty(SaslTestUtil.authProviderProperty); + System.clearProperty(SaslTestUtil.jaasConfig); + } + + @Test + public void testClientOpWithFailedSASLAuth() throws Exception { + ZooKeeper zk = null; + CountdownWatcher watcher = new CountdownWatcher(); + try { + zk = createClient(watcher); + zk.create("/bar", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + fail("Client with wrong SASL config should not pass SASL authentication."); + } catch (KeeperException e) { + assertTrue(e.code() == KeeperException.Code.AUTHFAILED); + // Verify that "eventually" this client closes the connection between itself and the server. + watcher.waitForDisconnected(SaslTestUtil.CLIENT_DISCONNECT_TIMEOUT); + } finally { + if (zk != null) { + zk.close(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredTest.java new file mode 100644 index 0000000..173a3cb --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslAuthRequiredTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SaslAuthRequiredTest extends ClientBase { + + @BeforeClass + public static void setUpBeforeClass() { + System.setProperty(SaslTestUtil.requireSASLAuthProperty, "true"); + System.setProperty(SaslTestUtil.authProviderProperty, SaslTestUtil.authProvider); + System.setProperty(SaslTestUtil.jaasConfig, SaslTestUtil.createJAASConfigFile("jaas.conf", "test")); + } + + @AfterClass + public static void tearDownAfterClass() { + System.clearProperty(SaslTestUtil.requireSASLAuthProperty); + System.clearProperty(SaslTestUtil.authProviderProperty); + System.clearProperty(SaslTestUtil.jaasConfig); + } + + @Test + public void testClientOpWithValidSASLAuth() throws Exception { + ZooKeeper zk = null; + CountdownWatcher watcher = new CountdownWatcher(); + try { + zk = createClient(watcher); + zk.create("/foobar", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + } catch (KeeperException e) { + fail("Client operation should succeed with valid SASL configuration."); + } finally { + if (zk != null) { + zk.close(); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslClientTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslClientTest.java new file mode 100644 index 0000000..adc6902 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslClientTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import java.util.Arrays; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.client.ZKClientConfig; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class SaslClientTest extends ZKTestCase { + + private String existingPropertyValue = null; + + @Before + public void setUp() { + existingPropertyValue = System.getProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY); + } + + @After + public void tearDown() { + // Restore the System property if it was set previously + if (existingPropertyValue != null) { + System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, existingPropertyValue); + } + } + + @Test + public void testSaslClientDisabled() { + System.clearProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY); + assertTrue("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); + + for (String value : Arrays.asList("true", "TRUE")) { + System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, value); + assertTrue("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); + } + + for (String value : Arrays.asList("false", "FALSE")) { + System.setProperty(ZKClientConfig.ENABLE_CLIENT_SASL_KEY, value); + assertFalse("SASL client disabled", new ZKClientConfig().isSaslClientEnabled()); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslDigestAuthOverSSLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslDigestAuthOverSSLTest.java new file mode 100644 index 0000000..09d9297 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslDigestAuthOverSSLTest.java @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.ZKClientConfig.LOGIN_CONTEXT_NAME_KEY; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import javax.security.auth.login.Configuration; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class SaslDigestAuthOverSSLTest extends ClientBase { + + private ClientX509Util clientX509Util; + private File saslConfFile; + + @Before + @Override + public void setUp() throws Exception { + initSaslConfig(); + clientX509Util = setUpSSLWithNoAuth(); + + String host = "localhost"; + int port = PortAssignment.unique(); + hostPort = host + ":" + port; + + serverFactory = ServerCnxnFactory.createFactory(); + serverFactory.configure(new InetSocketAddress(host, port), maxCnxns, -1, true); + + super.setUp(); + } + + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + clearSslSetting(clientX509Util); + clearSaslConfig(); + } + + + @Test + public void testAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Thread.sleep(1000); + } catch (KeeperException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + + public void initSaslConfig() { + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(LOGIN_CONTEXT_NAME_KEY, "ClientUsingDigest"); + try { + File tmpDir = createTmpDir(); + saslConfFile = new File(tmpDir, "jaas.conf"); + PrintWriter saslConf = new PrintWriter(new FileWriter(saslConfFile)); + saslConf.println("Server {"); + saslConf.println("org.apache.zookeeper.server.auth.DigestLoginModule required"); + saslConf.println("user_super=\"test\";"); + saslConf.println("};"); + saslConf.println("ClientUsingDigest {"); + saslConf.println("org.apache.zookeeper.server.auth.DigestLoginModule required"); + saslConf.println("username=\"super\""); + saslConf.println("password=\"test\";"); + saslConf.println("};"); + saslConf.close(); + System.setProperty(Environment.JAAS_CONF_KEY, saslConfFile.getAbsolutePath()); + } catch (IOException e) { + LOG.error("could not create tmp directory to hold JAAS conf file, test will fail...", e); + } + + // refresh the SASL configuration in this JVM (making sure that we use the latest config + // even if other tests already have been executed and initialized the SASL configs before) + Configuration.getConfiguration().refresh(); + } + + public void clearSaslConfig() { + FileUtils.deleteQuietly(saslConfFile); + System.clearProperty(Environment.JAAS_CONF_KEY); + System.clearProperty("zookeeper.authProvider.1"); + } + + public ClientX509Util setUpSSLWithNoAuth() { + String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data"); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); + System.setProperty("zookeeper.ssl.clientAuth", "none"); + System.setProperty("zookeeper.ssl.quorum.clientAuth", "none"); + + ClientX509Util x509Util = new ClientX509Util(); + System.setProperty(x509Util.getSslTruststoreLocationProperty(), testDataPath + "/ssl/testTrustStore.jks"); + System.setProperty(x509Util.getSslTruststorePasswdProperty(), "testpass"); + System.setProperty(x509Util.getSslKeystoreLocationProperty(), testDataPath + "/ssl/testKeyStore.jks"); + System.setProperty(x509Util.getSslKeystorePasswdProperty(), "testpass"); + + return x509Util; + } + + public void clearSslSetting(ClientX509Util clientX509Util) { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + System.clearProperty(ZKClientConfig.SECURE_CLIENT); + System.clearProperty(clientX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(clientX509Util.getSslTruststorePasswdProperty()); + System.clearProperty(clientX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(clientX509Util.getSslKeystorePasswdProperty()); + System.clearProperty("zookeeper.ssl.clientAuth"); + System.clearProperty("zookeeper.ssl.quorum.clientAuth"); + clientX509Util.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslKerberosAuthOverSSLTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslKerberosAuthOverSSLTest.java new file mode 100644 index 0000000..d466c8b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslKerberosAuthOverSSLTest.java @@ -0,0 +1,246 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.client.ZKClientConfig.ENABLE_CLIENT_SASL_KEY; +import static org.apache.zookeeper.client.ZKClientConfig.LOGIN_CONTEXT_NAME_KEY; +import static org.apache.zookeeper.client.ZKClientConfig.ZK_SASL_CLIENT_USERNAME; +import static org.apache.zookeeper.client.ZKClientConfig.ZOOKEEPER_SERVER_PRINCIPAL; +import static org.apache.zookeeper.client.ZKClientConfig.ZOOKEEPER_SERVER_REALM; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.InetSocketAddress; +import java.util.Properties; +import javax.security.auth.login.Configuration; +import org.apache.commons.io.FileUtils; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.Environment; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.common.ClientX509Util; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.quorum.auth.KerberosTestUtils; +import org.apache.zookeeper.server.quorum.auth.MiniKdc; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SaslKerberosAuthOverSSLTest extends ClientBase { + + private ClientX509Util clientX509Util; + private File keytabFileForKerberosPrincipals; + private File saslConfFile; + + private static MiniKdc kdc; + private static File kdcWorkDir; + private static Properties conf; + + + + @BeforeClass + public static void setupKdc() { + startMiniKdc(); + } + + @AfterClass + public static void tearDownKdc() { + stopMiniKdc(); + FileUtils.deleteQuietly(kdcWorkDir); + } + + + + @Before + @Override + public void setUp() throws Exception { + initSaslConfig(); + clientX509Util = setUpSSLWithNoAuth(); + + String host = "localhost"; + int port = PortAssignment.unique(); + hostPort = host + ":" + port; + + serverFactory = ServerCnxnFactory.createFactory(); + serverFactory.configure(new InetSocketAddress(host, port), maxCnxns, -1, true); + + super.setUp(); + } + + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + clearSslSetting(clientX509Util); + clearSaslConfig(); + } + + + @Test + public void testAuth() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/path1", null, Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT); + Thread.sleep(1000); + } catch (KeeperException e) { + fail("test failed :" + e); + } finally { + zk.close(); + } + } + + + public void initSaslConfig() throws Exception { + + // registering the server and client users in the KDC mini server + keytabFileForKerberosPrincipals = new File(KerberosTestUtils.getKeytabFile()); + String clientPrincipal = KerberosTestUtils.getClientPrincipal(); + String serverPrincipal = KerberosTestUtils.getServerPrincipal(); + clientPrincipal = clientPrincipal.substring(0, clientPrincipal.lastIndexOf("@")); + serverPrincipal = serverPrincipal.substring(0, serverPrincipal.lastIndexOf("@")); + kdc.createPrincipal(keytabFileForKerberosPrincipals, clientPrincipal, serverPrincipal); + + // client-side SASL config + System.setProperty(ZOOKEEPER_SERVER_PRINCIPAL, KerberosTestUtils.getServerPrincipal()); + System.setProperty(ENABLE_CLIENT_SASL_KEY, "true"); + System.setProperty(ZOOKEEPER_SERVER_REALM, KerberosTestUtils.getRealm()); + System.setProperty(LOGIN_CONTEXT_NAME_KEY, "ClientUsingKerberos"); + + // server side SASL config + System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + System.setProperty(SaslTestUtil.requireSASLAuthProperty, "true"); + + // generating the SASL config to use (contains sections both for the client and the server) + // note: we use "refreshKrb5Config=true" to refresh the kerberos config in the JVM, + // making sure that we use the latest config even if other tests already have been executed + // and initialized the kerberos client configs before) + try { + File tmpDir = createTmpDir(); + saslConfFile = new File(tmpDir, "jaas.conf"); + PrintWriter saslConf = new PrintWriter(new FileWriter(saslConfFile)); + saslConf.println("Server {"); + saslConf.println(" com.sun.security.auth.module.Krb5LoginModule required"); + saslConf.println(" storeKey=\"true\""); + saslConf.println(" useTicketCache=\"false\""); + saslConf.println(" useKeyTab=\"true\""); + saslConf.println(" doNotPrompt=\"true\""); + saslConf.println(" debug=\"true\""); + saslConf.println(" refreshKrb5Config=\"true\""); + saslConf.println(" keyTab=\"" + keytabFileForKerberosPrincipals.getAbsolutePath() + "\""); + saslConf.println(" principal=\"" + KerberosTestUtils.getServerPrincipal() + "\";"); + saslConf.println("};"); + saslConf.println("ClientUsingKerberos {"); + saslConf.println(" com.sun.security.auth.module.Krb5LoginModule required"); + saslConf.println(" storeKey=\"false\""); + saslConf.println(" useTicketCache=\"false\""); + saslConf.println(" useKeyTab=\"true\""); + saslConf.println(" doNotPrompt=\"true\""); + saslConf.println(" debug=\"true\""); + saslConf.println(" refreshKrb5Config=\"true\""); + saslConf.println(" keyTab=\"" + keytabFileForKerberosPrincipals.getAbsolutePath() + "\""); + saslConf.println(" principal=\"" + KerberosTestUtils.getClientPrincipal() + "\";"); + saslConf.println("};"); + saslConf.close(); + System.setProperty(Environment.JAAS_CONF_KEY, saslConfFile.getAbsolutePath()); + + } catch (IOException e) { + LOG.error("could not create tmp directory to hold JAAS conf file, test will fail...", e); + } + + // refresh the SASL configuration in this JVM (making sure that we use the latest config + // even if other tests already have been executed and initialized the SASL configs before) + Configuration.getConfiguration().refresh(); + } + + public void clearSaslConfig() { + FileUtils.deleteQuietly(keytabFileForKerberosPrincipals); + FileUtils.deleteQuietly(saslConfFile); + + System.clearProperty(Environment.JAAS_CONF_KEY); + System.clearProperty(ZK_SASL_CLIENT_USERNAME); + System.clearProperty(ENABLE_CLIENT_SASL_KEY); + System.clearProperty(LOGIN_CONTEXT_NAME_KEY); + System.clearProperty("zookeeper.authProvider.1"); + + System.clearProperty(SaslTestUtil.requireSASLAuthProperty); + System.clearProperty(ZOOKEEPER_SERVER_PRINCIPAL); + } + + public ClientX509Util setUpSSLWithNoAuth() { + String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data"); + System.setProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY, "org.apache.zookeeper.server.NettyServerCnxnFactory"); + System.setProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET, "org.apache.zookeeper.ClientCnxnSocketNetty"); + System.setProperty(ZKClientConfig.SECURE_CLIENT, "true"); + System.setProperty("zookeeper.ssl.clientAuth", "none"); + System.setProperty("zookeeper.ssl.quorum.clientAuth", "none"); + + ClientX509Util x509Util = new ClientX509Util(); + System.setProperty(x509Util.getSslTruststoreLocationProperty(), testDataPath + "/ssl/testTrustStore.jks"); + System.setProperty(x509Util.getSslTruststorePasswdProperty(), "testpass"); + System.setProperty(x509Util.getSslKeystoreLocationProperty(), testDataPath + "/ssl/testKeyStore.jks"); + System.setProperty(x509Util.getSslKeystorePasswdProperty(), "testpass"); + + return x509Util; + } + + public void clearSslSetting(ClientX509Util clientX509Util) { + System.clearProperty(ServerCnxnFactory.ZOOKEEPER_SERVER_CNXN_FACTORY); + System.clearProperty(ZKClientConfig.ZOOKEEPER_CLIENT_CNXN_SOCKET); + System.clearProperty(ZKClientConfig.SECURE_CLIENT); + System.clearProperty(clientX509Util.getSslTruststoreLocationProperty()); + System.clearProperty(clientX509Util.getSslTruststorePasswdProperty()); + System.clearProperty(clientX509Util.getSslKeystoreLocationProperty()); + System.clearProperty(clientX509Util.getSslKeystorePasswdProperty()); + System.clearProperty("javax.net.debug"); + System.clearProperty("zookeeper.ssl.clientAuth"); + System.clearProperty("zookeeper.ssl.quorum.clientAuth"); + clientX509Util.close(); + } + + + + public static void startMiniKdc() { + try { + kdcWorkDir = createEmptyTestDir(); + conf = MiniKdc.createConf(); + conf.setProperty("debug", "true"); + + kdc = new MiniKdc(conf, kdcWorkDir); + kdc.start(); + } catch (Exception e) { + throw new RuntimeException("failed to start MiniKdc", e); + } + + } + + public static void stopMiniKdc() { + if (kdc != null) { + kdc.stop(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslSuperUserTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslSuperUserTest.java new file mode 100644 index 0000000..426eeb1 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslSuperUserTest.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Perms; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Id; +import org.apache.zookeeper.server.auth.DigestAuthenticationProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class SaslSuperUserTest extends ClientBase { + + private static Id otherSaslUser = new Id("sasl", "joe"); + private static Id otherDigestUser; + private static String oldAuthProvider; + private static String oldLoginConfig; + private static String oldSuperUser; + + @BeforeClass + public static void setupStatic() throws Exception { + oldAuthProvider = System.setProperty("zookeeper.authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); + + File tmpDir = createTmpDir(); + File saslConfFile = new File(tmpDir, "jaas.conf"); + FileWriter fwriter = new FileWriter(saslConfFile); + + fwriter.write("" + + "Server {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " user_super_duper=\"test\";\n" + + "};\n" + + "Client {\n" + + " org.apache.zookeeper.server.auth.DigestLoginModule required\n" + + " username=\"super_duper\"\n" + + " password=\"test\";\n" + + "};" + + "\n"); + fwriter.close(); + oldLoginConfig = System.setProperty("java.security.auth.login.config", saslConfFile.getAbsolutePath()); + oldSuperUser = System.setProperty("zookeeper.superUser", "super_duper"); + otherDigestUser = new Id("digest", DigestAuthenticationProvider.generateDigest("jack:jack")); + } + + @AfterClass + public static void cleanupStatic() { + if (oldAuthProvider != null) { + System.setProperty("zookeeper.authProvider.1", oldAuthProvider); + } else { + System.clearProperty("zookeeper.authProvider.1"); + } + oldAuthProvider = null; + + if (oldLoginConfig != null) { + System.setProperty("java.security.auth.login.config", oldLoginConfig); + } else { + System.clearProperty("java.security.auth.login.config"); + } + oldLoginConfig = null; + + if (oldSuperUser != null) { + System.setProperty("zookeeper.superUser", oldSuperUser); + } else { + System.clearProperty("zookeeper.superUser"); + } + oldSuperUser = null; + } + + private AtomicInteger authFailed = new AtomicInteger(0); + + @Override + protected TestableZooKeeper createClient(String hp) throws IOException, InterruptedException { + MyWatcher watcher = new MyWatcher(); + return createClient(watcher, hp); + } + + private class MyWatcher extends CountdownWatcher { + + @Override + public synchronized void process(WatchedEvent event) { + if (event.getState() == KeeperState.AuthFailed) { + authFailed.incrementAndGet(); + } else { + super.process(event); + } + } + + } + + @Test + public void testSuperIsSuper() throws Exception { + ZooKeeper zk = createClient(); + try { + zk.create("/digest_read", null, Arrays.asList(new ACL(Perms.READ, otherDigestUser)), CreateMode.PERSISTENT); + zk.create("/digest_read/sub", null, Arrays.asList(new ACL(Perms.READ, otherDigestUser)), CreateMode.PERSISTENT); + zk.create("/sasl_read", null, Arrays.asList(new ACL(Perms.READ, otherSaslUser)), CreateMode.PERSISTENT); + zk.create("/sasl_read/sub", null, Arrays.asList(new ACL(Perms.READ, otherSaslUser)), CreateMode.PERSISTENT); + zk.delete("/digest_read/sub", -1); + zk.delete("/digest_read", -1); + zk.delete("/sasl_read/sub", -1); + zk.delete("/sasl_read", -1); + //If the test failes it will most likely fail with a NoAuth exception before it ever gets to this assertion + assertEquals(authFailed.get(), 0); + } finally { + zk.close(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslTestUtil.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslTestUtil.java new file mode 100644 index 0000000..f787fb2 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SaslTestUtil.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +public class SaslTestUtil extends ClientBase { + + // The maximum time (in milliseconds) a client should take to observe + // a disconnect event of the same client from server. + static Integer CLIENT_DISCONNECT_TIMEOUT = 3000; + static String requireSASLAuthProperty = "zookeeper.sessionRequireClientSASLAuth"; + static String authProviderProperty = "zookeeper.authProvider.1"; + static String authProvider = "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"; + static String digestLoginModule = "org.apache.zookeeper.server.auth.DigestLoginModule"; + static String jaasConfig = "java.security.auth.login.config"; + + static String createJAASConfigFile(String fileName, String password) { + String ret = null; + try { + File tmpDir = createTmpDir(); + File jaasFile = new File(tmpDir, fileName); + FileWriter fwriter = new FileWriter(jaasFile); + fwriter.write("" + + "Server {\n" + + " " + + digestLoginModule + + " required\n" + + " user_super=\"test\";\n" + + "};\n" + + "Client {\n" + + " " + + digestLoginModule + + " required\n" + + " username=\"super\"\n" + + " password=\"" + + password + + "\";\n" + + "};" + + "\n"); + fwriter.close(); + ret = jaasFile.getAbsolutePath(); + } catch (IOException e) { + fail("Unable to create JaaS configuration file!"); + } + + return ret; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ServerCnxnTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ServerCnxnTest.java new file mode 100644 index 0000000..35db43d --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ServerCnxnTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.Socket; +import org.apache.zookeeper.server.NIOServerCnxnFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ServerCnxnTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(ServerCnxnTest.class); + + private static int cnxnTimeout = 1000; + + @Before + public void setUp() throws Exception { + System.setProperty(NIOServerCnxnFactory.ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT, Integer.toString(cnxnTimeout)); + super.setUp(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + System.clearProperty(NIOServerCnxnFactory.ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT); + } + + @Test + public void testServerCnxnExpiry() throws Exception { + verify("ruok", "imok"); + + // Expiry time is (now/cnxnTimeout + 1)*cnxnTimeout + // Range is (now + cnxnTimeout) to (now + 2*cnxnTimeout) + // Add 1s buffer to be safe. + String resp = sendRequest("ruok", 2 * cnxnTimeout + 1000); + assertEquals("Connection should have closed", "", resp); + } + + private void verify(String cmd, String expected) throws IOException { + String resp = sendRequest(cmd, 0); + LOG.info("cmd {} expected {} got {}", cmd, expected, resp); + assertTrue(resp.contains(expected)); + } + + private String sendRequest(String cmd, int delay) throws IOException { + HostPort hpobj = ClientBase.parseHostPortList(hostPort).get(0); + return send4LetterWord(hpobj.host, hpobj.port, cmd, delay); + } + + private static String send4LetterWord( + String host, int port, String cmd, int delay) throws IOException { + LOG.info("connecting to {} {}", host, port); + Socket sock = new Socket(host, port); + BufferedReader reader = null; + try { + try { + LOG.info("Sleeping for {}ms", delay); + Thread.sleep(delay); + } catch (InterruptedException e) { + // ignore + } + + OutputStream outstream = sock.getOutputStream(); + outstream.write(cmd.getBytes()); + outstream.flush(); + // this replicates NC - close the output stream before reading + sock.shutdownOutput(); + + reader = new BufferedReader(new InputStreamReader(sock.getInputStream())); + StringBuilder sb = readLine(reader); + return sb.toString(); + } finally { + sock.close(); + if (reader != null) { + reader.close(); + } + } + } + + private static StringBuilder readLine(BufferedReader reader) { + StringBuilder sb = new StringBuilder(); + String line; + try { + while ((line = reader.readLine()) != null) { + sb.append(line + "\n"); + } + } catch (IOException ioe) { + // During connection expiry the server will close the connection. + // After the socket is closed, when the client tries to read a + // line of text it will throw java.net.SocketException. + // @see jira issue ZOOKEEPER-1862 + LOG.info("Connnection is expired", ioe); + } + return sb; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionInvalidationTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionInvalidationTest.java new file mode 100644 index 0000000..0177e86 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionInvalidationTest.java @@ -0,0 +1,104 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import org.apache.jute.BinaryOutputArchive; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooDefs.OpCode; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.proto.ConnectRequest; +import org.apache.zookeeper.proto.CreateRequest; +import org.apache.zookeeper.proto.RequestHeader; +import org.junit.Test; + +public class SessionInvalidationTest extends ClientBase { + + /** + * Test solution for ZOOKEEPER-1208. Verify that operations are not + * accepted after a close session. + * + * We're using our own marshalling here in order to force an operation + * after the session is closed (ZooKeeper.class will not allow this). Also + * by filling the pipe with operations it increases the likelyhood that + * the server will process the create before FinalRequestProcessor + * removes the session from the tracker. + */ + @Test + public void testCreateAfterCloseShouldFail() throws Exception { + for (int i = 0; i < 10; i++) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + BinaryOutputArchive boa = BinaryOutputArchive.getArchive(baos); + + // open a connection + boa.writeInt(44, "len"); + ConnectRequest conReq = new ConnectRequest(0, 0, 30000, 0, new byte[16]); + conReq.serialize(boa, "connect"); + + // close connection + boa.writeInt(8, "len"); + RequestHeader h = new RequestHeader(1, ZooDefs.OpCode.closeSession); + h.serialize(boa, "header"); + + // create ephemeral znode + boa.writeInt(52, "len"); // We'll fill this in later + RequestHeader header = new RequestHeader(2, OpCode.create); + header.serialize(boa, "header"); + CreateRequest createReq = new CreateRequest("/foo" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, 1); + createReq.serialize(boa, "request"); + baos.close(); + + System.out.println("Length:" + baos.toByteArray().length); + + String[] hp = hostPort.split(":"); + Socket sock = new Socket(hp[0], Integer.parseInt(hp[1])); + InputStream resultStream = null; + try { + OutputStream outstream = sock.getOutputStream(); + byte[] data = baos.toByteArray(); + outstream.write(data); + outstream.flush(); + + resultStream = sock.getInputStream(); + byte[] b = new byte[10000]; + int len; + while ((len = resultStream.read(b)) >= 0) { + // got results + System.out.println("gotlen:" + len); + } + } finally { + if (resultStream != null) { + resultStream.close(); + } + sock.close(); + } + } + + ZooKeeper zk = createClient(); + assertEquals(1, zk.getChildren("/", false).size()); + + zk.close(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTest.java new file mode 100644 index 0000000..9a5376b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTest.java @@ -0,0 +1,400 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SessionTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(SessionTest.class); + + private static final String HOSTPORT = "127.0.0.1:" + PortAssignment.unique(); + + private ServerCnxnFactory serverFactory; + private ZooKeeperServer zs; + + private CountDownLatch startSignal; + + File tmpDir; + + private final int TICK_TIME = 3000; + + @Before + public void setUp() throws Exception { + if (tmpDir == null) { + tmpDir = ClientBase.createTmpDir(); + } + + ClientBase.setupTestEnv(); + zs = new ZooKeeperServer(tmpDir, tmpDir, TICK_TIME); + + final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]); + serverFactory = ServerCnxnFactory.createFactory(PORT, -1); + serverFactory.startup(zs); + + assertTrue("waiting for server up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + } + + @After + public void tearDown() throws Exception { + serverFactory.shutdown(); + zs.shutdown(); + assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + } + + private static class CountdownWatcher implements Watcher { + + volatile CountDownLatch clientConnected = new CountDownLatch(1); + + public void process(WatchedEvent event) { + if (event.getState() == KeeperState.SyncConnected) { + clientConnected.countDown(); + } + } + + } + + private DisconnectableZooKeeper createClient() throws IOException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createClient(CONNECTION_TIMEOUT, watcher); + } + + private DisconnectableZooKeeper createClient(int timeout) throws IOException, InterruptedException { + CountdownWatcher watcher = new CountdownWatcher(); + return createClient(timeout, watcher); + } + + private DisconnectableZooKeeper createClient(int timeout, CountdownWatcher watcher) throws IOException, InterruptedException { + DisconnectableZooKeeper zk = new DisconnectableZooKeeper(HOSTPORT, timeout, watcher); + if (!watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS)) { + fail("Unable to connect to server"); + } + + return zk; + } + + // TODO this test is failing due to client close race condition fixing in separate patch for ZOOKEEPER-63 + // /** + // * this test checks to see if the sessionid that was created for the + // * first zookeeper client can be reused for the second one immidiately + // * after the first client closes and the new client resues them. + // * @throws IOException + // * @throws InterruptedException + // * @throws KeeperException + // */ + // public void testSessionReuse() throws IOException, InterruptedException { + // ZooKeeper zk = createClient(); + // + // long sessionId = zk.getSessionId(); + // byte[] passwd = zk.getSessionPasswd(); + // zk.close(); + // + // zk.close(); + // + // LOG.info("Closed first session"); + // + // startSignal = new CountDownLatch(1); + // zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this, + // sessionId, passwd); + // startSignal.await(); + // + // LOG.info("Opened reuse"); + // + // assertEquals(sessionId, zk.getSessionId()); + // + // zk.close(); + // } + + private class MyWatcher implements Watcher { + + private String name; + public MyWatcher(String name) { + this.name = name; + } + public void process(WatchedEvent event) { + LOG.info("{} event:{} {} {}", name, event.getState(), event.getType(), event.getPath()); + if (event.getState() == KeeperState.SyncConnected && startSignal != null && startSignal.getCount() > 0) { + startSignal.countDown(); + } + } + + } + + /** + * This test verifies that when the session id is reused, and the original + * client is disconnected, but not session closed, that the server + * will remove ephemeral nodes created by the original session. + */ + @Test + public void testSession() throws IOException, InterruptedException, KeeperException { + DisconnectableZooKeeper zk = createClient(); + zk.create("/e", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + LOG.info("zk with session id 0x{} was destroyed!", Long.toHexString(zk.getSessionId())); + + // disconnect the client by killing the socket, not sending the + // session disconnect to the server as usual. This allows the test + // to verify disconnect handling + zk.disconnect(); + + Stat stat = new Stat(); + startSignal = new CountDownLatch(1); + zk = new DisconnectableZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, new MyWatcher("testSession"), zk.getSessionId(), zk.getSessionPasswd()); + startSignal.await(); + + LOG.info("zk with session id 0x{} was created!", Long.toHexString(zk.getSessionId())); + zk.getData("/e", false, stat); + LOG.info("After get data /e"); + zk.close(); + + zk = createClient(); + assertEquals(null, zk.exists("/e", false)); + LOG.info("before close zk with session id 0x{}!", Long.toHexString(zk.getSessionId())); + zk.close(); + try { + zk.getData("/e", false, stat); + fail("Should have received a SessionExpiredException"); + } catch (KeeperException.SessionExpiredException e) { + } + + AsyncCallback.DataCallback cb = new AsyncCallback.DataCallback() { + String status = "not done"; + public void processResult(int rc, String p, Object c, byte[] b, Stat s) { + synchronized (this) { + status = KeeperException.Code.get(rc).toString(); + this.notify(); + } + } + public String toString() { + return status; + } + }; + zk.getData("/e", false, cb, null); + synchronized (cb) { + if (cb.toString().equals("not done")) { + cb.wait(1000); + } + } + assertEquals(KeeperException.Code.SESSIONEXPIRED.toString(), cb.toString()); + } + + /** + * Make sure that we cannot have two connections with the same + * session id. + * + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + */ + @Test + public void testSessionMove() throws Exception { + String[] hostPorts = HOSTPORT.split(","); + DisconnectableZooKeeper zk = new DisconnectableZooKeeper(hostPorts[0], CONNECTION_TIMEOUT, new MyWatcher("0")); + zk.create("/sessionMoveTest", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + // we want to loop through the list twice + for (int i = 0; i < hostPorts.length * 2; i++) { + zk.dontReconnect(); + // This should stomp the zk handle + DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(hostPorts[(i + 1) + % hostPorts.length], CONNECTION_TIMEOUT, new MyWatcher(Integer.toString( + i + + 1)), zk.getSessionId(), zk.getSessionPasswd()); + final int[] result = new int[1]; + result[0] = Integer.MAX_VALUE; + zknew.sync("/", new AsyncCallback.VoidCallback() { + public void processResult(int rc, String path, Object ctx) { + synchronized (result) { + result[0] = rc; + result.notify(); + } + } + }, null); + synchronized (result) { + if (result[0] == Integer.MAX_VALUE) { + result.wait(5000); + } + } + LOG.info("{} Sync returned {}", hostPorts[(i + 1) % hostPorts.length], result[0]); + assertTrue(result[0] == KeeperException.Code.OK.intValue()); + zknew.setData("/", new byte[1], -1); + try { + zk.setData("/", new byte[1], -1); + fail("Should have lost the connection"); + } catch (KeeperException.ConnectionLossException e) { + LOG.info("Got connection loss exception as expected"); + } + //zk.close(); + zk = zknew; + } + zk.close(); + } + /** + * This test makes sure that duplicate state changes are not communicated + * to the client watcher. For example we should not notify state as + * "disconnected" if the watch has already been disconnected. In general + * we don't consider a dup state notification if the event type is + * not "None" (ie non-None communicates an event). + */ + @Test + public void testSessionStateNoDupStateReporting() throws IOException, InterruptedException, KeeperException { + final int TIMEOUT = 3000; + DupWatcher watcher = new DupWatcher(); + ZooKeeper zk = createClient(TIMEOUT, watcher); + + // shutdown the server + serverFactory.shutdown(); + + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + // ignore + } + + // verify that the size is just 2 - ie connect then disconnect + // if the client attempts reconnect and we are not handling current + // state correctly (ie eventing on duplicate disconnects) then we'll + // see a disconnect for each failed connection attempt + assertEquals(2, watcher.states.size()); + + zk.close(); + } + + /** + * Verify access to the negotiated session timeout. + */ + @Test + public void testSessionTimeoutAccess() throws Exception { + // validate typical case - requested == negotiated + DisconnectableZooKeeper zk = createClient(TICK_TIME * 4); + assertEquals(TICK_TIME * 4, zk.getSessionTimeout()); + // make sure tostring works in both cases + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + + // validate lower limit + zk = createClient(TICK_TIME); + assertEquals(TICK_TIME * 2, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + + // validate upper limit + zk = createClient(TICK_TIME * 30); + assertEquals(TICK_TIME * 20, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + } + + private class DupWatcher extends CountdownWatcher { + + public List states = new LinkedList(); + public void process(WatchedEvent event) { + super.process(event); + if (event.getType() == EventType.None) { + states.add(event); + } + } + + } + + @Test + public void testMinMaxSessionTimeout() throws Exception { + // override the defaults + final int MINSESS = 20000; + final int MAXSESS = 240000; + { + ZooKeeperServer zs = serverFactory.getZooKeeperServer(); + zs.setMinSessionTimeout(MINSESS); + zs.setMaxSessionTimeout(MAXSESS); + } + + // validate typical case - requested == negotiated + int timeout = 120000; + DisconnectableZooKeeper zk = createClient(timeout); + assertEquals(timeout, zk.getSessionTimeout()); + // make sure tostring works in both cases + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + + // validate lower limit + zk = createClient(MINSESS / 2); + assertEquals(MINSESS, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + + // validate upper limit + zk = createClient(MAXSESS * 2); + assertEquals(MAXSESS, zk.getSessionTimeout()); + LOG.info(zk.toString()); + zk.close(); + LOG.info(zk.toString()); + } + + @Test + public void testMaximumCnxnPerIP() throws Exception { + final int maxClientCnxnsPerIP = 3; + serverFactory.setMaxClientCnxnsPerHost(maxClientCnxnsPerIP); + ZooKeeper[] clients = new ZooKeeper[maxClientCnxnsPerIP + 1]; + for (int i = 0; i < clients.length; i++) { + CountdownWatcher watcher = new CountdownWatcher(); + // wait for 3s + int timeout = 3000; + clients[i] = new DisconnectableZooKeeper(HOSTPORT, timeout, watcher); + boolean result = watcher.clientConnected.await(timeout, TimeUnit.MILLISECONDS); + if (i >= maxClientCnxnsPerIP) { + assertFalse(result); + } else { + assertTrue(result); + } + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTimeoutTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTimeoutTest.java new file mode 100644 index 0000000..9bf23ca --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTimeoutTest.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SessionTimeoutTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(SessionTimeoutTest.class); + + private TestableZooKeeper zk; + + @Before + public void setUp() throws Exception { + super.setUp(); + zk = createClient(); + } + + @Test + public void testSessionExpiration() throws InterruptedException, KeeperException { + final CountDownLatch expirationLatch = new CountDownLatch(1); + Watcher watcher = event -> { + if (event.getState() == Watcher.Event.KeeperState.Expired) { + expirationLatch.countDown(); + } + }; + zk.exists("/foo", watcher); + + zk.getTestable().injectSessionExpiration(); + assertTrue(expirationLatch.await(5, TimeUnit.SECONDS)); + + boolean gotException = false; + try { + zk.exists("/foo", false); + fail("Should have thrown a SessionExpiredException"); + } catch (KeeperException.SessionExpiredException e) { + // correct + gotException = true; + } + assertTrue(gotException); + } + + @Test + public void testQueueEvent() throws InterruptedException, KeeperException { + final CountDownLatch eventLatch = new CountDownLatch(1); + Watcher watcher = event -> { + if (event.getType() == Watcher.Event.EventType.NodeDataChanged) { + if (event.getPath().equals("/foo/bar")) { + eventLatch.countDown(); + } + } + }; + zk.exists("/foo/bar", watcher); + + WatchedEvent event = new WatchedEvent(Watcher.Event.EventType.NodeDataChanged, Watcher.Event.KeeperState.SyncConnected, "/foo/bar"); + zk.getTestable().queueEvent(event); + assertTrue(eventLatch.await(5, TimeUnit.SECONDS)); + } + + /** + * Make sure ephemerals get cleaned up when session disconnects. + */ + @Test + public void testSessionDisconnect() throws KeeperException, InterruptedException, IOException { + zk.create("/sdisconnect", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertNotNull("Ephemeral node has not been created", zk.exists("/sdisconnect", null)); + + zk.close(); + + zk = createClient(); + assertNull("Ephemeral node shouldn't exist after client disconnect", zk.exists("/sdisconnect", null)); + } + + /** + * Make sure ephemerals are kept when session restores. + */ + @Test + public void testSessionRestore() throws KeeperException, InterruptedException, IOException { + zk.create("/srestore", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertNotNull("Ephemeral node has not been created", zk.exists("/srestore", null)); + + zk.disconnect(); + zk.close(); + + zk = createClient(); + assertNotNull("Ephemeral node should be present when session is restored", zk.exists("/srestore", null)); + } + + /** + * Make sure ephemerals are kept when server restarts. + */ + @Test + public void testSessionSurviveServerRestart() throws Exception { + zk.create("/sdeath", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + assertNotNull("Ephemeral node has not been created", zk.exists("/sdeath", null)); + + zk.disconnect(); + stopServer(); + startServer(); + zk = createClient(); + + assertNotNull("Ephemeral node should be present when server restarted", zk.exists("/sdeath", null)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTrackerCheckTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTrackerCheckTest.java new file mode 100644 index 0000000..a0d5c91 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionTrackerCheckTest.java @@ -0,0 +1,221 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.zookeeper.KeeperException.SessionExpiredException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.SessionTracker.Session; +import org.apache.zookeeper.server.SessionTracker.SessionExpirer; +import org.apache.zookeeper.server.ZooKeeperServerListener; +import org.apache.zookeeper.server.quorum.LeaderSessionTracker; +import org.apache.zookeeper.server.quorum.LearnerSessionTracker; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Validate various type of sessions against leader session tracker and learner + * session tracker + */ +public class SessionTrackerCheckTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(SessionTrackerCheckTest.class); + public static final int TICK_TIME = 1000; + public static final int CONNECTION_TIMEOUT = TICK_TIME * 10; + + private ConcurrentHashMap sessionsWithTimeouts = new ConcurrentHashMap(); + + private class Expirer implements SessionExpirer { + + long sid; + + public Expirer(long sid) { + this.sid = sid; + } + + public void expire(Session session) { + } + + public long getServerId() { + return sid; + } + + } + + @Before + public void setUp() throws Exception { + sessionsWithTimeouts.clear(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testLearnerSessionTracker() throws Exception { + Expirer expirer = new Expirer(1); + // With local session on + LearnerSessionTracker tracker = new LearnerSessionTracker(expirer, sessionsWithTimeouts, TICK_TIME, expirer.sid, true, testZKSListener()); + + // Unknown session + long sessionId = 0xb100ded; + try { + tracker.checkSession(sessionId, null); + fail("Unknown session should have failed"); + } catch (SessionExpiredException e) { + // Get expected exception + } + + // Global session + sessionsWithTimeouts.put(sessionId, CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Global session should not fail"); + } + + // Local session + sessionId = tracker.createSession(CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Local session should not fail"); + } + + // During session upgrade + sessionsWithTimeouts.put(sessionId, CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Session during upgrade should not fail"); + } + + // With local session off + tracker = new LearnerSessionTracker(expirer, sessionsWithTimeouts, TICK_TIME, expirer.sid, false, testZKSListener()); + + // Should be noop + sessionId = 0xdeadbeef; + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Should not get any exception"); + } + + } + + @Test + public void testLeaderSessionTracker() throws Exception { + Expirer expirer = new Expirer(2); + // With local session on + LeaderSessionTracker tracker = new LeaderSessionTracker(expirer, sessionsWithTimeouts, TICK_TIME, expirer.sid, true, testZKSListener()); + + // Local session from other server + long sessionId = ((expirer.sid + 1) << 56) + 1; + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("local session from other server should not fail"); + } + + // Track global session + tracker.trackSession(sessionId, CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Global session should not fail"); + } + try { + tracker.checkGlobalSession(sessionId, null); + } catch (Exception e) { + fail("Global session should not fail " + e); + } + + // Local session from the leader + sessionId = tracker.createSession(CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Local session on the leader should not fail"); + } + + // During session upgrade + tracker.trackSession(sessionId, CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Session during upgrade should not fail"); + } + try { + tracker.checkGlobalSession(sessionId, null); + } catch (Exception e) { + fail("Global session should not fail " + e); + } + + // With local session off + tracker = new LeaderSessionTracker(expirer, sessionsWithTimeouts, TICK_TIME, expirer.sid, false, testZKSListener()); + + // Global session + sessionId = 0xdeadbeef; + tracker.trackSession(sessionId, CONNECTION_TIMEOUT); + try { + tracker.checkSession(sessionId, null); + } catch (Exception e) { + fail("Global session should not fail"); + } + try { + tracker.checkGlobalSession(sessionId, null); + } catch (Exception e) { + fail("Global session should not fail"); + } + + // Local session from other server + sessionId = ((expirer.sid + 1) << 56) + 2; + try { + tracker.checkSession(sessionId, null); + fail("local session from other server should fail"); + } catch (SessionExpiredException e) { + // Got expected exception + } + + // Local session from the leader + sessionId = ((expirer.sid) << 56) + 2; + try { + tracker.checkSession(sessionId, null); + fail("local session from the leader should fail"); + } catch (SessionExpiredException e) { + // Got expected exception + } + + } + + ZooKeeperServerListener testZKSListener() { + return new ZooKeeperServerListener() { + + @Override + public void notifyStopping(String errMsg, int exitCode) { + + } + }; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionUpgradeTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionUpgradeTest.java new file mode 100644 index 0000000..f10f6c8 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SessionUpgradeTest.java @@ -0,0 +1,211 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Tests that session upgrade works from local to global sessions. + * Expected behavior is that if global-only sessions are unset, + * and no upgrade interval is specified, then sessions will be + * created locally to the host. They will be upgraded to global + * sessions iff an operation is done on that session which requires + * persistence, i.e. creating an ephemeral node. + */ +public class SessionUpgradeTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(SessionUpgradeTest.class); + public static final int CONNECTION_TIMEOUT = ClientBase.CONNECTION_TIMEOUT; + + private final QuorumBase qb = new QuorumBase(); + + @Before + public void setUp() throws Exception { + LOG.info("STARTING quorum {}", getClass().getName()); + qb.localSessionsEnabled = true; + qb.localSessionsUpgradingEnabled = true; + qb.setUp(); + ClientBase.waitForServerUp(qb.hostPort, 10000); + } + + @After + public void tearDown() throws Exception { + LOG.info("STOPPING quorum {}", getClass().getName()); + qb.tearDown(); + } + + @Test + public void testLocalSessionsWithoutEphemeralOnFollower() throws Exception { + testLocalSessionsWithoutEphemeral(false); + } + + @Test + public void testLocalSessionsWithoutEphemeralOnLeader() throws Exception { + testLocalSessionsWithoutEphemeral(true); + } + + private void testLocalSessionsWithoutEphemeral(boolean testLeader) throws Exception { + String nodePrefix = "/testLocalSessions-" + (testLeader ? "leaderTest-" : "followerTest-"); + int leaderIdx = qb.getLeaderIndex(); + assertFalse("No leader in quorum?", leaderIdx == -1); + int followerIdx = (leaderIdx + 1) % 5; + int otherFollowerIdx = (leaderIdx + 2) % 5; + int testPeerIdx = testLeader ? leaderIdx : followerIdx; + String[] hostPorts = qb.hostPort.split(","); + CountdownWatcher watcher = new CountdownWatcher(); + DisconnectableZooKeeper zk = new DisconnectableZooKeeper(hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + // Try creating some data. + for (int i = 0; i < 5; i++) { + zk.create(nodePrefix + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + + long localSessionId = zk.getSessionId(); + byte[] localSessionPwd = zk.getSessionPasswd().clone(); + + // Try connecting with the same session id on a different + // server. This should fail since it is a local sesion. + try { + watcher.reset(); + DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher, localSessionId, localSessionPwd); + + zknew.create(nodePrefix + "5", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("Connection on the same session ID should fail."); + } catch (KeeperException.SessionExpiredException e) { + } catch (KeeperException.ConnectionLossException e) { + } + + // If we're testing a follower, also check the session id on the + // leader. This should also fail + if (!testLeader) { + try { + watcher.reset(); + DisconnectableZooKeeper zknew = new DisconnectableZooKeeper(hostPorts[leaderIdx], CONNECTION_TIMEOUT, watcher, localSessionId, localSessionPwd); + + zknew.create(nodePrefix + "5", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("Connection on the same session ID should fail."); + } catch (KeeperException.SessionExpiredException e) { + } catch (KeeperException.ConnectionLossException e) { + } + } + + // However, we should be able to disconnect and reconnect to the same + // server with the same session id (as long as we do it quickly + // before expiration). + zk.disconnect(); + + watcher.reset(); + zk = new DisconnectableZooKeeper(hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher, localSessionId, localSessionPwd); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + zk.create(nodePrefix + "6", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + // If we explicitly close the session, then the session id should no + // longer be valid. + zk.close(); + try { + watcher.reset(); + zk = new DisconnectableZooKeeper(hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher, localSessionId, localSessionPwd); + + zk.create(nodePrefix + "7", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + fail("Reconnecting to a closed session ID should fail."); + } catch (KeeperException.SessionExpiredException e) { + } + } + + @Test + public void testUpgradeWithEphemeralOnFollower() throws Exception { + testUpgradeWithEphemeral(false); + } + + @Test + public void testUpgradeWithEphemeralOnLeader() throws Exception { + testUpgradeWithEphemeral(true); + } + + private void testUpgradeWithEphemeral(boolean testLeader) throws Exception { + String nodePrefix = "/testUpgrade-" + (testLeader ? "leaderTest-" : "followerTest-"); + int leaderIdx = qb.getLeaderIndex(); + assertFalse("No leader in quorum?", leaderIdx == -1); + int followerIdx = (leaderIdx + 1) % 5; + int otherFollowerIdx = (leaderIdx + 2) % 5; + int testPeerIdx = testLeader ? leaderIdx : followerIdx; + String[] hostPorts = qb.hostPort.split(","); + + CountdownWatcher watcher = new CountdownWatcher(); + DisconnectableZooKeeper zk = new DisconnectableZooKeeper(hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + // Create some ephemeral nodes. This should force the session to + // be propagated to the other servers in the ensemble. + for (int i = 0; i < 5; i++) { + zk.create(nodePrefix + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + } + + // We should be able to reconnect with the same session id on a + // different server, since it has been propagated. + long localSessionId = zk.getSessionId(); + byte[] localSessionPwd = zk.getSessionPasswd().clone(); + + zk.disconnect(); + watcher.reset(); + zk = new DisconnectableZooKeeper(hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher, localSessionId, localSessionPwd); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + // The created ephemeral nodes are still around. + for (int i = 0; i < 5; i++) { + assertNotNull(zk.exists(nodePrefix + i, null)); + } + + // When we explicitly close the session, we should not be able to + // reconnect with the same session id + zk.close(); + + try { + watcher.reset(); + zk = new DisconnectableZooKeeper(hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher, localSessionId, localSessionPwd); + zk.exists(nodePrefix + "0", null); + fail("Reconnecting to a closed session ID should fail."); + } catch (KeeperException.SessionExpiredException e) { + } + + watcher.reset(); + // And the ephemeral nodes will be gone since the session died. + zk = new DisconnectableZooKeeper(hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + for (int i = 0; i < 5; i++) { + assertNull(zk.exists(nodePrefix + i, null)); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SledgeHammer.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SledgeHammer.java new file mode 100644 index 0000000..8190797 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SledgeHammer.java @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ExitCode; + +public class SledgeHammer extends Thread { + + ZooKeeper zk; + + int count; + + int readsPerWrite; + + public SledgeHammer(String hosts, int count, int readsPerWrite) throws Exception { + zk = ClientBase.createZKClient(hosts, 10000); + this.count = count; + this.readsPerWrite = readsPerWrite; + } + + public void run() { + try { + Stat stat = new Stat(); + String path = zk.create("/hammers/hammer-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + byte[] tag = (path + " was here!").getBytes(); + synchronized (this) { + String startPath = "/hammers/start"; + System.out.println("Waiting for " + startPath); + while (zk.exists(startPath, true) == null) { + wait(); + } + System.out.println("Running"); + } + for (int i = 0; i < count; i++) { + try { + System.out.print(i + "\r"); + List childs = zk.getChildren("/hammers", false); + Collections.shuffle(childs); + for (String s : childs) { + if (s.startsWith("hammer-")) { + s = "/hammers/" + s; + zk.setData(s, tag, -1); + for (int j = 0; j < readsPerWrite; j++) { + zk.getData(s, false, stat); + } + break; + } + } + } catch (KeeperException.ConnectionLossException e) { + // ignore connection loss + } catch (KeeperException e) { + e.printStackTrace(); + } + } + System.out.println(); + zk.close(); + } catch (RuntimeException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (KeeperException e) { + e.printStackTrace(); + } + } + + /** + * @param args + * @throws IOException + * @throws KeeperException + * @throws NumberFormatException + */ + public static void main(String[] args) throws Exception { + if (args.length != 3) { + System.err.println("USAGE: SledgeHammer zookeeper_server reps reads_per_rep"); + System.exit(ExitCode.UNABLE_TO_ACCESS_DATADIR.getValue()); + } + SledgeHammer h = new SledgeHammer(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2])); + h.start(); + System.exit(ExitCode.EXECUTION_FINISHED.getValue()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/StandaloneTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StandaloneTest.java new file mode 100644 index 0000000..8541d7a --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StandaloneTest.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.admin.ZooKeeperAdmin; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerTestBase; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Standalone server tests. + */ +public class StandaloneTest extends QuorumPeerTestBase implements Watcher { + + protected static final Logger LOG = LoggerFactory.getLogger(StandaloneTest.class); + + @Before + public void setup() { + System.setProperty("zookeeper.DigestAuthenticationProvider.superDigest", "super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is 'test'*/); + QuorumPeerConfig.setReconfigEnabled(true); + } + + /** + * This test wouldn't create any dynamic config. + * However, it adds a "clientPort=xxx" in static config file. + * It checks the standard way of standalone mode. + */ + @Test + public void testNoDynamicConfig() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT = PortAssignment.unique(); + + MainThread mt = new MainThread(MainThread.UNSET_MYID, CLIENT_PORT, "", false); + verifyStandalone(mt, CLIENT_PORT); + } + + /** + * This test creates a dynamic config of new format. + * The dynamic config is written in dynamic config file. + * It checks that the client port will be read from the dynamic config. + * + * This handles the case of HBase, which adds a single server line to the config. + * Maintain b/w compatibility. + */ + @Test + public void testClientPortInDynamicFile() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":participant;" + CLIENT_PORT + "\n"; + + MainThread mt = new MainThread(1, quorumCfgSection); + verifyStandalone(mt, CLIENT_PORT); + } + + /** + * This test creates a dynamic config of new format. + * The dynamic config is written in static config file. + * It checks that the client port will be read from the dynamic config. + */ + @Test + public void testClientPortInStaticFile() throws Exception { + ClientBase.setupTestEnv(); + final int CLIENT_PORT = PortAssignment.unique(); + + String quorumCfgSection = "server.1=127.0.0.1:" + (PortAssignment.unique()) + ":" + (PortAssignment.unique()) + ":participant;" + CLIENT_PORT + "\n"; + + MainThread mt = new MainThread(1, quorumCfgSection, false); + verifyStandalone(mt, CLIENT_PORT); + } + + void verifyStandalone(MainThread mt, int clientPort) throws InterruptedException { + mt.start(); + try { + assertTrue( + "waiting for server 1 being up", + ClientBase.waitForServerUp("127.0.0.1:" + clientPort, CONNECTION_TIMEOUT)); + } finally { + assertFalse("Error- MainThread started in Quorum Mode!", mt.isQuorumPeerRunning()); + mt.shutdown(); + } + } + + /** + * Verify that reconfiguration in standalone mode fails with + * KeeperException.UnimplementedException. + */ + @Test + public void testStandaloneReconfigFails() throws Exception { + ClientBase.setupTestEnv(); + + final int CLIENT_PORT = PortAssignment.unique(); + final String HOSTPORT = "127.0.0.1:" + CLIENT_PORT; + + File tmpDir = ClientBase.createTmpDir(); + ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000); + + ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1); + f.startup(zks); + assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT)); + + CountdownWatcher watcher = new CountdownWatcher(); + ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, watcher); + ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin(HOSTPORT, CONNECTION_TIMEOUT, watcher); + watcher.waitForConnected(CONNECTION_TIMEOUT); + + List joiners = new ArrayList(); + joiners.add("server.2=localhost:1234:1235;1236"); + // generate some transactions that will get logged + try { + zkAdmin.addAuthInfo("digest", "super:test".getBytes()); + zkAdmin.reconfigure(joiners, null, null, -1, new Stat()); + fail("Reconfiguration in standalone should trigger " + "UnimplementedException"); + } catch (KeeperException.UnimplementedException ex) { + // expected + } + zk.close(); + + zks.shutdown(); + f.shutdown(); + assertTrue("waiting for server being down ", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/StatTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StatTest.java new file mode 100644 index 0000000..cf57513 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StatTest.java @@ -0,0 +1,193 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.Test; + +public class StatTest extends ClientBase { + + private ZooKeeper zk; + + @Override + public void setUp() throws Exception { + super.setUp(); + + zk = createClient(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + + zk.close(); + } + + /** + * Create a new Stat, fill in dummy values trying to catch failure + * to copy in client or server code. + * + * @return a new stat with dummy values + */ + private Stat newStat() { + Stat stat = new Stat(); + + stat.setAversion(100); + stat.setCtime(100); + stat.setCversion(100); + stat.setCzxid(100); + stat.setDataLength(100); + stat.setEphemeralOwner(100); + stat.setMtime(100); + stat.setMzxid(100); + stat.setNumChildren(100); + stat.setPzxid(100); + stat.setVersion(100); + + return stat; + } + + @Test + public void testBasic() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + Stat stat; + + stat = newStat(); + zk.getData(name, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + } + + @Test + public void testChild() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + String childname = name + "/bar"; + zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + Stat stat; + + stat = newStat(); + zk.getData(name, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid() + 1, stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(1, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(1, stat.getNumChildren()); + + stat = newStat(); + zk.getData(childname, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(zk.getSessionId(), stat.getEphemeralOwner()); + assertEquals(childname.length(), stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + } + + @Test + public void testChildren() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + for (int i = 0; i < 10; i++) { + String childname = name + "/bar" + i; + zk.create(childname, childname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + Stat stat; + + stat = newStat(); + zk.getData(name, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid() + i + 1, stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(i + 1, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(i + 1, stat.getNumChildren()); + } + } + + @Test + public void testDataSizeChange() throws IOException, KeeperException, InterruptedException { + String name = "/foo"; + zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + Stat stat; + + stat = newStat(); + zk.getData(name, false, stat); + + assertEquals(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertEquals(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(0, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length(), stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + + zk.setData(name, (name + name).getBytes(), -1); + + stat = newStat(); + zk.getData(name, false, stat); + + assertNotSame(stat.getCzxid(), stat.getMzxid()); + assertEquals(stat.getCzxid(), stat.getPzxid()); + assertNotSame(stat.getCtime(), stat.getMtime()); + assertEquals(0, stat.getCversion()); + assertEquals(1, stat.getVersion()); + assertEquals(0, stat.getAversion()); + assertEquals(0, stat.getEphemeralOwner()); + assertEquals(name.length() * 2, stat.getDataLength()); + assertEquals(0, stat.getNumChildren()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/StaticHostProviderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StaticHostProviderTest.java new file mode 100644 index 0000000..7095c14 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StaticHostProviderTest.java @@ -0,0 +1,909 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.hamcrest.CoreMatchers.anyOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.client.HostProvider; +import org.apache.zookeeper.client.StaticHostProvider; +import org.apache.zookeeper.common.Time; +import org.junit.Test; + +public class StaticHostProviderTest extends ZKTestCase { + + private Random r = new Random(1); + + @Test + public void testNextGoesRound() { + HostProvider hostProvider = getHostProvider((byte) 2); + InetSocketAddress first = hostProvider.next(0); + assertTrue(first != null); + hostProvider.next(0); + assertEquals(first, hostProvider.next(0)); + } + + @Test + public void testNextGoesRoundAndSleeps() { + byte size = 2; + HostProvider hostProvider = getHostProvider(size); + while (size > 0) { + hostProvider.next(0); + --size; + } + long start = Time.currentElapsedTime(); + hostProvider.next(1000); + long stop = Time.currentElapsedTime(); + assertTrue(900 <= stop - start); + } + + @Test + public void testNextDoesNotSleepForZero() { + byte size = 2; + HostProvider hostProvider = getHostProvider(size); + while (size > 0) { + hostProvider.next(0); + --size; + } + long start = Time.currentElapsedTime(); + hostProvider.next(0); + long stop = Time.currentElapsedTime(); + assertTrue(5 > stop - start); + } + + @Test(expected = IllegalArgumentException.class) + public void testEmptyServerAddressesList() { + HostProvider hp = new StaticHostProvider(new ArrayList<>()); + } + + @Test + public void testInvalidHostAddresses() { + // Arrange + final List invalidAddresses = new ArrayList<>(); + InetSocketAddress unresolved = InetSocketAddress.createUnresolved("a", 1234); + invalidAddresses.add(unresolved); + StaticHostProvider.Resolver resolver = new StaticHostProvider.Resolver() { + @Override + public InetAddress[] getAllByName(String name) throws UnknownHostException { + throw new UnknownHostException(); + } + }; + StaticHostProvider sp = new StaticHostProvider(invalidAddresses, resolver); + + // Act & Assert + InetSocketAddress n1 = sp.next(0); + assertTrue("Provider should return unresolved address is host is unresolvable", n1.isUnresolved()); + assertSame("Provider should return original address is host is unresolvable", unresolved, n1); + } + + @Test + public void testTwoConsequitiveCallsToNextReturnDifferentElement() { + HostProvider hostProvider = getHostProvider((byte) 2); + assertNotSame(hostProvider.next(0), hostProvider.next(0)); + } + + @Test + public void testOnConnectDoesNotReset() { + HostProvider hostProvider = getHostProvider((byte) 2); + InetSocketAddress first = hostProvider.next(0); + hostProvider.onConnected(); + InetSocketAddress second = hostProvider.next(0); + assertNotSame(first, second); + } + + /* Reconfig tests with IP addresses */ + + private final double slackPercent = 10; + private final int numClients = 10000; + + @Test + public void testUpdateClientMigrateOrNot() throws UnknownHostException { + HostProvider hostProvider = getHostProvider((byte) 4); // 10.10.10.4:1238, 10.10.10.3:1237, 10.10.10.2:1236, 10.10.10.1:1235 + Collection newList = getServerAddresses((byte) 3); // 10.10.10.3:1237, 10.10.10.2:1236, 10.10.10.1:1235 + + InetSocketAddress myServer = new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, 3}), 1237); + + // Number of machines becomes smaller, my server is in the new cluster + boolean disconnectRequired = hostProvider.updateServerList(newList, myServer); + assertFalse(disconnectRequired); + hostProvider.onConnected(); + + // Number of machines stayed the same, my server is in the new cluster + disconnectRequired = hostProvider.updateServerList(newList, myServer); + assertFalse(disconnectRequired); + hostProvider.onConnected(); + + // Number of machines became smaller, my server is not in the new + // cluster + newList = getServerAddresses((byte) 2); // 10.10.10.2:1236, 10.10.10.1:1235 + disconnectRequired = hostProvider.updateServerList(newList, myServer); + assertTrue(disconnectRequired); + hostProvider.onConnected(); + + // Number of machines stayed the same, my server is not in the new + // cluster + disconnectRequired = hostProvider.updateServerList(newList, myServer); + assertTrue(disconnectRequired); + hostProvider.onConnected(); + + // Number of machines increased, my server is not in the new cluster + newList = new ArrayList(3); + for (byte i = 4; i > 1; i--) { // 10.10.10.4:1238, 10.10.10.3:1237, 10.10.10.2:1236 + newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, i}), 1234 + i)); + } + myServer = new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, 1}), 1235); + disconnectRequired = hostProvider.updateServerList(newList, myServer); + assertTrue(disconnectRequired); + hostProvider.onConnected(); + + // Number of machines increased, my server is in the new cluster + // Here whether to move or not depends on the difference of cluster + // sizes + // With probability 1 - |old|/|new} the client disconnects + // In the test below 1-9/10 = 1/10 chance of disconnecting + HostProvider[] hostProviderArray = new HostProvider[numClients]; + newList = getServerAddresses((byte) 10); + int numDisconnects = 0; + for (int i = 0; i < numClients; i++) { + hostProviderArray[i] = getHostProvider((byte) 9); + disconnectRequired = hostProviderArray[i].updateServerList(newList, myServer); + if (disconnectRequired) { + numDisconnects++; + } + } + hostProvider.onConnected(); + + // should be numClients/10 in expectation, we test that its numClients/10 +- slackPercent + assertTrue(numDisconnects < upperboundCPS(numClients, 10)); + } + + @Test + public void testUpdateMigrationGoesRound() throws UnknownHostException { + HostProvider hostProvider = getHostProvider((byte) 4); + // old list (just the ports): 1238, 1237, 1236, 1235 + Collection newList = new ArrayList(10); + for (byte i = 12; i > 2; i--) { // 1246, 1245, 1244, 1243, 1242, 1241, + // 1240, 1239, 1238, 1237 + newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, i}), 1234 + i)); + } + + // servers from the old list that appear in the new list + Collection oldStaying = new ArrayList(2); + for (byte i = 4; i > 2; i--) { // 1238, 1237 + oldStaying.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, i}), 1234 + i)); + } + + // servers in the new list that are not in the old list + Collection newComing = new ArrayList(10); + for (byte i = 12; i > 4; i--) {// 1246, 1245, 1244, 1243, 1242, 1241, 1240, 1139 + newComing.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, i}), 1234 + i)); + } + + // Number of machines increases, my server is not in the new cluster + // load on old servers must be decreased, so must connect to one of the + // new servers + // i.e., pNew = 1. + + boolean disconnectRequired = hostProvider.updateServerList(newList, new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, 1}), 1235)); + assertTrue(disconnectRequired); + + // This means reconfigMode = true, and nextHostInReconfigMode will be + // called from next + // Since pNew = 1 we should first try the new servers + ArrayList seen = new ArrayList(); + for (int i = 0; i < newComing.size(); i++) { + InetSocketAddress addr = hostProvider.next(0); + assertTrue(newComing.contains(addr)); + assertTrue(!seen.contains(addr)); + seen.add(addr); + } + + // Next the old servers + seen.clear(); + for (int i = 0; i < oldStaying.size(); i++) { + InetSocketAddress addr = hostProvider.next(0); + assertTrue(oldStaying.contains(addr)); + assertTrue(!seen.contains(addr)); + seen.add(addr); + } + + // And now it goes back to normal next() so it should be everything + // together like in testNextGoesRound() + InetSocketAddress first = hostProvider.next(0); + assertTrue(first != null); + for (int i = 0; i < newList.size() - 1; i++) { + hostProvider.next(0); + } + + assertEquals(first, hostProvider.next(0)); + hostProvider.onConnected(); + } + + @Test + public void testUpdateLoadBalancing() throws UnknownHostException { + // Start with 9 servers and 10000 clients + boolean disconnectRequired; + HostProvider[] hostProviderArray = new HostProvider[numClients]; + InetSocketAddress[] curHostForEachClient = new InetSocketAddress[numClients]; + int[] numClientsPerHost = new int[9]; + + // initialization + for (int i = 0; i < numClients; i++) { + hostProviderArray[i] = getHostProvider((byte) 9); + curHostForEachClient[i] = hostProviderArray[i].next(0); + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 9; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 9)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 9)); + numClientsPerHost[i] = 0; // prepare for next test + } + + // remove host number 8 (the last one in a list of 9 hosts) + Collection newList = getServerAddresses((byte) 8); + + for (int i = 0; i < numClients; i++) { + disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 8; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 8)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 8)); + numClientsPerHost[i] = 0; // prepare for next test + } + assertTrue(numClientsPerHost[8] == 0); + + // remove hosts number 6 and 7 (the currently last two in the list) + newList = getServerAddresses((byte) 6); + + for (int i = 0; i < numClients; i++) { + disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 6; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 6)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 6)); + numClientsPerHost[i] = 0; // prepare for next test + } + assertTrue(numClientsPerHost[6] == 0); + assertTrue(numClientsPerHost[7] == 0); + assertTrue(numClientsPerHost[8] == 0); + + // remove host number 0 (the first one in the current list) + // and add back hosts 6, 7 and 8 + newList = new ArrayList(8); + for (byte i = 9; i > 1; i--) { + newList.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, i}), 1234 + i)); + } + + for (int i = 0; i < numClients; i++) { + disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + assertTrue(numClientsPerHost[0] == 0); + + for (int i = 1; i < 9; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 8)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 8)); + numClientsPerHost[i] = 0; // prepare for next test + } + + // add back host number 0 + newList = getServerAddresses((byte) 9); + + for (int i = 0; i < numClients; i++) { + disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 9; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 9)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 9)); + } + } + + @Test + public void testNoCurrentHostDuringNormalMode() throws UnknownHostException { + // Start with 9 servers and 10000 clients + boolean disconnectRequired; + StaticHostProvider[] hostProviderArray = new StaticHostProvider[numClients]; + InetSocketAddress[] curHostForEachClient = new InetSocketAddress[numClients]; + int[] numClientsPerHost = new int[9]; + + // initialization + for (int i = 0; i < numClients; i++) { + hostProviderArray[i] = getHostProvider((byte) 9); + if (i >= (numClients / 2)) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } else { + // its supposed to be the first server on serverList. + // we'll set it later, see below (*) + curHostForEachClient[i] = null; + } + } + + // remove hosts 7 and 8 (the last two in a list of 9 hosts) + Collection newList = getServerAddresses((byte) 7); + + for (int i = 0; i < numClients; i++) { + // tests the case currentHost == null && lastIndex == -1 + // calls next for clients with index < numClients/2 + disconnectRequired = hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } else if (curHostForEachClient[i] == null) { + // (*) setting it to what it should be + curHostForEachClient[i] = hostProviderArray[i].getServerAtIndex(0); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + // sets lastIndex, resets reconfigMode + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 7; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 7)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 7)); + numClientsPerHost[i] = 0; // prepare for next test + } + assertTrue(numClientsPerHost[7] == 0); + assertTrue(numClientsPerHost[8] == 0); + + // add back server 7 + newList = getServerAddresses((byte) 8); + + for (int i = 0; i < numClients; i++) { + InetSocketAddress myServer = (i < (numClients / 2)) ? null : curHostForEachClient[i]; + // tests the case currentHost == null && lastIndex >= 0 + disconnectRequired = hostProviderArray[i].updateServerList(newList, myServer); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 8; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 8)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 8)); + } + } + + @Test + public void testReconfigDuringReconfigMode() throws UnknownHostException { + // Start with 9 servers and 10000 clients + boolean disconnectRequired; + StaticHostProvider[] hostProviderArray = new StaticHostProvider[numClients]; + InetSocketAddress[] curHostForEachClient = new InetSocketAddress[numClients]; + int[] numClientsPerHost = new int[9]; + + // initialization + for (int i = 0; i < numClients; i++) { + hostProviderArray[i] = getHostProvider((byte) 9); + curHostForEachClient[i] = hostProviderArray[i].next(0); + } + + // remove hosts 7 and 8 (the last two in a list of 9 hosts) + Collection newList = getServerAddresses((byte) 7); + + for (int i = 0; i < numClients; i++) { + // sets reconfigMode + hostProviderArray[i].updateServerList(newList, curHostForEachClient[i]); + } + + // add back servers 7 and 8 while still in reconfigMode (we didn't call + // next) + newList = getServerAddresses((byte) 9); + + for (int i = 0; i < numClients; i++) { + InetSocketAddress myServer = (i < (numClients / 2)) ? null : curHostForEachClient[i]; + // for i < (numClients/2) this tests the case currentHost == null && + // reconfigMode = true + // for i >= (numClients/2) this tests the case currentHost!=null && + // reconfigMode = true + disconnectRequired = hostProviderArray[i].updateServerList(newList, myServer); + if (disconnectRequired) { + curHostForEachClient[i] = hostProviderArray[i].next(0); + } else { + // currentIndex was set by the call to updateServerList, which + // called next + curHostForEachClient[i] = hostProviderArray[i].getServerAtCurrentIndex(); + } + numClientsPerHost[curHostForEachClient[i].getPort() - 1235]++; + hostProviderArray[i].onConnected(); + } + + for (int i = 0; i < 9; i++) { + assertTrue(numClientsPerHost[i] <= upperboundCPS(numClients, 9)); + assertTrue(numClientsPerHost[i] >= lowerboundCPS(numClients, 9)); + } + } + + private StaticHostProvider getHostProvider(byte size) { + return new StaticHostProvider(getServerAddresses(size), r.nextLong()); + } + + private Collection getServerAddresses(byte size) { + ArrayList list = new ArrayList(size); + while (size > 0) { + try { + list.add(new InetSocketAddress(InetAddress.getByAddress(new byte[]{10, 10, 10, size}), 1234 + size)); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + --size; + } + return list; + } + + /* Reconfig test with unresolved hostnames */ + + /** + * Number of machines becomes smaller, my server is in the new cluster + */ + @Test + public void testUpdateServerList_UnresolvedHostnames_NoDisconnection1() { + // Arrange + // [testhost-4.testdomain.com:1238, testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + HostProvider hostProvider = getHostProviderWithUnresolvedHostnames(4); + // [testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + Collection newList = getUnresolvedHostnames(3); + InetSocketAddress myServer = InetSocketAddress.createUnresolved("testhost-3.testdomain.com", 1237); + + // Act + boolean disconnectRequired = hostProvider.updateServerList(newList, myServer); + + // Assert + assertFalse(disconnectRequired); + hostProvider.onConnected(); + } + + /** + * Number of machines stayed the same, my server is in the new cluster + */ + @Test + public void testUpdateServerList_UnresolvedHostnames_NoDisconnection2() { + // Arrange + // [testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + HostProvider hostProvider = getHostProviderWithUnresolvedHostnames(3); + // [testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + Collection newList = getUnresolvedHostnames(3); + InetSocketAddress myServer = InetSocketAddress.createUnresolved("testhost-3.testdomain.com", 1237); + + // Act + boolean disconnectRequired = hostProvider.updateServerList(newList, myServer); + + // Assert + assertFalse(disconnectRequired); + hostProvider.onConnected(); + } + + /** + * Number of machines became smaller, my server is not in the new cluster + */ + @Test + public void testUpdateServerList_UnresolvedHostnames_Disconnection1() { + // Arrange + // [testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + HostProvider hostProvider = getHostProviderWithUnresolvedHostnames(3); + // [testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + Collection newList = getUnresolvedHostnames(2); + InetSocketAddress myServer = InetSocketAddress.createUnresolved("testhost-3.testdomain.com", 1237); + + // Act + boolean disconnectRequired = hostProvider.updateServerList(newList, myServer); + + // Assert + assertTrue(disconnectRequired); + hostProvider.onConnected(); + } + + /** + * Number of machines stayed the same, my server is not in the new cluster + */ + @Test + public void testUpdateServerList_UnresolvedHostnames_Disconnection2() { + // Arrange + // [testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + HostProvider hostProvider = getHostProviderWithUnresolvedHostnames(3); + // [testhost-3.testdomain.com:1237, testhost-2.testdomain.com:1236, testhost-1.testdomain.com:1235] + Collection newList = getUnresolvedHostnames(3); + InetSocketAddress myServer = InetSocketAddress.createUnresolved("testhost-4.testdomain.com", 1237); + + // Act + boolean disconnectRequired = hostProvider.updateServerList(newList, myServer); + + // Assert + assertTrue(disconnectRequired); + hostProvider.onConnected(); + } + + @Test + public void testUpdateServerList_ResolvedWithUnResolvedAddress_ForceDisconnect() { + // Arrange + // Create a HostProvider with a list of unresolved server address(es) + List addresses = Collections.singletonList(InetSocketAddress.createUnresolved("testhost-1.resolvable.zk", 1235)); + HostProvider hostProvider = new StaticHostProvider(addresses, new TestResolver()); + InetSocketAddress currentHost = hostProvider.next(100); + assertThat("CurrentHost is which the client is currently connecting to, it should be resolved", currentHost.isUnresolved(), is(false)); + + // Act + InetSocketAddress replaceHost = InetSocketAddress.createUnresolved("testhost-1.resolvable.zk", 1235); + assertThat("Replace host must be unresolved in this test case", replaceHost.isUnresolved(), is(true)); + boolean disconnect = hostProvider.updateServerList(new ArrayList<>(Collections.singletonList(replaceHost)), currentHost); + + // Assert + assertThat(disconnect, is(false)); + } + + @Test + public void testUpdateServerList_ResolvedWithResolvedAddress_NoDisconnect() throws UnknownHostException { + // Arrange + // Create a HostProvider with a list of unresolved server address(es) + List addresses = Collections.singletonList(InetSocketAddress.createUnresolved("testhost-1.resolvable.zk", 1235)); + HostProvider hostProvider = new StaticHostProvider(addresses, new TestResolver()); + InetSocketAddress currentHost = hostProvider.next(100); + assertThat("CurrentHost is which the client is currently connecting to, it should be resolved", currentHost.isUnresolved(), is(false)); + + // Act + InetSocketAddress replaceHost = new InetSocketAddress(InetAddress.getByAddress(currentHost.getHostString(), currentHost.getAddress().getAddress()), currentHost.getPort()); + assertThat("Replace host must be resolved in this test case", replaceHost.isUnresolved(), is(false)); + boolean disconnect = hostProvider.updateServerList(new ArrayList<>(Collections.singletonList(replaceHost)), currentHost); + + // Assert + assertThat(disconnect, equalTo(false)); + } + + @Test + public void testUpdateServerList_UnResolvedWithUnResolvedAddress_ForceDisconnect() { + // Arrange + // Create a HostProvider with a list of unresolved server address(es) + List addresses = Collections.singletonList(InetSocketAddress.createUnresolved("testhost-1.zookeepertest.zk", 1235)); + HostProvider hostProvider = new StaticHostProvider(addresses, new TestResolver()); + InetSocketAddress currentHost = hostProvider.next(100); + assertThat("CurrentHost is not resolvable in this test case", currentHost.isUnresolved(), is(true)); + + // Act + InetSocketAddress replaceHost = InetSocketAddress.createUnresolved("testhost-1.resolvable.zk", 1235); + assertThat("Replace host must be unresolved in this test case", replaceHost.isUnresolved(), is(true)); + boolean disconnect = hostProvider.updateServerList(new ArrayList<>(Collections.singletonList(replaceHost)), currentHost); + + // Assert + assertThat(disconnect, is(true)); + } + + @Test + public void testUpdateServerList_UnResolvedWithResolvedAddress_ForceDisconnect() throws UnknownHostException { + // Arrange + // Create a HostProvider with a list of unresolved server address(es) + List addresses = Collections.singletonList(InetSocketAddress.createUnresolved("testhost-1.zookeepertest.zk", 1235)); + HostProvider hostProvider = new StaticHostProvider(addresses, new TestResolver()); + InetSocketAddress currentHost = hostProvider.next(100); + assertThat("CurrentHost not resolvable in this test case", currentHost.isUnresolved(), is(true)); + + // Act + byte[] addr = new byte[]{10, 0, 0, 1}; + InetSocketAddress replaceHost = new InetSocketAddress(InetAddress.getByAddress(currentHost.getHostString(), addr), currentHost.getPort()); + assertThat("Replace host must be resolved in this test case", replaceHost.isUnresolved(), is(false)); + boolean disconnect = hostProvider.updateServerList(new ArrayList<>(Collections.singletonList(replaceHost)), currentHost); + + // Assert + assertThat(disconnect, equalTo(false)); + } + + private class TestResolver implements StaticHostProvider.Resolver { + + private byte counter = 1; + + @Override + public InetAddress[] getAllByName(String name) throws UnknownHostException { + if (name.contains("resolvable")) { + byte[] addr = new byte[]{10, 0, 0, (byte) (counter++ % 10)}; + return new InetAddress[]{InetAddress.getByAddress(name, addr)}; + } + throw new UnknownHostException(); + } + + } + + private double lowerboundCPS(int numClients, int numServers) { + return (1 - slackPercent / 100.0) * numClients / numServers; + } + + private double upperboundCPS(int numClients, int numServers) { + return (1 + slackPercent / 100.0) * numClients / numServers; + } + + /* DNS resolution tests */ + + @Test + public void testLiteralIPNoReverseNS() { + byte size = 30; + HostProvider hostProvider = getHostProviderUnresolved(size); + for (int i = 0; i < size; i++) { + InetSocketAddress next = hostProvider.next(0); + assertThat(next, instanceOf(InetSocketAddress.class)); + assertFalse(next.isUnresolved()); + assertTrue(next.toString().startsWith("/")); + // Do NOT trigger the reverse name service lookup. + String hostname = next.getHostString(); + // In this case, the hostname equals literal IP address. + assertEquals(next.getAddress().getHostAddress(), hostname); + } + } + + @Test + public void testReResolvingSingle() throws UnknownHostException { + // Arrange + byte size = 1; + ArrayList list = new ArrayList(size); + + // Test a hostname that resolves to a single address + list.add(InetSocketAddress.createUnresolved("issues.apache.org", 1234)); + + final InetAddress issuesApacheOrg = mock(InetAddress.class); + when(issuesApacheOrg.getHostAddress()).thenReturn("192.168.1.1"); + when(issuesApacheOrg.toString()).thenReturn("issues.apache.org"); + when(issuesApacheOrg.getHostName()).thenReturn("issues.apache.org"); + + StaticHostProvider.Resolver resolver = new StaticHostProvider.Resolver() { + @Override + public InetAddress[] getAllByName(String name) { + return new InetAddress[]{issuesApacheOrg}; + } + }; + StaticHostProvider.Resolver spyResolver = spy(resolver); + + // Act + StaticHostProvider hostProvider = new StaticHostProvider(list, spyResolver); + for (int i = 0; i < 10; i++) { + InetSocketAddress next = hostProvider.next(0); + assertEquals(issuesApacheOrg, next.getAddress()); + } + + // Assert + // Resolver called 10 times, because we shouldn't cache the resolved addresses + verify(spyResolver, times(10)).getAllByName("issues.apache.org"); // resolution occurred + } + + @Test + public void testReResolvingMultiple() throws UnknownHostException { + // Arrange + byte size = 1; + ArrayList list = new ArrayList(size); + + // Test a hostname that resolves to multiple addresses + list.add(InetSocketAddress.createUnresolved("www.apache.org", 1234)); + + final InetAddress apacheOrg1 = mock(InetAddress.class); + when(apacheOrg1.getHostAddress()).thenReturn("192.168.1.1"); + when(apacheOrg1.toString()).thenReturn("www.apache.org"); + when(apacheOrg1.getHostName()).thenReturn("www.apache.org"); + + final InetAddress apacheOrg2 = mock(InetAddress.class); + when(apacheOrg2.getHostAddress()).thenReturn("192.168.1.2"); + when(apacheOrg2.toString()).thenReturn("www.apache.org"); + when(apacheOrg2.getHostName()).thenReturn("www.apache.org"); + + final List resolvedAddresses = new ArrayList(); + resolvedAddresses.add(apacheOrg1); + resolvedAddresses.add(apacheOrg2); + StaticHostProvider.Resolver resolver = new StaticHostProvider.Resolver() { + @Override + public InetAddress[] getAllByName(String name) { + return resolvedAddresses.toArray(new InetAddress[resolvedAddresses.size()]); + } + }; + StaticHostProvider.Resolver spyResolver = spy(resolver); + + // Act & Assert + StaticHostProvider hostProvider = new StaticHostProvider(list, spyResolver); + assertEquals(1, hostProvider.size()); // single address not extracted + + for (int i = 0; i < 10; i++) { + InetSocketAddress next = hostProvider.next(0); + assertThat("Bad IP address returned", next.getAddress().getHostAddress(), anyOf(equalTo(apacheOrg1.getHostAddress()), equalTo(apacheOrg2.getHostAddress()))); + assertEquals(1, hostProvider.size()); // resolve() call keeps the size of provider + } + // Resolver called 10 times, because we shouldn't cache the resolved addresses + verify(spyResolver, times(10)).getAllByName("www.apache.org"); // resolution occurred + } + + @Test + public void testReResolveMultipleOneFailing() throws UnknownHostException { + // Arrange + final List list = new ArrayList(); + list.add(InetSocketAddress.createUnresolved("www.apache.org", 1234)); + final List ipList = new ArrayList(); + final List resolvedAddresses = new ArrayList(); + for (int i = 0; i < 3; i++) { + ipList.add(String.format("192.168.1.%d", i + 1)); + final InetAddress apacheOrg = mock(InetAddress.class); + when(apacheOrg.getHostAddress()).thenReturn(String.format("192.168.1.%d", i + 1)); + when(apacheOrg.toString()).thenReturn(String.format("192.168.1.%d", i + 1)); + when(apacheOrg.getHostName()).thenReturn("www.apache.org"); + resolvedAddresses.add(apacheOrg); + } + + StaticHostProvider.Resolver resolver = new StaticHostProvider.Resolver() { + @Override + public InetAddress[] getAllByName(String name) { + return resolvedAddresses.toArray(new InetAddress[resolvedAddresses.size()]); + } + }; + StaticHostProvider.Resolver spyResolver = spy(resolver); + StaticHostProvider hostProvider = new StaticHostProvider(list, spyResolver); + + // Act & Assert + InetSocketAddress resolvedFirst = hostProvider.next(0); + assertFalse("HostProvider should return resolved addresses", resolvedFirst.isUnresolved()); + assertThat("Bad IP address returned", ipList, hasItems(resolvedFirst.getAddress().getHostAddress())); + + hostProvider.onConnected(); // first address worked + + InetSocketAddress resolvedSecond = hostProvider.next(0); + assertFalse("HostProvider should return resolved addresses", resolvedSecond.isUnresolved()); + assertThat("Bad IP address returned", ipList, hasItems(resolvedSecond.getAddress().getHostAddress())); + + // Second address doesn't work, so we don't call onConnected() this time + // StaticHostProvider should try to re-resolve the address in this case + + InetSocketAddress resolvedThird = hostProvider.next(0); + assertFalse("HostProvider should return resolved addresses", resolvedThird.isUnresolved()); + assertThat("Bad IP address returned", ipList, hasItems(resolvedThird.getAddress().getHostAddress())); + + verify(spyResolver, times(3)).getAllByName("www.apache.org"); // resolution occured every time + } + + @Test + public void testEmptyResolution() throws UnknownHostException { + // Arrange + final List list = new ArrayList(); + list.add(InetSocketAddress.createUnresolved("www.apache.org", 1234)); + list.add(InetSocketAddress.createUnresolved("www.google.com", 1234)); + final List resolvedAddresses = new ArrayList(); + + final InetAddress apacheOrg1 = mock(InetAddress.class); + when(apacheOrg1.getHostAddress()).thenReturn("192.168.1.1"); + when(apacheOrg1.toString()).thenReturn("www.apache.org"); + when(apacheOrg1.getHostName()).thenReturn("www.apache.org"); + + resolvedAddresses.add(apacheOrg1); + + StaticHostProvider.Resolver resolver = new StaticHostProvider.Resolver() { + @Override + public InetAddress[] getAllByName(String name) { + if ("www.apache.org".equalsIgnoreCase(name)) { + return resolvedAddresses.toArray(new InetAddress[resolvedAddresses.size()]); + } else { + return new InetAddress[0]; + } + } + }; + StaticHostProvider.Resolver spyResolver = spy(resolver); + StaticHostProvider hostProvider = new StaticHostProvider(list, spyResolver); + + // Act & Assert + for (int i = 0; i < 10; i++) { + InetSocketAddress resolved = hostProvider.next(0); + hostProvider.onConnected(); + if (resolved.getHostName().equals("www.google.com")) { + assertTrue("HostProvider should return unresolved address if host is unresolvable", resolved.isUnresolved()); + } else { + assertFalse("HostProvider should return resolved addresses", resolved.isUnresolved()); + assertEquals("192.168.1.1", resolved.getAddress().getHostAddress()); + } + } + + verify(spyResolver, times(5)).getAllByName("www.apache.org"); + verify(spyResolver, times(5)).getAllByName("www.google.com"); + } + + @Test + public void testReResolvingLocalhost() { + byte size = 2; + ArrayList list = new ArrayList(size); + + // Test a hostname that resolves to multiple addresses + list.add(InetSocketAddress.createUnresolved("localhost", 1234)); + list.add(InetSocketAddress.createUnresolved("localhost", 1235)); + StaticHostProvider hostProvider = new StaticHostProvider(list); + int sizeBefore = hostProvider.size(); + InetSocketAddress next = hostProvider.next(0); + next = hostProvider.next(0); + assertTrue("Different number of addresses in the list: " + + hostProvider.size() + + " (after), " + + sizeBefore + + " (before)", hostProvider.size() == sizeBefore); + } + + private StaticHostProvider getHostProviderUnresolved(byte size) { + return new StaticHostProvider(getUnresolvedServerAddresses(size), r.nextLong()); + } + + private Collection getUnresolvedServerAddresses(byte size) { + ArrayList list = new ArrayList(size); + while (size > 0) { + list.add(InetSocketAddress.createUnresolved("10.10.10." + size, 1234 + size)); + --size; + } + return list; + } + + private StaticHostProvider getHostProviderWithUnresolvedHostnames(int size) { + return new StaticHostProvider(getUnresolvedHostnames(size), r.nextLong()); + } + + private Collection getUnresolvedHostnames(int size) { + ArrayList list = new ArrayList<>(size); + while (size > 0) { + list.add(InetSocketAddress.createUnresolved(String.format("testhost-%d.testdomain.com", size), 1234 + + size)); + --size; + } + System.out.println(Arrays.toString(list.toArray())); + return list; + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java new file mode 100644 index 0000000..caa3db6 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.common.StringUtils; +import org.junit.Test; + +public class StringUtilTest extends ZKTestCase { + + @Test + public void testStrings() { + + String s1 = " a , b , "; + assertEquals("[a, b]", StringUtils.split(s1, ",").toString()); + + String s2 = ""; + assertEquals(0, StringUtils.split(s2, ",").size()); + + String s3 = "1, , 2"; + assertEquals("[1, 2]", StringUtils.split(s3, ",").toString()); + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/SyncCallTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SyncCallTest.java new file mode 100644 index 0000000..d743da0 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/SyncCallTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.AsyncCallback.Children2Callback; +import org.apache.zookeeper.AsyncCallback.ChildrenCallback; +import org.apache.zookeeper.AsyncCallback.Create2Callback; +import org.apache.zookeeper.AsyncCallback.StringCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.DummyWatcher; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.junit.Test; + +public class SyncCallTest extends ClientBase implements ChildrenCallback, Children2Callback, StringCallback, VoidCallback, Create2Callback { + + private CountDownLatch opsCount; + + List results = new LinkedList<>(); + Integer limit = 100 + 1 + 100 + 100; + + @Test + public void testSync() throws Exception { + try { + LOG.info("Starting ZK:{}", (new Date()).toString()); + opsCount = new CountDownLatch(limit); + ZooKeeper zk = createClient(); + + LOG.info("Beginning test:{}", (new Date()).toString()); + for (int i = 0; i < 50; i++) { + zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (StringCallback) this, results); + } + + for (int i = 50; i < 100; i++) { + zk.create("/test" + i, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, (Create2Callback) this, results); + } + zk.sync("/test", this, results); + for (int i = 0; i < 100; i++) { + zk.delete("/test" + i, 0, this, results); + } + for (int i = 0; i < 100; i++) { + zk.getChildren("/", DummyWatcher.INSTANCE, (ChildrenCallback) this, results); + } + for (int i = 0; i < 100; i++) { + zk.getChildren("/", DummyWatcher.INSTANCE, (Children2Callback) this, results); + } + LOG.info("Submitted all operations:{}", (new Date()).toString()); + + if (!opsCount.await(10000, TimeUnit.MILLISECONDS)) { + fail("Haven't received all confirmations" + opsCount.getCount()); + } + + for (int i = 0; i < limit; i++) { + assertEquals(0, (int) results.get(i)); + } + + } catch (IOException e) { + System.out.println(e.toString()); + } + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, List children) { + ((List) ctx).add(rc); + opsCount.countDown(); + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, List children, Stat stat) { + ((List) ctx).add(rc); + opsCount.countDown(); + } + + @SuppressWarnings("unchecked") + public void processResult(int rc, String path, Object ctx, String name) { + ((List) ctx).add(rc); + opsCount.countDown(); + + } + + @SuppressWarnings("unchecked") + @Override + public void processResult(int rc, String path, Object ctx) { + ((List) ctx).add(rc); + opsCount.countDown(); + + } + + @SuppressWarnings("unchecked") + @Override + public void processResult(int rc, String path, Object ctx, String name, Stat stat) { + ((List) ctx).add(rc); + opsCount.countDown(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestByteBufAllocator.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestByteBufAllocator.java new file mode 100644 index 0000000..a6b3251 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestByteBufAllocator.java @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.CompositeByteBuf; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.util.ResourceLeakDetector; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; + +/** + * This is a custom ByteBufAllocator that tracks outstanding allocations and + * crashes the program if any of them are leaked. + * + * Never use this class in production, it will cause your server to run out + * of memory! This is because it holds strong references to all allocated + * buffers and doesn't release them until checkForLeaks() is called at the + * end of a unit test. + * + * Note: the original code was copied from https://github.com/airlift/drift, + * with the permission and encouragement of airlift's author (dain). Airlift + * uses the same apache 2.0 license as Zookeeper so this should be ok. + * + * However, the code was modified to take advantage of Netty's built-in + * leak tracking and make a best effort to print details about buffer leaks. + * + */ +public class TestByteBufAllocator extends PooledByteBufAllocator { + + private static AtomicReference INSTANCE = new AtomicReference<>(null); + + /** + * Get the singleton testing allocator. + * @return the singleton allocator, creating it if one does not exist. + */ + public static TestByteBufAllocator getInstance() { + TestByteBufAllocator result = INSTANCE.get(); + if (result == null) { + ResourceLeakDetector.Level oldLevel = ResourceLeakDetector.getLevel(); + ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.PARANOID); + INSTANCE.compareAndSet(null, new TestByteBufAllocator(oldLevel)); + result = INSTANCE.get(); + } + return result; + } + + /** + * Destroys the singleton testing allocator and throws an error if any of the + * buffers allocated by it have been leaked. Attempts to print leak details to + * standard error before throwing, by using netty's built-in leak tracking. + * Note that this might not always work, since it only triggers when a buffer + * is garbage-collected and calling System.gc() does not guarantee that a buffer + * will actually be GC'ed. + * + * This should be called at the end of a unit test's tearDown() method. + */ + public static void checkForLeaks() { + TestByteBufAllocator result = INSTANCE.getAndSet(null); + if (result != null) { + result.checkInstanceForLeaks(); + } + } + + private final List trackedBuffers = new ArrayList<>(); + private final ResourceLeakDetector.Level oldLevel; + + private TestByteBufAllocator(ResourceLeakDetector.Level oldLevel) { + super(false); + this.oldLevel = oldLevel; + } + + @Override + protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) { + return track(super.newHeapBuffer(initialCapacity, maxCapacity)); + } + + @Override + protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { + return track(super.newDirectBuffer(initialCapacity, maxCapacity)); + } + + @Override + public CompositeByteBuf compositeHeapBuffer(int maxNumComponents) { + return track(super.compositeHeapBuffer(maxNumComponents)); + } + + @Override + public CompositeByteBuf compositeDirectBuffer(int maxNumComponents) { + return track(super.compositeDirectBuffer(maxNumComponents)); + } + + private synchronized CompositeByteBuf track(CompositeByteBuf byteBuf) { + trackedBuffers.add(Objects.requireNonNull(byteBuf)); + return byteBuf; + } + + private synchronized ByteBuf track(ByteBuf byteBuf) { + trackedBuffers.add(Objects.requireNonNull(byteBuf)); + return byteBuf; + } + + private void checkInstanceForLeaks() { + try { + long referencedBuffersCount = 0; + synchronized (this) { + referencedBuffersCount = trackedBuffers.stream().filter(byteBuf -> byteBuf.refCnt() > 0).count(); + // Make tracked buffers eligible for GC + trackedBuffers.clear(); + } + // Throw an error if there were any leaked buffers + if (referencedBuffersCount > 0) { + // Trigger a GC. This will hopefully (but not necessarily) print + // details about detected leaks to standard error before the error + // is thrown. + System.gc(); + throw new AssertionError("Found a netty ByteBuf leak!"); + } + } finally { + ResourceLeakDetector.setLevel(oldLevel); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestByteBufAllocatorTestHelper.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestByteBufAllocatorTestHelper.java new file mode 100644 index 0000000..2fa40da --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestByteBufAllocatorTestHelper.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import io.netty.buffer.ByteBufAllocator; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import org.apache.zookeeper.ClientCnxnSocketNetty; +import org.apache.zookeeper.server.NettyServerCnxnFactory; + +/** + * Uses reflection to call package-private methods in Netty connection classes + * to set/clear the test ByteBufAllocator. + */ +public class TestByteBufAllocatorTestHelper { + + public static void setTestAllocator(ByteBufAllocator allocator) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method m1 = NettyServerCnxnFactory.class.getDeclaredMethod("setTestAllocator", ByteBufAllocator.class); + m1.setAccessible(true); + m1.invoke(null, allocator); + Method m2 = ClientCnxnSocketNetty.class.getDeclaredMethod("setTestAllocator", ByteBufAllocator.class); + m2.setAccessible(true); + m2.invoke(null, allocator); + } + + public static void clearTestAllocator() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method m1 = NettyServerCnxnFactory.class.getDeclaredMethod("clearTestAllocator"); + m1.setAccessible(true); + m1.invoke(null); + Method m2 = ClientCnxnSocketNetty.class.getDeclaredMethod("clearTestAllocator"); + m2.setAccessible(true); + m2.invoke(null); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestHammer.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestHammer.java new file mode 100644 index 0000000..162b9e4 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestHammer.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.Time; + +public class TestHammer implements VoidCallback { + + static int REPS = 50000; + public static void main(String[] args) { + long startTime = Time.currentElapsedTime(); + ZooKeeper zk = null; + try { + zk = ClientBase.createZKClient(args[0], 10000); + } catch (Exception e1) { + e1.printStackTrace(); + throw new RuntimeException(e1); + } + for (int i = 0; i < REPS; i++) { + try { + String name = zk.create("/testFile-", new byte[16], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); + zk.delete(name, -1, new TestHammer(), null); + } catch (Exception e) { + i--; + e.printStackTrace(); + } + } + System.out.println("creates/sec=" + (REPS * 1000 / (Time.currentElapsedTime() - startTime))); + } + + public void processResult(int rc, String path, Object ctx) { + // TODO Auto-generated method stub + + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestUtils.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestUtils.java new file mode 100644 index 0000000..a38d49e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TestUtils.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.fail; +import java.io.File; + +/** + * This class contains test utility methods + */ +public class TestUtils { + + /** + * deletes a folder recursively + * + * @param file + * folder to be deleted + * @param failOnError + * if true file deletion success is ensured + */ + public static boolean deleteFileRecursively( + File file, final boolean failOnError) { + if (file != null) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + int size = files.length; + for (int i = 0; i < size; i++) { + File f = files[i]; + boolean deleted = deleteFileRecursively(files[i], failOnError); + if (!deleted && failOnError) { + fail("file '" + f.getAbsolutePath() + "' deletion failed"); + } + } + } + return file.delete(); + } + return true; + } + + public static boolean deleteFileRecursively(File file) { + return deleteFileRecursively(file, false); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/TruncateTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TruncateTest.java new file mode 100644 index 0000000..089a764 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/TruncateTest.java @@ -0,0 +1,237 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.HashMap; +import java.util.Map; +import org.apache.jute.Record; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.PortAssignment; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.Request; +import org.apache.zookeeper.server.ServerCnxnFactory; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.apache.zookeeper.server.persistence.FileTxnLog; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer; +import org.apache.zookeeper.txn.SetDataTxn; +import org.apache.zookeeper.txn.TxnHeader; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TruncateTest extends ZKTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(TruncateTest.class); + File dataDir1, dataDir2, dataDir3; + + @Before + public void setUp() throws IOException { + dataDir1 = ClientBase.createTmpDir(); + dataDir2 = ClientBase.createTmpDir(); + dataDir3 = ClientBase.createTmpDir(); + } + + @After + public void tearDown() { + ClientBase.recursiveDelete(dataDir1); + ClientBase.recursiveDelete(dataDir2); + ClientBase.recursiveDelete(dataDir3); + } + + @Test + public void testTruncationStreamReset() throws Exception { + File tmpdir = ClientBase.createTmpDir(); + FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir); + ZKDatabase zkdb = new ZKDatabase(snaplog); + // make sure to snapshot, so that we have something there when + // truncateLog reloads the db + snaplog.save(zkdb.getDataTree(), zkdb.getSessionWithTimeOuts(), false); + + for (int i = 1; i <= 100; i++) { + append(zkdb, i); + } + + zkdb.truncateLog(1); + + append(zkdb, 200); + + zkdb.close(); + + // verify that the truncation and subsequent append were processed + // correctly + FileTxnLog txnlog = new FileTxnLog(new File(tmpdir, "version-2")); + TxnIterator iter = txnlog.read(1); + + TxnHeader hdr = iter.getHeader(); + Record txn = iter.getTxn(); + assertEquals(1, hdr.getZxid()); + assertTrue(txn instanceof SetDataTxn); + + iter.next(); + + hdr = iter.getHeader(); + txn = iter.getTxn(); + assertEquals(200, hdr.getZxid()); + assertTrue(txn instanceof SetDataTxn); + iter.close(); + ClientBase.recursiveDelete(tmpdir); + } + + @Test + public void testTruncationNullLog() throws Exception { + File tmpdir = ClientBase.createTmpDir(); + FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir); + ZKDatabase zkdb = new ZKDatabase(snaplog); + + for (int i = 1; i <= 100; i++) { + append(zkdb, i); + } + zkdb.close(); + File[] logs = snaplog.getDataDir().listFiles(); + for (int i = 0; i < logs.length; i++) { + LOG.debug("Deleting: {}", logs[i].getName()); + assertTrue("Failed to delete log file: " + logs[i].getName(), logs[i].delete()); + } + try { + assertThat("truncateLog() should return false if truncation fails instead of throwing exception", zkdb.truncateLog(1), is(false)); + } catch (NullPointerException npe) { + fail("This should not throw NPE!"); + } + + ClientBase.recursiveDelete(tmpdir); + } + + private void append(ZKDatabase zkdb, int i) throws IOException { + TxnHeader hdr = new TxnHeader(1, 1, i, 1, ZooDefs.OpCode.setData); + Record txn = new SetDataTxn("/foo" + i, new byte[0], 1); + Request req = new Request(0, 0, 0, hdr, txn, 0); + + zkdb.append(req); + zkdb.commit(); + } + + @Test + public void testTruncate() throws Exception { + // Prime the server that is going to come in late with 50 txns + String hostPort = "127.0.0.1:" + PortAssignment.unique(); + int maxCnxns = 100; + ServerCnxnFactory factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns); + ClientBase.startServerInstance(dataDir1, factory, hostPort, 1); + ClientBase.shutdownServerInstance(factory, hostPort); + + // standalone starts with 0 epoch while quorum starts with 1 + File origfile = new File(new File(dataDir1, "version-2"), "snapshot.0"); + File newfile = new File(new File(dataDir1, "version-2"), "snapshot.100000000"); + origfile.renameTo(newfile); + + factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns); + ClientBase.startServerInstance(dataDir1, factory, hostPort, 1); + + ZooKeeper zk = ClientBase.createZKClient(hostPort, 15000); + for (int i = 0; i < 50; i++) { + zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + + ZKDatabase zkDb; + { + ZooKeeperServer zs = factory.getZooKeeperServer(); + + zkDb = zs.getZKDatabase(); + } + factory.shutdown(); + try { + zkDb.close(); + } catch (IOException ie) { + LOG.warn("Error closing logs ", ie); + } + int tickTime = 2000; + int initLimit = 3; + int syncLimit = 3; + int connectToLearnerMasterLimit = 3; + + int port1 = PortAssignment.unique(); + int port2 = PortAssignment.unique(); + int port3 = PortAssignment.unique(); + + // Start up two of the quorum and add 10 txns + Map peers = new HashMap(); + peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port1))); + peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port2))); + peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port3))); + + QuorumPeer s2 = new QuorumPeer(peers, dataDir2, dataDir2, port2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + s2.start(); + QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + s3.start(); + zk = ClientBase.createZKClient("127.0.0.1:" + port2, 15000); + + for (int i = 0; i < 10; i++) { + zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } + zk.close(); + final ZooKeeper zk2 = ClientBase.createZKClient("127.0.0.1:" + port2, 15000); + zk2.getData("/9", false, new Stat()); + try { + zk2.getData("/10", false, new Stat()); + fail("Should have gotten an error"); + } catch (KeeperException.NoNodeException e) { + // this is what we want + } + QuorumPeer s1 = new QuorumPeer(peers, dataDir1, dataDir1, port1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit); + s1.start(); + ZooKeeper zk1 = ClientBase.createZKClient("127.0.0.1:" + port1, 15000); + zk1.getData("/9", false, new Stat()); + try { + // /10 wont work because the session expiration + // will match the zxid for /10 and so we wont + // actually truncate the zxid for /10 creation + // due to an artifact of switching the xid of the standalone + // /11 is the last entry in the log for the xid + // as a result /12 is the first of the truncated znodes to check for + zk1.getData("/12", false, new Stat()); + fail("Should have gotten an error"); + } catch (KeeperException.NoNodeException e) { + // this is what we want + } + zk1.close(); + QuorumBase.shutdown(s1); + QuorumBase.shutdown(s2); + QuorumBase.shutdown(s3); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/UnsupportedAddWatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/UnsupportedAddWatcherTest.java new file mode 100644 index 0000000..95b5569 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/UnsupportedAddWatcherTest.java @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.zookeeper.test; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Collections; +import org.apache.zookeeper.AddWatchMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.watch.IWatchManager; +import org.apache.zookeeper.server.watch.WatchManagerFactory; +import org.apache.zookeeper.server.watch.WatcherOrBitSet; +import org.apache.zookeeper.server.watch.WatchesPathReport; +import org.apache.zookeeper.server.watch.WatchesReport; +import org.apache.zookeeper.server.watch.WatchesSummary; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class UnsupportedAddWatcherTest extends ClientBase { + + public static class StubbedWatchManager implements IWatchManager { + @Override + public boolean addWatch(String path, Watcher watcher) { + return false; + } + + @Override + public boolean containsWatcher(String path, Watcher watcher) { + return false; + } + + @Override + public boolean removeWatcher(String path, Watcher watcher) { + return false; + } + + @Override + public void removeWatcher(Watcher watcher) { + // NOP + } + + @Override + public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type) { + return new WatcherOrBitSet(Collections.emptySet()); + } + + @Override + public WatcherOrBitSet triggerWatch(String path, Watcher.Event.EventType type, WatcherOrBitSet suppress) { + return new WatcherOrBitSet(Collections.emptySet()); + } + + @Override + public int size() { + return 0; + } + + @Override + public void shutdown() { + // NOP + } + + @Override + public WatchesSummary getWatchesSummary() { + return null; + } + + @Override + public WatchesReport getWatches() { + return null; + } + + @Override + public WatchesPathReport getWatchesByPath() { + return null; + } + + @Override + public void dumpWatches(PrintWriter pwriter, boolean byPath) { + // NOP + } + } + + @Before + public void setUp() throws Exception { + System.setProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME, StubbedWatchManager.class.getName()); + super.setUp(); + } + + @After + public void tearDown() throws Exception { + try { + super.tearDown(); + } finally { + System.clearProperty(WatchManagerFactory.ZOOKEEPER_WATCH_MANAGER_NAME); + } + } + + @Test(expected = KeeperException.MarshallingErrorException.class) + public void testBehavior() throws IOException, InterruptedException, KeeperException { + try (ZooKeeper zk = createClient(hostPort)) { + // the server will generate an exception as our custom watch manager doesn't implement + // the new version of addWatch() + zk.addWatch("/foo", event -> {}, AddWatchMode.PERSISTENT_RECURSIVE); + } + } +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatchEventWhenAutoResetTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatchEventWhenAutoResetTest.java new file mode 100644 index 0000000..005b525 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatchEventWhenAutoResetTest.java @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.test.ClientBase.CountdownWatcher; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WatchEventWhenAutoResetTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(WatchEventWhenAutoResetTest.class); + + // waiting time for expected condition + private static final int TIMEOUT = 30000; + private QuorumUtil qu; + private EventsWatcher watcher; + private ZooKeeper zk1, zk2; + + public static class EventsWatcher extends CountdownWatcher { + + private LinkedBlockingQueue dataEvents = new LinkedBlockingQueue(); + + @Override + public void process(WatchedEvent event) { + super.process(event); + try { + if (event.getType() != Event.EventType.None) { + dataEvents.put(event); + } + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt during EventsWatcher process"); + } + } + + public void assertEvent(long timeout, EventType eventType) { + try { + WatchedEvent event = dataEvents.poll(timeout, TimeUnit.MILLISECONDS); + assertNotNull("do not receive a " + eventType, event); + assertEquals(eventType, event.getType()); + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt during EventsWatcher assertEvent"); + } + } + + } + + private ZooKeeper createClient(QuorumUtil qu, int id, EventsWatcher watcher) throws IOException { + String hostPort = "127.0.0.1:" + qu.getPeer(id).clientPort; + ZooKeeper zk = new ZooKeeper(hostPort, TIMEOUT, watcher); + try { + watcher.waitForConnected(TIMEOUT); + } catch (InterruptedException e) { + // ignoring the interrupt + } catch (TimeoutException e) { + fail("can not connect to " + hostPort); + } + return zk; + } + + private ZooKeeper createClient(QuorumUtil qu, int id) throws IOException { + return createClient(qu, id, new EventsWatcher()); + } + + @Before + public void setUp() throws IOException { + System.setProperty("zookeeper.admin.enableServer", "false"); + + qu = new QuorumUtil(1); + qu.startAll(); + + watcher = new EventsWatcher(); + zk1 = createClient(qu, 1, watcher); + zk2 = createClient(qu, 2); + } + + @After + public void tearDown() throws InterruptedException { + if (zk1 != null) { + zk1.close(); + zk1 = null; + } + if (zk2 != null) { + zk2.close(); + zk2 = null; + } + if (watcher != null) { + watcher = null; + } + if (qu != null) { + qu.shutdownAll(); + qu = null; + } + } + + @Test + public void testNodeDataChanged() throws Exception { + String path = "/test-changed"; + zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + Stat stat1 = zk1.exists(path, watcher); + qu.shutdown(1); + zk2.setData(path, new byte[2], stat1.getVersion()); + qu.start(1); + watcher.waitForConnected(TIMEOUT); + watcher.assertEvent(TIMEOUT, EventType.NodeDataChanged); + } + + @Test + public void testNodeCreated() throws Exception { + String path = "/test1-created"; + + zk1.exists(path, watcher); + qu.shutdown(1); + zk2.create(path, new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + qu.start(1); + watcher.waitForConnected(TIMEOUT * 1000L); + watcher.assertEvent(TIMEOUT, EventType.NodeCreated); + } + + @Test + public void testNodeDeleted() throws Exception { + String path = "/test-deleted"; + + zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.getData(path, watcher, null); + qu.shutdown(1); + zk2.delete(path, -1); + qu.start(1); + watcher.waitForConnected(TIMEOUT * 1000L); + watcher.assertEvent(TIMEOUT, EventType.NodeDeleted); + + zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.exists(path, watcher); + qu.shutdown(1); + zk2.delete(path, -1); + qu.start(1); + watcher.waitForConnected(TIMEOUT * 1000L); + watcher.assertEvent(TIMEOUT, EventType.NodeDeleted); + + zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.getChildren(path, watcher); + qu.shutdown(1); + zk2.delete(path, -1); + qu.start(1); + watcher.waitForConnected(TIMEOUT * 1000L); + watcher.assertEvent(TIMEOUT, EventType.NodeDeleted); + } + + @Test + public void testNodeChildrenChanged() throws Exception { + String path = "/test-children-changed"; + + zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk1.getChildren(path, watcher); + qu.shutdown(1); + zk2.create(path + "/children-1", new byte[2], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + qu.start(1); + watcher.waitForConnected(TIMEOUT * 1000L); + watcher.assertEvent(TIMEOUT, EventType.NodeChildrenChanged); + } + +} + diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatchedEventTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatchedEventTest.java new file mode 100644 index 0000000..17d3510 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatchedEventTest.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; +import java.util.EnumSet; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.proto.WatcherEvent; +import org.junit.Test; + +public class WatchedEventTest extends ZKTestCase { + + @Test + public void testCreatingWatchedEvent() { + // EventWatch is a simple, immutable type, so all we need to do + // is make sure we can create all possible combinations of values. + + EnumSet allTypes = EnumSet.allOf(EventType.class); + EnumSet allStates = EnumSet.allOf(KeeperState.class); + WatchedEvent we; + + for (EventType et : allTypes) { + for (KeeperState ks : allStates) { + we = new WatchedEvent(et, ks, "blah"); + assertEquals(et, we.getType()); + assertEquals(ks, we.getState()); + assertEquals("blah", we.getPath()); + } + } + } + + @Test + public void testCreatingWatchedEventFromWrapper() { + // Make sure we can handle any type of correct wrapper + + EnumSet allTypes = EnumSet.allOf(EventType.class); + EnumSet allStates = EnumSet.allOf(KeeperState.class); + WatchedEvent we; + WatcherEvent wep; + + for (EventType et : allTypes) { + for (KeeperState ks : allStates) { + wep = new WatcherEvent(et.getIntValue(), ks.getIntValue(), "blah"); + we = new WatchedEvent(wep); + assertEquals(et, we.getType()); + assertEquals(ks, we.getState()); + assertEquals("blah", we.getPath()); + } + } + } + + @Test + public void testCreatingWatchedEventFromInvalidWrapper() { + // Make sure we can't convert from an invalid wrapper + + try { + WatcherEvent wep = new WatcherEvent(-2342, -252352, "foo"); + new WatchedEvent(wep); + fail("Was able to create WatchedEvent from bad wrapper"); + } catch (RuntimeException re) { + // we're good + } + } + + @Test + public void testConvertingToEventWrapper() { + WatchedEvent we = new WatchedEvent(EventType.NodeCreated, KeeperState.Expired, "blah"); + WatcherEvent wew = we.getWrapper(); + + assertEquals(EventType.NodeCreated.getIntValue(), wew.getType()); + assertEquals(KeeperState.Expired.getIntValue(), wew.getState()); + assertEquals("blah", wew.getPath()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherFuncTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherFuncTest.java new file mode 100644 index 0000000..5a274b5 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherFuncTest.java @@ -0,0 +1,465 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.Watcher.Event.KeeperState; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.junit.Test; + +public class WatcherFuncTest extends ClientBase { + + private static class SimpleWatcher implements Watcher { + + private LinkedBlockingQueue events = new LinkedBlockingQueue(); + private CountDownLatch latch; + + public SimpleWatcher(CountDownLatch latch) { + this.latch = latch; + } + + public void process(WatchedEvent event) { + if (event.getState() == KeeperState.SyncConnected) { + if (latch != null) { + latch.countDown(); + } + } + + if (event.getType() == EventType.None) { + return; + } + try { + events.put(event); + } catch (InterruptedException e) { + assertTrue("interruption unexpected", false); + } + } + public void verify(List expected) throws InterruptedException { + WatchedEvent event; + int count = 0; + while (count < expected.size() && (event = events.poll(30, TimeUnit.SECONDS)) != null) { + assertEquals(expected.get(count), event.getType()); + count++; + } + assertEquals(expected.size(), count); + events.clear(); + } + + } + + private SimpleWatcher client_dwatch; + private volatile CountDownLatch client_latch; + private ZooKeeper client; + private SimpleWatcher lsnr_dwatch; + private volatile CountDownLatch lsnr_latch; + private ZooKeeper lsnr; + + private List expected; + + @Override + public void setUp() throws Exception { + super.setUp(); + + client_latch = new CountDownLatch(1); + client_dwatch = new SimpleWatcher(client_latch); + client = createClient(client_dwatch, client_latch); + + lsnr_latch = new CountDownLatch(1); + lsnr_dwatch = new SimpleWatcher(lsnr_latch); + lsnr = createClient(lsnr_dwatch, lsnr_latch); + + expected = new ArrayList(); + } + + @Override + public void tearDown() throws Exception { + client.close(); + lsnr.close(); + super.tearDown(); + } + + protected ZooKeeper createClient(Watcher watcher, CountDownLatch latch) throws IOException, InterruptedException { + ZooKeeper zk = new ZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher); + if (!latch.await(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)) { + fail("Unable to connect to server"); + } + return zk; + } + + private void verify() throws InterruptedException { + lsnr_dwatch.verify(expected); + expected.clear(); + } + + @Test + public void testExistsSync() throws IOException, InterruptedException, KeeperException { + assertNull(lsnr.exists("/foo", true)); + assertNull(lsnr.exists("/foo/bar", true)); + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeCreated); + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeCreated); + + verify(); + + assertNotNull(lsnr.exists("/foo", true)); + assertNotNull(lsnr.exists("/foo/bar", true)); + + try { + assertNull(lsnr.exists("/car", true)); + client.setData("/car", "missing".getBytes(), -1); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/car", e.getPath()); + } + + try { + assertNull(lsnr.exists("/foo/car", true)); + client.setData("/foo/car", "missing".getBytes(), -1); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo/car", e.getPath()); + } + + client.setData("/foo", "parent".getBytes(), -1); + expected.add(EventType.NodeDataChanged); + client.setData("/foo/bar", "child".getBytes(), -1); + expected.add(EventType.NodeDataChanged); + + verify(); + + assertNotNull(lsnr.exists("/foo", true)); + assertNotNull(lsnr.exists("/foo/bar", true)); + + client.delete("/foo/bar", -1); + expected.add(EventType.NodeDeleted); + client.delete("/foo", -1); + expected.add(EventType.NodeDeleted); + + verify(); + } + + @Test + public void testGetDataSync() throws IOException, InterruptedException, KeeperException { + try { + lsnr.getData("/foo", true, null); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo", e.getPath()); + } + try { + lsnr.getData("/foo/bar", true, null); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull(lsnr.getData("/foo", true, null)); + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull(lsnr.getData("/foo/bar", true, null)); + + client.setData("/foo", "parent".getBytes(), -1); + expected.add(EventType.NodeDataChanged); + client.setData("/foo/bar", "child".getBytes(), -1); + expected.add(EventType.NodeDataChanged); + + verify(); + + assertNotNull(lsnr.getData("/foo", true, null)); + assertNotNull(lsnr.getData("/foo/bar", true, null)); + + client.delete("/foo/bar", -1); + expected.add(EventType.NodeDeleted); + client.delete("/foo", -1); + expected.add(EventType.NodeDeleted); + + verify(); + } + + @Test + public void testGetChildrenSync() throws IOException, InterruptedException, KeeperException { + try { + lsnr.getChildren("/foo", true); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo", e.getPath()); + } + try { + lsnr.getChildren("/foo/bar", true); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull(lsnr.getChildren("/foo", true)); + + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeChildrenChanged); // /foo + assertNotNull(lsnr.getChildren("/foo/bar", true)); + + client.setData("/foo", "parent".getBytes(), -1); + client.setData("/foo/bar", "child".getBytes(), -1); + + assertNotNull(lsnr.exists("/foo", true)); + + assertNotNull(lsnr.getChildren("/foo", true)); + assertNotNull(lsnr.getChildren("/foo/bar", true)); + + client.delete("/foo/bar", -1); + expected.add(EventType.NodeDeleted); // /foo/bar childwatch + expected.add(EventType.NodeChildrenChanged); // /foo + client.delete("/foo", -1); + expected.add(EventType.NodeDeleted); + + verify(); + } + + @Test + public void testExistsSyncWObj() throws IOException, InterruptedException, KeeperException { + SimpleWatcher w1 = new SimpleWatcher(null); + SimpleWatcher w2 = new SimpleWatcher(null); + SimpleWatcher w3 = new SimpleWatcher(null); + SimpleWatcher w4 = new SimpleWatcher(null); + + List e2 = new ArrayList(); + + assertNull(lsnr.exists("/foo", true)); + assertNull(lsnr.exists("/foo", w1)); + + assertNull(lsnr.exists("/foo/bar", w2)); + assertNull(lsnr.exists("/foo/bar", w3)); + assertNull(lsnr.exists("/foo/bar", w3)); + assertNull(lsnr.exists("/foo/bar", w4)); + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeCreated); + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + e2.add(EventType.NodeCreated); + + lsnr_dwatch.verify(expected); + w1.verify(expected); + w2.verify(e2); + w3.verify(e2); + w4.verify(e2); + expected.clear(); + e2.clear(); + + // default not registered + assertNotNull(lsnr.exists("/foo", w1)); + + assertNotNull(lsnr.exists("/foo/bar", w2)); + assertNotNull(lsnr.exists("/foo/bar", w3)); + assertNotNull(lsnr.exists("/foo/bar", w4)); + assertNotNull(lsnr.exists("/foo/bar", w4)); + + client.setData("/foo", "parent".getBytes(), -1); + expected.add(EventType.NodeDataChanged); + client.setData("/foo/bar", "child".getBytes(), -1); + e2.add(EventType.NodeDataChanged); + + lsnr_dwatch.verify(new ArrayList()); // not reg so should = 0 + w1.verify(expected); + w2.verify(e2); + w3.verify(e2); + w4.verify(e2); + expected.clear(); + e2.clear(); + + assertNotNull(lsnr.exists("/foo", true)); + assertNotNull(lsnr.exists("/foo", w1)); + assertNotNull(lsnr.exists("/foo", w1)); + + assertNotNull(lsnr.exists("/foo/bar", w2)); + assertNotNull(lsnr.exists("/foo/bar", w2)); + assertNotNull(lsnr.exists("/foo/bar", w3)); + assertNotNull(lsnr.exists("/foo/bar", w4)); + + client.delete("/foo/bar", -1); + expected.add(EventType.NodeDeleted); + client.delete("/foo", -1); + e2.add(EventType.NodeDeleted); + + lsnr_dwatch.verify(expected); + w1.verify(expected); + w2.verify(e2); + w3.verify(e2); + w4.verify(e2); + expected.clear(); + e2.clear(); + + } + + @Test + public void testGetDataSyncWObj() throws IOException, InterruptedException, KeeperException { + SimpleWatcher w1 = new SimpleWatcher(null); + SimpleWatcher w2 = new SimpleWatcher(null); + SimpleWatcher w3 = new SimpleWatcher(null); + SimpleWatcher w4 = new SimpleWatcher(null); + + List e2 = new ArrayList(); + + try { + lsnr.getData("/foo", w1, null); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo", e.getPath()); + } + try { + lsnr.getData("/foo/bar", w2, null); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull(lsnr.getData("/foo", true, null)); + assertNotNull(lsnr.getData("/foo", w1, null)); + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull(lsnr.getData("/foo/bar", w2, null)); + assertNotNull(lsnr.getData("/foo/bar", w3, null)); + assertNotNull(lsnr.getData("/foo/bar", w4, null)); + assertNotNull(lsnr.getData("/foo/bar", w4, null)); + + client.setData("/foo", "parent".getBytes(), -1); + expected.add(EventType.NodeDataChanged); + client.setData("/foo/bar", "child".getBytes(), -1); + e2.add(EventType.NodeDataChanged); + + lsnr_dwatch.verify(expected); + w1.verify(expected); + w2.verify(e2); + w3.verify(e2); + w4.verify(e2); + expected.clear(); + e2.clear(); + + assertNotNull(lsnr.getData("/foo", true, null)); + assertNotNull(lsnr.getData("/foo", w1, null)); + assertNotNull(lsnr.getData("/foo/bar", w2, null)); + assertNotNull(lsnr.getData("/foo/bar", w3, null)); + assertNotNull(lsnr.getData("/foo/bar", w3, null)); + assertNotNull(lsnr.getData("/foo/bar", w4, null)); + + client.delete("/foo/bar", -1); + expected.add(EventType.NodeDeleted); + client.delete("/foo", -1); + e2.add(EventType.NodeDeleted); + + lsnr_dwatch.verify(expected); + w1.verify(expected); + w2.verify(e2); + w3.verify(e2); + w4.verify(e2); + expected.clear(); + e2.clear(); + } + + @Test + public void testGetChildrenSyncWObj() throws IOException, InterruptedException, KeeperException { + SimpleWatcher w1 = new SimpleWatcher(null); + SimpleWatcher w2 = new SimpleWatcher(null); + SimpleWatcher w3 = new SimpleWatcher(null); + SimpleWatcher w4 = new SimpleWatcher(null); + + List e2 = new ArrayList(); + + try { + lsnr.getChildren("/foo", true); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo", e.getPath()); + } + try { + lsnr.getChildren("/foo/bar", true); + fail(); + } catch (KeeperException e) { + assertEquals(KeeperException.Code.NONODE, e.code()); + assertEquals("/foo/bar", e.getPath()); + } + + client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + assertNotNull(lsnr.getChildren("/foo", true)); + assertNotNull(lsnr.getChildren("/foo", w1)); + + client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + expected.add(EventType.NodeChildrenChanged); // /foo + assertNotNull(lsnr.getChildren("/foo/bar", w2)); + assertNotNull(lsnr.getChildren("/foo/bar", w2)); + assertNotNull(lsnr.getChildren("/foo/bar", w3)); + assertNotNull(lsnr.getChildren("/foo/bar", w4)); + + client.setData("/foo", "parent".getBytes(), -1); + client.setData("/foo/bar", "child".getBytes(), -1); + + assertNotNull(lsnr.exists("/foo", true)); + assertNotNull(lsnr.exists("/foo", w1)); + assertNotNull(lsnr.exists("/foo", true)); + assertNotNull(lsnr.exists("/foo", w1)); + + assertNotNull(lsnr.getChildren("/foo", true)); + assertNotNull(lsnr.getChildren("/foo", w1)); + assertNotNull(lsnr.getChildren("/foo/bar", w2)); + assertNotNull(lsnr.getChildren("/foo/bar", w3)); + assertNotNull(lsnr.getChildren("/foo/bar", w4)); + assertNotNull(lsnr.getChildren("/foo/bar", w4)); + + client.delete("/foo/bar", -1); + e2.add(EventType.NodeDeleted); // /foo/bar childwatch + expected.add(EventType.NodeChildrenChanged); // /foo + client.delete("/foo", -1); + expected.add(EventType.NodeDeleted); + + lsnr_dwatch.verify(expected); + w1.verify(expected); + w2.verify(e2); + w3.verify(e2); + w4.verify(e2); + expected.clear(); + e2.clear(); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherTest.java new file mode 100644 index 0000000..6f419ed --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherTest.java @@ -0,0 +1,451 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.zookeeper.AsyncCallback.StatCallback; +import org.apache.zookeeper.AsyncCallback.VoidCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.TestableZooKeeper; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.client.ZKClientConfig; +import org.apache.zookeeper.data.Stat; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WatcherTest extends ClientBase { + + protected static final Logger LOG = LoggerFactory.getLogger(WatcherTest.class); + + private long timeOfLastWatcherInvocation; + + private static final class MyStatCallback implements StatCallback { + + int rc; + public void processResult(int rc, String path, Object ctx, Stat stat) { + ((int[]) ctx)[0]++; + this.rc = rc; + } + + } + + private class MyWatcher extends CountdownWatcher { + + LinkedBlockingQueue events = new LinkedBlockingQueue(); + + public void process(WatchedEvent event) { + super.process(event); + if (event.getType() != Event.EventType.None) { + timeOfLastWatcherInvocation = System.currentTimeMillis(); + try { + events.put(event); + } catch (InterruptedException e) { + LOG.warn("ignoring interrupt during event.put"); + } + } + } + + } + + @Before + public void setUp() throws Exception { + super.setUp(); + // Reset to default value since some test cases set this to true. + // Needed for JDK7 since unit test can run is random order + System.setProperty(ZKClientConfig.DISABLE_AUTO_WATCH_RESET, "false"); + } + + /** + * Verify that we get all of the events we expect to get. This particular + * case verifies that we see all of the data events on a particular node. + * There was a bug (ZOOKEEPER-137) that resulted in events being dropped + * in some cases (timing). + * + * @throws IOException + * @throws InterruptedException + * @throws KeeperException + */ + @Test + public void testWatcherCorrectness() throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = null; + try { + MyWatcher watcher = new MyWatcher(); + zk = createClient(watcher, hostPort); + + StatCallback scb = new StatCallback() { + public void processResult(int rc, String path, Object ctx, Stat stat) { + // don't do anything + } + }; + VoidCallback vcb = new VoidCallback() { + public void processResult(int rc, String path, Object ctx) { + // don't do anything + } + }; + + String[] names = new String[10]; + for (int i = 0; i < names.length; i++) { + String name = zk.create("/tc-", "initialvalue".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); + names[i] = name; + + Stat stat = new Stat(); + zk.getData(name, watcher, stat); + zk.setData(name, "new".getBytes(), stat.getVersion(), scb, null); + stat = zk.exists(name, watcher); + zk.delete(name, stat.getVersion(), vcb, null); + } + + for (int i = 0; i < names.length; i++) { + String name = names[i]; + WatchedEvent event = watcher.events.poll(10, TimeUnit.SECONDS); + assertEquals(name, event.getPath()); + assertEquals(Event.EventType.NodeDataChanged, event.getType()); + assertEquals(Event.KeeperState.SyncConnected, event.getState()); + event = watcher.events.poll(10, TimeUnit.SECONDS); + assertEquals(name, event.getPath()); + assertEquals(Event.EventType.NodeDeleted, event.getType()); + assertEquals(Event.KeeperState.SyncConnected, event.getState()); + } + } finally { + if (zk != null) { + zk.close(); + } + } + } + + @Test + public void testWatcherDisconnectOnClose() throws IOException, InterruptedException, KeeperException { + ZooKeeper zk = null; + try { + final BlockingQueue queue = new LinkedBlockingQueue<>(); + + MyWatcher connWatcher = new MyWatcher(); + + Watcher watcher = event -> { + try { + queue.put(event); + } catch (InterruptedException e) { + // Oh well, never mind + } + }; + + zk = createClient(connWatcher, hostPort); + + StatCallback scb = new StatCallback() { + public void processResult(int rc, String path, Object ctx, Stat stat) { + // don't do anything + } + }; + + // Register a watch on the node + zk.exists("/missing", watcher, scb, null); + + // Close the client without changing the node + zk.close(); + + WatchedEvent event = queue.poll(10, TimeUnit.SECONDS); + + assertNotNull("No watch event was received after closing the Zookeeper client. A 'Closed' event should have occurred", event); + assertEquals("Closed events are not generated by the server, and so should have a type of 'None'", Event.EventType.None, event.getType()); + assertEquals("A 'Closed' event was expected as the Zookeeper client was closed without altering the node it was watching", Event.KeeperState.Closed, event.getState()); + } finally { + if (zk != null) { + zk.close(); + } + } + + } + + @Test + public void testWatcherCount() throws IOException, InterruptedException, KeeperException { + ZooKeeper zk1 = null, zk2 = null; + try { + MyWatcher w1 = new MyWatcher(); + zk1 = createClient(w1, hostPort); + + MyWatcher w2 = new MyWatcher(); + zk2 = createClient(w2, hostPort); + + Stat stat = new Stat(); + zk1.create("/watch-count-test", "value".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + zk1.create("/watch-count-test-2", "value".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + + zk1.getData("/watch-count-test", w1, stat); + zk1.getData("/watch-count-test-2", w1, stat); + zk2.getData("/watch-count-test", w2, stat); + + assertEquals(serverFactory.getZooKeeperServer().getZKDatabase().getDataTree().getWatchCount(), 3); + + } finally { + if (zk1 != null) { + zk1.close(); + } + if (zk2 != null) { + zk2.close(); + } + } + + } + + static final int COUNT = 100; + /** + * This test checks that watches for pending requests do not get triggered, + * but watches set by previous requests do. + * + * @throws Exception + */ + @Test + public void testWatchAutoResetWithPending() throws Exception { + MyWatcher[] watches = new MyWatcher[COUNT]; + MyStatCallback[] cbs = new MyStatCallback[COUNT]; + MyWatcher watcher = new MyWatcher(); + int[] count = new int[1]; + TestableZooKeeper zk = createClient(watcher, hostPort, 6000); + ZooKeeper zk2 = createClient(watcher, hostPort, 5000); + zk2.create("/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + for (int i = 0; i < COUNT / 2; i++) { + watches[i] = new MyWatcher(); + cbs[i] = new MyStatCallback(); + zk.exists("/test", watches[i], cbs[i], count); + } + zk.exists("/test", false); + assertTrue("Failed to pause the connection!", zk.pauseCnxn(3000)); + zk2.close(); + stopServer(); + watches[0].waitForDisconnected(60000); + for (int i = COUNT / 2; i < COUNT; i++) { + watches[i] = new MyWatcher(); + cbs[i] = new MyStatCallback(); + zk.exists("/test", watches[i], cbs[i], count); + } + startServer(); + watches[COUNT / 2 - 1].waitForConnected(60000); + assertEquals(null, zk.exists("/test", false)); + waitForAllWatchers(); + for (int i = 0; i < COUNT / 2; i++) { + assertEquals("For " + i, 1, watches[i].events.size()); + } + for (int i = COUNT / 2; i < COUNT; i++) { + if (cbs[i].rc == 0) { + assertEquals("For " + i, 1, watches[i].events.size()); + } else { + assertEquals("For " + i, 0, watches[i].events.size()); + } + } + assertEquals(COUNT, count[0]); + zk.close(); + } + + /** + * Wait until no watcher has been fired in the last second to ensure that all watches + * that are waiting to be fired have been fired + * @throws Exception + */ + private void waitForAllWatchers() throws Exception { + timeOfLastWatcherInvocation = System.currentTimeMillis(); + while (System.currentTimeMillis() - timeOfLastWatcherInvocation < 1000) { + Thread.sleep(1000); + } + } + + final int TIMEOUT = 5000; + + @Test + public void testWatcherAutoResetWithGlobal() throws Exception { + ZooKeeper zk = null; + MyWatcher watcher = new MyWatcher(); + zk = createClient(watcher, hostPort, TIMEOUT); + testWatcherAutoReset(zk, watcher, watcher); + zk.close(); + } + + @Test + public void testWatcherAutoResetWithLocal() throws Exception { + ZooKeeper zk = null; + MyWatcher watcher = new MyWatcher(); + zk = createClient(watcher, hostPort, TIMEOUT); + testWatcherAutoReset(zk, watcher, new MyWatcher()); + zk.close(); + } + + @Test + public void testWatcherAutoResetDisabledWithGlobal() throws Exception { + /** + * When ZooKeeper is created this property will get used. + */ + System.setProperty(ZKClientConfig.DISABLE_AUTO_WATCH_RESET, "true"); + testWatcherAutoResetWithGlobal(); + } + + @Test + public void testWatcherAutoResetDisabledWithLocal() throws Exception { + System.setProperty(ZKClientConfig.DISABLE_AUTO_WATCH_RESET, "true"); + testWatcherAutoResetWithLocal(); + } + + private void testWatcherAutoReset(ZooKeeper zk, MyWatcher globalWatcher, MyWatcher localWatcher) throws Exception { + boolean isGlobal = (localWatcher == globalWatcher); + // First test to see if the watch survives across reconnects + zk.create("/watchtest", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/watchtest/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + if (isGlobal) { + zk.getChildren("/watchtest", true); + zk.getData("/watchtest/child", true, new Stat()); + zk.exists("/watchtest/child2", true); + } else { + zk.getChildren("/watchtest", localWatcher); + zk.getData("/watchtest/child", localWatcher, new Stat()); + zk.exists("/watchtest/child2", localWatcher); + } + + assertTrue(localWatcher.events.isEmpty()); + + stopServer(); + globalWatcher.waitForDisconnected(3000); + localWatcher.waitForDisconnected(500); + startServer(); + globalWatcher.waitForConnected(3000); + boolean disableAutoWatchReset = zk.getClientConfig().getBoolean(ZKClientConfig.DISABLE_AUTO_WATCH_RESET); + if (!isGlobal && !disableAutoWatchReset) { + localWatcher.waitForConnected(500); + } + + assertTrue(localWatcher.events.isEmpty()); + zk.setData("/watchtest/child", new byte[1], -1); + zk.create("/watchtest/child2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + WatchedEvent e; + if (!disableAutoWatchReset) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(e.getPath(), EventType.NodeDataChanged, e.getType()); + assertEquals("/watchtest/child", e.getPath()); + } else { + // we'll catch this later if it does happen after timeout, so + // why waste the time on poll + } + + if (!disableAutoWatchReset) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + // The create will trigger the get children and the exist + // watches + assertEquals(EventType.NodeCreated, e.getType()); + assertEquals("/watchtest/child2", e.getPath()); + } else { + // we'll catch this later if it does happen after timeout, so + // why waste the time on poll + } + + if (!disableAutoWatchReset) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/watchtest", e.getPath()); + } else { + // we'll catch this later if it does happen after timeout, so + // why waste the time on poll + } + + assertTrue(localWatcher.events.isEmpty()); // ensure no late arrivals + stopServer(); + globalWatcher.waitForDisconnected(TIMEOUT); + try { + try { + localWatcher.waitForDisconnected(500); + if (!isGlobal && !disableAutoWatchReset) { + fail("Got an event when I shouldn't have"); + } + } catch (TimeoutException toe) { + if (disableAutoWatchReset) { + fail("Didn't get an event when I should have"); + } + // Else what we are expecting since there are no outstanding watches + } + } catch (Exception e1) { + LOG.error("bad", e1); + throw new RuntimeException(e1); + } + startServer(); + globalWatcher.waitForConnected(TIMEOUT); + + if (isGlobal) { + zk.getChildren("/watchtest", true); + zk.getData("/watchtest/child", true, new Stat()); + zk.exists("/watchtest/child2", true); + } else { + zk.getChildren("/watchtest", localWatcher); + zk.getData("/watchtest/child", localWatcher, new Stat()); + zk.exists("/watchtest/child2", localWatcher); + } + + // Do trigger an event to make sure that we do not get + // it later + zk.delete("/watchtest/child2", -1); + + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(EventType.NodeDeleted, e.getType()); + assertEquals("/watchtest/child2", e.getPath()); + + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(EventType.NodeChildrenChanged, e.getType()); + assertEquals("/watchtest", e.getPath()); + + assertTrue(localWatcher.events.isEmpty()); + + stopServer(); + globalWatcher.waitForDisconnected(TIMEOUT); + localWatcher.waitForDisconnected(500); + startServer(); + globalWatcher.waitForConnected(TIMEOUT); + if (!isGlobal && !disableAutoWatchReset) { + localWatcher.waitForConnected(500); + } + + zk.delete("/watchtest/child", -1); + zk.delete("/watchtest", -1); + + if (!disableAutoWatchReset) { + e = localWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(EventType.NodeDeleted, e.getType()); + assertEquals("/watchtest/child", e.getPath()); + } else { + // we'll catch this later if it does happen after timeout, so + // why waste the time on poll + } + + // Make sure nothing is straggling! + Thread.sleep(1000); + assertTrue(localWatcher.events.isEmpty()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/X509AuthTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/X509AuthTest.java new file mode 100644 index 0000000..62dabcc --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/X509AuthTest.java @@ -0,0 +1,284 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import java.math.BigInteger; +import java.net.Socket; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Principal; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.SignatureException; +import java.security.cert.CertificateEncodingException; +import java.security.cert.CertificateException; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.X509Certificate; +import java.util.Arrays; +import java.util.Date; +import java.util.Set; +import javax.net.ssl.X509KeyManager; +import javax.net.ssl.X509TrustManager; +import javax.security.auth.x500.X500Principal; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.server.MockServerCnxn; +import org.apache.zookeeper.server.auth.X509AuthenticationProvider; +import org.junit.Before; +import org.junit.Test; + +public class X509AuthTest extends ZKTestCase { + + private static TestCertificate clientCert; + private static TestCertificate superCert; + private static TestCertificate unknownCert; + + @Before + public void setUp() { + System.setProperty("zookeeper.X509AuthenticationProvider.superUser", "CN=SUPER"); + System.setProperty("zookeeper.ssl.keyManager", "org.apache.zookeeper.test.X509AuthTest.TestKeyManager"); + System.setProperty("zookeeper.ssl.trustManager", "org.apache.zookeeper.test.X509AuthTest.TestTrustManager"); + + clientCert = new TestCertificate("CLIENT"); + superCert = new TestCertificate("SUPER"); + unknownCert = new TestCertificate("UNKNOWN"); + } + + @Test + public void testTrustedAuth() { + X509AuthenticationProvider provider = createProvider(clientCert); + MockServerCnxn cnxn = new MockServerCnxn(); + cnxn.clientChain = new X509Certificate[]{clientCert}; + assertEquals(KeeperException.Code.OK, provider.handleAuthentication(cnxn, null)); + } + + @Test + public void testSuperAuth() { + X509AuthenticationProvider provider = createProvider(superCert); + MockServerCnxn cnxn = new MockServerCnxn(); + cnxn.clientChain = new X509Certificate[]{superCert}; + assertEquals(KeeperException.Code.OK, provider.handleAuthentication(cnxn, null)); + assertEquals("super", cnxn.getAuthInfo().get(0).getScheme()); + } + + @Test + public void testUntrustedAuth() { + X509AuthenticationProvider provider = createProvider(clientCert); + MockServerCnxn cnxn = new MockServerCnxn(); + cnxn.clientChain = new X509Certificate[]{unknownCert}; + assertEquals(KeeperException.Code.AUTHFAILED, provider.handleAuthentication(cnxn, null)); + } + + private static class TestPublicKey implements PublicKey { + + private static final long serialVersionUID = 1L; + @Override + public String getAlgorithm() { + return null; + } + @Override + public String getFormat() { + return null; + } + @Override + public byte[] getEncoded() { + return null; + } + + } + + private static class TestCertificate extends X509Certificate { + + private byte[] encoded; + private X500Principal principal; + private PublicKey publicKey; + public TestCertificate(String name) { + encoded = name.getBytes(); + principal = new X500Principal("CN=" + name); + publicKey = new TestPublicKey(); + } + @Override + public boolean hasUnsupportedCriticalExtension() { + return false; + } + @Override + public Set getCriticalExtensionOIDs() { + return null; + } + @Override + public Set getNonCriticalExtensionOIDs() { + return null; + } + @Override + public byte[] getExtensionValue(String oid) { + return null; + } + @Override + public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException { + } + @Override + public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException { + } + @Override + public int getVersion() { + return 0; + } + @Override + public BigInteger getSerialNumber() { + return null; + } + @Override + public Principal getIssuerDN() { + return null; + } + @Override + public Principal getSubjectDN() { + return null; + } + @Override + public Date getNotBefore() { + return null; + } + @Override + public Date getNotAfter() { + return null; + } + @Override + public byte[] getTBSCertificate() throws CertificateEncodingException { + return null; + } + @Override + public byte[] getSignature() { + return null; + } + @Override + public String getSigAlgName() { + return null; + } + @Override + public String getSigAlgOID() { + return null; + } + @Override + public byte[] getSigAlgParams() { + return null; + } + @Override + public boolean[] getIssuerUniqueID() { + return null; + } + @Override + public boolean[] getSubjectUniqueID() { + return null; + } + @Override + public boolean[] getKeyUsage() { + return null; + } + @Override + public int getBasicConstraints() { + return 0; + } + @Override + public byte[] getEncoded() throws CertificateEncodingException { + return encoded; + } + @Override + public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { + } + @Override + public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { + } + @Override + public String toString() { + return null; + } + @Override + public PublicKey getPublicKey() { + return publicKey; + } + @Override + public X500Principal getSubjectX500Principal() { + return principal; + } + + } + + public static class TestKeyManager implements X509KeyManager { + + @Override + public String chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket) { + return null; + } + @Override + public String chooseServerAlias(String keyType, Principal[] issuers, Socket socket) { + return null; + } + @Override + public X509Certificate[] getCertificateChain(String alias) { + return null; + } + @Override + public String[] getClientAliases(String keyType, Principal[] issuers) { + return null; + } + @Override + public PrivateKey getPrivateKey(String alias) { + return null; + } + @Override + public String[] getServerAliases(String keyType, Principal[] issuers) { + return null; + } + + } + + public static class TestTrustManager implements X509TrustManager { + + X509Certificate cert; + public TestTrustManager(X509Certificate testCert) { + cert = testCert; + } + @Override + public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { + if (!Arrays.equals(cert.getEncoded(), chain[0].getEncoded())) { + throw new CertificateException("Client cert not trusted"); + } + } + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { + if (!Arrays.equals(cert.getEncoded(), chain[0].getEncoded())) { + throw new CertificateException("Server cert not trusted"); + } + } + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + } + + protected X509AuthenticationProvider createProvider(X509Certificate trustedCert) { + return new X509AuthenticationProvider(new TestTrustManager(trustedCert), new TestKeyManager()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZkDatabaseCorruptionTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZkDatabaseCorruptionTest.java new file mode 100644 index 0000000..133ed39 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZkDatabaseCorruptionTest.java @@ -0,0 +1,199 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.Arrays; +import org.apache.zookeeper.AsyncCallback; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.server.SyncRequestProcessor; +import org.apache.zookeeper.server.ZKDatabase; +import org.apache.zookeeper.server.persistence.FileTxnSnapLog; +import org.apache.zookeeper.server.quorum.QuorumPeer; +import org.apache.zookeeper.server.quorum.QuorumPeer.ServerState; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ZkDatabaseCorruptionTest extends ZKTestCase { + + protected static final Logger LOG = LoggerFactory.getLogger(ZkDatabaseCorruptionTest.class); + public static final long CONNECTION_TIMEOUT = ClientTest.CONNECTION_TIMEOUT; + + private final QuorumBase qb = new QuorumBase(); + + @Before + public void setUp() throws Exception { + LOG.info("STARTING quorum {}", getClass().getName()); + qb.setUp(); + } + + @After + public void tearDown() throws Exception { + LOG.info("STOPPING quorum {}", getClass().getName()); + } + + private void corruptFile(File f) throws IOException { + RandomAccessFile outFile = new RandomAccessFile(f, "rw"); + outFile.write("fail servers".getBytes()); + outFile.close(); + } + + private void corruptAllSnapshots(File snapDir) throws IOException { + File[] listFiles = snapDir.listFiles(); + for (File f : listFiles) { + if (f.getName().startsWith("snapshot")) { + corruptFile(f); + } + } + } + + private class NoopStringCallback implements AsyncCallback.StringCallback { + + @Override + public void processResult(int rc, String path, Object ctx, String name) { + } + + } + + @Test + public void testCorruption() throws Exception { + ClientBase.waitForServerUp(qb.hostPort, 10000); + ClientBase.waitForServerUp(qb.hostPort, 10000); + ZooKeeper zk = ClientBase.createZKClient(qb.hostPort, 10000); + SyncRequestProcessor.setSnapCount(100); + for (int i = 0; i < 2000; i++) { + zk.create("/0-" + + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new NoopStringCallback(), null); + } + zk.close(); + + long leaderSid = 1; + QuorumPeer leader = null; + //find out who is the leader and kill it + for (QuorumPeer quorumPeer : Arrays.asList(qb.s1, qb.s2, qb.s3, qb.s4, qb.s5)) { + if (quorumPeer.getPeerState() == ServerState.LEADING) { + leader = quorumPeer; + break; + } + ++leaderSid; + } + + assertNotNull("Cannot find the leader.", leader); + leader.shutdown(); + + // now corrupt the leader's database + FileTxnSnapLog snapLog = leader.getTxnFactory(); + File snapDir = snapLog.getSnapDir(); + //corrupt all the snapshot in the snapshot directory + corruptAllSnapshots(snapDir); + qb.shutdownServers(); + qb.setupServers(); + + if (leaderSid != 1) { + qb.s1.start(); + } else { + leader = qb.s1; + } + if (leaderSid != 2) { + qb.s2.start(); + } else { + leader = qb.s2; + } + if (leaderSid != 3) { + qb.s3.start(); + } else { + leader = qb.s3; + } + if (leaderSid != 4) { + qb.s4.start(); + } else { + leader = qb.s4; + } + if (leaderSid != 5) { + qb.s5.start(); + } else { + leader = qb.s5; + } + + try { + leader.start(); + assertTrue(false); + } catch (RuntimeException re) { + LOG.info("Got an error: expected", re); + } + //wait for servers to be up + String[] list = qb.hostPort.split(","); + for (int i = 0; i < 5; i++) { + if (leaderSid != (i + 1)) { + String hp = list[i]; + assertTrue("waiting for server up", ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT)); + LOG.info("{} is accepting client connections", hp); + } else { + LOG.info("Skipping the leader"); + } + } + + zk = qb.createClient(); + SyncRequestProcessor.setSnapCount(100); + for (int i = 2000; i < 4000; i++) { + zk.create("/0-" + + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new NoopStringCallback(), null); + } + zk.close(); + + if (leaderSid != 1) { + QuorumBase.shutdown(qb.s1); + } + if (leaderSid != 2) { + QuorumBase.shutdown(qb.s2); + } + if (leaderSid != 3) { + QuorumBase.shutdown(qb.s3); + } + if (leaderSid != 4) { + QuorumBase.shutdown(qb.s4); + } + if (leaderSid != 5) { + QuorumBase.shutdown(qb.s5); + } + } + + @Test + public void testAbsentRecentSnapshot() throws IOException { + ZKDatabase zkDatabase = new ZKDatabase(new FileTxnSnapLog(new File("foo"), new File("bar")) { + @Override + public File findMostRecentSnapshot() throws IOException { + return null; + } + }); + assertEquals(0, zkDatabase.calculateTxnLogSizeLimit()); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperQuotaTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperQuotaTest.java new file mode 100644 index 0000000..2eb2c6c --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperQuotaTest.java @@ -0,0 +1,141 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import java.io.IOException; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.Quotas; +import org.apache.zookeeper.StatsTrack; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.cli.MalformedPathException; +import org.apache.zookeeper.cli.SetQuotaCommand; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ZooKeeperServer; +import org.junit.Test; + +public class ZooKeeperQuotaTest extends ClientBase { + + @Test + public void testQuota() throws Exception { + final ZooKeeper zk = createClient(); + final String path = "/a/b/v"; + // making sure setdata works on / + zk.setData("/", "some".getBytes(), -1); + zk.create("/a", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + zk.create("/a/b/v/d", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + SetQuotaCommand.createQuota(zk, path, 5L, 10); + + // see if its set + String absolutePath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; + byte[] data = zk.getData(absolutePath, false, new Stat()); + StatsTrack st = new StatsTrack(new String(data)); + assertTrue("bytes are set", st.getBytes() == 5L); + assertTrue("num count is set", st.getCount() == 10); + + String statPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; + byte[] qdata = zk.getData(statPath, false, new Stat()); + StatsTrack qst = new StatsTrack(new String(qdata)); + assertTrue("bytes are set", qst.getBytes() == 8L); + assertTrue("count is set", qst.getCount() == 2); + + //force server to restart and load from snapshot, not txn log + stopServer(); + startServer(); + stopServer(); + startServer(); + ZooKeeperServer server = serverFactory.getZooKeeperServer(); + assertNotNull("Quota is still set", server.getZKDatabase().getDataTree().getMaxPrefixWithQuota(path) + != null); + } + + @Test + public void testSetQuota() throws IOException, InterruptedException, KeeperException, MalformedPathException { + final ZooKeeper zk = createClient(); + + String path = "/c1"; + String nodeData = "foo"; + zk.create(path, nodeData.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + int count = 10; + long bytes = 5L; + SetQuotaCommand.createQuota(zk, path, bytes, count); + + //check the limit + String absoluteLimitPath = Quotas.quotaZookeeper + path + "/" + Quotas.limitNode; + byte[] data = zk.getData(absoluteLimitPath, false, null); + StatsTrack st = new StatsTrack(new String(data)); + assertEquals(bytes, st.getBytes()); + assertEquals(count, st.getCount()); + //check the stats + String absoluteStatPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; + data = zk.getData(absoluteStatPath, false, null); + st = new StatsTrack(new String(data)); + assertEquals(nodeData.length(), st.getBytes()); + assertEquals(1, st.getCount()); + + //create another node + String path2 = "/c1/c2"; + String nodeData2 = "bar"; + zk.create(path2, nodeData2.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + absoluteStatPath = Quotas.quotaZookeeper + path + "/" + Quotas.statNode; + data = zk.getData(absoluteStatPath, false, null); + st = new StatsTrack(new String(data)); + //check the stats + assertEquals(nodeData.length() + nodeData2.length(), st.getBytes()); + assertEquals(2, st.getCount()); + } + + @Test + public void testSetQuotaWhenSetQuotaOnParentOrChildPath() throws IOException, InterruptedException, KeeperException, MalformedPathException { + final ZooKeeper zk = createClient(); + + zk.create("/c1", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/c1/c2", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/c1/c2/c3", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/c1/c2/c3/c4", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + zk.create("/c1/c2/c3/c4/c5", "some".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + + //set the quota on the path:/c1/c2/c3 + SetQuotaCommand.createQuota(zk, "/c1/c2/c3", 5L, 10); + + try { + SetQuotaCommand.createQuota(zk, "/c1", 5L, 10); + } catch (IllegalArgumentException e) { + assertEquals("/c1 has a child /c1/c2/c3 which has a quota", e.getMessage()); + } + + try { + SetQuotaCommand.createQuota(zk, "/c1/c2/c3/c4/c5", 5L, 10); + } catch (IllegalArgumentException e) { + assertEquals("/c1/c2/c3/c4/c5 has a parent /c1/c2/c3 which has a quota", e.getMessage()); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperTestClient.java b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperTestClient.java new file mode 100644 index 0000000..42f0ebe --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/ZooKeeperTestClient.java @@ -0,0 +1,454 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.KeeperException.Code; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.Watcher.Event.EventType; +import org.apache.zookeeper.ZKTestCase; +import org.apache.zookeeper.ZooDefs.Ids; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.common.Time; +import org.apache.zookeeper.data.Stat; + +public class ZooKeeperTestClient extends ZKTestCase implements Watcher { + + protected String hostPort = "127.0.0.1:22801"; + + protected static final String dirOnZK = "/test_dir"; + + protected String testDirOnZK = dirOnZK + "/" + Time.currentElapsedTime(); + + LinkedBlockingQueue events = new LinkedBlockingQueue(); + + private WatchedEvent getEvent(int numTries) throws InterruptedException { + WatchedEvent event = null; + for (int i = 0; i < numTries; i++) { + System.out.println("i = " + i); + event = events.poll(10, TimeUnit.SECONDS); + if (event != null) { + break; + } + Thread.sleep(5000); + } + return event; + + } + + private void deleteZKDir(ZooKeeper zk, String nodeName) throws IOException, InterruptedException, KeeperException { + + Stat stat = zk.exists(nodeName, false); + if (stat == null) { + return; + } + + List children1 = zk.getChildren(nodeName, false); + List c2 = zk.getChildren(nodeName, false, stat); + + if (!children1.equals(c2)) { + fail("children lists from getChildren()/getChildren2() do not match"); + } + + if (!stat.equals(stat)) { + fail("stats from exists()/getChildren2() do not match"); + } + + if (children1.size() == 0) { + zk.delete(nodeName, -1); + return; + } + for (String n : children1) { + deleteZKDir(zk, n); + } + } + + private void checkRoot() throws IOException, InterruptedException { + ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); + + try { + zk.create(dirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException ke) { + // expected, sort of + } catch (KeeperException ke) { + fail("Unexpected exception code for create " + dirOnZK + ": " + ke.getMessage()); + } + + try { + zk.create(testDirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException.NodeExistsException ke) { + // expected, sort of + } catch (KeeperException ke) { + fail("Unexpected exception code for create " + testDirOnZK + ": " + ke.getMessage()); + } + + zk.close(); + } + + private void enode_test_1() throws IOException, InterruptedException, KeeperException { + checkRoot(); + String parentName = testDirOnZK; + String nodeName = parentName + "/enode_abc"; + ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); + + Stat stat = zk.exists(parentName, false); + if (stat == null) { + try { + zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException ke) { + fail("Creating node " + parentName + ke.getMessage()); + } + } + + try { + zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NODEEXISTS; + if (!valid) { + fail("Unexpected exception code for createin: " + ke.getMessage()); + } + } + + stat = zk.exists(nodeName, false); + if (stat == null) { + fail("node " + nodeName + " should exist"); + } + System.out.println("Closing client with sessionid: 0x" + Long.toHexString(zk.getSessionId())); + zk.close(); + zk = new ZooKeeper(hostPort, 10000, this); + + for (int i = 0; i < 10; i++) { + System.out.println("i = " + i); + stat = zk.exists(nodeName, false); + if (stat != null) { + System.out.println("node " + nodeName + " should not exist after reconnection close"); + } else { + System.out.println("node " + nodeName + " is gone after reconnection close!"); + break; + } + Thread.sleep(5000); + } + deleteZKDir(zk, nodeName); + zk.close(); + + } + + private void enode_test_2() throws IOException, InterruptedException, KeeperException { + checkRoot(); + String parentName = testDirOnZK; + String nodeName = parentName + "/enode_abc"; + ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); + ZooKeeper zk_1 = new ZooKeeper(hostPort, 10000, this); + + Stat stat_parent = zk_1.exists(parentName, false); + if (stat_parent == null) { + try { + zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException ke) { + fail("Creating node " + parentName + ke.getMessage()); + } + } + + Stat stat_node = zk_1.exists(nodeName, false); + if (stat_node != null) { + + try { + zk.delete(nodeName, -1); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY; + if (!valid) { + fail("Unexpected exception code for delete: " + ke.getMessage()); + } + } + } + + List firstGen1 = zk_1.getChildren(parentName, true); + Stat stat = new Stat(); + List firstGen2 = zk_1.getChildren(parentName, true, stat); + + if (!firstGen1.equals(firstGen2)) { + fail("children lists from getChildren()/getChildren2() do not match"); + } + + if (!stat_parent.equals(stat)) { + fail("stat from exists()/getChildren() do not match"); + } + + try { + zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NODEEXISTS; + if (!valid) { + fail("Unexpected exception code for createin: " + ke.getMessage()); + } + } + + Thread.sleep(5000); + WatchedEvent event = events.poll(10, TimeUnit.SECONDS); + if (event == null) { + throw new IOException("No event was delivered promptly"); + } + if (event.getType() != EventType.NodeChildrenChanged || !event.getPath().equalsIgnoreCase(parentName)) { + fail("Unexpected event was delivered: " + event.toString()); + } + + stat_node = zk_1.exists(nodeName, false); + if (stat_node == null) { + fail("node " + nodeName + " should exist"); + } + + try { + zk.delete(parentName, -1); + fail("Should be impossible to delete a non-empty node " + parentName); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NOTEMPTY; + if (!valid) { + fail("Unexpected exception code for delete: " + code); + } + } + + try { + zk.create(nodeName + "/def", null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + fail("Should be impossible to create child off Ephemeral node " + nodeName); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NOCHILDRENFOREPHEMERALS; + if (!valid) { + fail("Unexpected exception code for createin: " + code); + } + } + + try { + List children1 = zk.getChildren(nodeName, false); + List children2 = zk.getChildren(nodeName, false, null); + + if (!children1.equals(children2)) { + fail("children lists from getChildren()/getChildren2() does not match"); + } + + if (children1.size() > 0) { + fail("ephemeral node " + nodeName + " should not have children"); + } + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NONODE; + if (!valid) { + fail("Unexpected exception code for createin: " + code); + } + } + firstGen1 = zk_1.getChildren(parentName, true); + firstGen2 = zk_1.getChildren(parentName, true, null); + + if (!firstGen1.equals(firstGen2)) { + fail("children list from getChildren()/getChildren2() does not match"); + } + + stat_node = zk_1.exists(nodeName, true); + if (stat_node == null) { + fail("node " + nodeName + " should exist"); + } + System.out.println("session id of zk: " + zk.getSessionId()); + System.out.println("session id of zk_1: " + zk_1.getSessionId()); + zk.close(); + + zk_1.exists("nosuchnode", false); + + event = this.getEvent(10); + if (event == null) { + throw new Error("First event was not delivered promptly"); + } + if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || ( + event.getType() == EventType.NodeDeleted + && event.getPath().equalsIgnoreCase(nodeName)))) { + System.out.print(parentName + + " " + + EventType.NodeChildrenChanged + + " " + + nodeName + + " " + + EventType.NodeDeleted); + fail("Unexpected first event was delivered: " + event.toString()); + } + + event = this.getEvent(10); + + if (event == null) { + throw new Error("Second event was not delivered promptly"); + } + if (!((event.getType() == EventType.NodeChildrenChanged && event.getPath().equalsIgnoreCase(parentName)) || ( + event.getType() == EventType.NodeDeleted + && event.getPath().equalsIgnoreCase(nodeName)))) { + System.out.print(parentName + + " " + + EventType.NodeChildrenChanged + + " " + + nodeName + + " " + + EventType.NodeDeleted); + fail("Unexpected second event was delivered: " + event.toString()); + } + + firstGen1 = zk_1.getChildren(parentName, false); + stat_node = zk_1.exists(nodeName, false); + if (stat_node != null) { + fail("node " + nodeName + " should have been deleted"); + } + if (firstGen1.contains(nodeName)) { + fail("node " + nodeName + " should not be a children"); + } + deleteZKDir(zk_1, nodeName); + zk_1.close(); + } + + private void delete_create_get_set_test_1() throws IOException, InterruptedException, KeeperException { + checkRoot(); + ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); + String parentName = testDirOnZK; + String nodeName = parentName + "/benwashere"; + try { + zk.delete(nodeName, -1); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY; + if (!valid) { + fail("Unexpected exception code for delete: " + ke.getMessage()); + } + } + try { + zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NODEEXISTS; + if (!valid) { + fail("Unexpected exception code for create: " + ke.getMessage()); + } + } + try { + zk.setData(nodeName, "hi".getBytes(), 5700); + fail("Should have gotten BadVersion exception"); + } catch (KeeperException ke) { + if (ke.code() != Code.BADVERSION) { + fail("Should have gotten BadVersion exception"); + } + } + zk.setData(nodeName, "hi".getBytes(), -1); + Stat st = new Stat(); + byte[] bytes = zk.getData(nodeName, false, st); + String retrieved = new String(bytes); + if (!"hi".equals(retrieved)) { + fail("The retrieved data [" + retrieved + "] is differented than the expected [hi]"); + } + try { + zk.delete(nodeName, 6800); + fail("Should have gotten BadVersion exception"); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NOTEMPTY || code == KeeperException.Code.BADVERSION; + if (!valid) { + fail("Unexpected exception code for delete: " + ke.getMessage()); + } + } + try { + zk.delete(nodeName, -1); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NOTEMPTY; + if (!valid) { + fail("Unexpected exception code for delete: " + code); + } + } + deleteZKDir(zk, nodeName); + zk.close(); + } + + private void deleteNodeIfExists(ZooKeeper zk, String nodeName) throws InterruptedException { + try { + zk.delete(nodeName, -1); + } catch (KeeperException ke) { + Code code = ke.code(); + boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY; + if (!valid) { + fail("Unexpected exception code for delete: " + ke.getMessage()); + } + } + } + + private void create_get_stat_test() throws IOException, InterruptedException, KeeperException { + checkRoot(); + ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); + String parentName = testDirOnZK; + String nodeName = parentName + "/create_with_stat_tmp"; + deleteNodeIfExists(zk, nodeName); + deleteNodeIfExists(zk, nodeName + "_2"); + Stat stat = new Stat(); + zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat); + assertNotNull(stat); + assertTrue(stat.getCzxid() > 0); + assertTrue(stat.getCtime() > 0); + + Stat stat2 = new Stat(); + zk.create(nodeName + "_2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat2); + assertNotNull(stat2); + assertTrue(stat2.getCzxid() > stat.getCzxid()); + assertTrue(stat2.getCtime() > stat.getCtime()); + + deleteNodeIfExists(zk, nodeName); + deleteNodeIfExists(zk, nodeName + "_2"); + zk.close(); + } + + public void my_test_1() throws IOException, InterruptedException, KeeperException { + enode_test_1(); + enode_test_2(); + delete_create_get_set_test_1(); + create_get_stat_test(); + } + + public synchronized void process(WatchedEvent event) { + try { + System.out.println("Got an event " + event.toString()); + events.put(event); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + ZooKeeperTestClient zktc = new ZooKeeperTestClient(); + try { + zktc.my_test_1(); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/util/PemReaderTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/util/PemReaderTest.java new file mode 100644 index 0000000..423b38b --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/util/PemReaderTest.java @@ -0,0 +1,125 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import static org.junit.Assert.assertEquals; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.security.KeyStoreException; +import java.security.PrivateKey; +import java.security.cert.X509Certificate; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import org.apache.zookeeper.common.BaseX509ParameterizedTestCase; +import org.apache.zookeeper.common.KeyStoreFileType; +import org.apache.zookeeper.common.X509KeyType; +import org.apache.zookeeper.common.X509TestContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class PemReaderTest extends BaseX509ParameterizedTestCase { + + @Parameterized.Parameters + public static Collection params() { + return BaseX509ParameterizedTestCase.defaultParams(); + } + + public PemReaderTest( + X509KeyType caKeyType, X509KeyType certKeyType, String keyPassword, Integer paramIndex) { + super(paramIndex, () -> { + try { + return X509TestContext.newBuilder() + .setTempDir(tempDir) + .setKeyStorePassword(keyPassword) + .setKeyStoreKeyType(certKeyType) + .setTrustStorePassword(keyPassword) + .setTrustStoreKeyType(caKeyType) + .build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Test + public void testLoadPrivateKeyFromKeyStore() throws IOException, GeneralSecurityException { + Optional optPassword = x509TestContext.getKeyStorePassword().length() > 0 + ? Optional.of(x509TestContext.getKeyStorePassword()) + : Optional.empty(); + PrivateKey privateKey = PemReader.loadPrivateKey(x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM), optPassword); + assertEquals(x509TestContext.getKeyStoreKeyPair().getPrivate(), privateKey); + } + + // Try to load a password-protected private key without providing a password + @Test(expected = GeneralSecurityException.class) + public void testLoadEncryptedPrivateKeyFromKeyStoreWithoutPassword() throws GeneralSecurityException, IOException { + if (!x509TestContext.isKeyStoreEncrypted()) { + throw new GeneralSecurityException(); // this case is not tested so throw the expected exception + } + PemReader.loadPrivateKey(x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM), Optional.empty()); + } + + // Try to load a password-protected private key with the wrong password + @Test(expected = GeneralSecurityException.class) + public void testLoadEncryptedPrivateKeyFromKeyStoreWithWrongPassword() throws GeneralSecurityException, IOException { + if (!x509TestContext.isKeyStoreEncrypted()) { + throw new GeneralSecurityException(); // this case is not tested so throw the expected exception + } + PemReader.loadPrivateKey(x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM), Optional.of("wrong password")); + } + + // Try to load a non-protected private key while providing a password + @Test(expected = IOException.class) + public void testLoadUnencryptedPrivateKeyFromKeyStoreWithWrongPassword() throws GeneralSecurityException, IOException { + if (x509TestContext.isKeyStoreEncrypted()) { + throw new IOException(); + } + PemReader.loadPrivateKey(x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM), Optional.of("wrong password")); + } + + // Expect this to fail, the trust store does not contain a private key + @Test(expected = KeyStoreException.class) + public void testLoadPrivateKeyFromTrustStore() throws IOException, GeneralSecurityException { + PemReader.loadPrivateKey(x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM), Optional.empty()); + } + + // Expect this to fail, the trust store does not contain a private key + @Test(expected = KeyStoreException.class) + public void testLoadPrivateKeyFromTrustStoreWithPassword() throws IOException, GeneralSecurityException { + PemReader.loadPrivateKey(x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM), Optional.of("foobar")); + } + + @Test + public void testLoadCertificateFromKeyStore() throws IOException, GeneralSecurityException { + List certs = PemReader.readCertificateChain(x509TestContext.getKeyStoreFile(KeyStoreFileType.PEM)); + assertEquals(1, certs.size()); + assertEquals(x509TestContext.getKeyStoreCertificate(), certs.get(0)); + } + + @Test + public void testLoadCertificateFromTrustStore() throws IOException, GeneralSecurityException { + List certs = PemReader.readCertificateChain(x509TestContext.getTrustStoreFile(KeyStoreFileType.PEM)); + assertEquals(1, certs.size()); + assertEquals(x509TestContext.getTrustStoreCertificate(), certs.get(0)); + } + +} diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/util/TestCircularBlockingQueue.java b/zookeeper-server/src/test/java/org/apache/zookeeper/util/TestCircularBlockingQueue.java new file mode 100644 index 0000000..ac24d2e --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/util/TestCircularBlockingQueue.java @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.util; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import org.junit.Assert; +import org.junit.Test; + +public class TestCircularBlockingQueue { + + @Test + public void testCircularBlockingQueue() throws InterruptedException { + final CircularBlockingQueue testQueue = + new CircularBlockingQueue<>(2); + + testQueue.offer(1); + testQueue.offer(2); + testQueue.offer(3); + + Assert.assertEquals(2, testQueue.size()); + + Assert.assertEquals(2, testQueue.take().intValue()); + Assert.assertEquals(3, testQueue.take().intValue()); + + Assert.assertEquals(1L, testQueue.getDroppedCount()); + Assert.assertEquals(0, testQueue.size()); + Assert.assertEquals(true, testQueue.isEmpty()); + } + + @Test(timeout = 10000L) + public void testCircularBlockingQueueTakeBlock() + throws InterruptedException, ExecutionException { + + final CircularBlockingQueue testQueue = new CircularBlockingQueue<>(2); + + ExecutorService executor = Executors.newSingleThreadExecutor(); + try { + Future testTake = executor.submit(() -> { + return testQueue.take(); + }); + + // Allow the other thread to get into position; waiting for item to be + // inserted + while (!testQueue.isConsumerThreadBlocked()) { + Thread.sleep(50L); + } + + testQueue.offer(10); + + Integer result = testTake.get(); + Assert.assertEquals(10, result.intValue()); + } finally { + executor.shutdown(); + } + } + +} diff --git a/zookeeper-server/src/test/resources/check_compatibility.py b/zookeeper-server/src/test/resources/check_compatibility.py new file mode 100644 index 0000000..cad8195 --- /dev/null +++ b/zookeeper-server/src/test/resources/check_compatibility.py @@ -0,0 +1,204 @@ +#!/usr/bin/env python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Script which checks Java API compatibility between two revisions of the +# Java client. +# +# Based on the compatibility checker from the HBase project, but ported to +# Python for better readability. + +# Lifted from Kudu: https://github.com/apache/kudu/blob/master/build-support/check_compatibility.py + +import logging +import optparse +import os +import shutil +import subprocess +import sys + +JAVA_ACC_GIT_URL = "https://github.com/lvc/japi-compliance-checker.git" + +# The annotations for what we consider our public API. +PUBLIC_ANNOTATIONS = ["org.apache.yetus.audience.InterfaceAudience.LimitedPrivate", + "org.apache.yetus.audience.InterfaceAudience.Public"] + +# Various relative paths +PATH_TO_REPO_DIR = "../../../../" +PATH_TO_BUILD_DIR = PATH_TO_REPO_DIR + "build/compat-check" +PATH_TO_JACC_DIR = PATH_TO_REPO_DIR + "build/jacc" + +def check_output(*popenargs, **kwargs): + # r"""Run command with arguments and return its output as a byte string. + # Backported from Python 2.7 as it's implemented as pure python on stdlib. + # >>> check_output(['/usr/bin/python', '--version']) + # Python 2.6.2 + # """ + process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) + output, unused_err = process.communicate() + retcode = process.poll() + if retcode: + cmd = kwargs.get("args") + if cmd is None: + cmd = popenargs[0] + error = subprocess.CalledProcessError(retcode, cmd) + error.output = output + raise error + return output + +def get_repo_dir(): + """ Return the path to the top of the repo. """ + dirname, _ = os.path.split(os.path.abspath(__file__)) + return os.path.abspath(os.path.join(dirname, PATH_TO_REPO_DIR)) + + +def get_scratch_dir(): + """ Return the path to the scratch dir that we build within. """ + dirname, _ = os.path.split(os.path.abspath(__file__)) + return os.path.abspath(os.path.join(dirname, PATH_TO_BUILD_DIR)) + + +def get_java_acc_dir(): + dirname, _ = os.path.split(os.path.abspath(__file__)) + return os.path.abspath(os.path.join(dirname, PATH_TO_JACC_DIR)) + + +def clean_scratch_dir(scratch_dir): + """ Clean up and re-create the scratch directory. """ + if os.path.exists(scratch_dir): + logging.info("Removing scratch dir %s...", scratch_dir) + shutil.rmtree(scratch_dir) + logging.info("Creating empty scratch dir %s...", scratch_dir) + os.makedirs(scratch_dir) + + +def checkout_tree(rev, path): + """ Check out the Java source tree for the given revision into the given path. """ + logging.info("Checking out %s in %s", rev, path) + os.makedirs(path) + # Extract java source + subprocess.check_call(["bash", '-o', 'pipefail', "-c", + ("git archive --format=tar %s | " + + "tar -C \"%s\" -xf -") % (rev, path)], + cwd=get_repo_dir()) + + +def get_git_hash(revname): + """ Convert 'revname' to its SHA-1 hash. """ + return check_output(["git", "rev-parse", revname], + cwd=get_repo_dir()).strip() + + +def build_tree(path): + """ Run the Java build within 'path'. """ + logging.info("Building in %s...", path) + subprocess.check_call(["ant", "jar"], cwd=path) + + +def checkout_java_acc(force): + """ + Check out the Java API Compliance Checker. If 'force' is true, will re-download even if the + directory exists. + """ + acc_dir = get_java_acc_dir() + if os.path.exists(acc_dir): + logging.info("Java JAVA_ACC is already downloaded.") + if not force: + return + logging.info("Forcing re-download.") + shutil.rmtree(acc_dir) + logging.info("Checking out Java JAVA_ACC...") + subprocess.check_call(["git", "clone", "-b", "2.1", "--single-branch", "--depth=1", JAVA_ACC_GIT_URL, acc_dir]) + + +def find_client_jars(path): + """ Return a list of jars within 'path' to be checked for compatibility. """ + return check_output(["find", path, "-name", "zookeeper*.jar"]).rstrip('\n') + + +def run_java_acc(src_name, src, dst_name, dst): + """ Run the compliance checker to compare 'src' and 'dst'. """ + src_jar = find_client_jars(src) + dst_jar = find_client_jars(dst) + logging.info("Will check compatibility between original jars:\n%s\n" + + "and new jars:\n%s", + src_jar, dst_jar) + + annotations_path = os.path.join(get_scratch_dir(), "annotations.txt") + with file(annotations_path, "w") as f: + for ann in PUBLIC_ANNOTATIONS: + print >>f, ann + + java_acc_path = os.path.join(get_java_acc_dir(), "japi-compliance-checker.pl") + + out_path = os.path.join(get_scratch_dir(), "report.html") + subprocess.check_call(["perl", java_acc_path, + "-lib", "ZooKeeper", + "-v1", src_name, + "-v2", dst_name, + "-d1", src_jar, + "-d2", dst_jar, + "-annotations-list", annotations_path, + "-report-path", out_path]) + + +def main(argv): + logging.basicConfig(level=logging.INFO) + parser = optparse.OptionParser( + usage="usage: %prog SRC..[DST]") + parser.add_option("-f", "--force-download", dest="force_download_deps", + help=("Download dependencies (i.e. Java JAVA_ACC) even if they are " + + "already present")) + opts, args = parser.parse_args() + + if len(args) != 1: + parser.error("no src/dst revision specified") + sys.exit(1) + + src_rev, dst_rev = args[0].split("..", 1) + if dst_rev == "": + dst_rev = "HEAD" + src_rev = get_git_hash(src_rev) + dst_rev = get_git_hash(dst_rev) + + logging.info("Source revision: %s", src_rev) + logging.info("Destination revision: %s", dst_rev) + + # Download deps. + checkout_java_acc(opts.force_download_deps) + + # Set up the build. + scratch_dir = get_scratch_dir() + clean_scratch_dir(scratch_dir) + + # Check out the src and dst source trees. + src_dir = os.path.join(scratch_dir, "src") + dst_dir = os.path.join(scratch_dir, "dst") + checkout_tree(src_rev, src_dir) + checkout_tree(dst_rev, dst_dir) + + # Run the build in each. + build_tree(src_dir) + build_tree(dst_dir) + + run_java_acc(src_rev, src_dir + "/build", + dst_rev, dst_dir + "/build") + + +if __name__ == "__main__": + main(sys.argv) \ No newline at end of file diff --git a/zookeeper-server/src/test/resources/checkstyle-noframes-sorted.xsl b/zookeeper-server/src/test/resources/checkstyle-noframes-sorted.xsl new file mode 100644 index 0000000..5f9e93b --- /dev/null +++ b/zookeeper-server/src/test/resources/checkstyle-noframes-sorted.xsl @@ -0,0 +1,178 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      + +

      CheckStyle Audit

      Designed for use with CheckStyle and Ant.
      +


      + + + +
      + + + +
      + + + + +
      + + + + + + + + + +

      Files

      + + + + + + + + + + + + + + +
      NameErrors
      +
      + + + + +

      File

      + + + + + + + + + + + + + + +
      Error DescriptionLine
      + Back to top +
      + + + +

      Summary

      + + + + + + + + + + + + +
      FilesErrors
      +
      + + + + a + b + + + + + diff --git a/zookeeper-server/src/test/resources/checkstyle.xml b/zookeeper-server/src/test/resources/checkstyle.xml new file mode 100644 index 0000000..a5d5182 --- /dev/null +++ b/zookeeper-server/src/test/resources/checkstyle.xml @@ -0,0 +1,187 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-server/src/test/resources/data/buffersize/create/version-2/log.1 b/zookeeper-server/src/test/resources/data/buffersize/create/version-2/log.1 new file mode 100644 index 0000000..4f05bc1 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/create/version-2/log.1 differ diff --git a/zookeeper-server/src/test/resources/data/buffersize/create/version-2/snapshot.0 b/zookeeper-server/src/test/resources/data/buffersize/create/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/create/version-2/snapshot.0 differ diff --git a/zookeeper-server/src/test/resources/data/buffersize/set/version-2/log.1 b/zookeeper-server/src/test/resources/data/buffersize/set/version-2/log.1 new file mode 100644 index 0000000..8586f16 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/set/version-2/log.1 differ diff --git a/zookeeper-server/src/test/resources/data/buffersize/set/version-2/snapshot.0 b/zookeeper-server/src/test/resources/data/buffersize/set/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/set/version-2/snapshot.0 differ diff --git a/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/log.1 b/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/log.1 new file mode 100644 index 0000000..2f145a6 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/log.1 differ diff --git a/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/snapshot.0 b/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/snapshot.0 differ diff --git a/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/snapshot.2 b/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/snapshot.2 new file mode 100644 index 0000000..59dfa70 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/buffersize/snapshot/version-2/snapshot.2 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.1 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.1 new file mode 100644 index 0000000..9dc15ae Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.1 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.274 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.274 new file mode 100644 index 0000000..3bb868e Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.274 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.42 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.42 new file mode 100644 index 0000000..5385be5 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.42 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.63b b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.63b new file mode 100644 index 0000000..4ee0a98 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/log.63b differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.0 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.0 new file mode 100644 index 0000000..49b512e Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.0 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.272 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.272 new file mode 100644 index 0000000..71f32a5 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.272 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.273 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.273 new file mode 100644 index 0000000..3146f56 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.273 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.639 b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.639 new file mode 100644 index 0000000..cf9e389 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.639 differ diff --git a/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.83f b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.83f new file mode 100644 index 0000000..26dc5f6 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/invalidsnap/version-2/snapshot.83f differ diff --git a/zookeeper-server/src/test/resources/data/kerberos/minikdc-krb5.conf b/zookeeper-server/src/test/resources/data/kerberos/minikdc-krb5.conf new file mode 100644 index 0000000..43ec7c4 --- /dev/null +++ b/zookeeper-server/src/test/resources/data/kerberos/minikdc-krb5.conf @@ -0,0 +1,30 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +# This resource is originally from HDFS, see the similarly named files there +# in case of bug fixing, history, etc. +# Branch : trunk +# Github Revision: 1d1ab587e4e92ce3aea4cb144811f69145cb3b33 +# +[libdefaults] + default_realm = {0} + udp_preference_limit = 1 + +[realms] + {0} = '{' + kdc = {1}:{2} + '}' \ No newline at end of file diff --git a/zookeeper-server/src/test/resources/data/kerberos/minikdc.ldiff b/zookeeper-server/src/test/resources/data/kerberos/minikdc.ldiff new file mode 100644 index 0000000..20c8d77 --- /dev/null +++ b/zookeeper-server/src/test/resources/data/kerberos/minikdc.ldiff @@ -0,0 +1,52 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# +# This resource is originally from HDFS, see the similarly named files there +# in case of bug fixing, history, etc. +# Branch : trunk +# Github Revision: 1d1ab587e4e92ce3aea4cb144811f69145cb3b33 +# +dn: ou=users,dc=${0},dc=${1} +objectClass: organizationalUnit +objectClass: top +ou: users + +dn: uid=krbtgt,ou=users,dc=${0},dc=${1} +objectClass: top +objectClass: person +objectClass: inetOrgPerson +objectClass: krb5principal +objectClass: krb5kdcentry +cn: KDC Service +sn: Service +uid: krbtgt +userPassword: secret +krb5PrincipalName: krbtgt/${2}.${3}@${2}.${3} +krb5KeyVersionNumber: 0 + +dn: uid=ldap,ou=users,dc=${0},dc=${1} +objectClass: top +objectClass: person +objectClass: inetOrgPerson +objectClass: krb5principal +objectClass: krb5kdcentry +cn: LDAP +sn: Service +uid: ldap +userPassword: secret +krb5PrincipalName: ldap/${4}@${2}.${3} +krb5KeyVersionNumber: 0 \ No newline at end of file diff --git a/zookeeper-server/src/test/resources/data/ssl/README.md b/zookeeper-server/src/test/resources/data/ssl/README.md new file mode 100644 index 0000000..b8823d8 --- /dev/null +++ b/zookeeper-server/src/test/resources/data/ssl/README.md @@ -0,0 +1,10 @@ +SSL test data +=================== + +testKeyStore.jks +--- +Testing keystore, password is "testpass". + +testTrustStore.jks +--- +Testing truststore, password is "testpass". diff --git a/zookeeper-server/src/test/resources/data/ssl/testKeyStore.jks b/zookeeper-server/src/test/resources/data/ssl/testKeyStore.jks new file mode 100644 index 0000000..40a7d0b Binary files /dev/null and b/zookeeper-server/src/test/resources/data/ssl/testKeyStore.jks differ diff --git a/zookeeper-server/src/test/resources/data/ssl/testTrustStore.jks b/zookeeper-server/src/test/resources/data/ssl/testTrustStore.jks new file mode 100644 index 0000000..33f09c1 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/ssl/testTrustStore.jks differ diff --git a/zookeeper-server/src/test/resources/data/upgrade/log.100000001 b/zookeeper-server/src/test/resources/data/upgrade/log.100000001 new file mode 100644 index 0000000..00655f1 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/upgrade/log.100000001 differ diff --git a/zookeeper-server/src/test/resources/data/upgrade/log.100001bf0 b/zookeeper-server/src/test/resources/data/upgrade/log.100001bf0 new file mode 100644 index 0000000..83fa697 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/upgrade/log.100001bf0 differ diff --git a/zookeeper-server/src/test/resources/data/upgrade/snapshot.100000000 b/zookeeper-server/src/test/resources/data/upgrade/snapshot.100000000 new file mode 100644 index 0000000..56f9015 Binary files /dev/null and b/zookeeper-server/src/test/resources/data/upgrade/snapshot.100000000 differ diff --git a/zookeeper-server/src/test/resources/data/upgrade/snapshot.100001bec b/zookeeper-server/src/test/resources/data/upgrade/snapshot.100001bec new file mode 100644 index 0000000..0010d1e Binary files /dev/null and b/zookeeper-server/src/test/resources/data/upgrade/snapshot.100001bec differ diff --git a/zookeeper-server/src/test/resources/findbugsExcludeFile.xml b/zookeeper-server/src/test/resources/findbugsExcludeFile.xml new file mode 100644 index 0000000..2a352cc --- /dev/null +++ b/zookeeper-server/src/test/resources/findbugsExcludeFile.xml @@ -0,0 +1,212 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zookeeper-server/src/test/resources/log4j.properties b/zookeeper-server/src/test/resources/log4j.properties new file mode 100644 index 0000000..4a2ede9 --- /dev/null +++ b/zookeeper-server/src/test/resources/log4j.properties @@ -0,0 +1,65 @@ +# Copyright 2012 The Apache Software Foundation +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# Define some default values that can be overridden by system properties +zookeeper.root.logger=INFO, CONSOLE + +zookeeper.console.threshold=INFO + +zookeeper.log.dir=. +zookeeper.log.file=zookeeper.log +zookeeper.log.threshold=INFO +zookeeper.log.maxfilesize=256MB +zookeeper.log.maxbackupindex=20 + +zookeeper.tracelog.dir=${zookeeper.log.dir} +zookeeper.tracelog.file=zookeeper_trace.log + +log4j.rootLogger=${zookeeper.root.logger} + +# +# console +# Add "console" to rootlogger above if you want to use this +# +log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender +log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold} +log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout +log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n + +# +# Add ROLLINGFILE to rootLogger to get log file output +# +log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender +log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold} +log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file} +log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize} +log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex} +log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout +log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n + +# +# Add TRACEFILE to rootLogger to get log file output +# Log TRACE level and above messages to a log file +# +log4j.appender.TRACEFILE=org.apache.log4j.FileAppender +log4j.appender.TRACEFILE.Threshold=TRACE +log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file} + +log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout +### Notice we are including log4j's NDC here (%x) +log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n diff --git a/zookeeper-server/src/test/resources/test-github-pr.sh b/zookeeper-server/src/test/resources/test-github-pr.sh new file mode 100644 index 0000000..3acccc3 --- /dev/null +++ b/zookeeper-server/src/test/resources/test-github-pr.sh @@ -0,0 +1,614 @@ +#!/usr/bin/env bash +# 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. + + +#set -x + +### Setup some variables. +### GIT_COMMIT and BUILD_URL are set by Hudson if it is run by patch process +### Read variables from properties file +. `dirname $0`/test-patch.properties + +############################################################################### +parseArgs() { + case "$1" in + QABUILD) + ### Set QABUILD to true to indicate that this script is being run by Hudson + QABUILD=true + if [[ $# != 13 ]] ; then + echo "ERROR: usage $0 QABUILD " + cleanupAndExit 0 + fi + PATCH_DIR=$2 + PS=$3 + WGET=$4 + JIRACLI=$5 + GIT=$6 + GREP=$7 + PATCH=$8 + FINDBUGS_HOME=$9 + BASEDIR=${10} + JIRA_PASSWD=${11} + JAVA5_HOME=${12} + CURL=${13} + if [ ! -e "$PATCH_DIR" ] ; then + mkdir -p $PATCH_DIR + fi + + ## Obtain PR number and title + PULLREQUEST_ID=${GIT_PR_NUMBER} + PULLREQUEST_TITLE="${GIT_PR_TITLE}" + + ## Extract jira number from PR title + local prefix=${PULLREQUEST_TITLE%ZOOKEEPER\-[0-9]*} + local noprefix=${PULLREQUEST_TITLE#$prefix} + local regex='\(ZOOKEEPER-.[0-9]*\)' + defect=$(expr "$noprefix" : ${regex}) + + echo "Pull request id: ${PULLREQUEST_ID}" + echo "Pull request title: ${PULLREQUEST_TITLE}" + echo "Defect number: ${defect}" + + JIRA_COMMENT="GitHub Pull Request ${PULLREQUEST_NUMBER} Build + " + ;; + DEVELOPER) + ### Set QABUILD to false to indicate that this script is being run by a developer + QABUILD=false + if [[ $# != 9 ]] ; then + echo "ERROR: usage $0 DEVELOPER " + cleanupAndExit 0 + fi + PATCH_DIR=$3 + PATCH_FILE=${PATCH_DIR}/patch + curl -L $2.diff > ${PATCH_FILE} + ### PATCH_FILE contains the location of the patchfile + if [[ ! -e "$PATCH_FILE" ]] ; then + echo "Unable to locate the patch file $PATCH_FILE" + cleanupAndExit 0 + fi + ### Check if $PATCH_DIR exists. If it does not exist, create a new directory + if [[ ! -e "$PATCH_DIR" ]] ; then + mkdir "$PATCH_DIR" + if [[ $? == 0 ]] ; then + echo "$PATCH_DIR has been created" + else + echo "Unable to create $PATCH_DIR" + cleanupAndExit 0 + fi + fi + GIT=$4 + GREP=$5 + PATCH=$6 + FINDBUGS_HOME=$7 + BASEDIR=$8 + JAVA5_HOME=${9} + ### Obtain the patch filename to append it to the version number + local subject=`grep "Subject:" ${PATCH_FILE}` + local length=`expr match ${subject} ZOOKEEPER-[0-9]*` + local position=`expr index ${subject} ZOOKEEPER-` + defect=${${subject:$position:$length}#ZOOKEEPER-} + ;; + *) + echo "ERROR: usage $0 QABUILD [args] | DEVELOPER [args]" + cleanupAndExit 0 + ;; + esac +} + +############################################################################### +checkout () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Testing patch for pull request ${PULLREQUEST_ID}." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + ### When run by a developer, if the workspace contains modifications, do not continue + # Ref http://stackoverflow.com/a/2659808 for details on checking dirty status + ${GIT} diff-index --quiet HEAD + if [[ $? -ne 0 ]] ; then + uncommitted=`${GIT} diff --name-only HEAD` + uncommitted="You have the following files with uncommitted changes:${NEWLINE}${uncommitted}" + fi + untracked="$(${GIT} ls-files --exclude-standard --others)" && test -z "${untracked}" + if [[ $? -ne 0 ]] ; then + untracked="You have untracked and unignored files:${NEWLINE}${untracked}" + fi + + if [[ $QABUILD == "false" ]] ; then + if [[ $uncommitted || $untracked ]] ; then + echo "ERROR: can't run in a workspace that contains the following modifications" + echo "" + echo "${uncommitted}" + echo "" + echo "${untracked}" + cleanupAndExit 1 + fi + else + # I don't believe we need to do anything here - the jenkins job will + # cleanup the environment for us ("cleanup before checkout" action) + # on the precommit jenkins job + echo + fi + return $? +} + +############################################################################### +setup () { + ### exit if warnings are NOT defined in the properties file + if [ -z "$OK_FINDBUGS_WARNINGS" ] || [[ -z "$OK_JAVADOC_WARNINGS" ]] || [[ -z $OK_RELEASEAUDIT_WARNINGS ]]; then + echo "Please define the following properties in test-patch.properties file" + echo "OK_FINDBUGS_WARNINGS" + echo "OK_RELEASEAUDIT_WARNINGS" + echo "OK_JAVADOC_WARNINGS" + cleanupAndExit 1 + fi + ### get pull request diff + ${CURL} -L ${GIT_PR_URL}.diff > $PATCH_DIR/patch + + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Pre-build trunk to verify trunk stability and javac warnings" + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/trunkJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/trunkJavacWarnings.txt 2>&1 + if [[ $? != 0 ]] ; then + echo "Trunk compilation is broken?" + cleanupAndExit 1 + fi +} + +############################################################################### +### Check for @author tags in the patch +checkAuthor () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Checking there are no @author tags in the patch." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + authorTags=`$GREP -c -i '@author' $PATCH_DIR/patch` + echo "There appear to be $authorTags @author tags in the patch." + if [[ $authorTags != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 @author. The patch appears to contain $authorTags @author tags which the Zookeeper community has agreed to not allow in code contributions." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 @author. The patch does not contain any @author tags." + return 0 +} + +############################################################################### +### Check for tests in the patch +checkTests () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Checking there are new or changed tests in the patch." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + testReferences=`$GREP -c -i '/test' $PATCH_DIR/patch` + echo "There appear to be $testReferences test files referenced in the patch." + if [[ $testReferences == 0 ]] ; then + if [[ $QABUILD == "true" ]] ; then + patchIsDoc=`$GREP -c -i 'title="documentation' $PATCH_DIR/jira` + if [[ $patchIsDoc != 0 ]] ; then + echo "The patch appears to be a documentation patch that doesn't require tests." + JIRA_COMMENT="$JIRA_COMMENT + + +0 tests included. The patch appears to be a documentation patch that doesn't require tests." + return 0 + fi + fi + JIRA_COMMENT="$JIRA_COMMENT + + -1 tests included. The patch doesn't appear to include any new or modified tests. + Please justify why no new tests are needed for this patch. + Also please list what manual steps were performed to verify this patch." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 tests included. The patch appears to include $testReferences new or modified tests." + return 0 +} + +############################################################################### +### Check there are no javadoc warnings +checkJavadocWarnings () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched javadoc warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= clean javadoc | tee $PATCH_DIR/patchJavadocWarnings.txt" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= clean javadoc | tee $PATCH_DIR/patchJavadocWarnings.txt + javadocWarnings=`$GREP -o '\[javadoc\] [0-9]* warning' $PATCH_DIR/patchJavadocWarnings.txt | awk '{total += $2} END {print total}'` + echo "" + echo "" + echo "There appear to be $javadocWarnings javadoc warnings generated by the patched build." + + ### if current warnings greater than OK_JAVADOC_WARNINGS + if [[ $javadocWarnings > $OK_JAVADOC_WARNINGS ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 javadoc. The javadoc tool appears to have generated `expr $(($javadocWarnings-$OK_JAVADOC_WARNINGS))` warning messages." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 javadoc. The javadoc tool did not generate any warning messages." + return 0 +} + +############################################################################### +### Check there are no changes in the number of Javac warnings +checkJavacWarnings () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched javac warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/patchJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/patchJavacWarnings.txt 2>&1 + if [[ $? != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 javac. The patch appears to cause tar ant target to fail." + return 1 + fi + ### Compare trunk and patch javac warning numbers + if [[ -f $PATCH_DIR/patchJavacWarnings.txt ]] ; then + trunkJavacWarnings=`$GREP -o '\[javac\] [0-9]* warning' $PATCH_DIR/trunkJavacWarnings.txt | awk '{total += $2} END {print total}'` + patchJavacWarnings=`$GREP -o '\[javac\] [0-9]* warning' $PATCH_DIR/patchJavacWarnings.txt | awk '{total += $2} END {print total}'` + echo "There appear to be $trunkJavacWarnings javac compiler warnings before the patch and $patchJavacWarnings javac compiler warnings after applying the patch." + if [[ $patchJavacWarnings != "" && $trunkJavacWarnings != "" ]] ; then + if [[ $patchJavacWarnings -gt $trunkJavacWarnings ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 javac. The applied patch generated $patchJavacWarnings javac compiler warnings (more than the trunk's current $trunkJavacWarnings warnings)." + return 1 + fi + fi + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 javac. The applied patch does not increase the total number of javac compiler warnings." + return 0 +} + +############################################################################### +### Check there are no changes in the number of release audit (RAT) warnings +checkReleaseAuditWarnings () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched release audit warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= releaseaudit > $PATCH_DIR/patchReleaseAuditWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= releaseaudit > $PATCH_DIR/patchReleaseAuditWarnings.txt 2>&1 + + ### Compare trunk and patch release audit warning numbers + if [[ -f $PATCH_DIR/patchReleaseAuditWarnings.txt ]] ; then + patchReleaseAuditWarnings=`$GREP -c '\!?????' $PATCH_DIR/patchReleaseAuditWarnings.txt` + echo "" + echo "" + echo "There appear to be $OK_RELEASEAUDIT_WARNINGS release audit warnings before the patch and $patchReleaseAuditWarnings release audit warnings after applying the patch." + if [[ $patchReleaseAuditWarnings != "" && $OK_RELEASEAUDIT_WARNINGS != "" ]] ; then + if [[ $patchReleaseAuditWarnings -gt $OK_RELEASEAUDIT_WARNINGS ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 release audit. The applied patch generated $patchReleaseAuditWarnings release audit warnings (more than the trunk's current $OK_RELEASEAUDIT_WARNINGS warnings)." + $GREP '\!?????' $PATCH_DIR/patchReleaseAuditWarnings.txt > $PATCH_DIR/patchReleaseAuditProblems.txt + echo "Lines that start with ????? in the release audit report indicate files that do not have an Apache license header." >> $PATCH_DIR/patchReleaseAuditProblems.txt + JIRA_COMMENT_FOOTER="Release audit warnings: $BUILD_URL/artifact/trunk/patchprocess/patchReleaseAuditProblems.txt +$JIRA_COMMENT_FOOTER" + return 1 + fi + fi + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 release audit. The applied patch does not increase the total number of release audit warnings." + return 0 +} + +############################################################################### +### Check there are no changes in the number of Checkstyle warnings +checkStyle () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched checkstyle warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "THIS IS NOT IMPLEMENTED YET" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= checkstyle" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= checkstyle + JIRA_COMMENT_FOOTER="Checkstyle results: $BUILD_URL/artifact/trunk/build/test/checkstyle-errors.html +$JIRA_COMMENT_FOOTER" + ### TODO: calculate actual patchStyleErrors +# patchStyleErrors=0 +# if [[ $patchStyleErrors != 0 ]] ; then +# JIRA_COMMENT="$JIRA_COMMENT +# +# -1 checkstyle. The patch generated $patchStyleErrors code style errors." +# return 1 +# fi +# JIRA_COMMENT="$JIRA_COMMENT +# +# +1 checkstyle. The patch generated 0 code style errors." + return 0 +} + +############################################################################### +### Check there are no changes in the number of Findbugs warnings +checkFindbugsWarnings () { + findbugs_version=`${FINDBUGS_HOME}/bin/findbugs -version` + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched Findbugs warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Dfindbugs.home=$FINDBUGS_HOME -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= findbugs" + $ANT_HOME/bin/ant -Dfindbugs.home=$FINDBUGS_HOME -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= findbugs + if [ $? != 0 ] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 findbugs. The patch appears to cause Findbugs (version ${findbugs_version}) to fail." + return 1 + fi +JIRA_COMMENT_FOOTER="Findbugs warnings: $BUILD_URL/artifact/trunk/build/test/findbugs/newPatchFindbugsWarnings.html +$JIRA_COMMENT_FOOTER" + cp $BASEDIR/build/test/findbugs/*.xml $PATCH_DIR/patchFindbugsWarnings.xml + $FINDBUGS_HOME/bin/setBugDatabaseInfo -timestamp "01/01/2000" \ + $PATCH_DIR/patchFindbugsWarnings.xml \ + $PATCH_DIR/patchFindbugsWarnings.xml + findbugsWarnings=`$FINDBUGS_HOME/bin/filterBugs -first "01/01/2000" $PATCH_DIR/patchFindbugsWarnings.xml \ + $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.xml | /usr/bin/awk '{print $1}'` + $FINDBUGS_HOME/bin/convertXmlToText -html \ + $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.xml \ + $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.html + cp $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.html $PATCH_DIR/newPatchFindbugsWarnings.html + cp $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.xml $PATCH_DIR/newPatchFindbugsWarnings.xml + + ### if current warnings greater than OK_FINDBUGS_WARNINGS + if [[ $findbugsWarnings > $OK_FINDBUGS_WARNINGS ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 findbugs. The patch appears to introduce `expr $(($findbugsWarnings-$OK_FINDBUGS_WARNINGS))` new Findbugs (version ${findbugs_version}) warnings." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 findbugs. The patch does not introduce any new Findbugs (version ${findbugs_version}) warnings." + return 0 +} + +############################################################################### +### Run the test-core target +runCoreTests () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Running core tests." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + + ### Kill any rogue build processes from the last attempt + $PS auxwww | $GREP ZookeeperPatchProcess | /usr/bin/nawk '{print $2}' | /usr/bin/xargs -t -I {} /bin/kill -9 {} > /dev/null + + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=no -Dtest.junit.threads=4 -Dcompile.c++=yes -Djava5.home=$JAVA5_HOME test-core" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=no -Dtest.junit.threads=4 -Dcompile.c++=yes -Djava5.home=$JAVA5_HOME test-core + if [[ $? != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 core tests. The patch failed core unit tests." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 core tests. The patch passed core unit tests." + return 0 +} + +############################################################################### +### Run the test-contrib target +runContribTests () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Running contrib tests." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + + ### Kill any rogue build processes from the last attempt + $PS auxwww | $GREP ZookeeperPatchProcess | /usr/bin/nawk '{print $2}' | /usr/bin/xargs -t -I {} /bin/kill -9 {} > /dev/null + + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=no test-contrib" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=no test-contrib + if [[ $? != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 contrib tests. The patch failed contrib unit tests." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 contrib tests. The patch passed contrib unit tests." + return 0 +} + +############################################################################### +### Submit a comment to the defect's Jira +submitJiraComment () { + local result=$1 + ### Do not output the value of JIRA_COMMENT_FOOTER when run by a developer + if [[ $QABUILD == "false" ]] ; then + JIRA_COMMENT_FOOTER="" + fi + if [[ $result == 0 ]] ; then + comment="+1 overall. $JIRA_COMMENT + +$JIRA_COMMENT_FOOTER" + else + comment="-1 overall. $JIRA_COMMENT + +$JIRA_COMMENT_FOOTER" + fi + ### Output the test result to the console + echo " + + + +$comment" + + if [[ $QABUILD == "true" ]] ; then + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Adding comment to Jira." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + ### Update Jira with a comment + export USER=jenkins + $JIRACLI -s https://issues.apache.org/jira -a addcomment -u hadoopqa -p $JIRA_PASSWD --comment "$comment" --issue $defect + $JIRACLI -s https://issues.apache.org/jira -a logout -u hadoopqa -p $JIRA_PASSWD + fi +} + +############################################################################### +### Cleanup files +cleanupAndExit () { + local result=$1 + if [[ $QABUILD == "true" ]] ; then + if [ -e "$PATCH_DIR" ] ; then + mv $PATCH_DIR $BASEDIR + fi + fi + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Finished build." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + exit $result +} + +############################################################################### +############################################################################### +############################################################################### + +JIRA_COMMENT="" +JIRA_COMMENT_FOOTER="Console output: $BUILD_URL/console + +This message is automatically generated." + +### Check if arguments to the script have been specified properly or not +echo "----- Going to parser args -----" +parseArgs $@ +cd $BASEDIR + +echo "----- Parsed args, going to checkout -----" +checkout +RESULT=$? +if [[ $QABUILD == "true" ]] ; then + if [[ $RESULT != 0 ]] ; then + exit 100 + fi +fi +setup +checkAuthor +(( RESULT = RESULT + $? )) + +checkTests +checkTestsResult=$? +(( RESULT = RESULT + $checkTestsResult )) +if [[ $checkTestsResult != 0 ]] ; then + submitJiraComment 1 + cleanupAndExit 1 +fi +checkJavadocWarnings +(( RESULT = RESULT + $? )) +checkJavacWarnings +(( RESULT = RESULT + $? )) +### Checkstyle not implemented yet +#checkStyle +#(( RESULT = RESULT + $? )) +checkFindbugsWarnings +(( RESULT = RESULT + $? )) +checkReleaseAuditWarnings +(( RESULT = RESULT + $? )) +### Do not call these when run by a developer +if [[ $QABUILD == "true" ]] ; then + runCoreTests + (( RESULT = RESULT + $? )) + runContribTests + (( RESULT = RESULT + $? )) +fi +JIRA_COMMENT_FOOTER="Test results: $BUILD_URL/testReport/ +$JIRA_COMMENT_FOOTER" + +submitJiraComment $RESULT +cleanupAndExit $RESULT diff --git a/zookeeper-server/src/test/resources/test-patch.properties b/zookeeper-server/src/test/resources/test-patch.properties new file mode 100644 index 0000000..e459e3c --- /dev/null +++ b/zookeeper-server/src/test/resources/test-patch.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +OK_RELEASEAUDIT_WARNINGS=0 +OK_FINDBUGS_WARNINGS=0 +OK_JAVADOC_WARNINGS=0 diff --git a/zookeeper-server/src/test/resources/test-patch.sh b/zookeeper-server/src/test/resources/test-patch.sh new file mode 100644 index 0000000..90b0a0b --- /dev/null +++ b/zookeeper-server/src/test/resources/test-patch.sh @@ -0,0 +1,647 @@ +#!/usr/bin/env bash +# 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. + + +#set -x + +### Setup some variables. +### GIT_COMMIT and BUILD_URL are set by Hudson if it is run by patch process +### Read variables from properties file +. `dirname $0`/test-patch.properties + +############################################################################### +parseArgs() { + case "$1" in + HUDSON) + ### Set HUDSON to true to indicate that this script is being run by Hudson + HUDSON=true + if [[ $# != 14 ]] ; then + echo "ERROR: usage $0 HUDSON " + cleanupAndExit 0 + fi + PATCH_DIR=$2 + PS=$3 + WGET=$4 + JIRACLI=$5 + GIT=$6 + GREP=$7 + PATCH=$8 + FINDBUGS_HOME=$9 + BASEDIR=${10} + JIRA_PASSWD=${11} + JAVA5_HOME=${12} + CURL=${13} + defect=${14} + + ### Retrieve the defect number + if [ -z "$defect" ] ; then + echo "Could not determine the patch to test. Exiting." + cleanupAndExit 0 + fi + + if [ ! -e "$PATCH_DIR" ] ; then + mkdir -p $PATCH_DIR + fi + + ;; + DEVELOPER) + ### Set HUDSON to false to indicate that this script is being run by a developer + HUDSON=false + if [[ $# != 9 ]] ; then + echo "ERROR: usage $0 DEVELOPER " + cleanupAndExit 0 + fi + ### PATCH_FILE contains the location of the patchfile + PATCH_FILE=$2 + if [[ ! -e "$PATCH_FILE" ]] ; then + echo "Unable to locate the patch file $PATCH_FILE" + cleanupAndExit 0 + fi + PATCH_DIR=$3 + ### Check if $PATCH_DIR exists. If it does not exist, create a new directory + if [[ ! -e "$PATCH_DIR" ]] ; then + mkdir "$PATCH_DIR" + if [[ $? == 0 ]] ; then + echo "$PATCH_DIR has been created" + else + echo "Unable to create $PATCH_DIR" + cleanupAndExit 0 + fi + fi + GIT=$4 + GREP=$5 + PATCH=$6 + FINDBUGS_HOME=$7 + BASEDIR=$8 + JAVA5_HOME=${9} + ### Obtain the patch filename to append it to the version number + defect=`basename $PATCH_FILE` + ;; + *) + echo "ERROR: usage $0 HUDSON [args] | DEVELOPER [args]" + cleanupAndExit 0 + ;; + esac +} + +############################################################################### +checkout () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Testing patch for ${defect}." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + ### When run by a developer, if the workspace contains modifications, do not continue + # Ref http://stackoverflow.com/a/2659808 for details on checking dirty status + ${GIT} diff-index --quiet HEAD + if [[ $? -ne 0 ]] ; then + uncommitted=`${GIT} diff --name-only HEAD` + uncommitted="You have the following files with uncommitted changes:${NEWLINE}${uncommitted}" + fi + untracked="$(${GIT} ls-files --exclude-standard --others)" && test -z "${untracked}" + if [[ $? -ne 0 ]] ; then + untracked="You have untracked and unignored files:${NEWLINE}${untracked}" + fi + + if [[ $HUDSON == "false" ]] ; then + if [[ $uncommitted || $untracked ]] ; then + echo "ERROR: can't run in a workspace that contains the following modifications" + echo "" + echo "${uncommitted}" + echo "" + echo "${untracked}" + cleanupAndExit 1 + fi + else + # I don't believe we need to do anything here - the jenkins job will + # cleanup the environment for us ("cleanup before checkout" action) + # on the precommit jenkins job + echo + fi + return $? +} + +############################################################################### +setup () { + ### Download latest patch file (ignoring .htm and .html) when run from patch process + if [[ $HUDSON == "true" ]] ; then + $WGET -q -O $PATCH_DIR/jira http://issues.apache.org/jira/browse/$defect + if [[ `$GREP -c 'Patch Available' $PATCH_DIR/jira` == 0 ]] ; then + echo "$defect is not \"Patch Available\". Exiting." + cleanupAndExit 0 + fi + relativePatchURL=`$GREP -o '"/jira/secure/attachment/[0-9]*/[^"]*' $PATCH_DIR/jira | $GREP -v -e 'htm[l]*$' | sort | tail -1 | $GREP -o '/jira/secure/attachment/[0-9]*/[^"]*'` + patchURL="http://issues.apache.org${relativePatchURL}" + patchNum=`echo $patchURL | $GREP -o '[0-9]*/' | $GREP -o '[0-9]*'` + echo "$defect patch is being downloaded at `date` from" + echo "$patchURL" + $WGET -q -O $PATCH_DIR/patch $patchURL + JIRA_COMMENT="Here are the results of testing the latest attachment + $patchURL + against trunk revision ${GIT_COMMIT}." + + ### Copy the patch file to $PATCH_DIR + else + cp $PATCH_FILE $PATCH_DIR/patch + if [[ $? == 0 ]] ; then + echo "Patch file $PATCH_FILE copied to $PATCH_DIR" + else + echo "Could not copy $PATCH_FILE to $PATCH_DIR" + cleanupAndExit 0 + fi + fi + ### exit if warnings are NOT defined in the properties file + if [ -z "$OK_FINDBUGS_WARNINGS" ] || [[ -z "$OK_JAVADOC_WARNINGS" ]] || [[ -z $OK_RELEASEAUDIT_WARNINGS ]]; then + echo "Please define the following properties in test-patch.properties file" + echo "OK_FINDBUGS_WARNINGS" + echo "OK_RELEASEAUDIT_WARNINGS" + echo "OK_JAVADOC_WARNINGS" + cleanupAndExit 1 + fi + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Pre-build trunk to verify trunk stability and javac warnings" + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/trunkJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/trunkJavacWarnings.txt 2>&1 + if [[ $? != 0 ]] ; then + echo "Trunk compilation is broken?" + cleanupAndExit 1 + fi +} + +############################################################################### +### Check for @author tags in the patch +checkAuthor () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Checking there are no @author tags in the patch." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + authorTags=`$GREP -c -i '@author' $PATCH_DIR/patch` + echo "There appear to be $authorTags @author tags in the patch." + if [[ $authorTags != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 @author. The patch appears to contain $authorTags @author tags which the Zookeeper community has agreed to not allow in code contributions." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 @author. The patch does not contain any @author tags." + return 0 +} + +############################################################################### +### Check for tests in the patch +checkTests () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Checking there are new or changed tests in the patch." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + testReferences=`$GREP -c -i '/test' $PATCH_DIR/patch` + echo "There appear to be $testReferences test files referenced in the patch." + if [[ $testReferences == 0 ]] ; then + if [[ $HUDSON == "true" ]] ; then + patchIsDoc=`$GREP -c -i 'title="documentation' $PATCH_DIR/jira` + if [[ $patchIsDoc != 0 ]] ; then + echo "The patch appears to be a documentation patch that doesn't require tests." + JIRA_COMMENT="$JIRA_COMMENT + + +0 tests included. The patch appears to be a documentation patch that doesn't require tests." + return 0 + fi + fi + JIRA_COMMENT="$JIRA_COMMENT + + -1 tests included. The patch doesn't appear to include any new or modified tests. + Please justify why no new tests are needed for this patch. + Also please list what manual steps were performed to verify this patch." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 tests included. The patch appears to include $testReferences new or modified tests." + return 0 +} + +############################################################################### +### Attempt to apply the patch +applyPatch () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Applying patch." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + $PATCH -E -p0 < $PATCH_DIR/patch + if [[ $? != 0 ]] ; then + echo "PATCH APPLICATION FAILED" + JIRA_COMMENT="$JIRA_COMMENT + + -1 patch. The patch command could not apply the patch." + return 1 + fi + return 0 +} + +############################################################################### +### Check there are no javadoc warnings +checkJavadocWarnings () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched javadoc warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= clean javadoc | tee $PATCH_DIR/patchJavadocWarnings.txt" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= clean javadoc | tee $PATCH_DIR/patchJavadocWarnings.txt + javadocWarnings=`$GREP -o '\[javadoc\] [0-9]* warning' $PATCH_DIR/patchJavadocWarnings.txt | awk '{total += $2} END {print total}'` + echo "" + echo "" + echo "There appear to be $javadocWarnings javadoc warnings generated by the patched build." + + ### if current warnings greater than OK_JAVADOC_WARNINGS + if [[ $javadocWarnings > $OK_JAVADOC_WARNINGS ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 javadoc. The javadoc tool appears to have generated `expr $(($javadocWarnings-$OK_JAVADOC_WARNINGS))` warning messages." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 javadoc. The javadoc tool did not generate any warning messages." + return 0 +} + +############################################################################### +### Check there are no changes in the number of Javac warnings +checkJavacWarnings () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched javac warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/patchJavacWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djavac.args="-Xlint -Xmaxwarns 1000" -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= clean tar > $PATCH_DIR/patchJavacWarnings.txt 2>&1 + if [[ $? != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 javac. The patch appears to cause tar ant target to fail." + return 1 + fi + ### Compare trunk and patch javac warning numbers + if [[ -f $PATCH_DIR/patchJavacWarnings.txt ]] ; then + trunkJavacWarnings=`$GREP -o '\[javac\] [0-9]* warning' $PATCH_DIR/trunkJavacWarnings.txt | awk '{total += $2} END {print total}'` + patchJavacWarnings=`$GREP -o '\[javac\] [0-9]* warning' $PATCH_DIR/patchJavacWarnings.txt | awk '{total += $2} END {print total}'` + echo "There appear to be $trunkJavacWarnings javac compiler warnings before the patch and $patchJavacWarnings javac compiler warnings after applying the patch." + if [[ $patchJavacWarnings != "" && $trunkJavacWarnings != "" ]] ; then + if [[ $patchJavacWarnings -gt $trunkJavacWarnings ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 javac. The applied patch generated $patchJavacWarnings javac compiler warnings (more than the trunk's current $trunkJavacWarnings warnings)." + return 1 + fi + fi + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 javac. The applied patch does not increase the total number of javac compiler warnings." + return 0 +} + +############################################################################### +### Check there are no changes in the number of release audit (RAT) warnings +checkReleaseAuditWarnings () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched release audit warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= releaseaudit > $PATCH_DIR/patchReleaseAuditWarnings.txt 2>&1" + $ANT_HOME/bin/ant -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= releaseaudit > $PATCH_DIR/patchReleaseAuditWarnings.txt 2>&1 + + ### Compare trunk and patch release audit warning numbers + if [[ -f $PATCH_DIR/patchReleaseAuditWarnings.txt ]] ; then + patchReleaseAuditWarnings=`$GREP -c '\!?????' $PATCH_DIR/patchReleaseAuditWarnings.txt` + echo "" + echo "" + echo "There appear to be $OK_RELEASEAUDIT_WARNINGS release audit warnings before the patch and $patchReleaseAuditWarnings release audit warnings after applying the patch." + if [[ $patchReleaseAuditWarnings != "" && $OK_RELEASEAUDIT_WARNINGS != "" ]] ; then + if [[ $patchReleaseAuditWarnings -gt $OK_RELEASEAUDIT_WARNINGS ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 release audit. The applied patch generated $patchReleaseAuditWarnings release audit warnings (more than the trunk's current $OK_RELEASEAUDIT_WARNINGS warnings)." + $GREP '\!?????' $PATCH_DIR/patchReleaseAuditWarnings.txt > $PATCH_DIR/patchReleaseAuditProblems.txt + echo "Lines that start with ????? in the release audit report indicate files that do not have an Apache license header." >> $PATCH_DIR/patchReleaseAuditProblems.txt + JIRA_COMMENT_FOOTER="Release audit warnings: $BUILD_URL/artifact/trunk/patchprocess/patchReleaseAuditProblems.txt +$JIRA_COMMENT_FOOTER" + return 1 + fi + fi + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 release audit. The applied patch does not increase the total number of release audit warnings." + return 0 +} + +############################################################################### +### Check there are no changes in the number of Checkstyle warnings +checkStyle () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched checkstyle warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "THIS IS NOT IMPLEMENTED YET" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= checkstyle" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= checkstyle + JIRA_COMMENT_FOOTER="Checkstyle results: $BUILD_URL/artifact/trunk/build/test/checkstyle-errors.html +$JIRA_COMMENT_FOOTER" + ### TODO: calculate actual patchStyleErrors +# patchStyleErrors=0 +# if [[ $patchStyleErrors != 0 ]] ; then +# JIRA_COMMENT="$JIRA_COMMENT +# +# -1 checkstyle. The patch generated $patchStyleErrors code style errors." +# return 1 +# fi +# JIRA_COMMENT="$JIRA_COMMENT +# +# +1 checkstyle. The patch generated 0 code style errors." + return 0 +} + +############################################################################### +### Check there are no changes in the number of Findbugs warnings +checkFindbugsWarnings () { + findbugs_version=`${FINDBUGS_HOME}/bin/findbugs -version` + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Determining number of patched Findbugs warnings." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + echo "$ANT_HOME/bin/ant -Dfindbugs.home=$FINDBUGS_HOME -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= findbugs" + $ANT_HOME/bin/ant -Dfindbugs.home=$FINDBUGS_HOME -Djava5.home=${JAVA5_HOME} -DZookeeperPatchProcess= findbugs + if [ $? != 0 ] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 findbugs. The patch appears to cause Findbugs (version ${findbugs_version}) to fail." + return 1 + fi +JIRA_COMMENT_FOOTER="Findbugs warnings: $BUILD_URL/artifact/trunk/build/test/findbugs/newPatchFindbugsWarnings.html +$JIRA_COMMENT_FOOTER" + cp $BASEDIR/build/test/findbugs/*.xml $PATCH_DIR/patchFindbugsWarnings.xml + $FINDBUGS_HOME/bin/setBugDatabaseInfo -timestamp "01/01/2000" \ + $PATCH_DIR/patchFindbugsWarnings.xml \ + $PATCH_DIR/patchFindbugsWarnings.xml + findbugsWarnings=`$FINDBUGS_HOME/bin/filterBugs -first "01/01/2000" $PATCH_DIR/patchFindbugsWarnings.xml \ + $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.xml | /usr/bin/awk '{print $1}'` + $FINDBUGS_HOME/bin/convertXmlToText -html \ + $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.xml \ + $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.html + cp $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.html $PATCH_DIR/newPatchFindbugsWarnings.html + cp $BASEDIR/build/test/findbugs/newPatchFindbugsWarnings.xml $PATCH_DIR/newPatchFindbugsWarnings.xml + + ### if current warnings greater than OK_FINDBUGS_WARNINGS + if [[ $findbugsWarnings > $OK_FINDBUGS_WARNINGS ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 findbugs. The patch appears to introduce `expr $(($findbugsWarnings-$OK_FINDBUGS_WARNINGS))` new Findbugs (version ${findbugs_version}) warnings." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 findbugs. The patch does not introduce any new Findbugs (version ${findbugs_version}) warnings." + return 0 +} + +############################################################################### +### Run the test-core target +runCoreTests () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Running core tests." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + + ### Kill any rogue build processes from the last attempt + $PS auxwww | $GREP ZookeeperPatchProcess | /usr/bin/nawk '{print $2}' | /usr/bin/xargs -t -I {} /bin/kill -9 {} > /dev/null + + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=yes -Dtest.junit.threads=8 -Dcompile.c++=yes -Djava5.home=$JAVA5_HOME test-core" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=yes -Dtest.junit.threads=8 -Dcompile.c++=yes -Djava5.home=$JAVA5_HOME test-core + if [[ $? != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 core tests. The patch failed core unit tests." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 core tests. The patch passed core unit tests." + return 0 +} + +############################################################################### +### Run the test-contrib target +runContribTests () { + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Running contrib tests." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + + ### Kill any rogue build processes from the last attempt + $PS auxwww | $GREP ZookeeperPatchProcess | /usr/bin/nawk '{print $2}' | /usr/bin/xargs -t -I {} /bin/kill -9 {} > /dev/null + + echo "$ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=yes test-contrib" + $ANT_HOME/bin/ant -DZookeeperPatchProcess= -Dtest.junit.output.format=xml -Dtest.output=yes test-contrib + if [[ $? != 0 ]] ; then + JIRA_COMMENT="$JIRA_COMMENT + + -1 contrib tests. The patch failed contrib unit tests." + return 1 + fi + JIRA_COMMENT="$JIRA_COMMENT + + +1 contrib tests. The patch passed contrib unit tests." + return 0 +} + +############################################################################### +### Submit a comment to the defect's Jira +submitJiraComment () { + local result=$1 + ### Do not output the value of JIRA_COMMENT_FOOTER when run by a developer + if [[ $HUDSON == "false" ]] ; then + JIRA_COMMENT_FOOTER="" + fi + if [[ $result == 0 ]] ; then + comment="+1 overall. $JIRA_COMMENT + +$JIRA_COMMENT_FOOTER" + else + comment="-1 overall. $JIRA_COMMENT + +$JIRA_COMMENT_FOOTER" + fi + ### Output the test result to the console + echo " + + + +$comment" + + if [[ $HUDSON == "true" ]] ; then + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Adding comment to Jira." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + ### Update Jira with a comment + export USER=jenkins + $JIRACLI -s https://issues.apache.org/jira -a addcomment -u hadoopqa -p $JIRA_PASSWD --comment "$comment" --issue $defect + $JIRACLI -s https://issues.apache.org/jira -a logout -u hadoopqa -p $JIRA_PASSWD + fi +} + +############################################################################### +### Cleanup files +cleanupAndExit () { + local result=$1 + if [[ $HUDSON == "true" ]] ; then + if [ -e "$PATCH_DIR" ] ; then + mv $PATCH_DIR $BASEDIR + fi + fi + echo "" + echo "" + echo "======================================================================" + echo "======================================================================" + echo " Finished build." + echo "======================================================================" + echo "======================================================================" + echo "" + echo "" + exit $result +} + +############################################################################### +############################################################################### +############################################################################### + +JIRA_COMMENT="" +JIRA_COMMENT_FOOTER="Console output: $BUILD_URL/console + +This message is automatically generated." + +### Check if arguments to the script have been specified properly or not +parseArgs $@ +cd $BASEDIR + +checkout +RESULT=$? +if [[ $HUDSON == "true" ]] ; then + if [[ $RESULT != 0 ]] ; then + exit 100 + fi +fi +setup +checkAuthor +RESULT=$? + +checkTests +(( RESULT = RESULT + $? )) +applyPatch +if [[ $? != 0 ]] ; then + submitJiraComment 1 + cleanupAndExit 1 +fi +checkJavadocWarnings +(( RESULT = RESULT + $? )) +checkJavacWarnings +(( RESULT = RESULT + $? )) +### Checkstyle not implemented yet +#checkStyle +#(( RESULT = RESULT + $? )) +checkFindbugsWarnings +(( RESULT = RESULT + $? )) +checkReleaseAuditWarnings +(( RESULT = RESULT + $? )) +### Do not call these when run by a developer +if [[ $HUDSON == "true" ]] ; then + runCoreTests + (( RESULT = RESULT + $? )) + runContribTests + (( RESULT = RESULT + $? )) +fi +JIRA_COMMENT_FOOTER="Test results: $BUILD_URL/testReport/ +$JIRA_COMMENT_FOOTER" + +submitJiraComment $RESULT +cleanupAndExit $RESULT diff --git a/zookeeper-server/src/test/resources/test-scripts.sh b/zookeeper-server/src/test/resources/test-scripts.sh new file mode 100644 index 0000000..25854b8 --- /dev/null +++ b/zookeeper-server/src/test/resources/test-scripts.sh @@ -0,0 +1,221 @@ +#!/usr/bin/env bash + +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You 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. + +ZKS=bin/zkServer.sh +ZKSI=bin/zkServer-initialize.sh + +if [ ! -d "conf" ]; then + echo "run this from the toplevel directory" + exit 1 +fi + +DATADIR=test-scripts_datadir +DATALOGDIR=test-scripts_datalogdir + +case "`uname`" in + CYGWIN*) cygwin=true ;; + *) cygwin=false ;; +esac + +if $cygwin +then + ZOOCFG=`cygpath -wp "$ZOOCFG"` + # cygwin has a "kill" in the shell itself, gets confused + KILL=/bin/kill +else + KILL=kill +fi + +fail() { + # don't run clear_tmp to allow debugging + echo "FAIL $1" + $KILL -9 $(cat "$ZOOPIDFILE") + $KILL -9 $$ +} + +#generate a minimal config +genconfig1() { + cat > test-scripts.cfg <> test-scripts.cfg </dev/null` +[ "$TEST_PRINT_VERSION" != "" ] || fail $LINENO + +#done, cleanup and exit +clear_tmp +echo "SUCCESS"